4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/mpage.h>
15 #include <linux/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/pagevec.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
21 #include <linux/uio.h>
22 #include <linux/cleancache.h>
23 #include <linux/sched/signal.h>
29 #include <trace/events/f2fs.h>
31 #define NUM_PREALLOC_POST_READ_CTXS 128
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static mempool_t *bio_post_read_ctx_pool;
36 static bool __is_cp_guaranteed(struct page *page)
38 struct address_space *mapping = page->mapping;
40 struct f2fs_sb_info *sbi;
45 inode = mapping->host;
46 sbi = F2FS_I_SB(inode);
48 if (inode->i_ino == F2FS_META_INO(sbi) ||
49 inode->i_ino == F2FS_NODE_INO(sbi) ||
50 S_ISDIR(inode->i_mode) ||
51 (S_ISREG(inode->i_mode) &&
52 is_inode_flag_set(inode, FI_ATOMIC_FILE)) ||
58 /* postprocessing steps for read bios */
59 enum bio_post_read_step {
64 struct bio_post_read_ctx {
66 struct work_struct work;
67 unsigned int cur_step;
68 unsigned int enabled_steps;
71 static void __read_end_io(struct bio *bio)
77 bio_for_each_segment_all(bv, bio, i) {
80 /* PG_error was set if any post_read step failed */
81 if (bio->bi_status || PageError(page)) {
82 ClearPageUptodate(page);
85 SetPageUptodate(page);
90 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
94 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
96 static void decrypt_work(struct work_struct *work)
98 struct bio_post_read_ctx *ctx =
99 container_of(work, struct bio_post_read_ctx, work);
101 fscrypt_decrypt_bio(ctx->bio);
103 bio_post_read_processing(ctx);
106 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
108 switch (++ctx->cur_step) {
110 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
111 INIT_WORK(&ctx->work, decrypt_work);
112 fscrypt_enqueue_decrypt_work(&ctx->work);
118 __read_end_io(ctx->bio);
122 static bool f2fs_bio_post_read_required(struct bio *bio)
124 return bio->bi_private && !bio->bi_status;
127 static void f2fs_read_end_io(struct bio *bio)
129 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)), FAULT_IO)) {
130 f2fs_show_injection_info(FAULT_IO);
131 bio->bi_status = BLK_STS_IOERR;
134 if (f2fs_bio_post_read_required(bio)) {
135 struct bio_post_read_ctx *ctx = bio->bi_private;
137 ctx->cur_step = STEP_INITIAL;
138 bio_post_read_processing(ctx);
145 static void f2fs_write_end_io(struct bio *bio)
147 struct f2fs_sb_info *sbi = bio->bi_private;
148 struct bio_vec *bvec;
151 bio_for_each_segment_all(bvec, bio, i) {
152 struct page *page = bvec->bv_page;
153 enum count_type type = WB_DATA_TYPE(page);
155 if (IS_DUMMY_WRITTEN_PAGE(page)) {
156 set_page_private(page, (unsigned long)NULL);
157 ClearPagePrivate(page);
159 mempool_free(page, sbi->write_io_dummy);
161 if (unlikely(bio->bi_status))
162 f2fs_stop_checkpoint(sbi, true);
166 fscrypt_pullback_bio_page(&page, true);
168 if (unlikely(bio->bi_status)) {
169 mapping_set_error(page->mapping, -EIO);
170 if (type == F2FS_WB_CP_DATA)
171 f2fs_stop_checkpoint(sbi, true);
174 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
175 page->index != nid_of_node(page));
177 dec_page_count(sbi, type);
178 if (f2fs_in_warm_node_list(sbi, page))
179 f2fs_del_fsync_node_entry(sbi, page);
180 clear_cold_data(page);
181 end_page_writeback(page);
183 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
184 wq_has_sleeper(&sbi->cp_wait))
185 wake_up(&sbi->cp_wait);
191 * Return true, if pre_bio's bdev is same as its target device.
193 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
194 block_t blk_addr, struct bio *bio)
196 struct block_device *bdev = sbi->sb->s_bdev;
199 for (i = 0; i < sbi->s_ndevs; i++) {
200 if (FDEV(i).start_blk <= blk_addr &&
201 FDEV(i).end_blk >= blk_addr) {
202 blk_addr -= FDEV(i).start_blk;
208 bio_set_dev(bio, bdev);
209 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
214 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
218 for (i = 0; i < sbi->s_ndevs; i++)
219 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
224 static bool __same_bdev(struct f2fs_sb_info *sbi,
225 block_t blk_addr, struct bio *bio)
227 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
228 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
232 * Low-level block read/write IO operations.
234 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
235 struct writeback_control *wbc,
236 int npages, bool is_read,
237 enum page_type type, enum temp_type temp)
241 bio = f2fs_bio_alloc(sbi, npages, true);
243 f2fs_target_device(sbi, blk_addr, bio);
245 bio->bi_end_io = f2fs_read_end_io;
246 bio->bi_private = NULL;
248 bio->bi_end_io = f2fs_write_end_io;
249 bio->bi_private = sbi;
250 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
253 wbc_init_bio(wbc, bio);
258 static inline void __submit_bio(struct f2fs_sb_info *sbi,
259 struct bio *bio, enum page_type type)
261 if (!is_read_io(bio_op(bio))) {
264 if (type != DATA && type != NODE)
267 if (test_opt(sbi, LFS) && current->plug)
268 blk_finish_plug(current->plug);
270 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
271 start %= F2FS_IO_SIZE(sbi);
276 /* fill dummy pages */
277 for (; start < F2FS_IO_SIZE(sbi); start++) {
279 mempool_alloc(sbi->write_io_dummy,
280 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
281 f2fs_bug_on(sbi, !page);
283 SetPagePrivate(page);
284 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
286 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
290 * In the NODE case, we lose next block address chain. So, we
291 * need to do checkpoint in f2fs_sync_file.
294 set_sbi_flag(sbi, SBI_NEED_CP);
297 if (is_read_io(bio_op(bio)))
298 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
300 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
304 static void __submit_merged_bio(struct f2fs_bio_info *io)
306 struct f2fs_io_info *fio = &io->fio;
311 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
313 if (is_read_io(fio->op))
314 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
316 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
318 __submit_bio(io->sbi, io->bio, fio->type);
322 static bool __has_merged_page(struct f2fs_bio_info *io,
323 struct inode *inode, nid_t ino, pgoff_t idx)
325 struct bio_vec *bvec;
335 bio_for_each_segment_all(bvec, io->bio, i) {
337 if (bvec->bv_page->mapping)
338 target = bvec->bv_page;
340 target = fscrypt_control_page(bvec->bv_page);
342 if (idx != target->index)
345 if (inode && inode == target->mapping->host)
347 if (ino && ino == ino_of_node(target))
354 static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
355 nid_t ino, pgoff_t idx, enum page_type type)
357 enum page_type btype = PAGE_TYPE_OF_BIO(type);
359 struct f2fs_bio_info *io;
362 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
363 io = sbi->write_io[btype] + temp;
365 down_read(&io->io_rwsem);
366 ret = __has_merged_page(io, inode, ino, idx);
367 up_read(&io->io_rwsem);
369 /* TODO: use HOT temp only for meta pages now. */
370 if (ret || btype == META)
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, nid_t ino, pgoff_t idx,
398 enum page_type type, bool force)
402 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
405 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
407 __f2fs_submit_merged_write(sbi, type, temp);
409 /* TODO: use HOT temp only for meta pages now. */
415 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
417 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
420 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
421 struct inode *inode, nid_t ino, pgoff_t idx,
424 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
427 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
429 f2fs_submit_merged_write(sbi, DATA);
430 f2fs_submit_merged_write(sbi, NODE);
431 f2fs_submit_merged_write(sbi, META);
435 * Fill the locked page with data located in the block address.
436 * A caller needs to unlock the page on failure.
438 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
441 struct page *page = fio->encrypted_page ?
442 fio->encrypted_page : fio->page;
444 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
445 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
448 trace_f2fs_submit_page_bio(page, fio);
449 f2fs_trace_ios(fio, 0);
451 /* Allocate a new bio */
452 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
453 1, is_read_io(fio->op), fio->type, fio->temp);
455 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
459 bio_set_op_attrs(bio, fio->op, fio->op_flags);
461 __submit_bio(fio->sbi, bio, fio->type);
463 if (!is_read_io(fio->op))
464 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
468 void f2fs_submit_page_write(struct f2fs_io_info *fio)
470 struct f2fs_sb_info *sbi = fio->sbi;
471 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
472 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
473 struct page *bio_page;
475 f2fs_bug_on(sbi, is_read_io(fio->op));
477 down_write(&io->io_rwsem);
480 spin_lock(&io->io_lock);
481 if (list_empty(&io->io_list)) {
482 spin_unlock(&io->io_lock);
485 fio = list_first_entry(&io->io_list,
486 struct f2fs_io_info, list);
487 list_del(&fio->list);
488 spin_unlock(&io->io_lock);
491 if (__is_valid_data_blkaddr(fio->old_blkaddr))
492 verify_block_addr(fio, fio->old_blkaddr);
493 verify_block_addr(fio, fio->new_blkaddr);
495 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
497 /* set submitted = true as a return value */
498 fio->submitted = true;
500 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
502 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
503 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
504 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
505 __submit_merged_bio(io);
507 if (io->bio == NULL) {
508 if ((fio->type == DATA || fio->type == NODE) &&
509 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
510 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
514 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
515 BIO_MAX_PAGES, false,
516 fio->type, fio->temp);
520 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
521 __submit_merged_bio(io);
526 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
528 io->last_block_in_bio = fio->new_blkaddr;
529 f2fs_trace_ios(fio, 0);
531 trace_f2fs_submit_page_write(fio->page, fio);
536 up_write(&io->io_rwsem);
539 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
540 unsigned nr_pages, unsigned op_flag)
542 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
544 struct bio_post_read_ctx *ctx;
545 unsigned int post_read_steps = 0;
547 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
548 return ERR_PTR(-EFAULT);
550 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
552 return ERR_PTR(-ENOMEM);
553 f2fs_target_device(sbi, blkaddr, bio);
554 bio->bi_end_io = f2fs_read_end_io;
555 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
557 if (f2fs_encrypted_file(inode))
558 post_read_steps |= 1 << STEP_DECRYPT;
559 if (post_read_steps) {
560 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
563 return ERR_PTR(-ENOMEM);
566 ctx->enabled_steps = post_read_steps;
567 bio->bi_private = ctx;
569 /* wait the page to be moved by cleaning */
570 f2fs_wait_on_block_writeback(sbi, blkaddr);
576 /* This can handle encryption stuffs */
577 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
580 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
585 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
589 __submit_bio(F2FS_I_SB(inode), bio, DATA);
593 static void __set_data_blkaddr(struct dnode_of_data *dn)
595 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
599 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
600 base = get_extra_isize(dn->inode);
602 /* Get physical address of data block */
603 addr_array = blkaddr_in_node(rn);
604 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
608 * Lock ordering for the change of data block address:
611 * update block addresses in the node page
613 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
615 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
616 __set_data_blkaddr(dn);
617 if (set_page_dirty(dn->node_page))
618 dn->node_changed = true;
621 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
623 dn->data_blkaddr = blkaddr;
624 f2fs_set_data_blkaddr(dn);
625 f2fs_update_extent_cache(dn);
628 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
629 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
631 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
637 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
639 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
642 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
643 dn->ofs_in_node, count);
645 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
647 for (; count > 0; dn->ofs_in_node++) {
648 block_t blkaddr = datablock_addr(dn->inode,
649 dn->node_page, dn->ofs_in_node);
650 if (blkaddr == NULL_ADDR) {
651 dn->data_blkaddr = NEW_ADDR;
652 __set_data_blkaddr(dn);
657 if (set_page_dirty(dn->node_page))
658 dn->node_changed = true;
662 /* Should keep dn->ofs_in_node unchanged */
663 int f2fs_reserve_new_block(struct dnode_of_data *dn)
665 unsigned int ofs_in_node = dn->ofs_in_node;
668 ret = f2fs_reserve_new_blocks(dn, 1);
669 dn->ofs_in_node = ofs_in_node;
673 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
675 bool need_put = dn->inode_page ? false : true;
678 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
682 if (dn->data_blkaddr == NULL_ADDR)
683 err = f2fs_reserve_new_block(dn);
689 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
691 struct extent_info ei = {0,0,0};
692 struct inode *inode = dn->inode;
694 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
695 dn->data_blkaddr = ei.blk + index - ei.fofs;
699 return f2fs_reserve_block(dn, index);
702 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
703 int op_flags, bool for_write)
705 struct address_space *mapping = inode->i_mapping;
706 struct dnode_of_data dn;
708 struct extent_info ei = {0,0,0};
711 page = f2fs_grab_cache_page(mapping, index, for_write);
713 return ERR_PTR(-ENOMEM);
715 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
716 dn.data_blkaddr = ei.blk + index - ei.fofs;
720 set_new_dnode(&dn, inode, NULL, NULL, 0);
721 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
726 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
731 if (PageUptodate(page)) {
737 * A new dentry page is allocated but not able to be written, since its
738 * new inode page couldn't be allocated due to -ENOSPC.
739 * In such the case, its blkaddr can be remained as NEW_ADDR.
740 * see, f2fs_add_link -> f2fs_get_new_data_page ->
741 * f2fs_init_inode_metadata.
743 if (dn.data_blkaddr == NEW_ADDR) {
744 zero_user_segment(page, 0, PAGE_SIZE);
745 if (!PageUptodate(page))
746 SetPageUptodate(page);
751 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
757 f2fs_put_page(page, 1);
761 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
763 struct address_space *mapping = inode->i_mapping;
766 page = find_get_page(mapping, index);
767 if (page && PageUptodate(page))
769 f2fs_put_page(page, 0);
771 page = f2fs_get_read_data_page(inode, index, 0, false);
775 if (PageUptodate(page))
778 wait_on_page_locked(page);
779 if (unlikely(!PageUptodate(page))) {
780 f2fs_put_page(page, 0);
781 return ERR_PTR(-EIO);
787 * If it tries to access a hole, return an error.
788 * Because, the callers, functions in dir.c and GC, should be able to know
789 * whether this page exists or not.
791 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
794 struct address_space *mapping = inode->i_mapping;
797 page = f2fs_get_read_data_page(inode, index, 0, for_write);
801 /* wait for read completion */
803 if (unlikely(page->mapping != mapping)) {
804 f2fs_put_page(page, 1);
807 if (unlikely(!PageUptodate(page))) {
808 f2fs_put_page(page, 1);
809 return ERR_PTR(-EIO);
815 * Caller ensures that this data page is never allocated.
816 * A new zero-filled data page is allocated in the page cache.
818 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
820 * Note that, ipage is set only by make_empty_dir, and if any error occur,
821 * ipage should be released by this function.
823 struct page *f2fs_get_new_data_page(struct inode *inode,
824 struct page *ipage, pgoff_t index, bool new_i_size)
826 struct address_space *mapping = inode->i_mapping;
828 struct dnode_of_data dn;
831 page = f2fs_grab_cache_page(mapping, index, true);
834 * before exiting, we should make sure ipage will be released
835 * if any error occur.
837 f2fs_put_page(ipage, 1);
838 return ERR_PTR(-ENOMEM);
841 set_new_dnode(&dn, inode, ipage, NULL, 0);
842 err = f2fs_reserve_block(&dn, index);
844 f2fs_put_page(page, 1);
850 if (PageUptodate(page))
853 if (dn.data_blkaddr == NEW_ADDR) {
854 zero_user_segment(page, 0, PAGE_SIZE);
855 if (!PageUptodate(page))
856 SetPageUptodate(page);
858 f2fs_put_page(page, 1);
860 /* if ipage exists, blkaddr should be NEW_ADDR */
861 f2fs_bug_on(F2FS_I_SB(inode), ipage);
862 page = f2fs_get_lock_data_page(inode, index, true);
867 if (new_i_size && i_size_read(inode) <
868 ((loff_t)(index + 1) << PAGE_SHIFT))
869 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
873 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
875 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
876 struct f2fs_summary sum;
883 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
886 err = f2fs_get_node_info(sbi, dn->nid, &ni);
890 dn->data_blkaddr = datablock_addr(dn->inode,
891 dn->node_page, dn->ofs_in_node);
892 if (dn->data_blkaddr == NEW_ADDR)
895 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
899 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
900 old_blkaddr = dn->data_blkaddr;
901 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
902 &sum, seg_type, NULL, false);
903 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
904 invalidate_mapping_pages(META_MAPPING(sbi),
905 old_blkaddr, old_blkaddr);
906 f2fs_set_data_blkaddr(dn);
909 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
911 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
912 f2fs_i_size_write(dn->inode,
913 ((loff_t)(fofs + 1) << PAGE_SHIFT));
917 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
919 struct inode *inode = file_inode(iocb->ki_filp);
920 struct f2fs_map_blocks map;
923 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
925 /* convert inline data for Direct I/O*/
927 err = f2fs_convert_inline_inode(inode);
932 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
935 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
936 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
937 if (map.m_len > map.m_lblk)
938 map.m_len -= map.m_lblk;
942 map.m_next_pgofs = NULL;
943 map.m_next_extent = NULL;
944 map.m_seg_type = NO_CHECK_TYPE;
947 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
948 flag = f2fs_force_buffered_io(inode, WRITE) ?
949 F2FS_GET_BLOCK_PRE_AIO :
950 F2FS_GET_BLOCK_PRE_DIO;
953 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
954 err = f2fs_convert_inline_inode(inode);
958 if (f2fs_has_inline_data(inode))
961 flag = F2FS_GET_BLOCK_PRE_AIO;
964 err = f2fs_map_blocks(inode, &map, 1, flag);
965 if (map.m_len > 0 && err == -ENOSPC) {
967 set_inode_flag(inode, FI_NO_PREALLOC);
973 static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
975 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
977 down_read(&sbi->node_change);
979 up_read(&sbi->node_change);
989 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
990 * f2fs_map_blocks structure.
991 * If original data blocks are allocated, then give them to blockdev.
993 * a. preallocate requested block addresses
994 * b. do not use extent cache for better performance
995 * c. give the block addresses to blockdev
997 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
998 int create, int flag)
1000 unsigned int maxblocks = map->m_len;
1001 struct dnode_of_data dn;
1002 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1003 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
1004 pgoff_t pgofs, end_offset, end;
1005 int err = 0, ofs = 1;
1006 unsigned int ofs_in_node, last_ofs_in_node;
1008 struct extent_info ei = {0,0,0};
1010 unsigned int start_pgofs;
1018 /* it only supports block size == page size */
1019 pgofs = (pgoff_t)map->m_lblk;
1020 end = pgofs + maxblocks;
1022 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1023 map->m_pblk = ei.blk + pgofs - ei.fofs;
1024 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1025 map->m_flags = F2FS_MAP_MAPPED;
1026 if (map->m_next_extent)
1027 *map->m_next_extent = pgofs + map->m_len;
1033 __do_map_lock(sbi, flag, true);
1035 /* When reading holes, we need its node page */
1036 set_new_dnode(&dn, inode, NULL, NULL, 0);
1037 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1039 if (flag == F2FS_GET_BLOCK_BMAP)
1041 if (err == -ENOENT) {
1043 if (map->m_next_pgofs)
1044 *map->m_next_pgofs =
1045 f2fs_get_next_page_offset(&dn, pgofs);
1046 if (map->m_next_extent)
1047 *map->m_next_extent =
1048 f2fs_get_next_page_offset(&dn, pgofs);
1053 start_pgofs = pgofs;
1055 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1056 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1059 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1061 if (__is_valid_data_blkaddr(blkaddr) &&
1062 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1067 if (!is_valid_data_blkaddr(sbi, blkaddr)) {
1069 if (unlikely(f2fs_cp_error(sbi))) {
1073 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1074 if (blkaddr == NULL_ADDR) {
1076 last_ofs_in_node = dn.ofs_in_node;
1079 err = __allocate_data_block(&dn,
1082 set_inode_flag(inode, FI_APPEND_WRITE);
1086 map->m_flags |= F2FS_MAP_NEW;
1087 blkaddr = dn.data_blkaddr;
1089 if (flag == F2FS_GET_BLOCK_BMAP) {
1093 if (flag == F2FS_GET_BLOCK_PRECACHE)
1095 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1096 blkaddr == NULL_ADDR) {
1097 if (map->m_next_pgofs)
1098 *map->m_next_pgofs = pgofs + 1;
1101 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1102 /* for defragment case */
1103 if (map->m_next_pgofs)
1104 *map->m_next_pgofs = pgofs + 1;
1110 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1113 if (map->m_len == 0) {
1114 /* preallocated unwritten block should be mapped for fiemap. */
1115 if (blkaddr == NEW_ADDR)
1116 map->m_flags |= F2FS_MAP_UNWRITTEN;
1117 map->m_flags |= F2FS_MAP_MAPPED;
1119 map->m_pblk = blkaddr;
1121 } else if ((map->m_pblk != NEW_ADDR &&
1122 blkaddr == (map->m_pblk + ofs)) ||
1123 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1124 flag == F2FS_GET_BLOCK_PRE_DIO) {
1135 /* preallocate blocks in batch for one dnode page */
1136 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1137 (pgofs == end || dn.ofs_in_node == end_offset)) {
1139 dn.ofs_in_node = ofs_in_node;
1140 err = f2fs_reserve_new_blocks(&dn, prealloc);
1144 map->m_len += dn.ofs_in_node - ofs_in_node;
1145 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1149 dn.ofs_in_node = end_offset;
1154 else if (dn.ofs_in_node < end_offset)
1157 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1158 if (map->m_flags & F2FS_MAP_MAPPED) {
1159 unsigned int ofs = start_pgofs - map->m_lblk;
1161 f2fs_update_extent_cache_range(&dn,
1162 start_pgofs, map->m_pblk + ofs,
1167 f2fs_put_dnode(&dn);
1170 __do_map_lock(sbi, flag, false);
1171 f2fs_balance_fs(sbi, dn.node_changed);
1176 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1177 if (map->m_flags & F2FS_MAP_MAPPED) {
1178 unsigned int ofs = start_pgofs - map->m_lblk;
1180 f2fs_update_extent_cache_range(&dn,
1181 start_pgofs, map->m_pblk + ofs,
1184 if (map->m_next_extent)
1185 *map->m_next_extent = pgofs + 1;
1187 f2fs_put_dnode(&dn);
1190 __do_map_lock(sbi, flag, false);
1191 f2fs_balance_fs(sbi, dn.node_changed);
1194 trace_f2fs_map_blocks(inode, map, err);
1198 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1200 struct f2fs_map_blocks map;
1204 if (pos + len > i_size_read(inode))
1207 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1208 map.m_next_pgofs = NULL;
1209 map.m_next_extent = NULL;
1210 map.m_seg_type = NO_CHECK_TYPE;
1211 last_lblk = F2FS_BLK_ALIGN(pos + len);
1213 while (map.m_lblk < last_lblk) {
1214 map.m_len = last_lblk - map.m_lblk;
1215 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1216 if (err || map.m_len == 0)
1218 map.m_lblk += map.m_len;
1223 static int __get_data_block(struct inode *inode, sector_t iblock,
1224 struct buffer_head *bh, int create, int flag,
1225 pgoff_t *next_pgofs, int seg_type)
1227 struct f2fs_map_blocks map;
1230 map.m_lblk = iblock;
1231 map.m_len = bh->b_size >> inode->i_blkbits;
1232 map.m_next_pgofs = next_pgofs;
1233 map.m_next_extent = NULL;
1234 map.m_seg_type = seg_type;
1236 err = f2fs_map_blocks(inode, &map, create, flag);
1238 map_bh(bh, inode->i_sb, map.m_pblk);
1239 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1240 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1245 static int get_data_block(struct inode *inode, sector_t iblock,
1246 struct buffer_head *bh_result, int create, int flag,
1247 pgoff_t *next_pgofs)
1249 return __get_data_block(inode, iblock, bh_result, create,
1254 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1255 struct buffer_head *bh_result, int create)
1257 return __get_data_block(inode, iblock, bh_result, create,
1258 F2FS_GET_BLOCK_DEFAULT, NULL,
1259 f2fs_rw_hint_to_seg_type(
1260 inode->i_write_hint));
1263 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1264 struct buffer_head *bh_result, int create)
1266 /* Block number less than F2FS MAX BLOCKS */
1267 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1270 return __get_data_block(inode, iblock, bh_result, create,
1271 F2FS_GET_BLOCK_BMAP, NULL,
1275 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1277 return (offset >> inode->i_blkbits);
1280 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1282 return (blk << inode->i_blkbits);
1285 static int f2fs_xattr_fiemap(struct inode *inode,
1286 struct fiemap_extent_info *fieinfo)
1288 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1290 struct node_info ni;
1291 __u64 phys = 0, len;
1293 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1296 if (f2fs_has_inline_xattr(inode)) {
1299 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1300 inode->i_ino, false);
1304 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1306 f2fs_put_page(page, 1);
1310 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1311 offset = offsetof(struct f2fs_inode, i_addr) +
1312 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1313 get_inline_xattr_addrs(inode));
1316 len = inline_xattr_size(inode);
1318 f2fs_put_page(page, 1);
1320 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1323 flags |= FIEMAP_EXTENT_LAST;
1325 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1326 if (err || err == 1)
1331 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1335 err = f2fs_get_node_info(sbi, xnid, &ni);
1337 f2fs_put_page(page, 1);
1341 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1342 len = inode->i_sb->s_blocksize;
1344 f2fs_put_page(page, 1);
1346 flags = FIEMAP_EXTENT_LAST;
1350 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1352 return (err < 0 ? err : 0);
1355 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1358 struct buffer_head map_bh;
1359 sector_t start_blk, last_blk;
1361 u64 logical = 0, phys = 0, size = 0;
1365 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1366 ret = f2fs_precache_extents(inode);
1371 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1377 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1378 ret = f2fs_xattr_fiemap(inode, fieinfo);
1382 if (f2fs_has_inline_data(inode)) {
1383 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1388 if (logical_to_blk(inode, len) == 0)
1389 len = blk_to_logical(inode, 1);
1391 start_blk = logical_to_blk(inode, start);
1392 last_blk = logical_to_blk(inode, start + len - 1);
1395 memset(&map_bh, 0, sizeof(struct buffer_head));
1396 map_bh.b_size = len;
1398 ret = get_data_block(inode, start_blk, &map_bh, 0,
1399 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1404 if (!buffer_mapped(&map_bh)) {
1405 start_blk = next_pgofs;
1407 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1408 F2FS_I_SB(inode)->max_file_blocks))
1411 flags |= FIEMAP_EXTENT_LAST;
1415 if (f2fs_encrypted_inode(inode))
1416 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1418 ret = fiemap_fill_next_extent(fieinfo, logical,
1422 if (start_blk > last_blk || ret)
1425 logical = blk_to_logical(inode, start_blk);
1426 phys = blk_to_logical(inode, map_bh.b_blocknr);
1427 size = map_bh.b_size;
1429 if (buffer_unwritten(&map_bh))
1430 flags = FIEMAP_EXTENT_UNWRITTEN;
1432 start_blk += logical_to_blk(inode, size);
1436 if (fatal_signal_pending(current))
1444 inode_unlock(inode);
1449 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1450 * Major change was from block_size == page_size in f2fs by default.
1452 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1453 * this function ever deviates from doing just read-ahead, it should either
1454 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1457 static int f2fs_mpage_readpages(struct address_space *mapping,
1458 struct list_head *pages, struct page *page,
1459 unsigned nr_pages, bool is_readahead)
1461 struct bio *bio = NULL;
1462 sector_t last_block_in_bio = 0;
1463 struct inode *inode = mapping->host;
1464 const unsigned blkbits = inode->i_blkbits;
1465 const unsigned blocksize = 1 << blkbits;
1466 sector_t block_in_file;
1467 sector_t last_block;
1468 sector_t last_block_in_file;
1470 struct f2fs_map_blocks map;
1476 map.m_next_pgofs = NULL;
1477 map.m_next_extent = NULL;
1478 map.m_seg_type = NO_CHECK_TYPE;
1480 for (; nr_pages; nr_pages--) {
1482 page = list_last_entry(pages, struct page, lru);
1484 prefetchw(&page->flags);
1485 list_del(&page->lru);
1486 if (add_to_page_cache_lru(page, mapping,
1488 readahead_gfp_mask(mapping)))
1492 block_in_file = (sector_t)page->index;
1493 last_block = block_in_file + nr_pages;
1494 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1496 if (last_block > last_block_in_file)
1497 last_block = last_block_in_file;
1500 * Map blocks using the previous result first.
1502 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1503 block_in_file > map.m_lblk &&
1504 block_in_file < (map.m_lblk + map.m_len))
1508 * Then do more f2fs_map_blocks() calls until we are
1509 * done with this page.
1513 if (block_in_file < last_block) {
1514 map.m_lblk = block_in_file;
1515 map.m_len = last_block - block_in_file;
1517 if (f2fs_map_blocks(inode, &map, 0,
1518 F2FS_GET_BLOCK_DEFAULT))
1519 goto set_error_page;
1522 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1523 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1524 SetPageMappedToDisk(page);
1526 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1527 SetPageUptodate(page);
1531 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1533 goto set_error_page;
1535 zero_user_segment(page, 0, PAGE_SIZE);
1536 if (!PageUptodate(page))
1537 SetPageUptodate(page);
1543 * This page will go to BIO. Do we need to send this
1546 if (bio && (last_block_in_bio != block_nr - 1 ||
1547 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
1549 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1553 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1554 is_readahead ? REQ_RAHEAD : 0);
1557 goto set_error_page;
1561 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1562 goto submit_and_realloc;
1564 last_block_in_bio = block_nr;
1568 zero_user_segment(page, 0, PAGE_SIZE);
1573 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1581 BUG_ON(pages && !list_empty(pages));
1583 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1587 static int f2fs_read_data_page(struct file *file, struct page *page)
1589 struct inode *inode = page->mapping->host;
1592 trace_f2fs_readpage(page, DATA);
1594 /* If the file has inline data, try to read it directly */
1595 if (f2fs_has_inline_data(inode))
1596 ret = f2fs_read_inline_data(inode, page);
1598 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
1602 static int f2fs_read_data_pages(struct file *file,
1603 struct address_space *mapping,
1604 struct list_head *pages, unsigned nr_pages)
1606 struct inode *inode = mapping->host;
1607 struct page *page = list_last_entry(pages, struct page, lru);
1609 trace_f2fs_readpages(inode, page, nr_pages);
1611 /* If the file has inline data, skip readpages */
1612 if (f2fs_has_inline_data(inode))
1615 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
1618 static int encrypt_one_page(struct f2fs_io_info *fio)
1620 struct inode *inode = fio->page->mapping->host;
1622 gfp_t gfp_flags = GFP_NOFS;
1624 if (!f2fs_encrypted_file(inode))
1627 /* wait for GCed page writeback via META_MAPPING */
1628 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
1631 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1632 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1633 if (IS_ERR(fio->encrypted_page)) {
1634 /* flush pending IOs and wait for a while in the ENOMEM case */
1635 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1636 f2fs_flush_merged_writes(fio->sbi);
1637 congestion_wait(BLK_RW_ASYNC, HZ/50);
1638 gfp_flags |= __GFP_NOFAIL;
1641 return PTR_ERR(fio->encrypted_page);
1644 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1646 if (PageUptodate(mpage))
1647 memcpy(page_address(mpage),
1648 page_address(fio->encrypted_page), PAGE_SIZE);
1649 f2fs_put_page(mpage, 1);
1654 static inline bool check_inplace_update_policy(struct inode *inode,
1655 struct f2fs_io_info *fio)
1657 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1658 unsigned int policy = SM_I(sbi)->ipu_policy;
1660 if (policy & (0x1 << F2FS_IPU_FORCE))
1662 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
1664 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1665 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1667 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
1668 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1672 * IPU for rewrite async pages
1674 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1675 fio && fio->op == REQ_OP_WRITE &&
1676 !(fio->op_flags & REQ_SYNC) &&
1677 !f2fs_encrypted_inode(inode))
1680 /* this is only set during fdatasync */
1681 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1682 is_inode_flag_set(inode, FI_NEED_IPU))
1688 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1690 if (f2fs_is_pinned_file(inode))
1693 /* if this is cold file, we should overwrite to avoid fragmentation */
1694 if (file_is_cold(inode))
1697 return check_inplace_update_policy(inode, fio);
1700 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1702 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1704 if (test_opt(sbi, LFS))
1706 if (S_ISDIR(inode->i_mode))
1708 if (f2fs_is_atomic_file(inode))
1711 if (is_cold_data(fio->page))
1713 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1719 static inline bool need_inplace_update(struct f2fs_io_info *fio)
1721 struct inode *inode = fio->page->mapping->host;
1723 if (f2fs_should_update_outplace(inode, fio))
1726 return f2fs_should_update_inplace(inode, fio);
1729 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
1731 struct page *page = fio->page;
1732 struct inode *inode = page->mapping->host;
1733 struct dnode_of_data dn;
1734 struct extent_info ei = {0,0,0};
1735 struct node_info ni;
1736 bool ipu_force = false;
1739 set_new_dnode(&dn, inode, NULL, NULL, 0);
1740 if (need_inplace_update(fio) &&
1741 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1742 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1744 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1749 fio->need_lock = LOCK_DONE;
1753 /* Deadlock due to between page->lock and f2fs_lock_op */
1754 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1757 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
1761 fio->old_blkaddr = dn.data_blkaddr;
1763 /* This page is already truncated */
1764 if (fio->old_blkaddr == NULL_ADDR) {
1765 ClearPageUptodate(page);
1769 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1770 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1776 * If current allocation needs SSR,
1777 * it had better in-place writes for updated data.
1779 if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
1780 need_inplace_update(fio))) {
1781 err = encrypt_one_page(fio);
1785 set_page_writeback(page);
1786 ClearPageError(page);
1787 f2fs_put_dnode(&dn);
1788 if (fio->need_lock == LOCK_REQ)
1789 f2fs_unlock_op(fio->sbi);
1790 err = f2fs_inplace_write_data(fio);
1791 trace_f2fs_do_write_data_page(fio->page, IPU);
1792 set_inode_flag(inode, FI_UPDATE_WRITE);
1796 if (fio->need_lock == LOCK_RETRY) {
1797 if (!f2fs_trylock_op(fio->sbi)) {
1801 fio->need_lock = LOCK_REQ;
1804 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1808 fio->version = ni.version;
1810 err = encrypt_one_page(fio);
1814 set_page_writeback(page);
1815 ClearPageError(page);
1817 /* LFS mode write path */
1818 f2fs_outplace_write_data(&dn, fio);
1819 trace_f2fs_do_write_data_page(page, OPU);
1820 set_inode_flag(inode, FI_APPEND_WRITE);
1821 if (page->index == 0)
1822 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1824 f2fs_put_dnode(&dn);
1826 if (fio->need_lock == LOCK_REQ)
1827 f2fs_unlock_op(fio->sbi);
1831 static int __write_data_page(struct page *page, bool *submitted,
1832 struct writeback_control *wbc,
1833 enum iostat_type io_type)
1835 struct inode *inode = page->mapping->host;
1836 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1837 loff_t i_size = i_size_read(inode);
1838 const pgoff_t end_index = ((unsigned long long) i_size)
1840 loff_t psize = (page->index + 1) << PAGE_SHIFT;
1841 unsigned offset = 0;
1842 bool need_balance_fs = false;
1844 struct f2fs_io_info fio = {
1846 .ino = inode->i_ino,
1849 .op_flags = wbc_to_write_flags(wbc),
1850 .old_blkaddr = NULL_ADDR,
1852 .encrypted_page = NULL,
1854 .need_lock = LOCK_RETRY,
1859 trace_f2fs_writepage(page, DATA);
1861 /* we should bypass data pages to proceed the kworkder jobs */
1862 if (unlikely(f2fs_cp_error(sbi))) {
1863 mapping_set_error(page->mapping, -EIO);
1865 * don't drop any dirty dentry pages for keeping lastest
1866 * directory structure.
1868 if (S_ISDIR(inode->i_mode))
1873 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1876 if (page->index < end_index)
1880 * If the offset is out-of-range of file size,
1881 * this page does not have to be written to disk.
1883 offset = i_size & (PAGE_SIZE - 1);
1884 if ((page->index >= end_index + 1) || !offset)
1887 zero_user_segment(page, offset, PAGE_SIZE);
1889 if (f2fs_is_drop_cache(inode))
1891 /* we should not write 0'th page having journal header */
1892 if (f2fs_is_volatile_file(inode) && (!page->index ||
1893 (!wbc->for_reclaim &&
1894 f2fs_available_free_memory(sbi, BASE_CHECK))))
1897 /* Dentry blocks are controlled by checkpoint */
1898 if (S_ISDIR(inode->i_mode)) {
1899 fio.need_lock = LOCK_DONE;
1900 err = f2fs_do_write_data_page(&fio);
1904 if (!wbc->for_reclaim)
1905 need_balance_fs = true;
1906 else if (has_not_enough_free_secs(sbi, 0, 0))
1909 set_inode_flag(inode, FI_HOT_DATA);
1912 if (f2fs_has_inline_data(inode)) {
1913 err = f2fs_write_inline_data(inode, page);
1918 if (err == -EAGAIN) {
1919 err = f2fs_do_write_data_page(&fio);
1920 if (err == -EAGAIN) {
1921 fio.need_lock = LOCK_REQ;
1922 err = f2fs_do_write_data_page(&fio);
1927 file_set_keep_isize(inode);
1929 down_write(&F2FS_I(inode)->i_sem);
1930 if (F2FS_I(inode)->last_disk_size < psize)
1931 F2FS_I(inode)->last_disk_size = psize;
1932 up_write(&F2FS_I(inode)->i_sem);
1936 if (err && err != -ENOENT)
1940 inode_dec_dirty_pages(inode);
1942 ClearPageUptodate(page);
1944 if (wbc->for_reclaim) {
1945 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
1946 clear_inode_flag(inode, FI_HOT_DATA);
1947 f2fs_remove_dirty_inode(inode);
1952 if (!S_ISDIR(inode->i_mode))
1953 f2fs_balance_fs(sbi, need_balance_fs);
1955 if (unlikely(f2fs_cp_error(sbi))) {
1956 f2fs_submit_merged_write(sbi, DATA);
1961 *submitted = fio.submitted;
1966 redirty_page_for_writepage(wbc, page);
1968 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1969 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1970 * file_write_and_wait_range() will see EIO error, which is critical
1971 * to return value of fsync() followed by atomic_write failure to user.
1973 if (!err || wbc->for_reclaim)
1974 return AOP_WRITEPAGE_ACTIVATE;
1979 static int f2fs_write_data_page(struct page *page,
1980 struct writeback_control *wbc)
1982 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
1986 * This function was copied from write_cche_pages from mm/page-writeback.c.
1987 * The major change is making write step of cold data page separately from
1988 * warm/hot data page.
1990 static int f2fs_write_cache_pages(struct address_space *mapping,
1991 struct writeback_control *wbc,
1992 enum iostat_type io_type)
1996 struct pagevec pvec;
1997 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1999 pgoff_t uninitialized_var(writeback_index);
2001 pgoff_t end; /* Inclusive */
2003 pgoff_t last_idx = ULONG_MAX;
2005 int range_whole = 0;
2008 pagevec_init(&pvec);
2010 if (get_dirty_pages(mapping->host) <=
2011 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2012 set_inode_flag(mapping->host, FI_HOT_DATA);
2014 clear_inode_flag(mapping->host, FI_HOT_DATA);
2016 if (wbc->range_cyclic) {
2017 writeback_index = mapping->writeback_index; /* prev offset */
2018 index = writeback_index;
2025 index = wbc->range_start >> PAGE_SHIFT;
2026 end = wbc->range_end >> PAGE_SHIFT;
2027 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2029 cycled = 1; /* ignore range_cyclic tests */
2031 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2032 tag = PAGECACHE_TAG_TOWRITE;
2034 tag = PAGECACHE_TAG_DIRTY;
2036 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2037 tag_pages_for_writeback(mapping, index, end);
2039 while (!done && (index <= end)) {
2042 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2047 for (i = 0; i < nr_pages; i++) {
2048 struct page *page = pvec.pages[i];
2049 bool submitted = false;
2051 /* give a priority to WB_SYNC threads */
2052 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2053 wbc->sync_mode == WB_SYNC_NONE) {
2058 done_index = page->index;
2062 if (unlikely(page->mapping != mapping)) {
2068 if (!PageDirty(page)) {
2069 /* someone wrote it for us */
2070 goto continue_unlock;
2073 if (PageWriteback(page)) {
2074 if (wbc->sync_mode != WB_SYNC_NONE)
2075 f2fs_wait_on_page_writeback(page,
2078 goto continue_unlock;
2081 BUG_ON(PageWriteback(page));
2082 if (!clear_page_dirty_for_io(page))
2083 goto continue_unlock;
2085 ret = __write_data_page(page, &submitted, wbc, io_type);
2086 if (unlikely(ret)) {
2088 * keep nr_to_write, since vfs uses this to
2089 * get # of written pages.
2091 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2095 } else if (ret == -EAGAIN) {
2097 if (wbc->sync_mode == WB_SYNC_ALL) {
2099 congestion_wait(BLK_RW_ASYNC,
2105 done_index = page->index + 1;
2108 } else if (submitted) {
2109 last_idx = page->index;
2112 if (--wbc->nr_to_write <= 0 &&
2113 wbc->sync_mode == WB_SYNC_NONE) {
2118 pagevec_release(&pvec);
2122 if (!cycled && !done) {
2125 end = writeback_index - 1;
2128 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2129 mapping->writeback_index = done_index;
2131 if (last_idx != ULONG_MAX)
2132 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2138 static inline bool __should_serialize_io(struct inode *inode,
2139 struct writeback_control *wbc)
2141 if (!S_ISREG(inode->i_mode))
2143 if (wbc->sync_mode != WB_SYNC_ALL)
2145 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2150 static int __f2fs_write_data_pages(struct address_space *mapping,
2151 struct writeback_control *wbc,
2152 enum iostat_type io_type)
2154 struct inode *inode = mapping->host;
2155 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2156 struct blk_plug plug;
2158 bool locked = false;
2160 /* deal with chardevs and other special file */
2161 if (!mapping->a_ops->writepage)
2164 /* skip writing if there is no dirty page in this inode */
2165 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2168 /* during POR, we don't need to trigger writepage at all. */
2169 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2172 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2173 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2174 f2fs_available_free_memory(sbi, DIRTY_DENTS))
2177 /* skip writing during file defragment */
2178 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2181 trace_f2fs_writepages(mapping->host, wbc, DATA);
2183 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2184 if (wbc->sync_mode == WB_SYNC_ALL)
2185 atomic_inc(&sbi->wb_sync_req[DATA]);
2186 else if (atomic_read(&sbi->wb_sync_req[DATA]))
2189 if (__should_serialize_io(inode, wbc)) {
2190 mutex_lock(&sbi->writepages);
2194 blk_start_plug(&plug);
2195 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2196 blk_finish_plug(&plug);
2199 mutex_unlock(&sbi->writepages);
2201 if (wbc->sync_mode == WB_SYNC_ALL)
2202 atomic_dec(&sbi->wb_sync_req[DATA]);
2204 * if some pages were truncated, we cannot guarantee its mapping->host
2205 * to detect pending bios.
2208 f2fs_remove_dirty_inode(inode);
2212 wbc->pages_skipped += get_dirty_pages(inode);
2213 trace_f2fs_writepages(mapping->host, wbc, DATA);
2217 static int f2fs_write_data_pages(struct address_space *mapping,
2218 struct writeback_control *wbc)
2220 struct inode *inode = mapping->host;
2222 return __f2fs_write_data_pages(mapping, wbc,
2223 F2FS_I(inode)->cp_task == current ?
2224 FS_CP_DATA_IO : FS_DATA_IO);
2227 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2229 struct inode *inode = mapping->host;
2230 loff_t i_size = i_size_read(inode);
2233 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2234 down_write(&F2FS_I(inode)->i_mmap_sem);
2236 truncate_pagecache(inode, i_size);
2237 f2fs_truncate_blocks(inode, i_size, true);
2239 up_write(&F2FS_I(inode)->i_mmap_sem);
2240 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2244 static int prepare_write_begin(struct f2fs_sb_info *sbi,
2245 struct page *page, loff_t pos, unsigned len,
2246 block_t *blk_addr, bool *node_changed)
2248 struct inode *inode = page->mapping->host;
2249 pgoff_t index = page->index;
2250 struct dnode_of_data dn;
2252 bool locked = false;
2253 struct extent_info ei = {0,0,0};
2257 * we already allocated all the blocks, so we don't need to get
2258 * the block addresses when there is no need to fill the page.
2260 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2261 !is_inode_flag_set(inode, FI_NO_PREALLOC))
2264 if (f2fs_has_inline_data(inode) ||
2265 (pos & PAGE_MASK) >= i_size_read(inode)) {
2266 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
2270 /* check inline_data */
2271 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2272 if (IS_ERR(ipage)) {
2273 err = PTR_ERR(ipage);
2277 set_new_dnode(&dn, inode, ipage, ipage, 0);
2279 if (f2fs_has_inline_data(inode)) {
2280 if (pos + len <= MAX_INLINE_DATA(inode)) {
2281 f2fs_do_read_inline_data(page, ipage);
2282 set_inode_flag(inode, FI_DATA_EXIST);
2284 set_inline_node(ipage);
2286 err = f2fs_convert_inline_page(&dn, page);
2289 if (dn.data_blkaddr == NULL_ADDR)
2290 err = f2fs_get_block(&dn, index);
2292 } else if (locked) {
2293 err = f2fs_get_block(&dn, index);
2295 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2296 dn.data_blkaddr = ei.blk + index - ei.fofs;
2299 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
2300 if (err || dn.data_blkaddr == NULL_ADDR) {
2301 f2fs_put_dnode(&dn);
2302 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2310 /* convert_inline_page can make node_changed */
2311 *blk_addr = dn.data_blkaddr;
2312 *node_changed = dn.node_changed;
2314 f2fs_put_dnode(&dn);
2317 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
2321 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2322 loff_t pos, unsigned len, unsigned flags,
2323 struct page **pagep, void **fsdata)
2325 struct inode *inode = mapping->host;
2326 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2327 struct page *page = NULL;
2328 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
2329 bool need_balance = false, drop_atomic = false;
2330 block_t blkaddr = NULL_ADDR;
2333 trace_f2fs_write_begin(inode, pos, len, flags);
2335 if ((f2fs_is_atomic_file(inode) &&
2336 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2337 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2344 * We should check this at this moment to avoid deadlock on inode page
2345 * and #0 page. The locking rule for inline_data conversion should be:
2346 * lock_page(page #0) -> lock_page(inode_page)
2349 err = f2fs_convert_inline_inode(inode);
2355 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2356 * wait_for_stable_page. Will wait that below with our IO control.
2358 page = f2fs_pagecache_get_page(mapping, index,
2359 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
2367 err = prepare_write_begin(sbi, page, pos, len,
2368 &blkaddr, &need_balance);
2372 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
2374 f2fs_balance_fs(sbi, true);
2376 if (page->mapping != mapping) {
2377 /* The page got truncated from under us */
2378 f2fs_put_page(page, 1);
2383 f2fs_wait_on_page_writeback(page, DATA, false);
2385 /* wait for GCed page writeback via META_MAPPING */
2386 if (f2fs_post_read_required(inode))
2387 f2fs_wait_on_block_writeback(sbi, blkaddr);
2389 if (len == PAGE_SIZE || PageUptodate(page))
2392 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2393 zero_user_segment(page, len, PAGE_SIZE);
2397 if (blkaddr == NEW_ADDR) {
2398 zero_user_segment(page, 0, PAGE_SIZE);
2399 SetPageUptodate(page);
2401 err = f2fs_submit_page_read(inode, page, blkaddr);
2406 if (unlikely(page->mapping != mapping)) {
2407 f2fs_put_page(page, 1);
2410 if (unlikely(!PageUptodate(page))) {
2418 f2fs_put_page(page, 1);
2419 f2fs_write_failed(mapping, pos + len);
2421 f2fs_drop_inmem_pages_all(sbi, false);
2425 static int f2fs_write_end(struct file *file,
2426 struct address_space *mapping,
2427 loff_t pos, unsigned len, unsigned copied,
2428 struct page *page, void *fsdata)
2430 struct inode *inode = page->mapping->host;
2432 trace_f2fs_write_end(inode, pos, len, copied);
2435 * This should be come from len == PAGE_SIZE, and we expect copied
2436 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2437 * let generic_perform_write() try to copy data again through copied=0.
2439 if (!PageUptodate(page)) {
2440 if (unlikely(copied != len))
2443 SetPageUptodate(page);
2448 set_page_dirty(page);
2450 if (pos + copied > i_size_read(inode))
2451 f2fs_i_size_write(inode, pos + copied);
2453 f2fs_put_page(page, 1);
2454 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2458 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2461 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2462 unsigned blkbits = i_blkbits;
2463 unsigned blocksize_mask = (1 << blkbits) - 1;
2464 unsigned long align = offset | iov_iter_alignment(iter);
2465 struct block_device *bdev = inode->i_sb->s_bdev;
2467 if (align & blocksize_mask) {
2469 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2470 blocksize_mask = (1 << blkbits) - 1;
2471 if (align & blocksize_mask)
2478 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2480 struct address_space *mapping = iocb->ki_filp->f_mapping;
2481 struct inode *inode = mapping->host;
2482 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2483 size_t count = iov_iter_count(iter);
2484 loff_t offset = iocb->ki_pos;
2485 int rw = iov_iter_rw(iter);
2487 enum rw_hint hint = iocb->ki_hint;
2488 int whint_mode = F2FS_OPTION(sbi).whint_mode;
2490 err = check_direct_IO(inode, iter, offset);
2492 return err < 0 ? err : 0;
2494 if (f2fs_force_buffered_io(inode, rw))
2497 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
2499 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2500 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2502 if (!down_read_trylock(&F2FS_I(inode)->i_gc_rwsem[rw])) {
2503 if (iocb->ki_flags & IOCB_NOWAIT) {
2504 iocb->ki_hint = hint;
2508 down_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
2511 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
2512 up_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
2515 if (whint_mode == WHINT_MODE_OFF)
2516 iocb->ki_hint = hint;
2518 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2520 set_inode_flag(inode, FI_UPDATE_WRITE);
2521 } else if (err < 0) {
2522 f2fs_write_failed(mapping, offset + count);
2527 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
2532 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2533 unsigned int length)
2535 struct inode *inode = page->mapping->host;
2536 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2538 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2539 (offset % PAGE_SIZE || length != PAGE_SIZE))
2542 if (PageDirty(page)) {
2543 if (inode->i_ino == F2FS_META_INO(sbi)) {
2544 dec_page_count(sbi, F2FS_DIRTY_META);
2545 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
2546 dec_page_count(sbi, F2FS_DIRTY_NODES);
2548 inode_dec_dirty_pages(inode);
2549 f2fs_remove_dirty_inode(inode);
2553 /* This is atomic written page, keep Private */
2554 if (IS_ATOMIC_WRITTEN_PAGE(page))
2555 return f2fs_drop_inmem_page(inode, page);
2557 set_page_private(page, 0);
2558 ClearPagePrivate(page);
2561 int f2fs_release_page(struct page *page, gfp_t wait)
2563 /* If this is dirty page, keep PagePrivate */
2564 if (PageDirty(page))
2567 /* This is atomic written page, keep Private */
2568 if (IS_ATOMIC_WRITTEN_PAGE(page))
2571 set_page_private(page, 0);
2572 ClearPagePrivate(page);
2576 static int f2fs_set_data_page_dirty(struct page *page)
2578 struct address_space *mapping = page->mapping;
2579 struct inode *inode = mapping->host;
2581 trace_f2fs_set_page_dirty(page, DATA);
2583 if (!PageUptodate(page))
2584 SetPageUptodate(page);
2586 /* don't remain PG_checked flag which was set during GC */
2587 if (is_cold_data(page))
2588 clear_cold_data(page);
2590 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
2591 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2592 f2fs_register_inmem_page(inode, page);
2596 * Previously, this page has been registered, we just
2602 if (!PageDirty(page)) {
2603 __set_page_dirty_nobuffers(page);
2604 f2fs_update_dirty_page(inode, page);
2610 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2612 struct inode *inode = mapping->host;
2614 if (f2fs_has_inline_data(inode))
2617 /* make sure allocating whole blocks */
2618 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2619 filemap_write_and_wait(mapping);
2621 return generic_block_bmap(mapping, block, get_data_block_bmap);
2624 #ifdef CONFIG_MIGRATION
2625 #include <linux/migrate.h>
2627 int f2fs_migrate_page(struct address_space *mapping,
2628 struct page *newpage, struct page *page, enum migrate_mode mode)
2630 int rc, extra_count;
2631 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2632 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2634 BUG_ON(PageWriteback(page));
2636 /* migrating an atomic written page is safe with the inmem_lock hold */
2637 if (atomic_written) {
2638 if (mode != MIGRATE_SYNC)
2640 if (!mutex_trylock(&fi->inmem_lock))
2645 * A reference is expected if PagePrivate set when move mapping,
2646 * however F2FS breaks this for maintaining dirty page counts when
2647 * truncating pages. So here adjusting the 'extra_count' make it work.
2649 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2650 rc = migrate_page_move_mapping(mapping, newpage,
2651 page, NULL, mode, extra_count);
2652 if (rc != MIGRATEPAGE_SUCCESS) {
2654 mutex_unlock(&fi->inmem_lock);
2658 if (atomic_written) {
2659 struct inmem_pages *cur;
2660 list_for_each_entry(cur, &fi->inmem_pages, list)
2661 if (cur->page == page) {
2662 cur->page = newpage;
2665 mutex_unlock(&fi->inmem_lock);
2670 if (PagePrivate(page))
2671 SetPagePrivate(newpage);
2672 set_page_private(newpage, page_private(page));
2674 if (mode != MIGRATE_SYNC_NO_COPY)
2675 migrate_page_copy(newpage, page);
2677 migrate_page_states(newpage, page);
2679 return MIGRATEPAGE_SUCCESS;
2683 const struct address_space_operations f2fs_dblock_aops = {
2684 .readpage = f2fs_read_data_page,
2685 .readpages = f2fs_read_data_pages,
2686 .writepage = f2fs_write_data_page,
2687 .writepages = f2fs_write_data_pages,
2688 .write_begin = f2fs_write_begin,
2689 .write_end = f2fs_write_end,
2690 .set_page_dirty = f2fs_set_data_page_dirty,
2691 .invalidatepage = f2fs_invalidate_page,
2692 .releasepage = f2fs_release_page,
2693 .direct_IO = f2fs_direct_IO,
2695 #ifdef CONFIG_MIGRATION
2696 .migratepage = f2fs_migrate_page,
2700 void f2fs_clear_radix_tree_dirty_tag(struct page *page)
2702 struct address_space *mapping = page_mapping(page);
2703 unsigned long flags;
2705 xa_lock_irqsave(&mapping->i_pages, flags);
2706 radix_tree_tag_clear(&mapping->i_pages, page_index(page),
2707 PAGECACHE_TAG_DIRTY);
2708 xa_unlock_irqrestore(&mapping->i_pages, flags);
2711 int __init f2fs_init_post_read_processing(void)
2713 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2714 if (!bio_post_read_ctx_cache)
2716 bio_post_read_ctx_pool =
2717 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2718 bio_post_read_ctx_cache);
2719 if (!bio_post_read_ctx_pool)
2720 goto fail_free_cache;
2724 kmem_cache_destroy(bio_post_read_ctx_cache);
2729 void __exit f2fs_destroy_post_read_processing(void)
2731 mempool_destroy(bio_post_read_ctx_pool);
2732 kmem_cache_destroy(bio_post_read_ctx_cache);