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> 16127e670aSJaegeuk Kim 17127e670aSJaegeuk Kim #include "f2fs.h" 18127e670aSJaegeuk Kim #include "node.h" 19127e670aSJaegeuk Kim #include "segment.h" 209e4ded3fSJaegeuk Kim #include "trace.h" 212af4bd6cSNamjae Jeon #include <trace/events/f2fs.h> 22127e670aSJaegeuk Kim 236451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab; 244d57b86dSChao Yu struct kmem_cache *f2fs_inode_entry_slab; 25127e670aSJaegeuk Kim 2638f91ca8SJaegeuk Kim void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io) 2738f91ca8SJaegeuk Kim { 28d494500aSChao Yu f2fs_build_fault_attr(sbi, 0, 0); 29aaec2b1dSChao Yu set_ckpt_flags(sbi, CP_ERROR_FLAG); 3038f91ca8SJaegeuk Kim if (!end_io) 31b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 3238f91ca8SJaegeuk Kim } 3338f91ca8SJaegeuk Kim 340a8165d7SJaegeuk Kim /* 35127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 36127e670aSJaegeuk Kim */ 374d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 38127e670aSJaegeuk Kim { 399df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 40127e670aSJaegeuk Kim struct page *page = NULL; 41127e670aSJaegeuk Kim repeat: 42300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 43127e670aSJaegeuk Kim if (!page) { 44127e670aSJaegeuk Kim cond_resched(); 45127e670aSJaegeuk Kim goto repeat; 46127e670aSJaegeuk Kim } 47bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 48237c0790SJaegeuk Kim if (!PageUptodate(page)) 49127e670aSJaegeuk Kim SetPageUptodate(page); 50127e670aSJaegeuk Kim return page; 51127e670aSJaegeuk Kim } 52127e670aSJaegeuk Kim 532b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index, 542b947003SChao Yu bool is_meta) 55127e670aSJaegeuk Kim { 569df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 57127e670aSJaegeuk Kim struct page *page; 58cf04e8ebSJaegeuk Kim struct f2fs_io_info fio = { 5905ca3632SJaegeuk Kim .sbi = sbi, 60cf04e8ebSJaegeuk Kim .type = META, 6104d328deSMike Christie .op = REQ_OP_READ, 6270fd7614SChristoph Hellwig .op_flags = REQ_META | REQ_PRIO, 637a9d7548SChao Yu .old_blkaddr = index, 647a9d7548SChao Yu .new_blkaddr = index, 654375a336SJaegeuk Kim .encrypted_page = NULL, 666dc3a126SChao Yu .is_por = !is_meta, 67cf04e8ebSJaegeuk Kim }; 687735730dSChao Yu int err; 692b947003SChao Yu 702b947003SChao Yu if (unlikely(!is_meta)) 7104d328deSMike Christie fio.op_flags &= ~REQ_META; 72127e670aSJaegeuk Kim repeat: 73300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 74127e670aSJaegeuk Kim if (!page) { 75127e670aSJaegeuk Kim cond_resched(); 76127e670aSJaegeuk Kim goto repeat; 77127e670aSJaegeuk Kim } 78393ff91fSJaegeuk Kim if (PageUptodate(page)) 79393ff91fSJaegeuk Kim goto out; 80127e670aSJaegeuk Kim 8105ca3632SJaegeuk Kim fio.page = page; 8205ca3632SJaegeuk Kim 837735730dSChao Yu err = f2fs_submit_page_bio(&fio); 847735730dSChao Yu if (err) { 857735730dSChao Yu f2fs_put_page(page, 1); 867735730dSChao Yu return ERR_PTR(err); 8786531d6bSJaegeuk Kim } 88393ff91fSJaegeuk Kim 89393ff91fSJaegeuk Kim lock_page(page); 906bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) { 91afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 92afcb7ca0SJaegeuk Kim goto repeat; 93afcb7ca0SJaegeuk Kim } 94f3f338caSChao Yu 9581114baaSChao Yu if (unlikely(!PageUptodate(page))) { 967735730dSChao Yu f2fs_put_page(page, 1); 977735730dSChao Yu return ERR_PTR(-EIO); 9881114baaSChao Yu } 99393ff91fSJaegeuk Kim out: 100127e670aSJaegeuk Kim return page; 101127e670aSJaegeuk Kim } 102127e670aSJaegeuk Kim 1034d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 1042b947003SChao Yu { 1052b947003SChao Yu return __get_meta_page(sbi, index, true); 1062b947003SChao Yu } 1072b947003SChao Yu 1087735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index) 1097735730dSChao Yu { 1107735730dSChao Yu struct page *page; 1117735730dSChao Yu int count = 0; 1127735730dSChao Yu 1137735730dSChao Yu retry: 1147735730dSChao Yu page = __get_meta_page(sbi, index, true); 1157735730dSChao Yu if (IS_ERR(page)) { 1167735730dSChao Yu if (PTR_ERR(page) == -EIO && 1177735730dSChao Yu ++count <= DEFAULT_RETRY_IO_COUNT) 1187735730dSChao Yu goto retry; 1197735730dSChao Yu f2fs_stop_checkpoint(sbi, false); 1207735730dSChao Yu } 1217735730dSChao Yu return page; 1227735730dSChao Yu } 1237735730dSChao Yu 1242b947003SChao Yu /* for POR only */ 1254d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 1262b947003SChao Yu { 1272b947003SChao Yu return __get_meta_page(sbi, index, false); 1282b947003SChao Yu } 1292b947003SChao Yu 13093770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr, 13193770ab7SChao Yu int type) 13293770ab7SChao Yu { 13393770ab7SChao Yu struct seg_entry *se; 13493770ab7SChao Yu unsigned int segno, offset; 13593770ab7SChao Yu bool exist; 13693770ab7SChao Yu 13793770ab7SChao Yu if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ) 13893770ab7SChao Yu return true; 13993770ab7SChao Yu 14093770ab7SChao Yu segno = GET_SEGNO(sbi, blkaddr); 14193770ab7SChao Yu offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 14293770ab7SChao Yu se = get_seg_entry(sbi, segno); 14393770ab7SChao Yu 14493770ab7SChao Yu exist = f2fs_test_bit(offset, se->cur_valid_map); 14593770ab7SChao Yu if (!exist && type == DATA_GENERIC_ENHANCE) { 146dcbb4c10SJoe Perches f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", 147dcbb4c10SJoe Perches blkaddr, exist); 14893770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 14993770ab7SChao Yu WARN_ON(1); 15093770ab7SChao Yu } 15193770ab7SChao Yu return exist; 15293770ab7SChao Yu } 15393770ab7SChao Yu 154e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 1554d57b86dSChao Yu block_t blkaddr, int type) 156662befdaSChao Yu { 157662befdaSChao Yu switch (type) { 158662befdaSChao Yu case META_NAT: 15966b00c18SChao Yu break; 160662befdaSChao Yu case META_SIT: 16166b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 16266b00c18SChao Yu return false; 16366b00c18SChao Yu break; 16481c1a0f1SChao Yu case META_SSA: 16566b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 16666b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr)) 16766b00c18SChao Yu return false; 16866b00c18SChao Yu break; 169662befdaSChao Yu case META_CP: 17066b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 17166b00c18SChao Yu blkaddr < __start_cp_addr(sbi))) 17266b00c18SChao Yu return false; 17366b00c18SChao Yu break; 1744c521f49SJaegeuk Kim case META_POR: 17593770ab7SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 17693770ab7SChao Yu blkaddr < MAIN_BLKADDR(sbi))) 17793770ab7SChao Yu return false; 17893770ab7SChao Yu break; 179e1da7872SChao Yu case DATA_GENERIC: 18093770ab7SChao Yu case DATA_GENERIC_ENHANCE: 18193770ab7SChao Yu case DATA_GENERIC_ENHANCE_READ: 18266b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 183c9b60788SChao Yu blkaddr < MAIN_BLKADDR(sbi))) { 184dcbb4c10SJoe Perches f2fs_warn(sbi, "access invalid blkaddr:%u", 185dcbb4c10SJoe Perches blkaddr); 18693770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 187c9b60788SChao Yu WARN_ON(1); 18866b00c18SChao Yu return false; 18993770ab7SChao Yu } else { 19093770ab7SChao Yu return __is_bitmap_valid(sbi, blkaddr, type); 191c9b60788SChao Yu } 19266b00c18SChao Yu break; 193e1da7872SChao Yu case META_GENERIC: 194e1da7872SChao Yu if (unlikely(blkaddr < SEG0_BLKADDR(sbi) || 195e1da7872SChao Yu blkaddr >= MAIN_BLKADDR(sbi))) 196e1da7872SChao Yu return false; 197e1da7872SChao Yu break; 198662befdaSChao Yu default: 199662befdaSChao Yu BUG(); 200662befdaSChao Yu } 20166b00c18SChao Yu 20266b00c18SChao Yu return true; 203662befdaSChao Yu } 204662befdaSChao Yu 205662befdaSChao Yu /* 2067a88ddb5SChao Yu * Readahead CP/NAT/SIT/SSA/POR pages 207662befdaSChao Yu */ 2084d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 20926879fb1SChao Yu int type, bool sync) 210662befdaSChao Yu { 211662befdaSChao Yu struct page *page; 2124c521f49SJaegeuk Kim block_t blkno = start; 213662befdaSChao Yu struct f2fs_io_info fio = { 21405ca3632SJaegeuk Kim .sbi = sbi, 215662befdaSChao Yu .type = META, 21604d328deSMike Christie .op = REQ_OP_READ, 21770fd7614SChristoph Hellwig .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, 2184375a336SJaegeuk Kim .encrypted_page = NULL, 219fb830fc5SChao Yu .in_list = false, 2206dc3a126SChao Yu .is_por = (type == META_POR), 221662befdaSChao Yu }; 222e9f5b8b8SChao Yu struct blk_plug plug; 223*ce4c638cSChao Yu int err; 224662befdaSChao Yu 2252b947003SChao Yu if (unlikely(type == META_POR)) 22604d328deSMike Christie fio.op_flags &= ~REQ_META; 2272b947003SChao Yu 228e9f5b8b8SChao Yu blk_start_plug(&plug); 229662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 230662befdaSChao Yu 231e1da7872SChao Yu if (!f2fs_is_valid_blkaddr(sbi, blkno, type)) 23266b00c18SChao Yu goto out; 23366b00c18SChao Yu 234662befdaSChao Yu switch (type) { 235662befdaSChao Yu case META_NAT: 23666b00c18SChao Yu if (unlikely(blkno >= 23766b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 238662befdaSChao Yu blkno = 0; 23966b00c18SChao Yu /* get nat block addr */ 2407a9d7548SChao Yu fio.new_blkaddr = current_nat_addr(sbi, 241662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 242662befdaSChao Yu break; 243662befdaSChao Yu case META_SIT: 244662befdaSChao Yu /* get sit block addr */ 2457a9d7548SChao Yu fio.new_blkaddr = current_sit_addr(sbi, 246662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 247662befdaSChao Yu break; 24881c1a0f1SChao Yu case META_SSA: 249662befdaSChao Yu case META_CP: 2504c521f49SJaegeuk Kim case META_POR: 2517a9d7548SChao Yu fio.new_blkaddr = blkno; 252662befdaSChao Yu break; 253662befdaSChao Yu default: 254662befdaSChao Yu BUG(); 255662befdaSChao Yu } 256662befdaSChao Yu 257300e129cSJaegeuk Kim page = f2fs_grab_cache_page(META_MAPPING(sbi), 258300e129cSJaegeuk Kim fio.new_blkaddr, false); 259662befdaSChao Yu if (!page) 260662befdaSChao Yu continue; 261662befdaSChao Yu if (PageUptodate(page)) { 262662befdaSChao Yu f2fs_put_page(page, 1); 263662befdaSChao Yu continue; 264662befdaSChao Yu } 265662befdaSChao Yu 26605ca3632SJaegeuk Kim fio.page = page; 267*ce4c638cSChao Yu err = f2fs_submit_page_bio(&fio); 268*ce4c638cSChao Yu f2fs_put_page(page, err ? 1 : 0); 269662befdaSChao Yu } 270662befdaSChao Yu out: 271e9f5b8b8SChao Yu blk_finish_plug(&plug); 272662befdaSChao Yu return blkno - start; 273662befdaSChao Yu } 274662befdaSChao Yu 2754d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index) 276635aee1fSChao Yu { 277635aee1fSChao Yu struct page *page; 278635aee1fSChao Yu bool readahead = false; 279635aee1fSChao Yu 280635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 2814da7bf5aSJaegeuk Kim if (!page || !PageUptodate(page)) 282635aee1fSChao Yu readahead = true; 283635aee1fSChao Yu f2fs_put_page(page, 0); 284635aee1fSChao Yu 285635aee1fSChao Yu if (readahead) 2864d57b86dSChao Yu f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true); 287635aee1fSChao Yu } 288635aee1fSChao Yu 289b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page, 290b0af6d49SChao Yu struct writeback_control *wbc, 291b0af6d49SChao Yu enum iostat_type io_type) 292127e670aSJaegeuk Kim { 2934081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 294577e3495SJaegeuk Kim 295ecda0de3SChao Yu trace_f2fs_writepage(page, META); 296ecda0de3SChao Yu 297af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 298af697c0fSJaegeuk Kim goto redirty_out; 299caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 300cfb271d4SChao Yu goto redirty_out; 301857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 302cfb271d4SChao Yu goto redirty_out; 303127e670aSJaegeuk Kim 3044d57b86dSChao Yu f2fs_do_write_meta_page(sbi, page, io_type); 305127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 3060c3a5797SChao Yu 3070c3a5797SChao Yu if (wbc->for_reclaim) 308bab475c5SChao Yu f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META); 3090c3a5797SChao Yu 310127e670aSJaegeuk Kim unlock_page(page); 311857dc4e0SJaegeuk Kim 3120c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 313b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, META); 3140c3a5797SChao Yu 315577e3495SJaegeuk Kim return 0; 316cfb271d4SChao Yu 317cfb271d4SChao Yu redirty_out: 31876f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 319cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 320127e670aSJaegeuk Kim } 321127e670aSJaegeuk Kim 322b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page, 323b0af6d49SChao Yu struct writeback_control *wbc) 324b0af6d49SChao Yu { 325b0af6d49SChao Yu return __f2fs_write_meta_page(page, wbc, FS_META_IO); 326b0af6d49SChao Yu } 327b0af6d49SChao Yu 328127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 329127e670aSJaegeuk Kim struct writeback_control *wbc) 330127e670aSJaegeuk Kim { 3314081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 33250c8cdb3SJaegeuk Kim long diff, written; 333127e670aSJaegeuk Kim 3340771fcc7SChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 3350771fcc7SChao Yu goto skip_write; 3360771fcc7SChao Yu 3375459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 338812a9597SJaegeuk Kim if (wbc->sync_mode != WB_SYNC_ALL && 339812a9597SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < 340812a9597SJaegeuk Kim nr_pages_to_skip(sbi, META)) 341d3baf95dSJaegeuk Kim goto skip_write; 342127e670aSJaegeuk Kim 343a29d0e0bSYunlei He /* if locked failed, cp will flush dirty pages instead */ 344a29d0e0bSYunlei He if (!mutex_trylock(&sbi->cp_mutex)) 345a29d0e0bSYunlei He goto skip_write; 346d31c7c3fSYunlei He 347a29d0e0bSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 34850c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 3494d57b86dSChao Yu written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); 350127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 35150c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 352127e670aSJaegeuk Kim return 0; 353d3baf95dSJaegeuk Kim 354d3baf95dSJaegeuk Kim skip_write: 355d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 356d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 357d3baf95dSJaegeuk Kim return 0; 358127e670aSJaegeuk Kim } 359127e670aSJaegeuk Kim 3604d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 361b0af6d49SChao Yu long nr_to_write, enum iostat_type io_type) 362127e670aSJaegeuk Kim { 3639df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 364028a63a6SJan Kara pgoff_t index = 0, prev = ULONG_MAX; 365127e670aSJaegeuk Kim struct pagevec pvec; 366127e670aSJaegeuk Kim long nwritten = 0; 367028a63a6SJan Kara int nr_pages; 368127e670aSJaegeuk Kim struct writeback_control wbc = { 369127e670aSJaegeuk Kim .for_reclaim = 0, 370127e670aSJaegeuk Kim }; 371e9f5b8b8SChao Yu struct blk_plug plug; 372127e670aSJaegeuk Kim 37386679820SMel Gorman pagevec_init(&pvec); 374127e670aSJaegeuk Kim 375e9f5b8b8SChao Yu blk_start_plug(&plug); 376e9f5b8b8SChao Yu 377028a63a6SJan Kara while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 37867fd707fSJan Kara PAGECACHE_TAG_DIRTY))) { 379028a63a6SJan Kara int i; 380127e670aSJaegeuk Kim 381127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 382127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 383203681f6SJaegeuk Kim 38480dd9c0eSChao Yu if (prev == ULONG_MAX) 3856066d8cdSJaegeuk Kim prev = page->index - 1; 3866066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 3876066d8cdSJaegeuk Kim pagevec_release(&pvec); 3886066d8cdSJaegeuk Kim goto stop; 3896066d8cdSJaegeuk Kim } 3906066d8cdSJaegeuk Kim 391127e670aSJaegeuk Kim lock_page(page); 392203681f6SJaegeuk Kim 393203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 394203681f6SJaegeuk Kim continue_unlock: 395203681f6SJaegeuk Kim unlock_page(page); 396203681f6SJaegeuk Kim continue; 397203681f6SJaegeuk Kim } 398203681f6SJaegeuk Kim if (!PageDirty(page)) { 399203681f6SJaegeuk Kim /* someone wrote it for us */ 400203681f6SJaegeuk Kim goto continue_unlock; 401203681f6SJaegeuk Kim } 402203681f6SJaegeuk Kim 403bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 404fa3d2bdfSJaegeuk Kim 405203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 406203681f6SJaegeuk Kim goto continue_unlock; 407203681f6SJaegeuk Kim 408b0af6d49SChao Yu if (__f2fs_write_meta_page(page, &wbc, io_type)) { 409577e3495SJaegeuk Kim unlock_page(page); 410577e3495SJaegeuk Kim break; 411577e3495SJaegeuk Kim } 412cfb271d4SChao Yu nwritten++; 4136066d8cdSJaegeuk Kim prev = page->index; 414cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 415127e670aSJaegeuk Kim break; 416127e670aSJaegeuk Kim } 417127e670aSJaegeuk Kim pagevec_release(&pvec); 418127e670aSJaegeuk Kim cond_resched(); 419127e670aSJaegeuk Kim } 4206066d8cdSJaegeuk Kim stop: 421127e670aSJaegeuk Kim if (nwritten) 422b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, type); 423127e670aSJaegeuk Kim 424e9f5b8b8SChao Yu blk_finish_plug(&plug); 425e9f5b8b8SChao Yu 426127e670aSJaegeuk Kim return nwritten; 427127e670aSJaegeuk Kim } 428127e670aSJaegeuk Kim 429127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 430127e670aSJaegeuk Kim { 43126c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 43226c6b887SJaegeuk Kim 433237c0790SJaegeuk Kim if (!PageUptodate(page)) 434127e670aSJaegeuk Kim SetPageUptodate(page); 435127e670aSJaegeuk Kim if (!PageDirty(page)) { 436b87078adSJaegeuk Kim __set_page_dirty_nobuffers(page); 4374081363fSJaegeuk Kim inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); 438240a5915SChao Yu f2fs_set_page_private(page, 0); 4399e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 440127e670aSJaegeuk Kim return 1; 441127e670aSJaegeuk Kim } 442127e670aSJaegeuk Kim return 0; 443127e670aSJaegeuk Kim } 444127e670aSJaegeuk Kim 445127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 446127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 447127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 448127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 449487261f3SChao Yu .invalidatepage = f2fs_invalidate_page, 450487261f3SChao Yu .releasepage = f2fs_release_page, 4515b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION 4525b7a487cSWeichao Guo .migratepage = f2fs_migrate_page, 4535b7a487cSWeichao Guo #endif 454127e670aSJaegeuk Kim }; 455127e670aSJaegeuk Kim 45639d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, 45739d787beSChao Yu unsigned int devidx, int type) 458953e6cc6SJaegeuk Kim { 45967298804SChao Yu struct inode_management *im = &sbi->im[type]; 46080c54505SJaegeuk Kim struct ino_entry *e, *tmp; 46180c54505SJaegeuk Kim 46280c54505SJaegeuk Kim tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS); 46319526d74SChao Yu 46480c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 465769ec6e5SJaegeuk Kim 46667298804SChao Yu spin_lock(&im->ino_lock); 46767298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 46839efac41SJaegeuk Kim if (!e) { 46980c54505SJaegeuk Kim e = tmp; 47019526d74SChao Yu if (unlikely(radix_tree_insert(&im->ino_root, ino, e))) 47119526d74SChao Yu f2fs_bug_on(sbi, 1); 47219526d74SChao Yu 47339efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 47439efac41SJaegeuk Kim e->ino = ino; 47539efac41SJaegeuk Kim 47667298804SChao Yu list_add_tail(&e->list, &im->ino_list); 4778c402946SJaegeuk Kim if (type != ORPHAN_INO) 47867298804SChao Yu im->ino_num++; 47939efac41SJaegeuk Kim } 48039d787beSChao Yu 48139d787beSChao Yu if (type == FLUSH_INO) 48239d787beSChao Yu f2fs_set_bit(devidx, (char *)&e->dirty_device); 48339d787beSChao Yu 48467298804SChao Yu spin_unlock(&im->ino_lock); 485769ec6e5SJaegeuk Kim radix_tree_preload_end(); 48680c54505SJaegeuk Kim 48780c54505SJaegeuk Kim if (e != tmp) 48880c54505SJaegeuk Kim kmem_cache_free(ino_entry_slab, tmp); 489953e6cc6SJaegeuk Kim } 490953e6cc6SJaegeuk Kim 4916451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 492953e6cc6SJaegeuk Kim { 49367298804SChao Yu struct inode_management *im = &sbi->im[type]; 4946451e041SJaegeuk Kim struct ino_entry *e; 495953e6cc6SJaegeuk Kim 49667298804SChao Yu spin_lock(&im->ino_lock); 49767298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 49839efac41SJaegeuk Kim if (e) { 499953e6cc6SJaegeuk Kim list_del(&e->list); 50067298804SChao Yu radix_tree_delete(&im->ino_root, ino); 50167298804SChao Yu im->ino_num--; 50267298804SChao Yu spin_unlock(&im->ino_lock); 5036451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 504953e6cc6SJaegeuk Kim return; 505953e6cc6SJaegeuk Kim } 50667298804SChao Yu spin_unlock(&im->ino_lock); 507953e6cc6SJaegeuk Kim } 508953e6cc6SJaegeuk Kim 5094d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 510fff04f90SJaegeuk Kim { 511fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 51239d787beSChao Yu __add_ino_entry(sbi, ino, 0, type); 513fff04f90SJaegeuk Kim } 514fff04f90SJaegeuk Kim 5154d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 516fff04f90SJaegeuk Kim { 517fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 518fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 519fff04f90SJaegeuk Kim } 520fff04f90SJaegeuk Kim 521fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */ 5224d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 523fff04f90SJaegeuk Kim { 52467298804SChao Yu struct inode_management *im = &sbi->im[mode]; 525fff04f90SJaegeuk Kim struct ino_entry *e; 52667298804SChao Yu 52767298804SChao Yu spin_lock(&im->ino_lock); 52867298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 52967298804SChao Yu spin_unlock(&im->ino_lock); 530fff04f90SJaegeuk Kim return e ? true : false; 531fff04f90SJaegeuk Kim } 532fff04f90SJaegeuk Kim 5334d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all) 534fff04f90SJaegeuk Kim { 535fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 536fff04f90SJaegeuk Kim int i; 537fff04f90SJaegeuk Kim 53839d787beSChao Yu for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) { 53967298804SChao Yu struct inode_management *im = &sbi->im[i]; 54067298804SChao Yu 54167298804SChao Yu spin_lock(&im->ino_lock); 54267298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 543fff04f90SJaegeuk Kim list_del(&e->list); 54467298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 545fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 54667298804SChao Yu im->ino_num--; 547fff04f90SJaegeuk Kim } 54867298804SChao Yu spin_unlock(&im->ino_lock); 549fff04f90SJaegeuk Kim } 550fff04f90SJaegeuk Kim } 551fff04f90SJaegeuk Kim 5524d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 55339d787beSChao Yu unsigned int devidx, int type) 55439d787beSChao Yu { 55539d787beSChao Yu __add_ino_entry(sbi, ino, devidx, type); 55639d787beSChao Yu } 55739d787beSChao Yu 5584d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 55939d787beSChao Yu unsigned int devidx, int type) 56039d787beSChao Yu { 56139d787beSChao Yu struct inode_management *im = &sbi->im[type]; 56239d787beSChao Yu struct ino_entry *e; 56339d787beSChao Yu bool is_dirty = false; 56439d787beSChao Yu 56539d787beSChao Yu spin_lock(&im->ino_lock); 56639d787beSChao Yu e = radix_tree_lookup(&im->ino_root, ino); 56739d787beSChao Yu if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device)) 56839d787beSChao Yu is_dirty = true; 56939d787beSChao Yu spin_unlock(&im->ino_lock); 57039d787beSChao Yu return is_dirty; 57139d787beSChao Yu } 57239d787beSChao Yu 5734d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi) 574127e670aSJaegeuk Kim { 57567298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 576127e670aSJaegeuk Kim int err = 0; 577127e670aSJaegeuk Kim 57867298804SChao Yu spin_lock(&im->ino_lock); 579cb78942bSJaegeuk Kim 5801ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) { 581cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock); 582c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_ORPHAN); 583cb78942bSJaegeuk Kim return -ENOSPC; 584cb78942bSJaegeuk Kim } 5857fa750a1SArnd Bergmann 58667298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 587127e670aSJaegeuk Kim err = -ENOSPC; 588cbd56e7dSJaegeuk Kim else 58967298804SChao Yu im->ino_num++; 59067298804SChao Yu spin_unlock(&im->ino_lock); 5910d47c1adSGu Zheng 592127e670aSJaegeuk Kim return err; 593127e670aSJaegeuk Kim } 594127e670aSJaegeuk Kim 5954d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi) 596cbd56e7dSJaegeuk Kim { 59767298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 59867298804SChao Yu 59967298804SChao Yu spin_lock(&im->ino_lock); 60067298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 60167298804SChao Yu im->ino_num--; 60267298804SChao Yu spin_unlock(&im->ino_lock); 603cbd56e7dSJaegeuk Kim } 604cbd56e7dSJaegeuk Kim 6054d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode) 606127e670aSJaegeuk Kim { 60739efac41SJaegeuk Kim /* add new orphan ino entry into list */ 60839d787beSChao Yu __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO); 6094d57b86dSChao Yu f2fs_update_inode_page(inode); 610127e670aSJaegeuk Kim } 611127e670aSJaegeuk Kim 6124d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 613127e670aSJaegeuk Kim { 614953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 6156451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 616127e670aSJaegeuk Kim } 617127e670aSJaegeuk Kim 6188c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 619127e670aSJaegeuk Kim { 6208c14bfadSChao Yu struct inode *inode; 6215905f9afSJaegeuk Kim struct node_info ni; 62276a45e3cSChao Yu int err; 6238c14bfadSChao Yu 6245905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino); 6258c14bfadSChao Yu if (IS_ERR(inode)) { 6268c14bfadSChao Yu /* 6278c14bfadSChao Yu * there should be a bug that we can't find the entry 6288c14bfadSChao Yu * to orphan inode. 6298c14bfadSChao Yu */ 6308c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 6318c14bfadSChao Yu return PTR_ERR(inode); 6328c14bfadSChao Yu } 6338c14bfadSChao Yu 6340f9ec2a8SJaegeuk Kim err = dquot_initialize(inode); 635a515d12fSSheng Yong if (err) { 636a515d12fSSheng Yong iput(inode); 6370f9ec2a8SJaegeuk Kim goto err_out; 638a515d12fSSheng Yong } 6390f9ec2a8SJaegeuk Kim 640127e670aSJaegeuk Kim clear_nlink(inode); 641127e670aSJaegeuk Kim 642127e670aSJaegeuk Kim /* truncate all the data during iput */ 643127e670aSJaegeuk Kim iput(inode); 6445905f9afSJaegeuk Kim 6457735730dSChao Yu err = f2fs_get_node_info(sbi, ino, &ni); 6467735730dSChao Yu if (err) 6477735730dSChao Yu goto err_out; 6485905f9afSJaegeuk Kim 6495905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */ 6505905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) { 6510f9ec2a8SJaegeuk Kim err = -EIO; 6520f9ec2a8SJaegeuk Kim goto err_out; 6535905f9afSJaegeuk Kim } 6548c14bfadSChao Yu return 0; 6550f9ec2a8SJaegeuk Kim 6560f9ec2a8SJaegeuk Kim err_out: 6570f9ec2a8SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 658dcbb4c10SJoe Perches f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.", 6590f9ec2a8SJaegeuk Kim __func__, ino); 6600f9ec2a8SJaegeuk Kim return err; 661127e670aSJaegeuk Kim } 662127e670aSJaegeuk Kim 6634d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) 664127e670aSJaegeuk Kim { 6653c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 6664b2414d0SChao Yu unsigned int s_flags = sbi->sb->s_flags; 6674b2414d0SChao Yu int err = 0; 668ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA 669ea676733SJaegeuk Kim int quota_enabled; 670ea676733SJaegeuk Kim #endif 671127e670aSJaegeuk Kim 672aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 6738c14bfadSChao Yu return 0; 674127e670aSJaegeuk Kim 675b61af314SChao Yu if (bdev_read_only(sbi->sb->s_bdev)) { 676dcbb4c10SJoe Perches f2fs_info(sbi, "write access unavailable, skipping orphan cleanup"); 677b61af314SChao Yu return 0; 678b61af314SChao Yu } 679b61af314SChao Yu 6801751e8a6SLinus Torvalds if (s_flags & SB_RDONLY) { 681dcbb4c10SJoe Perches f2fs_info(sbi, "orphan cleanup on readonly fs"); 6821751e8a6SLinus Torvalds sbi->sb->s_flags &= ~SB_RDONLY; 6834b2414d0SChao Yu } 6844b2414d0SChao Yu 6854b2414d0SChao Yu #ifdef CONFIG_QUOTA 6864b2414d0SChao Yu /* Needed for iput() to work correctly and not trash data */ 6871751e8a6SLinus Torvalds sbi->sb->s_flags |= SB_ACTIVE; 688ea676733SJaegeuk Kim 68976cf05d7SSheng Yong /* 69076cf05d7SSheng Yong * Turn on quotas which were not enabled for read-only mounts if 69176cf05d7SSheng Yong * filesystem has quota feature, so that they are updated correctly. 69276cf05d7SSheng Yong */ 6931751e8a6SLinus Torvalds quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY); 6944b2414d0SChao Yu #endif 6954b2414d0SChao Yu 69655141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 6973c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 698127e670aSJaegeuk Kim 6994d57b86dSChao Yu f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 700662befdaSChao Yu 7013c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 7027735730dSChao Yu struct page *page; 703127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 704127e670aSJaegeuk Kim 7057735730dSChao Yu page = f2fs_get_meta_page(sbi, start_blk + i); 7067735730dSChao Yu if (IS_ERR(page)) { 7077735730dSChao Yu err = PTR_ERR(page); 7087735730dSChao Yu goto out; 7097735730dSChao Yu } 7107735730dSChao Yu 711127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 712127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 713127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 7148c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 7158c14bfadSChao Yu if (err) { 7168c14bfadSChao Yu f2fs_put_page(page, 1); 7174b2414d0SChao Yu goto out; 7188c14bfadSChao Yu } 719127e670aSJaegeuk Kim } 720127e670aSJaegeuk Kim f2fs_put_page(page, 1); 721127e670aSJaegeuk Kim } 722127e670aSJaegeuk Kim /* clear Orphan Flag */ 723aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 7244b2414d0SChao Yu out: 7251378752bSChao Yu set_sbi_flag(sbi, SBI_IS_RECOVERED); 7261378752bSChao Yu 7274b2414d0SChao Yu #ifdef CONFIG_QUOTA 7284b2414d0SChao Yu /* Turn quotas off */ 729ea676733SJaegeuk Kim if (quota_enabled) 7304b2414d0SChao Yu f2fs_quota_off_umount(sbi->sb); 7314b2414d0SChao Yu #endif 7321751e8a6SLinus Torvalds sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 7334b2414d0SChao Yu 7344b2414d0SChao Yu return err; 735127e670aSJaegeuk Kim } 736127e670aSJaegeuk Kim 737127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 738127e670aSJaegeuk Kim { 739502c6e0bSGu Zheng struct list_head *head; 740127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 741127e670aSJaegeuk Kim unsigned int nentries = 0; 742bd936f84SChao Yu unsigned short index = 1; 7438c402946SJaegeuk Kim unsigned short orphan_blocks; 7444531929eSGu Zheng struct page *page = NULL; 7456451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 74667298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 747127e670aSJaegeuk Kim 74867298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 7498c402946SJaegeuk Kim 750d6c67a4fSJaegeuk Kim /* 751d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 752d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 753d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 754d6c67a4fSJaegeuk Kim */ 75567298804SChao Yu head = &im->ino_list; 756127e670aSJaegeuk Kim 757127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 758502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 759502c6e0bSGu Zheng if (!page) { 7604d57b86dSChao Yu page = f2fs_grab_meta_page(sbi, start_blk++); 761502c6e0bSGu Zheng orphan_blk = 762502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 763502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 764502c6e0bSGu Zheng } 765127e670aSJaegeuk Kim 76636795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 767127e670aSJaegeuk Kim 76836795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 769127e670aSJaegeuk Kim /* 770127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 771127e670aSJaegeuk Kim * then we need to flush current orphan blocks 772127e670aSJaegeuk Kim * and bring another one in memory 773127e670aSJaegeuk Kim */ 774127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 775127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 776127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 777127e670aSJaegeuk Kim set_page_dirty(page); 778127e670aSJaegeuk Kim f2fs_put_page(page, 1); 779127e670aSJaegeuk Kim index++; 780127e670aSJaegeuk Kim nentries = 0; 781127e670aSJaegeuk Kim page = NULL; 782127e670aSJaegeuk Kim } 783127e670aSJaegeuk Kim } 784127e670aSJaegeuk Kim 785502c6e0bSGu Zheng if (page) { 786127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 787127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 788127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 789127e670aSJaegeuk Kim set_page_dirty(page); 790127e670aSJaegeuk Kim f2fs_put_page(page, 1); 791502c6e0bSGu Zheng } 792127e670aSJaegeuk Kim } 793127e670aSJaegeuk Kim 794d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi, 795d7eb8f1cSChao Yu struct f2fs_checkpoint *ckpt) 796d7eb8f1cSChao Yu { 797d7eb8f1cSChao Yu unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset); 798d7eb8f1cSChao Yu __u32 chksum; 799d7eb8f1cSChao Yu 800d7eb8f1cSChao Yu chksum = f2fs_crc32(sbi, ckpt, chksum_ofs); 801d7eb8f1cSChao Yu if (chksum_ofs < CP_CHKSUM_OFFSET) { 802d7eb8f1cSChao Yu chksum_ofs += sizeof(chksum); 803d7eb8f1cSChao Yu chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs, 804d7eb8f1cSChao Yu F2FS_BLKSIZE - chksum_ofs); 805d7eb8f1cSChao Yu } 806d7eb8f1cSChao Yu return chksum; 807d7eb8f1cSChao Yu } 808d7eb8f1cSChao Yu 809fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 810fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page, 811fc0065adSTiezhu Yang unsigned long long *version) 812fc0065adSTiezhu Yang { 813fc0065adSTiezhu Yang size_t crc_offset = 0; 814d7eb8f1cSChao Yu __u32 crc; 815fc0065adSTiezhu Yang 8164d57b86dSChao Yu *cp_page = f2fs_get_meta_page(sbi, cp_addr); 8177735730dSChao Yu if (IS_ERR(*cp_page)) 8187735730dSChao Yu return PTR_ERR(*cp_page); 8197735730dSChao Yu 820fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 821fc0065adSTiezhu Yang 822fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 823d7eb8f1cSChao Yu if (crc_offset < CP_MIN_CHKSUM_OFFSET || 824d7eb8f1cSChao Yu crc_offset > CP_CHKSUM_OFFSET) { 825d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 826dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset); 827fc0065adSTiezhu Yang return -EINVAL; 828fc0065adSTiezhu Yang } 829fc0065adSTiezhu Yang 830d7eb8f1cSChao Yu crc = f2fs_checkpoint_chksum(sbi, *cp_block); 831d7eb8f1cSChao Yu if (crc != cur_cp_crc(*cp_block)) { 832d3f07c04SChao Yu f2fs_put_page(*cp_page, 1); 833dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc value"); 834fc0065adSTiezhu Yang return -EINVAL; 835fc0065adSTiezhu Yang } 836fc0065adSTiezhu Yang 837fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block); 838fc0065adSTiezhu Yang return 0; 839fc0065adSTiezhu Yang } 840fc0065adSTiezhu Yang 841127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 842127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 843127e670aSJaegeuk Kim { 844fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 845fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL; 846127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 847fc0065adSTiezhu Yang int err; 848127e670aSJaegeuk Kim 849fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 850fc0065adSTiezhu Yang &cp_page_1, version); 851fc0065adSTiezhu Yang if (err) 852d3f07c04SChao Yu return NULL; 853c9b60788SChao Yu 854c9b60788SChao Yu if (le32_to_cpu(cp_block->cp_pack_total_block_count) > 855c9b60788SChao Yu sbi->blocks_per_seg) { 856dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", 857c9b60788SChao Yu le32_to_cpu(cp_block->cp_pack_total_block_count)); 858d3f07c04SChao Yu goto invalid_cp; 859c9b60788SChao Yu } 860fc0065adSTiezhu Yang pre_version = *version; 861127e670aSJaegeuk Kim 86225ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 863fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 864fc0065adSTiezhu Yang &cp_page_2, version); 865fc0065adSTiezhu Yang if (err) 866d3f07c04SChao Yu goto invalid_cp; 867fc0065adSTiezhu Yang cur_version = *version; 868127e670aSJaegeuk Kim 869127e670aSJaegeuk Kim if (cur_version == pre_version) { 870127e670aSJaegeuk Kim *version = cur_version; 871127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 872127e670aSJaegeuk Kim return cp_page_1; 873127e670aSJaegeuk Kim } 874127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 875d3f07c04SChao Yu invalid_cp: 876127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 877127e670aSJaegeuk Kim return NULL; 878127e670aSJaegeuk Kim } 879127e670aSJaegeuk Kim 8804d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi) 881127e670aSJaegeuk Kim { 882127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 883127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 884127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 885127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 886127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 887127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 88855141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 8891dbe4152SChangman Lee block_t cp_blk_no; 8901dbe4152SChangman Lee int i; 89110f966bbSChao Yu int err; 892127e670aSJaegeuk Kim 893026f0507SKees Cook sbi->ckpt = f2fs_kzalloc(sbi, array_size(blk_size, cp_blks), 894026f0507SKees Cook GFP_KERNEL); 895127e670aSJaegeuk Kim if (!sbi->ckpt) 896127e670aSJaegeuk Kim return -ENOMEM; 897127e670aSJaegeuk Kim /* 898127e670aSJaegeuk Kim * Finding out valid cp block involves read both 899127e670aSJaegeuk Kim * sets( cp pack 1 and cp pack 2) 900127e670aSJaegeuk Kim */ 901127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 902127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 903127e670aSJaegeuk Kim 904127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 905f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 906f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 907127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 908127e670aSJaegeuk Kim 909127e670aSJaegeuk Kim if (cp1 && cp2) { 910127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 911127e670aSJaegeuk Kim cur_page = cp2; 912127e670aSJaegeuk Kim else 913127e670aSJaegeuk Kim cur_page = cp1; 914127e670aSJaegeuk Kim } else if (cp1) { 915127e670aSJaegeuk Kim cur_page = cp1; 916127e670aSJaegeuk Kim } else if (cp2) { 917127e670aSJaegeuk Kim cur_page = cp2; 918127e670aSJaegeuk Kim } else { 91910f966bbSChao Yu err = -EFSCORRUPTED; 920127e670aSJaegeuk Kim goto fail_no_cp; 921127e670aSJaegeuk Kim } 922127e670aSJaegeuk Kim 923127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 924127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 925127e670aSJaegeuk Kim 9268508e44aSJaegeuk Kim if (cur_page == cp1) 9278508e44aSJaegeuk Kim sbi->cur_cp_pack = 1; 9288508e44aSJaegeuk Kim else 9298508e44aSJaegeuk Kim sbi->cur_cp_pack = 2; 930984ec63cSShawn Lin 931e494c2f9SChao Yu /* Sanity checking of checkpoint */ 93210f966bbSChao Yu if (f2fs_sanity_check_ckpt(sbi)) { 93310f966bbSChao Yu err = -EFSCORRUPTED; 934e494c2f9SChao Yu goto free_fail_no_cp; 93510f966bbSChao Yu } 936e494c2f9SChao Yu 9371dbe4152SChangman Lee if (cp_blks <= 1) 9381dbe4152SChangman Lee goto done; 9391dbe4152SChangman Lee 9401dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 9411dbe4152SChangman Lee if (cur_page == cp2) 9421dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 9431dbe4152SChangman Lee 9441dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 9451dbe4152SChangman Lee void *sit_bitmap_ptr; 9461dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 9471dbe4152SChangman Lee 9484d57b86dSChao Yu cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i); 94910f966bbSChao Yu if (IS_ERR(cur_page)) { 95010f966bbSChao Yu err = PTR_ERR(cur_page); 9517735730dSChao Yu goto free_fail_no_cp; 95210f966bbSChao Yu } 9531dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 9541dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 9551dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 9561dbe4152SChangman Lee } 9571dbe4152SChangman Lee done: 958127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 959127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 960127e670aSJaegeuk Kim return 0; 961127e670aSJaegeuk Kim 962a2125ff7SJaegeuk Kim free_fail_no_cp: 963a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1); 964a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1); 965127e670aSJaegeuk Kim fail_no_cp: 9665222595dSJaegeuk Kim kvfree(sbi->ckpt); 96710f966bbSChao Yu return err; 968127e670aSJaegeuk Kim } 969127e670aSJaegeuk Kim 970c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 971127e670aSJaegeuk Kim { 9724081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 973c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 9742d7b822aSChao Yu 97591942321SJaegeuk Kim if (is_inode_flag_set(inode, flag)) 9762710fd7eSChao Yu return; 9772d7b822aSChao Yu 97891942321SJaegeuk Kim set_inode_flag(inode, flag); 97999f4b917SChao Yu if (!f2fs_is_volatile_file(inode)) 98099f4b917SChao Yu list_add_tail(&F2FS_I(inode)->dirty_list, 98199f4b917SChao Yu &sbi->inode_list[type]); 98233fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 9835deb8267SJaegeuk Kim } 9845deb8267SJaegeuk Kim 985c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 9866ad7609aSChao Yu { 987c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 9886ad7609aSChao Yu 98991942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 9906ad7609aSChao Yu return; 9916ad7609aSChao Yu 99291942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list); 99391942321SJaegeuk Kim clear_inode_flag(inode, flag); 99433fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 9956ad7609aSChao Yu } 9966ad7609aSChao Yu 9974d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page) 9985deb8267SJaegeuk Kim { 9994081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1000c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 10015deb8267SJaegeuk Kim 10025ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 10035ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 1004127e670aSJaegeuk Kim return; 10057bd59381SGu Zheng 1006c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 10071c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 1008c227f912SChao Yu __add_dirty_inode(inode, type); 1009b951a4ecSYunlei He inode_inc_dirty_pages(inode); 10101c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]); 10111c4bf763SJaegeuk Kim 1012240a5915SChao Yu f2fs_set_page_private(page, 0); 10139e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 10145deb8267SJaegeuk Kim } 1015127e670aSJaegeuk Kim 10164d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode) 1017127e670aSJaegeuk Kim { 10184081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1019c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 1020127e670aSJaegeuk Kim 1021c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 1022c227f912SChao Yu !S_ISLNK(inode->i_mode)) 1023127e670aSJaegeuk Kim return; 1024127e670aSJaegeuk Kim 102510aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 102610aa97c3SJaegeuk Kim return; 102710aa97c3SJaegeuk Kim 1028c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1029c227f912SChao Yu __remove_dirty_inode(inode, type); 1030c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1031afc3eda2SJaegeuk Kim } 103274d0b917SJaegeuk Kim 10334d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 1034127e670aSJaegeuk Kim { 1035ce3b7d80SGu Zheng struct list_head *head; 1036127e670aSJaegeuk Kim struct inode *inode; 10372710fd7eSChao Yu struct f2fs_inode_info *fi; 10384cf18537SChao Yu bool is_dir = (type == DIR_INODE); 10394db08d01SJaegeuk Kim unsigned long ino = 0; 10404cf18537SChao Yu 10414cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 10424cf18537SChao Yu get_pages(sbi, is_dir ? 10434cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 1044127e670aSJaegeuk Kim retry: 1045af41d3eeSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 10466d5a1495SChao Yu return -EIO; 1047af41d3eeSJaegeuk Kim 1048c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 1049ce3b7d80SGu Zheng 1050c227f912SChao Yu head = &sbi->inode_list[type]; 1051127e670aSJaegeuk Kim if (list_empty(head)) { 1052c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 10534cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 10544cf18537SChao Yu get_pages(sbi, is_dir ? 10554cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 10566d5a1495SChao Yu return 0; 1057127e670aSJaegeuk Kim } 1058939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 10592710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 1060c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 1061127e670aSJaegeuk Kim if (inode) { 10624db08d01SJaegeuk Kim unsigned long cur_ino = inode->i_ino; 10634db08d01SJaegeuk Kim 1064b0af6d49SChao Yu F2FS_I(inode)->cp_task = current; 1065b0af6d49SChao Yu 106687d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 1067b0af6d49SChao Yu 1068b0af6d49SChao Yu F2FS_I(inode)->cp_task = NULL; 1069b0af6d49SChao Yu 1070127e670aSJaegeuk Kim iput(inode); 10714db08d01SJaegeuk Kim /* We need to give cpu to another writers. */ 10722a63531aSYunlei He if (ino == cur_ino) 10734db08d01SJaegeuk Kim cond_resched(); 10742a63531aSYunlei He else 10754db08d01SJaegeuk Kim ino = cur_ino; 1076127e670aSJaegeuk Kim } else { 1077127e670aSJaegeuk Kim /* 1078127e670aSJaegeuk Kim * We should submit bio, since it exists several 1079127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 1080127e670aSJaegeuk Kim */ 1081b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, DATA); 10827ecebe5eSSebastian Andrzej Siewior cond_resched(); 1083127e670aSJaegeuk Kim } 1084127e670aSJaegeuk Kim goto retry; 1085127e670aSJaegeuk Kim } 1086127e670aSJaegeuk Kim 10870f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 10880f18b462SJaegeuk Kim { 10890f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META]; 10900f18b462SJaegeuk Kim struct inode *inode; 10910f18b462SJaegeuk Kim struct f2fs_inode_info *fi; 10920f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 10930f18b462SJaegeuk Kim 10940f18b462SJaegeuk Kim while (total--) { 10950f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 10960f18b462SJaegeuk Kim return -EIO; 10970f18b462SJaegeuk Kim 10980f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 10990f18b462SJaegeuk Kim if (list_empty(head)) { 11000f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11010f18b462SJaegeuk Kim return 0; 11020f18b462SJaegeuk Kim } 1103939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, 11040f18b462SJaegeuk Kim gdirty_list); 11050f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode); 11060f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 11070f18b462SJaegeuk Kim if (inode) { 110818340edcSJaegeuk Kim sync_inode_metadata(inode, 0); 110918340edcSJaegeuk Kim 111018340edcSJaegeuk Kim /* it's on eviction */ 111118340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 11124d57b86dSChao Yu f2fs_update_inode_page(inode); 11130f18b462SJaegeuk Kim iput(inode); 11140f18b462SJaegeuk Kim } 1115dee668c1SChao Yu } 11160f18b462SJaegeuk Kim return 0; 11170f18b462SJaegeuk Kim } 11180f18b462SJaegeuk Kim 111959c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi) 112059c9081bSYunlei He { 112159c9081bSYunlei He struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 112259c9081bSYunlei He struct f2fs_nm_info *nm_i = NM_I(sbi); 112359c9081bSYunlei He nid_t last_nid = nm_i->next_scan_nid; 112459c9081bSYunlei He 112559c9081bSYunlei He next_free_nid(sbi, &last_nid); 112659c9081bSYunlei He ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 112759c9081bSYunlei He ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 112859c9081bSYunlei He ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 112959c9081bSYunlei He ckpt->next_free_nid = cpu_to_le32(last_nid); 113059c9081bSYunlei He } 113159c9081bSYunlei He 1132af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi) 1133af033b2aSChao Yu { 1134db6ec53bSJaegeuk Kim bool ret = false; 1135db6ec53bSJaegeuk Kim 1136af033b2aSChao Yu if (!is_journalled_quota(sbi)) 1137af033b2aSChao Yu return false; 1138db6ec53bSJaegeuk Kim 1139db6ec53bSJaegeuk Kim down_write(&sbi->quota_sem); 1140db6ec53bSJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { 1141db6ec53bSJaegeuk Kim ret = false; 1142db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { 1143db6ec53bSJaegeuk Kim ret = false; 1144db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) { 1145db6ec53bSJaegeuk Kim clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1146db6ec53bSJaegeuk Kim ret = true; 1147db6ec53bSJaegeuk Kim } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { 1148db6ec53bSJaegeuk Kim ret = true; 1149db6ec53bSJaegeuk Kim } 1150db6ec53bSJaegeuk Kim up_write(&sbi->quota_sem); 1151db6ec53bSJaegeuk Kim return ret; 1152af033b2aSChao Yu } 1153af033b2aSChao Yu 11540a8165d7SJaegeuk Kim /* 1155127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 1156127e670aSJaegeuk Kim */ 1157cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 1158127e670aSJaegeuk Kim { 1159127e670aSJaegeuk Kim struct writeback_control wbc = { 1160127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 1161127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 1162127e670aSJaegeuk Kim .for_reclaim = 0, 1163127e670aSJaegeuk Kim }; 1164c718379bSJaegeuk Kim struct blk_plug plug; 1165af033b2aSChao Yu int err = 0, cnt = 0; 1166c718379bSJaegeuk Kim 1167c718379bSJaegeuk Kim blk_start_plug(&plug); 1168c718379bSJaegeuk Kim 1169af033b2aSChao Yu retry_flush_quotas: 1170db6ec53bSJaegeuk Kim f2fs_lock_all(sbi); 1171af033b2aSChao Yu if (__need_flush_quota(sbi)) { 1172af033b2aSChao Yu int locked; 1173af033b2aSChao Yu 1174af033b2aSChao Yu if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { 1175af033b2aSChao Yu set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1176db6ec53bSJaegeuk Kim set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 1177af033b2aSChao Yu goto retry_flush_dents; 1178af033b2aSChao Yu } 1179db6ec53bSJaegeuk Kim f2fs_unlock_all(sbi); 1180af033b2aSChao Yu 1181af033b2aSChao Yu /* only failed during mount/umount/freeze/quotactl */ 1182af033b2aSChao Yu locked = down_read_trylock(&sbi->sb->s_umount); 1183af033b2aSChao Yu f2fs_quota_sync(sbi->sb, -1); 1184af033b2aSChao Yu if (locked) 1185af033b2aSChao Yu up_read(&sbi->sb->s_umount); 1186af033b2aSChao Yu cond_resched(); 1187af033b2aSChao Yu goto retry_flush_quotas; 1188af033b2aSChao Yu } 1189af033b2aSChao Yu 1190af033b2aSChao Yu retry_flush_dents: 1191127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 1192127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 1193e479556bSGu Zheng f2fs_unlock_all(sbi); 11944d57b86dSChao Yu err = f2fs_sync_dirty_inodes(sbi, DIR_INODE); 11956d5a1495SChao Yu if (err) 1196cf779cabSJaegeuk Kim goto out; 119730973883SJaegeuk Kim cond_resched(); 1198af033b2aSChao Yu goto retry_flush_quotas; 1199127e670aSJaegeuk Kim } 1200127e670aSJaegeuk Kim 120159c9081bSYunlei He /* 120259c9081bSYunlei He * POR: we should ensure that there are no dirty node pages 120359c9081bSYunlei He * until finishing nat/sit flush. inode->i_blocks can be updated. 120459c9081bSYunlei He */ 120559c9081bSYunlei He down_write(&sbi->node_change); 120659c9081bSYunlei He 12070f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 120859c9081bSYunlei He up_write(&sbi->node_change); 12090f18b462SJaegeuk Kim f2fs_unlock_all(sbi); 12100f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi); 12110f18b462SJaegeuk Kim if (err) 12120f18b462SJaegeuk Kim goto out; 121330973883SJaegeuk Kim cond_resched(); 1214af033b2aSChao Yu goto retry_flush_quotas; 12150f18b462SJaegeuk Kim } 12160f18b462SJaegeuk Kim 121739936837SJaegeuk Kim retry_flush_nodes: 1218b3582c68SChao Yu down_write(&sbi->node_write); 1219127e670aSJaegeuk Kim 1220127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1221b3582c68SChao Yu up_write(&sbi->node_write); 1222c29fd0c0SChao Yu atomic_inc(&sbi->wb_sync_req[NODE]); 12234d57b86dSChao Yu err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); 1224c29fd0c0SChao Yu atomic_dec(&sbi->wb_sync_req[NODE]); 12256d5a1495SChao Yu if (err) { 122659c9081bSYunlei He up_write(&sbi->node_change); 1227cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 1228cf779cabSJaegeuk Kim goto out; 1229cf779cabSJaegeuk Kim } 123030973883SJaegeuk Kim cond_resched(); 123139936837SJaegeuk Kim goto retry_flush_nodes; 1232127e670aSJaegeuk Kim } 123359c9081bSYunlei He 123459c9081bSYunlei He /* 123559c9081bSYunlei He * sbi->node_change is used only for AIO write_begin path which produces 123659c9081bSYunlei He * dirty node blocks and some checkpoint values by block allocation. 123759c9081bSYunlei He */ 123859c9081bSYunlei He __prepare_cp_block(sbi); 123959c9081bSYunlei He up_write(&sbi->node_change); 1240cf779cabSJaegeuk Kim out: 1241c718379bSJaegeuk Kim blk_finish_plug(&plug); 1242cf779cabSJaegeuk Kim return err; 1243127e670aSJaegeuk Kim } 1244127e670aSJaegeuk Kim 1245127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 1246127e670aSJaegeuk Kim { 1247b3582c68SChao Yu up_write(&sbi->node_write); 1248e479556bSGu Zheng f2fs_unlock_all(sbi); 1249127e670aSJaegeuk Kim } 1250127e670aSJaegeuk Kim 1251bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type) 1252fb51b5efSChangman Lee { 1253fb51b5efSChangman Lee DEFINE_WAIT(wait); 1254fb51b5efSChangman Lee 1255fb51b5efSChangman Lee for (;;) { 1256fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 1257fb51b5efSChangman Lee 1258bf22c3ccSSahitya Tummala if (!get_pages(sbi, type)) 1259fb51b5efSChangman Lee break; 1260fb51b5efSChangman Lee 1261af697c0fSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1262af697c0fSJaegeuk Kim break; 1263af697c0fSJaegeuk Kim 12645df7731fSChao Yu io_schedule_timeout(DEFAULT_IO_TIMEOUT); 1265fb51b5efSChangman Lee } 1266fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 1267fb51b5efSChangman Lee } 1268fb51b5efSChangman Lee 1269e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1270e4c5d848SJaegeuk Kim { 1271e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1272e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1273d1aa2453SChao Yu unsigned long flags; 1274e4c5d848SJaegeuk Kim 1275d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1276e4c5d848SJaegeuk Kim 1277c473f1a9SChao Yu if ((cpc->reason & CP_UMOUNT) && 127810047f53SKinglong Mee le32_to_cpu(ckpt->cp_pack_total_block_count) > 127922ad0b6aSJaegeuk Kim sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks) 128022ad0b6aSJaegeuk Kim disable_nat_bits(sbi, false); 128122ad0b6aSJaegeuk Kim 12821f43e2adSChao Yu if (cpc->reason & CP_TRIMMED) 12831f43e2adSChao Yu __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 1284cd36d7a1SChao Yu else 1285cd36d7a1SChao Yu __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG); 12861f43e2adSChao Yu 1287c473f1a9SChao Yu if (cpc->reason & CP_UMOUNT) 1288e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1289e4c5d848SJaegeuk Kim else 1290e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1291e4c5d848SJaegeuk Kim 1292c473f1a9SChao Yu if (cpc->reason & CP_FASTBOOT) 1293e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1294e4c5d848SJaegeuk Kim else 1295e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1296e4c5d848SJaegeuk Kim 1297e4c5d848SJaegeuk Kim if (orphan_num) 1298e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1299e4c5d848SJaegeuk Kim else 1300e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1301e4c5d848SJaegeuk Kim 1302c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1303e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1304e4c5d848SJaegeuk Kim 1305c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS)) 1306c84ef3c5SSahitya Tummala __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1307c84ef3c5SSahitya Tummala else 1308c84ef3c5SSahitya Tummala __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); 1309c84ef3c5SSahitya Tummala 13104354994fSDaniel Rosenberg if (is_sbi_flag_set(sbi, SBI_CP_DISABLED)) 13114354994fSDaniel Rosenberg __set_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13124354994fSDaniel Rosenberg else 13134354994fSDaniel Rosenberg __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); 13144354994fSDaniel Rosenberg 1315db610a64SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK)) 1316db610a64SJaegeuk Kim __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1317db610a64SJaegeuk Kim else 1318db610a64SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); 1319db610a64SJaegeuk Kim 1320af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) 1321af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1322bc88ac96SJaegeuk Kim else 1323bc88ac96SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1324af033b2aSChao Yu 1325af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) 1326af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); 1327af033b2aSChao Yu 1328e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */ 1329e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1330f2367923SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); 1331e4c5d848SJaegeuk Kim 1332d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1333e4c5d848SJaegeuk Kim } 1334e4c5d848SJaegeuk Kim 133546706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi, 133646706d59SGao Xiang void *src, block_t blk_addr) 133746706d59SGao Xiang { 133846706d59SGao Xiang struct writeback_control wbc = { 133946706d59SGao Xiang .for_reclaim = 0, 134046706d59SGao Xiang }; 134146706d59SGao Xiang 134246706d59SGao Xiang /* 134346706d59SGao Xiang * pagevec_lookup_tag and lock_page again will take 13444d57b86dSChao Yu * some extra time. Therefore, f2fs_update_meta_pages and 13454d57b86dSChao Yu * f2fs_sync_meta_pages are combined in this function. 134646706d59SGao Xiang */ 13474d57b86dSChao Yu struct page *page = f2fs_grab_meta_page(sbi, blk_addr); 134846706d59SGao Xiang int err; 134946706d59SGao Xiang 1350bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true); 13518d64d365SChao Yu 13528d64d365SChao Yu memcpy(page_address(page), src, PAGE_SIZE); 13538d64d365SChao Yu 13548d64d365SChao Yu set_page_dirty(page); 135546706d59SGao Xiang if (unlikely(!clear_page_dirty_for_io(page))) 135646706d59SGao Xiang f2fs_bug_on(sbi, 1); 135746706d59SGao Xiang 135846706d59SGao Xiang /* writeout cp pack 2 page */ 135946706d59SGao Xiang err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO); 1360af697c0fSJaegeuk Kim if (unlikely(err && f2fs_cp_error(sbi))) { 1361af697c0fSJaegeuk Kim f2fs_put_page(page, 1); 1362af697c0fSJaegeuk Kim return; 1363af697c0fSJaegeuk Kim } 136446706d59SGao Xiang 1365af697c0fSJaegeuk Kim f2fs_bug_on(sbi, err); 136646706d59SGao Xiang f2fs_put_page(page, 0); 136746706d59SGao Xiang 136846706d59SGao Xiang /* submit checkpoint (with barrier if NOBARRIER is not set) */ 136946706d59SGao Xiang f2fs_submit_merged_write(sbi, META_FLUSH); 137046706d59SGao Xiang } 137146706d59SGao Xiang 1372c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1373127e670aSJaegeuk Kim { 1374127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 137577041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 1376d1aa2453SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags; 1377127e670aSJaegeuk Kim block_t start_blk; 1378127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 13797e586fa0SJaegeuk Kim __u32 crc32 = 0; 1380127e670aSJaegeuk Kim int i; 138155141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 13828f1dbbbbSShuoran Liu struct super_block *sb = sbi->sb; 13838f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 13848f1dbbbbSShuoran Liu u64 kbytes_written; 13851228b482SChao Yu int err; 1386127e670aSJaegeuk Kim 1387127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 13884d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1389127e670aSJaegeuk Kim 13907a88ddb5SChao Yu /* start to update checkpoint, cp ver is already updated previously */ 1391a1f72ac2SChao Yu ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true)); 1392127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1393b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1394127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 1395127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 1396127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 1397127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 1398127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 1399127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 1400127e670aSJaegeuk Kim } 1401b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1402127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 1403127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 1404127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 1405127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 1406127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 1407127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 1408127e670aSJaegeuk Kim } 1409127e670aSJaegeuk Kim 1410127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 14114d57b86dSChao Yu data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); 1412d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1413b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1414aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1415127e670aSJaegeuk Kim else 1416aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1417d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1418127e670aSJaegeuk Kim 141967298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 14201dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 14211dbe4152SChangman Lee orphan_blocks); 1422127e670aSJaegeuk Kim 1423119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1424b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+ 14251dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14261dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1427119ee914SJaegeuk Kim else 1428b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 14291dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 14301dbe4152SChangman Lee orphan_blocks); 1431119ee914SJaegeuk Kim 1432e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */ 1433e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc); 1434a468f0efSJaegeuk Kim 1435127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1436127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1437127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1438127e670aSJaegeuk Kim 1439d7eb8f1cSChao Yu crc32 = f2fs_checkpoint_chksum(sbi, ckpt); 14407e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 14417e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1442127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1443127e670aSJaegeuk Kim 14448508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi); 1445127e670aSJaegeuk Kim 144622ad0b6aSJaegeuk Kim /* write nat bits */ 144722ad0b6aSJaegeuk Kim if (enabled_nat_bits(sbi, cpc)) { 144822ad0b6aSJaegeuk Kim __u64 cp_ver = cur_cp_version(ckpt); 144922ad0b6aSJaegeuk Kim block_t blk; 145022ad0b6aSJaegeuk Kim 145122ad0b6aSJaegeuk Kim cp_ver |= ((__u64)crc32 << 32); 145222ad0b6aSJaegeuk Kim *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); 145322ad0b6aSJaegeuk Kim 145422ad0b6aSJaegeuk Kim blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks; 145522ad0b6aSJaegeuk Kim for (i = 0; i < nm_i->nat_bits_blocks; i++) 14564d57b86dSChao Yu f2fs_update_meta_page(sbi, nm_i->nat_bits + 145722ad0b6aSJaegeuk Kim (i << F2FS_BLKSIZE_BITS), blk + i); 145822ad0b6aSJaegeuk Kim } 145922ad0b6aSJaegeuk Kim 1460127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 14614d57b86dSChao Yu f2fs_update_meta_page(sbi, ckpt, start_blk++); 1462127e670aSJaegeuk Kim 1463381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 14644d57b86dSChao Yu f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1465381722d2SChao Yu start_blk++); 14661dbe4152SChangman Lee 146767298804SChao Yu if (orphan_num) { 1468127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1469127e670aSJaegeuk Kim start_blk += orphan_blocks; 1470127e670aSJaegeuk Kim } 1471127e670aSJaegeuk Kim 14724d57b86dSChao Yu f2fs_write_data_summaries(sbi, start_blk); 1473127e670aSJaegeuk Kim start_blk += data_sum_blocks; 14748f1dbbbbSShuoran Liu 14758f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 14768f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 14778f1dbbbbSShuoran Liu if (sb->s_bdev->bd_part) 14788f1dbbbbSShuoran Liu kbytes_written += BD_PART_WRITTEN(sbi); 14798f1dbbbbSShuoran Liu 1480b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 14818f1dbbbbSShuoran Liu 1482119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 14834d57b86dSChao Yu f2fs_write_node_summaries(sbi, start_blk); 1484127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1485127e670aSJaegeuk Kim } 1486127e670aSJaegeuk Kim 148746706d59SGao Xiang /* update user_block_counts */ 148846706d59SGao Xiang sbi->last_valid_block_count = sbi->total_valid_block_count; 148946706d59SGao Xiang percpu_counter_set(&sbi->alloc_valid_block_count, 0); 1490127e670aSJaegeuk Kim 149146706d59SGao Xiang /* Here, we have one bio having CP pack except cp pack 2 page */ 14924d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); 1493bf22c3ccSSahitya Tummala /* Wait for all dirty meta pages to be submitted for IO */ 1494bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META); 149546706d59SGao Xiang 149646706d59SGao Xiang /* wait for previous submitted meta pages writeback */ 1497bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1498127e670aSJaegeuk Kim 149946706d59SGao Xiang /* flush all device cache */ 150046706d59SGao Xiang err = f2fs_flush_device_cache(sbi); 150146706d59SGao Xiang if (err) 150246706d59SGao Xiang return err; 1503127e670aSJaegeuk Kim 150446706d59SGao Xiang /* barrier and flush checkpoint cp pack 2 page if it can */ 150546706d59SGao Xiang commit_checkpoint(sbi, ckpt, start_blk); 1506bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 15076a8f8ca5SJaegeuk Kim 150818767e62SChao Yu /* 1509542989b6SEric Biggers * invalidate intermediate page cache borrowed from meta inode which are 1510542989b6SEric Biggers * used for migration of encrypted or verity inode's blocks. 151118767e62SChao Yu */ 1512542989b6SEric Biggers if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi)) 151318767e62SChao Yu invalidate_mapping_pages(META_MAPPING(sbi), 151418767e62SChao Yu MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1); 151518767e62SChao Yu 15164d57b86dSChao Yu f2fs_release_ino_entry(sbi, false); 1517cf779cabSJaegeuk Kim 151850fa53ecSChao Yu f2fs_reset_fsync_node_info(sbi); 151950fa53ecSChao Yu 1520caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1521bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP); 1522af033b2aSChao Yu clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); 1523c9c8ed50SChao Yu 1524c9c8ed50SChao Yu spin_lock(&sbi->stat_lock); 15254354994fSDaniel Rosenberg sbi->unusable_block_count = 0; 1526c9c8ed50SChao Yu spin_unlock(&sbi->stat_lock); 1527c9c8ed50SChao Yu 15288508e44aSJaegeuk Kim __set_cp_next_pack(sbi); 1529c34f42e2SChao Yu 1530c2a080aeSChao Yu /* 1531c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is 1532c2a080aeSChao Yu * updated during writing checkpoint. 1533c2a080aeSChao Yu */ 1534c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) || 1535c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA)) 1536c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 1537c2a080aeSChao Yu 1538c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1539c2a080aeSChao Yu 1540af697c0fSJaegeuk Kim return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0; 1541127e670aSJaegeuk Kim } 1542127e670aSJaegeuk Kim 15434d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1544127e670aSJaegeuk Kim { 1545127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1546127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1547c34f42e2SChao Yu int err = 0; 1548127e670aSJaegeuk Kim 1549f5a131bbSChao Yu if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi)) 1550f5a131bbSChao Yu return -EROFS; 1551f5a131bbSChao Yu 15524354994fSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 15534354994fSDaniel Rosenberg if (cpc->reason != CP_PAUSE) 15544354994fSDaniel Rosenberg return 0; 1555dcbb4c10SJoe Perches f2fs_warn(sbi, "Start checkpoint disabled!"); 15564354994fSDaniel Rosenberg } 1557127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 15588501017eSJaegeuk Kim 1559caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1560c473f1a9SChao Yu ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1561c473f1a9SChao Yu ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) 15628501017eSJaegeuk Kim goto out; 1563c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1564c34f42e2SChao Yu err = -EIO; 1565cf779cabSJaegeuk Kim goto out; 1566c34f42e2SChao Yu } 15672bda542dSWanpeng Li 15682bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 15692bda542dSWanpeng Li 1570c34f42e2SChao Yu err = block_operations(sbi); 1571c34f42e2SChao Yu if (err) 1572cf779cabSJaegeuk Kim goto out; 1573127e670aSJaegeuk Kim 157475ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 15752af4bd6cSNamjae Jeon 1576b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi); 1577127e670aSJaegeuk Kim 157858cce381SYunlei He /* this is the case of multiple fstrims without any changes */ 1579c473f1a9SChao Yu if (cpc->reason & CP_DISCARD) { 15804d57b86dSChao Yu if (!f2fs_exist_trim_candidates(sbi, cpc)) { 158125290fa5SJaegeuk Kim unblock_operations(sbi); 158225290fa5SJaegeuk Kim goto out; 158325290fa5SJaegeuk Kim } 158425290fa5SJaegeuk Kim 15850333ad4eSJaegeuk Kim if (NM_I(sbi)->dirty_nat_cnt == 0 && 15860333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 && 15870333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) { 15884d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 15894d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 159058cce381SYunlei He unblock_operations(sbi); 159158cce381SYunlei He goto out; 159258cce381SYunlei He } 15930333ad4eSJaegeuk Kim } 159458cce381SYunlei He 1595127e670aSJaegeuk Kim /* 1596127e670aSJaegeuk Kim * update checkpoint pack index 1597127e670aSJaegeuk Kim * Increase the version number so that 1598127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1599127e670aSJaegeuk Kim */ 1600d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1601127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1602127e670aSJaegeuk Kim 1603127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1604edc55aafSJaegeuk Kim err = f2fs_flush_nat_entries(sbi, cpc); 1605edc55aafSJaegeuk Kim if (err) 1606edc55aafSJaegeuk Kim goto stop; 1607edc55aafSJaegeuk Kim 16084d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc); 1609127e670aSJaegeuk Kim 1610c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 16114e6a8d9bSJaegeuk Kim if (err) 16124d57b86dSChao Yu f2fs_release_discard_addrs(sbi); 16134e6a8d9bSJaegeuk Kim else 16144d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc); 1615edc55aafSJaegeuk Kim stop: 1616127e670aSJaegeuk Kim unblock_operations(sbi); 1617942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 161810027551SJaegeuk Kim 1619c473f1a9SChao Yu if (cpc->reason & CP_RECOVERY) 1620dcbb4c10SJoe Perches f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver); 162160b99b48SJaegeuk Kim 16227a88ddb5SChao Yu /* update CP_TIME to trigger checkpoint periodically */ 16236beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 162455d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 16258501017eSJaegeuk Kim out: 16268501017eSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 1627c34f42e2SChao Yu return err; 1628127e670aSJaegeuk Kim } 1629127e670aSJaegeuk Kim 16304d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi) 1631127e670aSJaegeuk Kim { 16326451e041SJaegeuk Kim int i; 16336451e041SJaegeuk Kim 16346451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 163567298804SChao Yu struct inode_management *im = &sbi->im[i]; 163667298804SChao Yu 163767298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 163867298804SChao Yu spin_lock_init(&im->ino_lock); 163967298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 164067298804SChao Yu im->ino_num = 0; 16416451e041SJaegeuk Kim } 16426451e041SJaegeuk Kim 1643b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 164414b42817SWanpeng Li NR_CURSEG_TYPE - __cp_payload(sbi)) * 164514b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1646127e670aSJaegeuk Kim } 1647127e670aSJaegeuk Kim 16484d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void) 1649127e670aSJaegeuk Kim { 16506451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 16516451e041SJaegeuk Kim sizeof(struct ino_entry)); 16526451e041SJaegeuk Kim if (!ino_entry_slab) 1653127e670aSJaegeuk Kim return -ENOMEM; 16544d57b86dSChao Yu f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 165506292073SChao Yu sizeof(struct inode_entry)); 16564d57b86dSChao Yu if (!f2fs_inode_entry_slab) { 16576451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1658127e670aSJaegeuk Kim return -ENOMEM; 1659127e670aSJaegeuk Kim } 1660127e670aSJaegeuk Kim return 0; 1661127e670aSJaegeuk Kim } 1662127e670aSJaegeuk Kim 16634d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void) 1664127e670aSJaegeuk Kim { 16656451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 16664d57b86dSChao Yu kmem_cache_destroy(f2fs_inode_entry_slab); 1667127e670aSJaegeuk Kim } 1668