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 } 42fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(page, META, true); 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 struct page *page; 1474c521f49SJaegeuk Kim block_t blkno = start; 148662befdaSChao Yu struct f2fs_io_info fio = { 14905ca3632SJaegeuk Kim .sbi = sbi, 150662befdaSChao Yu .type = META, 15126879fb1SChao Yu .rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA, 1524375a336SJaegeuk Kim .encrypted_page = NULL, 153662befdaSChao Yu }; 154e9f5b8b8SChao Yu struct blk_plug plug; 155662befdaSChao Yu 1562b947003SChao Yu if (unlikely(type == META_POR)) 1572b947003SChao Yu fio.rw &= ~REQ_META; 1582b947003SChao Yu 159e9f5b8b8SChao Yu blk_start_plug(&plug); 160662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 161662befdaSChao Yu 16266b00c18SChao Yu if (!is_valid_blkaddr(sbi, blkno, type)) 16366b00c18SChao Yu goto out; 16466b00c18SChao Yu 165662befdaSChao Yu switch (type) { 166662befdaSChao Yu case META_NAT: 16766b00c18SChao Yu if (unlikely(blkno >= 16866b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 169662befdaSChao Yu blkno = 0; 17066b00c18SChao Yu /* get nat block addr */ 171cf04e8ebSJaegeuk Kim fio.blk_addr = current_nat_addr(sbi, 172662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 173662befdaSChao Yu break; 174662befdaSChao Yu case META_SIT: 175662befdaSChao Yu /* get sit block addr */ 176cf04e8ebSJaegeuk Kim fio.blk_addr = current_sit_addr(sbi, 177662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 178662befdaSChao Yu break; 17981c1a0f1SChao Yu case META_SSA: 180662befdaSChao Yu case META_CP: 1814c521f49SJaegeuk Kim case META_POR: 182cf04e8ebSJaegeuk Kim fio.blk_addr = blkno; 183662befdaSChao Yu break; 184662befdaSChao Yu default: 185662befdaSChao Yu BUG(); 186662befdaSChao Yu } 187662befdaSChao Yu 188cf04e8ebSJaegeuk Kim page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr); 189662befdaSChao Yu if (!page) 190662befdaSChao Yu continue; 191662befdaSChao Yu if (PageUptodate(page)) { 192662befdaSChao Yu f2fs_put_page(page, 1); 193662befdaSChao Yu continue; 194662befdaSChao Yu } 195662befdaSChao Yu 19605ca3632SJaegeuk Kim fio.page = page; 19705ca3632SJaegeuk Kim f2fs_submit_page_mbio(&fio); 198662befdaSChao Yu f2fs_put_page(page, 0); 199662befdaSChao Yu } 200662befdaSChao Yu out: 201662befdaSChao Yu f2fs_submit_merged_bio(sbi, META, READ); 202e9f5b8b8SChao Yu blk_finish_plug(&plug); 203662befdaSChao Yu return blkno - start; 204662befdaSChao Yu } 205662befdaSChao Yu 206635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index) 207635aee1fSChao Yu { 208635aee1fSChao Yu struct page *page; 209635aee1fSChao Yu bool readahead = false; 210635aee1fSChao Yu 211635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 212635aee1fSChao Yu if (!page || (page && !PageUptodate(page))) 213635aee1fSChao Yu readahead = true; 214635aee1fSChao Yu f2fs_put_page(page, 0); 215635aee1fSChao Yu 216635aee1fSChao Yu if (readahead) 21726879fb1SChao Yu ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true); 218635aee1fSChao Yu } 219635aee1fSChao Yu 220127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page, 221127e670aSJaegeuk Kim struct writeback_control *wbc) 222127e670aSJaegeuk Kim { 2234081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 224577e3495SJaegeuk Kim 225ecda0de3SChao Yu trace_f2fs_writepage(page, META); 226ecda0de3SChao Yu 227caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 228cfb271d4SChao Yu goto redirty_out; 229857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 230cfb271d4SChao Yu goto redirty_out; 2311e968fdfSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 232cf779cabSJaegeuk Kim goto redirty_out; 233127e670aSJaegeuk Kim 234577e3495SJaegeuk Kim write_meta_page(sbi, page); 235127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 2360c3a5797SChao Yu 2370c3a5797SChao Yu if (wbc->for_reclaim) 2380c3a5797SChao Yu f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE); 2390c3a5797SChao Yu 240127e670aSJaegeuk Kim unlock_page(page); 241857dc4e0SJaegeuk Kim 2420c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 243857dc4e0SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 2440c3a5797SChao Yu 245577e3495SJaegeuk Kim return 0; 246cfb271d4SChao Yu 247cfb271d4SChao Yu redirty_out: 24876f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 249cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 250127e670aSJaegeuk Kim } 251127e670aSJaegeuk Kim 252127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 253127e670aSJaegeuk Kim struct writeback_control *wbc) 254127e670aSJaegeuk Kim { 2554081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 25650c8cdb3SJaegeuk Kim long diff, written; 257127e670aSJaegeuk Kim 2585459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 25950c8cdb3SJaegeuk Kim if (wbc->for_kupdate || 26050c8cdb3SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META)) 261d3baf95dSJaegeuk Kim goto skip_write; 262127e670aSJaegeuk Kim 263d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 264d31c7c3fSYunlei He 265127e670aSJaegeuk Kim /* if mounting is failed, skip writing node pages */ 266127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 26750c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 26850c8cdb3SJaegeuk Kim written = sync_meta_pages(sbi, META, wbc->nr_to_write); 269127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 27050c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 271127e670aSJaegeuk Kim return 0; 272d3baf95dSJaegeuk Kim 273d3baf95dSJaegeuk Kim skip_write: 274d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 275d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 276d3baf95dSJaegeuk Kim return 0; 277127e670aSJaegeuk Kim } 278127e670aSJaegeuk Kim 279127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 280127e670aSJaegeuk Kim long nr_to_write) 281127e670aSJaegeuk Kim { 2829df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 2836066d8cdSJaegeuk Kim pgoff_t index = 0, end = LONG_MAX, prev = LONG_MAX; 284127e670aSJaegeuk Kim struct pagevec pvec; 285127e670aSJaegeuk Kim long nwritten = 0; 286127e670aSJaegeuk Kim struct writeback_control wbc = { 287127e670aSJaegeuk Kim .for_reclaim = 0, 288127e670aSJaegeuk Kim }; 289e9f5b8b8SChao Yu struct blk_plug plug; 290127e670aSJaegeuk Kim 291127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 292127e670aSJaegeuk Kim 293e9f5b8b8SChao Yu blk_start_plug(&plug); 294e9f5b8b8SChao Yu 295127e670aSJaegeuk Kim while (index <= end) { 296127e670aSJaegeuk Kim int i, nr_pages; 297127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 298127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 299127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 300cfb271d4SChao Yu if (unlikely(nr_pages == 0)) 301127e670aSJaegeuk Kim break; 302127e670aSJaegeuk Kim 303127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 304127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 305203681f6SJaegeuk Kim 3066066d8cdSJaegeuk Kim if (prev == LONG_MAX) 3076066d8cdSJaegeuk Kim prev = page->index - 1; 3086066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 3096066d8cdSJaegeuk Kim pagevec_release(&pvec); 3106066d8cdSJaegeuk Kim goto stop; 3116066d8cdSJaegeuk Kim } 3126066d8cdSJaegeuk Kim 313127e670aSJaegeuk Kim lock_page(page); 314203681f6SJaegeuk Kim 315203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 316203681f6SJaegeuk Kim continue_unlock: 317203681f6SJaegeuk Kim unlock_page(page); 318203681f6SJaegeuk Kim continue; 319203681f6SJaegeuk Kim } 320203681f6SJaegeuk Kim if (!PageDirty(page)) { 321203681f6SJaegeuk Kim /* someone wrote it for us */ 322203681f6SJaegeuk Kim goto continue_unlock; 323203681f6SJaegeuk Kim } 324203681f6SJaegeuk Kim 325fa3d2bdfSJaegeuk Kim f2fs_wait_on_page_writeback(page, META, true); 326fa3d2bdfSJaegeuk Kim 327fa3d2bdfSJaegeuk Kim BUG_ON(PageWriteback(page)); 328203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 329203681f6SJaegeuk Kim goto continue_unlock; 330203681f6SJaegeuk Kim 33197dc3fd2SChao Yu if (mapping->a_ops->writepage(page, &wbc)) { 332577e3495SJaegeuk Kim unlock_page(page); 333577e3495SJaegeuk Kim break; 334577e3495SJaegeuk Kim } 335cfb271d4SChao Yu nwritten++; 3366066d8cdSJaegeuk Kim prev = page->index; 337cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 338127e670aSJaegeuk Kim break; 339127e670aSJaegeuk Kim } 340127e670aSJaegeuk Kim pagevec_release(&pvec); 341127e670aSJaegeuk Kim cond_resched(); 342127e670aSJaegeuk Kim } 3436066d8cdSJaegeuk Kim stop: 344127e670aSJaegeuk Kim if (nwritten) 345458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, type, WRITE); 346127e670aSJaegeuk Kim 347e9f5b8b8SChao Yu blk_finish_plug(&plug); 348e9f5b8b8SChao Yu 349127e670aSJaegeuk Kim return nwritten; 350127e670aSJaegeuk Kim } 351127e670aSJaegeuk Kim 352127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 353127e670aSJaegeuk Kim { 35426c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 35526c6b887SJaegeuk Kim 356127e670aSJaegeuk Kim SetPageUptodate(page); 357127e670aSJaegeuk Kim if (!PageDirty(page)) { 358127e670aSJaegeuk Kim __set_page_dirty_nobuffers(page); 3594081363fSJaegeuk Kim inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); 3601601839eSChao Yu SetPagePrivate(page); 3619e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 362127e670aSJaegeuk Kim return 1; 363127e670aSJaegeuk Kim } 364127e670aSJaegeuk Kim return 0; 365127e670aSJaegeuk Kim } 366127e670aSJaegeuk Kim 367127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 368127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 369127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 370127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 371487261f3SChao Yu .invalidatepage = f2fs_invalidate_page, 372487261f3SChao Yu .releasepage = f2fs_release_page, 373127e670aSJaegeuk Kim }; 374127e670aSJaegeuk Kim 3756451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 376953e6cc6SJaegeuk Kim { 37767298804SChao Yu struct inode_management *im = &sbi->im[type]; 37880c54505SJaegeuk Kim struct ino_entry *e, *tmp; 37980c54505SJaegeuk Kim 38080c54505SJaegeuk Kim tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS); 38139efac41SJaegeuk Kim retry: 38280c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 383769ec6e5SJaegeuk Kim 38467298804SChao Yu spin_lock(&im->ino_lock); 38567298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 38639efac41SJaegeuk Kim if (!e) { 38780c54505SJaegeuk Kim e = tmp; 38867298804SChao Yu if (radix_tree_insert(&im->ino_root, ino, e)) { 38967298804SChao Yu spin_unlock(&im->ino_lock); 390769ec6e5SJaegeuk Kim radix_tree_preload_end(); 39139efac41SJaegeuk Kim goto retry; 39239efac41SJaegeuk Kim } 39339efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 39439efac41SJaegeuk Kim e->ino = ino; 39539efac41SJaegeuk Kim 39667298804SChao Yu list_add_tail(&e->list, &im->ino_list); 3978c402946SJaegeuk Kim if (type != ORPHAN_INO) 39867298804SChao Yu im->ino_num++; 39939efac41SJaegeuk Kim } 40067298804SChao Yu spin_unlock(&im->ino_lock); 401769ec6e5SJaegeuk Kim radix_tree_preload_end(); 40280c54505SJaegeuk Kim 40380c54505SJaegeuk Kim if (e != tmp) 40480c54505SJaegeuk Kim kmem_cache_free(ino_entry_slab, tmp); 405953e6cc6SJaegeuk Kim } 406953e6cc6SJaegeuk Kim 4076451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 408953e6cc6SJaegeuk Kim { 40967298804SChao Yu struct inode_management *im = &sbi->im[type]; 4106451e041SJaegeuk Kim struct ino_entry *e; 411953e6cc6SJaegeuk Kim 41267298804SChao Yu spin_lock(&im->ino_lock); 41367298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 41439efac41SJaegeuk Kim if (e) { 415953e6cc6SJaegeuk Kim list_del(&e->list); 41667298804SChao Yu radix_tree_delete(&im->ino_root, ino); 41767298804SChao Yu im->ino_num--; 41867298804SChao Yu spin_unlock(&im->ino_lock); 4196451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 420953e6cc6SJaegeuk Kim return; 421953e6cc6SJaegeuk Kim } 42267298804SChao Yu spin_unlock(&im->ino_lock); 423953e6cc6SJaegeuk Kim } 424953e6cc6SJaegeuk Kim 425a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 426fff04f90SJaegeuk Kim { 427fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 428fff04f90SJaegeuk Kim __add_ino_entry(sbi, ino, type); 429fff04f90SJaegeuk Kim } 430fff04f90SJaegeuk Kim 431a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 432fff04f90SJaegeuk Kim { 433fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 434fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 435fff04f90SJaegeuk Kim } 436fff04f90SJaegeuk Kim 437fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */ 438fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 439fff04f90SJaegeuk Kim { 44067298804SChao Yu struct inode_management *im = &sbi->im[mode]; 441fff04f90SJaegeuk Kim struct ino_entry *e; 44267298804SChao Yu 44367298804SChao Yu spin_lock(&im->ino_lock); 44467298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 44567298804SChao Yu spin_unlock(&im->ino_lock); 446fff04f90SJaegeuk Kim return e ? true : false; 447fff04f90SJaegeuk Kim } 448fff04f90SJaegeuk Kim 449a49324f1SChao Yu void release_ino_entry(struct f2fs_sb_info *sbi) 450fff04f90SJaegeuk Kim { 451fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 452fff04f90SJaegeuk Kim int i; 453fff04f90SJaegeuk Kim 454fff04f90SJaegeuk Kim for (i = APPEND_INO; i <= UPDATE_INO; i++) { 45567298804SChao Yu struct inode_management *im = &sbi->im[i]; 45667298804SChao Yu 45767298804SChao Yu spin_lock(&im->ino_lock); 45867298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 459fff04f90SJaegeuk Kim list_del(&e->list); 46067298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 461fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 46267298804SChao Yu im->ino_num--; 463fff04f90SJaegeuk Kim } 46467298804SChao Yu spin_unlock(&im->ino_lock); 465fff04f90SJaegeuk Kim } 466fff04f90SJaegeuk Kim } 467fff04f90SJaegeuk Kim 468cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi) 469127e670aSJaegeuk Kim { 47067298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 471127e670aSJaegeuk Kim int err = 0; 472127e670aSJaegeuk Kim 47367298804SChao Yu spin_lock(&im->ino_lock); 47467298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 475127e670aSJaegeuk Kim err = -ENOSPC; 476cbd56e7dSJaegeuk Kim else 47767298804SChao Yu im->ino_num++; 47867298804SChao Yu spin_unlock(&im->ino_lock); 4790d47c1adSGu Zheng 480127e670aSJaegeuk Kim return err; 481127e670aSJaegeuk Kim } 482127e670aSJaegeuk Kim 483cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi) 484cbd56e7dSJaegeuk Kim { 48567298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 48667298804SChao Yu 48767298804SChao Yu spin_lock(&im->ino_lock); 48867298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 48967298804SChao Yu im->ino_num--; 49067298804SChao Yu spin_unlock(&im->ino_lock); 491cbd56e7dSJaegeuk Kim } 492cbd56e7dSJaegeuk Kim 493127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 494127e670aSJaegeuk Kim { 49539efac41SJaegeuk Kim /* add new orphan ino entry into list */ 4966451e041SJaegeuk Kim __add_ino_entry(sbi, ino, ORPHAN_INO); 497127e670aSJaegeuk Kim } 498127e670aSJaegeuk Kim 499127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 500127e670aSJaegeuk Kim { 501953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 5026451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 503127e670aSJaegeuk Kim } 504127e670aSJaegeuk Kim 5058c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 506127e670aSJaegeuk Kim { 5078c14bfadSChao Yu struct inode *inode; 5088c14bfadSChao Yu 5098c14bfadSChao Yu inode = f2fs_iget(sbi->sb, ino); 5108c14bfadSChao Yu if (IS_ERR(inode)) { 5118c14bfadSChao Yu /* 5128c14bfadSChao Yu * there should be a bug that we can't find the entry 5138c14bfadSChao Yu * to orphan inode. 5148c14bfadSChao Yu */ 5158c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 5168c14bfadSChao Yu return PTR_ERR(inode); 5178c14bfadSChao Yu } 5188c14bfadSChao Yu 519127e670aSJaegeuk Kim clear_nlink(inode); 520127e670aSJaegeuk Kim 521127e670aSJaegeuk Kim /* truncate all the data during iput */ 522127e670aSJaegeuk Kim iput(inode); 5238c14bfadSChao Yu return 0; 524127e670aSJaegeuk Kim } 525127e670aSJaegeuk Kim 5268c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi) 527127e670aSJaegeuk Kim { 5283c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 5298c14bfadSChao Yu int err; 530127e670aSJaegeuk Kim 53125ca923bSJaegeuk Kim if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG)) 5328c14bfadSChao Yu return 0; 533127e670aSJaegeuk Kim 53455141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 5353c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 536127e670aSJaegeuk Kim 53726879fb1SChao Yu ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 538662befdaSChao Yu 5393c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 540127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 541127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 542127e670aSJaegeuk Kim 543127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 544127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 545127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 5468c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 5478c14bfadSChao Yu if (err) { 5488c14bfadSChao Yu f2fs_put_page(page, 1); 5498c14bfadSChao Yu return err; 5508c14bfadSChao Yu } 551127e670aSJaegeuk Kim } 552127e670aSJaegeuk Kim f2fs_put_page(page, 1); 553127e670aSJaegeuk Kim } 554127e670aSJaegeuk Kim /* clear Orphan Flag */ 55525ca923bSJaegeuk Kim clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG); 5568c14bfadSChao Yu return 0; 557127e670aSJaegeuk Kim } 558127e670aSJaegeuk Kim 559127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 560127e670aSJaegeuk Kim { 561502c6e0bSGu Zheng struct list_head *head; 562127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 563127e670aSJaegeuk Kim unsigned int nentries = 0; 564bd936f84SChao Yu unsigned short index = 1; 5658c402946SJaegeuk Kim unsigned short orphan_blocks; 5664531929eSGu Zheng struct page *page = NULL; 5676451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 56867298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 569127e670aSJaegeuk Kim 57067298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 5718c402946SJaegeuk Kim 572d6c67a4fSJaegeuk Kim /* 573d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 574d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 575d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 576d6c67a4fSJaegeuk Kim */ 57767298804SChao Yu head = &im->ino_list; 578127e670aSJaegeuk Kim 579127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 580502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 581502c6e0bSGu Zheng if (!page) { 582bd936f84SChao Yu page = grab_meta_page(sbi, start_blk++); 583502c6e0bSGu Zheng orphan_blk = 584502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 585502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 586502c6e0bSGu Zheng } 587127e670aSJaegeuk Kim 58836795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 589127e670aSJaegeuk Kim 59036795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 591127e670aSJaegeuk Kim /* 592127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 593127e670aSJaegeuk Kim * then we need to flush current orphan blocks 594127e670aSJaegeuk Kim * and bring another one in memory 595127e670aSJaegeuk Kim */ 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); 601127e670aSJaegeuk Kim index++; 602127e670aSJaegeuk Kim nentries = 0; 603127e670aSJaegeuk Kim page = NULL; 604127e670aSJaegeuk Kim } 605127e670aSJaegeuk Kim } 606127e670aSJaegeuk Kim 607502c6e0bSGu Zheng if (page) { 608127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 609127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 610127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 611127e670aSJaegeuk Kim set_page_dirty(page); 612127e670aSJaegeuk Kim f2fs_put_page(page, 1); 613502c6e0bSGu Zheng } 614127e670aSJaegeuk Kim } 615127e670aSJaegeuk Kim 616127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 617127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 618127e670aSJaegeuk Kim { 619127e670aSJaegeuk Kim struct page *cp_page_1, *cp_page_2 = NULL; 620127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 621127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 622127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 623127e670aSJaegeuk Kim size_t crc_offset; 6247e586fa0SJaegeuk Kim __u32 crc = 0; 625127e670aSJaegeuk Kim 626127e670aSJaegeuk Kim /* Read the 1st cp block in this CP pack */ 627127e670aSJaegeuk Kim cp_page_1 = get_meta_page(sbi, cp_addr); 628127e670aSJaegeuk Kim 629127e670aSJaegeuk Kim /* get the version number */ 630127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); 631127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 632127e670aSJaegeuk Kim if (crc_offset >= blk_size) 633127e670aSJaegeuk Kim goto invalid_cp1; 634127e670aSJaegeuk Kim 63529e7043fSJaegeuk Kim crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset))); 636127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 637127e670aSJaegeuk Kim goto invalid_cp1; 638127e670aSJaegeuk Kim 639d71b5564SJaegeuk Kim pre_version = cur_cp_version(cp_block); 640127e670aSJaegeuk Kim 641127e670aSJaegeuk Kim /* Read the 2nd cp block in this CP pack */ 64225ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 643127e670aSJaegeuk Kim cp_page_2 = get_meta_page(sbi, cp_addr); 644127e670aSJaegeuk Kim 645127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); 646127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 647127e670aSJaegeuk Kim if (crc_offset >= blk_size) 648127e670aSJaegeuk Kim goto invalid_cp2; 649127e670aSJaegeuk Kim 65029e7043fSJaegeuk Kim crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset))); 651127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 652127e670aSJaegeuk Kim goto invalid_cp2; 653127e670aSJaegeuk Kim 654d71b5564SJaegeuk Kim cur_version = cur_cp_version(cp_block); 655127e670aSJaegeuk Kim 656127e670aSJaegeuk Kim if (cur_version == pre_version) { 657127e670aSJaegeuk Kim *version = cur_version; 658127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 659127e670aSJaegeuk Kim return cp_page_1; 660127e670aSJaegeuk Kim } 661127e670aSJaegeuk Kim invalid_cp2: 662127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 663127e670aSJaegeuk Kim invalid_cp1: 664127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 665127e670aSJaegeuk Kim return NULL; 666127e670aSJaegeuk Kim } 667127e670aSJaegeuk Kim 668127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 669127e670aSJaegeuk Kim { 670127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 671127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 672127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 673127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 674127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 675127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 67655141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 6771dbe4152SChangman Lee block_t cp_blk_no; 6781dbe4152SChangman Lee int i; 679127e670aSJaegeuk Kim 6801dbe4152SChangman Lee sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL); 681127e670aSJaegeuk Kim if (!sbi->ckpt) 682127e670aSJaegeuk Kim return -ENOMEM; 683127e670aSJaegeuk Kim /* 684127e670aSJaegeuk Kim * Finding out valid cp block involves read both 685127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 686127e670aSJaegeuk Kim */ 687127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 688127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 689127e670aSJaegeuk Kim 690127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 691f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 692f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 693127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 694127e670aSJaegeuk Kim 695127e670aSJaegeuk Kim if (cp1 && cp2) { 696127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 697127e670aSJaegeuk Kim cur_page = cp2; 698127e670aSJaegeuk Kim else 699127e670aSJaegeuk Kim cur_page = cp1; 700127e670aSJaegeuk Kim } else if (cp1) { 701127e670aSJaegeuk Kim cur_page = cp1; 702127e670aSJaegeuk Kim } else if (cp2) { 703127e670aSJaegeuk Kim cur_page = cp2; 704127e670aSJaegeuk Kim } else { 705127e670aSJaegeuk Kim goto fail_no_cp; 706127e670aSJaegeuk Kim } 707127e670aSJaegeuk Kim 708127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 709127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 710127e670aSJaegeuk Kim 711*984ec63cSShawn Lin /* Sanity checking of checkpoint */ 712*984ec63cSShawn Lin if (sanity_check_ckpt(sbi)) 713*984ec63cSShawn Lin goto fail_no_cp; 714*984ec63cSShawn Lin 7151dbe4152SChangman Lee if (cp_blks <= 1) 7161dbe4152SChangman Lee goto done; 7171dbe4152SChangman Lee 7181dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 7191dbe4152SChangman Lee if (cur_page == cp2) 7201dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 7211dbe4152SChangman Lee 7221dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 7231dbe4152SChangman Lee void *sit_bitmap_ptr; 7241dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 7251dbe4152SChangman Lee 7261dbe4152SChangman Lee cur_page = get_meta_page(sbi, cp_blk_no + i); 7271dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 7281dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 7291dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 7301dbe4152SChangman Lee } 7311dbe4152SChangman Lee done: 732127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 733127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 734127e670aSJaegeuk Kim return 0; 735127e670aSJaegeuk Kim 736127e670aSJaegeuk Kim fail_no_cp: 737127e670aSJaegeuk Kim kfree(sbi->ckpt); 738127e670aSJaegeuk Kim return -EINVAL; 739127e670aSJaegeuk Kim } 740127e670aSJaegeuk Kim 741c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 742127e670aSJaegeuk Kim { 7434081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 7442710fd7eSChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 745c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 7462d7b822aSChao Yu 747c227f912SChao Yu if (is_inode_flag_set(fi, flag)) 7482710fd7eSChao Yu return; 7492d7b822aSChao Yu 750c227f912SChao Yu set_inode_flag(fi, flag); 751c227f912SChao Yu list_add_tail(&fi->dirty_list, &sbi->inode_list[type]); 75233fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 7535deb8267SJaegeuk Kim } 7545deb8267SJaegeuk Kim 755c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 7566ad7609aSChao Yu { 7576ad7609aSChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 758c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 7596ad7609aSChao Yu 7606ad7609aSChao Yu if (get_dirty_pages(inode) || 761c227f912SChao Yu !is_inode_flag_set(F2FS_I(inode), flag)) 7626ad7609aSChao Yu return; 7636ad7609aSChao Yu 7646ad7609aSChao Yu list_del_init(&fi->dirty_list); 765c227f912SChao Yu clear_inode_flag(fi, flag); 76633fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 7676ad7609aSChao Yu } 7686ad7609aSChao Yu 769a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page) 7705deb8267SJaegeuk Kim { 7714081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 772c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 7735deb8267SJaegeuk Kim 7745ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 7755ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 776127e670aSJaegeuk Kim return; 7777bd59381SGu Zheng 778c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 779c227f912SChao Yu __add_dirty_inode(inode, type); 780a7ffdbe2SJaegeuk Kim inode_inc_dirty_pages(inode); 781c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 782a7ffdbe2SJaegeuk Kim 783a7ffdbe2SJaegeuk Kim SetPagePrivate(page); 7849e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 7855deb8267SJaegeuk Kim } 786127e670aSJaegeuk Kim 7875deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode) 7885deb8267SJaegeuk Kim { 7894081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 7905deb8267SJaegeuk Kim 791c227f912SChao Yu spin_lock(&sbi->inode_lock[DIR_INODE]); 792c227f912SChao Yu __add_dirty_inode(inode, DIR_INODE); 793c227f912SChao Yu spin_unlock(&sbi->inode_lock[DIR_INODE]); 794127e670aSJaegeuk Kim } 795127e670aSJaegeuk Kim 796c227f912SChao Yu void remove_dirty_inode(struct inode *inode) 797127e670aSJaegeuk Kim { 7984081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 7992710fd7eSChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 800c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 801127e670aSJaegeuk Kim 802c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 803c227f912SChao Yu !S_ISLNK(inode->i_mode)) 804127e670aSJaegeuk Kim return; 805127e670aSJaegeuk Kim 806c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 807c227f912SChao Yu __remove_dirty_inode(inode, type); 808c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 80974d0b917SJaegeuk Kim 81074d0b917SJaegeuk Kim /* Only from the recovery routine */ 8112710fd7eSChao Yu if (is_inode_flag_set(fi, FI_DELAY_IPUT)) { 8122710fd7eSChao Yu clear_inode_flag(fi, FI_DELAY_IPUT); 81374d0b917SJaegeuk Kim iput(inode); 81474d0b917SJaegeuk Kim } 815afc3eda2SJaegeuk Kim } 81674d0b917SJaegeuk Kim 8176d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 818127e670aSJaegeuk Kim { 819ce3b7d80SGu Zheng struct list_head *head; 820127e670aSJaegeuk Kim struct inode *inode; 8212710fd7eSChao Yu struct f2fs_inode_info *fi; 8224cf18537SChao Yu bool is_dir = (type == DIR_INODE); 8234cf18537SChao Yu 8244cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 8254cf18537SChao Yu get_pages(sbi, is_dir ? 8264cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 827127e670aSJaegeuk Kim retry: 828af41d3eeSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 8296d5a1495SChao Yu return -EIO; 830af41d3eeSJaegeuk Kim 831c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 832ce3b7d80SGu Zheng 833c227f912SChao Yu head = &sbi->inode_list[type]; 834127e670aSJaegeuk Kim if (list_empty(head)) { 835c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 8364cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 8374cf18537SChao Yu get_pages(sbi, is_dir ? 8384cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 8396d5a1495SChao Yu return 0; 840127e670aSJaegeuk Kim } 8412710fd7eSChao Yu fi = list_entry(head->next, struct f2fs_inode_info, dirty_list); 8422710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 843c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 844127e670aSJaegeuk Kim if (inode) { 84587d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 846127e670aSJaegeuk Kim iput(inode); 847127e670aSJaegeuk Kim } else { 848127e670aSJaegeuk Kim /* 849127e670aSJaegeuk Kim * We should submit bio, since it exists several 850127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 851127e670aSJaegeuk Kim */ 852458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 8537ecebe5eSSebastian Andrzej Siewior cond_resched(); 854127e670aSJaegeuk Kim } 855127e670aSJaegeuk Kim goto retry; 856127e670aSJaegeuk Kim } 857127e670aSJaegeuk Kim 8580a8165d7SJaegeuk Kim /* 859127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 860127e670aSJaegeuk Kim */ 861cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 862127e670aSJaegeuk Kim { 863127e670aSJaegeuk Kim struct writeback_control wbc = { 864127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 865127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 866127e670aSJaegeuk Kim .for_reclaim = 0, 867127e670aSJaegeuk Kim }; 868c718379bSJaegeuk Kim struct blk_plug plug; 869cf779cabSJaegeuk Kim int err = 0; 870c718379bSJaegeuk Kim 871c718379bSJaegeuk Kim blk_start_plug(&plug); 872c718379bSJaegeuk Kim 87339936837SJaegeuk Kim retry_flush_dents: 874e479556bSGu Zheng f2fs_lock_all(sbi); 875127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 876127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 877e479556bSGu Zheng f2fs_unlock_all(sbi); 8786d5a1495SChao Yu err = sync_dirty_inodes(sbi, DIR_INODE); 8796d5a1495SChao Yu if (err) 880cf779cabSJaegeuk Kim goto out; 88139936837SJaegeuk Kim goto retry_flush_dents; 882127e670aSJaegeuk Kim } 883127e670aSJaegeuk Kim 884127e670aSJaegeuk Kim /* 885e1c42045Sarter97 * POR: we should ensure that there are no dirty node pages 886127e670aSJaegeuk Kim * until finishing nat/sit flush. 887127e670aSJaegeuk Kim */ 88839936837SJaegeuk Kim retry_flush_nodes: 889b3582c68SChao Yu down_write(&sbi->node_write); 890127e670aSJaegeuk Kim 891127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 892b3582c68SChao Yu up_write(&sbi->node_write); 8936d5a1495SChao Yu err = sync_node_pages(sbi, 0, &wbc); 8946d5a1495SChao Yu if (err) { 895cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 896cf779cabSJaegeuk Kim goto out; 897cf779cabSJaegeuk Kim } 89839936837SJaegeuk Kim goto retry_flush_nodes; 899127e670aSJaegeuk Kim } 900cf779cabSJaegeuk Kim out: 901c718379bSJaegeuk Kim blk_finish_plug(&plug); 902cf779cabSJaegeuk Kim return err; 903127e670aSJaegeuk Kim } 904127e670aSJaegeuk Kim 905127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 906127e670aSJaegeuk Kim { 907b3582c68SChao Yu up_write(&sbi->node_write); 908e479556bSGu Zheng f2fs_unlock_all(sbi); 909127e670aSJaegeuk Kim } 910127e670aSJaegeuk Kim 911fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 912fb51b5efSChangman Lee { 913fb51b5efSChangman Lee DEFINE_WAIT(wait); 914fb51b5efSChangman Lee 915fb51b5efSChangman Lee for (;;) { 916fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 917fb51b5efSChangman Lee 918fb51b5efSChangman Lee if (!get_pages(sbi, F2FS_WRITEBACK)) 919fb51b5efSChangman Lee break; 920fb51b5efSChangman Lee 921fb51b5efSChangman Lee io_schedule(); 922fb51b5efSChangman Lee } 923fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 924fb51b5efSChangman Lee } 925fb51b5efSChangman Lee 926c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 927127e670aSJaegeuk Kim { 928127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 929cf2271e7SJaegeuk Kim struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE); 93077041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 93167298804SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 93277041823SHuang Ying nid_t last_nid = nm_i->next_scan_nid; 933127e670aSJaegeuk Kim block_t start_blk; 934127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 9357e586fa0SJaegeuk Kim __u32 crc32 = 0; 936127e670aSJaegeuk Kim int i; 93755141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 938e90c2d28SChao Yu block_t discard_blk = NEXT_FREE_BLKADDR(sbi, curseg); 939e90c2d28SChao Yu bool invalidate = false; 9408f1dbbbbSShuoran Liu struct super_block *sb = sbi->sb; 9418f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 9428f1dbbbbSShuoran Liu u64 kbytes_written; 943127e670aSJaegeuk Kim 9441e87a78dSJaegeuk Kim /* 9451e87a78dSJaegeuk Kim * This avoids to conduct wrong roll-forward operations and uses 9461e87a78dSJaegeuk Kim * metapages, so should be called prior to sync_meta_pages below. 9471e87a78dSJaegeuk Kim */ 948e90c2d28SChao Yu if (discard_next_dnode(sbi, discard_blk)) 949e90c2d28SChao Yu invalidate = true; 950127e670aSJaegeuk Kim 951127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 952cf779cabSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) { 953127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 954cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 955c34f42e2SChao Yu return -EIO; 956cf779cabSJaegeuk Kim } 957127e670aSJaegeuk Kim 958127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 959127e670aSJaegeuk Kim 960127e670aSJaegeuk Kim /* 961127e670aSJaegeuk Kim * modify checkpoint 962127e670aSJaegeuk Kim * version number is already updated 963127e670aSJaegeuk Kim */ 964127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 965127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 966127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 967b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 968127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 969127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 970127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 971127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 972127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 973127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 974127e670aSJaegeuk Kim } 975b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 976127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 977127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 978127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 979127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 980127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 981127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 982127e670aSJaegeuk Kim } 983127e670aSJaegeuk Kim 984127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 985127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 986127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 987127e670aSJaegeuk Kim 988127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 9893fa06d7bSChao Yu data_sum_blocks = npages_for_summary_flush(sbi, false); 990b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 99125ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 992127e670aSJaegeuk Kim else 99325ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 994127e670aSJaegeuk Kim 99567298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 9961dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 9971dbe4152SChangman Lee orphan_blocks); 998127e670aSJaegeuk Kim 999119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1000b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+ 10011dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 10021dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1003119ee914SJaegeuk Kim else 1004b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 10051dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 10061dbe4152SChangman Lee orphan_blocks); 1007119ee914SJaegeuk Kim 1008119ee914SJaegeuk Kim if (cpc->reason == CP_UMOUNT) 1009119ee914SJaegeuk Kim set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1010119ee914SJaegeuk Kim else 1011119ee914SJaegeuk Kim clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1012119ee914SJaegeuk Kim 1013119ee914SJaegeuk Kim if (cpc->reason == CP_FASTBOOT) 1014119ee914SJaegeuk Kim set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1015119ee914SJaegeuk Kim else 1016119ee914SJaegeuk Kim clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1017127e670aSJaegeuk Kim 101867298804SChao Yu if (orphan_num) 101925ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1020127e670aSJaegeuk Kim else 102125ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1022127e670aSJaegeuk Kim 1023caf0047eSChao Yu if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 10242ae4c673SJaegeuk Kim set_ckpt_flags(ckpt, CP_FSCK_FLAG); 10252ae4c673SJaegeuk Kim 1026127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1027127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1028127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1029127e670aSJaegeuk Kim 1030127e670aSJaegeuk Kim crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); 10317e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 10327e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1033127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1034127e670aSJaegeuk Kim 1035127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi); 1036127e670aSJaegeuk Kim 1037a7230d16SJaegeuk Kim /* need to wait for end_io results */ 1038a7230d16SJaegeuk Kim wait_on_all_pages_writeback(sbi); 1039a7230d16SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1040c34f42e2SChao Yu return -EIO; 1041a7230d16SJaegeuk Kim 1042127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 1043381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk++); 1044127e670aSJaegeuk Kim 1045381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 1046381722d2SChao Yu update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1047381722d2SChao Yu start_blk++); 10481dbe4152SChangman Lee 104967298804SChao Yu if (orphan_num) { 1050127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1051127e670aSJaegeuk Kim start_blk += orphan_blocks; 1052127e670aSJaegeuk Kim } 1053127e670aSJaegeuk Kim 1054127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 1055127e670aSJaegeuk Kim start_blk += data_sum_blocks; 10568f1dbbbbSShuoran Liu 10578f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 10588f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 10598f1dbbbbSShuoran Liu if (sb->s_bdev->bd_part) 10608f1dbbbbSShuoran Liu kbytes_written += BD_PART_WRITTEN(sbi); 10618f1dbbbbSShuoran Liu 1062b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 10638f1dbbbbSShuoran Liu 1064119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 1065127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 1066127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1067127e670aSJaegeuk Kim } 1068127e670aSJaegeuk Kim 1069127e670aSJaegeuk Kim /* writeout checkpoint block */ 1070381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk); 1071127e670aSJaegeuk Kim 1072127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 1073fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 1074127e670aSJaegeuk Kim 1075cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1076c34f42e2SChao Yu return -EIO; 1077cf779cabSJaegeuk Kim 10784ef51a8fSJaegeuk Kim filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX); 10799df27d98SGu Zheng filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX); 1080127e670aSJaegeuk Kim 1081127e670aSJaegeuk Kim /* update user_block_counts */ 1082127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 1083127e670aSJaegeuk Kim sbi->alloc_valid_block_count = 0; 1084127e670aSJaegeuk Kim 1085127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 1086127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 1087127e670aSJaegeuk Kim 10886a8f8ca5SJaegeuk Kim /* wait for previous submitted meta pages writeback */ 10896a8f8ca5SJaegeuk Kim wait_on_all_pages_writeback(sbi); 10906a8f8ca5SJaegeuk Kim 1091e90c2d28SChao Yu /* 1092e90c2d28SChao Yu * invalidate meta page which is used temporarily for zeroing out 1093e90c2d28SChao Yu * block at the end of warm node chain. 1094e90c2d28SChao Yu */ 1095e90c2d28SChao Yu if (invalidate) 1096e90c2d28SChao Yu invalidate_mapping_pages(META_MAPPING(sbi), discard_blk, 1097e90c2d28SChao Yu discard_blk); 1098e90c2d28SChao Yu 1099a49324f1SChao Yu release_ino_entry(sbi); 1100cf779cabSJaegeuk Kim 1101cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1102c34f42e2SChao Yu return -EIO; 1103cf779cabSJaegeuk Kim 1104836b5a63SJaegeuk Kim clear_prefree_segments(sbi, cpc); 1105caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1106c34f42e2SChao Yu 1107c34f42e2SChao Yu return 0; 1108127e670aSJaegeuk Kim } 1109127e670aSJaegeuk Kim 11100a8165d7SJaegeuk Kim /* 1111e1c42045Sarter97 * We guarantee that this checkpoint procedure will not fail. 1112127e670aSJaegeuk Kim */ 1113c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1114127e670aSJaegeuk Kim { 1115127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1116127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1117c34f42e2SChao Yu int err = 0; 1118127e670aSJaegeuk Kim 1119127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 11208501017eSJaegeuk Kim 1121caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1122a66cdd98SJaegeuk Kim (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC || 1123a66cdd98SJaegeuk Kim (cpc->reason == CP_DISCARD && !sbi->discard_blks))) 11248501017eSJaegeuk Kim goto out; 1125c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1126c34f42e2SChao Yu err = -EIO; 1127cf779cabSJaegeuk Kim goto out; 1128c34f42e2SChao Yu } 1129c34f42e2SChao Yu if (f2fs_readonly(sbi->sb)) { 1130c34f42e2SChao Yu err = -EROFS; 113111504a8eSJaegeuk Kim goto out; 1132c34f42e2SChao Yu } 11332bda542dSWanpeng Li 11342bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 11352bda542dSWanpeng Li 1136c34f42e2SChao Yu err = block_operations(sbi); 1137c34f42e2SChao Yu if (err) 1138cf779cabSJaegeuk Kim goto out; 1139127e670aSJaegeuk Kim 114075ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 11412af4bd6cSNamjae Jeon 1142458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 1143458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, NODE, WRITE); 1144458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 1145127e670aSJaegeuk Kim 1146127e670aSJaegeuk Kim /* 1147127e670aSJaegeuk Kim * update checkpoint pack index 1148127e670aSJaegeuk Kim * Increase the version number so that 1149127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1150127e670aSJaegeuk Kim */ 1151d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1152127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1153127e670aSJaegeuk Kim 1154127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1155127e670aSJaegeuk Kim flush_nat_entries(sbi); 11564b2fecc8SJaegeuk Kim flush_sit_entries(sbi, cpc); 1157127e670aSJaegeuk Kim 1158127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 1159c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 1160127e670aSJaegeuk Kim 1161127e670aSJaegeuk Kim unblock_operations(sbi); 1162942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 116310027551SJaegeuk Kim 116410027551SJaegeuk Kim if (cpc->reason == CP_RECOVERY) 116510027551SJaegeuk Kim f2fs_msg(sbi->sb, KERN_NOTICE, 116610027551SJaegeuk Kim "checkpoint: version = %llx", ckpt_ver); 116760b99b48SJaegeuk Kim 116860b99b48SJaegeuk Kim /* do checkpoint periodically */ 11696beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 117055d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 11718501017eSJaegeuk Kim out: 11728501017eSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 1173c34f42e2SChao Yu return err; 1174127e670aSJaegeuk Kim } 1175127e670aSJaegeuk Kim 11766451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi) 1177127e670aSJaegeuk Kim { 11786451e041SJaegeuk Kim int i; 11796451e041SJaegeuk Kim 11806451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 118167298804SChao Yu struct inode_management *im = &sbi->im[i]; 118267298804SChao Yu 118367298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 118467298804SChao Yu spin_lock_init(&im->ino_lock); 118567298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 118667298804SChao Yu im->ino_num = 0; 11876451e041SJaegeuk Kim } 11886451e041SJaegeuk Kim 1189b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 119014b42817SWanpeng Li NR_CURSEG_TYPE - __cp_payload(sbi)) * 119114b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1192127e670aSJaegeuk Kim } 1193127e670aSJaegeuk Kim 11946e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 1195127e670aSJaegeuk Kim { 11966451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 11976451e041SJaegeuk Kim sizeof(struct ino_entry)); 11986451e041SJaegeuk Kim if (!ino_entry_slab) 1199127e670aSJaegeuk Kim return -ENOMEM; 120006292073SChao Yu inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 120106292073SChao Yu sizeof(struct inode_entry)); 12026bacf52fSJaegeuk Kim if (!inode_entry_slab) { 12036451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1204127e670aSJaegeuk Kim return -ENOMEM; 1205127e670aSJaegeuk Kim } 1206127e670aSJaegeuk Kim return 0; 1207127e670aSJaegeuk Kim } 1208127e670aSJaegeuk Kim 1209127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 1210127e670aSJaegeuk Kim { 12116451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1212127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 1213127e670aSJaegeuk Kim } 1214