io_uring: don't use 'fd' for openat/openat2/statx
[linux/fpc-iii.git] / fs / f2fs / data.c
blob34990866cfe965a6048079032ce108e588d5b699
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/data.c
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
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>
23 #include "f2fs.h"
24 #include "node.h"
25 #include "segment.h"
26 #include "trace.h"
27 #include <trace/events/f2fs.h>
29 #define NUM_PREALLOC_POST_READ_CTXS 128
31 static struct kmem_cache *bio_post_read_ctx_cache;
32 static struct kmem_cache *bio_entry_slab;
33 static mempool_t *bio_post_read_ctx_pool;
34 static struct bio_set f2fs_bioset;
36 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
38 int __init f2fs_init_bioset(void)
40 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
41 0, BIOSET_NEED_BVECS))
42 return -ENOMEM;
43 return 0;
46 void f2fs_destroy_bioset(void)
48 bioset_exit(&f2fs_bioset);
51 static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
52 unsigned int nr_iovecs)
54 return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
57 struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool no_fail)
59 struct bio *bio;
61 if (no_fail) {
62 /* No failure on bio allocation */
63 bio = __f2fs_bio_alloc(GFP_NOIO, npages);
64 if (!bio)
65 bio = __f2fs_bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
66 return bio;
68 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
69 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
70 return NULL;
73 return __f2fs_bio_alloc(GFP_KERNEL, npages);
76 static bool __is_cp_guaranteed(struct page *page)
78 struct address_space *mapping = page->mapping;
79 struct inode *inode;
80 struct f2fs_sb_info *sbi;
82 if (!mapping)
83 return false;
85 if (f2fs_is_compressed_page(page))
86 return false;
88 inode = mapping->host;
89 sbi = F2FS_I_SB(inode);
91 if (inode->i_ino == F2FS_META_INO(sbi) ||
92 inode->i_ino == F2FS_NODE_INO(sbi) ||
93 S_ISDIR(inode->i_mode) ||
94 (S_ISREG(inode->i_mode) &&
95 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
96 is_cold_data(page))
97 return true;
98 return false;
101 static enum count_type __read_io_type(struct page *page)
103 struct address_space *mapping = page_file_mapping(page);
105 if (mapping) {
106 struct inode *inode = mapping->host;
107 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
109 if (inode->i_ino == F2FS_META_INO(sbi))
110 return F2FS_RD_META;
112 if (inode->i_ino == F2FS_NODE_INO(sbi))
113 return F2FS_RD_NODE;
115 return F2FS_RD_DATA;
118 /* postprocessing steps for read bios */
119 enum bio_post_read_step {
120 STEP_DECRYPT,
121 STEP_DECOMPRESS,
122 STEP_VERITY,
125 struct bio_post_read_ctx {
126 struct bio *bio;
127 struct f2fs_sb_info *sbi;
128 struct work_struct work;
129 unsigned int enabled_steps;
132 static void __read_end_io(struct bio *bio, bool compr, bool verity)
134 struct page *page;
135 struct bio_vec *bv;
136 struct bvec_iter_all iter_all;
138 bio_for_each_segment_all(bv, bio, iter_all) {
139 page = bv->bv_page;
141 #ifdef CONFIG_F2FS_FS_COMPRESSION
142 if (compr && f2fs_is_compressed_page(page)) {
143 f2fs_decompress_pages(bio, page, verity);
144 continue;
146 #endif
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);
153 } else {
154 SetPageUptodate(page);
156 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
157 unlock_page(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)
164 if (!compr)
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)
194 struct bio_vec *bv;
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);
203 if (dic) {
204 if (refcount_dec_not_one(&dic->ref))
205 continue;
206 f2fs_verify_pages(dic->rpages,
207 dic->cluster_size);
208 f2fs_free_dic(dic);
209 continue;
212 if (bio->bi_status || PageError(page))
213 goto clear_uptodate;
215 if (fsverity_verify_page(page)) {
216 SetPageUptodate(page);
217 goto unlock;
219 clear_uptodate:
220 ClearPageUptodate(page);
221 ClearPageError(page);
222 unlock:
223 unlock_page(page);
226 #endif
228 static void f2fs_verity_work(struct work_struct *work)
230 struct bio_post_read_ctx *ctx =
231 container_of(work, struct bio_post_read_ctx, work);
232 struct bio *bio = ctx->bio;
233 #ifdef CONFIG_F2FS_FS_COMPRESSION
234 unsigned int enabled_steps = ctx->enabled_steps;
235 #endif
238 * fsverity_verify_bio() may call readpages() again, and while verity
239 * will be disabled for this, decryption may still be needed, resulting
240 * in another bio_post_read_ctx being allocated. So to prevent
241 * deadlocks we need to release the current ctx to the mempool first.
242 * This assumes that verity is the last post-read step.
244 mempool_free(ctx, bio_post_read_ctx_pool);
245 bio->bi_private = NULL;
247 #ifdef CONFIG_F2FS_FS_COMPRESSION
248 /* previous step is decompression */
249 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
250 f2fs_verify_bio(bio);
251 f2fs_release_read_bio(bio);
252 return;
254 #endif
256 fsverity_verify_bio(bio);
257 __f2fs_read_end_io(bio, false, false);
260 static void f2fs_post_read_work(struct work_struct *work)
262 struct bio_post_read_ctx *ctx =
263 container_of(work, struct bio_post_read_ctx, work);
265 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
266 f2fs_decrypt_work(ctx);
268 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
269 f2fs_decompress_work(ctx);
271 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
272 INIT_WORK(&ctx->work, f2fs_verity_work);
273 fsverity_enqueue_verify_work(&ctx->work);
274 return;
277 __f2fs_read_end_io(ctx->bio,
278 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
281 static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
282 struct work_struct *work)
284 queue_work(sbi->post_read_wq, work);
287 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
290 * We use different work queues for decryption and for verity because
291 * verity may require reading metadata pages that need decryption, and
292 * we shouldn't recurse to the same workqueue.
295 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
296 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
297 INIT_WORK(&ctx->work, f2fs_post_read_work);
298 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
299 return;
302 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
303 INIT_WORK(&ctx->work, f2fs_verity_work);
304 fsverity_enqueue_verify_work(&ctx->work);
305 return;
308 __f2fs_read_end_io(ctx->bio, false, false);
311 static bool f2fs_bio_post_read_required(struct bio *bio)
313 return bio->bi_private;
316 static void f2fs_read_end_io(struct bio *bio)
318 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
320 if (time_to_inject(sbi, FAULT_READ_IO)) {
321 f2fs_show_injection_info(sbi, FAULT_READ_IO);
322 bio->bi_status = BLK_STS_IOERR;
325 if (f2fs_bio_post_read_required(bio)) {
326 struct bio_post_read_ctx *ctx = bio->bi_private;
328 bio_post_read_processing(ctx);
329 return;
332 __f2fs_read_end_io(bio, false, false);
335 static void f2fs_write_end_io(struct bio *bio)
337 struct f2fs_sb_info *sbi = bio->bi_private;
338 struct bio_vec *bvec;
339 struct bvec_iter_all iter_all;
341 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
342 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
343 bio->bi_status = BLK_STS_IOERR;
346 bio_for_each_segment_all(bvec, bio, iter_all) {
347 struct page *page = bvec->bv_page;
348 enum count_type type = WB_DATA_TYPE(page);
350 if (IS_DUMMY_WRITTEN_PAGE(page)) {
351 set_page_private(page, (unsigned long)NULL);
352 ClearPagePrivate(page);
353 unlock_page(page);
354 mempool_free(page, sbi->write_io_dummy);
356 if (unlikely(bio->bi_status))
357 f2fs_stop_checkpoint(sbi, true);
358 continue;
361 fscrypt_finalize_bounce_page(&page);
363 #ifdef CONFIG_F2FS_FS_COMPRESSION
364 if (f2fs_is_compressed_page(page)) {
365 f2fs_compress_write_end_io(bio, page);
366 continue;
368 #endif
370 if (unlikely(bio->bi_status)) {
371 mapping_set_error(page->mapping, -EIO);
372 if (type == F2FS_WB_CP_DATA)
373 f2fs_stop_checkpoint(sbi, true);
376 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
377 page->index != nid_of_node(page));
379 dec_page_count(sbi, type);
380 if (f2fs_in_warm_node_list(sbi, page))
381 f2fs_del_fsync_node_entry(sbi, page);
382 clear_cold_data(page);
383 end_page_writeback(page);
385 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
386 wq_has_sleeper(&sbi->cp_wait))
387 wake_up(&sbi->cp_wait);
389 bio_put(bio);
393 * Return true, if pre_bio's bdev is same as its target device.
395 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
396 block_t blk_addr, struct bio *bio)
398 struct block_device *bdev = sbi->sb->s_bdev;
399 int i;
401 if (f2fs_is_multi_device(sbi)) {
402 for (i = 0; i < sbi->s_ndevs; i++) {
403 if (FDEV(i).start_blk <= blk_addr &&
404 FDEV(i).end_blk >= blk_addr) {
405 blk_addr -= FDEV(i).start_blk;
406 bdev = FDEV(i).bdev;
407 break;
411 if (bio) {
412 bio_set_dev(bio, bdev);
413 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
415 return bdev;
418 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
420 int i;
422 if (!f2fs_is_multi_device(sbi))
423 return 0;
425 for (i = 0; i < sbi->s_ndevs; i++)
426 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
427 return i;
428 return 0;
431 static bool __same_bdev(struct f2fs_sb_info *sbi,
432 block_t blk_addr, struct bio *bio)
434 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
435 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
439 * Low-level block read/write IO operations.
441 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
443 struct f2fs_sb_info *sbi = fio->sbi;
444 struct bio *bio;
446 bio = f2fs_bio_alloc(sbi, npages, true);
448 f2fs_target_device(sbi, fio->new_blkaddr, bio);
449 if (is_read_io(fio->op)) {
450 bio->bi_end_io = f2fs_read_end_io;
451 bio->bi_private = NULL;
452 } else {
453 bio->bi_end_io = f2fs_write_end_io;
454 bio->bi_private = sbi;
455 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
456 fio->type, fio->temp);
458 if (fio->io_wbc)
459 wbc_init_bio(fio->io_wbc, bio);
461 return bio;
464 static inline void __submit_bio(struct f2fs_sb_info *sbi,
465 struct bio *bio, enum page_type type)
467 if (!is_read_io(bio_op(bio))) {
468 unsigned int start;
470 if (type != DATA && type != NODE)
471 goto submit_io;
473 if (test_opt(sbi, LFS) && current->plug)
474 blk_finish_plug(current->plug);
476 if (F2FS_IO_ALIGNED(sbi))
477 goto submit_io;
479 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
480 start %= F2FS_IO_SIZE(sbi);
482 if (start == 0)
483 goto submit_io;
485 /* fill dummy pages */
486 for (; start < F2FS_IO_SIZE(sbi); start++) {
487 struct page *page =
488 mempool_alloc(sbi->write_io_dummy,
489 GFP_NOIO | __GFP_NOFAIL);
490 f2fs_bug_on(sbi, !page);
492 zero_user_segment(page, 0, PAGE_SIZE);
493 SetPagePrivate(page);
494 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
495 lock_page(page);
496 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
497 f2fs_bug_on(sbi, 1);
500 * In the NODE case, we lose next block address chain. So, we
501 * need to do checkpoint in f2fs_sync_file.
503 if (type == NODE)
504 set_sbi_flag(sbi, SBI_NEED_CP);
506 submit_io:
507 if (is_read_io(bio_op(bio)))
508 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
509 else
510 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
511 submit_bio(bio);
514 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
515 struct bio *bio, enum page_type type)
517 __submit_bio(sbi, bio, type);
520 static void __submit_merged_bio(struct f2fs_bio_info *io)
522 struct f2fs_io_info *fio = &io->fio;
524 if (!io->bio)
525 return;
527 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
529 if (is_read_io(fio->op))
530 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
531 else
532 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
534 __submit_bio(io->sbi, io->bio, fio->type);
535 io->bio = NULL;
538 static bool __has_merged_page(struct bio *bio, struct inode *inode,
539 struct page *page, nid_t ino)
541 struct bio_vec *bvec;
542 struct bvec_iter_all iter_all;
544 if (!bio)
545 return false;
547 if (!inode && !page && !ino)
548 return true;
550 bio_for_each_segment_all(bvec, bio, iter_all) {
551 struct page *target = bvec->bv_page;
553 if (fscrypt_is_bounce_page(target)) {
554 target = fscrypt_pagecache_page(target);
555 if (IS_ERR(target))
556 continue;
558 if (f2fs_is_compressed_page(target)) {
559 target = f2fs_compress_control_page(target);
560 if (IS_ERR(target))
561 continue;
564 if (inode && inode == target->mapping->host)
565 return true;
566 if (page && page == target)
567 return true;
568 if (ino && ino == ino_of_node(target))
569 return true;
572 return false;
575 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
576 enum page_type type, enum temp_type temp)
578 enum page_type btype = PAGE_TYPE_OF_BIO(type);
579 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
581 down_write(&io->io_rwsem);
583 /* change META to META_FLUSH in the checkpoint procedure */
584 if (type >= META_FLUSH) {
585 io->fio.type = META_FLUSH;
586 io->fio.op = REQ_OP_WRITE;
587 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
588 if (!test_opt(sbi, NOBARRIER))
589 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
591 __submit_merged_bio(io);
592 up_write(&io->io_rwsem);
595 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
596 struct inode *inode, struct page *page,
597 nid_t ino, enum page_type type, bool force)
599 enum temp_type temp;
600 bool ret = true;
602 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
603 if (!force) {
604 enum page_type btype = PAGE_TYPE_OF_BIO(type);
605 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
607 down_read(&io->io_rwsem);
608 ret = __has_merged_page(io->bio, inode, page, ino);
609 up_read(&io->io_rwsem);
611 if (ret)
612 __f2fs_submit_merged_write(sbi, type, temp);
614 /* TODO: use HOT temp only for meta pages now. */
615 if (type >= META)
616 break;
620 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
622 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
625 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
626 struct inode *inode, struct page *page,
627 nid_t ino, enum page_type type)
629 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
632 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
634 f2fs_submit_merged_write(sbi, DATA);
635 f2fs_submit_merged_write(sbi, NODE);
636 f2fs_submit_merged_write(sbi, META);
640 * Fill the locked page with data located in the block address.
641 * A caller needs to unlock the page on failure.
643 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
645 struct bio *bio;
646 struct page *page = fio->encrypted_page ?
647 fio->encrypted_page : fio->page;
649 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
650 fio->is_por ? META_POR : (__is_meta_io(fio) ?
651 META_GENERIC : DATA_GENERIC_ENHANCE)))
652 return -EFSCORRUPTED;
654 trace_f2fs_submit_page_bio(page, fio);
655 f2fs_trace_ios(fio, 0);
657 /* Allocate a new bio */
658 bio = __bio_alloc(fio, 1);
660 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
661 bio_put(bio);
662 return -EFAULT;
665 if (fio->io_wbc && !is_read_io(fio->op))
666 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
668 bio_set_op_attrs(bio, fio->op, fio->op_flags);
670 inc_page_count(fio->sbi, is_read_io(fio->op) ?
671 __read_io_type(page): WB_DATA_TYPE(fio->page));
673 __submit_bio(fio->sbi, bio, fio->type);
674 return 0;
677 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
678 block_t last_blkaddr, block_t cur_blkaddr)
680 if (last_blkaddr + 1 != cur_blkaddr)
681 return false;
682 return __same_bdev(sbi, cur_blkaddr, bio);
685 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
686 struct f2fs_io_info *fio)
688 if (io->fio.op != fio->op)
689 return false;
690 return io->fio.op_flags == fio->op_flags;
693 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
694 struct f2fs_bio_info *io,
695 struct f2fs_io_info *fio,
696 block_t last_blkaddr,
697 block_t cur_blkaddr)
699 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
700 unsigned int filled_blocks =
701 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
702 unsigned int io_size = F2FS_IO_SIZE(sbi);
703 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
705 /* IOs in bio is aligned and left space of vectors is not enough */
706 if (!(filled_blocks % io_size) && left_vecs < io_size)
707 return false;
709 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
710 return false;
711 return io_type_is_mergeable(io, fio);
714 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
715 struct page *page, enum temp_type temp)
717 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
718 struct bio_entry *be;
720 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
721 be->bio = bio;
722 bio_get(bio);
724 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
725 f2fs_bug_on(sbi, 1);
727 down_write(&io->bio_list_lock);
728 list_add_tail(&be->list, &io->bio_list);
729 up_write(&io->bio_list_lock);
732 static void del_bio_entry(struct bio_entry *be)
734 list_del(&be->list);
735 kmem_cache_free(bio_entry_slab, be);
738 static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio,
739 struct page *page)
741 enum temp_type temp;
742 bool found = false;
743 int ret = -EAGAIN;
745 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
746 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
747 struct list_head *head = &io->bio_list;
748 struct bio_entry *be;
750 down_write(&io->bio_list_lock);
751 list_for_each_entry(be, head, list) {
752 if (be->bio != *bio)
753 continue;
755 found = true;
757 if (bio_add_page(*bio, page, PAGE_SIZE, 0) ==
758 PAGE_SIZE) {
759 ret = 0;
760 break;
763 /* bio is full */
764 del_bio_entry(be);
765 __submit_bio(sbi, *bio, DATA);
766 break;
768 up_write(&io->bio_list_lock);
771 if (ret) {
772 bio_put(*bio);
773 *bio = NULL;
776 return ret;
779 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
780 struct bio **bio, struct page *page)
782 enum temp_type temp;
783 bool found = false;
784 struct bio *target = bio ? *bio : NULL;
786 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
787 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
788 struct list_head *head = &io->bio_list;
789 struct bio_entry *be;
791 if (list_empty(head))
792 continue;
794 down_read(&io->bio_list_lock);
795 list_for_each_entry(be, head, list) {
796 if (target)
797 found = (target == be->bio);
798 else
799 found = __has_merged_page(be->bio, NULL,
800 page, 0);
801 if (found)
802 break;
804 up_read(&io->bio_list_lock);
806 if (!found)
807 continue;
809 found = false;
811 down_write(&io->bio_list_lock);
812 list_for_each_entry(be, head, list) {
813 if (target)
814 found = (target == be->bio);
815 else
816 found = __has_merged_page(be->bio, NULL,
817 page, 0);
818 if (found) {
819 target = be->bio;
820 del_bio_entry(be);
821 break;
824 up_write(&io->bio_list_lock);
827 if (found)
828 __submit_bio(sbi, target, DATA);
829 if (bio && *bio) {
830 bio_put(*bio);
831 *bio = NULL;
835 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
837 struct bio *bio = *fio->bio;
838 struct page *page = fio->encrypted_page ?
839 fio->encrypted_page : fio->page;
841 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
842 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
843 return -EFSCORRUPTED;
845 trace_f2fs_submit_page_bio(page, fio);
846 f2fs_trace_ios(fio, 0);
848 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
849 fio->new_blkaddr))
850 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
851 alloc_new:
852 if (!bio) {
853 bio = __bio_alloc(fio, BIO_MAX_PAGES);
854 bio_set_op_attrs(bio, fio->op, fio->op_flags);
856 add_bio_entry(fio->sbi, bio, page, fio->temp);
857 } else {
858 if (add_ipu_page(fio->sbi, &bio, page))
859 goto alloc_new;
862 if (fio->io_wbc)
863 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
865 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
867 *fio->last_block = fio->new_blkaddr;
868 *fio->bio = bio;
870 return 0;
873 void f2fs_submit_page_write(struct f2fs_io_info *fio)
875 struct f2fs_sb_info *sbi = fio->sbi;
876 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
877 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
878 struct page *bio_page;
880 f2fs_bug_on(sbi, is_read_io(fio->op));
882 down_write(&io->io_rwsem);
883 next:
884 if (fio->in_list) {
885 spin_lock(&io->io_lock);
886 if (list_empty(&io->io_list)) {
887 spin_unlock(&io->io_lock);
888 goto out;
890 fio = list_first_entry(&io->io_list,
891 struct f2fs_io_info, list);
892 list_del(&fio->list);
893 spin_unlock(&io->io_lock);
896 verify_fio_blkaddr(fio);
898 if (fio->encrypted_page)
899 bio_page = fio->encrypted_page;
900 else if (fio->compressed_page)
901 bio_page = fio->compressed_page;
902 else
903 bio_page = fio->page;
905 /* set submitted = true as a return value */
906 fio->submitted = true;
908 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
910 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio,
911 io->last_block_in_bio, fio->new_blkaddr))
912 __submit_merged_bio(io);
913 alloc_new:
914 if (io->bio == NULL) {
915 if (F2FS_IO_ALIGNED(sbi) &&
916 (fio->type == DATA || fio->type == NODE) &&
917 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
918 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
919 fio->retry = true;
920 goto skip;
922 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
923 io->fio = *fio;
926 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
927 __submit_merged_bio(io);
928 goto alloc_new;
931 if (fio->io_wbc)
932 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
934 io->last_block_in_bio = fio->new_blkaddr;
935 f2fs_trace_ios(fio, 0);
937 trace_f2fs_submit_page_write(fio->page, fio);
938 skip:
939 if (fio->in_list)
940 goto next;
941 out:
942 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
943 !f2fs_is_checkpoint_ready(sbi))
944 __submit_merged_bio(io);
945 up_write(&io->io_rwsem);
948 static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
950 return fsverity_active(inode) &&
951 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
954 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
955 unsigned nr_pages, unsigned op_flag,
956 pgoff_t first_idx)
958 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
959 struct bio *bio;
960 struct bio_post_read_ctx *ctx;
961 unsigned int post_read_steps = 0;
963 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
964 if (!bio)
965 return ERR_PTR(-ENOMEM);
966 f2fs_target_device(sbi, blkaddr, bio);
967 bio->bi_end_io = f2fs_read_end_io;
968 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
970 if (f2fs_encrypted_file(inode))
971 post_read_steps |= 1 << STEP_DECRYPT;
972 if (f2fs_compressed_file(inode))
973 post_read_steps |= 1 << STEP_DECOMPRESS;
974 if (f2fs_need_verity(inode, first_idx))
975 post_read_steps |= 1 << STEP_VERITY;
977 if (post_read_steps) {
978 /* Due to the mempool, this never fails. */
979 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
980 ctx->bio = bio;
981 ctx->sbi = sbi;
982 ctx->enabled_steps = post_read_steps;
983 bio->bi_private = ctx;
986 return bio;
989 static void f2fs_release_read_bio(struct bio *bio)
991 if (bio->bi_private)
992 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
993 bio_put(bio);
996 /* This can handle encryption stuffs */
997 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
998 block_t blkaddr)
1000 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1001 struct bio *bio;
1003 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index);
1004 if (IS_ERR(bio))
1005 return PTR_ERR(bio);
1007 /* wait for GCed page writeback via META_MAPPING */
1008 f2fs_wait_on_block_writeback(inode, blkaddr);
1010 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1011 bio_put(bio);
1012 return -EFAULT;
1014 ClearPageError(page);
1015 inc_page_count(sbi, F2FS_RD_DATA);
1016 __submit_bio(sbi, bio, DATA);
1017 return 0;
1020 static void __set_data_blkaddr(struct dnode_of_data *dn)
1022 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1023 __le32 *addr_array;
1024 int base = 0;
1026 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1027 base = get_extra_isize(dn->inode);
1029 /* Get physical address of data block */
1030 addr_array = blkaddr_in_node(rn);
1031 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1035 * Lock ordering for the change of data block address:
1036 * ->data_page
1037 * ->node_page
1038 * update block addresses in the node page
1040 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1042 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1043 __set_data_blkaddr(dn);
1044 if (set_page_dirty(dn->node_page))
1045 dn->node_changed = true;
1048 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1050 dn->data_blkaddr = blkaddr;
1051 f2fs_set_data_blkaddr(dn);
1052 f2fs_update_extent_cache(dn);
1055 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1056 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1058 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1059 int err;
1061 if (!count)
1062 return 0;
1064 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1065 return -EPERM;
1066 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1067 return err;
1069 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1070 dn->ofs_in_node, count);
1072 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1074 for (; count > 0; dn->ofs_in_node++) {
1075 block_t blkaddr = datablock_addr(dn->inode,
1076 dn->node_page, dn->ofs_in_node);
1077 if (blkaddr == NULL_ADDR) {
1078 dn->data_blkaddr = NEW_ADDR;
1079 __set_data_blkaddr(dn);
1080 count--;
1084 if (set_page_dirty(dn->node_page))
1085 dn->node_changed = true;
1086 return 0;
1089 /* Should keep dn->ofs_in_node unchanged */
1090 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1092 unsigned int ofs_in_node = dn->ofs_in_node;
1093 int ret;
1095 ret = f2fs_reserve_new_blocks(dn, 1);
1096 dn->ofs_in_node = ofs_in_node;
1097 return ret;
1100 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1102 bool need_put = dn->inode_page ? false : true;
1103 int err;
1105 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1106 if (err)
1107 return err;
1109 if (dn->data_blkaddr == NULL_ADDR)
1110 err = f2fs_reserve_new_block(dn);
1111 if (err || need_put)
1112 f2fs_put_dnode(dn);
1113 return err;
1116 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1118 struct extent_info ei = {0,0,0};
1119 struct inode *inode = dn->inode;
1121 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1122 dn->data_blkaddr = ei.blk + index - ei.fofs;
1123 return 0;
1126 return f2fs_reserve_block(dn, index);
1129 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1130 int op_flags, bool for_write)
1132 struct address_space *mapping = inode->i_mapping;
1133 struct dnode_of_data dn;
1134 struct page *page;
1135 struct extent_info ei = {0,0,0};
1136 int err;
1138 page = f2fs_grab_cache_page(mapping, index, for_write);
1139 if (!page)
1140 return ERR_PTR(-ENOMEM);
1142 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1143 dn.data_blkaddr = ei.blk + index - ei.fofs;
1144 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1145 DATA_GENERIC_ENHANCE_READ)) {
1146 err = -EFSCORRUPTED;
1147 goto put_err;
1149 goto got_it;
1152 set_new_dnode(&dn, inode, NULL, NULL, 0);
1153 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1154 if (err)
1155 goto put_err;
1156 f2fs_put_dnode(&dn);
1158 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1159 err = -ENOENT;
1160 goto put_err;
1162 if (dn.data_blkaddr != NEW_ADDR &&
1163 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1164 dn.data_blkaddr,
1165 DATA_GENERIC_ENHANCE)) {
1166 err = -EFSCORRUPTED;
1167 goto put_err;
1169 got_it:
1170 if (PageUptodate(page)) {
1171 unlock_page(page);
1172 return page;
1176 * A new dentry page is allocated but not able to be written, since its
1177 * new inode page couldn't be allocated due to -ENOSPC.
1178 * In such the case, its blkaddr can be remained as NEW_ADDR.
1179 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1180 * f2fs_init_inode_metadata.
1182 if (dn.data_blkaddr == NEW_ADDR) {
1183 zero_user_segment(page, 0, PAGE_SIZE);
1184 if (!PageUptodate(page))
1185 SetPageUptodate(page);
1186 unlock_page(page);
1187 return page;
1190 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
1191 if (err)
1192 goto put_err;
1193 return page;
1195 put_err:
1196 f2fs_put_page(page, 1);
1197 return ERR_PTR(err);
1200 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1202 struct address_space *mapping = inode->i_mapping;
1203 struct page *page;
1205 page = find_get_page(mapping, index);
1206 if (page && PageUptodate(page))
1207 return page;
1208 f2fs_put_page(page, 0);
1210 page = f2fs_get_read_data_page(inode, index, 0, false);
1211 if (IS_ERR(page))
1212 return page;
1214 if (PageUptodate(page))
1215 return page;
1217 wait_on_page_locked(page);
1218 if (unlikely(!PageUptodate(page))) {
1219 f2fs_put_page(page, 0);
1220 return ERR_PTR(-EIO);
1222 return page;
1226 * If it tries to access a hole, return an error.
1227 * Because, the callers, functions in dir.c and GC, should be able to know
1228 * whether this page exists or not.
1230 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1231 bool for_write)
1233 struct address_space *mapping = inode->i_mapping;
1234 struct page *page;
1235 repeat:
1236 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1237 if (IS_ERR(page))
1238 return page;
1240 /* wait for read completion */
1241 lock_page(page);
1242 if (unlikely(page->mapping != mapping)) {
1243 f2fs_put_page(page, 1);
1244 goto repeat;
1246 if (unlikely(!PageUptodate(page))) {
1247 f2fs_put_page(page, 1);
1248 return ERR_PTR(-EIO);
1250 return page;
1254 * Caller ensures that this data page is never allocated.
1255 * A new zero-filled data page is allocated in the page cache.
1257 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1258 * f2fs_unlock_op().
1259 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1260 * ipage should be released by this function.
1262 struct page *f2fs_get_new_data_page(struct inode *inode,
1263 struct page *ipage, pgoff_t index, bool new_i_size)
1265 struct address_space *mapping = inode->i_mapping;
1266 struct page *page;
1267 struct dnode_of_data dn;
1268 int err;
1270 page = f2fs_grab_cache_page(mapping, index, true);
1271 if (!page) {
1273 * before exiting, we should make sure ipage will be released
1274 * if any error occur.
1276 f2fs_put_page(ipage, 1);
1277 return ERR_PTR(-ENOMEM);
1280 set_new_dnode(&dn, inode, ipage, NULL, 0);
1281 err = f2fs_reserve_block(&dn, index);
1282 if (err) {
1283 f2fs_put_page(page, 1);
1284 return ERR_PTR(err);
1286 if (!ipage)
1287 f2fs_put_dnode(&dn);
1289 if (PageUptodate(page))
1290 goto got_it;
1292 if (dn.data_blkaddr == NEW_ADDR) {
1293 zero_user_segment(page, 0, PAGE_SIZE);
1294 if (!PageUptodate(page))
1295 SetPageUptodate(page);
1296 } else {
1297 f2fs_put_page(page, 1);
1299 /* if ipage exists, blkaddr should be NEW_ADDR */
1300 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1301 page = f2fs_get_lock_data_page(inode, index, true);
1302 if (IS_ERR(page))
1303 return page;
1305 got_it:
1306 if (new_i_size && i_size_read(inode) <
1307 ((loff_t)(index + 1) << PAGE_SHIFT))
1308 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1309 return page;
1312 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1314 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1315 struct f2fs_summary sum;
1316 struct node_info ni;
1317 block_t old_blkaddr;
1318 blkcnt_t count = 1;
1319 int err;
1321 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1322 return -EPERM;
1324 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1325 if (err)
1326 return err;
1328 dn->data_blkaddr = datablock_addr(dn->inode,
1329 dn->node_page, dn->ofs_in_node);
1330 if (dn->data_blkaddr != NULL_ADDR)
1331 goto alloc;
1333 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1334 return err;
1336 alloc:
1337 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1338 old_blkaddr = dn->data_blkaddr;
1339 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1340 &sum, seg_type, NULL, false);
1341 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1342 invalidate_mapping_pages(META_MAPPING(sbi),
1343 old_blkaddr, old_blkaddr);
1344 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1347 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1348 * data from unwritten block via dio_read.
1350 return 0;
1353 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1355 struct inode *inode = file_inode(iocb->ki_filp);
1356 struct f2fs_map_blocks map;
1357 int flag;
1358 int err = 0;
1359 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1361 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1362 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1363 if (map.m_len > map.m_lblk)
1364 map.m_len -= map.m_lblk;
1365 else
1366 map.m_len = 0;
1368 map.m_next_pgofs = NULL;
1369 map.m_next_extent = NULL;
1370 map.m_seg_type = NO_CHECK_TYPE;
1371 map.m_may_create = true;
1373 if (direct_io) {
1374 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1375 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1376 F2FS_GET_BLOCK_PRE_AIO :
1377 F2FS_GET_BLOCK_PRE_DIO;
1378 goto map_blocks;
1380 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1381 err = f2fs_convert_inline_inode(inode);
1382 if (err)
1383 return err;
1385 if (f2fs_has_inline_data(inode))
1386 return err;
1388 flag = F2FS_GET_BLOCK_PRE_AIO;
1390 map_blocks:
1391 err = f2fs_map_blocks(inode, &map, 1, flag);
1392 if (map.m_len > 0 && err == -ENOSPC) {
1393 if (!direct_io)
1394 set_inode_flag(inode, FI_NO_PREALLOC);
1395 err = 0;
1397 return err;
1400 void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1402 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1403 if (lock)
1404 down_read(&sbi->node_change);
1405 else
1406 up_read(&sbi->node_change);
1407 } else {
1408 if (lock)
1409 f2fs_lock_op(sbi);
1410 else
1411 f2fs_unlock_op(sbi);
1416 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1417 * f2fs_map_blocks structure.
1418 * If original data blocks are allocated, then give them to blockdev.
1419 * Otherwise,
1420 * a. preallocate requested block addresses
1421 * b. do not use extent cache for better performance
1422 * c. give the block addresses to blockdev
1424 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1425 int create, int flag)
1427 unsigned int maxblocks = map->m_len;
1428 struct dnode_of_data dn;
1429 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1430 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1431 pgoff_t pgofs, end_offset, end;
1432 int err = 0, ofs = 1;
1433 unsigned int ofs_in_node, last_ofs_in_node;
1434 blkcnt_t prealloc;
1435 struct extent_info ei = {0,0,0};
1436 block_t blkaddr;
1437 unsigned int start_pgofs;
1439 if (!maxblocks)
1440 return 0;
1442 map->m_len = 0;
1443 map->m_flags = 0;
1445 /* it only supports block size == page size */
1446 pgofs = (pgoff_t)map->m_lblk;
1447 end = pgofs + maxblocks;
1449 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1450 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1451 map->m_may_create)
1452 goto next_dnode;
1454 map->m_pblk = ei.blk + pgofs - ei.fofs;
1455 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1456 map->m_flags = F2FS_MAP_MAPPED;
1457 if (map->m_next_extent)
1458 *map->m_next_extent = pgofs + map->m_len;
1460 /* for hardware encryption, but to avoid potential issue in future */
1461 if (flag == F2FS_GET_BLOCK_DIO)
1462 f2fs_wait_on_block_writeback_range(inode,
1463 map->m_pblk, map->m_len);
1464 goto out;
1467 next_dnode:
1468 if (map->m_may_create)
1469 __do_map_lock(sbi, flag, true);
1471 /* When reading holes, we need its node page */
1472 set_new_dnode(&dn, inode, NULL, NULL, 0);
1473 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1474 if (err) {
1475 if (flag == F2FS_GET_BLOCK_BMAP)
1476 map->m_pblk = 0;
1477 if (err == -ENOENT) {
1478 err = 0;
1479 if (map->m_next_pgofs)
1480 *map->m_next_pgofs =
1481 f2fs_get_next_page_offset(&dn, pgofs);
1482 if (map->m_next_extent)
1483 *map->m_next_extent =
1484 f2fs_get_next_page_offset(&dn, pgofs);
1486 goto unlock_out;
1489 start_pgofs = pgofs;
1490 prealloc = 0;
1491 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1492 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1494 next_block:
1495 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1497 if (__is_valid_data_blkaddr(blkaddr) &&
1498 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1499 err = -EFSCORRUPTED;
1500 goto sync_out;
1503 if (__is_valid_data_blkaddr(blkaddr)) {
1504 /* use out-place-update for driect IO under LFS mode */
1505 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1506 map->m_may_create) {
1507 err = __allocate_data_block(&dn, map->m_seg_type);
1508 if (err)
1509 goto sync_out;
1510 blkaddr = dn.data_blkaddr;
1511 set_inode_flag(inode, FI_APPEND_WRITE);
1513 } else {
1514 if (create) {
1515 if (unlikely(f2fs_cp_error(sbi))) {
1516 err = -EIO;
1517 goto sync_out;
1519 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1520 if (blkaddr == NULL_ADDR) {
1521 prealloc++;
1522 last_ofs_in_node = dn.ofs_in_node;
1524 } else {
1525 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1526 flag != F2FS_GET_BLOCK_DIO);
1527 err = __allocate_data_block(&dn,
1528 map->m_seg_type);
1529 if (!err)
1530 set_inode_flag(inode, FI_APPEND_WRITE);
1532 if (err)
1533 goto sync_out;
1534 map->m_flags |= F2FS_MAP_NEW;
1535 blkaddr = dn.data_blkaddr;
1536 } else {
1537 if (flag == F2FS_GET_BLOCK_BMAP) {
1538 map->m_pblk = 0;
1539 goto sync_out;
1541 if (flag == F2FS_GET_BLOCK_PRECACHE)
1542 goto sync_out;
1543 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1544 blkaddr == NULL_ADDR) {
1545 if (map->m_next_pgofs)
1546 *map->m_next_pgofs = pgofs + 1;
1547 goto sync_out;
1549 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1550 /* for defragment case */
1551 if (map->m_next_pgofs)
1552 *map->m_next_pgofs = pgofs + 1;
1553 goto sync_out;
1558 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1559 goto skip;
1561 if (map->m_len == 0) {
1562 /* preallocated unwritten block should be mapped for fiemap. */
1563 if (blkaddr == NEW_ADDR)
1564 map->m_flags |= F2FS_MAP_UNWRITTEN;
1565 map->m_flags |= F2FS_MAP_MAPPED;
1567 map->m_pblk = blkaddr;
1568 map->m_len = 1;
1569 } else if ((map->m_pblk != NEW_ADDR &&
1570 blkaddr == (map->m_pblk + ofs)) ||
1571 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1572 flag == F2FS_GET_BLOCK_PRE_DIO) {
1573 ofs++;
1574 map->m_len++;
1575 } else {
1576 goto sync_out;
1579 skip:
1580 dn.ofs_in_node++;
1581 pgofs++;
1583 /* preallocate blocks in batch for one dnode page */
1584 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1585 (pgofs == end || dn.ofs_in_node == end_offset)) {
1587 dn.ofs_in_node = ofs_in_node;
1588 err = f2fs_reserve_new_blocks(&dn, prealloc);
1589 if (err)
1590 goto sync_out;
1592 map->m_len += dn.ofs_in_node - ofs_in_node;
1593 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1594 err = -ENOSPC;
1595 goto sync_out;
1597 dn.ofs_in_node = end_offset;
1600 if (pgofs >= end)
1601 goto sync_out;
1602 else if (dn.ofs_in_node < end_offset)
1603 goto next_block;
1605 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1606 if (map->m_flags & F2FS_MAP_MAPPED) {
1607 unsigned int ofs = start_pgofs - map->m_lblk;
1609 f2fs_update_extent_cache_range(&dn,
1610 start_pgofs, map->m_pblk + ofs,
1611 map->m_len - ofs);
1615 f2fs_put_dnode(&dn);
1617 if (map->m_may_create) {
1618 __do_map_lock(sbi, flag, false);
1619 f2fs_balance_fs(sbi, dn.node_changed);
1621 goto next_dnode;
1623 sync_out:
1625 /* for hardware encryption, but to avoid potential issue in future */
1626 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1627 f2fs_wait_on_block_writeback_range(inode,
1628 map->m_pblk, map->m_len);
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,
1636 map->m_len - ofs);
1638 if (map->m_next_extent)
1639 *map->m_next_extent = pgofs + 1;
1641 f2fs_put_dnode(&dn);
1642 unlock_out:
1643 if (map->m_may_create) {
1644 __do_map_lock(sbi, flag, false);
1645 f2fs_balance_fs(sbi, dn.node_changed);
1647 out:
1648 trace_f2fs_map_blocks(inode, map, err);
1649 return err;
1652 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1654 struct f2fs_map_blocks map;
1655 block_t last_lblk;
1656 int err;
1658 if (pos + len > i_size_read(inode))
1659 return false;
1661 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1662 map.m_next_pgofs = NULL;
1663 map.m_next_extent = NULL;
1664 map.m_seg_type = NO_CHECK_TYPE;
1665 map.m_may_create = false;
1666 last_lblk = F2FS_BLK_ALIGN(pos + len);
1668 while (map.m_lblk < last_lblk) {
1669 map.m_len = last_lblk - map.m_lblk;
1670 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1671 if (err || map.m_len == 0)
1672 return false;
1673 map.m_lblk += map.m_len;
1675 return true;
1678 static int __get_data_block(struct inode *inode, sector_t iblock,
1679 struct buffer_head *bh, int create, int flag,
1680 pgoff_t *next_pgofs, int seg_type, bool may_write)
1682 struct f2fs_map_blocks map;
1683 int err;
1685 map.m_lblk = iblock;
1686 map.m_len = bh->b_size >> inode->i_blkbits;
1687 map.m_next_pgofs = next_pgofs;
1688 map.m_next_extent = NULL;
1689 map.m_seg_type = seg_type;
1690 map.m_may_create = may_write;
1692 err = f2fs_map_blocks(inode, &map, create, flag);
1693 if (!err) {
1694 map_bh(bh, inode->i_sb, map.m_pblk);
1695 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1696 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1698 return err;
1701 static int get_data_block(struct inode *inode, sector_t iblock,
1702 struct buffer_head *bh_result, int create, int flag,
1703 pgoff_t *next_pgofs)
1705 return __get_data_block(inode, iblock, bh_result, create,
1706 flag, next_pgofs,
1707 NO_CHECK_TYPE, create);
1710 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1711 struct buffer_head *bh_result, int create)
1713 return __get_data_block(inode, iblock, bh_result, create,
1714 F2FS_GET_BLOCK_DIO, NULL,
1715 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1716 IS_SWAPFILE(inode) ? false : true);
1719 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1720 struct buffer_head *bh_result, int create)
1722 return __get_data_block(inode, iblock, bh_result, create,
1723 F2FS_GET_BLOCK_DIO, NULL,
1724 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1725 false);
1728 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1729 struct buffer_head *bh_result, int create)
1731 /* Block number less than F2FS MAX BLOCKS */
1732 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1733 return -EFBIG;
1735 return __get_data_block(inode, iblock, bh_result, create,
1736 F2FS_GET_BLOCK_BMAP, NULL,
1737 NO_CHECK_TYPE, create);
1740 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1742 return (offset >> inode->i_blkbits);
1745 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1747 return (blk << inode->i_blkbits);
1750 static int f2fs_xattr_fiemap(struct inode *inode,
1751 struct fiemap_extent_info *fieinfo)
1753 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1754 struct page *page;
1755 struct node_info ni;
1756 __u64 phys = 0, len;
1757 __u32 flags;
1758 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1759 int err = 0;
1761 if (f2fs_has_inline_xattr(inode)) {
1762 int offset;
1764 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1765 inode->i_ino, false);
1766 if (!page)
1767 return -ENOMEM;
1769 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1770 if (err) {
1771 f2fs_put_page(page, 1);
1772 return err;
1775 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1776 offset = offsetof(struct f2fs_inode, i_addr) +
1777 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1778 get_inline_xattr_addrs(inode));
1780 phys += offset;
1781 len = inline_xattr_size(inode);
1783 f2fs_put_page(page, 1);
1785 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1787 if (!xnid)
1788 flags |= FIEMAP_EXTENT_LAST;
1790 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1791 if (err || err == 1)
1792 return err;
1795 if (xnid) {
1796 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1797 if (!page)
1798 return -ENOMEM;
1800 err = f2fs_get_node_info(sbi, xnid, &ni);
1801 if (err) {
1802 f2fs_put_page(page, 1);
1803 return err;
1806 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1807 len = inode->i_sb->s_blocksize;
1809 f2fs_put_page(page, 1);
1811 flags = FIEMAP_EXTENT_LAST;
1814 if (phys)
1815 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1817 return (err < 0 ? err : 0);
1820 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1821 u64 start, u64 len)
1823 struct buffer_head map_bh;
1824 sector_t start_blk, last_blk;
1825 pgoff_t next_pgofs;
1826 u64 logical = 0, phys = 0, size = 0;
1827 u32 flags = 0;
1828 int ret = 0;
1830 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1831 ret = f2fs_precache_extents(inode);
1832 if (ret)
1833 return ret;
1836 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1837 if (ret)
1838 return ret;
1840 inode_lock(inode);
1842 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1843 ret = f2fs_xattr_fiemap(inode, fieinfo);
1844 goto out;
1847 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1848 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1849 if (ret != -EAGAIN)
1850 goto out;
1853 if (logical_to_blk(inode, len) == 0)
1854 len = blk_to_logical(inode, 1);
1856 start_blk = logical_to_blk(inode, start);
1857 last_blk = logical_to_blk(inode, start + len - 1);
1859 next:
1860 memset(&map_bh, 0, sizeof(struct buffer_head));
1861 map_bh.b_size = len;
1863 ret = get_data_block(inode, start_blk, &map_bh, 0,
1864 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1865 if (ret)
1866 goto out;
1868 /* HOLE */
1869 if (!buffer_mapped(&map_bh)) {
1870 start_blk = next_pgofs;
1872 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1873 F2FS_I_SB(inode)->max_file_blocks))
1874 goto prep_next;
1876 flags |= FIEMAP_EXTENT_LAST;
1879 if (size) {
1880 if (IS_ENCRYPTED(inode))
1881 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1883 ret = fiemap_fill_next_extent(fieinfo, logical,
1884 phys, size, flags);
1887 if (start_blk > last_blk || ret)
1888 goto out;
1890 logical = blk_to_logical(inode, start_blk);
1891 phys = blk_to_logical(inode, map_bh.b_blocknr);
1892 size = map_bh.b_size;
1893 flags = 0;
1894 if (buffer_unwritten(&map_bh))
1895 flags = FIEMAP_EXTENT_UNWRITTEN;
1897 start_blk += logical_to_blk(inode, size);
1899 prep_next:
1900 cond_resched();
1901 if (fatal_signal_pending(current))
1902 ret = -EINTR;
1903 else
1904 goto next;
1905 out:
1906 if (ret == 1)
1907 ret = 0;
1909 inode_unlock(inode);
1910 return ret;
1913 static inline loff_t f2fs_readpage_limit(struct inode *inode)
1915 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1916 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1917 return inode->i_sb->s_maxbytes;
1919 return i_size_read(inode);
1922 static int f2fs_read_single_page(struct inode *inode, struct page *page,
1923 unsigned nr_pages,
1924 struct f2fs_map_blocks *map,
1925 struct bio **bio_ret,
1926 sector_t *last_block_in_bio,
1927 bool is_readahead)
1929 struct bio *bio = *bio_ret;
1930 const unsigned blkbits = inode->i_blkbits;
1931 const unsigned blocksize = 1 << blkbits;
1932 sector_t block_in_file;
1933 sector_t last_block;
1934 sector_t last_block_in_file;
1935 sector_t block_nr;
1936 int ret = 0;
1938 block_in_file = (sector_t)page_index(page);
1939 last_block = block_in_file + nr_pages;
1940 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
1941 blkbits;
1942 if (last_block > last_block_in_file)
1943 last_block = last_block_in_file;
1945 /* just zeroing out page which is beyond EOF */
1946 if (block_in_file >= last_block)
1947 goto zero_out;
1949 * Map blocks using the previous result first.
1951 if ((map->m_flags & F2FS_MAP_MAPPED) &&
1952 block_in_file > map->m_lblk &&
1953 block_in_file < (map->m_lblk + map->m_len))
1954 goto got_it;
1957 * Then do more f2fs_map_blocks() calls until we are
1958 * done with this page.
1960 map->m_lblk = block_in_file;
1961 map->m_len = last_block - block_in_file;
1963 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
1964 if (ret)
1965 goto out;
1966 got_it:
1967 if ((map->m_flags & F2FS_MAP_MAPPED)) {
1968 block_nr = map->m_pblk + block_in_file - map->m_lblk;
1969 SetPageMappedToDisk(page);
1971 if (!PageUptodate(page) && (!PageSwapCache(page) &&
1972 !cleancache_get_page(page))) {
1973 SetPageUptodate(page);
1974 goto confused;
1977 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1978 DATA_GENERIC_ENHANCE_READ)) {
1979 ret = -EFSCORRUPTED;
1980 goto out;
1982 } else {
1983 zero_out:
1984 zero_user_segment(page, 0, PAGE_SIZE);
1985 if (f2fs_need_verity(inode, page->index) &&
1986 !fsverity_verify_page(page)) {
1987 ret = -EIO;
1988 goto out;
1990 if (!PageUptodate(page))
1991 SetPageUptodate(page);
1992 unlock_page(page);
1993 goto out;
1997 * This page will go to BIO. Do we need to send this
1998 * BIO off first?
2000 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio,
2001 *last_block_in_bio, block_nr)) {
2002 submit_and_realloc:
2003 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2004 bio = NULL;
2006 if (bio == NULL) {
2007 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2008 is_readahead ? REQ_RAHEAD : 0, page->index);
2009 if (IS_ERR(bio)) {
2010 ret = PTR_ERR(bio);
2011 bio = NULL;
2012 goto out;
2017 * If the page is under writeback, we need to wait for
2018 * its completion to see the correct decrypted data.
2020 f2fs_wait_on_block_writeback(inode, block_nr);
2022 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2023 goto submit_and_realloc;
2025 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2026 ClearPageError(page);
2027 *last_block_in_bio = block_nr;
2028 goto out;
2029 confused:
2030 if (bio) {
2031 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2032 bio = NULL;
2034 unlock_page(page);
2035 out:
2036 *bio_ret = bio;
2037 return ret;
2040 #ifdef CONFIG_F2FS_FS_COMPRESSION
2041 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2042 unsigned nr_pages, sector_t *last_block_in_bio,
2043 bool is_readahead)
2045 struct dnode_of_data dn;
2046 struct inode *inode = cc->inode;
2047 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2048 struct bio *bio = *bio_ret;
2049 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2050 sector_t last_block_in_file;
2051 const unsigned blkbits = inode->i_blkbits;
2052 const unsigned blocksize = 1 << blkbits;
2053 struct decompress_io_ctx *dic = NULL;
2054 int i;
2055 int ret = 0;
2057 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2059 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
2061 /* get rid of pages beyond EOF */
2062 for (i = 0; i < cc->cluster_size; i++) {
2063 struct page *page = cc->rpages[i];
2065 if (!page)
2066 continue;
2067 if ((sector_t)page->index >= last_block_in_file) {
2068 zero_user_segment(page, 0, PAGE_SIZE);
2069 if (!PageUptodate(page))
2070 SetPageUptodate(page);
2071 } else if (!PageUptodate(page)) {
2072 continue;
2074 unlock_page(page);
2075 cc->rpages[i] = NULL;
2076 cc->nr_rpages--;
2079 /* we are done since all pages are beyond EOF */
2080 if (f2fs_cluster_is_empty(cc))
2081 goto out;
2083 set_new_dnode(&dn, inode, NULL, NULL, 0);
2084 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2085 if (ret)
2086 goto out;
2088 /* cluster was overwritten as normal cluster */
2089 if (dn.data_blkaddr != COMPRESS_ADDR)
2090 goto out;
2092 for (i = 1; i < cc->cluster_size; i++) {
2093 block_t blkaddr;
2095 blkaddr = datablock_addr(dn.inode, dn.node_page,
2096 dn.ofs_in_node + i);
2098 if (!__is_valid_data_blkaddr(blkaddr))
2099 break;
2101 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2102 ret = -EFAULT;
2103 goto out_put_dnode;
2105 cc->nr_cpages++;
2108 /* nothing to decompress */
2109 if (cc->nr_cpages == 0) {
2110 ret = 0;
2111 goto out_put_dnode;
2114 dic = f2fs_alloc_dic(cc);
2115 if (IS_ERR(dic)) {
2116 ret = PTR_ERR(dic);
2117 goto out_put_dnode;
2120 for (i = 0; i < dic->nr_cpages; i++) {
2121 struct page *page = dic->cpages[i];
2122 block_t blkaddr;
2124 blkaddr = datablock_addr(dn.inode, dn.node_page,
2125 dn.ofs_in_node + i + 1);
2127 if (bio && !page_is_mergeable(sbi, bio,
2128 *last_block_in_bio, blkaddr)) {
2129 submit_and_realloc:
2130 __submit_bio(sbi, bio, DATA);
2131 bio = NULL;
2134 if (!bio) {
2135 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2136 is_readahead ? REQ_RAHEAD : 0,
2137 page->index);
2138 if (IS_ERR(bio)) {
2139 ret = PTR_ERR(bio);
2140 bio = NULL;
2141 dic->failed = true;
2142 if (refcount_sub_and_test(dic->nr_cpages - i,
2143 &dic->ref))
2144 f2fs_decompress_end_io(dic->rpages,
2145 cc->cluster_size, true,
2146 false);
2147 f2fs_free_dic(dic);
2148 f2fs_put_dnode(&dn);
2149 *bio_ret = bio;
2150 return ret;
2154 f2fs_wait_on_block_writeback(inode, blkaddr);
2156 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2157 goto submit_and_realloc;
2159 inc_page_count(sbi, F2FS_RD_DATA);
2160 ClearPageError(page);
2161 *last_block_in_bio = blkaddr;
2164 f2fs_put_dnode(&dn);
2166 *bio_ret = bio;
2167 return 0;
2169 out_put_dnode:
2170 f2fs_put_dnode(&dn);
2171 out:
2172 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2173 *bio_ret = bio;
2174 return ret;
2176 #endif
2179 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2180 * Major change was from block_size == page_size in f2fs by default.
2182 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2183 * this function ever deviates from doing just read-ahead, it should either
2184 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2185 * from read-ahead.
2187 int f2fs_mpage_readpages(struct address_space *mapping,
2188 struct list_head *pages, struct page *page,
2189 unsigned nr_pages, bool is_readahead)
2191 struct bio *bio = NULL;
2192 sector_t last_block_in_bio = 0;
2193 struct inode *inode = mapping->host;
2194 struct f2fs_map_blocks map;
2195 #ifdef CONFIG_F2FS_FS_COMPRESSION
2196 struct compress_ctx cc = {
2197 .inode = inode,
2198 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2199 .cluster_size = F2FS_I(inode)->i_cluster_size,
2200 .cluster_idx = NULL_CLUSTER,
2201 .rpages = NULL,
2202 .cpages = NULL,
2203 .nr_rpages = 0,
2204 .nr_cpages = 0,
2206 #endif
2207 unsigned max_nr_pages = nr_pages;
2208 int ret = 0;
2210 map.m_pblk = 0;
2211 map.m_lblk = 0;
2212 map.m_len = 0;
2213 map.m_flags = 0;
2214 map.m_next_pgofs = NULL;
2215 map.m_next_extent = NULL;
2216 map.m_seg_type = NO_CHECK_TYPE;
2217 map.m_may_create = false;
2219 for (; nr_pages; nr_pages--) {
2220 if (pages) {
2221 page = list_last_entry(pages, struct page, lru);
2223 prefetchw(&page->flags);
2224 list_del(&page->lru);
2225 if (add_to_page_cache_lru(page, mapping,
2226 page_index(page),
2227 readahead_gfp_mask(mapping)))
2228 goto next_page;
2231 #ifdef CONFIG_F2FS_FS_COMPRESSION
2232 if (f2fs_compressed_file(inode)) {
2233 /* there are remained comressed pages, submit them */
2234 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2235 ret = f2fs_read_multi_pages(&cc, &bio,
2236 max_nr_pages,
2237 &last_block_in_bio,
2238 is_readahead);
2239 f2fs_destroy_compress_ctx(&cc);
2240 if (ret)
2241 goto set_error_page;
2243 ret = f2fs_is_compressed_cluster(inode, page->index);
2244 if (ret < 0)
2245 goto set_error_page;
2246 else if (!ret)
2247 goto read_single_page;
2249 ret = f2fs_init_compress_ctx(&cc);
2250 if (ret)
2251 goto set_error_page;
2253 f2fs_compress_ctx_add_page(&cc, page);
2255 goto next_page;
2257 read_single_page:
2258 #endif
2260 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2261 &bio, &last_block_in_bio, is_readahead);
2262 if (ret) {
2263 #ifdef CONFIG_F2FS_FS_COMPRESSION
2264 set_error_page:
2265 #endif
2266 SetPageError(page);
2267 zero_user_segment(page, 0, PAGE_SIZE);
2268 unlock_page(page);
2270 next_page:
2271 if (pages)
2272 put_page(page);
2274 #ifdef CONFIG_F2FS_FS_COMPRESSION
2275 if (f2fs_compressed_file(inode)) {
2276 /* last page */
2277 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2278 ret = f2fs_read_multi_pages(&cc, &bio,
2279 max_nr_pages,
2280 &last_block_in_bio,
2281 is_readahead);
2282 f2fs_destroy_compress_ctx(&cc);
2285 #endif
2287 BUG_ON(pages && !list_empty(pages));
2288 if (bio)
2289 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2290 return pages ? 0 : ret;
2293 static int f2fs_read_data_page(struct file *file, struct page *page)
2295 struct inode *inode = page_file_mapping(page)->host;
2296 int ret = -EAGAIN;
2298 trace_f2fs_readpage(page, DATA);
2300 if (!f2fs_is_compress_backend_ready(inode)) {
2301 unlock_page(page);
2302 return -EOPNOTSUPP;
2305 /* If the file has inline data, try to read it directly */
2306 if (f2fs_has_inline_data(inode))
2307 ret = f2fs_read_inline_data(inode, page);
2308 if (ret == -EAGAIN)
2309 ret = f2fs_mpage_readpages(page_file_mapping(page),
2310 NULL, page, 1, false);
2311 return ret;
2314 static int f2fs_read_data_pages(struct file *file,
2315 struct address_space *mapping,
2316 struct list_head *pages, unsigned nr_pages)
2318 struct inode *inode = mapping->host;
2319 struct page *page = list_last_entry(pages, struct page, lru);
2321 trace_f2fs_readpages(inode, page, nr_pages);
2323 if (!f2fs_is_compress_backend_ready(inode))
2324 return 0;
2326 /* If the file has inline data, skip readpages */
2327 if (f2fs_has_inline_data(inode))
2328 return 0;
2330 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
2333 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2335 struct inode *inode = fio->page->mapping->host;
2336 struct page *mpage, *page;
2337 gfp_t gfp_flags = GFP_NOFS;
2339 if (!f2fs_encrypted_file(inode))
2340 return 0;
2342 page = fio->compressed_page ? fio->compressed_page : fio->page;
2344 /* wait for GCed page writeback via META_MAPPING */
2345 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2347 retry_encrypt:
2348 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2349 PAGE_SIZE, 0, gfp_flags);
2350 if (IS_ERR(fio->encrypted_page)) {
2351 /* flush pending IOs and wait for a while in the ENOMEM case */
2352 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2353 f2fs_flush_merged_writes(fio->sbi);
2354 congestion_wait(BLK_RW_ASYNC, HZ/50);
2355 gfp_flags |= __GFP_NOFAIL;
2356 goto retry_encrypt;
2358 return PTR_ERR(fio->encrypted_page);
2361 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2362 if (mpage) {
2363 if (PageUptodate(mpage))
2364 memcpy(page_address(mpage),
2365 page_address(fio->encrypted_page), PAGE_SIZE);
2366 f2fs_put_page(mpage, 1);
2368 return 0;
2371 static inline bool check_inplace_update_policy(struct inode *inode,
2372 struct f2fs_io_info *fio)
2374 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2375 unsigned int policy = SM_I(sbi)->ipu_policy;
2377 if (policy & (0x1 << F2FS_IPU_FORCE))
2378 return true;
2379 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2380 return true;
2381 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2382 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2383 return true;
2384 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2385 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2386 return true;
2389 * IPU for rewrite async pages
2391 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2392 fio && fio->op == REQ_OP_WRITE &&
2393 !(fio->op_flags & REQ_SYNC) &&
2394 !IS_ENCRYPTED(inode))
2395 return true;
2397 /* this is only set during fdatasync */
2398 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2399 is_inode_flag_set(inode, FI_NEED_IPU))
2400 return true;
2402 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2403 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2404 return true;
2406 return false;
2409 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2411 if (f2fs_is_pinned_file(inode))
2412 return true;
2414 /* if this is cold file, we should overwrite to avoid fragmentation */
2415 if (file_is_cold(inode))
2416 return true;
2418 return check_inplace_update_policy(inode, fio);
2421 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2423 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2425 if (test_opt(sbi, LFS))
2426 return true;
2427 if (S_ISDIR(inode->i_mode))
2428 return true;
2429 if (IS_NOQUOTA(inode))
2430 return true;
2431 if (f2fs_is_atomic_file(inode))
2432 return true;
2433 if (fio) {
2434 if (is_cold_data(fio->page))
2435 return true;
2436 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2437 return true;
2438 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2439 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2440 return true;
2442 return false;
2445 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2447 struct inode *inode = fio->page->mapping->host;
2449 if (f2fs_should_update_outplace(inode, fio))
2450 return false;
2452 return f2fs_should_update_inplace(inode, fio);
2455 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2457 struct page *page = fio->page;
2458 struct inode *inode = page->mapping->host;
2459 struct dnode_of_data dn;
2460 struct extent_info ei = {0,0,0};
2461 struct node_info ni;
2462 bool ipu_force = false;
2463 int err = 0;
2465 set_new_dnode(&dn, inode, NULL, NULL, 0);
2466 if (need_inplace_update(fio) &&
2467 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2468 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2470 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2471 DATA_GENERIC_ENHANCE))
2472 return -EFSCORRUPTED;
2474 ipu_force = true;
2475 fio->need_lock = LOCK_DONE;
2476 goto got_it;
2479 /* Deadlock due to between page->lock and f2fs_lock_op */
2480 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2481 return -EAGAIN;
2483 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2484 if (err)
2485 goto out;
2487 fio->old_blkaddr = dn.data_blkaddr;
2489 /* This page is already truncated */
2490 if (fio->old_blkaddr == NULL_ADDR) {
2491 ClearPageUptodate(page);
2492 clear_cold_data(page);
2493 goto out_writepage;
2495 got_it:
2496 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2497 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2498 DATA_GENERIC_ENHANCE)) {
2499 err = -EFSCORRUPTED;
2500 goto out_writepage;
2503 * If current allocation needs SSR,
2504 * it had better in-place writes for updated data.
2506 if (ipu_force ||
2507 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2508 need_inplace_update(fio))) {
2509 err = f2fs_encrypt_one_page(fio);
2510 if (err)
2511 goto out_writepage;
2513 set_page_writeback(page);
2514 ClearPageError(page);
2515 f2fs_put_dnode(&dn);
2516 if (fio->need_lock == LOCK_REQ)
2517 f2fs_unlock_op(fio->sbi);
2518 err = f2fs_inplace_write_data(fio);
2519 if (err) {
2520 if (f2fs_encrypted_file(inode))
2521 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2522 if (PageWriteback(page))
2523 end_page_writeback(page);
2524 } else {
2525 set_inode_flag(inode, FI_UPDATE_WRITE);
2527 trace_f2fs_do_write_data_page(fio->page, IPU);
2528 return err;
2531 if (fio->need_lock == LOCK_RETRY) {
2532 if (!f2fs_trylock_op(fio->sbi)) {
2533 err = -EAGAIN;
2534 goto out_writepage;
2536 fio->need_lock = LOCK_REQ;
2539 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2540 if (err)
2541 goto out_writepage;
2543 fio->version = ni.version;
2545 err = f2fs_encrypt_one_page(fio);
2546 if (err)
2547 goto out_writepage;
2549 set_page_writeback(page);
2550 ClearPageError(page);
2552 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2553 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2555 /* LFS mode write path */
2556 f2fs_outplace_write_data(&dn, fio);
2557 trace_f2fs_do_write_data_page(page, OPU);
2558 set_inode_flag(inode, FI_APPEND_WRITE);
2559 if (page->index == 0)
2560 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2561 out_writepage:
2562 f2fs_put_dnode(&dn);
2563 out:
2564 if (fio->need_lock == LOCK_REQ)
2565 f2fs_unlock_op(fio->sbi);
2566 return err;
2569 int f2fs_write_single_data_page(struct page *page, int *submitted,
2570 struct bio **bio,
2571 sector_t *last_block,
2572 struct writeback_control *wbc,
2573 enum iostat_type io_type,
2574 int compr_blocks)
2576 struct inode *inode = page->mapping->host;
2577 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2578 loff_t i_size = i_size_read(inode);
2579 const pgoff_t end_index = ((unsigned long long)i_size)
2580 >> PAGE_SHIFT;
2581 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2582 unsigned offset = 0;
2583 bool need_balance_fs = false;
2584 int err = 0;
2585 struct f2fs_io_info fio = {
2586 .sbi = sbi,
2587 .ino = inode->i_ino,
2588 .type = DATA,
2589 .op = REQ_OP_WRITE,
2590 .op_flags = wbc_to_write_flags(wbc),
2591 .old_blkaddr = NULL_ADDR,
2592 .page = page,
2593 .encrypted_page = NULL,
2594 .submitted = false,
2595 .compr_blocks = compr_blocks,
2596 .need_lock = LOCK_RETRY,
2597 .io_type = io_type,
2598 .io_wbc = wbc,
2599 .bio = bio,
2600 .last_block = last_block,
2603 trace_f2fs_writepage(page, DATA);
2605 /* we should bypass data pages to proceed the kworkder jobs */
2606 if (unlikely(f2fs_cp_error(sbi))) {
2607 mapping_set_error(page->mapping, -EIO);
2609 * don't drop any dirty dentry pages for keeping lastest
2610 * directory structure.
2612 if (S_ISDIR(inode->i_mode))
2613 goto redirty_out;
2614 goto out;
2617 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2618 goto redirty_out;
2620 if (page->index < end_index ||
2621 f2fs_verity_in_progress(inode) ||
2622 compr_blocks)
2623 goto write;
2626 * If the offset is out-of-range of file size,
2627 * this page does not have to be written to disk.
2629 offset = i_size & (PAGE_SIZE - 1);
2630 if ((page->index >= end_index + 1) || !offset)
2631 goto out;
2633 zero_user_segment(page, offset, PAGE_SIZE);
2634 write:
2635 if (f2fs_is_drop_cache(inode))
2636 goto out;
2637 /* we should not write 0'th page having journal header */
2638 if (f2fs_is_volatile_file(inode) && (!page->index ||
2639 (!wbc->for_reclaim &&
2640 f2fs_available_free_memory(sbi, BASE_CHECK))))
2641 goto redirty_out;
2643 /* Dentry blocks are controlled by checkpoint */
2644 if (S_ISDIR(inode->i_mode)) {
2645 fio.need_lock = LOCK_DONE;
2646 err = f2fs_do_write_data_page(&fio);
2647 goto done;
2650 if (!wbc->for_reclaim)
2651 need_balance_fs = true;
2652 else if (has_not_enough_free_secs(sbi, 0, 0))
2653 goto redirty_out;
2654 else
2655 set_inode_flag(inode, FI_HOT_DATA);
2657 err = -EAGAIN;
2658 if (f2fs_has_inline_data(inode)) {
2659 err = f2fs_write_inline_data(inode, page);
2660 if (!err)
2661 goto out;
2664 if (err == -EAGAIN) {
2665 err = f2fs_do_write_data_page(&fio);
2666 if (err == -EAGAIN) {
2667 fio.need_lock = LOCK_REQ;
2668 err = f2fs_do_write_data_page(&fio);
2672 if (err) {
2673 file_set_keep_isize(inode);
2674 } else {
2675 down_write(&F2FS_I(inode)->i_sem);
2676 if (F2FS_I(inode)->last_disk_size < psize)
2677 F2FS_I(inode)->last_disk_size = psize;
2678 up_write(&F2FS_I(inode)->i_sem);
2681 done:
2682 if (err && err != -ENOENT)
2683 goto redirty_out;
2685 out:
2686 inode_dec_dirty_pages(inode);
2687 if (err) {
2688 ClearPageUptodate(page);
2689 clear_cold_data(page);
2692 if (wbc->for_reclaim) {
2693 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2694 clear_inode_flag(inode, FI_HOT_DATA);
2695 f2fs_remove_dirty_inode(inode);
2696 submitted = NULL;
2698 unlock_page(page);
2699 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2700 !F2FS_I(inode)->cp_task)
2701 f2fs_balance_fs(sbi, need_balance_fs);
2703 if (unlikely(f2fs_cp_error(sbi))) {
2704 f2fs_submit_merged_write(sbi, DATA);
2705 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2706 submitted = NULL;
2709 if (submitted)
2710 *submitted = fio.submitted ? 1 : 0;
2712 return 0;
2714 redirty_out:
2715 redirty_page_for_writepage(wbc, page);
2717 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2718 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2719 * file_write_and_wait_range() will see EIO error, which is critical
2720 * to return value of fsync() followed by atomic_write failure to user.
2722 if (!err || wbc->for_reclaim)
2723 return AOP_WRITEPAGE_ACTIVATE;
2724 unlock_page(page);
2725 return err;
2728 static int f2fs_write_data_page(struct page *page,
2729 struct writeback_control *wbc)
2731 #ifdef CONFIG_F2FS_FS_COMPRESSION
2732 struct inode *inode = page->mapping->host;
2734 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2735 goto out;
2737 if (f2fs_compressed_file(inode)) {
2738 if (f2fs_is_compressed_cluster(inode, page->index)) {
2739 redirty_page_for_writepage(wbc, page);
2740 return AOP_WRITEPAGE_ACTIVATE;
2743 out:
2744 #endif
2746 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2747 wbc, FS_DATA_IO, 0);
2751 * This function was copied from write_cche_pages from mm/page-writeback.c.
2752 * The major change is making write step of cold data page separately from
2753 * warm/hot data page.
2755 static int f2fs_write_cache_pages(struct address_space *mapping,
2756 struct writeback_control *wbc,
2757 enum iostat_type io_type)
2759 int ret = 0;
2760 int done = 0, retry = 0;
2761 struct pagevec pvec;
2762 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2763 struct bio *bio = NULL;
2764 sector_t last_block;
2765 #ifdef CONFIG_F2FS_FS_COMPRESSION
2766 struct inode *inode = mapping->host;
2767 struct compress_ctx cc = {
2768 .inode = inode,
2769 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2770 .cluster_size = F2FS_I(inode)->i_cluster_size,
2771 .cluster_idx = NULL_CLUSTER,
2772 .rpages = NULL,
2773 .nr_rpages = 0,
2774 .cpages = NULL,
2775 .rbuf = NULL,
2776 .cbuf = NULL,
2777 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2778 .private = NULL,
2780 #endif
2781 int nr_pages;
2782 pgoff_t uninitialized_var(writeback_index);
2783 pgoff_t index;
2784 pgoff_t end; /* Inclusive */
2785 pgoff_t done_index;
2786 int cycled;
2787 int range_whole = 0;
2788 xa_mark_t tag;
2789 int nwritten = 0;
2790 int submitted = 0;
2791 int i;
2793 pagevec_init(&pvec);
2795 if (get_dirty_pages(mapping->host) <=
2796 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2797 set_inode_flag(mapping->host, FI_HOT_DATA);
2798 else
2799 clear_inode_flag(mapping->host, FI_HOT_DATA);
2801 if (wbc->range_cyclic) {
2802 writeback_index = mapping->writeback_index; /* prev offset */
2803 index = writeback_index;
2804 if (index == 0)
2805 cycled = 1;
2806 else
2807 cycled = 0;
2808 end = -1;
2809 } else {
2810 index = wbc->range_start >> PAGE_SHIFT;
2811 end = wbc->range_end >> PAGE_SHIFT;
2812 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2813 range_whole = 1;
2814 cycled = 1; /* ignore range_cyclic tests */
2816 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2817 tag = PAGECACHE_TAG_TOWRITE;
2818 else
2819 tag = PAGECACHE_TAG_DIRTY;
2820 retry:
2821 retry = 0;
2822 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2823 tag_pages_for_writeback(mapping, index, end);
2824 done_index = index;
2825 while (!done && !retry && (index <= end)) {
2826 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2827 tag);
2828 if (nr_pages == 0)
2829 break;
2831 for (i = 0; i < nr_pages; i++) {
2832 struct page *page = pvec.pages[i];
2833 bool need_readd;
2834 readd:
2835 need_readd = false;
2836 #ifdef CONFIG_F2FS_FS_COMPRESSION
2837 if (f2fs_compressed_file(inode)) {
2838 ret = f2fs_init_compress_ctx(&cc);
2839 if (ret) {
2840 done = 1;
2841 break;
2844 if (!f2fs_cluster_can_merge_page(&cc,
2845 page->index)) {
2846 ret = f2fs_write_multi_pages(&cc,
2847 &submitted, wbc, io_type);
2848 if (!ret)
2849 need_readd = true;
2850 goto result;
2853 if (unlikely(f2fs_cp_error(sbi)))
2854 goto lock_page;
2856 if (f2fs_cluster_is_empty(&cc)) {
2857 void *fsdata = NULL;
2858 struct page *pagep;
2859 int ret2;
2861 ret2 = f2fs_prepare_compress_overwrite(
2862 inode, &pagep,
2863 page->index, &fsdata);
2864 if (ret2 < 0) {
2865 ret = ret2;
2866 done = 1;
2867 break;
2868 } else if (ret2 &&
2869 !f2fs_compress_write_end(inode,
2870 fsdata, page->index,
2871 1)) {
2872 retry = 1;
2873 break;
2875 } else {
2876 goto lock_page;
2879 #endif
2880 /* give a priority to WB_SYNC threads */
2881 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2882 wbc->sync_mode == WB_SYNC_NONE) {
2883 done = 1;
2884 break;
2886 #ifdef CONFIG_F2FS_FS_COMPRESSION
2887 lock_page:
2888 #endif
2889 done_index = page->index;
2890 retry_write:
2891 lock_page(page);
2893 if (unlikely(page->mapping != mapping)) {
2894 continue_unlock:
2895 unlock_page(page);
2896 continue;
2899 if (!PageDirty(page)) {
2900 /* someone wrote it for us */
2901 goto continue_unlock;
2904 if (PageWriteback(page)) {
2905 if (wbc->sync_mode != WB_SYNC_NONE)
2906 f2fs_wait_on_page_writeback(page,
2907 DATA, true, true);
2908 else
2909 goto continue_unlock;
2912 if (!clear_page_dirty_for_io(page))
2913 goto continue_unlock;
2915 #ifdef CONFIG_F2FS_FS_COMPRESSION
2916 if (f2fs_compressed_file(inode)) {
2917 get_page(page);
2918 f2fs_compress_ctx_add_page(&cc, page);
2919 continue;
2921 #endif
2922 ret = f2fs_write_single_data_page(page, &submitted,
2923 &bio, &last_block, wbc, io_type, 0);
2924 if (ret == AOP_WRITEPAGE_ACTIVATE)
2925 unlock_page(page);
2926 #ifdef CONFIG_F2FS_FS_COMPRESSION
2927 result:
2928 #endif
2929 nwritten += submitted;
2930 wbc->nr_to_write -= submitted;
2932 if (unlikely(ret)) {
2934 * keep nr_to_write, since vfs uses this to
2935 * get # of written pages.
2937 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2938 ret = 0;
2939 goto next;
2940 } else if (ret == -EAGAIN) {
2941 ret = 0;
2942 if (wbc->sync_mode == WB_SYNC_ALL) {
2943 cond_resched();
2944 congestion_wait(BLK_RW_ASYNC,
2945 HZ/50);
2946 goto retry_write;
2948 goto next;
2950 done_index = page->index + 1;
2951 done = 1;
2952 break;
2955 if (wbc->nr_to_write <= 0 &&
2956 wbc->sync_mode == WB_SYNC_NONE) {
2957 done = 1;
2958 break;
2960 next:
2961 if (need_readd)
2962 goto readd;
2964 pagevec_release(&pvec);
2965 cond_resched();
2967 #ifdef CONFIG_F2FS_FS_COMPRESSION
2968 /* flush remained pages in compress cluster */
2969 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
2970 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
2971 nwritten += submitted;
2972 wbc->nr_to_write -= submitted;
2973 if (ret) {
2974 done = 1;
2975 retry = 0;
2978 #endif
2979 if ((!cycled && !done) || retry) {
2980 cycled = 1;
2981 index = 0;
2982 end = writeback_index - 1;
2983 goto retry;
2985 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2986 mapping->writeback_index = done_index;
2988 if (nwritten)
2989 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2990 NULL, 0, DATA);
2991 /* submit cached bio of IPU write */
2992 if (bio)
2993 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
2995 return ret;
2998 static inline bool __should_serialize_io(struct inode *inode,
2999 struct writeback_control *wbc)
3001 if (!S_ISREG(inode->i_mode))
3002 return false;
3003 if (f2fs_compressed_file(inode))
3004 return true;
3005 if (IS_NOQUOTA(inode))
3006 return false;
3007 /* to avoid deadlock in path of data flush */
3008 if (F2FS_I(inode)->cp_task)
3009 return false;
3010 if (wbc->sync_mode != WB_SYNC_ALL)
3011 return true;
3012 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3013 return true;
3014 return false;
3017 static int __f2fs_write_data_pages(struct address_space *mapping,
3018 struct writeback_control *wbc,
3019 enum iostat_type io_type)
3021 struct inode *inode = mapping->host;
3022 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3023 struct blk_plug plug;
3024 int ret;
3025 bool locked = false;
3027 /* deal with chardevs and other special file */
3028 if (!mapping->a_ops->writepage)
3029 return 0;
3031 /* skip writing if there is no dirty page in this inode */
3032 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3033 return 0;
3035 /* during POR, we don't need to trigger writepage at all. */
3036 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3037 goto skip_write;
3039 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3040 wbc->sync_mode == WB_SYNC_NONE &&
3041 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3042 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3043 goto skip_write;
3045 /* skip writing during file defragment */
3046 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3047 goto skip_write;
3049 trace_f2fs_writepages(mapping->host, wbc, DATA);
3051 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3052 if (wbc->sync_mode == WB_SYNC_ALL)
3053 atomic_inc(&sbi->wb_sync_req[DATA]);
3054 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3055 goto skip_write;
3057 if (__should_serialize_io(inode, wbc)) {
3058 mutex_lock(&sbi->writepages);
3059 locked = true;
3062 blk_start_plug(&plug);
3063 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3064 blk_finish_plug(&plug);
3066 if (locked)
3067 mutex_unlock(&sbi->writepages);
3069 if (wbc->sync_mode == WB_SYNC_ALL)
3070 atomic_dec(&sbi->wb_sync_req[DATA]);
3072 * if some pages were truncated, we cannot guarantee its mapping->host
3073 * to detect pending bios.
3076 f2fs_remove_dirty_inode(inode);
3077 return ret;
3079 skip_write:
3080 wbc->pages_skipped += get_dirty_pages(inode);
3081 trace_f2fs_writepages(mapping->host, wbc, DATA);
3082 return 0;
3085 static int f2fs_write_data_pages(struct address_space *mapping,
3086 struct writeback_control *wbc)
3088 struct inode *inode = mapping->host;
3090 return __f2fs_write_data_pages(mapping, wbc,
3091 F2FS_I(inode)->cp_task == current ?
3092 FS_CP_DATA_IO : FS_DATA_IO);
3095 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3097 struct inode *inode = mapping->host;
3098 loff_t i_size = i_size_read(inode);
3100 if (IS_NOQUOTA(inode))
3101 return;
3103 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3104 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3105 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3106 down_write(&F2FS_I(inode)->i_mmap_sem);
3108 truncate_pagecache(inode, i_size);
3109 f2fs_truncate_blocks(inode, i_size, true);
3111 up_write(&F2FS_I(inode)->i_mmap_sem);
3112 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3116 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3117 struct page *page, loff_t pos, unsigned len,
3118 block_t *blk_addr, bool *node_changed)
3120 struct inode *inode = page->mapping->host;
3121 pgoff_t index = page->index;
3122 struct dnode_of_data dn;
3123 struct page *ipage;
3124 bool locked = false;
3125 struct extent_info ei = {0,0,0};
3126 int err = 0;
3127 int flag;
3130 * we already allocated all the blocks, so we don't need to get
3131 * the block addresses when there is no need to fill the page.
3133 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3134 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3135 !f2fs_verity_in_progress(inode))
3136 return 0;
3138 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3139 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3140 flag = F2FS_GET_BLOCK_DEFAULT;
3141 else
3142 flag = F2FS_GET_BLOCK_PRE_AIO;
3144 if (f2fs_has_inline_data(inode) ||
3145 (pos & PAGE_MASK) >= i_size_read(inode)) {
3146 __do_map_lock(sbi, flag, true);
3147 locked = true;
3150 restart:
3151 /* check inline_data */
3152 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3153 if (IS_ERR(ipage)) {
3154 err = PTR_ERR(ipage);
3155 goto unlock_out;
3158 set_new_dnode(&dn, inode, ipage, ipage, 0);
3160 if (f2fs_has_inline_data(inode)) {
3161 if (pos + len <= MAX_INLINE_DATA(inode)) {
3162 f2fs_do_read_inline_data(page, ipage);
3163 set_inode_flag(inode, FI_DATA_EXIST);
3164 if (inode->i_nlink)
3165 set_inline_node(ipage);
3166 } else {
3167 err = f2fs_convert_inline_page(&dn, page);
3168 if (err)
3169 goto out;
3170 if (dn.data_blkaddr == NULL_ADDR)
3171 err = f2fs_get_block(&dn, index);
3173 } else if (locked) {
3174 err = f2fs_get_block(&dn, index);
3175 } else {
3176 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3177 dn.data_blkaddr = ei.blk + index - ei.fofs;
3178 } else {
3179 /* hole case */
3180 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3181 if (err || dn.data_blkaddr == NULL_ADDR) {
3182 f2fs_put_dnode(&dn);
3183 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3184 true);
3185 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3186 locked = true;
3187 goto restart;
3192 /* convert_inline_page can make node_changed */
3193 *blk_addr = dn.data_blkaddr;
3194 *node_changed = dn.node_changed;
3195 out:
3196 f2fs_put_dnode(&dn);
3197 unlock_out:
3198 if (locked)
3199 __do_map_lock(sbi, flag, false);
3200 return err;
3203 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3204 loff_t pos, unsigned len, unsigned flags,
3205 struct page **pagep, void **fsdata)
3207 struct inode *inode = mapping->host;
3208 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3209 struct page *page = NULL;
3210 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3211 bool need_balance = false, drop_atomic = false;
3212 block_t blkaddr = NULL_ADDR;
3213 int err = 0;
3215 trace_f2fs_write_begin(inode, pos, len, flags);
3217 if (!f2fs_is_checkpoint_ready(sbi)) {
3218 err = -ENOSPC;
3219 goto fail;
3222 if ((f2fs_is_atomic_file(inode) &&
3223 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3224 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3225 err = -ENOMEM;
3226 drop_atomic = true;
3227 goto fail;
3231 * We should check this at this moment to avoid deadlock on inode page
3232 * and #0 page. The locking rule for inline_data conversion should be:
3233 * lock_page(page #0) -> lock_page(inode_page)
3235 if (index != 0) {
3236 err = f2fs_convert_inline_inode(inode);
3237 if (err)
3238 goto fail;
3241 #ifdef CONFIG_F2FS_FS_COMPRESSION
3242 if (f2fs_compressed_file(inode)) {
3243 int ret;
3245 *fsdata = NULL;
3247 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3248 index, fsdata);
3249 if (ret < 0) {
3250 err = ret;
3251 goto fail;
3252 } else if (ret) {
3253 return 0;
3256 #endif
3258 repeat:
3260 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3261 * wait_for_stable_page. Will wait that below with our IO control.
3263 page = f2fs_pagecache_get_page(mapping, index,
3264 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3265 if (!page) {
3266 err = -ENOMEM;
3267 goto fail;
3270 /* TODO: cluster can be compressed due to race with .writepage */
3272 *pagep = page;
3274 err = prepare_write_begin(sbi, page, pos, len,
3275 &blkaddr, &need_balance);
3276 if (err)
3277 goto fail;
3279 if (need_balance && !IS_NOQUOTA(inode) &&
3280 has_not_enough_free_secs(sbi, 0, 0)) {
3281 unlock_page(page);
3282 f2fs_balance_fs(sbi, true);
3283 lock_page(page);
3284 if (page->mapping != mapping) {
3285 /* The page got truncated from under us */
3286 f2fs_put_page(page, 1);
3287 goto repeat;
3291 f2fs_wait_on_page_writeback(page, DATA, false, true);
3293 if (len == PAGE_SIZE || PageUptodate(page))
3294 return 0;
3296 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3297 !f2fs_verity_in_progress(inode)) {
3298 zero_user_segment(page, len, PAGE_SIZE);
3299 return 0;
3302 if (blkaddr == NEW_ADDR) {
3303 zero_user_segment(page, 0, PAGE_SIZE);
3304 SetPageUptodate(page);
3305 } else {
3306 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3307 DATA_GENERIC_ENHANCE_READ)) {
3308 err = -EFSCORRUPTED;
3309 goto fail;
3311 err = f2fs_submit_page_read(inode, page, blkaddr);
3312 if (err)
3313 goto fail;
3315 lock_page(page);
3316 if (unlikely(page->mapping != mapping)) {
3317 f2fs_put_page(page, 1);
3318 goto repeat;
3320 if (unlikely(!PageUptodate(page))) {
3321 err = -EIO;
3322 goto fail;
3325 return 0;
3327 fail:
3328 f2fs_put_page(page, 1);
3329 f2fs_write_failed(mapping, pos + len);
3330 if (drop_atomic)
3331 f2fs_drop_inmem_pages_all(sbi, false);
3332 return err;
3335 static int f2fs_write_end(struct file *file,
3336 struct address_space *mapping,
3337 loff_t pos, unsigned len, unsigned copied,
3338 struct page *page, void *fsdata)
3340 struct inode *inode = page->mapping->host;
3342 trace_f2fs_write_end(inode, pos, len, copied);
3345 * This should be come from len == PAGE_SIZE, and we expect copied
3346 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3347 * let generic_perform_write() try to copy data again through copied=0.
3349 if (!PageUptodate(page)) {
3350 if (unlikely(copied != len))
3351 copied = 0;
3352 else
3353 SetPageUptodate(page);
3356 #ifdef CONFIG_F2FS_FS_COMPRESSION
3357 /* overwrite compressed file */
3358 if (f2fs_compressed_file(inode) && fsdata) {
3359 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3360 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3361 return copied;
3363 #endif
3365 if (!copied)
3366 goto unlock_out;
3368 set_page_dirty(page);
3370 if (pos + copied > i_size_read(inode) &&
3371 !f2fs_verity_in_progress(inode))
3372 f2fs_i_size_write(inode, pos + copied);
3373 unlock_out:
3374 f2fs_put_page(page, 1);
3375 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3376 return copied;
3379 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3380 loff_t offset)
3382 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3383 unsigned blkbits = i_blkbits;
3384 unsigned blocksize_mask = (1 << blkbits) - 1;
3385 unsigned long align = offset | iov_iter_alignment(iter);
3386 struct block_device *bdev = inode->i_sb->s_bdev;
3388 if (align & blocksize_mask) {
3389 if (bdev)
3390 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3391 blocksize_mask = (1 << blkbits) - 1;
3392 if (align & blocksize_mask)
3393 return -EINVAL;
3394 return 1;
3396 return 0;
3399 static void f2fs_dio_end_io(struct bio *bio)
3401 struct f2fs_private_dio *dio = bio->bi_private;
3403 dec_page_count(F2FS_I_SB(dio->inode),
3404 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3406 bio->bi_private = dio->orig_private;
3407 bio->bi_end_io = dio->orig_end_io;
3409 kvfree(dio);
3411 bio_endio(bio);
3414 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3415 loff_t file_offset)
3417 struct f2fs_private_dio *dio;
3418 bool write = (bio_op(bio) == REQ_OP_WRITE);
3420 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3421 sizeof(struct f2fs_private_dio), GFP_NOFS);
3422 if (!dio)
3423 goto out;
3425 dio->inode = inode;
3426 dio->orig_end_io = bio->bi_end_io;
3427 dio->orig_private = bio->bi_private;
3428 dio->write = write;
3430 bio->bi_end_io = f2fs_dio_end_io;
3431 bio->bi_private = dio;
3433 inc_page_count(F2FS_I_SB(inode),
3434 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3436 submit_bio(bio);
3437 return;
3438 out:
3439 bio->bi_status = BLK_STS_IOERR;
3440 bio_endio(bio);
3443 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3445 struct address_space *mapping = iocb->ki_filp->f_mapping;
3446 struct inode *inode = mapping->host;
3447 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3448 struct f2fs_inode_info *fi = F2FS_I(inode);
3449 size_t count = iov_iter_count(iter);
3450 loff_t offset = iocb->ki_pos;
3451 int rw = iov_iter_rw(iter);
3452 int err;
3453 enum rw_hint hint = iocb->ki_hint;
3454 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3455 bool do_opu;
3457 err = check_direct_IO(inode, iter, offset);
3458 if (err)
3459 return err < 0 ? err : 0;
3461 if (f2fs_force_buffered_io(inode, iocb, iter))
3462 return 0;
3464 do_opu = allow_outplace_dio(inode, iocb, iter);
3466 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3468 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3469 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3471 if (iocb->ki_flags & IOCB_NOWAIT) {
3472 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3473 iocb->ki_hint = hint;
3474 err = -EAGAIN;
3475 goto out;
3477 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3478 up_read(&fi->i_gc_rwsem[rw]);
3479 iocb->ki_hint = hint;
3480 err = -EAGAIN;
3481 goto out;
3483 } else {
3484 down_read(&fi->i_gc_rwsem[rw]);
3485 if (do_opu)
3486 down_read(&fi->i_gc_rwsem[READ]);
3489 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3490 iter, rw == WRITE ? get_data_block_dio_write :
3491 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3492 DIO_LOCKING | DIO_SKIP_HOLES);
3494 if (do_opu)
3495 up_read(&fi->i_gc_rwsem[READ]);
3497 up_read(&fi->i_gc_rwsem[rw]);
3499 if (rw == WRITE) {
3500 if (whint_mode == WHINT_MODE_OFF)
3501 iocb->ki_hint = hint;
3502 if (err > 0) {
3503 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3504 err);
3505 if (!do_opu)
3506 set_inode_flag(inode, FI_UPDATE_WRITE);
3507 } else if (err < 0) {
3508 f2fs_write_failed(mapping, offset + count);
3512 out:
3513 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3515 return err;
3518 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3519 unsigned int length)
3521 struct inode *inode = page->mapping->host;
3522 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3524 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3525 (offset % PAGE_SIZE || length != PAGE_SIZE))
3526 return;
3528 if (PageDirty(page)) {
3529 if (inode->i_ino == F2FS_META_INO(sbi)) {
3530 dec_page_count(sbi, F2FS_DIRTY_META);
3531 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3532 dec_page_count(sbi, F2FS_DIRTY_NODES);
3533 } else {
3534 inode_dec_dirty_pages(inode);
3535 f2fs_remove_dirty_inode(inode);
3539 clear_cold_data(page);
3541 if (IS_ATOMIC_WRITTEN_PAGE(page))
3542 return f2fs_drop_inmem_page(inode, page);
3544 f2fs_clear_page_private(page);
3547 int f2fs_release_page(struct page *page, gfp_t wait)
3549 /* If this is dirty page, keep PagePrivate */
3550 if (PageDirty(page))
3551 return 0;
3553 /* This is atomic written page, keep Private */
3554 if (IS_ATOMIC_WRITTEN_PAGE(page))
3555 return 0;
3557 clear_cold_data(page);
3558 f2fs_clear_page_private(page);
3559 return 1;
3562 static int f2fs_set_data_page_dirty(struct page *page)
3564 struct inode *inode = page_file_mapping(page)->host;
3566 trace_f2fs_set_page_dirty(page, DATA);
3568 if (!PageUptodate(page))
3569 SetPageUptodate(page);
3570 if (PageSwapCache(page))
3571 return __set_page_dirty_nobuffers(page);
3573 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3574 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3575 f2fs_register_inmem_page(inode, page);
3576 return 1;
3579 * Previously, this page has been registered, we just
3580 * return here.
3582 return 0;
3585 if (!PageDirty(page)) {
3586 __set_page_dirty_nobuffers(page);
3587 f2fs_update_dirty_page(inode, page);
3588 return 1;
3590 return 0;
3593 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3595 struct inode *inode = mapping->host;
3597 if (f2fs_has_inline_data(inode))
3598 return 0;
3600 /* make sure allocating whole blocks */
3601 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3602 filemap_write_and_wait(mapping);
3604 return generic_block_bmap(mapping, block, get_data_block_bmap);
3607 #ifdef CONFIG_MIGRATION
3608 #include <linux/migrate.h>
3610 int f2fs_migrate_page(struct address_space *mapping,
3611 struct page *newpage, struct page *page, enum migrate_mode mode)
3613 int rc, extra_count;
3614 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3615 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3617 BUG_ON(PageWriteback(page));
3619 /* migrating an atomic written page is safe with the inmem_lock hold */
3620 if (atomic_written) {
3621 if (mode != MIGRATE_SYNC)
3622 return -EBUSY;
3623 if (!mutex_trylock(&fi->inmem_lock))
3624 return -EAGAIN;
3627 /* one extra reference was held for atomic_write page */
3628 extra_count = atomic_written ? 1 : 0;
3629 rc = migrate_page_move_mapping(mapping, newpage,
3630 page, extra_count);
3631 if (rc != MIGRATEPAGE_SUCCESS) {
3632 if (atomic_written)
3633 mutex_unlock(&fi->inmem_lock);
3634 return rc;
3637 if (atomic_written) {
3638 struct inmem_pages *cur;
3639 list_for_each_entry(cur, &fi->inmem_pages, list)
3640 if (cur->page == page) {
3641 cur->page = newpage;
3642 break;
3644 mutex_unlock(&fi->inmem_lock);
3645 put_page(page);
3646 get_page(newpage);
3649 if (PagePrivate(page)) {
3650 f2fs_set_page_private(newpage, page_private(page));
3651 f2fs_clear_page_private(page);
3654 if (mode != MIGRATE_SYNC_NO_COPY)
3655 migrate_page_copy(newpage, page);
3656 else
3657 migrate_page_states(newpage, page);
3659 return MIGRATEPAGE_SUCCESS;
3661 #endif
3663 #ifdef CONFIG_SWAP
3664 /* Copied from generic_swapfile_activate() to check any holes */
3665 static int check_swap_activate(struct swap_info_struct *sis,
3666 struct file *swap_file, sector_t *span)
3668 struct address_space *mapping = swap_file->f_mapping;
3669 struct inode *inode = mapping->host;
3670 unsigned blocks_per_page;
3671 unsigned long page_no;
3672 unsigned blkbits;
3673 sector_t probe_block;
3674 sector_t last_block;
3675 sector_t lowest_block = -1;
3676 sector_t highest_block = 0;
3677 int nr_extents = 0;
3678 int ret;
3680 blkbits = inode->i_blkbits;
3681 blocks_per_page = PAGE_SIZE >> blkbits;
3684 * Map all the blocks into the extent list. This code doesn't try
3685 * to be very smart.
3687 probe_block = 0;
3688 page_no = 0;
3689 last_block = i_size_read(inode) >> blkbits;
3690 while ((probe_block + blocks_per_page) <= last_block &&
3691 page_no < sis->max) {
3692 unsigned block_in_page;
3693 sector_t first_block;
3694 sector_t block = 0;
3695 int err = 0;
3697 cond_resched();
3699 block = probe_block;
3700 err = bmap(inode, &block);
3701 if (err || !block)
3702 goto bad_bmap;
3703 first_block = block;
3706 * It must be PAGE_SIZE aligned on-disk
3708 if (first_block & (blocks_per_page - 1)) {
3709 probe_block++;
3710 goto reprobe;
3713 for (block_in_page = 1; block_in_page < blocks_per_page;
3714 block_in_page++) {
3716 block = probe_block + block_in_page;
3717 err = bmap(inode, &block);
3719 if (err || !block)
3720 goto bad_bmap;
3722 if (block != first_block + block_in_page) {
3723 /* Discontiguity */
3724 probe_block++;
3725 goto reprobe;
3729 first_block >>= (PAGE_SHIFT - blkbits);
3730 if (page_no) { /* exclude the header page */
3731 if (first_block < lowest_block)
3732 lowest_block = first_block;
3733 if (first_block > highest_block)
3734 highest_block = first_block;
3738 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3740 ret = add_swap_extent(sis, page_no, 1, first_block);
3741 if (ret < 0)
3742 goto out;
3743 nr_extents += ret;
3744 page_no++;
3745 probe_block += blocks_per_page;
3746 reprobe:
3747 continue;
3749 ret = nr_extents;
3750 *span = 1 + highest_block - lowest_block;
3751 if (page_no == 0)
3752 page_no = 1; /* force Empty message */
3753 sis->max = page_no;
3754 sis->pages = page_no - 1;
3755 sis->highest_bit = page_no - 1;
3756 out:
3757 return ret;
3758 bad_bmap:
3759 pr_err("swapon: swapfile has holes\n");
3760 return -EINVAL;
3763 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3764 sector_t *span)
3766 struct inode *inode = file_inode(file);
3767 int ret;
3769 if (!S_ISREG(inode->i_mode))
3770 return -EINVAL;
3772 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3773 return -EROFS;
3775 ret = f2fs_convert_inline_inode(inode);
3776 if (ret)
3777 return ret;
3779 if (f2fs_disable_compressed_file(inode))
3780 return -EINVAL;
3782 ret = check_swap_activate(sis, file, span);
3783 if (ret < 0)
3784 return ret;
3786 set_inode_flag(inode, FI_PIN_FILE);
3787 f2fs_precache_extents(inode);
3788 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3789 return ret;
3792 static void f2fs_swap_deactivate(struct file *file)
3794 struct inode *inode = file_inode(file);
3796 clear_inode_flag(inode, FI_PIN_FILE);
3798 #else
3799 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3800 sector_t *span)
3802 return -EOPNOTSUPP;
3805 static void f2fs_swap_deactivate(struct file *file)
3808 #endif
3810 const struct address_space_operations f2fs_dblock_aops = {
3811 .readpage = f2fs_read_data_page,
3812 .readpages = f2fs_read_data_pages,
3813 .writepage = f2fs_write_data_page,
3814 .writepages = f2fs_write_data_pages,
3815 .write_begin = f2fs_write_begin,
3816 .write_end = f2fs_write_end,
3817 .set_page_dirty = f2fs_set_data_page_dirty,
3818 .invalidatepage = f2fs_invalidate_page,
3819 .releasepage = f2fs_release_page,
3820 .direct_IO = f2fs_direct_IO,
3821 .bmap = f2fs_bmap,
3822 .swap_activate = f2fs_swap_activate,
3823 .swap_deactivate = f2fs_swap_deactivate,
3824 #ifdef CONFIG_MIGRATION
3825 .migratepage = f2fs_migrate_page,
3826 #endif
3829 void f2fs_clear_page_cache_dirty_tag(struct page *page)
3831 struct address_space *mapping = page_mapping(page);
3832 unsigned long flags;
3834 xa_lock_irqsave(&mapping->i_pages, flags);
3835 __xa_clear_mark(&mapping->i_pages, page_index(page),
3836 PAGECACHE_TAG_DIRTY);
3837 xa_unlock_irqrestore(&mapping->i_pages, flags);
3840 int __init f2fs_init_post_read_processing(void)
3842 bio_post_read_ctx_cache =
3843 kmem_cache_create("f2fs_bio_post_read_ctx",
3844 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
3845 if (!bio_post_read_ctx_cache)
3846 goto fail;
3847 bio_post_read_ctx_pool =
3848 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3849 bio_post_read_ctx_cache);
3850 if (!bio_post_read_ctx_pool)
3851 goto fail_free_cache;
3852 return 0;
3854 fail_free_cache:
3855 kmem_cache_destroy(bio_post_read_ctx_cache);
3856 fail:
3857 return -ENOMEM;
3860 void f2fs_destroy_post_read_processing(void)
3862 mempool_destroy(bio_post_read_ctx_pool);
3863 kmem_cache_destroy(bio_post_read_ctx_cache);
3866 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
3868 if (!f2fs_sb_has_encrypt(sbi) &&
3869 !f2fs_sb_has_verity(sbi) &&
3870 !f2fs_sb_has_compression(sbi))
3871 return 0;
3873 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
3874 WQ_UNBOUND | WQ_HIGHPRI,
3875 num_online_cpus());
3876 if (!sbi->post_read_wq)
3877 return -ENOMEM;
3878 return 0;
3881 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
3883 if (sbi->post_read_wq)
3884 destroy_workqueue(sbi->post_read_wq);
3887 int __init f2fs_init_bio_entry_cache(void)
3889 bio_entry_slab = f2fs_kmem_cache_create("bio_entry_slab",
3890 sizeof(struct bio_entry));
3891 if (!bio_entry_slab)
3892 return -ENOMEM;
3893 return 0;
3896 void f2fs_destroy_bio_entry_cache(void)
3898 kmem_cache_destroy(bio_entry_slab);