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 29a9cfee0eSChao Yu void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io, 30a9cfee0eSChao Yu unsigned char reason) 3138f91ca8SJaegeuk Kim { 32d494500aSChao Yu f2fs_build_fault_attr(sbi, 0, 0); 33aaec2b1dSChao Yu set_ckpt_flags(sbi, CP_ERROR_FLAG); 34a9cfee0eSChao Yu if (!end_io) { 35b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 36a9cfee0eSChao Yu 37a9cfee0eSChao Yu f2fs_handle_stop(sbi, reason); 38a9cfee0eSChao Yu } 3938f91ca8SJaegeuk Kim } 4038f91ca8SJaegeuk Kim 410a8165d7SJaegeuk Kim /* 42127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 43127e670aSJaegeuk Kim */ 444d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 45127e670aSJaegeuk Kim { 469df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 47beb78181SZhang Qilong struct page *page; 48127e670aSJaegeuk Kim repeat: 49300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 50127e670aSJaegeuk Kim if (!page) { 51127e670aSJaegeuk Kim cond_resched(); 52127e670aSJaegeuk Kim goto repeat; 53127e670aSJaegeuk Kim } 54bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 55237c0790SJaegeuk Kim if (!PageUptodate(page)) 56127e670aSJaegeuk Kim SetPageUptodate(page); 57127e670aSJaegeuk Kim return page; 58127e670aSJaegeuk Kim } 59127e670aSJaegeuk Kim 602b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index, 612b947003SChao Yu bool is_meta) 62127e670aSJaegeuk Kim { 639df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 64127e670aSJaegeuk Kim struct page *page; 65cf04e8ebSJaegeuk Kim struct f2fs_io_info fio = { 6605ca3632SJaegeuk Kim .sbi = sbi, 67cf04e8ebSJaegeuk Kim .type = META, 6804d328deSMike Christie .op = REQ_OP_READ, 6970fd7614SChristoph Hellwig .op_flags = REQ_META | REQ_PRIO, 707a9d7548SChao Yu .old_blkaddr = index, 717a9d7548SChao Yu .new_blkaddr = index, 724375a336SJaegeuk Kim .encrypted_page = NULL, 736dc3a126SChao Yu .is_por = !is_meta, 74cf04e8ebSJaegeuk Kim }; 757735730dSChao Yu int err; 762b947003SChao Yu 772b947003SChao Yu if (unlikely(!is_meta)) 7804d328deSMike Christie fio.op_flags &= ~REQ_META; 79127e670aSJaegeuk Kim repeat: 80300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 81127e670aSJaegeuk Kim if (!page) { 82127e670aSJaegeuk Kim cond_resched(); 83127e670aSJaegeuk Kim goto repeat; 84127e670aSJaegeuk Kim } 85393ff91fSJaegeuk Kim if (PageUptodate(page)) 86393ff91fSJaegeuk Kim goto out; 87127e670aSJaegeuk Kim 8805ca3632SJaegeuk Kim fio.page = page; 8905ca3632SJaegeuk Kim 907735730dSChao Yu err = f2fs_submit_page_bio(&fio); 917735730dSChao Yu if (err) { 927735730dSChao Yu f2fs_put_page(page, 1); 937735730dSChao Yu return ERR_PTR(err); 9486531d6bSJaegeuk Kim } 95393ff91fSJaegeuk Kim 9634a23525SChao Yu f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE); 978b83ac81SChao Yu 98393ff91fSJaegeuk Kim lock_page(page); 996bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) { 100afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 101afcb7ca0SJaegeuk Kim goto repeat; 102afcb7ca0SJaegeuk Kim } 103f3f338caSChao Yu 10481114baaSChao Yu if (unlikely(!PageUptodate(page))) { 105a7b8618aSJaegeuk Kim f2fs_handle_page_eio(sbi, page->index, META); 1067735730dSChao Yu f2fs_put_page(page, 1); 1077735730dSChao Yu return ERR_PTR(-EIO); 10881114baaSChao Yu } 109393ff91fSJaegeuk Kim out: 110127e670aSJaegeuk Kim return page; 111127e670aSJaegeuk Kim } 112127e670aSJaegeuk Kim 1134d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 1142b947003SChao Yu { 1152b947003SChao Yu return __get_meta_page(sbi, index, true); 1162b947003SChao Yu } 1172b947003SChao Yu 11886f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index) 1197735730dSChao Yu { 1207735730dSChao Yu struct page *page; 1217735730dSChao Yu int count = 0; 1227735730dSChao Yu 1237735730dSChao Yu retry: 1247735730dSChao Yu page = __get_meta_page(sbi, index, true); 1257735730dSChao Yu if (IS_ERR(page)) { 1267735730dSChao Yu if (PTR_ERR(page) == -EIO && 1277735730dSChao Yu ++count <= DEFAULT_RETRY_IO_COUNT) 1287735730dSChao Yu goto retry; 129a9cfee0eSChao Yu f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE); 1307735730dSChao Yu } 1317735730dSChao Yu return page; 1327735730dSChao Yu } 1337735730dSChao Yu 1342b947003SChao Yu /* for POR only */ 1354d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 1362b947003SChao Yu { 1372b947003SChao Yu return __get_meta_page(sbi, index, false); 1382b947003SChao Yu } 1392b947003SChao Yu 14093770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr, 14193770ab7SChao Yu int type) 14293770ab7SChao Yu { 14393770ab7SChao Yu struct seg_entry *se; 14493770ab7SChao Yu unsigned int segno, offset; 14593770ab7SChao Yu bool exist; 14693770ab7SChao Yu 1470ef4ca04SChao Yu if (type == DATA_GENERIC) 14893770ab7SChao Yu return true; 14993770ab7SChao Yu 15093770ab7SChao Yu segno = GET_SEGNO(sbi, blkaddr); 15193770ab7SChao Yu offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 15293770ab7SChao Yu se = get_seg_entry(sbi, segno); 15393770ab7SChao Yu 15493770ab7SChao Yu exist = f2fs_test_bit(offset, se->cur_valid_map); 1550ef4ca04SChao Yu if (exist && type == DATA_GENERIC_ENHANCE_UPDATE) { 1560ef4ca04SChao Yu f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", 1570ef4ca04SChao Yu blkaddr, exist); 1580ef4ca04SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 1590ef4ca04SChao Yu return exist; 1600ef4ca04SChao Yu } 1610ef4ca04SChao Yu 16293770ab7SChao Yu if (!exist && type == DATA_GENERIC_ENHANCE) { 163dcbb4c10SJoe Perches f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", 164dcbb4c10SJoe Perches blkaddr, exist); 16593770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 166dc2f78e2SDongliang Mu dump_stack(); 16793770ab7SChao Yu } 16893770ab7SChao Yu return exist; 16993770ab7SChao Yu } 17093770ab7SChao Yu 171e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 1724d57b86dSChao Yu block_t blkaddr, int type) 173662befdaSChao Yu { 174*c40e15a9SYangtao Li if (time_to_inject(sbi, FAULT_BLKADDR)) 17518792e64SChao Yu return false; 17618792e64SChao Yu 177662befdaSChao Yu switch (type) { 178662befdaSChao Yu case META_NAT: 17966b00c18SChao Yu break; 180662befdaSChao Yu case META_SIT: 18166b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 18266b00c18SChao Yu return false; 18366b00c18SChao Yu break; 18481c1a0f1SChao Yu case META_SSA: 18566b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 18666b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr)) 18766b00c18SChao Yu return false; 18866b00c18SChao Yu break; 189662befdaSChao Yu case META_CP: 19066b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 19166b00c18SChao Yu blkaddr < __start_cp_addr(sbi))) 19266b00c18SChao Yu return false; 19366b00c18SChao Yu break; 1944c521f49SJaegeuk Kim case META_POR: 19593770ab7SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 19693770ab7SChao Yu blkaddr < MAIN_BLKADDR(sbi))) 19793770ab7SChao Yu return false; 19893770ab7SChao Yu break; 199e1da7872SChao Yu case DATA_GENERIC: 20093770ab7SChao Yu case DATA_GENERIC_ENHANCE: 20193770ab7SChao Yu case DATA_GENERIC_ENHANCE_READ: 2020ef4ca04SChao Yu case DATA_GENERIC_ENHANCE_UPDATE: 20366b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 204c9b60788SChao Yu blkaddr < MAIN_BLKADDR(sbi))) { 205dcbb4c10SJoe Perches f2fs_warn(sbi, "access invalid blkaddr:%u", 206dcbb4c10SJoe Perches blkaddr); 20793770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 208dc2f78e2SDongliang Mu dump_stack(); 20966b00c18SChao Yu return false; 21093770ab7SChao Yu } else { 21193770ab7SChao Yu return __is_bitmap_valid(sbi, blkaddr, type); 212c9b60788SChao Yu } 21366b00c18SChao Yu break; 214e1da7872SChao Yu case META_GENERIC: 215e1da7872SChao Yu if (unlikely(blkaddr < SEG0_BLKADDR(sbi) || 216e1da7872SChao Yu blkaddr >= MAIN_BLKADDR(sbi))) 217e1da7872SChao Yu return false; 218e1da7872SChao Yu break; 219662befdaSChao Yu default: 220662befdaSChao Yu BUG(); 221662befdaSChao Yu } 22266b00c18SChao Yu 22366b00c18SChao Yu return true; 224662befdaSChao Yu } 225662befdaSChao Yu 226662befdaSChao Yu /* 2277a88ddb5SChao Yu * Readahead CP/NAT/SIT/SSA/POR pages 228662befdaSChao Yu */ 2294d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 23026879fb1SChao Yu int type, bool sync) 231662befdaSChao Yu { 232662befdaSChao Yu struct page *page; 2334c521f49SJaegeuk Kim block_t blkno = start; 234662befdaSChao Yu struct f2fs_io_info fio = { 23505ca3632SJaegeuk Kim .sbi = sbi, 236662befdaSChao Yu .type = META, 23704d328deSMike Christie .op = REQ_OP_READ, 23870fd7614SChristoph Hellwig .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, 2394375a336SJaegeuk Kim .encrypted_page = NULL, 240fb830fc5SChao Yu .in_list = false, 2416dc3a126SChao Yu .is_por = (type == META_POR), 242662befdaSChao Yu }; 243e9f5b8b8SChao Yu struct blk_plug plug; 244ce4c638cSChao Yu int err; 245662befdaSChao Yu 2462b947003SChao Yu if (unlikely(type == META_POR)) 24704d328deSMike Christie fio.op_flags &= ~REQ_META; 2482b947003SChao Yu 249e9f5b8b8SChao Yu blk_start_plug(&plug); 250662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 251662befdaSChao Yu 252e1da7872SChao Yu if (!f2fs_is_valid_blkaddr(sbi, blkno, type)) 25366b00c18SChao Yu goto out; 25466b00c18SChao Yu 255662befdaSChao Yu switch (type) { 256662befdaSChao Yu case META_NAT: 25766b00c18SChao Yu if (unlikely(blkno >= 25866b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 259662befdaSChao Yu blkno = 0; 26066b00c18SChao Yu /* get nat block addr */ 2617a9d7548SChao Yu fio.new_blkaddr = current_nat_addr(sbi, 262662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 263662befdaSChao Yu break; 264662befdaSChao Yu case META_SIT: 2656a257471SChao Yu if (unlikely(blkno >= TOTAL_SEGS(sbi))) 2666a257471SChao Yu goto out; 267662befdaSChao Yu /* get sit block addr */ 2687a9d7548SChao Yu fio.new_blkaddr = current_sit_addr(sbi, 269662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 270662befdaSChao Yu break; 27181c1a0f1SChao Yu case META_SSA: 272662befdaSChao Yu case META_CP: 2734c521f49SJaegeuk Kim case META_POR: 2747a9d7548SChao Yu fio.new_blkaddr = blkno; 275662befdaSChao Yu break; 276662befdaSChao Yu default: 277662befdaSChao Yu BUG(); 278662befdaSChao Yu } 279662befdaSChao Yu 280300e129cSJaegeuk Kim page = f2fs_grab_cache_page(META_MAPPING(sbi), 281300e129cSJaegeuk Kim fio.new_blkaddr, false); 282662befdaSChao Yu if (!page) 283662befdaSChao Yu continue; 284662befdaSChao Yu if (PageUptodate(page)) { 285662befdaSChao Yu f2fs_put_page(page, 1); 286662befdaSChao Yu continue; 287662befdaSChao Yu } 288662befdaSChao Yu 28905ca3632SJaegeuk Kim fio.page = page; 290ce4c638cSChao Yu err = f2fs_submit_page_bio(&fio); 291ce4c638cSChao Yu f2fs_put_page(page, err ? 1 : 0); 2928b83ac81SChao Yu 2938b83ac81SChao Yu if (!err) 29434a23525SChao Yu f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, 29534a23525SChao Yu F2FS_BLKSIZE); 296662befdaSChao Yu } 297662befdaSChao Yu out: 298e9f5b8b8SChao Yu blk_finish_plug(&plug); 299662befdaSChao Yu return blkno - start; 300662befdaSChao Yu } 301662befdaSChao Yu 302430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index, 303430f163bSChao Yu unsigned int ra_blocks) 304635aee1fSChao Yu { 305635aee1fSChao Yu struct page *page; 306635aee1fSChao Yu bool readahead = false; 307635aee1fSChao Yu 308430f163bSChao Yu if (ra_blocks == RECOVERY_MIN_RA_BLOCKS) 309430f163bSChao Yu return; 310430f163bSChao Yu 311635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 3124da7bf5aSJaegeuk Kim if (!page || !PageUptodate(page)) 313635aee1fSChao Yu readahead = true; 314635aee1fSChao Yu f2fs_put_page(page, 0); 315635aee1fSChao Yu 316635aee1fSChao Yu if (readahead) 317430f163bSChao Yu f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true); 318635aee1fSChao Yu } 319635aee1fSChao Yu 320b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page, 321b0af6d49SChao Yu struct writeback_control *wbc, 322b0af6d49SChao Yu enum iostat_type io_type) 323127e670aSJaegeuk Kim { 3244081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 325577e3495SJaegeuk Kim 326ecda0de3SChao Yu trace_f2fs_writepage(page, META); 327ecda0de3SChao Yu 328af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 329af697c0fSJaegeuk Kim goto redirty_out; 330caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 331cfb271d4SChao Yu goto redirty_out; 332857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 333cfb271d4SChao Yu goto redirty_out; 334127e670aSJaegeuk Kim 3354d57b86dSChao Yu f2fs_do_write_meta_page(sbi, page, io_type); 336127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 3370c3a5797SChao Yu 3380c3a5797SChao Yu if (wbc->for_reclaim) 339bab475c5SChao Yu f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META); 3400c3a5797SChao Yu 341127e670aSJaegeuk Kim unlock_page(page); 342857dc4e0SJaegeuk Kim 3430c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 344b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, META); 3450c3a5797SChao Yu 346577e3495SJaegeuk Kim return 0; 347cfb271d4SChao Yu 348cfb271d4SChao Yu redirty_out: 34976f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 350cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 351127e670aSJaegeuk Kim } 352127e670aSJaegeuk Kim 353b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page, 354b0af6d49SChao Yu struct writeback_control *wbc) 355b0af6d49SChao Yu { 356b0af6d49SChao Yu return __f2fs_write_meta_page(page, wbc, FS_META_IO); 357b0af6d49SChao Yu } 358b0af6d49SChao Yu 359127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 360127e670aSJaegeuk Kim struct writeback_control *wbc) 361127e670aSJaegeuk Kim { 3624081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 36350c8cdb3SJaegeuk Kim long diff, written; 364127e670aSJaegeuk Kim 3650771fcc7SChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3660771fcc7SChao Yu goto skip_write; 3670771fcc7SChao Yu 3685459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 369812a9597SJaegeuk Kim if (wbc->sync_mode != WB_SYNC_ALL && 370812a9597SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < 371812a9597SJaegeuk Kim nr_pages_to_skip(sbi, META)) 372d3baf95dSJaegeuk Kim goto skip_write; 373127e670aSJaegeuk Kim 374a29d0e0bSYunlei He /* if locked failed, cp will flush dirty pages instead */ 375e4544b63STim Murray if (!f2fs_down_write_trylock(&sbi->cp_global_sem)) 376a29d0e0bSYunlei He goto skip_write; 377d31c7c3fSYunlei He 378a29d0e0bSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 37950c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 3804d57b86dSChao Yu written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); 381e4544b63STim Murray f2fs_up_write(&sbi->cp_global_sem); 38250c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 383127e670aSJaegeuk Kim return 0; 384d3baf95dSJaegeuk Kim 385d3baf95dSJaegeuk Kim skip_write: 386d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 387d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 388d3baf95dSJaegeuk Kim return 0; 389127e670aSJaegeuk Kim } 390127e670aSJaegeuk Kim 3914d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 392b0af6d49SChao Yu long nr_to_write, enum iostat_type io_type) 393127e670aSJaegeuk Kim { 3949df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 395028a63a6SJan Kara pgoff_t index = 0, prev = ULONG_MAX; 396127e670aSJaegeuk Kim struct pagevec pvec; 397127e670aSJaegeuk Kim long nwritten = 0; 398028a63a6SJan Kara int nr_pages; 399127e670aSJaegeuk Kim struct writeback_control wbc = { 400127e670aSJaegeuk Kim .for_reclaim = 0, 401127e670aSJaegeuk Kim }; 402e9f5b8b8SChao Yu struct blk_plug plug; 403127e670aSJaegeuk Kim 40486679820SMel Gorman pagevec_init(&pvec); 405127e670aSJaegeuk Kim 406e9f5b8b8SChao Yu blk_start_plug(&plug); 407e9f5b8b8SChao Yu 408028a63a6SJan Kara while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 40967fd707fSJan Kara PAGECACHE_TAG_DIRTY))) { 410028a63a6SJan Kara int i; 411127e670aSJaegeuk Kim 412127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 413127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 414203681f6SJaegeuk Kim 41580dd9c0eSChao Yu if (prev == ULONG_MAX) 4166066d8cdSJaegeuk Kim prev = page->index - 1; 4176066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 4186066d8cdSJaegeuk Kim pagevec_release(&pvec); 4196066d8cdSJaegeuk Kim goto stop; 4206066d8cdSJaegeuk Kim } 4216066d8cdSJaegeuk Kim 422127e670aSJaegeuk Kim lock_page(page); 423203681f6SJaegeuk Kim 424203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 425203681f6SJaegeuk Kim continue_unlock: 426203681f6SJaegeuk Kim unlock_page(page); 427203681f6SJaegeuk Kim continue; 428203681f6SJaegeuk Kim } 429203681f6SJaegeuk Kim if (!PageDirty(page)) { 430203681f6SJaegeuk Kim /* someone wrote it for us */ 431203681f6SJaegeuk Kim goto continue_unlock; 432203681f6SJaegeuk Kim } 433203681f6SJaegeuk Kim 434bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 435fa3d2bdfSJaegeuk Kim 436203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 437203681f6SJaegeuk Kim goto continue_unlock; 438203681f6SJaegeuk Kim 439b0af6d49SChao Yu if (__f2fs_write_meta_page(page, &wbc, io_type)) { 440577e3495SJaegeuk Kim unlock_page(page); 441577e3495SJaegeuk Kim break; 442577e3495SJaegeuk Kim } 443cfb271d4SChao Yu nwritten++; 4446066d8cdSJaegeuk Kim prev = page->index; 445cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 446127e670aSJaegeuk Kim break; 447127e670aSJaegeuk Kim } 448127e670aSJaegeuk Kim pagevec_release(&pvec); 449127e670aSJaegeuk Kim cond_resched(); 450127e670aSJaegeuk Kim } 4516066d8cdSJaegeuk Kim stop: 452127e670aSJaegeuk Kim if (nwritten) 453b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, type); 454127e670aSJaegeuk Kim 455e9f5b8b8SChao Yu blk_finish_plug(&plug); 456e9f5b8b8SChao Yu 457127e670aSJaegeuk Kim return nwritten; 458127e670aSJaegeuk Kim } 459127e670aSJaegeuk Kim 4601d9ac659SMatthew Wilcox (Oracle) static bool f2fs_dirty_meta_folio(struct address_space *mapping, 4611d9ac659SMatthew Wilcox (Oracle) struct folio *folio) 462127e670aSJaegeuk Kim { 4631d9ac659SMatthew Wilcox (Oracle) trace_f2fs_set_page_dirty(&folio->page, META); 46426c6b887SJaegeuk Kim 4651d9ac659SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) 4661d9ac659SMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 4679b7eadd9SShuqi Zhang if (filemap_dirty_folio(mapping, folio)) { 46829c87793SMatthew Wilcox (Oracle) inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META); 4691d9ac659SMatthew Wilcox (Oracle) set_page_private_reference(&folio->page); 4701d9ac659SMatthew Wilcox (Oracle) return true; 471127e670aSJaegeuk Kim } 4721d9ac659SMatthew Wilcox (Oracle) return false; 473127e670aSJaegeuk Kim } 474127e670aSJaegeuk Kim 475127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 476127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 477127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 4781d9ac659SMatthew Wilcox (Oracle) .dirty_folio = f2fs_dirty_meta_folio, 47991503996SMatthew Wilcox (Oracle) .invalidate_folio = f2fs_invalidate_folio, 480c26cd045SMatthew Wilcox (Oracle) .release_folio = f2fs_release_folio, 4811d5b9bd6SMatthew Wilcox (Oracle) .migrate_folio = filemap_migrate_folio, 482127e670aSJaegeuk Kim }; 483127e670aSJaegeuk Kim 48439d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, 48539d787beSChao Yu unsigned int devidx, int type) 486953e6cc6SJaegeuk Kim { 48767298804SChao Yu struct inode_management *im = &sbi->im[type]; 4884b106518SChao Yu struct ino_entry *e = NULL, *new = NULL; 48980c54505SJaegeuk Kim 4904b106518SChao Yu if (type == FLUSH_INO) { 4914b106518SChao Yu rcu_read_lock(); 4924b106518SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 4934b106518SChao Yu rcu_read_unlock(); 4944b106518SChao Yu } 4954b106518SChao Yu 4964b106518SChao Yu retry: 4974b106518SChao Yu if (!e) 49832410577SChao Yu new = f2fs_kmem_cache_alloc(ino_entry_slab, 49932410577SChao Yu GFP_NOFS, true, NULL); 50019526d74SChao Yu 50180c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 502769ec6e5SJaegeuk Kim 50367298804SChao Yu spin_lock(&im->ino_lock); 50467298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 50539efac41SJaegeuk Kim if (!e) { 5064b106518SChao Yu if (!new) { 5074b106518SChao Yu spin_unlock(&im->ino_lock); 5084b106518SChao Yu goto retry; 5094b106518SChao Yu } 5104b106518SChao Yu e = new; 51119526d74SChao Yu if (unlikely(radix_tree_insert(&im->ino_root, ino, e))) 51219526d74SChao Yu f2fs_bug_on(sbi, 1); 51319526d74SChao Yu 51439efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 51539efac41SJaegeuk Kim e->ino = ino; 51639efac41SJaegeuk Kim 51767298804SChao Yu list_add_tail(&e->list, &im->ino_list); 5188c402946SJaegeuk Kim if (type != ORPHAN_INO) 51967298804SChao Yu im->ino_num++; 52039efac41SJaegeuk Kim } 52139d787beSChao Yu 52239d787beSChao Yu if (type == FLUSH_INO) 52339d787beSChao Yu f2fs_set_bit(devidx, (char *)&e->dirty_device); 52439d787beSChao Yu 52567298804SChao Yu spin_unlock(&im->ino_lock); 526769ec6e5SJaegeuk Kim radix_tree_preload_end(); 52780c54505SJaegeuk Kim 5284b106518SChao Yu if (new && e != new) 5294b106518SChao Yu kmem_cache_free(ino_entry_slab, new); 530953e6cc6SJaegeuk Kim } 531953e6cc6SJaegeuk Kim 5326451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 533953e6cc6SJaegeuk Kim { 53467298804SChao Yu struct inode_management *im = &sbi->im[type]; 5356451e041SJaegeuk Kim struct ino_entry *e; 536953e6cc6SJaegeuk Kim 53767298804SChao Yu spin_lock(&im->ino_lock); 53867298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 53939efac41SJaegeuk Kim if (e) { 540953e6cc6SJaegeuk Kim list_del(&e->list); 54167298804SChao Yu radix_tree_delete(&im->ino_root, ino); 54267298804SChao Yu im->ino_num--; 54367298804SChao Yu spin_unlock(&im->ino_lock); 5446451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 545953e6cc6SJaegeuk Kim return; 546953e6cc6SJaegeuk Kim } 54767298804SChao Yu spin_unlock(&im->ino_lock); 548953e6cc6SJaegeuk Kim } 549953e6cc6SJaegeuk Kim 5504d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 551fff04f90SJaegeuk Kim { 552fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 55339d787beSChao Yu __add_ino_entry(sbi, ino, 0, type); 554fff04f90SJaegeuk Kim } 555fff04f90SJaegeuk Kim 5564d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 557fff04f90SJaegeuk Kim { 558fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 559fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 560fff04f90SJaegeuk Kim } 561fff04f90SJaegeuk Kim 5621f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */ 5634d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 564fff04f90SJaegeuk Kim { 56567298804SChao Yu struct inode_management *im = &sbi->im[mode]; 566fff04f90SJaegeuk Kim struct ino_entry *e; 56767298804SChao Yu 56867298804SChao Yu spin_lock(&im->ino_lock); 56967298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 57067298804SChao Yu spin_unlock(&im->ino_lock); 571fff04f90SJaegeuk Kim return e ? true : false; 572fff04f90SJaegeuk Kim } 573fff04f90SJaegeuk Kim 5744d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all) 575fff04f90SJaegeuk Kim { 576fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 577fff04f90SJaegeuk Kim int i; 578fff04f90SJaegeuk Kim 57939d787beSChao Yu for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) { 58067298804SChao Yu struct inode_management *im = &sbi->im[i]; 58167298804SChao Yu 58267298804SChao Yu spin_lock(&im->ino_lock); 58367298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 584fff04f90SJaegeuk Kim list_del(&e->list); 58567298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 586fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 58767298804SChao Yu im->ino_num--; 588fff04f90SJaegeuk Kim } 58967298804SChao Yu spin_unlock(&im->ino_lock); 590fff04f90SJaegeuk Kim } 591fff04f90SJaegeuk Kim } 592fff04f90SJaegeuk Kim 5934d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 59439d787beSChao Yu unsigned int devidx, int type) 59539d787beSChao Yu { 59639d787beSChao Yu __add_ino_entry(sbi, ino, devidx, type); 59739d787beSChao Yu } 59839d787beSChao Yu 5994d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 60039d787beSChao Yu unsigned int devidx, int type) 60139d787beSChao Yu { 60239d787beSChao Yu struct inode_management *im = &sbi->im[type]; 60339d787beSChao Yu struct ino_entry *e; 60439d787beSChao Yu bool is_dirty = false; 60539d787beSChao Yu 60639d787beSChao Yu spin_lock(&im->ino_lock); 60739d787beSChao Yu e = radix_tree_lookup(&im->ino_root, ino); 60839d787beSChao Yu if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device)) 60939d787beSChao Yu is_dirty = true; 61039d787beSChao Yu spin_unlock(&im->ino_lock); 61139d787beSChao Yu return is_dirty; 61239d787beSChao Yu } 61339d787beSChao Yu 6144d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi) 615127e670aSJaegeuk Kim { 61667298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 617127e670aSJaegeuk Kim int err = 0; 618127e670aSJaegeuk Kim 61967298804SChao Yu spin_lock(&im->ino_lock); 620cb78942bSJaegeuk Kim 6211ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) { 622cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock); 623cb78942bSJaegeuk Kim return -ENOSPC; 624cb78942bSJaegeuk Kim } 6257fa750a1SArnd Bergmann 62667298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 627127e670aSJaegeuk Kim err = -ENOSPC; 628cbd56e7dSJaegeuk Kim else 62967298804SChao Yu im->ino_num++; 63067298804SChao Yu spin_unlock(&im->ino_lock); 6310d47c1adSGu Zheng 632127e670aSJaegeuk Kim return err; 633127e670aSJaegeuk Kim } 634127e670aSJaegeuk Kim 6354d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi) 636cbd56e7dSJaegeuk Kim { 63767298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 63867298804SChao Yu 63967298804SChao Yu spin_lock(&im->ino_lock); 64067298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 64167298804SChao Yu im->ino_num--; 64267298804SChao Yu spin_unlock(&im->ino_lock); 643cbd56e7dSJaegeuk Kim } 644cbd56e7dSJaegeuk Kim 6454d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode) 646127e670aSJaegeuk Kim { 64739efac41SJaegeuk Kim /* add new orphan ino entry into list */ 64839d787beSChao Yu __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO); 6494d57b86dSChao Yu f2fs_update_inode_page(inode); 650127e670aSJaegeuk Kim } 651127e670aSJaegeuk Kim 6524d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 653127e670aSJaegeuk Kim { 654953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 6556451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 656127e670aSJaegeuk Kim } 657127e670aSJaegeuk Kim 6588c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 659127e670aSJaegeuk Kim { 6608c14bfadSChao Yu struct inode *inode; 6615905f9afSJaegeuk Kim struct node_info ni; 66276a45e3cSChao Yu int err; 6638c14bfadSChao Yu 6645905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino); 6658c14bfadSChao Yu if (IS_ERR(inode)) { 6668c14bfadSChao Yu /* 6678c14bfadSChao Yu * there should be a bug that we can't find the entry 6688c14bfadSChao Yu * to orphan inode. 6698c14bfadSChao Yu */ 6708c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 6718c14bfadSChao Yu return PTR_ERR(inode); 6728c14bfadSChao Yu } 6738c14bfadSChao Yu 67410a26878SChao Yu err = f2fs_dquot_initialize(inode); 675a515d12fSSheng Yong if (err) { 676a515d12fSSheng Yong iput(inode); 6770f9ec2a8SJaegeuk Kim goto err_out; 678a515d12fSSheng Yong } 6790f9ec2a8SJaegeuk Kim 680127e670aSJaegeuk Kim clear_nlink(inode); 681127e670aSJaegeuk Kim 682127e670aSJaegeuk Kim /* truncate all the data during iput */ 683127e670aSJaegeuk Kim iput(inode); 6845905f9afSJaegeuk Kim 685a9419b63SJaegeuk Kim err = f2fs_get_node_info(sbi, ino, &ni, false); 6867735730dSChao Yu if (err) 6877735730dSChao Yu goto err_out; 6885905f9afSJaegeuk Kim 6895905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */ 6905905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) { 6910f9ec2a8SJaegeuk Kim err = -EIO; 6920f9ec2a8SJaegeuk Kim goto err_out; 6935905f9afSJaegeuk Kim } 6948c14bfadSChao Yu return 0; 6950f9ec2a8SJaegeuk Kim 6960f9ec2a8SJaegeuk Kim err_out: 6970f9ec2a8SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 698dcbb4c10SJoe Perches f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.", 6990f9ec2a8SJaegeuk Kim __func__, ino); 7000f9ec2a8SJaegeuk Kim return err; 701127e670aSJaegeuk Kim } 702127e670aSJaegeuk Kim 7034d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) 704127e670aSJaegeuk Kim { 7053c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 7064b2414d0SChao Yu unsigned int s_flags = sbi->sb->s_flags; 7074b2414d0SChao Yu int err = 0; 708ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA 709ea676733SJaegeuk Kim int quota_enabled; 710ea676733SJaegeuk Kim #endif 711127e670aSJaegeuk Kim 712aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 7138c14bfadSChao Yu return 0; 714127e670aSJaegeuk Kim 715b61af314SChao Yu if (bdev_read_only(sbi->sb->s_bdev)) { 716dcbb4c10SJoe Perches f2fs_info(sbi, "write access unavailable, skipping orphan cleanup"); 717b61af314SChao Yu return 0; 718b61af314SChao Yu } 719b61af314SChao Yu 7201751e8a6SLinus Torvalds if (s_flags & SB_RDONLY) { 721dcbb4c10SJoe Perches f2fs_info(sbi, "orphan cleanup on readonly fs"); 7221751e8a6SLinus Torvalds sbi->sb->s_flags &= ~SB_RDONLY; 7234b2414d0SChao Yu } 7244b2414d0SChao Yu 7254b2414d0SChao Yu #ifdef CONFIG_QUOTA 72676cf05d7SSheng Yong /* 72776cf05d7SSheng Yong * Turn on quotas which were not enabled for read-only mounts if 72876cf05d7SSheng Yong * filesystem has quota feature, so that they are updated correctly. 72976cf05d7SSheng Yong */ 7301751e8a6SLinus Torvalds quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY); 7314b2414d0SChao Yu #endif 7324b2414d0SChao Yu 73355141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 7343c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 735127e670aSJaegeuk Kim 7364d57b86dSChao Yu f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 737662befdaSChao Yu 7383c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 7397735730dSChao Yu struct page *page; 740127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 741127e670aSJaegeuk Kim 7427735730dSChao Yu page = f2fs_get_meta_page(sbi, start_blk + i); 7437735730dSChao Yu if (IS_ERR(page)) { 7447735730dSChao Yu err = PTR_ERR(page); 7457735730dSChao Yu goto out; 7467735730dSChao Yu } 7477735730dSChao Yu 748127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 749127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 750127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 7515f029c04SYi Zhuang 7528c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 7538c14bfadSChao Yu if (err) { 7548c14bfadSChao Yu f2fs_put_page(page, 1); 7554b2414d0SChao Yu goto out; 7568c14bfadSChao Yu } 757127e670aSJaegeuk Kim } 758127e670aSJaegeuk Kim f2fs_put_page(page, 1); 759127e670aSJaegeuk Kim } 760127e670aSJaegeuk Kim /* clear Orphan Flag */ 761aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 7624b2414d0SChao Yu out: 7631378752bSChao Yu set_sbi_flag(sbi, SBI_IS_RECOVERED); 7641378752bSChao Yu 7654b2414d0SChao Yu #ifdef CONFIG_QUOTA 7664b2414d0SChao Yu /* Turn quotas off */ 767ea676733SJaegeuk Kim if (quota_enabled) 7684b2414d0SChao Yu f2fs_quota_off_umount(sbi->sb); 7694b2414d0SChao Yu #endif 7701751e8a6SLinus Torvalds sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 7714b2414d0SChao Yu 7724b2414d0SChao Yu return err; 773127e670aSJaegeuk Kim } 774127e670aSJaegeuk Kim 775127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 776127e670aSJaegeuk Kim { 777502c6e0bSGu Zheng struct list_head *head; 778127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 779127e670aSJaegeuk Kim unsigned int nentries = 0; 780bd936f84SChao Yu unsigned short index = 1; 7818c402946SJaegeuk Kim unsigned short orphan_blocks; 7824531929eSGu Zheng struct page *page = NULL; 7836451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 78467298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 785127e670aSJaegeuk Kim 78667298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 7878c402946SJaegeuk Kim 788d6c67a4fSJaegeuk Kim /* 789d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 790d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 791d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 792d6c67a4fSJaegeuk Kim */ 79367298804SChao Yu head = &im->ino_list; 794127e670aSJaegeuk Kim 795127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 796502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 797502c6e0bSGu Zheng if (!page) { 7984d57b86dSChao Yu page = f2fs_grab_meta_page(sbi, start_blk++); 799502c6e0bSGu Zheng orphan_blk = 800502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 801502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 802502c6e0bSGu Zheng } 803127e670aSJaegeuk Kim 80436795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 805127e670aSJaegeuk Kim 80636795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 807127e670aSJaegeuk Kim /* 808127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 809127e670aSJaegeuk Kim * then we need to flush current orphan blocks 810127e670aSJaegeuk Kim * and bring another one in memory 811127e670aSJaegeuk Kim */ 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); 817127e670aSJaegeuk Kim index++; 818127e670aSJaegeuk Kim nentries = 0; 819127e670aSJaegeuk Kim page = NULL; 820127e670aSJaegeuk Kim } 821127e670aSJaegeuk Kim } 822127e670aSJaegeuk Kim 823502c6e0bSGu Zheng if (page) { 824127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 825127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 826127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 827127e670aSJaegeuk Kim set_page_dirty(page); 828127e670aSJaegeuk Kim f2fs_put_page(page, 1); 829502c6e0bSGu Zheng } 830127e670aSJaegeuk Kim } 831127e670aSJaegeuk Kim 832d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi, 833d7eb8f1cSChao Yu struct f2fs_checkpoint *ckpt) 834d7eb8f1cSChao Yu { 835d7eb8f1cSChao Yu unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset); 836d7eb8f1cSChao Yu __u32 chksum; 837d7eb8f1cSChao Yu 838d7eb8f1cSChao Yu chksum = f2fs_crc32(sbi, ckpt, chksum_ofs); 839d7eb8f1cSChao Yu if (chksum_ofs < CP_CHKSUM_OFFSET) { 840d7eb8f1cSChao Yu chksum_ofs += sizeof(chksum); 841d7eb8f1cSChao Yu chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs, 842d7eb8f1cSChao Yu F2FS_BLKSIZE - chksum_ofs); 843d7eb8f1cSChao Yu } 844d7eb8f1cSChao Yu return chksum; 845d7eb8f1cSChao Yu } 846d7eb8f1cSChao Yu 847fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 848fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page, 849fc0065adSTiezhu Yang unsigned long long *version) 850fc0065adSTiezhu Yang { 851fc0065adSTiezhu Yang size_t crc_offset = 0; 852d7eb8f1cSChao Yu __u32 crc; 853fc0065adSTiezhu Yang 8544d57b86dSChao Yu *cp_page = f2fs_get_meta_page(sbi, cp_addr); 8557735730dSChao Yu if (IS_ERR(*cp_page)) 8567735730dSChao Yu return PTR_ERR(*cp_page); 8577735730dSChao Yu 858fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 859fc0065adSTiezhu Yang 860fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 861d7eb8f1cSChao Yu if (crc_offset < CP_MIN_CHKSUM_OFFSET || 862d7eb8f1cSChao Yu crc_offset > CP_CHKSUM_OFFSET) { 863d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 864dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset); 865fc0065adSTiezhu Yang return -EINVAL; 866fc0065adSTiezhu Yang } 867fc0065adSTiezhu Yang 868d7eb8f1cSChao Yu crc = f2fs_checkpoint_chksum(sbi, *cp_block); 869d7eb8f1cSChao Yu if (crc != cur_cp_crc(*cp_block)) { 870d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 871dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc value"); 872fc0065adSTiezhu Yang return -EINVAL; 873fc0065adSTiezhu Yang } 874fc0065adSTiezhu Yang 875fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block); 876fc0065adSTiezhu Yang return 0; 877fc0065adSTiezhu Yang } 878fc0065adSTiezhu Yang 879127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 880127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 881127e670aSJaegeuk Kim { 882fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 883fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL; 884127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 8855b5b4f85SChao Yu unsigned int cp_blocks; 886fc0065adSTiezhu Yang int err; 887127e670aSJaegeuk Kim 888fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 889fc0065adSTiezhu Yang &cp_page_1, version); 890fc0065adSTiezhu Yang if (err) 891d3f07c04SChao Yu return NULL; 892c9b60788SChao Yu 8935b5b4f85SChao Yu cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count); 8945b5b4f85SChao Yu 8955b5b4f85SChao Yu if (cp_blocks > sbi->blocks_per_seg || cp_blocks <= F2FS_CP_PACKS) { 896dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", 897c9b60788SChao Yu le32_to_cpu(cp_block->cp_pack_total_block_count)); 898d3f07c04SChao Yu goto invalid_cp; 899c9b60788SChao Yu } 900fc0065adSTiezhu Yang pre_version = *version; 901127e670aSJaegeuk Kim 9025b5b4f85SChao Yu cp_addr += cp_blocks - 1; 903fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 904fc0065adSTiezhu Yang &cp_page_2, version); 905fc0065adSTiezhu Yang if (err) 906d3f07c04SChao Yu goto invalid_cp; 907fc0065adSTiezhu Yang cur_version = *version; 908127e670aSJaegeuk Kim 909127e670aSJaegeuk Kim if (cur_version == pre_version) { 910127e670aSJaegeuk Kim *version = cur_version; 911127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 912127e670aSJaegeuk Kim return cp_page_1; 913127e670aSJaegeuk Kim } 914127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 915d3f07c04SChao Yu invalid_cp: 916127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 917127e670aSJaegeuk Kim return NULL; 918127e670aSJaegeuk Kim } 919127e670aSJaegeuk Kim 9204d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi) 921127e670aSJaegeuk Kim { 922127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 923127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 924127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 925127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 926127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 927127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 92855141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 9291dbe4152SChangman Lee block_t cp_blk_no; 9301dbe4152SChangman Lee int i; 93110f966bbSChao Yu int err; 932127e670aSJaegeuk Kim 9330b6d4ca0SEric Biggers sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks), 934026f0507SKees Cook GFP_KERNEL); 935127e670aSJaegeuk Kim if (!sbi->ckpt) 936127e670aSJaegeuk Kim return -ENOMEM; 937127e670aSJaegeuk Kim /* 938127e670aSJaegeuk Kim * Finding out valid cp block involves read both 939127e670aSJaegeuk Kim * sets( cp pack 1 and cp pack 2) 940127e670aSJaegeuk Kim */ 941127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 942127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 943127e670aSJaegeuk Kim 944127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 945f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 946f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 947127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 948127e670aSJaegeuk Kim 949127e670aSJaegeuk Kim if (cp1 && cp2) { 950127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 951127e670aSJaegeuk Kim cur_page = cp2; 952127e670aSJaegeuk Kim else 953127e670aSJaegeuk Kim cur_page = cp1; 954127e670aSJaegeuk Kim } else if (cp1) { 955127e670aSJaegeuk Kim cur_page = cp1; 956127e670aSJaegeuk Kim } else if (cp2) { 957127e670aSJaegeuk Kim cur_page = cp2; 958127e670aSJaegeuk Kim } else { 95910f966bbSChao Yu err = -EFSCORRUPTED; 960127e670aSJaegeuk Kim goto fail_no_cp; 961127e670aSJaegeuk Kim } 962127e670aSJaegeuk Kim 963127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 964127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 965127e670aSJaegeuk Kim 9668508e44aSJaegeuk Kim if (cur_page == cp1) 9678508e44aSJaegeuk Kim sbi->cur_cp_pack = 1; 9688508e44aSJaegeuk Kim else 9698508e44aSJaegeuk Kim sbi->cur_cp_pack = 2; 970984ec63cSShawn Lin 971e494c2f9SChao Yu /* Sanity checking of checkpoint */ 97210f966bbSChao Yu if (f2fs_sanity_check_ckpt(sbi)) { 97310f966bbSChao Yu err = -EFSCORRUPTED; 974e494c2f9SChao Yu goto free_fail_no_cp; 97510f966bbSChao Yu } 976e494c2f9SChao Yu 9771dbe4152SChangman Lee if (cp_blks <= 1) 9781dbe4152SChangman Lee goto done; 9791dbe4152SChangman Lee 9801dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 9811dbe4152SChangman Lee if (cur_page == cp2) 9821dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 9831dbe4152SChangman Lee 9841dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 9851dbe4152SChangman Lee void *sit_bitmap_ptr; 9861dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 9871dbe4152SChangman Lee 9884d57b86dSChao Yu cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i); 98910f966bbSChao Yu if (IS_ERR(cur_page)) { 99010f966bbSChao Yu err = PTR_ERR(cur_page); 9917735730dSChao Yu goto free_fail_no_cp; 99210f966bbSChao Yu } 9931dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 9941dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 9951dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 9961dbe4152SChangman Lee } 9971dbe4152SChangman Lee done: 998127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 999127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 1000127e670aSJaegeuk Kim return 0; 1001127e670aSJaegeuk Kim 1002a2125ff7SJaegeuk Kim free_fail_no_cp: 1003a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1); 1004a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1); 1005127e670aSJaegeuk Kim fail_no_cp: 10065222595dSJaegeuk Kim kvfree(sbi->ckpt); 100710f966bbSChao Yu return err; 1008127e670aSJaegeuk Kim } 1009127e670aSJaegeuk Kim 1010c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 1011127e670aSJaegeuk Kim { 10124081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1013c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 10142d7b822aSChao Yu 101591942321SJaegeuk Kim if (is_inode_flag_set(inode, flag)) 10162710fd7eSChao Yu return; 10172d7b822aSChao Yu 101891942321SJaegeuk Kim set_inode_flag(inode, flag); 10197bc155feSJaegeuk Kim list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]); 102033fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 10215deb8267SJaegeuk Kim } 10225deb8267SJaegeuk Kim 1023c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 10246ad7609aSChao Yu { 1025c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 10266ad7609aSChao Yu 102791942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 10286ad7609aSChao Yu return; 10296ad7609aSChao Yu 103091942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list); 103191942321SJaegeuk Kim clear_inode_flag(inode, flag); 103233fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 10336ad7609aSChao Yu } 10346ad7609aSChao Yu 10354f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio) 10365deb8267SJaegeuk Kim { 10374081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1038c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 10395deb8267SJaegeuk Kim 10405ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 10415ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 1042127e670aSJaegeuk Kim return; 10437bd59381SGu Zheng 1044c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 10451c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 1046c227f912SChao Yu __add_dirty_inode(inode, type); 1047b951a4ecSYunlei He inode_inc_dirty_pages(inode); 10481c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]); 10491c4bf763SJaegeuk Kim 10504f5e34f7SMatthew Wilcox (Oracle) set_page_private_reference(&folio->page); 10515deb8267SJaegeuk Kim } 1052127e670aSJaegeuk Kim 10534d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode) 1054127e670aSJaegeuk Kim { 10554081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1056c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 1057127e670aSJaegeuk Kim 1058c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 1059c227f912SChao Yu !S_ISLNK(inode->i_mode)) 1060127e670aSJaegeuk Kim return; 1061127e670aSJaegeuk Kim 106210aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 106310aa97c3SJaegeuk Kim return; 106410aa97c3SJaegeuk Kim 1065c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1066c227f912SChao Yu __remove_dirty_inode(inode, type); 1067c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1068afc3eda2SJaegeuk Kim } 106974d0b917SJaegeuk Kim 1070d80afefbSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type, 1071d80afefbSChao Yu bool from_cp) 1072127e670aSJaegeuk Kim { 1073ce3b7d80SGu Zheng struct list_head *head; 1074127e670aSJaegeuk Kim struct inode *inode; 10752710fd7eSChao Yu struct f2fs_inode_info *fi; 10764cf18537SChao Yu bool is_dir = (type == DIR_INODE); 10774db08d01SJaegeuk Kim unsigned long ino = 0; 10784cf18537SChao Yu 10794cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 10804cf18537SChao Yu get_pages(sbi, is_dir ? 10814cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1082127e670aSJaegeuk Kim retry: 10839b664822SZhang Qilong if (unlikely(f2fs_cp_error(sbi))) { 10849b664822SZhang Qilong trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10859b664822SZhang Qilong get_pages(sbi, is_dir ? 10869b664822SZhang Qilong F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10876d5a1495SChao Yu return -EIO; 10889b664822SZhang Qilong } 1089af41d3eeSJaegeuk Kim 1090c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1091ce3b7d80SGu Zheng 1092c227f912SChao Yu head = &sbi->inode_list[type]; 1093127e670aSJaegeuk Kim if (list_empty(head)) { 1094c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 10954cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10964cf18537SChao Yu get_pages(sbi, is_dir ? 10974cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10986d5a1495SChao Yu return 0; 1099127e670aSJaegeuk Kim } 1100939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 11012710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 1102c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1103127e670aSJaegeuk Kim if (inode) { 11044db08d01SJaegeuk Kim unsigned long cur_ino = inode->i_ino; 11054db08d01SJaegeuk Kim 1106d80afefbSChao Yu if (from_cp) 1107b0af6d49SChao Yu F2FS_I(inode)->cp_task = current; 1108d80afefbSChao Yu F2FS_I(inode)->wb_task = current; 1109b0af6d49SChao Yu 111087d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 1111b0af6d49SChao Yu 1112d80afefbSChao Yu F2FS_I(inode)->wb_task = NULL; 1113d80afefbSChao Yu if (from_cp) 1114b0af6d49SChao Yu F2FS_I(inode)->cp_task = NULL; 1115b0af6d49SChao Yu 1116127e670aSJaegeuk Kim iput(inode); 11174db08d01SJaegeuk Kim /* We need to give cpu to another writers. */ 11182a63531aSYunlei He if (ino == cur_ino) 11194db08d01SJaegeuk Kim cond_resched(); 11202a63531aSYunlei He else 11214db08d01SJaegeuk Kim ino = cur_ino; 1122127e670aSJaegeuk Kim } else { 1123127e670aSJaegeuk Kim /* 1124127e670aSJaegeuk Kim * We should submit bio, since it exists several 1125127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 1126127e670aSJaegeuk Kim */ 1127b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 11287ecebe5eSSebastian Andrzej Siewior cond_resched(); 1129127e670aSJaegeuk Kim } 1130127e670aSJaegeuk Kim goto retry; 1131127e670aSJaegeuk Kim } 1132127e670aSJaegeuk Kim 11330f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 11340f18b462SJaegeuk Kim { 11350f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META]; 11360f18b462SJaegeuk Kim struct inode *inode; 11370f18b462SJaegeuk Kim struct f2fs_inode_info *fi; 11380f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 11390f18b462SJaegeuk Kim 11400f18b462SJaegeuk Kim while (total--) { 11410f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 11420f18b462SJaegeuk Kim return -EIO; 11430f18b462SJaegeuk Kim 11440f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 11450f18b462SJaegeuk Kim if (list_empty(head)) { 11460f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11470f18b462SJaegeuk Kim return 0; 11480f18b462SJaegeuk Kim } 1149939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, 11500f18b462SJaegeuk Kim gdirty_list); 11510f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode); 11520f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11530f18b462SJaegeuk Kim if (inode) { 115418340edcSJaegeuk Kim sync_inode_metadata(inode, 0); 115518340edcSJaegeuk Kim 115618340edcSJaegeuk Kim /* it's on eviction */ 115718340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 11584d57b86dSChao Yu f2fs_update_inode_page(inode); 11590f18b462SJaegeuk Kim iput(inode); 11600f18b462SJaegeuk Kim } 1161dee668c1SChao Yu } 11620f18b462SJaegeuk Kim return 0; 11630f18b462SJaegeuk Kim } 11640f18b462SJaegeuk Kim 116559c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi) 116659c9081bSYunlei He { 116759c9081bSYunlei He struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 116859c9081bSYunlei He struct f2fs_nm_info *nm_i = NM_I(sbi); 116959c9081bSYunlei He nid_t last_nid = nm_i->next_scan_nid; 117059c9081bSYunlei He 117159c9081bSYunlei He next_free_nid(sbi, &last_nid); 117259c9081bSYunlei He ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 117359c9081bSYunlei He ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 117459c9081bSYunlei He ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 117559c9081bSYunlei He ckpt->next_free_nid = cpu_to_le32(last_nid); 117659c9081bSYunlei He } 117759c9081bSYunlei He 1178af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi) 1179af033b2aSChao Yu { 1180db6ec53bSJaegeuk Kim bool ret = false; 1181db6ec53bSJaegeuk Kim 1182af033b2aSChao Yu if (!is_journalled_quota(sbi)) 1183af033b2aSChao Yu return false; 1184db6ec53bSJaegeuk Kim 1185e4544b63STim Murray if (!f2fs_down_write_trylock(&sbi->quota_sem)) 1186a5c00422SChao Yu return true; 1187db6ec53bSJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { 1188db6ec53bSJaegeuk Kim ret = false; 1189db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { 1190db6ec53bSJaegeuk Kim ret = false; 1191db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) { 1192db6ec53bSJaegeuk Kim clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1193db6ec53bSJaegeuk Kim ret = true; 1194db6ec53bSJaegeuk Kim } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { 1195db6ec53bSJaegeuk Kim ret = true; 1196db6ec53bSJaegeuk Kim } 1197e4544b63STim Murray f2fs_up_write(&sbi->quota_sem); 1198db6ec53bSJaegeuk Kim return ret; 1199af033b2aSChao Yu } 1200af033b2aSChao Yu 12010a8165d7SJaegeuk Kim /* 1202127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 1203127e670aSJaegeuk Kim */ 1204cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 1205127e670aSJaegeuk Kim { 1206127e670aSJaegeuk Kim struct writeback_control wbc = { 1207127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 1208127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 1209127e670aSJaegeuk Kim .for_reclaim = 0, 1210127e670aSJaegeuk Kim }; 1211af033b2aSChao Yu int err = 0, cnt = 0; 1212c718379bSJaegeuk Kim 121334c061adSSayali Lokhande /* 121434c061adSSayali Lokhande * Let's flush inline_data in dirty node pages. 121534c061adSSayali Lokhande */ 121634c061adSSayali Lokhande f2fs_flush_inline_data(sbi); 121734c061adSSayali Lokhande 1218af033b2aSChao Yu retry_flush_quotas: 1219db6ec53bSJaegeuk Kim f2fs_lock_all(sbi); 1220af033b2aSChao Yu if (__need_flush_quota(sbi)) { 1221af033b2aSChao Yu int locked; 1222af033b2aSChao Yu 1223af033b2aSChao Yu if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { 1224af033b2aSChao Yu set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1225db6ec53bSJaegeuk Kim set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1226af033b2aSChao Yu goto retry_flush_dents; 1227af033b2aSChao Yu } 1228db6ec53bSJaegeuk Kim f2fs_unlock_all(sbi); 1229af033b2aSChao Yu 1230af033b2aSChao Yu /* only failed during mount/umount/freeze/quotactl */ 1231af033b2aSChao Yu locked = down_read_trylock(&sbi->sb->s_umount); 1232af033b2aSChao Yu f2fs_quota_sync(sbi->sb, -1); 1233af033b2aSChao Yu if (locked) 1234af033b2aSChao Yu up_read(&sbi->sb->s_umount); 1235af033b2aSChao Yu cond_resched(); 1236af033b2aSChao Yu goto retry_flush_quotas; 1237af033b2aSChao Yu } 1238af033b2aSChao Yu 1239af033b2aSChao Yu retry_flush_dents: 1240127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 1241127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 1242e479556bSGu Zheng f2fs_unlock_all(sbi); 1243d80afefbSChao Yu err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true); 12446d5a1495SChao Yu if (err) 12451f5f11a3SJaegeuk Kim return err; 124630973883SJaegeuk Kim cond_resched(); 1247af033b2aSChao Yu goto retry_flush_quotas; 1248127e670aSJaegeuk Kim } 1249127e670aSJaegeuk Kim 125059c9081bSYunlei He /* 125159c9081bSYunlei He * POR: we should ensure that there are no dirty node pages 125259c9081bSYunlei He * until finishing nat/sit flush. inode->i_blocks can be updated. 125359c9081bSYunlei He */ 1254e4544b63STim Murray f2fs_down_write(&sbi->node_change); 125559c9081bSYunlei He 12560f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 1257e4544b63STim Murray f2fs_up_write(&sbi->node_change); 12580f18b462SJaegeuk Kim f2fs_unlock_all(sbi); 12590f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi); 12600f18b462SJaegeuk Kim if (err) 12611f5f11a3SJaegeuk Kim return err; 126230973883SJaegeuk Kim cond_resched(); 1263af033b2aSChao Yu goto retry_flush_quotas; 12640f18b462SJaegeuk Kim } 12650f18b462SJaegeuk Kim 126639936837SJaegeuk Kim retry_flush_nodes: 1267e4544b63STim Murray f2fs_down_write(&sbi->node_write); 1268127e670aSJaegeuk Kim 1269127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1270e4544b63STim Murray f2fs_up_write(&sbi->node_write); 1271c29fd0c0SChao Yu atomic_inc(&sbi->wb_sync_req[NODE]); 12724d57b86dSChao Yu err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); 1273c29fd0c0SChao Yu atomic_dec(&sbi->wb_sync_req[NODE]); 12746d5a1495SChao Yu if (err) { 1275e4544b63STim Murray f2fs_up_write(&sbi->node_change); 1276cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 12771f5f11a3SJaegeuk Kim return err; 1278cf779cabSJaegeuk Kim } 127930973883SJaegeuk Kim cond_resched(); 128039936837SJaegeuk Kim goto retry_flush_nodes; 1281127e670aSJaegeuk Kim } 128259c9081bSYunlei He 128359c9081bSYunlei He /* 128459c9081bSYunlei He * sbi->node_change is used only for AIO write_begin path which produces 128559c9081bSYunlei He * dirty node blocks and some checkpoint values by block allocation. 128659c9081bSYunlei He */ 128759c9081bSYunlei He __prepare_cp_block(sbi); 1288e4544b63STim Murray f2fs_up_write(&sbi->node_change); 1289cf779cabSJaegeuk Kim return err; 1290127e670aSJaegeuk Kim } 1291127e670aSJaegeuk Kim 1292127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 1293127e670aSJaegeuk Kim { 1294e4544b63STim Murray f2fs_up_write(&sbi->node_write); 1295e479556bSGu Zheng f2fs_unlock_all(sbi); 1296127e670aSJaegeuk Kim } 1297127e670aSJaegeuk Kim 1298bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type) 1299fb51b5efSChangman Lee { 1300fb51b5efSChangman Lee DEFINE_WAIT(wait); 1301fb51b5efSChangman Lee 1302fb51b5efSChangman Lee for (;;) { 1303bf22c3ccSSahitya Tummala if (!get_pages(sbi, type)) 1304fb51b5efSChangman Lee break; 1305fb51b5efSChangman Lee 1306af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1307af697c0fSJaegeuk Kim break; 1308af697c0fSJaegeuk Kim 13099c30df7cSJaegeuk Kim if (type == F2FS_DIRTY_META) 13109c30df7cSJaegeuk Kim f2fs_sync_meta_pages(sbi, META, LONG_MAX, 13119c30df7cSJaegeuk Kim FS_CP_META_IO); 13121fd28018SJaegeuk Kim else if (type == F2FS_WB_CP_DATA) 13131fd28018SJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 1314828add77SJaegeuk Kim 1315828add77SJaegeuk Kim prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 13165df7731fSChao Yu io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1317fb51b5efSChangman Lee } 1318fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 1319fb51b5efSChangman Lee } 1320fb51b5efSChangman Lee 1321e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1322e4c5d848SJaegeuk Kim { 1323e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1324e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1325d1aa2453SChao Yu unsigned long flags; 1326e4c5d848SJaegeuk Kim 132794c821fbSChao Yu if (cpc->reason & CP_UMOUNT) { 1328b702c83eSChao Yu if (le32_to_cpu(ckpt->cp_pack_total_block_count) + 1329b702c83eSChao Yu NM_I(sbi)->nat_bits_blocks > sbi->blocks_per_seg) { 133094c821fbSChao Yu clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 133194c821fbSChao Yu f2fs_notice(sbi, "Disable nat_bits due to no space"); 133294c821fbSChao Yu } else if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG) && 133394c821fbSChao Yu f2fs_nat_bitmap_enabled(sbi)) { 133494c821fbSChao Yu f2fs_enable_nat_bits(sbi); 133594c821fbSChao Yu set_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 133694c821fbSChao Yu f2fs_notice(sbi, "Rebuild and enable nat_bits"); 133794c821fbSChao Yu } 133894c821fbSChao Yu } 1339e4c5d848SJaegeuk Kim 134094c821fbSChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 134122ad0b6aSJaegeuk Kim 13421f43e2adSChao Yu if (cpc->reason & CP_TRIMMED) 13431f43e2adSChao Yu __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 1344cd36d7a1SChao Yu else 1345cd36d7a1SChao Yu __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 13461f43e2adSChao Yu 1347c473f1a9SChao Yu if (cpc->reason & CP_UMOUNT) 1348e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1349e4c5d848SJaegeuk Kim else 1350e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1351e4c5d848SJaegeuk Kim 1352c473f1a9SChao Yu if (cpc->reason & CP_FASTBOOT) 1353e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1354e4c5d848SJaegeuk Kim else 1355e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1356e4c5d848SJaegeuk Kim 1357e4c5d848SJaegeuk Kim if (orphan_num) 1358e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1359e4c5d848SJaegeuk Kim else 1360e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1361e4c5d848SJaegeuk Kim 1362c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1363e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1364e4c5d848SJaegeuk Kim 1365c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS)) 1366c84ef3c5SSahitya Tummala __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1367c84ef3c5SSahitya Tummala else 1368c84ef3c5SSahitya Tummala __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1369c84ef3c5SSahitya Tummala 13704354994fSDaniel Rosenberg if (is_sbi_flag_set(sbi, SBI_CP_DISABLED)) 13714354994fSDaniel Rosenberg __set_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13724354994fSDaniel Rosenberg else 13734354994fSDaniel Rosenberg __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13744354994fSDaniel Rosenberg 1375db610a64SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK)) 1376db610a64SJaegeuk Kim __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1377db610a64SJaegeuk Kim else 1378db610a64SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1379db610a64SJaegeuk Kim 1380af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) 1381af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1382bc88ac96SJaegeuk Kim else 1383bc88ac96SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1384af033b2aSChao Yu 1385af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) 1386af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1387af033b2aSChao Yu 1388e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */ 1389e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1390f2367923SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); 1391e4c5d848SJaegeuk Kim 1392d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1393e4c5d848SJaegeuk Kim } 1394e4c5d848SJaegeuk Kim 139546706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi, 139646706d59SGao Xiang void *src, block_t blk_addr) 139746706d59SGao Xiang { 139846706d59SGao Xiang struct writeback_control wbc = { 139946706d59SGao Xiang .for_reclaim = 0, 140046706d59SGao Xiang }; 140146706d59SGao Xiang 140246706d59SGao Xiang /* 140346706d59SGao Xiang * pagevec_lookup_tag and lock_page again will take 14044d57b86dSChao Yu * some extra time. Therefore, f2fs_update_meta_pages and 14054d57b86dSChao Yu * f2fs_sync_meta_pages are combined in this function. 140646706d59SGao Xiang */ 14074d57b86dSChao Yu struct page *page = f2fs_grab_meta_page(sbi, blk_addr); 140846706d59SGao Xiang int err; 140946706d59SGao Xiang 1410bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 14118d64d365SChao Yu 14128d64d365SChao Yu memcpy(page_address(page), src, PAGE_SIZE); 14138d64d365SChao Yu 14148d64d365SChao Yu set_page_dirty(page); 141546706d59SGao Xiang if (unlikely(!clear_page_dirty_for_io(page))) 141646706d59SGao Xiang f2fs_bug_on(sbi, 1); 141746706d59SGao Xiang 141846706d59SGao Xiang /* writeout cp pack 2 page */ 141946706d59SGao Xiang err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO); 1420af697c0fSJaegeuk Kim if (unlikely(err && f2fs_cp_error(sbi))) { 1421af697c0fSJaegeuk Kim f2fs_put_page(page, 1); 1422af697c0fSJaegeuk Kim return; 1423af697c0fSJaegeuk Kim } 142446706d59SGao Xiang 1425af697c0fSJaegeuk Kim f2fs_bug_on(sbi, err); 142646706d59SGao Xiang f2fs_put_page(page, 0); 142746706d59SGao Xiang 142846706d59SGao Xiang /* submit checkpoint (with barrier if NOBARRIER is not set) */ 142946706d59SGao Xiang f2fs_submit_merged_write(sbi, META_FLUSH); 143046706d59SGao Xiang } 143146706d59SGao Xiang 14323a0a9cbcSChao Yu static inline u64 get_sectors_written(struct block_device *bdev) 14333a0a9cbcSChao Yu { 1434ff49c86fSLinus Torvalds return (u64)part_stat_read(bdev, sectors[STAT_WRITE]); 14353a0a9cbcSChao Yu } 14363a0a9cbcSChao Yu 14373a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi) 14383a0a9cbcSChao Yu { 14393a0a9cbcSChao Yu if (f2fs_is_multi_device(sbi)) { 14403a0a9cbcSChao Yu u64 sectors = 0; 14413a0a9cbcSChao Yu int i; 14423a0a9cbcSChao Yu 14433a0a9cbcSChao Yu for (i = 0; i < sbi->s_ndevs; i++) 14443a0a9cbcSChao Yu sectors += get_sectors_written(FDEV(i).bdev); 14453a0a9cbcSChao Yu 14463a0a9cbcSChao Yu return sectors; 14473a0a9cbcSChao Yu } 14483a0a9cbcSChao Yu 14493a0a9cbcSChao Yu return get_sectors_written(sbi->sb->s_bdev); 14503a0a9cbcSChao Yu } 14513a0a9cbcSChao Yu 1452c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1453127e670aSJaegeuk Kim { 1454127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 145577041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 1456d1aa2453SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags; 1457127e670aSJaegeuk Kim block_t start_blk; 1458127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 14597e586fa0SJaegeuk Kim __u32 crc32 = 0; 1460127e670aSJaegeuk Kim int i; 146155141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 14628f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 14638f1dbbbbSShuoran Liu u64 kbytes_written; 14641228b482SChao Yu int err; 1465127e670aSJaegeuk Kim 1466127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 14674d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1468127e670aSJaegeuk Kim 14697a88ddb5SChao Yu /* start to update checkpoint, cp ver is already updated previously */ 1470a1f72ac2SChao Yu ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true)); 1471127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1472b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1473127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 1474127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 1475127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 1476127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 1477127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 1478127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 1479127e670aSJaegeuk Kim } 1480b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1481127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 1482127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 1483127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 1484127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 1485127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 1486127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 1487127e670aSJaegeuk Kim } 1488127e670aSJaegeuk Kim 1489127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 14904d57b86dSChao Yu data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); 1491d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1492b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1493aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1494127e670aSJaegeuk Kim else 1495aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1496d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1497127e670aSJaegeuk Kim 149867298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 14991dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 15001dbe4152SChangman Lee orphan_blocks); 1501127e670aSJaegeuk Kim 1502119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1503b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 15041dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 15051dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1506119ee914SJaegeuk Kim else 1507b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 15081dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 15091dbe4152SChangman Lee orphan_blocks); 1510119ee914SJaegeuk Kim 1511e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */ 1512e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc); 1513a468f0efSJaegeuk Kim 1514127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1515127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1516127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1517127e670aSJaegeuk Kim 1518d7eb8f1cSChao Yu crc32 = f2fs_checkpoint_chksum(sbi, ckpt); 15197e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 15207e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1521127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1522127e670aSJaegeuk Kim 15238508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi); 1524127e670aSJaegeuk Kim 152522ad0b6aSJaegeuk Kim /* write nat bits */ 152694c821fbSChao Yu if ((cpc->reason & CP_UMOUNT) && 152794c821fbSChao Yu is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) { 152822ad0b6aSJaegeuk Kim __u64 cp_ver = cur_cp_version(ckpt); 152922ad0b6aSJaegeuk Kim block_t blk; 153022ad0b6aSJaegeuk Kim 153122ad0b6aSJaegeuk Kim cp_ver |= ((__u64)crc32 << 32); 153222ad0b6aSJaegeuk Kim *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); 153322ad0b6aSJaegeuk Kim 153422ad0b6aSJaegeuk Kim blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks; 153522ad0b6aSJaegeuk Kim for (i = 0; i < nm_i->nat_bits_blocks; i++) 15364d57b86dSChao Yu f2fs_update_meta_page(sbi, nm_i->nat_bits + 153722ad0b6aSJaegeuk Kim (i << F2FS_BLKSIZE_BITS), blk + i); 153822ad0b6aSJaegeuk Kim } 153922ad0b6aSJaegeuk Kim 1540127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 15414d57b86dSChao Yu f2fs_update_meta_page(sbi, ckpt, start_blk++); 1542127e670aSJaegeuk Kim 1543381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 15444d57b86dSChao Yu f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1545381722d2SChao Yu start_blk++); 15461dbe4152SChangman Lee 154767298804SChao Yu if (orphan_num) { 1548127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1549127e670aSJaegeuk Kim start_blk += orphan_blocks; 1550127e670aSJaegeuk Kim } 1551127e670aSJaegeuk Kim 15524d57b86dSChao Yu f2fs_write_data_summaries(sbi, start_blk); 1553127e670aSJaegeuk Kim start_blk += data_sum_blocks; 15548f1dbbbbSShuoran Liu 15558f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 15568f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 15573a0a9cbcSChao Yu kbytes_written += (f2fs_get_sectors_written(sbi) - 15583a0a9cbcSChao Yu sbi->sectors_written_start) >> 1; 1559b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 15608f1dbbbbSShuoran Liu 1561119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 15624d57b86dSChao Yu f2fs_write_node_summaries(sbi, start_blk); 1563127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1564127e670aSJaegeuk Kim } 1565127e670aSJaegeuk Kim 156646706d59SGao Xiang /* update user_block_counts */ 156746706d59SGao Xiang sbi->last_valid_block_count = sbi->total_valid_block_count; 156846706d59SGao Xiang percpu_counter_set(&sbi->alloc_valid_block_count, 0); 156947c8ebccSJaegeuk Kim percpu_counter_set(&sbi->rf_node_block_count, 0); 1570127e670aSJaegeuk Kim 157146706d59SGao Xiang /* Here, we have one bio having CP pack except cp pack 2 page */ 15724d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1573bf22c3ccSSahitya Tummala /* Wait for all dirty meta pages to be submitted for IO */ 1574bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META); 157546706d59SGao Xiang 157646706d59SGao Xiang /* wait for previous submitted meta pages writeback */ 1577bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1578127e670aSJaegeuk Kim 157946706d59SGao Xiang /* flush all device cache */ 158046706d59SGao Xiang err = f2fs_flush_device_cache(sbi); 158146706d59SGao Xiang if (err) 158246706d59SGao Xiang return err; 1583127e670aSJaegeuk Kim 158446706d59SGao Xiang /* barrier and flush checkpoint cp pack 2 page if it can */ 158546706d59SGao Xiang commit_checkpoint(sbi, ckpt, start_blk); 1586bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 15876a8f8ca5SJaegeuk Kim 158818767e62SChao Yu /* 1589542989b6SEric Biggers * invalidate intermediate page cache borrowed from meta inode which are 1590aff6fbbeSChao Yu * used for migration of encrypted, verity or compressed inode's blocks. 159118767e62SChao Yu */ 1592aff6fbbeSChao Yu if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) || 1593aff6fbbeSChao Yu f2fs_sb_has_compression(sbi)) 159418767e62SChao Yu invalidate_mapping_pages(META_MAPPING(sbi), 159518767e62SChao Yu MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1); 159618767e62SChao Yu 15974d57b86dSChao Yu f2fs_release_ino_entry(sbi, false); 1598cf779cabSJaegeuk Kim 159950fa53ecSChao Yu f2fs_reset_fsync_node_info(sbi); 160050fa53ecSChao Yu 1601caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1602bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP); 1603af033b2aSChao Yu clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1604c9c8ed50SChao Yu 1605c9c8ed50SChao Yu spin_lock(&sbi->stat_lock); 16064354994fSDaniel Rosenberg sbi->unusable_block_count = 0; 1607c9c8ed50SChao Yu spin_unlock(&sbi->stat_lock); 1608c9c8ed50SChao Yu 16098508e44aSJaegeuk Kim __set_cp_next_pack(sbi); 1610c34f42e2SChao Yu 1611c2a080aeSChao Yu /* 1612c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is 1613c2a080aeSChao Yu * updated during writing checkpoint. 1614c2a080aeSChao Yu */ 1615c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) || 1616c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA)) 1617c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 1618c2a080aeSChao Yu 1619c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1620c2a080aeSChao Yu 1621af697c0fSJaegeuk Kim return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0; 1622127e670aSJaegeuk Kim } 1623127e670aSJaegeuk Kim 16244d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1625127e670aSJaegeuk Kim { 1626127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1627127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1628c34f42e2SChao Yu int err = 0; 1629127e670aSJaegeuk Kim 1630f5a131bbSChao Yu if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi)) 1631f5a131bbSChao Yu return -EROFS; 1632f5a131bbSChao Yu 16334354994fSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 16344354994fSDaniel Rosenberg if (cpc->reason != CP_PAUSE) 16354354994fSDaniel Rosenberg return 0; 1636dcbb4c10SJoe Perches f2fs_warn(sbi, "Start checkpoint disabled!"); 16374354994fSDaniel Rosenberg } 1638b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 1639e4544b63STim Murray f2fs_down_write(&sbi->cp_global_sem); 16408501017eSJaegeuk Kim 1641caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1642c473f1a9SChao Yu ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1643c473f1a9SChao Yu ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) 16448501017eSJaegeuk Kim goto out; 1645c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1646c34f42e2SChao Yu err = -EIO; 1647cf779cabSJaegeuk Kim goto out; 1648c34f42e2SChao Yu } 16492bda542dSWanpeng Li 16502bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 16512bda542dSWanpeng Li 1652c34f42e2SChao Yu err = block_operations(sbi); 1653c34f42e2SChao Yu if (err) 1654cf779cabSJaegeuk Kim goto out; 1655127e670aSJaegeuk Kim 165675ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 16572af4bd6cSNamjae Jeon 1658b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 1659127e670aSJaegeuk Kim 166058cce381SYunlei He /* this is the case of multiple fstrims without any changes */ 1661c473f1a9SChao Yu if (cpc->reason & CP_DISCARD) { 16624d57b86dSChao Yu if (!f2fs_exist_trim_candidates(sbi, cpc)) { 166325290fa5SJaegeuk Kim unblock_operations(sbi); 166425290fa5SJaegeuk Kim goto out; 166525290fa5SJaegeuk Kim } 166625290fa5SJaegeuk Kim 1667a95ba66aSJaegeuk Kim if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 && 16680333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 && 16690333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) { 16704d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 16714d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 167258cce381SYunlei He unblock_operations(sbi); 167358cce381SYunlei He goto out; 167458cce381SYunlei He } 16750333ad4eSJaegeuk Kim } 167658cce381SYunlei He 1677127e670aSJaegeuk Kim /* 1678127e670aSJaegeuk Kim * update checkpoint pack index 1679127e670aSJaegeuk Kim * Increase the version number so that 1680127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1681127e670aSJaegeuk Kim */ 1682d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1683127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1684127e670aSJaegeuk Kim 1685127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1686edc55aafSJaegeuk Kim err = f2fs_flush_nat_entries(sbi, cpc); 168791803392SChao Yu if (err) { 168891803392SChao Yu f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err); 168991803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 1690edc55aafSJaegeuk Kim goto stop; 169191803392SChao Yu } 1692edc55aafSJaegeuk Kim 16934d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 1694127e670aSJaegeuk Kim 1695d0b9e42aSChao Yu /* save inmem log status */ 1696093749e2SChao Yu f2fs_save_inmem_curseg(sbi); 1697d0b9e42aSChao Yu 1698c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 169991803392SChao Yu if (err) { 170091803392SChao Yu f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err); 170191803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 17024d57b86dSChao Yu f2fs_release_discard_addrs(sbi); 170391803392SChao Yu } else { 17044d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 170591803392SChao Yu } 1706d0b9e42aSChao Yu 1707093749e2SChao Yu f2fs_restore_inmem_curseg(sbi); 1708edc55aafSJaegeuk Kim stop: 1709127e670aSJaegeuk Kim unblock_operations(sbi); 1710942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 171110027551SJaegeuk Kim 1712c473f1a9SChao Yu if (cpc->reason & CP_RECOVERY) 1713dcbb4c10SJoe Perches f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver); 171460b99b48SJaegeuk Kim 17157a88ddb5SChao Yu /* update CP_TIME to trigger checkpoint periodically */ 17166beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 171755d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 17188501017eSJaegeuk Kim out: 1719b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 1720e4544b63STim Murray f2fs_up_write(&sbi->cp_global_sem); 1721c34f42e2SChao Yu return err; 1722127e670aSJaegeuk Kim } 1723127e670aSJaegeuk Kim 17244d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi) 1725127e670aSJaegeuk Kim { 17266451e041SJaegeuk Kim int i; 17276451e041SJaegeuk Kim 17286451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 172967298804SChao Yu struct inode_management *im = &sbi->im[i]; 173067298804SChao Yu 173167298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 173267298804SChao Yu spin_lock_init(&im->ino_lock); 173367298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 173467298804SChao Yu im->ino_num = 0; 17356451e041SJaegeuk Kim } 17366451e041SJaegeuk Kim 1737b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 1738d0b9e42aSChao Yu NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) * 173914b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1740127e670aSJaegeuk Kim } 1741127e670aSJaegeuk Kim 17424d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void) 1743127e670aSJaegeuk Kim { 17446451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 17456451e041SJaegeuk Kim sizeof(struct ino_entry)); 17466451e041SJaegeuk Kim if (!ino_entry_slab) 1747127e670aSJaegeuk Kim return -ENOMEM; 17484d57b86dSChao Yu f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 174906292073SChao Yu sizeof(struct inode_entry)); 17504d57b86dSChao Yu if (!f2fs_inode_entry_slab) { 17516451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1752127e670aSJaegeuk Kim return -ENOMEM; 1753127e670aSJaegeuk Kim } 1754127e670aSJaegeuk Kim return 0; 1755127e670aSJaegeuk Kim } 1756127e670aSJaegeuk Kim 17574d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void) 1758127e670aSJaegeuk Kim { 17596451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 17604d57b86dSChao Yu kmem_cache_destroy(f2fs_inode_entry_slab); 1761127e670aSJaegeuk Kim } 1762261eeb9cSDaeho Jeong 1763261eeb9cSDaeho Jeong static int __write_checkpoint_sync(struct f2fs_sb_info *sbi) 1764261eeb9cSDaeho Jeong { 1765261eeb9cSDaeho Jeong struct cp_control cpc = { .reason = CP_SYNC, }; 1766261eeb9cSDaeho Jeong int err; 1767261eeb9cSDaeho Jeong 1768e4544b63STim Murray f2fs_down_write(&sbi->gc_lock); 1769261eeb9cSDaeho Jeong err = f2fs_write_checkpoint(sbi, &cpc); 1770e4544b63STim Murray f2fs_up_write(&sbi->gc_lock); 1771261eeb9cSDaeho Jeong 1772261eeb9cSDaeho Jeong return err; 1773261eeb9cSDaeho Jeong } 1774261eeb9cSDaeho Jeong 1775261eeb9cSDaeho Jeong static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi) 1776261eeb9cSDaeho Jeong { 1777261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1778261eeb9cSDaeho Jeong struct ckpt_req *req, *next; 1779261eeb9cSDaeho Jeong struct llist_node *dispatch_list; 1780261eeb9cSDaeho Jeong u64 sum_diff = 0, diff, count = 0; 1781261eeb9cSDaeho Jeong int ret; 1782261eeb9cSDaeho Jeong 1783261eeb9cSDaeho Jeong dispatch_list = llist_del_all(&cprc->issue_list); 1784261eeb9cSDaeho Jeong if (!dispatch_list) 1785261eeb9cSDaeho Jeong return; 1786261eeb9cSDaeho Jeong dispatch_list = llist_reverse_order(dispatch_list); 1787261eeb9cSDaeho Jeong 1788261eeb9cSDaeho Jeong ret = __write_checkpoint_sync(sbi); 1789261eeb9cSDaeho Jeong atomic_inc(&cprc->issued_ckpt); 1790261eeb9cSDaeho Jeong 1791261eeb9cSDaeho Jeong llist_for_each_entry_safe(req, next, dispatch_list, llnode) { 1792261eeb9cSDaeho Jeong diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time); 1793261eeb9cSDaeho Jeong req->ret = ret; 1794261eeb9cSDaeho Jeong complete(&req->wait); 1795261eeb9cSDaeho Jeong 1796261eeb9cSDaeho Jeong sum_diff += diff; 1797261eeb9cSDaeho Jeong count++; 1798261eeb9cSDaeho Jeong } 1799261eeb9cSDaeho Jeong atomic_sub(count, &cprc->queued_ckpt); 1800261eeb9cSDaeho Jeong atomic_add(count, &cprc->total_ckpt); 1801261eeb9cSDaeho Jeong 1802261eeb9cSDaeho Jeong spin_lock(&cprc->stat_lock); 1803261eeb9cSDaeho Jeong cprc->cur_time = (unsigned int)div64_u64(sum_diff, count); 1804261eeb9cSDaeho Jeong if (cprc->peak_time < cprc->cur_time) 1805261eeb9cSDaeho Jeong cprc->peak_time = cprc->cur_time; 1806261eeb9cSDaeho Jeong spin_unlock(&cprc->stat_lock); 1807261eeb9cSDaeho Jeong } 1808261eeb9cSDaeho Jeong 1809261eeb9cSDaeho Jeong static int issue_checkpoint_thread(void *data) 1810261eeb9cSDaeho Jeong { 1811261eeb9cSDaeho Jeong struct f2fs_sb_info *sbi = data; 1812261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1813261eeb9cSDaeho Jeong wait_queue_head_t *q = &cprc->ckpt_wait_queue; 1814261eeb9cSDaeho Jeong repeat: 1815261eeb9cSDaeho Jeong if (kthread_should_stop()) 1816261eeb9cSDaeho Jeong return 0; 1817261eeb9cSDaeho Jeong 1818261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) 1819261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1820261eeb9cSDaeho Jeong 1821261eeb9cSDaeho Jeong wait_event_interruptible(*q, 1822261eeb9cSDaeho Jeong kthread_should_stop() || !llist_empty(&cprc->issue_list)); 1823261eeb9cSDaeho Jeong goto repeat; 1824261eeb9cSDaeho Jeong } 1825261eeb9cSDaeho Jeong 1826261eeb9cSDaeho Jeong static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi, 1827261eeb9cSDaeho Jeong struct ckpt_req *wait_req) 1828261eeb9cSDaeho Jeong { 1829261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1830261eeb9cSDaeho Jeong 1831261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) { 1832261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1833261eeb9cSDaeho Jeong } else { 1834261eeb9cSDaeho Jeong /* already dispatched by issue_checkpoint_thread */ 1835261eeb9cSDaeho Jeong if (wait_req) 1836261eeb9cSDaeho Jeong wait_for_completion(&wait_req->wait); 1837261eeb9cSDaeho Jeong } 1838261eeb9cSDaeho Jeong } 1839261eeb9cSDaeho Jeong 1840261eeb9cSDaeho Jeong static void init_ckpt_req(struct ckpt_req *req) 1841261eeb9cSDaeho Jeong { 1842261eeb9cSDaeho Jeong memset(req, 0, sizeof(struct ckpt_req)); 1843261eeb9cSDaeho Jeong 1844261eeb9cSDaeho Jeong init_completion(&req->wait); 1845261eeb9cSDaeho Jeong req->queue_time = ktime_get(); 1846261eeb9cSDaeho Jeong } 1847261eeb9cSDaeho Jeong 1848261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) 1849261eeb9cSDaeho Jeong { 1850261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1851261eeb9cSDaeho Jeong struct ckpt_req req; 1852261eeb9cSDaeho Jeong struct cp_control cpc; 1853261eeb9cSDaeho Jeong 1854261eeb9cSDaeho Jeong cpc.reason = __get_cp_reason(sbi); 1855261eeb9cSDaeho Jeong if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) { 1856261eeb9cSDaeho Jeong int ret; 1857261eeb9cSDaeho Jeong 1858e4544b63STim Murray f2fs_down_write(&sbi->gc_lock); 1859261eeb9cSDaeho Jeong ret = f2fs_write_checkpoint(sbi, &cpc); 1860e4544b63STim Murray f2fs_up_write(&sbi->gc_lock); 1861261eeb9cSDaeho Jeong 1862261eeb9cSDaeho Jeong return ret; 1863261eeb9cSDaeho Jeong } 1864261eeb9cSDaeho Jeong 1865261eeb9cSDaeho Jeong if (!cprc->f2fs_issue_ckpt) 1866261eeb9cSDaeho Jeong return __write_checkpoint_sync(sbi); 1867261eeb9cSDaeho Jeong 1868261eeb9cSDaeho Jeong init_ckpt_req(&req); 1869261eeb9cSDaeho Jeong 1870261eeb9cSDaeho Jeong llist_add(&req.llnode, &cprc->issue_list); 1871261eeb9cSDaeho Jeong atomic_inc(&cprc->queued_ckpt); 1872261eeb9cSDaeho Jeong 18733b42c741SChao Yu /* 18743b42c741SChao Yu * update issue_list before we wake up issue_checkpoint thread, 18753b42c741SChao Yu * this smp_mb() pairs with another barrier in ___wait_event(), 18763b42c741SChao Yu * see more details in comments of waitqueue_active(). 18773b42c741SChao Yu */ 1878261eeb9cSDaeho Jeong smp_mb(); 1879261eeb9cSDaeho Jeong 1880261eeb9cSDaeho Jeong if (waitqueue_active(&cprc->ckpt_wait_queue)) 1881261eeb9cSDaeho Jeong wake_up(&cprc->ckpt_wait_queue); 1882261eeb9cSDaeho Jeong 1883261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1884261eeb9cSDaeho Jeong wait_for_completion(&req.wait); 1885261eeb9cSDaeho Jeong else 1886261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, &req); 1887261eeb9cSDaeho Jeong 1888261eeb9cSDaeho Jeong return req.ret; 1889261eeb9cSDaeho Jeong } 1890261eeb9cSDaeho Jeong 1891261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi) 1892261eeb9cSDaeho Jeong { 1893261eeb9cSDaeho Jeong dev_t dev = sbi->sb->s_bdev->bd_dev; 1894261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1895261eeb9cSDaeho Jeong 1896261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1897261eeb9cSDaeho Jeong return 0; 1898261eeb9cSDaeho Jeong 1899261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi, 1900261eeb9cSDaeho Jeong "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev)); 1901261eeb9cSDaeho Jeong if (IS_ERR(cprc->f2fs_issue_ckpt)) { 1902146dbcbfSYangtao Li int err = PTR_ERR(cprc->f2fs_issue_ckpt); 1903146dbcbfSYangtao Li 1904261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1905146dbcbfSYangtao Li return err; 1906261eeb9cSDaeho Jeong } 1907261eeb9cSDaeho Jeong 1908e6592066SDaeho Jeong set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio); 1909261eeb9cSDaeho Jeong 1910261eeb9cSDaeho Jeong return 0; 1911261eeb9cSDaeho Jeong } 1912261eeb9cSDaeho Jeong 1913261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi) 1914261eeb9cSDaeho Jeong { 1915261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1916c7b58576SJaegeuk Kim struct task_struct *ckpt_task; 1917261eeb9cSDaeho Jeong 1918c7b58576SJaegeuk Kim if (!cprc->f2fs_issue_ckpt) 1919c7b58576SJaegeuk Kim return; 1920261eeb9cSDaeho Jeong 1921c7b58576SJaegeuk Kim ckpt_task = cprc->f2fs_issue_ckpt; 1922261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1923261eeb9cSDaeho Jeong kthread_stop(ckpt_task); 1924261eeb9cSDaeho Jeong 1925c7b58576SJaegeuk Kim f2fs_flush_ckpt_thread(sbi); 1926261eeb9cSDaeho Jeong } 1927c7b58576SJaegeuk Kim 1928c7b58576SJaegeuk Kim void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi) 1929c7b58576SJaegeuk Kim { 1930c7b58576SJaegeuk Kim struct ckpt_req_control *cprc = &sbi->cprc_info; 1931c7b58576SJaegeuk Kim 1932c7b58576SJaegeuk Kim flush_remained_ckpt_reqs(sbi, NULL); 1933c7b58576SJaegeuk Kim 1934c7b58576SJaegeuk Kim /* Let's wait for the previous dispatched checkpoint. */ 1935c7b58576SJaegeuk Kim while (atomic_read(&cprc->queued_ckpt)) 1936c7b58576SJaegeuk Kim io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1937261eeb9cSDaeho Jeong } 1938261eeb9cSDaeho Jeong 1939261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi) 1940261eeb9cSDaeho Jeong { 1941261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1942261eeb9cSDaeho Jeong 1943261eeb9cSDaeho Jeong atomic_set(&cprc->issued_ckpt, 0); 1944261eeb9cSDaeho Jeong atomic_set(&cprc->total_ckpt, 0); 1945261eeb9cSDaeho Jeong atomic_set(&cprc->queued_ckpt, 0); 1946e6592066SDaeho Jeong cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO; 1947261eeb9cSDaeho Jeong init_waitqueue_head(&cprc->ckpt_wait_queue); 1948261eeb9cSDaeho Jeong init_llist_head(&cprc->issue_list); 1949261eeb9cSDaeho Jeong spin_lock_init(&cprc->stat_lock); 1950261eeb9cSDaeho Jeong } 1951