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/swap.h>
18 #include <linux/prefetch.h>
19 #include <linux/uio.h>
20 #include <linux/cleancache.h>
21 #include <linux/sched/signal.h>
22 #include <linux/fiemap.h>
28 #include <trace/events/f2fs.h>
30 #define NUM_PREALLOC_POST_READ_CTXS 128
32 static struct kmem_cache *bio_post_read_ctx_cache;
33 static struct kmem_cache *bio_entry_slab;
34 static mempool_t *bio_post_read_ctx_pool;
35 static struct bio_set f2fs_bioset;
37 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
39 int __init f2fs_init_bioset(void)
41 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
42 0, BIOSET_NEED_BVECS))
47 void f2fs_destroy_bioset(void)
49 bioset_exit(&f2fs_bioset);
52 static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
53 unsigned int nr_iovecs)
55 return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
58 struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio)
61 /* No failure on bio allocation */
62 return __f2fs_bio_alloc(GFP_NOIO, npages);
65 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
66 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
70 return __f2fs_bio_alloc(GFP_KERNEL, npages);
73 static bool __is_cp_guaranteed(struct page *page)
75 struct address_space *mapping = page->mapping;
77 struct f2fs_sb_info *sbi;
82 if (f2fs_is_compressed_page(page))
85 inode = mapping->host;
86 sbi = F2FS_I_SB(inode);
88 if (inode->i_ino == F2FS_META_INO(sbi) ||
89 inode->i_ino == F2FS_NODE_INO(sbi) ||
90 S_ISDIR(inode->i_mode) ||
91 (S_ISREG(inode->i_mode) &&
92 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
98 static enum count_type __read_io_type(struct page *page)
100 struct address_space *mapping = page_file_mapping(page);
103 struct inode *inode = mapping->host;
104 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
106 if (inode->i_ino == F2FS_META_INO(sbi))
109 if (inode->i_ino == F2FS_NODE_INO(sbi))
115 /* postprocessing steps for read bios */
116 enum bio_post_read_step {
118 STEP_DECOMPRESS_NOWQ, /* handle normal cluster data inplace */
119 STEP_DECOMPRESS, /* handle compressed cluster data in workqueue */
123 struct bio_post_read_ctx {
125 struct f2fs_sb_info *sbi;
126 struct work_struct work;
127 unsigned int enabled_steps;
130 static void __read_end_io(struct bio *bio, bool compr, bool verity)
134 struct bvec_iter_all iter_all;
136 bio_for_each_segment_all(bv, bio, iter_all) {
139 #ifdef CONFIG_F2FS_FS_COMPRESSION
140 if (compr && f2fs_is_compressed_page(page)) {
141 f2fs_decompress_pages(bio, page, verity);
148 /* PG_error was set if any post_read step failed */
149 if (bio->bi_status || PageError(page)) {
150 ClearPageUptodate(page);
151 /* will re-read again later */
152 ClearPageError(page);
154 SetPageUptodate(page);
156 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
161 static void f2fs_release_read_bio(struct bio *bio);
162 static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
165 __read_end_io(bio, false, verity);
166 f2fs_release_read_bio(bio);
169 static void f2fs_decompress_bio(struct bio *bio, bool verity)
171 __read_end_io(bio, true, verity);
174 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
176 static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
178 fscrypt_decrypt_bio(ctx->bio);
181 static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
183 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
186 #ifdef CONFIG_F2FS_FS_COMPRESSION
187 static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
189 f2fs_decompress_end_io(rpages, cluster_size, false, true);
192 static void f2fs_verify_bio(struct bio *bio)
195 struct bvec_iter_all iter_all;
197 bio_for_each_segment_all(bv, bio, iter_all) {
198 struct page *page = bv->bv_page;
199 struct decompress_io_ctx *dic;
201 dic = (struct decompress_io_ctx *)page_private(page);
204 if (refcount_dec_not_one(&dic->ref))
206 f2fs_verify_pages(dic->rpages,
212 if (bio->bi_status || PageError(page))
215 if (fsverity_verify_page(page)) {
216 SetPageUptodate(page);
220 ClearPageUptodate(page);
221 ClearPageError(page);
223 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
229 static void f2fs_verity_work(struct work_struct *work)
231 struct bio_post_read_ctx *ctx =
232 container_of(work, struct bio_post_read_ctx, work);
233 struct bio *bio = ctx->bio;
234 #ifdef CONFIG_F2FS_FS_COMPRESSION
235 unsigned int enabled_steps = ctx->enabled_steps;
239 * fsverity_verify_bio() may call readpages() again, and while verity
240 * will be disabled for this, decryption may still be needed, resulting
241 * in another bio_post_read_ctx being allocated. So to prevent
242 * deadlocks we need to release the current ctx to the mempool first.
243 * This assumes that verity is the last post-read step.
245 mempool_free(ctx, bio_post_read_ctx_pool);
246 bio->bi_private = NULL;
248 #ifdef CONFIG_F2FS_FS_COMPRESSION
249 /* previous step is decompression */
250 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
251 f2fs_verify_bio(bio);
252 f2fs_release_read_bio(bio);
257 fsverity_verify_bio(bio);
258 __f2fs_read_end_io(bio, false, false);
261 static void f2fs_post_read_work(struct work_struct *work)
263 struct bio_post_read_ctx *ctx =
264 container_of(work, struct bio_post_read_ctx, work);
266 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
267 f2fs_decrypt_work(ctx);
269 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
270 f2fs_decompress_work(ctx);
272 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
273 INIT_WORK(&ctx->work, f2fs_verity_work);
274 fsverity_enqueue_verify_work(&ctx->work);
278 __f2fs_read_end_io(ctx->bio,
279 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
282 static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
283 struct work_struct *work)
285 queue_work(sbi->post_read_wq, work);
288 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
291 * We use different work queues for decryption and for verity because
292 * verity may require reading metadata pages that need decryption, and
293 * we shouldn't recurse to the same workqueue.
296 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
297 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
298 INIT_WORK(&ctx->work, f2fs_post_read_work);
299 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
303 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
304 INIT_WORK(&ctx->work, f2fs_verity_work);
305 fsverity_enqueue_verify_work(&ctx->work);
309 __f2fs_read_end_io(ctx->bio, false, false);
312 static bool f2fs_bio_post_read_required(struct bio *bio)
314 return bio->bi_private;
317 static void f2fs_read_end_io(struct bio *bio)
319 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
321 if (time_to_inject(sbi, FAULT_READ_IO)) {
322 f2fs_show_injection_info(sbi, FAULT_READ_IO);
323 bio->bi_status = BLK_STS_IOERR;
326 if (f2fs_bio_post_read_required(bio)) {
327 struct bio_post_read_ctx *ctx = bio->bi_private;
329 bio_post_read_processing(ctx);
333 __f2fs_read_end_io(bio, false, false);
336 static void f2fs_write_end_io(struct bio *bio)
338 struct f2fs_sb_info *sbi = bio->bi_private;
339 struct bio_vec *bvec;
340 struct bvec_iter_all iter_all;
342 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
343 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
344 bio->bi_status = BLK_STS_IOERR;
347 bio_for_each_segment_all(bvec, bio, iter_all) {
348 struct page *page = bvec->bv_page;
349 enum count_type type = WB_DATA_TYPE(page);
351 if (IS_DUMMY_WRITTEN_PAGE(page)) {
352 set_page_private(page, (unsigned long)NULL);
353 ClearPagePrivate(page);
355 mempool_free(page, sbi->write_io_dummy);
357 if (unlikely(bio->bi_status))
358 f2fs_stop_checkpoint(sbi, true);
362 fscrypt_finalize_bounce_page(&page);
364 #ifdef CONFIG_F2FS_FS_COMPRESSION
365 if (f2fs_is_compressed_page(page)) {
366 f2fs_compress_write_end_io(bio, page);
371 if (unlikely(bio->bi_status)) {
372 mapping_set_error(page->mapping, -EIO);
373 if (type == F2FS_WB_CP_DATA)
374 f2fs_stop_checkpoint(sbi, true);
377 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
378 page->index != nid_of_node(page));
380 dec_page_count(sbi, type);
381 if (f2fs_in_warm_node_list(sbi, page))
382 f2fs_del_fsync_node_entry(sbi, page);
383 clear_cold_data(page);
384 end_page_writeback(page);
386 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
387 wq_has_sleeper(&sbi->cp_wait))
388 wake_up(&sbi->cp_wait);
393 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
394 block_t blk_addr, struct bio *bio)
396 struct block_device *bdev = sbi->sb->s_bdev;
399 if (f2fs_is_multi_device(sbi)) {
400 for (i = 0; i < sbi->s_ndevs; i++) {
401 if (FDEV(i).start_blk <= blk_addr &&
402 FDEV(i).end_blk >= blk_addr) {
403 blk_addr -= FDEV(i).start_blk;
410 bio_set_dev(bio, bdev);
411 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
416 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
420 if (!f2fs_is_multi_device(sbi))
423 for (i = 0; i < sbi->s_ndevs; i++)
424 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
430 * Return true, if pre_bio's bdev is same as its target device.
432 static bool __same_bdev(struct f2fs_sb_info *sbi,
433 block_t blk_addr, struct bio *bio)
435 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
436 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
439 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
441 struct f2fs_sb_info *sbi = fio->sbi;
444 bio = f2fs_bio_alloc(sbi, npages, true);
446 f2fs_target_device(sbi, fio->new_blkaddr, bio);
447 if (is_read_io(fio->op)) {
448 bio->bi_end_io = f2fs_read_end_io;
449 bio->bi_private = NULL;
451 bio->bi_end_io = f2fs_write_end_io;
452 bio->bi_private = sbi;
453 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
454 fio->type, fio->temp);
457 wbc_init_bio(fio->io_wbc, bio);
462 static inline void __submit_bio(struct f2fs_sb_info *sbi,
463 struct bio *bio, enum page_type type)
465 if (!is_read_io(bio_op(bio))) {
468 if (type != DATA && type != NODE)
471 if (f2fs_lfs_mode(sbi) && current->plug)
472 blk_finish_plug(current->plug);
474 if (F2FS_IO_ALIGNED(sbi))
477 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
478 start %= F2FS_IO_SIZE(sbi);
483 /* fill dummy pages */
484 for (; start < F2FS_IO_SIZE(sbi); start++) {
486 mempool_alloc(sbi->write_io_dummy,
487 GFP_NOIO | __GFP_NOFAIL);
488 f2fs_bug_on(sbi, !page);
490 zero_user_segment(page, 0, PAGE_SIZE);
491 SetPagePrivate(page);
492 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
494 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
498 * In the NODE case, we lose next block address chain. So, we
499 * need to do checkpoint in f2fs_sync_file.
502 set_sbi_flag(sbi, SBI_NEED_CP);
505 if (is_read_io(bio_op(bio)))
506 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
508 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
512 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
513 struct bio *bio, enum page_type type)
515 __submit_bio(sbi, bio, type);
518 static void __attach_io_flag(struct f2fs_io_info *fio)
520 struct f2fs_sb_info *sbi = fio->sbi;
521 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
522 unsigned int io_flag, fua_flag, meta_flag;
524 if (fio->type == DATA)
525 io_flag = sbi->data_io_flag;
526 else if (fio->type == NODE)
527 io_flag = sbi->node_io_flag;
531 fua_flag = io_flag & temp_mask;
532 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
535 * data/node io flag bits per temp:
536 * REQ_META | REQ_FUA |
537 * 5 | 4 | 3 | 2 | 1 | 0 |
538 * Cold | Warm | Hot | Cold | Warm | Hot |
540 if ((1 << fio->temp) & meta_flag)
541 fio->op_flags |= REQ_META;
542 if ((1 << fio->temp) & fua_flag)
543 fio->op_flags |= REQ_FUA;
546 static void __submit_merged_bio(struct f2fs_bio_info *io)
548 struct f2fs_io_info *fio = &io->fio;
553 __attach_io_flag(fio);
554 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
556 if (is_read_io(fio->op))
557 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
559 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
561 __submit_bio(io->sbi, io->bio, fio->type);
565 static bool __has_merged_page(struct bio *bio, struct inode *inode,
566 struct page *page, nid_t ino)
568 struct bio_vec *bvec;
569 struct bvec_iter_all iter_all;
574 if (!inode && !page && !ino)
577 bio_for_each_segment_all(bvec, bio, iter_all) {
578 struct page *target = bvec->bv_page;
580 if (fscrypt_is_bounce_page(target)) {
581 target = fscrypt_pagecache_page(target);
585 if (f2fs_is_compressed_page(target)) {
586 target = f2fs_compress_control_page(target);
591 if (inode && inode == target->mapping->host)
593 if (page && page == target)
595 if (ino && ino == ino_of_node(target))
602 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
603 enum page_type type, enum temp_type temp)
605 enum page_type btype = PAGE_TYPE_OF_BIO(type);
606 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
608 down_write(&io->io_rwsem);
610 /* change META to META_FLUSH in the checkpoint procedure */
611 if (type >= META_FLUSH) {
612 io->fio.type = META_FLUSH;
613 io->fio.op = REQ_OP_WRITE;
614 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
615 if (!test_opt(sbi, NOBARRIER))
616 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
618 __submit_merged_bio(io);
619 up_write(&io->io_rwsem);
622 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
623 struct inode *inode, struct page *page,
624 nid_t ino, enum page_type type, bool force)
629 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
631 enum page_type btype = PAGE_TYPE_OF_BIO(type);
632 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
634 down_read(&io->io_rwsem);
635 ret = __has_merged_page(io->bio, inode, page, ino);
636 up_read(&io->io_rwsem);
639 __f2fs_submit_merged_write(sbi, type, temp);
641 /* TODO: use HOT temp only for meta pages now. */
647 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
649 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
652 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
653 struct inode *inode, struct page *page,
654 nid_t ino, enum page_type type)
656 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
659 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
661 f2fs_submit_merged_write(sbi, DATA);
662 f2fs_submit_merged_write(sbi, NODE);
663 f2fs_submit_merged_write(sbi, META);
667 * Fill the locked page with data located in the block address.
668 * A caller needs to unlock the page on failure.
670 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
673 struct page *page = fio->encrypted_page ?
674 fio->encrypted_page : fio->page;
676 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
677 fio->is_por ? META_POR : (__is_meta_io(fio) ?
678 META_GENERIC : DATA_GENERIC_ENHANCE)))
679 return -EFSCORRUPTED;
681 trace_f2fs_submit_page_bio(page, fio);
682 f2fs_trace_ios(fio, 0);
684 /* Allocate a new bio */
685 bio = __bio_alloc(fio, 1);
687 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
692 if (fio->io_wbc && !is_read_io(fio->op))
693 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
695 __attach_io_flag(fio);
696 bio_set_op_attrs(bio, fio->op, fio->op_flags);
698 inc_page_count(fio->sbi, is_read_io(fio->op) ?
699 __read_io_type(page): WB_DATA_TYPE(fio->page));
701 __submit_bio(fio->sbi, bio, fio->type);
705 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
706 block_t last_blkaddr, block_t cur_blkaddr)
708 if (last_blkaddr + 1 != cur_blkaddr)
710 return __same_bdev(sbi, cur_blkaddr, bio);
713 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
714 struct f2fs_io_info *fio)
716 if (io->fio.op != fio->op)
718 return io->fio.op_flags == fio->op_flags;
721 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
722 struct f2fs_bio_info *io,
723 struct f2fs_io_info *fio,
724 block_t last_blkaddr,
727 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
728 unsigned int filled_blocks =
729 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
730 unsigned int io_size = F2FS_IO_SIZE(sbi);
731 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
733 /* IOs in bio is aligned and left space of vectors is not enough */
734 if (!(filled_blocks % io_size) && left_vecs < io_size)
737 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
739 return io_type_is_mergeable(io, fio);
742 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
743 struct page *page, enum temp_type temp)
745 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
746 struct bio_entry *be;
748 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
752 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
755 down_write(&io->bio_list_lock);
756 list_add_tail(&be->list, &io->bio_list);
757 up_write(&io->bio_list_lock);
760 static void del_bio_entry(struct bio_entry *be)
763 kmem_cache_free(bio_entry_slab, be);
766 static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio,
773 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
774 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
775 struct list_head *head = &io->bio_list;
776 struct bio_entry *be;
778 down_write(&io->bio_list_lock);
779 list_for_each_entry(be, head, list) {
785 if (bio_add_page(*bio, page, PAGE_SIZE, 0) ==
793 __submit_bio(sbi, *bio, DATA);
796 up_write(&io->bio_list_lock);
807 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
808 struct bio **bio, struct page *page)
812 struct bio *target = bio ? *bio : NULL;
814 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
815 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
816 struct list_head *head = &io->bio_list;
817 struct bio_entry *be;
819 if (list_empty(head))
822 down_read(&io->bio_list_lock);
823 list_for_each_entry(be, head, list) {
825 found = (target == be->bio);
827 found = __has_merged_page(be->bio, NULL,
832 up_read(&io->bio_list_lock);
839 down_write(&io->bio_list_lock);
840 list_for_each_entry(be, head, list) {
842 found = (target == be->bio);
844 found = __has_merged_page(be->bio, NULL,
852 up_write(&io->bio_list_lock);
856 __submit_bio(sbi, target, DATA);
863 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
865 struct bio *bio = *fio->bio;
866 struct page *page = fio->encrypted_page ?
867 fio->encrypted_page : fio->page;
869 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
870 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
871 return -EFSCORRUPTED;
873 trace_f2fs_submit_page_bio(page, fio);
874 f2fs_trace_ios(fio, 0);
876 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
878 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
881 bio = __bio_alloc(fio, BIO_MAX_PAGES);
882 __attach_io_flag(fio);
883 bio_set_op_attrs(bio, fio->op, fio->op_flags);
885 add_bio_entry(fio->sbi, bio, page, fio->temp);
887 if (add_ipu_page(fio->sbi, &bio, page))
892 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
894 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
896 *fio->last_block = fio->new_blkaddr;
902 void f2fs_submit_page_write(struct f2fs_io_info *fio)
904 struct f2fs_sb_info *sbi = fio->sbi;
905 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
906 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
907 struct page *bio_page;
909 f2fs_bug_on(sbi, is_read_io(fio->op));
911 down_write(&io->io_rwsem);
914 spin_lock(&io->io_lock);
915 if (list_empty(&io->io_list)) {
916 spin_unlock(&io->io_lock);
919 fio = list_first_entry(&io->io_list,
920 struct f2fs_io_info, list);
921 list_del(&fio->list);
922 spin_unlock(&io->io_lock);
925 verify_fio_blkaddr(fio);
927 if (fio->encrypted_page)
928 bio_page = fio->encrypted_page;
929 else if (fio->compressed_page)
930 bio_page = fio->compressed_page;
932 bio_page = fio->page;
934 /* set submitted = true as a return value */
935 fio->submitted = true;
937 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
939 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio,
940 io->last_block_in_bio, fio->new_blkaddr))
941 __submit_merged_bio(io);
943 if (io->bio == NULL) {
944 if (F2FS_IO_ALIGNED(sbi) &&
945 (fio->type == DATA || fio->type == NODE) &&
946 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
947 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
951 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
955 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
956 __submit_merged_bio(io);
961 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
963 io->last_block_in_bio = fio->new_blkaddr;
964 f2fs_trace_ios(fio, 0);
966 trace_f2fs_submit_page_write(fio->page, fio);
971 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
972 !f2fs_is_checkpoint_ready(sbi))
973 __submit_merged_bio(io);
974 up_write(&io->io_rwsem);
977 static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
979 return fsverity_active(inode) &&
980 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
983 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
984 unsigned nr_pages, unsigned op_flag,
985 pgoff_t first_idx, bool for_write)
987 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
989 struct bio_post_read_ctx *ctx;
990 unsigned int post_read_steps = 0;
992 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
995 return ERR_PTR(-ENOMEM);
996 f2fs_target_device(sbi, blkaddr, bio);
997 bio->bi_end_io = f2fs_read_end_io;
998 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
1000 if (f2fs_encrypted_file(inode))
1001 post_read_steps |= 1 << STEP_DECRYPT;
1002 if (f2fs_compressed_file(inode))
1003 post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
1004 if (f2fs_need_verity(inode, first_idx))
1005 post_read_steps |= 1 << STEP_VERITY;
1007 if (post_read_steps) {
1008 /* Due to the mempool, this never fails. */
1009 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1012 ctx->enabled_steps = post_read_steps;
1013 bio->bi_private = ctx;
1019 static void f2fs_release_read_bio(struct bio *bio)
1021 if (bio->bi_private)
1022 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1026 /* This can handle encryption stuffs */
1027 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1028 block_t blkaddr, bool for_write)
1030 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1033 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index, for_write);
1035 return PTR_ERR(bio);
1037 /* wait for GCed page writeback via META_MAPPING */
1038 f2fs_wait_on_block_writeback(inode, blkaddr);
1040 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1044 ClearPageError(page);
1045 inc_page_count(sbi, F2FS_RD_DATA);
1046 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1047 __submit_bio(sbi, bio, DATA);
1051 static void __set_data_blkaddr(struct dnode_of_data *dn)
1053 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1057 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1058 base = get_extra_isize(dn->inode);
1060 /* Get physical address of data block */
1061 addr_array = blkaddr_in_node(rn);
1062 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1066 * Lock ordering for the change of data block address:
1069 * update block addresses in the node page
1071 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1073 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1074 __set_data_blkaddr(dn);
1075 if (set_page_dirty(dn->node_page))
1076 dn->node_changed = true;
1079 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1081 dn->data_blkaddr = blkaddr;
1082 f2fs_set_data_blkaddr(dn);
1083 f2fs_update_extent_cache(dn);
1086 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1087 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1089 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1095 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1097 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1100 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1101 dn->ofs_in_node, count);
1103 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1105 for (; count > 0; dn->ofs_in_node++) {
1106 block_t blkaddr = f2fs_data_blkaddr(dn);
1107 if (blkaddr == NULL_ADDR) {
1108 dn->data_blkaddr = NEW_ADDR;
1109 __set_data_blkaddr(dn);
1114 if (set_page_dirty(dn->node_page))
1115 dn->node_changed = true;
1119 /* Should keep dn->ofs_in_node unchanged */
1120 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1122 unsigned int ofs_in_node = dn->ofs_in_node;
1125 ret = f2fs_reserve_new_blocks(dn, 1);
1126 dn->ofs_in_node = ofs_in_node;
1130 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1132 bool need_put = dn->inode_page ? false : true;
1135 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1139 if (dn->data_blkaddr == NULL_ADDR)
1140 err = f2fs_reserve_new_block(dn);
1141 if (err || need_put)
1146 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1148 struct extent_info ei = {0,0,0};
1149 struct inode *inode = dn->inode;
1151 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1152 dn->data_blkaddr = ei.blk + index - ei.fofs;
1156 return f2fs_reserve_block(dn, index);
1159 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1160 int op_flags, bool for_write)
1162 struct address_space *mapping = inode->i_mapping;
1163 struct dnode_of_data dn;
1165 struct extent_info ei = {0,0,0};
1168 page = f2fs_grab_cache_page(mapping, index, for_write);
1170 return ERR_PTR(-ENOMEM);
1172 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1173 dn.data_blkaddr = ei.blk + index - ei.fofs;
1174 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1175 DATA_GENERIC_ENHANCE_READ)) {
1176 err = -EFSCORRUPTED;
1182 set_new_dnode(&dn, inode, NULL, NULL, 0);
1183 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1186 f2fs_put_dnode(&dn);
1188 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1192 if (dn.data_blkaddr != NEW_ADDR &&
1193 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1195 DATA_GENERIC_ENHANCE)) {
1196 err = -EFSCORRUPTED;
1200 if (PageUptodate(page)) {
1206 * A new dentry page is allocated but not able to be written, since its
1207 * new inode page couldn't be allocated due to -ENOSPC.
1208 * In such the case, its blkaddr can be remained as NEW_ADDR.
1209 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1210 * f2fs_init_inode_metadata.
1212 if (dn.data_blkaddr == NEW_ADDR) {
1213 zero_user_segment(page, 0, PAGE_SIZE);
1214 if (!PageUptodate(page))
1215 SetPageUptodate(page);
1220 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr, for_write);
1226 f2fs_put_page(page, 1);
1227 return ERR_PTR(err);
1230 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1232 struct address_space *mapping = inode->i_mapping;
1235 page = find_get_page(mapping, index);
1236 if (page && PageUptodate(page))
1238 f2fs_put_page(page, 0);
1240 page = f2fs_get_read_data_page(inode, index, 0, false);
1244 if (PageUptodate(page))
1247 wait_on_page_locked(page);
1248 if (unlikely(!PageUptodate(page))) {
1249 f2fs_put_page(page, 0);
1250 return ERR_PTR(-EIO);
1256 * If it tries to access a hole, return an error.
1257 * Because, the callers, functions in dir.c and GC, should be able to know
1258 * whether this page exists or not.
1260 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1263 struct address_space *mapping = inode->i_mapping;
1266 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1270 /* wait for read completion */
1272 if (unlikely(page->mapping != mapping)) {
1273 f2fs_put_page(page, 1);
1276 if (unlikely(!PageUptodate(page))) {
1277 f2fs_put_page(page, 1);
1278 return ERR_PTR(-EIO);
1284 * Caller ensures that this data page is never allocated.
1285 * A new zero-filled data page is allocated in the page cache.
1287 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1289 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1290 * ipage should be released by this function.
1292 struct page *f2fs_get_new_data_page(struct inode *inode,
1293 struct page *ipage, pgoff_t index, bool new_i_size)
1295 struct address_space *mapping = inode->i_mapping;
1297 struct dnode_of_data dn;
1300 page = f2fs_grab_cache_page(mapping, index, true);
1303 * before exiting, we should make sure ipage will be released
1304 * if any error occur.
1306 f2fs_put_page(ipage, 1);
1307 return ERR_PTR(-ENOMEM);
1310 set_new_dnode(&dn, inode, ipage, NULL, 0);
1311 err = f2fs_reserve_block(&dn, index);
1313 f2fs_put_page(page, 1);
1314 return ERR_PTR(err);
1317 f2fs_put_dnode(&dn);
1319 if (PageUptodate(page))
1322 if (dn.data_blkaddr == NEW_ADDR) {
1323 zero_user_segment(page, 0, PAGE_SIZE);
1324 if (!PageUptodate(page))
1325 SetPageUptodate(page);
1327 f2fs_put_page(page, 1);
1329 /* if ipage exists, blkaddr should be NEW_ADDR */
1330 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1331 page = f2fs_get_lock_data_page(inode, index, true);
1336 if (new_i_size && i_size_read(inode) <
1337 ((loff_t)(index + 1) << PAGE_SHIFT))
1338 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1342 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1344 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1345 struct f2fs_summary sum;
1346 struct node_info ni;
1347 block_t old_blkaddr;
1351 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1354 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1358 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1359 if (dn->data_blkaddr != NULL_ADDR)
1362 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1366 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1367 old_blkaddr = dn->data_blkaddr;
1368 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1369 &sum, seg_type, NULL, false);
1370 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1371 invalidate_mapping_pages(META_MAPPING(sbi),
1372 old_blkaddr, old_blkaddr);
1373 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1376 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1377 * data from unwritten block via dio_read.
1382 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1384 struct inode *inode = file_inode(iocb->ki_filp);
1385 struct f2fs_map_blocks map;
1388 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1390 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1391 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1392 if (map.m_len > map.m_lblk)
1393 map.m_len -= map.m_lblk;
1397 map.m_next_pgofs = NULL;
1398 map.m_next_extent = NULL;
1399 map.m_seg_type = NO_CHECK_TYPE;
1400 map.m_may_create = true;
1403 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1404 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1405 F2FS_GET_BLOCK_PRE_AIO :
1406 F2FS_GET_BLOCK_PRE_DIO;
1409 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1410 err = f2fs_convert_inline_inode(inode);
1414 if (f2fs_has_inline_data(inode))
1417 flag = F2FS_GET_BLOCK_PRE_AIO;
1420 err = f2fs_map_blocks(inode, &map, 1, flag);
1421 if (map.m_len > 0 && err == -ENOSPC) {
1423 set_inode_flag(inode, FI_NO_PREALLOC);
1429 void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1431 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1433 down_read(&sbi->node_change);
1435 up_read(&sbi->node_change);
1440 f2fs_unlock_op(sbi);
1445 * f2fs_map_blocks() tries to find or build mapping relationship which
1446 * maps continuous logical blocks to physical blocks, and return such
1447 * info via f2fs_map_blocks structure.
1449 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1450 int create, int flag)
1452 unsigned int maxblocks = map->m_len;
1453 struct dnode_of_data dn;
1454 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1455 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1456 pgoff_t pgofs, end_offset, end;
1457 int err = 0, ofs = 1;
1458 unsigned int ofs_in_node, last_ofs_in_node;
1460 struct extent_info ei = {0,0,0};
1462 unsigned int start_pgofs;
1470 /* it only supports block size == page size */
1471 pgofs = (pgoff_t)map->m_lblk;
1472 end = pgofs + maxblocks;
1474 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1475 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1479 map->m_pblk = ei.blk + pgofs - ei.fofs;
1480 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1481 map->m_flags = F2FS_MAP_MAPPED;
1482 if (map->m_next_extent)
1483 *map->m_next_extent = pgofs + map->m_len;
1485 /* for hardware encryption, but to avoid potential issue in future */
1486 if (flag == F2FS_GET_BLOCK_DIO)
1487 f2fs_wait_on_block_writeback_range(inode,
1488 map->m_pblk, map->m_len);
1493 if (map->m_may_create)
1494 __do_map_lock(sbi, flag, true);
1496 /* When reading holes, we need its node page */
1497 set_new_dnode(&dn, inode, NULL, NULL, 0);
1498 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1500 if (flag == F2FS_GET_BLOCK_BMAP)
1502 if (err == -ENOENT) {
1504 if (map->m_next_pgofs)
1505 *map->m_next_pgofs =
1506 f2fs_get_next_page_offset(&dn, pgofs);
1507 if (map->m_next_extent)
1508 *map->m_next_extent =
1509 f2fs_get_next_page_offset(&dn, pgofs);
1514 start_pgofs = pgofs;
1516 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1517 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1520 blkaddr = f2fs_data_blkaddr(&dn);
1522 if (__is_valid_data_blkaddr(blkaddr) &&
1523 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1524 err = -EFSCORRUPTED;
1528 if (__is_valid_data_blkaddr(blkaddr)) {
1529 /* use out-place-update for driect IO under LFS mode */
1530 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1531 map->m_may_create) {
1532 err = __allocate_data_block(&dn, map->m_seg_type);
1535 blkaddr = dn.data_blkaddr;
1536 set_inode_flag(inode, FI_APPEND_WRITE);
1540 if (unlikely(f2fs_cp_error(sbi))) {
1544 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1545 if (blkaddr == NULL_ADDR) {
1547 last_ofs_in_node = dn.ofs_in_node;
1550 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1551 flag != F2FS_GET_BLOCK_DIO);
1552 err = __allocate_data_block(&dn,
1555 set_inode_flag(inode, FI_APPEND_WRITE);
1559 map->m_flags |= F2FS_MAP_NEW;
1560 blkaddr = dn.data_blkaddr;
1562 if (flag == F2FS_GET_BLOCK_BMAP) {
1566 if (flag == F2FS_GET_BLOCK_PRECACHE)
1568 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1569 blkaddr == NULL_ADDR) {
1570 if (map->m_next_pgofs)
1571 *map->m_next_pgofs = pgofs + 1;
1574 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1575 /* for defragment case */
1576 if (map->m_next_pgofs)
1577 *map->m_next_pgofs = pgofs + 1;
1583 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1586 if (map->m_len == 0) {
1587 /* preallocated unwritten block should be mapped for fiemap. */
1588 if (blkaddr == NEW_ADDR)
1589 map->m_flags |= F2FS_MAP_UNWRITTEN;
1590 map->m_flags |= F2FS_MAP_MAPPED;
1592 map->m_pblk = blkaddr;
1594 } else if ((map->m_pblk != NEW_ADDR &&
1595 blkaddr == (map->m_pblk + ofs)) ||
1596 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1597 flag == F2FS_GET_BLOCK_PRE_DIO) {
1608 /* preallocate blocks in batch for one dnode page */
1609 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1610 (pgofs == end || dn.ofs_in_node == end_offset)) {
1612 dn.ofs_in_node = ofs_in_node;
1613 err = f2fs_reserve_new_blocks(&dn, prealloc);
1617 map->m_len += dn.ofs_in_node - ofs_in_node;
1618 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1622 dn.ofs_in_node = end_offset;
1627 else if (dn.ofs_in_node < end_offset)
1630 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1631 if (map->m_flags & F2FS_MAP_MAPPED) {
1632 unsigned int ofs = start_pgofs - map->m_lblk;
1634 f2fs_update_extent_cache_range(&dn,
1635 start_pgofs, map->m_pblk + ofs,
1640 f2fs_put_dnode(&dn);
1642 if (map->m_may_create) {
1643 __do_map_lock(sbi, flag, false);
1644 f2fs_balance_fs(sbi, dn.node_changed);
1650 /* for hardware encryption, but to avoid potential issue in future */
1651 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1652 f2fs_wait_on_block_writeback_range(inode,
1653 map->m_pblk, map->m_len);
1655 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1656 if (map->m_flags & F2FS_MAP_MAPPED) {
1657 unsigned int ofs = start_pgofs - map->m_lblk;
1659 f2fs_update_extent_cache_range(&dn,
1660 start_pgofs, map->m_pblk + ofs,
1663 if (map->m_next_extent)
1664 *map->m_next_extent = pgofs + 1;
1666 f2fs_put_dnode(&dn);
1668 if (map->m_may_create) {
1669 __do_map_lock(sbi, flag, false);
1670 f2fs_balance_fs(sbi, dn.node_changed);
1673 trace_f2fs_map_blocks(inode, map, err);
1677 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1679 struct f2fs_map_blocks map;
1683 if (pos + len > i_size_read(inode))
1686 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1687 map.m_next_pgofs = NULL;
1688 map.m_next_extent = NULL;
1689 map.m_seg_type = NO_CHECK_TYPE;
1690 map.m_may_create = false;
1691 last_lblk = F2FS_BLK_ALIGN(pos + len);
1693 while (map.m_lblk < last_lblk) {
1694 map.m_len = last_lblk - map.m_lblk;
1695 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1696 if (err || map.m_len == 0)
1698 map.m_lblk += map.m_len;
1703 static int __get_data_block(struct inode *inode, sector_t iblock,
1704 struct buffer_head *bh, int create, int flag,
1705 pgoff_t *next_pgofs, int seg_type, bool may_write)
1707 struct f2fs_map_blocks map;
1710 map.m_lblk = iblock;
1711 map.m_len = bh->b_size >> inode->i_blkbits;
1712 map.m_next_pgofs = next_pgofs;
1713 map.m_next_extent = NULL;
1714 map.m_seg_type = seg_type;
1715 map.m_may_create = may_write;
1717 err = f2fs_map_blocks(inode, &map, create, flag);
1719 map_bh(bh, inode->i_sb, map.m_pblk);
1720 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1721 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1726 static int get_data_block(struct inode *inode, sector_t iblock,
1727 struct buffer_head *bh_result, int create, int flag,
1728 pgoff_t *next_pgofs)
1730 return __get_data_block(inode, iblock, bh_result, create,
1732 NO_CHECK_TYPE, create);
1735 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1736 struct buffer_head *bh_result, int create)
1738 return __get_data_block(inode, iblock, bh_result, create,
1739 F2FS_GET_BLOCK_DIO, NULL,
1740 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1741 IS_SWAPFILE(inode) ? false : true);
1744 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1745 struct buffer_head *bh_result, int create)
1747 return __get_data_block(inode, iblock, bh_result, create,
1748 F2FS_GET_BLOCK_DIO, NULL,
1749 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1753 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1754 struct buffer_head *bh_result, int create)
1756 /* Block number less than F2FS MAX BLOCKS */
1757 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1760 return __get_data_block(inode, iblock, bh_result, create,
1761 F2FS_GET_BLOCK_BMAP, NULL,
1762 NO_CHECK_TYPE, create);
1765 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1767 return (offset >> inode->i_blkbits);
1770 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1772 return (blk << inode->i_blkbits);
1775 static int f2fs_xattr_fiemap(struct inode *inode,
1776 struct fiemap_extent_info *fieinfo)
1778 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1780 struct node_info ni;
1781 __u64 phys = 0, len;
1783 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1786 if (f2fs_has_inline_xattr(inode)) {
1789 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1790 inode->i_ino, false);
1794 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1796 f2fs_put_page(page, 1);
1800 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1801 offset = offsetof(struct f2fs_inode, i_addr) +
1802 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1803 get_inline_xattr_addrs(inode));
1806 len = inline_xattr_size(inode);
1808 f2fs_put_page(page, 1);
1810 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1813 flags |= FIEMAP_EXTENT_LAST;
1815 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1816 if (err || err == 1)
1821 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1825 err = f2fs_get_node_info(sbi, xnid, &ni);
1827 f2fs_put_page(page, 1);
1831 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1832 len = inode->i_sb->s_blocksize;
1834 f2fs_put_page(page, 1);
1836 flags = FIEMAP_EXTENT_LAST;
1840 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1842 return (err < 0 ? err : 0);
1845 static loff_t max_inode_blocks(struct inode *inode)
1847 loff_t result = ADDRS_PER_INODE(inode);
1848 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1850 /* two direct node blocks */
1851 result += (leaf_count * 2);
1853 /* two indirect node blocks */
1854 leaf_count *= NIDS_PER_BLOCK;
1855 result += (leaf_count * 2);
1857 /* one double indirect node block */
1858 leaf_count *= NIDS_PER_BLOCK;
1859 result += leaf_count;
1864 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1867 struct buffer_head map_bh;
1868 sector_t start_blk, last_blk;
1870 u64 logical = 0, phys = 0, size = 0;
1873 bool compr_cluster = false;
1874 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1876 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1877 ret = f2fs_precache_extents(inode);
1882 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1888 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1889 ret = f2fs_xattr_fiemap(inode, fieinfo);
1893 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1894 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1899 if (logical_to_blk(inode, len) == 0)
1900 len = blk_to_logical(inode, 1);
1902 start_blk = logical_to_blk(inode, start);
1903 last_blk = logical_to_blk(inode, start + len - 1);
1906 memset(&map_bh, 0, sizeof(struct buffer_head));
1907 map_bh.b_size = len;
1910 map_bh.b_size = blk_to_logical(inode, cluster_size - 1);
1912 ret = get_data_block(inode, start_blk, &map_bh, 0,
1913 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1918 if (!buffer_mapped(&map_bh)) {
1919 start_blk = next_pgofs;
1921 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1922 max_inode_blocks(inode)))
1925 flags |= FIEMAP_EXTENT_LAST;
1929 if (IS_ENCRYPTED(inode))
1930 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1932 ret = fiemap_fill_next_extent(fieinfo, logical,
1939 if (start_blk > last_blk)
1942 if (compr_cluster) {
1943 compr_cluster = false;
1946 logical = blk_to_logical(inode, start_blk - 1);
1947 phys = blk_to_logical(inode, map_bh.b_blocknr);
1948 size = blk_to_logical(inode, cluster_size);
1950 flags |= FIEMAP_EXTENT_ENCODED;
1952 start_blk += cluster_size - 1;
1954 if (start_blk > last_blk)
1960 if (map_bh.b_blocknr == COMPRESS_ADDR) {
1961 compr_cluster = true;
1966 logical = blk_to_logical(inode, start_blk);
1967 phys = blk_to_logical(inode, map_bh.b_blocknr);
1968 size = map_bh.b_size;
1970 if (buffer_unwritten(&map_bh))
1971 flags = FIEMAP_EXTENT_UNWRITTEN;
1973 start_blk += logical_to_blk(inode, size);
1977 if (fatal_signal_pending(current))
1985 inode_unlock(inode);
1989 static inline loff_t f2fs_readpage_limit(struct inode *inode)
1991 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1992 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1993 return inode->i_sb->s_maxbytes;
1995 return i_size_read(inode);
1998 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2000 struct f2fs_map_blocks *map,
2001 struct bio **bio_ret,
2002 sector_t *last_block_in_bio,
2005 struct bio *bio = *bio_ret;
2006 const unsigned blkbits = inode->i_blkbits;
2007 const unsigned blocksize = 1 << blkbits;
2008 sector_t block_in_file;
2009 sector_t last_block;
2010 sector_t last_block_in_file;
2014 block_in_file = (sector_t)page_index(page);
2015 last_block = block_in_file + nr_pages;
2016 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
2018 if (last_block > last_block_in_file)
2019 last_block = last_block_in_file;
2021 /* just zeroing out page which is beyond EOF */
2022 if (block_in_file >= last_block)
2025 * Map blocks using the previous result first.
2027 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2028 block_in_file > map->m_lblk &&
2029 block_in_file < (map->m_lblk + map->m_len))
2033 * Then do more f2fs_map_blocks() calls until we are
2034 * done with this page.
2036 map->m_lblk = block_in_file;
2037 map->m_len = last_block - block_in_file;
2039 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2043 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2044 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2045 SetPageMappedToDisk(page);
2047 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2048 !cleancache_get_page(page))) {
2049 SetPageUptodate(page);
2053 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2054 DATA_GENERIC_ENHANCE_READ)) {
2055 ret = -EFSCORRUPTED;
2060 zero_user_segment(page, 0, PAGE_SIZE);
2061 if (f2fs_need_verity(inode, page->index) &&
2062 !fsverity_verify_page(page)) {
2066 if (!PageUptodate(page))
2067 SetPageUptodate(page);
2073 * This page will go to BIO. Do we need to send this
2076 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio,
2077 *last_block_in_bio, block_nr)) {
2079 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2083 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2084 is_readahead ? REQ_RAHEAD : 0, page->index,
2094 * If the page is under writeback, we need to wait for
2095 * its completion to see the correct decrypted data.
2097 f2fs_wait_on_block_writeback(inode, block_nr);
2099 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2100 goto submit_and_realloc;
2102 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2103 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2104 ClearPageError(page);
2105 *last_block_in_bio = block_nr;
2109 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2118 #ifdef CONFIG_F2FS_FS_COMPRESSION
2119 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2120 unsigned nr_pages, sector_t *last_block_in_bio,
2121 bool is_readahead, bool for_write)
2123 struct dnode_of_data dn;
2124 struct inode *inode = cc->inode;
2125 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2126 struct bio *bio = *bio_ret;
2127 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2128 sector_t last_block_in_file;
2129 const unsigned blkbits = inode->i_blkbits;
2130 const unsigned blocksize = 1 << blkbits;
2131 struct decompress_io_ctx *dic = NULL;
2135 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2137 last_block_in_file = (f2fs_readpage_limit(inode) +
2138 blocksize - 1) >> blkbits;
2140 /* get rid of pages beyond EOF */
2141 for (i = 0; i < cc->cluster_size; i++) {
2142 struct page *page = cc->rpages[i];
2146 if ((sector_t)page->index >= last_block_in_file) {
2147 zero_user_segment(page, 0, PAGE_SIZE);
2148 if (!PageUptodate(page))
2149 SetPageUptodate(page);
2150 } else if (!PageUptodate(page)) {
2154 cc->rpages[i] = NULL;
2158 /* we are done since all pages are beyond EOF */
2159 if (f2fs_cluster_is_empty(cc))
2162 set_new_dnode(&dn, inode, NULL, NULL, 0);
2163 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2167 /* cluster was overwritten as normal cluster */
2168 if (dn.data_blkaddr != COMPRESS_ADDR)
2171 for (i = 1; i < cc->cluster_size; i++) {
2174 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2175 dn.ofs_in_node + i);
2177 if (!__is_valid_data_blkaddr(blkaddr))
2180 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2187 /* nothing to decompress */
2188 if (cc->nr_cpages == 0) {
2193 dic = f2fs_alloc_dic(cc);
2199 for (i = 0; i < dic->nr_cpages; i++) {
2200 struct page *page = dic->cpages[i];
2202 struct bio_post_read_ctx *ctx;
2204 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2205 dn.ofs_in_node + i + 1);
2207 if (bio && !page_is_mergeable(sbi, bio,
2208 *last_block_in_bio, blkaddr)) {
2210 __submit_bio(sbi, bio, DATA);
2215 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2216 is_readahead ? REQ_RAHEAD : 0,
2217 page->index, for_write);
2221 if (refcount_sub_and_test(dic->nr_cpages - i,
2223 f2fs_decompress_end_io(dic->rpages,
2224 cc->cluster_size, true,
2228 f2fs_put_dnode(&dn);
2234 f2fs_wait_on_block_writeback(inode, blkaddr);
2236 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2237 goto submit_and_realloc;
2239 /* tag STEP_DECOMPRESS to handle IO in wq */
2240 ctx = bio->bi_private;
2241 if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2242 ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2244 inc_page_count(sbi, F2FS_RD_DATA);
2245 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2246 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2247 ClearPageError(page);
2248 *last_block_in_bio = blkaddr;
2251 f2fs_put_dnode(&dn);
2257 f2fs_put_dnode(&dn);
2259 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2266 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2267 * Major change was from block_size == page_size in f2fs by default.
2269 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2270 * this function ever deviates from doing just read-ahead, it should either
2271 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2274 static int f2fs_mpage_readpages(struct inode *inode,
2275 struct readahead_control *rac, struct page *page)
2277 struct bio *bio = NULL;
2278 sector_t last_block_in_bio = 0;
2279 struct f2fs_map_blocks map;
2280 #ifdef CONFIG_F2FS_FS_COMPRESSION
2281 struct compress_ctx cc = {
2283 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2284 .cluster_size = F2FS_I(inode)->i_cluster_size,
2285 .cluster_idx = NULL_CLUSTER,
2292 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2293 unsigned max_nr_pages = nr_pages;
2300 map.m_next_pgofs = NULL;
2301 map.m_next_extent = NULL;
2302 map.m_seg_type = NO_CHECK_TYPE;
2303 map.m_may_create = false;
2305 for (; nr_pages; nr_pages--) {
2307 page = readahead_page(rac);
2308 prefetchw(&page->flags);
2311 #ifdef CONFIG_F2FS_FS_COMPRESSION
2312 if (f2fs_compressed_file(inode)) {
2313 /* there are remained comressed pages, submit them */
2314 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2315 ret = f2fs_read_multi_pages(&cc, &bio,
2318 rac != NULL, false);
2319 f2fs_destroy_compress_ctx(&cc);
2321 goto set_error_page;
2323 ret = f2fs_is_compressed_cluster(inode, page->index);
2325 goto set_error_page;
2327 goto read_single_page;
2329 ret = f2fs_init_compress_ctx(&cc);
2331 goto set_error_page;
2333 f2fs_compress_ctx_add_page(&cc, page);
2340 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2341 &bio, &last_block_in_bio, rac);
2343 #ifdef CONFIG_F2FS_FS_COMPRESSION
2347 zero_user_segment(page, 0, PAGE_SIZE);
2350 #ifdef CONFIG_F2FS_FS_COMPRESSION
2356 #ifdef CONFIG_F2FS_FS_COMPRESSION
2357 if (f2fs_compressed_file(inode)) {
2359 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2360 ret = f2fs_read_multi_pages(&cc, &bio,
2363 rac != NULL, false);
2364 f2fs_destroy_compress_ctx(&cc);
2370 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2374 static int f2fs_read_data_page(struct file *file, struct page *page)
2376 struct inode *inode = page_file_mapping(page)->host;
2379 trace_f2fs_readpage(page, DATA);
2381 if (!f2fs_is_compress_backend_ready(inode)) {
2386 /* If the file has inline data, try to read it directly */
2387 if (f2fs_has_inline_data(inode))
2388 ret = f2fs_read_inline_data(inode, page);
2390 ret = f2fs_mpage_readpages(inode, NULL, page);
2394 static void f2fs_readahead(struct readahead_control *rac)
2396 struct inode *inode = rac->mapping->host;
2398 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2400 if (!f2fs_is_compress_backend_ready(inode))
2403 /* If the file has inline data, skip readpages */
2404 if (f2fs_has_inline_data(inode))
2407 f2fs_mpage_readpages(inode, rac, NULL);
2410 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2412 struct inode *inode = fio->page->mapping->host;
2413 struct page *mpage, *page;
2414 gfp_t gfp_flags = GFP_NOFS;
2416 if (!f2fs_encrypted_file(inode))
2419 page = fio->compressed_page ? fio->compressed_page : fio->page;
2421 /* wait for GCed page writeback via META_MAPPING */
2422 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2425 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2426 PAGE_SIZE, 0, gfp_flags);
2427 if (IS_ERR(fio->encrypted_page)) {
2428 /* flush pending IOs and wait for a while in the ENOMEM case */
2429 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2430 f2fs_flush_merged_writes(fio->sbi);
2431 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2432 gfp_flags |= __GFP_NOFAIL;
2435 return PTR_ERR(fio->encrypted_page);
2438 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2440 if (PageUptodate(mpage))
2441 memcpy(page_address(mpage),
2442 page_address(fio->encrypted_page), PAGE_SIZE);
2443 f2fs_put_page(mpage, 1);
2448 static inline bool check_inplace_update_policy(struct inode *inode,
2449 struct f2fs_io_info *fio)
2451 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2452 unsigned int policy = SM_I(sbi)->ipu_policy;
2454 if (policy & (0x1 << F2FS_IPU_FORCE))
2456 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2458 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2459 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2461 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2462 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2466 * IPU for rewrite async pages
2468 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2469 fio && fio->op == REQ_OP_WRITE &&
2470 !(fio->op_flags & REQ_SYNC) &&
2471 !IS_ENCRYPTED(inode))
2474 /* this is only set during fdatasync */
2475 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2476 is_inode_flag_set(inode, FI_NEED_IPU))
2479 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2480 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2486 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2488 if (f2fs_is_pinned_file(inode))
2491 /* if this is cold file, we should overwrite to avoid fragmentation */
2492 if (file_is_cold(inode))
2495 return check_inplace_update_policy(inode, fio);
2498 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2502 if (f2fs_lfs_mode(sbi))
2504 if (S_ISDIR(inode->i_mode))
2506 if (IS_NOQUOTA(inode))
2508 if (f2fs_is_atomic_file(inode))
2511 if (is_cold_data(fio->page))
2513 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2515 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2516 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2522 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2524 struct inode *inode = fio->page->mapping->host;
2526 if (f2fs_should_update_outplace(inode, fio))
2529 return f2fs_should_update_inplace(inode, fio);
2532 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2534 struct page *page = fio->page;
2535 struct inode *inode = page->mapping->host;
2536 struct dnode_of_data dn;
2537 struct extent_info ei = {0,0,0};
2538 struct node_info ni;
2539 bool ipu_force = false;
2542 set_new_dnode(&dn, inode, NULL, NULL, 0);
2543 if (need_inplace_update(fio) &&
2544 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2545 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2547 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2548 DATA_GENERIC_ENHANCE))
2549 return -EFSCORRUPTED;
2552 fio->need_lock = LOCK_DONE;
2556 /* Deadlock due to between page->lock and f2fs_lock_op */
2557 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2560 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2564 fio->old_blkaddr = dn.data_blkaddr;
2566 /* This page is already truncated */
2567 if (fio->old_blkaddr == NULL_ADDR) {
2568 ClearPageUptodate(page);
2569 clear_cold_data(page);
2573 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2574 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2575 DATA_GENERIC_ENHANCE)) {
2576 err = -EFSCORRUPTED;
2580 * If current allocation needs SSR,
2581 * it had better in-place writes for updated data.
2584 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2585 need_inplace_update(fio))) {
2586 err = f2fs_encrypt_one_page(fio);
2590 set_page_writeback(page);
2591 ClearPageError(page);
2592 f2fs_put_dnode(&dn);
2593 if (fio->need_lock == LOCK_REQ)
2594 f2fs_unlock_op(fio->sbi);
2595 err = f2fs_inplace_write_data(fio);
2597 if (f2fs_encrypted_file(inode))
2598 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2599 if (PageWriteback(page))
2600 end_page_writeback(page);
2602 set_inode_flag(inode, FI_UPDATE_WRITE);
2604 trace_f2fs_do_write_data_page(fio->page, IPU);
2608 if (fio->need_lock == LOCK_RETRY) {
2609 if (!f2fs_trylock_op(fio->sbi)) {
2613 fio->need_lock = LOCK_REQ;
2616 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2620 fio->version = ni.version;
2622 err = f2fs_encrypt_one_page(fio);
2626 set_page_writeback(page);
2627 ClearPageError(page);
2629 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2630 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2632 /* LFS mode write path */
2633 f2fs_outplace_write_data(&dn, fio);
2634 trace_f2fs_do_write_data_page(page, OPU);
2635 set_inode_flag(inode, FI_APPEND_WRITE);
2636 if (page->index == 0)
2637 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2639 f2fs_put_dnode(&dn);
2641 if (fio->need_lock == LOCK_REQ)
2642 f2fs_unlock_op(fio->sbi);
2646 int f2fs_write_single_data_page(struct page *page, int *submitted,
2648 sector_t *last_block,
2649 struct writeback_control *wbc,
2650 enum iostat_type io_type,
2653 struct inode *inode = page->mapping->host;
2654 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2655 loff_t i_size = i_size_read(inode);
2656 const pgoff_t end_index = ((unsigned long long)i_size)
2658 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2659 unsigned offset = 0;
2660 bool need_balance_fs = false;
2662 struct f2fs_io_info fio = {
2664 .ino = inode->i_ino,
2667 .op_flags = wbc_to_write_flags(wbc),
2668 .old_blkaddr = NULL_ADDR,
2670 .encrypted_page = NULL,
2672 .compr_blocks = compr_blocks,
2673 .need_lock = LOCK_RETRY,
2677 .last_block = last_block,
2680 trace_f2fs_writepage(page, DATA);
2682 /* we should bypass data pages to proceed the kworkder jobs */
2683 if (unlikely(f2fs_cp_error(sbi))) {
2684 mapping_set_error(page->mapping, -EIO);
2686 * don't drop any dirty dentry pages for keeping lastest
2687 * directory structure.
2689 if (S_ISDIR(inode->i_mode))
2694 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2697 if (page->index < end_index ||
2698 f2fs_verity_in_progress(inode) ||
2703 * If the offset is out-of-range of file size,
2704 * this page does not have to be written to disk.
2706 offset = i_size & (PAGE_SIZE - 1);
2707 if ((page->index >= end_index + 1) || !offset)
2710 zero_user_segment(page, offset, PAGE_SIZE);
2712 if (f2fs_is_drop_cache(inode))
2714 /* we should not write 0'th page having journal header */
2715 if (f2fs_is_volatile_file(inode) && (!page->index ||
2716 (!wbc->for_reclaim &&
2717 f2fs_available_free_memory(sbi, BASE_CHECK))))
2720 /* Dentry/quota blocks are controlled by checkpoint */
2721 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2722 fio.need_lock = LOCK_DONE;
2723 err = f2fs_do_write_data_page(&fio);
2727 if (!wbc->for_reclaim)
2728 need_balance_fs = true;
2729 else if (has_not_enough_free_secs(sbi, 0, 0))
2732 set_inode_flag(inode, FI_HOT_DATA);
2735 if (f2fs_has_inline_data(inode)) {
2736 err = f2fs_write_inline_data(inode, page);
2741 if (err == -EAGAIN) {
2742 err = f2fs_do_write_data_page(&fio);
2743 if (err == -EAGAIN) {
2744 fio.need_lock = LOCK_REQ;
2745 err = f2fs_do_write_data_page(&fio);
2750 file_set_keep_isize(inode);
2752 spin_lock(&F2FS_I(inode)->i_size_lock);
2753 if (F2FS_I(inode)->last_disk_size < psize)
2754 F2FS_I(inode)->last_disk_size = psize;
2755 spin_unlock(&F2FS_I(inode)->i_size_lock);
2759 if (err && err != -ENOENT)
2763 inode_dec_dirty_pages(inode);
2765 ClearPageUptodate(page);
2766 clear_cold_data(page);
2769 if (wbc->for_reclaim) {
2770 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2771 clear_inode_flag(inode, FI_HOT_DATA);
2772 f2fs_remove_dirty_inode(inode);
2776 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2777 !F2FS_I(inode)->cp_task)
2778 f2fs_balance_fs(sbi, need_balance_fs);
2780 if (unlikely(f2fs_cp_error(sbi))) {
2781 f2fs_submit_merged_write(sbi, DATA);
2782 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2787 *submitted = fio.submitted ? 1 : 0;
2792 redirty_page_for_writepage(wbc, page);
2794 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2795 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2796 * file_write_and_wait_range() will see EIO error, which is critical
2797 * to return value of fsync() followed by atomic_write failure to user.
2799 if (!err || wbc->for_reclaim)
2800 return AOP_WRITEPAGE_ACTIVATE;
2805 static int f2fs_write_data_page(struct page *page,
2806 struct writeback_control *wbc)
2808 #ifdef CONFIG_F2FS_FS_COMPRESSION
2809 struct inode *inode = page->mapping->host;
2811 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2814 if (f2fs_compressed_file(inode)) {
2815 if (f2fs_is_compressed_cluster(inode, page->index)) {
2816 redirty_page_for_writepage(wbc, page);
2817 return AOP_WRITEPAGE_ACTIVATE;
2823 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2824 wbc, FS_DATA_IO, 0);
2828 * This function was copied from write_cche_pages from mm/page-writeback.c.
2829 * The major change is making write step of cold data page separately from
2830 * warm/hot data page.
2832 static int f2fs_write_cache_pages(struct address_space *mapping,
2833 struct writeback_control *wbc,
2834 enum iostat_type io_type)
2837 int done = 0, retry = 0;
2838 struct pagevec pvec;
2839 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2840 struct bio *bio = NULL;
2841 sector_t last_block;
2842 #ifdef CONFIG_F2FS_FS_COMPRESSION
2843 struct inode *inode = mapping->host;
2844 struct compress_ctx cc = {
2846 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2847 .cluster_size = F2FS_I(inode)->i_cluster_size,
2848 .cluster_idx = NULL_CLUSTER,
2854 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2859 pgoff_t uninitialized_var(writeback_index);
2861 pgoff_t end; /* Inclusive */
2863 int range_whole = 0;
2869 pagevec_init(&pvec);
2871 if (get_dirty_pages(mapping->host) <=
2872 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2873 set_inode_flag(mapping->host, FI_HOT_DATA);
2875 clear_inode_flag(mapping->host, FI_HOT_DATA);
2877 if (wbc->range_cyclic) {
2878 writeback_index = mapping->writeback_index; /* prev offset */
2879 index = writeback_index;
2882 index = wbc->range_start >> PAGE_SHIFT;
2883 end = wbc->range_end >> PAGE_SHIFT;
2884 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2887 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2888 tag = PAGECACHE_TAG_TOWRITE;
2890 tag = PAGECACHE_TAG_DIRTY;
2893 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2894 tag_pages_for_writeback(mapping, index, end);
2896 while (!done && !retry && (index <= end)) {
2897 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2902 for (i = 0; i < nr_pages; i++) {
2903 struct page *page = pvec.pages[i];
2907 #ifdef CONFIG_F2FS_FS_COMPRESSION
2908 if (f2fs_compressed_file(inode)) {
2909 ret = f2fs_init_compress_ctx(&cc);
2915 if (!f2fs_cluster_can_merge_page(&cc,
2917 ret = f2fs_write_multi_pages(&cc,
2918 &submitted, wbc, io_type);
2924 if (unlikely(f2fs_cp_error(sbi)))
2927 if (f2fs_cluster_is_empty(&cc)) {
2928 void *fsdata = NULL;
2932 ret2 = f2fs_prepare_compress_overwrite(
2934 page->index, &fsdata);
2940 !f2fs_compress_write_end(inode,
2941 fsdata, page->index,
2951 /* give a priority to WB_SYNC threads */
2952 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2953 wbc->sync_mode == WB_SYNC_NONE) {
2957 #ifdef CONFIG_F2FS_FS_COMPRESSION
2960 done_index = page->index;
2964 if (unlikely(page->mapping != mapping)) {
2970 if (!PageDirty(page)) {
2971 /* someone wrote it for us */
2972 goto continue_unlock;
2975 if (PageWriteback(page)) {
2976 if (wbc->sync_mode != WB_SYNC_NONE)
2977 f2fs_wait_on_page_writeback(page,
2980 goto continue_unlock;
2983 if (!clear_page_dirty_for_io(page))
2984 goto continue_unlock;
2986 #ifdef CONFIG_F2FS_FS_COMPRESSION
2987 if (f2fs_compressed_file(inode)) {
2989 f2fs_compress_ctx_add_page(&cc, page);
2993 ret = f2fs_write_single_data_page(page, &submitted,
2994 &bio, &last_block, wbc, io_type, 0);
2995 if (ret == AOP_WRITEPAGE_ACTIVATE)
2997 #ifdef CONFIG_F2FS_FS_COMPRESSION
3000 nwritten += submitted;
3001 wbc->nr_to_write -= submitted;
3003 if (unlikely(ret)) {
3005 * keep nr_to_write, since vfs uses this to
3006 * get # of written pages.
3008 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3011 } else if (ret == -EAGAIN) {
3013 if (wbc->sync_mode == WB_SYNC_ALL) {
3015 congestion_wait(BLK_RW_ASYNC,
3016 DEFAULT_IO_TIMEOUT);
3021 done_index = page->index + 1;
3026 if (wbc->nr_to_write <= 0 &&
3027 wbc->sync_mode == WB_SYNC_NONE) {
3035 pagevec_release(&pvec);
3038 #ifdef CONFIG_F2FS_FS_COMPRESSION
3039 /* flush remained pages in compress cluster */
3040 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3041 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3042 nwritten += submitted;
3043 wbc->nr_to_write -= submitted;
3055 if (wbc->range_cyclic && !done)
3057 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3058 mapping->writeback_index = done_index;
3061 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3063 /* submit cached bio of IPU write */
3065 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3070 static inline bool __should_serialize_io(struct inode *inode,
3071 struct writeback_control *wbc)
3073 /* to avoid deadlock in path of data flush */
3074 if (F2FS_I(inode)->cp_task)
3077 if (!S_ISREG(inode->i_mode))
3079 if (IS_NOQUOTA(inode))
3082 if (f2fs_compressed_file(inode))
3084 if (wbc->sync_mode != WB_SYNC_ALL)
3086 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3091 static int __f2fs_write_data_pages(struct address_space *mapping,
3092 struct writeback_control *wbc,
3093 enum iostat_type io_type)
3095 struct inode *inode = mapping->host;
3096 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3097 struct blk_plug plug;
3099 bool locked = false;
3101 /* deal with chardevs and other special file */
3102 if (!mapping->a_ops->writepage)
3105 /* skip writing if there is no dirty page in this inode */
3106 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3109 /* during POR, we don't need to trigger writepage at all. */
3110 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3113 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3114 wbc->sync_mode == WB_SYNC_NONE &&
3115 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3116 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3119 /* skip writing during file defragment */
3120 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3123 trace_f2fs_writepages(mapping->host, wbc, DATA);
3125 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3126 if (wbc->sync_mode == WB_SYNC_ALL)
3127 atomic_inc(&sbi->wb_sync_req[DATA]);
3128 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3131 if (__should_serialize_io(inode, wbc)) {
3132 mutex_lock(&sbi->writepages);
3136 blk_start_plug(&plug);
3137 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3138 blk_finish_plug(&plug);
3141 mutex_unlock(&sbi->writepages);
3143 if (wbc->sync_mode == WB_SYNC_ALL)
3144 atomic_dec(&sbi->wb_sync_req[DATA]);
3146 * if some pages were truncated, we cannot guarantee its mapping->host
3147 * to detect pending bios.
3150 f2fs_remove_dirty_inode(inode);
3154 wbc->pages_skipped += get_dirty_pages(inode);
3155 trace_f2fs_writepages(mapping->host, wbc, DATA);
3159 static int f2fs_write_data_pages(struct address_space *mapping,
3160 struct writeback_control *wbc)
3162 struct inode *inode = mapping->host;
3164 return __f2fs_write_data_pages(mapping, wbc,
3165 F2FS_I(inode)->cp_task == current ?
3166 FS_CP_DATA_IO : FS_DATA_IO);
3169 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3171 struct inode *inode = mapping->host;
3172 loff_t i_size = i_size_read(inode);
3174 if (IS_NOQUOTA(inode))
3177 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3178 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3179 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3180 down_write(&F2FS_I(inode)->i_mmap_sem);
3182 truncate_pagecache(inode, i_size);
3183 f2fs_truncate_blocks(inode, i_size, true);
3185 up_write(&F2FS_I(inode)->i_mmap_sem);
3186 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3190 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3191 struct page *page, loff_t pos, unsigned len,
3192 block_t *blk_addr, bool *node_changed)
3194 struct inode *inode = page->mapping->host;
3195 pgoff_t index = page->index;
3196 struct dnode_of_data dn;
3198 bool locked = false;
3199 struct extent_info ei = {0,0,0};
3204 * we already allocated all the blocks, so we don't need to get
3205 * the block addresses when there is no need to fill the page.
3207 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3208 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3209 !f2fs_verity_in_progress(inode))
3212 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3213 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3214 flag = F2FS_GET_BLOCK_DEFAULT;
3216 flag = F2FS_GET_BLOCK_PRE_AIO;
3218 if (f2fs_has_inline_data(inode) ||
3219 (pos & PAGE_MASK) >= i_size_read(inode)) {
3220 __do_map_lock(sbi, flag, true);
3225 /* check inline_data */
3226 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3227 if (IS_ERR(ipage)) {
3228 err = PTR_ERR(ipage);
3232 set_new_dnode(&dn, inode, ipage, ipage, 0);
3234 if (f2fs_has_inline_data(inode)) {
3235 if (pos + len <= MAX_INLINE_DATA(inode)) {
3236 f2fs_do_read_inline_data(page, ipage);
3237 set_inode_flag(inode, FI_DATA_EXIST);
3239 set_inline_node(ipage);
3241 err = f2fs_convert_inline_page(&dn, page);
3244 if (dn.data_blkaddr == NULL_ADDR)
3245 err = f2fs_get_block(&dn, index);
3247 } else if (locked) {
3248 err = f2fs_get_block(&dn, index);
3250 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3251 dn.data_blkaddr = ei.blk + index - ei.fofs;
3254 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3255 if (err || dn.data_blkaddr == NULL_ADDR) {
3256 f2fs_put_dnode(&dn);
3257 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3259 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3266 /* convert_inline_page can make node_changed */
3267 *blk_addr = dn.data_blkaddr;
3268 *node_changed = dn.node_changed;
3270 f2fs_put_dnode(&dn);
3273 __do_map_lock(sbi, flag, false);
3277 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3278 loff_t pos, unsigned len, unsigned flags,
3279 struct page **pagep, void **fsdata)
3281 struct inode *inode = mapping->host;
3282 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3283 struct page *page = NULL;
3284 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3285 bool need_balance = false, drop_atomic = false;
3286 block_t blkaddr = NULL_ADDR;
3289 trace_f2fs_write_begin(inode, pos, len, flags);
3291 if (!f2fs_is_checkpoint_ready(sbi)) {
3296 if ((f2fs_is_atomic_file(inode) &&
3297 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3298 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3305 * We should check this at this moment to avoid deadlock on inode page
3306 * and #0 page. The locking rule for inline_data conversion should be:
3307 * lock_page(page #0) -> lock_page(inode_page)
3310 err = f2fs_convert_inline_inode(inode);
3315 #ifdef CONFIG_F2FS_FS_COMPRESSION
3316 if (f2fs_compressed_file(inode)) {
3321 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3334 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3335 * wait_for_stable_page. Will wait that below with our IO control.
3337 page = f2fs_pagecache_get_page(mapping, index,
3338 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3344 /* TODO: cluster can be compressed due to race with .writepage */
3348 err = prepare_write_begin(sbi, page, pos, len,
3349 &blkaddr, &need_balance);
3353 if (need_balance && !IS_NOQUOTA(inode) &&
3354 has_not_enough_free_secs(sbi, 0, 0)) {
3356 f2fs_balance_fs(sbi, true);
3358 if (page->mapping != mapping) {
3359 /* The page got truncated from under us */
3360 f2fs_put_page(page, 1);
3365 f2fs_wait_on_page_writeback(page, DATA, false, true);
3367 if (len == PAGE_SIZE || PageUptodate(page))
3370 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3371 !f2fs_verity_in_progress(inode)) {
3372 zero_user_segment(page, len, PAGE_SIZE);
3376 if (blkaddr == NEW_ADDR) {
3377 zero_user_segment(page, 0, PAGE_SIZE);
3378 SetPageUptodate(page);
3380 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3381 DATA_GENERIC_ENHANCE_READ)) {
3382 err = -EFSCORRUPTED;
3385 err = f2fs_submit_page_read(inode, page, blkaddr, true);
3390 if (unlikely(page->mapping != mapping)) {
3391 f2fs_put_page(page, 1);
3394 if (unlikely(!PageUptodate(page))) {
3402 f2fs_put_page(page, 1);
3403 f2fs_write_failed(mapping, pos + len);
3405 f2fs_drop_inmem_pages_all(sbi, false);
3409 static int f2fs_write_end(struct file *file,
3410 struct address_space *mapping,
3411 loff_t pos, unsigned len, unsigned copied,
3412 struct page *page, void *fsdata)
3414 struct inode *inode = page->mapping->host;
3416 trace_f2fs_write_end(inode, pos, len, copied);
3419 * This should be come from len == PAGE_SIZE, and we expect copied
3420 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3421 * let generic_perform_write() try to copy data again through copied=0.
3423 if (!PageUptodate(page)) {
3424 if (unlikely(copied != len))
3427 SetPageUptodate(page);
3430 #ifdef CONFIG_F2FS_FS_COMPRESSION
3431 /* overwrite compressed file */
3432 if (f2fs_compressed_file(inode) && fsdata) {
3433 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3434 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3442 set_page_dirty(page);
3444 if (pos + copied > i_size_read(inode) &&
3445 !f2fs_verity_in_progress(inode))
3446 f2fs_i_size_write(inode, pos + copied);
3448 f2fs_put_page(page, 1);
3449 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3453 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3456 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3457 unsigned blkbits = i_blkbits;
3458 unsigned blocksize_mask = (1 << blkbits) - 1;
3459 unsigned long align = offset | iov_iter_alignment(iter);
3460 struct block_device *bdev = inode->i_sb->s_bdev;
3462 if (align & blocksize_mask) {
3464 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3465 blocksize_mask = (1 << blkbits) - 1;
3466 if (align & blocksize_mask)
3473 static void f2fs_dio_end_io(struct bio *bio)
3475 struct f2fs_private_dio *dio = bio->bi_private;
3477 dec_page_count(F2FS_I_SB(dio->inode),
3478 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3480 bio->bi_private = dio->orig_private;
3481 bio->bi_end_io = dio->orig_end_io;
3488 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3491 struct f2fs_private_dio *dio;
3492 bool write = (bio_op(bio) == REQ_OP_WRITE);
3494 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3495 sizeof(struct f2fs_private_dio), GFP_NOFS);
3500 dio->orig_end_io = bio->bi_end_io;
3501 dio->orig_private = bio->bi_private;
3504 bio->bi_end_io = f2fs_dio_end_io;
3505 bio->bi_private = dio;
3507 inc_page_count(F2FS_I_SB(inode),
3508 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3513 bio->bi_status = BLK_STS_IOERR;
3517 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3519 struct address_space *mapping = iocb->ki_filp->f_mapping;
3520 struct inode *inode = mapping->host;
3521 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3522 struct f2fs_inode_info *fi = F2FS_I(inode);
3523 size_t count = iov_iter_count(iter);
3524 loff_t offset = iocb->ki_pos;
3525 int rw = iov_iter_rw(iter);
3527 enum rw_hint hint = iocb->ki_hint;
3528 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3531 err = check_direct_IO(inode, iter, offset);
3533 return err < 0 ? err : 0;
3535 if (f2fs_force_buffered_io(inode, iocb, iter))
3538 do_opu = allow_outplace_dio(inode, iocb, iter);
3540 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3542 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3543 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3545 if (iocb->ki_flags & IOCB_NOWAIT) {
3546 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3547 iocb->ki_hint = hint;
3551 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3552 up_read(&fi->i_gc_rwsem[rw]);
3553 iocb->ki_hint = hint;
3558 down_read(&fi->i_gc_rwsem[rw]);
3560 down_read(&fi->i_gc_rwsem[READ]);
3563 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3564 iter, rw == WRITE ? get_data_block_dio_write :
3565 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3566 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3570 up_read(&fi->i_gc_rwsem[READ]);
3572 up_read(&fi->i_gc_rwsem[rw]);
3575 if (whint_mode == WHINT_MODE_OFF)
3576 iocb->ki_hint = hint;
3578 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3581 set_inode_flag(inode, FI_UPDATE_WRITE);
3582 } else if (err < 0) {
3583 f2fs_write_failed(mapping, offset + count);
3587 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3591 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3596 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3597 unsigned int length)
3599 struct inode *inode = page->mapping->host;
3600 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3602 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3603 (offset % PAGE_SIZE || length != PAGE_SIZE))
3606 if (PageDirty(page)) {
3607 if (inode->i_ino == F2FS_META_INO(sbi)) {
3608 dec_page_count(sbi, F2FS_DIRTY_META);
3609 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3610 dec_page_count(sbi, F2FS_DIRTY_NODES);
3612 inode_dec_dirty_pages(inode);
3613 f2fs_remove_dirty_inode(inode);
3617 clear_cold_data(page);
3619 if (IS_ATOMIC_WRITTEN_PAGE(page))
3620 return f2fs_drop_inmem_page(inode, page);
3622 f2fs_clear_page_private(page);
3625 int f2fs_release_page(struct page *page, gfp_t wait)
3627 /* If this is dirty page, keep PagePrivate */
3628 if (PageDirty(page))
3631 /* This is atomic written page, keep Private */
3632 if (IS_ATOMIC_WRITTEN_PAGE(page))
3635 clear_cold_data(page);
3636 f2fs_clear_page_private(page);
3640 static int f2fs_set_data_page_dirty(struct page *page)
3642 struct inode *inode = page_file_mapping(page)->host;
3644 trace_f2fs_set_page_dirty(page, DATA);
3646 if (!PageUptodate(page))
3647 SetPageUptodate(page);
3648 if (PageSwapCache(page))
3649 return __set_page_dirty_nobuffers(page);
3651 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3652 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3653 f2fs_register_inmem_page(inode, page);
3657 * Previously, this page has been registered, we just
3663 if (!PageDirty(page)) {
3664 __set_page_dirty_nobuffers(page);
3665 f2fs_update_dirty_page(inode, page);
3672 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3674 #ifdef CONFIG_F2FS_FS_COMPRESSION
3675 struct dnode_of_data dn;
3676 sector_t start_idx, blknr = 0;
3679 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3681 set_new_dnode(&dn, inode, NULL, NULL, 0);
3682 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3686 if (dn.data_blkaddr != COMPRESS_ADDR) {
3687 dn.ofs_in_node += block - start_idx;
3688 blknr = f2fs_data_blkaddr(&dn);
3689 if (!__is_valid_data_blkaddr(blknr))
3693 f2fs_put_dnode(&dn);
3702 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3704 struct inode *inode = mapping->host;
3706 if (f2fs_has_inline_data(inode))
3709 /* make sure allocating whole blocks */
3710 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3711 filemap_write_and_wait(mapping);
3713 if (f2fs_compressed_file(inode))
3714 return f2fs_bmap_compress(inode, block);
3716 return generic_block_bmap(mapping, block, get_data_block_bmap);
3719 #ifdef CONFIG_MIGRATION
3720 #include <linux/migrate.h>
3722 int f2fs_migrate_page(struct address_space *mapping,
3723 struct page *newpage, struct page *page, enum migrate_mode mode)
3725 int rc, extra_count;
3726 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3727 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3729 BUG_ON(PageWriteback(page));
3731 /* migrating an atomic written page is safe with the inmem_lock hold */
3732 if (atomic_written) {
3733 if (mode != MIGRATE_SYNC)
3735 if (!mutex_trylock(&fi->inmem_lock))
3739 /* one extra reference was held for atomic_write page */
3740 extra_count = atomic_written ? 1 : 0;
3741 rc = migrate_page_move_mapping(mapping, newpage,
3743 if (rc != MIGRATEPAGE_SUCCESS) {
3745 mutex_unlock(&fi->inmem_lock);
3749 if (atomic_written) {
3750 struct inmem_pages *cur;
3751 list_for_each_entry(cur, &fi->inmem_pages, list)
3752 if (cur->page == page) {
3753 cur->page = newpage;
3756 mutex_unlock(&fi->inmem_lock);
3761 if (PagePrivate(page)) {
3762 f2fs_set_page_private(newpage, page_private(page));
3763 f2fs_clear_page_private(page);
3766 if (mode != MIGRATE_SYNC_NO_COPY)
3767 migrate_page_copy(newpage, page);
3769 migrate_page_states(newpage, page);
3771 return MIGRATEPAGE_SUCCESS;
3776 /* Copied from generic_swapfile_activate() to check any holes */
3777 static int check_swap_activate(struct swap_info_struct *sis,
3778 struct file *swap_file, sector_t *span)
3780 struct address_space *mapping = swap_file->f_mapping;
3781 struct inode *inode = mapping->host;
3782 unsigned blocks_per_page;
3783 unsigned long page_no;
3785 sector_t probe_block;
3786 sector_t last_block;
3787 sector_t lowest_block = -1;
3788 sector_t highest_block = 0;
3792 blkbits = inode->i_blkbits;
3793 blocks_per_page = PAGE_SIZE >> blkbits;
3796 * Map all the blocks into the extent list. This code doesn't try
3801 last_block = i_size_read(inode) >> blkbits;
3802 while ((probe_block + blocks_per_page) <= last_block &&
3803 page_no < sis->max) {
3804 unsigned block_in_page;
3805 sector_t first_block;
3811 block = probe_block;
3812 err = bmap(inode, &block);
3815 first_block = block;
3818 * It must be PAGE_SIZE aligned on-disk
3820 if (first_block & (blocks_per_page - 1)) {
3825 for (block_in_page = 1; block_in_page < blocks_per_page;
3828 block = probe_block + block_in_page;
3829 err = bmap(inode, &block);
3834 if (block != first_block + block_in_page) {
3841 first_block >>= (PAGE_SHIFT - blkbits);
3842 if (page_no) { /* exclude the header page */
3843 if (first_block < lowest_block)
3844 lowest_block = first_block;
3845 if (first_block > highest_block)
3846 highest_block = first_block;
3850 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3852 ret = add_swap_extent(sis, page_no, 1, first_block);
3857 probe_block += blocks_per_page;
3862 *span = 1 + highest_block - lowest_block;
3864 page_no = 1; /* force Empty message */
3866 sis->pages = page_no - 1;
3867 sis->highest_bit = page_no - 1;
3871 pr_err("swapon: swapfile has holes\n");
3875 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3878 struct inode *inode = file_inode(file);
3881 if (!S_ISREG(inode->i_mode))
3884 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3887 ret = f2fs_convert_inline_inode(inode);
3891 if (f2fs_disable_compressed_file(inode))
3894 ret = check_swap_activate(sis, file, span);
3898 set_inode_flag(inode, FI_PIN_FILE);
3899 f2fs_precache_extents(inode);
3900 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3904 static void f2fs_swap_deactivate(struct file *file)
3906 struct inode *inode = file_inode(file);
3908 clear_inode_flag(inode, FI_PIN_FILE);
3911 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3917 static void f2fs_swap_deactivate(struct file *file)
3922 const struct address_space_operations f2fs_dblock_aops = {
3923 .readpage = f2fs_read_data_page,
3924 .readahead = f2fs_readahead,
3925 .writepage = f2fs_write_data_page,
3926 .writepages = f2fs_write_data_pages,
3927 .write_begin = f2fs_write_begin,
3928 .write_end = f2fs_write_end,
3929 .set_page_dirty = f2fs_set_data_page_dirty,
3930 .invalidatepage = f2fs_invalidate_page,
3931 .releasepage = f2fs_release_page,
3932 .direct_IO = f2fs_direct_IO,
3934 .swap_activate = f2fs_swap_activate,
3935 .swap_deactivate = f2fs_swap_deactivate,
3936 #ifdef CONFIG_MIGRATION
3937 .migratepage = f2fs_migrate_page,
3941 void f2fs_clear_page_cache_dirty_tag(struct page *page)
3943 struct address_space *mapping = page_mapping(page);
3944 unsigned long flags;
3946 xa_lock_irqsave(&mapping->i_pages, flags);
3947 __xa_clear_mark(&mapping->i_pages, page_index(page),
3948 PAGECACHE_TAG_DIRTY);
3949 xa_unlock_irqrestore(&mapping->i_pages, flags);
3952 int __init f2fs_init_post_read_processing(void)
3954 bio_post_read_ctx_cache =
3955 kmem_cache_create("f2fs_bio_post_read_ctx",
3956 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
3957 if (!bio_post_read_ctx_cache)
3959 bio_post_read_ctx_pool =
3960 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3961 bio_post_read_ctx_cache);
3962 if (!bio_post_read_ctx_pool)
3963 goto fail_free_cache;
3967 kmem_cache_destroy(bio_post_read_ctx_cache);
3972 void f2fs_destroy_post_read_processing(void)
3974 mempool_destroy(bio_post_read_ctx_pool);
3975 kmem_cache_destroy(bio_post_read_ctx_cache);
3978 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
3980 if (!f2fs_sb_has_encrypt(sbi) &&
3981 !f2fs_sb_has_verity(sbi) &&
3982 !f2fs_sb_has_compression(sbi))
3985 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
3986 WQ_UNBOUND | WQ_HIGHPRI,
3988 if (!sbi->post_read_wq)
3993 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
3995 if (sbi->post_read_wq)
3996 destroy_workqueue(sbi->post_read_wq);
3999 int __init f2fs_init_bio_entry_cache(void)
4001 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4002 sizeof(struct bio_entry));
4003 if (!bio_entry_slab)
4008 void f2fs_destroy_bio_entry_cache(void)
4010 kmem_cache_destroy(bio_entry_slab);