10a8165d7SJaegeuk Kim /* 2127e670aSJaegeuk Kim * fs/f2fs/checkpoint.c 3127e670aSJaegeuk Kim * 4127e670aSJaegeuk Kim * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5127e670aSJaegeuk Kim * http://www.samsung.com/ 6127e670aSJaegeuk Kim * 7127e670aSJaegeuk Kim * This program is free software; you can redistribute it and/or modify 8127e670aSJaegeuk Kim * it under the terms of the GNU General Public License version 2 as 9127e670aSJaegeuk Kim * published by the Free Software Foundation. 10127e670aSJaegeuk Kim */ 11127e670aSJaegeuk Kim #include <linux/fs.h> 12127e670aSJaegeuk Kim #include <linux/bio.h> 13127e670aSJaegeuk Kim #include <linux/mpage.h> 14127e670aSJaegeuk Kim #include <linux/writeback.h> 15127e670aSJaegeuk Kim #include <linux/blkdev.h> 16127e670aSJaegeuk Kim #include <linux/f2fs_fs.h> 17127e670aSJaegeuk Kim #include <linux/pagevec.h> 18127e670aSJaegeuk Kim #include <linux/swap.h> 19127e670aSJaegeuk Kim 20127e670aSJaegeuk Kim #include "f2fs.h" 21127e670aSJaegeuk Kim #include "node.h" 22127e670aSJaegeuk Kim #include "segment.h" 239e4ded3fSJaegeuk Kim #include "trace.h" 242af4bd6cSNamjae Jeon #include <trace/events/f2fs.h> 25127e670aSJaegeuk Kim 266451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab; 2706292073SChao Yu struct kmem_cache *inode_entry_slab; 28127e670aSJaegeuk Kim 290a8165d7SJaegeuk Kim /* 30127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 31127e670aSJaegeuk Kim */ 32127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 33127e670aSJaegeuk Kim { 349df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 35127e670aSJaegeuk Kim struct page *page = NULL; 36127e670aSJaegeuk Kim repeat: 37bde44686SJaegeuk Kim page = grab_cache_page(mapping, index); 38127e670aSJaegeuk Kim if (!page) { 39127e670aSJaegeuk Kim cond_resched(); 40127e670aSJaegeuk Kim goto repeat; 41127e670aSJaegeuk Kim } 42bde44686SJaegeuk Kim f2fs_wait_on_page_writeback(page, META); 43127e670aSJaegeuk Kim SetPageUptodate(page); 44127e670aSJaegeuk Kim return page; 45127e670aSJaegeuk Kim } 46127e670aSJaegeuk Kim 470a8165d7SJaegeuk Kim /* 48127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 49127e670aSJaegeuk Kim */ 502b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index, 512b947003SChao Yu bool is_meta) 52127e670aSJaegeuk Kim { 539df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 54127e670aSJaegeuk Kim struct page *page; 55cf04e8ebSJaegeuk Kim struct f2fs_io_info fio = { 5605ca3632SJaegeuk Kim .sbi = sbi, 57cf04e8ebSJaegeuk Kim .type = META, 58cf04e8ebSJaegeuk Kim .rw = READ_SYNC | REQ_META | REQ_PRIO, 59cf04e8ebSJaegeuk Kim .blk_addr = index, 604375a336SJaegeuk Kim .encrypted_page = NULL, 61cf04e8ebSJaegeuk Kim }; 622b947003SChao Yu 632b947003SChao Yu if (unlikely(!is_meta)) 642b947003SChao Yu fio.rw &= ~REQ_META; 65127e670aSJaegeuk Kim repeat: 66127e670aSJaegeuk Kim page = grab_cache_page(mapping, index); 67127e670aSJaegeuk Kim if (!page) { 68127e670aSJaegeuk Kim cond_resched(); 69127e670aSJaegeuk Kim goto repeat; 70127e670aSJaegeuk Kim } 71393ff91fSJaegeuk Kim if (PageUptodate(page)) 72393ff91fSJaegeuk Kim goto out; 73127e670aSJaegeuk Kim 7405ca3632SJaegeuk Kim fio.page = page; 7505ca3632SJaegeuk Kim 7686531d6bSJaegeuk Kim if (f2fs_submit_page_bio(&fio)) { 7786531d6bSJaegeuk Kim f2fs_put_page(page, 1); 78393ff91fSJaegeuk Kim goto repeat; 7986531d6bSJaegeuk Kim } 80393ff91fSJaegeuk Kim 81393ff91fSJaegeuk Kim lock_page(page); 826bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) { 83afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 84afcb7ca0SJaegeuk Kim goto repeat; 85afcb7ca0SJaegeuk Kim } 86f3f338caSChao Yu 87f3f338caSChao Yu /* 88f3f338caSChao Yu * if there is any IO error when accessing device, make our filesystem 89f3f338caSChao Yu * readonly and make sure do not write checkpoint with non-uptodate 90f3f338caSChao Yu * meta page. 91f3f338caSChao Yu */ 92f3f338caSChao Yu if (unlikely(!PageUptodate(page))) 93f3f338caSChao Yu f2fs_stop_checkpoint(sbi); 94393ff91fSJaegeuk Kim out: 95127e670aSJaegeuk Kim return page; 96127e670aSJaegeuk Kim } 97127e670aSJaegeuk Kim 982b947003SChao Yu struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 992b947003SChao Yu { 1002b947003SChao Yu return __get_meta_page(sbi, index, true); 1012b947003SChao Yu } 1022b947003SChao Yu 1032b947003SChao Yu /* for POR only */ 1042b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 1052b947003SChao Yu { 1062b947003SChao Yu return __get_meta_page(sbi, index, false); 1072b947003SChao Yu } 1082b947003SChao Yu 109f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type) 110662befdaSChao Yu { 111662befdaSChao Yu switch (type) { 112662befdaSChao Yu case META_NAT: 11366b00c18SChao Yu break; 114662befdaSChao Yu case META_SIT: 11566b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 11666b00c18SChao Yu return false; 11766b00c18SChao Yu break; 11881c1a0f1SChao Yu case META_SSA: 11966b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 12066b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr)) 12166b00c18SChao Yu return false; 12266b00c18SChao Yu break; 123662befdaSChao Yu case META_CP: 12466b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 12566b00c18SChao Yu blkaddr < __start_cp_addr(sbi))) 12666b00c18SChao Yu return false; 12766b00c18SChao Yu break; 1284c521f49SJaegeuk Kim case META_POR: 12966b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 13066b00c18SChao Yu blkaddr < MAIN_BLKADDR(sbi))) 13166b00c18SChao Yu return false; 13266b00c18SChao Yu break; 133662befdaSChao Yu default: 134662befdaSChao Yu BUG(); 135662befdaSChao Yu } 13666b00c18SChao Yu 13766b00c18SChao Yu return true; 138662befdaSChao Yu } 139662befdaSChao Yu 140662befdaSChao Yu /* 14181c1a0f1SChao Yu * Readahead CP/NAT/SIT/SSA pages 142662befdaSChao Yu */ 14326879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 14426879fb1SChao Yu int type, bool sync) 145662befdaSChao Yu { 146662befdaSChao Yu block_t prev_blk_addr = 0; 147662befdaSChao Yu struct page *page; 1484c521f49SJaegeuk Kim block_t blkno = start; 149662befdaSChao Yu struct f2fs_io_info fio = { 15005ca3632SJaegeuk Kim .sbi = sbi, 151662befdaSChao Yu .type = META, 15226879fb1SChao Yu .rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA, 1534375a336SJaegeuk Kim .encrypted_page = NULL, 154662befdaSChao Yu }; 155662befdaSChao Yu 1562b947003SChao Yu if (unlikely(type == META_POR)) 1572b947003SChao Yu fio.rw &= ~REQ_META; 1582b947003SChao Yu 159662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 160662befdaSChao Yu 16166b00c18SChao Yu if (!is_valid_blkaddr(sbi, blkno, type)) 16266b00c18SChao Yu goto out; 16366b00c18SChao Yu 164662befdaSChao Yu switch (type) { 165662befdaSChao Yu case META_NAT: 16666b00c18SChao Yu if (unlikely(blkno >= 16766b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 168662befdaSChao Yu blkno = 0; 16966b00c18SChao Yu /* get nat block addr */ 170cf04e8ebSJaegeuk Kim fio.blk_addr = current_nat_addr(sbi, 171662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 172662befdaSChao Yu break; 173662befdaSChao Yu case META_SIT: 174662befdaSChao Yu /* get sit block addr */ 175cf04e8ebSJaegeuk Kim fio.blk_addr = current_sit_addr(sbi, 176662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 177cf04e8ebSJaegeuk Kim if (blkno != start && prev_blk_addr + 1 != fio.blk_addr) 178662befdaSChao Yu goto out; 179cf04e8ebSJaegeuk Kim prev_blk_addr = fio.blk_addr; 180662befdaSChao Yu break; 18181c1a0f1SChao Yu case META_SSA: 182662befdaSChao Yu case META_CP: 1834c521f49SJaegeuk Kim case META_POR: 184cf04e8ebSJaegeuk Kim fio.blk_addr = blkno; 185662befdaSChao Yu break; 186662befdaSChao Yu default: 187662befdaSChao Yu BUG(); 188662befdaSChao Yu } 189662befdaSChao Yu 190cf04e8ebSJaegeuk Kim page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr); 191662befdaSChao Yu if (!page) 192662befdaSChao Yu continue; 193662befdaSChao Yu if (PageUptodate(page)) { 194662befdaSChao Yu f2fs_put_page(page, 1); 195662befdaSChao Yu continue; 196662befdaSChao Yu } 197662befdaSChao Yu 19805ca3632SJaegeuk Kim fio.page = page; 19905ca3632SJaegeuk Kim f2fs_submit_page_mbio(&fio); 200662befdaSChao Yu f2fs_put_page(page, 0); 201662befdaSChao Yu } 202662befdaSChao Yu out: 203662befdaSChao Yu f2fs_submit_merged_bio(sbi, META, READ); 204662befdaSChao Yu return blkno - start; 205662befdaSChao Yu } 206662befdaSChao Yu 207635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index) 208635aee1fSChao Yu { 209635aee1fSChao Yu struct page *page; 210635aee1fSChao Yu bool readahead = false; 211635aee1fSChao Yu 212635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 213635aee1fSChao Yu if (!page || (page && !PageUptodate(page))) 214635aee1fSChao Yu readahead = true; 215635aee1fSChao Yu f2fs_put_page(page, 0); 216635aee1fSChao Yu 217635aee1fSChao Yu if (readahead) 21826879fb1SChao Yu ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true); 219635aee1fSChao Yu } 220635aee1fSChao Yu 221127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page, 222127e670aSJaegeuk Kim struct writeback_control *wbc) 223127e670aSJaegeuk Kim { 2244081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 225577e3495SJaegeuk Kim 226ecda0de3SChao Yu trace_f2fs_writepage(page, META); 227ecda0de3SChao Yu 228caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 229cfb271d4SChao Yu goto redirty_out; 230857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 231cfb271d4SChao Yu goto redirty_out; 2321e968fdfSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 233cf779cabSJaegeuk Kim goto redirty_out; 234127e670aSJaegeuk Kim 2353cb5ad15SJaegeuk Kim f2fs_wait_on_page_writeback(page, META); 236577e3495SJaegeuk Kim write_meta_page(sbi, page); 237127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 238127e670aSJaegeuk Kim unlock_page(page); 239857dc4e0SJaegeuk Kim 2408d4ea29bSJaegeuk Kim if (wbc->for_reclaim || unlikely(f2fs_cp_error(sbi))) 241857dc4e0SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 242577e3495SJaegeuk Kim return 0; 243cfb271d4SChao Yu 244cfb271d4SChao Yu redirty_out: 24576f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 246cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 247127e670aSJaegeuk Kim } 248127e670aSJaegeuk Kim 249127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 250127e670aSJaegeuk Kim struct writeback_control *wbc) 251127e670aSJaegeuk Kim { 2524081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 25350c8cdb3SJaegeuk Kim long diff, written; 254127e670aSJaegeuk Kim 255e5748434SChao Yu trace_f2fs_writepages(mapping->host, wbc, META); 256e5748434SChao Yu 2575459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 25850c8cdb3SJaegeuk Kim if (wbc->for_kupdate || 25950c8cdb3SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META)) 260d3baf95dSJaegeuk Kim goto skip_write; 261127e670aSJaegeuk Kim 262127e670aSJaegeuk Kim /* if mounting is failed, skip writing node pages */ 263127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 26450c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 26550c8cdb3SJaegeuk Kim written = sync_meta_pages(sbi, META, wbc->nr_to_write); 266127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 26750c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 268127e670aSJaegeuk Kim return 0; 269d3baf95dSJaegeuk Kim 270d3baf95dSJaegeuk Kim skip_write: 271d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 272d3baf95dSJaegeuk Kim return 0; 273127e670aSJaegeuk Kim } 274127e670aSJaegeuk Kim 275127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 276127e670aSJaegeuk Kim long nr_to_write) 277127e670aSJaegeuk Kim { 2789df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 2796066d8cdSJaegeuk Kim pgoff_t index = 0, end = LONG_MAX, prev = LONG_MAX; 280127e670aSJaegeuk Kim struct pagevec pvec; 281127e670aSJaegeuk Kim long nwritten = 0; 282127e670aSJaegeuk Kim struct writeback_control wbc = { 283127e670aSJaegeuk Kim .for_reclaim = 0, 284127e670aSJaegeuk Kim }; 285127e670aSJaegeuk Kim 286127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 287127e670aSJaegeuk Kim 288127e670aSJaegeuk Kim while (index <= end) { 289127e670aSJaegeuk Kim int i, nr_pages; 290127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 291127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 292127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 293cfb271d4SChao Yu if (unlikely(nr_pages == 0)) 294127e670aSJaegeuk Kim break; 295127e670aSJaegeuk Kim 296127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 297127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 298203681f6SJaegeuk Kim 2996066d8cdSJaegeuk Kim if (prev == LONG_MAX) 3006066d8cdSJaegeuk Kim prev = page->index - 1; 3016066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 3026066d8cdSJaegeuk Kim pagevec_release(&pvec); 3036066d8cdSJaegeuk Kim goto stop; 3046066d8cdSJaegeuk Kim } 3056066d8cdSJaegeuk Kim 306127e670aSJaegeuk Kim lock_page(page); 307203681f6SJaegeuk Kim 308203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 309203681f6SJaegeuk Kim continue_unlock: 310203681f6SJaegeuk Kim unlock_page(page); 311203681f6SJaegeuk Kim continue; 312203681f6SJaegeuk Kim } 313203681f6SJaegeuk Kim if (!PageDirty(page)) { 314203681f6SJaegeuk Kim /* someone wrote it for us */ 315203681f6SJaegeuk Kim goto continue_unlock; 316203681f6SJaegeuk Kim } 317203681f6SJaegeuk Kim 318203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 319203681f6SJaegeuk Kim goto continue_unlock; 320203681f6SJaegeuk Kim 32197dc3fd2SChao Yu if (mapping->a_ops->writepage(page, &wbc)) { 322577e3495SJaegeuk Kim unlock_page(page); 323577e3495SJaegeuk Kim break; 324577e3495SJaegeuk Kim } 325cfb271d4SChao Yu nwritten++; 3266066d8cdSJaegeuk Kim prev = page->index; 327cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 328127e670aSJaegeuk Kim break; 329127e670aSJaegeuk Kim } 330127e670aSJaegeuk Kim pagevec_release(&pvec); 331127e670aSJaegeuk Kim cond_resched(); 332127e670aSJaegeuk Kim } 3336066d8cdSJaegeuk Kim stop: 334127e670aSJaegeuk Kim if (nwritten) 335458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, type, WRITE); 336127e670aSJaegeuk Kim 337127e670aSJaegeuk Kim return nwritten; 338127e670aSJaegeuk Kim } 339127e670aSJaegeuk Kim 340127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 341127e670aSJaegeuk Kim { 34226c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 34326c6b887SJaegeuk Kim 344127e670aSJaegeuk Kim SetPageUptodate(page); 345127e670aSJaegeuk Kim if (!PageDirty(page)) { 346127e670aSJaegeuk Kim __set_page_dirty_nobuffers(page); 3474081363fSJaegeuk Kim inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); 3481601839eSChao Yu SetPagePrivate(page); 3499e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 350127e670aSJaegeuk Kim return 1; 351127e670aSJaegeuk Kim } 352127e670aSJaegeuk Kim return 0; 353127e670aSJaegeuk Kim } 354127e670aSJaegeuk Kim 355127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 356127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 357127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 358127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 359487261f3SChao Yu .invalidatepage = f2fs_invalidate_page, 360487261f3SChao Yu .releasepage = f2fs_release_page, 361127e670aSJaegeuk Kim }; 362127e670aSJaegeuk Kim 3636451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 364953e6cc6SJaegeuk Kim { 36567298804SChao Yu struct inode_management *im = &sbi->im[type]; 36680c54505SJaegeuk Kim struct ino_entry *e, *tmp; 36780c54505SJaegeuk Kim 36880c54505SJaegeuk Kim tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS); 36939efac41SJaegeuk Kim retry: 37080c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 371769ec6e5SJaegeuk Kim 37267298804SChao Yu spin_lock(&im->ino_lock); 37367298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 37439efac41SJaegeuk Kim if (!e) { 37580c54505SJaegeuk Kim e = tmp; 37667298804SChao Yu if (radix_tree_insert(&im->ino_root, ino, e)) { 37767298804SChao Yu spin_unlock(&im->ino_lock); 378769ec6e5SJaegeuk Kim radix_tree_preload_end(); 37939efac41SJaegeuk Kim goto retry; 38039efac41SJaegeuk Kim } 38139efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 38239efac41SJaegeuk Kim e->ino = ino; 38339efac41SJaegeuk Kim 38467298804SChao Yu list_add_tail(&e->list, &im->ino_list); 3858c402946SJaegeuk Kim if (type != ORPHAN_INO) 38667298804SChao Yu im->ino_num++; 38739efac41SJaegeuk Kim } 38867298804SChao Yu spin_unlock(&im->ino_lock); 389769ec6e5SJaegeuk Kim radix_tree_preload_end(); 39080c54505SJaegeuk Kim 39180c54505SJaegeuk Kim if (e != tmp) 39280c54505SJaegeuk Kim kmem_cache_free(ino_entry_slab, tmp); 393953e6cc6SJaegeuk Kim } 394953e6cc6SJaegeuk Kim 3956451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 396953e6cc6SJaegeuk Kim { 39767298804SChao Yu struct inode_management *im = &sbi->im[type]; 3986451e041SJaegeuk Kim struct ino_entry *e; 399953e6cc6SJaegeuk Kim 40067298804SChao Yu spin_lock(&im->ino_lock); 40167298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 40239efac41SJaegeuk Kim if (e) { 403953e6cc6SJaegeuk Kim list_del(&e->list); 40467298804SChao Yu radix_tree_delete(&im->ino_root, ino); 40567298804SChao Yu im->ino_num--; 40667298804SChao Yu spin_unlock(&im->ino_lock); 4076451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 408953e6cc6SJaegeuk Kim return; 409953e6cc6SJaegeuk Kim } 41067298804SChao Yu spin_unlock(&im->ino_lock); 411953e6cc6SJaegeuk Kim } 412953e6cc6SJaegeuk Kim 413a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 414fff04f90SJaegeuk Kim { 415fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 416fff04f90SJaegeuk Kim __add_ino_entry(sbi, ino, type); 417fff04f90SJaegeuk Kim } 418fff04f90SJaegeuk Kim 419a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 420fff04f90SJaegeuk Kim { 421fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 422fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 423fff04f90SJaegeuk Kim } 424fff04f90SJaegeuk Kim 425fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */ 426fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 427fff04f90SJaegeuk Kim { 42867298804SChao Yu struct inode_management *im = &sbi->im[mode]; 429fff04f90SJaegeuk Kim struct ino_entry *e; 43067298804SChao Yu 43167298804SChao Yu spin_lock(&im->ino_lock); 43267298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 43367298804SChao Yu spin_unlock(&im->ino_lock); 434fff04f90SJaegeuk Kim return e ? true : false; 435fff04f90SJaegeuk Kim } 436fff04f90SJaegeuk Kim 437a49324f1SChao Yu void release_ino_entry(struct f2fs_sb_info *sbi) 438fff04f90SJaegeuk Kim { 439fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 440fff04f90SJaegeuk Kim int i; 441fff04f90SJaegeuk Kim 442fff04f90SJaegeuk Kim for (i = APPEND_INO; i <= UPDATE_INO; i++) { 44367298804SChao Yu struct inode_management *im = &sbi->im[i]; 44467298804SChao Yu 44567298804SChao Yu spin_lock(&im->ino_lock); 44667298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 447fff04f90SJaegeuk Kim list_del(&e->list); 44867298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 449fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 45067298804SChao Yu im->ino_num--; 451fff04f90SJaegeuk Kim } 45267298804SChao Yu spin_unlock(&im->ino_lock); 453fff04f90SJaegeuk Kim } 454fff04f90SJaegeuk Kim } 455fff04f90SJaegeuk Kim 456cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi) 457127e670aSJaegeuk Kim { 45867298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 459127e670aSJaegeuk Kim int err = 0; 460127e670aSJaegeuk Kim 46167298804SChao Yu spin_lock(&im->ino_lock); 46267298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 463127e670aSJaegeuk Kim err = -ENOSPC; 464cbd56e7dSJaegeuk Kim else 46567298804SChao Yu im->ino_num++; 46667298804SChao Yu spin_unlock(&im->ino_lock); 4670d47c1adSGu Zheng 468127e670aSJaegeuk Kim return err; 469127e670aSJaegeuk Kim } 470127e670aSJaegeuk Kim 471cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi) 472cbd56e7dSJaegeuk Kim { 47367298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 47467298804SChao Yu 47567298804SChao Yu spin_lock(&im->ino_lock); 47667298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 47767298804SChao Yu im->ino_num--; 47867298804SChao Yu spin_unlock(&im->ino_lock); 479cbd56e7dSJaegeuk Kim } 480cbd56e7dSJaegeuk Kim 481127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 482127e670aSJaegeuk Kim { 48339efac41SJaegeuk Kim /* add new orphan ino entry into list */ 4846451e041SJaegeuk Kim __add_ino_entry(sbi, ino, ORPHAN_INO); 485127e670aSJaegeuk Kim } 486127e670aSJaegeuk Kim 487127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 488127e670aSJaegeuk Kim { 489953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 4906451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 491127e670aSJaegeuk Kim } 492127e670aSJaegeuk Kim 4938c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 494127e670aSJaegeuk Kim { 4958c14bfadSChao Yu struct inode *inode; 4968c14bfadSChao Yu 4978c14bfadSChao Yu inode = f2fs_iget(sbi->sb, ino); 4988c14bfadSChao Yu if (IS_ERR(inode)) { 4998c14bfadSChao Yu /* 5008c14bfadSChao Yu * there should be a bug that we can't find the entry 5018c14bfadSChao Yu * to orphan inode. 5028c14bfadSChao Yu */ 5038c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 5048c14bfadSChao Yu return PTR_ERR(inode); 5058c14bfadSChao Yu } 5068c14bfadSChao Yu 507127e670aSJaegeuk Kim clear_nlink(inode); 508127e670aSJaegeuk Kim 509127e670aSJaegeuk Kim /* truncate all the data during iput */ 510127e670aSJaegeuk Kim iput(inode); 5118c14bfadSChao Yu return 0; 512127e670aSJaegeuk Kim } 513127e670aSJaegeuk Kim 5148c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi) 515127e670aSJaegeuk Kim { 5163c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 5178c14bfadSChao Yu int err; 518127e670aSJaegeuk Kim 51925ca923bSJaegeuk Kim if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG)) 5208c14bfadSChao Yu return 0; 521127e670aSJaegeuk Kim 52255141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 5233c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 524127e670aSJaegeuk Kim 52526879fb1SChao Yu ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 526662befdaSChao Yu 5273c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 528127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 529127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 530127e670aSJaegeuk Kim 531127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 532127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 533127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 5348c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 5358c14bfadSChao Yu if (err) { 5368c14bfadSChao Yu f2fs_put_page(page, 1); 5378c14bfadSChao Yu return err; 5388c14bfadSChao Yu } 539127e670aSJaegeuk Kim } 540127e670aSJaegeuk Kim f2fs_put_page(page, 1); 541127e670aSJaegeuk Kim } 542127e670aSJaegeuk Kim /* clear Orphan Flag */ 54325ca923bSJaegeuk Kim clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG); 5448c14bfadSChao Yu return 0; 545127e670aSJaegeuk Kim } 546127e670aSJaegeuk Kim 547127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 548127e670aSJaegeuk Kim { 549502c6e0bSGu Zheng struct list_head *head; 550127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 551127e670aSJaegeuk Kim unsigned int nentries = 0; 552bd936f84SChao Yu unsigned short index = 1; 5538c402946SJaegeuk Kim unsigned short orphan_blocks; 5544531929eSGu Zheng struct page *page = NULL; 5556451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 55667298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 557127e670aSJaegeuk Kim 55867298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 5598c402946SJaegeuk Kim 560d6c67a4fSJaegeuk Kim /* 561d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 562d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 563d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 564d6c67a4fSJaegeuk Kim */ 56567298804SChao Yu head = &im->ino_list; 566127e670aSJaegeuk Kim 567127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 568502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 569502c6e0bSGu Zheng if (!page) { 570bd936f84SChao Yu page = grab_meta_page(sbi, start_blk++); 571502c6e0bSGu Zheng orphan_blk = 572502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 573502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 574502c6e0bSGu Zheng } 575127e670aSJaegeuk Kim 57636795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 577127e670aSJaegeuk Kim 57836795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 579127e670aSJaegeuk Kim /* 580127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 581127e670aSJaegeuk Kim * then we need to flush current orphan blocks 582127e670aSJaegeuk Kim * and bring another one in memory 583127e670aSJaegeuk Kim */ 584127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 585127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 586127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 587127e670aSJaegeuk Kim set_page_dirty(page); 588127e670aSJaegeuk Kim f2fs_put_page(page, 1); 589127e670aSJaegeuk Kim index++; 590127e670aSJaegeuk Kim nentries = 0; 591127e670aSJaegeuk Kim page = NULL; 592127e670aSJaegeuk Kim } 593127e670aSJaegeuk Kim } 594127e670aSJaegeuk Kim 595502c6e0bSGu Zheng if (page) { 596127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 597127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 598127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 599127e670aSJaegeuk Kim set_page_dirty(page); 600127e670aSJaegeuk Kim f2fs_put_page(page, 1); 601502c6e0bSGu Zheng } 602127e670aSJaegeuk Kim } 603127e670aSJaegeuk Kim 604127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 605127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 606127e670aSJaegeuk Kim { 607127e670aSJaegeuk Kim struct page *cp_page_1, *cp_page_2 = NULL; 608127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 609127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 610127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 611127e670aSJaegeuk Kim size_t crc_offset; 6127e586fa0SJaegeuk Kim __u32 crc = 0; 613127e670aSJaegeuk Kim 614127e670aSJaegeuk Kim /* Read the 1st cp block in this CP pack */ 615127e670aSJaegeuk Kim cp_page_1 = get_meta_page(sbi, cp_addr); 616127e670aSJaegeuk Kim 617127e670aSJaegeuk Kim /* get the version number */ 618127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); 619127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 620127e670aSJaegeuk Kim if (crc_offset >= blk_size) 621127e670aSJaegeuk Kim goto invalid_cp1; 622127e670aSJaegeuk Kim 62329e7043fSJaegeuk Kim crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset))); 624127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 625127e670aSJaegeuk Kim goto invalid_cp1; 626127e670aSJaegeuk Kim 627d71b5564SJaegeuk Kim pre_version = cur_cp_version(cp_block); 628127e670aSJaegeuk Kim 629127e670aSJaegeuk Kim /* Read the 2nd cp block in this CP pack */ 63025ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 631127e670aSJaegeuk Kim cp_page_2 = get_meta_page(sbi, cp_addr); 632127e670aSJaegeuk Kim 633127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); 634127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 635127e670aSJaegeuk Kim if (crc_offset >= blk_size) 636127e670aSJaegeuk Kim goto invalid_cp2; 637127e670aSJaegeuk Kim 63829e7043fSJaegeuk Kim crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset))); 639127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 640127e670aSJaegeuk Kim goto invalid_cp2; 641127e670aSJaegeuk Kim 642d71b5564SJaegeuk Kim cur_version = cur_cp_version(cp_block); 643127e670aSJaegeuk Kim 644127e670aSJaegeuk Kim if (cur_version == pre_version) { 645127e670aSJaegeuk Kim *version = cur_version; 646127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 647127e670aSJaegeuk Kim return cp_page_1; 648127e670aSJaegeuk Kim } 649127e670aSJaegeuk Kim invalid_cp2: 650127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 651127e670aSJaegeuk Kim invalid_cp1: 652127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 653127e670aSJaegeuk Kim return NULL; 654127e670aSJaegeuk Kim } 655127e670aSJaegeuk Kim 656127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 657127e670aSJaegeuk Kim { 658127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 659127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 660127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 661127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 662127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 663127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 66455141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 6651dbe4152SChangman Lee block_t cp_blk_no; 6661dbe4152SChangman Lee int i; 667127e670aSJaegeuk Kim 6681dbe4152SChangman Lee sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL); 669127e670aSJaegeuk Kim if (!sbi->ckpt) 670127e670aSJaegeuk Kim return -ENOMEM; 671127e670aSJaegeuk Kim /* 672127e670aSJaegeuk Kim * Finding out valid cp block involves read both 673127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 674127e670aSJaegeuk Kim */ 675127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 676127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 677127e670aSJaegeuk Kim 678127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 679f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 680f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 681127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 682127e670aSJaegeuk Kim 683127e670aSJaegeuk Kim if (cp1 && cp2) { 684127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 685127e670aSJaegeuk Kim cur_page = cp2; 686127e670aSJaegeuk Kim else 687127e670aSJaegeuk Kim cur_page = cp1; 688127e670aSJaegeuk Kim } else if (cp1) { 689127e670aSJaegeuk Kim cur_page = cp1; 690127e670aSJaegeuk Kim } else if (cp2) { 691127e670aSJaegeuk Kim cur_page = cp2; 692127e670aSJaegeuk Kim } else { 693127e670aSJaegeuk Kim goto fail_no_cp; 694127e670aSJaegeuk Kim } 695127e670aSJaegeuk Kim 696127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 697127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 698127e670aSJaegeuk Kim 6991dbe4152SChangman Lee if (cp_blks <= 1) 7001dbe4152SChangman Lee goto done; 7011dbe4152SChangman Lee 7021dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 7031dbe4152SChangman Lee if (cur_page == cp2) 7041dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 7051dbe4152SChangman Lee 7061dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 7071dbe4152SChangman Lee void *sit_bitmap_ptr; 7081dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 7091dbe4152SChangman Lee 7101dbe4152SChangman Lee cur_page = get_meta_page(sbi, cp_blk_no + i); 7111dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 7121dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 7131dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 7141dbe4152SChangman Lee } 7151dbe4152SChangman Lee done: 716127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 717127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 718127e670aSJaegeuk Kim return 0; 719127e670aSJaegeuk Kim 720127e670aSJaegeuk Kim fail_no_cp: 721127e670aSJaegeuk Kim kfree(sbi->ckpt); 722127e670aSJaegeuk Kim return -EINVAL; 723127e670aSJaegeuk Kim } 724127e670aSJaegeuk Kim 725c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 726127e670aSJaegeuk Kim { 7274081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 7282710fd7eSChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 729c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 7302d7b822aSChao Yu 731c227f912SChao Yu if (is_inode_flag_set(fi, flag)) 7322710fd7eSChao Yu return; 7332d7b822aSChao Yu 734c227f912SChao Yu set_inode_flag(fi, flag); 735c227f912SChao Yu list_add_tail(&fi->dirty_list, &sbi->inode_list[type]); 73633fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 7375deb8267SJaegeuk Kim } 7385deb8267SJaegeuk Kim 739c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 7406ad7609aSChao Yu { 7416ad7609aSChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 742c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 7436ad7609aSChao Yu 7446ad7609aSChao Yu if (get_dirty_pages(inode) || 745c227f912SChao Yu !is_inode_flag_set(F2FS_I(inode), flag)) 7466ad7609aSChao Yu return; 7476ad7609aSChao Yu 7486ad7609aSChao Yu list_del_init(&fi->dirty_list); 749c227f912SChao Yu clear_inode_flag(fi, flag); 75033fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 7516ad7609aSChao Yu } 7526ad7609aSChao Yu 753a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page) 7545deb8267SJaegeuk Kim { 7554081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 756c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 7575deb8267SJaegeuk Kim 7585ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 7595ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 760127e670aSJaegeuk Kim return; 7617bd59381SGu Zheng 762c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 763c227f912SChao Yu __add_dirty_inode(inode, type); 764a7ffdbe2SJaegeuk Kim inode_inc_dirty_pages(inode); 765c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 766a7ffdbe2SJaegeuk Kim 767a7ffdbe2SJaegeuk Kim SetPagePrivate(page); 7689e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 7695deb8267SJaegeuk Kim } 770127e670aSJaegeuk Kim 7715deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode) 7725deb8267SJaegeuk Kim { 7734081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 7745deb8267SJaegeuk Kim 775c227f912SChao Yu spin_lock(&sbi->inode_lock[DIR_INODE]); 776c227f912SChao Yu __add_dirty_inode(inode, DIR_INODE); 777c227f912SChao Yu spin_unlock(&sbi->inode_lock[DIR_INODE]); 778127e670aSJaegeuk Kim } 779127e670aSJaegeuk Kim 780c227f912SChao Yu void remove_dirty_inode(struct inode *inode) 781127e670aSJaegeuk Kim { 7824081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 7832710fd7eSChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 784c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 785127e670aSJaegeuk Kim 786c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 787c227f912SChao Yu !S_ISLNK(inode->i_mode)) 788127e670aSJaegeuk Kim return; 789127e670aSJaegeuk Kim 790c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 791c227f912SChao Yu __remove_dirty_inode(inode, type); 792c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 79374d0b917SJaegeuk Kim 79474d0b917SJaegeuk Kim /* Only from the recovery routine */ 7952710fd7eSChao Yu if (is_inode_flag_set(fi, FI_DELAY_IPUT)) { 7962710fd7eSChao Yu clear_inode_flag(fi, FI_DELAY_IPUT); 79774d0b917SJaegeuk Kim iput(inode); 79874d0b917SJaegeuk Kim } 799afc3eda2SJaegeuk Kim } 80074d0b917SJaegeuk Kim 8016d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 802127e670aSJaegeuk Kim { 803ce3b7d80SGu Zheng struct list_head *head; 804127e670aSJaegeuk Kim struct inode *inode; 8052710fd7eSChao Yu struct f2fs_inode_info *fi; 8064cf18537SChao Yu bool is_dir = (type == DIR_INODE); 8074cf18537SChao Yu 8084cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 8094cf18537SChao Yu get_pages(sbi, is_dir ? 8104cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 811127e670aSJaegeuk Kim retry: 812af41d3eeSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 8136d5a1495SChao Yu return -EIO; 814af41d3eeSJaegeuk Kim 815c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 816ce3b7d80SGu Zheng 817c227f912SChao Yu head = &sbi->inode_list[type]; 818127e670aSJaegeuk Kim if (list_empty(head)) { 819c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 8204cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 8214cf18537SChao Yu get_pages(sbi, is_dir ? 8224cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 8236d5a1495SChao Yu return 0; 824127e670aSJaegeuk Kim } 8252710fd7eSChao Yu fi = list_entry(head->next, struct f2fs_inode_info, dirty_list); 8262710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 827c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 828127e670aSJaegeuk Kim if (inode) { 82987d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 830127e670aSJaegeuk Kim iput(inode); 831127e670aSJaegeuk Kim } else { 832127e670aSJaegeuk Kim /* 833127e670aSJaegeuk Kim * We should submit bio, since it exists several 834127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 835127e670aSJaegeuk Kim */ 836458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 8377ecebe5eSSebastian Andrzej Siewior cond_resched(); 838127e670aSJaegeuk Kim } 839127e670aSJaegeuk Kim goto retry; 840127e670aSJaegeuk Kim } 841127e670aSJaegeuk Kim 8420a8165d7SJaegeuk Kim /* 843127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 844127e670aSJaegeuk Kim */ 845cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 846127e670aSJaegeuk Kim { 847127e670aSJaegeuk Kim struct writeback_control wbc = { 848127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 849127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 850127e670aSJaegeuk Kim .for_reclaim = 0, 851127e670aSJaegeuk Kim }; 852c718379bSJaegeuk Kim struct blk_plug plug; 853cf779cabSJaegeuk Kim int err = 0; 854c718379bSJaegeuk Kim 855c718379bSJaegeuk Kim blk_start_plug(&plug); 856c718379bSJaegeuk Kim 85739936837SJaegeuk Kim retry_flush_dents: 858e479556bSGu Zheng f2fs_lock_all(sbi); 859127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 860127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 861e479556bSGu Zheng f2fs_unlock_all(sbi); 8626d5a1495SChao Yu err = sync_dirty_inodes(sbi, DIR_INODE); 8636d5a1495SChao Yu if (err) 864cf779cabSJaegeuk Kim goto out; 86539936837SJaegeuk Kim goto retry_flush_dents; 866127e670aSJaegeuk Kim } 867127e670aSJaegeuk Kim 868127e670aSJaegeuk Kim /* 869e1c42045Sarter97 * POR: we should ensure that there are no dirty node pages 870127e670aSJaegeuk Kim * until finishing nat/sit flush. 871127e670aSJaegeuk Kim */ 87239936837SJaegeuk Kim retry_flush_nodes: 873b3582c68SChao Yu down_write(&sbi->node_write); 874127e670aSJaegeuk Kim 875127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 876b3582c68SChao Yu up_write(&sbi->node_write); 8776d5a1495SChao Yu err = sync_node_pages(sbi, 0, &wbc); 8786d5a1495SChao Yu if (err) { 879cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 880cf779cabSJaegeuk Kim goto out; 881cf779cabSJaegeuk Kim } 88239936837SJaegeuk Kim goto retry_flush_nodes; 883127e670aSJaegeuk Kim } 884cf779cabSJaegeuk Kim out: 885c718379bSJaegeuk Kim blk_finish_plug(&plug); 886cf779cabSJaegeuk Kim return err; 887127e670aSJaegeuk Kim } 888127e670aSJaegeuk Kim 889127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 890127e670aSJaegeuk Kim { 891b3582c68SChao Yu up_write(&sbi->node_write); 892e479556bSGu Zheng f2fs_unlock_all(sbi); 893127e670aSJaegeuk Kim } 894127e670aSJaegeuk Kim 895fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 896fb51b5efSChangman Lee { 897fb51b5efSChangman Lee DEFINE_WAIT(wait); 898fb51b5efSChangman Lee 899fb51b5efSChangman Lee for (;;) { 900fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 901fb51b5efSChangman Lee 902fb51b5efSChangman Lee if (!get_pages(sbi, F2FS_WRITEBACK)) 903fb51b5efSChangman Lee break; 904fb51b5efSChangman Lee 905fb51b5efSChangman Lee io_schedule(); 906fb51b5efSChangman Lee } 907fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 908fb51b5efSChangman Lee } 909fb51b5efSChangman Lee 910c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 911127e670aSJaegeuk Kim { 912127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 913cf2271e7SJaegeuk Kim struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE); 91477041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 91567298804SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 91677041823SHuang Ying nid_t last_nid = nm_i->next_scan_nid; 917127e670aSJaegeuk Kim block_t start_blk; 918127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 9197e586fa0SJaegeuk Kim __u32 crc32 = 0; 920127e670aSJaegeuk Kim int i; 92155141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 922e90c2d28SChao Yu block_t discard_blk = NEXT_FREE_BLKADDR(sbi, curseg); 923e90c2d28SChao Yu bool invalidate = false; 924127e670aSJaegeuk Kim 9251e87a78dSJaegeuk Kim /* 9261e87a78dSJaegeuk Kim * This avoids to conduct wrong roll-forward operations and uses 9271e87a78dSJaegeuk Kim * metapages, so should be called prior to sync_meta_pages below. 9281e87a78dSJaegeuk Kim */ 929e90c2d28SChao Yu if (discard_next_dnode(sbi, discard_blk)) 930e90c2d28SChao Yu invalidate = true; 931127e670aSJaegeuk Kim 932127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 933cf779cabSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) { 934127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 935cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 936c34f42e2SChao Yu return -EIO; 937cf779cabSJaegeuk Kim } 938127e670aSJaegeuk Kim 939127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 940127e670aSJaegeuk Kim 941127e670aSJaegeuk Kim /* 942127e670aSJaegeuk Kim * modify checkpoint 943127e670aSJaegeuk Kim * version number is already updated 944127e670aSJaegeuk Kim */ 945127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 946127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 947127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 948b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 949127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 950127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 951127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 952127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 953127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 954127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 955127e670aSJaegeuk Kim } 956b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 957127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 958127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 959127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 960127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 961127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 962127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 963127e670aSJaegeuk Kim } 964127e670aSJaegeuk Kim 965127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 966127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 967127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 968127e670aSJaegeuk Kim 969127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 9703fa06d7bSChao Yu data_sum_blocks = npages_for_summary_flush(sbi, false); 971b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 97225ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 973127e670aSJaegeuk Kim else 97425ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 975127e670aSJaegeuk Kim 97667298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 9771dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 9781dbe4152SChangman Lee orphan_blocks); 979127e670aSJaegeuk Kim 980119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 981b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+ 9821dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 9831dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 984119ee914SJaegeuk Kim else 985b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 9861dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 9871dbe4152SChangman Lee orphan_blocks); 988119ee914SJaegeuk Kim 989119ee914SJaegeuk Kim if (cpc->reason == CP_UMOUNT) 990119ee914SJaegeuk Kim set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 991119ee914SJaegeuk Kim else 992119ee914SJaegeuk Kim clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 993119ee914SJaegeuk Kim 994119ee914SJaegeuk Kim if (cpc->reason == CP_FASTBOOT) 995119ee914SJaegeuk Kim set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 996119ee914SJaegeuk Kim else 997119ee914SJaegeuk Kim clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 998127e670aSJaegeuk Kim 99967298804SChao Yu if (orphan_num) 100025ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1001127e670aSJaegeuk Kim else 100225ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1003127e670aSJaegeuk Kim 1004caf0047eSChao Yu if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 10052ae4c673SJaegeuk Kim set_ckpt_flags(ckpt, CP_FSCK_FLAG); 10062ae4c673SJaegeuk Kim 1007127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1008127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1009127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1010127e670aSJaegeuk Kim 1011127e670aSJaegeuk Kim crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); 10127e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 10137e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1014127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1015127e670aSJaegeuk Kim 1016127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi); 1017127e670aSJaegeuk Kim 1018a7230d16SJaegeuk Kim /* need to wait for end_io results */ 1019a7230d16SJaegeuk Kim wait_on_all_pages_writeback(sbi); 1020a7230d16SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1021c34f42e2SChao Yu return -EIO; 1022a7230d16SJaegeuk Kim 1023127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 1024381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk++); 1025127e670aSJaegeuk Kim 1026381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 1027381722d2SChao Yu update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1028381722d2SChao Yu start_blk++); 10291dbe4152SChangman Lee 103067298804SChao Yu if (orphan_num) { 1031127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1032127e670aSJaegeuk Kim start_blk += orphan_blocks; 1033127e670aSJaegeuk Kim } 1034127e670aSJaegeuk Kim 1035127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 1036127e670aSJaegeuk Kim start_blk += data_sum_blocks; 1037119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 1038127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 1039127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1040127e670aSJaegeuk Kim } 1041127e670aSJaegeuk Kim 1042127e670aSJaegeuk Kim /* writeout checkpoint block */ 1043381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk); 1044127e670aSJaegeuk Kim 1045127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 1046fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 1047127e670aSJaegeuk Kim 1048cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1049c34f42e2SChao Yu return -EIO; 1050cf779cabSJaegeuk Kim 10514ef51a8fSJaegeuk Kim filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX); 10529df27d98SGu Zheng filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX); 1053127e670aSJaegeuk Kim 1054127e670aSJaegeuk Kim /* update user_block_counts */ 1055127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 1056127e670aSJaegeuk Kim sbi->alloc_valid_block_count = 0; 1057127e670aSJaegeuk Kim 1058127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 1059127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 1060127e670aSJaegeuk Kim 10616a8f8ca5SJaegeuk Kim /* wait for previous submitted meta pages writeback */ 10626a8f8ca5SJaegeuk Kim wait_on_all_pages_writeback(sbi); 10636a8f8ca5SJaegeuk Kim 1064e90c2d28SChao Yu /* 1065e90c2d28SChao Yu * invalidate meta page which is used temporarily for zeroing out 1066e90c2d28SChao Yu * block at the end of warm node chain. 1067e90c2d28SChao Yu */ 1068e90c2d28SChao Yu if (invalidate) 1069e90c2d28SChao Yu invalidate_mapping_pages(META_MAPPING(sbi), discard_blk, 1070e90c2d28SChao Yu discard_blk); 1071e90c2d28SChao Yu 1072a49324f1SChao Yu release_ino_entry(sbi); 1073cf779cabSJaegeuk Kim 1074cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1075c34f42e2SChao Yu return -EIO; 1076cf779cabSJaegeuk Kim 1077836b5a63SJaegeuk Kim clear_prefree_segments(sbi, cpc); 1078caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1079c34f42e2SChao Yu 1080c34f42e2SChao Yu return 0; 1081127e670aSJaegeuk Kim } 1082127e670aSJaegeuk Kim 10830a8165d7SJaegeuk Kim /* 1084e1c42045Sarter97 * We guarantee that this checkpoint procedure will not fail. 1085127e670aSJaegeuk Kim */ 1086c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1087127e670aSJaegeuk Kim { 1088127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1089127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1090c34f42e2SChao Yu int err = 0; 1091127e670aSJaegeuk Kim 1092127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 10938501017eSJaegeuk Kim 1094caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1095a66cdd98SJaegeuk Kim (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC || 1096a66cdd98SJaegeuk Kim (cpc->reason == CP_DISCARD && !sbi->discard_blks))) 10978501017eSJaegeuk Kim goto out; 1098c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1099c34f42e2SChao Yu err = -EIO; 1100cf779cabSJaegeuk Kim goto out; 1101c34f42e2SChao Yu } 1102c34f42e2SChao Yu if (f2fs_readonly(sbi->sb)) { 1103c34f42e2SChao Yu err = -EROFS; 110411504a8eSJaegeuk Kim goto out; 1105c34f42e2SChao Yu } 11062bda542dSWanpeng Li 11072bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 11082bda542dSWanpeng Li 1109c34f42e2SChao Yu err = block_operations(sbi); 1110c34f42e2SChao Yu if (err) 1111cf779cabSJaegeuk Kim goto out; 1112127e670aSJaegeuk Kim 111375ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 11142af4bd6cSNamjae Jeon 1115458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 1116458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, NODE, WRITE); 1117458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 1118127e670aSJaegeuk Kim 1119127e670aSJaegeuk Kim /* 1120127e670aSJaegeuk Kim * update checkpoint pack index 1121127e670aSJaegeuk Kim * Increase the version number so that 1122127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1123127e670aSJaegeuk Kim */ 1124d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1125127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1126127e670aSJaegeuk Kim 1127127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1128127e670aSJaegeuk Kim flush_nat_entries(sbi); 11294b2fecc8SJaegeuk Kim flush_sit_entries(sbi, cpc); 1130127e670aSJaegeuk Kim 1131127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 1132c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 1133127e670aSJaegeuk Kim 1134127e670aSJaegeuk Kim unblock_operations(sbi); 1135942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 113610027551SJaegeuk Kim 113710027551SJaegeuk Kim if (cpc->reason == CP_RECOVERY) 113810027551SJaegeuk Kim f2fs_msg(sbi->sb, KERN_NOTICE, 113910027551SJaegeuk Kim "checkpoint: version = %llx", ckpt_ver); 114060b99b48SJaegeuk Kim 114160b99b48SJaegeuk Kim /* do checkpoint periodically */ 1142*6beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 114355d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 11448501017eSJaegeuk Kim out: 11458501017eSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 1146c34f42e2SChao Yu return err; 1147127e670aSJaegeuk Kim } 1148127e670aSJaegeuk Kim 11496451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi) 1150127e670aSJaegeuk Kim { 11516451e041SJaegeuk Kim int i; 11526451e041SJaegeuk Kim 11536451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 115467298804SChao Yu struct inode_management *im = &sbi->im[i]; 115567298804SChao Yu 115667298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 115767298804SChao Yu spin_lock_init(&im->ino_lock); 115867298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 115967298804SChao Yu im->ino_num = 0; 11606451e041SJaegeuk Kim } 11616451e041SJaegeuk Kim 1162b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 116314b42817SWanpeng Li NR_CURSEG_TYPE - __cp_payload(sbi)) * 116414b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1165127e670aSJaegeuk Kim } 1166127e670aSJaegeuk Kim 11676e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 1168127e670aSJaegeuk Kim { 11696451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 11706451e041SJaegeuk Kim sizeof(struct ino_entry)); 11716451e041SJaegeuk Kim if (!ino_entry_slab) 1172127e670aSJaegeuk Kim return -ENOMEM; 117306292073SChao Yu inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 117406292073SChao Yu sizeof(struct inode_entry)); 11756bacf52fSJaegeuk Kim if (!inode_entry_slab) { 11766451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1177127e670aSJaegeuk Kim return -ENOMEM; 1178127e670aSJaegeuk Kim } 1179127e670aSJaegeuk Kim return 0; 1180127e670aSJaegeuk Kim } 1181127e670aSJaegeuk Kim 1182127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 1183127e670aSJaegeuk Kim { 11846451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1185127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 1186127e670aSJaegeuk Kim } 1187