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" 212af4bd6cSNamjae Jeon #include <trace/events/f2fs.h> 22127e670aSJaegeuk Kim 23261eeb9cSDaeho Jeong #define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) 24261eeb9cSDaeho Jeong 256451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab; 264d57b86dSChao Yu struct kmem_cache *f2fs_inode_entry_slab; 27127e670aSJaegeuk Kim 2838f91ca8SJaegeuk Kim void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io) 2938f91ca8SJaegeuk Kim { 30d494500aSChao Yu f2fs_build_fault_attr(sbi, 0, 0); 31aaec2b1dSChao Yu set_ckpt_flags(sbi, CP_ERROR_FLAG); 3238f91ca8SJaegeuk Kim if (!end_io) 33b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 3438f91ca8SJaegeuk Kim } 3538f91ca8SJaegeuk Kim 360a8165d7SJaegeuk Kim /* 37127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 38127e670aSJaegeuk Kim */ 394d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 40127e670aSJaegeuk Kim { 419df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 42beb78181SZhang Qilong struct page *page; 43127e670aSJaegeuk Kim repeat: 44300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 45127e670aSJaegeuk Kim if (!page) { 46127e670aSJaegeuk Kim cond_resched(); 47127e670aSJaegeuk Kim goto repeat; 48127e670aSJaegeuk Kim } 49bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 50237c0790SJaegeuk Kim if (!PageUptodate(page)) 51127e670aSJaegeuk Kim SetPageUptodate(page); 52127e670aSJaegeuk Kim return page; 53127e670aSJaegeuk Kim } 54127e670aSJaegeuk Kim 552b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index, 562b947003SChao Yu bool is_meta) 57127e670aSJaegeuk Kim { 589df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 59127e670aSJaegeuk Kim struct page *page; 60cf04e8ebSJaegeuk Kim struct f2fs_io_info fio = { 6105ca3632SJaegeuk Kim .sbi = sbi, 62cf04e8ebSJaegeuk Kim .type = META, 6304d328deSMike Christie .op = REQ_OP_READ, 6470fd7614SChristoph Hellwig .op_flags = REQ_META | REQ_PRIO, 657a9d7548SChao Yu .old_blkaddr = index, 667a9d7548SChao Yu .new_blkaddr = index, 674375a336SJaegeuk Kim .encrypted_page = NULL, 686dc3a126SChao Yu .is_por = !is_meta, 69cf04e8ebSJaegeuk Kim }; 707735730dSChao Yu int err; 712b947003SChao Yu 722b947003SChao Yu if (unlikely(!is_meta)) 7304d328deSMike Christie fio.op_flags &= ~REQ_META; 74127e670aSJaegeuk Kim repeat: 75300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 76127e670aSJaegeuk Kim if (!page) { 77127e670aSJaegeuk Kim cond_resched(); 78127e670aSJaegeuk Kim goto repeat; 79127e670aSJaegeuk Kim } 80393ff91fSJaegeuk Kim if (PageUptodate(page)) 81393ff91fSJaegeuk Kim goto out; 82127e670aSJaegeuk Kim 8305ca3632SJaegeuk Kim fio.page = page; 8405ca3632SJaegeuk Kim 857735730dSChao Yu err = f2fs_submit_page_bio(&fio); 867735730dSChao Yu if (err) { 877735730dSChao Yu f2fs_put_page(page, 1); 887735730dSChao Yu return ERR_PTR(err); 8986531d6bSJaegeuk Kim } 90393ff91fSJaegeuk Kim 918b83ac81SChao Yu f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE); 928b83ac81SChao Yu 93393ff91fSJaegeuk Kim lock_page(page); 946bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) { 95afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 96afcb7ca0SJaegeuk Kim goto repeat; 97afcb7ca0SJaegeuk Kim } 98f3f338caSChao Yu 9981114baaSChao Yu if (unlikely(!PageUptodate(page))) { 1007735730dSChao Yu f2fs_put_page(page, 1); 1017735730dSChao Yu return ERR_PTR(-EIO); 10281114baaSChao Yu } 103393ff91fSJaegeuk Kim out: 104127e670aSJaegeuk Kim return page; 105127e670aSJaegeuk Kim } 106127e670aSJaegeuk Kim 1074d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 1082b947003SChao Yu { 1092b947003SChao Yu return __get_meta_page(sbi, index, true); 1102b947003SChao Yu } 1112b947003SChao Yu 11286f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index) 1137735730dSChao Yu { 1147735730dSChao Yu struct page *page; 1157735730dSChao Yu int count = 0; 1167735730dSChao Yu 1177735730dSChao Yu retry: 1187735730dSChao Yu page = __get_meta_page(sbi, index, true); 1197735730dSChao Yu if (IS_ERR(page)) { 1207735730dSChao Yu if (PTR_ERR(page) == -EIO && 1217735730dSChao Yu ++count <= DEFAULT_RETRY_IO_COUNT) 1227735730dSChao Yu goto retry; 1237735730dSChao Yu f2fs_stop_checkpoint(sbi, false); 1247735730dSChao Yu } 1257735730dSChao Yu return page; 1267735730dSChao Yu } 1277735730dSChao Yu 1282b947003SChao Yu /* for POR only */ 1294d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 1302b947003SChao Yu { 1312b947003SChao Yu return __get_meta_page(sbi, index, false); 1322b947003SChao Yu } 1332b947003SChao Yu 13493770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr, 13593770ab7SChao Yu int type) 13693770ab7SChao Yu { 13793770ab7SChao Yu struct seg_entry *se; 13893770ab7SChao Yu unsigned int segno, offset; 13993770ab7SChao Yu bool exist; 14093770ab7SChao Yu 14193770ab7SChao Yu if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ) 14293770ab7SChao Yu return true; 14393770ab7SChao Yu 14493770ab7SChao Yu segno = GET_SEGNO(sbi, blkaddr); 14593770ab7SChao Yu offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 14693770ab7SChao Yu se = get_seg_entry(sbi, segno); 14793770ab7SChao Yu 14893770ab7SChao Yu exist = f2fs_test_bit(offset, se->cur_valid_map); 14993770ab7SChao Yu if (!exist && type == DATA_GENERIC_ENHANCE) { 150dcbb4c10SJoe Perches f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", 151dcbb4c10SJoe Perches blkaddr, exist); 15293770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 15393770ab7SChao Yu WARN_ON(1); 15493770ab7SChao Yu } 15593770ab7SChao Yu return exist; 15693770ab7SChao Yu } 15793770ab7SChao Yu 158e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 1594d57b86dSChao Yu block_t blkaddr, int type) 160662befdaSChao Yu { 161662befdaSChao Yu switch (type) { 162662befdaSChao Yu case META_NAT: 16366b00c18SChao Yu break; 164662befdaSChao Yu case META_SIT: 16566b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 16666b00c18SChao Yu return false; 16766b00c18SChao Yu break; 16881c1a0f1SChao Yu case META_SSA: 16966b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 17066b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr)) 17166b00c18SChao Yu return false; 17266b00c18SChao Yu break; 173662befdaSChao Yu case META_CP: 17466b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 17566b00c18SChao Yu blkaddr < __start_cp_addr(sbi))) 17666b00c18SChao Yu return false; 17766b00c18SChao Yu break; 1784c521f49SJaegeuk Kim case META_POR: 17993770ab7SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 18093770ab7SChao Yu blkaddr < MAIN_BLKADDR(sbi))) 18193770ab7SChao Yu return false; 18293770ab7SChao Yu break; 183e1da7872SChao Yu case DATA_GENERIC: 18493770ab7SChao Yu case DATA_GENERIC_ENHANCE: 18593770ab7SChao Yu case DATA_GENERIC_ENHANCE_READ: 18666b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 187c9b60788SChao Yu blkaddr < MAIN_BLKADDR(sbi))) { 188dcbb4c10SJoe Perches f2fs_warn(sbi, "access invalid blkaddr:%u", 189dcbb4c10SJoe Perches blkaddr); 19093770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 191c9b60788SChao Yu WARN_ON(1); 19266b00c18SChao Yu return false; 19393770ab7SChao Yu } else { 19493770ab7SChao Yu return __is_bitmap_valid(sbi, blkaddr, type); 195c9b60788SChao Yu } 19666b00c18SChao Yu break; 197e1da7872SChao Yu case META_GENERIC: 198e1da7872SChao Yu if (unlikely(blkaddr < SEG0_BLKADDR(sbi) || 199e1da7872SChao Yu blkaddr >= MAIN_BLKADDR(sbi))) 200e1da7872SChao Yu return false; 201e1da7872SChao Yu break; 202662befdaSChao Yu default: 203662befdaSChao Yu BUG(); 204662befdaSChao Yu } 20566b00c18SChao Yu 20666b00c18SChao Yu return true; 207662befdaSChao Yu } 208662befdaSChao Yu 209662befdaSChao Yu /* 2107a88ddb5SChao Yu * Readahead CP/NAT/SIT/SSA/POR pages 211662befdaSChao Yu */ 2124d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 21326879fb1SChao Yu int type, bool sync) 214662befdaSChao Yu { 215662befdaSChao Yu struct page *page; 2164c521f49SJaegeuk Kim block_t blkno = start; 217662befdaSChao Yu struct f2fs_io_info fio = { 21805ca3632SJaegeuk Kim .sbi = sbi, 219662befdaSChao Yu .type = META, 22004d328deSMike Christie .op = REQ_OP_READ, 22170fd7614SChristoph Hellwig .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, 2224375a336SJaegeuk Kim .encrypted_page = NULL, 223fb830fc5SChao Yu .in_list = false, 2246dc3a126SChao Yu .is_por = (type == META_POR), 225662befdaSChao Yu }; 226e9f5b8b8SChao Yu struct blk_plug plug; 227ce4c638cSChao Yu int err; 228662befdaSChao Yu 2292b947003SChao Yu if (unlikely(type == META_POR)) 23004d328deSMike Christie fio.op_flags &= ~REQ_META; 2312b947003SChao Yu 232e9f5b8b8SChao Yu blk_start_plug(&plug); 233662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 234662befdaSChao Yu 235e1da7872SChao Yu if (!f2fs_is_valid_blkaddr(sbi, blkno, type)) 23666b00c18SChao Yu goto out; 23766b00c18SChao Yu 238662befdaSChao Yu switch (type) { 239662befdaSChao Yu case META_NAT: 24066b00c18SChao Yu if (unlikely(blkno >= 24166b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 242662befdaSChao Yu blkno = 0; 24366b00c18SChao Yu /* get nat block addr */ 2447a9d7548SChao Yu fio.new_blkaddr = current_nat_addr(sbi, 245662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 246662befdaSChao Yu break; 247662befdaSChao Yu case META_SIT: 2486a257471SChao Yu if (unlikely(blkno >= TOTAL_SEGS(sbi))) 2496a257471SChao Yu goto out; 250662befdaSChao Yu /* get sit block addr */ 2517a9d7548SChao Yu fio.new_blkaddr = current_sit_addr(sbi, 252662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 253662befdaSChao Yu break; 25481c1a0f1SChao Yu case META_SSA: 255662befdaSChao Yu case META_CP: 2564c521f49SJaegeuk Kim case META_POR: 2577a9d7548SChao Yu fio.new_blkaddr = blkno; 258662befdaSChao Yu break; 259662befdaSChao Yu default: 260662befdaSChao Yu BUG(); 261662befdaSChao Yu } 262662befdaSChao Yu 263300e129cSJaegeuk Kim page = f2fs_grab_cache_page(META_MAPPING(sbi), 264300e129cSJaegeuk Kim fio.new_blkaddr, false); 265662befdaSChao Yu if (!page) 266662befdaSChao Yu continue; 267662befdaSChao Yu if (PageUptodate(page)) { 268662befdaSChao Yu f2fs_put_page(page, 1); 269662befdaSChao Yu continue; 270662befdaSChao Yu } 271662befdaSChao Yu 27205ca3632SJaegeuk Kim fio.page = page; 273ce4c638cSChao Yu err = f2fs_submit_page_bio(&fio); 274ce4c638cSChao Yu f2fs_put_page(page, err ? 1 : 0); 2758b83ac81SChao Yu 2768b83ac81SChao Yu if (!err) 2778b83ac81SChao Yu f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE); 278662befdaSChao Yu } 279662befdaSChao Yu out: 280e9f5b8b8SChao Yu blk_finish_plug(&plug); 281662befdaSChao Yu return blkno - start; 282662befdaSChao Yu } 283662befdaSChao Yu 2844d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index) 285635aee1fSChao Yu { 286635aee1fSChao Yu struct page *page; 287635aee1fSChao Yu bool readahead = false; 288635aee1fSChao Yu 289635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 2904da7bf5aSJaegeuk Kim if (!page || !PageUptodate(page)) 291635aee1fSChao Yu readahead = true; 292635aee1fSChao Yu f2fs_put_page(page, 0); 293635aee1fSChao Yu 294635aee1fSChao Yu if (readahead) 2954d57b86dSChao Yu f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true); 296635aee1fSChao Yu } 297635aee1fSChao Yu 298b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page, 299b0af6d49SChao Yu struct writeback_control *wbc, 300b0af6d49SChao Yu enum iostat_type io_type) 301127e670aSJaegeuk Kim { 3024081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 303577e3495SJaegeuk Kim 304ecda0de3SChao Yu trace_f2fs_writepage(page, META); 305ecda0de3SChao Yu 306af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 307af697c0fSJaegeuk Kim goto redirty_out; 308caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 309cfb271d4SChao Yu goto redirty_out; 310857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 311cfb271d4SChao Yu goto redirty_out; 312127e670aSJaegeuk Kim 3134d57b86dSChao Yu f2fs_do_write_meta_page(sbi, page, io_type); 314127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 3150c3a5797SChao Yu 3160c3a5797SChao Yu if (wbc->for_reclaim) 317bab475c5SChao Yu f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META); 3180c3a5797SChao Yu 319127e670aSJaegeuk Kim unlock_page(page); 320857dc4e0SJaegeuk Kim 3210c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 322b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, META); 3230c3a5797SChao Yu 324577e3495SJaegeuk Kim return 0; 325cfb271d4SChao Yu 326cfb271d4SChao Yu redirty_out: 32776f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 328cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 329127e670aSJaegeuk Kim } 330127e670aSJaegeuk Kim 331b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page, 332b0af6d49SChao Yu struct writeback_control *wbc) 333b0af6d49SChao Yu { 334b0af6d49SChao Yu return __f2fs_write_meta_page(page, wbc, FS_META_IO); 335b0af6d49SChao Yu } 336b0af6d49SChao Yu 337127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 338127e670aSJaegeuk Kim struct writeback_control *wbc) 339127e670aSJaegeuk Kim { 3404081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 34150c8cdb3SJaegeuk Kim long diff, written; 342127e670aSJaegeuk Kim 3430771fcc7SChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3440771fcc7SChao Yu goto skip_write; 3450771fcc7SChao Yu 3465459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 347812a9597SJaegeuk Kim if (wbc->sync_mode != WB_SYNC_ALL && 348812a9597SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < 349812a9597SJaegeuk Kim nr_pages_to_skip(sbi, META)) 350d3baf95dSJaegeuk Kim goto skip_write; 351127e670aSJaegeuk Kim 352a29d0e0bSYunlei He /* if locked failed, cp will flush dirty pages instead */ 3538769918bSSahitya Tummala if (!down_write_trylock(&sbi->cp_global_sem)) 354a29d0e0bSYunlei He goto skip_write; 355d31c7c3fSYunlei He 356a29d0e0bSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 35750c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 3584d57b86dSChao Yu written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); 3598769918bSSahitya Tummala up_write(&sbi->cp_global_sem); 36050c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 361127e670aSJaegeuk Kim return 0; 362d3baf95dSJaegeuk Kim 363d3baf95dSJaegeuk Kim skip_write: 364d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 365d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 366d3baf95dSJaegeuk Kim return 0; 367127e670aSJaegeuk Kim } 368127e670aSJaegeuk Kim 3694d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 370b0af6d49SChao Yu long nr_to_write, enum iostat_type io_type) 371127e670aSJaegeuk Kim { 3729df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 373028a63a6SJan Kara pgoff_t index = 0, prev = ULONG_MAX; 374127e670aSJaegeuk Kim struct pagevec pvec; 375127e670aSJaegeuk Kim long nwritten = 0; 376028a63a6SJan Kara int nr_pages; 377127e670aSJaegeuk Kim struct writeback_control wbc = { 378127e670aSJaegeuk Kim .for_reclaim = 0, 379127e670aSJaegeuk Kim }; 380e9f5b8b8SChao Yu struct blk_plug plug; 381127e670aSJaegeuk Kim 38286679820SMel Gorman pagevec_init(&pvec); 383127e670aSJaegeuk Kim 384e9f5b8b8SChao Yu blk_start_plug(&plug); 385e9f5b8b8SChao Yu 386028a63a6SJan Kara while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 38767fd707fSJan Kara PAGECACHE_TAG_DIRTY))) { 388028a63a6SJan Kara int i; 389127e670aSJaegeuk Kim 390127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 391127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 392203681f6SJaegeuk Kim 39380dd9c0eSChao Yu if (prev == ULONG_MAX) 3946066d8cdSJaegeuk Kim prev = page->index - 1; 3956066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 3966066d8cdSJaegeuk Kim pagevec_release(&pvec); 3976066d8cdSJaegeuk Kim goto stop; 3986066d8cdSJaegeuk Kim } 3996066d8cdSJaegeuk Kim 400127e670aSJaegeuk Kim lock_page(page); 401203681f6SJaegeuk Kim 402203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 403203681f6SJaegeuk Kim continue_unlock: 404203681f6SJaegeuk Kim unlock_page(page); 405203681f6SJaegeuk Kim continue; 406203681f6SJaegeuk Kim } 407203681f6SJaegeuk Kim if (!PageDirty(page)) { 408203681f6SJaegeuk Kim /* someone wrote it for us */ 409203681f6SJaegeuk Kim goto continue_unlock; 410203681f6SJaegeuk Kim } 411203681f6SJaegeuk Kim 412bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 413fa3d2bdfSJaegeuk Kim 414203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 415203681f6SJaegeuk Kim goto continue_unlock; 416203681f6SJaegeuk Kim 417b0af6d49SChao Yu if (__f2fs_write_meta_page(page, &wbc, io_type)) { 418577e3495SJaegeuk Kim unlock_page(page); 419577e3495SJaegeuk Kim break; 420577e3495SJaegeuk Kim } 421cfb271d4SChao Yu nwritten++; 4226066d8cdSJaegeuk Kim prev = page->index; 423cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 424127e670aSJaegeuk Kim break; 425127e670aSJaegeuk Kim } 426127e670aSJaegeuk Kim pagevec_release(&pvec); 427127e670aSJaegeuk Kim cond_resched(); 428127e670aSJaegeuk Kim } 4296066d8cdSJaegeuk Kim stop: 430127e670aSJaegeuk Kim if (nwritten) 431b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, type); 432127e670aSJaegeuk Kim 433e9f5b8b8SChao Yu blk_finish_plug(&plug); 434e9f5b8b8SChao Yu 435127e670aSJaegeuk Kim return nwritten; 436127e670aSJaegeuk Kim } 437127e670aSJaegeuk Kim 438127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 439127e670aSJaegeuk Kim { 44026c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 44126c6b887SJaegeuk Kim 442237c0790SJaegeuk Kim if (!PageUptodate(page)) 443127e670aSJaegeuk Kim SetPageUptodate(page); 444127e670aSJaegeuk Kim if (!PageDirty(page)) { 445b87078adSJaegeuk Kim __set_page_dirty_nobuffers(page); 4464081363fSJaegeuk Kim inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); 447240a5915SChao Yu f2fs_set_page_private(page, 0); 448127e670aSJaegeuk Kim return 1; 449127e670aSJaegeuk Kim } 450127e670aSJaegeuk Kim return 0; 451127e670aSJaegeuk Kim } 452127e670aSJaegeuk Kim 453127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 454127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 455127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 456127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 457487261f3SChao Yu .invalidatepage = f2fs_invalidate_page, 458487261f3SChao Yu .releasepage = f2fs_release_page, 4595b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION 4605b7a487cSWeichao Guo .migratepage = f2fs_migrate_page, 4615b7a487cSWeichao Guo #endif 462127e670aSJaegeuk Kim }; 463127e670aSJaegeuk Kim 46439d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, 46539d787beSChao Yu unsigned int devidx, int type) 466953e6cc6SJaegeuk Kim { 46767298804SChao Yu struct inode_management *im = &sbi->im[type]; 46880c54505SJaegeuk Kim struct ino_entry *e, *tmp; 46980c54505SJaegeuk Kim 47080c54505SJaegeuk Kim tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS); 47119526d74SChao Yu 47280c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 473769ec6e5SJaegeuk Kim 47467298804SChao Yu spin_lock(&im->ino_lock); 47567298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 47639efac41SJaegeuk Kim if (!e) { 47780c54505SJaegeuk Kim e = tmp; 47819526d74SChao Yu if (unlikely(radix_tree_insert(&im->ino_root, ino, e))) 47919526d74SChao Yu f2fs_bug_on(sbi, 1); 48019526d74SChao Yu 48139efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 48239efac41SJaegeuk Kim e->ino = ino; 48339efac41SJaegeuk Kim 48467298804SChao Yu list_add_tail(&e->list, &im->ino_list); 4858c402946SJaegeuk Kim if (type != ORPHAN_INO) 48667298804SChao Yu im->ino_num++; 48739efac41SJaegeuk Kim } 48839d787beSChao Yu 48939d787beSChao Yu if (type == FLUSH_INO) 49039d787beSChao Yu f2fs_set_bit(devidx, (char *)&e->dirty_device); 49139d787beSChao Yu 49267298804SChao Yu spin_unlock(&im->ino_lock); 493769ec6e5SJaegeuk Kim radix_tree_preload_end(); 49480c54505SJaegeuk Kim 49580c54505SJaegeuk Kim if (e != tmp) 49680c54505SJaegeuk Kim kmem_cache_free(ino_entry_slab, tmp); 497953e6cc6SJaegeuk Kim } 498953e6cc6SJaegeuk Kim 4996451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 500953e6cc6SJaegeuk Kim { 50167298804SChao Yu struct inode_management *im = &sbi->im[type]; 5026451e041SJaegeuk Kim struct ino_entry *e; 503953e6cc6SJaegeuk Kim 50467298804SChao Yu spin_lock(&im->ino_lock); 50567298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 50639efac41SJaegeuk Kim if (e) { 507953e6cc6SJaegeuk Kim list_del(&e->list); 50867298804SChao Yu radix_tree_delete(&im->ino_root, ino); 50967298804SChao Yu im->ino_num--; 51067298804SChao Yu spin_unlock(&im->ino_lock); 5116451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 512953e6cc6SJaegeuk Kim return; 513953e6cc6SJaegeuk Kim } 51467298804SChao Yu spin_unlock(&im->ino_lock); 515953e6cc6SJaegeuk Kim } 516953e6cc6SJaegeuk Kim 5174d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 518fff04f90SJaegeuk Kim { 519fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 52039d787beSChao Yu __add_ino_entry(sbi, ino, 0, type); 521fff04f90SJaegeuk Kim } 522fff04f90SJaegeuk Kim 5234d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 524fff04f90SJaegeuk Kim { 525fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 526fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 527fff04f90SJaegeuk Kim } 528fff04f90SJaegeuk Kim 5291f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */ 5304d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 531fff04f90SJaegeuk Kim { 53267298804SChao Yu struct inode_management *im = &sbi->im[mode]; 533fff04f90SJaegeuk Kim struct ino_entry *e; 53467298804SChao Yu 53567298804SChao Yu spin_lock(&im->ino_lock); 53667298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 53767298804SChao Yu spin_unlock(&im->ino_lock); 538fff04f90SJaegeuk Kim return e ? true : false; 539fff04f90SJaegeuk Kim } 540fff04f90SJaegeuk Kim 5414d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all) 542fff04f90SJaegeuk Kim { 543fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 544fff04f90SJaegeuk Kim int i; 545fff04f90SJaegeuk Kim 54639d787beSChao Yu for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) { 54767298804SChao Yu struct inode_management *im = &sbi->im[i]; 54867298804SChao Yu 54967298804SChao Yu spin_lock(&im->ino_lock); 55067298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 551fff04f90SJaegeuk Kim list_del(&e->list); 55267298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 553fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 55467298804SChao Yu im->ino_num--; 555fff04f90SJaegeuk Kim } 55667298804SChao Yu spin_unlock(&im->ino_lock); 557fff04f90SJaegeuk Kim } 558fff04f90SJaegeuk Kim } 559fff04f90SJaegeuk Kim 5604d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 56139d787beSChao Yu unsigned int devidx, int type) 56239d787beSChao Yu { 56339d787beSChao Yu __add_ino_entry(sbi, ino, devidx, type); 56439d787beSChao Yu } 56539d787beSChao Yu 5664d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 56739d787beSChao Yu unsigned int devidx, int type) 56839d787beSChao Yu { 56939d787beSChao Yu struct inode_management *im = &sbi->im[type]; 57039d787beSChao Yu struct ino_entry *e; 57139d787beSChao Yu bool is_dirty = false; 57239d787beSChao Yu 57339d787beSChao Yu spin_lock(&im->ino_lock); 57439d787beSChao Yu e = radix_tree_lookup(&im->ino_root, ino); 57539d787beSChao Yu if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device)) 57639d787beSChao Yu is_dirty = true; 57739d787beSChao Yu spin_unlock(&im->ino_lock); 57839d787beSChao Yu return is_dirty; 57939d787beSChao Yu } 58039d787beSChao Yu 5814d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi) 582127e670aSJaegeuk Kim { 58367298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 584127e670aSJaegeuk Kim int err = 0; 585127e670aSJaegeuk Kim 58667298804SChao Yu spin_lock(&im->ino_lock); 587cb78942bSJaegeuk Kim 5881ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) { 589cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock); 590c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_ORPHAN); 591cb78942bSJaegeuk Kim return -ENOSPC; 592cb78942bSJaegeuk Kim } 5937fa750a1SArnd Bergmann 59467298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 595127e670aSJaegeuk Kim err = -ENOSPC; 596cbd56e7dSJaegeuk Kim else 59767298804SChao Yu im->ino_num++; 59867298804SChao Yu spin_unlock(&im->ino_lock); 5990d47c1adSGu Zheng 600127e670aSJaegeuk Kim return err; 601127e670aSJaegeuk Kim } 602127e670aSJaegeuk Kim 6034d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi) 604cbd56e7dSJaegeuk Kim { 60567298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 60667298804SChao Yu 60767298804SChao Yu spin_lock(&im->ino_lock); 60867298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 60967298804SChao Yu im->ino_num--; 61067298804SChao Yu spin_unlock(&im->ino_lock); 611cbd56e7dSJaegeuk Kim } 612cbd56e7dSJaegeuk Kim 6134d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode) 614127e670aSJaegeuk Kim { 61539efac41SJaegeuk Kim /* add new orphan ino entry into list */ 61639d787beSChao Yu __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO); 6174d57b86dSChao Yu f2fs_update_inode_page(inode); 618127e670aSJaegeuk Kim } 619127e670aSJaegeuk Kim 6204d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 621127e670aSJaegeuk Kim { 622953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 6236451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 624127e670aSJaegeuk Kim } 625127e670aSJaegeuk Kim 6268c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 627127e670aSJaegeuk Kim { 6288c14bfadSChao Yu struct inode *inode; 6295905f9afSJaegeuk Kim struct node_info ni; 63076a45e3cSChao Yu int err; 6318c14bfadSChao Yu 6325905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino); 6338c14bfadSChao Yu if (IS_ERR(inode)) { 6348c14bfadSChao Yu /* 6358c14bfadSChao Yu * there should be a bug that we can't find the entry 6368c14bfadSChao Yu * to orphan inode. 6378c14bfadSChao Yu */ 6388c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 6398c14bfadSChao Yu return PTR_ERR(inode); 6408c14bfadSChao Yu } 6418c14bfadSChao Yu 6420f9ec2a8SJaegeuk Kim err = dquot_initialize(inode); 643a515d12fSSheng Yong if (err) { 644a515d12fSSheng Yong iput(inode); 6450f9ec2a8SJaegeuk Kim goto err_out; 646a515d12fSSheng Yong } 6470f9ec2a8SJaegeuk Kim 648127e670aSJaegeuk Kim clear_nlink(inode); 649127e670aSJaegeuk Kim 650127e670aSJaegeuk Kim /* truncate all the data during iput */ 651127e670aSJaegeuk Kim iput(inode); 6525905f9afSJaegeuk Kim 6537735730dSChao Yu err = f2fs_get_node_info(sbi, ino, &ni); 6547735730dSChao Yu if (err) 6557735730dSChao Yu goto err_out; 6565905f9afSJaegeuk Kim 6575905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */ 6585905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) { 6590f9ec2a8SJaegeuk Kim err = -EIO; 6600f9ec2a8SJaegeuk Kim goto err_out; 6615905f9afSJaegeuk Kim } 6628c14bfadSChao Yu return 0; 6630f9ec2a8SJaegeuk Kim 6640f9ec2a8SJaegeuk Kim err_out: 6650f9ec2a8SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 666dcbb4c10SJoe Perches f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.", 6670f9ec2a8SJaegeuk Kim __func__, ino); 6680f9ec2a8SJaegeuk Kim return err; 669127e670aSJaegeuk Kim } 670127e670aSJaegeuk Kim 6714d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) 672127e670aSJaegeuk Kim { 6733c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 6744b2414d0SChao Yu unsigned int s_flags = sbi->sb->s_flags; 6754b2414d0SChao Yu int err = 0; 676ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA 677ea676733SJaegeuk Kim int quota_enabled; 678ea676733SJaegeuk Kim #endif 679127e670aSJaegeuk Kim 680aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 6818c14bfadSChao Yu return 0; 682127e670aSJaegeuk Kim 683b61af314SChao Yu if (bdev_read_only(sbi->sb->s_bdev)) { 684dcbb4c10SJoe Perches f2fs_info(sbi, "write access unavailable, skipping orphan cleanup"); 685b61af314SChao Yu return 0; 686b61af314SChao Yu } 687b61af314SChao Yu 6881751e8a6SLinus Torvalds if (s_flags & SB_RDONLY) { 689dcbb4c10SJoe Perches f2fs_info(sbi, "orphan cleanup on readonly fs"); 6901751e8a6SLinus Torvalds sbi->sb->s_flags &= ~SB_RDONLY; 6914b2414d0SChao Yu } 6924b2414d0SChao Yu 6934b2414d0SChao Yu #ifdef CONFIG_QUOTA 6944b2414d0SChao Yu /* Needed for iput() to work correctly and not trash data */ 6951751e8a6SLinus Torvalds sbi->sb->s_flags |= SB_ACTIVE; 696ea676733SJaegeuk Kim 69776cf05d7SSheng Yong /* 69876cf05d7SSheng Yong * Turn on quotas which were not enabled for read-only mounts if 69976cf05d7SSheng Yong * filesystem has quota feature, so that they are updated correctly. 70076cf05d7SSheng Yong */ 7011751e8a6SLinus Torvalds quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY); 7024b2414d0SChao Yu #endif 7034b2414d0SChao Yu 70455141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 7053c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 706127e670aSJaegeuk Kim 7074d57b86dSChao Yu f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 708662befdaSChao Yu 7093c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 7107735730dSChao Yu struct page *page; 711127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 712127e670aSJaegeuk Kim 7137735730dSChao Yu page = f2fs_get_meta_page(sbi, start_blk + i); 7147735730dSChao Yu if (IS_ERR(page)) { 7157735730dSChao Yu err = PTR_ERR(page); 7167735730dSChao Yu goto out; 7177735730dSChao Yu } 7187735730dSChao Yu 719127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 720127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 721127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 7228c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 7238c14bfadSChao Yu if (err) { 7248c14bfadSChao Yu f2fs_put_page(page, 1); 7254b2414d0SChao Yu goto out; 7268c14bfadSChao Yu } 727127e670aSJaegeuk Kim } 728127e670aSJaegeuk Kim f2fs_put_page(page, 1); 729127e670aSJaegeuk Kim } 730127e670aSJaegeuk Kim /* clear Orphan Flag */ 731aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 7324b2414d0SChao Yu out: 7331378752bSChao Yu set_sbi_flag(sbi, SBI_IS_RECOVERED); 7341378752bSChao Yu 7354b2414d0SChao Yu #ifdef CONFIG_QUOTA 7364b2414d0SChao Yu /* Turn quotas off */ 737ea676733SJaegeuk Kim if (quota_enabled) 7384b2414d0SChao Yu f2fs_quota_off_umount(sbi->sb); 7394b2414d0SChao Yu #endif 7401751e8a6SLinus Torvalds sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 7414b2414d0SChao Yu 7424b2414d0SChao Yu return err; 743127e670aSJaegeuk Kim } 744127e670aSJaegeuk Kim 745127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 746127e670aSJaegeuk Kim { 747502c6e0bSGu Zheng struct list_head *head; 748127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 749127e670aSJaegeuk Kim unsigned int nentries = 0; 750bd936f84SChao Yu unsigned short index = 1; 7518c402946SJaegeuk Kim unsigned short orphan_blocks; 7524531929eSGu Zheng struct page *page = NULL; 7536451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 75467298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 755127e670aSJaegeuk Kim 75667298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 7578c402946SJaegeuk Kim 758d6c67a4fSJaegeuk Kim /* 759d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 760d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 761d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 762d6c67a4fSJaegeuk Kim */ 76367298804SChao Yu head = &im->ino_list; 764127e670aSJaegeuk Kim 765127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 766502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 767502c6e0bSGu Zheng if (!page) { 7684d57b86dSChao Yu page = f2fs_grab_meta_page(sbi, start_blk++); 769502c6e0bSGu Zheng orphan_blk = 770502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 771502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 772502c6e0bSGu Zheng } 773127e670aSJaegeuk Kim 77436795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 775127e670aSJaegeuk Kim 77636795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 777127e670aSJaegeuk Kim /* 778127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 779127e670aSJaegeuk Kim * then we need to flush current orphan blocks 780127e670aSJaegeuk Kim * and bring another one in memory 781127e670aSJaegeuk Kim */ 782127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 783127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 784127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 785127e670aSJaegeuk Kim set_page_dirty(page); 786127e670aSJaegeuk Kim f2fs_put_page(page, 1); 787127e670aSJaegeuk Kim index++; 788127e670aSJaegeuk Kim nentries = 0; 789127e670aSJaegeuk Kim page = NULL; 790127e670aSJaegeuk Kim } 791127e670aSJaegeuk Kim } 792127e670aSJaegeuk Kim 793502c6e0bSGu Zheng if (page) { 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); 799502c6e0bSGu Zheng } 800127e670aSJaegeuk Kim } 801127e670aSJaegeuk Kim 802d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi, 803d7eb8f1cSChao Yu struct f2fs_checkpoint *ckpt) 804d7eb8f1cSChao Yu { 805d7eb8f1cSChao Yu unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset); 806d7eb8f1cSChao Yu __u32 chksum; 807d7eb8f1cSChao Yu 808d7eb8f1cSChao Yu chksum = f2fs_crc32(sbi, ckpt, chksum_ofs); 809d7eb8f1cSChao Yu if (chksum_ofs < CP_CHKSUM_OFFSET) { 810d7eb8f1cSChao Yu chksum_ofs += sizeof(chksum); 811d7eb8f1cSChao Yu chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs, 812d7eb8f1cSChao Yu F2FS_BLKSIZE - chksum_ofs); 813d7eb8f1cSChao Yu } 814d7eb8f1cSChao Yu return chksum; 815d7eb8f1cSChao Yu } 816d7eb8f1cSChao Yu 817fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 818fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page, 819fc0065adSTiezhu Yang unsigned long long *version) 820fc0065adSTiezhu Yang { 821fc0065adSTiezhu Yang size_t crc_offset = 0; 822d7eb8f1cSChao Yu __u32 crc; 823fc0065adSTiezhu Yang 8244d57b86dSChao Yu *cp_page = f2fs_get_meta_page(sbi, cp_addr); 8257735730dSChao Yu if (IS_ERR(*cp_page)) 8267735730dSChao Yu return PTR_ERR(*cp_page); 8277735730dSChao Yu 828fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 829fc0065adSTiezhu Yang 830fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 831d7eb8f1cSChao Yu if (crc_offset < CP_MIN_CHKSUM_OFFSET || 832d7eb8f1cSChao Yu crc_offset > CP_CHKSUM_OFFSET) { 833d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 834dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset); 835fc0065adSTiezhu Yang return -EINVAL; 836fc0065adSTiezhu Yang } 837fc0065adSTiezhu Yang 838d7eb8f1cSChao Yu crc = f2fs_checkpoint_chksum(sbi, *cp_block); 839d7eb8f1cSChao Yu if (crc != cur_cp_crc(*cp_block)) { 840d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 841dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc value"); 842fc0065adSTiezhu Yang return -EINVAL; 843fc0065adSTiezhu Yang } 844fc0065adSTiezhu Yang 845fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block); 846fc0065adSTiezhu Yang return 0; 847fc0065adSTiezhu Yang } 848fc0065adSTiezhu Yang 849127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 850127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 851127e670aSJaegeuk Kim { 852fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 853fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL; 854127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 855fc0065adSTiezhu Yang int err; 856127e670aSJaegeuk Kim 857fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 858fc0065adSTiezhu Yang &cp_page_1, version); 859fc0065adSTiezhu Yang if (err) 860d3f07c04SChao Yu return NULL; 861c9b60788SChao Yu 862c9b60788SChao Yu if (le32_to_cpu(cp_block->cp_pack_total_block_count) > 863c9b60788SChao Yu sbi->blocks_per_seg) { 864dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", 865c9b60788SChao Yu le32_to_cpu(cp_block->cp_pack_total_block_count)); 866d3f07c04SChao Yu goto invalid_cp; 867c9b60788SChao Yu } 868fc0065adSTiezhu Yang pre_version = *version; 869127e670aSJaegeuk Kim 87025ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 871fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 872fc0065adSTiezhu Yang &cp_page_2, version); 873fc0065adSTiezhu Yang if (err) 874d3f07c04SChao Yu goto invalid_cp; 875fc0065adSTiezhu Yang cur_version = *version; 876127e670aSJaegeuk Kim 877127e670aSJaegeuk Kim if (cur_version == pre_version) { 878127e670aSJaegeuk Kim *version = cur_version; 879127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 880127e670aSJaegeuk Kim return cp_page_1; 881127e670aSJaegeuk Kim } 882127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 883d3f07c04SChao Yu invalid_cp: 884127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 885127e670aSJaegeuk Kim return NULL; 886127e670aSJaegeuk Kim } 887127e670aSJaegeuk Kim 8884d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi) 889127e670aSJaegeuk Kim { 890127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 891127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 892127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 893127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 894127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 895127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 89655141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 8971dbe4152SChangman Lee block_t cp_blk_no; 8981dbe4152SChangman Lee int i; 89910f966bbSChao Yu int err; 900127e670aSJaegeuk Kim 9010b6d4ca0SEric Biggers sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks), 902026f0507SKees Cook GFP_KERNEL); 903127e670aSJaegeuk Kim if (!sbi->ckpt) 904127e670aSJaegeuk Kim return -ENOMEM; 905127e670aSJaegeuk Kim /* 906127e670aSJaegeuk Kim * Finding out valid cp block involves read both 907127e670aSJaegeuk Kim * sets( cp pack 1 and cp pack 2) 908127e670aSJaegeuk Kim */ 909127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 910127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 911127e670aSJaegeuk Kim 912127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 913f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 914f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 915127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 916127e670aSJaegeuk Kim 917127e670aSJaegeuk Kim if (cp1 && cp2) { 918127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 919127e670aSJaegeuk Kim cur_page = cp2; 920127e670aSJaegeuk Kim else 921127e670aSJaegeuk Kim cur_page = cp1; 922127e670aSJaegeuk Kim } else if (cp1) { 923127e670aSJaegeuk Kim cur_page = cp1; 924127e670aSJaegeuk Kim } else if (cp2) { 925127e670aSJaegeuk Kim cur_page = cp2; 926127e670aSJaegeuk Kim } else { 92710f966bbSChao Yu err = -EFSCORRUPTED; 928127e670aSJaegeuk Kim goto fail_no_cp; 929127e670aSJaegeuk Kim } 930127e670aSJaegeuk Kim 931127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 932127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 933127e670aSJaegeuk Kim 9348508e44aSJaegeuk Kim if (cur_page == cp1) 9358508e44aSJaegeuk Kim sbi->cur_cp_pack = 1; 9368508e44aSJaegeuk Kim else 9378508e44aSJaegeuk Kim sbi->cur_cp_pack = 2; 938984ec63cSShawn Lin 939e494c2f9SChao Yu /* Sanity checking of checkpoint */ 94010f966bbSChao Yu if (f2fs_sanity_check_ckpt(sbi)) { 94110f966bbSChao Yu err = -EFSCORRUPTED; 942e494c2f9SChao Yu goto free_fail_no_cp; 94310f966bbSChao Yu } 944e494c2f9SChao Yu 9451dbe4152SChangman Lee if (cp_blks <= 1) 9461dbe4152SChangman Lee goto done; 9471dbe4152SChangman Lee 9481dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 9491dbe4152SChangman Lee if (cur_page == cp2) 9501dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 9511dbe4152SChangman Lee 9521dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 9531dbe4152SChangman Lee void *sit_bitmap_ptr; 9541dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 9551dbe4152SChangman Lee 9564d57b86dSChao Yu cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i); 95710f966bbSChao Yu if (IS_ERR(cur_page)) { 95810f966bbSChao Yu err = PTR_ERR(cur_page); 9597735730dSChao Yu goto free_fail_no_cp; 96010f966bbSChao Yu } 9611dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 9621dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 9631dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 9641dbe4152SChangman Lee } 9651dbe4152SChangman Lee done: 966127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 967127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 968127e670aSJaegeuk Kim return 0; 969127e670aSJaegeuk Kim 970a2125ff7SJaegeuk Kim free_fail_no_cp: 971a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1); 972a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1); 973127e670aSJaegeuk Kim fail_no_cp: 9745222595dSJaegeuk Kim kvfree(sbi->ckpt); 97510f966bbSChao Yu return err; 976127e670aSJaegeuk Kim } 977127e670aSJaegeuk Kim 978c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 979127e670aSJaegeuk Kim { 9804081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 981c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 9822d7b822aSChao Yu 98391942321SJaegeuk Kim if (is_inode_flag_set(inode, flag)) 9842710fd7eSChao Yu return; 9852d7b822aSChao Yu 98691942321SJaegeuk Kim set_inode_flag(inode, flag); 98799f4b917SChao Yu if (!f2fs_is_volatile_file(inode)) 98899f4b917SChao Yu list_add_tail(&F2FS_I(inode)->dirty_list, 98999f4b917SChao Yu &sbi->inode_list[type]); 99033fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 9915deb8267SJaegeuk Kim } 9925deb8267SJaegeuk Kim 993c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 9946ad7609aSChao Yu { 995c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 9966ad7609aSChao Yu 99791942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 9986ad7609aSChao Yu return; 9996ad7609aSChao Yu 100091942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list); 100191942321SJaegeuk Kim clear_inode_flag(inode, flag); 100233fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 10036ad7609aSChao Yu } 10046ad7609aSChao Yu 10054d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page) 10065deb8267SJaegeuk Kim { 10074081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1008c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 10095deb8267SJaegeuk Kim 10105ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 10115ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 1012127e670aSJaegeuk Kim return; 10137bd59381SGu Zheng 1014c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 10151c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 1016c227f912SChao Yu __add_dirty_inode(inode, type); 1017b951a4ecSYunlei He inode_inc_dirty_pages(inode); 10181c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]); 10191c4bf763SJaegeuk Kim 1020240a5915SChao Yu f2fs_set_page_private(page, 0); 10215deb8267SJaegeuk Kim } 1022127e670aSJaegeuk Kim 10234d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode) 1024127e670aSJaegeuk Kim { 10254081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1026c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 1027127e670aSJaegeuk Kim 1028c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 1029c227f912SChao Yu !S_ISLNK(inode->i_mode)) 1030127e670aSJaegeuk Kim return; 1031127e670aSJaegeuk Kim 103210aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 103310aa97c3SJaegeuk Kim return; 103410aa97c3SJaegeuk Kim 1035c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1036c227f912SChao Yu __remove_dirty_inode(inode, type); 1037c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1038afc3eda2SJaegeuk Kim } 103974d0b917SJaegeuk Kim 10404d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 1041127e670aSJaegeuk Kim { 1042ce3b7d80SGu Zheng struct list_head *head; 1043127e670aSJaegeuk Kim struct inode *inode; 10442710fd7eSChao Yu struct f2fs_inode_info *fi; 10454cf18537SChao Yu bool is_dir = (type == DIR_INODE); 10464db08d01SJaegeuk Kim unsigned long ino = 0; 10474cf18537SChao Yu 10484cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 10494cf18537SChao Yu get_pages(sbi, is_dir ? 10504cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1051127e670aSJaegeuk Kim retry: 10529b664822SZhang Qilong if (unlikely(f2fs_cp_error(sbi))) { 10539b664822SZhang Qilong trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10549b664822SZhang Qilong get_pages(sbi, is_dir ? 10559b664822SZhang Qilong F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10566d5a1495SChao Yu return -EIO; 10579b664822SZhang Qilong } 1058af41d3eeSJaegeuk Kim 1059c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1060ce3b7d80SGu Zheng 1061c227f912SChao Yu head = &sbi->inode_list[type]; 1062127e670aSJaegeuk Kim if (list_empty(head)) { 1063c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 10644cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10654cf18537SChao Yu get_pages(sbi, is_dir ? 10664cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10676d5a1495SChao Yu return 0; 1068127e670aSJaegeuk Kim } 1069939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 10702710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 1071c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1072127e670aSJaegeuk Kim if (inode) { 10734db08d01SJaegeuk Kim unsigned long cur_ino = inode->i_ino; 10744db08d01SJaegeuk Kim 1075b0af6d49SChao Yu F2FS_I(inode)->cp_task = current; 1076b0af6d49SChao Yu 107787d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 1078b0af6d49SChao Yu 1079b0af6d49SChao Yu F2FS_I(inode)->cp_task = NULL; 1080b0af6d49SChao Yu 1081127e670aSJaegeuk Kim iput(inode); 10824db08d01SJaegeuk Kim /* We need to give cpu to another writers. */ 10832a63531aSYunlei He if (ino == cur_ino) 10844db08d01SJaegeuk Kim cond_resched(); 10852a63531aSYunlei He else 10864db08d01SJaegeuk Kim ino = cur_ino; 1087127e670aSJaegeuk Kim } else { 1088127e670aSJaegeuk Kim /* 1089127e670aSJaegeuk Kim * We should submit bio, since it exists several 1090127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 1091127e670aSJaegeuk Kim */ 1092b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 10937ecebe5eSSebastian Andrzej Siewior cond_resched(); 1094127e670aSJaegeuk Kim } 1095127e670aSJaegeuk Kim goto retry; 1096127e670aSJaegeuk Kim } 1097127e670aSJaegeuk Kim 10980f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 10990f18b462SJaegeuk Kim { 11000f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META]; 11010f18b462SJaegeuk Kim struct inode *inode; 11020f18b462SJaegeuk Kim struct f2fs_inode_info *fi; 11030f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 11040f18b462SJaegeuk Kim 11050f18b462SJaegeuk Kim while (total--) { 11060f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 11070f18b462SJaegeuk Kim return -EIO; 11080f18b462SJaegeuk Kim 11090f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 11100f18b462SJaegeuk Kim if (list_empty(head)) { 11110f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11120f18b462SJaegeuk Kim return 0; 11130f18b462SJaegeuk Kim } 1114939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, 11150f18b462SJaegeuk Kim gdirty_list); 11160f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode); 11170f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11180f18b462SJaegeuk Kim if (inode) { 111918340edcSJaegeuk Kim sync_inode_metadata(inode, 0); 112018340edcSJaegeuk Kim 112118340edcSJaegeuk Kim /* it's on eviction */ 112218340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 11234d57b86dSChao Yu f2fs_update_inode_page(inode); 11240f18b462SJaegeuk Kim iput(inode); 11250f18b462SJaegeuk Kim } 1126dee668c1SChao Yu } 11270f18b462SJaegeuk Kim return 0; 11280f18b462SJaegeuk Kim } 11290f18b462SJaegeuk Kim 113059c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi) 113159c9081bSYunlei He { 113259c9081bSYunlei He struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 113359c9081bSYunlei He struct f2fs_nm_info *nm_i = NM_I(sbi); 113459c9081bSYunlei He nid_t last_nid = nm_i->next_scan_nid; 113559c9081bSYunlei He 113659c9081bSYunlei He next_free_nid(sbi, &last_nid); 113759c9081bSYunlei He ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 113859c9081bSYunlei He ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 113959c9081bSYunlei He ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 114059c9081bSYunlei He ckpt->next_free_nid = cpu_to_le32(last_nid); 114159c9081bSYunlei He } 114259c9081bSYunlei He 1143af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi) 1144af033b2aSChao Yu { 1145db6ec53bSJaegeuk Kim bool ret = false; 1146db6ec53bSJaegeuk Kim 1147af033b2aSChao Yu if (!is_journalled_quota(sbi)) 1148af033b2aSChao Yu return false; 1149db6ec53bSJaegeuk Kim 1150db6ec53bSJaegeuk Kim down_write(&sbi->quota_sem); 1151db6ec53bSJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { 1152db6ec53bSJaegeuk Kim ret = false; 1153db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { 1154db6ec53bSJaegeuk Kim ret = false; 1155db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) { 1156db6ec53bSJaegeuk Kim clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1157db6ec53bSJaegeuk Kim ret = true; 1158db6ec53bSJaegeuk Kim } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { 1159db6ec53bSJaegeuk Kim ret = true; 1160db6ec53bSJaegeuk Kim } 1161db6ec53bSJaegeuk Kim up_write(&sbi->quota_sem); 1162db6ec53bSJaegeuk Kim return ret; 1163af033b2aSChao Yu } 1164af033b2aSChao Yu 11650a8165d7SJaegeuk Kim /* 1166127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 1167127e670aSJaegeuk Kim */ 1168cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 1169127e670aSJaegeuk Kim { 1170127e670aSJaegeuk Kim struct writeback_control wbc = { 1171127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 1172127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 1173127e670aSJaegeuk Kim .for_reclaim = 0, 1174127e670aSJaegeuk Kim }; 1175af033b2aSChao Yu int err = 0, cnt = 0; 1176c718379bSJaegeuk Kim 117734c061adSSayali Lokhande /* 117834c061adSSayali Lokhande * Let's flush inline_data in dirty node pages. 117934c061adSSayali Lokhande */ 118034c061adSSayali Lokhande f2fs_flush_inline_data(sbi); 118134c061adSSayali Lokhande 1182af033b2aSChao Yu retry_flush_quotas: 1183db6ec53bSJaegeuk Kim f2fs_lock_all(sbi); 1184af033b2aSChao Yu if (__need_flush_quota(sbi)) { 1185af033b2aSChao Yu int locked; 1186af033b2aSChao Yu 1187af033b2aSChao Yu if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { 1188af033b2aSChao Yu set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1189db6ec53bSJaegeuk Kim set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1190af033b2aSChao Yu goto retry_flush_dents; 1191af033b2aSChao Yu } 1192db6ec53bSJaegeuk Kim f2fs_unlock_all(sbi); 1193af033b2aSChao Yu 1194af033b2aSChao Yu /* only failed during mount/umount/freeze/quotactl */ 1195af033b2aSChao Yu locked = down_read_trylock(&sbi->sb->s_umount); 1196af033b2aSChao Yu f2fs_quota_sync(sbi->sb, -1); 1197af033b2aSChao Yu if (locked) 1198af033b2aSChao Yu up_read(&sbi->sb->s_umount); 1199af033b2aSChao Yu cond_resched(); 1200af033b2aSChao Yu goto retry_flush_quotas; 1201af033b2aSChao Yu } 1202af033b2aSChao Yu 1203af033b2aSChao Yu retry_flush_dents: 1204127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 1205127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 1206e479556bSGu Zheng f2fs_unlock_all(sbi); 12074d57b86dSChao Yu err = f2fs_sync_dirty_inodes(sbi, DIR_INODE); 12086d5a1495SChao Yu if (err) 12091f5f11a3SJaegeuk Kim return err; 121030973883SJaegeuk Kim cond_resched(); 1211af033b2aSChao Yu goto retry_flush_quotas; 1212127e670aSJaegeuk Kim } 1213127e670aSJaegeuk Kim 121459c9081bSYunlei He /* 121559c9081bSYunlei He * POR: we should ensure that there are no dirty node pages 121659c9081bSYunlei He * until finishing nat/sit flush. inode->i_blocks can be updated. 121759c9081bSYunlei He */ 121859c9081bSYunlei He down_write(&sbi->node_change); 121959c9081bSYunlei He 12200f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 122159c9081bSYunlei He up_write(&sbi->node_change); 12220f18b462SJaegeuk Kim f2fs_unlock_all(sbi); 12230f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi); 12240f18b462SJaegeuk Kim if (err) 12251f5f11a3SJaegeuk Kim return err; 122630973883SJaegeuk Kim cond_resched(); 1227af033b2aSChao Yu goto retry_flush_quotas; 12280f18b462SJaegeuk Kim } 12290f18b462SJaegeuk Kim 123039936837SJaegeuk Kim retry_flush_nodes: 1231b3582c68SChao Yu down_write(&sbi->node_write); 1232127e670aSJaegeuk Kim 1233127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1234b3582c68SChao Yu up_write(&sbi->node_write); 1235c29fd0c0SChao Yu atomic_inc(&sbi->wb_sync_req[NODE]); 12364d57b86dSChao Yu err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); 1237c29fd0c0SChao Yu atomic_dec(&sbi->wb_sync_req[NODE]); 12386d5a1495SChao Yu if (err) { 123959c9081bSYunlei He up_write(&sbi->node_change); 1240cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 12411f5f11a3SJaegeuk Kim return err; 1242cf779cabSJaegeuk Kim } 124330973883SJaegeuk Kim cond_resched(); 124439936837SJaegeuk Kim goto retry_flush_nodes; 1245127e670aSJaegeuk Kim } 124659c9081bSYunlei He 124759c9081bSYunlei He /* 124859c9081bSYunlei He * sbi->node_change is used only for AIO write_begin path which produces 124959c9081bSYunlei He * dirty node blocks and some checkpoint values by block allocation. 125059c9081bSYunlei He */ 125159c9081bSYunlei He __prepare_cp_block(sbi); 125259c9081bSYunlei He up_write(&sbi->node_change); 1253cf779cabSJaegeuk Kim return err; 1254127e670aSJaegeuk Kim } 1255127e670aSJaegeuk Kim 1256127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 1257127e670aSJaegeuk Kim { 1258b3582c68SChao Yu up_write(&sbi->node_write); 1259e479556bSGu Zheng f2fs_unlock_all(sbi); 1260127e670aSJaegeuk Kim } 1261127e670aSJaegeuk Kim 1262bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type) 1263fb51b5efSChangman Lee { 1264fb51b5efSChangman Lee DEFINE_WAIT(wait); 1265fb51b5efSChangman Lee 1266fb51b5efSChangman Lee for (;;) { 1267bf22c3ccSSahitya Tummala if (!get_pages(sbi, type)) 1268fb51b5efSChangman Lee break; 1269fb51b5efSChangman Lee 1270af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1271af697c0fSJaegeuk Kim break; 1272af697c0fSJaegeuk Kim 12739c30df7cSJaegeuk Kim if (type == F2FS_DIRTY_META) 12749c30df7cSJaegeuk Kim f2fs_sync_meta_pages(sbi, META, LONG_MAX, 12759c30df7cSJaegeuk Kim FS_CP_META_IO); 12761fd28018SJaegeuk Kim else if (type == F2FS_WB_CP_DATA) 12771fd28018SJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 1278828add77SJaegeuk Kim 1279828add77SJaegeuk Kim prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 12805df7731fSChao Yu io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1281fb51b5efSChangman Lee } 1282fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 1283fb51b5efSChangman Lee } 1284fb51b5efSChangman Lee 1285e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1286e4c5d848SJaegeuk Kim { 1287e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1288e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1289d1aa2453SChao Yu unsigned long flags; 1290e4c5d848SJaegeuk Kim 1291d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1292e4c5d848SJaegeuk Kim 1293c473f1a9SChao Yu if ((cpc->reason & CP_UMOUNT) && 129410047f53SKinglong Mee le32_to_cpu(ckpt->cp_pack_total_block_count) > 129522ad0b6aSJaegeuk Kim sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks) 129622ad0b6aSJaegeuk Kim disable_nat_bits(sbi, false); 129722ad0b6aSJaegeuk Kim 12981f43e2adSChao Yu if (cpc->reason & CP_TRIMMED) 12991f43e2adSChao Yu __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 1300cd36d7a1SChao Yu else 1301cd36d7a1SChao Yu __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 13021f43e2adSChao Yu 1303c473f1a9SChao Yu if (cpc->reason & CP_UMOUNT) 1304e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1305e4c5d848SJaegeuk Kim else 1306e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1307e4c5d848SJaegeuk Kim 1308c473f1a9SChao Yu if (cpc->reason & CP_FASTBOOT) 1309e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1310e4c5d848SJaegeuk Kim else 1311e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1312e4c5d848SJaegeuk Kim 1313e4c5d848SJaegeuk Kim if (orphan_num) 1314e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1315e4c5d848SJaegeuk Kim else 1316e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1317e4c5d848SJaegeuk Kim 1318c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1319e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1320e4c5d848SJaegeuk Kim 1321c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS)) 1322c84ef3c5SSahitya Tummala __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1323c84ef3c5SSahitya Tummala else 1324c84ef3c5SSahitya Tummala __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1325c84ef3c5SSahitya Tummala 13264354994fSDaniel Rosenberg if (is_sbi_flag_set(sbi, SBI_CP_DISABLED)) 13274354994fSDaniel Rosenberg __set_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13284354994fSDaniel Rosenberg else 13294354994fSDaniel Rosenberg __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13304354994fSDaniel Rosenberg 1331db610a64SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK)) 1332db610a64SJaegeuk Kim __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1333db610a64SJaegeuk Kim else 1334db610a64SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1335db610a64SJaegeuk Kim 1336af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) 1337af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1338bc88ac96SJaegeuk Kim else 1339bc88ac96SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1340af033b2aSChao Yu 1341af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) 1342af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1343af033b2aSChao Yu 1344e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */ 1345e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1346f2367923SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); 1347e4c5d848SJaegeuk Kim 1348d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1349e4c5d848SJaegeuk Kim } 1350e4c5d848SJaegeuk Kim 135146706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi, 135246706d59SGao Xiang void *src, block_t blk_addr) 135346706d59SGao Xiang { 135446706d59SGao Xiang struct writeback_control wbc = { 135546706d59SGao Xiang .for_reclaim = 0, 135646706d59SGao Xiang }; 135746706d59SGao Xiang 135846706d59SGao Xiang /* 135946706d59SGao Xiang * pagevec_lookup_tag and lock_page again will take 13604d57b86dSChao Yu * some extra time. Therefore, f2fs_update_meta_pages and 13614d57b86dSChao Yu * f2fs_sync_meta_pages are combined in this function. 136246706d59SGao Xiang */ 13634d57b86dSChao Yu struct page *page = f2fs_grab_meta_page(sbi, blk_addr); 136446706d59SGao Xiang int err; 136546706d59SGao Xiang 1366bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 13678d64d365SChao Yu 13688d64d365SChao Yu memcpy(page_address(page), src, PAGE_SIZE); 13698d64d365SChao Yu 13708d64d365SChao Yu set_page_dirty(page); 137146706d59SGao Xiang if (unlikely(!clear_page_dirty_for_io(page))) 137246706d59SGao Xiang f2fs_bug_on(sbi, 1); 137346706d59SGao Xiang 137446706d59SGao Xiang /* writeout cp pack 2 page */ 137546706d59SGao Xiang err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO); 1376af697c0fSJaegeuk Kim if (unlikely(err && f2fs_cp_error(sbi))) { 1377af697c0fSJaegeuk Kim f2fs_put_page(page, 1); 1378af697c0fSJaegeuk Kim return; 1379af697c0fSJaegeuk Kim } 138046706d59SGao Xiang 1381af697c0fSJaegeuk Kim f2fs_bug_on(sbi, err); 138246706d59SGao Xiang f2fs_put_page(page, 0); 138346706d59SGao Xiang 138446706d59SGao Xiang /* submit checkpoint (with barrier if NOBARRIER is not set) */ 138546706d59SGao Xiang f2fs_submit_merged_write(sbi, META_FLUSH); 138646706d59SGao Xiang } 138746706d59SGao Xiang 13883a0a9cbcSChao Yu static inline u64 get_sectors_written(struct block_device *bdev) 13893a0a9cbcSChao Yu { 1390ff49c86fSLinus Torvalds return (u64)part_stat_read(bdev, sectors[STAT_WRITE]); 13913a0a9cbcSChao Yu } 13923a0a9cbcSChao Yu 13933a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi) 13943a0a9cbcSChao Yu { 13953a0a9cbcSChao Yu if (f2fs_is_multi_device(sbi)) { 13963a0a9cbcSChao Yu u64 sectors = 0; 13973a0a9cbcSChao Yu int i; 13983a0a9cbcSChao Yu 13993a0a9cbcSChao Yu for (i = 0; i < sbi->s_ndevs; i++) 14003a0a9cbcSChao Yu sectors += get_sectors_written(FDEV(i).bdev); 14013a0a9cbcSChao Yu 14023a0a9cbcSChao Yu return sectors; 14033a0a9cbcSChao Yu } 14043a0a9cbcSChao Yu 14053a0a9cbcSChao Yu return get_sectors_written(sbi->sb->s_bdev); 14063a0a9cbcSChao Yu } 14073a0a9cbcSChao Yu 1408c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1409127e670aSJaegeuk Kim { 1410127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 141177041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 1412d1aa2453SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags; 1413127e670aSJaegeuk Kim block_t start_blk; 1414127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 14157e586fa0SJaegeuk Kim __u32 crc32 = 0; 1416127e670aSJaegeuk Kim int i; 141755141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 14188f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 14198f1dbbbbSShuoran Liu u64 kbytes_written; 14201228b482SChao Yu int err; 1421127e670aSJaegeuk Kim 1422127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 14234d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1424127e670aSJaegeuk Kim 14257a88ddb5SChao Yu /* start to update checkpoint, cp ver is already updated previously */ 1426a1f72ac2SChao Yu ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true)); 1427127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1428b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1429127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 1430127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 1431127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 1432127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 1433127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 1434127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 1435127e670aSJaegeuk Kim } 1436b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1437127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 1438127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 1439127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 1440127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 1441127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 1442127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 1443127e670aSJaegeuk Kim } 1444127e670aSJaegeuk Kim 1445127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 14464d57b86dSChao Yu data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); 1447d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1448b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1449aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1450127e670aSJaegeuk Kim else 1451aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1452d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1453127e670aSJaegeuk Kim 145467298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 14551dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 14561dbe4152SChangman Lee orphan_blocks); 1457127e670aSJaegeuk Kim 1458119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1459b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 14601dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14611dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1462119ee914SJaegeuk Kim else 1463b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 14641dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14651dbe4152SChangman Lee orphan_blocks); 1466119ee914SJaegeuk Kim 1467e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */ 1468e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc); 1469a468f0efSJaegeuk Kim 1470127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1471127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1472127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1473127e670aSJaegeuk Kim 1474d7eb8f1cSChao Yu crc32 = f2fs_checkpoint_chksum(sbi, ckpt); 14757e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 14767e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1477127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1478127e670aSJaegeuk Kim 14798508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi); 1480127e670aSJaegeuk Kim 148122ad0b6aSJaegeuk Kim /* write nat bits */ 148222ad0b6aSJaegeuk Kim if (enabled_nat_bits(sbi, cpc)) { 148322ad0b6aSJaegeuk Kim __u64 cp_ver = cur_cp_version(ckpt); 148422ad0b6aSJaegeuk Kim block_t blk; 148522ad0b6aSJaegeuk Kim 148622ad0b6aSJaegeuk Kim cp_ver |= ((__u64)crc32 << 32); 148722ad0b6aSJaegeuk Kim *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); 148822ad0b6aSJaegeuk Kim 148922ad0b6aSJaegeuk Kim blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks; 149022ad0b6aSJaegeuk Kim for (i = 0; i < nm_i->nat_bits_blocks; i++) 14914d57b86dSChao Yu f2fs_update_meta_page(sbi, nm_i->nat_bits + 149222ad0b6aSJaegeuk Kim (i << F2FS_BLKSIZE_BITS), blk + i); 149322ad0b6aSJaegeuk Kim } 149422ad0b6aSJaegeuk Kim 1495127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 14964d57b86dSChao Yu f2fs_update_meta_page(sbi, ckpt, start_blk++); 1497127e670aSJaegeuk Kim 1498381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 14994d57b86dSChao Yu f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1500381722d2SChao Yu start_blk++); 15011dbe4152SChangman Lee 150267298804SChao Yu if (orphan_num) { 1503127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1504127e670aSJaegeuk Kim start_blk += orphan_blocks; 1505127e670aSJaegeuk Kim } 1506127e670aSJaegeuk Kim 15074d57b86dSChao Yu f2fs_write_data_summaries(sbi, start_blk); 1508127e670aSJaegeuk Kim start_blk += data_sum_blocks; 15098f1dbbbbSShuoran Liu 15108f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 15118f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 15123a0a9cbcSChao Yu kbytes_written += (f2fs_get_sectors_written(sbi) - 15133a0a9cbcSChao Yu sbi->sectors_written_start) >> 1; 1514b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 15158f1dbbbbSShuoran Liu 1516119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 15174d57b86dSChao Yu f2fs_write_node_summaries(sbi, start_blk); 1518127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1519127e670aSJaegeuk Kim } 1520127e670aSJaegeuk Kim 152146706d59SGao Xiang /* update user_block_counts */ 152246706d59SGao Xiang sbi->last_valid_block_count = sbi->total_valid_block_count; 152346706d59SGao Xiang percpu_counter_set(&sbi->alloc_valid_block_count, 0); 1524127e670aSJaegeuk Kim 152546706d59SGao Xiang /* Here, we have one bio having CP pack except cp pack 2 page */ 15264d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1527bf22c3ccSSahitya Tummala /* Wait for all dirty meta pages to be submitted for IO */ 1528bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META); 152946706d59SGao Xiang 153046706d59SGao Xiang /* wait for previous submitted meta pages writeback */ 1531bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1532127e670aSJaegeuk Kim 153346706d59SGao Xiang /* flush all device cache */ 153446706d59SGao Xiang err = f2fs_flush_device_cache(sbi); 153546706d59SGao Xiang if (err) 153646706d59SGao Xiang return err; 1537127e670aSJaegeuk Kim 153846706d59SGao Xiang /* barrier and flush checkpoint cp pack 2 page if it can */ 153946706d59SGao Xiang commit_checkpoint(sbi, ckpt, start_blk); 1540bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 15416a8f8ca5SJaegeuk Kim 154218767e62SChao Yu /* 1543542989b6SEric Biggers * invalidate intermediate page cache borrowed from meta inode which are 1544aff6fbbeSChao Yu * used for migration of encrypted, verity or compressed inode's blocks. 154518767e62SChao Yu */ 1546aff6fbbeSChao Yu if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) || 1547aff6fbbeSChao Yu f2fs_sb_has_compression(sbi)) 154818767e62SChao Yu invalidate_mapping_pages(META_MAPPING(sbi), 154918767e62SChao Yu MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1); 155018767e62SChao Yu 15514d57b86dSChao Yu f2fs_release_ino_entry(sbi, false); 1552cf779cabSJaegeuk Kim 155350fa53ecSChao Yu f2fs_reset_fsync_node_info(sbi); 155450fa53ecSChao Yu 1555caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1556bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP); 1557af033b2aSChao Yu clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1558c9c8ed50SChao Yu 1559c9c8ed50SChao Yu spin_lock(&sbi->stat_lock); 15604354994fSDaniel Rosenberg sbi->unusable_block_count = 0; 1561c9c8ed50SChao Yu spin_unlock(&sbi->stat_lock); 1562c9c8ed50SChao Yu 15638508e44aSJaegeuk Kim __set_cp_next_pack(sbi); 1564c34f42e2SChao Yu 1565c2a080aeSChao Yu /* 1566c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is 1567c2a080aeSChao Yu * updated during writing checkpoint. 1568c2a080aeSChao Yu */ 1569c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) || 1570c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA)) 1571c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 1572c2a080aeSChao Yu 1573c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1574c2a080aeSChao Yu 1575af697c0fSJaegeuk Kim return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0; 1576127e670aSJaegeuk Kim } 1577127e670aSJaegeuk Kim 15784d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1579127e670aSJaegeuk Kim { 1580127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1581127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1582c34f42e2SChao Yu int err = 0; 1583127e670aSJaegeuk Kim 1584f5a131bbSChao Yu if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi)) 1585f5a131bbSChao Yu return -EROFS; 1586f5a131bbSChao Yu 15874354994fSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 15884354994fSDaniel Rosenberg if (cpc->reason != CP_PAUSE) 15894354994fSDaniel Rosenberg return 0; 1590dcbb4c10SJoe Perches f2fs_warn(sbi, "Start checkpoint disabled!"); 15914354994fSDaniel Rosenberg } 1592b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 15938769918bSSahitya Tummala down_write(&sbi->cp_global_sem); 15948501017eSJaegeuk Kim 1595caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1596c473f1a9SChao Yu ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1597c473f1a9SChao Yu ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) 15988501017eSJaegeuk Kim goto out; 1599c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1600c34f42e2SChao Yu err = -EIO; 1601cf779cabSJaegeuk Kim goto out; 1602c34f42e2SChao Yu } 16032bda542dSWanpeng Li 16042bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 16052bda542dSWanpeng Li 1606c34f42e2SChao Yu err = block_operations(sbi); 1607c34f42e2SChao Yu if (err) 1608cf779cabSJaegeuk Kim goto out; 1609127e670aSJaegeuk Kim 161075ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 16112af4bd6cSNamjae Jeon 1612b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 1613127e670aSJaegeuk Kim 161458cce381SYunlei He /* this is the case of multiple fstrims without any changes */ 1615c473f1a9SChao Yu if (cpc->reason & CP_DISCARD) { 16164d57b86dSChao Yu if (!f2fs_exist_trim_candidates(sbi, cpc)) { 161725290fa5SJaegeuk Kim unblock_operations(sbi); 161825290fa5SJaegeuk Kim goto out; 161925290fa5SJaegeuk Kim } 162025290fa5SJaegeuk Kim 1621a95ba66aSJaegeuk Kim if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 && 16220333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 && 16230333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) { 16244d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 16254d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 162658cce381SYunlei He unblock_operations(sbi); 162758cce381SYunlei He goto out; 162858cce381SYunlei He } 16290333ad4eSJaegeuk Kim } 163058cce381SYunlei He 1631127e670aSJaegeuk Kim /* 1632127e670aSJaegeuk Kim * update checkpoint pack index 1633127e670aSJaegeuk Kim * Increase the version number so that 1634127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1635127e670aSJaegeuk Kim */ 1636d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1637127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1638127e670aSJaegeuk Kim 1639127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1640edc55aafSJaegeuk Kim err = f2fs_flush_nat_entries(sbi, cpc); 1641edc55aafSJaegeuk Kim if (err) 1642edc55aafSJaegeuk Kim goto stop; 1643edc55aafSJaegeuk Kim 16444d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 1645127e670aSJaegeuk Kim 1646d0b9e42aSChao Yu /* save inmem log status */ 1647093749e2SChao Yu f2fs_save_inmem_curseg(sbi); 1648d0b9e42aSChao Yu 1649c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 16504e6a8d9bSJaegeuk Kim if (err) 16514d57b86dSChao Yu f2fs_release_discard_addrs(sbi); 16524e6a8d9bSJaegeuk Kim else 16534d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 1654d0b9e42aSChao Yu 1655093749e2SChao Yu f2fs_restore_inmem_curseg(sbi); 1656edc55aafSJaegeuk Kim stop: 1657127e670aSJaegeuk Kim unblock_operations(sbi); 1658942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 165910027551SJaegeuk Kim 1660c473f1a9SChao Yu if (cpc->reason & CP_RECOVERY) 1661dcbb4c10SJoe Perches f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver); 166260b99b48SJaegeuk Kim 16637a88ddb5SChao Yu /* update CP_TIME to trigger checkpoint periodically */ 16646beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 166555d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 16668501017eSJaegeuk Kim out: 1667b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE) 16688769918bSSahitya Tummala up_write(&sbi->cp_global_sem); 1669c34f42e2SChao Yu return err; 1670127e670aSJaegeuk Kim } 1671127e670aSJaegeuk Kim 16724d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi) 1673127e670aSJaegeuk Kim { 16746451e041SJaegeuk Kim int i; 16756451e041SJaegeuk Kim 16766451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 167767298804SChao Yu struct inode_management *im = &sbi->im[i]; 167867298804SChao Yu 167967298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 168067298804SChao Yu spin_lock_init(&im->ino_lock); 168167298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 168267298804SChao Yu im->ino_num = 0; 16836451e041SJaegeuk Kim } 16846451e041SJaegeuk Kim 1685b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 1686d0b9e42aSChao Yu NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) * 168714b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1688127e670aSJaegeuk Kim } 1689127e670aSJaegeuk Kim 16904d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void) 1691127e670aSJaegeuk Kim { 16926451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 16936451e041SJaegeuk Kim sizeof(struct ino_entry)); 16946451e041SJaegeuk Kim if (!ino_entry_slab) 1695127e670aSJaegeuk Kim return -ENOMEM; 16964d57b86dSChao Yu f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 169706292073SChao Yu sizeof(struct inode_entry)); 16984d57b86dSChao Yu if (!f2fs_inode_entry_slab) { 16996451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1700127e670aSJaegeuk Kim return -ENOMEM; 1701127e670aSJaegeuk Kim } 1702127e670aSJaegeuk Kim return 0; 1703127e670aSJaegeuk Kim } 1704127e670aSJaegeuk Kim 17054d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void) 1706127e670aSJaegeuk Kim { 17076451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 17084d57b86dSChao Yu kmem_cache_destroy(f2fs_inode_entry_slab); 1709127e670aSJaegeuk Kim } 1710261eeb9cSDaeho Jeong 1711261eeb9cSDaeho Jeong static int __write_checkpoint_sync(struct f2fs_sb_info *sbi) 1712261eeb9cSDaeho Jeong { 1713261eeb9cSDaeho Jeong struct cp_control cpc = { .reason = CP_SYNC, }; 1714261eeb9cSDaeho Jeong int err; 1715261eeb9cSDaeho Jeong 1716261eeb9cSDaeho Jeong down_write(&sbi->gc_lock); 1717261eeb9cSDaeho Jeong err = f2fs_write_checkpoint(sbi, &cpc); 1718261eeb9cSDaeho Jeong up_write(&sbi->gc_lock); 1719261eeb9cSDaeho Jeong 1720261eeb9cSDaeho Jeong return err; 1721261eeb9cSDaeho Jeong } 1722261eeb9cSDaeho Jeong 1723261eeb9cSDaeho Jeong static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi) 1724261eeb9cSDaeho Jeong { 1725261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1726261eeb9cSDaeho Jeong struct ckpt_req *req, *next; 1727261eeb9cSDaeho Jeong struct llist_node *dispatch_list; 1728261eeb9cSDaeho Jeong u64 sum_diff = 0, diff, count = 0; 1729261eeb9cSDaeho Jeong int ret; 1730261eeb9cSDaeho Jeong 1731261eeb9cSDaeho Jeong dispatch_list = llist_del_all(&cprc->issue_list); 1732261eeb9cSDaeho Jeong if (!dispatch_list) 1733261eeb9cSDaeho Jeong return; 1734261eeb9cSDaeho Jeong dispatch_list = llist_reverse_order(dispatch_list); 1735261eeb9cSDaeho Jeong 1736261eeb9cSDaeho Jeong ret = __write_checkpoint_sync(sbi); 1737261eeb9cSDaeho Jeong atomic_inc(&cprc->issued_ckpt); 1738261eeb9cSDaeho Jeong 1739261eeb9cSDaeho Jeong llist_for_each_entry_safe(req, next, dispatch_list, llnode) { 1740261eeb9cSDaeho Jeong diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time); 1741261eeb9cSDaeho Jeong req->ret = ret; 1742261eeb9cSDaeho Jeong complete(&req->wait); 1743261eeb9cSDaeho Jeong 1744261eeb9cSDaeho Jeong sum_diff += diff; 1745261eeb9cSDaeho Jeong count++; 1746261eeb9cSDaeho Jeong } 1747261eeb9cSDaeho Jeong atomic_sub(count, &cprc->queued_ckpt); 1748261eeb9cSDaeho Jeong atomic_add(count, &cprc->total_ckpt); 1749261eeb9cSDaeho Jeong 1750261eeb9cSDaeho Jeong spin_lock(&cprc->stat_lock); 1751261eeb9cSDaeho Jeong cprc->cur_time = (unsigned int)div64_u64(sum_diff, count); 1752261eeb9cSDaeho Jeong if (cprc->peak_time < cprc->cur_time) 1753261eeb9cSDaeho Jeong cprc->peak_time = cprc->cur_time; 1754261eeb9cSDaeho Jeong spin_unlock(&cprc->stat_lock); 1755261eeb9cSDaeho Jeong } 1756261eeb9cSDaeho Jeong 1757261eeb9cSDaeho Jeong static int issue_checkpoint_thread(void *data) 1758261eeb9cSDaeho Jeong { 1759261eeb9cSDaeho Jeong struct f2fs_sb_info *sbi = data; 1760261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1761261eeb9cSDaeho Jeong wait_queue_head_t *q = &cprc->ckpt_wait_queue; 1762261eeb9cSDaeho Jeong repeat: 1763261eeb9cSDaeho Jeong if (kthread_should_stop()) 1764261eeb9cSDaeho Jeong return 0; 1765261eeb9cSDaeho Jeong 1766261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) 1767261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1768261eeb9cSDaeho Jeong 1769261eeb9cSDaeho Jeong wait_event_interruptible(*q, 1770261eeb9cSDaeho Jeong kthread_should_stop() || !llist_empty(&cprc->issue_list)); 1771261eeb9cSDaeho Jeong goto repeat; 1772261eeb9cSDaeho Jeong } 1773261eeb9cSDaeho Jeong 1774261eeb9cSDaeho Jeong static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi, 1775261eeb9cSDaeho Jeong struct ckpt_req *wait_req) 1776261eeb9cSDaeho Jeong { 1777261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1778261eeb9cSDaeho Jeong 1779261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) { 1780261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi); 1781261eeb9cSDaeho Jeong } else { 1782261eeb9cSDaeho Jeong /* already dispatched by issue_checkpoint_thread */ 1783261eeb9cSDaeho Jeong if (wait_req) 1784261eeb9cSDaeho Jeong wait_for_completion(&wait_req->wait); 1785261eeb9cSDaeho Jeong } 1786261eeb9cSDaeho Jeong } 1787261eeb9cSDaeho Jeong 1788261eeb9cSDaeho Jeong static void init_ckpt_req(struct ckpt_req *req) 1789261eeb9cSDaeho Jeong { 1790261eeb9cSDaeho Jeong memset(req, 0, sizeof(struct ckpt_req)); 1791261eeb9cSDaeho Jeong 1792261eeb9cSDaeho Jeong init_completion(&req->wait); 1793261eeb9cSDaeho Jeong req->queue_time = ktime_get(); 1794261eeb9cSDaeho Jeong } 1795261eeb9cSDaeho Jeong 1796261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) 1797261eeb9cSDaeho Jeong { 1798261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1799261eeb9cSDaeho Jeong struct ckpt_req req; 1800261eeb9cSDaeho Jeong struct cp_control cpc; 1801261eeb9cSDaeho Jeong 1802261eeb9cSDaeho Jeong cpc.reason = __get_cp_reason(sbi); 1803261eeb9cSDaeho Jeong if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) { 1804261eeb9cSDaeho Jeong int ret; 1805261eeb9cSDaeho Jeong 1806261eeb9cSDaeho Jeong down_write(&sbi->gc_lock); 1807261eeb9cSDaeho Jeong ret = f2fs_write_checkpoint(sbi, &cpc); 1808261eeb9cSDaeho Jeong up_write(&sbi->gc_lock); 1809261eeb9cSDaeho Jeong 1810261eeb9cSDaeho Jeong return ret; 1811261eeb9cSDaeho Jeong } 1812261eeb9cSDaeho Jeong 1813261eeb9cSDaeho Jeong if (!cprc->f2fs_issue_ckpt) 1814261eeb9cSDaeho Jeong return __write_checkpoint_sync(sbi); 1815261eeb9cSDaeho Jeong 1816261eeb9cSDaeho Jeong init_ckpt_req(&req); 1817261eeb9cSDaeho Jeong 1818261eeb9cSDaeho Jeong llist_add(&req.llnode, &cprc->issue_list); 1819261eeb9cSDaeho Jeong atomic_inc(&cprc->queued_ckpt); 1820261eeb9cSDaeho Jeong 1821*3b42c741SChao Yu /* 1822*3b42c741SChao Yu * update issue_list before we wake up issue_checkpoint thread, 1823*3b42c741SChao Yu * this smp_mb() pairs with another barrier in ___wait_event(), 1824*3b42c741SChao Yu * see more details in comments of waitqueue_active(). 1825*3b42c741SChao Yu */ 1826261eeb9cSDaeho Jeong smp_mb(); 1827261eeb9cSDaeho Jeong 1828261eeb9cSDaeho Jeong if (waitqueue_active(&cprc->ckpt_wait_queue)) 1829261eeb9cSDaeho Jeong wake_up(&cprc->ckpt_wait_queue); 1830261eeb9cSDaeho Jeong 1831261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1832261eeb9cSDaeho Jeong wait_for_completion(&req.wait); 1833261eeb9cSDaeho Jeong else 1834261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, &req); 1835261eeb9cSDaeho Jeong 1836261eeb9cSDaeho Jeong return req.ret; 1837261eeb9cSDaeho Jeong } 1838261eeb9cSDaeho Jeong 1839261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi) 1840261eeb9cSDaeho Jeong { 1841261eeb9cSDaeho Jeong dev_t dev = sbi->sb->s_bdev->bd_dev; 1842261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1843261eeb9cSDaeho Jeong 1844261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) 1845261eeb9cSDaeho Jeong return 0; 1846261eeb9cSDaeho Jeong 1847261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi, 1848261eeb9cSDaeho Jeong "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev)); 1849261eeb9cSDaeho Jeong if (IS_ERR(cprc->f2fs_issue_ckpt)) { 1850261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1851261eeb9cSDaeho Jeong return -ENOMEM; 1852261eeb9cSDaeho Jeong } 1853261eeb9cSDaeho Jeong 1854e6592066SDaeho Jeong set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio); 1855261eeb9cSDaeho Jeong 1856261eeb9cSDaeho Jeong return 0; 1857261eeb9cSDaeho Jeong } 1858261eeb9cSDaeho Jeong 1859261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi) 1860261eeb9cSDaeho Jeong { 1861261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1862261eeb9cSDaeho Jeong 1863261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt) { 1864261eeb9cSDaeho Jeong struct task_struct *ckpt_task = cprc->f2fs_issue_ckpt; 1865261eeb9cSDaeho Jeong 1866261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL; 1867261eeb9cSDaeho Jeong kthread_stop(ckpt_task); 1868261eeb9cSDaeho Jeong 1869261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, NULL); 1870261eeb9cSDaeho Jeong } 1871261eeb9cSDaeho Jeong } 1872261eeb9cSDaeho Jeong 1873261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi) 1874261eeb9cSDaeho Jeong { 1875261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info; 1876261eeb9cSDaeho Jeong 1877261eeb9cSDaeho Jeong atomic_set(&cprc->issued_ckpt, 0); 1878261eeb9cSDaeho Jeong atomic_set(&cprc->total_ckpt, 0); 1879261eeb9cSDaeho Jeong atomic_set(&cprc->queued_ckpt, 0); 1880e6592066SDaeho Jeong cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO; 1881261eeb9cSDaeho Jeong init_waitqueue_head(&cprc->ckpt_wait_queue); 1882261eeb9cSDaeho Jeong init_llist_head(&cprc->issue_list); 1883261eeb9cSDaeho Jeong spin_lock_init(&cprc->stat_lock); 1884261eeb9cSDaeho Jeong } 1885