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;
121 static void f2fs_finish_read_bio(struct bio *bio)
124 struct bvec_iter_all iter_all;
127 * Update and unlock the bio's pagecache pages, and put the
128 * decompression context for any compressed pages.
130 bio_for_each_segment_all(bv, bio, iter_all) {
131 struct page *page = bv->bv_page;
133 if (f2fs_is_compressed_page(page)) {
135 f2fs_end_read_compressed_page(page, true, 0);
136 f2fs_put_page_dic(page);
140 /* PG_error was set if decryption or verity failed. */
141 if (bio->bi_status || PageError(page)) {
142 ClearPageUptodate(page);
143 /* will re-read again later */
144 ClearPageError(page);
146 SetPageUptodate(page);
148 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
153 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
157 static void f2fs_verify_bio(struct work_struct *work)
159 struct bio_post_read_ctx *ctx =
160 container_of(work, struct bio_post_read_ctx, work);
161 struct bio *bio = ctx->bio;
162 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
165 * fsverity_verify_bio() may call readpages() again, and while verity
166 * will be disabled for this, decryption and/or decompression may still
167 * be needed, resulting in another bio_post_read_ctx being allocated.
168 * So to prevent deadlocks we need to release the current ctx to the
169 * mempool first. This assumes that verity is the last post-read step.
171 mempool_free(ctx, bio_post_read_ctx_pool);
172 bio->bi_private = NULL;
175 * Verify the bio's pages with fs-verity. Exclude compressed pages,
176 * as those were handled separately by f2fs_end_read_compressed_page().
178 if (may_have_compressed_pages) {
180 struct bvec_iter_all iter_all;
182 bio_for_each_segment_all(bv, bio, iter_all) {
183 struct page *page = bv->bv_page;
185 if (!f2fs_is_compressed_page(page) &&
186 !PageError(page) && !fsverity_verify_page(page))
190 fsverity_verify_bio(bio);
193 f2fs_finish_read_bio(bio);
197 * If the bio's data needs to be verified with fs-verity, then enqueue the
198 * verity work for the bio. Otherwise finish the bio now.
200 * Note that to avoid deadlocks, the verity work can't be done on the
201 * decryption/decompression workqueue. This is because verifying the data pages
202 * can involve reading verity metadata pages from the file, and these verity
203 * metadata pages may be encrypted and/or compressed.
205 static void f2fs_verify_and_finish_bio(struct bio *bio)
207 struct bio_post_read_ctx *ctx = bio->bi_private;
209 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
210 INIT_WORK(&ctx->work, f2fs_verify_bio);
211 fsverity_enqueue_verify_work(&ctx->work);
213 f2fs_finish_read_bio(bio);
218 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
219 * remaining page was read by @ctx->bio.
221 * Note that a bio may span clusters (even a mix of compressed and uncompressed
222 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
223 * that the bio includes at least one compressed page. The actual decompression
224 * is done on a per-cluster basis, not a per-bio basis.
226 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx)
229 struct bvec_iter_all iter_all;
230 bool all_compressed = true;
231 block_t blkaddr = SECTOR_TO_BLOCK(ctx->bio->bi_iter.bi_sector);
233 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
234 struct page *page = bv->bv_page;
236 /* PG_error was set if decryption failed. */
237 if (f2fs_is_compressed_page(page))
238 f2fs_end_read_compressed_page(page, PageError(page),
241 all_compressed = false;
247 * Optimization: if all the bio's pages are compressed, then scheduling
248 * the per-bio verity work is unnecessary, as verity will be fully
249 * handled at the compression cluster level.
252 ctx->enabled_steps &= ~STEP_VERITY;
255 static void f2fs_post_read_work(struct work_struct *work)
257 struct bio_post_read_ctx *ctx =
258 container_of(work, struct bio_post_read_ctx, work);
260 if (ctx->enabled_steps & STEP_DECRYPT)
261 fscrypt_decrypt_bio(ctx->bio);
263 if (ctx->enabled_steps & STEP_DECOMPRESS)
264 f2fs_handle_step_decompress(ctx);
266 f2fs_verify_and_finish_bio(ctx->bio);
269 static void f2fs_read_end_io(struct bio *bio)
271 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
272 struct bio_post_read_ctx *ctx = bio->bi_private;
274 if (time_to_inject(sbi, FAULT_READ_IO)) {
275 f2fs_show_injection_info(sbi, FAULT_READ_IO);
276 bio->bi_status = BLK_STS_IOERR;
279 if (bio->bi_status) {
280 f2fs_finish_read_bio(bio);
284 if (ctx && (ctx->enabled_steps & (STEP_DECRYPT | STEP_DECOMPRESS))) {
285 INIT_WORK(&ctx->work, f2fs_post_read_work);
286 queue_work(ctx->sbi->post_read_wq, &ctx->work);
288 f2fs_verify_and_finish_bio(bio);
292 static void f2fs_write_end_io(struct bio *bio)
294 struct f2fs_sb_info *sbi = bio->bi_private;
295 struct bio_vec *bvec;
296 struct bvec_iter_all iter_all;
298 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
299 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
300 bio->bi_status = BLK_STS_IOERR;
303 bio_for_each_segment_all(bvec, bio, iter_all) {
304 struct page *page = bvec->bv_page;
305 enum count_type type = WB_DATA_TYPE(page);
307 if (page_private_dummy(page)) {
308 clear_page_private_dummy(page);
310 mempool_free(page, sbi->write_io_dummy);
312 if (unlikely(bio->bi_status))
313 f2fs_stop_checkpoint(sbi, true);
317 fscrypt_finalize_bounce_page(&page);
319 #ifdef CONFIG_F2FS_FS_COMPRESSION
320 if (f2fs_is_compressed_page(page)) {
321 f2fs_compress_write_end_io(bio, page);
326 if (unlikely(bio->bi_status)) {
327 mapping_set_error(page->mapping, -EIO);
328 if (type == F2FS_WB_CP_DATA)
329 f2fs_stop_checkpoint(sbi, true);
332 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
333 page->index != nid_of_node(page));
335 dec_page_count(sbi, type);
336 if (f2fs_in_warm_node_list(sbi, page))
337 f2fs_del_fsync_node_entry(sbi, page);
338 clear_page_private_gcing(page);
339 end_page_writeback(page);
341 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
342 wq_has_sleeper(&sbi->cp_wait))
343 wake_up(&sbi->cp_wait);
348 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
349 block_t blk_addr, struct bio *bio)
351 struct block_device *bdev = sbi->sb->s_bdev;
354 if (f2fs_is_multi_device(sbi)) {
355 for (i = 0; i < sbi->s_ndevs; i++) {
356 if (FDEV(i).start_blk <= blk_addr &&
357 FDEV(i).end_blk >= blk_addr) {
358 blk_addr -= FDEV(i).start_blk;
365 bio_set_dev(bio, bdev);
366 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
371 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
375 if (!f2fs_is_multi_device(sbi))
378 for (i = 0; i < sbi->s_ndevs; i++)
379 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
384 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
386 struct f2fs_sb_info *sbi = fio->sbi;
389 bio = bio_alloc_bioset(GFP_NOIO, npages, &f2fs_bioset);
391 f2fs_target_device(sbi, fio->new_blkaddr, bio);
392 if (is_read_io(fio->op)) {
393 bio->bi_end_io = f2fs_read_end_io;
394 bio->bi_private = NULL;
396 bio->bi_end_io = f2fs_write_end_io;
397 bio->bi_private = sbi;
398 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
399 fio->type, fio->temp);
402 wbc_init_bio(fio->io_wbc, bio);
407 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
409 const struct f2fs_io_info *fio,
413 * The f2fs garbage collector sets ->encrypted_page when it wants to
414 * read/write raw data without encryption.
416 if (!fio || !fio->encrypted_page)
417 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
420 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
422 const struct f2fs_io_info *fio)
425 * The f2fs garbage collector sets ->encrypted_page when it wants to
426 * read/write raw data without encryption.
428 if (fio && fio->encrypted_page)
429 return !bio_has_crypt_ctx(bio);
431 return fscrypt_mergeable_bio(bio, inode, next_idx);
434 static inline void __submit_bio(struct f2fs_sb_info *sbi,
435 struct bio *bio, enum page_type type)
437 if (!is_read_io(bio_op(bio))) {
440 if (type != DATA && type != NODE)
443 if (f2fs_lfs_mode(sbi) && current->plug)
444 blk_finish_plug(current->plug);
446 if (!F2FS_IO_ALIGNED(sbi))
449 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
450 start %= F2FS_IO_SIZE(sbi);
455 /* fill dummy pages */
456 for (; start < F2FS_IO_SIZE(sbi); start++) {
458 mempool_alloc(sbi->write_io_dummy,
459 GFP_NOIO | __GFP_NOFAIL);
460 f2fs_bug_on(sbi, !page);
464 zero_user_segment(page, 0, PAGE_SIZE);
465 set_page_private_dummy(page);
467 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
471 * In the NODE case, we lose next block address chain. So, we
472 * need to do checkpoint in f2fs_sync_file.
475 set_sbi_flag(sbi, SBI_NEED_CP);
478 if (is_read_io(bio_op(bio)))
479 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
481 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
485 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
486 struct bio *bio, enum page_type type)
488 __submit_bio(sbi, bio, type);
491 static void __attach_io_flag(struct f2fs_io_info *fio)
493 struct f2fs_sb_info *sbi = fio->sbi;
494 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
495 unsigned int io_flag, fua_flag, meta_flag;
497 if (fio->type == DATA)
498 io_flag = sbi->data_io_flag;
499 else if (fio->type == NODE)
500 io_flag = sbi->node_io_flag;
504 fua_flag = io_flag & temp_mask;
505 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
508 * data/node io flag bits per temp:
509 * REQ_META | REQ_FUA |
510 * 5 | 4 | 3 | 2 | 1 | 0 |
511 * Cold | Warm | Hot | Cold | Warm | Hot |
513 if ((1 << fio->temp) & meta_flag)
514 fio->op_flags |= REQ_META;
515 if ((1 << fio->temp) & fua_flag)
516 fio->op_flags |= REQ_FUA;
519 static void __submit_merged_bio(struct f2fs_bio_info *io)
521 struct f2fs_io_info *fio = &io->fio;
526 __attach_io_flag(fio);
527 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
529 if (is_read_io(fio->op))
530 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
532 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
534 __submit_bio(io->sbi, io->bio, fio->type);
538 static bool __has_merged_page(struct bio *bio, struct inode *inode,
539 struct page *page, nid_t ino)
541 struct bio_vec *bvec;
542 struct bvec_iter_all iter_all;
547 if (!inode && !page && !ino)
550 bio_for_each_segment_all(bvec, bio, iter_all) {
551 struct page *target = bvec->bv_page;
553 if (fscrypt_is_bounce_page(target)) {
554 target = fscrypt_pagecache_page(target);
558 if (f2fs_is_compressed_page(target)) {
559 target = f2fs_compress_control_page(target);
564 if (inode && inode == target->mapping->host)
566 if (page && page == target)
568 if (ino && ino == ino_of_node(target))
575 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
576 enum page_type type, enum temp_type temp)
578 enum page_type btype = PAGE_TYPE_OF_BIO(type);
579 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
581 down_write(&io->io_rwsem);
583 /* change META to META_FLUSH in the checkpoint procedure */
584 if (type >= META_FLUSH) {
585 io->fio.type = META_FLUSH;
586 io->fio.op = REQ_OP_WRITE;
587 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
588 if (!test_opt(sbi, NOBARRIER))
589 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
591 __submit_merged_bio(io);
592 up_write(&io->io_rwsem);
595 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
596 struct inode *inode, struct page *page,
597 nid_t ino, enum page_type type, bool force)
602 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
604 enum page_type btype = PAGE_TYPE_OF_BIO(type);
605 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
607 down_read(&io->io_rwsem);
608 ret = __has_merged_page(io->bio, inode, page, ino);
609 up_read(&io->io_rwsem);
612 __f2fs_submit_merged_write(sbi, type, temp);
614 /* TODO: use HOT temp only for meta pages now. */
620 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
622 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
625 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
626 struct inode *inode, struct page *page,
627 nid_t ino, enum page_type type)
629 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
632 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
634 f2fs_submit_merged_write(sbi, DATA);
635 f2fs_submit_merged_write(sbi, NODE);
636 f2fs_submit_merged_write(sbi, META);
640 * Fill the locked page with data located in the block address.
641 * A caller needs to unlock the page on failure.
643 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
646 struct page *page = fio->encrypted_page ?
647 fio->encrypted_page : fio->page;
649 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
650 fio->is_por ? META_POR : (__is_meta_io(fio) ?
651 META_GENERIC : DATA_GENERIC_ENHANCE)))
652 return -EFSCORRUPTED;
654 trace_f2fs_submit_page_bio(page, fio);
656 /* Allocate a new bio */
657 bio = __bio_alloc(fio, 1);
659 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
660 fio->page->index, fio, GFP_NOIO);
662 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
667 if (fio->io_wbc && !is_read_io(fio->op))
668 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
670 __attach_io_flag(fio);
671 bio_set_op_attrs(bio, fio->op, fio->op_flags);
673 inc_page_count(fio->sbi, is_read_io(fio->op) ?
674 __read_io_type(page): WB_DATA_TYPE(fio->page));
676 __submit_bio(fio->sbi, bio, fio->type);
680 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
681 block_t last_blkaddr, block_t cur_blkaddr)
683 if (unlikely(sbi->max_io_bytes &&
684 bio->bi_iter.bi_size >= sbi->max_io_bytes))
686 if (last_blkaddr + 1 != cur_blkaddr)
688 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
691 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
692 struct f2fs_io_info *fio)
694 if (io->fio.op != fio->op)
696 return io->fio.op_flags == fio->op_flags;
699 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
700 struct f2fs_bio_info *io,
701 struct f2fs_io_info *fio,
702 block_t last_blkaddr,
705 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
706 unsigned int filled_blocks =
707 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
708 unsigned int io_size = F2FS_IO_SIZE(sbi);
709 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
711 /* IOs in bio is aligned and left space of vectors is not enough */
712 if (!(filled_blocks % io_size) && left_vecs < io_size)
715 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
717 return io_type_is_mergeable(io, fio);
720 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
721 struct page *page, enum temp_type temp)
723 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
724 struct bio_entry *be;
726 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
730 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
733 down_write(&io->bio_list_lock);
734 list_add_tail(&be->list, &io->bio_list);
735 up_write(&io->bio_list_lock);
738 static void del_bio_entry(struct bio_entry *be)
741 kmem_cache_free(bio_entry_slab, be);
744 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
747 struct f2fs_sb_info *sbi = fio->sbi;
752 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
753 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
754 struct list_head *head = &io->bio_list;
755 struct bio_entry *be;
757 down_write(&io->bio_list_lock);
758 list_for_each_entry(be, head, list) {
764 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
767 if (f2fs_crypt_mergeable_bio(*bio,
768 fio->page->mapping->host,
769 fio->page->index, fio) &&
770 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
776 /* page can't be merged into bio; submit the bio */
778 __submit_bio(sbi, *bio, DATA);
781 up_write(&io->bio_list_lock);
792 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
793 struct bio **bio, struct page *page)
797 struct bio *target = bio ? *bio : NULL;
799 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
800 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
801 struct list_head *head = &io->bio_list;
802 struct bio_entry *be;
804 if (list_empty(head))
807 down_read(&io->bio_list_lock);
808 list_for_each_entry(be, head, list) {
810 found = (target == be->bio);
812 found = __has_merged_page(be->bio, NULL,
817 up_read(&io->bio_list_lock);
824 down_write(&io->bio_list_lock);
825 list_for_each_entry(be, head, list) {
827 found = (target == be->bio);
829 found = __has_merged_page(be->bio, NULL,
837 up_write(&io->bio_list_lock);
841 __submit_bio(sbi, target, DATA);
848 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
850 struct bio *bio = *fio->bio;
851 struct page *page = fio->encrypted_page ?
852 fio->encrypted_page : fio->page;
854 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
855 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
856 return -EFSCORRUPTED;
858 trace_f2fs_submit_page_bio(page, fio);
860 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
862 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
865 bio = __bio_alloc(fio, BIO_MAX_VECS);
866 __attach_io_flag(fio);
867 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
868 fio->page->index, fio, GFP_NOIO);
869 bio_set_op_attrs(bio, fio->op, fio->op_flags);
871 add_bio_entry(fio->sbi, bio, page, fio->temp);
873 if (add_ipu_page(fio, &bio, page))
878 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
880 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
882 *fio->last_block = fio->new_blkaddr;
888 void f2fs_submit_page_write(struct f2fs_io_info *fio)
890 struct f2fs_sb_info *sbi = fio->sbi;
891 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
892 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
893 struct page *bio_page;
895 f2fs_bug_on(sbi, is_read_io(fio->op));
897 down_write(&io->io_rwsem);
900 spin_lock(&io->io_lock);
901 if (list_empty(&io->io_list)) {
902 spin_unlock(&io->io_lock);
905 fio = list_first_entry(&io->io_list,
906 struct f2fs_io_info, list);
907 list_del(&fio->list);
908 spin_unlock(&io->io_lock);
911 verify_fio_blkaddr(fio);
913 if (fio->encrypted_page)
914 bio_page = fio->encrypted_page;
915 else if (fio->compressed_page)
916 bio_page = fio->compressed_page;
918 bio_page = fio->page;
920 /* set submitted = true as a return value */
921 fio->submitted = true;
923 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
926 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
928 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
929 bio_page->index, fio)))
930 __submit_merged_bio(io);
932 if (io->bio == NULL) {
933 if (F2FS_IO_ALIGNED(sbi) &&
934 (fio->type == DATA || fio->type == NODE) &&
935 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
936 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
940 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
941 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
942 bio_page->index, fio, GFP_NOIO);
946 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
947 __submit_merged_bio(io);
952 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
954 io->last_block_in_bio = fio->new_blkaddr;
956 trace_f2fs_submit_page_write(fio->page, fio);
961 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
962 !f2fs_is_checkpoint_ready(sbi))
963 __submit_merged_bio(io);
964 up_write(&io->io_rwsem);
967 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
968 unsigned nr_pages, unsigned op_flag,
969 pgoff_t first_idx, bool for_write)
971 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
973 struct bio_post_read_ctx *ctx;
974 unsigned int post_read_steps = 0;
976 bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
977 bio_max_segs(nr_pages), &f2fs_bioset);
979 return ERR_PTR(-ENOMEM);
981 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
983 f2fs_target_device(sbi, blkaddr, bio);
984 bio->bi_end_io = f2fs_read_end_io;
985 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
987 if (fscrypt_inode_uses_fs_layer_crypto(inode))
988 post_read_steps |= STEP_DECRYPT;
990 if (f2fs_need_verity(inode, first_idx))
991 post_read_steps |= STEP_VERITY;
994 * STEP_DECOMPRESS is handled specially, since a compressed file might
995 * contain both compressed and uncompressed clusters. We'll allocate a
996 * bio_post_read_ctx if the file is compressed, but the caller is
997 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1000 if (post_read_steps || f2fs_compressed_file(inode)) {
1001 /* Due to the mempool, this never fails. */
1002 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1005 ctx->enabled_steps = post_read_steps;
1006 bio->bi_private = ctx;
1012 /* This can handle encryption stuffs */
1013 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1014 block_t blkaddr, int op_flags, bool for_write)
1016 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1019 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1020 page->index, for_write);
1022 return PTR_ERR(bio);
1024 /* wait for GCed page writeback via META_MAPPING */
1025 f2fs_wait_on_block_writeback(inode, blkaddr);
1027 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1031 ClearPageError(page);
1032 inc_page_count(sbi, F2FS_RD_DATA);
1033 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1034 __submit_bio(sbi, bio, DATA);
1038 static void __set_data_blkaddr(struct dnode_of_data *dn)
1040 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1044 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1045 base = get_extra_isize(dn->inode);
1047 /* Get physical address of data block */
1048 addr_array = blkaddr_in_node(rn);
1049 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1053 * Lock ordering for the change of data block address:
1056 * update block addresses in the node page
1058 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1060 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1061 __set_data_blkaddr(dn);
1062 if (set_page_dirty(dn->node_page))
1063 dn->node_changed = true;
1066 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1068 dn->data_blkaddr = blkaddr;
1069 f2fs_set_data_blkaddr(dn);
1070 f2fs_update_extent_cache(dn);
1073 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1074 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1076 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1082 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1084 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1087 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1088 dn->ofs_in_node, count);
1090 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1092 for (; count > 0; dn->ofs_in_node++) {
1093 block_t blkaddr = f2fs_data_blkaddr(dn);
1095 if (blkaddr == NULL_ADDR) {
1096 dn->data_blkaddr = NEW_ADDR;
1097 __set_data_blkaddr(dn);
1102 if (set_page_dirty(dn->node_page))
1103 dn->node_changed = true;
1107 /* Should keep dn->ofs_in_node unchanged */
1108 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1110 unsigned int ofs_in_node = dn->ofs_in_node;
1113 ret = f2fs_reserve_new_blocks(dn, 1);
1114 dn->ofs_in_node = ofs_in_node;
1118 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1120 bool need_put = dn->inode_page ? false : true;
1123 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1127 if (dn->data_blkaddr == NULL_ADDR)
1128 err = f2fs_reserve_new_block(dn);
1129 if (err || need_put)
1134 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1136 struct extent_info ei = {0, 0, 0};
1137 struct inode *inode = dn->inode;
1139 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1140 dn->data_blkaddr = ei.blk + index - ei.fofs;
1144 return f2fs_reserve_block(dn, index);
1147 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1148 int op_flags, bool for_write)
1150 struct address_space *mapping = inode->i_mapping;
1151 struct dnode_of_data dn;
1153 struct extent_info ei = {0,0,0};
1156 page = f2fs_grab_cache_page(mapping, index, for_write);
1158 return ERR_PTR(-ENOMEM);
1160 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1161 dn.data_blkaddr = ei.blk + index - ei.fofs;
1162 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1163 DATA_GENERIC_ENHANCE_READ)) {
1164 err = -EFSCORRUPTED;
1170 set_new_dnode(&dn, inode, NULL, NULL, 0);
1171 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1174 f2fs_put_dnode(&dn);
1176 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1180 if (dn.data_blkaddr != NEW_ADDR &&
1181 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1183 DATA_GENERIC_ENHANCE)) {
1184 err = -EFSCORRUPTED;
1188 if (PageUptodate(page)) {
1194 * A new dentry page is allocated but not able to be written, since its
1195 * new inode page couldn't be allocated due to -ENOSPC.
1196 * In such the case, its blkaddr can be remained as NEW_ADDR.
1197 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1198 * f2fs_init_inode_metadata.
1200 if (dn.data_blkaddr == NEW_ADDR) {
1201 zero_user_segment(page, 0, PAGE_SIZE);
1202 if (!PageUptodate(page))
1203 SetPageUptodate(page);
1208 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1209 op_flags, for_write);
1215 f2fs_put_page(page, 1);
1216 return ERR_PTR(err);
1219 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1221 struct address_space *mapping = inode->i_mapping;
1224 page = find_get_page(mapping, index);
1225 if (page && PageUptodate(page))
1227 f2fs_put_page(page, 0);
1229 page = f2fs_get_read_data_page(inode, index, 0, false);
1233 if (PageUptodate(page))
1236 wait_on_page_locked(page);
1237 if (unlikely(!PageUptodate(page))) {
1238 f2fs_put_page(page, 0);
1239 return ERR_PTR(-EIO);
1245 * If it tries to access a hole, return an error.
1246 * Because, the callers, functions in dir.c and GC, should be able to know
1247 * whether this page exists or not.
1249 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1252 struct address_space *mapping = inode->i_mapping;
1255 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1259 /* wait for read completion */
1261 if (unlikely(page->mapping != mapping)) {
1262 f2fs_put_page(page, 1);
1265 if (unlikely(!PageUptodate(page))) {
1266 f2fs_put_page(page, 1);
1267 return ERR_PTR(-EIO);
1273 * Caller ensures that this data page is never allocated.
1274 * A new zero-filled data page is allocated in the page cache.
1276 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1278 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1279 * ipage should be released by this function.
1281 struct page *f2fs_get_new_data_page(struct inode *inode,
1282 struct page *ipage, pgoff_t index, bool new_i_size)
1284 struct address_space *mapping = inode->i_mapping;
1286 struct dnode_of_data dn;
1289 page = f2fs_grab_cache_page(mapping, index, true);
1292 * before exiting, we should make sure ipage will be released
1293 * if any error occur.
1295 f2fs_put_page(ipage, 1);
1296 return ERR_PTR(-ENOMEM);
1299 set_new_dnode(&dn, inode, ipage, NULL, 0);
1300 err = f2fs_reserve_block(&dn, index);
1302 f2fs_put_page(page, 1);
1303 return ERR_PTR(err);
1306 f2fs_put_dnode(&dn);
1308 if (PageUptodate(page))
1311 if (dn.data_blkaddr == NEW_ADDR) {
1312 zero_user_segment(page, 0, PAGE_SIZE);
1313 if (!PageUptodate(page))
1314 SetPageUptodate(page);
1316 f2fs_put_page(page, 1);
1318 /* if ipage exists, blkaddr should be NEW_ADDR */
1319 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1320 page = f2fs_get_lock_data_page(inode, index, true);
1325 if (new_i_size && i_size_read(inode) <
1326 ((loff_t)(index + 1) << PAGE_SHIFT))
1327 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1331 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1333 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1334 struct f2fs_summary sum;
1335 struct node_info ni;
1336 block_t old_blkaddr;
1340 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1343 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1347 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1348 if (dn->data_blkaddr != NULL_ADDR)
1351 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1355 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1356 old_blkaddr = dn->data_blkaddr;
1357 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1358 &sum, seg_type, NULL);
1359 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
1360 invalidate_mapping_pages(META_MAPPING(sbi),
1361 old_blkaddr, old_blkaddr);
1362 f2fs_invalidate_compress_page(sbi, old_blkaddr);
1364 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1367 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1368 * data from unwritten block via dio_read.
1373 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1375 struct inode *inode = file_inode(iocb->ki_filp);
1376 struct f2fs_map_blocks map;
1379 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1381 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1382 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1383 if (map.m_len > map.m_lblk)
1384 map.m_len -= map.m_lblk;
1388 map.m_next_pgofs = NULL;
1389 map.m_next_extent = NULL;
1390 map.m_seg_type = NO_CHECK_TYPE;
1391 map.m_may_create = true;
1394 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1395 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1396 F2FS_GET_BLOCK_PRE_AIO :
1397 F2FS_GET_BLOCK_PRE_DIO;
1400 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1401 err = f2fs_convert_inline_inode(inode);
1405 if (f2fs_has_inline_data(inode))
1408 flag = F2FS_GET_BLOCK_PRE_AIO;
1411 err = f2fs_map_blocks(inode, &map, 1, flag);
1412 if (map.m_len > 0 && err == -ENOSPC) {
1414 set_inode_flag(inode, FI_NO_PREALLOC);
1420 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1422 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1424 down_read(&sbi->node_change);
1426 up_read(&sbi->node_change);
1431 f2fs_unlock_op(sbi);
1436 * f2fs_map_blocks() tries to find or build mapping relationship which
1437 * maps continuous logical blocks to physical blocks, and return such
1438 * info via f2fs_map_blocks structure.
1440 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1441 int create, int flag)
1443 unsigned int maxblocks = map->m_len;
1444 struct dnode_of_data dn;
1445 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1446 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1447 pgoff_t pgofs, end_offset, end;
1448 int err = 0, ofs = 1;
1449 unsigned int ofs_in_node, last_ofs_in_node;
1451 struct extent_info ei = {0,0,0};
1453 unsigned int start_pgofs;
1461 /* it only supports block size == page size */
1462 pgofs = (pgoff_t)map->m_lblk;
1463 end = pgofs + maxblocks;
1465 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1466 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1470 map->m_pblk = ei.blk + pgofs - ei.fofs;
1471 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1472 map->m_flags = F2FS_MAP_MAPPED;
1473 if (map->m_next_extent)
1474 *map->m_next_extent = pgofs + map->m_len;
1476 /* for hardware encryption, but to avoid potential issue in future */
1477 if (flag == F2FS_GET_BLOCK_DIO)
1478 f2fs_wait_on_block_writeback_range(inode,
1479 map->m_pblk, map->m_len);
1484 if (map->m_may_create)
1485 f2fs_do_map_lock(sbi, flag, true);
1487 /* When reading holes, we need its node page */
1488 set_new_dnode(&dn, inode, NULL, NULL, 0);
1489 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1491 if (flag == F2FS_GET_BLOCK_BMAP)
1493 if (err == -ENOENT) {
1495 if (map->m_next_pgofs)
1496 *map->m_next_pgofs =
1497 f2fs_get_next_page_offset(&dn, pgofs);
1498 if (map->m_next_extent)
1499 *map->m_next_extent =
1500 f2fs_get_next_page_offset(&dn, pgofs);
1505 start_pgofs = pgofs;
1507 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1508 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1511 blkaddr = f2fs_data_blkaddr(&dn);
1513 if (__is_valid_data_blkaddr(blkaddr) &&
1514 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1515 err = -EFSCORRUPTED;
1519 if (__is_valid_data_blkaddr(blkaddr)) {
1520 /* use out-place-update for driect IO under LFS mode */
1521 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1522 map->m_may_create) {
1523 err = __allocate_data_block(&dn, map->m_seg_type);
1526 blkaddr = dn.data_blkaddr;
1527 set_inode_flag(inode, FI_APPEND_WRITE);
1531 if (unlikely(f2fs_cp_error(sbi))) {
1535 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1536 if (blkaddr == NULL_ADDR) {
1538 last_ofs_in_node = dn.ofs_in_node;
1541 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1542 flag != F2FS_GET_BLOCK_DIO);
1543 err = __allocate_data_block(&dn,
1546 set_inode_flag(inode, FI_APPEND_WRITE);
1550 map->m_flags |= F2FS_MAP_NEW;
1551 blkaddr = dn.data_blkaddr;
1553 if (flag == F2FS_GET_BLOCK_BMAP) {
1557 if (flag == F2FS_GET_BLOCK_PRECACHE)
1559 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1560 blkaddr == NULL_ADDR) {
1561 if (map->m_next_pgofs)
1562 *map->m_next_pgofs = pgofs + 1;
1565 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1566 /* for defragment case */
1567 if (map->m_next_pgofs)
1568 *map->m_next_pgofs = pgofs + 1;
1574 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1577 if (map->m_len == 0) {
1578 /* preallocated unwritten block should be mapped for fiemap. */
1579 if (blkaddr == NEW_ADDR)
1580 map->m_flags |= F2FS_MAP_UNWRITTEN;
1581 map->m_flags |= F2FS_MAP_MAPPED;
1583 map->m_pblk = blkaddr;
1585 } else if ((map->m_pblk != NEW_ADDR &&
1586 blkaddr == (map->m_pblk + ofs)) ||
1587 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1588 flag == F2FS_GET_BLOCK_PRE_DIO) {
1599 /* preallocate blocks in batch for one dnode page */
1600 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1601 (pgofs == end || dn.ofs_in_node == end_offset)) {
1603 dn.ofs_in_node = ofs_in_node;
1604 err = f2fs_reserve_new_blocks(&dn, prealloc);
1608 map->m_len += dn.ofs_in_node - ofs_in_node;
1609 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1613 dn.ofs_in_node = end_offset;
1618 else if (dn.ofs_in_node < end_offset)
1621 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1622 if (map->m_flags & F2FS_MAP_MAPPED) {
1623 unsigned int ofs = start_pgofs - map->m_lblk;
1625 f2fs_update_extent_cache_range(&dn,
1626 start_pgofs, map->m_pblk + ofs,
1631 f2fs_put_dnode(&dn);
1633 if (map->m_may_create) {
1634 f2fs_do_map_lock(sbi, flag, false);
1635 f2fs_balance_fs(sbi, dn.node_changed);
1641 /* for hardware encryption, but to avoid potential issue in future */
1642 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1643 f2fs_wait_on_block_writeback_range(inode,
1644 map->m_pblk, map->m_len);
1646 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1647 if (map->m_flags & F2FS_MAP_MAPPED) {
1648 unsigned int ofs = start_pgofs - map->m_lblk;
1650 f2fs_update_extent_cache_range(&dn,
1651 start_pgofs, map->m_pblk + ofs,
1654 if (map->m_next_extent)
1655 *map->m_next_extent = pgofs + 1;
1657 f2fs_put_dnode(&dn);
1659 if (map->m_may_create) {
1660 f2fs_do_map_lock(sbi, flag, false);
1661 f2fs_balance_fs(sbi, dn.node_changed);
1664 trace_f2fs_map_blocks(inode, map, err);
1668 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1670 struct f2fs_map_blocks map;
1674 if (pos + len > i_size_read(inode))
1677 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1678 map.m_next_pgofs = NULL;
1679 map.m_next_extent = NULL;
1680 map.m_seg_type = NO_CHECK_TYPE;
1681 map.m_may_create = false;
1682 last_lblk = F2FS_BLK_ALIGN(pos + len);
1684 while (map.m_lblk < last_lblk) {
1685 map.m_len = last_lblk - map.m_lblk;
1686 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1687 if (err || map.m_len == 0)
1689 map.m_lblk += map.m_len;
1694 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1696 return (bytes >> inode->i_blkbits);
1699 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1701 return (blks << inode->i_blkbits);
1704 static int __get_data_block(struct inode *inode, sector_t iblock,
1705 struct buffer_head *bh, int create, int flag,
1706 pgoff_t *next_pgofs, int seg_type, bool may_write)
1708 struct f2fs_map_blocks map;
1711 map.m_lblk = iblock;
1712 map.m_len = bytes_to_blks(inode, bh->b_size);
1713 map.m_next_pgofs = next_pgofs;
1714 map.m_next_extent = NULL;
1715 map.m_seg_type = seg_type;
1716 map.m_may_create = may_write;
1718 err = f2fs_map_blocks(inode, &map, create, flag);
1720 map_bh(bh, inode->i_sb, map.m_pblk);
1721 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1722 bh->b_size = blks_to_bytes(inode, map.m_len);
1727 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1728 struct buffer_head *bh_result, int create)
1730 return __get_data_block(inode, iblock, bh_result, create,
1731 F2FS_GET_BLOCK_DIO, NULL,
1732 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1736 static int get_data_block_dio(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 f2fs_xattr_fiemap(struct inode *inode,
1746 struct fiemap_extent_info *fieinfo)
1748 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1750 struct node_info ni;
1751 __u64 phys = 0, len;
1753 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1756 if (f2fs_has_inline_xattr(inode)) {
1759 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1760 inode->i_ino, false);
1764 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1766 f2fs_put_page(page, 1);
1770 phys = blks_to_bytes(inode, ni.blk_addr);
1771 offset = offsetof(struct f2fs_inode, i_addr) +
1772 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1773 get_inline_xattr_addrs(inode));
1776 len = inline_xattr_size(inode);
1778 f2fs_put_page(page, 1);
1780 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1783 flags |= FIEMAP_EXTENT_LAST;
1785 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1786 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1787 if (err || err == 1)
1792 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1796 err = f2fs_get_node_info(sbi, xnid, &ni);
1798 f2fs_put_page(page, 1);
1802 phys = blks_to_bytes(inode, ni.blk_addr);
1803 len = inode->i_sb->s_blocksize;
1805 f2fs_put_page(page, 1);
1807 flags = FIEMAP_EXTENT_LAST;
1811 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1812 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1815 return (err < 0 ? err : 0);
1818 static loff_t max_inode_blocks(struct inode *inode)
1820 loff_t result = ADDRS_PER_INODE(inode);
1821 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1823 /* two direct node blocks */
1824 result += (leaf_count * 2);
1826 /* two indirect node blocks */
1827 leaf_count *= NIDS_PER_BLOCK;
1828 result += (leaf_count * 2);
1830 /* one double indirect node block */
1831 leaf_count *= NIDS_PER_BLOCK;
1832 result += leaf_count;
1837 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1840 struct f2fs_map_blocks map;
1841 sector_t start_blk, last_blk;
1843 u64 logical = 0, phys = 0, size = 0;
1846 bool compr_cluster = false;
1847 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1850 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1851 ret = f2fs_precache_extents(inode);
1856 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1862 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1863 if (start > maxbytes) {
1868 if (len > maxbytes || (maxbytes - len) < start)
1869 len = maxbytes - start;
1871 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1872 ret = f2fs_xattr_fiemap(inode, fieinfo);
1876 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1877 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1882 if (bytes_to_blks(inode, len) == 0)
1883 len = blks_to_bytes(inode, 1);
1885 start_blk = bytes_to_blks(inode, start);
1886 last_blk = bytes_to_blks(inode, start + len - 1);
1889 memset(&map, 0, sizeof(map));
1890 map.m_lblk = start_blk;
1891 map.m_len = bytes_to_blks(inode, len);
1892 map.m_next_pgofs = &next_pgofs;
1893 map.m_seg_type = NO_CHECK_TYPE;
1896 map.m_len = cluster_size - 1;
1898 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1903 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1904 start_blk = next_pgofs;
1906 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1907 max_inode_blocks(inode)))
1910 flags |= FIEMAP_EXTENT_LAST;
1914 flags |= FIEMAP_EXTENT_MERGED;
1915 if (IS_ENCRYPTED(inode))
1916 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1918 ret = fiemap_fill_next_extent(fieinfo, logical,
1920 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1926 if (start_blk > last_blk)
1929 if (compr_cluster) {
1930 compr_cluster = false;
1933 logical = blks_to_bytes(inode, start_blk - 1);
1934 phys = blks_to_bytes(inode, map.m_pblk);
1935 size = blks_to_bytes(inode, cluster_size);
1937 flags |= FIEMAP_EXTENT_ENCODED;
1939 start_blk += cluster_size - 1;
1941 if (start_blk > last_blk)
1947 if (map.m_pblk == COMPRESS_ADDR) {
1948 compr_cluster = true;
1953 logical = blks_to_bytes(inode, start_blk);
1954 phys = blks_to_bytes(inode, map.m_pblk);
1955 size = blks_to_bytes(inode, map.m_len);
1957 if (map.m_flags & F2FS_MAP_UNWRITTEN)
1958 flags = FIEMAP_EXTENT_UNWRITTEN;
1960 start_blk += bytes_to_blks(inode, size);
1964 if (fatal_signal_pending(current))
1972 inode_unlock(inode);
1976 static inline loff_t f2fs_readpage_limit(struct inode *inode)
1978 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1979 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1980 return inode->i_sb->s_maxbytes;
1982 return i_size_read(inode);
1985 static int f2fs_read_single_page(struct inode *inode, struct page *page,
1987 struct f2fs_map_blocks *map,
1988 struct bio **bio_ret,
1989 sector_t *last_block_in_bio,
1992 struct bio *bio = *bio_ret;
1993 const unsigned blocksize = blks_to_bytes(inode, 1);
1994 sector_t block_in_file;
1995 sector_t last_block;
1996 sector_t last_block_in_file;
2000 block_in_file = (sector_t)page_index(page);
2001 last_block = block_in_file + nr_pages;
2002 last_block_in_file = bytes_to_blks(inode,
2003 f2fs_readpage_limit(inode) + blocksize - 1);
2004 if (last_block > last_block_in_file)
2005 last_block = last_block_in_file;
2007 /* just zeroing out page which is beyond EOF */
2008 if (block_in_file >= last_block)
2011 * Map blocks using the previous result first.
2013 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2014 block_in_file > map->m_lblk &&
2015 block_in_file < (map->m_lblk + map->m_len))
2019 * Then do more f2fs_map_blocks() calls until we are
2020 * done with this page.
2022 map->m_lblk = block_in_file;
2023 map->m_len = last_block - block_in_file;
2025 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2029 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2030 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2031 SetPageMappedToDisk(page);
2033 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2034 !cleancache_get_page(page))) {
2035 SetPageUptodate(page);
2039 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2040 DATA_GENERIC_ENHANCE_READ)) {
2041 ret = -EFSCORRUPTED;
2046 zero_user_segment(page, 0, PAGE_SIZE);
2047 if (f2fs_need_verity(inode, page->index) &&
2048 !fsverity_verify_page(page)) {
2052 if (!PageUptodate(page))
2053 SetPageUptodate(page);
2059 * This page will go to BIO. Do we need to send this
2062 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2063 *last_block_in_bio, block_nr) ||
2064 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2066 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2070 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2071 is_readahead ? REQ_RAHEAD : 0, page->index,
2081 * If the page is under writeback, we need to wait for
2082 * its completion to see the correct decrypted data.
2084 f2fs_wait_on_block_writeback(inode, block_nr);
2086 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2087 goto submit_and_realloc;
2089 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2090 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2091 ClearPageError(page);
2092 *last_block_in_bio = block_nr;
2096 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2105 #ifdef CONFIG_F2FS_FS_COMPRESSION
2106 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2107 unsigned nr_pages, sector_t *last_block_in_bio,
2108 bool is_readahead, bool for_write)
2110 struct dnode_of_data dn;
2111 struct inode *inode = cc->inode;
2112 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2113 struct bio *bio = *bio_ret;
2114 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2115 sector_t last_block_in_file;
2116 const unsigned blocksize = blks_to_bytes(inode, 1);
2117 struct decompress_io_ctx *dic = NULL;
2121 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2123 last_block_in_file = bytes_to_blks(inode,
2124 f2fs_readpage_limit(inode) + blocksize - 1);
2126 /* get rid of pages beyond EOF */
2127 for (i = 0; i < cc->cluster_size; i++) {
2128 struct page *page = cc->rpages[i];
2132 if ((sector_t)page->index >= last_block_in_file) {
2133 zero_user_segment(page, 0, PAGE_SIZE);
2134 if (!PageUptodate(page))
2135 SetPageUptodate(page);
2136 } else if (!PageUptodate(page)) {
2140 cc->rpages[i] = NULL;
2144 /* we are done since all pages are beyond EOF */
2145 if (f2fs_cluster_is_empty(cc))
2148 set_new_dnode(&dn, inode, NULL, NULL, 0);
2149 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2153 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2155 for (i = 1; i < cc->cluster_size; i++) {
2158 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2159 dn.ofs_in_node + i);
2161 if (!__is_valid_data_blkaddr(blkaddr))
2164 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2171 /* nothing to decompress */
2172 if (cc->nr_cpages == 0) {
2177 dic = f2fs_alloc_dic(cc);
2183 for (i = 0; i < cc->nr_cpages; i++) {
2184 struct page *page = dic->cpages[i];
2186 struct bio_post_read_ctx *ctx;
2188 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2189 dn.ofs_in_node + i + 1);
2191 f2fs_wait_on_block_writeback(inode, blkaddr);
2193 if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2194 if (atomic_dec_and_test(&dic->remaining_pages))
2195 f2fs_decompress_cluster(dic);
2199 if (bio && (!page_is_mergeable(sbi, bio,
2200 *last_block_in_bio, blkaddr) ||
2201 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2203 __submit_bio(sbi, bio, DATA);
2208 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2209 is_readahead ? REQ_RAHEAD : 0,
2210 page->index, for_write);
2213 f2fs_decompress_end_io(dic, ret);
2214 f2fs_put_dnode(&dn);
2220 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2221 goto submit_and_realloc;
2223 ctx = bio->bi_private;
2224 ctx->enabled_steps |= STEP_DECOMPRESS;
2225 refcount_inc(&dic->refcnt);
2227 inc_page_count(sbi, F2FS_RD_DATA);
2228 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2229 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2230 ClearPageError(page);
2231 *last_block_in_bio = blkaddr;
2234 f2fs_put_dnode(&dn);
2240 f2fs_put_dnode(&dn);
2242 for (i = 0; i < cc->cluster_size; i++) {
2243 if (cc->rpages[i]) {
2244 ClearPageUptodate(cc->rpages[i]);
2245 ClearPageError(cc->rpages[i]);
2246 unlock_page(cc->rpages[i]);
2255 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2256 * Major change was from block_size == page_size in f2fs by default.
2258 static int f2fs_mpage_readpages(struct inode *inode,
2259 struct readahead_control *rac, struct page *page)
2261 struct bio *bio = NULL;
2262 sector_t last_block_in_bio = 0;
2263 struct f2fs_map_blocks map;
2264 #ifdef CONFIG_F2FS_FS_COMPRESSION
2265 struct compress_ctx cc = {
2267 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2268 .cluster_size = F2FS_I(inode)->i_cluster_size,
2269 .cluster_idx = NULL_CLUSTER,
2276 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2277 unsigned max_nr_pages = nr_pages;
2284 map.m_next_pgofs = NULL;
2285 map.m_next_extent = NULL;
2286 map.m_seg_type = NO_CHECK_TYPE;
2287 map.m_may_create = false;
2289 for (; nr_pages; nr_pages--) {
2291 page = readahead_page(rac);
2292 prefetchw(&page->flags);
2295 #ifdef CONFIG_F2FS_FS_COMPRESSION
2296 if (f2fs_compressed_file(inode)) {
2297 /* there are remained comressed pages, submit them */
2298 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2299 ret = f2fs_read_multi_pages(&cc, &bio,
2302 rac != NULL, false);
2303 f2fs_destroy_compress_ctx(&cc, false);
2305 goto set_error_page;
2307 ret = f2fs_is_compressed_cluster(inode, page->index);
2309 goto set_error_page;
2311 goto read_single_page;
2313 ret = f2fs_init_compress_ctx(&cc);
2315 goto set_error_page;
2317 f2fs_compress_ctx_add_page(&cc, page);
2324 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2325 &bio, &last_block_in_bio, rac);
2327 #ifdef CONFIG_F2FS_FS_COMPRESSION
2331 zero_user_segment(page, 0, PAGE_SIZE);
2334 #ifdef CONFIG_F2FS_FS_COMPRESSION
2340 #ifdef CONFIG_F2FS_FS_COMPRESSION
2341 if (f2fs_compressed_file(inode)) {
2343 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2344 ret = f2fs_read_multi_pages(&cc, &bio,
2347 rac != NULL, false);
2348 f2fs_destroy_compress_ctx(&cc, false);
2354 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2358 static int f2fs_read_data_page(struct file *file, struct page *page)
2360 struct inode *inode = page_file_mapping(page)->host;
2363 trace_f2fs_readpage(page, DATA);
2365 if (!f2fs_is_compress_backend_ready(inode)) {
2370 /* If the file has inline data, try to read it directly */
2371 if (f2fs_has_inline_data(inode))
2372 ret = f2fs_read_inline_data(inode, page);
2374 ret = f2fs_mpage_readpages(inode, NULL, page);
2378 static void f2fs_readahead(struct readahead_control *rac)
2380 struct inode *inode = rac->mapping->host;
2382 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2384 if (!f2fs_is_compress_backend_ready(inode))
2387 /* If the file has inline data, skip readpages */
2388 if (f2fs_has_inline_data(inode))
2391 f2fs_mpage_readpages(inode, rac, NULL);
2394 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2396 struct inode *inode = fio->page->mapping->host;
2397 struct page *mpage, *page;
2398 gfp_t gfp_flags = GFP_NOFS;
2400 if (!f2fs_encrypted_file(inode))
2403 page = fio->compressed_page ? fio->compressed_page : fio->page;
2405 /* wait for GCed page writeback via META_MAPPING */
2406 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2408 if (fscrypt_inode_uses_inline_crypto(inode))
2412 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2413 PAGE_SIZE, 0, gfp_flags);
2414 if (IS_ERR(fio->encrypted_page)) {
2415 /* flush pending IOs and wait for a while in the ENOMEM case */
2416 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2417 f2fs_flush_merged_writes(fio->sbi);
2418 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2419 gfp_flags |= __GFP_NOFAIL;
2422 return PTR_ERR(fio->encrypted_page);
2425 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2427 if (PageUptodate(mpage))
2428 memcpy(page_address(mpage),
2429 page_address(fio->encrypted_page), PAGE_SIZE);
2430 f2fs_put_page(mpage, 1);
2435 static inline bool check_inplace_update_policy(struct inode *inode,
2436 struct f2fs_io_info *fio)
2438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2439 unsigned int policy = SM_I(sbi)->ipu_policy;
2441 if (policy & (0x1 << F2FS_IPU_FORCE))
2443 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2445 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2446 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2448 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2449 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2453 * IPU for rewrite async pages
2455 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2456 fio && fio->op == REQ_OP_WRITE &&
2457 !(fio->op_flags & REQ_SYNC) &&
2458 !IS_ENCRYPTED(inode))
2461 /* this is only set during fdatasync */
2462 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2463 is_inode_flag_set(inode, FI_NEED_IPU))
2466 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2467 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2473 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2475 /* swap file is migrating in aligned write mode */
2476 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2479 if (f2fs_is_pinned_file(inode))
2482 /* if this is cold file, we should overwrite to avoid fragmentation */
2483 if (file_is_cold(inode))
2486 return check_inplace_update_policy(inode, fio);
2489 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2491 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2493 if (f2fs_lfs_mode(sbi))
2495 if (S_ISDIR(inode->i_mode))
2497 if (IS_NOQUOTA(inode))
2499 if (f2fs_is_atomic_file(inode))
2502 /* swap file is migrating in aligned write mode */
2503 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2507 if (page_private_gcing(fio->page))
2509 if (page_private_dummy(fio->page))
2511 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2512 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2518 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2520 struct inode *inode = fio->page->mapping->host;
2522 if (f2fs_should_update_outplace(inode, fio))
2525 return f2fs_should_update_inplace(inode, fio);
2528 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2530 struct page *page = fio->page;
2531 struct inode *inode = page->mapping->host;
2532 struct dnode_of_data dn;
2533 struct extent_info ei = {0,0,0};
2534 struct node_info ni;
2535 bool ipu_force = false;
2538 set_new_dnode(&dn, inode, NULL, NULL, 0);
2539 if (need_inplace_update(fio) &&
2540 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2541 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2543 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2544 DATA_GENERIC_ENHANCE))
2545 return -EFSCORRUPTED;
2548 fio->need_lock = LOCK_DONE;
2552 /* Deadlock due to between page->lock and f2fs_lock_op */
2553 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2556 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2560 fio->old_blkaddr = dn.data_blkaddr;
2562 /* This page is already truncated */
2563 if (fio->old_blkaddr == NULL_ADDR) {
2564 ClearPageUptodate(page);
2565 clear_page_private_gcing(page);
2569 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2570 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2571 DATA_GENERIC_ENHANCE)) {
2572 err = -EFSCORRUPTED;
2576 * If current allocation needs SSR,
2577 * it had better in-place writes for updated data.
2580 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2581 need_inplace_update(fio))) {
2582 err = f2fs_encrypt_one_page(fio);
2586 set_page_writeback(page);
2587 ClearPageError(page);
2588 f2fs_put_dnode(&dn);
2589 if (fio->need_lock == LOCK_REQ)
2590 f2fs_unlock_op(fio->sbi);
2591 err = f2fs_inplace_write_data(fio);
2593 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2594 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2595 if (PageWriteback(page))
2596 end_page_writeback(page);
2598 set_inode_flag(inode, FI_UPDATE_WRITE);
2600 trace_f2fs_do_write_data_page(fio->page, IPU);
2604 if (fio->need_lock == LOCK_RETRY) {
2605 if (!f2fs_trylock_op(fio->sbi)) {
2609 fio->need_lock = LOCK_REQ;
2612 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2616 fio->version = ni.version;
2618 err = f2fs_encrypt_one_page(fio);
2622 set_page_writeback(page);
2623 ClearPageError(page);
2625 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2626 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2628 /* LFS mode write path */
2629 f2fs_outplace_write_data(&dn, fio);
2630 trace_f2fs_do_write_data_page(page, OPU);
2631 set_inode_flag(inode, FI_APPEND_WRITE);
2632 if (page->index == 0)
2633 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2635 f2fs_put_dnode(&dn);
2637 if (fio->need_lock == LOCK_REQ)
2638 f2fs_unlock_op(fio->sbi);
2642 int f2fs_write_single_data_page(struct page *page, int *submitted,
2644 sector_t *last_block,
2645 struct writeback_control *wbc,
2646 enum iostat_type io_type,
2650 struct inode *inode = page->mapping->host;
2651 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2652 loff_t i_size = i_size_read(inode);
2653 const pgoff_t end_index = ((unsigned long long)i_size)
2655 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2656 unsigned offset = 0;
2657 bool need_balance_fs = false;
2659 struct f2fs_io_info fio = {
2661 .ino = inode->i_ino,
2664 .op_flags = wbc_to_write_flags(wbc),
2665 .old_blkaddr = NULL_ADDR,
2667 .encrypted_page = NULL,
2669 .compr_blocks = compr_blocks,
2670 .need_lock = LOCK_RETRY,
2674 .last_block = last_block,
2677 trace_f2fs_writepage(page, DATA);
2679 /* we should bypass data pages to proceed the kworkder jobs */
2680 if (unlikely(f2fs_cp_error(sbi))) {
2681 mapping_set_error(page->mapping, -EIO);
2683 * don't drop any dirty dentry pages for keeping lastest
2684 * directory structure.
2686 if (S_ISDIR(inode->i_mode))
2691 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2694 if (page->index < end_index ||
2695 f2fs_verity_in_progress(inode) ||
2700 * If the offset is out-of-range of file size,
2701 * this page does not have to be written to disk.
2703 offset = i_size & (PAGE_SIZE - 1);
2704 if ((page->index >= end_index + 1) || !offset)
2707 zero_user_segment(page, offset, PAGE_SIZE);
2709 if (f2fs_is_drop_cache(inode))
2711 /* we should not write 0'th page having journal header */
2712 if (f2fs_is_volatile_file(inode) && (!page->index ||
2713 (!wbc->for_reclaim &&
2714 f2fs_available_free_memory(sbi, BASE_CHECK))))
2717 /* Dentry/quota blocks are controlled by checkpoint */
2718 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2720 * We need to wait for node_write to avoid block allocation during
2721 * checkpoint. This can only happen to quota writes which can cause
2722 * the below discard race condition.
2724 if (IS_NOQUOTA(inode))
2725 down_read(&sbi->node_write);
2727 fio.need_lock = LOCK_DONE;
2728 err = f2fs_do_write_data_page(&fio);
2730 if (IS_NOQUOTA(inode))
2731 up_read(&sbi->node_write);
2736 if (!wbc->for_reclaim)
2737 need_balance_fs = true;
2738 else if (has_not_enough_free_secs(sbi, 0, 0))
2741 set_inode_flag(inode, FI_HOT_DATA);
2744 if (f2fs_has_inline_data(inode)) {
2745 err = f2fs_write_inline_data(inode, page);
2750 if (err == -EAGAIN) {
2751 err = f2fs_do_write_data_page(&fio);
2752 if (err == -EAGAIN) {
2753 fio.need_lock = LOCK_REQ;
2754 err = f2fs_do_write_data_page(&fio);
2759 file_set_keep_isize(inode);
2761 spin_lock(&F2FS_I(inode)->i_size_lock);
2762 if (F2FS_I(inode)->last_disk_size < psize)
2763 F2FS_I(inode)->last_disk_size = psize;
2764 spin_unlock(&F2FS_I(inode)->i_size_lock);
2768 if (err && err != -ENOENT)
2772 inode_dec_dirty_pages(inode);
2774 ClearPageUptodate(page);
2775 clear_page_private_gcing(page);
2778 if (wbc->for_reclaim) {
2779 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2780 clear_inode_flag(inode, FI_HOT_DATA);
2781 f2fs_remove_dirty_inode(inode);
2785 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2786 !F2FS_I(inode)->cp_task && allow_balance)
2787 f2fs_balance_fs(sbi, need_balance_fs);
2789 if (unlikely(f2fs_cp_error(sbi))) {
2790 f2fs_submit_merged_write(sbi, DATA);
2791 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2796 *submitted = fio.submitted ? 1 : 0;
2801 redirty_page_for_writepage(wbc, page);
2803 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2804 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2805 * file_write_and_wait_range() will see EIO error, which is critical
2806 * to return value of fsync() followed by atomic_write failure to user.
2808 if (!err || wbc->for_reclaim)
2809 return AOP_WRITEPAGE_ACTIVATE;
2814 static int f2fs_write_data_page(struct page *page,
2815 struct writeback_control *wbc)
2817 #ifdef CONFIG_F2FS_FS_COMPRESSION
2818 struct inode *inode = page->mapping->host;
2820 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2823 if (f2fs_compressed_file(inode)) {
2824 if (f2fs_is_compressed_cluster(inode, page->index)) {
2825 redirty_page_for_writepage(wbc, page);
2826 return AOP_WRITEPAGE_ACTIVATE;
2832 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2833 wbc, FS_DATA_IO, 0, true);
2837 * This function was copied from write_cche_pages from mm/page-writeback.c.
2838 * The major change is making write step of cold data page separately from
2839 * warm/hot data page.
2841 static int f2fs_write_cache_pages(struct address_space *mapping,
2842 struct writeback_control *wbc,
2843 enum iostat_type io_type)
2846 int done = 0, retry = 0;
2847 struct pagevec pvec;
2848 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2849 struct bio *bio = NULL;
2850 sector_t last_block;
2851 #ifdef CONFIG_F2FS_FS_COMPRESSION
2852 struct inode *inode = mapping->host;
2853 struct compress_ctx cc = {
2855 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2856 .cluster_size = F2FS_I(inode)->i_cluster_size,
2857 .cluster_idx = NULL_CLUSTER,
2863 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2869 pgoff_t end; /* Inclusive */
2871 int range_whole = 0;
2877 pagevec_init(&pvec);
2879 if (get_dirty_pages(mapping->host) <=
2880 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2881 set_inode_flag(mapping->host, FI_HOT_DATA);
2883 clear_inode_flag(mapping->host, FI_HOT_DATA);
2885 if (wbc->range_cyclic) {
2886 index = mapping->writeback_index; /* prev offset */
2889 index = wbc->range_start >> PAGE_SHIFT;
2890 end = wbc->range_end >> PAGE_SHIFT;
2891 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2894 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2895 tag = PAGECACHE_TAG_TOWRITE;
2897 tag = PAGECACHE_TAG_DIRTY;
2900 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2901 tag_pages_for_writeback(mapping, index, end);
2903 while (!done && !retry && (index <= end)) {
2904 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2909 for (i = 0; i < nr_pages; i++) {
2910 struct page *page = pvec.pages[i];
2914 #ifdef CONFIG_F2FS_FS_COMPRESSION
2915 if (f2fs_compressed_file(inode)) {
2916 ret = f2fs_init_compress_ctx(&cc);
2922 if (!f2fs_cluster_can_merge_page(&cc,
2924 ret = f2fs_write_multi_pages(&cc,
2925 &submitted, wbc, io_type);
2931 if (unlikely(f2fs_cp_error(sbi)))
2934 if (f2fs_cluster_is_empty(&cc)) {
2935 void *fsdata = NULL;
2939 ret2 = f2fs_prepare_compress_overwrite(
2941 page->index, &fsdata);
2947 !f2fs_compress_write_end(inode,
2948 fsdata, page->index,
2958 /* give a priority to WB_SYNC threads */
2959 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2960 wbc->sync_mode == WB_SYNC_NONE) {
2964 #ifdef CONFIG_F2FS_FS_COMPRESSION
2967 done_index = page->index;
2971 if (unlikely(page->mapping != mapping)) {
2977 if (!PageDirty(page)) {
2978 /* someone wrote it for us */
2979 goto continue_unlock;
2982 if (PageWriteback(page)) {
2983 if (wbc->sync_mode != WB_SYNC_NONE)
2984 f2fs_wait_on_page_writeback(page,
2987 goto continue_unlock;
2990 if (!clear_page_dirty_for_io(page))
2991 goto continue_unlock;
2993 #ifdef CONFIG_F2FS_FS_COMPRESSION
2994 if (f2fs_compressed_file(inode)) {
2996 f2fs_compress_ctx_add_page(&cc, page);
3000 ret = f2fs_write_single_data_page(page, &submitted,
3001 &bio, &last_block, wbc, io_type,
3003 if (ret == AOP_WRITEPAGE_ACTIVATE)
3005 #ifdef CONFIG_F2FS_FS_COMPRESSION
3008 nwritten += submitted;
3009 wbc->nr_to_write -= submitted;
3011 if (unlikely(ret)) {
3013 * keep nr_to_write, since vfs uses this to
3014 * get # of written pages.
3016 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3019 } else if (ret == -EAGAIN) {
3021 if (wbc->sync_mode == WB_SYNC_ALL) {
3023 congestion_wait(BLK_RW_ASYNC,
3024 DEFAULT_IO_TIMEOUT);
3029 done_index = page->index + 1;
3034 if (wbc->nr_to_write <= 0 &&
3035 wbc->sync_mode == WB_SYNC_NONE) {
3043 pagevec_release(&pvec);
3046 #ifdef CONFIG_F2FS_FS_COMPRESSION
3047 /* flush remained pages in compress cluster */
3048 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3049 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3050 nwritten += submitted;
3051 wbc->nr_to_write -= submitted;
3057 if (f2fs_compressed_file(inode))
3058 f2fs_destroy_compress_ctx(&cc, false);
3065 if (wbc->range_cyclic && !done)
3067 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3068 mapping->writeback_index = done_index;
3071 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3073 /* submit cached bio of IPU write */
3075 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3080 static inline bool __should_serialize_io(struct inode *inode,
3081 struct writeback_control *wbc)
3083 /* to avoid deadlock in path of data flush */
3084 if (F2FS_I(inode)->cp_task)
3087 if (!S_ISREG(inode->i_mode))
3089 if (IS_NOQUOTA(inode))
3092 if (f2fs_need_compress_data(inode))
3094 if (wbc->sync_mode != WB_SYNC_ALL)
3096 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3101 static int __f2fs_write_data_pages(struct address_space *mapping,
3102 struct writeback_control *wbc,
3103 enum iostat_type io_type)
3105 struct inode *inode = mapping->host;
3106 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3107 struct blk_plug plug;
3109 bool locked = false;
3111 /* deal with chardevs and other special file */
3112 if (!mapping->a_ops->writepage)
3115 /* skip writing if there is no dirty page in this inode */
3116 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3119 /* during POR, we don't need to trigger writepage at all. */
3120 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3123 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3124 wbc->sync_mode == WB_SYNC_NONE &&
3125 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3126 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3129 /* skip writing during file defragment */
3130 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3133 trace_f2fs_writepages(mapping->host, wbc, DATA);
3135 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3136 if (wbc->sync_mode == WB_SYNC_ALL)
3137 atomic_inc(&sbi->wb_sync_req[DATA]);
3138 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3141 if (__should_serialize_io(inode, wbc)) {
3142 mutex_lock(&sbi->writepages);
3146 blk_start_plug(&plug);
3147 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3148 blk_finish_plug(&plug);
3151 mutex_unlock(&sbi->writepages);
3153 if (wbc->sync_mode == WB_SYNC_ALL)
3154 atomic_dec(&sbi->wb_sync_req[DATA]);
3156 * if some pages were truncated, we cannot guarantee its mapping->host
3157 * to detect pending bios.
3160 f2fs_remove_dirty_inode(inode);
3164 wbc->pages_skipped += get_dirty_pages(inode);
3165 trace_f2fs_writepages(mapping->host, wbc, DATA);
3169 static int f2fs_write_data_pages(struct address_space *mapping,
3170 struct writeback_control *wbc)
3172 struct inode *inode = mapping->host;
3174 return __f2fs_write_data_pages(mapping, wbc,
3175 F2FS_I(inode)->cp_task == current ?
3176 FS_CP_DATA_IO : FS_DATA_IO);
3179 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3181 struct inode *inode = mapping->host;
3182 loff_t i_size = i_size_read(inode);
3184 if (IS_NOQUOTA(inode))
3187 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3188 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3189 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3190 down_write(&F2FS_I(inode)->i_mmap_sem);
3192 truncate_pagecache(inode, i_size);
3193 f2fs_truncate_blocks(inode, i_size, true);
3195 up_write(&F2FS_I(inode)->i_mmap_sem);
3196 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3200 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3201 struct page *page, loff_t pos, unsigned len,
3202 block_t *blk_addr, bool *node_changed)
3204 struct inode *inode = page->mapping->host;
3205 pgoff_t index = page->index;
3206 struct dnode_of_data dn;
3208 bool locked = false;
3209 struct extent_info ei = {0,0,0};
3214 * we already allocated all the blocks, so we don't need to get
3215 * the block addresses when there is no need to fill the page.
3217 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3218 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3219 !f2fs_verity_in_progress(inode))
3222 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3223 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3224 flag = F2FS_GET_BLOCK_DEFAULT;
3226 flag = F2FS_GET_BLOCK_PRE_AIO;
3228 if (f2fs_has_inline_data(inode) ||
3229 (pos & PAGE_MASK) >= i_size_read(inode)) {
3230 f2fs_do_map_lock(sbi, flag, true);
3235 /* check inline_data */
3236 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3237 if (IS_ERR(ipage)) {
3238 err = PTR_ERR(ipage);
3242 set_new_dnode(&dn, inode, ipage, ipage, 0);
3244 if (f2fs_has_inline_data(inode)) {
3245 if (pos + len <= MAX_INLINE_DATA(inode)) {
3246 f2fs_do_read_inline_data(page, ipage);
3247 set_inode_flag(inode, FI_DATA_EXIST);
3249 set_page_private_inline(ipage);
3251 err = f2fs_convert_inline_page(&dn, page);
3254 if (dn.data_blkaddr == NULL_ADDR)
3255 err = f2fs_get_block(&dn, index);
3257 } else if (locked) {
3258 err = f2fs_get_block(&dn, index);
3260 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3261 dn.data_blkaddr = ei.blk + index - ei.fofs;
3264 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3265 if (err || dn.data_blkaddr == NULL_ADDR) {
3266 f2fs_put_dnode(&dn);
3267 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3269 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3276 /* convert_inline_page can make node_changed */
3277 *blk_addr = dn.data_blkaddr;
3278 *node_changed = dn.node_changed;
3280 f2fs_put_dnode(&dn);
3283 f2fs_do_map_lock(sbi, flag, false);
3287 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3288 loff_t pos, unsigned len, unsigned flags,
3289 struct page **pagep, void **fsdata)
3291 struct inode *inode = mapping->host;
3292 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3293 struct page *page = NULL;
3294 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3295 bool need_balance = false, drop_atomic = false;
3296 block_t blkaddr = NULL_ADDR;
3299 trace_f2fs_write_begin(inode, pos, len, flags);
3301 if (!f2fs_is_checkpoint_ready(sbi)) {
3306 if ((f2fs_is_atomic_file(inode) &&
3307 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3308 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3315 * We should check this at this moment to avoid deadlock on inode page
3316 * and #0 page. The locking rule for inline_data conversion should be:
3317 * lock_page(page #0) -> lock_page(inode_page)
3320 err = f2fs_convert_inline_inode(inode);
3325 #ifdef CONFIG_F2FS_FS_COMPRESSION
3326 if (f2fs_compressed_file(inode)) {
3331 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3344 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3345 * wait_for_stable_page. Will wait that below with our IO control.
3347 page = f2fs_pagecache_get_page(mapping, index,
3348 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3354 /* TODO: cluster can be compressed due to race with .writepage */
3358 err = prepare_write_begin(sbi, page, pos, len,
3359 &blkaddr, &need_balance);
3363 if (need_balance && !IS_NOQUOTA(inode) &&
3364 has_not_enough_free_secs(sbi, 0, 0)) {
3366 f2fs_balance_fs(sbi, true);
3368 if (page->mapping != mapping) {
3369 /* The page got truncated from under us */
3370 f2fs_put_page(page, 1);
3375 f2fs_wait_on_page_writeback(page, DATA, false, true);
3377 if (len == PAGE_SIZE || PageUptodate(page))
3380 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3381 !f2fs_verity_in_progress(inode)) {
3382 zero_user_segment(page, len, PAGE_SIZE);
3386 if (blkaddr == NEW_ADDR) {
3387 zero_user_segment(page, 0, PAGE_SIZE);
3388 SetPageUptodate(page);
3390 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3391 DATA_GENERIC_ENHANCE_READ)) {
3392 err = -EFSCORRUPTED;
3395 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3400 if (unlikely(page->mapping != mapping)) {
3401 f2fs_put_page(page, 1);
3404 if (unlikely(!PageUptodate(page))) {
3412 f2fs_put_page(page, 1);
3413 f2fs_write_failed(mapping, pos + len);
3415 f2fs_drop_inmem_pages_all(sbi, false);
3419 static int f2fs_write_end(struct file *file,
3420 struct address_space *mapping,
3421 loff_t pos, unsigned len, unsigned copied,
3422 struct page *page, void *fsdata)
3424 struct inode *inode = page->mapping->host;
3426 trace_f2fs_write_end(inode, pos, len, copied);
3429 * This should be come from len == PAGE_SIZE, and we expect copied
3430 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3431 * let generic_perform_write() try to copy data again through copied=0.
3433 if (!PageUptodate(page)) {
3434 if (unlikely(copied != len))
3437 SetPageUptodate(page);
3440 #ifdef CONFIG_F2FS_FS_COMPRESSION
3441 /* overwrite compressed file */
3442 if (f2fs_compressed_file(inode) && fsdata) {
3443 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3444 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3446 if (pos + copied > i_size_read(inode) &&
3447 !f2fs_verity_in_progress(inode))
3448 f2fs_i_size_write(inode, pos + copied);
3456 set_page_dirty(page);
3458 if (pos + copied > i_size_read(inode) &&
3459 !f2fs_verity_in_progress(inode))
3460 f2fs_i_size_write(inode, pos + copied);
3462 f2fs_put_page(page, 1);
3463 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3467 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3470 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3471 unsigned blkbits = i_blkbits;
3472 unsigned blocksize_mask = (1 << blkbits) - 1;
3473 unsigned long align = offset | iov_iter_alignment(iter);
3474 struct block_device *bdev = inode->i_sb->s_bdev;
3476 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3479 if (align & blocksize_mask) {
3481 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3482 blocksize_mask = (1 << blkbits) - 1;
3483 if (align & blocksize_mask)
3490 static void f2fs_dio_end_io(struct bio *bio)
3492 struct f2fs_private_dio *dio = bio->bi_private;
3494 dec_page_count(F2FS_I_SB(dio->inode),
3495 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3497 bio->bi_private = dio->orig_private;
3498 bio->bi_end_io = dio->orig_end_io;
3505 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3508 struct f2fs_private_dio *dio;
3509 bool write = (bio_op(bio) == REQ_OP_WRITE);
3511 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3512 sizeof(struct f2fs_private_dio), GFP_NOFS);
3517 dio->orig_end_io = bio->bi_end_io;
3518 dio->orig_private = bio->bi_private;
3521 bio->bi_end_io = f2fs_dio_end_io;
3522 bio->bi_private = dio;
3524 inc_page_count(F2FS_I_SB(inode),
3525 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3530 bio->bi_status = BLK_STS_IOERR;
3534 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3536 struct address_space *mapping = iocb->ki_filp->f_mapping;
3537 struct inode *inode = mapping->host;
3538 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3539 struct f2fs_inode_info *fi = F2FS_I(inode);
3540 size_t count = iov_iter_count(iter);
3541 loff_t offset = iocb->ki_pos;
3542 int rw = iov_iter_rw(iter);
3544 enum rw_hint hint = iocb->ki_hint;
3545 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3548 err = check_direct_IO(inode, iter, offset);
3550 return err < 0 ? err : 0;
3552 if (f2fs_force_buffered_io(inode, iocb, iter))
3555 do_opu = allow_outplace_dio(inode, iocb, iter);
3557 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3559 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3560 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3562 if (iocb->ki_flags & IOCB_NOWAIT) {
3563 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3564 iocb->ki_hint = hint;
3568 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3569 up_read(&fi->i_gc_rwsem[rw]);
3570 iocb->ki_hint = hint;
3575 down_read(&fi->i_gc_rwsem[rw]);
3577 down_read(&fi->i_gc_rwsem[READ]);
3580 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3581 iter, rw == WRITE ? get_data_block_dio_write :
3582 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3583 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3587 up_read(&fi->i_gc_rwsem[READ]);
3589 up_read(&fi->i_gc_rwsem[rw]);
3592 if (whint_mode == WHINT_MODE_OFF)
3593 iocb->ki_hint = hint;
3595 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3598 set_inode_flag(inode, FI_UPDATE_WRITE);
3599 } else if (err == -EIOCBQUEUED) {
3600 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3601 count - iov_iter_count(iter));
3602 } else if (err < 0) {
3603 f2fs_write_failed(mapping, offset + count);
3607 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3608 else if (err == -EIOCBQUEUED)
3609 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3610 count - iov_iter_count(iter));
3614 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3619 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3620 unsigned int length)
3622 struct inode *inode = page->mapping->host;
3623 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3625 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3626 (offset % PAGE_SIZE || length != PAGE_SIZE))
3629 if (PageDirty(page)) {
3630 if (inode->i_ino == F2FS_META_INO(sbi)) {
3631 dec_page_count(sbi, F2FS_DIRTY_META);
3632 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3633 dec_page_count(sbi, F2FS_DIRTY_NODES);
3635 inode_dec_dirty_pages(inode);
3636 f2fs_remove_dirty_inode(inode);
3640 clear_page_private_gcing(page);
3642 if (test_opt(sbi, COMPRESS_CACHE)) {
3643 if (f2fs_compressed_file(inode))
3644 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3645 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3646 clear_page_private_data(page);
3649 if (page_private_atomic(page))
3650 return f2fs_drop_inmem_page(inode, page);
3652 detach_page_private(page);
3653 set_page_private(page, 0);
3656 int f2fs_release_page(struct page *page, gfp_t wait)
3658 /* If this is dirty page, keep PagePrivate */
3659 if (PageDirty(page))
3662 /* This is atomic written page, keep Private */
3663 if (page_private_atomic(page))
3666 if (test_opt(F2FS_P_SB(page), COMPRESS_CACHE)) {
3667 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3668 struct inode *inode = page->mapping->host;
3670 if (f2fs_compressed_file(inode))
3671 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3672 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3673 clear_page_private_data(page);
3676 clear_page_private_gcing(page);
3678 detach_page_private(page);
3679 set_page_private(page, 0);
3683 static int f2fs_set_data_page_dirty(struct page *page)
3685 struct inode *inode = page_file_mapping(page)->host;
3687 trace_f2fs_set_page_dirty(page, DATA);
3689 if (!PageUptodate(page))
3690 SetPageUptodate(page);
3691 if (PageSwapCache(page))
3692 return __set_page_dirty_nobuffers(page);
3694 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3695 if (!page_private_atomic(page)) {
3696 f2fs_register_inmem_page(inode, page);
3700 * Previously, this page has been registered, we just
3706 if (!PageDirty(page)) {
3707 __set_page_dirty_nobuffers(page);
3708 f2fs_update_dirty_page(inode, page);
3715 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3717 #ifdef CONFIG_F2FS_FS_COMPRESSION
3718 struct dnode_of_data dn;
3719 sector_t start_idx, blknr = 0;
3722 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3724 set_new_dnode(&dn, inode, NULL, NULL, 0);
3725 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3729 if (dn.data_blkaddr != COMPRESS_ADDR) {
3730 dn.ofs_in_node += block - start_idx;
3731 blknr = f2fs_data_blkaddr(&dn);
3732 if (!__is_valid_data_blkaddr(blknr))
3736 f2fs_put_dnode(&dn);
3744 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3746 struct inode *inode = mapping->host;
3749 if (f2fs_has_inline_data(inode))
3752 /* make sure allocating whole blocks */
3753 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3754 filemap_write_and_wait(mapping);
3756 /* Block number less than F2FS MAX BLOCKS */
3757 if (unlikely(block >= max_file_blocks(inode)))
3760 if (f2fs_compressed_file(inode)) {
3761 blknr = f2fs_bmap_compress(inode, block);
3763 struct f2fs_map_blocks map;
3765 memset(&map, 0, sizeof(map));
3768 map.m_next_pgofs = NULL;
3769 map.m_seg_type = NO_CHECK_TYPE;
3771 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3775 trace_f2fs_bmap(inode, block, blknr);
3779 #ifdef CONFIG_MIGRATION
3780 #include <linux/migrate.h>
3782 int f2fs_migrate_page(struct address_space *mapping,
3783 struct page *newpage, struct page *page, enum migrate_mode mode)
3785 int rc, extra_count;
3786 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3787 bool atomic_written = page_private_atomic(page);
3789 BUG_ON(PageWriteback(page));
3791 /* migrating an atomic written page is safe with the inmem_lock hold */
3792 if (atomic_written) {
3793 if (mode != MIGRATE_SYNC)
3795 if (!mutex_trylock(&fi->inmem_lock))
3799 /* one extra reference was held for atomic_write page */
3800 extra_count = atomic_written ? 1 : 0;
3801 rc = migrate_page_move_mapping(mapping, newpage,
3803 if (rc != MIGRATEPAGE_SUCCESS) {
3805 mutex_unlock(&fi->inmem_lock);
3809 if (atomic_written) {
3810 struct inmem_pages *cur;
3812 list_for_each_entry(cur, &fi->inmem_pages, list)
3813 if (cur->page == page) {
3814 cur->page = newpage;
3817 mutex_unlock(&fi->inmem_lock);
3822 /* guarantee to start from no stale private field */
3823 set_page_private(newpage, 0);
3824 if (PagePrivate(page)) {
3825 set_page_private(newpage, page_private(page));
3826 SetPagePrivate(newpage);
3829 set_page_private(page, 0);
3830 ClearPagePrivate(page);
3834 if (mode != MIGRATE_SYNC_NO_COPY)
3835 migrate_page_copy(newpage, page);
3837 migrate_page_states(newpage, page);
3839 return MIGRATEPAGE_SUCCESS;
3844 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3845 unsigned int blkcnt)
3847 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3848 unsigned int blkofs;
3849 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3850 unsigned int secidx = start_blk / blk_per_sec;
3851 unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3854 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3855 down_write(&F2FS_I(inode)->i_mmap_sem);
3857 set_inode_flag(inode, FI_ALIGNED_WRITE);
3859 for (; secidx < end_sec; secidx++) {
3860 down_write(&sbi->pin_sem);
3863 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
3864 f2fs_unlock_op(sbi);
3866 set_inode_flag(inode, FI_DO_DEFRAG);
3868 for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
3870 unsigned int blkidx = secidx * blk_per_sec + blkofs;
3872 page = f2fs_get_lock_data_page(inode, blkidx, true);
3874 up_write(&sbi->pin_sem);
3875 ret = PTR_ERR(page);
3879 set_page_dirty(page);
3880 f2fs_put_page(page, 1);
3883 clear_inode_flag(inode, FI_DO_DEFRAG);
3885 ret = filemap_fdatawrite(inode->i_mapping);
3887 up_write(&sbi->pin_sem);
3894 clear_inode_flag(inode, FI_DO_DEFRAG);
3895 clear_inode_flag(inode, FI_ALIGNED_WRITE);
3897 up_write(&F2FS_I(inode)->i_mmap_sem);
3898 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3903 static int check_swap_activate(struct swap_info_struct *sis,
3904 struct file *swap_file, sector_t *span)
3906 struct address_space *mapping = swap_file->f_mapping;
3907 struct inode *inode = mapping->host;
3908 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3909 sector_t cur_lblock;
3910 sector_t last_lblock;
3912 sector_t lowest_pblock = -1;
3913 sector_t highest_pblock = 0;
3915 unsigned long nr_pblocks;
3916 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3917 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
3918 unsigned int not_aligned = 0;
3922 * Map all the blocks into the extent list. This code doesn't try
3926 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3928 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
3929 struct f2fs_map_blocks map;
3933 memset(&map, 0, sizeof(map));
3934 map.m_lblk = cur_lblock;
3935 map.m_len = last_lblock - cur_lblock;
3936 map.m_next_pgofs = NULL;
3937 map.m_next_extent = NULL;
3938 map.m_seg_type = NO_CHECK_TYPE;
3939 map.m_may_create = false;
3941 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3946 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3947 f2fs_err(sbi, "Swapfile has holes");
3952 pblock = map.m_pblk;
3953 nr_pblocks = map.m_len;
3955 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
3956 nr_pblocks & sec_blks_mask) {
3959 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
3960 if (cur_lblock + nr_pblocks > sis->max)
3961 nr_pblocks -= blks_per_sec;
3964 /* this extent is last one */
3965 nr_pblocks = map.m_len;
3966 f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
3970 ret = f2fs_migrate_blocks(inode, cur_lblock,
3977 if (cur_lblock + nr_pblocks >= sis->max)
3978 nr_pblocks = sis->max - cur_lblock;
3980 if (cur_lblock) { /* exclude the header page */
3981 if (pblock < lowest_pblock)
3982 lowest_pblock = pblock;
3983 if (pblock + nr_pblocks - 1 > highest_pblock)
3984 highest_pblock = pblock + nr_pblocks - 1;
3988 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3990 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3994 cur_lblock += nr_pblocks;
3997 *span = 1 + highest_pblock - lowest_pblock;
3998 if (cur_lblock == 0)
3999 cur_lblock = 1; /* force Empty message */
4000 sis->max = cur_lblock;
4001 sis->pages = cur_lblock - 1;
4002 sis->highest_bit = cur_lblock - 1;
4005 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
4006 not_aligned, blks_per_sec * F2FS_BLKSIZE);
4010 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4013 struct inode *inode = file_inode(file);
4016 if (!S_ISREG(inode->i_mode))
4019 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
4022 if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
4023 f2fs_err(F2FS_I_SB(inode),
4024 "Swapfile not supported in LFS mode");
4028 ret = f2fs_convert_inline_inode(inode);
4032 if (!f2fs_disable_compressed_file(inode))
4035 f2fs_precache_extents(inode);
4037 ret = check_swap_activate(sis, file, span);
4041 set_inode_flag(inode, FI_PIN_FILE);
4042 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4046 static void f2fs_swap_deactivate(struct file *file)
4048 struct inode *inode = file_inode(file);
4050 clear_inode_flag(inode, FI_PIN_FILE);
4053 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4059 static void f2fs_swap_deactivate(struct file *file)
4064 const struct address_space_operations f2fs_dblock_aops = {
4065 .readpage = f2fs_read_data_page,
4066 .readahead = f2fs_readahead,
4067 .writepage = f2fs_write_data_page,
4068 .writepages = f2fs_write_data_pages,
4069 .write_begin = f2fs_write_begin,
4070 .write_end = f2fs_write_end,
4071 .set_page_dirty = f2fs_set_data_page_dirty,
4072 .invalidatepage = f2fs_invalidate_page,
4073 .releasepage = f2fs_release_page,
4074 .direct_IO = f2fs_direct_IO,
4076 .swap_activate = f2fs_swap_activate,
4077 .swap_deactivate = f2fs_swap_deactivate,
4078 #ifdef CONFIG_MIGRATION
4079 .migratepage = f2fs_migrate_page,
4083 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4085 struct address_space *mapping = page_mapping(page);
4086 unsigned long flags;
4088 xa_lock_irqsave(&mapping->i_pages, flags);
4089 __xa_clear_mark(&mapping->i_pages, page_index(page),
4090 PAGECACHE_TAG_DIRTY);
4091 xa_unlock_irqrestore(&mapping->i_pages, flags);
4094 int __init f2fs_init_post_read_processing(void)
4096 bio_post_read_ctx_cache =
4097 kmem_cache_create("f2fs_bio_post_read_ctx",
4098 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4099 if (!bio_post_read_ctx_cache)
4101 bio_post_read_ctx_pool =
4102 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4103 bio_post_read_ctx_cache);
4104 if (!bio_post_read_ctx_pool)
4105 goto fail_free_cache;
4109 kmem_cache_destroy(bio_post_read_ctx_cache);
4114 void f2fs_destroy_post_read_processing(void)
4116 mempool_destroy(bio_post_read_ctx_pool);
4117 kmem_cache_destroy(bio_post_read_ctx_cache);
4120 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4122 if (!f2fs_sb_has_encrypt(sbi) &&
4123 !f2fs_sb_has_verity(sbi) &&
4124 !f2fs_sb_has_compression(sbi))
4127 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4128 WQ_UNBOUND | WQ_HIGHPRI,
4130 if (!sbi->post_read_wq)
4135 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4137 if (sbi->post_read_wq)
4138 destroy_workqueue(sbi->post_read_wq);
4141 int __init f2fs_init_bio_entry_cache(void)
4143 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4144 sizeof(struct bio_entry));
4145 if (!bio_entry_slab)
4150 void f2fs_destroy_bio_entry_cache(void)
4152 kmem_cache_destroy(bio_entry_slab);