1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
12 #include <linux/writeback.h>
13 #include <linux/backing-dev.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/blk-crypto.h>
18 #include <linux/swap.h>
19 #include <linux/prefetch.h>
20 #include <linux/uio.h>
21 #include <linux/cleancache.h>
22 #include <linux/sched/signal.h>
23 #include <linux/fiemap.h>
29 #include <trace/events/f2fs.h>
31 #define NUM_PREALLOC_POST_READ_CTXS 128
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static struct kmem_cache *bio_entry_slab;
35 static mempool_t *bio_post_read_ctx_pool;
36 static struct bio_set f2fs_bioset;
38 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
40 int __init f2fs_init_bioset(void)
42 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
43 0, BIOSET_NEED_BVECS))
48 void f2fs_destroy_bioset(void)
50 bioset_exit(&f2fs_bioset);
53 static bool __is_cp_guaranteed(struct page *page)
55 struct address_space *mapping = page->mapping;
57 struct f2fs_sb_info *sbi;
62 inode = mapping->host;
63 sbi = F2FS_I_SB(inode);
65 if (inode->i_ino == F2FS_META_INO(sbi) ||
66 inode->i_ino == F2FS_NODE_INO(sbi) ||
67 S_ISDIR(inode->i_mode))
70 if (f2fs_is_compressed_page(page))
72 if ((S_ISREG(inode->i_mode) &&
73 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
74 page_private_gcing(page))
79 static enum count_type __read_io_type(struct page *page)
81 struct address_space *mapping = page_file_mapping(page);
84 struct inode *inode = mapping->host;
85 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
87 if (inode->i_ino == F2FS_META_INO(sbi))
90 if (inode->i_ino == F2FS_NODE_INO(sbi))
96 /* postprocessing steps for read bios */
97 enum bio_post_read_step {
98 #ifdef CONFIG_FS_ENCRYPTION
99 STEP_DECRYPT = 1 << 0,
101 STEP_DECRYPT = 0, /* compile out the decryption-related code */
103 #ifdef CONFIG_F2FS_FS_COMPRESSION
104 STEP_DECOMPRESS = 1 << 1,
106 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
108 #ifdef CONFIG_FS_VERITY
109 STEP_VERITY = 1 << 2,
111 STEP_VERITY = 0, /* compile out the verity-related code */
115 struct bio_post_read_ctx {
117 struct f2fs_sb_info *sbi;
118 struct work_struct work;
119 unsigned int enabled_steps;
123 static void f2fs_finish_read_bio(struct bio *bio)
126 struct bvec_iter_all iter_all;
129 * Update and unlock the bio's pagecache pages, and put the
130 * decompression context for any compressed pages.
132 bio_for_each_segment_all(bv, bio, iter_all) {
133 struct page *page = bv->bv_page;
135 if (f2fs_is_compressed_page(page)) {
137 f2fs_end_read_compressed_page(page, true, 0);
138 f2fs_put_page_dic(page);
142 /* PG_error was set if decryption or verity failed. */
143 if (bio->bi_status || PageError(page)) {
144 ClearPageUptodate(page);
145 /* will re-read again later */
146 ClearPageError(page);
148 SetPageUptodate(page);
150 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
155 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
159 static void f2fs_verify_bio(struct work_struct *work)
161 struct bio_post_read_ctx *ctx =
162 container_of(work, struct bio_post_read_ctx, work);
163 struct bio *bio = ctx->bio;
164 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
167 * fsverity_verify_bio() may call readpages() again, and while verity
168 * will be disabled for this, decryption and/or decompression may still
169 * be needed, resulting in another bio_post_read_ctx being allocated.
170 * So to prevent deadlocks we need to release the current ctx to the
171 * mempool first. This assumes that verity is the last post-read step.
173 mempool_free(ctx, bio_post_read_ctx_pool);
174 bio->bi_private = NULL;
177 * Verify the bio's pages with fs-verity. Exclude compressed pages,
178 * as those were handled separately by f2fs_end_read_compressed_page().
180 if (may_have_compressed_pages) {
182 struct bvec_iter_all iter_all;
184 bio_for_each_segment_all(bv, bio, iter_all) {
185 struct page *page = bv->bv_page;
187 if (!f2fs_is_compressed_page(page) &&
188 !PageError(page) && !fsverity_verify_page(page))
192 fsverity_verify_bio(bio);
195 f2fs_finish_read_bio(bio);
199 * If the bio's data needs to be verified with fs-verity, then enqueue the
200 * verity work for the bio. Otherwise finish the bio now.
202 * Note that to avoid deadlocks, the verity work can't be done on the
203 * decryption/decompression workqueue. This is because verifying the data pages
204 * can involve reading verity metadata pages from the file, and these verity
205 * metadata pages may be encrypted and/or compressed.
207 static void f2fs_verify_and_finish_bio(struct bio *bio)
209 struct bio_post_read_ctx *ctx = bio->bi_private;
211 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
212 INIT_WORK(&ctx->work, f2fs_verify_bio);
213 fsverity_enqueue_verify_work(&ctx->work);
215 f2fs_finish_read_bio(bio);
220 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
221 * remaining page was read by @ctx->bio.
223 * Note that a bio may span clusters (even a mix of compressed and uncompressed
224 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
225 * that the bio includes at least one compressed page. The actual decompression
226 * is done on a per-cluster basis, not a per-bio basis.
228 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx)
231 struct bvec_iter_all iter_all;
232 bool all_compressed = true;
233 block_t blkaddr = ctx->fs_blkaddr;
235 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
236 struct page *page = bv->bv_page;
238 /* PG_error was set if decryption failed. */
239 if (f2fs_is_compressed_page(page))
240 f2fs_end_read_compressed_page(page, PageError(page),
243 all_compressed = false;
249 * Optimization: if all the bio's pages are compressed, then scheduling
250 * the per-bio verity work is unnecessary, as verity will be fully
251 * handled at the compression cluster level.
254 ctx->enabled_steps &= ~STEP_VERITY;
257 static void f2fs_post_read_work(struct work_struct *work)
259 struct bio_post_read_ctx *ctx =
260 container_of(work, struct bio_post_read_ctx, work);
262 if (ctx->enabled_steps & STEP_DECRYPT)
263 fscrypt_decrypt_bio(ctx->bio);
265 if (ctx->enabled_steps & STEP_DECOMPRESS)
266 f2fs_handle_step_decompress(ctx);
268 f2fs_verify_and_finish_bio(ctx->bio);
271 static void f2fs_read_end_io(struct bio *bio)
273 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
274 struct bio_post_read_ctx *ctx;
276 iostat_update_and_unbind_ctx(bio, 0);
277 ctx = bio->bi_private;
279 if (time_to_inject(sbi, FAULT_READ_IO)) {
280 f2fs_show_injection_info(sbi, FAULT_READ_IO);
281 bio->bi_status = BLK_STS_IOERR;
284 if (bio->bi_status) {
285 f2fs_finish_read_bio(bio);
289 if (ctx && (ctx->enabled_steps & (STEP_DECRYPT | STEP_DECOMPRESS))) {
290 INIT_WORK(&ctx->work, f2fs_post_read_work);
291 queue_work(ctx->sbi->post_read_wq, &ctx->work);
293 f2fs_verify_and_finish_bio(bio);
297 static void f2fs_write_end_io(struct bio *bio)
299 struct f2fs_sb_info *sbi;
300 struct bio_vec *bvec;
301 struct bvec_iter_all iter_all;
303 iostat_update_and_unbind_ctx(bio, 1);
304 sbi = bio->bi_private;
306 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
307 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
308 bio->bi_status = BLK_STS_IOERR;
311 bio_for_each_segment_all(bvec, bio, iter_all) {
312 struct page *page = bvec->bv_page;
313 enum count_type type = WB_DATA_TYPE(page);
315 if (page_private_dummy(page)) {
316 clear_page_private_dummy(page);
318 mempool_free(page, sbi->write_io_dummy);
320 if (unlikely(bio->bi_status))
321 f2fs_stop_checkpoint(sbi, true);
325 fscrypt_finalize_bounce_page(&page);
327 #ifdef CONFIG_F2FS_FS_COMPRESSION
328 if (f2fs_is_compressed_page(page)) {
329 f2fs_compress_write_end_io(bio, page);
334 if (unlikely(bio->bi_status)) {
335 mapping_set_error(page->mapping, -EIO);
336 if (type == F2FS_WB_CP_DATA)
337 f2fs_stop_checkpoint(sbi, true);
340 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
341 page->index != nid_of_node(page));
343 dec_page_count(sbi, type);
344 if (f2fs_in_warm_node_list(sbi, page))
345 f2fs_del_fsync_node_entry(sbi, page);
346 clear_page_private_gcing(page);
347 end_page_writeback(page);
349 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
350 wq_has_sleeper(&sbi->cp_wait))
351 wake_up(&sbi->cp_wait);
356 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
357 block_t blk_addr, struct bio *bio)
359 struct block_device *bdev = sbi->sb->s_bdev;
362 if (f2fs_is_multi_device(sbi)) {
363 for (i = 0; i < sbi->s_ndevs; i++) {
364 if (FDEV(i).start_blk <= blk_addr &&
365 FDEV(i).end_blk >= blk_addr) {
366 blk_addr -= FDEV(i).start_blk;
373 bio_set_dev(bio, bdev);
374 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
379 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
383 if (!f2fs_is_multi_device(sbi))
386 for (i = 0; i < sbi->s_ndevs; i++)
387 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
392 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
394 struct f2fs_sb_info *sbi = fio->sbi;
397 bio = bio_alloc_bioset(GFP_NOIO, npages, &f2fs_bioset);
399 f2fs_target_device(sbi, fio->new_blkaddr, bio);
400 if (is_read_io(fio->op)) {
401 bio->bi_end_io = f2fs_read_end_io;
402 bio->bi_private = NULL;
404 bio->bi_end_io = f2fs_write_end_io;
405 bio->bi_private = sbi;
406 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
407 fio->type, fio->temp);
409 iostat_alloc_and_bind_ctx(sbi, bio, NULL);
412 wbc_init_bio(fio->io_wbc, bio);
417 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
419 const struct f2fs_io_info *fio,
423 * The f2fs garbage collector sets ->encrypted_page when it wants to
424 * read/write raw data without encryption.
426 if (!fio || !fio->encrypted_page)
427 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
430 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
432 const struct f2fs_io_info *fio)
435 * The f2fs garbage collector sets ->encrypted_page when it wants to
436 * read/write raw data without encryption.
438 if (fio && fio->encrypted_page)
439 return !bio_has_crypt_ctx(bio);
441 return fscrypt_mergeable_bio(bio, inode, next_idx);
444 static inline void __submit_bio(struct f2fs_sb_info *sbi,
445 struct bio *bio, enum page_type type)
447 if (!is_read_io(bio_op(bio))) {
450 if (type != DATA && type != NODE)
453 if (f2fs_lfs_mode(sbi) && current->plug)
454 blk_finish_plug(current->plug);
456 if (!F2FS_IO_ALIGNED(sbi))
459 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
460 start %= F2FS_IO_SIZE(sbi);
465 /* fill dummy pages */
466 for (; start < F2FS_IO_SIZE(sbi); start++) {
468 mempool_alloc(sbi->write_io_dummy,
469 GFP_NOIO | __GFP_NOFAIL);
470 f2fs_bug_on(sbi, !page);
474 zero_user_segment(page, 0, PAGE_SIZE);
475 set_page_private_dummy(page);
477 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
481 * In the NODE case, we lose next block address chain. So, we
482 * need to do checkpoint in f2fs_sync_file.
485 set_sbi_flag(sbi, SBI_NEED_CP);
488 if (is_read_io(bio_op(bio)))
489 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
491 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
493 iostat_update_submit_ctx(bio, type);
497 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
498 struct bio *bio, enum page_type type)
500 __submit_bio(sbi, bio, type);
503 static void __attach_io_flag(struct f2fs_io_info *fio)
505 struct f2fs_sb_info *sbi = fio->sbi;
506 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
507 unsigned int io_flag, fua_flag, meta_flag;
509 if (fio->type == DATA)
510 io_flag = sbi->data_io_flag;
511 else if (fio->type == NODE)
512 io_flag = sbi->node_io_flag;
516 fua_flag = io_flag & temp_mask;
517 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
520 * data/node io flag bits per temp:
521 * REQ_META | REQ_FUA |
522 * 5 | 4 | 3 | 2 | 1 | 0 |
523 * Cold | Warm | Hot | Cold | Warm | Hot |
525 if ((1 << fio->temp) & meta_flag)
526 fio->op_flags |= REQ_META;
527 if ((1 << fio->temp) & fua_flag)
528 fio->op_flags |= REQ_FUA;
531 static void __submit_merged_bio(struct f2fs_bio_info *io)
533 struct f2fs_io_info *fio = &io->fio;
538 __attach_io_flag(fio);
539 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
541 if (is_read_io(fio->op))
542 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
544 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
546 __submit_bio(io->sbi, io->bio, fio->type);
550 static bool __has_merged_page(struct bio *bio, struct inode *inode,
551 struct page *page, nid_t ino)
553 struct bio_vec *bvec;
554 struct bvec_iter_all iter_all;
559 if (!inode && !page && !ino)
562 bio_for_each_segment_all(bvec, bio, iter_all) {
563 struct page *target = bvec->bv_page;
565 if (fscrypt_is_bounce_page(target)) {
566 target = fscrypt_pagecache_page(target);
570 if (f2fs_is_compressed_page(target)) {
571 target = f2fs_compress_control_page(target);
576 if (inode && inode == target->mapping->host)
578 if (page && page == target)
580 if (ino && ino == ino_of_node(target))
587 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
588 enum page_type type, enum temp_type temp)
590 enum page_type btype = PAGE_TYPE_OF_BIO(type);
591 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
593 down_write(&io->io_rwsem);
595 /* change META to META_FLUSH in the checkpoint procedure */
596 if (type >= META_FLUSH) {
597 io->fio.type = META_FLUSH;
598 io->fio.op = REQ_OP_WRITE;
599 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
600 if (!test_opt(sbi, NOBARRIER))
601 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
603 __submit_merged_bio(io);
604 up_write(&io->io_rwsem);
607 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
608 struct inode *inode, struct page *page,
609 nid_t ino, enum page_type type, bool force)
614 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
616 enum page_type btype = PAGE_TYPE_OF_BIO(type);
617 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
619 down_read(&io->io_rwsem);
620 ret = __has_merged_page(io->bio, inode, page, ino);
621 up_read(&io->io_rwsem);
624 __f2fs_submit_merged_write(sbi, type, temp);
626 /* TODO: use HOT temp only for meta pages now. */
632 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
634 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
637 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
638 struct inode *inode, struct page *page,
639 nid_t ino, enum page_type type)
641 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
644 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
646 f2fs_submit_merged_write(sbi, DATA);
647 f2fs_submit_merged_write(sbi, NODE);
648 f2fs_submit_merged_write(sbi, META);
652 * Fill the locked page with data located in the block address.
653 * A caller needs to unlock the page on failure.
655 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
658 struct page *page = fio->encrypted_page ?
659 fio->encrypted_page : fio->page;
661 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
662 fio->is_por ? META_POR : (__is_meta_io(fio) ?
663 META_GENERIC : DATA_GENERIC_ENHANCE)))
664 return -EFSCORRUPTED;
666 trace_f2fs_submit_page_bio(page, fio);
668 /* Allocate a new bio */
669 bio = __bio_alloc(fio, 1);
671 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
672 fio->page->index, fio, GFP_NOIO);
674 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
679 if (fio->io_wbc && !is_read_io(fio->op))
680 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
682 __attach_io_flag(fio);
683 bio_set_op_attrs(bio, fio->op, fio->op_flags);
685 inc_page_count(fio->sbi, is_read_io(fio->op) ?
686 __read_io_type(page): WB_DATA_TYPE(fio->page));
688 __submit_bio(fio->sbi, bio, fio->type);
692 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
693 block_t last_blkaddr, block_t cur_blkaddr)
695 if (unlikely(sbi->max_io_bytes &&
696 bio->bi_iter.bi_size >= sbi->max_io_bytes))
698 if (last_blkaddr + 1 != cur_blkaddr)
700 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
703 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
704 struct f2fs_io_info *fio)
706 if (io->fio.op != fio->op)
708 return io->fio.op_flags == fio->op_flags;
711 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
712 struct f2fs_bio_info *io,
713 struct f2fs_io_info *fio,
714 block_t last_blkaddr,
717 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
718 unsigned int filled_blocks =
719 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
720 unsigned int io_size = F2FS_IO_SIZE(sbi);
721 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
723 /* IOs in bio is aligned and left space of vectors is not enough */
724 if (!(filled_blocks % io_size) && left_vecs < io_size)
727 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
729 return io_type_is_mergeable(io, fio);
732 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
733 struct page *page, enum temp_type temp)
735 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
736 struct bio_entry *be;
738 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
742 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
745 down_write(&io->bio_list_lock);
746 list_add_tail(&be->list, &io->bio_list);
747 up_write(&io->bio_list_lock);
750 static void del_bio_entry(struct bio_entry *be)
753 kmem_cache_free(bio_entry_slab, be);
756 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
759 struct f2fs_sb_info *sbi = fio->sbi;
764 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
765 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
766 struct list_head *head = &io->bio_list;
767 struct bio_entry *be;
769 down_write(&io->bio_list_lock);
770 list_for_each_entry(be, head, list) {
776 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
779 if (f2fs_crypt_mergeable_bio(*bio,
780 fio->page->mapping->host,
781 fio->page->index, fio) &&
782 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
788 /* page can't be merged into bio; submit the bio */
790 __submit_bio(sbi, *bio, DATA);
793 up_write(&io->bio_list_lock);
804 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
805 struct bio **bio, struct page *page)
809 struct bio *target = bio ? *bio : NULL;
811 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
812 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
813 struct list_head *head = &io->bio_list;
814 struct bio_entry *be;
816 if (list_empty(head))
819 down_read(&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,
829 up_read(&io->bio_list_lock);
836 down_write(&io->bio_list_lock);
837 list_for_each_entry(be, head, list) {
839 found = (target == be->bio);
841 found = __has_merged_page(be->bio, NULL,
849 up_write(&io->bio_list_lock);
853 __submit_bio(sbi, target, DATA);
860 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
862 struct bio *bio = *fio->bio;
863 struct page *page = fio->encrypted_page ?
864 fio->encrypted_page : fio->page;
866 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
867 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
868 return -EFSCORRUPTED;
870 trace_f2fs_submit_page_bio(page, fio);
872 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
874 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
877 bio = __bio_alloc(fio, BIO_MAX_VECS);
878 __attach_io_flag(fio);
879 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
880 fio->page->index, fio, GFP_NOIO);
881 bio_set_op_attrs(bio, fio->op, fio->op_flags);
883 add_bio_entry(fio->sbi, bio, page, fio->temp);
885 if (add_ipu_page(fio, &bio, page))
890 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
892 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
894 *fio->last_block = fio->new_blkaddr;
900 void f2fs_submit_page_write(struct f2fs_io_info *fio)
902 struct f2fs_sb_info *sbi = fio->sbi;
903 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
904 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
905 struct page *bio_page;
907 f2fs_bug_on(sbi, is_read_io(fio->op));
909 down_write(&io->io_rwsem);
912 spin_lock(&io->io_lock);
913 if (list_empty(&io->io_list)) {
914 spin_unlock(&io->io_lock);
917 fio = list_first_entry(&io->io_list,
918 struct f2fs_io_info, list);
919 list_del(&fio->list);
920 spin_unlock(&io->io_lock);
923 verify_fio_blkaddr(fio);
925 if (fio->encrypted_page)
926 bio_page = fio->encrypted_page;
927 else if (fio->compressed_page)
928 bio_page = fio->compressed_page;
930 bio_page = fio->page;
932 /* set submitted = true as a return value */
933 fio->submitted = true;
935 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
938 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
940 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
941 bio_page->index, fio)))
942 __submit_merged_bio(io);
944 if (io->bio == NULL) {
945 if (F2FS_IO_ALIGNED(sbi) &&
946 (fio->type == DATA || fio->type == NODE) &&
947 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
948 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
952 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
953 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
954 bio_page->index, fio, GFP_NOIO);
958 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
959 __submit_merged_bio(io);
964 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
966 io->last_block_in_bio = fio->new_blkaddr;
968 trace_f2fs_submit_page_write(fio->page, fio);
973 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
974 !f2fs_is_checkpoint_ready(sbi))
975 __submit_merged_bio(io);
976 up_write(&io->io_rwsem);
979 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
980 unsigned nr_pages, unsigned op_flag,
981 pgoff_t first_idx, bool for_write)
983 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
985 struct bio_post_read_ctx *ctx = NULL;
986 unsigned int post_read_steps = 0;
988 bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
989 bio_max_segs(nr_pages), &f2fs_bioset);
991 return ERR_PTR(-ENOMEM);
993 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
995 f2fs_target_device(sbi, blkaddr, bio);
996 bio->bi_end_io = f2fs_read_end_io;
997 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
999 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1000 post_read_steps |= STEP_DECRYPT;
1002 if (f2fs_need_verity(inode, first_idx))
1003 post_read_steps |= STEP_VERITY;
1006 * STEP_DECOMPRESS is handled specially, since a compressed file might
1007 * contain both compressed and uncompressed clusters. We'll allocate a
1008 * bio_post_read_ctx if the file is compressed, but the caller is
1009 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1012 if (post_read_steps || f2fs_compressed_file(inode)) {
1013 /* Due to the mempool, this never fails. */
1014 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1017 ctx->enabled_steps = post_read_steps;
1018 ctx->fs_blkaddr = blkaddr;
1019 bio->bi_private = ctx;
1021 iostat_alloc_and_bind_ctx(sbi, bio, ctx);
1026 /* This can handle encryption stuffs */
1027 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1028 block_t blkaddr, int op_flags, bool for_write)
1030 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1033 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1034 page->index, for_write);
1036 return PTR_ERR(bio);
1038 /* wait for GCed page writeback via META_MAPPING */
1039 f2fs_wait_on_block_writeback(inode, blkaddr);
1041 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1045 ClearPageError(page);
1046 inc_page_count(sbi, F2FS_RD_DATA);
1047 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1048 __submit_bio(sbi, bio, DATA);
1052 static void __set_data_blkaddr(struct dnode_of_data *dn)
1054 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1058 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1059 base = get_extra_isize(dn->inode);
1061 /* Get physical address of data block */
1062 addr_array = blkaddr_in_node(rn);
1063 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1067 * Lock ordering for the change of data block address:
1070 * update block addresses in the node page
1072 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1074 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1075 __set_data_blkaddr(dn);
1076 if (set_page_dirty(dn->node_page))
1077 dn->node_changed = true;
1080 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1082 dn->data_blkaddr = blkaddr;
1083 f2fs_set_data_blkaddr(dn);
1084 f2fs_update_extent_cache(dn);
1087 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1088 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1090 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1096 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1098 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1101 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1102 dn->ofs_in_node, count);
1104 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1106 for (; count > 0; dn->ofs_in_node++) {
1107 block_t blkaddr = f2fs_data_blkaddr(dn);
1109 if (blkaddr == NULL_ADDR) {
1110 dn->data_blkaddr = NEW_ADDR;
1111 __set_data_blkaddr(dn);
1116 if (set_page_dirty(dn->node_page))
1117 dn->node_changed = true;
1121 /* Should keep dn->ofs_in_node unchanged */
1122 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1124 unsigned int ofs_in_node = dn->ofs_in_node;
1127 ret = f2fs_reserve_new_blocks(dn, 1);
1128 dn->ofs_in_node = ofs_in_node;
1132 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1134 bool need_put = dn->inode_page ? false : true;
1137 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1141 if (dn->data_blkaddr == NULL_ADDR)
1142 err = f2fs_reserve_new_block(dn);
1143 if (err || need_put)
1148 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1150 struct extent_info ei = {0, };
1151 struct inode *inode = dn->inode;
1153 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1154 dn->data_blkaddr = ei.blk + index - ei.fofs;
1158 return f2fs_reserve_block(dn, index);
1161 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1162 int op_flags, bool for_write)
1164 struct address_space *mapping = inode->i_mapping;
1165 struct dnode_of_data dn;
1167 struct extent_info ei = {0, };
1170 page = f2fs_grab_cache_page(mapping, index, for_write);
1172 return ERR_PTR(-ENOMEM);
1174 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1175 dn.data_blkaddr = ei.blk + index - ei.fofs;
1176 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1177 DATA_GENERIC_ENHANCE_READ)) {
1178 err = -EFSCORRUPTED;
1184 set_new_dnode(&dn, inode, NULL, NULL, 0);
1185 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1188 f2fs_put_dnode(&dn);
1190 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1194 if (dn.data_blkaddr != NEW_ADDR &&
1195 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1197 DATA_GENERIC_ENHANCE)) {
1198 err = -EFSCORRUPTED;
1202 if (PageUptodate(page)) {
1208 * A new dentry page is allocated but not able to be written, since its
1209 * new inode page couldn't be allocated due to -ENOSPC.
1210 * In such the case, its blkaddr can be remained as NEW_ADDR.
1211 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1212 * f2fs_init_inode_metadata.
1214 if (dn.data_blkaddr == NEW_ADDR) {
1215 zero_user_segment(page, 0, PAGE_SIZE);
1216 if (!PageUptodate(page))
1217 SetPageUptodate(page);
1222 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1223 op_flags, for_write);
1229 f2fs_put_page(page, 1);
1230 return ERR_PTR(err);
1233 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1235 struct address_space *mapping = inode->i_mapping;
1238 page = find_get_page(mapping, index);
1239 if (page && PageUptodate(page))
1241 f2fs_put_page(page, 0);
1243 page = f2fs_get_read_data_page(inode, index, 0, false);
1247 if (PageUptodate(page))
1250 wait_on_page_locked(page);
1251 if (unlikely(!PageUptodate(page))) {
1252 f2fs_put_page(page, 0);
1253 return ERR_PTR(-EIO);
1259 * If it tries to access a hole, return an error.
1260 * Because, the callers, functions in dir.c and GC, should be able to know
1261 * whether this page exists or not.
1263 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1266 struct address_space *mapping = inode->i_mapping;
1269 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1273 /* wait for read completion */
1275 if (unlikely(page->mapping != mapping)) {
1276 f2fs_put_page(page, 1);
1279 if (unlikely(!PageUptodate(page))) {
1280 f2fs_put_page(page, 1);
1281 return ERR_PTR(-EIO);
1287 * Caller ensures that this data page is never allocated.
1288 * A new zero-filled data page is allocated in the page cache.
1290 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1292 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1293 * ipage should be released by this function.
1295 struct page *f2fs_get_new_data_page(struct inode *inode,
1296 struct page *ipage, pgoff_t index, bool new_i_size)
1298 struct address_space *mapping = inode->i_mapping;
1300 struct dnode_of_data dn;
1303 page = f2fs_grab_cache_page(mapping, index, true);
1306 * before exiting, we should make sure ipage will be released
1307 * if any error occur.
1309 f2fs_put_page(ipage, 1);
1310 return ERR_PTR(-ENOMEM);
1313 set_new_dnode(&dn, inode, ipage, NULL, 0);
1314 err = f2fs_reserve_block(&dn, index);
1316 f2fs_put_page(page, 1);
1317 return ERR_PTR(err);
1320 f2fs_put_dnode(&dn);
1322 if (PageUptodate(page))
1325 if (dn.data_blkaddr == NEW_ADDR) {
1326 zero_user_segment(page, 0, PAGE_SIZE);
1327 if (!PageUptodate(page))
1328 SetPageUptodate(page);
1330 f2fs_put_page(page, 1);
1332 /* if ipage exists, blkaddr should be NEW_ADDR */
1333 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1334 page = f2fs_get_lock_data_page(inode, index, true);
1339 if (new_i_size && i_size_read(inode) <
1340 ((loff_t)(index + 1) << PAGE_SHIFT))
1341 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1345 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1347 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1348 struct f2fs_summary sum;
1349 struct node_info ni;
1350 block_t old_blkaddr;
1354 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1357 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1361 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1362 if (dn->data_blkaddr != NULL_ADDR)
1365 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1369 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1370 old_blkaddr = dn->data_blkaddr;
1371 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1372 &sum, seg_type, NULL);
1373 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
1374 invalidate_mapping_pages(META_MAPPING(sbi),
1375 old_blkaddr, old_blkaddr);
1376 f2fs_invalidate_compress_page(sbi, old_blkaddr);
1378 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1381 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1382 * data from unwritten block via dio_read.
1387 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1389 struct inode *inode = file_inode(iocb->ki_filp);
1390 struct f2fs_map_blocks map;
1393 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1395 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1396 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1397 if (map.m_len > map.m_lblk)
1398 map.m_len -= map.m_lblk;
1402 map.m_next_pgofs = NULL;
1403 map.m_next_extent = NULL;
1404 map.m_seg_type = NO_CHECK_TYPE;
1405 map.m_may_create = true;
1408 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1409 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1410 F2FS_GET_BLOCK_PRE_AIO :
1411 F2FS_GET_BLOCK_PRE_DIO;
1414 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1415 err = f2fs_convert_inline_inode(inode);
1419 if (f2fs_has_inline_data(inode))
1422 flag = F2FS_GET_BLOCK_PRE_AIO;
1425 err = f2fs_map_blocks(inode, &map, 1, flag);
1426 if (map.m_len > 0 && err == -ENOSPC) {
1428 set_inode_flag(inode, FI_NO_PREALLOC);
1434 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1436 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1438 down_read(&sbi->node_change);
1440 up_read(&sbi->node_change);
1445 f2fs_unlock_op(sbi);
1450 * f2fs_map_blocks() tries to find or build mapping relationship which
1451 * maps continuous logical blocks to physical blocks, and return such
1452 * info via f2fs_map_blocks structure.
1454 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1455 int create, int flag)
1457 unsigned int maxblocks = map->m_len;
1458 struct dnode_of_data dn;
1459 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1460 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1461 pgoff_t pgofs, end_offset, end;
1462 int err = 0, ofs = 1;
1463 unsigned int ofs_in_node, last_ofs_in_node;
1465 struct extent_info ei = {0, };
1467 unsigned int start_pgofs;
1473 map->m_bdev = inode->i_sb->s_bdev;
1474 map->m_multidev_dio =
1475 f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
1480 /* it only supports block size == page size */
1481 pgofs = (pgoff_t)map->m_lblk;
1482 end = pgofs + maxblocks;
1484 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1485 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1489 map->m_pblk = ei.blk + pgofs - ei.fofs;
1490 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1491 map->m_flags = F2FS_MAP_MAPPED;
1492 if (map->m_next_extent)
1493 *map->m_next_extent = pgofs + map->m_len;
1495 /* for hardware encryption, but to avoid potential issue in future */
1496 if (flag == F2FS_GET_BLOCK_DIO)
1497 f2fs_wait_on_block_writeback_range(inode,
1498 map->m_pblk, map->m_len);
1500 if (map->m_multidev_dio) {
1501 block_t blk_addr = map->m_pblk;
1503 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1505 map->m_bdev = FDEV(bidx).bdev;
1506 map->m_pblk -= FDEV(bidx).start_blk;
1507 map->m_len = min(map->m_len,
1508 FDEV(bidx).end_blk + 1 - map->m_pblk);
1510 if (map->m_may_create)
1511 f2fs_update_device_state(sbi, inode->i_ino,
1512 blk_addr, map->m_len);
1518 if (map->m_may_create)
1519 f2fs_do_map_lock(sbi, flag, true);
1521 /* When reading holes, we need its node page */
1522 set_new_dnode(&dn, inode, NULL, NULL, 0);
1523 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1525 if (flag == F2FS_GET_BLOCK_BMAP)
1528 if (err == -ENOENT) {
1530 * There is one exceptional case that read_node_page()
1531 * may return -ENOENT due to filesystem has been
1532 * shutdown or cp_error, so force to convert error
1533 * number to EIO for such case.
1535 if (map->m_may_create &&
1536 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1537 f2fs_cp_error(sbi))) {
1543 if (map->m_next_pgofs)
1544 *map->m_next_pgofs =
1545 f2fs_get_next_page_offset(&dn, pgofs);
1546 if (map->m_next_extent)
1547 *map->m_next_extent =
1548 f2fs_get_next_page_offset(&dn, pgofs);
1553 start_pgofs = pgofs;
1555 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1556 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1559 blkaddr = f2fs_data_blkaddr(&dn);
1561 if (__is_valid_data_blkaddr(blkaddr) &&
1562 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1563 err = -EFSCORRUPTED;
1567 if (__is_valid_data_blkaddr(blkaddr)) {
1568 /* use out-place-update for driect IO under LFS mode */
1569 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1570 map->m_may_create) {
1571 err = __allocate_data_block(&dn, map->m_seg_type);
1574 blkaddr = dn.data_blkaddr;
1575 set_inode_flag(inode, FI_APPEND_WRITE);
1579 if (unlikely(f2fs_cp_error(sbi))) {
1583 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1584 if (blkaddr == NULL_ADDR) {
1586 last_ofs_in_node = dn.ofs_in_node;
1589 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1590 flag != F2FS_GET_BLOCK_DIO);
1591 err = __allocate_data_block(&dn,
1594 set_inode_flag(inode, FI_APPEND_WRITE);
1598 map->m_flags |= F2FS_MAP_NEW;
1599 blkaddr = dn.data_blkaddr;
1601 if (f2fs_compressed_file(inode) &&
1602 f2fs_sanity_check_cluster(&dn) &&
1603 (flag != F2FS_GET_BLOCK_FIEMAP ||
1604 IS_ENABLED(CONFIG_F2FS_CHECK_FS))) {
1605 err = -EFSCORRUPTED;
1608 if (flag == F2FS_GET_BLOCK_BMAP) {
1612 if (flag == F2FS_GET_BLOCK_PRECACHE)
1614 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1615 blkaddr == NULL_ADDR) {
1616 if (map->m_next_pgofs)
1617 *map->m_next_pgofs = pgofs + 1;
1620 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1621 /* for defragment case */
1622 if (map->m_next_pgofs)
1623 *map->m_next_pgofs = pgofs + 1;
1629 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1632 if (map->m_multidev_dio)
1633 bidx = f2fs_target_device_index(sbi, blkaddr);
1635 if (map->m_len == 0) {
1636 /* preallocated unwritten block should be mapped for fiemap. */
1637 if (blkaddr == NEW_ADDR)
1638 map->m_flags |= F2FS_MAP_UNWRITTEN;
1639 map->m_flags |= F2FS_MAP_MAPPED;
1641 map->m_pblk = blkaddr;
1644 if (map->m_multidev_dio)
1645 map->m_bdev = FDEV(bidx).bdev;
1646 } else if ((map->m_pblk != NEW_ADDR &&
1647 blkaddr == (map->m_pblk + ofs)) ||
1648 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1649 flag == F2FS_GET_BLOCK_PRE_DIO) {
1650 if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
1662 /* preallocate blocks in batch for one dnode page */
1663 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1664 (pgofs == end || dn.ofs_in_node == end_offset)) {
1666 dn.ofs_in_node = ofs_in_node;
1667 err = f2fs_reserve_new_blocks(&dn, prealloc);
1671 map->m_len += dn.ofs_in_node - ofs_in_node;
1672 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1676 dn.ofs_in_node = end_offset;
1681 else if (dn.ofs_in_node < end_offset)
1684 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1685 if (map->m_flags & F2FS_MAP_MAPPED) {
1686 unsigned int ofs = start_pgofs - map->m_lblk;
1688 f2fs_update_extent_cache_range(&dn,
1689 start_pgofs, map->m_pblk + ofs,
1694 f2fs_put_dnode(&dn);
1696 if (map->m_may_create) {
1697 f2fs_do_map_lock(sbi, flag, false);
1698 f2fs_balance_fs(sbi, dn.node_changed);
1704 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
1706 * for hardware encryption, but to avoid potential issue
1709 f2fs_wait_on_block_writeback_range(inode,
1710 map->m_pblk, map->m_len);
1712 if (map->m_multidev_dio) {
1713 block_t blk_addr = map->m_pblk;
1715 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1717 map->m_bdev = FDEV(bidx).bdev;
1718 map->m_pblk -= FDEV(bidx).start_blk;
1720 if (map->m_may_create)
1721 f2fs_update_device_state(sbi, inode->i_ino,
1722 blk_addr, map->m_len);
1724 f2fs_bug_on(sbi, blk_addr + map->m_len >
1725 FDEV(bidx).end_blk + 1);
1729 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1730 if (map->m_flags & F2FS_MAP_MAPPED) {
1731 unsigned int ofs = start_pgofs - map->m_lblk;
1733 f2fs_update_extent_cache_range(&dn,
1734 start_pgofs, map->m_pblk + ofs,
1737 if (map->m_next_extent)
1738 *map->m_next_extent = pgofs + 1;
1740 f2fs_put_dnode(&dn);
1742 if (map->m_may_create) {
1743 f2fs_do_map_lock(sbi, flag, false);
1744 f2fs_balance_fs(sbi, dn.node_changed);
1747 trace_f2fs_map_blocks(inode, map, create, flag, err);
1751 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1753 struct f2fs_map_blocks map;
1757 if (pos + len > i_size_read(inode))
1760 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1761 map.m_next_pgofs = NULL;
1762 map.m_next_extent = NULL;
1763 map.m_seg_type = NO_CHECK_TYPE;
1764 map.m_may_create = false;
1765 last_lblk = F2FS_BLK_ALIGN(pos + len);
1767 while (map.m_lblk < last_lblk) {
1768 map.m_len = last_lblk - map.m_lblk;
1769 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1770 if (err || map.m_len == 0)
1772 map.m_lblk += map.m_len;
1777 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1779 return (bytes >> inode->i_blkbits);
1782 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1784 return (blks << inode->i_blkbits);
1787 static int __get_data_block(struct inode *inode, sector_t iblock,
1788 struct buffer_head *bh, int create, int flag,
1789 pgoff_t *next_pgofs, int seg_type, bool may_write)
1791 struct f2fs_map_blocks map;
1794 map.m_lblk = iblock;
1795 map.m_len = bytes_to_blks(inode, bh->b_size);
1796 map.m_next_pgofs = next_pgofs;
1797 map.m_next_extent = NULL;
1798 map.m_seg_type = seg_type;
1799 map.m_may_create = may_write;
1801 err = f2fs_map_blocks(inode, &map, create, flag);
1803 map_bh(bh, inode->i_sb, map.m_pblk);
1804 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1805 bh->b_size = blks_to_bytes(inode, map.m_len);
1807 if (map.m_multidev_dio)
1808 bh->b_bdev = map.m_bdev;
1813 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1814 struct buffer_head *bh_result, int create)
1816 return __get_data_block(inode, iblock, bh_result, create,
1817 F2FS_GET_BLOCK_DIO, NULL,
1818 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1822 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1823 struct buffer_head *bh_result, int create)
1825 return __get_data_block(inode, iblock, bh_result, create,
1826 F2FS_GET_BLOCK_DIO, NULL,
1827 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1831 static int f2fs_xattr_fiemap(struct inode *inode,
1832 struct fiemap_extent_info *fieinfo)
1834 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1836 struct node_info ni;
1837 __u64 phys = 0, len;
1839 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1842 if (f2fs_has_inline_xattr(inode)) {
1845 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1846 inode->i_ino, false);
1850 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1852 f2fs_put_page(page, 1);
1856 phys = blks_to_bytes(inode, ni.blk_addr);
1857 offset = offsetof(struct f2fs_inode, i_addr) +
1858 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1859 get_inline_xattr_addrs(inode));
1862 len = inline_xattr_size(inode);
1864 f2fs_put_page(page, 1);
1866 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1869 flags |= FIEMAP_EXTENT_LAST;
1871 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1872 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1873 if (err || err == 1)
1878 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1882 err = f2fs_get_node_info(sbi, xnid, &ni);
1884 f2fs_put_page(page, 1);
1888 phys = blks_to_bytes(inode, ni.blk_addr);
1889 len = inode->i_sb->s_blocksize;
1891 f2fs_put_page(page, 1);
1893 flags = FIEMAP_EXTENT_LAST;
1897 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1898 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1901 return (err < 0 ? err : 0);
1904 static loff_t max_inode_blocks(struct inode *inode)
1906 loff_t result = ADDRS_PER_INODE(inode);
1907 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1909 /* two direct node blocks */
1910 result += (leaf_count * 2);
1912 /* two indirect node blocks */
1913 leaf_count *= NIDS_PER_BLOCK;
1914 result += (leaf_count * 2);
1916 /* one double indirect node block */
1917 leaf_count *= NIDS_PER_BLOCK;
1918 result += leaf_count;
1923 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1926 struct f2fs_map_blocks map;
1927 sector_t start_blk, last_blk;
1929 u64 logical = 0, phys = 0, size = 0;
1932 bool compr_cluster = false, compr_appended;
1933 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1934 unsigned int count_in_cluster = 0;
1937 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1938 ret = f2fs_precache_extents(inode);
1943 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1949 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1950 if (start > maxbytes) {
1955 if (len > maxbytes || (maxbytes - len) < start)
1956 len = maxbytes - start;
1958 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1959 ret = f2fs_xattr_fiemap(inode, fieinfo);
1963 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1964 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1969 if (bytes_to_blks(inode, len) == 0)
1970 len = blks_to_bytes(inode, 1);
1972 start_blk = bytes_to_blks(inode, start);
1973 last_blk = bytes_to_blks(inode, start + len - 1);
1976 memset(&map, 0, sizeof(map));
1977 map.m_lblk = start_blk;
1978 map.m_len = bytes_to_blks(inode, len);
1979 map.m_next_pgofs = &next_pgofs;
1980 map.m_seg_type = NO_CHECK_TYPE;
1982 if (compr_cluster) {
1984 map.m_len = cluster_size - count_in_cluster;
1987 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1992 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
1993 start_blk = next_pgofs;
1995 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1996 max_inode_blocks(inode)))
1999 flags |= FIEMAP_EXTENT_LAST;
2002 compr_appended = false;
2003 /* In a case of compressed cluster, append this to the last extent */
2004 if (compr_cluster && ((map.m_flags & F2FS_MAP_UNWRITTEN) ||
2005 !(map.m_flags & F2FS_MAP_FLAGS))) {
2006 compr_appended = true;
2011 flags |= FIEMAP_EXTENT_MERGED;
2012 if (IS_ENCRYPTED(inode))
2013 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
2015 ret = fiemap_fill_next_extent(fieinfo, logical,
2017 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
2023 if (start_blk > last_blk)
2027 if (map.m_pblk == COMPRESS_ADDR) {
2028 compr_cluster = true;
2029 count_in_cluster = 1;
2030 } else if (compr_appended) {
2031 unsigned int appended_blks = cluster_size -
2032 count_in_cluster + 1;
2033 size += blks_to_bytes(inode, appended_blks);
2034 start_blk += appended_blks;
2035 compr_cluster = false;
2037 logical = blks_to_bytes(inode, start_blk);
2038 phys = __is_valid_data_blkaddr(map.m_pblk) ?
2039 blks_to_bytes(inode, map.m_pblk) : 0;
2040 size = blks_to_bytes(inode, map.m_len);
2043 if (compr_cluster) {
2044 flags = FIEMAP_EXTENT_ENCODED;
2045 count_in_cluster += map.m_len;
2046 if (count_in_cluster == cluster_size) {
2047 compr_cluster = false;
2048 size += blks_to_bytes(inode, 1);
2050 } else if (map.m_flags & F2FS_MAP_UNWRITTEN) {
2051 flags = FIEMAP_EXTENT_UNWRITTEN;
2054 start_blk += bytes_to_blks(inode, size);
2059 if (fatal_signal_pending(current))
2067 inode_unlock(inode);
2071 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2073 if (IS_ENABLED(CONFIG_FS_VERITY) &&
2074 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2075 return inode->i_sb->s_maxbytes;
2077 return i_size_read(inode);
2080 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2082 struct f2fs_map_blocks *map,
2083 struct bio **bio_ret,
2084 sector_t *last_block_in_bio,
2087 struct bio *bio = *bio_ret;
2088 const unsigned blocksize = blks_to_bytes(inode, 1);
2089 sector_t block_in_file;
2090 sector_t last_block;
2091 sector_t last_block_in_file;
2095 block_in_file = (sector_t)page_index(page);
2096 last_block = block_in_file + nr_pages;
2097 last_block_in_file = bytes_to_blks(inode,
2098 f2fs_readpage_limit(inode) + blocksize - 1);
2099 if (last_block > last_block_in_file)
2100 last_block = last_block_in_file;
2102 /* just zeroing out page which is beyond EOF */
2103 if (block_in_file >= last_block)
2106 * Map blocks using the previous result first.
2108 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2109 block_in_file > map->m_lblk &&
2110 block_in_file < (map->m_lblk + map->m_len))
2114 * Then do more f2fs_map_blocks() calls until we are
2115 * done with this page.
2117 map->m_lblk = block_in_file;
2118 map->m_len = last_block - block_in_file;
2120 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2124 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2125 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2126 SetPageMappedToDisk(page);
2128 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2129 !cleancache_get_page(page))) {
2130 SetPageUptodate(page);
2134 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2135 DATA_GENERIC_ENHANCE_READ)) {
2136 ret = -EFSCORRUPTED;
2141 zero_user_segment(page, 0, PAGE_SIZE);
2142 if (f2fs_need_verity(inode, page->index) &&
2143 !fsverity_verify_page(page)) {
2147 if (!PageUptodate(page))
2148 SetPageUptodate(page);
2154 * This page will go to BIO. Do we need to send this
2157 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2158 *last_block_in_bio, block_nr) ||
2159 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2161 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2165 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2166 is_readahead ? REQ_RAHEAD : 0, page->index,
2176 * If the page is under writeback, we need to wait for
2177 * its completion to see the correct decrypted data.
2179 f2fs_wait_on_block_writeback(inode, block_nr);
2181 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2182 goto submit_and_realloc;
2184 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2185 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2186 ClearPageError(page);
2187 *last_block_in_bio = block_nr;
2191 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2200 #ifdef CONFIG_F2FS_FS_COMPRESSION
2201 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2202 unsigned nr_pages, sector_t *last_block_in_bio,
2203 bool is_readahead, bool for_write)
2205 struct dnode_of_data dn;
2206 struct inode *inode = cc->inode;
2207 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2208 struct bio *bio = *bio_ret;
2209 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2210 sector_t last_block_in_file;
2211 const unsigned blocksize = blks_to_bytes(inode, 1);
2212 struct decompress_io_ctx *dic = NULL;
2213 struct extent_info ei = {0, };
2214 bool from_dnode = true;
2218 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2220 last_block_in_file = bytes_to_blks(inode,
2221 f2fs_readpage_limit(inode) + blocksize - 1);
2223 /* get rid of pages beyond EOF */
2224 for (i = 0; i < cc->cluster_size; i++) {
2225 struct page *page = cc->rpages[i];
2229 if ((sector_t)page->index >= last_block_in_file) {
2230 zero_user_segment(page, 0, PAGE_SIZE);
2231 if (!PageUptodate(page))
2232 SetPageUptodate(page);
2233 } else if (!PageUptodate(page)) {
2239 cc->rpages[i] = NULL;
2243 /* we are done since all pages are beyond EOF */
2244 if (f2fs_cluster_is_empty(cc))
2247 if (f2fs_lookup_extent_cache(inode, start_idx, &ei))
2251 goto skip_reading_dnode;
2253 set_new_dnode(&dn, inode, NULL, NULL, 0);
2254 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2258 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2261 for (i = 1; i < cc->cluster_size; i++) {
2264 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2265 dn.ofs_in_node + i) :
2268 if (!__is_valid_data_blkaddr(blkaddr))
2271 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2277 if (!from_dnode && i >= ei.c_len)
2281 /* nothing to decompress */
2282 if (cc->nr_cpages == 0) {
2287 dic = f2fs_alloc_dic(cc);
2293 for (i = 0; i < cc->nr_cpages; i++) {
2294 struct page *page = dic->cpages[i];
2296 struct bio_post_read_ctx *ctx;
2298 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2299 dn.ofs_in_node + i + 1) :
2302 f2fs_wait_on_block_writeback(inode, blkaddr);
2304 if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2305 if (atomic_dec_and_test(&dic->remaining_pages))
2306 f2fs_decompress_cluster(dic);
2310 if (bio && (!page_is_mergeable(sbi, bio,
2311 *last_block_in_bio, blkaddr) ||
2312 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2314 __submit_bio(sbi, bio, DATA);
2319 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2320 is_readahead ? REQ_RAHEAD : 0,
2321 page->index, for_write);
2324 f2fs_decompress_end_io(dic, ret);
2325 f2fs_put_dnode(&dn);
2331 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2332 goto submit_and_realloc;
2334 ctx = get_post_read_ctx(bio);
2335 ctx->enabled_steps |= STEP_DECOMPRESS;
2336 refcount_inc(&dic->refcnt);
2338 inc_page_count(sbi, F2FS_RD_DATA);
2339 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2340 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2341 ClearPageError(page);
2342 *last_block_in_bio = blkaddr;
2346 f2fs_put_dnode(&dn);
2353 f2fs_put_dnode(&dn);
2355 for (i = 0; i < cc->cluster_size; i++) {
2356 if (cc->rpages[i]) {
2357 ClearPageUptodate(cc->rpages[i]);
2358 ClearPageError(cc->rpages[i]);
2359 unlock_page(cc->rpages[i]);
2368 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2369 * Major change was from block_size == page_size in f2fs by default.
2371 static int f2fs_mpage_readpages(struct inode *inode,
2372 struct readahead_control *rac, struct page *page)
2374 struct bio *bio = NULL;
2375 sector_t last_block_in_bio = 0;
2376 struct f2fs_map_blocks map;
2377 #ifdef CONFIG_F2FS_FS_COMPRESSION
2378 struct compress_ctx cc = {
2380 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2381 .cluster_size = F2FS_I(inode)->i_cluster_size,
2382 .cluster_idx = NULL_CLUSTER,
2388 pgoff_t nc_cluster_idx = NULL_CLUSTER;
2390 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2391 unsigned max_nr_pages = nr_pages;
2398 map.m_next_pgofs = NULL;
2399 map.m_next_extent = NULL;
2400 map.m_seg_type = NO_CHECK_TYPE;
2401 map.m_may_create = false;
2403 for (; nr_pages; nr_pages--) {
2405 page = readahead_page(rac);
2406 prefetchw(&page->flags);
2409 #ifdef CONFIG_F2FS_FS_COMPRESSION
2410 if (f2fs_compressed_file(inode)) {
2411 /* there are remained comressed pages, submit them */
2412 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2413 ret = f2fs_read_multi_pages(&cc, &bio,
2416 rac != NULL, false);
2417 f2fs_destroy_compress_ctx(&cc, false);
2419 goto set_error_page;
2421 if (cc.cluster_idx == NULL_CLUSTER) {
2422 if (nc_cluster_idx ==
2423 page->index >> cc.log_cluster_size) {
2424 goto read_single_page;
2427 ret = f2fs_is_compressed_cluster(inode, page->index);
2429 goto set_error_page;
2432 page->index >> cc.log_cluster_size;
2433 goto read_single_page;
2436 nc_cluster_idx = NULL_CLUSTER;
2438 ret = f2fs_init_compress_ctx(&cc);
2440 goto set_error_page;
2442 f2fs_compress_ctx_add_page(&cc, page);
2449 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2450 &bio, &last_block_in_bio, rac);
2452 #ifdef CONFIG_F2FS_FS_COMPRESSION
2456 zero_user_segment(page, 0, PAGE_SIZE);
2459 #ifdef CONFIG_F2FS_FS_COMPRESSION
2465 #ifdef CONFIG_F2FS_FS_COMPRESSION
2466 if (f2fs_compressed_file(inode)) {
2468 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2469 ret = f2fs_read_multi_pages(&cc, &bio,
2472 rac != NULL, false);
2473 f2fs_destroy_compress_ctx(&cc, false);
2479 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2483 static int f2fs_read_data_page(struct file *file, struct page *page)
2485 struct inode *inode = page_file_mapping(page)->host;
2488 trace_f2fs_readpage(page, DATA);
2490 if (!f2fs_is_compress_backend_ready(inode)) {
2495 /* If the file has inline data, try to read it directly */
2496 if (f2fs_has_inline_data(inode))
2497 ret = f2fs_read_inline_data(inode, page);
2499 ret = f2fs_mpage_readpages(inode, NULL, page);
2503 static void f2fs_readahead(struct readahead_control *rac)
2505 struct inode *inode = rac->mapping->host;
2507 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2509 if (!f2fs_is_compress_backend_ready(inode))
2512 /* If the file has inline data, skip readpages */
2513 if (f2fs_has_inline_data(inode))
2516 f2fs_mpage_readpages(inode, rac, NULL);
2519 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2521 struct inode *inode = fio->page->mapping->host;
2522 struct page *mpage, *page;
2523 gfp_t gfp_flags = GFP_NOFS;
2525 if (!f2fs_encrypted_file(inode))
2528 page = fio->compressed_page ? fio->compressed_page : fio->page;
2530 /* wait for GCed page writeback via META_MAPPING */
2531 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2533 if (fscrypt_inode_uses_inline_crypto(inode))
2537 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2538 PAGE_SIZE, 0, gfp_flags);
2539 if (IS_ERR(fio->encrypted_page)) {
2540 /* flush pending IOs and wait for a while in the ENOMEM case */
2541 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2542 f2fs_flush_merged_writes(fio->sbi);
2543 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2544 gfp_flags |= __GFP_NOFAIL;
2547 return PTR_ERR(fio->encrypted_page);
2550 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2552 if (PageUptodate(mpage))
2553 memcpy(page_address(mpage),
2554 page_address(fio->encrypted_page), PAGE_SIZE);
2555 f2fs_put_page(mpage, 1);
2560 static inline bool check_inplace_update_policy(struct inode *inode,
2561 struct f2fs_io_info *fio)
2563 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2564 unsigned int policy = SM_I(sbi)->ipu_policy;
2566 if (policy & (0x1 << F2FS_IPU_FORCE))
2568 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2570 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2571 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2573 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2574 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2578 * IPU for rewrite async pages
2580 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2581 fio && fio->op == REQ_OP_WRITE &&
2582 !(fio->op_flags & REQ_SYNC) &&
2583 !IS_ENCRYPTED(inode))
2586 /* this is only set during fdatasync */
2587 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2588 is_inode_flag_set(inode, FI_NEED_IPU))
2591 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2592 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2598 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2600 /* swap file is migrating in aligned write mode */
2601 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2604 if (f2fs_is_pinned_file(inode))
2607 /* if this is cold file, we should overwrite to avoid fragmentation */
2608 if (file_is_cold(inode))
2611 return check_inplace_update_policy(inode, fio);
2614 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2616 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2618 if (f2fs_lfs_mode(sbi))
2620 if (S_ISDIR(inode->i_mode))
2622 if (IS_NOQUOTA(inode))
2624 if (f2fs_is_atomic_file(inode))
2626 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2629 /* swap file is migrating in aligned write mode */
2630 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2634 if (page_private_gcing(fio->page))
2636 if (page_private_dummy(fio->page))
2638 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2639 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2645 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2647 struct inode *inode = fio->page->mapping->host;
2649 if (f2fs_should_update_outplace(inode, fio))
2652 return f2fs_should_update_inplace(inode, fio);
2655 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2657 struct page *page = fio->page;
2658 struct inode *inode = page->mapping->host;
2659 struct dnode_of_data dn;
2660 struct extent_info ei = {0, };
2661 struct node_info ni;
2662 bool ipu_force = false;
2665 set_new_dnode(&dn, inode, NULL, NULL, 0);
2666 if (need_inplace_update(fio) &&
2667 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2668 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2670 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2671 DATA_GENERIC_ENHANCE))
2672 return -EFSCORRUPTED;
2675 fio->need_lock = LOCK_DONE;
2679 /* Deadlock due to between page->lock and f2fs_lock_op */
2680 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2683 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2687 fio->old_blkaddr = dn.data_blkaddr;
2689 /* This page is already truncated */
2690 if (fio->old_blkaddr == NULL_ADDR) {
2691 ClearPageUptodate(page);
2692 clear_page_private_gcing(page);
2696 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2697 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2698 DATA_GENERIC_ENHANCE)) {
2699 err = -EFSCORRUPTED;
2703 * If current allocation needs SSR,
2704 * it had better in-place writes for updated data.
2707 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2708 need_inplace_update(fio))) {
2709 err = f2fs_encrypt_one_page(fio);
2713 set_page_writeback(page);
2714 ClearPageError(page);
2715 f2fs_put_dnode(&dn);
2716 if (fio->need_lock == LOCK_REQ)
2717 f2fs_unlock_op(fio->sbi);
2718 err = f2fs_inplace_write_data(fio);
2720 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2721 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2722 if (PageWriteback(page))
2723 end_page_writeback(page);
2725 set_inode_flag(inode, FI_UPDATE_WRITE);
2727 trace_f2fs_do_write_data_page(fio->page, IPU);
2731 if (fio->need_lock == LOCK_RETRY) {
2732 if (!f2fs_trylock_op(fio->sbi)) {
2736 fio->need_lock = LOCK_REQ;
2739 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2743 fio->version = ni.version;
2745 err = f2fs_encrypt_one_page(fio);
2749 set_page_writeback(page);
2750 ClearPageError(page);
2752 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2753 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2755 /* LFS mode write path */
2756 f2fs_outplace_write_data(&dn, fio);
2757 trace_f2fs_do_write_data_page(page, OPU);
2758 set_inode_flag(inode, FI_APPEND_WRITE);
2759 if (page->index == 0)
2760 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2762 f2fs_put_dnode(&dn);
2764 if (fio->need_lock == LOCK_REQ)
2765 f2fs_unlock_op(fio->sbi);
2769 int f2fs_write_single_data_page(struct page *page, int *submitted,
2771 sector_t *last_block,
2772 struct writeback_control *wbc,
2773 enum iostat_type io_type,
2777 struct inode *inode = page->mapping->host;
2778 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2779 loff_t i_size = i_size_read(inode);
2780 const pgoff_t end_index = ((unsigned long long)i_size)
2782 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2783 unsigned offset = 0;
2784 bool need_balance_fs = false;
2786 struct f2fs_io_info fio = {
2788 .ino = inode->i_ino,
2791 .op_flags = wbc_to_write_flags(wbc),
2792 .old_blkaddr = NULL_ADDR,
2794 .encrypted_page = NULL,
2796 .compr_blocks = compr_blocks,
2797 .need_lock = LOCK_RETRY,
2801 .last_block = last_block,
2804 trace_f2fs_writepage(page, DATA);
2806 /* we should bypass data pages to proceed the kworkder jobs */
2807 if (unlikely(f2fs_cp_error(sbi))) {
2808 mapping_set_error(page->mapping, -EIO);
2810 * don't drop any dirty dentry pages for keeping lastest
2811 * directory structure.
2813 if (S_ISDIR(inode->i_mode))
2818 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2821 if (page->index < end_index ||
2822 f2fs_verity_in_progress(inode) ||
2827 * If the offset is out-of-range of file size,
2828 * this page does not have to be written to disk.
2830 offset = i_size & (PAGE_SIZE - 1);
2831 if ((page->index >= end_index + 1) || !offset)
2834 zero_user_segment(page, offset, PAGE_SIZE);
2836 if (f2fs_is_drop_cache(inode))
2838 /* we should not write 0'th page having journal header */
2839 if (f2fs_is_volatile_file(inode) && (!page->index ||
2840 (!wbc->for_reclaim &&
2841 f2fs_available_free_memory(sbi, BASE_CHECK))))
2844 /* Dentry/quota blocks are controlled by checkpoint */
2845 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2847 * We need to wait for node_write to avoid block allocation during
2848 * checkpoint. This can only happen to quota writes which can cause
2849 * the below discard race condition.
2851 if (IS_NOQUOTA(inode))
2852 down_read(&sbi->node_write);
2854 fio.need_lock = LOCK_DONE;
2855 err = f2fs_do_write_data_page(&fio);
2857 if (IS_NOQUOTA(inode))
2858 up_read(&sbi->node_write);
2863 if (!wbc->for_reclaim)
2864 need_balance_fs = true;
2865 else if (has_not_enough_free_secs(sbi, 0, 0))
2868 set_inode_flag(inode, FI_HOT_DATA);
2871 if (f2fs_has_inline_data(inode)) {
2872 err = f2fs_write_inline_data(inode, page);
2877 if (err == -EAGAIN) {
2878 err = f2fs_do_write_data_page(&fio);
2879 if (err == -EAGAIN) {
2880 fio.need_lock = LOCK_REQ;
2881 err = f2fs_do_write_data_page(&fio);
2886 file_set_keep_isize(inode);
2888 spin_lock(&F2FS_I(inode)->i_size_lock);
2889 if (F2FS_I(inode)->last_disk_size < psize)
2890 F2FS_I(inode)->last_disk_size = psize;
2891 spin_unlock(&F2FS_I(inode)->i_size_lock);
2895 if (err && err != -ENOENT)
2899 inode_dec_dirty_pages(inode);
2901 ClearPageUptodate(page);
2902 clear_page_private_gcing(page);
2905 if (wbc->for_reclaim) {
2906 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2907 clear_inode_flag(inode, FI_HOT_DATA);
2908 f2fs_remove_dirty_inode(inode);
2912 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2913 !F2FS_I(inode)->cp_task && allow_balance)
2914 f2fs_balance_fs(sbi, need_balance_fs);
2916 if (unlikely(f2fs_cp_error(sbi))) {
2917 f2fs_submit_merged_write(sbi, DATA);
2918 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2923 *submitted = fio.submitted ? 1 : 0;
2928 redirty_page_for_writepage(wbc, page);
2930 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2931 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2932 * file_write_and_wait_range() will see EIO error, which is critical
2933 * to return value of fsync() followed by atomic_write failure to user.
2935 if (!err || wbc->for_reclaim)
2936 return AOP_WRITEPAGE_ACTIVATE;
2941 static int f2fs_write_data_page(struct page *page,
2942 struct writeback_control *wbc)
2944 #ifdef CONFIG_F2FS_FS_COMPRESSION
2945 struct inode *inode = page->mapping->host;
2947 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2950 if (f2fs_compressed_file(inode)) {
2951 if (f2fs_is_compressed_cluster(inode, page->index)) {
2952 redirty_page_for_writepage(wbc, page);
2953 return AOP_WRITEPAGE_ACTIVATE;
2959 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2960 wbc, FS_DATA_IO, 0, true);
2964 * This function was copied from write_cche_pages from mm/page-writeback.c.
2965 * The major change is making write step of cold data page separately from
2966 * warm/hot data page.
2968 static int f2fs_write_cache_pages(struct address_space *mapping,
2969 struct writeback_control *wbc,
2970 enum iostat_type io_type)
2973 int done = 0, retry = 0;
2974 struct pagevec pvec;
2975 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2976 struct bio *bio = NULL;
2977 sector_t last_block;
2978 #ifdef CONFIG_F2FS_FS_COMPRESSION
2979 struct inode *inode = mapping->host;
2980 struct compress_ctx cc = {
2982 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2983 .cluster_size = F2FS_I(inode)->i_cluster_size,
2984 .cluster_idx = NULL_CLUSTER,
2990 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2996 pgoff_t end; /* Inclusive */
2998 int range_whole = 0;
3004 pagevec_init(&pvec);
3006 if (get_dirty_pages(mapping->host) <=
3007 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
3008 set_inode_flag(mapping->host, FI_HOT_DATA);
3010 clear_inode_flag(mapping->host, FI_HOT_DATA);
3012 if (wbc->range_cyclic) {
3013 index = mapping->writeback_index; /* prev offset */
3016 index = wbc->range_start >> PAGE_SHIFT;
3017 end = wbc->range_end >> PAGE_SHIFT;
3018 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3021 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3022 tag = PAGECACHE_TAG_TOWRITE;
3024 tag = PAGECACHE_TAG_DIRTY;
3027 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3028 tag_pages_for_writeback(mapping, index, end);
3030 while (!done && !retry && (index <= end)) {
3031 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
3036 for (i = 0; i < nr_pages; i++) {
3037 struct page *page = pvec.pages[i];
3041 #ifdef CONFIG_F2FS_FS_COMPRESSION
3042 if (f2fs_compressed_file(inode)) {
3043 void *fsdata = NULL;
3047 ret = f2fs_init_compress_ctx(&cc);
3053 if (!f2fs_cluster_can_merge_page(&cc,
3055 ret = f2fs_write_multi_pages(&cc,
3056 &submitted, wbc, io_type);
3062 if (unlikely(f2fs_cp_error(sbi)))
3065 if (!f2fs_cluster_is_empty(&cc))
3068 ret2 = f2fs_prepare_compress_overwrite(
3070 page->index, &fsdata);
3076 (!f2fs_compress_write_end(inode,
3077 fsdata, page->index, 1) ||
3078 !f2fs_all_cluster_page_loaded(&cc,
3079 &pvec, i, nr_pages))) {
3085 /* give a priority to WB_SYNC threads */
3086 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3087 wbc->sync_mode == WB_SYNC_NONE) {
3091 #ifdef CONFIG_F2FS_FS_COMPRESSION
3094 done_index = page->index;
3098 if (unlikely(page->mapping != mapping)) {
3104 if (!PageDirty(page)) {
3105 /* someone wrote it for us */
3106 goto continue_unlock;
3109 if (PageWriteback(page)) {
3110 if (wbc->sync_mode != WB_SYNC_NONE)
3111 f2fs_wait_on_page_writeback(page,
3114 goto continue_unlock;
3117 if (!clear_page_dirty_for_io(page))
3118 goto continue_unlock;
3120 #ifdef CONFIG_F2FS_FS_COMPRESSION
3121 if (f2fs_compressed_file(inode)) {
3123 f2fs_compress_ctx_add_page(&cc, page);
3127 ret = f2fs_write_single_data_page(page, &submitted,
3128 &bio, &last_block, wbc, io_type,
3130 if (ret == AOP_WRITEPAGE_ACTIVATE)
3132 #ifdef CONFIG_F2FS_FS_COMPRESSION
3135 nwritten += submitted;
3136 wbc->nr_to_write -= submitted;
3138 if (unlikely(ret)) {
3140 * keep nr_to_write, since vfs uses this to
3141 * get # of written pages.
3143 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3146 } else if (ret == -EAGAIN) {
3148 if (wbc->sync_mode == WB_SYNC_ALL) {
3150 congestion_wait(BLK_RW_ASYNC,
3151 DEFAULT_IO_TIMEOUT);
3156 done_index = page->index + 1;
3161 if (wbc->nr_to_write <= 0 &&
3162 wbc->sync_mode == WB_SYNC_NONE) {
3170 pagevec_release(&pvec);
3173 #ifdef CONFIG_F2FS_FS_COMPRESSION
3174 /* flush remained pages in compress cluster */
3175 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3176 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3177 nwritten += submitted;
3178 wbc->nr_to_write -= submitted;
3184 if (f2fs_compressed_file(inode))
3185 f2fs_destroy_compress_ctx(&cc, false);
3192 if (wbc->range_cyclic && !done)
3194 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3195 mapping->writeback_index = done_index;
3198 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3200 /* submit cached bio of IPU write */
3202 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3207 static inline bool __should_serialize_io(struct inode *inode,
3208 struct writeback_control *wbc)
3210 /* to avoid deadlock in path of data flush */
3211 if (F2FS_I(inode)->cp_task)
3214 if (!S_ISREG(inode->i_mode))
3216 if (IS_NOQUOTA(inode))
3219 if (f2fs_need_compress_data(inode))
3221 if (wbc->sync_mode != WB_SYNC_ALL)
3223 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3228 static int __f2fs_write_data_pages(struct address_space *mapping,
3229 struct writeback_control *wbc,
3230 enum iostat_type io_type)
3232 struct inode *inode = mapping->host;
3233 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3234 struct blk_plug plug;
3236 bool locked = false;
3238 /* deal with chardevs and other special file */
3239 if (!mapping->a_ops->writepage)
3242 /* skip writing if there is no dirty page in this inode */
3243 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3246 /* during POR, we don't need to trigger writepage at all. */
3247 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3250 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3251 wbc->sync_mode == WB_SYNC_NONE &&
3252 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3253 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3256 /* skip writing during file defragment */
3257 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3260 trace_f2fs_writepages(mapping->host, wbc, DATA);
3262 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3263 if (wbc->sync_mode == WB_SYNC_ALL)
3264 atomic_inc(&sbi->wb_sync_req[DATA]);
3265 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3268 if (__should_serialize_io(inode, wbc)) {
3269 mutex_lock(&sbi->writepages);
3273 blk_start_plug(&plug);
3274 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3275 blk_finish_plug(&plug);
3278 mutex_unlock(&sbi->writepages);
3280 if (wbc->sync_mode == WB_SYNC_ALL)
3281 atomic_dec(&sbi->wb_sync_req[DATA]);
3283 * if some pages were truncated, we cannot guarantee its mapping->host
3284 * to detect pending bios.
3287 f2fs_remove_dirty_inode(inode);
3291 wbc->pages_skipped += get_dirty_pages(inode);
3292 trace_f2fs_writepages(mapping->host, wbc, DATA);
3296 static int f2fs_write_data_pages(struct address_space *mapping,
3297 struct writeback_control *wbc)
3299 struct inode *inode = mapping->host;
3301 return __f2fs_write_data_pages(mapping, wbc,
3302 F2FS_I(inode)->cp_task == current ?
3303 FS_CP_DATA_IO : FS_DATA_IO);
3306 static void f2fs_write_failed(struct inode *inode, loff_t to)
3308 loff_t i_size = i_size_read(inode);
3310 if (IS_NOQUOTA(inode))
3313 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3314 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3315 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3316 filemap_invalidate_lock(inode->i_mapping);
3318 truncate_pagecache(inode, i_size);
3319 f2fs_truncate_blocks(inode, i_size, true);
3321 filemap_invalidate_unlock(inode->i_mapping);
3322 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3326 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3327 struct page *page, loff_t pos, unsigned len,
3328 block_t *blk_addr, bool *node_changed)
3330 struct inode *inode = page->mapping->host;
3331 pgoff_t index = page->index;
3332 struct dnode_of_data dn;
3334 bool locked = false;
3335 struct extent_info ei = {0, };
3340 * we already allocated all the blocks, so we don't need to get
3341 * the block addresses when there is no need to fill the page.
3343 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3344 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3345 !f2fs_verity_in_progress(inode))
3348 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3349 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3350 flag = F2FS_GET_BLOCK_DEFAULT;
3352 flag = F2FS_GET_BLOCK_PRE_AIO;
3354 if (f2fs_has_inline_data(inode) ||
3355 (pos & PAGE_MASK) >= i_size_read(inode)) {
3356 f2fs_do_map_lock(sbi, flag, true);
3361 /* check inline_data */
3362 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3363 if (IS_ERR(ipage)) {
3364 err = PTR_ERR(ipage);
3368 set_new_dnode(&dn, inode, ipage, ipage, 0);
3370 if (f2fs_has_inline_data(inode)) {
3371 if (pos + len <= MAX_INLINE_DATA(inode)) {
3372 f2fs_do_read_inline_data(page, ipage);
3373 set_inode_flag(inode, FI_DATA_EXIST);
3375 set_page_private_inline(ipage);
3377 err = f2fs_convert_inline_page(&dn, page);
3380 if (dn.data_blkaddr == NULL_ADDR)
3381 err = f2fs_get_block(&dn, index);
3383 } else if (locked) {
3384 err = f2fs_get_block(&dn, index);
3386 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3387 dn.data_blkaddr = ei.blk + index - ei.fofs;
3390 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3391 if (err || dn.data_blkaddr == NULL_ADDR) {
3392 f2fs_put_dnode(&dn);
3393 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3395 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3402 /* convert_inline_page can make node_changed */
3403 *blk_addr = dn.data_blkaddr;
3404 *node_changed = dn.node_changed;
3406 f2fs_put_dnode(&dn);
3409 f2fs_do_map_lock(sbi, flag, false);
3413 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3414 loff_t pos, unsigned len, unsigned flags,
3415 struct page **pagep, void **fsdata)
3417 struct inode *inode = mapping->host;
3418 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3419 struct page *page = NULL;
3420 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3421 bool need_balance = false, drop_atomic = false;
3422 block_t blkaddr = NULL_ADDR;
3425 trace_f2fs_write_begin(inode, pos, len, flags);
3427 if (!f2fs_is_checkpoint_ready(sbi)) {
3432 if ((f2fs_is_atomic_file(inode) &&
3433 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3434 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3441 * We should check this at this moment to avoid deadlock on inode page
3442 * and #0 page. The locking rule for inline_data conversion should be:
3443 * lock_page(page #0) -> lock_page(inode_page)
3446 err = f2fs_convert_inline_inode(inode);
3451 #ifdef CONFIG_F2FS_FS_COMPRESSION
3452 if (f2fs_compressed_file(inode)) {
3457 if (len == PAGE_SIZE)
3460 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3473 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3474 * wait_for_stable_page. Will wait that below with our IO control.
3476 page = f2fs_pagecache_get_page(mapping, index,
3477 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3483 /* TODO: cluster can be compressed due to race with .writepage */
3487 err = prepare_write_begin(sbi, page, pos, len,
3488 &blkaddr, &need_balance);
3492 if (need_balance && !IS_NOQUOTA(inode) &&
3493 has_not_enough_free_secs(sbi, 0, 0)) {
3495 f2fs_balance_fs(sbi, true);
3497 if (page->mapping != mapping) {
3498 /* The page got truncated from under us */
3499 f2fs_put_page(page, 1);
3504 f2fs_wait_on_page_writeback(page, DATA, false, true);
3506 if (len == PAGE_SIZE || PageUptodate(page))
3509 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3510 !f2fs_verity_in_progress(inode)) {
3511 zero_user_segment(page, len, PAGE_SIZE);
3515 if (blkaddr == NEW_ADDR) {
3516 zero_user_segment(page, 0, PAGE_SIZE);
3517 SetPageUptodate(page);
3519 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3520 DATA_GENERIC_ENHANCE_READ)) {
3521 err = -EFSCORRUPTED;
3524 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3529 if (unlikely(page->mapping != mapping)) {
3530 f2fs_put_page(page, 1);
3533 if (unlikely(!PageUptodate(page))) {
3541 f2fs_put_page(page, 1);
3542 f2fs_write_failed(inode, pos + len);
3544 f2fs_drop_inmem_pages_all(sbi, false);
3548 static int f2fs_write_end(struct file *file,
3549 struct address_space *mapping,
3550 loff_t pos, unsigned len, unsigned copied,
3551 struct page *page, void *fsdata)
3553 struct inode *inode = page->mapping->host;
3555 trace_f2fs_write_end(inode, pos, len, copied);
3558 * This should be come from len == PAGE_SIZE, and we expect copied
3559 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3560 * let generic_perform_write() try to copy data again through copied=0.
3562 if (!PageUptodate(page)) {
3563 if (unlikely(copied != len))
3566 SetPageUptodate(page);
3569 #ifdef CONFIG_F2FS_FS_COMPRESSION
3570 /* overwrite compressed file */
3571 if (f2fs_compressed_file(inode) && fsdata) {
3572 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3573 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3575 if (pos + copied > i_size_read(inode) &&
3576 !f2fs_verity_in_progress(inode))
3577 f2fs_i_size_write(inode, pos + copied);
3585 set_page_dirty(page);
3587 if (pos + copied > i_size_read(inode) &&
3588 !f2fs_verity_in_progress(inode))
3589 f2fs_i_size_write(inode, pos + copied);
3591 f2fs_put_page(page, 1);
3592 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3596 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3599 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3600 unsigned blkbits = i_blkbits;
3601 unsigned blocksize_mask = (1 << blkbits) - 1;
3602 unsigned long align = offset | iov_iter_alignment(iter);
3603 struct block_device *bdev = inode->i_sb->s_bdev;
3605 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3608 if (align & blocksize_mask) {
3610 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3611 blocksize_mask = (1 << blkbits) - 1;
3612 if (align & blocksize_mask)
3619 static void f2fs_dio_end_io(struct bio *bio)
3621 struct f2fs_private_dio *dio = bio->bi_private;
3623 dec_page_count(F2FS_I_SB(dio->inode),
3624 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3626 bio->bi_private = dio->orig_private;
3627 bio->bi_end_io = dio->orig_end_io;
3634 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3637 struct f2fs_private_dio *dio;
3638 bool write = (bio_op(bio) == REQ_OP_WRITE);
3640 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3641 sizeof(struct f2fs_private_dio), GFP_NOFS);
3646 dio->orig_end_io = bio->bi_end_io;
3647 dio->orig_private = bio->bi_private;
3650 bio->bi_end_io = f2fs_dio_end_io;
3651 bio->bi_private = dio;
3653 inc_page_count(F2FS_I_SB(inode),
3654 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3659 bio->bi_status = BLK_STS_IOERR;
3663 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3665 struct address_space *mapping = iocb->ki_filp->f_mapping;
3666 struct inode *inode = mapping->host;
3667 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3668 struct f2fs_inode_info *fi = F2FS_I(inode);
3669 size_t count = iov_iter_count(iter);
3670 loff_t offset = iocb->ki_pos;
3671 int rw = iov_iter_rw(iter);
3673 enum rw_hint hint = iocb->ki_hint;
3674 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3677 err = check_direct_IO(inode, iter, offset);
3679 return err < 0 ? err : 0;
3681 if (f2fs_force_buffered_io(inode, iocb, iter))
3684 do_opu = rw == WRITE && f2fs_lfs_mode(sbi);
3686 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3688 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3689 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3691 if (iocb->ki_flags & IOCB_NOWAIT) {
3692 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3693 iocb->ki_hint = hint;
3697 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3698 up_read(&fi->i_gc_rwsem[rw]);
3699 iocb->ki_hint = hint;
3704 down_read(&fi->i_gc_rwsem[rw]);
3706 down_read(&fi->i_gc_rwsem[READ]);
3709 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3710 iter, rw == WRITE ? get_data_block_dio_write :
3711 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3712 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3716 up_read(&fi->i_gc_rwsem[READ]);
3718 up_read(&fi->i_gc_rwsem[rw]);
3721 if (whint_mode == WHINT_MODE_OFF)
3722 iocb->ki_hint = hint;
3724 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3727 set_inode_flag(inode, FI_UPDATE_WRITE);
3728 } else if (err == -EIOCBQUEUED) {
3729 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3730 count - iov_iter_count(iter));
3731 } else if (err < 0) {
3732 f2fs_write_failed(inode, offset + count);
3736 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3737 else if (err == -EIOCBQUEUED)
3738 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3739 count - iov_iter_count(iter));
3743 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3748 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3749 unsigned int length)
3751 struct inode *inode = page->mapping->host;
3752 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3754 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3755 (offset % PAGE_SIZE || length != PAGE_SIZE))
3758 if (PageDirty(page)) {
3759 if (inode->i_ino == F2FS_META_INO(sbi)) {
3760 dec_page_count(sbi, F2FS_DIRTY_META);
3761 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3762 dec_page_count(sbi, F2FS_DIRTY_NODES);
3764 inode_dec_dirty_pages(inode);
3765 f2fs_remove_dirty_inode(inode);
3769 clear_page_private_gcing(page);
3771 if (test_opt(sbi, COMPRESS_CACHE)) {
3772 if (f2fs_compressed_file(inode))
3773 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3774 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3775 clear_page_private_data(page);
3778 if (page_private_atomic(page))
3779 return f2fs_drop_inmem_page(inode, page);
3781 detach_page_private(page);
3782 set_page_private(page, 0);
3785 int f2fs_release_page(struct page *page, gfp_t wait)
3787 /* If this is dirty page, keep PagePrivate */
3788 if (PageDirty(page))
3791 /* This is atomic written page, keep Private */
3792 if (page_private_atomic(page))
3795 if (test_opt(F2FS_P_SB(page), COMPRESS_CACHE)) {
3796 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3797 struct inode *inode = page->mapping->host;
3799 if (f2fs_compressed_file(inode))
3800 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3801 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3802 clear_page_private_data(page);
3805 clear_page_private_gcing(page);
3807 detach_page_private(page);
3808 set_page_private(page, 0);
3812 static int f2fs_set_data_page_dirty(struct page *page)
3814 struct inode *inode = page_file_mapping(page)->host;
3816 trace_f2fs_set_page_dirty(page, DATA);
3818 if (!PageUptodate(page))
3819 SetPageUptodate(page);
3820 if (PageSwapCache(page))
3821 return __set_page_dirty_nobuffers(page);
3823 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3824 if (!page_private_atomic(page)) {
3825 f2fs_register_inmem_page(inode, page);
3829 * Previously, this page has been registered, we just
3835 if (!PageDirty(page)) {
3836 __set_page_dirty_nobuffers(page);
3837 f2fs_update_dirty_page(inode, page);
3844 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3846 #ifdef CONFIG_F2FS_FS_COMPRESSION
3847 struct dnode_of_data dn;
3848 sector_t start_idx, blknr = 0;
3851 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3853 set_new_dnode(&dn, inode, NULL, NULL, 0);
3854 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3858 if (dn.data_blkaddr != COMPRESS_ADDR) {
3859 dn.ofs_in_node += block - start_idx;
3860 blknr = f2fs_data_blkaddr(&dn);
3861 if (!__is_valid_data_blkaddr(blknr))
3865 f2fs_put_dnode(&dn);
3873 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3875 struct inode *inode = mapping->host;
3878 if (f2fs_has_inline_data(inode))
3881 /* make sure allocating whole blocks */
3882 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3883 filemap_write_and_wait(mapping);
3885 /* Block number less than F2FS MAX BLOCKS */
3886 if (unlikely(block >= max_file_blocks(inode)))
3889 if (f2fs_compressed_file(inode)) {
3890 blknr = f2fs_bmap_compress(inode, block);
3892 struct f2fs_map_blocks map;
3894 memset(&map, 0, sizeof(map));
3897 map.m_next_pgofs = NULL;
3898 map.m_seg_type = NO_CHECK_TYPE;
3900 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3904 trace_f2fs_bmap(inode, block, blknr);
3908 #ifdef CONFIG_MIGRATION
3909 #include <linux/migrate.h>
3911 int f2fs_migrate_page(struct address_space *mapping,
3912 struct page *newpage, struct page *page, enum migrate_mode mode)
3914 int rc, extra_count;
3915 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3916 bool atomic_written = page_private_atomic(page);
3918 BUG_ON(PageWriteback(page));
3920 /* migrating an atomic written page is safe with the inmem_lock hold */
3921 if (atomic_written) {
3922 if (mode != MIGRATE_SYNC)
3924 if (!mutex_trylock(&fi->inmem_lock))
3928 /* one extra reference was held for atomic_write page */
3929 extra_count = atomic_written ? 1 : 0;
3930 rc = migrate_page_move_mapping(mapping, newpage,
3932 if (rc != MIGRATEPAGE_SUCCESS) {
3934 mutex_unlock(&fi->inmem_lock);
3938 if (atomic_written) {
3939 struct inmem_pages *cur;
3941 list_for_each_entry(cur, &fi->inmem_pages, list)
3942 if (cur->page == page) {
3943 cur->page = newpage;
3946 mutex_unlock(&fi->inmem_lock);
3951 /* guarantee to start from no stale private field */
3952 set_page_private(newpage, 0);
3953 if (PagePrivate(page)) {
3954 set_page_private(newpage, page_private(page));
3955 SetPagePrivate(newpage);
3958 set_page_private(page, 0);
3959 ClearPagePrivate(page);
3963 if (mode != MIGRATE_SYNC_NO_COPY)
3964 migrate_page_copy(newpage, page);
3966 migrate_page_states(newpage, page);
3968 return MIGRATEPAGE_SUCCESS;
3973 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3974 unsigned int blkcnt)
3976 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3977 unsigned int blkofs;
3978 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3979 unsigned int secidx = start_blk / blk_per_sec;
3980 unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3983 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3984 filemap_invalidate_lock(inode->i_mapping);
3986 set_inode_flag(inode, FI_ALIGNED_WRITE);
3988 for (; secidx < end_sec; secidx++) {
3989 down_write(&sbi->pin_sem);
3992 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
3993 f2fs_unlock_op(sbi);
3995 set_inode_flag(inode, FI_DO_DEFRAG);
3997 for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
3999 unsigned int blkidx = secidx * blk_per_sec + blkofs;
4001 page = f2fs_get_lock_data_page(inode, blkidx, true);
4003 up_write(&sbi->pin_sem);
4004 ret = PTR_ERR(page);
4008 set_page_dirty(page);
4009 f2fs_put_page(page, 1);
4012 clear_inode_flag(inode, FI_DO_DEFRAG);
4014 ret = filemap_fdatawrite(inode->i_mapping);
4016 up_write(&sbi->pin_sem);
4023 clear_inode_flag(inode, FI_DO_DEFRAG);
4024 clear_inode_flag(inode, FI_ALIGNED_WRITE);
4026 filemap_invalidate_unlock(inode->i_mapping);
4027 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4032 static int check_swap_activate(struct swap_info_struct *sis,
4033 struct file *swap_file, sector_t *span)
4035 struct address_space *mapping = swap_file->f_mapping;
4036 struct inode *inode = mapping->host;
4037 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4038 sector_t cur_lblock;
4039 sector_t last_lblock;
4041 sector_t lowest_pblock = -1;
4042 sector_t highest_pblock = 0;
4044 unsigned long nr_pblocks;
4045 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
4046 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
4047 unsigned int not_aligned = 0;
4051 * Map all the blocks into the extent list. This code doesn't try
4055 last_lblock = bytes_to_blks(inode, i_size_read(inode));
4057 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
4058 struct f2fs_map_blocks map;
4062 memset(&map, 0, sizeof(map));
4063 map.m_lblk = cur_lblock;
4064 map.m_len = last_lblock - cur_lblock;
4065 map.m_next_pgofs = NULL;
4066 map.m_next_extent = NULL;
4067 map.m_seg_type = NO_CHECK_TYPE;
4068 map.m_may_create = false;
4070 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
4075 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
4076 f2fs_err(sbi, "Swapfile has holes");
4081 pblock = map.m_pblk;
4082 nr_pblocks = map.m_len;
4084 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
4085 nr_pblocks & sec_blks_mask) {
4088 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
4089 if (cur_lblock + nr_pblocks > sis->max)
4090 nr_pblocks -= blks_per_sec;
4093 /* this extent is last one */
4094 nr_pblocks = map.m_len;
4095 f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
4099 ret = f2fs_migrate_blocks(inode, cur_lblock,
4106 if (cur_lblock + nr_pblocks >= sis->max)
4107 nr_pblocks = sis->max - cur_lblock;
4109 if (cur_lblock) { /* exclude the header page */
4110 if (pblock < lowest_pblock)
4111 lowest_pblock = pblock;
4112 if (pblock + nr_pblocks - 1 > highest_pblock)
4113 highest_pblock = pblock + nr_pblocks - 1;
4117 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
4119 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
4123 cur_lblock += nr_pblocks;
4126 *span = 1 + highest_pblock - lowest_pblock;
4127 if (cur_lblock == 0)
4128 cur_lblock = 1; /* force Empty message */
4129 sis->max = cur_lblock;
4130 sis->pages = cur_lblock - 1;
4131 sis->highest_bit = cur_lblock - 1;
4134 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
4135 not_aligned, blks_per_sec * F2FS_BLKSIZE);
4139 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4142 struct inode *inode = file_inode(file);
4145 if (!S_ISREG(inode->i_mode))
4148 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
4151 if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
4152 f2fs_err(F2FS_I_SB(inode),
4153 "Swapfile not supported in LFS mode");
4157 ret = f2fs_convert_inline_inode(inode);
4161 if (!f2fs_disable_compressed_file(inode))
4164 f2fs_precache_extents(inode);
4166 ret = check_swap_activate(sis, file, span);
4170 set_inode_flag(inode, FI_PIN_FILE);
4171 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4175 static void f2fs_swap_deactivate(struct file *file)
4177 struct inode *inode = file_inode(file);
4179 clear_inode_flag(inode, FI_PIN_FILE);
4182 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4188 static void f2fs_swap_deactivate(struct file *file)
4193 const struct address_space_operations f2fs_dblock_aops = {
4194 .readpage = f2fs_read_data_page,
4195 .readahead = f2fs_readahead,
4196 .writepage = f2fs_write_data_page,
4197 .writepages = f2fs_write_data_pages,
4198 .write_begin = f2fs_write_begin,
4199 .write_end = f2fs_write_end,
4200 .set_page_dirty = f2fs_set_data_page_dirty,
4201 .invalidatepage = f2fs_invalidate_page,
4202 .releasepage = f2fs_release_page,
4203 .direct_IO = f2fs_direct_IO,
4205 .swap_activate = f2fs_swap_activate,
4206 .swap_deactivate = f2fs_swap_deactivate,
4207 #ifdef CONFIG_MIGRATION
4208 .migratepage = f2fs_migrate_page,
4212 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4214 struct address_space *mapping = page_mapping(page);
4215 unsigned long flags;
4217 xa_lock_irqsave(&mapping->i_pages, flags);
4218 __xa_clear_mark(&mapping->i_pages, page_index(page),
4219 PAGECACHE_TAG_DIRTY);
4220 xa_unlock_irqrestore(&mapping->i_pages, flags);
4223 int __init f2fs_init_post_read_processing(void)
4225 bio_post_read_ctx_cache =
4226 kmem_cache_create("f2fs_bio_post_read_ctx",
4227 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4228 if (!bio_post_read_ctx_cache)
4230 bio_post_read_ctx_pool =
4231 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4232 bio_post_read_ctx_cache);
4233 if (!bio_post_read_ctx_pool)
4234 goto fail_free_cache;
4238 kmem_cache_destroy(bio_post_read_ctx_cache);
4243 void f2fs_destroy_post_read_processing(void)
4245 mempool_destroy(bio_post_read_ctx_pool);
4246 kmem_cache_destroy(bio_post_read_ctx_cache);
4249 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4251 if (!f2fs_sb_has_encrypt(sbi) &&
4252 !f2fs_sb_has_verity(sbi) &&
4253 !f2fs_sb_has_compression(sbi))
4256 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4257 WQ_UNBOUND | WQ_HIGHPRI,
4259 if (!sbi->post_read_wq)
4264 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4266 if (sbi->post_read_wq)
4267 destroy_workqueue(sbi->post_read_wq);
4270 int __init f2fs_init_bio_entry_cache(void)
4272 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4273 sizeof(struct bio_entry));
4274 if (!bio_entry_slab)
4279 void f2fs_destroy_bio_entry_cache(void)
4281 kmem_cache_destroy(bio_entry_slab);