17c1a000dSChao Yu // SPDX-License-Identifier: GPL-2.0 20a8165d7SJaegeuk Kim /* 3127e670aSJaegeuk Kim * fs/f2fs/checkpoint.c 4127e670aSJaegeuk Kim * 5127e670aSJaegeuk Kim * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6127e670aSJaegeuk Kim * http://www.samsung.com/ 7127e670aSJaegeuk Kim */ 8127e670aSJaegeuk Kim #include <linux/fs.h> 9127e670aSJaegeuk Kim #include <linux/bio.h> 10127e670aSJaegeuk Kim #include <linux/mpage.h> 11127e670aSJaegeuk Kim #include <linux/writeback.h> 12127e670aSJaegeuk Kim #include <linux/blkdev.h> 13127e670aSJaegeuk Kim #include <linux/f2fs_fs.h> 14127e670aSJaegeuk Kim #include <linux/pagevec.h> 15127e670aSJaegeuk Kim #include <linux/swap.h> 16261eeb9cSDaeho Jeong #include <linux/kthread.h> 17127e670aSJaegeuk Kim 18127e670aSJaegeuk Kim #include "f2fs.h" 19127e670aSJaegeuk Kim #include "node.h" 20127e670aSJaegeuk Kim #include "segment.h" 2152118743SDaeho Jeong #include "iostat.h" 222af4bd6cSNamjae Jeon #include <trace/events/f2fs.h> 23127e670aSJaegeuk Kim 24261eeb9cSDaeho Jeong #define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) 25261eeb9cSDaeho Jeong 266451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab; 274d57b86dSChao Yu struct kmem_cache *f2fs_inode_entry_slab; 28127e670aSJaegeuk Kim 2938f91ca8SJaegeuk Kim void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io) 3038f91ca8SJaegeuk Kim { 31d494500aSChao Yu f2fs_build_fault_attr(sbi, 0, 0); 32aaec2b1dSChao Yu set_ckpt_flags(sbi, CP_ERROR_FLAG); 3338f91ca8SJaegeuk Kim if (!end_io) 34b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 3538f91ca8SJaegeuk Kim } 3638f91ca8SJaegeuk Kim 370a8165d7SJaegeuk Kim /* 38127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 39127e670aSJaegeuk Kim */ 404d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 41127e670aSJaegeuk Kim { 429df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 43beb78181SZhang Qilong struct page *page; 44127e670aSJaegeuk Kim repeat: 45300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 46127e670aSJaegeuk Kim if (!page) { 47127e670aSJaegeuk Kim cond_resched(); 48127e670aSJaegeuk Kim goto repeat; 49127e670aSJaegeuk Kim } 50bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 51237c0790SJaegeuk Kim if (!PageUptodate(page)) 52127e670aSJaegeuk Kim SetPageUptodate(page); 53127e670aSJaegeuk Kim return page; 54127e670aSJaegeuk Kim } 55127e670aSJaegeuk Kim 562b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index, 572b947003SChao Yu bool is_meta) 58127e670aSJaegeuk Kim { 599df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 60127e670aSJaegeuk Kim struct page *page; 61cf04e8ebSJaegeuk Kim struct f2fs_io_info fio = { 6205ca3632SJaegeuk Kim .sbi = sbi, 63cf04e8ebSJaegeuk Kim .type = META, 6404d328deSMike Christie .op = REQ_OP_READ, 6570fd7614SChristoph Hellwig .op_flags = REQ_META | REQ_PRIO, 667a9d7548SChao Yu .old_blkaddr = index, 677a9d7548SChao Yu .new_blkaddr = index, 684375a336SJaegeuk Kim .encrypted_page = NULL, 696dc3a126SChao Yu .is_por = !is_meta, 70cf04e8ebSJaegeuk Kim }; 717735730dSChao Yu int err; 722b947003SChao Yu 732b947003SChao Yu if (unlikely(!is_meta)) 7404d328deSMike Christie fio.op_flags &= ~REQ_META; 75127e670aSJaegeuk Kim repeat: 76300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 77127e670aSJaegeuk Kim if (!page) { 78127e670aSJaegeuk Kim cond_resched(); 79127e670aSJaegeuk Kim goto repeat; 80127e670aSJaegeuk Kim } 81393ff91fSJaegeuk Kim if (PageUptodate(page)) 82393ff91fSJaegeuk Kim goto out; 83127e670aSJaegeuk Kim 8405ca3632SJaegeuk Kim fio.page = page; 8505ca3632SJaegeuk Kim 867735730dSChao Yu err = f2fs_submit_page_bio(&fio); 877735730dSChao Yu if (err) { 887735730dSChao Yu f2fs_put_page(page, 1); 897735730dSChao Yu return ERR_PTR(err); 9086531d6bSJaegeuk Kim } 91393ff91fSJaegeuk Kim 928b83ac81SChao Yu f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE); 938b83ac81SChao Yu 94393ff91fSJaegeuk Kim lock_page(page); 956bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) { 96afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 97afcb7ca0SJaegeuk Kim goto repeat; 98afcb7ca0SJaegeuk Kim } 99f3f338caSChao Yu 10081114baaSChao Yu if (unlikely(!PageUptodate(page))) { 101*a7b8618aSJaegeuk Kim f2fs_handle_page_eio(sbi, page->index, META); 1027735730dSChao Yu f2fs_put_page(page, 1); 1037735730dSChao Yu return ERR_PTR(-EIO); 10481114baaSChao Yu } 105393ff91fSJaegeuk Kim out: 106127e670aSJaegeuk Kim return page; 107127e670aSJaegeuk Kim } 108127e670aSJaegeuk Kim 1094d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 1102b947003SChao Yu { 1112b947003SChao Yu return __get_meta_page(sbi, index, true); 1122b947003SChao Yu } 1132b947003SChao Yu 11486f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index) 1157735730dSChao Yu { 1167735730dSChao Yu struct page *page; 1177735730dSChao Yu int count = 0; 1187735730dSChao Yu 1197735730dSChao Yu retry: 1207735730dSChao Yu page = __get_meta_page(sbi, index, true); 1217735730dSChao Yu if (IS_ERR(page)) { 1227735730dSChao Yu if (PTR_ERR(page) == -EIO && 1237735730dSChao Yu ++count <= DEFAULT_RETRY_IO_COUNT) 1247735730dSChao Yu goto retry; 1257735730dSChao Yu f2fs_stop_checkpoint(sbi, false); 1267735730dSChao Yu } 1277735730dSChao Yu return page; 1287735730dSChao Yu } 1297735730dSChao Yu 1302b947003SChao Yu /* for POR only */ 1314d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 1322b947003SChao Yu { 1332b947003SChao Yu return __get_meta_page(sbi, index, false); 1342b947003SChao Yu } 1352b947003SChao Yu 13693770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr, 13793770ab7SChao Yu int type) 13893770ab7SChao Yu { 13993770ab7SChao Yu struct seg_entry *se; 14093770ab7SChao Yu unsigned int segno, offset; 14193770ab7SChao Yu bool exist; 14293770ab7SChao Yu 14393770ab7SChao Yu if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ) 14493770ab7SChao Yu return true; 14593770ab7SChao Yu 14693770ab7SChao Yu segno = GET_SEGNO(sbi, blkaddr); 14793770ab7SChao Yu offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 14893770ab7SChao Yu se = get_seg_entry(sbi, segno); 14993770ab7SChao Yu 15093770ab7SChao Yu exist = f2fs_test_bit(offset, se->cur_valid_map); 15193770ab7SChao Yu if (!exist && type == DATA_GENERIC_ENHANCE) { 152dcbb4c10SJoe Perches f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", 153dcbb4c10SJoe Perches blkaddr, exist); 15493770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 155dc2f78e2SDongliang Mu dump_stack(); 15693770ab7SChao Yu } 15793770ab7SChao Yu return exist; 15893770ab7SChao Yu } 15993770ab7SChao Yu 160e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 1614d57b86dSChao Yu block_t blkaddr, int type) 162662befdaSChao Yu { 163662befdaSChao Yu switch (type) { 164662befdaSChao Yu case META_NAT: 16566b00c18SChao Yu break; 166662befdaSChao Yu case META_SIT: 16766b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 16866b00c18SChao Yu return false; 16966b00c18SChao Yu break; 17081c1a0f1SChao Yu case META_SSA: 17166b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 17266b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr)) 17366b00c18SChao Yu return false; 17466b00c18SChao Yu break; 175662befdaSChao Yu case META_CP: 17666b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 17766b00c18SChao Yu blkaddr < __start_cp_addr(sbi))) 17866b00c18SChao Yu return false; 17966b00c18SChao Yu break; 1804c521f49SJaegeuk Kim case META_POR: 18193770ab7SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 18293770ab7SChao Yu blkaddr < MAIN_BLKADDR(sbi))) 18393770ab7SChao Yu return false; 18493770ab7SChao Yu break; 185e1da7872SChao Yu case DATA_GENERIC: 18693770ab7SChao Yu case DATA_GENERIC_ENHANCE: 18793770ab7SChao Yu case DATA_GENERIC_ENHANCE_READ: 18866b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 189c9b60788SChao Yu blkaddr < MAIN_BLKADDR(sbi))) { 190dcbb4c10SJoe Perches f2fs_warn(sbi, "access invalid blkaddr:%u", 191dcbb4c10SJoe Perches blkaddr); 19293770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 193dc2f78e2SDongliang Mu dump_stack(); 19466b00c18SChao Yu return false; 19593770ab7SChao Yu } else { 19693770ab7SChao Yu return __is_bitmap_valid(sbi, blkaddr, type); 197c9b60788SChao Yu } 19866b00c18SChao Yu break; 199e1da7872SChao Yu case META_GENERIC: 200e1da7872SChao Yu if (unlikely(blkaddr < SEG0_BLKADDR(sbi) || 201e1da7872SChao Yu blkaddr >= MAIN_BLKADDR(sbi))) 202e1da7872SChao Yu return false; 203e1da7872SChao Yu break; 204662befdaSChao Yu default: 205662befdaSChao Yu BUG(); 206662befdaSChao Yu } 20766b00c18SChao Yu 20866b00c18SChao Yu return true; 209662befdaSChao Yu } 210662befdaSChao Yu 211662befdaSChao Yu /* 2127a88ddb5SChao Yu * Readahead CP/NAT/SIT/SSA/POR pages 213662befdaSChao Yu */ 2144d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 21526879fb1SChao Yu int type, bool sync) 216662befdaSChao Yu { 217662befdaSChao Yu struct page *page; 2184c521f49SJaegeuk Kim block_t blkno = start; 219662befdaSChao Yu struct f2fs_io_info fio = { 22005ca3632SJaegeuk Kim .sbi = sbi, 221662befdaSChao Yu .type = META, 22204d328deSMike Christie .op = REQ_OP_READ, 22370fd7614SChristoph Hellwig .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, 2244375a336SJaegeuk Kim .encrypted_page = NULL, 225fb830fc5SChao Yu .in_list = false, 2266dc3a126SChao Yu .is_por = (type == META_POR), 227662befdaSChao Yu }; 228e9f5b8b8SChao Yu struct blk_plug plug; 229ce4c638cSChao Yu int err; 230662befdaSChao Yu 2312b947003SChao Yu if (unlikely(type == META_POR)) 23204d328deSMike Christie fio.op_flags &= ~REQ_META; 2332b947003SChao Yu 234e9f5b8b8SChao Yu blk_start_plug(&plug); 235662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 236662befdaSChao Yu 237e1da7872SChao Yu if (!f2fs_is_valid_blkaddr(sbi, blkno, type)) 23866b00c18SChao Yu goto out; 23966b00c18SChao Yu 240662befdaSChao Yu switch (type) { 241662befdaSChao Yu case META_NAT: 24266b00c18SChao Yu if (unlikely(blkno >= 24366b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 244662befdaSChao Yu blkno = 0; 24566b00c18SChao Yu /* get nat block addr */ 2467a9d7548SChao Yu fio.new_blkaddr = current_nat_addr(sbi, 247662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 248662befdaSChao Yu break; 249662befdaSChao Yu case META_SIT: 2506a257471SChao Yu if (unlikely(blkno >= TOTAL_SEGS(sbi))) 2516a257471SChao Yu goto out; 252662befdaSChao Yu /* get sit block addr */ 2537a9d7548SChao Yu fio.new_blkaddr = current_sit_addr(sbi, 254662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 255662befdaSChao Yu break; 25681c1a0f1SChao Yu case META_SSA: 257662befdaSChao Yu case META_CP: 2584c521f49SJaegeuk Kim case META_POR: 2597a9d7548SChao Yu fio.new_blkaddr = blkno; 260662befdaSChao Yu break; 261662befdaSChao Yu default: 262662befdaSChao Yu BUG(); 263662befdaSChao Yu } 264662befdaSChao Yu 265300e129cSJaegeuk Kim page = f2fs_grab_cache_page(META_MAPPING(sbi), 266300e129cSJaegeuk Kim fio.new_blkaddr, false); 267662befdaSChao Yu if (!page) 268662befdaSChao Yu continue; 269662befdaSChao Yu if (PageUptodate(page)) { 270662befdaSChao Yu f2fs_put_page(page, 1); 271662befdaSChao Yu continue; 272662befdaSChao Yu } 273662befdaSChao Yu 27405ca3632SJaegeuk Kim fio.page = page; 275ce4c638cSChao Yu err = f2fs_submit_page_bio(&fio); 276ce4c638cSChao Yu f2fs_put_page(page, err ? 1 : 0); 2778b83ac81SChao Yu 2788b83ac81SChao Yu if (!err) 2798b83ac81SChao Yu f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE); 280662befdaSChao Yu } 281662befdaSChao Yu out: 282e9f5b8b8SChao Yu blk_finish_plug(&plug); 283662befdaSChao Yu return blkno - start; 284662befdaSChao Yu } 285662befdaSChao Yu 286430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index, 287430f163bSChao Yu unsigned int ra_blocks) 288635aee1fSChao Yu { 289635aee1fSChao Yu struct page *page; 290635aee1fSChao Yu bool readahead = false; 291635aee1fSChao Yu 292430f163bSChao Yu if (ra_blocks == RECOVERY_MIN_RA_BLOCKS) 293430f163bSChao Yu return; 294430f163bSChao Yu 295635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 2964da7bf5aSJaegeuk Kim if (!page || !PageUptodate(page)) 297635aee1fSChao Yu readahead = true; 298635aee1fSChao Yu f2fs_put_page(page, 0); 299635aee1fSChao Yu 300635aee1fSChao Yu if (readahead) 301430f163bSChao Yu f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true); 302635aee1fSChao Yu } 303635aee1fSChao Yu 304b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page, 305b0af6d49SChao Yu struct writeback_control *wbc, 306b0af6d49SChao Yu enum iostat_type io_type) 307127e670aSJaegeuk Kim { 3084081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 309577e3495SJaegeuk Kim 310ecda0de3SChao Yu trace_f2fs_writepage(page, META); 311ecda0de3SChao Yu 312af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 313af697c0fSJaegeuk Kim goto redirty_out; 314caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 315cfb271d4SChao Yu goto redirty_out; 316857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 317cfb271d4SChao Yu goto redirty_out; 318127e670aSJaegeuk Kim 3194d57b86dSChao Yu f2fs_do_write_meta_page(sbi, page, io_type); 320127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 3210c3a5797SChao Yu 3220c3a5797SChao Yu if (wbc->for_reclaim) 323bab475c5SChao Yu f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META); 3240c3a5797SChao Yu 325127e670aSJaegeuk Kim unlock_page(page); 326857dc4e0SJaegeuk Kim 3270c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 328b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, META); 3290c3a5797SChao Yu 330577e3495SJaegeuk Kim return 0; 331cfb271d4SChao Yu 332cfb271d4SChao Yu redirty_out: 33376f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 334cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 335127e670aSJaegeuk Kim } 336127e670aSJaegeuk Kim 337b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page, 338b0af6d49SChao Yu struct writeback_control *wbc) 339b0af6d49SChao Yu { 340b0af6d49SChao Yu return __f2fs_write_meta_page(page, wbc, FS_META_IO); 341b0af6d49SChao Yu } 342b0af6d49SChao Yu 343127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 344127e670aSJaegeuk Kim struct writeback_control *wbc) 345127e670aSJaegeuk Kim { 3464081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 34750c8cdb3SJaegeuk Kim long diff, written; 348127e670aSJaegeuk Kim 3490771fcc7SChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3500771fcc7SChao Yu goto skip_write; 3510771fcc7SChao Yu 3525459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 353812a9597SJaegeuk Kim if (wbc->sync_mode != WB_SYNC_ALL && 354812a9597SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < 355812a9597SJaegeuk Kim nr_pages_to_skip(sbi, META)) 356d3baf95dSJaegeuk Kim goto skip_write; 357127e670aSJaegeuk Kim 358a29d0e0bSYunlei He /* if locked failed, cp will flush dirty pages instead */ 359e4544b63STim Murray if (!f2fs_down_write_trylock(&sbi->cp_global_sem)) 360a29d0e0bSYunlei He goto skip_write; 361d31c7c3fSYunlei He 362a29d0e0bSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 36350c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 3644d57b86dSChao Yu written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); 365e4544b63STim Murray f2fs_up_write(&sbi->cp_global_sem); 36650c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 367127e670aSJaegeuk Kim return 0; 368d3baf95dSJaegeuk Kim 369d3baf95dSJaegeuk Kim skip_write: 370d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 371d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 372d3baf95dSJaegeuk Kim return 0; 373127e670aSJaegeuk Kim } 374127e670aSJaegeuk Kim 3754d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 376b0af6d49SChao Yu long nr_to_write, enum iostat_type io_type) 377127e670aSJaegeuk Kim { 3789df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 379028a63a6SJan Kara pgoff_t index = 0, prev = ULONG_MAX; 380127e670aSJaegeuk Kim struct pagevec pvec; 381127e670aSJaegeuk Kim long nwritten = 0; 382028a63a6SJan Kara int nr_pages; 383127e670aSJaegeuk Kim struct writeback_control wbc = { 384127e670aSJaegeuk Kim .for_reclaim = 0, 385127e670aSJaegeuk Kim }; 386e9f5b8b8SChao Yu struct blk_plug plug; 387127e670aSJaegeuk Kim 38886679820SMel Gorman pagevec_init(&pvec); 389127e670aSJaegeuk Kim 390e9f5b8b8SChao Yu blk_start_plug(&plug); 391e9f5b8b8SChao Yu 392028a63a6SJan Kara while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 39367fd707fSJan Kara PAGECACHE_TAG_DIRTY))) { 394028a63a6SJan Kara int i; 395127e670aSJaegeuk Kim 396127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 397127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 398203681f6SJaegeuk Kim 39980dd9c0eSChao Yu if (prev == ULONG_MAX) 4006066d8cdSJaegeuk Kim prev = page->index - 1; 4016066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 4026066d8cdSJaegeuk Kim pagevec_release(&pvec); 4036066d8cdSJaegeuk Kim goto stop; 4046066d8cdSJaegeuk Kim } 4056066d8cdSJaegeuk Kim 406127e670aSJaegeuk Kim lock_page(page); 407203681f6SJaegeuk Kim 408203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 409203681f6SJaegeuk Kim continue_unlock: 410203681f6SJaegeuk Kim unlock_page(page); 411203681f6SJaegeuk Kim continue; 412203681f6SJaegeuk Kim } 413203681f6SJaegeuk Kim if (!PageDirty(page)) { 414203681f6SJaegeuk Kim /* someone wrote it for us */ 415203681f6SJaegeuk Kim goto continue_unlock; 416203681f6SJaegeuk Kim } 417203681f6SJaegeuk Kim 418bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 419fa3d2bdfSJaegeuk Kim 420203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 421203681f6SJaegeuk Kim goto continue_unlock; 422203681f6SJaegeuk Kim 423b0af6d49SChao Yu if (__f2fs_write_meta_page(page, &wbc, io_type)) { 424577e3495SJaegeuk Kim unlock_page(page); 425577e3495SJaegeuk Kim break; 426577e3495SJaegeuk Kim } 427cfb271d4SChao Yu nwritten++; 4286066d8cdSJaegeuk Kim prev = page->index; 429cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 430127e670aSJaegeuk Kim break; 431127e670aSJaegeuk Kim } 432127e670aSJaegeuk Kim pagevec_release(&pvec); 433127e670aSJaegeuk Kim cond_resched(); 434127e670aSJaegeuk Kim } 4356066d8cdSJaegeuk Kim stop: 436127e670aSJaegeuk Kim if (nwritten) 437b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, type); 438127e670aSJaegeuk Kim 439e9f5b8b8SChao Yu blk_finish_plug(&plug); 440e9f5b8b8SChao Yu 441127e670aSJaegeuk Kim return nwritten; 442127e670aSJaegeuk Kim } 443127e670aSJaegeuk Kim 4441d9ac659SMatthew Wilcox (Oracle) static bool f2fs_dirty_meta_folio(struct address_space *mapping, 4451d9ac659SMatthew Wilcox (Oracle) struct folio *folio) 446127e670aSJaegeuk Kim { 4471d9ac659SMatthew Wilcox (Oracle) trace_f2fs_set_page_dirty(&folio->page, META); 44826c6b887SJaegeuk Kim 4491d9ac659SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) 4501d9ac659SMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 4511d9ac659SMatthew Wilcox (Oracle) if (!folio_test_dirty(folio)) { 4521d9ac659SMatthew Wilcox (Oracle) filemap_dirty_folio(mapping, folio); 45329c87793SMatthew Wilcox (Oracle) inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META); 4541d9ac659SMatthew Wilcox (Oracle) set_page_private_reference(&folio->page); 4551d9ac659SMatthew Wilcox (Oracle) return true; 456127e670aSJaegeuk Kim } 4571d9ac659SMatthew Wilcox (Oracle) return false; 458127e670aSJaegeuk Kim } 459127e670aSJaegeuk Kim 460127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 461127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 462127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 4631d9ac659SMatthew Wilcox (Oracle) .dirty_folio = f2fs_dirty_meta_folio, 46491503996SMatthew Wilcox (Oracle) .invalidate_folio = f2fs_invalidate_folio, 465487261f3SChao Yu .releasepage = f2fs_release_page, 4665b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION 4675b7a487cSWeichao Guo .migratepage = f2fs_migrate_page, 4685b7a487cSWeichao Guo #endif 469127e670aSJaegeuk Kim }; 470127e670aSJaegeuk Kim 47139d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, 47239d787beSChao Yu unsigned int devidx, int type) 473953e6cc6SJaegeuk Kim { 47467298804SChao Yu struct inode_management *im = &sbi->im[type]; 4754b106518SChao Yu struct ino_entry *e = NULL, *new = NULL; 47680c54505SJaegeuk Kim 4774b106518SChao Yu if (type == FLUSH_INO) { 4784b106518SChao Yu rcu_read_lock(); 4794b106518SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 4804b106518SChao Yu rcu_read_unlock(); 4814b106518SChao Yu } 4824b106518SChao Yu 4834b106518SChao Yu retry: 4844b106518SChao Yu if (!e) 48532410577SChao Yu new = f2fs_kmem_cache_alloc(ino_entry_slab, 48632410577SChao Yu GFP_NOFS, true, NULL); 48719526d74SChao Yu 48880c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 489769ec6e5SJaegeuk Kim 49067298804SChao Yu spin_lock(&im->ino_lock); 49167298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 49239efac41SJaegeuk Kim if (!e) { 4934b106518SChao Yu if (!new) { 4944b106518SChao Yu spin_unlock(&im->ino_lock); 4954b106518SChao Yu goto retry; 4964b106518SChao Yu } 4974b106518SChao Yu e = new; 49819526d74SChao Yu if (unlikely(radix_tree_insert(&im->ino_root, ino, e))) 49919526d74SChao Yu f2fs_bug_on(sbi, 1); 50019526d74SChao Yu 50139efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 50239efac41SJaegeuk Kim e->ino = ino; 50339efac41SJaegeuk Kim 50467298804SChao Yu list_add_tail(&e->list, &im->ino_list); 5058c402946SJaegeuk Kim if (type != ORPHAN_INO) 50667298804SChao Yu im->ino_num++; 50739efac41SJaegeuk Kim } 50839d787beSChao Yu 50939d787beSChao Yu if (type == FLUSH_INO) 51039d787beSChao Yu f2fs_set_bit(devidx, (char *)&e->dirty_device); 51139d787beSChao Yu 51267298804SChao Yu spin_unlock(&im->ino_lock); 513769ec6e5SJaegeuk Kim radix_tree_preload_end(); 51480c54505SJaegeuk Kim 5154b106518SChao Yu if (new && e != new) 5164b106518SChao Yu kmem_cache_free(ino_entry_slab, new); 517953e6cc6SJaegeuk Kim } 518953e6cc6SJaegeuk Kim 5196451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 520953e6cc6SJaegeuk Kim { 52167298804SChao Yu struct inode_management *im = &sbi->im[type]; 5226451e041SJaegeuk Kim struct ino_entry *e; 523953e6cc6SJaegeuk Kim 52467298804SChao Yu spin_lock(&im->ino_lock); 52567298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 52639efac41SJaegeuk Kim if (e) { 527953e6cc6SJaegeuk Kim list_del(&e->list); 52867298804SChao Yu radix_tree_delete(&im->ino_root, ino); 52967298804SChao Yu im->ino_num--; 53067298804SChao Yu spin_unlock(&im->ino_lock); 5316451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 532953e6cc6SJaegeuk Kim return; 533953e6cc6SJaegeuk Kim } 53467298804SChao Yu spin_unlock(&im->ino_lock); 535953e6cc6SJaegeuk Kim } 536953e6cc6SJaegeuk Kim 5374d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 538fff04f90SJaegeuk Kim { 539fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 54039d787beSChao Yu __add_ino_entry(sbi, ino, 0, type); 541fff04f90SJaegeuk Kim } 542fff04f90SJaegeuk Kim 5434d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 544fff04f90SJaegeuk Kim { 545fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 546fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 547fff04f90SJaegeuk Kim } 548fff04f90SJaegeuk Kim 5491f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */ 5504d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 551fff04f90SJaegeuk Kim { 55267298804SChao Yu struct inode_management *im = &sbi->im[mode]; 553fff04f90SJaegeuk Kim struct ino_entry *e; 55467298804SChao Yu 55567298804SChao Yu spin_lock(&im->ino_lock); 55667298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 55767298804SChao Yu spin_unlock(&im->ino_lock); 558fff04f90SJaegeuk Kim return e ? true : false; 559fff04f90SJaegeuk Kim } 560fff04f90SJaegeuk Kim 5614d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all) 562fff04f90SJaegeuk Kim { 563fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 564fff04f90SJaegeuk Kim int i; 565fff04f90SJaegeuk Kim 56639d787beSChao Yu for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) { 56767298804SChao Yu struct inode_management *im = &sbi->im[i]; 56867298804SChao Yu 56967298804SChao Yu spin_lock(&im->ino_lock); 57067298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 571fff04f90SJaegeuk Kim list_del(&e->list); 57267298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 573fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 57467298804SChao Yu im->ino_num--; 575fff04f90SJaegeuk Kim } 57667298804SChao Yu spin_unlock(&im->ino_lock); 577fff04f90SJaegeuk Kim } 578fff04f90SJaegeuk Kim } 579fff04f90SJaegeuk Kim 5804d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 58139d787beSChao Yu unsigned int devidx, int type) 58239d787beSChao Yu { 58339d787beSChao Yu __add_ino_entry(sbi, ino, devidx, type); 58439d787beSChao Yu } 58539d787beSChao Yu 5864d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 58739d787beSChao Yu unsigned int devidx, int type) 58839d787beSChao Yu { 58939d787beSChao Yu struct inode_management *im = &sbi->im[type]; 59039d787beSChao Yu struct ino_entry *e; 59139d787beSChao Yu bool is_dirty = false; 59239d787beSChao Yu 59339d787beSChao Yu spin_lock(&im->ino_lock); 59439d787beSChao Yu e = radix_tree_lookup(&im->ino_root, ino); 59539d787beSChao Yu if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device)) 59639d787beSChao Yu is_dirty = true; 59739d787beSChao Yu spin_unlock(&im->ino_lock); 59839d787beSChao Yu return is_dirty; 59939d787beSChao Yu } 60039d787beSChao Yu 6014d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi) 602127e670aSJaegeuk Kim { 60367298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 604127e670aSJaegeuk Kim int err = 0; 605127e670aSJaegeuk Kim 60667298804SChao Yu spin_lock(&im->ino_lock); 607cb78942bSJaegeuk Kim 6081ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) { 609cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock); 610c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_ORPHAN); 611cb78942bSJaegeuk Kim return -ENOSPC; 612cb78942bSJaegeuk Kim } 6137fa750a1SArnd Bergmann 61467298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 615127e670aSJaegeuk Kim err = -ENOSPC; 616cbd56e7dSJaegeuk Kim else 61767298804SChao Yu im->ino_num++; 61867298804SChao Yu spin_unlock(&im->ino_lock); 6190d47c1adSGu Zheng 620127e670aSJaegeuk Kim return err; 621127e670aSJaegeuk Kim } 622127e670aSJaegeuk Kim 6234d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi) 624cbd56e7dSJaegeuk Kim { 62567298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 62667298804SChao Yu 62767298804SChao Yu spin_lock(&im->ino_lock); 62867298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 62967298804SChao Yu im->ino_num--; 63067298804SChao Yu spin_unlock(&im->ino_lock); 631cbd56e7dSJaegeuk Kim } 632cbd56e7dSJaegeuk Kim 6334d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode) 634127e670aSJaegeuk Kim { 63539efac41SJaegeuk Kim /* add new orphan ino entry into list */ 63639d787beSChao Yu __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO); 6374d57b86dSChao Yu f2fs_update_inode_page(inode); 638127e670aSJaegeuk Kim } 639127e670aSJaegeuk Kim 6404d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 641127e670aSJaegeuk Kim { 642953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 6436451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 644127e670aSJaegeuk Kim } 645127e670aSJaegeuk Kim 6468c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 647127e670aSJaegeuk Kim { 6488c14bfadSChao Yu struct inode *inode; 6495905f9afSJaegeuk Kim struct node_info ni; 65076a45e3cSChao Yu int err; 6518c14bfadSChao Yu 6525905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino); 6538c14bfadSChao Yu if (IS_ERR(inode)) { 6548c14bfadSChao Yu /* 6558c14bfadSChao Yu * there should be a bug that we can't find the entry 6568c14bfadSChao Yu * to orphan inode. 6578c14bfadSChao Yu */ 6588c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 6598c14bfadSChao Yu return PTR_ERR(inode); 6608c14bfadSChao Yu } 6618c14bfadSChao Yu 66210a26878SChao Yu err = f2fs_dquot_initialize(inode); 663a515d12fSSheng Yong if (err) { 664a515d12fSSheng Yong iput(inode); 6650f9ec2a8SJaegeuk Kim goto err_out; 666a515d12fSSheng Yong } 6670f9ec2a8SJaegeuk Kim 668127e670aSJaegeuk Kim clear_nlink(inode); 669127e670aSJaegeuk Kim 670127e670aSJaegeuk Kim /* truncate all the data during iput */ 671127e670aSJaegeuk Kim iput(inode); 6725905f9afSJaegeuk Kim 673a9419b63SJaegeuk Kim err = f2fs_get_node_info(sbi, ino, &ni, false); 6747735730dSChao Yu if (err) 6757735730dSChao Yu goto err_out; 6765905f9afSJaegeuk Kim 6775905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */ 6785905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) { 6790f9ec2a8SJaegeuk Kim err = -EIO; 6800f9ec2a8SJaegeuk Kim goto err_out; 6815905f9afSJaegeuk Kim } 6828c14bfadSChao Yu return 0; 6830f9ec2a8SJaegeuk Kim 6840f9ec2a8SJaegeuk Kim err_out: 6850f9ec2a8SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 686dcbb4c10SJoe Perches f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.", 6870f9ec2a8SJaegeuk Kim __func__, ino); 6880f9ec2a8SJaegeuk Kim return err; 689127e670aSJaegeuk Kim } 690127e670aSJaegeuk Kim 6914d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) 692127e670aSJaegeuk Kim { 6933c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 6944b2414d0SChao Yu unsigned int s_flags = sbi->sb->s_flags; 6954b2414d0SChao Yu int err = 0; 696ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA 697ea676733SJaegeuk Kim int quota_enabled; 698ea676733SJaegeuk Kim #endif 699127e670aSJaegeuk Kim 700aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 7018c14bfadSChao Yu return 0; 702127e670aSJaegeuk Kim 703b61af314SChao Yu if (bdev_read_only(sbi->sb->s_bdev)) { 704dcbb4c10SJoe Perches f2fs_info(sbi, "write access unavailable, skipping orphan cleanup"); 705b61af314SChao Yu return 0; 706b61af314SChao Yu } 707b61af314SChao Yu 7081751e8a6SLinus Torvalds if (s_flags & SB_RDONLY) { 709dcbb4c10SJoe Perches f2fs_info(sbi, "orphan cleanup on readonly fs"); 7101751e8a6SLinus Torvalds sbi->sb->s_flags &= ~SB_RDONLY; 7114b2414d0SChao Yu } 7124b2414d0SChao Yu 7134b2414d0SChao Yu #ifdef CONFIG_QUOTA 71476cf05d7SSheng Yong /* 71576cf05d7SSheng Yong * Turn on quotas which were not enabled for read-only mounts if 71676cf05d7SSheng Yong * filesystem has quota feature, so that they are updated correctly. 71776cf05d7SSheng Yong */ 7181751e8a6SLinus Torvalds quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY); 7194b2414d0SChao Yu #endif 7204b2414d0SChao Yu 72155141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 7223c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 723127e670aSJaegeuk Kim 7244d57b86dSChao Yu f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 725662befdaSChao Yu 7263c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 7277735730dSChao Yu struct page *page; 728127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 729127e670aSJaegeuk Kim 7307735730dSChao Yu page = f2fs_get_meta_page(sbi, start_blk + i); 7317735730dSChao Yu if (IS_ERR(page)) { 7327735730dSChao Yu err = PTR_ERR(page); 7337735730dSChao Yu goto out; 7347735730dSChao Yu } 7357735730dSChao Yu 736127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 737127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 738127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 7395f029c04SYi Zhuang 7408c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 7418c14bfadSChao Yu if (err) { 7428c14bfadSChao Yu f2fs_put_page(page, 1); 7434b2414d0SChao Yu goto out; 7448c14bfadSChao Yu } 745127e670aSJaegeuk Kim } 746127e670aSJaegeuk Kim f2fs_put_page(page, 1); 747127e670aSJaegeuk Kim } 748127e670aSJaegeuk Kim /* clear Orphan Flag */ 749aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 7504b2414d0SChao Yu out: 7511378752bSChao Yu set_sbi_flag(sbi, SBI_IS_RECOVERED); 7521378752bSChao Yu 7534b2414d0SChao Yu #ifdef CONFIG_QUOTA 7544b2414d0SChao Yu /* Turn quotas off */ 755ea676733SJaegeuk Kim if (quota_enabled) 7564b2414d0SChao Yu f2fs_quota_off_umount(sbi->sb); 7574b2414d0SChao Yu #endif 7581751e8a6SLinus Torvalds sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 7594b2414d0SChao Yu 7604b2414d0SChao Yu return err; 761127e670aSJaegeuk Kim } 762127e670aSJaegeuk Kim 763127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 764127e670aSJaegeuk Kim { 765502c6e0bSGu Zheng struct list_head *head; 766127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 767127e670aSJaegeuk Kim unsigned int nentries = 0; 768bd936f84SChao Yu unsigned short index = 1; 7698c402946SJaegeuk Kim unsigned short orphan_blocks; 7704531929eSGu Zheng struct page *page = NULL; 7716451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 77267298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 773127e670aSJaegeuk Kim 77467298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 7758c402946SJaegeuk Kim 776d6c67a4fSJaegeuk Kim /* 777d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 778d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 779d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 780d6c67a4fSJaegeuk Kim */ 78167298804SChao Yu head = &im->ino_list; 782127e670aSJaegeuk Kim 783127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 784502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 785502c6e0bSGu Zheng if (!page) { 7864d57b86dSChao Yu page = f2fs_grab_meta_page(sbi, start_blk++); 787502c6e0bSGu Zheng orphan_blk = 788502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 789502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 790502c6e0bSGu Zheng } 791127e670aSJaegeuk Kim 79236795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 793127e670aSJaegeuk Kim 79436795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 795127e670aSJaegeuk Kim /* 796127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 797127e670aSJaegeuk Kim * then we need to flush current orphan blocks 798127e670aSJaegeuk Kim * and bring another one in memory 799127e670aSJaegeuk Kim */ 800127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 801127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 802127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 803127e670aSJaegeuk Kim set_page_dirty(page); 804127e670aSJaegeuk Kim f2fs_put_page(page, 1); 805127e670aSJaegeuk Kim index++; 806127e670aSJaegeuk Kim nentries = 0; 807127e670aSJaegeuk Kim page = NULL; 808127e670aSJaegeuk Kim } 809127e670aSJaegeuk Kim } 810127e670aSJaegeuk Kim 811502c6e0bSGu Zheng if (page) { 812127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 813127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 814127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 815127e670aSJaegeuk Kim set_page_dirty(page); 816127e670aSJaegeuk Kim f2fs_put_page(page, 1); 817502c6e0bSGu Zheng } 818127e670aSJaegeuk Kim } 819127e670aSJaegeuk Kim 820d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi, 821d7eb8f1cSChao Yu struct f2fs_checkpoint *ckpt) 822d7eb8f1cSChao Yu { 823d7eb8f1cSChao Yu unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset); 824d7eb8f1cSChao Yu __u32 chksum; 825d7eb8f1cSChao Yu 826d7eb8f1cSChao Yu chksum = f2fs_crc32(sbi, ckpt, chksum_ofs); 827d7eb8f1cSChao Yu if (chksum_ofs < CP_CHKSUM_OFFSET) { 828d7eb8f1cSChao Yu chksum_ofs += sizeof(chksum); 829d7eb8f1cSChao Yu chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs, 830d7eb8f1cSChao Yu F2FS_BLKSIZE - chksum_ofs); 831d7eb8f1cSChao Yu } 832d7eb8f1cSChao Yu return chksum; 833d7eb8f1cSChao Yu } 834d7eb8f1cSChao Yu 835fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 836fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page, 837fc0065adSTiezhu Yang unsigned long long *version) 838fc0065adSTiezhu Yang { 839fc0065adSTiezhu Yang size_t crc_offset = 0; 840d7eb8f1cSChao Yu __u32 crc; 841fc0065adSTiezhu Yang 8424d57b86dSChao Yu *cp_page = f2fs_get_meta_page(sbi, cp_addr); 8437735730dSChao Yu if (IS_ERR(*cp_page)) 8447735730dSChao Yu return PTR_ERR(*cp_page); 8457735730dSChao Yu 846fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 847fc0065adSTiezhu Yang 848fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 849d7eb8f1cSChao Yu if (crc_offset < CP_MIN_CHKSUM_OFFSET || 850d7eb8f1cSChao Yu crc_offset > CP_CHKSUM_OFFSET) { 851d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 852dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset); 853fc0065adSTiezhu Yang return -EINVAL; 854fc0065adSTiezhu Yang } 855fc0065adSTiezhu Yang 856d7eb8f1cSChao Yu crc = f2fs_checkpoint_chksum(sbi, *cp_block); 857d7eb8f1cSChao Yu if (crc != cur_cp_crc(*cp_block)) { 858d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 859dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc value"); 860fc0065adSTiezhu Yang return -EINVAL; 861fc0065adSTiezhu Yang } 862fc0065adSTiezhu Yang 863fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block); 864fc0065adSTiezhu Yang return 0; 865fc0065adSTiezhu Yang } 866fc0065adSTiezhu Yang 867127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 868127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 869127e670aSJaegeuk Kim { 870fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 871fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL; 872127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 8735b5b4f85SChao Yu unsigned int cp_blocks; 874fc0065adSTiezhu Yang int err; 875127e670aSJaegeuk Kim 876fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 877fc0065adSTiezhu Yang &cp_page_1, version); 878fc0065adSTiezhu Yang if (err) 879d3f07c04SChao Yu return NULL; 880c9b60788SChao Yu 8815b5b4f85SChao Yu cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count); 8825b5b4f85SChao Yu 8835b5b4f85SChao Yu if (cp_blocks > sbi->blocks_per_seg || cp_blocks <= F2FS_CP_PACKS) { 884dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", 885c9b60788SChao Yu le32_to_cpu(cp_block->cp_pack_total_block_count)); 886d3f07c04SChao Yu goto invalid_cp; 887c9b60788SChao Yu } 888fc0065adSTiezhu Yang pre_version = *version; 889127e670aSJaegeuk Kim 8905b5b4f85SChao Yu cp_addr += cp_blocks - 1; 891fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 892fc0065adSTiezhu Yang &cp_page_2, version); 893fc0065adSTiezhu Yang if (err) 894d3f07c04SChao Yu goto invalid_cp; 895fc0065adSTiezhu Yang cur_version = *version; 896127e670aSJaegeuk Kim 897127e670aSJaegeuk Kim if (cur_version == pre_version) { 898127e670aSJaegeuk Kim *version = cur_version; 899127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 900127e670aSJaegeuk Kim return cp_page_1; 901127e670aSJaegeuk Kim } 902127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 903d3f07c04SChao Yu invalid_cp: 904127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 905127e670aSJaegeuk Kim return NULL; 906127e670aSJaegeuk Kim } 907127e670aSJaegeuk Kim 9084d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi) 909127e670aSJaegeuk Kim { 910127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 911127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 912127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 913127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 914127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 915127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 91655141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 9171dbe4152SChangman Lee block_t cp_blk_no; 9181dbe4152SChangman Lee int i; 91910f966bbSChao Yu int err; 920127e670aSJaegeuk Kim 9210b6d4ca0SEric Biggers sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks), 922026f0507SKees Cook GFP_KERNEL); 923127e670aSJaegeuk Kim if (!sbi->ckpt) 924127e670aSJaegeuk Kim return -ENOMEM; 925127e670aSJaegeuk Kim /* 926127e670aSJaegeuk Kim * Finding out valid cp block involves read both 927127e670aSJaegeuk Kim * sets( cp pack 1 and cp pack 2) 928127e670aSJaegeuk Kim */ 929127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 930127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 931127e670aSJaegeuk Kim 932127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 933f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 934f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 935127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 936127e670aSJaegeuk Kim 937127e670aSJaegeuk Kim if (cp1 && cp2) { 938127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 939127e670aSJaegeuk Kim cur_page = cp2; 940127e670aSJaegeuk Kim else 941127e670aSJaegeuk Kim cur_page = cp1; 942127e670aSJaegeuk Kim } else if (cp1) { 943127e670aSJaegeuk Kim cur_page = cp1; 944127e670aSJaegeuk Kim } else if (cp2) { 945127e670aSJaegeuk Kim cur_page = cp2; 946127e670aSJaegeuk Kim } else { 94710f966bbSChao Yu err = -EFSCORRUPTED; 948127e670aSJaegeuk Kim goto fail_no_cp; 949127e670aSJaegeuk Kim } 950127e670aSJaegeuk Kim 951127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 952127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 953127e670aSJaegeuk Kim 9548508e44aSJaegeuk Kim if (cur_page == cp1) 9558508e44aSJaegeuk Kim sbi->cur_cp_pack = 1; 9568508e44aSJaegeuk Kim else 9578508e44aSJaegeuk Kim sbi->cur_cp_pack = 2; 958984ec63cSShawn Lin 959e494c2f9SChao Yu /* Sanity checking of checkpoint */ 96010f966bbSChao Yu if (f2fs_sanity_check_ckpt(sbi)) { 96110f966bbSChao Yu err = -EFSCORRUPTED; 962e494c2f9SChao Yu goto free_fail_no_cp; 96310f966bbSChao Yu } 964e494c2f9SChao Yu 9651dbe4152SChangman Lee if (cp_blks <= 1) 9661dbe4152SChangman Lee goto done; 9671dbe4152SChangman Lee 9681dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 9691dbe4152SChangman Lee if (cur_page == cp2) 9701dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 9711dbe4152SChangman Lee 9721dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 9731dbe4152SChangman Lee void *sit_bitmap_ptr; 9741dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 9751dbe4152SChangman Lee 9764d57b86dSChao Yu cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i); 97710f966bbSChao Yu if (IS_ERR(cur_page)) { 97810f966bbSChao Yu err = PTR_ERR(cur_page); 9797735730dSChao Yu goto free_fail_no_cp; 98010f966bbSChao Yu } 9811dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 9821dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 9831dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 9841dbe4152SChangman Lee } 9851dbe4152SChangman Lee done: 986127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 987127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 988127e670aSJaegeuk Kim return 0; 989127e670aSJaegeuk Kim 990a2125ff7SJaegeuk Kim free_fail_no_cp: 991a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1); 992a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1); 993127e670aSJaegeuk Kim fail_no_cp: 9945222595dSJaegeuk Kim kvfree(sbi->ckpt); 99510f966bbSChao Yu return err; 996127e670aSJaegeuk Kim } 997127e670aSJaegeuk Kim 998c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 999127e670aSJaegeuk Kim { 10004081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1001c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 10022d7b822aSChao Yu 100391942321SJaegeuk Kim if (is_inode_flag_set(inode, flag)) 10042710fd7eSChao Yu return; 10052d7b822aSChao Yu 100691942321SJaegeuk Kim set_inode_flag(inode, flag); 100799f4b917SChao Yu if (!f2fs_is_volatile_file(inode)) 100899f4b917SChao Yu list_add_tail(&F2FS_I(inode)->dirty_list, 100999f4b917SChao Yu &sbi->inode_list[type]); 101033fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 10115deb8267SJaegeuk Kim } 10125deb8267SJaegeuk Kim 1013c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 10146ad7609aSChao Yu { 1015c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 10166ad7609aSChao Yu 101791942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 10186ad7609aSChao Yu return; 10196ad7609aSChao Yu 102091942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list); 102191942321SJaegeuk Kim clear_inode_flag(inode, flag); 102233fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 10236ad7609aSChao Yu } 10246ad7609aSChao Yu 10254f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio) 10265deb8267SJaegeuk Kim { 10274081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1028c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 10295deb8267SJaegeuk Kim 10305ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 10315ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 1032127e670aSJaegeuk Kim return; 10337bd59381SGu Zheng 1034c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 10351c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 1036c227f912SChao Yu __add_dirty_inode(inode, type); 1037b951a4ecSYunlei He inode_inc_dirty_pages(inode); 10381c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]); 10391c4bf763SJaegeuk Kim 10404f5e34f7SMatthew Wilcox (Oracle) set_page_private_reference(&folio->page); 10415deb8267SJaegeuk Kim } 1042127e670aSJaegeuk Kim 10434d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode) 1044127e670aSJaegeuk Kim { 10454081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1046c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 1047127e670aSJaegeuk Kim 1048c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 1049c227f912SChao Yu !S_ISLNK(inode->i_mode)) 1050127e670aSJaegeuk Kim return; 1051127e670aSJaegeuk Kim 105210aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 105310aa97c3SJaegeuk Kim return; 105410aa97c3SJaegeuk Kim 1055c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1056c227f912SChao Yu __remove_dirty_inode(inode, type); 1057c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1058afc3eda2SJaegeuk Kim } 105974d0b917SJaegeuk Kim 10604d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 1061127e670aSJaegeuk Kim { 1062ce3b7d80SGu Zheng struct list_head *head; 1063127e670aSJaegeuk Kim struct inode *inode; 10642710fd7eSChao Yu struct f2fs_inode_info *fi; 10654cf18537SChao Yu bool is_dir = (type == DIR_INODE); 10664db08d01SJaegeuk Kim unsigned long ino = 0; 10674cf18537SChao Yu 10684cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 10694cf18537SChao Yu get_pages(sbi, is_dir ? 10704cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1071127e670aSJaegeuk Kim retry: 10729b664822SZhang Qilong if (unlikely(f2fs_cp_error(sbi))) { 10739b664822SZhang Qilong trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10749b664822SZhang Qilong get_pages(sbi, is_dir ? 10759b664822SZhang Qilong F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10766d5a1495SChao Yu return -EIO; 10779b664822SZhang Qilong } 1078af41d3eeSJaegeuk Kim 1079c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1080ce3b7d80SGu Zheng 1081c227f912SChao Yu head = &sbi->inode_list[type]; 1082127e670aSJaegeuk Kim if (list_empty(head)) { 1083c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 10844cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10854cf18537SChao Yu get_pages(sbi, is_dir ? 10864cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10876d5a1495SChao Yu return 0; 1088127e670aSJaegeuk Kim } 1089939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 10902710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 1091c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1092127e670aSJaegeuk Kim if (inode) { 10934db08d01SJaegeuk Kim unsigned long cur_ino = inode->i_ino; 10944db08d01SJaegeuk Kim 1095b0af6d49SChao Yu F2FS_I(inode)->cp_task = current; 1096b0af6d49SChao Yu 109787d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 1098b0af6d49SChao Yu 1099b0af6d49SChao Yu F2FS_I(inode)->cp_task = NULL; 1100b0af6d49SChao Yu 1101127e670aSJaegeuk Kim iput(inode); 11024db08d01SJaegeuk Kim /* We need to give cpu to another writers. */ 11032a63531aSYunlei He if (ino == cur_ino) 11044db08d01SJaegeuk Kim cond_resched(); 11052a63531aSYunlei He else 11064db08d01SJaegeuk Kim ino = cur_ino; 1107127e670aSJaegeuk Kim } else { 1108127e670aSJaegeuk Kim /* 1109127e670aSJaegeuk Kim * We should submit bio, since it exists several 1110127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 1111127e670aSJaegeuk Kim */ 1112b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 11137ecebe5eSSebastian Andrzej Siewior cond_resched(); 1114127e670aSJaegeuk Kim } 1115127e670aSJaegeuk Kim goto retry; 1116127e670aSJaegeuk Kim } 1117127e670aSJaegeuk Kim 11180f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 11190f18b462SJaegeuk Kim { 11200f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META]; 11210f18b462SJaegeuk Kim struct inode *inode; 11220f18b462SJaegeuk Kim struct f2fs_inode_info *fi; 11230f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 11240f18b462SJaegeuk Kim 11250f18b462SJaegeuk Kim while (total--) { 11260f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 11270f18b462SJaegeuk Kim return -EIO; 11280f18b462SJaegeuk Kim 11290f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 11300f18b462SJaegeuk Kim if (list_empty(head)) { 11310f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11320f18b462SJaegeuk Kim return 0; 11330f18b462SJaegeuk Kim } 1134939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, 11350f18b462SJaegeuk Kim gdirty_list); 11360f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode); 11370f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11380f18b462SJaegeuk Kim if (inode) { 113918340edcSJaegeuk Kim sync_inode_metadata(inode, 0); 114018340edcSJaegeuk Kim 114118340edcSJaegeuk Kim /* it's on eviction */ 114218340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 11434d57b86dSChao Yu f2fs_update_inode_page(inode); 11440f18b462SJaegeuk Kim iput(inode); 11450f18b462SJaegeuk Kim } 1146dee668c1SChao Yu } 11470f18b462SJaegeuk Kim return 0; 11480f18b462SJaegeuk Kim } 11490f18b462SJaegeuk Kim 115059c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi) 115159c9081bSYunlei He { 115259c9081bSYunlei He struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 115359c9081bSYunlei He struct f2fs_nm_info *nm_i = NM_I(sbi); 115459c9081bSYunlei He nid_t last_nid = nm_i->next_scan_nid; 115559c9081bSYunlei He 115659c9081bSYunlei He next_free_nid(sbi, &last_nid); 115759c9081bSYunlei He ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 115859c9081bSYunlei He ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 115959c9081bSYunlei He ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 116059c9081bSYunlei He ckpt->next_free_nid = cpu_to_le32(last_nid); 116159c9081bSYunlei He } 116259c9081bSYunlei He 1163af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi) 1164af033b2aSChao Yu { 1165db6ec53bSJaegeuk Kim bool ret = false; 1166db6ec53bSJaegeuk Kim 1167af033b2aSChao Yu if (!is_journalled_quota(sbi)) 1168af033b2aSChao Yu return false; 1169db6ec53bSJaegeuk Kim 1170e4544b63STim Murray if (!f2fs_down_write_trylock(&sbi->quota_sem)) 1171a5c00422SChao Yu return true; 1172db6ec53bSJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { 1173db6ec53bSJaegeuk Kim ret = false; 1174db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { 1175db6ec53bSJaegeuk Kim ret = false; 1176db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) { 1177db6ec53bSJaegeuk Kim clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1178db6ec53bSJaegeuk Kim ret = true; 1179db6ec53bSJaegeuk Kim } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { 1180db6ec53bSJaegeuk Kim ret = true; 1181db6ec53bSJaegeuk Kim } 1182e4544b63STim Murray f2fs_up_write(&sbi->quota_sem); 1183db6ec53bSJaegeuk Kim return ret; 1184af033b2aSChao Yu } 1185af033b2aSChao Yu 11860a8165d7SJaegeuk Kim /* 1187127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 1188127e670aSJaegeuk Kim */ 1189cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 1190127e670aSJaegeuk Kim { 1191127e670aSJaegeuk Kim struct writeback_control wbc = { 1192127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 1193127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 1194127e670aSJaegeuk Kim .for_reclaim = 0, 1195127e670aSJaegeuk Kim }; 1196af033b2aSChao Yu int err = 0, cnt = 0; 1197c718379bSJaegeuk Kim 119834c061adSSayali Lokhande /* 119934c061adSSayali Lokhande * Let's flush inline_data in dirty node pages. 120034c061adSSayali Lokhande */ 120134c061adSSayali Lokhande f2fs_flush_inline_data(sbi); 120234c061adSSayali Lokhande 1203af033b2aSChao Yu retry_flush_quotas: 1204db6ec53bSJaegeuk Kim f2fs_lock_all(sbi); 1205af033b2aSChao Yu if (__need_flush_quota(sbi)) { 1206af033b2aSChao Yu int locked; 1207af033b2aSChao Yu 1208af033b2aSChao Yu if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { 1209af033b2aSChao Yu set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1210db6ec53bSJaegeuk Kim set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1211af033b2aSChao Yu goto retry_flush_dents; 1212af033b2aSChao Yu } 1213db6ec53bSJaegeuk Kim f2fs_unlock_all(sbi); 1214af033b2aSChao Yu 1215af033b2aSChao Yu /* only failed during mount/umount/freeze/quotactl */ 1216af033b2aSChao Yu locked = down_read_trylock(&sbi->sb->s_umount); 1217af033b2aSChao Yu f2fs_quota_sync(sbi->sb, -1); 1218af033b2aSChao Yu if (locked) 1219af033b2aSChao Yu up_read(&sbi->sb->s_umount); 1220af033b2aSChao Yu cond_resched(); 1221af033b2aSChao Yu goto retry_flush_quotas; 1222af033b2aSChao Yu } 1223af033b2aSChao Yu 1224af033b2aSChao Yu retry_flush_dents: 1225127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 1226127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 1227e479556bSGu Zheng f2fs_unlock_all(sbi); 12284d57b86dSChao Yu err = f2fs_sync_dirty_inodes(sbi, DIR_INODE); 12296d5a1495SChao Yu if (err) 12301f5f11a3SJaegeuk Kim return err; 123130973883SJaegeuk Kim cond_resched(); 1232af033b2aSChao Yu goto retry_flush_quotas; 1233127e670aSJaegeuk Kim } 1234127e670aSJaegeuk Kim 123559c9081bSYunlei He /* 123659c9081bSYunlei He * POR: we should ensure that there are no dirty node pages 123759c9081bSYunlei He * until finishing nat/sit flush. inode->i_blocks can be updated. 123859c9081bSYunlei He */ 1239e4544b63STim Murray f2fs_down_write(&sbi->node_change); 124059c9081bSYunlei He 12410f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 1242e4544b63STim Murray f2fs_up_write(&sbi->node_change); 12430f18b462SJaegeuk Kim f2fs_unlock_all(sbi); 12440f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi); 12450f18b462SJaegeuk Kim if (err) 12461f5f11a3SJaegeuk Kim return err; 124730973883SJaegeuk Kim cond_resched(); 1248af033b2aSChao Yu goto retry_flush_quotas; 12490f18b462SJaegeuk Kim } 12500f18b462SJaegeuk Kim 125139936837SJaegeuk Kim retry_flush_nodes: 1252e4544b63STim Murray f2fs_down_write(&sbi->node_write); 1253127e670aSJaegeuk Kim 1254127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1255e4544b63STim Murray f2fs_up_write(&sbi->node_write); 1256c29fd0c0SChao Yu atomic_inc(&sbi->wb_sync_req[NODE]); 12574d57b86dSChao Yu err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); 1258c29fd0c0SChao Yu atomic_dec(&sbi->wb_sync_req[NODE]); 12596d5a1495SChao Yu if (err) { 1260e4544b63STim Murray f2fs_up_write(&sbi->node_change); 1261cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 12621f5f11a3SJaegeuk Kim return err; 1263cf779cabSJaegeuk Kim } 126430973883SJaegeuk Kim cond_resched(); 126539936837SJaegeuk Kim goto retry_flush_nodes; 1266127e670aSJaegeuk Kim } 126759c9081bSYunlei He 126859c9081bSYunlei He /* 126959c9081bSYunlei He * sbi->node_change is used only for AIO write_begin path which produces 127059c9081bSYunlei He * dirty node blocks and some checkpoint values by block allocation. 127159c9081bSYunlei He */ 127259c9081bSYunlei He __prepare_cp_block(sbi); 1273e4544b63STim Murray f2fs_up_write(&sbi->node_change); 1274cf779cabSJaegeuk Kim return err; 1275127e670aSJaegeuk Kim } 1276127e670aSJaegeuk Kim 1277127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 1278127e670aSJaegeuk Kim { 1279e4544b63STim Murray f2fs_up_write(&sbi->node_write); 1280e479556bSGu Zheng f2fs_unlock_all(sbi); 1281127e670aSJaegeuk Kim } 1282127e670aSJaegeuk Kim 1283bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type) 1284fb51b5efSChangman Lee { 1285fb51b5efSChangman Lee DEFINE_WAIT(wait); 1286fb51b5efSChangman Lee 1287fb51b5efSChangman Lee for (;;) { 1288bf22c3ccSSahitya Tummala if (!get_pages(sbi, type)) 1289fb51b5efSChangman Lee break; 1290fb51b5efSChangman Lee 1291af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1292af697c0fSJaegeuk Kim break; 1293af697c0fSJaegeuk Kim 12949c30df7cSJaegeuk Kim if (type == F2FS_DIRTY_META) 12959c30df7cSJaegeuk Kim f2fs_sync_meta_pages(sbi, META, LONG_MAX, 12969c30df7cSJaegeuk Kim FS_CP_META_IO); 12971fd28018SJaegeuk Kim else if (type == F2FS_WB_CP_DATA) 12981fd28018SJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 1299828add77SJaegeuk Kim 1300828add77SJaegeuk Kim prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 13015df7731fSChao Yu io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1302fb51b5efSChangman Lee } 1303fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 1304fb51b5efSChangman Lee } 1305fb51b5efSChangman Lee 1306e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1307e4c5d848SJaegeuk Kim { 1308e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1309e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1310d1aa2453SChao Yu unsigned long flags; 1311e4c5d848SJaegeuk Kim 131294c821fbSChao Yu if (cpc->reason & CP_UMOUNT) { 1313b702c83eSChao Yu if (le32_to_cpu(ckpt->cp_pack_total_block_count) + 1314b702c83eSChao Yu NM_I(sbi)->nat_bits_blocks > sbi->blocks_per_seg) { 131594c821fbSChao Yu clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 131694c821fbSChao Yu f2fs_notice(sbi, "Disable nat_bits due to no space"); 131794c821fbSChao Yu } else if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG) && 131894c821fbSChao Yu f2fs_nat_bitmap_enabled(sbi)) { 131994c821fbSChao Yu f2fs_enable_nat_bits(sbi); 132094c821fbSChao Yu set_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 132194c821fbSChao Yu f2fs_notice(sbi, "Rebuild and enable nat_bits"); 132294c821fbSChao Yu } 132394c821fbSChao Yu } 1324e4c5d848SJaegeuk Kim 132594c821fbSChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 132622ad0b6aSJaegeuk Kim 13271f43e2adSChao Yu if (cpc->reason & CP_TRIMMED) 13281f43e2adSChao Yu __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 1329cd36d7a1SChao Yu else 1330cd36d7a1SChao Yu __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 13311f43e2adSChao Yu 1332c473f1a9SChao Yu if (cpc->reason & CP_UMOUNT) 1333e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1334e4c5d848SJaegeuk Kim else 1335e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1336e4c5d848SJaegeuk Kim 1337c473f1a9SChao Yu if (cpc->reason & CP_FASTBOOT) 1338e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1339e4c5d848SJaegeuk Kim else 1340e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1341e4c5d848SJaegeuk Kim 1342e4c5d848SJaegeuk Kim if (orphan_num) 1343e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1344e4c5d848SJaegeuk Kim else 1345e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1346e4c5d848SJaegeuk Kim 1347c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1348e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1349e4c5d848SJaegeuk Kim 1350c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS)) 1351c84ef3c5SSahitya Tummala __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1352c84ef3c5SSahitya Tummala else 1353c84ef3c5SSahitya Tummala __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1354c84ef3c5SSahitya Tummala 13554354994fSDaniel Rosenberg if (is_sbi_flag_set(sbi, SBI_CP_DISABLED)) 13564354994fSDaniel Rosenberg __set_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13574354994fSDaniel Rosenberg else 13584354994fSDaniel Rosenberg __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13594354994fSDaniel Rosenberg 1360db610a64SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK)) 1361db610a64SJaegeuk Kim __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1362db610a64SJaegeuk Kim else 1363db610a64SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1364db610a64SJaegeuk Kim 1365af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) 1366af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1367bc88ac96SJaegeuk Kim else 1368bc88ac96SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1369af033b2aSChao Yu 1370af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) 1371af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1372af033b2aSChao Yu 1373e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */ 1374e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1375f2367923SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); 1376e4c5d848SJaegeuk Kim 1377d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1378e4c5d848SJaegeuk Kim } 1379e4c5d848SJaegeuk Kim 138046706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi, 138146706d59SGao Xiang void *src, block_t blk_addr) 138246706d59SGao Xiang { 138346706d59SGao Xiang struct writeback_control wbc = { 138446706d59SGao Xiang .for_reclaim = 0, 138546706d59SGao Xiang }; 138646706d59SGao Xiang 138746706d59SGao Xiang /* 138846706d59SGao Xiang * pagevec_lookup_tag and lock_page again will take 13894d57b86dSChao Yu * some extra time. Therefore, f2fs_update_meta_pages and 13904d57b86dSChao Yu * f2fs_sync_meta_pages are combined in this function. 139146706d59SGao Xiang */ 13924d57b86dSChao Yu struct page *page = f2fs_grab_meta_page(sbi, blk_addr); 139346706d59SGao Xiang int err; 139446706d59SGao Xiang 1395bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 13968d64d365SChao Yu 13978d64d365SChao Yu memcpy(page_address(page), src, PAGE_SIZE); 13988d64d365SChao Yu 13998d64d365SChao Yu set_page_dirty(page); 140046706d59SGao Xiang if (unlikely(!clear_page_dirty_for_io(page))) 140146706d59SGao Xiang f2fs_bug_on(sbi, 1); 140246706d59SGao Xiang 140346706d59SGao Xiang /* writeout cp pack 2 page */ 140446706d59SGao Xiang err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO); 1405af697c0fSJaegeuk Kim if (unlikely(err && f2fs_cp_error(sbi))) { 1406af697c0fSJaegeuk Kim f2fs_put_page(page, 1); 1407af697c0fSJaegeuk Kim return; 1408af697c0fSJaegeuk Kim } 140946706d59SGao Xiang 1410af697c0fSJaegeuk Kim f2fs_bug_on(sbi, err); 141146706d59SGao Xiang f2fs_put_page(page, 0); 141246706d59SGao Xiang 141346706d59SGao Xiang /* submit checkpoint (with barrier if NOBARRIER is not set) */ 141446706d59SGao Xiang f2fs_submit_merged_write(sbi, META_FLUSH); 141546706d59SGao Xiang } 141646706d59SGao Xiang 14173a0a9cbcSChao Yu static inline u64 get_sectors_written(struct block_device *bdev) 14183a0a9cbcSChao Yu { 1419ff49c86fSLinus Torvalds return (u64)part_stat_read(bdev, sectors[STAT_WRITE]); 14203a0a9cbcSChao Yu } 14213a0a9cbcSChao Yu 14223a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi) 14233a0a9cbcSChao Yu { 14243a0a9cbcSChao Yu if (f2fs_is_multi_device(sbi)) { 14253a0a9cbcSChao Yu u64 sectors = 0; 14263a0a9cbcSChao Yu int i; 14273a0a9cbcSChao Yu 14283a0a9cbcSChao Yu for (i = 0; i < sbi->s_ndevs; i++) 14293a0a9cbcSChao Yu sectors += get_sectors_written(FDEV(i).bdev); 14303a0a9cbcSChao Yu 14313a0a9cbcSChao Yu return sectors; 14323a0a9cbcSChao Yu } 14333a0a9cbcSChao Yu 14343a0a9cbcSChao Yu return get_sectors_written(sbi->sb->s_bdev); 14353a0a9cbcSChao Yu } 14363a0a9cbcSChao Yu 1437c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1438127e670aSJaegeuk Kim { 1439127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 144077041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 1441d1aa2453SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags; 1442127e670aSJaegeuk Kim block_t start_blk; 1443127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 14447e586fa0SJaegeuk Kim __u32 crc32 = 0; 1445127e670aSJaegeuk Kim int i; 144655141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 14478f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 14488f1dbbbbSShuoran Liu u64 kbytes_written; 14491228b482SChao Yu int err; 1450127e670aSJaegeuk Kim 1451127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 14524d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1453127e670aSJaegeuk Kim 14547a88ddb5SChao Yu /* start to update checkpoint, cp ver is already updated previously */ 1455a1f72ac2SChao Yu ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true)); 1456127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1457b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1458127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 1459127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 1460127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 1461127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 1462127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 1463127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 1464127e670aSJaegeuk Kim } 1465b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1466127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 1467127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 1468127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 1469127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 1470127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 1471127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 1472127e670aSJaegeuk Kim } 1473127e670aSJaegeuk Kim 1474127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 14754d57b86dSChao Yu data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); 1476d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1477b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1478aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1479127e670aSJaegeuk Kim else 1480aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1481d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1482127e670aSJaegeuk Kim 148367298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 14841dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 14851dbe4152SChangman Lee orphan_blocks); 1486127e670aSJaegeuk Kim 1487119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1488b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 14891dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14901dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1491119ee914SJaegeuk Kim else 1492b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 14931dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14941dbe4152SChangman Lee orphan_blocks); 1495119ee914SJaegeuk Kim 1496e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */ 1497e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc); 1498a468f0efSJaegeuk Kim 1499127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1500127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1501127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1502127e670aSJaegeuk Kim 1503d7eb8f1cSChao Yu crc32 = f2fs_checkpoint_chksum(sbi, ckpt); 15047e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 15057e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1506127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1507127e670aSJaegeuk Kim 15088508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi); 1509127e670aSJaegeuk Kim 151022ad0b6aSJaegeuk Kim /* write nat bits */ 151194c821fbSChao Yu if ((cpc->reason & CP_UMOUNT) && 151294c821fbSChao Yu is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) { 151322ad0b6aSJaegeuk Kim __u64 cp_ver = cur_cp_version(ckpt); 151422ad0b6aSJaegeuk Kim block_t blk; 151522ad0b6aSJaegeuk Kim 151622ad0b6aSJaegeuk Kim cp_ver |= ((__u64)crc32 << 32); 151722ad0b6aSJaegeuk Kim *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); 151822ad0b6aSJaegeuk Kim 151922ad0b6aSJaegeuk Kim blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks; 152022ad0b6aSJaegeuk Kim for (i = 0; i < nm_i->nat_bits_blocks; i++) 15214d57b86dSChao Yu f2fs_update_meta_page(sbi, nm_i->nat_bits + 152222ad0b6aSJaegeuk Kim (i << F2FS_BLKSIZE_BITS), blk + i); 152322ad0b6aSJaegeuk Kim } 152422ad0b6aSJaegeuk Kim 1525127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 15264d57b86dSChao Yu f2fs_update_meta_page(sbi, ckpt, start_blk++); 1527127e670aSJaegeuk Kim 1528381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 15294d57b86dSChao Yu f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1530381722d2SChao Yu start_blk++); 15311dbe4152SChangman Lee 153267298804SChao Yu if (orphan_num) { 1533127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1534127e670aSJaegeuk Kim start_blk += orphan_blocks; 1535127e670aSJaegeuk Kim } 1536127e670aSJaegeuk Kim 15374d57b86dSChao Yu f2fs_write_data_summaries(sbi, start_blk); 1538127e670aSJaegeuk Kim start_blk += data_sum_blocks; 15398f1dbbbbSShuoran Liu 15408f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 15418f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 15423a0a9cbcSChao Yu kbytes_written += (f2fs_get_sectors_written(sbi) - 15433a0a9cbcSChao Yu sbi->sectors_written_start) >> 1; 1544b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 15458f1dbbbbSShuoran Liu 1546119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 15474d57b86dSChao Yu f2fs_write_node_summaries(sbi, start_blk); 1548127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1549127e670aSJaegeuk Kim } 1550127e670aSJaegeuk Kim 155146706d59SGao Xiang /* update user_block_counts */ 155246706d59SGao Xiang sbi->last_valid_block_count = sbi->total_valid_block_count; 155346706d59SGao Xiang percpu_counter_set(&sbi->alloc_valid_block_count, 0); 155447c8ebccSJaegeuk Kim percpu_counter_set(&sbi->rf_node_block_count, 0); 1555127e670aSJaegeuk Kim 155646706d59SGao Xiang /* Here, we have one bio having CP pack except cp pack 2 page */ 15574d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1558bf22c3ccSSahitya Tummala /* Wait for all dirty meta pages to be submitted for IO */ 1559bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META); 156046706d59SGao Xiang 156146706d59SGao Xiang /* wait for previous submitted meta pages writeback */ 1562bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1563127e670aSJaegeuk Kim 156446706d59SGao Xiang /* flush all device cache */ 156546706d59SGao Xiang err = f2fs_flush_device_cache(sbi); 156646706d59SGao Xiang if (err) 156746706d59SGao Xiang return err; 1568127e670aSJaegeuk Kim 156946706d59SGao Xiang /* barrier and flush checkpoint cp pack 2 page if it can */ 157046706d59SGao Xiang commit_checkpoint(sbi, ckpt, start_blk); 1571bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 15726a8f8ca5SJaegeuk Kim 157318767e62SChao Yu /* 1574542989b6SEric Biggers * invalidate intermediate page cache borrowed from meta inode which are 1575aff6fbbeSChao Yu * used for migration of encrypted, verity or compressed inode's blocks. 157618767e62SChao Yu */ 1577aff6fbbeSChao Yu if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) || 1578aff6fbbeSChao Yu f2fs_sb_has_compression(sbi)) 157918767e62SChao Yu invalidate_mapping_pages(META_MAPPING(sbi), 158018767e62SChao Yu MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1); 158118767e62SChao Yu 15824d57b86dSChao Yu f2fs_release_ino_entry(sbi, false); 1583cf779cabSJaegeuk Kim 158450fa53ecSChao Yu f2fs_reset_fsync_node_info(sbi); 158550fa53ecSChao Yu 1586caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1587bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP); 1588af033b2aSChao Yu clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1589c9c8ed50SChao Yu 1590c9c8ed50SChao Yu spin_lock(&sbi->stat_lock); 15914354994fSDaniel Rosenberg sbi->unusable_block_count = 0; 1592c9c8ed50SChao Yu spin_unlock(&sbi->stat_lock); 1593c9c8ed50SChao Yu 15948508e44aSJaegeuk Kim __set_cp_next_pack(sbi); 1595c34f42e2SChao Yu 1596c2a080aeSChao Yu /* 1597c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is 1598c2a080aeSChao Yu * updated during writing checkpoint. 1599c2a080aeSChao Yu */ 1600c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) || 1601c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA)) 1602c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 1603c2a080aeSChao Yu 1604c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1605c2a080aeSChao Yu 1606af697c0fSJaegeuk Kim return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0; 1607127e670aSJaegeuk Kim } 1608127e670aSJaegeuk Kim 16094d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1610127e670aSJaegeuk Kim { 1611127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1612127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1613c34f42e2SChao Yu int err = 0; 1614127e670aSJaegeuk Kim 1615f5a131bbSChao Yu if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi)) 1616f5a131bbSChao Yu return -EROFS; 1617f5a131bbSChao Yu 16184354994fSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 16194354994fSDaniel Rosenberg if (cpc->reason != CP_PAUSE) 16204354994fSDaniel Rosenberg return 0; 1621dcbb4c10SJoe Perches f2fs_warn(sbi, "Start checkpoint disabled!"); 16224354994fSDaniel Rosenberg } 1623b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 1624e4544b63STim Murray f2fs_down_write(&sbi->cp_global_sem); 16258501017eSJaegeuk Kim 1626caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1627c473f1a9SChao Yu ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1628c473f1a9SChao Yu ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) 16298501017eSJaegeuk Kim goto out; 1630c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1631c34f42e2SChao Yu err = -EIO; 1632cf779cabSJaegeuk Kim goto out; 1633c34f42e2SChao Yu } 16342bda542dSWanpeng Li 16352bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 16362bda542dSWanpeng Li 1637c34f42e2SChao Yu err = block_operations(sbi); 1638c34f42e2SChao Yu if (err) 1639cf779cabSJaegeuk Kim goto out; 1640127e670aSJaegeuk Kim 164175ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 16422af4bd6cSNamjae Jeon 1643b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 1644127e670aSJaegeuk Kim 164558cce381SYunlei He /* this is the case of multiple fstrims without any changes */ 1646c473f1a9SChao Yu if (cpc->reason & CP_DISCARD) { 16474d57b86dSChao Yu if (!f2fs_exist_trim_candidates(sbi, cpc)) { 164825290fa5SJaegeuk Kim unblock_operations(sbi); 164925290fa5SJaegeuk Kim goto out; 165025290fa5SJaegeuk Kim } 165125290fa5SJaegeuk Kim 1652a95ba66aSJaegeuk Kim if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 && 16530333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 && 16540333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) { 16554d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 16564d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 165758cce381SYunlei He unblock_operations(sbi); 165858cce381SYunlei He goto out; 165958cce381SYunlei He } 16600333ad4eSJaegeuk Kim } 166158cce381SYunlei He 1662127e670aSJaegeuk Kim /* 1663127e670aSJaegeuk Kim * update checkpoint pack index 1664127e670aSJaegeuk Kim * Increase the version number so that 1665127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1666127e670aSJaegeuk Kim */ 1667d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1668127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1669127e670aSJaegeuk Kim 1670127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1671edc55aafSJaegeuk Kim err = f2fs_flush_nat_entries(sbi, cpc); 167291803392SChao Yu if (err) { 167391803392SChao Yu f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err); 167491803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 1675edc55aafSJaegeuk Kim goto stop; 167691803392SChao Yu } 1677edc55aafSJaegeuk Kim 16784d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 1679127e670aSJaegeuk Kim 1680d0b9e42aSChao Yu /* save inmem log status */ 1681093749e2SChao Yu f2fs_save_inmem_curseg(sbi); 1682d0b9e42aSChao Yu 1683c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 168491803392SChao Yu if (err) { 168591803392SChao Yu f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err); 168691803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 16874d57b86dSChao Yu f2fs_release_discard_addrs(sbi); 168891803392SChao Yu } else { 16894d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 169091803392SChao Yu } 1691d0b9e42aSChao Yu 1692093749e2SChao Yu f2fs_restore_inmem_curseg(sbi); 1693edc55aafSJaegeuk Kim stop: 1694127e670aSJaegeuk Kim unblock_operations(sbi); 1695942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 169610027551SJaegeuk Kim 1697c473f1a9SChao Yu if (cpc->reason & CP_RECOVERY) 1698dcbb4c10SJoe Perches f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver); 169960b99b48SJaegeuk Kim 17007a88ddb5SChao Yu /* update CP_TIME to trigger checkpoint periodically */ 17016beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 170255d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 17038501017eSJaegeuk Kim out: 1704b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 1705e4544b63STim Murray f2fs_up_write(&sbi->cp_global_sem); 1706c34f42e2SChao Yu return err; 1707127e670aSJaegeuk Kim } 1708127e670aSJaegeuk Kim 17094d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi) 1710127e670aSJaegeuk Kim { 17116451e041SJaegeuk Kim int i; 17126451e041SJaegeuk Kim 17136451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 171467298804SChao Yu struct inode_management *im = &sbi->im[i]; 171567298804SChao Yu 171667298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 171767298804SChao Yu spin_lock_init(&im->ino_lock); 171867298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 171967298804SChao Yu im->ino_num = 0; 17206451e041SJaegeuk Kim } 17216451e041SJaegeuk Kim 1722b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 1723d0b9e42aSChao Yu NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) * 172414b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1725127e670aSJaegeuk Kim } 1726127e670aSJaegeuk Kim 17274d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void) 1728127e670aSJaegeuk Kim { 17296451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 17306451e041SJaegeuk Kim sizeof(struct ino_entry)); 17316451e041SJaegeuk Kim if (!ino_entry_slab) 1732127e670aSJaegeuk Kim return -ENOMEM; 17334d57b86dSChao Yu f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 173406292073SChao Yu sizeof(struct inode_entry)); 17354d57b86dSChao Yu if (!f2fs_inode_entry_slab) { 17366451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1737127e670aSJaegeuk Kim return -ENOMEM; 1738127e670aSJaegeuk Kim } 1739127e670aSJaegeuk Kim return 0; 1740127e670aSJaegeuk Kim } 1741127e670aSJaegeuk Kim 17424d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void) 1743127e670aSJaegeuk Kim { 17446451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 17454d57b86dSChao Yu kmem_cache_destroy(f2fs_inode_entry_slab); 1746127e670aSJaegeuk Kim } 1747261eeb9cSDaeho Jeong 1748261eeb9cSDaeho Jeong static int __write_checkpoint_sync(struct f2fs_sb_info *sbi) 1749261eeb9cSDaeho Jeong { 1750261eeb9cSDaeho Jeong struct cp_control cpc = { .reason = CP_SYNC, }; 1751261eeb9cSDaeho Jeong int err; 1752261eeb9cSDaeho Jeong 1753e4544b63STim Murray f2fs_down_write(&sbi->gc_lock); 1754261eeb9cSDaeho Jeong err = f2fs_write_checkpoint(sbi, &cpc); 1755e4544b63STim Murray f2fs_up_write(&sbi->gc_lock); 1756261eeb9cSDaeho Jeong 1757261eeb9cSDaeho Jeong return err; 1758261eeb9cSDaeho Jeong } 1759261eeb9cSDaeho Jeong 1760261eeb9cSDaeho Jeong static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi) 1761261eeb9cSDaeho Jeong { 1762261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1763261eeb9cSDaeho Jeong struct ckpt_req *req, *next; 1764261eeb9cSDaeho Jeong struct llist_node *dispatch_list; 1765261eeb9cSDaeho Jeong u64 sum_diff = 0, diff, count = 0; 1766261eeb9cSDaeho Jeong int ret; 1767261eeb9cSDaeho Jeong 1768261eeb9cSDaeho Jeong dispatch_list = llist_del_all(&cprc->issue_list); 1769261eeb9cSDaeho Jeong if (!dispatch_list) 1770261eeb9cSDaeho Jeong return; 1771261eeb9cSDaeho Jeong dispatch_list = llist_reverse_order(dispatch_list); 1772261eeb9cSDaeho Jeong 1773261eeb9cSDaeho Jeong ret = __write_checkpoint_sync(sbi); 1774261eeb9cSDaeho Jeong atomic_inc(&cprc->issued_ckpt); 1775261eeb9cSDaeho Jeong 1776261eeb9cSDaeho Jeong llist_for_each_entry_safe(req, next, dispatch_list, llnode) { 1777261eeb9cSDaeho Jeong diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time); 1778261eeb9cSDaeho Jeong req->ret = ret; 1779261eeb9cSDaeho Jeong complete(&req->wait); 1780261eeb9cSDaeho Jeong 1781261eeb9cSDaeho Jeong sum_diff += diff; 1782261eeb9cSDaeho Jeong count++; 1783261eeb9cSDaeho Jeong } 1784261eeb9cSDaeho Jeong atomic_sub(count, &cprc->queued_ckpt); 1785261eeb9cSDaeho Jeong atomic_add(count, &cprc->total_ckpt); 1786261eeb9cSDaeho Jeong 1787261eeb9cSDaeho Jeong spin_lock(&cprc->stat_lock); 1788261eeb9cSDaeho Jeong cprc->cur_time = (unsigned int)div64_u64(sum_diff, count); 1789261eeb9cSDaeho Jeong if (cprc->peak_time < cprc->cur_time) 1790261eeb9cSDaeho Jeong cprc->peak_time = cprc->cur_time; 1791261eeb9cSDaeho Jeong spin_unlock(&cprc->stat_lock); 1792261eeb9cSDaeho Jeong } 1793261eeb9cSDaeho Jeong 1794261eeb9cSDaeho Jeong static int issue_checkpoint_thread(void *data) 1795261eeb9cSDaeho Jeong { 1796261eeb9cSDaeho Jeong struct f2fs_sb_info *sbi = data; 1797261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1798261eeb9cSDaeho Jeong wait_queue_head_t *q = &cprc->ckpt_wait_queue; 1799261eeb9cSDaeho Jeong repeat: 1800261eeb9cSDaeho Jeong if (kthread_should_stop()) 1801261eeb9cSDaeho Jeong return 0; 1802261eeb9cSDaeho Jeong 1803261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) 1804261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1805261eeb9cSDaeho Jeong 1806261eeb9cSDaeho Jeong wait_event_interruptible(*q, 1807261eeb9cSDaeho Jeong kthread_should_stop() || !llist_empty(&cprc->issue_list)); 1808261eeb9cSDaeho Jeong goto repeat; 1809261eeb9cSDaeho Jeong } 1810261eeb9cSDaeho Jeong 1811261eeb9cSDaeho Jeong static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi, 1812261eeb9cSDaeho Jeong struct ckpt_req *wait_req) 1813261eeb9cSDaeho Jeong { 1814261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1815261eeb9cSDaeho Jeong 1816261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) { 1817261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1818261eeb9cSDaeho Jeong } else { 1819261eeb9cSDaeho Jeong /* already dispatched by issue_checkpoint_thread */ 1820261eeb9cSDaeho Jeong if (wait_req) 1821261eeb9cSDaeho Jeong wait_for_completion(&wait_req->wait); 1822261eeb9cSDaeho Jeong } 1823261eeb9cSDaeho Jeong } 1824261eeb9cSDaeho Jeong 1825261eeb9cSDaeho Jeong static void init_ckpt_req(struct ckpt_req *req) 1826261eeb9cSDaeho Jeong { 1827261eeb9cSDaeho Jeong memset(req, 0, sizeof(struct ckpt_req)); 1828261eeb9cSDaeho Jeong 1829261eeb9cSDaeho Jeong init_completion(&req->wait); 1830261eeb9cSDaeho Jeong req->queue_time = ktime_get(); 1831261eeb9cSDaeho Jeong } 1832261eeb9cSDaeho Jeong 1833261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) 1834261eeb9cSDaeho Jeong { 1835261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1836261eeb9cSDaeho Jeong struct ckpt_req req; 1837261eeb9cSDaeho Jeong struct cp_control cpc; 1838261eeb9cSDaeho Jeong 1839261eeb9cSDaeho Jeong cpc.reason = __get_cp_reason(sbi); 1840261eeb9cSDaeho Jeong if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) { 1841261eeb9cSDaeho Jeong int ret; 1842261eeb9cSDaeho Jeong 1843e4544b63STim Murray f2fs_down_write(&sbi->gc_lock); 1844261eeb9cSDaeho Jeong ret = f2fs_write_checkpoint(sbi, &cpc); 1845e4544b63STim Murray f2fs_up_write(&sbi->gc_lock); 1846261eeb9cSDaeho Jeong 1847261eeb9cSDaeho Jeong return ret; 1848261eeb9cSDaeho Jeong } 1849261eeb9cSDaeho Jeong 1850261eeb9cSDaeho Jeong if (!cprc->f2fs_issue_ckpt) 1851261eeb9cSDaeho Jeong return __write_checkpoint_sync(sbi); 1852261eeb9cSDaeho Jeong 1853261eeb9cSDaeho Jeong init_ckpt_req(&req); 1854261eeb9cSDaeho Jeong 1855261eeb9cSDaeho Jeong llist_add(&req.llnode, &cprc->issue_list); 1856261eeb9cSDaeho Jeong atomic_inc(&cprc->queued_ckpt); 1857261eeb9cSDaeho Jeong 18583b42c741SChao Yu /* 18593b42c741SChao Yu * update issue_list before we wake up issue_checkpoint thread, 18603b42c741SChao Yu * this smp_mb() pairs with another barrier in ___wait_event(), 18613b42c741SChao Yu * see more details in comments of waitqueue_active(). 18623b42c741SChao Yu */ 1863261eeb9cSDaeho Jeong smp_mb(); 1864261eeb9cSDaeho Jeong 1865261eeb9cSDaeho Jeong if (waitqueue_active(&cprc->ckpt_wait_queue)) 1866261eeb9cSDaeho Jeong wake_up(&cprc->ckpt_wait_queue); 1867261eeb9cSDaeho Jeong 1868261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1869261eeb9cSDaeho Jeong wait_for_completion(&req.wait); 1870261eeb9cSDaeho Jeong else 1871261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, &req); 1872261eeb9cSDaeho Jeong 1873261eeb9cSDaeho Jeong return req.ret; 1874261eeb9cSDaeho Jeong } 1875261eeb9cSDaeho Jeong 1876261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi) 1877261eeb9cSDaeho Jeong { 1878261eeb9cSDaeho Jeong dev_t dev = sbi->sb->s_bdev->bd_dev; 1879261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1880261eeb9cSDaeho Jeong 1881261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1882261eeb9cSDaeho Jeong return 0; 1883261eeb9cSDaeho Jeong 1884261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi, 1885261eeb9cSDaeho Jeong "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev)); 1886261eeb9cSDaeho Jeong if (IS_ERR(cprc->f2fs_issue_ckpt)) { 1887261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1888261eeb9cSDaeho Jeong return -ENOMEM; 1889261eeb9cSDaeho Jeong } 1890261eeb9cSDaeho Jeong 1891e6592066SDaeho Jeong set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio); 1892261eeb9cSDaeho Jeong 1893261eeb9cSDaeho Jeong return 0; 1894261eeb9cSDaeho Jeong } 1895261eeb9cSDaeho Jeong 1896261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi) 1897261eeb9cSDaeho Jeong { 1898261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1899261eeb9cSDaeho Jeong 1900261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) { 1901261eeb9cSDaeho Jeong struct task_struct *ckpt_task = cprc->f2fs_issue_ckpt; 1902261eeb9cSDaeho Jeong 1903261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1904261eeb9cSDaeho Jeong kthread_stop(ckpt_task); 1905261eeb9cSDaeho Jeong 1906261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, NULL); 1907261eeb9cSDaeho Jeong } 1908261eeb9cSDaeho Jeong } 1909261eeb9cSDaeho Jeong 1910261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi) 1911261eeb9cSDaeho Jeong { 1912261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1913261eeb9cSDaeho Jeong 1914261eeb9cSDaeho Jeong atomic_set(&cprc->issued_ckpt, 0); 1915261eeb9cSDaeho Jeong atomic_set(&cprc->total_ckpt, 0); 1916261eeb9cSDaeho Jeong atomic_set(&cprc->queued_ckpt, 0); 1917e6592066SDaeho Jeong cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO; 1918261eeb9cSDaeho Jeong init_waitqueue_head(&cprc->ckpt_wait_queue); 1919261eeb9cSDaeho Jeong init_llist_head(&cprc->issue_list); 1920261eeb9cSDaeho Jeong spin_lock_init(&cprc->stat_lock); 1921261eeb9cSDaeho Jeong } 1922