1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
12 #include <linux/writeback.h>
13 #include <linux/backing-dev.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/prefetch.h>
18 #include <linux/uio.h>
19 #include <linux/cleancache.h>
20 #include <linux/sched/signal.h>
26 #include <trace/events/f2fs.h>
28 #define NUM_PREALLOC_POST_READ_CTXS 128
30 static struct kmem_cache *bio_post_read_ctx_cache;
31 static mempool_t *bio_post_read_ctx_pool;
33 static bool __is_cp_guaranteed(struct page *page)
35 struct address_space *mapping = page->mapping;
37 struct f2fs_sb_info *sbi;
42 inode = mapping->host;
43 sbi = F2FS_I_SB(inode);
45 if (inode->i_ino == F2FS_META_INO(sbi) ||
46 inode->i_ino == F2FS_NODE_INO(sbi) ||
47 S_ISDIR(inode->i_mode) ||
48 (S_ISREG(inode->i_mode) &&
49 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
55 static enum count_type __read_io_type(struct page *page)
57 struct address_space *mapping = page->mapping;
60 struct inode *inode = mapping->host;
61 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
63 if (inode->i_ino == F2FS_META_INO(sbi))
66 if (inode->i_ino == F2FS_NODE_INO(sbi))
72 /* postprocessing steps for read bios */
73 enum bio_post_read_step {
78 struct bio_post_read_ctx {
80 struct work_struct work;
81 unsigned int cur_step;
82 unsigned int enabled_steps;
85 static void __read_end_io(struct bio *bio)
89 struct bvec_iter_all iter_all;
91 bio_for_each_segment_all(bv, bio, iter_all) {
94 /* PG_error was set if any post_read step failed */
95 if (bio->bi_status || PageError(page)) {
96 ClearPageUptodate(page);
97 /* will re-read again later */
100 SetPageUptodate(page);
102 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
106 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
110 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
112 static void decrypt_work(struct work_struct *work)
114 struct bio_post_read_ctx *ctx =
115 container_of(work, struct bio_post_read_ctx, work);
117 fscrypt_decrypt_bio(ctx->bio);
119 bio_post_read_processing(ctx);
122 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
124 switch (++ctx->cur_step) {
126 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
127 INIT_WORK(&ctx->work, decrypt_work);
128 fscrypt_enqueue_decrypt_work(&ctx->work);
134 __read_end_io(ctx->bio);
138 static bool f2fs_bio_post_read_required(struct bio *bio)
140 return bio->bi_private && !bio->bi_status;
143 static void f2fs_read_end_io(struct bio *bio)
145 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)),
147 f2fs_show_injection_info(FAULT_READ_IO);
148 bio->bi_status = BLK_STS_IOERR;
151 if (f2fs_bio_post_read_required(bio)) {
152 struct bio_post_read_ctx *ctx = bio->bi_private;
154 ctx->cur_step = STEP_INITIAL;
155 bio_post_read_processing(ctx);
162 static void f2fs_write_end_io(struct bio *bio)
164 struct f2fs_sb_info *sbi = bio->bi_private;
165 struct bio_vec *bvec;
166 struct bvec_iter_all iter_all;
168 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
169 f2fs_show_injection_info(FAULT_WRITE_IO);
170 bio->bi_status = BLK_STS_IOERR;
173 bio_for_each_segment_all(bvec, bio, iter_all) {
174 struct page *page = bvec->bv_page;
175 enum count_type type = WB_DATA_TYPE(page);
177 if (IS_DUMMY_WRITTEN_PAGE(page)) {
178 set_page_private(page, (unsigned long)NULL);
179 ClearPagePrivate(page);
181 mempool_free(page, sbi->write_io_dummy);
183 if (unlikely(bio->bi_status))
184 f2fs_stop_checkpoint(sbi, true);
188 fscrypt_pullback_bio_page(&page, true);
190 if (unlikely(bio->bi_status)) {
191 mapping_set_error(page->mapping, -EIO);
192 if (type == F2FS_WB_CP_DATA)
193 f2fs_stop_checkpoint(sbi, true);
196 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
197 page->index != nid_of_node(page));
199 dec_page_count(sbi, type);
200 if (f2fs_in_warm_node_list(sbi, page))
201 f2fs_del_fsync_node_entry(sbi, page);
202 clear_cold_data(page);
203 end_page_writeback(page);
205 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
206 wq_has_sleeper(&sbi->cp_wait))
207 wake_up(&sbi->cp_wait);
213 * Return true, if pre_bio's bdev is same as its target device.
215 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
216 block_t blk_addr, struct bio *bio)
218 struct block_device *bdev = sbi->sb->s_bdev;
221 for (i = 0; i < sbi->s_ndevs; i++) {
222 if (FDEV(i).start_blk <= blk_addr &&
223 FDEV(i).end_blk >= blk_addr) {
224 blk_addr -= FDEV(i).start_blk;
230 bio_set_dev(bio, bdev);
231 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
236 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
240 for (i = 0; i < sbi->s_ndevs; i++)
241 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
246 static bool __same_bdev(struct f2fs_sb_info *sbi,
247 block_t blk_addr, struct bio *bio)
249 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
250 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
254 * Low-level block read/write IO operations.
256 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
257 struct writeback_control *wbc,
258 int npages, bool is_read,
259 enum page_type type, enum temp_type temp)
263 bio = f2fs_bio_alloc(sbi, npages, true);
265 f2fs_target_device(sbi, blk_addr, bio);
267 bio->bi_end_io = f2fs_read_end_io;
268 bio->bi_private = NULL;
270 bio->bi_end_io = f2fs_write_end_io;
271 bio->bi_private = sbi;
272 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
275 wbc_init_bio(wbc, bio);
280 static inline void __submit_bio(struct f2fs_sb_info *sbi,
281 struct bio *bio, enum page_type type)
283 if (!is_read_io(bio_op(bio))) {
286 if (type != DATA && type != NODE)
289 if (test_opt(sbi, LFS) && current->plug)
290 blk_finish_plug(current->plug);
292 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
293 start %= F2FS_IO_SIZE(sbi);
298 /* fill dummy pages */
299 for (; start < F2FS_IO_SIZE(sbi); start++) {
301 mempool_alloc(sbi->write_io_dummy,
302 GFP_NOIO | __GFP_NOFAIL);
303 f2fs_bug_on(sbi, !page);
305 zero_user_segment(page, 0, PAGE_SIZE);
306 SetPagePrivate(page);
307 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
309 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
313 * In the NODE case, we lose next block address chain. So, we
314 * need to do checkpoint in f2fs_sync_file.
317 set_sbi_flag(sbi, SBI_NEED_CP);
320 if (is_read_io(bio_op(bio)))
321 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
323 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
327 static void __submit_merged_bio(struct f2fs_bio_info *io)
329 struct f2fs_io_info *fio = &io->fio;
334 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
336 if (is_read_io(fio->op))
337 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
339 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
341 __submit_bio(io->sbi, io->bio, fio->type);
345 static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
346 struct page *page, nid_t ino)
348 struct bio_vec *bvec;
350 struct bvec_iter_all iter_all;
355 if (!inode && !page && !ino)
358 bio_for_each_segment_all(bvec, io->bio, iter_all) {
360 if (bvec->bv_page->mapping)
361 target = bvec->bv_page;
363 target = fscrypt_control_page(bvec->bv_page);
365 if (inode && inode == target->mapping->host)
367 if (page && page == target)
369 if (ino && ino == ino_of_node(target))
376 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
377 enum page_type type, enum temp_type temp)
379 enum page_type btype = PAGE_TYPE_OF_BIO(type);
380 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
382 down_write(&io->io_rwsem);
384 /* change META to META_FLUSH in the checkpoint procedure */
385 if (type >= META_FLUSH) {
386 io->fio.type = META_FLUSH;
387 io->fio.op = REQ_OP_WRITE;
388 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
389 if (!test_opt(sbi, NOBARRIER))
390 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
392 __submit_merged_bio(io);
393 up_write(&io->io_rwsem);
396 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
397 struct inode *inode, struct page *page,
398 nid_t ino, enum page_type type, bool force)
403 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
405 enum page_type btype = PAGE_TYPE_OF_BIO(type);
406 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
408 down_read(&io->io_rwsem);
409 ret = __has_merged_page(io, inode, page, ino);
410 up_read(&io->io_rwsem);
413 __f2fs_submit_merged_write(sbi, type, temp);
415 /* TODO: use HOT temp only for meta pages now. */
421 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
423 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
426 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
427 struct inode *inode, struct page *page,
428 nid_t ino, enum page_type type)
430 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
433 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
435 f2fs_submit_merged_write(sbi, DATA);
436 f2fs_submit_merged_write(sbi, NODE);
437 f2fs_submit_merged_write(sbi, META);
441 * Fill the locked page with data located in the block address.
442 * A caller needs to unlock the page on failure.
444 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
447 struct page *page = fio->encrypted_page ?
448 fio->encrypted_page : fio->page;
450 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
451 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
454 trace_f2fs_submit_page_bio(page, fio);
455 f2fs_trace_ios(fio, 0);
457 /* Allocate a new bio */
458 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
459 1, is_read_io(fio->op), fio->type, fio->temp);
461 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
466 if (fio->io_wbc && !is_read_io(fio->op))
467 wbc_account_io(fio->io_wbc, page, PAGE_SIZE);
469 bio_set_op_attrs(bio, fio->op, fio->op_flags);
471 inc_page_count(fio->sbi, is_read_io(fio->op) ?
472 __read_io_type(page): WB_DATA_TYPE(fio->page));
474 __submit_bio(fio->sbi, bio, fio->type);
478 void f2fs_submit_page_write(struct f2fs_io_info *fio)
480 struct f2fs_sb_info *sbi = fio->sbi;
481 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
482 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
483 struct page *bio_page;
485 f2fs_bug_on(sbi, is_read_io(fio->op));
487 down_write(&io->io_rwsem);
490 spin_lock(&io->io_lock);
491 if (list_empty(&io->io_list)) {
492 spin_unlock(&io->io_lock);
495 fio = list_first_entry(&io->io_list,
496 struct f2fs_io_info, list);
497 list_del(&fio->list);
498 spin_unlock(&io->io_lock);
501 if (__is_valid_data_blkaddr(fio->old_blkaddr))
502 verify_block_addr(fio, fio->old_blkaddr);
503 verify_block_addr(fio, fio->new_blkaddr);
505 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
507 /* set submitted = true as a return value */
508 fio->submitted = true;
510 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
512 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
513 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
514 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
515 __submit_merged_bio(io);
517 if (io->bio == NULL) {
518 if ((fio->type == DATA || fio->type == NODE) &&
519 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
520 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
524 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
525 BIO_MAX_PAGES, false,
526 fio->type, fio->temp);
530 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
531 __submit_merged_bio(io);
536 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
538 io->last_block_in_bio = fio->new_blkaddr;
539 f2fs_trace_ios(fio, 0);
541 trace_f2fs_submit_page_write(fio->page, fio);
546 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
547 f2fs_is_checkpoint_ready(sbi))
548 __submit_merged_bio(io);
549 up_write(&io->io_rwsem);
552 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
553 unsigned nr_pages, unsigned op_flag)
555 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
557 struct bio_post_read_ctx *ctx;
558 unsigned int post_read_steps = 0;
560 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
561 return ERR_PTR(-EFAULT);
563 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
565 return ERR_PTR(-ENOMEM);
566 f2fs_target_device(sbi, blkaddr, bio);
567 bio->bi_end_io = f2fs_read_end_io;
568 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
570 if (f2fs_encrypted_file(inode))
571 post_read_steps |= 1 << STEP_DECRYPT;
572 if (post_read_steps) {
573 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
576 return ERR_PTR(-ENOMEM);
579 ctx->enabled_steps = post_read_steps;
580 bio->bi_private = ctx;
586 /* This can handle encryption stuffs */
587 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
590 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
595 /* wait for GCed page writeback via META_MAPPING */
596 f2fs_wait_on_block_writeback(inode, blkaddr);
598 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
602 ClearPageError(page);
603 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
604 __submit_bio(F2FS_I_SB(inode), bio, DATA);
608 static void __set_data_blkaddr(struct dnode_of_data *dn)
610 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
614 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
615 base = get_extra_isize(dn->inode);
617 /* Get physical address of data block */
618 addr_array = blkaddr_in_node(rn);
619 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
623 * Lock ordering for the change of data block address:
626 * update block addresses in the node page
628 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
630 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
631 __set_data_blkaddr(dn);
632 if (set_page_dirty(dn->node_page))
633 dn->node_changed = true;
636 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
638 dn->data_blkaddr = blkaddr;
639 f2fs_set_data_blkaddr(dn);
640 f2fs_update_extent_cache(dn);
643 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
644 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
646 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
652 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
654 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
657 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
658 dn->ofs_in_node, count);
660 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
662 for (; count > 0; dn->ofs_in_node++) {
663 block_t blkaddr = datablock_addr(dn->inode,
664 dn->node_page, dn->ofs_in_node);
665 if (blkaddr == NULL_ADDR) {
666 dn->data_blkaddr = NEW_ADDR;
667 __set_data_blkaddr(dn);
672 if (set_page_dirty(dn->node_page))
673 dn->node_changed = true;
677 /* Should keep dn->ofs_in_node unchanged */
678 int f2fs_reserve_new_block(struct dnode_of_data *dn)
680 unsigned int ofs_in_node = dn->ofs_in_node;
683 ret = f2fs_reserve_new_blocks(dn, 1);
684 dn->ofs_in_node = ofs_in_node;
688 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
690 bool need_put = dn->inode_page ? false : true;
693 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
697 if (dn->data_blkaddr == NULL_ADDR)
698 err = f2fs_reserve_new_block(dn);
704 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
706 struct extent_info ei = {0,0,0};
707 struct inode *inode = dn->inode;
709 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
710 dn->data_blkaddr = ei.blk + index - ei.fofs;
714 return f2fs_reserve_block(dn, index);
717 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
718 int op_flags, bool for_write)
720 struct address_space *mapping = inode->i_mapping;
721 struct dnode_of_data dn;
723 struct extent_info ei = {0,0,0};
726 page = f2fs_grab_cache_page(mapping, index, for_write);
728 return ERR_PTR(-ENOMEM);
730 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
731 dn.data_blkaddr = ei.blk + index - ei.fofs;
735 set_new_dnode(&dn, inode, NULL, NULL, 0);
736 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
741 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
746 if (PageUptodate(page)) {
752 * A new dentry page is allocated but not able to be written, since its
753 * new inode page couldn't be allocated due to -ENOSPC.
754 * In such the case, its blkaddr can be remained as NEW_ADDR.
755 * see, f2fs_add_link -> f2fs_get_new_data_page ->
756 * f2fs_init_inode_metadata.
758 if (dn.data_blkaddr == NEW_ADDR) {
759 zero_user_segment(page, 0, PAGE_SIZE);
760 if (!PageUptodate(page))
761 SetPageUptodate(page);
766 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
772 f2fs_put_page(page, 1);
776 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
778 struct address_space *mapping = inode->i_mapping;
781 page = find_get_page(mapping, index);
782 if (page && PageUptodate(page))
784 f2fs_put_page(page, 0);
786 page = f2fs_get_read_data_page(inode, index, 0, false);
790 if (PageUptodate(page))
793 wait_on_page_locked(page);
794 if (unlikely(!PageUptodate(page))) {
795 f2fs_put_page(page, 0);
796 return ERR_PTR(-EIO);
802 * If it tries to access a hole, return an error.
803 * Because, the callers, functions in dir.c and GC, should be able to know
804 * whether this page exists or not.
806 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
809 struct address_space *mapping = inode->i_mapping;
812 page = f2fs_get_read_data_page(inode, index, 0, for_write);
816 /* wait for read completion */
818 if (unlikely(page->mapping != mapping)) {
819 f2fs_put_page(page, 1);
822 if (unlikely(!PageUptodate(page))) {
823 f2fs_put_page(page, 1);
824 return ERR_PTR(-EIO);
830 * Caller ensures that this data page is never allocated.
831 * A new zero-filled data page is allocated in the page cache.
833 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
835 * Note that, ipage is set only by make_empty_dir, and if any error occur,
836 * ipage should be released by this function.
838 struct page *f2fs_get_new_data_page(struct inode *inode,
839 struct page *ipage, pgoff_t index, bool new_i_size)
841 struct address_space *mapping = inode->i_mapping;
843 struct dnode_of_data dn;
846 page = f2fs_grab_cache_page(mapping, index, true);
849 * before exiting, we should make sure ipage will be released
850 * if any error occur.
852 f2fs_put_page(ipage, 1);
853 return ERR_PTR(-ENOMEM);
856 set_new_dnode(&dn, inode, ipage, NULL, 0);
857 err = f2fs_reserve_block(&dn, index);
859 f2fs_put_page(page, 1);
865 if (PageUptodate(page))
868 if (dn.data_blkaddr == NEW_ADDR) {
869 zero_user_segment(page, 0, PAGE_SIZE);
870 if (!PageUptodate(page))
871 SetPageUptodate(page);
873 f2fs_put_page(page, 1);
875 /* if ipage exists, blkaddr should be NEW_ADDR */
876 f2fs_bug_on(F2FS_I_SB(inode), ipage);
877 page = f2fs_get_lock_data_page(inode, index, true);
882 if (new_i_size && i_size_read(inode) <
883 ((loff_t)(index + 1) << PAGE_SHIFT))
884 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
888 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
890 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
891 struct f2fs_summary sum;
897 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
900 err = f2fs_get_node_info(sbi, dn->nid, &ni);
904 dn->data_blkaddr = datablock_addr(dn->inode,
905 dn->node_page, dn->ofs_in_node);
906 if (dn->data_blkaddr != NULL_ADDR)
909 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
913 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
914 old_blkaddr = dn->data_blkaddr;
915 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
916 &sum, seg_type, NULL, false);
917 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
918 invalidate_mapping_pages(META_MAPPING(sbi),
919 old_blkaddr, old_blkaddr);
920 f2fs_set_data_blkaddr(dn);
923 * i_size will be updated by direct_IO. Otherwise, we'll get stale
924 * data from unwritten block via dio_read.
929 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
931 struct inode *inode = file_inode(iocb->ki_filp);
932 struct f2fs_map_blocks map;
935 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
937 /* convert inline data for Direct I/O*/
939 err = f2fs_convert_inline_inode(inode);
944 if (direct_io && allow_outplace_dio(inode, iocb, from))
947 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
950 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
951 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
952 if (map.m_len > map.m_lblk)
953 map.m_len -= map.m_lblk;
957 map.m_next_pgofs = NULL;
958 map.m_next_extent = NULL;
959 map.m_seg_type = NO_CHECK_TYPE;
960 map.m_may_create = true;
963 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
964 flag = f2fs_force_buffered_io(inode, iocb, from) ?
965 F2FS_GET_BLOCK_PRE_AIO :
966 F2FS_GET_BLOCK_PRE_DIO;
969 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
970 err = f2fs_convert_inline_inode(inode);
974 if (f2fs_has_inline_data(inode))
977 flag = F2FS_GET_BLOCK_PRE_AIO;
980 err = f2fs_map_blocks(inode, &map, 1, flag);
981 if (map.m_len > 0 && err == -ENOSPC) {
983 set_inode_flag(inode, FI_NO_PREALLOC);
989 void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
991 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
993 down_read(&sbi->node_change);
995 up_read(&sbi->node_change);
1000 f2fs_unlock_op(sbi);
1005 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1006 * f2fs_map_blocks structure.
1007 * If original data blocks are allocated, then give them to blockdev.
1009 * a. preallocate requested block addresses
1010 * b. do not use extent cache for better performance
1011 * c. give the block addresses to blockdev
1013 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1014 int create, int flag)
1016 unsigned int maxblocks = map->m_len;
1017 struct dnode_of_data dn;
1018 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1019 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1020 pgoff_t pgofs, end_offset, end;
1021 int err = 0, ofs = 1;
1022 unsigned int ofs_in_node, last_ofs_in_node;
1024 struct extent_info ei = {0,0,0};
1026 unsigned int start_pgofs;
1034 /* it only supports block size == page size */
1035 pgofs = (pgoff_t)map->m_lblk;
1036 end = pgofs + maxblocks;
1038 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1039 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1043 map->m_pblk = ei.blk + pgofs - ei.fofs;
1044 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1045 map->m_flags = F2FS_MAP_MAPPED;
1046 if (map->m_next_extent)
1047 *map->m_next_extent = pgofs + map->m_len;
1049 /* for hardware encryption, but to avoid potential issue in future */
1050 if (flag == F2FS_GET_BLOCK_DIO)
1051 f2fs_wait_on_block_writeback_range(inode,
1052 map->m_pblk, map->m_len);
1057 if (map->m_may_create)
1058 __do_map_lock(sbi, flag, true);
1060 /* When reading holes, we need its node page */
1061 set_new_dnode(&dn, inode, NULL, NULL, 0);
1062 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1064 if (flag == F2FS_GET_BLOCK_BMAP)
1066 if (err == -ENOENT) {
1068 if (map->m_next_pgofs)
1069 *map->m_next_pgofs =
1070 f2fs_get_next_page_offset(&dn, pgofs);
1071 if (map->m_next_extent)
1072 *map->m_next_extent =
1073 f2fs_get_next_page_offset(&dn, pgofs);
1078 start_pgofs = pgofs;
1080 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1081 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1084 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1086 if (__is_valid_data_blkaddr(blkaddr) &&
1087 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1092 if (is_valid_data_blkaddr(sbi, blkaddr)) {
1093 /* use out-place-update for driect IO under LFS mode */
1094 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1095 map->m_may_create) {
1096 err = __allocate_data_block(&dn, map->m_seg_type);
1098 blkaddr = dn.data_blkaddr;
1099 set_inode_flag(inode, FI_APPEND_WRITE);
1104 if (unlikely(f2fs_cp_error(sbi))) {
1108 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1109 if (blkaddr == NULL_ADDR) {
1111 last_ofs_in_node = dn.ofs_in_node;
1114 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1115 flag != F2FS_GET_BLOCK_DIO);
1116 err = __allocate_data_block(&dn,
1119 set_inode_flag(inode, FI_APPEND_WRITE);
1123 map->m_flags |= F2FS_MAP_NEW;
1124 blkaddr = dn.data_blkaddr;
1126 if (flag == F2FS_GET_BLOCK_BMAP) {
1130 if (flag == F2FS_GET_BLOCK_PRECACHE)
1132 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1133 blkaddr == NULL_ADDR) {
1134 if (map->m_next_pgofs)
1135 *map->m_next_pgofs = pgofs + 1;
1138 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1139 /* for defragment case */
1140 if (map->m_next_pgofs)
1141 *map->m_next_pgofs = pgofs + 1;
1147 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1150 if (map->m_len == 0) {
1151 /* preallocated unwritten block should be mapped for fiemap. */
1152 if (blkaddr == NEW_ADDR)
1153 map->m_flags |= F2FS_MAP_UNWRITTEN;
1154 map->m_flags |= F2FS_MAP_MAPPED;
1156 map->m_pblk = blkaddr;
1158 } else if ((map->m_pblk != NEW_ADDR &&
1159 blkaddr == (map->m_pblk + ofs)) ||
1160 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1161 flag == F2FS_GET_BLOCK_PRE_DIO) {
1172 /* preallocate blocks in batch for one dnode page */
1173 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1174 (pgofs == end || dn.ofs_in_node == end_offset)) {
1176 dn.ofs_in_node = ofs_in_node;
1177 err = f2fs_reserve_new_blocks(&dn, prealloc);
1181 map->m_len += dn.ofs_in_node - ofs_in_node;
1182 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1186 dn.ofs_in_node = end_offset;
1191 else if (dn.ofs_in_node < end_offset)
1194 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1195 if (map->m_flags & F2FS_MAP_MAPPED) {
1196 unsigned int ofs = start_pgofs - map->m_lblk;
1198 f2fs_update_extent_cache_range(&dn,
1199 start_pgofs, map->m_pblk + ofs,
1204 f2fs_put_dnode(&dn);
1206 if (map->m_may_create) {
1207 __do_map_lock(sbi, flag, false);
1208 f2fs_balance_fs(sbi, dn.node_changed);
1214 /* for hardware encryption, but to avoid potential issue in future */
1215 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1216 f2fs_wait_on_block_writeback_range(inode,
1217 map->m_pblk, map->m_len);
1219 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1220 if (map->m_flags & F2FS_MAP_MAPPED) {
1221 unsigned int ofs = start_pgofs - map->m_lblk;
1223 f2fs_update_extent_cache_range(&dn,
1224 start_pgofs, map->m_pblk + ofs,
1227 if (map->m_next_extent)
1228 *map->m_next_extent = pgofs + 1;
1230 f2fs_put_dnode(&dn);
1232 if (map->m_may_create) {
1233 __do_map_lock(sbi, flag, false);
1234 f2fs_balance_fs(sbi, dn.node_changed);
1237 trace_f2fs_map_blocks(inode, map, err);
1241 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1243 struct f2fs_map_blocks map;
1247 if (pos + len > i_size_read(inode))
1250 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1251 map.m_next_pgofs = NULL;
1252 map.m_next_extent = NULL;
1253 map.m_seg_type = NO_CHECK_TYPE;
1254 map.m_may_create = false;
1255 last_lblk = F2FS_BLK_ALIGN(pos + len);
1257 while (map.m_lblk < last_lblk) {
1258 map.m_len = last_lblk - map.m_lblk;
1259 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1260 if (err || map.m_len == 0)
1262 map.m_lblk += map.m_len;
1267 static int __get_data_block(struct inode *inode, sector_t iblock,
1268 struct buffer_head *bh, int create, int flag,
1269 pgoff_t *next_pgofs, int seg_type, bool may_write)
1271 struct f2fs_map_blocks map;
1274 map.m_lblk = iblock;
1275 map.m_len = bh->b_size >> inode->i_blkbits;
1276 map.m_next_pgofs = next_pgofs;
1277 map.m_next_extent = NULL;
1278 map.m_seg_type = seg_type;
1279 map.m_may_create = may_write;
1281 err = f2fs_map_blocks(inode, &map, create, flag);
1283 map_bh(bh, inode->i_sb, map.m_pblk);
1284 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1285 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1290 static int get_data_block(struct inode *inode, sector_t iblock,
1291 struct buffer_head *bh_result, int create, int flag,
1292 pgoff_t *next_pgofs)
1294 return __get_data_block(inode, iblock, bh_result, create,
1296 NO_CHECK_TYPE, create);
1299 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1300 struct buffer_head *bh_result, int create)
1302 return __get_data_block(inode, iblock, bh_result, create,
1303 F2FS_GET_BLOCK_DIO, NULL,
1304 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1308 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1309 struct buffer_head *bh_result, int create)
1311 return __get_data_block(inode, iblock, bh_result, create,
1312 F2FS_GET_BLOCK_DIO, NULL,
1313 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1317 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1318 struct buffer_head *bh_result, int create)
1320 /* Block number less than F2FS MAX BLOCKS */
1321 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1324 return __get_data_block(inode, iblock, bh_result, create,
1325 F2FS_GET_BLOCK_BMAP, NULL,
1326 NO_CHECK_TYPE, create);
1329 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1331 return (offset >> inode->i_blkbits);
1334 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1336 return (blk << inode->i_blkbits);
1339 static int f2fs_xattr_fiemap(struct inode *inode,
1340 struct fiemap_extent_info *fieinfo)
1342 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1344 struct node_info ni;
1345 __u64 phys = 0, len;
1347 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1350 if (f2fs_has_inline_xattr(inode)) {
1353 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1354 inode->i_ino, false);
1358 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1360 f2fs_put_page(page, 1);
1364 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1365 offset = offsetof(struct f2fs_inode, i_addr) +
1366 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1367 get_inline_xattr_addrs(inode));
1370 len = inline_xattr_size(inode);
1372 f2fs_put_page(page, 1);
1374 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1377 flags |= FIEMAP_EXTENT_LAST;
1379 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1380 if (err || err == 1)
1385 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1389 err = f2fs_get_node_info(sbi, xnid, &ni);
1391 f2fs_put_page(page, 1);
1395 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1396 len = inode->i_sb->s_blocksize;
1398 f2fs_put_page(page, 1);
1400 flags = FIEMAP_EXTENT_LAST;
1404 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1406 return (err < 0 ? err : 0);
1409 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1412 struct buffer_head map_bh;
1413 sector_t start_blk, last_blk;
1415 u64 logical = 0, phys = 0, size = 0;
1419 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1420 ret = f2fs_precache_extents(inode);
1425 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1431 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1432 ret = f2fs_xattr_fiemap(inode, fieinfo);
1436 if (f2fs_has_inline_data(inode)) {
1437 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1442 if (logical_to_blk(inode, len) == 0)
1443 len = blk_to_logical(inode, 1);
1445 start_blk = logical_to_blk(inode, start);
1446 last_blk = logical_to_blk(inode, start + len - 1);
1449 memset(&map_bh, 0, sizeof(struct buffer_head));
1450 map_bh.b_size = len;
1452 ret = get_data_block(inode, start_blk, &map_bh, 0,
1453 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1458 if (!buffer_mapped(&map_bh)) {
1459 start_blk = next_pgofs;
1461 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1462 F2FS_I_SB(inode)->max_file_blocks))
1465 flags |= FIEMAP_EXTENT_LAST;
1469 if (IS_ENCRYPTED(inode))
1470 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1472 ret = fiemap_fill_next_extent(fieinfo, logical,
1476 if (start_blk > last_blk || ret)
1479 logical = blk_to_logical(inode, start_blk);
1480 phys = blk_to_logical(inode, map_bh.b_blocknr);
1481 size = map_bh.b_size;
1483 if (buffer_unwritten(&map_bh))
1484 flags = FIEMAP_EXTENT_UNWRITTEN;
1486 start_blk += logical_to_blk(inode, size);
1490 if (fatal_signal_pending(current))
1498 inode_unlock(inode);
1503 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1504 * Major change was from block_size == page_size in f2fs by default.
1506 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1507 * this function ever deviates from doing just read-ahead, it should either
1508 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1511 static int f2fs_mpage_readpages(struct address_space *mapping,
1512 struct list_head *pages, struct page *page,
1513 unsigned nr_pages, bool is_readahead)
1515 struct bio *bio = NULL;
1516 sector_t last_block_in_bio = 0;
1517 struct inode *inode = mapping->host;
1518 const unsigned blkbits = inode->i_blkbits;
1519 const unsigned blocksize = 1 << blkbits;
1520 sector_t block_in_file;
1521 sector_t last_block;
1522 sector_t last_block_in_file;
1524 struct f2fs_map_blocks map;
1530 map.m_next_pgofs = NULL;
1531 map.m_next_extent = NULL;
1532 map.m_seg_type = NO_CHECK_TYPE;
1533 map.m_may_create = false;
1535 for (; nr_pages; nr_pages--) {
1537 page = list_last_entry(pages, struct page, lru);
1539 prefetchw(&page->flags);
1540 list_del(&page->lru);
1541 if (add_to_page_cache_lru(page, mapping,
1543 readahead_gfp_mask(mapping)))
1547 block_in_file = (sector_t)page->index;
1548 last_block = block_in_file + nr_pages;
1549 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1551 if (last_block > last_block_in_file)
1552 last_block = last_block_in_file;
1554 /* just zeroing out page which is beyond EOF */
1555 if (block_in_file >= last_block)
1558 * Map blocks using the previous result first.
1560 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1561 block_in_file > map.m_lblk &&
1562 block_in_file < (map.m_lblk + map.m_len))
1566 * Then do more f2fs_map_blocks() calls until we are
1567 * done with this page.
1569 map.m_lblk = block_in_file;
1570 map.m_len = last_block - block_in_file;
1572 if (f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT))
1573 goto set_error_page;
1575 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1576 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1577 SetPageMappedToDisk(page);
1579 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1580 SetPageUptodate(page);
1584 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1586 goto set_error_page;
1589 zero_user_segment(page, 0, PAGE_SIZE);
1590 if (!PageUptodate(page))
1591 SetPageUptodate(page);
1597 * This page will go to BIO. Do we need to send this
1600 if (bio && (last_block_in_bio != block_nr - 1 ||
1601 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
1603 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1607 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1608 is_readahead ? REQ_RAHEAD : 0);
1611 goto set_error_page;
1616 * If the page is under writeback, we need to wait for
1617 * its completion to see the correct decrypted data.
1619 f2fs_wait_on_block_writeback(inode, block_nr);
1621 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1622 goto submit_and_realloc;
1624 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
1625 ClearPageError(page);
1626 last_block_in_bio = block_nr;
1630 zero_user_segment(page, 0, PAGE_SIZE);
1635 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1643 BUG_ON(pages && !list_empty(pages));
1645 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1649 static int f2fs_read_data_page(struct file *file, struct page *page)
1651 struct inode *inode = page->mapping->host;
1654 trace_f2fs_readpage(page, DATA);
1656 /* If the file has inline data, try to read it directly */
1657 if (f2fs_has_inline_data(inode))
1658 ret = f2fs_read_inline_data(inode, page);
1660 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
1664 static int f2fs_read_data_pages(struct file *file,
1665 struct address_space *mapping,
1666 struct list_head *pages, unsigned nr_pages)
1668 struct inode *inode = mapping->host;
1669 struct page *page = list_last_entry(pages, struct page, lru);
1671 trace_f2fs_readpages(inode, page, nr_pages);
1673 /* If the file has inline data, skip readpages */
1674 if (f2fs_has_inline_data(inode))
1677 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
1680 static int encrypt_one_page(struct f2fs_io_info *fio)
1682 struct inode *inode = fio->page->mapping->host;
1684 gfp_t gfp_flags = GFP_NOFS;
1686 if (!f2fs_encrypted_file(inode))
1689 /* wait for GCed page writeback via META_MAPPING */
1690 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
1693 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1694 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1695 if (IS_ERR(fio->encrypted_page)) {
1696 /* flush pending IOs and wait for a while in the ENOMEM case */
1697 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1698 f2fs_flush_merged_writes(fio->sbi);
1699 congestion_wait(BLK_RW_ASYNC, HZ/50);
1700 gfp_flags |= __GFP_NOFAIL;
1703 return PTR_ERR(fio->encrypted_page);
1706 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1708 if (PageUptodate(mpage))
1709 memcpy(page_address(mpage),
1710 page_address(fio->encrypted_page), PAGE_SIZE);
1711 f2fs_put_page(mpage, 1);
1716 static inline bool check_inplace_update_policy(struct inode *inode,
1717 struct f2fs_io_info *fio)
1719 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1720 unsigned int policy = SM_I(sbi)->ipu_policy;
1722 if (policy & (0x1 << F2FS_IPU_FORCE))
1724 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
1726 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1727 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1729 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
1730 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1734 * IPU for rewrite async pages
1736 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1737 fio && fio->op == REQ_OP_WRITE &&
1738 !(fio->op_flags & REQ_SYNC) &&
1739 !IS_ENCRYPTED(inode))
1742 /* this is only set during fdatasync */
1743 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1744 is_inode_flag_set(inode, FI_NEED_IPU))
1747 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1748 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1754 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1756 if (f2fs_is_pinned_file(inode))
1759 /* if this is cold file, we should overwrite to avoid fragmentation */
1760 if (file_is_cold(inode))
1763 return check_inplace_update_policy(inode, fio);
1766 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1768 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1770 if (test_opt(sbi, LFS))
1772 if (S_ISDIR(inode->i_mode))
1774 if (IS_NOQUOTA(inode))
1776 if (f2fs_is_atomic_file(inode))
1779 if (is_cold_data(fio->page))
1781 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1783 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1784 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1790 static inline bool need_inplace_update(struct f2fs_io_info *fio)
1792 struct inode *inode = fio->page->mapping->host;
1794 if (f2fs_should_update_outplace(inode, fio))
1797 return f2fs_should_update_inplace(inode, fio);
1800 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
1802 struct page *page = fio->page;
1803 struct inode *inode = page->mapping->host;
1804 struct dnode_of_data dn;
1805 struct extent_info ei = {0,0,0};
1806 struct node_info ni;
1807 bool ipu_force = false;
1810 set_new_dnode(&dn, inode, NULL, NULL, 0);
1811 if (need_inplace_update(fio) &&
1812 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1813 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1815 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1820 fio->need_lock = LOCK_DONE;
1824 /* Deadlock due to between page->lock and f2fs_lock_op */
1825 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1828 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
1832 fio->old_blkaddr = dn.data_blkaddr;
1834 /* This page is already truncated */
1835 if (fio->old_blkaddr == NULL_ADDR) {
1836 ClearPageUptodate(page);
1837 clear_cold_data(page);
1841 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1842 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1848 * If current allocation needs SSR,
1849 * it had better in-place writes for updated data.
1851 if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
1852 need_inplace_update(fio))) {
1853 err = encrypt_one_page(fio);
1857 set_page_writeback(page);
1858 ClearPageError(page);
1859 f2fs_put_dnode(&dn);
1860 if (fio->need_lock == LOCK_REQ)
1861 f2fs_unlock_op(fio->sbi);
1862 err = f2fs_inplace_write_data(fio);
1864 if (f2fs_encrypted_file(inode))
1865 fscrypt_pullback_bio_page(&fio->encrypted_page,
1867 if (PageWriteback(page))
1868 end_page_writeback(page);
1870 trace_f2fs_do_write_data_page(fio->page, IPU);
1871 set_inode_flag(inode, FI_UPDATE_WRITE);
1875 if (fio->need_lock == LOCK_RETRY) {
1876 if (!f2fs_trylock_op(fio->sbi)) {
1880 fio->need_lock = LOCK_REQ;
1883 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1887 fio->version = ni.version;
1889 err = encrypt_one_page(fio);
1893 set_page_writeback(page);
1894 ClearPageError(page);
1896 /* LFS mode write path */
1897 f2fs_outplace_write_data(&dn, fio);
1898 trace_f2fs_do_write_data_page(page, OPU);
1899 set_inode_flag(inode, FI_APPEND_WRITE);
1900 if (page->index == 0)
1901 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1903 f2fs_put_dnode(&dn);
1905 if (fio->need_lock == LOCK_REQ)
1906 f2fs_unlock_op(fio->sbi);
1910 static int __write_data_page(struct page *page, bool *submitted,
1911 struct writeback_control *wbc,
1912 enum iostat_type io_type)
1914 struct inode *inode = page->mapping->host;
1915 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1916 loff_t i_size = i_size_read(inode);
1917 const pgoff_t end_index = ((unsigned long long) i_size)
1919 loff_t psize = (page->index + 1) << PAGE_SHIFT;
1920 unsigned offset = 0;
1921 bool need_balance_fs = false;
1923 struct f2fs_io_info fio = {
1925 .ino = inode->i_ino,
1928 .op_flags = wbc_to_write_flags(wbc),
1929 .old_blkaddr = NULL_ADDR,
1931 .encrypted_page = NULL,
1933 .need_lock = LOCK_RETRY,
1938 trace_f2fs_writepage(page, DATA);
1940 /* we should bypass data pages to proceed the kworkder jobs */
1941 if (unlikely(f2fs_cp_error(sbi))) {
1942 mapping_set_error(page->mapping, -EIO);
1944 * don't drop any dirty dentry pages for keeping lastest
1945 * directory structure.
1947 if (S_ISDIR(inode->i_mode))
1952 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1955 if (page->index < end_index)
1959 * If the offset is out-of-range of file size,
1960 * this page does not have to be written to disk.
1962 offset = i_size & (PAGE_SIZE - 1);
1963 if ((page->index >= end_index + 1) || !offset)
1966 zero_user_segment(page, offset, PAGE_SIZE);
1968 if (f2fs_is_drop_cache(inode))
1970 /* we should not write 0'th page having journal header */
1971 if (f2fs_is_volatile_file(inode) && (!page->index ||
1972 (!wbc->for_reclaim &&
1973 f2fs_available_free_memory(sbi, BASE_CHECK))))
1976 /* Dentry blocks are controlled by checkpoint */
1977 if (S_ISDIR(inode->i_mode)) {
1978 fio.need_lock = LOCK_DONE;
1979 err = f2fs_do_write_data_page(&fio);
1983 if (!wbc->for_reclaim)
1984 need_balance_fs = true;
1985 else if (has_not_enough_free_secs(sbi, 0, 0))
1988 set_inode_flag(inode, FI_HOT_DATA);
1991 if (f2fs_has_inline_data(inode)) {
1992 err = f2fs_write_inline_data(inode, page);
1997 if (err == -EAGAIN) {
1998 err = f2fs_do_write_data_page(&fio);
1999 if (err == -EAGAIN) {
2000 fio.need_lock = LOCK_REQ;
2001 err = f2fs_do_write_data_page(&fio);
2006 file_set_keep_isize(inode);
2008 down_write(&F2FS_I(inode)->i_sem);
2009 if (F2FS_I(inode)->last_disk_size < psize)
2010 F2FS_I(inode)->last_disk_size = psize;
2011 up_write(&F2FS_I(inode)->i_sem);
2015 if (err && err != -ENOENT)
2019 inode_dec_dirty_pages(inode);
2021 ClearPageUptodate(page);
2022 clear_cold_data(page);
2025 if (wbc->for_reclaim) {
2026 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2027 clear_inode_flag(inode, FI_HOT_DATA);
2028 f2fs_remove_dirty_inode(inode);
2033 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode))
2034 f2fs_balance_fs(sbi, need_balance_fs);
2036 if (unlikely(f2fs_cp_error(sbi))) {
2037 f2fs_submit_merged_write(sbi, DATA);
2042 *submitted = fio.submitted;
2047 redirty_page_for_writepage(wbc, page);
2049 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2050 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2051 * file_write_and_wait_range() will see EIO error, which is critical
2052 * to return value of fsync() followed by atomic_write failure to user.
2054 if (!err || wbc->for_reclaim)
2055 return AOP_WRITEPAGE_ACTIVATE;
2060 static int f2fs_write_data_page(struct page *page,
2061 struct writeback_control *wbc)
2063 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
2067 * This function was copied from write_cche_pages from mm/page-writeback.c.
2068 * The major change is making write step of cold data page separately from
2069 * warm/hot data page.
2071 static int f2fs_write_cache_pages(struct address_space *mapping,
2072 struct writeback_control *wbc,
2073 enum iostat_type io_type)
2077 struct pagevec pvec;
2078 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2080 pgoff_t uninitialized_var(writeback_index);
2082 pgoff_t end; /* Inclusive */
2085 int range_whole = 0;
2089 pagevec_init(&pvec);
2091 if (get_dirty_pages(mapping->host) <=
2092 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2093 set_inode_flag(mapping->host, FI_HOT_DATA);
2095 clear_inode_flag(mapping->host, FI_HOT_DATA);
2097 if (wbc->range_cyclic) {
2098 writeback_index = mapping->writeback_index; /* prev offset */
2099 index = writeback_index;
2106 index = wbc->range_start >> PAGE_SHIFT;
2107 end = wbc->range_end >> PAGE_SHIFT;
2108 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2110 cycled = 1; /* ignore range_cyclic tests */
2112 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2113 tag = PAGECACHE_TAG_TOWRITE;
2115 tag = PAGECACHE_TAG_DIRTY;
2117 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2118 tag_pages_for_writeback(mapping, index, end);
2120 while (!done && (index <= end)) {
2123 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2128 for (i = 0; i < nr_pages; i++) {
2129 struct page *page = pvec.pages[i];
2130 bool submitted = false;
2132 /* give a priority to WB_SYNC threads */
2133 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2134 wbc->sync_mode == WB_SYNC_NONE) {
2139 done_index = page->index;
2143 if (unlikely(page->mapping != mapping)) {
2149 if (!PageDirty(page)) {
2150 /* someone wrote it for us */
2151 goto continue_unlock;
2154 if (PageWriteback(page)) {
2155 if (wbc->sync_mode != WB_SYNC_NONE)
2156 f2fs_wait_on_page_writeback(page,
2159 goto continue_unlock;
2162 if (!clear_page_dirty_for_io(page))
2163 goto continue_unlock;
2165 ret = __write_data_page(page, &submitted, wbc, io_type);
2166 if (unlikely(ret)) {
2168 * keep nr_to_write, since vfs uses this to
2169 * get # of written pages.
2171 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2175 } else if (ret == -EAGAIN) {
2177 if (wbc->sync_mode == WB_SYNC_ALL) {
2179 congestion_wait(BLK_RW_ASYNC,
2185 done_index = page->index + 1;
2188 } else if (submitted) {
2192 if (--wbc->nr_to_write <= 0 &&
2193 wbc->sync_mode == WB_SYNC_NONE) {
2198 pagevec_release(&pvec);
2202 if (!cycled && !done) {
2205 end = writeback_index - 1;
2208 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2209 mapping->writeback_index = done_index;
2212 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2218 static inline bool __should_serialize_io(struct inode *inode,
2219 struct writeback_control *wbc)
2221 if (!S_ISREG(inode->i_mode))
2223 if (IS_NOQUOTA(inode))
2225 if (wbc->sync_mode != WB_SYNC_ALL)
2227 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2232 static int __f2fs_write_data_pages(struct address_space *mapping,
2233 struct writeback_control *wbc,
2234 enum iostat_type io_type)
2236 struct inode *inode = mapping->host;
2237 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2238 struct blk_plug plug;
2240 bool locked = false;
2242 /* deal with chardevs and other special file */
2243 if (!mapping->a_ops->writepage)
2246 /* skip writing if there is no dirty page in this inode */
2247 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2250 /* during POR, we don't need to trigger writepage at all. */
2251 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2254 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2255 wbc->sync_mode == WB_SYNC_NONE &&
2256 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2257 f2fs_available_free_memory(sbi, DIRTY_DENTS))
2260 /* skip writing during file defragment */
2261 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2264 trace_f2fs_writepages(mapping->host, wbc, DATA);
2266 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2267 if (wbc->sync_mode == WB_SYNC_ALL)
2268 atomic_inc(&sbi->wb_sync_req[DATA]);
2269 else if (atomic_read(&sbi->wb_sync_req[DATA]))
2272 if (__should_serialize_io(inode, wbc)) {
2273 mutex_lock(&sbi->writepages);
2277 blk_start_plug(&plug);
2278 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2279 blk_finish_plug(&plug);
2282 mutex_unlock(&sbi->writepages);
2284 if (wbc->sync_mode == WB_SYNC_ALL)
2285 atomic_dec(&sbi->wb_sync_req[DATA]);
2287 * if some pages were truncated, we cannot guarantee its mapping->host
2288 * to detect pending bios.
2291 f2fs_remove_dirty_inode(inode);
2295 wbc->pages_skipped += get_dirty_pages(inode);
2296 trace_f2fs_writepages(mapping->host, wbc, DATA);
2300 static int f2fs_write_data_pages(struct address_space *mapping,
2301 struct writeback_control *wbc)
2303 struct inode *inode = mapping->host;
2305 return __f2fs_write_data_pages(mapping, wbc,
2306 F2FS_I(inode)->cp_task == current ?
2307 FS_CP_DATA_IO : FS_DATA_IO);
2310 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2312 struct inode *inode = mapping->host;
2313 loff_t i_size = i_size_read(inode);
2316 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2317 down_write(&F2FS_I(inode)->i_mmap_sem);
2319 truncate_pagecache(inode, i_size);
2320 if (!IS_NOQUOTA(inode))
2321 f2fs_truncate_blocks(inode, i_size, true);
2323 up_write(&F2FS_I(inode)->i_mmap_sem);
2324 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2328 static int prepare_write_begin(struct f2fs_sb_info *sbi,
2329 struct page *page, loff_t pos, unsigned len,
2330 block_t *blk_addr, bool *node_changed)
2332 struct inode *inode = page->mapping->host;
2333 pgoff_t index = page->index;
2334 struct dnode_of_data dn;
2336 bool locked = false;
2337 struct extent_info ei = {0,0,0};
2342 * we already allocated all the blocks, so we don't need to get
2343 * the block addresses when there is no need to fill the page.
2345 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2346 !is_inode_flag_set(inode, FI_NO_PREALLOC))
2349 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2350 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2351 flag = F2FS_GET_BLOCK_DEFAULT;
2353 flag = F2FS_GET_BLOCK_PRE_AIO;
2355 if (f2fs_has_inline_data(inode) ||
2356 (pos & PAGE_MASK) >= i_size_read(inode)) {
2357 __do_map_lock(sbi, flag, true);
2361 /* check inline_data */
2362 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2363 if (IS_ERR(ipage)) {
2364 err = PTR_ERR(ipage);
2368 set_new_dnode(&dn, inode, ipage, ipage, 0);
2370 if (f2fs_has_inline_data(inode)) {
2371 if (pos + len <= MAX_INLINE_DATA(inode)) {
2372 f2fs_do_read_inline_data(page, ipage);
2373 set_inode_flag(inode, FI_DATA_EXIST);
2375 set_inline_node(ipage);
2377 err = f2fs_convert_inline_page(&dn, page);
2380 if (dn.data_blkaddr == NULL_ADDR)
2381 err = f2fs_get_block(&dn, index);
2383 } else if (locked) {
2384 err = f2fs_get_block(&dn, index);
2386 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2387 dn.data_blkaddr = ei.blk + index - ei.fofs;
2390 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
2391 if (err || dn.data_blkaddr == NULL_ADDR) {
2392 f2fs_put_dnode(&dn);
2393 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2395 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
2402 /* convert_inline_page can make node_changed */
2403 *blk_addr = dn.data_blkaddr;
2404 *node_changed = dn.node_changed;
2406 f2fs_put_dnode(&dn);
2409 __do_map_lock(sbi, flag, false);
2413 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2414 loff_t pos, unsigned len, unsigned flags,
2415 struct page **pagep, void **fsdata)
2417 struct inode *inode = mapping->host;
2418 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2419 struct page *page = NULL;
2420 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
2421 bool need_balance = false, drop_atomic = false;
2422 block_t blkaddr = NULL_ADDR;
2425 trace_f2fs_write_begin(inode, pos, len, flags);
2427 err = f2fs_is_checkpoint_ready(sbi);
2431 if ((f2fs_is_atomic_file(inode) &&
2432 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2433 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2440 * We should check this at this moment to avoid deadlock on inode page
2441 * and #0 page. The locking rule for inline_data conversion should be:
2442 * lock_page(page #0) -> lock_page(inode_page)
2445 err = f2fs_convert_inline_inode(inode);
2451 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2452 * wait_for_stable_page. Will wait that below with our IO control.
2454 page = f2fs_pagecache_get_page(mapping, index,
2455 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
2463 err = prepare_write_begin(sbi, page, pos, len,
2464 &blkaddr, &need_balance);
2468 if (need_balance && !IS_NOQUOTA(inode) &&
2469 has_not_enough_free_secs(sbi, 0, 0)) {
2471 f2fs_balance_fs(sbi, true);
2473 if (page->mapping != mapping) {
2474 /* The page got truncated from under us */
2475 f2fs_put_page(page, 1);
2480 f2fs_wait_on_page_writeback(page, DATA, false, true);
2482 if (len == PAGE_SIZE || PageUptodate(page))
2485 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2486 zero_user_segment(page, len, PAGE_SIZE);
2490 if (blkaddr == NEW_ADDR) {
2491 zero_user_segment(page, 0, PAGE_SIZE);
2492 SetPageUptodate(page);
2494 err = f2fs_submit_page_read(inode, page, blkaddr);
2499 if (unlikely(page->mapping != mapping)) {
2500 f2fs_put_page(page, 1);
2503 if (unlikely(!PageUptodate(page))) {
2511 f2fs_put_page(page, 1);
2512 f2fs_write_failed(mapping, pos + len);
2514 f2fs_drop_inmem_pages_all(sbi, false);
2518 static int f2fs_write_end(struct file *file,
2519 struct address_space *mapping,
2520 loff_t pos, unsigned len, unsigned copied,
2521 struct page *page, void *fsdata)
2523 struct inode *inode = page->mapping->host;
2525 trace_f2fs_write_end(inode, pos, len, copied);
2528 * This should be come from len == PAGE_SIZE, and we expect copied
2529 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2530 * let generic_perform_write() try to copy data again through copied=0.
2532 if (!PageUptodate(page)) {
2533 if (unlikely(copied != len))
2536 SetPageUptodate(page);
2541 set_page_dirty(page);
2543 if (pos + copied > i_size_read(inode))
2544 f2fs_i_size_write(inode, pos + copied);
2546 f2fs_put_page(page, 1);
2547 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2551 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2554 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2555 unsigned blkbits = i_blkbits;
2556 unsigned blocksize_mask = (1 << blkbits) - 1;
2557 unsigned long align = offset | iov_iter_alignment(iter);
2558 struct block_device *bdev = inode->i_sb->s_bdev;
2560 if (align & blocksize_mask) {
2562 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2563 blocksize_mask = (1 << blkbits) - 1;
2564 if (align & blocksize_mask)
2571 static void f2fs_dio_end_io(struct bio *bio)
2573 struct f2fs_private_dio *dio = bio->bi_private;
2575 dec_page_count(F2FS_I_SB(dio->inode),
2576 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2578 bio->bi_private = dio->orig_private;
2579 bio->bi_end_io = dio->orig_end_io;
2586 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
2589 struct f2fs_private_dio *dio;
2590 bool write = (bio_op(bio) == REQ_OP_WRITE);
2592 dio = f2fs_kzalloc(F2FS_I_SB(inode),
2593 sizeof(struct f2fs_private_dio), GFP_NOFS);
2598 dio->orig_end_io = bio->bi_end_io;
2599 dio->orig_private = bio->bi_private;
2602 bio->bi_end_io = f2fs_dio_end_io;
2603 bio->bi_private = dio;
2605 inc_page_count(F2FS_I_SB(inode),
2606 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2611 bio->bi_status = BLK_STS_IOERR;
2615 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2617 struct address_space *mapping = iocb->ki_filp->f_mapping;
2618 struct inode *inode = mapping->host;
2619 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2620 struct f2fs_inode_info *fi = F2FS_I(inode);
2621 size_t count = iov_iter_count(iter);
2622 loff_t offset = iocb->ki_pos;
2623 int rw = iov_iter_rw(iter);
2625 enum rw_hint hint = iocb->ki_hint;
2626 int whint_mode = F2FS_OPTION(sbi).whint_mode;
2629 err = check_direct_IO(inode, iter, offset);
2631 return err < 0 ? err : 0;
2633 if (f2fs_force_buffered_io(inode, iocb, iter))
2636 do_opu = allow_outplace_dio(inode, iocb, iter);
2638 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
2640 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2641 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2643 if (iocb->ki_flags & IOCB_NOWAIT) {
2644 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
2645 iocb->ki_hint = hint;
2649 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
2650 up_read(&fi->i_gc_rwsem[rw]);
2651 iocb->ki_hint = hint;
2656 down_read(&fi->i_gc_rwsem[rw]);
2658 down_read(&fi->i_gc_rwsem[READ]);
2661 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
2662 iter, rw == WRITE ? get_data_block_dio_write :
2663 get_data_block_dio, NULL, f2fs_dio_submit_bio,
2664 DIO_LOCKING | DIO_SKIP_HOLES);
2667 up_read(&fi->i_gc_rwsem[READ]);
2669 up_read(&fi->i_gc_rwsem[rw]);
2672 if (whint_mode == WHINT_MODE_OFF)
2673 iocb->ki_hint = hint;
2675 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2678 set_inode_flag(inode, FI_UPDATE_WRITE);
2679 } else if (err < 0) {
2680 f2fs_write_failed(mapping, offset + count);
2685 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
2690 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2691 unsigned int length)
2693 struct inode *inode = page->mapping->host;
2694 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2696 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2697 (offset % PAGE_SIZE || length != PAGE_SIZE))
2700 if (PageDirty(page)) {
2701 if (inode->i_ino == F2FS_META_INO(sbi)) {
2702 dec_page_count(sbi, F2FS_DIRTY_META);
2703 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
2704 dec_page_count(sbi, F2FS_DIRTY_NODES);
2706 inode_dec_dirty_pages(inode);
2707 f2fs_remove_dirty_inode(inode);
2711 clear_cold_data(page);
2713 if (IS_ATOMIC_WRITTEN_PAGE(page))
2714 return f2fs_drop_inmem_page(inode, page);
2716 f2fs_clear_page_private(page);
2719 int f2fs_release_page(struct page *page, gfp_t wait)
2721 /* If this is dirty page, keep PagePrivate */
2722 if (PageDirty(page))
2725 /* This is atomic written page, keep Private */
2726 if (IS_ATOMIC_WRITTEN_PAGE(page))
2729 clear_cold_data(page);
2730 f2fs_clear_page_private(page);
2734 static int f2fs_set_data_page_dirty(struct page *page)
2736 struct address_space *mapping = page->mapping;
2737 struct inode *inode = mapping->host;
2739 trace_f2fs_set_page_dirty(page, DATA);
2741 if (!PageUptodate(page))
2742 SetPageUptodate(page);
2744 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
2745 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2746 f2fs_register_inmem_page(inode, page);
2750 * Previously, this page has been registered, we just
2756 if (!PageDirty(page)) {
2757 __set_page_dirty_nobuffers(page);
2758 f2fs_update_dirty_page(inode, page);
2764 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2766 struct inode *inode = mapping->host;
2768 if (f2fs_has_inline_data(inode))
2771 /* make sure allocating whole blocks */
2772 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2773 filemap_write_and_wait(mapping);
2775 return generic_block_bmap(mapping, block, get_data_block_bmap);
2778 #ifdef CONFIG_MIGRATION
2779 #include <linux/migrate.h>
2781 int f2fs_migrate_page(struct address_space *mapping,
2782 struct page *newpage, struct page *page, enum migrate_mode mode)
2784 int rc, extra_count;
2785 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2786 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2788 BUG_ON(PageWriteback(page));
2790 /* migrating an atomic written page is safe with the inmem_lock hold */
2791 if (atomic_written) {
2792 if (mode != MIGRATE_SYNC)
2794 if (!mutex_trylock(&fi->inmem_lock))
2798 /* one extra reference was held for atomic_write page */
2799 extra_count = atomic_written ? 1 : 0;
2800 rc = migrate_page_move_mapping(mapping, newpage,
2801 page, mode, extra_count);
2802 if (rc != MIGRATEPAGE_SUCCESS) {
2804 mutex_unlock(&fi->inmem_lock);
2808 if (atomic_written) {
2809 struct inmem_pages *cur;
2810 list_for_each_entry(cur, &fi->inmem_pages, list)
2811 if (cur->page == page) {
2812 cur->page = newpage;
2815 mutex_unlock(&fi->inmem_lock);
2820 if (PagePrivate(page)) {
2821 f2fs_set_page_private(newpage, page_private(page));
2822 f2fs_clear_page_private(page);
2825 if (mode != MIGRATE_SYNC_NO_COPY)
2826 migrate_page_copy(newpage, page);
2828 migrate_page_states(newpage, page);
2830 return MIGRATEPAGE_SUCCESS;
2834 const struct address_space_operations f2fs_dblock_aops = {
2835 .readpage = f2fs_read_data_page,
2836 .readpages = f2fs_read_data_pages,
2837 .writepage = f2fs_write_data_page,
2838 .writepages = f2fs_write_data_pages,
2839 .write_begin = f2fs_write_begin,
2840 .write_end = f2fs_write_end,
2841 .set_page_dirty = f2fs_set_data_page_dirty,
2842 .invalidatepage = f2fs_invalidate_page,
2843 .releasepage = f2fs_release_page,
2844 .direct_IO = f2fs_direct_IO,
2846 #ifdef CONFIG_MIGRATION
2847 .migratepage = f2fs_migrate_page,
2851 void f2fs_clear_page_cache_dirty_tag(struct page *page)
2853 struct address_space *mapping = page_mapping(page);
2854 unsigned long flags;
2856 xa_lock_irqsave(&mapping->i_pages, flags);
2857 __xa_clear_mark(&mapping->i_pages, page_index(page),
2858 PAGECACHE_TAG_DIRTY);
2859 xa_unlock_irqrestore(&mapping->i_pages, flags);
2862 int __init f2fs_init_post_read_processing(void)
2864 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2865 if (!bio_post_read_ctx_cache)
2867 bio_post_read_ctx_pool =
2868 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2869 bio_post_read_ctx_cache);
2870 if (!bio_post_read_ctx_pool)
2871 goto fail_free_cache;
2875 kmem_cache_destroy(bio_post_read_ctx_cache);
2880 void __exit f2fs_destroy_post_read_processing(void)
2882 mempool_destroy(bio_post_read_ctx_pool);
2883 kmem_cache_destroy(bio_post_read_ctx_cache);