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))) { 1017735730dSChao Yu f2fs_put_page(page, 1); 1027735730dSChao Yu return ERR_PTR(-EIO); 10381114baaSChao Yu } 104393ff91fSJaegeuk Kim out: 105127e670aSJaegeuk Kim return page; 106127e670aSJaegeuk Kim } 107127e670aSJaegeuk Kim 1084d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 1092b947003SChao Yu { 1102b947003SChao Yu return __get_meta_page(sbi, index, true); 1112b947003SChao Yu } 1122b947003SChao Yu 11386f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index) 1147735730dSChao Yu { 1157735730dSChao Yu struct page *page; 1167735730dSChao Yu int count = 0; 1177735730dSChao Yu 1187735730dSChao Yu retry: 1197735730dSChao Yu page = __get_meta_page(sbi, index, true); 1207735730dSChao Yu if (IS_ERR(page)) { 1217735730dSChao Yu if (PTR_ERR(page) == -EIO && 1227735730dSChao Yu ++count <= DEFAULT_RETRY_IO_COUNT) 1237735730dSChao Yu goto retry; 1247735730dSChao Yu f2fs_stop_checkpoint(sbi, false); 1257735730dSChao Yu } 1267735730dSChao Yu return page; 1277735730dSChao Yu } 1287735730dSChao Yu 1292b947003SChao Yu /* for POR only */ 1304d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 1312b947003SChao Yu { 1322b947003SChao Yu return __get_meta_page(sbi, index, false); 1332b947003SChao Yu } 1342b947003SChao Yu 13593770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr, 13693770ab7SChao Yu int type) 13793770ab7SChao Yu { 13893770ab7SChao Yu struct seg_entry *se; 13993770ab7SChao Yu unsigned int segno, offset; 14093770ab7SChao Yu bool exist; 14193770ab7SChao Yu 14293770ab7SChao Yu if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ) 14393770ab7SChao Yu return true; 14493770ab7SChao Yu 14593770ab7SChao Yu segno = GET_SEGNO(sbi, blkaddr); 14693770ab7SChao Yu offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 14793770ab7SChao Yu se = get_seg_entry(sbi, segno); 14893770ab7SChao Yu 14993770ab7SChao Yu exist = f2fs_test_bit(offset, se->cur_valid_map); 15093770ab7SChao Yu if (!exist && type == DATA_GENERIC_ENHANCE) { 151dcbb4c10SJoe Perches f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", 152dcbb4c10SJoe Perches blkaddr, exist); 15393770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 15493770ab7SChao Yu WARN_ON(1); 15593770ab7SChao Yu } 15693770ab7SChao Yu return exist; 15793770ab7SChao Yu } 15893770ab7SChao Yu 159e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 1604d57b86dSChao Yu block_t blkaddr, int type) 161662befdaSChao Yu { 162662befdaSChao Yu switch (type) { 163662befdaSChao Yu case META_NAT: 16466b00c18SChao Yu break; 165662befdaSChao Yu case META_SIT: 16666b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 16766b00c18SChao Yu return false; 16866b00c18SChao Yu break; 16981c1a0f1SChao Yu case META_SSA: 17066b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 17166b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr)) 17266b00c18SChao Yu return false; 17366b00c18SChao Yu break; 174662befdaSChao Yu case META_CP: 17566b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 17666b00c18SChao Yu blkaddr < __start_cp_addr(sbi))) 17766b00c18SChao Yu return false; 17866b00c18SChao Yu break; 1794c521f49SJaegeuk Kim case META_POR: 18093770ab7SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 18193770ab7SChao Yu blkaddr < MAIN_BLKADDR(sbi))) 18293770ab7SChao Yu return false; 18393770ab7SChao Yu break; 184e1da7872SChao Yu case DATA_GENERIC: 18593770ab7SChao Yu case DATA_GENERIC_ENHANCE: 18693770ab7SChao Yu case DATA_GENERIC_ENHANCE_READ: 18766b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 188c9b60788SChao Yu blkaddr < MAIN_BLKADDR(sbi))) { 189dcbb4c10SJoe Perches f2fs_warn(sbi, "access invalid blkaddr:%u", 190dcbb4c10SJoe Perches blkaddr); 19193770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 192c9b60788SChao Yu WARN_ON(1); 19366b00c18SChao Yu return false; 19493770ab7SChao Yu } else { 19593770ab7SChao Yu return __is_bitmap_valid(sbi, blkaddr, type); 196c9b60788SChao Yu } 19766b00c18SChao Yu break; 198e1da7872SChao Yu case META_GENERIC: 199e1da7872SChao Yu if (unlikely(blkaddr < SEG0_BLKADDR(sbi) || 200e1da7872SChao Yu blkaddr >= MAIN_BLKADDR(sbi))) 201e1da7872SChao Yu return false; 202e1da7872SChao Yu break; 203662befdaSChao Yu default: 204662befdaSChao Yu BUG(); 205662befdaSChao Yu } 20666b00c18SChao Yu 20766b00c18SChao Yu return true; 208662befdaSChao Yu } 209662befdaSChao Yu 210662befdaSChao Yu /* 2117a88ddb5SChao Yu * Readahead CP/NAT/SIT/SSA/POR pages 212662befdaSChao Yu */ 2134d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 21426879fb1SChao Yu int type, bool sync) 215662befdaSChao Yu { 216662befdaSChao Yu struct page *page; 2174c521f49SJaegeuk Kim block_t blkno = start; 218662befdaSChao Yu struct f2fs_io_info fio = { 21905ca3632SJaegeuk Kim .sbi = sbi, 220662befdaSChao Yu .type = META, 22104d328deSMike Christie .op = REQ_OP_READ, 22270fd7614SChristoph Hellwig .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, 2234375a336SJaegeuk Kim .encrypted_page = NULL, 224fb830fc5SChao Yu .in_list = false, 2256dc3a126SChao Yu .is_por = (type == META_POR), 226662befdaSChao Yu }; 227e9f5b8b8SChao Yu struct blk_plug plug; 228ce4c638cSChao Yu int err; 229662befdaSChao Yu 2302b947003SChao Yu if (unlikely(type == META_POR)) 23104d328deSMike Christie fio.op_flags &= ~REQ_META; 2322b947003SChao Yu 233e9f5b8b8SChao Yu blk_start_plug(&plug); 234662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 235662befdaSChao Yu 236e1da7872SChao Yu if (!f2fs_is_valid_blkaddr(sbi, blkno, type)) 23766b00c18SChao Yu goto out; 23866b00c18SChao Yu 239662befdaSChao Yu switch (type) { 240662befdaSChao Yu case META_NAT: 24166b00c18SChao Yu if (unlikely(blkno >= 24266b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 243662befdaSChao Yu blkno = 0; 24466b00c18SChao Yu /* get nat block addr */ 2457a9d7548SChao Yu fio.new_blkaddr = current_nat_addr(sbi, 246662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 247662befdaSChao Yu break; 248662befdaSChao Yu case META_SIT: 2496a257471SChao Yu if (unlikely(blkno >= TOTAL_SEGS(sbi))) 2506a257471SChao Yu goto out; 251662befdaSChao Yu /* get sit block addr */ 2527a9d7548SChao Yu fio.new_blkaddr = current_sit_addr(sbi, 253662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 254662befdaSChao Yu break; 25581c1a0f1SChao Yu case META_SSA: 256662befdaSChao Yu case META_CP: 2574c521f49SJaegeuk Kim case META_POR: 2587a9d7548SChao Yu fio.new_blkaddr = blkno; 259662befdaSChao Yu break; 260662befdaSChao Yu default: 261662befdaSChao Yu BUG(); 262662befdaSChao Yu } 263662befdaSChao Yu 264300e129cSJaegeuk Kim page = f2fs_grab_cache_page(META_MAPPING(sbi), 265300e129cSJaegeuk Kim fio.new_blkaddr, false); 266662befdaSChao Yu if (!page) 267662befdaSChao Yu continue; 268662befdaSChao Yu if (PageUptodate(page)) { 269662befdaSChao Yu f2fs_put_page(page, 1); 270662befdaSChao Yu continue; 271662befdaSChao Yu } 272662befdaSChao Yu 27305ca3632SJaegeuk Kim fio.page = page; 274ce4c638cSChao Yu err = f2fs_submit_page_bio(&fio); 275ce4c638cSChao Yu f2fs_put_page(page, err ? 1 : 0); 2768b83ac81SChao Yu 2778b83ac81SChao Yu if (!err) 2788b83ac81SChao Yu f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE); 279662befdaSChao Yu } 280662befdaSChao Yu out: 281e9f5b8b8SChao Yu blk_finish_plug(&plug); 282662befdaSChao Yu return blkno - start; 283662befdaSChao Yu } 284662befdaSChao Yu 2854d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index) 286635aee1fSChao Yu { 287635aee1fSChao Yu struct page *page; 288635aee1fSChao Yu bool readahead = false; 289635aee1fSChao Yu 290635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 2914da7bf5aSJaegeuk Kim if (!page || !PageUptodate(page)) 292635aee1fSChao Yu readahead = true; 293635aee1fSChao Yu f2fs_put_page(page, 0); 294635aee1fSChao Yu 295635aee1fSChao Yu if (readahead) 296a8affc03SChristoph Hellwig f2fs_ra_meta_pages(sbi, index, BIO_MAX_VECS, META_POR, true); 297635aee1fSChao Yu } 298635aee1fSChao Yu 299b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page, 300b0af6d49SChao Yu struct writeback_control *wbc, 301b0af6d49SChao Yu enum iostat_type io_type) 302127e670aSJaegeuk Kim { 3034081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 304577e3495SJaegeuk Kim 305ecda0de3SChao Yu trace_f2fs_writepage(page, META); 306ecda0de3SChao Yu 307af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 308af697c0fSJaegeuk Kim goto redirty_out; 309caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 310cfb271d4SChao Yu goto redirty_out; 311857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 312cfb271d4SChao Yu goto redirty_out; 313127e670aSJaegeuk Kim 3144d57b86dSChao Yu f2fs_do_write_meta_page(sbi, page, io_type); 315127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 3160c3a5797SChao Yu 3170c3a5797SChao Yu if (wbc->for_reclaim) 318bab475c5SChao Yu f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META); 3190c3a5797SChao Yu 320127e670aSJaegeuk Kim unlock_page(page); 321857dc4e0SJaegeuk Kim 3220c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 323b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, META); 3240c3a5797SChao Yu 325577e3495SJaegeuk Kim return 0; 326cfb271d4SChao Yu 327cfb271d4SChao Yu redirty_out: 32876f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 329cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 330127e670aSJaegeuk Kim } 331127e670aSJaegeuk Kim 332b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page, 333b0af6d49SChao Yu struct writeback_control *wbc) 334b0af6d49SChao Yu { 335b0af6d49SChao Yu return __f2fs_write_meta_page(page, wbc, FS_META_IO); 336b0af6d49SChao Yu } 337b0af6d49SChao Yu 338127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 339127e670aSJaegeuk Kim struct writeback_control *wbc) 340127e670aSJaegeuk Kim { 3414081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 34250c8cdb3SJaegeuk Kim long diff, written; 343127e670aSJaegeuk Kim 3440771fcc7SChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3450771fcc7SChao Yu goto skip_write; 3460771fcc7SChao Yu 3475459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 348812a9597SJaegeuk Kim if (wbc->sync_mode != WB_SYNC_ALL && 349812a9597SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < 350812a9597SJaegeuk Kim nr_pages_to_skip(sbi, META)) 351d3baf95dSJaegeuk Kim goto skip_write; 352127e670aSJaegeuk Kim 353a29d0e0bSYunlei He /* if locked failed, cp will flush dirty pages instead */ 3548769918bSSahitya Tummala if (!down_write_trylock(&sbi->cp_global_sem)) 355a29d0e0bSYunlei He goto skip_write; 356d31c7c3fSYunlei He 357a29d0e0bSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 35850c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 3594d57b86dSChao Yu written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); 3608769918bSSahitya Tummala up_write(&sbi->cp_global_sem); 36150c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 362127e670aSJaegeuk Kim return 0; 363d3baf95dSJaegeuk Kim 364d3baf95dSJaegeuk Kim skip_write: 365d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 366d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 367d3baf95dSJaegeuk Kim return 0; 368127e670aSJaegeuk Kim } 369127e670aSJaegeuk Kim 3704d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 371b0af6d49SChao Yu long nr_to_write, enum iostat_type io_type) 372127e670aSJaegeuk Kim { 3739df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 374028a63a6SJan Kara pgoff_t index = 0, prev = ULONG_MAX; 375127e670aSJaegeuk Kim struct pagevec pvec; 376127e670aSJaegeuk Kim long nwritten = 0; 377028a63a6SJan Kara int nr_pages; 378127e670aSJaegeuk Kim struct writeback_control wbc = { 379127e670aSJaegeuk Kim .for_reclaim = 0, 380127e670aSJaegeuk Kim }; 381e9f5b8b8SChao Yu struct blk_plug plug; 382127e670aSJaegeuk Kim 38386679820SMel Gorman pagevec_init(&pvec); 384127e670aSJaegeuk Kim 385e9f5b8b8SChao Yu blk_start_plug(&plug); 386e9f5b8b8SChao Yu 387028a63a6SJan Kara while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 38867fd707fSJan Kara PAGECACHE_TAG_DIRTY))) { 389028a63a6SJan Kara int i; 390127e670aSJaegeuk Kim 391127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 392127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 393203681f6SJaegeuk Kim 39480dd9c0eSChao Yu if (prev == ULONG_MAX) 3956066d8cdSJaegeuk Kim prev = page->index - 1; 3966066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 3976066d8cdSJaegeuk Kim pagevec_release(&pvec); 3986066d8cdSJaegeuk Kim goto stop; 3996066d8cdSJaegeuk Kim } 4006066d8cdSJaegeuk Kim 401127e670aSJaegeuk Kim lock_page(page); 402203681f6SJaegeuk Kim 403203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 404203681f6SJaegeuk Kim continue_unlock: 405203681f6SJaegeuk Kim unlock_page(page); 406203681f6SJaegeuk Kim continue; 407203681f6SJaegeuk Kim } 408203681f6SJaegeuk Kim if (!PageDirty(page)) { 409203681f6SJaegeuk Kim /* someone wrote it for us */ 410203681f6SJaegeuk Kim goto continue_unlock; 411203681f6SJaegeuk Kim } 412203681f6SJaegeuk Kim 413bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 414fa3d2bdfSJaegeuk Kim 415203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 416203681f6SJaegeuk Kim goto continue_unlock; 417203681f6SJaegeuk Kim 418b0af6d49SChao Yu if (__f2fs_write_meta_page(page, &wbc, io_type)) { 419577e3495SJaegeuk Kim unlock_page(page); 420577e3495SJaegeuk Kim break; 421577e3495SJaegeuk Kim } 422cfb271d4SChao Yu nwritten++; 4236066d8cdSJaegeuk Kim prev = page->index; 424cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 425127e670aSJaegeuk Kim break; 426127e670aSJaegeuk Kim } 427127e670aSJaegeuk Kim pagevec_release(&pvec); 428127e670aSJaegeuk Kim cond_resched(); 429127e670aSJaegeuk Kim } 4306066d8cdSJaegeuk Kim stop: 431127e670aSJaegeuk Kim if (nwritten) 432b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, type); 433127e670aSJaegeuk Kim 434e9f5b8b8SChao Yu blk_finish_plug(&plug); 435e9f5b8b8SChao Yu 436127e670aSJaegeuk Kim return nwritten; 437127e670aSJaegeuk Kim } 438127e670aSJaegeuk Kim 439127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 440127e670aSJaegeuk Kim { 44126c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 44226c6b887SJaegeuk Kim 443237c0790SJaegeuk Kim if (!PageUptodate(page)) 444127e670aSJaegeuk Kim SetPageUptodate(page); 445127e670aSJaegeuk Kim if (!PageDirty(page)) { 446b87078adSJaegeuk Kim __set_page_dirty_nobuffers(page); 4474081363fSJaegeuk Kim inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); 448b763f3beSChao Yu set_page_private_reference(page); 449127e670aSJaegeuk Kim return 1; 450127e670aSJaegeuk Kim } 451127e670aSJaegeuk Kim return 0; 452127e670aSJaegeuk Kim } 453127e670aSJaegeuk Kim 454127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 455127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 456127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 457127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 458487261f3SChao Yu .invalidatepage = f2fs_invalidate_page, 459487261f3SChao Yu .releasepage = f2fs_release_page, 4605b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION 4615b7a487cSWeichao Guo .migratepage = f2fs_migrate_page, 4625b7a487cSWeichao Guo #endif 463127e670aSJaegeuk Kim }; 464127e670aSJaegeuk Kim 46539d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, 46639d787beSChao Yu unsigned int devidx, int type) 467953e6cc6SJaegeuk Kim { 46867298804SChao Yu struct inode_management *im = &sbi->im[type]; 4694b106518SChao Yu struct ino_entry *e = NULL, *new = NULL; 47080c54505SJaegeuk Kim 4714b106518SChao Yu if (type == FLUSH_INO) { 4724b106518SChao Yu rcu_read_lock(); 4734b106518SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 4744b106518SChao Yu rcu_read_unlock(); 4754b106518SChao Yu } 4764b106518SChao Yu 4774b106518SChao Yu retry: 4784b106518SChao Yu if (!e) 47932410577SChao Yu new = f2fs_kmem_cache_alloc(ino_entry_slab, 48032410577SChao Yu GFP_NOFS, true, NULL); 48119526d74SChao Yu 48280c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 483769ec6e5SJaegeuk Kim 48467298804SChao Yu spin_lock(&im->ino_lock); 48567298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 48639efac41SJaegeuk Kim if (!e) { 4874b106518SChao Yu if (!new) { 4884b106518SChao Yu spin_unlock(&im->ino_lock); 4894b106518SChao Yu goto retry; 4904b106518SChao Yu } 4914b106518SChao Yu e = new; 49219526d74SChao Yu if (unlikely(radix_tree_insert(&im->ino_root, ino, e))) 49319526d74SChao Yu f2fs_bug_on(sbi, 1); 49419526d74SChao Yu 49539efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 49639efac41SJaegeuk Kim e->ino = ino; 49739efac41SJaegeuk Kim 49867298804SChao Yu list_add_tail(&e->list, &im->ino_list); 4998c402946SJaegeuk Kim if (type != ORPHAN_INO) 50067298804SChao Yu im->ino_num++; 50139efac41SJaegeuk Kim } 50239d787beSChao Yu 50339d787beSChao Yu if (type == FLUSH_INO) 50439d787beSChao Yu f2fs_set_bit(devidx, (char *)&e->dirty_device); 50539d787beSChao Yu 50667298804SChao Yu spin_unlock(&im->ino_lock); 507769ec6e5SJaegeuk Kim radix_tree_preload_end(); 50880c54505SJaegeuk Kim 5094b106518SChao Yu if (new && e != new) 5104b106518SChao Yu kmem_cache_free(ino_entry_slab, new); 511953e6cc6SJaegeuk Kim } 512953e6cc6SJaegeuk Kim 5136451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 514953e6cc6SJaegeuk Kim { 51567298804SChao Yu struct inode_management *im = &sbi->im[type]; 5166451e041SJaegeuk Kim struct ino_entry *e; 517953e6cc6SJaegeuk Kim 51867298804SChao Yu spin_lock(&im->ino_lock); 51967298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 52039efac41SJaegeuk Kim if (e) { 521953e6cc6SJaegeuk Kim list_del(&e->list); 52267298804SChao Yu radix_tree_delete(&im->ino_root, ino); 52367298804SChao Yu im->ino_num--; 52467298804SChao Yu spin_unlock(&im->ino_lock); 5256451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 526953e6cc6SJaegeuk Kim return; 527953e6cc6SJaegeuk Kim } 52867298804SChao Yu spin_unlock(&im->ino_lock); 529953e6cc6SJaegeuk Kim } 530953e6cc6SJaegeuk Kim 5314d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 532fff04f90SJaegeuk Kim { 533fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 53439d787beSChao Yu __add_ino_entry(sbi, ino, 0, type); 535fff04f90SJaegeuk Kim } 536fff04f90SJaegeuk Kim 5374d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 538fff04f90SJaegeuk Kim { 539fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 540fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 541fff04f90SJaegeuk Kim } 542fff04f90SJaegeuk Kim 5431f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */ 5444d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 545fff04f90SJaegeuk Kim { 54667298804SChao Yu struct inode_management *im = &sbi->im[mode]; 547fff04f90SJaegeuk Kim struct ino_entry *e; 54867298804SChao Yu 54967298804SChao Yu spin_lock(&im->ino_lock); 55067298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 55167298804SChao Yu spin_unlock(&im->ino_lock); 552fff04f90SJaegeuk Kim return e ? true : false; 553fff04f90SJaegeuk Kim } 554fff04f90SJaegeuk Kim 5554d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all) 556fff04f90SJaegeuk Kim { 557fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 558fff04f90SJaegeuk Kim int i; 559fff04f90SJaegeuk Kim 56039d787beSChao Yu for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) { 56167298804SChao Yu struct inode_management *im = &sbi->im[i]; 56267298804SChao Yu 56367298804SChao Yu spin_lock(&im->ino_lock); 56467298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 565fff04f90SJaegeuk Kim list_del(&e->list); 56667298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 567fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 56867298804SChao Yu im->ino_num--; 569fff04f90SJaegeuk Kim } 57067298804SChao Yu spin_unlock(&im->ino_lock); 571fff04f90SJaegeuk Kim } 572fff04f90SJaegeuk Kim } 573fff04f90SJaegeuk Kim 5744d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 57539d787beSChao Yu unsigned int devidx, int type) 57639d787beSChao Yu { 57739d787beSChao Yu __add_ino_entry(sbi, ino, devidx, type); 57839d787beSChao Yu } 57939d787beSChao Yu 5804d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 58139d787beSChao Yu unsigned int devidx, int type) 58239d787beSChao Yu { 58339d787beSChao Yu struct inode_management *im = &sbi->im[type]; 58439d787beSChao Yu struct ino_entry *e; 58539d787beSChao Yu bool is_dirty = false; 58639d787beSChao Yu 58739d787beSChao Yu spin_lock(&im->ino_lock); 58839d787beSChao Yu e = radix_tree_lookup(&im->ino_root, ino); 58939d787beSChao Yu if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device)) 59039d787beSChao Yu is_dirty = true; 59139d787beSChao Yu spin_unlock(&im->ino_lock); 59239d787beSChao Yu return is_dirty; 59339d787beSChao Yu } 59439d787beSChao Yu 5954d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi) 596127e670aSJaegeuk Kim { 59767298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 598127e670aSJaegeuk Kim int err = 0; 599127e670aSJaegeuk Kim 60067298804SChao Yu spin_lock(&im->ino_lock); 601cb78942bSJaegeuk Kim 6021ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) { 603cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock); 604c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_ORPHAN); 605cb78942bSJaegeuk Kim return -ENOSPC; 606cb78942bSJaegeuk Kim } 6077fa750a1SArnd Bergmann 60867298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 609127e670aSJaegeuk Kim err = -ENOSPC; 610cbd56e7dSJaegeuk Kim else 61167298804SChao Yu im->ino_num++; 61267298804SChao Yu spin_unlock(&im->ino_lock); 6130d47c1adSGu Zheng 614127e670aSJaegeuk Kim return err; 615127e670aSJaegeuk Kim } 616127e670aSJaegeuk Kim 6174d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi) 618cbd56e7dSJaegeuk Kim { 61967298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 62067298804SChao Yu 62167298804SChao Yu spin_lock(&im->ino_lock); 62267298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 62367298804SChao Yu im->ino_num--; 62467298804SChao Yu spin_unlock(&im->ino_lock); 625cbd56e7dSJaegeuk Kim } 626cbd56e7dSJaegeuk Kim 6274d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode) 628127e670aSJaegeuk Kim { 62939efac41SJaegeuk Kim /* add new orphan ino entry into list */ 63039d787beSChao Yu __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO); 6314d57b86dSChao Yu f2fs_update_inode_page(inode); 632127e670aSJaegeuk Kim } 633127e670aSJaegeuk Kim 6344d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 635127e670aSJaegeuk Kim { 636953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 6376451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 638127e670aSJaegeuk Kim } 639127e670aSJaegeuk Kim 6408c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 641127e670aSJaegeuk Kim { 6428c14bfadSChao Yu struct inode *inode; 6435905f9afSJaegeuk Kim struct node_info ni; 64476a45e3cSChao Yu int err; 6458c14bfadSChao Yu 6465905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino); 6478c14bfadSChao Yu if (IS_ERR(inode)) { 6488c14bfadSChao Yu /* 6498c14bfadSChao Yu * there should be a bug that we can't find the entry 6508c14bfadSChao Yu * to orphan inode. 6518c14bfadSChao Yu */ 6528c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 6538c14bfadSChao Yu return PTR_ERR(inode); 6548c14bfadSChao Yu } 6558c14bfadSChao Yu 65610a26878SChao Yu err = f2fs_dquot_initialize(inode); 657a515d12fSSheng Yong if (err) { 658a515d12fSSheng Yong iput(inode); 6590f9ec2a8SJaegeuk Kim goto err_out; 660a515d12fSSheng Yong } 6610f9ec2a8SJaegeuk Kim 662127e670aSJaegeuk Kim clear_nlink(inode); 663127e670aSJaegeuk Kim 664127e670aSJaegeuk Kim /* truncate all the data during iput */ 665127e670aSJaegeuk Kim iput(inode); 6665905f9afSJaegeuk Kim 667a9419b63SJaegeuk Kim err = f2fs_get_node_info(sbi, ino, &ni, false); 6687735730dSChao Yu if (err) 6697735730dSChao Yu goto err_out; 6705905f9afSJaegeuk Kim 6715905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */ 6725905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) { 6730f9ec2a8SJaegeuk Kim err = -EIO; 6740f9ec2a8SJaegeuk Kim goto err_out; 6755905f9afSJaegeuk Kim } 6768c14bfadSChao Yu return 0; 6770f9ec2a8SJaegeuk Kim 6780f9ec2a8SJaegeuk Kim err_out: 6790f9ec2a8SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 680dcbb4c10SJoe Perches f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.", 6810f9ec2a8SJaegeuk Kim __func__, ino); 6820f9ec2a8SJaegeuk Kim return err; 683127e670aSJaegeuk Kim } 684127e670aSJaegeuk Kim 6854d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) 686127e670aSJaegeuk Kim { 6873c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 6884b2414d0SChao Yu unsigned int s_flags = sbi->sb->s_flags; 6894b2414d0SChao Yu int err = 0; 690ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA 691ea676733SJaegeuk Kim int quota_enabled; 692ea676733SJaegeuk Kim #endif 693127e670aSJaegeuk Kim 694aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 6958c14bfadSChao Yu return 0; 696127e670aSJaegeuk Kim 697b61af314SChao Yu if (bdev_read_only(sbi->sb->s_bdev)) { 698dcbb4c10SJoe Perches f2fs_info(sbi, "write access unavailable, skipping orphan cleanup"); 699b61af314SChao Yu return 0; 700b61af314SChao Yu } 701b61af314SChao Yu 7021751e8a6SLinus Torvalds if (s_flags & SB_RDONLY) { 703dcbb4c10SJoe Perches f2fs_info(sbi, "orphan cleanup on readonly fs"); 7041751e8a6SLinus Torvalds sbi->sb->s_flags &= ~SB_RDONLY; 7054b2414d0SChao Yu } 7064b2414d0SChao Yu 7074b2414d0SChao Yu #ifdef CONFIG_QUOTA 70876cf05d7SSheng Yong /* 70976cf05d7SSheng Yong * Turn on quotas which were not enabled for read-only mounts if 71076cf05d7SSheng Yong * filesystem has quota feature, so that they are updated correctly. 71176cf05d7SSheng Yong */ 7121751e8a6SLinus Torvalds quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY); 7134b2414d0SChao Yu #endif 7144b2414d0SChao Yu 71555141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 7163c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 717127e670aSJaegeuk Kim 7184d57b86dSChao Yu f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 719662befdaSChao Yu 7203c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 7217735730dSChao Yu struct page *page; 722127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 723127e670aSJaegeuk Kim 7247735730dSChao Yu page = f2fs_get_meta_page(sbi, start_blk + i); 7257735730dSChao Yu if (IS_ERR(page)) { 7267735730dSChao Yu err = PTR_ERR(page); 7277735730dSChao Yu goto out; 7287735730dSChao Yu } 7297735730dSChao Yu 730127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 731127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 732127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 7335f029c04SYi Zhuang 7348c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 7358c14bfadSChao Yu if (err) { 7368c14bfadSChao Yu f2fs_put_page(page, 1); 7374b2414d0SChao Yu goto out; 7388c14bfadSChao Yu } 739127e670aSJaegeuk Kim } 740127e670aSJaegeuk Kim f2fs_put_page(page, 1); 741127e670aSJaegeuk Kim } 742127e670aSJaegeuk Kim /* clear Orphan Flag */ 743aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 7444b2414d0SChao Yu out: 7451378752bSChao Yu set_sbi_flag(sbi, SBI_IS_RECOVERED); 7461378752bSChao Yu 7474b2414d0SChao Yu #ifdef CONFIG_QUOTA 7484b2414d0SChao Yu /* Turn quotas off */ 749ea676733SJaegeuk Kim if (quota_enabled) 7504b2414d0SChao Yu f2fs_quota_off_umount(sbi->sb); 7514b2414d0SChao Yu #endif 7521751e8a6SLinus Torvalds sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 7534b2414d0SChao Yu 7544b2414d0SChao Yu return err; 755127e670aSJaegeuk Kim } 756127e670aSJaegeuk Kim 757127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 758127e670aSJaegeuk Kim { 759502c6e0bSGu Zheng struct list_head *head; 760127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 761127e670aSJaegeuk Kim unsigned int nentries = 0; 762bd936f84SChao Yu unsigned short index = 1; 7638c402946SJaegeuk Kim unsigned short orphan_blocks; 7644531929eSGu Zheng struct page *page = NULL; 7656451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 76667298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 767127e670aSJaegeuk Kim 76867298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 7698c402946SJaegeuk Kim 770d6c67a4fSJaegeuk Kim /* 771d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 772d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 773d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 774d6c67a4fSJaegeuk Kim */ 77567298804SChao Yu head = &im->ino_list; 776127e670aSJaegeuk Kim 777127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 778502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 779502c6e0bSGu Zheng if (!page) { 7804d57b86dSChao Yu page = f2fs_grab_meta_page(sbi, start_blk++); 781502c6e0bSGu Zheng orphan_blk = 782502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 783502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 784502c6e0bSGu Zheng } 785127e670aSJaegeuk Kim 78636795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 787127e670aSJaegeuk Kim 78836795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 789127e670aSJaegeuk Kim /* 790127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 791127e670aSJaegeuk Kim * then we need to flush current orphan blocks 792127e670aSJaegeuk Kim * and bring another one in memory 793127e670aSJaegeuk Kim */ 794127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 795127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 796127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 797127e670aSJaegeuk Kim set_page_dirty(page); 798127e670aSJaegeuk Kim f2fs_put_page(page, 1); 799127e670aSJaegeuk Kim index++; 800127e670aSJaegeuk Kim nentries = 0; 801127e670aSJaegeuk Kim page = NULL; 802127e670aSJaegeuk Kim } 803127e670aSJaegeuk Kim } 804127e670aSJaegeuk Kim 805502c6e0bSGu Zheng if (page) { 806127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 807127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 808127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 809127e670aSJaegeuk Kim set_page_dirty(page); 810127e670aSJaegeuk Kim f2fs_put_page(page, 1); 811502c6e0bSGu Zheng } 812127e670aSJaegeuk Kim } 813127e670aSJaegeuk Kim 814d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi, 815d7eb8f1cSChao Yu struct f2fs_checkpoint *ckpt) 816d7eb8f1cSChao Yu { 817d7eb8f1cSChao Yu unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset); 818d7eb8f1cSChao Yu __u32 chksum; 819d7eb8f1cSChao Yu 820d7eb8f1cSChao Yu chksum = f2fs_crc32(sbi, ckpt, chksum_ofs); 821d7eb8f1cSChao Yu if (chksum_ofs < CP_CHKSUM_OFFSET) { 822d7eb8f1cSChao Yu chksum_ofs += sizeof(chksum); 823d7eb8f1cSChao Yu chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs, 824d7eb8f1cSChao Yu F2FS_BLKSIZE - chksum_ofs); 825d7eb8f1cSChao Yu } 826d7eb8f1cSChao Yu return chksum; 827d7eb8f1cSChao Yu } 828d7eb8f1cSChao Yu 829fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 830fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page, 831fc0065adSTiezhu Yang unsigned long long *version) 832fc0065adSTiezhu Yang { 833fc0065adSTiezhu Yang size_t crc_offset = 0; 834d7eb8f1cSChao Yu __u32 crc; 835fc0065adSTiezhu Yang 8364d57b86dSChao Yu *cp_page = f2fs_get_meta_page(sbi, cp_addr); 8377735730dSChao Yu if (IS_ERR(*cp_page)) 8387735730dSChao Yu return PTR_ERR(*cp_page); 8397735730dSChao Yu 840fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 841fc0065adSTiezhu Yang 842fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 843d7eb8f1cSChao Yu if (crc_offset < CP_MIN_CHKSUM_OFFSET || 844d7eb8f1cSChao Yu crc_offset > CP_CHKSUM_OFFSET) { 845d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 846dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset); 847fc0065adSTiezhu Yang return -EINVAL; 848fc0065adSTiezhu Yang } 849fc0065adSTiezhu Yang 850d7eb8f1cSChao Yu crc = f2fs_checkpoint_chksum(sbi, *cp_block); 851d7eb8f1cSChao Yu if (crc != cur_cp_crc(*cp_block)) { 852d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 853dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc value"); 854fc0065adSTiezhu Yang return -EINVAL; 855fc0065adSTiezhu Yang } 856fc0065adSTiezhu Yang 857fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block); 858fc0065adSTiezhu Yang return 0; 859fc0065adSTiezhu Yang } 860fc0065adSTiezhu Yang 861127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 862127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 863127e670aSJaegeuk Kim { 864fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 865fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL; 866127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 867fc0065adSTiezhu Yang int err; 868127e670aSJaegeuk Kim 869fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 870fc0065adSTiezhu Yang &cp_page_1, version); 871fc0065adSTiezhu Yang if (err) 872d3f07c04SChao Yu return NULL; 873c9b60788SChao Yu 874c9b60788SChao Yu if (le32_to_cpu(cp_block->cp_pack_total_block_count) > 875c9b60788SChao Yu sbi->blocks_per_seg) { 876dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", 877c9b60788SChao Yu le32_to_cpu(cp_block->cp_pack_total_block_count)); 878d3f07c04SChao Yu goto invalid_cp; 879c9b60788SChao Yu } 880fc0065adSTiezhu Yang pre_version = *version; 881127e670aSJaegeuk Kim 88225ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 883fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 884fc0065adSTiezhu Yang &cp_page_2, version); 885fc0065adSTiezhu Yang if (err) 886d3f07c04SChao Yu goto invalid_cp; 887fc0065adSTiezhu Yang cur_version = *version; 888127e670aSJaegeuk Kim 889127e670aSJaegeuk Kim if (cur_version == pre_version) { 890127e670aSJaegeuk Kim *version = cur_version; 891127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 892127e670aSJaegeuk Kim return cp_page_1; 893127e670aSJaegeuk Kim } 894127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 895d3f07c04SChao Yu invalid_cp: 896127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 897127e670aSJaegeuk Kim return NULL; 898127e670aSJaegeuk Kim } 899127e670aSJaegeuk Kim 9004d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi) 901127e670aSJaegeuk Kim { 902127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 903127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 904127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 905127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 906127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 907127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 90855141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 9091dbe4152SChangman Lee block_t cp_blk_no; 9101dbe4152SChangman Lee int i; 91110f966bbSChao Yu int err; 912127e670aSJaegeuk Kim 9130b6d4ca0SEric Biggers sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks), 914026f0507SKees Cook GFP_KERNEL); 915127e670aSJaegeuk Kim if (!sbi->ckpt) 916127e670aSJaegeuk Kim return -ENOMEM; 917127e670aSJaegeuk Kim /* 918127e670aSJaegeuk Kim * Finding out valid cp block involves read both 919127e670aSJaegeuk Kim * sets( cp pack 1 and cp pack 2) 920127e670aSJaegeuk Kim */ 921127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 922127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 923127e670aSJaegeuk Kim 924127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 925f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 926f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 927127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 928127e670aSJaegeuk Kim 929127e670aSJaegeuk Kim if (cp1 && cp2) { 930127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 931127e670aSJaegeuk Kim cur_page = cp2; 932127e670aSJaegeuk Kim else 933127e670aSJaegeuk Kim cur_page = cp1; 934127e670aSJaegeuk Kim } else if (cp1) { 935127e670aSJaegeuk Kim cur_page = cp1; 936127e670aSJaegeuk Kim } else if (cp2) { 937127e670aSJaegeuk Kim cur_page = cp2; 938127e670aSJaegeuk Kim } else { 93910f966bbSChao Yu err = -EFSCORRUPTED; 940127e670aSJaegeuk Kim goto fail_no_cp; 941127e670aSJaegeuk Kim } 942127e670aSJaegeuk Kim 943127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 944127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 945127e670aSJaegeuk Kim 9468508e44aSJaegeuk Kim if (cur_page == cp1) 9478508e44aSJaegeuk Kim sbi->cur_cp_pack = 1; 9488508e44aSJaegeuk Kim else 9498508e44aSJaegeuk Kim sbi->cur_cp_pack = 2; 950984ec63cSShawn Lin 951e494c2f9SChao Yu /* Sanity checking of checkpoint */ 95210f966bbSChao Yu if (f2fs_sanity_check_ckpt(sbi)) { 95310f966bbSChao Yu err = -EFSCORRUPTED; 954e494c2f9SChao Yu goto free_fail_no_cp; 95510f966bbSChao Yu } 956e494c2f9SChao Yu 9571dbe4152SChangman Lee if (cp_blks <= 1) 9581dbe4152SChangman Lee goto done; 9591dbe4152SChangman Lee 9601dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 9611dbe4152SChangman Lee if (cur_page == cp2) 9621dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 9631dbe4152SChangman Lee 9641dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 9651dbe4152SChangman Lee void *sit_bitmap_ptr; 9661dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 9671dbe4152SChangman Lee 9684d57b86dSChao Yu cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i); 96910f966bbSChao Yu if (IS_ERR(cur_page)) { 97010f966bbSChao Yu err = PTR_ERR(cur_page); 9717735730dSChao Yu goto free_fail_no_cp; 97210f966bbSChao Yu } 9731dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 9741dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 9751dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 9761dbe4152SChangman Lee } 9771dbe4152SChangman Lee done: 978127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 979127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 980127e670aSJaegeuk Kim return 0; 981127e670aSJaegeuk Kim 982a2125ff7SJaegeuk Kim free_fail_no_cp: 983a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1); 984a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1); 985127e670aSJaegeuk Kim fail_no_cp: 9865222595dSJaegeuk Kim kvfree(sbi->ckpt); 98710f966bbSChao Yu return err; 988127e670aSJaegeuk Kim } 989127e670aSJaegeuk Kim 990c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 991127e670aSJaegeuk Kim { 9924081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 993c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 9942d7b822aSChao Yu 99591942321SJaegeuk Kim if (is_inode_flag_set(inode, flag)) 9962710fd7eSChao Yu return; 9972d7b822aSChao Yu 99891942321SJaegeuk Kim set_inode_flag(inode, flag); 99999f4b917SChao Yu if (!f2fs_is_volatile_file(inode)) 100099f4b917SChao Yu list_add_tail(&F2FS_I(inode)->dirty_list, 100199f4b917SChao Yu &sbi->inode_list[type]); 100233fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 10035deb8267SJaegeuk Kim } 10045deb8267SJaegeuk Kim 1005c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 10066ad7609aSChao Yu { 1007c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 10086ad7609aSChao Yu 100991942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 10106ad7609aSChao Yu return; 10116ad7609aSChao Yu 101291942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list); 101391942321SJaegeuk Kim clear_inode_flag(inode, flag); 101433fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 10156ad7609aSChao Yu } 10166ad7609aSChao Yu 10174d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page) 10185deb8267SJaegeuk Kim { 10194081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1020c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 10215deb8267SJaegeuk Kim 10225ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 10235ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 1024127e670aSJaegeuk Kim return; 10257bd59381SGu Zheng 1026c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 10271c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 1028c227f912SChao Yu __add_dirty_inode(inode, type); 1029b951a4ecSYunlei He inode_inc_dirty_pages(inode); 10301c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]); 10311c4bf763SJaegeuk Kim 1032b763f3beSChao Yu set_page_private_reference(page); 10335deb8267SJaegeuk Kim } 1034127e670aSJaegeuk Kim 10354d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode) 1036127e670aSJaegeuk 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; 1039127e670aSJaegeuk Kim 1040c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 1041c227f912SChao Yu !S_ISLNK(inode->i_mode)) 1042127e670aSJaegeuk Kim return; 1043127e670aSJaegeuk Kim 104410aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 104510aa97c3SJaegeuk Kim return; 104610aa97c3SJaegeuk Kim 1047c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1048c227f912SChao Yu __remove_dirty_inode(inode, type); 1049c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1050afc3eda2SJaegeuk Kim } 105174d0b917SJaegeuk Kim 10524d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 1053127e670aSJaegeuk Kim { 1054ce3b7d80SGu Zheng struct list_head *head; 1055127e670aSJaegeuk Kim struct inode *inode; 10562710fd7eSChao Yu struct f2fs_inode_info *fi; 10574cf18537SChao Yu bool is_dir = (type == DIR_INODE); 10584db08d01SJaegeuk Kim unsigned long ino = 0; 10594cf18537SChao Yu 10604cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 10614cf18537SChao Yu get_pages(sbi, is_dir ? 10624cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1063127e670aSJaegeuk Kim retry: 10649b664822SZhang Qilong if (unlikely(f2fs_cp_error(sbi))) { 10659b664822SZhang Qilong trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10669b664822SZhang Qilong get_pages(sbi, is_dir ? 10679b664822SZhang Qilong F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10686d5a1495SChao Yu return -EIO; 10699b664822SZhang Qilong } 1070af41d3eeSJaegeuk Kim 1071c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1072ce3b7d80SGu Zheng 1073c227f912SChao Yu head = &sbi->inode_list[type]; 1074127e670aSJaegeuk Kim if (list_empty(head)) { 1075c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 10764cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10774cf18537SChao Yu get_pages(sbi, is_dir ? 10784cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10796d5a1495SChao Yu return 0; 1080127e670aSJaegeuk Kim } 1081939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 10822710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 1083c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1084127e670aSJaegeuk Kim if (inode) { 10854db08d01SJaegeuk Kim unsigned long cur_ino = inode->i_ino; 10864db08d01SJaegeuk Kim 1087b0af6d49SChao Yu F2FS_I(inode)->cp_task = current; 1088b0af6d49SChao Yu 108987d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 1090b0af6d49SChao Yu 1091b0af6d49SChao Yu F2FS_I(inode)->cp_task = NULL; 1092b0af6d49SChao Yu 1093127e670aSJaegeuk Kim iput(inode); 10944db08d01SJaegeuk Kim /* We need to give cpu to another writers. */ 10952a63531aSYunlei He if (ino == cur_ino) 10964db08d01SJaegeuk Kim cond_resched(); 10972a63531aSYunlei He else 10984db08d01SJaegeuk Kim ino = cur_ino; 1099127e670aSJaegeuk Kim } else { 1100127e670aSJaegeuk Kim /* 1101127e670aSJaegeuk Kim * We should submit bio, since it exists several 1102127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 1103127e670aSJaegeuk Kim */ 1104b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 11057ecebe5eSSebastian Andrzej Siewior cond_resched(); 1106127e670aSJaegeuk Kim } 1107127e670aSJaegeuk Kim goto retry; 1108127e670aSJaegeuk Kim } 1109127e670aSJaegeuk Kim 11100f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 11110f18b462SJaegeuk Kim { 11120f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META]; 11130f18b462SJaegeuk Kim struct inode *inode; 11140f18b462SJaegeuk Kim struct f2fs_inode_info *fi; 11150f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 11160f18b462SJaegeuk Kim 11170f18b462SJaegeuk Kim while (total--) { 11180f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 11190f18b462SJaegeuk Kim return -EIO; 11200f18b462SJaegeuk Kim 11210f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 11220f18b462SJaegeuk Kim if (list_empty(head)) { 11230f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11240f18b462SJaegeuk Kim return 0; 11250f18b462SJaegeuk Kim } 1126939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, 11270f18b462SJaegeuk Kim gdirty_list); 11280f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode); 11290f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11300f18b462SJaegeuk Kim if (inode) { 113118340edcSJaegeuk Kim sync_inode_metadata(inode, 0); 113218340edcSJaegeuk Kim 113318340edcSJaegeuk Kim /* it's on eviction */ 113418340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 11354d57b86dSChao Yu f2fs_update_inode_page(inode); 11360f18b462SJaegeuk Kim iput(inode); 11370f18b462SJaegeuk Kim } 1138dee668c1SChao Yu } 11390f18b462SJaegeuk Kim return 0; 11400f18b462SJaegeuk Kim } 11410f18b462SJaegeuk Kim 114259c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi) 114359c9081bSYunlei He { 114459c9081bSYunlei He struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 114559c9081bSYunlei He struct f2fs_nm_info *nm_i = NM_I(sbi); 114659c9081bSYunlei He nid_t last_nid = nm_i->next_scan_nid; 114759c9081bSYunlei He 114859c9081bSYunlei He next_free_nid(sbi, &last_nid); 114959c9081bSYunlei He ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 115059c9081bSYunlei He ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 115159c9081bSYunlei He ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 115259c9081bSYunlei He ckpt->next_free_nid = cpu_to_le32(last_nid); 115359c9081bSYunlei He } 115459c9081bSYunlei He 1155af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi) 1156af033b2aSChao Yu { 1157db6ec53bSJaegeuk Kim bool ret = false; 1158db6ec53bSJaegeuk Kim 1159af033b2aSChao Yu if (!is_journalled_quota(sbi)) 1160af033b2aSChao Yu return false; 1161db6ec53bSJaegeuk Kim 1162a5c00422SChao Yu if (!down_write_trylock(&sbi->quota_sem)) 1163a5c00422SChao Yu return true; 1164db6ec53bSJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { 1165db6ec53bSJaegeuk Kim ret = false; 1166db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { 1167db6ec53bSJaegeuk Kim ret = false; 1168db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) { 1169db6ec53bSJaegeuk Kim clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1170db6ec53bSJaegeuk Kim ret = true; 1171db6ec53bSJaegeuk Kim } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { 1172db6ec53bSJaegeuk Kim ret = true; 1173db6ec53bSJaegeuk Kim } 1174db6ec53bSJaegeuk Kim up_write(&sbi->quota_sem); 1175db6ec53bSJaegeuk Kim return ret; 1176af033b2aSChao Yu } 1177af033b2aSChao Yu 11780a8165d7SJaegeuk Kim /* 1179127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 1180127e670aSJaegeuk Kim */ 1181cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 1182127e670aSJaegeuk Kim { 1183127e670aSJaegeuk Kim struct writeback_control wbc = { 1184127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 1185127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 1186127e670aSJaegeuk Kim .for_reclaim = 0, 1187127e670aSJaegeuk Kim }; 1188af033b2aSChao Yu int err = 0, cnt = 0; 1189c718379bSJaegeuk Kim 119034c061adSSayali Lokhande /* 119134c061adSSayali Lokhande * Let's flush inline_data in dirty node pages. 119234c061adSSayali Lokhande */ 119334c061adSSayali Lokhande f2fs_flush_inline_data(sbi); 119434c061adSSayali Lokhande 1195af033b2aSChao Yu retry_flush_quotas: 1196db6ec53bSJaegeuk Kim f2fs_lock_all(sbi); 1197af033b2aSChao Yu if (__need_flush_quota(sbi)) { 1198af033b2aSChao Yu int locked; 1199af033b2aSChao Yu 1200af033b2aSChao Yu if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { 1201af033b2aSChao Yu set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1202db6ec53bSJaegeuk Kim set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1203af033b2aSChao Yu goto retry_flush_dents; 1204af033b2aSChao Yu } 1205db6ec53bSJaegeuk Kim f2fs_unlock_all(sbi); 1206af033b2aSChao Yu 1207af033b2aSChao Yu /* only failed during mount/umount/freeze/quotactl */ 1208af033b2aSChao Yu locked = down_read_trylock(&sbi->sb->s_umount); 1209af033b2aSChao Yu f2fs_quota_sync(sbi->sb, -1); 1210af033b2aSChao Yu if (locked) 1211af033b2aSChao Yu up_read(&sbi->sb->s_umount); 1212af033b2aSChao Yu cond_resched(); 1213af033b2aSChao Yu goto retry_flush_quotas; 1214af033b2aSChao Yu } 1215af033b2aSChao Yu 1216af033b2aSChao Yu retry_flush_dents: 1217127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 1218127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 1219e479556bSGu Zheng f2fs_unlock_all(sbi); 12204d57b86dSChao Yu err = f2fs_sync_dirty_inodes(sbi, DIR_INODE); 12216d5a1495SChao Yu if (err) 12221f5f11a3SJaegeuk Kim return err; 122330973883SJaegeuk Kim cond_resched(); 1224af033b2aSChao Yu goto retry_flush_quotas; 1225127e670aSJaegeuk Kim } 1226127e670aSJaegeuk Kim 122759c9081bSYunlei He /* 122859c9081bSYunlei He * POR: we should ensure that there are no dirty node pages 122959c9081bSYunlei He * until finishing nat/sit flush. inode->i_blocks can be updated. 123059c9081bSYunlei He */ 123159c9081bSYunlei He down_write(&sbi->node_change); 123259c9081bSYunlei He 12330f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 123459c9081bSYunlei He up_write(&sbi->node_change); 12350f18b462SJaegeuk Kim f2fs_unlock_all(sbi); 12360f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi); 12370f18b462SJaegeuk Kim if (err) 12381f5f11a3SJaegeuk Kim return err; 123930973883SJaegeuk Kim cond_resched(); 1240af033b2aSChao Yu goto retry_flush_quotas; 12410f18b462SJaegeuk Kim } 12420f18b462SJaegeuk Kim 124339936837SJaegeuk Kim retry_flush_nodes: 1244b3582c68SChao Yu down_write(&sbi->node_write); 1245127e670aSJaegeuk Kim 1246127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1247b3582c68SChao Yu up_write(&sbi->node_write); 1248c29fd0c0SChao Yu atomic_inc(&sbi->wb_sync_req[NODE]); 12494d57b86dSChao Yu err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); 1250c29fd0c0SChao Yu atomic_dec(&sbi->wb_sync_req[NODE]); 12516d5a1495SChao Yu if (err) { 125259c9081bSYunlei He up_write(&sbi->node_change); 1253cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 12541f5f11a3SJaegeuk Kim return err; 1255cf779cabSJaegeuk Kim } 125630973883SJaegeuk Kim cond_resched(); 125739936837SJaegeuk Kim goto retry_flush_nodes; 1258127e670aSJaegeuk Kim } 125959c9081bSYunlei He 126059c9081bSYunlei He /* 126159c9081bSYunlei He * sbi->node_change is used only for AIO write_begin path which produces 126259c9081bSYunlei He * dirty node blocks and some checkpoint values by block allocation. 126359c9081bSYunlei He */ 126459c9081bSYunlei He __prepare_cp_block(sbi); 126559c9081bSYunlei He up_write(&sbi->node_change); 1266cf779cabSJaegeuk Kim return err; 1267127e670aSJaegeuk Kim } 1268127e670aSJaegeuk Kim 1269127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 1270127e670aSJaegeuk Kim { 1271b3582c68SChao Yu up_write(&sbi->node_write); 1272e479556bSGu Zheng f2fs_unlock_all(sbi); 1273127e670aSJaegeuk Kim } 1274127e670aSJaegeuk Kim 1275bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type) 1276fb51b5efSChangman Lee { 1277fb51b5efSChangman Lee DEFINE_WAIT(wait); 1278fb51b5efSChangman Lee 1279fb51b5efSChangman Lee for (;;) { 1280bf22c3ccSSahitya Tummala if (!get_pages(sbi, type)) 1281fb51b5efSChangman Lee break; 1282fb51b5efSChangman Lee 1283af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1284af697c0fSJaegeuk Kim break; 1285af697c0fSJaegeuk Kim 12869c30df7cSJaegeuk Kim if (type == F2FS_DIRTY_META) 12879c30df7cSJaegeuk Kim f2fs_sync_meta_pages(sbi, META, LONG_MAX, 12889c30df7cSJaegeuk Kim FS_CP_META_IO); 12891fd28018SJaegeuk Kim else if (type == F2FS_WB_CP_DATA) 12901fd28018SJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 1291828add77SJaegeuk Kim 1292828add77SJaegeuk Kim prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 12935df7731fSChao Yu io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1294fb51b5efSChangman Lee } 1295fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 1296fb51b5efSChangman Lee } 1297fb51b5efSChangman Lee 1298e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1299e4c5d848SJaegeuk Kim { 1300e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1301e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1302d1aa2453SChao Yu unsigned long flags; 1303e4c5d848SJaegeuk Kim 130494c821fbSChao Yu if (cpc->reason & CP_UMOUNT) { 1305*b702c83eSChao Yu if (le32_to_cpu(ckpt->cp_pack_total_block_count) + 1306*b702c83eSChao Yu NM_I(sbi)->nat_bits_blocks > sbi->blocks_per_seg) { 130794c821fbSChao Yu clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 130894c821fbSChao Yu f2fs_notice(sbi, "Disable nat_bits due to no space"); 130994c821fbSChao Yu } else if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG) && 131094c821fbSChao Yu f2fs_nat_bitmap_enabled(sbi)) { 131194c821fbSChao Yu f2fs_enable_nat_bits(sbi); 131294c821fbSChao Yu set_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 131394c821fbSChao Yu f2fs_notice(sbi, "Rebuild and enable nat_bits"); 131494c821fbSChao Yu } 131594c821fbSChao Yu } 1316e4c5d848SJaegeuk Kim 131794c821fbSChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 131822ad0b6aSJaegeuk Kim 13191f43e2adSChao Yu if (cpc->reason & CP_TRIMMED) 13201f43e2adSChao Yu __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 1321cd36d7a1SChao Yu else 1322cd36d7a1SChao Yu __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 13231f43e2adSChao Yu 1324c473f1a9SChao Yu if (cpc->reason & CP_UMOUNT) 1325e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1326e4c5d848SJaegeuk Kim else 1327e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1328e4c5d848SJaegeuk Kim 1329c473f1a9SChao Yu if (cpc->reason & CP_FASTBOOT) 1330e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1331e4c5d848SJaegeuk Kim else 1332e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1333e4c5d848SJaegeuk Kim 1334e4c5d848SJaegeuk Kim if (orphan_num) 1335e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1336e4c5d848SJaegeuk Kim else 1337e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1338e4c5d848SJaegeuk Kim 1339c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1340e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1341e4c5d848SJaegeuk Kim 1342c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS)) 1343c84ef3c5SSahitya Tummala __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1344c84ef3c5SSahitya Tummala else 1345c84ef3c5SSahitya Tummala __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1346c84ef3c5SSahitya Tummala 13474354994fSDaniel Rosenberg if (is_sbi_flag_set(sbi, SBI_CP_DISABLED)) 13484354994fSDaniel Rosenberg __set_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13494354994fSDaniel Rosenberg else 13504354994fSDaniel Rosenberg __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13514354994fSDaniel Rosenberg 1352db610a64SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK)) 1353db610a64SJaegeuk Kim __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1354db610a64SJaegeuk Kim else 1355db610a64SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1356db610a64SJaegeuk Kim 1357af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) 1358af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1359bc88ac96SJaegeuk Kim else 1360bc88ac96SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1361af033b2aSChao Yu 1362af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) 1363af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1364af033b2aSChao Yu 1365e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */ 1366e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1367f2367923SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); 1368e4c5d848SJaegeuk Kim 1369d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1370e4c5d848SJaegeuk Kim } 1371e4c5d848SJaegeuk Kim 137246706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi, 137346706d59SGao Xiang void *src, block_t blk_addr) 137446706d59SGao Xiang { 137546706d59SGao Xiang struct writeback_control wbc = { 137646706d59SGao Xiang .for_reclaim = 0, 137746706d59SGao Xiang }; 137846706d59SGao Xiang 137946706d59SGao Xiang /* 138046706d59SGao Xiang * pagevec_lookup_tag and lock_page again will take 13814d57b86dSChao Yu * some extra time. Therefore, f2fs_update_meta_pages and 13824d57b86dSChao Yu * f2fs_sync_meta_pages are combined in this function. 138346706d59SGao Xiang */ 13844d57b86dSChao Yu struct page *page = f2fs_grab_meta_page(sbi, blk_addr); 138546706d59SGao Xiang int err; 138646706d59SGao Xiang 1387bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 13888d64d365SChao Yu 13898d64d365SChao Yu memcpy(page_address(page), src, PAGE_SIZE); 13908d64d365SChao Yu 13918d64d365SChao Yu set_page_dirty(page); 139246706d59SGao Xiang if (unlikely(!clear_page_dirty_for_io(page))) 139346706d59SGao Xiang f2fs_bug_on(sbi, 1); 139446706d59SGao Xiang 139546706d59SGao Xiang /* writeout cp pack 2 page */ 139646706d59SGao Xiang err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO); 1397af697c0fSJaegeuk Kim if (unlikely(err && f2fs_cp_error(sbi))) { 1398af697c0fSJaegeuk Kim f2fs_put_page(page, 1); 1399af697c0fSJaegeuk Kim return; 1400af697c0fSJaegeuk Kim } 140146706d59SGao Xiang 1402af697c0fSJaegeuk Kim f2fs_bug_on(sbi, err); 140346706d59SGao Xiang f2fs_put_page(page, 0); 140446706d59SGao Xiang 140546706d59SGao Xiang /* submit checkpoint (with barrier if NOBARRIER is not set) */ 140646706d59SGao Xiang f2fs_submit_merged_write(sbi, META_FLUSH); 140746706d59SGao Xiang } 140846706d59SGao Xiang 14093a0a9cbcSChao Yu static inline u64 get_sectors_written(struct block_device *bdev) 14103a0a9cbcSChao Yu { 1411ff49c86fSLinus Torvalds return (u64)part_stat_read(bdev, sectors[STAT_WRITE]); 14123a0a9cbcSChao Yu } 14133a0a9cbcSChao Yu 14143a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi) 14153a0a9cbcSChao Yu { 14163a0a9cbcSChao Yu if (f2fs_is_multi_device(sbi)) { 14173a0a9cbcSChao Yu u64 sectors = 0; 14183a0a9cbcSChao Yu int i; 14193a0a9cbcSChao Yu 14203a0a9cbcSChao Yu for (i = 0; i < sbi->s_ndevs; i++) 14213a0a9cbcSChao Yu sectors += get_sectors_written(FDEV(i).bdev); 14223a0a9cbcSChao Yu 14233a0a9cbcSChao Yu return sectors; 14243a0a9cbcSChao Yu } 14253a0a9cbcSChao Yu 14263a0a9cbcSChao Yu return get_sectors_written(sbi->sb->s_bdev); 14273a0a9cbcSChao Yu } 14283a0a9cbcSChao Yu 1429c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1430127e670aSJaegeuk Kim { 1431127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 143277041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 1433d1aa2453SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags; 1434127e670aSJaegeuk Kim block_t start_blk; 1435127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 14367e586fa0SJaegeuk Kim __u32 crc32 = 0; 1437127e670aSJaegeuk Kim int i; 143855141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 14398f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 14408f1dbbbbSShuoran Liu u64 kbytes_written; 14411228b482SChao Yu int err; 1442127e670aSJaegeuk Kim 1443127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 14444d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1445127e670aSJaegeuk Kim 14467a88ddb5SChao Yu /* start to update checkpoint, cp ver is already updated previously */ 1447a1f72ac2SChao Yu ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true)); 1448127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1449b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1450127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 1451127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 1452127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 1453127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 1454127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 1455127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 1456127e670aSJaegeuk Kim } 1457b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1458127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 1459127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 1460127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 1461127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 1462127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 1463127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 1464127e670aSJaegeuk Kim } 1465127e670aSJaegeuk Kim 1466127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 14674d57b86dSChao Yu data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); 1468d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1469b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1470aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1471127e670aSJaegeuk Kim else 1472aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1473d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1474127e670aSJaegeuk Kim 147567298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 14761dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 14771dbe4152SChangman Lee orphan_blocks); 1478127e670aSJaegeuk Kim 1479119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1480b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 14811dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14821dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1483119ee914SJaegeuk Kim else 1484b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 14851dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14861dbe4152SChangman Lee orphan_blocks); 1487119ee914SJaegeuk Kim 1488e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */ 1489e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc); 1490a468f0efSJaegeuk Kim 1491127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1492127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1493127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1494127e670aSJaegeuk Kim 1495d7eb8f1cSChao Yu crc32 = f2fs_checkpoint_chksum(sbi, ckpt); 14967e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 14977e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1498127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1499127e670aSJaegeuk Kim 15008508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi); 1501127e670aSJaegeuk Kim 150222ad0b6aSJaegeuk Kim /* write nat bits */ 150394c821fbSChao Yu if ((cpc->reason & CP_UMOUNT) && 150494c821fbSChao Yu is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) { 150522ad0b6aSJaegeuk Kim __u64 cp_ver = cur_cp_version(ckpt); 150622ad0b6aSJaegeuk Kim block_t blk; 150722ad0b6aSJaegeuk Kim 150822ad0b6aSJaegeuk Kim cp_ver |= ((__u64)crc32 << 32); 150922ad0b6aSJaegeuk Kim *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); 151022ad0b6aSJaegeuk Kim 151122ad0b6aSJaegeuk Kim blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks; 151222ad0b6aSJaegeuk Kim for (i = 0; i < nm_i->nat_bits_blocks; i++) 15134d57b86dSChao Yu f2fs_update_meta_page(sbi, nm_i->nat_bits + 151422ad0b6aSJaegeuk Kim (i << F2FS_BLKSIZE_BITS), blk + i); 151522ad0b6aSJaegeuk Kim } 151622ad0b6aSJaegeuk Kim 1517127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 15184d57b86dSChao Yu f2fs_update_meta_page(sbi, ckpt, start_blk++); 1519127e670aSJaegeuk Kim 1520381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 15214d57b86dSChao Yu f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1522381722d2SChao Yu start_blk++); 15231dbe4152SChangman Lee 152467298804SChao Yu if (orphan_num) { 1525127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1526127e670aSJaegeuk Kim start_blk += orphan_blocks; 1527127e670aSJaegeuk Kim } 1528127e670aSJaegeuk Kim 15294d57b86dSChao Yu f2fs_write_data_summaries(sbi, start_blk); 1530127e670aSJaegeuk Kim start_blk += data_sum_blocks; 15318f1dbbbbSShuoran Liu 15328f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 15338f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 15343a0a9cbcSChao Yu kbytes_written += (f2fs_get_sectors_written(sbi) - 15353a0a9cbcSChao Yu sbi->sectors_written_start) >> 1; 1536b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 15378f1dbbbbSShuoran Liu 1538119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 15394d57b86dSChao Yu f2fs_write_node_summaries(sbi, start_blk); 1540127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1541127e670aSJaegeuk Kim } 1542127e670aSJaegeuk Kim 154346706d59SGao Xiang /* update user_block_counts */ 154446706d59SGao Xiang sbi->last_valid_block_count = sbi->total_valid_block_count; 154546706d59SGao Xiang percpu_counter_set(&sbi->alloc_valid_block_count, 0); 1546127e670aSJaegeuk Kim 154746706d59SGao Xiang /* Here, we have one bio having CP pack except cp pack 2 page */ 15484d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1549bf22c3ccSSahitya Tummala /* Wait for all dirty meta pages to be submitted for IO */ 1550bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META); 155146706d59SGao Xiang 155246706d59SGao Xiang /* wait for previous submitted meta pages writeback */ 1553bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1554127e670aSJaegeuk Kim 155546706d59SGao Xiang /* flush all device cache */ 155646706d59SGao Xiang err = f2fs_flush_device_cache(sbi); 155746706d59SGao Xiang if (err) 155846706d59SGao Xiang return err; 1559127e670aSJaegeuk Kim 156046706d59SGao Xiang /* barrier and flush checkpoint cp pack 2 page if it can */ 156146706d59SGao Xiang commit_checkpoint(sbi, ckpt, start_blk); 1562bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 15636a8f8ca5SJaegeuk Kim 156418767e62SChao Yu /* 1565542989b6SEric Biggers * invalidate intermediate page cache borrowed from meta inode which are 1566aff6fbbeSChao Yu * used for migration of encrypted, verity or compressed inode's blocks. 156718767e62SChao Yu */ 1568aff6fbbeSChao Yu if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) || 1569aff6fbbeSChao Yu f2fs_sb_has_compression(sbi)) 157018767e62SChao Yu invalidate_mapping_pages(META_MAPPING(sbi), 157118767e62SChao Yu MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1); 157218767e62SChao Yu 15734d57b86dSChao Yu f2fs_release_ino_entry(sbi, false); 1574cf779cabSJaegeuk Kim 157550fa53ecSChao Yu f2fs_reset_fsync_node_info(sbi); 157650fa53ecSChao Yu 1577caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1578bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP); 1579af033b2aSChao Yu clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1580c9c8ed50SChao Yu 1581c9c8ed50SChao Yu spin_lock(&sbi->stat_lock); 15824354994fSDaniel Rosenberg sbi->unusable_block_count = 0; 1583c9c8ed50SChao Yu spin_unlock(&sbi->stat_lock); 1584c9c8ed50SChao Yu 15858508e44aSJaegeuk Kim __set_cp_next_pack(sbi); 1586c34f42e2SChao Yu 1587c2a080aeSChao Yu /* 1588c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is 1589c2a080aeSChao Yu * updated during writing checkpoint. 1590c2a080aeSChao Yu */ 1591c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) || 1592c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA)) 1593c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 1594c2a080aeSChao Yu 1595c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1596c2a080aeSChao Yu 1597af697c0fSJaegeuk Kim return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0; 1598127e670aSJaegeuk Kim } 1599127e670aSJaegeuk Kim 16004d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1601127e670aSJaegeuk Kim { 1602127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1603127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1604c34f42e2SChao Yu int err = 0; 1605127e670aSJaegeuk Kim 1606f5a131bbSChao Yu if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi)) 1607f5a131bbSChao Yu return -EROFS; 1608f5a131bbSChao Yu 16094354994fSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 16104354994fSDaniel Rosenberg if (cpc->reason != CP_PAUSE) 16114354994fSDaniel Rosenberg return 0; 1612dcbb4c10SJoe Perches f2fs_warn(sbi, "Start checkpoint disabled!"); 16134354994fSDaniel Rosenberg } 1614b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 16158769918bSSahitya Tummala down_write(&sbi->cp_global_sem); 16168501017eSJaegeuk Kim 1617caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1618c473f1a9SChao Yu ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1619c473f1a9SChao Yu ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) 16208501017eSJaegeuk Kim goto out; 1621c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1622c34f42e2SChao Yu err = -EIO; 1623cf779cabSJaegeuk Kim goto out; 1624c34f42e2SChao Yu } 16252bda542dSWanpeng Li 16262bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 16272bda542dSWanpeng Li 1628c34f42e2SChao Yu err = block_operations(sbi); 1629c34f42e2SChao Yu if (err) 1630cf779cabSJaegeuk Kim goto out; 1631127e670aSJaegeuk Kim 163275ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 16332af4bd6cSNamjae Jeon 1634b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 1635127e670aSJaegeuk Kim 163658cce381SYunlei He /* this is the case of multiple fstrims without any changes */ 1637c473f1a9SChao Yu if (cpc->reason & CP_DISCARD) { 16384d57b86dSChao Yu if (!f2fs_exist_trim_candidates(sbi, cpc)) { 163925290fa5SJaegeuk Kim unblock_operations(sbi); 164025290fa5SJaegeuk Kim goto out; 164125290fa5SJaegeuk Kim } 164225290fa5SJaegeuk Kim 1643a95ba66aSJaegeuk Kim if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 && 16440333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 && 16450333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) { 16464d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 16474d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 164858cce381SYunlei He unblock_operations(sbi); 164958cce381SYunlei He goto out; 165058cce381SYunlei He } 16510333ad4eSJaegeuk Kim } 165258cce381SYunlei He 1653127e670aSJaegeuk Kim /* 1654127e670aSJaegeuk Kim * update checkpoint pack index 1655127e670aSJaegeuk Kim * Increase the version number so that 1656127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1657127e670aSJaegeuk Kim */ 1658d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1659127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1660127e670aSJaegeuk Kim 1661127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1662edc55aafSJaegeuk Kim err = f2fs_flush_nat_entries(sbi, cpc); 166391803392SChao Yu if (err) { 166491803392SChao Yu f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err); 166591803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 1666edc55aafSJaegeuk Kim goto stop; 166791803392SChao Yu } 1668edc55aafSJaegeuk Kim 16694d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 1670127e670aSJaegeuk Kim 1671d0b9e42aSChao Yu /* save inmem log status */ 1672093749e2SChao Yu f2fs_save_inmem_curseg(sbi); 1673d0b9e42aSChao Yu 1674c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 167591803392SChao Yu if (err) { 167691803392SChao Yu f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err); 167791803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); 16784d57b86dSChao Yu f2fs_release_discard_addrs(sbi); 167991803392SChao Yu } else { 16804d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 168191803392SChao Yu } 1682d0b9e42aSChao Yu 1683093749e2SChao Yu f2fs_restore_inmem_curseg(sbi); 1684edc55aafSJaegeuk Kim stop: 1685127e670aSJaegeuk Kim unblock_operations(sbi); 1686942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 168710027551SJaegeuk Kim 1688c473f1a9SChao Yu if (cpc->reason & CP_RECOVERY) 1689dcbb4c10SJoe Perches f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver); 169060b99b48SJaegeuk Kim 16917a88ddb5SChao Yu /* update CP_TIME to trigger checkpoint periodically */ 16926beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 169355d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 16948501017eSJaegeuk Kim out: 1695b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 16968769918bSSahitya Tummala up_write(&sbi->cp_global_sem); 1697c34f42e2SChao Yu return err; 1698127e670aSJaegeuk Kim } 1699127e670aSJaegeuk Kim 17004d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi) 1701127e670aSJaegeuk Kim { 17026451e041SJaegeuk Kim int i; 17036451e041SJaegeuk Kim 17046451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 170567298804SChao Yu struct inode_management *im = &sbi->im[i]; 170667298804SChao Yu 170767298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 170867298804SChao Yu spin_lock_init(&im->ino_lock); 170967298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 171067298804SChao Yu im->ino_num = 0; 17116451e041SJaegeuk Kim } 17126451e041SJaegeuk Kim 1713b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 1714d0b9e42aSChao Yu NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) * 171514b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1716127e670aSJaegeuk Kim } 1717127e670aSJaegeuk Kim 17184d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void) 1719127e670aSJaegeuk Kim { 17206451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 17216451e041SJaegeuk Kim sizeof(struct ino_entry)); 17226451e041SJaegeuk Kim if (!ino_entry_slab) 1723127e670aSJaegeuk Kim return -ENOMEM; 17244d57b86dSChao Yu f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 172506292073SChao Yu sizeof(struct inode_entry)); 17264d57b86dSChao Yu if (!f2fs_inode_entry_slab) { 17276451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1728127e670aSJaegeuk Kim return -ENOMEM; 1729127e670aSJaegeuk Kim } 1730127e670aSJaegeuk Kim return 0; 1731127e670aSJaegeuk Kim } 1732127e670aSJaegeuk Kim 17334d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void) 1734127e670aSJaegeuk Kim { 17356451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 17364d57b86dSChao Yu kmem_cache_destroy(f2fs_inode_entry_slab); 1737127e670aSJaegeuk Kim } 1738261eeb9cSDaeho Jeong 1739261eeb9cSDaeho Jeong static int __write_checkpoint_sync(struct f2fs_sb_info *sbi) 1740261eeb9cSDaeho Jeong { 1741261eeb9cSDaeho Jeong struct cp_control cpc = { .reason = CP_SYNC, }; 1742261eeb9cSDaeho Jeong int err; 1743261eeb9cSDaeho Jeong 1744261eeb9cSDaeho Jeong down_write(&sbi->gc_lock); 1745261eeb9cSDaeho Jeong err = f2fs_write_checkpoint(sbi, &cpc); 1746261eeb9cSDaeho Jeong up_write(&sbi->gc_lock); 1747261eeb9cSDaeho Jeong 1748261eeb9cSDaeho Jeong return err; 1749261eeb9cSDaeho Jeong } 1750261eeb9cSDaeho Jeong 1751261eeb9cSDaeho Jeong static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi) 1752261eeb9cSDaeho Jeong { 1753261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1754261eeb9cSDaeho Jeong struct ckpt_req *req, *next; 1755261eeb9cSDaeho Jeong struct llist_node *dispatch_list; 1756261eeb9cSDaeho Jeong u64 sum_diff = 0, diff, count = 0; 1757261eeb9cSDaeho Jeong int ret; 1758261eeb9cSDaeho Jeong 1759261eeb9cSDaeho Jeong dispatch_list = llist_del_all(&cprc->issue_list); 1760261eeb9cSDaeho Jeong if (!dispatch_list) 1761261eeb9cSDaeho Jeong return; 1762261eeb9cSDaeho Jeong dispatch_list = llist_reverse_order(dispatch_list); 1763261eeb9cSDaeho Jeong 1764261eeb9cSDaeho Jeong ret = __write_checkpoint_sync(sbi); 1765261eeb9cSDaeho Jeong atomic_inc(&cprc->issued_ckpt); 1766261eeb9cSDaeho Jeong 1767261eeb9cSDaeho Jeong llist_for_each_entry_safe(req, next, dispatch_list, llnode) { 1768261eeb9cSDaeho Jeong diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time); 1769261eeb9cSDaeho Jeong req->ret = ret; 1770261eeb9cSDaeho Jeong complete(&req->wait); 1771261eeb9cSDaeho Jeong 1772261eeb9cSDaeho Jeong sum_diff += diff; 1773261eeb9cSDaeho Jeong count++; 1774261eeb9cSDaeho Jeong } 1775261eeb9cSDaeho Jeong atomic_sub(count, &cprc->queued_ckpt); 1776261eeb9cSDaeho Jeong atomic_add(count, &cprc->total_ckpt); 1777261eeb9cSDaeho Jeong 1778261eeb9cSDaeho Jeong spin_lock(&cprc->stat_lock); 1779261eeb9cSDaeho Jeong cprc->cur_time = (unsigned int)div64_u64(sum_diff, count); 1780261eeb9cSDaeho Jeong if (cprc->peak_time < cprc->cur_time) 1781261eeb9cSDaeho Jeong cprc->peak_time = cprc->cur_time; 1782261eeb9cSDaeho Jeong spin_unlock(&cprc->stat_lock); 1783261eeb9cSDaeho Jeong } 1784261eeb9cSDaeho Jeong 1785261eeb9cSDaeho Jeong static int issue_checkpoint_thread(void *data) 1786261eeb9cSDaeho Jeong { 1787261eeb9cSDaeho Jeong struct f2fs_sb_info *sbi = data; 1788261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1789261eeb9cSDaeho Jeong wait_queue_head_t *q = &cprc->ckpt_wait_queue; 1790261eeb9cSDaeho Jeong repeat: 1791261eeb9cSDaeho Jeong if (kthread_should_stop()) 1792261eeb9cSDaeho Jeong return 0; 1793261eeb9cSDaeho Jeong 1794261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) 1795261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1796261eeb9cSDaeho Jeong 1797261eeb9cSDaeho Jeong wait_event_interruptible(*q, 1798261eeb9cSDaeho Jeong kthread_should_stop() || !llist_empty(&cprc->issue_list)); 1799261eeb9cSDaeho Jeong goto repeat; 1800261eeb9cSDaeho Jeong } 1801261eeb9cSDaeho Jeong 1802261eeb9cSDaeho Jeong static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi, 1803261eeb9cSDaeho Jeong struct ckpt_req *wait_req) 1804261eeb9cSDaeho Jeong { 1805261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1806261eeb9cSDaeho Jeong 1807261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) { 1808261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1809261eeb9cSDaeho Jeong } else { 1810261eeb9cSDaeho Jeong /* already dispatched by issue_checkpoint_thread */ 1811261eeb9cSDaeho Jeong if (wait_req) 1812261eeb9cSDaeho Jeong wait_for_completion(&wait_req->wait); 1813261eeb9cSDaeho Jeong } 1814261eeb9cSDaeho Jeong } 1815261eeb9cSDaeho Jeong 1816261eeb9cSDaeho Jeong static void init_ckpt_req(struct ckpt_req *req) 1817261eeb9cSDaeho Jeong { 1818261eeb9cSDaeho Jeong memset(req, 0, sizeof(struct ckpt_req)); 1819261eeb9cSDaeho Jeong 1820261eeb9cSDaeho Jeong init_completion(&req->wait); 1821261eeb9cSDaeho Jeong req->queue_time = ktime_get(); 1822261eeb9cSDaeho Jeong } 1823261eeb9cSDaeho Jeong 1824261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) 1825261eeb9cSDaeho Jeong { 1826261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1827261eeb9cSDaeho Jeong struct ckpt_req req; 1828261eeb9cSDaeho Jeong struct cp_control cpc; 1829261eeb9cSDaeho Jeong 1830261eeb9cSDaeho Jeong cpc.reason = __get_cp_reason(sbi); 1831261eeb9cSDaeho Jeong if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) { 1832261eeb9cSDaeho Jeong int ret; 1833261eeb9cSDaeho Jeong 1834261eeb9cSDaeho Jeong down_write(&sbi->gc_lock); 1835261eeb9cSDaeho Jeong ret = f2fs_write_checkpoint(sbi, &cpc); 1836261eeb9cSDaeho Jeong up_write(&sbi->gc_lock); 1837261eeb9cSDaeho Jeong 1838261eeb9cSDaeho Jeong return ret; 1839261eeb9cSDaeho Jeong } 1840261eeb9cSDaeho Jeong 1841261eeb9cSDaeho Jeong if (!cprc->f2fs_issue_ckpt) 1842261eeb9cSDaeho Jeong return __write_checkpoint_sync(sbi); 1843261eeb9cSDaeho Jeong 1844261eeb9cSDaeho Jeong init_ckpt_req(&req); 1845261eeb9cSDaeho Jeong 1846261eeb9cSDaeho Jeong llist_add(&req.llnode, &cprc->issue_list); 1847261eeb9cSDaeho Jeong atomic_inc(&cprc->queued_ckpt); 1848261eeb9cSDaeho Jeong 18493b42c741SChao Yu /* 18503b42c741SChao Yu * update issue_list before we wake up issue_checkpoint thread, 18513b42c741SChao Yu * this smp_mb() pairs with another barrier in ___wait_event(), 18523b42c741SChao Yu * see more details in comments of waitqueue_active(). 18533b42c741SChao Yu */ 1854261eeb9cSDaeho Jeong smp_mb(); 1855261eeb9cSDaeho Jeong 1856261eeb9cSDaeho Jeong if (waitqueue_active(&cprc->ckpt_wait_queue)) 1857261eeb9cSDaeho Jeong wake_up(&cprc->ckpt_wait_queue); 1858261eeb9cSDaeho Jeong 1859261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1860261eeb9cSDaeho Jeong wait_for_completion(&req.wait); 1861261eeb9cSDaeho Jeong else 1862261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, &req); 1863261eeb9cSDaeho Jeong 1864261eeb9cSDaeho Jeong return req.ret; 1865261eeb9cSDaeho Jeong } 1866261eeb9cSDaeho Jeong 1867261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi) 1868261eeb9cSDaeho Jeong { 1869261eeb9cSDaeho Jeong dev_t dev = sbi->sb->s_bdev->bd_dev; 1870261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1871261eeb9cSDaeho Jeong 1872261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1873261eeb9cSDaeho Jeong return 0; 1874261eeb9cSDaeho Jeong 1875261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi, 1876261eeb9cSDaeho Jeong "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev)); 1877261eeb9cSDaeho Jeong if (IS_ERR(cprc->f2fs_issue_ckpt)) { 1878261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1879261eeb9cSDaeho Jeong return -ENOMEM; 1880261eeb9cSDaeho Jeong } 1881261eeb9cSDaeho Jeong 1882e6592066SDaeho Jeong set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio); 1883261eeb9cSDaeho Jeong 1884261eeb9cSDaeho Jeong return 0; 1885261eeb9cSDaeho Jeong } 1886261eeb9cSDaeho Jeong 1887261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi) 1888261eeb9cSDaeho Jeong { 1889261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1890261eeb9cSDaeho Jeong 1891261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) { 1892261eeb9cSDaeho Jeong struct task_struct *ckpt_task = cprc->f2fs_issue_ckpt; 1893261eeb9cSDaeho Jeong 1894261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1895261eeb9cSDaeho Jeong kthread_stop(ckpt_task); 1896261eeb9cSDaeho Jeong 1897261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, NULL); 1898261eeb9cSDaeho Jeong } 1899261eeb9cSDaeho Jeong } 1900261eeb9cSDaeho Jeong 1901261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi) 1902261eeb9cSDaeho Jeong { 1903261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1904261eeb9cSDaeho Jeong 1905261eeb9cSDaeho Jeong atomic_set(&cprc->issued_ckpt, 0); 1906261eeb9cSDaeho Jeong atomic_set(&cprc->total_ckpt, 0); 1907261eeb9cSDaeho Jeong atomic_set(&cprc->queued_ckpt, 0); 1908e6592066SDaeho Jeong cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO; 1909261eeb9cSDaeho Jeong init_waitqueue_head(&cprc->ckpt_wait_queue); 1910261eeb9cSDaeho Jeong init_llist_head(&cprc->issue_list); 1911261eeb9cSDaeho Jeong spin_lock_init(&cprc->stat_lock); 1912261eeb9cSDaeho Jeong } 1913