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 if (f2fs_is_compressed_page(page))
64 inode = mapping->host;
65 sbi = F2FS_I_SB(inode);
67 if (inode->i_ino == F2FS_META_INO(sbi) ||
68 inode->i_ino == F2FS_NODE_INO(sbi) ||
69 S_ISDIR(inode->i_mode) ||
70 (S_ISREG(inode->i_mode) &&
71 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
77 static enum count_type __read_io_type(struct page *page)
79 struct address_space *mapping = page_file_mapping(page);
82 struct inode *inode = mapping->host;
83 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
85 if (inode->i_ino == F2FS_META_INO(sbi))
88 if (inode->i_ino == F2FS_NODE_INO(sbi))
94 /* postprocessing steps for read bios */
95 enum bio_post_read_step {
96 #ifdef CONFIG_FS_ENCRYPTION
97 STEP_DECRYPT = 1 << 0,
99 STEP_DECRYPT = 0, /* compile out the decryption-related code */
101 #ifdef CONFIG_F2FS_FS_COMPRESSION
102 STEP_DECOMPRESS = 1 << 1,
104 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
106 #ifdef CONFIG_FS_VERITY
107 STEP_VERITY = 1 << 2,
109 STEP_VERITY = 0, /* compile out the verity-related code */
113 struct bio_post_read_ctx {
115 struct f2fs_sb_info *sbi;
116 struct work_struct work;
117 unsigned int enabled_steps;
120 static void f2fs_finish_read_bio(struct bio *bio)
123 struct bvec_iter_all iter_all;
126 * Update and unlock the bio's pagecache pages, and put the
127 * decompression context for any compressed pages.
129 bio_for_each_segment_all(bv, bio, iter_all) {
130 struct page *page = bv->bv_page;
132 if (f2fs_is_compressed_page(page)) {
134 f2fs_end_read_compressed_page(page, true);
135 f2fs_put_page_dic(page);
139 /* PG_error was set if decryption or verity failed. */
140 if (bio->bi_status || PageError(page)) {
141 ClearPageUptodate(page);
142 /* will re-read again later */
143 ClearPageError(page);
145 SetPageUptodate(page);
147 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
152 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
156 static void f2fs_verify_bio(struct work_struct *work)
158 struct bio_post_read_ctx *ctx =
159 container_of(work, struct bio_post_read_ctx, work);
160 struct bio *bio = ctx->bio;
161 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
164 * fsverity_verify_bio() may call readpages() again, and while verity
165 * will be disabled for this, decryption and/or decompression may still
166 * be needed, resulting in another bio_post_read_ctx being allocated.
167 * So to prevent deadlocks we need to release the current ctx to the
168 * mempool first. This assumes that verity is the last post-read step.
170 mempool_free(ctx, bio_post_read_ctx_pool);
171 bio->bi_private = NULL;
174 * Verify the bio's pages with fs-verity. Exclude compressed pages,
175 * as those were handled separately by f2fs_end_read_compressed_page().
177 if (may_have_compressed_pages) {
179 struct bvec_iter_all iter_all;
181 bio_for_each_segment_all(bv, bio, iter_all) {
182 struct page *page = bv->bv_page;
184 if (!f2fs_is_compressed_page(page) &&
185 !PageError(page) && !fsverity_verify_page(page))
189 fsverity_verify_bio(bio);
192 f2fs_finish_read_bio(bio);
196 * If the bio's data needs to be verified with fs-verity, then enqueue the
197 * verity work for the bio. Otherwise finish the bio now.
199 * Note that to avoid deadlocks, the verity work can't be done on the
200 * decryption/decompression workqueue. This is because verifying the data pages
201 * can involve reading verity metadata pages from the file, and these verity
202 * metadata pages may be encrypted and/or compressed.
204 static void f2fs_verify_and_finish_bio(struct bio *bio)
206 struct bio_post_read_ctx *ctx = bio->bi_private;
208 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
209 INIT_WORK(&ctx->work, f2fs_verify_bio);
210 fsverity_enqueue_verify_work(&ctx->work);
212 f2fs_finish_read_bio(bio);
217 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
218 * remaining page was read by @ctx->bio.
220 * Note that a bio may span clusters (even a mix of compressed and uncompressed
221 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
222 * that the bio includes at least one compressed page. The actual decompression
223 * is done on a per-cluster basis, not a per-bio basis.
225 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx)
228 struct bvec_iter_all iter_all;
229 bool all_compressed = true;
231 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
232 struct page *page = bv->bv_page;
234 /* PG_error was set if decryption failed. */
235 if (f2fs_is_compressed_page(page))
236 f2fs_end_read_compressed_page(page, PageError(page));
238 all_compressed = false;
242 * Optimization: if all the bio's pages are compressed, then scheduling
243 * the per-bio verity work is unnecessary, as verity will be fully
244 * handled at the compression cluster level.
247 ctx->enabled_steps &= ~STEP_VERITY;
250 static void f2fs_post_read_work(struct work_struct *work)
252 struct bio_post_read_ctx *ctx =
253 container_of(work, struct bio_post_read_ctx, work);
255 if (ctx->enabled_steps & STEP_DECRYPT)
256 fscrypt_decrypt_bio(ctx->bio);
258 if (ctx->enabled_steps & STEP_DECOMPRESS)
259 f2fs_handle_step_decompress(ctx);
261 f2fs_verify_and_finish_bio(ctx->bio);
264 static void f2fs_read_end_io(struct bio *bio)
266 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
267 struct bio_post_read_ctx *ctx = bio->bi_private;
269 if (time_to_inject(sbi, FAULT_READ_IO)) {
270 f2fs_show_injection_info(sbi, FAULT_READ_IO);
271 bio->bi_status = BLK_STS_IOERR;
274 if (bio->bi_status) {
275 f2fs_finish_read_bio(bio);
279 if (ctx && (ctx->enabled_steps & (STEP_DECRYPT | STEP_DECOMPRESS))) {
280 INIT_WORK(&ctx->work, f2fs_post_read_work);
281 queue_work(ctx->sbi->post_read_wq, &ctx->work);
283 f2fs_verify_and_finish_bio(bio);
287 static void f2fs_write_end_io(struct bio *bio)
289 struct f2fs_sb_info *sbi = bio->bi_private;
290 struct bio_vec *bvec;
291 struct bvec_iter_all iter_all;
293 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
294 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
295 bio->bi_status = BLK_STS_IOERR;
298 bio_for_each_segment_all(bvec, bio, iter_all) {
299 struct page *page = bvec->bv_page;
300 enum count_type type = WB_DATA_TYPE(page);
302 if (IS_DUMMY_WRITTEN_PAGE(page)) {
303 set_page_private(page, (unsigned long)NULL);
304 ClearPagePrivate(page);
306 mempool_free(page, sbi->write_io_dummy);
308 if (unlikely(bio->bi_status))
309 f2fs_stop_checkpoint(sbi, true);
313 fscrypt_finalize_bounce_page(&page);
315 #ifdef CONFIG_F2FS_FS_COMPRESSION
316 if (f2fs_is_compressed_page(page)) {
317 f2fs_compress_write_end_io(bio, page);
322 if (unlikely(bio->bi_status)) {
323 mapping_set_error(page->mapping, -EIO);
324 if (type == F2FS_WB_CP_DATA)
325 f2fs_stop_checkpoint(sbi, true);
328 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
329 page->index != nid_of_node(page));
331 dec_page_count(sbi, type);
332 if (f2fs_in_warm_node_list(sbi, page))
333 f2fs_del_fsync_node_entry(sbi, page);
334 clear_cold_data(page);
335 end_page_writeback(page);
337 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
338 wq_has_sleeper(&sbi->cp_wait))
339 wake_up(&sbi->cp_wait);
344 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
345 block_t blk_addr, struct bio *bio)
347 struct block_device *bdev = sbi->sb->s_bdev;
350 if (f2fs_is_multi_device(sbi)) {
351 for (i = 0; i < sbi->s_ndevs; i++) {
352 if (FDEV(i).start_blk <= blk_addr &&
353 FDEV(i).end_blk >= blk_addr) {
354 blk_addr -= FDEV(i).start_blk;
361 bio_set_dev(bio, bdev);
362 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
367 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
371 if (!f2fs_is_multi_device(sbi))
374 for (i = 0; i < sbi->s_ndevs; i++)
375 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
380 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
382 struct f2fs_sb_info *sbi = fio->sbi;
385 bio = bio_alloc_bioset(GFP_NOIO, npages, &f2fs_bioset);
387 f2fs_target_device(sbi, fio->new_blkaddr, bio);
388 if (is_read_io(fio->op)) {
389 bio->bi_end_io = f2fs_read_end_io;
390 bio->bi_private = NULL;
392 bio->bi_end_io = f2fs_write_end_io;
393 bio->bi_private = sbi;
394 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
395 fio->type, fio->temp);
398 wbc_init_bio(fio->io_wbc, bio);
403 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
405 const struct f2fs_io_info *fio,
409 * The f2fs garbage collector sets ->encrypted_page when it wants to
410 * read/write raw data without encryption.
412 if (!fio || !fio->encrypted_page)
413 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
416 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
418 const struct f2fs_io_info *fio)
421 * The f2fs garbage collector sets ->encrypted_page when it wants to
422 * read/write raw data without encryption.
424 if (fio && fio->encrypted_page)
425 return !bio_has_crypt_ctx(bio);
427 return fscrypt_mergeable_bio(bio, inode, next_idx);
430 static inline void __submit_bio(struct f2fs_sb_info *sbi,
431 struct bio *bio, enum page_type type)
433 if (!is_read_io(bio_op(bio))) {
436 if (type != DATA && type != NODE)
439 if (f2fs_lfs_mode(sbi) && current->plug)
440 blk_finish_plug(current->plug);
442 if (!F2FS_IO_ALIGNED(sbi))
445 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
446 start %= F2FS_IO_SIZE(sbi);
451 /* fill dummy pages */
452 for (; start < F2FS_IO_SIZE(sbi); start++) {
454 mempool_alloc(sbi->write_io_dummy,
455 GFP_NOIO | __GFP_NOFAIL);
456 f2fs_bug_on(sbi, !page);
458 zero_user_segment(page, 0, PAGE_SIZE);
459 SetPagePrivate(page);
460 set_page_private(page, DUMMY_WRITTEN_PAGE);
462 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
466 * In the NODE case, we lose next block address chain. So, we
467 * need to do checkpoint in f2fs_sync_file.
470 set_sbi_flag(sbi, SBI_NEED_CP);
473 if (is_read_io(bio_op(bio)))
474 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
476 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
480 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
481 struct bio *bio, enum page_type type)
483 __submit_bio(sbi, bio, type);
486 static void __attach_io_flag(struct f2fs_io_info *fio)
488 struct f2fs_sb_info *sbi = fio->sbi;
489 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
490 unsigned int io_flag, fua_flag, meta_flag;
492 if (fio->type == DATA)
493 io_flag = sbi->data_io_flag;
494 else if (fio->type == NODE)
495 io_flag = sbi->node_io_flag;
499 fua_flag = io_flag & temp_mask;
500 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
503 * data/node io flag bits per temp:
504 * REQ_META | REQ_FUA |
505 * 5 | 4 | 3 | 2 | 1 | 0 |
506 * Cold | Warm | Hot | Cold | Warm | Hot |
508 if ((1 << fio->temp) & meta_flag)
509 fio->op_flags |= REQ_META;
510 if ((1 << fio->temp) & fua_flag)
511 fio->op_flags |= REQ_FUA;
514 static void __submit_merged_bio(struct f2fs_bio_info *io)
516 struct f2fs_io_info *fio = &io->fio;
521 __attach_io_flag(fio);
522 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
524 if (is_read_io(fio->op))
525 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
527 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
529 __submit_bio(io->sbi, io->bio, fio->type);
533 static bool __has_merged_page(struct bio *bio, struct inode *inode,
534 struct page *page, nid_t ino)
536 struct bio_vec *bvec;
537 struct bvec_iter_all iter_all;
542 if (!inode && !page && !ino)
545 bio_for_each_segment_all(bvec, bio, iter_all) {
546 struct page *target = bvec->bv_page;
548 if (fscrypt_is_bounce_page(target)) {
549 target = fscrypt_pagecache_page(target);
553 if (f2fs_is_compressed_page(target)) {
554 target = f2fs_compress_control_page(target);
559 if (inode && inode == target->mapping->host)
561 if (page && page == target)
563 if (ino && ino == ino_of_node(target))
570 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
571 enum page_type type, enum temp_type temp)
573 enum page_type btype = PAGE_TYPE_OF_BIO(type);
574 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
576 down_write(&io->io_rwsem);
578 /* change META to META_FLUSH in the checkpoint procedure */
579 if (type >= META_FLUSH) {
580 io->fio.type = META_FLUSH;
581 io->fio.op = REQ_OP_WRITE;
582 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
583 if (!test_opt(sbi, NOBARRIER))
584 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
586 __submit_merged_bio(io);
587 up_write(&io->io_rwsem);
590 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
591 struct inode *inode, struct page *page,
592 nid_t ino, enum page_type type, bool force)
597 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
599 enum page_type btype = PAGE_TYPE_OF_BIO(type);
600 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
602 down_read(&io->io_rwsem);
603 ret = __has_merged_page(io->bio, inode, page, ino);
604 up_read(&io->io_rwsem);
607 __f2fs_submit_merged_write(sbi, type, temp);
609 /* TODO: use HOT temp only for meta pages now. */
615 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
617 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
620 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
621 struct inode *inode, struct page *page,
622 nid_t ino, enum page_type type)
624 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
627 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
629 f2fs_submit_merged_write(sbi, DATA);
630 f2fs_submit_merged_write(sbi, NODE);
631 f2fs_submit_merged_write(sbi, META);
635 * Fill the locked page with data located in the block address.
636 * A caller needs to unlock the page on failure.
638 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
641 struct page *page = fio->encrypted_page ?
642 fio->encrypted_page : fio->page;
644 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
645 fio->is_por ? META_POR : (__is_meta_io(fio) ?
646 META_GENERIC : DATA_GENERIC_ENHANCE)))
647 return -EFSCORRUPTED;
649 trace_f2fs_submit_page_bio(page, fio);
651 /* Allocate a new bio */
652 bio = __bio_alloc(fio, 1);
654 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
655 fio->page->index, fio, GFP_NOIO);
657 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
662 if (fio->io_wbc && !is_read_io(fio->op))
663 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
665 __attach_io_flag(fio);
666 bio_set_op_attrs(bio, fio->op, fio->op_flags);
668 inc_page_count(fio->sbi, is_read_io(fio->op) ?
669 __read_io_type(page): WB_DATA_TYPE(fio->page));
671 __submit_bio(fio->sbi, bio, fio->type);
675 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
676 block_t last_blkaddr, block_t cur_blkaddr)
678 if (unlikely(sbi->max_io_bytes &&
679 bio->bi_iter.bi_size >= sbi->max_io_bytes))
681 if (last_blkaddr + 1 != cur_blkaddr)
683 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
686 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
687 struct f2fs_io_info *fio)
689 if (io->fio.op != fio->op)
691 return io->fio.op_flags == fio->op_flags;
694 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
695 struct f2fs_bio_info *io,
696 struct f2fs_io_info *fio,
697 block_t last_blkaddr,
700 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
701 unsigned int filled_blocks =
702 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
703 unsigned int io_size = F2FS_IO_SIZE(sbi);
704 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
706 /* IOs in bio is aligned and left space of vectors is not enough */
707 if (!(filled_blocks % io_size) && left_vecs < io_size)
710 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
712 return io_type_is_mergeable(io, fio);
715 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
716 struct page *page, enum temp_type temp)
718 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
719 struct bio_entry *be;
721 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
725 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
728 down_write(&io->bio_list_lock);
729 list_add_tail(&be->list, &io->bio_list);
730 up_write(&io->bio_list_lock);
733 static void del_bio_entry(struct bio_entry *be)
736 kmem_cache_free(bio_entry_slab, be);
739 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
742 struct f2fs_sb_info *sbi = fio->sbi;
747 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
748 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
749 struct list_head *head = &io->bio_list;
750 struct bio_entry *be;
752 down_write(&io->bio_list_lock);
753 list_for_each_entry(be, head, list) {
759 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
762 if (f2fs_crypt_mergeable_bio(*bio,
763 fio->page->mapping->host,
764 fio->page->index, fio) &&
765 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
771 /* page can't be merged into bio; submit the bio */
773 __submit_bio(sbi, *bio, DATA);
776 up_write(&io->bio_list_lock);
787 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
788 struct bio **bio, struct page *page)
792 struct bio *target = bio ? *bio : NULL;
794 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
795 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
796 struct list_head *head = &io->bio_list;
797 struct bio_entry *be;
799 if (list_empty(head))
802 down_read(&io->bio_list_lock);
803 list_for_each_entry(be, head, list) {
805 found = (target == be->bio);
807 found = __has_merged_page(be->bio, NULL,
812 up_read(&io->bio_list_lock);
819 down_write(&io->bio_list_lock);
820 list_for_each_entry(be, head, list) {
822 found = (target == be->bio);
824 found = __has_merged_page(be->bio, NULL,
832 up_write(&io->bio_list_lock);
836 __submit_bio(sbi, target, DATA);
843 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
845 struct bio *bio = *fio->bio;
846 struct page *page = fio->encrypted_page ?
847 fio->encrypted_page : fio->page;
849 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
850 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
851 return -EFSCORRUPTED;
853 trace_f2fs_submit_page_bio(page, fio);
855 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
857 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
860 bio = __bio_alloc(fio, BIO_MAX_PAGES);
861 __attach_io_flag(fio);
862 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
863 fio->page->index, fio, GFP_NOIO);
864 bio_set_op_attrs(bio, fio->op, fio->op_flags);
866 add_bio_entry(fio->sbi, bio, page, fio->temp);
868 if (add_ipu_page(fio, &bio, page))
873 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
875 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
877 *fio->last_block = fio->new_blkaddr;
883 void f2fs_submit_page_write(struct f2fs_io_info *fio)
885 struct f2fs_sb_info *sbi = fio->sbi;
886 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
887 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
888 struct page *bio_page;
890 f2fs_bug_on(sbi, is_read_io(fio->op));
892 down_write(&io->io_rwsem);
895 spin_lock(&io->io_lock);
896 if (list_empty(&io->io_list)) {
897 spin_unlock(&io->io_lock);
900 fio = list_first_entry(&io->io_list,
901 struct f2fs_io_info, list);
902 list_del(&fio->list);
903 spin_unlock(&io->io_lock);
906 verify_fio_blkaddr(fio);
908 if (fio->encrypted_page)
909 bio_page = fio->encrypted_page;
910 else if (fio->compressed_page)
911 bio_page = fio->compressed_page;
913 bio_page = fio->page;
915 /* set submitted = true as a return value */
916 fio->submitted = true;
918 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
921 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
923 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
924 bio_page->index, fio)))
925 __submit_merged_bio(io);
927 if (io->bio == NULL) {
928 if (F2FS_IO_ALIGNED(sbi) &&
929 (fio->type == DATA || fio->type == NODE) &&
930 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
931 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
935 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
936 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
937 bio_page->index, fio, GFP_NOIO);
941 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
942 __submit_merged_bio(io);
947 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
949 io->last_block_in_bio = fio->new_blkaddr;
951 trace_f2fs_submit_page_write(fio->page, fio);
956 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
957 !f2fs_is_checkpoint_ready(sbi))
958 __submit_merged_bio(io);
959 up_write(&io->io_rwsem);
962 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
963 unsigned nr_pages, unsigned op_flag,
964 pgoff_t first_idx, bool for_write)
966 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
968 struct bio_post_read_ctx *ctx;
969 unsigned int post_read_steps = 0;
971 bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
972 min_t(int, nr_pages, BIO_MAX_PAGES),
975 return ERR_PTR(-ENOMEM);
977 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
979 f2fs_target_device(sbi, blkaddr, bio);
980 bio->bi_end_io = f2fs_read_end_io;
981 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
983 if (fscrypt_inode_uses_fs_layer_crypto(inode))
984 post_read_steps |= STEP_DECRYPT;
986 if (f2fs_need_verity(inode, first_idx))
987 post_read_steps |= STEP_VERITY;
990 * STEP_DECOMPRESS is handled specially, since a compressed file might
991 * contain both compressed and uncompressed clusters. We'll allocate a
992 * bio_post_read_ctx if the file is compressed, but the caller is
993 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
996 if (post_read_steps || f2fs_compressed_file(inode)) {
997 /* Due to the mempool, this never fails. */
998 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1001 ctx->enabled_steps = post_read_steps;
1002 bio->bi_private = ctx;
1008 /* This can handle encryption stuffs */
1009 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1010 block_t blkaddr, int op_flags, bool for_write)
1012 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1015 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1016 page->index, for_write);
1018 return PTR_ERR(bio);
1020 /* wait for GCed page writeback via META_MAPPING */
1021 f2fs_wait_on_block_writeback(inode, blkaddr);
1023 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1027 ClearPageError(page);
1028 inc_page_count(sbi, F2FS_RD_DATA);
1029 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1030 __submit_bio(sbi, bio, DATA);
1034 static void __set_data_blkaddr(struct dnode_of_data *dn)
1036 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1040 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1041 base = get_extra_isize(dn->inode);
1043 /* Get physical address of data block */
1044 addr_array = blkaddr_in_node(rn);
1045 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1049 * Lock ordering for the change of data block address:
1052 * update block addresses in the node page
1054 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1056 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1057 __set_data_blkaddr(dn);
1058 if (set_page_dirty(dn->node_page))
1059 dn->node_changed = true;
1062 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1064 dn->data_blkaddr = blkaddr;
1065 f2fs_set_data_blkaddr(dn);
1066 f2fs_update_extent_cache(dn);
1069 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1070 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1072 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1078 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1080 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1083 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1084 dn->ofs_in_node, count);
1086 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1088 for (; count > 0; dn->ofs_in_node++) {
1089 block_t blkaddr = f2fs_data_blkaddr(dn);
1090 if (blkaddr == NULL_ADDR) {
1091 dn->data_blkaddr = NEW_ADDR;
1092 __set_data_blkaddr(dn);
1097 if (set_page_dirty(dn->node_page))
1098 dn->node_changed = true;
1102 /* Should keep dn->ofs_in_node unchanged */
1103 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1105 unsigned int ofs_in_node = dn->ofs_in_node;
1108 ret = f2fs_reserve_new_blocks(dn, 1);
1109 dn->ofs_in_node = ofs_in_node;
1113 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1115 bool need_put = dn->inode_page ? false : true;
1118 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1122 if (dn->data_blkaddr == NULL_ADDR)
1123 err = f2fs_reserve_new_block(dn);
1124 if (err || need_put)
1129 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1131 struct extent_info ei = {0, 0, 0};
1132 struct inode *inode = dn->inode;
1134 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1135 dn->data_blkaddr = ei.blk + index - ei.fofs;
1139 return f2fs_reserve_block(dn, index);
1142 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1143 int op_flags, bool for_write)
1145 struct address_space *mapping = inode->i_mapping;
1146 struct dnode_of_data dn;
1148 struct extent_info ei = {0,0,0};
1151 page = f2fs_grab_cache_page(mapping, index, for_write);
1153 return ERR_PTR(-ENOMEM);
1155 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1156 dn.data_blkaddr = ei.blk + index - ei.fofs;
1157 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1158 DATA_GENERIC_ENHANCE_READ)) {
1159 err = -EFSCORRUPTED;
1165 set_new_dnode(&dn, inode, NULL, NULL, 0);
1166 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1169 f2fs_put_dnode(&dn);
1171 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1175 if (dn.data_blkaddr != NEW_ADDR &&
1176 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1178 DATA_GENERIC_ENHANCE)) {
1179 err = -EFSCORRUPTED;
1183 if (PageUptodate(page)) {
1189 * A new dentry page is allocated but not able to be written, since its
1190 * new inode page couldn't be allocated due to -ENOSPC.
1191 * In such the case, its blkaddr can be remained as NEW_ADDR.
1192 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1193 * f2fs_init_inode_metadata.
1195 if (dn.data_blkaddr == NEW_ADDR) {
1196 zero_user_segment(page, 0, PAGE_SIZE);
1197 if (!PageUptodate(page))
1198 SetPageUptodate(page);
1203 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1204 op_flags, for_write);
1210 f2fs_put_page(page, 1);
1211 return ERR_PTR(err);
1214 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1216 struct address_space *mapping = inode->i_mapping;
1219 page = find_get_page(mapping, index);
1220 if (page && PageUptodate(page))
1222 f2fs_put_page(page, 0);
1224 page = f2fs_get_read_data_page(inode, index, 0, false);
1228 if (PageUptodate(page))
1231 wait_on_page_locked(page);
1232 if (unlikely(!PageUptodate(page))) {
1233 f2fs_put_page(page, 0);
1234 return ERR_PTR(-EIO);
1240 * If it tries to access a hole, return an error.
1241 * Because, the callers, functions in dir.c and GC, should be able to know
1242 * whether this page exists or not.
1244 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1247 struct address_space *mapping = inode->i_mapping;
1250 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1254 /* wait for read completion */
1256 if (unlikely(page->mapping != mapping)) {
1257 f2fs_put_page(page, 1);
1260 if (unlikely(!PageUptodate(page))) {
1261 f2fs_put_page(page, 1);
1262 return ERR_PTR(-EIO);
1268 * Caller ensures that this data page is never allocated.
1269 * A new zero-filled data page is allocated in the page cache.
1271 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1273 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1274 * ipage should be released by this function.
1276 struct page *f2fs_get_new_data_page(struct inode *inode,
1277 struct page *ipage, pgoff_t index, bool new_i_size)
1279 struct address_space *mapping = inode->i_mapping;
1281 struct dnode_of_data dn;
1284 page = f2fs_grab_cache_page(mapping, index, true);
1287 * before exiting, we should make sure ipage will be released
1288 * if any error occur.
1290 f2fs_put_page(ipage, 1);
1291 return ERR_PTR(-ENOMEM);
1294 set_new_dnode(&dn, inode, ipage, NULL, 0);
1295 err = f2fs_reserve_block(&dn, index);
1297 f2fs_put_page(page, 1);
1298 return ERR_PTR(err);
1301 f2fs_put_dnode(&dn);
1303 if (PageUptodate(page))
1306 if (dn.data_blkaddr == NEW_ADDR) {
1307 zero_user_segment(page, 0, PAGE_SIZE);
1308 if (!PageUptodate(page))
1309 SetPageUptodate(page);
1311 f2fs_put_page(page, 1);
1313 /* if ipage exists, blkaddr should be NEW_ADDR */
1314 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1315 page = f2fs_get_lock_data_page(inode, index, true);
1320 if (new_i_size && i_size_read(inode) <
1321 ((loff_t)(index + 1) << PAGE_SHIFT))
1322 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1326 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1328 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1329 struct f2fs_summary sum;
1330 struct node_info ni;
1331 block_t old_blkaddr;
1335 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1338 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1342 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1343 if (dn->data_blkaddr != NULL_ADDR)
1346 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1350 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1351 old_blkaddr = dn->data_blkaddr;
1352 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1353 &sum, seg_type, NULL);
1354 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1355 invalidate_mapping_pages(META_MAPPING(sbi),
1356 old_blkaddr, old_blkaddr);
1357 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1360 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1361 * data from unwritten block via dio_read.
1366 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1368 struct inode *inode = file_inode(iocb->ki_filp);
1369 struct f2fs_map_blocks map;
1372 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1374 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1375 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1376 if (map.m_len > map.m_lblk)
1377 map.m_len -= map.m_lblk;
1381 map.m_next_pgofs = NULL;
1382 map.m_next_extent = NULL;
1383 map.m_seg_type = NO_CHECK_TYPE;
1384 map.m_may_create = true;
1387 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1388 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1389 F2FS_GET_BLOCK_PRE_AIO :
1390 F2FS_GET_BLOCK_PRE_DIO;
1393 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1394 err = f2fs_convert_inline_inode(inode);
1398 if (f2fs_has_inline_data(inode))
1401 flag = F2FS_GET_BLOCK_PRE_AIO;
1404 err = f2fs_map_blocks(inode, &map, 1, flag);
1405 if (map.m_len > 0 && err == -ENOSPC) {
1407 set_inode_flag(inode, FI_NO_PREALLOC);
1413 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1415 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1417 down_read(&sbi->node_change);
1419 up_read(&sbi->node_change);
1424 f2fs_unlock_op(sbi);
1429 * f2fs_map_blocks() tries to find or build mapping relationship which
1430 * maps continuous logical blocks to physical blocks, and return such
1431 * info via f2fs_map_blocks structure.
1433 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1434 int create, int flag)
1436 unsigned int maxblocks = map->m_len;
1437 struct dnode_of_data dn;
1438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1439 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1440 pgoff_t pgofs, end_offset, end;
1441 int err = 0, ofs = 1;
1442 unsigned int ofs_in_node, last_ofs_in_node;
1444 struct extent_info ei = {0,0,0};
1446 unsigned int start_pgofs;
1454 /* it only supports block size == page size */
1455 pgofs = (pgoff_t)map->m_lblk;
1456 end = pgofs + maxblocks;
1458 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1459 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1463 map->m_pblk = ei.blk + pgofs - ei.fofs;
1464 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1465 map->m_flags = F2FS_MAP_MAPPED;
1466 if (map->m_next_extent)
1467 *map->m_next_extent = pgofs + map->m_len;
1469 /* for hardware encryption, but to avoid potential issue in future */
1470 if (flag == F2FS_GET_BLOCK_DIO)
1471 f2fs_wait_on_block_writeback_range(inode,
1472 map->m_pblk, map->m_len);
1477 if (map->m_may_create)
1478 f2fs_do_map_lock(sbi, flag, true);
1480 /* When reading holes, we need its node page */
1481 set_new_dnode(&dn, inode, NULL, NULL, 0);
1482 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1484 if (flag == F2FS_GET_BLOCK_BMAP)
1486 if (err == -ENOENT) {
1488 if (map->m_next_pgofs)
1489 *map->m_next_pgofs =
1490 f2fs_get_next_page_offset(&dn, pgofs);
1491 if (map->m_next_extent)
1492 *map->m_next_extent =
1493 f2fs_get_next_page_offset(&dn, pgofs);
1498 start_pgofs = pgofs;
1500 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1501 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1504 blkaddr = f2fs_data_blkaddr(&dn);
1506 if (__is_valid_data_blkaddr(blkaddr) &&
1507 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1508 err = -EFSCORRUPTED;
1512 if (__is_valid_data_blkaddr(blkaddr)) {
1513 /* use out-place-update for driect IO under LFS mode */
1514 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1515 map->m_may_create) {
1516 err = __allocate_data_block(&dn, map->m_seg_type);
1519 blkaddr = dn.data_blkaddr;
1520 set_inode_flag(inode, FI_APPEND_WRITE);
1524 if (unlikely(f2fs_cp_error(sbi))) {
1528 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1529 if (blkaddr == NULL_ADDR) {
1531 last_ofs_in_node = dn.ofs_in_node;
1534 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1535 flag != F2FS_GET_BLOCK_DIO);
1536 err = __allocate_data_block(&dn,
1539 set_inode_flag(inode, FI_APPEND_WRITE);
1543 map->m_flags |= F2FS_MAP_NEW;
1544 blkaddr = dn.data_blkaddr;
1546 if (flag == F2FS_GET_BLOCK_BMAP) {
1550 if (flag == F2FS_GET_BLOCK_PRECACHE)
1552 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1553 blkaddr == NULL_ADDR) {
1554 if (map->m_next_pgofs)
1555 *map->m_next_pgofs = pgofs + 1;
1558 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1559 /* for defragment case */
1560 if (map->m_next_pgofs)
1561 *map->m_next_pgofs = pgofs + 1;
1567 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1570 if (map->m_len == 0) {
1571 /* preallocated unwritten block should be mapped for fiemap. */
1572 if (blkaddr == NEW_ADDR)
1573 map->m_flags |= F2FS_MAP_UNWRITTEN;
1574 map->m_flags |= F2FS_MAP_MAPPED;
1576 map->m_pblk = blkaddr;
1578 } else if ((map->m_pblk != NEW_ADDR &&
1579 blkaddr == (map->m_pblk + ofs)) ||
1580 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1581 flag == F2FS_GET_BLOCK_PRE_DIO) {
1592 /* preallocate blocks in batch for one dnode page */
1593 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1594 (pgofs == end || dn.ofs_in_node == end_offset)) {
1596 dn.ofs_in_node = ofs_in_node;
1597 err = f2fs_reserve_new_blocks(&dn, prealloc);
1601 map->m_len += dn.ofs_in_node - ofs_in_node;
1602 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1606 dn.ofs_in_node = end_offset;
1611 else if (dn.ofs_in_node < end_offset)
1614 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1615 if (map->m_flags & F2FS_MAP_MAPPED) {
1616 unsigned int ofs = start_pgofs - map->m_lblk;
1618 f2fs_update_extent_cache_range(&dn,
1619 start_pgofs, map->m_pblk + ofs,
1624 f2fs_put_dnode(&dn);
1626 if (map->m_may_create) {
1627 f2fs_do_map_lock(sbi, flag, false);
1628 f2fs_balance_fs(sbi, dn.node_changed);
1634 /* for hardware encryption, but to avoid potential issue in future */
1635 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1636 f2fs_wait_on_block_writeback_range(inode,
1637 map->m_pblk, map->m_len);
1639 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1640 if (map->m_flags & F2FS_MAP_MAPPED) {
1641 unsigned int ofs = start_pgofs - map->m_lblk;
1643 f2fs_update_extent_cache_range(&dn,
1644 start_pgofs, map->m_pblk + ofs,
1647 if (map->m_next_extent)
1648 *map->m_next_extent = pgofs + 1;
1650 f2fs_put_dnode(&dn);
1652 if (map->m_may_create) {
1653 f2fs_do_map_lock(sbi, flag, false);
1654 f2fs_balance_fs(sbi, dn.node_changed);
1657 trace_f2fs_map_blocks(inode, map, err);
1661 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1663 struct f2fs_map_blocks map;
1667 if (pos + len > i_size_read(inode))
1670 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1671 map.m_next_pgofs = NULL;
1672 map.m_next_extent = NULL;
1673 map.m_seg_type = NO_CHECK_TYPE;
1674 map.m_may_create = false;
1675 last_lblk = F2FS_BLK_ALIGN(pos + len);
1677 while (map.m_lblk < last_lblk) {
1678 map.m_len = last_lblk - map.m_lblk;
1679 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1680 if (err || map.m_len == 0)
1682 map.m_lblk += map.m_len;
1687 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1689 return (bytes >> inode->i_blkbits);
1692 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1694 return (blks << inode->i_blkbits);
1697 static int __get_data_block(struct inode *inode, sector_t iblock,
1698 struct buffer_head *bh, int create, int flag,
1699 pgoff_t *next_pgofs, int seg_type, bool may_write)
1701 struct f2fs_map_blocks map;
1704 map.m_lblk = iblock;
1705 map.m_len = bytes_to_blks(inode, bh->b_size);
1706 map.m_next_pgofs = next_pgofs;
1707 map.m_next_extent = NULL;
1708 map.m_seg_type = seg_type;
1709 map.m_may_create = may_write;
1711 err = f2fs_map_blocks(inode, &map, create, flag);
1713 map_bh(bh, inode->i_sb, map.m_pblk);
1714 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1715 bh->b_size = blks_to_bytes(inode, map.m_len);
1720 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1721 struct buffer_head *bh_result, int create)
1723 return __get_data_block(inode, iblock, bh_result, create,
1724 F2FS_GET_BLOCK_DIO, NULL,
1725 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1726 IS_SWAPFILE(inode) ? false : true);
1729 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1730 struct buffer_head *bh_result, int create)
1732 return __get_data_block(inode, iblock, bh_result, create,
1733 F2FS_GET_BLOCK_DIO, NULL,
1734 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1738 static int f2fs_xattr_fiemap(struct inode *inode,
1739 struct fiemap_extent_info *fieinfo)
1741 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1743 struct node_info ni;
1744 __u64 phys = 0, len;
1746 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1749 if (f2fs_has_inline_xattr(inode)) {
1752 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1753 inode->i_ino, false);
1757 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1759 f2fs_put_page(page, 1);
1763 phys = blks_to_bytes(inode, ni.blk_addr);
1764 offset = offsetof(struct f2fs_inode, i_addr) +
1765 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1766 get_inline_xattr_addrs(inode));
1769 len = inline_xattr_size(inode);
1771 f2fs_put_page(page, 1);
1773 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1776 flags |= FIEMAP_EXTENT_LAST;
1778 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1779 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1780 if (err || err == 1)
1785 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1789 err = f2fs_get_node_info(sbi, xnid, &ni);
1791 f2fs_put_page(page, 1);
1795 phys = blks_to_bytes(inode, ni.blk_addr);
1796 len = inode->i_sb->s_blocksize;
1798 f2fs_put_page(page, 1);
1800 flags = FIEMAP_EXTENT_LAST;
1804 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1805 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1808 return (err < 0 ? err : 0);
1811 static loff_t max_inode_blocks(struct inode *inode)
1813 loff_t result = ADDRS_PER_INODE(inode);
1814 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1816 /* two direct node blocks */
1817 result += (leaf_count * 2);
1819 /* two indirect node blocks */
1820 leaf_count *= NIDS_PER_BLOCK;
1821 result += (leaf_count * 2);
1823 /* one double indirect node block */
1824 leaf_count *= NIDS_PER_BLOCK;
1825 result += leaf_count;
1830 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1833 struct f2fs_map_blocks map;
1834 sector_t start_blk, last_blk;
1836 u64 logical = 0, phys = 0, size = 0;
1839 bool compr_cluster = false;
1840 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1842 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1843 ret = f2fs_precache_extents(inode);
1848 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1854 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1855 ret = f2fs_xattr_fiemap(inode, fieinfo);
1859 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1860 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1865 if (bytes_to_blks(inode, len) == 0)
1866 len = blks_to_bytes(inode, 1);
1868 start_blk = bytes_to_blks(inode, start);
1869 last_blk = bytes_to_blks(inode, start + len - 1);
1872 memset(&map, 0, sizeof(map));
1873 map.m_lblk = start_blk;
1874 map.m_len = bytes_to_blks(inode, len);
1875 map.m_next_pgofs = &next_pgofs;
1876 map.m_seg_type = NO_CHECK_TYPE;
1879 map.m_len = cluster_size - 1;
1881 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1886 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1887 start_blk = next_pgofs;
1889 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1890 max_inode_blocks(inode)))
1893 flags |= FIEMAP_EXTENT_LAST;
1897 flags |= FIEMAP_EXTENT_MERGED;
1898 if (IS_ENCRYPTED(inode))
1899 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1901 ret = fiemap_fill_next_extent(fieinfo, logical,
1903 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1909 if (start_blk > last_blk)
1912 if (compr_cluster) {
1913 compr_cluster = false;
1916 logical = blks_to_bytes(inode, start_blk - 1);
1917 phys = blks_to_bytes(inode, map.m_pblk);
1918 size = blks_to_bytes(inode, cluster_size);
1920 flags |= FIEMAP_EXTENT_ENCODED;
1922 start_blk += cluster_size - 1;
1924 if (start_blk > last_blk)
1930 if (map.m_pblk == COMPRESS_ADDR) {
1931 compr_cluster = true;
1936 logical = blks_to_bytes(inode, start_blk);
1937 phys = blks_to_bytes(inode, map.m_pblk);
1938 size = blks_to_bytes(inode, map.m_len);
1940 if (map.m_flags & F2FS_MAP_UNWRITTEN)
1941 flags = FIEMAP_EXTENT_UNWRITTEN;
1943 start_blk += bytes_to_blks(inode, size);
1947 if (fatal_signal_pending(current))
1955 inode_unlock(inode);
1959 static inline loff_t f2fs_readpage_limit(struct inode *inode)
1961 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1962 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1963 return inode->i_sb->s_maxbytes;
1965 return i_size_read(inode);
1968 static int f2fs_read_single_page(struct inode *inode, struct page *page,
1970 struct f2fs_map_blocks *map,
1971 struct bio **bio_ret,
1972 sector_t *last_block_in_bio,
1975 struct bio *bio = *bio_ret;
1976 const unsigned blocksize = blks_to_bytes(inode, 1);
1977 sector_t block_in_file;
1978 sector_t last_block;
1979 sector_t last_block_in_file;
1983 block_in_file = (sector_t)page_index(page);
1984 last_block = block_in_file + nr_pages;
1985 last_block_in_file = bytes_to_blks(inode,
1986 f2fs_readpage_limit(inode) + blocksize - 1);
1987 if (last_block > last_block_in_file)
1988 last_block = last_block_in_file;
1990 /* just zeroing out page which is beyond EOF */
1991 if (block_in_file >= last_block)
1994 * Map blocks using the previous result first.
1996 if ((map->m_flags & F2FS_MAP_MAPPED) &&
1997 block_in_file > map->m_lblk &&
1998 block_in_file < (map->m_lblk + map->m_len))
2002 * Then do more f2fs_map_blocks() calls until we are
2003 * done with this page.
2005 map->m_lblk = block_in_file;
2006 map->m_len = last_block - block_in_file;
2008 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2012 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2013 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2014 SetPageMappedToDisk(page);
2016 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2017 !cleancache_get_page(page))) {
2018 SetPageUptodate(page);
2022 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2023 DATA_GENERIC_ENHANCE_READ)) {
2024 ret = -EFSCORRUPTED;
2029 zero_user_segment(page, 0, PAGE_SIZE);
2030 if (f2fs_need_verity(inode, page->index) &&
2031 !fsverity_verify_page(page)) {
2035 if (!PageUptodate(page))
2036 SetPageUptodate(page);
2042 * This page will go to BIO. Do we need to send this
2045 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2046 *last_block_in_bio, block_nr) ||
2047 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2049 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2053 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2054 is_readahead ? REQ_RAHEAD : 0, page->index,
2064 * If the page is under writeback, we need to wait for
2065 * its completion to see the correct decrypted data.
2067 f2fs_wait_on_block_writeback(inode, block_nr);
2069 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2070 goto submit_and_realloc;
2072 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2073 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2074 ClearPageError(page);
2075 *last_block_in_bio = block_nr;
2079 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2088 #ifdef CONFIG_F2FS_FS_COMPRESSION
2089 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2090 unsigned nr_pages, sector_t *last_block_in_bio,
2091 bool is_readahead, bool for_write)
2093 struct dnode_of_data dn;
2094 struct inode *inode = cc->inode;
2095 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2096 struct bio *bio = *bio_ret;
2097 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2098 sector_t last_block_in_file;
2099 const unsigned blocksize = blks_to_bytes(inode, 1);
2100 struct decompress_io_ctx *dic = NULL;
2104 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2106 last_block_in_file = bytes_to_blks(inode,
2107 f2fs_readpage_limit(inode) + blocksize - 1);
2109 /* get rid of pages beyond EOF */
2110 for (i = 0; i < cc->cluster_size; i++) {
2111 struct page *page = cc->rpages[i];
2115 if ((sector_t)page->index >= last_block_in_file) {
2116 zero_user_segment(page, 0, PAGE_SIZE);
2117 if (!PageUptodate(page))
2118 SetPageUptodate(page);
2119 } else if (!PageUptodate(page)) {
2123 cc->rpages[i] = NULL;
2127 /* we are done since all pages are beyond EOF */
2128 if (f2fs_cluster_is_empty(cc))
2131 set_new_dnode(&dn, inode, NULL, NULL, 0);
2132 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2136 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2138 for (i = 1; i < cc->cluster_size; i++) {
2141 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2142 dn.ofs_in_node + i);
2144 if (!__is_valid_data_blkaddr(blkaddr))
2147 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2154 /* nothing to decompress */
2155 if (cc->nr_cpages == 0) {
2160 dic = f2fs_alloc_dic(cc);
2166 for (i = 0; i < dic->nr_cpages; i++) {
2167 struct page *page = dic->cpages[i];
2169 struct bio_post_read_ctx *ctx;
2171 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2172 dn.ofs_in_node + i + 1);
2174 if (bio && (!page_is_mergeable(sbi, bio,
2175 *last_block_in_bio, blkaddr) ||
2176 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2178 __submit_bio(sbi, bio, DATA);
2183 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2184 is_readahead ? REQ_RAHEAD : 0,
2185 page->index, for_write);
2188 f2fs_decompress_end_io(dic, ret);
2189 f2fs_put_dnode(&dn);
2195 f2fs_wait_on_block_writeback(inode, blkaddr);
2197 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2198 goto submit_and_realloc;
2200 ctx = bio->bi_private;
2201 ctx->enabled_steps |= STEP_DECOMPRESS;
2202 refcount_inc(&dic->refcnt);
2204 inc_page_count(sbi, F2FS_RD_DATA);
2205 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2206 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2207 ClearPageError(page);
2208 *last_block_in_bio = blkaddr;
2211 f2fs_put_dnode(&dn);
2217 f2fs_put_dnode(&dn);
2219 for (i = 0; i < cc->cluster_size; i++) {
2220 if (cc->rpages[i]) {
2221 ClearPageUptodate(cc->rpages[i]);
2222 ClearPageError(cc->rpages[i]);
2223 unlock_page(cc->rpages[i]);
2232 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2233 * Major change was from block_size == page_size in f2fs by default.
2235 static int f2fs_mpage_readpages(struct inode *inode,
2236 struct readahead_control *rac, struct page *page)
2238 struct bio *bio = NULL;
2239 sector_t last_block_in_bio = 0;
2240 struct f2fs_map_blocks map;
2241 #ifdef CONFIG_F2FS_FS_COMPRESSION
2242 struct compress_ctx cc = {
2244 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2245 .cluster_size = F2FS_I(inode)->i_cluster_size,
2246 .cluster_idx = NULL_CLUSTER,
2253 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2254 unsigned max_nr_pages = nr_pages;
2261 map.m_next_pgofs = NULL;
2262 map.m_next_extent = NULL;
2263 map.m_seg_type = NO_CHECK_TYPE;
2264 map.m_may_create = false;
2266 for (; nr_pages; nr_pages--) {
2268 page = readahead_page(rac);
2269 prefetchw(&page->flags);
2272 #ifdef CONFIG_F2FS_FS_COMPRESSION
2273 if (f2fs_compressed_file(inode)) {
2274 /* there are remained comressed pages, submit them */
2275 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2276 ret = f2fs_read_multi_pages(&cc, &bio,
2279 rac != NULL, false);
2280 f2fs_destroy_compress_ctx(&cc);
2282 goto set_error_page;
2284 ret = f2fs_is_compressed_cluster(inode, page->index);
2286 goto set_error_page;
2288 goto read_single_page;
2290 ret = f2fs_init_compress_ctx(&cc);
2292 goto set_error_page;
2294 f2fs_compress_ctx_add_page(&cc, page);
2301 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2302 &bio, &last_block_in_bio, rac);
2304 #ifdef CONFIG_F2FS_FS_COMPRESSION
2308 zero_user_segment(page, 0, PAGE_SIZE);
2311 #ifdef CONFIG_F2FS_FS_COMPRESSION
2317 #ifdef CONFIG_F2FS_FS_COMPRESSION
2318 if (f2fs_compressed_file(inode)) {
2320 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2321 ret = f2fs_read_multi_pages(&cc, &bio,
2324 rac != NULL, false);
2325 f2fs_destroy_compress_ctx(&cc);
2331 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2335 static int f2fs_read_data_page(struct file *file, struct page *page)
2337 struct inode *inode = page_file_mapping(page)->host;
2340 trace_f2fs_readpage(page, DATA);
2342 if (!f2fs_is_compress_backend_ready(inode)) {
2347 /* If the file has inline data, try to read it directly */
2348 if (f2fs_has_inline_data(inode))
2349 ret = f2fs_read_inline_data(inode, page);
2351 ret = f2fs_mpage_readpages(inode, NULL, page);
2355 static void f2fs_readahead(struct readahead_control *rac)
2357 struct inode *inode = rac->mapping->host;
2359 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2361 if (!f2fs_is_compress_backend_ready(inode))
2364 /* If the file has inline data, skip readpages */
2365 if (f2fs_has_inline_data(inode))
2368 f2fs_mpage_readpages(inode, rac, NULL);
2371 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2373 struct inode *inode = fio->page->mapping->host;
2374 struct page *mpage, *page;
2375 gfp_t gfp_flags = GFP_NOFS;
2377 if (!f2fs_encrypted_file(inode))
2380 page = fio->compressed_page ? fio->compressed_page : fio->page;
2382 /* wait for GCed page writeback via META_MAPPING */
2383 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2385 if (fscrypt_inode_uses_inline_crypto(inode))
2389 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2390 PAGE_SIZE, 0, gfp_flags);
2391 if (IS_ERR(fio->encrypted_page)) {
2392 /* flush pending IOs and wait for a while in the ENOMEM case */
2393 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2394 f2fs_flush_merged_writes(fio->sbi);
2395 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2396 gfp_flags |= __GFP_NOFAIL;
2399 return PTR_ERR(fio->encrypted_page);
2402 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2404 if (PageUptodate(mpage))
2405 memcpy(page_address(mpage),
2406 page_address(fio->encrypted_page), PAGE_SIZE);
2407 f2fs_put_page(mpage, 1);
2412 static inline bool check_inplace_update_policy(struct inode *inode,
2413 struct f2fs_io_info *fio)
2415 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2416 unsigned int policy = SM_I(sbi)->ipu_policy;
2418 if (policy & (0x1 << F2FS_IPU_FORCE))
2420 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2422 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2423 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2425 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2426 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2430 * IPU for rewrite async pages
2432 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2433 fio && fio->op == REQ_OP_WRITE &&
2434 !(fio->op_flags & REQ_SYNC) &&
2435 !IS_ENCRYPTED(inode))
2438 /* this is only set during fdatasync */
2439 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2440 is_inode_flag_set(inode, FI_NEED_IPU))
2443 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2444 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2450 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2452 if (f2fs_is_pinned_file(inode))
2455 /* if this is cold file, we should overwrite to avoid fragmentation */
2456 if (file_is_cold(inode))
2459 return check_inplace_update_policy(inode, fio);
2462 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2464 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2466 if (f2fs_lfs_mode(sbi))
2468 if (S_ISDIR(inode->i_mode))
2470 if (IS_NOQUOTA(inode))
2472 if (f2fs_is_atomic_file(inode))
2475 if (is_cold_data(fio->page))
2477 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2479 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2480 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2486 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2488 struct inode *inode = fio->page->mapping->host;
2490 if (f2fs_should_update_outplace(inode, fio))
2493 return f2fs_should_update_inplace(inode, fio);
2496 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2498 struct page *page = fio->page;
2499 struct inode *inode = page->mapping->host;
2500 struct dnode_of_data dn;
2501 struct extent_info ei = {0,0,0};
2502 struct node_info ni;
2503 bool ipu_force = false;
2506 set_new_dnode(&dn, inode, NULL, NULL, 0);
2507 if (need_inplace_update(fio) &&
2508 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2509 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2511 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2512 DATA_GENERIC_ENHANCE))
2513 return -EFSCORRUPTED;
2516 fio->need_lock = LOCK_DONE;
2520 /* Deadlock due to between page->lock and f2fs_lock_op */
2521 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2524 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2528 fio->old_blkaddr = dn.data_blkaddr;
2530 /* This page is already truncated */
2531 if (fio->old_blkaddr == NULL_ADDR) {
2532 ClearPageUptodate(page);
2533 clear_cold_data(page);
2537 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2538 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2539 DATA_GENERIC_ENHANCE)) {
2540 err = -EFSCORRUPTED;
2544 * If current allocation needs SSR,
2545 * it had better in-place writes for updated data.
2548 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2549 need_inplace_update(fio))) {
2550 err = f2fs_encrypt_one_page(fio);
2554 set_page_writeback(page);
2555 ClearPageError(page);
2556 f2fs_put_dnode(&dn);
2557 if (fio->need_lock == LOCK_REQ)
2558 f2fs_unlock_op(fio->sbi);
2559 err = f2fs_inplace_write_data(fio);
2561 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2562 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2563 if (PageWriteback(page))
2564 end_page_writeback(page);
2566 set_inode_flag(inode, FI_UPDATE_WRITE);
2568 trace_f2fs_do_write_data_page(fio->page, IPU);
2572 if (fio->need_lock == LOCK_RETRY) {
2573 if (!f2fs_trylock_op(fio->sbi)) {
2577 fio->need_lock = LOCK_REQ;
2580 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2584 fio->version = ni.version;
2586 err = f2fs_encrypt_one_page(fio);
2590 set_page_writeback(page);
2591 ClearPageError(page);
2593 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2594 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2596 /* LFS mode write path */
2597 f2fs_outplace_write_data(&dn, fio);
2598 trace_f2fs_do_write_data_page(page, OPU);
2599 set_inode_flag(inode, FI_APPEND_WRITE);
2600 if (page->index == 0)
2601 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2603 f2fs_put_dnode(&dn);
2605 if (fio->need_lock == LOCK_REQ)
2606 f2fs_unlock_op(fio->sbi);
2610 int f2fs_write_single_data_page(struct page *page, int *submitted,
2612 sector_t *last_block,
2613 struct writeback_control *wbc,
2614 enum iostat_type io_type,
2618 struct inode *inode = page->mapping->host;
2619 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2620 loff_t i_size = i_size_read(inode);
2621 const pgoff_t end_index = ((unsigned long long)i_size)
2623 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2624 unsigned offset = 0;
2625 bool need_balance_fs = false;
2627 struct f2fs_io_info fio = {
2629 .ino = inode->i_ino,
2632 .op_flags = wbc_to_write_flags(wbc),
2633 .old_blkaddr = NULL_ADDR,
2635 .encrypted_page = NULL,
2637 .compr_blocks = compr_blocks,
2638 .need_lock = LOCK_RETRY,
2642 .last_block = last_block,
2645 trace_f2fs_writepage(page, DATA);
2647 /* we should bypass data pages to proceed the kworkder jobs */
2648 if (unlikely(f2fs_cp_error(sbi))) {
2649 mapping_set_error(page->mapping, -EIO);
2651 * don't drop any dirty dentry pages for keeping lastest
2652 * directory structure.
2654 if (S_ISDIR(inode->i_mode))
2659 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2662 if (page->index < end_index ||
2663 f2fs_verity_in_progress(inode) ||
2668 * If the offset is out-of-range of file size,
2669 * this page does not have to be written to disk.
2671 offset = i_size & (PAGE_SIZE - 1);
2672 if ((page->index >= end_index + 1) || !offset)
2675 zero_user_segment(page, offset, PAGE_SIZE);
2677 if (f2fs_is_drop_cache(inode))
2679 /* we should not write 0'th page having journal header */
2680 if (f2fs_is_volatile_file(inode) && (!page->index ||
2681 (!wbc->for_reclaim &&
2682 f2fs_available_free_memory(sbi, BASE_CHECK))))
2685 /* Dentry/quota blocks are controlled by checkpoint */
2686 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2688 * We need to wait for node_write to avoid block allocation during
2689 * checkpoint. This can only happen to quota writes which can cause
2690 * the below discard race condition.
2692 if (IS_NOQUOTA(inode))
2693 down_read(&sbi->node_write);
2695 fio.need_lock = LOCK_DONE;
2696 err = f2fs_do_write_data_page(&fio);
2698 if (IS_NOQUOTA(inode))
2699 up_read(&sbi->node_write);
2704 if (!wbc->for_reclaim)
2705 need_balance_fs = true;
2706 else if (has_not_enough_free_secs(sbi, 0, 0))
2709 set_inode_flag(inode, FI_HOT_DATA);
2712 if (f2fs_has_inline_data(inode)) {
2713 err = f2fs_write_inline_data(inode, page);
2718 if (err == -EAGAIN) {
2719 err = f2fs_do_write_data_page(&fio);
2720 if (err == -EAGAIN) {
2721 fio.need_lock = LOCK_REQ;
2722 err = f2fs_do_write_data_page(&fio);
2727 file_set_keep_isize(inode);
2729 spin_lock(&F2FS_I(inode)->i_size_lock);
2730 if (F2FS_I(inode)->last_disk_size < psize)
2731 F2FS_I(inode)->last_disk_size = psize;
2732 spin_unlock(&F2FS_I(inode)->i_size_lock);
2736 if (err && err != -ENOENT)
2740 inode_dec_dirty_pages(inode);
2742 ClearPageUptodate(page);
2743 clear_cold_data(page);
2746 if (wbc->for_reclaim) {
2747 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2748 clear_inode_flag(inode, FI_HOT_DATA);
2749 f2fs_remove_dirty_inode(inode);
2753 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2754 !F2FS_I(inode)->cp_task && allow_balance)
2755 f2fs_balance_fs(sbi, need_balance_fs);
2757 if (unlikely(f2fs_cp_error(sbi))) {
2758 f2fs_submit_merged_write(sbi, DATA);
2759 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2764 *submitted = fio.submitted ? 1 : 0;
2769 redirty_page_for_writepage(wbc, page);
2771 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2772 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2773 * file_write_and_wait_range() will see EIO error, which is critical
2774 * to return value of fsync() followed by atomic_write failure to user.
2776 if (!err || wbc->for_reclaim)
2777 return AOP_WRITEPAGE_ACTIVATE;
2782 static int f2fs_write_data_page(struct page *page,
2783 struct writeback_control *wbc)
2785 #ifdef CONFIG_F2FS_FS_COMPRESSION
2786 struct inode *inode = page->mapping->host;
2788 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2791 if (f2fs_compressed_file(inode)) {
2792 if (f2fs_is_compressed_cluster(inode, page->index)) {
2793 redirty_page_for_writepage(wbc, page);
2794 return AOP_WRITEPAGE_ACTIVATE;
2800 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2801 wbc, FS_DATA_IO, 0, true);
2805 * This function was copied from write_cche_pages from mm/page-writeback.c.
2806 * The major change is making write step of cold data page separately from
2807 * warm/hot data page.
2809 static int f2fs_write_cache_pages(struct address_space *mapping,
2810 struct writeback_control *wbc,
2811 enum iostat_type io_type)
2814 int done = 0, retry = 0;
2815 struct pagevec pvec;
2816 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2817 struct bio *bio = NULL;
2818 sector_t last_block;
2819 #ifdef CONFIG_F2FS_FS_COMPRESSION
2820 struct inode *inode = mapping->host;
2821 struct compress_ctx cc = {
2823 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2824 .cluster_size = F2FS_I(inode)->i_cluster_size,
2825 .cluster_idx = NULL_CLUSTER,
2831 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2837 pgoff_t end; /* Inclusive */
2839 int range_whole = 0;
2845 pagevec_init(&pvec);
2847 if (get_dirty_pages(mapping->host) <=
2848 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2849 set_inode_flag(mapping->host, FI_HOT_DATA);
2851 clear_inode_flag(mapping->host, FI_HOT_DATA);
2853 if (wbc->range_cyclic) {
2854 index = mapping->writeback_index; /* prev offset */
2857 index = wbc->range_start >> PAGE_SHIFT;
2858 end = wbc->range_end >> PAGE_SHIFT;
2859 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2862 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2863 tag = PAGECACHE_TAG_TOWRITE;
2865 tag = PAGECACHE_TAG_DIRTY;
2868 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2869 tag_pages_for_writeback(mapping, index, end);
2871 while (!done && !retry && (index <= end)) {
2872 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2877 for (i = 0; i < nr_pages; i++) {
2878 struct page *page = pvec.pages[i];
2882 #ifdef CONFIG_F2FS_FS_COMPRESSION
2883 if (f2fs_compressed_file(inode)) {
2884 ret = f2fs_init_compress_ctx(&cc);
2890 if (!f2fs_cluster_can_merge_page(&cc,
2892 ret = f2fs_write_multi_pages(&cc,
2893 &submitted, wbc, io_type);
2899 if (unlikely(f2fs_cp_error(sbi)))
2902 if (f2fs_cluster_is_empty(&cc)) {
2903 void *fsdata = NULL;
2907 ret2 = f2fs_prepare_compress_overwrite(
2909 page->index, &fsdata);
2915 !f2fs_compress_write_end(inode,
2916 fsdata, page->index,
2926 /* give a priority to WB_SYNC threads */
2927 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2928 wbc->sync_mode == WB_SYNC_NONE) {
2932 #ifdef CONFIG_F2FS_FS_COMPRESSION
2935 done_index = page->index;
2939 if (unlikely(page->mapping != mapping)) {
2945 if (!PageDirty(page)) {
2946 /* someone wrote it for us */
2947 goto continue_unlock;
2950 if (PageWriteback(page)) {
2951 if (wbc->sync_mode != WB_SYNC_NONE)
2952 f2fs_wait_on_page_writeback(page,
2955 goto continue_unlock;
2958 if (!clear_page_dirty_for_io(page))
2959 goto continue_unlock;
2961 #ifdef CONFIG_F2FS_FS_COMPRESSION
2962 if (f2fs_compressed_file(inode)) {
2964 f2fs_compress_ctx_add_page(&cc, page);
2968 ret = f2fs_write_single_data_page(page, &submitted,
2969 &bio, &last_block, wbc, io_type,
2971 if (ret == AOP_WRITEPAGE_ACTIVATE)
2973 #ifdef CONFIG_F2FS_FS_COMPRESSION
2976 nwritten += submitted;
2977 wbc->nr_to_write -= submitted;
2979 if (unlikely(ret)) {
2981 * keep nr_to_write, since vfs uses this to
2982 * get # of written pages.
2984 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2987 } else if (ret == -EAGAIN) {
2989 if (wbc->sync_mode == WB_SYNC_ALL) {
2991 congestion_wait(BLK_RW_ASYNC,
2992 DEFAULT_IO_TIMEOUT);
2997 done_index = page->index + 1;
3002 if (wbc->nr_to_write <= 0 &&
3003 wbc->sync_mode == WB_SYNC_NONE) {
3011 pagevec_release(&pvec);
3014 #ifdef CONFIG_F2FS_FS_COMPRESSION
3015 /* flush remained pages in compress cluster */
3016 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3017 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3018 nwritten += submitted;
3019 wbc->nr_to_write -= submitted;
3025 if (f2fs_compressed_file(inode))
3026 f2fs_destroy_compress_ctx(&cc);
3033 if (wbc->range_cyclic && !done)
3035 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3036 mapping->writeback_index = done_index;
3039 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3041 /* submit cached bio of IPU write */
3043 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3048 static inline bool __should_serialize_io(struct inode *inode,
3049 struct writeback_control *wbc)
3051 /* to avoid deadlock in path of data flush */
3052 if (F2FS_I(inode)->cp_task)
3055 if (!S_ISREG(inode->i_mode))
3057 if (IS_NOQUOTA(inode))
3060 if (f2fs_need_compress_data(inode))
3062 if (wbc->sync_mode != WB_SYNC_ALL)
3064 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3069 static int __f2fs_write_data_pages(struct address_space *mapping,
3070 struct writeback_control *wbc,
3071 enum iostat_type io_type)
3073 struct inode *inode = mapping->host;
3074 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3075 struct blk_plug plug;
3077 bool locked = false;
3079 /* deal with chardevs and other special file */
3080 if (!mapping->a_ops->writepage)
3083 /* skip writing if there is no dirty page in this inode */
3084 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3087 /* during POR, we don't need to trigger writepage at all. */
3088 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3091 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3092 wbc->sync_mode == WB_SYNC_NONE &&
3093 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3094 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3097 /* skip writing during file defragment */
3098 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3101 trace_f2fs_writepages(mapping->host, wbc, DATA);
3103 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3104 if (wbc->sync_mode == WB_SYNC_ALL)
3105 atomic_inc(&sbi->wb_sync_req[DATA]);
3106 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3109 if (__should_serialize_io(inode, wbc)) {
3110 mutex_lock(&sbi->writepages);
3114 blk_start_plug(&plug);
3115 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3116 blk_finish_plug(&plug);
3119 mutex_unlock(&sbi->writepages);
3121 if (wbc->sync_mode == WB_SYNC_ALL)
3122 atomic_dec(&sbi->wb_sync_req[DATA]);
3124 * if some pages were truncated, we cannot guarantee its mapping->host
3125 * to detect pending bios.
3128 f2fs_remove_dirty_inode(inode);
3132 wbc->pages_skipped += get_dirty_pages(inode);
3133 trace_f2fs_writepages(mapping->host, wbc, DATA);
3137 static int f2fs_write_data_pages(struct address_space *mapping,
3138 struct writeback_control *wbc)
3140 struct inode *inode = mapping->host;
3142 return __f2fs_write_data_pages(mapping, wbc,
3143 F2FS_I(inode)->cp_task == current ?
3144 FS_CP_DATA_IO : FS_DATA_IO);
3147 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3149 struct inode *inode = mapping->host;
3150 loff_t i_size = i_size_read(inode);
3152 if (IS_NOQUOTA(inode))
3155 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3156 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3157 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3158 down_write(&F2FS_I(inode)->i_mmap_sem);
3160 truncate_pagecache(inode, i_size);
3161 f2fs_truncate_blocks(inode, i_size, true);
3163 up_write(&F2FS_I(inode)->i_mmap_sem);
3164 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3168 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3169 struct page *page, loff_t pos, unsigned len,
3170 block_t *blk_addr, bool *node_changed)
3172 struct inode *inode = page->mapping->host;
3173 pgoff_t index = page->index;
3174 struct dnode_of_data dn;
3176 bool locked = false;
3177 struct extent_info ei = {0,0,0};
3182 * we already allocated all the blocks, so we don't need to get
3183 * the block addresses when there is no need to fill the page.
3185 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3186 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3187 !f2fs_verity_in_progress(inode))
3190 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3191 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3192 flag = F2FS_GET_BLOCK_DEFAULT;
3194 flag = F2FS_GET_BLOCK_PRE_AIO;
3196 if (f2fs_has_inline_data(inode) ||
3197 (pos & PAGE_MASK) >= i_size_read(inode)) {
3198 f2fs_do_map_lock(sbi, flag, true);
3203 /* check inline_data */
3204 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3205 if (IS_ERR(ipage)) {
3206 err = PTR_ERR(ipage);
3210 set_new_dnode(&dn, inode, ipage, ipage, 0);
3212 if (f2fs_has_inline_data(inode)) {
3213 if (pos + len <= MAX_INLINE_DATA(inode)) {
3214 f2fs_do_read_inline_data(page, ipage);
3215 set_inode_flag(inode, FI_DATA_EXIST);
3217 set_inline_node(ipage);
3219 err = f2fs_convert_inline_page(&dn, page);
3222 if (dn.data_blkaddr == NULL_ADDR)
3223 err = f2fs_get_block(&dn, index);
3225 } else if (locked) {
3226 err = f2fs_get_block(&dn, index);
3228 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3229 dn.data_blkaddr = ei.blk + index - ei.fofs;
3232 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3233 if (err || dn.data_blkaddr == NULL_ADDR) {
3234 f2fs_put_dnode(&dn);
3235 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3237 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3244 /* convert_inline_page can make node_changed */
3245 *blk_addr = dn.data_blkaddr;
3246 *node_changed = dn.node_changed;
3248 f2fs_put_dnode(&dn);
3251 f2fs_do_map_lock(sbi, flag, false);
3255 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3256 loff_t pos, unsigned len, unsigned flags,
3257 struct page **pagep, void **fsdata)
3259 struct inode *inode = mapping->host;
3260 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3261 struct page *page = NULL;
3262 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3263 bool need_balance = false, drop_atomic = false;
3264 block_t blkaddr = NULL_ADDR;
3267 trace_f2fs_write_begin(inode, pos, len, flags);
3269 if (!f2fs_is_checkpoint_ready(sbi)) {
3274 if ((f2fs_is_atomic_file(inode) &&
3275 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3276 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3283 * We should check this at this moment to avoid deadlock on inode page
3284 * and #0 page. The locking rule for inline_data conversion should be:
3285 * lock_page(page #0) -> lock_page(inode_page)
3288 err = f2fs_convert_inline_inode(inode);
3293 #ifdef CONFIG_F2FS_FS_COMPRESSION
3294 if (f2fs_compressed_file(inode)) {
3299 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3312 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3313 * wait_for_stable_page. Will wait that below with our IO control.
3315 page = f2fs_pagecache_get_page(mapping, index,
3316 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3322 /* TODO: cluster can be compressed due to race with .writepage */
3326 err = prepare_write_begin(sbi, page, pos, len,
3327 &blkaddr, &need_balance);
3331 if (need_balance && !IS_NOQUOTA(inode) &&
3332 has_not_enough_free_secs(sbi, 0, 0)) {
3334 f2fs_balance_fs(sbi, true);
3336 if (page->mapping != mapping) {
3337 /* The page got truncated from under us */
3338 f2fs_put_page(page, 1);
3343 f2fs_wait_on_page_writeback(page, DATA, false, true);
3345 if (len == PAGE_SIZE || PageUptodate(page))
3348 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3349 !f2fs_verity_in_progress(inode)) {
3350 zero_user_segment(page, len, PAGE_SIZE);
3354 if (blkaddr == NEW_ADDR) {
3355 zero_user_segment(page, 0, PAGE_SIZE);
3356 SetPageUptodate(page);
3358 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3359 DATA_GENERIC_ENHANCE_READ)) {
3360 err = -EFSCORRUPTED;
3363 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3368 if (unlikely(page->mapping != mapping)) {
3369 f2fs_put_page(page, 1);
3372 if (unlikely(!PageUptodate(page))) {
3380 f2fs_put_page(page, 1);
3381 f2fs_write_failed(mapping, pos + len);
3383 f2fs_drop_inmem_pages_all(sbi, false);
3387 static int f2fs_write_end(struct file *file,
3388 struct address_space *mapping,
3389 loff_t pos, unsigned len, unsigned copied,
3390 struct page *page, void *fsdata)
3392 struct inode *inode = page->mapping->host;
3394 trace_f2fs_write_end(inode, pos, len, copied);
3397 * This should be come from len == PAGE_SIZE, and we expect copied
3398 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3399 * let generic_perform_write() try to copy data again through copied=0.
3401 if (!PageUptodate(page)) {
3402 if (unlikely(copied != len))
3405 SetPageUptodate(page);
3408 #ifdef CONFIG_F2FS_FS_COMPRESSION
3409 /* overwrite compressed file */
3410 if (f2fs_compressed_file(inode) && fsdata) {
3411 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3412 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3414 if (pos + copied > i_size_read(inode) &&
3415 !f2fs_verity_in_progress(inode))
3416 f2fs_i_size_write(inode, pos + copied);
3424 set_page_dirty(page);
3426 if (pos + copied > i_size_read(inode) &&
3427 !f2fs_verity_in_progress(inode))
3428 f2fs_i_size_write(inode, pos + copied);
3430 f2fs_put_page(page, 1);
3431 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3435 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3438 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3439 unsigned blkbits = i_blkbits;
3440 unsigned blocksize_mask = (1 << blkbits) - 1;
3441 unsigned long align = offset | iov_iter_alignment(iter);
3442 struct block_device *bdev = inode->i_sb->s_bdev;
3444 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3447 if (align & blocksize_mask) {
3449 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3450 blocksize_mask = (1 << blkbits) - 1;
3451 if (align & blocksize_mask)
3458 static void f2fs_dio_end_io(struct bio *bio)
3460 struct f2fs_private_dio *dio = bio->bi_private;
3462 dec_page_count(F2FS_I_SB(dio->inode),
3463 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3465 bio->bi_private = dio->orig_private;
3466 bio->bi_end_io = dio->orig_end_io;
3473 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3476 struct f2fs_private_dio *dio;
3477 bool write = (bio_op(bio) == REQ_OP_WRITE);
3479 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3480 sizeof(struct f2fs_private_dio), GFP_NOFS);
3485 dio->orig_end_io = bio->bi_end_io;
3486 dio->orig_private = bio->bi_private;
3489 bio->bi_end_io = f2fs_dio_end_io;
3490 bio->bi_private = dio;
3492 inc_page_count(F2FS_I_SB(inode),
3493 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3498 bio->bi_status = BLK_STS_IOERR;
3502 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3504 struct address_space *mapping = iocb->ki_filp->f_mapping;
3505 struct inode *inode = mapping->host;
3506 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3507 struct f2fs_inode_info *fi = F2FS_I(inode);
3508 size_t count = iov_iter_count(iter);
3509 loff_t offset = iocb->ki_pos;
3510 int rw = iov_iter_rw(iter);
3512 enum rw_hint hint = iocb->ki_hint;
3513 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3516 err = check_direct_IO(inode, iter, offset);
3518 return err < 0 ? err : 0;
3520 if (f2fs_force_buffered_io(inode, iocb, iter))
3523 do_opu = allow_outplace_dio(inode, iocb, iter);
3525 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3527 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3528 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3530 if (iocb->ki_flags & IOCB_NOWAIT) {
3531 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3532 iocb->ki_hint = hint;
3536 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3537 up_read(&fi->i_gc_rwsem[rw]);
3538 iocb->ki_hint = hint;
3543 down_read(&fi->i_gc_rwsem[rw]);
3545 down_read(&fi->i_gc_rwsem[READ]);
3548 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3549 iter, rw == WRITE ? get_data_block_dio_write :
3550 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3551 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3555 up_read(&fi->i_gc_rwsem[READ]);
3557 up_read(&fi->i_gc_rwsem[rw]);
3560 if (whint_mode == WHINT_MODE_OFF)
3561 iocb->ki_hint = hint;
3563 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3566 set_inode_flag(inode, FI_UPDATE_WRITE);
3567 } else if (err == -EIOCBQUEUED) {
3568 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3569 count - iov_iter_count(iter));
3570 } else if (err < 0) {
3571 f2fs_write_failed(mapping, offset + count);
3575 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3576 else if (err == -EIOCBQUEUED)
3577 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3578 count - iov_iter_count(iter));
3582 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3587 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3588 unsigned int length)
3590 struct inode *inode = page->mapping->host;
3591 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3593 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3594 (offset % PAGE_SIZE || length != PAGE_SIZE))
3597 if (PageDirty(page)) {
3598 if (inode->i_ino == F2FS_META_INO(sbi)) {
3599 dec_page_count(sbi, F2FS_DIRTY_META);
3600 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3601 dec_page_count(sbi, F2FS_DIRTY_NODES);
3603 inode_dec_dirty_pages(inode);
3604 f2fs_remove_dirty_inode(inode);
3608 clear_cold_data(page);
3610 if (IS_ATOMIC_WRITTEN_PAGE(page))
3611 return f2fs_drop_inmem_page(inode, page);
3613 f2fs_clear_page_private(page);
3616 int f2fs_release_page(struct page *page, gfp_t wait)
3618 /* If this is dirty page, keep PagePrivate */
3619 if (PageDirty(page))
3622 /* This is atomic written page, keep Private */
3623 if (IS_ATOMIC_WRITTEN_PAGE(page))
3626 clear_cold_data(page);
3627 f2fs_clear_page_private(page);
3631 static int f2fs_set_data_page_dirty(struct page *page)
3633 struct inode *inode = page_file_mapping(page)->host;
3635 trace_f2fs_set_page_dirty(page, DATA);
3637 if (!PageUptodate(page))
3638 SetPageUptodate(page);
3639 if (PageSwapCache(page))
3640 return __set_page_dirty_nobuffers(page);
3642 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3643 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3644 f2fs_register_inmem_page(inode, page);
3648 * Previously, this page has been registered, we just
3654 if (!PageDirty(page)) {
3655 __set_page_dirty_nobuffers(page);
3656 f2fs_update_dirty_page(inode, page);
3663 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3665 #ifdef CONFIG_F2FS_FS_COMPRESSION
3666 struct dnode_of_data dn;
3667 sector_t start_idx, blknr = 0;
3670 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3672 set_new_dnode(&dn, inode, NULL, NULL, 0);
3673 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3677 if (dn.data_blkaddr != COMPRESS_ADDR) {
3678 dn.ofs_in_node += block - start_idx;
3679 blknr = f2fs_data_blkaddr(&dn);
3680 if (!__is_valid_data_blkaddr(blknr))
3684 f2fs_put_dnode(&dn);
3692 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3694 struct inode *inode = mapping->host;
3697 if (f2fs_has_inline_data(inode))
3700 /* make sure allocating whole blocks */
3701 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3702 filemap_write_and_wait(mapping);
3704 /* Block number less than F2FS MAX BLOCKS */
3705 if (unlikely(block >= max_file_blocks(inode)))
3708 if (f2fs_compressed_file(inode)) {
3709 blknr = f2fs_bmap_compress(inode, block);
3711 struct f2fs_map_blocks map;
3713 memset(&map, 0, sizeof(map));
3716 map.m_next_pgofs = NULL;
3717 map.m_seg_type = NO_CHECK_TYPE;
3719 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3723 trace_f2fs_bmap(inode, block, blknr);
3727 #ifdef CONFIG_MIGRATION
3728 #include <linux/migrate.h>
3730 int f2fs_migrate_page(struct address_space *mapping,
3731 struct page *newpage, struct page *page, enum migrate_mode mode)
3733 int rc, extra_count;
3734 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3735 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3737 BUG_ON(PageWriteback(page));
3739 /* migrating an atomic written page is safe with the inmem_lock hold */
3740 if (atomic_written) {
3741 if (mode != MIGRATE_SYNC)
3743 if (!mutex_trylock(&fi->inmem_lock))
3747 /* one extra reference was held for atomic_write page */
3748 extra_count = atomic_written ? 1 : 0;
3749 rc = migrate_page_move_mapping(mapping, newpage,
3751 if (rc != MIGRATEPAGE_SUCCESS) {
3753 mutex_unlock(&fi->inmem_lock);
3757 if (atomic_written) {
3758 struct inmem_pages *cur;
3759 list_for_each_entry(cur, &fi->inmem_pages, list)
3760 if (cur->page == page) {
3761 cur->page = newpage;
3764 mutex_unlock(&fi->inmem_lock);
3769 if (PagePrivate(page)) {
3770 f2fs_set_page_private(newpage, page_private(page));
3771 f2fs_clear_page_private(page);
3774 if (mode != MIGRATE_SYNC_NO_COPY)
3775 migrate_page_copy(newpage, page);
3777 migrate_page_states(newpage, page);
3779 return MIGRATEPAGE_SUCCESS;
3784 static int check_swap_activate_fast(struct swap_info_struct *sis,
3785 struct file *swap_file, sector_t *span)
3787 struct address_space *mapping = swap_file->f_mapping;
3788 struct inode *inode = mapping->host;
3789 sector_t cur_lblock;
3790 sector_t last_lblock;
3792 sector_t lowest_pblock = -1;
3793 sector_t highest_pblock = 0;
3795 unsigned long nr_pblocks;
3800 * Map all the blocks into the extent list. This code doesn't try
3804 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3805 len = i_size_read(inode);
3807 while (cur_lblock <= last_lblock && cur_lblock < sis->max) {
3808 struct f2fs_map_blocks map;
3813 memset(&map, 0, sizeof(map));
3814 map.m_lblk = cur_lblock;
3815 map.m_len = bytes_to_blks(inode, len) - cur_lblock;
3816 map.m_next_pgofs = &next_pgofs;
3817 map.m_seg_type = NO_CHECK_TYPE;
3819 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3824 if (!(map.m_flags & F2FS_MAP_FLAGS))
3827 pblock = map.m_pblk;
3828 nr_pblocks = map.m_len;
3830 if (cur_lblock + nr_pblocks >= sis->max)
3831 nr_pblocks = sis->max - cur_lblock;
3833 if (cur_lblock) { /* exclude the header page */
3834 if (pblock < lowest_pblock)
3835 lowest_pblock = pblock;
3836 if (pblock + nr_pblocks - 1 > highest_pblock)
3837 highest_pblock = pblock + nr_pblocks - 1;
3841 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3843 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3847 cur_lblock += nr_pblocks;
3850 *span = 1 + highest_pblock - lowest_pblock;
3851 if (cur_lblock == 0)
3852 cur_lblock = 1; /* force Empty message */
3853 sis->max = cur_lblock;
3854 sis->pages = cur_lblock - 1;
3855 sis->highest_bit = cur_lblock - 1;
3859 pr_err("swapon: swapfile has holes\n");
3863 /* Copied from generic_swapfile_activate() to check any holes */
3864 static int check_swap_activate(struct swap_info_struct *sis,
3865 struct file *swap_file, sector_t *span)
3867 struct address_space *mapping = swap_file->f_mapping;
3868 struct inode *inode = mapping->host;
3869 unsigned blocks_per_page;
3870 unsigned long page_no;
3871 sector_t probe_block;
3872 sector_t last_block;
3873 sector_t lowest_block = -1;
3874 sector_t highest_block = 0;
3878 if (PAGE_SIZE == F2FS_BLKSIZE)
3879 return check_swap_activate_fast(sis, swap_file, span);
3881 blocks_per_page = bytes_to_blks(inode, PAGE_SIZE);
3884 * Map all the blocks into the extent list. This code doesn't try
3889 last_block = bytes_to_blks(inode, i_size_read(inode));
3890 while ((probe_block + blocks_per_page) <= last_block &&
3891 page_no < sis->max) {
3892 unsigned block_in_page;
3893 sector_t first_block;
3899 block = probe_block;
3900 err = bmap(inode, &block);
3903 first_block = block;
3906 * It must be PAGE_SIZE aligned on-disk
3908 if (first_block & (blocks_per_page - 1)) {
3913 for (block_in_page = 1; block_in_page < blocks_per_page;
3916 block = probe_block + block_in_page;
3917 err = bmap(inode, &block);
3922 if (block != first_block + block_in_page) {
3929 first_block >>= (PAGE_SHIFT - inode->i_blkbits);
3930 if (page_no) { /* exclude the header page */
3931 if (first_block < lowest_block)
3932 lowest_block = first_block;
3933 if (first_block > highest_block)
3934 highest_block = first_block;
3938 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3940 ret = add_swap_extent(sis, page_no, 1, first_block);
3945 probe_block += blocks_per_page;
3950 *span = 1 + highest_block - lowest_block;
3952 page_no = 1; /* force Empty message */
3954 sis->pages = page_no - 1;
3955 sis->highest_bit = page_no - 1;
3959 pr_err("swapon: swapfile has holes\n");
3963 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3966 struct inode *inode = file_inode(file);
3969 if (!S_ISREG(inode->i_mode))
3972 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3975 ret = f2fs_convert_inline_inode(inode);
3979 if (!f2fs_disable_compressed_file(inode))
3982 f2fs_precache_extents(inode);
3984 ret = check_swap_activate(sis, file, span);
3988 set_inode_flag(inode, FI_PIN_FILE);
3989 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3993 static void f2fs_swap_deactivate(struct file *file)
3995 struct inode *inode = file_inode(file);
3997 clear_inode_flag(inode, FI_PIN_FILE);
4000 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4006 static void f2fs_swap_deactivate(struct file *file)
4011 const struct address_space_operations f2fs_dblock_aops = {
4012 .readpage = f2fs_read_data_page,
4013 .readahead = f2fs_readahead,
4014 .writepage = f2fs_write_data_page,
4015 .writepages = f2fs_write_data_pages,
4016 .write_begin = f2fs_write_begin,
4017 .write_end = f2fs_write_end,
4018 .set_page_dirty = f2fs_set_data_page_dirty,
4019 .invalidatepage = f2fs_invalidate_page,
4020 .releasepage = f2fs_release_page,
4021 .direct_IO = f2fs_direct_IO,
4023 .swap_activate = f2fs_swap_activate,
4024 .swap_deactivate = f2fs_swap_deactivate,
4025 #ifdef CONFIG_MIGRATION
4026 .migratepage = f2fs_migrate_page,
4030 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4032 struct address_space *mapping = page_mapping(page);
4033 unsigned long flags;
4035 xa_lock_irqsave(&mapping->i_pages, flags);
4036 __xa_clear_mark(&mapping->i_pages, page_index(page),
4037 PAGECACHE_TAG_DIRTY);
4038 xa_unlock_irqrestore(&mapping->i_pages, flags);
4041 int __init f2fs_init_post_read_processing(void)
4043 bio_post_read_ctx_cache =
4044 kmem_cache_create("f2fs_bio_post_read_ctx",
4045 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4046 if (!bio_post_read_ctx_cache)
4048 bio_post_read_ctx_pool =
4049 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4050 bio_post_read_ctx_cache);
4051 if (!bio_post_read_ctx_pool)
4052 goto fail_free_cache;
4056 kmem_cache_destroy(bio_post_read_ctx_cache);
4061 void f2fs_destroy_post_read_processing(void)
4063 mempool_destroy(bio_post_read_ctx_pool);
4064 kmem_cache_destroy(bio_post_read_ctx_cache);
4067 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4069 if (!f2fs_sb_has_encrypt(sbi) &&
4070 !f2fs_sb_has_verity(sbi) &&
4071 !f2fs_sb_has_compression(sbi))
4074 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4075 WQ_UNBOUND | WQ_HIGHPRI,
4077 if (!sbi->post_read_wq)
4082 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4084 if (sbi->post_read_wq)
4085 destroy_workqueue(sbi->post_read_wq);
4088 int __init f2fs_init_bio_entry_cache(void)
4090 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4091 sizeof(struct bio_entry));
4092 if (!bio_entry_slab)
4097 void f2fs_destroy_bio_entry_cache(void)
4099 kmem_cache_destroy(bio_entry_slab);