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 2938f91ca8SJaegeuk Kim void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io) 3038f91ca8SJaegeuk Kim { 31aaec2b1dSChao Yu set_ckpt_flags(sbi, CP_ERROR_FLAG); 3238f91ca8SJaegeuk Kim sbi->sb->s_flags |= MS_RDONLY; 3338f91ca8SJaegeuk Kim if (!end_io) 3438f91ca8SJaegeuk Kim f2fs_flush_merged_bios(sbi); 3538f91ca8SJaegeuk Kim } 3638f91ca8SJaegeuk Kim 370a8165d7SJaegeuk Kim /* 38127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 39127e670aSJaegeuk Kim */ 40127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 41127e670aSJaegeuk Kim { 429df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 43127e670aSJaegeuk Kim struct page *page = NULL; 44127e670aSJaegeuk Kim repeat: 45300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 46127e670aSJaegeuk Kim if (!page) { 47127e670aSJaegeuk Kim cond_resched(); 48127e670aSJaegeuk Kim goto repeat; 49127e670aSJaegeuk Kim } 50fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(page, META, true); 51237c0790SJaegeuk Kim if (!PageUptodate(page)) 52127e670aSJaegeuk Kim SetPageUptodate(page); 53127e670aSJaegeuk Kim return page; 54127e670aSJaegeuk Kim } 55127e670aSJaegeuk Kim 560a8165d7SJaegeuk Kim /* 57127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 58127e670aSJaegeuk Kim */ 592b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index, 602b947003SChao Yu bool is_meta) 61127e670aSJaegeuk Kim { 629df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 63127e670aSJaegeuk Kim struct page *page; 64cf04e8ebSJaegeuk Kim struct f2fs_io_info fio = { 6505ca3632SJaegeuk Kim .sbi = sbi, 66cf04e8ebSJaegeuk Kim .type = META, 6704d328deSMike Christie .op = REQ_OP_READ, 6870fd7614SChristoph Hellwig .op_flags = REQ_META | REQ_PRIO, 697a9d7548SChao Yu .old_blkaddr = index, 707a9d7548SChao Yu .new_blkaddr = index, 714375a336SJaegeuk Kim .encrypted_page = NULL, 72cf04e8ebSJaegeuk Kim }; 732b947003SChao Yu 742b947003SChao Yu if (unlikely(!is_meta)) 7504d328deSMike Christie fio.op_flags &= ~REQ_META; 76127e670aSJaegeuk Kim repeat: 77300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false); 78127e670aSJaegeuk Kim if (!page) { 79127e670aSJaegeuk Kim cond_resched(); 80127e670aSJaegeuk Kim goto repeat; 81127e670aSJaegeuk Kim } 82393ff91fSJaegeuk Kim if (PageUptodate(page)) 83393ff91fSJaegeuk Kim goto out; 84127e670aSJaegeuk Kim 8505ca3632SJaegeuk Kim fio.page = page; 8605ca3632SJaegeuk Kim 8786531d6bSJaegeuk Kim if (f2fs_submit_page_bio(&fio)) { 8886531d6bSJaegeuk Kim f2fs_put_page(page, 1); 89393ff91fSJaegeuk Kim goto repeat; 9086531d6bSJaegeuk Kim } 91393ff91fSJaegeuk Kim 92393ff91fSJaegeuk Kim lock_page(page); 936bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) { 94afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 95afcb7ca0SJaegeuk Kim goto repeat; 96afcb7ca0SJaegeuk Kim } 97f3f338caSChao Yu 98f3f338caSChao Yu /* 99f3f338caSChao Yu * if there is any IO error when accessing device, make our filesystem 100f3f338caSChao Yu * readonly and make sure do not write checkpoint with non-uptodate 101f3f338caSChao Yu * meta page. 102f3f338caSChao Yu */ 103f3f338caSChao Yu if (unlikely(!PageUptodate(page))) 10438f91ca8SJaegeuk Kim f2fs_stop_checkpoint(sbi, false); 105393ff91fSJaegeuk Kim out: 106127e670aSJaegeuk Kim return page; 107127e670aSJaegeuk Kim } 108127e670aSJaegeuk Kim 1092b947003SChao Yu struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 1102b947003SChao Yu { 1112b947003SChao Yu return __get_meta_page(sbi, index, true); 1122b947003SChao Yu } 1132b947003SChao Yu 1142b947003SChao Yu /* for POR only */ 1152b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index) 1162b947003SChao Yu { 1172b947003SChao Yu return __get_meta_page(sbi, index, false); 1182b947003SChao Yu } 1192b947003SChao Yu 120f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type) 121662befdaSChao Yu { 122662befdaSChao Yu switch (type) { 123662befdaSChao Yu case META_NAT: 12466b00c18SChao Yu break; 125662befdaSChao Yu case META_SIT: 12666b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) 12766b00c18SChao Yu return false; 12866b00c18SChao Yu break; 12981c1a0f1SChao Yu case META_SSA: 13066b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || 13166b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr)) 13266b00c18SChao Yu return false; 13366b00c18SChao Yu break; 134662befdaSChao Yu case META_CP: 13566b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || 13666b00c18SChao Yu blkaddr < __start_cp_addr(sbi))) 13766b00c18SChao Yu return false; 13866b00c18SChao Yu break; 1394c521f49SJaegeuk Kim case META_POR: 14066b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || 14166b00c18SChao Yu blkaddr < MAIN_BLKADDR(sbi))) 14266b00c18SChao Yu return false; 14366b00c18SChao Yu break; 144662befdaSChao Yu default: 145662befdaSChao Yu BUG(); 146662befdaSChao Yu } 14766b00c18SChao Yu 14866b00c18SChao Yu return true; 149662befdaSChao Yu } 150662befdaSChao Yu 151662befdaSChao Yu /* 15281c1a0f1SChao Yu * Readahead CP/NAT/SIT/SSA pages 153662befdaSChao Yu */ 15426879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 15526879fb1SChao Yu int type, bool sync) 156662befdaSChao Yu { 157662befdaSChao Yu struct page *page; 1584c521f49SJaegeuk Kim block_t blkno = start; 159662befdaSChao Yu struct f2fs_io_info fio = { 16005ca3632SJaegeuk Kim .sbi = sbi, 161662befdaSChao Yu .type = META, 16204d328deSMike Christie .op = REQ_OP_READ, 16370fd7614SChristoph Hellwig .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, 1644375a336SJaegeuk Kim .encrypted_page = NULL, 165662befdaSChao Yu }; 166e9f5b8b8SChao Yu struct blk_plug plug; 167662befdaSChao Yu 1682b947003SChao Yu if (unlikely(type == META_POR)) 16904d328deSMike Christie fio.op_flags &= ~REQ_META; 1702b947003SChao Yu 171e9f5b8b8SChao Yu blk_start_plug(&plug); 172662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 173662befdaSChao Yu 17466b00c18SChao Yu if (!is_valid_blkaddr(sbi, blkno, type)) 17566b00c18SChao Yu goto out; 17666b00c18SChao Yu 177662befdaSChao Yu switch (type) { 178662befdaSChao Yu case META_NAT: 17966b00c18SChao Yu if (unlikely(blkno >= 18066b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) 181662befdaSChao Yu blkno = 0; 18266b00c18SChao Yu /* get nat block addr */ 1837a9d7548SChao Yu fio.new_blkaddr = current_nat_addr(sbi, 184662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 185662befdaSChao Yu break; 186662befdaSChao Yu case META_SIT: 187662befdaSChao Yu /* get sit block addr */ 1887a9d7548SChao Yu fio.new_blkaddr = current_sit_addr(sbi, 189662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 190662befdaSChao Yu break; 19181c1a0f1SChao Yu case META_SSA: 192662befdaSChao Yu case META_CP: 1934c521f49SJaegeuk Kim case META_POR: 1947a9d7548SChao Yu fio.new_blkaddr = blkno; 195662befdaSChao Yu break; 196662befdaSChao Yu default: 197662befdaSChao Yu BUG(); 198662befdaSChao Yu } 199662befdaSChao Yu 200300e129cSJaegeuk Kim page = f2fs_grab_cache_page(META_MAPPING(sbi), 201300e129cSJaegeuk Kim fio.new_blkaddr, false); 202662befdaSChao Yu if (!page) 203662befdaSChao Yu continue; 204662befdaSChao Yu if (PageUptodate(page)) { 205662befdaSChao Yu f2fs_put_page(page, 1); 206662befdaSChao Yu continue; 207662befdaSChao Yu } 208662befdaSChao Yu 20905ca3632SJaegeuk Kim fio.page = page; 2107a9d7548SChao Yu fio.old_blkaddr = fio.new_blkaddr; 21105ca3632SJaegeuk Kim f2fs_submit_page_mbio(&fio); 212662befdaSChao Yu f2fs_put_page(page, 0); 213662befdaSChao Yu } 214662befdaSChao Yu out: 215662befdaSChao Yu f2fs_submit_merged_bio(sbi, META, READ); 216e9f5b8b8SChao Yu blk_finish_plug(&plug); 217662befdaSChao Yu return blkno - start; 218662befdaSChao Yu } 219662befdaSChao Yu 220635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index) 221635aee1fSChao Yu { 222635aee1fSChao Yu struct page *page; 223635aee1fSChao Yu bool readahead = false; 224635aee1fSChao Yu 225635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index); 2264da7bf5aSJaegeuk Kim if (!page || !PageUptodate(page)) 227635aee1fSChao Yu readahead = true; 228635aee1fSChao Yu f2fs_put_page(page, 0); 229635aee1fSChao Yu 230635aee1fSChao Yu if (readahead) 231664ba972SJaegeuk Kim ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true); 232635aee1fSChao Yu } 233635aee1fSChao Yu 234127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page, 235127e670aSJaegeuk Kim struct writeback_control *wbc) 236127e670aSJaegeuk Kim { 2374081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page); 238577e3495SJaegeuk Kim 239ecda0de3SChao Yu trace_f2fs_writepage(page, META); 240ecda0de3SChao Yu 241caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 242cfb271d4SChao Yu goto redirty_out; 243857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) 244cfb271d4SChao Yu goto redirty_out; 2451e968fdfSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 246cf779cabSJaegeuk Kim goto redirty_out; 247127e670aSJaegeuk Kim 248577e3495SJaegeuk Kim write_meta_page(sbi, page); 249127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 2500c3a5797SChao Yu 2510c3a5797SChao Yu if (wbc->for_reclaim) 2520c3a5797SChao Yu f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE); 2530c3a5797SChao Yu 254127e670aSJaegeuk Kim unlock_page(page); 255857dc4e0SJaegeuk Kim 2560c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 257857dc4e0SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 2580c3a5797SChao Yu 259577e3495SJaegeuk Kim return 0; 260cfb271d4SChao Yu 261cfb271d4SChao Yu redirty_out: 26276f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 263cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 264127e670aSJaegeuk Kim } 265127e670aSJaegeuk Kim 266127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 267127e670aSJaegeuk Kim struct writeback_control *wbc) 268127e670aSJaegeuk Kim { 2694081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 27050c8cdb3SJaegeuk Kim long diff, written; 271127e670aSJaegeuk Kim 2725459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 27350c8cdb3SJaegeuk Kim if (wbc->for_kupdate || 27450c8cdb3SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META)) 275d3baf95dSJaegeuk Kim goto skip_write; 276127e670aSJaegeuk Kim 277d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 278d31c7c3fSYunlei He 279127e670aSJaegeuk Kim /* if mounting is failed, skip writing node pages */ 280127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 28150c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 28250c8cdb3SJaegeuk Kim written = sync_meta_pages(sbi, META, wbc->nr_to_write); 283127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 28450c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 285127e670aSJaegeuk Kim return 0; 286d3baf95dSJaegeuk Kim 287d3baf95dSJaegeuk Kim skip_write: 288d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 289d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 290d3baf95dSJaegeuk Kim return 0; 291127e670aSJaegeuk Kim } 292127e670aSJaegeuk Kim 293127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 294127e670aSJaegeuk Kim long nr_to_write) 295127e670aSJaegeuk Kim { 2969df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 29780dd9c0eSChao Yu pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX; 298127e670aSJaegeuk Kim struct pagevec pvec; 299127e670aSJaegeuk Kim long nwritten = 0; 300127e670aSJaegeuk Kim struct writeback_control wbc = { 301127e670aSJaegeuk Kim .for_reclaim = 0, 302127e670aSJaegeuk Kim }; 303e9f5b8b8SChao Yu struct blk_plug plug; 304127e670aSJaegeuk Kim 305127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 306127e670aSJaegeuk Kim 307e9f5b8b8SChao Yu blk_start_plug(&plug); 308e9f5b8b8SChao Yu 309127e670aSJaegeuk Kim while (index <= end) { 310127e670aSJaegeuk Kim int i, nr_pages; 311127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 312127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 313127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 314cfb271d4SChao Yu if (unlikely(nr_pages == 0)) 315127e670aSJaegeuk Kim break; 316127e670aSJaegeuk Kim 317127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 318127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 319203681f6SJaegeuk Kim 32080dd9c0eSChao Yu if (prev == ULONG_MAX) 3216066d8cdSJaegeuk Kim prev = page->index - 1; 3226066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 3236066d8cdSJaegeuk Kim pagevec_release(&pvec); 3246066d8cdSJaegeuk Kim goto stop; 3256066d8cdSJaegeuk Kim } 3266066d8cdSJaegeuk Kim 327127e670aSJaegeuk Kim lock_page(page); 328203681f6SJaegeuk Kim 329203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 330203681f6SJaegeuk Kim continue_unlock: 331203681f6SJaegeuk Kim unlock_page(page); 332203681f6SJaegeuk Kim continue; 333203681f6SJaegeuk Kim } 334203681f6SJaegeuk Kim if (!PageDirty(page)) { 335203681f6SJaegeuk Kim /* someone wrote it for us */ 336203681f6SJaegeuk Kim goto continue_unlock; 337203681f6SJaegeuk Kim } 338203681f6SJaegeuk Kim 339fa3d2bdfSJaegeuk Kim f2fs_wait_on_page_writeback(page, META, true); 340fa3d2bdfSJaegeuk Kim 341fa3d2bdfSJaegeuk Kim BUG_ON(PageWriteback(page)); 342203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 343203681f6SJaegeuk Kim goto continue_unlock; 344203681f6SJaegeuk Kim 34597dc3fd2SChao Yu if (mapping->a_ops->writepage(page, &wbc)) { 346577e3495SJaegeuk Kim unlock_page(page); 347577e3495SJaegeuk Kim break; 348577e3495SJaegeuk Kim } 349cfb271d4SChao Yu nwritten++; 3506066d8cdSJaegeuk Kim prev = page->index; 351cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 352127e670aSJaegeuk Kim break; 353127e670aSJaegeuk Kim } 354127e670aSJaegeuk Kim pagevec_release(&pvec); 355127e670aSJaegeuk Kim cond_resched(); 356127e670aSJaegeuk Kim } 3576066d8cdSJaegeuk Kim stop: 358127e670aSJaegeuk Kim if (nwritten) 359458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, type, WRITE); 360127e670aSJaegeuk Kim 361e9f5b8b8SChao Yu blk_finish_plug(&plug); 362e9f5b8b8SChao Yu 363127e670aSJaegeuk Kim return nwritten; 364127e670aSJaegeuk Kim } 365127e670aSJaegeuk Kim 366127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 367127e670aSJaegeuk Kim { 36826c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 36926c6b887SJaegeuk Kim 370237c0790SJaegeuk Kim if (!PageUptodate(page)) 371127e670aSJaegeuk Kim SetPageUptodate(page); 372127e670aSJaegeuk Kim if (!PageDirty(page)) { 373fe76b796SJaegeuk Kim f2fs_set_page_dirty_nobuffers(page); 3744081363fSJaegeuk Kim inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); 3751601839eSChao Yu SetPagePrivate(page); 3769e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 377127e670aSJaegeuk Kim return 1; 378127e670aSJaegeuk Kim } 379127e670aSJaegeuk Kim return 0; 380127e670aSJaegeuk Kim } 381127e670aSJaegeuk Kim 382127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 383127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 384127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 385127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 386487261f3SChao Yu .invalidatepage = f2fs_invalidate_page, 387487261f3SChao Yu .releasepage = f2fs_release_page, 3885b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION 3895b7a487cSWeichao Guo .migratepage = f2fs_migrate_page, 3905b7a487cSWeichao Guo #endif 391127e670aSJaegeuk Kim }; 392127e670aSJaegeuk Kim 3936451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 394953e6cc6SJaegeuk Kim { 39567298804SChao Yu struct inode_management *im = &sbi->im[type]; 39680c54505SJaegeuk Kim struct ino_entry *e, *tmp; 39780c54505SJaegeuk Kim 39880c54505SJaegeuk Kim tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS); 39939efac41SJaegeuk Kim retry: 40080c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 401769ec6e5SJaegeuk Kim 40267298804SChao Yu spin_lock(&im->ino_lock); 40367298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 40439efac41SJaegeuk Kim if (!e) { 40580c54505SJaegeuk Kim e = tmp; 40667298804SChao Yu if (radix_tree_insert(&im->ino_root, ino, e)) { 40767298804SChao Yu spin_unlock(&im->ino_lock); 408769ec6e5SJaegeuk Kim radix_tree_preload_end(); 40939efac41SJaegeuk Kim goto retry; 41039efac41SJaegeuk Kim } 41139efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 41239efac41SJaegeuk Kim e->ino = ino; 41339efac41SJaegeuk Kim 41467298804SChao Yu list_add_tail(&e->list, &im->ino_list); 4158c402946SJaegeuk Kim if (type != ORPHAN_INO) 41667298804SChao Yu im->ino_num++; 41739efac41SJaegeuk Kim } 41867298804SChao Yu spin_unlock(&im->ino_lock); 419769ec6e5SJaegeuk Kim radix_tree_preload_end(); 42080c54505SJaegeuk Kim 42180c54505SJaegeuk Kim if (e != tmp) 42280c54505SJaegeuk Kim kmem_cache_free(ino_entry_slab, tmp); 423953e6cc6SJaegeuk Kim } 424953e6cc6SJaegeuk Kim 4256451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 426953e6cc6SJaegeuk Kim { 42767298804SChao Yu struct inode_management *im = &sbi->im[type]; 4286451e041SJaegeuk Kim struct ino_entry *e; 429953e6cc6SJaegeuk Kim 43067298804SChao Yu spin_lock(&im->ino_lock); 43167298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 43239efac41SJaegeuk Kim if (e) { 433953e6cc6SJaegeuk Kim list_del(&e->list); 43467298804SChao Yu radix_tree_delete(&im->ino_root, ino); 43567298804SChao Yu im->ino_num--; 43667298804SChao Yu spin_unlock(&im->ino_lock); 4376451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 438953e6cc6SJaegeuk Kim return; 439953e6cc6SJaegeuk Kim } 44067298804SChao Yu spin_unlock(&im->ino_lock); 441953e6cc6SJaegeuk Kim } 442953e6cc6SJaegeuk Kim 443a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 444fff04f90SJaegeuk Kim { 445fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 446fff04f90SJaegeuk Kim __add_ino_entry(sbi, ino, type); 447fff04f90SJaegeuk Kim } 448fff04f90SJaegeuk Kim 449a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 450fff04f90SJaegeuk Kim { 451fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 452fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 453fff04f90SJaegeuk Kim } 454fff04f90SJaegeuk Kim 455fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */ 456fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 457fff04f90SJaegeuk Kim { 45867298804SChao Yu struct inode_management *im = &sbi->im[mode]; 459fff04f90SJaegeuk Kim struct ino_entry *e; 46067298804SChao Yu 46167298804SChao Yu spin_lock(&im->ino_lock); 46267298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 46367298804SChao Yu spin_unlock(&im->ino_lock); 464fff04f90SJaegeuk Kim return e ? true : false; 465fff04f90SJaegeuk Kim } 466fff04f90SJaegeuk Kim 46774ef9241SJaegeuk Kim void release_ino_entry(struct f2fs_sb_info *sbi, bool all) 468fff04f90SJaegeuk Kim { 469fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 470fff04f90SJaegeuk Kim int i; 471fff04f90SJaegeuk Kim 47274ef9241SJaegeuk Kim for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) { 47367298804SChao Yu struct inode_management *im = &sbi->im[i]; 47467298804SChao Yu 47567298804SChao Yu spin_lock(&im->ino_lock); 47667298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 477fff04f90SJaegeuk Kim list_del(&e->list); 47867298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 479fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 48067298804SChao Yu im->ino_num--; 481fff04f90SJaegeuk Kim } 48267298804SChao Yu spin_unlock(&im->ino_lock); 483fff04f90SJaegeuk Kim } 484fff04f90SJaegeuk Kim } 485fff04f90SJaegeuk Kim 486cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi) 487127e670aSJaegeuk Kim { 48867298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 489127e670aSJaegeuk Kim int err = 0; 490127e670aSJaegeuk Kim 49167298804SChao Yu spin_lock(&im->ino_lock); 492cb78942bSJaegeuk Kim 493cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION 4941ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) { 495cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock); 496cb78942bSJaegeuk Kim return -ENOSPC; 497cb78942bSJaegeuk Kim } 498cb78942bSJaegeuk Kim #endif 49967298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 500127e670aSJaegeuk Kim err = -ENOSPC; 501cbd56e7dSJaegeuk Kim else 50267298804SChao Yu im->ino_num++; 50367298804SChao Yu spin_unlock(&im->ino_lock); 5040d47c1adSGu Zheng 505127e670aSJaegeuk Kim return err; 506127e670aSJaegeuk Kim } 507127e670aSJaegeuk Kim 508cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi) 509cbd56e7dSJaegeuk Kim { 51067298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 51167298804SChao Yu 51267298804SChao Yu spin_lock(&im->ino_lock); 51367298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 51467298804SChao Yu im->ino_num--; 51567298804SChao Yu spin_unlock(&im->ino_lock); 516cbd56e7dSJaegeuk Kim } 517cbd56e7dSJaegeuk Kim 51867c3758dSJaegeuk Kim void add_orphan_inode(struct inode *inode) 519127e670aSJaegeuk Kim { 52039efac41SJaegeuk Kim /* add new orphan ino entry into list */ 52167c3758dSJaegeuk Kim __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO); 52267c3758dSJaegeuk Kim update_inode_page(inode); 523127e670aSJaegeuk Kim } 524127e670aSJaegeuk Kim 525127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 526127e670aSJaegeuk Kim { 527953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 5286451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 529127e670aSJaegeuk Kim } 530127e670aSJaegeuk Kim 5318c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 532127e670aSJaegeuk Kim { 5338c14bfadSChao Yu struct inode *inode; 5345905f9afSJaegeuk Kim struct node_info ni; 535d41065e2SJaegeuk Kim int err = acquire_orphan_inode(sbi); 536d41065e2SJaegeuk Kim 537d41065e2SJaegeuk Kim if (err) { 538d41065e2SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 539d41065e2SJaegeuk Kim f2fs_msg(sbi->sb, KERN_WARNING, 540d41065e2SJaegeuk Kim "%s: orphan failed (ino=%x), run fsck to fix.", 541d41065e2SJaegeuk Kim __func__, ino); 542d41065e2SJaegeuk Kim return err; 543d41065e2SJaegeuk Kim } 544d41065e2SJaegeuk Kim 545d41065e2SJaegeuk Kim __add_ino_entry(sbi, ino, ORPHAN_INO); 5468c14bfadSChao Yu 5475905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino); 5488c14bfadSChao Yu if (IS_ERR(inode)) { 5498c14bfadSChao Yu /* 5508c14bfadSChao Yu * there should be a bug that we can't find the entry 5518c14bfadSChao Yu * to orphan inode. 5528c14bfadSChao Yu */ 5538c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 5548c14bfadSChao Yu return PTR_ERR(inode); 5558c14bfadSChao Yu } 5568c14bfadSChao Yu 557127e670aSJaegeuk Kim clear_nlink(inode); 558127e670aSJaegeuk Kim 559127e670aSJaegeuk Kim /* truncate all the data during iput */ 560127e670aSJaegeuk Kim iput(inode); 5615905f9afSJaegeuk Kim 5625905f9afSJaegeuk Kim get_node_info(sbi, ino, &ni); 5635905f9afSJaegeuk Kim 5645905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */ 5655905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) { 5665905f9afSJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 5675905f9afSJaegeuk Kim f2fs_msg(sbi->sb, KERN_WARNING, 5685905f9afSJaegeuk Kim "%s: orphan failed (ino=%x), run fsck to fix.", 5695905f9afSJaegeuk Kim __func__, ino); 570d41065e2SJaegeuk Kim return -EIO; 5715905f9afSJaegeuk Kim } 572d41065e2SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 5738c14bfadSChao Yu return 0; 574127e670aSJaegeuk Kim } 575127e670aSJaegeuk Kim 5768c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi) 577127e670aSJaegeuk Kim { 5783c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 5798c14bfadSChao Yu int err; 580127e670aSJaegeuk Kim 581aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 5828c14bfadSChao Yu return 0; 583127e670aSJaegeuk Kim 58455141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 5853c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 586127e670aSJaegeuk Kim 58726879fb1SChao Yu ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 588662befdaSChao Yu 5893c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 590127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 591127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 592127e670aSJaegeuk Kim 593127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 594127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 595127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 5968c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 5978c14bfadSChao Yu if (err) { 5988c14bfadSChao Yu f2fs_put_page(page, 1); 5998c14bfadSChao Yu return err; 6008c14bfadSChao Yu } 601127e670aSJaegeuk Kim } 602127e670aSJaegeuk Kim f2fs_put_page(page, 1); 603127e670aSJaegeuk Kim } 604127e670aSJaegeuk Kim /* clear Orphan Flag */ 605aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 6068c14bfadSChao Yu return 0; 607127e670aSJaegeuk Kim } 608127e670aSJaegeuk Kim 609127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 610127e670aSJaegeuk Kim { 611502c6e0bSGu Zheng struct list_head *head; 612127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 613127e670aSJaegeuk Kim unsigned int nentries = 0; 614bd936f84SChao Yu unsigned short index = 1; 6158c402946SJaegeuk Kim unsigned short orphan_blocks; 6164531929eSGu Zheng struct page *page = NULL; 6176451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 61867298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 619127e670aSJaegeuk Kim 62067298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 6218c402946SJaegeuk Kim 622d6c67a4fSJaegeuk Kim /* 623d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 624d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 625d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 626d6c67a4fSJaegeuk Kim */ 62767298804SChao Yu head = &im->ino_list; 628127e670aSJaegeuk Kim 629127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 630502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 631502c6e0bSGu Zheng if (!page) { 632bd936f84SChao Yu page = grab_meta_page(sbi, start_blk++); 633502c6e0bSGu Zheng orphan_blk = 634502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 635502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 636502c6e0bSGu Zheng } 637127e670aSJaegeuk Kim 63836795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 639127e670aSJaegeuk Kim 64036795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 641127e670aSJaegeuk Kim /* 642127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 643127e670aSJaegeuk Kim * then we need to flush current orphan blocks 644127e670aSJaegeuk Kim * and bring another one in memory 645127e670aSJaegeuk Kim */ 646127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 647127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 648127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 649127e670aSJaegeuk Kim set_page_dirty(page); 650127e670aSJaegeuk Kim f2fs_put_page(page, 1); 651127e670aSJaegeuk Kim index++; 652127e670aSJaegeuk Kim nentries = 0; 653127e670aSJaegeuk Kim page = NULL; 654127e670aSJaegeuk Kim } 655127e670aSJaegeuk Kim } 656127e670aSJaegeuk Kim 657502c6e0bSGu Zheng if (page) { 658127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 659127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 660127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 661127e670aSJaegeuk Kim set_page_dirty(page); 662127e670aSJaegeuk Kim f2fs_put_page(page, 1); 663502c6e0bSGu Zheng } 664127e670aSJaegeuk Kim } 665127e670aSJaegeuk Kim 666fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 667fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page, 668fc0065adSTiezhu Yang unsigned long long *version) 669fc0065adSTiezhu Yang { 670fc0065adSTiezhu Yang unsigned long blk_size = sbi->blocksize; 671fc0065adSTiezhu Yang size_t crc_offset = 0; 672fc0065adSTiezhu Yang __u32 crc = 0; 673fc0065adSTiezhu Yang 674fc0065adSTiezhu Yang *cp_page = get_meta_page(sbi, cp_addr); 675fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 676fc0065adSTiezhu Yang 677fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 678fc0065adSTiezhu Yang if (crc_offset >= blk_size) { 679fc0065adSTiezhu Yang f2fs_msg(sbi->sb, KERN_WARNING, 680fc0065adSTiezhu Yang "invalid crc_offset: %zu", crc_offset); 681fc0065adSTiezhu Yang return -EINVAL; 682fc0065adSTiezhu Yang } 683fc0065adSTiezhu Yang 684fc0065adSTiezhu Yang crc = le32_to_cpu(*((__le32 *)((unsigned char *)*cp_block 685fc0065adSTiezhu Yang + crc_offset))); 686fc0065adSTiezhu Yang if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) { 687fc0065adSTiezhu Yang f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value"); 688fc0065adSTiezhu Yang return -EINVAL; 689fc0065adSTiezhu Yang } 690fc0065adSTiezhu Yang 691fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block); 692fc0065adSTiezhu Yang return 0; 693fc0065adSTiezhu Yang } 694fc0065adSTiezhu Yang 695127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 696127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 697127e670aSJaegeuk Kim { 698fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 699fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL; 700127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 701fc0065adSTiezhu Yang int err; 702127e670aSJaegeuk Kim 703fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 704fc0065adSTiezhu Yang &cp_page_1, version); 705fc0065adSTiezhu Yang if (err) 706127e670aSJaegeuk Kim goto invalid_cp1; 707fc0065adSTiezhu Yang pre_version = *version; 708127e670aSJaegeuk Kim 70925ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 710fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 711fc0065adSTiezhu Yang &cp_page_2, version); 712fc0065adSTiezhu Yang if (err) 713127e670aSJaegeuk Kim goto invalid_cp2; 714fc0065adSTiezhu Yang cur_version = *version; 715127e670aSJaegeuk Kim 716127e670aSJaegeuk Kim if (cur_version == pre_version) { 717127e670aSJaegeuk Kim *version = cur_version; 718127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 719127e670aSJaegeuk Kim return cp_page_1; 720127e670aSJaegeuk Kim } 721127e670aSJaegeuk Kim invalid_cp2: 722127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 723127e670aSJaegeuk Kim invalid_cp1: 724127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 725127e670aSJaegeuk Kim return NULL; 726127e670aSJaegeuk Kim } 727127e670aSJaegeuk Kim 728127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 729127e670aSJaegeuk Kim { 730127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 731127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 732127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 733127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 734127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 735127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 73655141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 7371dbe4152SChangman Lee block_t cp_blk_no; 7381dbe4152SChangman Lee int i; 739127e670aSJaegeuk Kim 7401dbe4152SChangman Lee sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL); 741127e670aSJaegeuk Kim if (!sbi->ckpt) 742127e670aSJaegeuk Kim return -ENOMEM; 743127e670aSJaegeuk Kim /* 744127e670aSJaegeuk Kim * Finding out valid cp block involves read both 745127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 746127e670aSJaegeuk Kim */ 747127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 748127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 749127e670aSJaegeuk Kim 750127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 751f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 752f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 753127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 754127e670aSJaegeuk Kim 755127e670aSJaegeuk Kim if (cp1 && cp2) { 756127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 757127e670aSJaegeuk Kim cur_page = cp2; 758127e670aSJaegeuk Kim else 759127e670aSJaegeuk Kim cur_page = cp1; 760127e670aSJaegeuk Kim } else if (cp1) { 761127e670aSJaegeuk Kim cur_page = cp1; 762127e670aSJaegeuk Kim } else if (cp2) { 763127e670aSJaegeuk Kim cur_page = cp2; 764127e670aSJaegeuk Kim } else { 765127e670aSJaegeuk Kim goto fail_no_cp; 766127e670aSJaegeuk Kim } 767127e670aSJaegeuk Kim 768127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 769127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 770127e670aSJaegeuk Kim 771984ec63cSShawn Lin /* Sanity checking of checkpoint */ 772984ec63cSShawn Lin if (sanity_check_ckpt(sbi)) 773a2125ff7SJaegeuk Kim goto free_fail_no_cp; 774984ec63cSShawn Lin 7758508e44aSJaegeuk Kim if (cur_page == cp1) 7768508e44aSJaegeuk Kim sbi->cur_cp_pack = 1; 7778508e44aSJaegeuk Kim else 7788508e44aSJaegeuk Kim sbi->cur_cp_pack = 2; 7791dbe4152SChangman Lee 7801dbe4152SChangman Lee if (cp_blks <= 1) 7811dbe4152SChangman Lee goto done; 7821dbe4152SChangman Lee 7831dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 7841dbe4152SChangman Lee if (cur_page == cp2) 7851dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 7861dbe4152SChangman Lee 7871dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 7881dbe4152SChangman Lee void *sit_bitmap_ptr; 7891dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 7901dbe4152SChangman Lee 7911dbe4152SChangman Lee cur_page = get_meta_page(sbi, cp_blk_no + i); 7921dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 793127e670aSJaegeuk Kim memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 794127e670aSJaegeuk Kim f2fs_put_page(cur_page, 1); 795127e670aSJaegeuk Kim } 796127e670aSJaegeuk Kim done: 797127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 798127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 799127e670aSJaegeuk Kim return 0; 800127e670aSJaegeuk Kim 801a2125ff7SJaegeuk Kim free_fail_no_cp: 802a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1); 803a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1); 804127e670aSJaegeuk Kim fail_no_cp: 805127e670aSJaegeuk Kim kfree(sbi->ckpt); 806127e670aSJaegeuk Kim return -EINVAL; 807127e670aSJaegeuk Kim } 808127e670aSJaegeuk Kim 809c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 810127e670aSJaegeuk Kim { 8114081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 812c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 8132d7b822aSChao Yu 81491942321SJaegeuk Kim if (is_inode_flag_set(inode, flag)) 8152710fd7eSChao Yu return; 8162d7b822aSChao Yu 81791942321SJaegeuk Kim set_inode_flag(inode, flag); 81891942321SJaegeuk Kim list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]); 81933fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 8205deb8267SJaegeuk Kim } 8215deb8267SJaegeuk Kim 822c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 8236ad7609aSChao Yu { 824c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 8256ad7609aSChao Yu 82691942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 8276ad7609aSChao Yu return; 8286ad7609aSChao Yu 82991942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list); 83091942321SJaegeuk Kim clear_inode_flag(inode, flag); 83133fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 8326ad7609aSChao Yu } 8336ad7609aSChao Yu 834a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page) 8355deb8267SJaegeuk Kim { 8364081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 837c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 8385deb8267SJaegeuk Kim 8395ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 8405ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 841127e670aSJaegeuk Kim return; 8427bd59381SGu Zheng 843c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 8441c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 845c227f912SChao Yu __add_dirty_inode(inode, type); 846b951a4ecSYunlei He inode_inc_dirty_pages(inode); 8471c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]); 8481c4bf763SJaegeuk Kim 849a7ffdbe2SJaegeuk Kim SetPagePrivate(page); 8509e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 8515deb8267SJaegeuk Kim } 852127e670aSJaegeuk Kim 853c227f912SChao Yu void remove_dirty_inode(struct inode *inode) 854127e670aSJaegeuk Kim { 8554081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 856c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 857127e670aSJaegeuk Kim 858c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 859c227f912SChao Yu !S_ISLNK(inode->i_mode)) 860127e670aSJaegeuk Kim return; 861127e670aSJaegeuk Kim 86210aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 86310aa97c3SJaegeuk Kim return; 86410aa97c3SJaegeuk Kim 865c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 866c227f912SChao Yu __remove_dirty_inode(inode, type); 867c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 868afc3eda2SJaegeuk Kim } 86974d0b917SJaegeuk Kim 8706d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 871127e670aSJaegeuk Kim { 872ce3b7d80SGu Zheng struct list_head *head; 873127e670aSJaegeuk Kim struct inode *inode; 8742710fd7eSChao Yu struct f2fs_inode_info *fi; 8754cf18537SChao Yu bool is_dir = (type == DIR_INODE); 8764cf18537SChao Yu 8774cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 8784cf18537SChao Yu get_pages(sbi, is_dir ? 8794cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 880127e670aSJaegeuk Kim retry: 881af41d3eeSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 8826d5a1495SChao Yu return -EIO; 883af41d3eeSJaegeuk Kim 884c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 885ce3b7d80SGu Zheng 886c227f912SChao Yu head = &sbi->inode_list[type]; 887127e670aSJaegeuk Kim if (list_empty(head)) { 888c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 8894cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 8904cf18537SChao Yu get_pages(sbi, is_dir ? 8914cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 8926d5a1495SChao Yu return 0; 893127e670aSJaegeuk Kim } 894*939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 8952710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 896c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 897127e670aSJaegeuk Kim if (inode) { 89887d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 899127e670aSJaegeuk Kim iput(inode); 900127e670aSJaegeuk Kim } else { 901127e670aSJaegeuk Kim /* 902127e670aSJaegeuk Kim * We should submit bio, since it exists several 903127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 904127e670aSJaegeuk Kim */ 905458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 9067ecebe5eSSebastian Andrzej Siewior cond_resched(); 907127e670aSJaegeuk Kim } 908127e670aSJaegeuk Kim goto retry; 909127e670aSJaegeuk Kim } 910127e670aSJaegeuk Kim 9110f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 9120f18b462SJaegeuk Kim { 9130f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META]; 9140f18b462SJaegeuk Kim struct inode *inode; 9150f18b462SJaegeuk Kim struct f2fs_inode_info *fi; 9160f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 9170f18b462SJaegeuk Kim 9180f18b462SJaegeuk Kim while (total--) { 9190f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 9200f18b462SJaegeuk Kim return -EIO; 9210f18b462SJaegeuk Kim 9220f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 9230f18b462SJaegeuk Kim if (list_empty(head)) { 9240f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 9250f18b462SJaegeuk Kim return 0; 9260f18b462SJaegeuk Kim } 927*939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, 9280f18b462SJaegeuk Kim gdirty_list); 9290f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode); 9300f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 9310f18b462SJaegeuk Kim if (inode) { 93218340edcSJaegeuk Kim sync_inode_metadata(inode, 0); 93318340edcSJaegeuk Kim 93418340edcSJaegeuk Kim /* it's on eviction */ 93518340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 9360f18b462SJaegeuk Kim update_inode_page(inode); 9370f18b462SJaegeuk Kim iput(inode); 9380f18b462SJaegeuk Kim } 9390f18b462SJaegeuk Kim }; 9400f18b462SJaegeuk Kim return 0; 9410f18b462SJaegeuk Kim } 9420f18b462SJaegeuk Kim 9430a8165d7SJaegeuk Kim /* 944127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 945127e670aSJaegeuk Kim */ 946cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 947127e670aSJaegeuk Kim { 948127e670aSJaegeuk Kim struct writeback_control wbc = { 949127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 950127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 951127e670aSJaegeuk Kim .for_reclaim = 0, 952127e670aSJaegeuk Kim }; 953c718379bSJaegeuk Kim struct blk_plug plug; 954cf779cabSJaegeuk Kim int err = 0; 955c718379bSJaegeuk Kim 956c718379bSJaegeuk Kim blk_start_plug(&plug); 957c718379bSJaegeuk Kim 95839936837SJaegeuk Kim retry_flush_dents: 959e479556bSGu Zheng f2fs_lock_all(sbi); 960127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 961127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 962e479556bSGu Zheng f2fs_unlock_all(sbi); 9636d5a1495SChao Yu err = sync_dirty_inodes(sbi, DIR_INODE); 9646d5a1495SChao Yu if (err) 965cf779cabSJaegeuk Kim goto out; 96639936837SJaegeuk Kim goto retry_flush_dents; 967127e670aSJaegeuk Kim } 968127e670aSJaegeuk Kim 9690f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 9700f18b462SJaegeuk Kim f2fs_unlock_all(sbi); 9710f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi); 9720f18b462SJaegeuk Kim if (err) 9730f18b462SJaegeuk Kim goto out; 9740f18b462SJaegeuk Kim goto retry_flush_dents; 9750f18b462SJaegeuk Kim } 9760f18b462SJaegeuk Kim 977127e670aSJaegeuk Kim /* 978e1c42045Sarter97 * POR: we should ensure that there are no dirty node pages 979127e670aSJaegeuk Kim * until finishing nat/sit flush. 980127e670aSJaegeuk Kim */ 98139936837SJaegeuk Kim retry_flush_nodes: 982b3582c68SChao Yu down_write(&sbi->node_write); 983127e670aSJaegeuk Kim 984127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 985b3582c68SChao Yu up_write(&sbi->node_write); 98652681375SJaegeuk Kim err = sync_node_pages(sbi, &wbc); 9876d5a1495SChao Yu if (err) { 988cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 989cf779cabSJaegeuk Kim goto out; 990cf779cabSJaegeuk Kim } 99139936837SJaegeuk Kim goto retry_flush_nodes; 992127e670aSJaegeuk Kim } 993cf779cabSJaegeuk Kim out: 994c718379bSJaegeuk Kim blk_finish_plug(&plug); 995cf779cabSJaegeuk Kim return err; 996127e670aSJaegeuk Kim } 997127e670aSJaegeuk Kim 998127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 999127e670aSJaegeuk Kim { 1000b3582c68SChao Yu up_write(&sbi->node_write); 1001ad4edb83SJaegeuk Kim 10023a2ad567SChao Yu build_free_nids(sbi, false); 1003e479556bSGu Zheng f2fs_unlock_all(sbi); 1004127e670aSJaegeuk Kim } 1005127e670aSJaegeuk Kim 1006fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 1007fb51b5efSChangman Lee { 1008fb51b5efSChangman Lee DEFINE_WAIT(wait); 1009fb51b5efSChangman Lee 1010fb51b5efSChangman Lee for (;;) { 1011fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 1012fb51b5efSChangman Lee 101336951b38SChao Yu if (!get_pages(sbi, F2FS_WB_CP_DATA)) 1014fb51b5efSChangman Lee break; 1015fb51b5efSChangman Lee 10160ff21646SYunlei He io_schedule_timeout(5*HZ); 1017fb51b5efSChangman Lee } 1018fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 1019fb51b5efSChangman Lee } 1020fb51b5efSChangman Lee 1021e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1022e4c5d848SJaegeuk Kim { 1023e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1024e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1025e4c5d848SJaegeuk Kim 1026e4c5d848SJaegeuk Kim spin_lock(&sbi->cp_lock); 1027e4c5d848SJaegeuk Kim 1028e4c5d848SJaegeuk Kim if (cpc->reason == CP_UMOUNT) 1029e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1030e4c5d848SJaegeuk Kim else 1031e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1032e4c5d848SJaegeuk Kim 1033e4c5d848SJaegeuk Kim if (cpc->reason == CP_FASTBOOT) 1034e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1035e4c5d848SJaegeuk Kim else 1036e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1037e4c5d848SJaegeuk Kim 1038e4c5d848SJaegeuk Kim if (orphan_num) 1039e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1040e4c5d848SJaegeuk Kim else 1041e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1042e4c5d848SJaegeuk Kim 1043e4c5d848SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1044e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1045e4c5d848SJaegeuk Kim 1046e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */ 1047e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1048e4c5d848SJaegeuk Kim 1049e4c5d848SJaegeuk Kim spin_unlock(&sbi->cp_lock); 1050e4c5d848SJaegeuk Kim } 1051e4c5d848SJaegeuk Kim 1052c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1053127e670aSJaegeuk Kim { 1054127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 105577041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 105667298804SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 105777041823SHuang Ying nid_t last_nid = nm_i->next_scan_nid; 1058127e670aSJaegeuk Kim block_t start_blk; 1059127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 10607e586fa0SJaegeuk Kim __u32 crc32 = 0; 1061127e670aSJaegeuk Kim int i; 106255141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 10638f1dbbbbSShuoran Liu struct super_block *sb = sbi->sb; 10648f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 10658f1dbbbbSShuoran Liu u64 kbytes_written; 1066127e670aSJaegeuk Kim 1067127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 1068cf779cabSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) { 1069127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 1070cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1071c34f42e2SChao Yu return -EIO; 1072cf779cabSJaegeuk Kim } 1073127e670aSJaegeuk Kim 1074127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 1075127e670aSJaegeuk Kim 1076127e670aSJaegeuk Kim /* 1077127e670aSJaegeuk Kim * modify checkpoint 1078127e670aSJaegeuk Kim * version number is already updated 1079127e670aSJaegeuk Kim */ 1080127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 1081127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 1082127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1083b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1084127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 1085127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 1086127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 1087127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 1088127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 1089127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 1090127e670aSJaegeuk Kim } 1091b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1092127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 1093127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 1094127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 1095127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 1096127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 1097127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 1098127e670aSJaegeuk Kim } 1099127e670aSJaegeuk Kim 1100127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 1101127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 1102127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 1103127e670aSJaegeuk Kim 1104127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 11053fa06d7bSChao Yu data_sum_blocks = npages_for_summary_flush(sbi, false); 1106aaec2b1dSChao Yu spin_lock(&sbi->cp_lock); 1107b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1108aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1109127e670aSJaegeuk Kim else 1110aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1111aaec2b1dSChao Yu spin_unlock(&sbi->cp_lock); 1112127e670aSJaegeuk Kim 111367298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 11141dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 11151dbe4152SChangman Lee orphan_blocks); 1116127e670aSJaegeuk Kim 1117119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1118b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+ 11191dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 11201dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1121119ee914SJaegeuk Kim else 1122b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 11231dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 11241dbe4152SChangman Lee orphan_blocks); 1125119ee914SJaegeuk Kim 1126e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */ 1127e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc); 1128a468f0efSJaegeuk Kim 1129127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1130127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1131127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1132127e670aSJaegeuk Kim 113343b6573bSKeith Mok crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset)); 11347e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 11357e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1136127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1137127e670aSJaegeuk Kim 11388508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi); 1139127e670aSJaegeuk Kim 1140a7230d16SJaegeuk Kim /* need to wait for end_io results */ 1141a7230d16SJaegeuk Kim wait_on_all_pages_writeback(sbi); 1142a7230d16SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1143c34f42e2SChao Yu return -EIO; 1144a7230d16SJaegeuk Kim 1145127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 1146381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk++); 1147127e670aSJaegeuk Kim 1148381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 1149381722d2SChao Yu update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1150381722d2SChao Yu start_blk++); 11511dbe4152SChangman Lee 115267298804SChao Yu if (orphan_num) { 1153127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1154127e670aSJaegeuk Kim start_blk += orphan_blocks; 1155127e670aSJaegeuk Kim } 1156127e670aSJaegeuk Kim 1157127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 1158127e670aSJaegeuk Kim start_blk += data_sum_blocks; 11598f1dbbbbSShuoran Liu 11608f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 11618f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 11628f1dbbbbSShuoran Liu if (sb->s_bdev->bd_part) 11638f1dbbbbSShuoran Liu kbytes_written += BD_PART_WRITTEN(sbi); 11648f1dbbbbSShuoran Liu 1165b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 11668f1dbbbbSShuoran Liu 1167119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 1168127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 1169127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1170127e670aSJaegeuk Kim } 1171127e670aSJaegeuk Kim 1172127e670aSJaegeuk Kim /* writeout checkpoint block */ 1173381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk); 1174127e670aSJaegeuk Kim 1175127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 1176fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 1177127e670aSJaegeuk Kim 1178cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1179c34f42e2SChao Yu return -EIO; 1180cf779cabSJaegeuk Kim 118180dd9c0eSChao Yu filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX); 118280dd9c0eSChao Yu filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX); 1183127e670aSJaegeuk Kim 1184127e670aSJaegeuk Kim /* update user_block_counts */ 1185127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 118641382ec4SJaegeuk Kim percpu_counter_set(&sbi->alloc_valid_block_count, 0); 1187127e670aSJaegeuk Kim 1188127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 1189127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 1190127e670aSJaegeuk Kim 11916a8f8ca5SJaegeuk Kim /* wait for previous submitted meta pages writeback */ 11926a8f8ca5SJaegeuk Kim wait_on_all_pages_writeback(sbi); 11936a8f8ca5SJaegeuk Kim 119474ef9241SJaegeuk Kim release_ino_entry(sbi, false); 1195cf779cabSJaegeuk Kim 1196cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1197c34f42e2SChao Yu return -EIO; 1198cf779cabSJaegeuk Kim 1199caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1200bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP); 12018508e44aSJaegeuk Kim __set_cp_next_pack(sbi); 1202c34f42e2SChao Yu 1203c2a080aeSChao Yu /* 1204c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is 1205c2a080aeSChao Yu * updated during writing checkpoint. 1206c2a080aeSChao Yu */ 1207c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) || 1208c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA)) 1209c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 1210c2a080aeSChao Yu 1211c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1212c2a080aeSChao Yu 1213c34f42e2SChao Yu return 0; 1214127e670aSJaegeuk Kim } 1215127e670aSJaegeuk Kim 12160a8165d7SJaegeuk Kim /* 1217e1c42045Sarter97 * We guarantee that this checkpoint procedure will not fail. 1218127e670aSJaegeuk Kim */ 1219c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1220127e670aSJaegeuk Kim { 1221127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1222127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1223c34f42e2SChao Yu int err = 0; 1224127e670aSJaegeuk Kim 1225127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 12268501017eSJaegeuk Kim 1227caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1228a66cdd98SJaegeuk Kim (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC || 1229a66cdd98SJaegeuk Kim (cpc->reason == CP_DISCARD && !sbi->discard_blks))) 12308501017eSJaegeuk Kim goto out; 1231c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1232c34f42e2SChao Yu err = -EIO; 1233cf779cabSJaegeuk Kim goto out; 1234c34f42e2SChao Yu } 1235c34f42e2SChao Yu if (f2fs_readonly(sbi->sb)) { 1236c34f42e2SChao Yu err = -EROFS; 123711504a8eSJaegeuk Kim goto out; 1238c34f42e2SChao Yu } 12392bda542dSWanpeng Li 12402bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 12412bda542dSWanpeng Li 1242c34f42e2SChao Yu err = block_operations(sbi); 1243c34f42e2SChao Yu if (err) 1244cf779cabSJaegeuk Kim goto out; 1245127e670aSJaegeuk Kim 124675ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 12472af4bd6cSNamjae Jeon 1248406657ddSChao Yu f2fs_flush_merged_bios(sbi); 1249127e670aSJaegeuk Kim 125058cce381SYunlei He /* this is the case of multiple fstrims without any changes */ 12510333ad4eSJaegeuk Kim if (cpc->reason == CP_DISCARD) { 125225290fa5SJaegeuk Kim if (!exist_trim_candidates(sbi, cpc)) { 125325290fa5SJaegeuk Kim unblock_operations(sbi); 125425290fa5SJaegeuk Kim goto out; 125525290fa5SJaegeuk Kim } 125625290fa5SJaegeuk Kim 12570333ad4eSJaegeuk Kim if (NM_I(sbi)->dirty_nat_cnt == 0 && 12580333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 && 12590333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) { 126058cce381SYunlei He flush_sit_entries(sbi, cpc); 126158cce381SYunlei He clear_prefree_segments(sbi, cpc); 126258cce381SYunlei He unblock_operations(sbi); 126358cce381SYunlei He goto out; 126458cce381SYunlei He } 12650333ad4eSJaegeuk Kim } 126658cce381SYunlei He 1267127e670aSJaegeuk Kim /* 1268127e670aSJaegeuk Kim * update checkpoint pack index 1269127e670aSJaegeuk Kim * Increase the version number so that 1270127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1271127e670aSJaegeuk Kim */ 1272d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1273127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1274127e670aSJaegeuk Kim 1275127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 1276127e670aSJaegeuk Kim flush_nat_entries(sbi); 12774b2fecc8SJaegeuk Kim flush_sit_entries(sbi, cpc); 1278127e670aSJaegeuk Kim 1279127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 1280c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 12814e6a8d9bSJaegeuk Kim if (err) 12822dd15654SChao Yu release_discard_addrs(sbi); 12834e6a8d9bSJaegeuk Kim else 12842dd15654SChao Yu clear_prefree_segments(sbi, cpc); 1285275b66b0SChao Yu 1286127e670aSJaegeuk Kim unblock_operations(sbi); 1287942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 128810027551SJaegeuk Kim 128910027551SJaegeuk Kim if (cpc->reason == CP_RECOVERY) 129010027551SJaegeuk Kim f2fs_msg(sbi->sb, KERN_NOTICE, 129110027551SJaegeuk Kim "checkpoint: version = %llx", ckpt_ver); 129260b99b48SJaegeuk Kim 129360b99b48SJaegeuk Kim /* do checkpoint periodically */ 12946beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 129555d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 12968501017eSJaegeuk Kim out: 12978501017eSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 1298c34f42e2SChao Yu return err; 1299127e670aSJaegeuk Kim } 1300127e670aSJaegeuk Kim 13016451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi) 1302127e670aSJaegeuk Kim { 13036451e041SJaegeuk Kim int i; 13046451e041SJaegeuk Kim 13056451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 130667298804SChao Yu struct inode_management *im = &sbi->im[i]; 130767298804SChao Yu 130867298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 130967298804SChao Yu spin_lock_init(&im->ino_lock); 131067298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 131167298804SChao Yu im->ino_num = 0; 13126451e041SJaegeuk Kim } 13136451e041SJaegeuk Kim 1314b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 131514b42817SWanpeng Li NR_CURSEG_TYPE - __cp_payload(sbi)) * 131614b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1317127e670aSJaegeuk Kim } 1318127e670aSJaegeuk Kim 13196e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 1320127e670aSJaegeuk Kim { 13216451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 13226451e041SJaegeuk Kim sizeof(struct ino_entry)); 13236451e041SJaegeuk Kim if (!ino_entry_slab) 1324127e670aSJaegeuk Kim return -ENOMEM; 132506292073SChao Yu inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 132606292073SChao Yu sizeof(struct inode_entry)); 13276bacf52fSJaegeuk Kim if (!inode_entry_slab) { 13286451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1329127e670aSJaegeuk Kim return -ENOMEM; 1330127e670aSJaegeuk Kim } 1331127e670aSJaegeuk Kim return 0; 1332127e670aSJaegeuk Kim } 1333127e670aSJaegeuk Kim 1334127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 1335127e670aSJaegeuk Kim { 13366451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1337127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 1338127e670aSJaegeuk Kim } 1339