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) 252942fd319SJaegeuk Kim f2fs_submit_merged_bio_cond(sbi, page->mapping->host, 253942fd319SJaegeuk Kim 0, page->index, META, WRITE); 2540c3a5797SChao Yu 255127e670aSJaegeuk Kim unlock_page(page); 256857dc4e0SJaegeuk Kim 2570c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi))) 258857dc4e0SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 2590c3a5797SChao Yu 260577e3495SJaegeuk Kim return 0; 261cfb271d4SChao Yu 262cfb271d4SChao Yu redirty_out: 26376f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 264cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 265127e670aSJaegeuk Kim } 266127e670aSJaegeuk Kim 267127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 268127e670aSJaegeuk Kim struct writeback_control *wbc) 269127e670aSJaegeuk Kim { 2704081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 27150c8cdb3SJaegeuk Kim long diff, written; 272127e670aSJaegeuk Kim 2735459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 27450c8cdb3SJaegeuk Kim if (wbc->for_kupdate || 27550c8cdb3SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META)) 276d3baf95dSJaegeuk Kim goto skip_write; 277127e670aSJaegeuk Kim 278a29d0e0bSYunlei He /* if locked failed, cp will flush dirty pages instead */ 279a29d0e0bSYunlei He if (!mutex_trylock(&sbi->cp_mutex)) 280a29d0e0bSYunlei He goto skip_write; 281d31c7c3fSYunlei He 282a29d0e0bSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 28350c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 28450c8cdb3SJaegeuk Kim written = sync_meta_pages(sbi, META, wbc->nr_to_write); 285127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 28650c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 287127e670aSJaegeuk Kim return 0; 288d3baf95dSJaegeuk Kim 289d3baf95dSJaegeuk Kim skip_write: 290d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 291d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META); 292d3baf95dSJaegeuk Kim return 0; 293127e670aSJaegeuk Kim } 294127e670aSJaegeuk Kim 295127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 296127e670aSJaegeuk Kim long nr_to_write) 297127e670aSJaegeuk Kim { 2989df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 29980dd9c0eSChao Yu pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX; 300127e670aSJaegeuk Kim struct pagevec pvec; 301127e670aSJaegeuk Kim long nwritten = 0; 302127e670aSJaegeuk Kim struct writeback_control wbc = { 303127e670aSJaegeuk Kim .for_reclaim = 0, 304127e670aSJaegeuk Kim }; 305e9f5b8b8SChao Yu struct blk_plug plug; 306127e670aSJaegeuk Kim 307127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 308127e670aSJaegeuk Kim 309e9f5b8b8SChao Yu blk_start_plug(&plug); 310e9f5b8b8SChao Yu 311127e670aSJaegeuk Kim while (index <= end) { 312127e670aSJaegeuk Kim int i, nr_pages; 313127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 314127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 315127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 316cfb271d4SChao Yu if (unlikely(nr_pages == 0)) 317127e670aSJaegeuk Kim break; 318127e670aSJaegeuk Kim 319127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 320127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 321203681f6SJaegeuk Kim 32280dd9c0eSChao Yu if (prev == ULONG_MAX) 3236066d8cdSJaegeuk Kim prev = page->index - 1; 3246066d8cdSJaegeuk Kim if (nr_to_write != LONG_MAX && page->index != prev + 1) { 3256066d8cdSJaegeuk Kim pagevec_release(&pvec); 3266066d8cdSJaegeuk Kim goto stop; 3276066d8cdSJaegeuk Kim } 3286066d8cdSJaegeuk Kim 329127e670aSJaegeuk Kim lock_page(page); 330203681f6SJaegeuk Kim 331203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 332203681f6SJaegeuk Kim continue_unlock: 333203681f6SJaegeuk Kim unlock_page(page); 334203681f6SJaegeuk Kim continue; 335203681f6SJaegeuk Kim } 336203681f6SJaegeuk Kim if (!PageDirty(page)) { 337203681f6SJaegeuk Kim /* someone wrote it for us */ 338203681f6SJaegeuk Kim goto continue_unlock; 339203681f6SJaegeuk Kim } 340203681f6SJaegeuk Kim 341fa3d2bdfSJaegeuk Kim f2fs_wait_on_page_writeback(page, META, true); 342fa3d2bdfSJaegeuk Kim 343fa3d2bdfSJaegeuk Kim BUG_ON(PageWriteback(page)); 344203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 345203681f6SJaegeuk Kim goto continue_unlock; 346203681f6SJaegeuk Kim 34797dc3fd2SChao Yu if (mapping->a_ops->writepage(page, &wbc)) { 348577e3495SJaegeuk Kim unlock_page(page); 349577e3495SJaegeuk Kim break; 350577e3495SJaegeuk Kim } 351cfb271d4SChao Yu nwritten++; 3526066d8cdSJaegeuk Kim prev = page->index; 353cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 354127e670aSJaegeuk Kim break; 355127e670aSJaegeuk Kim } 356127e670aSJaegeuk Kim pagevec_release(&pvec); 357127e670aSJaegeuk Kim cond_resched(); 358127e670aSJaegeuk Kim } 3596066d8cdSJaegeuk Kim stop: 360127e670aSJaegeuk Kim if (nwritten) 361458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, type, WRITE); 362127e670aSJaegeuk Kim 363e9f5b8b8SChao Yu blk_finish_plug(&plug); 364e9f5b8b8SChao Yu 365127e670aSJaegeuk Kim return nwritten; 366127e670aSJaegeuk Kim } 367127e670aSJaegeuk Kim 368127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 369127e670aSJaegeuk Kim { 37026c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 37126c6b887SJaegeuk Kim 372237c0790SJaegeuk Kim if (!PageUptodate(page)) 373127e670aSJaegeuk Kim SetPageUptodate(page); 374127e670aSJaegeuk Kim if (!PageDirty(page)) { 375fe76b796SJaegeuk Kim f2fs_set_page_dirty_nobuffers(page); 3764081363fSJaegeuk Kim inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); 3771601839eSChao Yu SetPagePrivate(page); 3789e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 379127e670aSJaegeuk Kim return 1; 380127e670aSJaegeuk Kim } 381127e670aSJaegeuk Kim return 0; 382127e670aSJaegeuk Kim } 383127e670aSJaegeuk Kim 384127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 385127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 386127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 387127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 388487261f3SChao Yu .invalidatepage = f2fs_invalidate_page, 389487261f3SChao Yu .releasepage = f2fs_release_page, 3905b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION 3915b7a487cSWeichao Guo .migratepage = f2fs_migrate_page, 3925b7a487cSWeichao Guo #endif 393127e670aSJaegeuk Kim }; 394127e670aSJaegeuk Kim 3956451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 396953e6cc6SJaegeuk Kim { 39767298804SChao Yu struct inode_management *im = &sbi->im[type]; 39880c54505SJaegeuk Kim struct ino_entry *e, *tmp; 39980c54505SJaegeuk Kim 40080c54505SJaegeuk Kim tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS); 40139efac41SJaegeuk Kim retry: 40280c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 403769ec6e5SJaegeuk Kim 40467298804SChao Yu spin_lock(&im->ino_lock); 40567298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 40639efac41SJaegeuk Kim if (!e) { 40780c54505SJaegeuk Kim e = tmp; 40867298804SChao Yu if (radix_tree_insert(&im->ino_root, ino, e)) { 40967298804SChao Yu spin_unlock(&im->ino_lock); 410769ec6e5SJaegeuk Kim radix_tree_preload_end(); 41139efac41SJaegeuk Kim goto retry; 41239efac41SJaegeuk Kim } 41339efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 41439efac41SJaegeuk Kim e->ino = ino; 41539efac41SJaegeuk Kim 41667298804SChao Yu list_add_tail(&e->list, &im->ino_list); 4178c402946SJaegeuk Kim if (type != ORPHAN_INO) 41867298804SChao Yu im->ino_num++; 41939efac41SJaegeuk Kim } 42067298804SChao Yu spin_unlock(&im->ino_lock); 421769ec6e5SJaegeuk Kim radix_tree_preload_end(); 42280c54505SJaegeuk Kim 42380c54505SJaegeuk Kim if (e != tmp) 42480c54505SJaegeuk Kim kmem_cache_free(ino_entry_slab, tmp); 425953e6cc6SJaegeuk Kim } 426953e6cc6SJaegeuk Kim 4276451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 428953e6cc6SJaegeuk Kim { 42967298804SChao Yu struct inode_management *im = &sbi->im[type]; 4306451e041SJaegeuk Kim struct ino_entry *e; 431953e6cc6SJaegeuk Kim 43267298804SChao Yu spin_lock(&im->ino_lock); 43367298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 43439efac41SJaegeuk Kim if (e) { 435953e6cc6SJaegeuk Kim list_del(&e->list); 43667298804SChao Yu radix_tree_delete(&im->ino_root, ino); 43767298804SChao Yu im->ino_num--; 43867298804SChao Yu spin_unlock(&im->ino_lock); 4396451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 440953e6cc6SJaegeuk Kim return; 441953e6cc6SJaegeuk Kim } 44267298804SChao Yu spin_unlock(&im->ino_lock); 443953e6cc6SJaegeuk Kim } 444953e6cc6SJaegeuk Kim 445a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 446fff04f90SJaegeuk Kim { 447fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 448fff04f90SJaegeuk Kim __add_ino_entry(sbi, ino, type); 449fff04f90SJaegeuk Kim } 450fff04f90SJaegeuk Kim 451a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 452fff04f90SJaegeuk Kim { 453fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 454fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 455fff04f90SJaegeuk Kim } 456fff04f90SJaegeuk Kim 457fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */ 458fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 459fff04f90SJaegeuk Kim { 46067298804SChao Yu struct inode_management *im = &sbi->im[mode]; 461fff04f90SJaegeuk Kim struct ino_entry *e; 46267298804SChao Yu 46367298804SChao Yu spin_lock(&im->ino_lock); 46467298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino); 46567298804SChao Yu spin_unlock(&im->ino_lock); 466fff04f90SJaegeuk Kim return e ? true : false; 467fff04f90SJaegeuk Kim } 468fff04f90SJaegeuk Kim 46974ef9241SJaegeuk Kim void release_ino_entry(struct f2fs_sb_info *sbi, bool all) 470fff04f90SJaegeuk Kim { 471fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 472fff04f90SJaegeuk Kim int i; 473fff04f90SJaegeuk Kim 47474ef9241SJaegeuk Kim for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) { 47567298804SChao Yu struct inode_management *im = &sbi->im[i]; 47667298804SChao Yu 47767298804SChao Yu spin_lock(&im->ino_lock); 47867298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) { 479fff04f90SJaegeuk Kim list_del(&e->list); 48067298804SChao Yu radix_tree_delete(&im->ino_root, e->ino); 481fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 48267298804SChao Yu im->ino_num--; 483fff04f90SJaegeuk Kim } 48467298804SChao Yu spin_unlock(&im->ino_lock); 485fff04f90SJaegeuk Kim } 486fff04f90SJaegeuk Kim } 487fff04f90SJaegeuk Kim 488cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi) 489127e670aSJaegeuk Kim { 49067298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 491127e670aSJaegeuk Kim int err = 0; 492127e670aSJaegeuk Kim 49367298804SChao Yu spin_lock(&im->ino_lock); 494cb78942bSJaegeuk Kim 495cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION 4961ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) { 497cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock); 49855523519SChao Yu f2fs_show_injection_info(FAULT_ORPHAN); 499cb78942bSJaegeuk Kim return -ENOSPC; 500cb78942bSJaegeuk Kim } 501cb78942bSJaegeuk Kim #endif 50267298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans)) 503127e670aSJaegeuk Kim err = -ENOSPC; 504cbd56e7dSJaegeuk Kim else 50567298804SChao Yu im->ino_num++; 50667298804SChao Yu spin_unlock(&im->ino_lock); 5070d47c1adSGu Zheng 508127e670aSJaegeuk Kim return err; 509127e670aSJaegeuk Kim } 510127e670aSJaegeuk Kim 511cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi) 512cbd56e7dSJaegeuk Kim { 51367298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 51467298804SChao Yu 51567298804SChao Yu spin_lock(&im->ino_lock); 51667298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0); 51767298804SChao Yu im->ino_num--; 51867298804SChao Yu spin_unlock(&im->ino_lock); 519cbd56e7dSJaegeuk Kim } 520cbd56e7dSJaegeuk Kim 52167c3758dSJaegeuk Kim void add_orphan_inode(struct inode *inode) 522127e670aSJaegeuk Kim { 52339efac41SJaegeuk Kim /* add new orphan ino entry into list */ 52467c3758dSJaegeuk Kim __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO); 52567c3758dSJaegeuk Kim update_inode_page(inode); 526127e670aSJaegeuk Kim } 527127e670aSJaegeuk Kim 528127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 529127e670aSJaegeuk Kim { 530953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 5316451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 532127e670aSJaegeuk Kim } 533127e670aSJaegeuk Kim 5348c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 535127e670aSJaegeuk Kim { 5368c14bfadSChao Yu struct inode *inode; 5375905f9afSJaegeuk Kim struct node_info ni; 538d41065e2SJaegeuk Kim int err = acquire_orphan_inode(sbi); 539d41065e2SJaegeuk Kim 540d41065e2SJaegeuk Kim if (err) { 541d41065e2SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 542d41065e2SJaegeuk Kim f2fs_msg(sbi->sb, KERN_WARNING, 543d41065e2SJaegeuk Kim "%s: orphan failed (ino=%x), run fsck to fix.", 544d41065e2SJaegeuk Kim __func__, ino); 545d41065e2SJaegeuk Kim return err; 546d41065e2SJaegeuk Kim } 547d41065e2SJaegeuk Kim 548d41065e2SJaegeuk Kim __add_ino_entry(sbi, ino, ORPHAN_INO); 5498c14bfadSChao Yu 5505905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino); 5518c14bfadSChao Yu if (IS_ERR(inode)) { 5528c14bfadSChao Yu /* 5538c14bfadSChao Yu * there should be a bug that we can't find the entry 5548c14bfadSChao Yu * to orphan inode. 5558c14bfadSChao Yu */ 5568c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); 5578c14bfadSChao Yu return PTR_ERR(inode); 5588c14bfadSChao Yu } 5598c14bfadSChao Yu 560127e670aSJaegeuk Kim clear_nlink(inode); 561127e670aSJaegeuk Kim 562127e670aSJaegeuk Kim /* truncate all the data during iput */ 563127e670aSJaegeuk Kim iput(inode); 5645905f9afSJaegeuk Kim 5655905f9afSJaegeuk Kim get_node_info(sbi, ino, &ni); 5665905f9afSJaegeuk Kim 5675905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */ 5685905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) { 5695905f9afSJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK); 5705905f9afSJaegeuk Kim f2fs_msg(sbi->sb, KERN_WARNING, 5715ce4738aSJaegeuk Kim "%s: orphan failed (ino=%x) by kernel, retry mount.", 5725905f9afSJaegeuk Kim __func__, ino); 573d41065e2SJaegeuk Kim return -EIO; 5745905f9afSJaegeuk Kim } 575d41065e2SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 5768c14bfadSChao Yu return 0; 577127e670aSJaegeuk Kim } 578127e670aSJaegeuk Kim 5798c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi) 580127e670aSJaegeuk Kim { 5813c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j; 5828c14bfadSChao Yu int err; 583127e670aSJaegeuk Kim 584aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 5858c14bfadSChao Yu return 0; 586127e670aSJaegeuk Kim 58755141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 5883c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 589127e670aSJaegeuk Kim 59026879fb1SChao Yu ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); 591662befdaSChao Yu 5923c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) { 593127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 594127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 595127e670aSJaegeuk Kim 596127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 597127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 598127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 5998c14bfadSChao Yu err = recover_orphan_inode(sbi, ino); 6008c14bfadSChao Yu if (err) { 6018c14bfadSChao Yu f2fs_put_page(page, 1); 6028c14bfadSChao Yu return err; 6038c14bfadSChao Yu } 604127e670aSJaegeuk Kim } 605127e670aSJaegeuk Kim f2fs_put_page(page, 1); 606127e670aSJaegeuk Kim } 607127e670aSJaegeuk Kim /* clear Orphan Flag */ 608aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 6098c14bfadSChao Yu return 0; 610127e670aSJaegeuk Kim } 611127e670aSJaegeuk Kim 612127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 613127e670aSJaegeuk Kim { 614502c6e0bSGu Zheng struct list_head *head; 615127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 616127e670aSJaegeuk Kim unsigned int nentries = 0; 617bd936f84SChao Yu unsigned short index = 1; 6188c402946SJaegeuk Kim unsigned short orphan_blocks; 6194531929eSGu Zheng struct page *page = NULL; 6206451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 62167298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO]; 622127e670aSJaegeuk Kim 62367298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); 6248c402946SJaegeuk Kim 625d6c67a4fSJaegeuk Kim /* 626d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the 627d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op(). 628d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below. 629d6c67a4fSJaegeuk Kim */ 63067298804SChao Yu head = &im->ino_list; 631127e670aSJaegeuk Kim 632127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 633502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 634502c6e0bSGu Zheng if (!page) { 635bd936f84SChao Yu page = grab_meta_page(sbi, start_blk++); 636502c6e0bSGu Zheng orphan_blk = 637502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 638502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 639502c6e0bSGu Zheng } 640127e670aSJaegeuk Kim 64136795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 642127e670aSJaegeuk Kim 64336795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 644127e670aSJaegeuk Kim /* 645127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 646127e670aSJaegeuk Kim * then we need to flush current orphan blocks 647127e670aSJaegeuk Kim * and bring another one in memory 648127e670aSJaegeuk Kim */ 649127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 650127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 651127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 652127e670aSJaegeuk Kim set_page_dirty(page); 653127e670aSJaegeuk Kim f2fs_put_page(page, 1); 654127e670aSJaegeuk Kim index++; 655127e670aSJaegeuk Kim nentries = 0; 656127e670aSJaegeuk Kim page = NULL; 657127e670aSJaegeuk Kim } 658127e670aSJaegeuk Kim } 659127e670aSJaegeuk Kim 660502c6e0bSGu Zheng if (page) { 661127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 662127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 663127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 664127e670aSJaegeuk Kim set_page_dirty(page); 665127e670aSJaegeuk Kim f2fs_put_page(page, 1); 666502c6e0bSGu Zheng } 667127e670aSJaegeuk Kim } 668127e670aSJaegeuk Kim 669fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, 670fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page, 671fc0065adSTiezhu Yang unsigned long long *version) 672fc0065adSTiezhu Yang { 673fc0065adSTiezhu Yang unsigned long blk_size = sbi->blocksize; 674fc0065adSTiezhu Yang size_t crc_offset = 0; 675fc0065adSTiezhu Yang __u32 crc = 0; 676fc0065adSTiezhu Yang 677fc0065adSTiezhu Yang *cp_page = get_meta_page(sbi, cp_addr); 678fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page); 679fc0065adSTiezhu Yang 680fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset); 681c6f89dfdSKinglong Mee if (crc_offset > (blk_size - sizeof(__le32))) { 682fc0065adSTiezhu Yang f2fs_msg(sbi->sb, KERN_WARNING, 683fc0065adSTiezhu Yang "invalid crc_offset: %zu", crc_offset); 684fc0065adSTiezhu Yang return -EINVAL; 685fc0065adSTiezhu Yang } 686fc0065adSTiezhu Yang 687ced2c7eaSKinglong Mee crc = cur_cp_crc(*cp_block); 688fc0065adSTiezhu Yang if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) { 689fc0065adSTiezhu Yang f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value"); 690fc0065adSTiezhu Yang return -EINVAL; 691fc0065adSTiezhu Yang } 692fc0065adSTiezhu Yang 693fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block); 694fc0065adSTiezhu Yang return 0; 695fc0065adSTiezhu Yang } 696fc0065adSTiezhu Yang 697127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 698127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 699127e670aSJaegeuk Kim { 700fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL; 701fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL; 702127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 703fc0065adSTiezhu Yang int err; 704127e670aSJaegeuk Kim 705fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 706fc0065adSTiezhu Yang &cp_page_1, version); 707fc0065adSTiezhu Yang if (err) 708127e670aSJaegeuk Kim goto invalid_cp1; 709fc0065adSTiezhu Yang pre_version = *version; 710127e670aSJaegeuk Kim 71125ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 712fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block, 713fc0065adSTiezhu Yang &cp_page_2, version); 714fc0065adSTiezhu Yang if (err) 715127e670aSJaegeuk Kim goto invalid_cp2; 716fc0065adSTiezhu Yang cur_version = *version; 717127e670aSJaegeuk Kim 718127e670aSJaegeuk Kim if (cur_version == pre_version) { 719127e670aSJaegeuk Kim *version = cur_version; 720127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 721127e670aSJaegeuk Kim return cp_page_1; 722127e670aSJaegeuk Kim } 723127e670aSJaegeuk Kim invalid_cp2: 724127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 725127e670aSJaegeuk Kim invalid_cp1: 726127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 727127e670aSJaegeuk Kim return NULL; 728127e670aSJaegeuk Kim } 729127e670aSJaegeuk Kim 730127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 731127e670aSJaegeuk Kim { 732127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 733127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 734127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 735127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 736127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 737127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 73855141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi); 7391dbe4152SChangman Lee block_t cp_blk_no; 7401dbe4152SChangman Lee int i; 741127e670aSJaegeuk Kim 7421dbe4152SChangman Lee sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL); 743127e670aSJaegeuk Kim if (!sbi->ckpt) 744127e670aSJaegeuk Kim return -ENOMEM; 745127e670aSJaegeuk Kim /* 746127e670aSJaegeuk Kim * Finding out valid cp block involves read both 747127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 748127e670aSJaegeuk Kim */ 749127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 750127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 751127e670aSJaegeuk Kim 752127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 753f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 754f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 755127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 756127e670aSJaegeuk Kim 757127e670aSJaegeuk Kim if (cp1 && cp2) { 758127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 759127e670aSJaegeuk Kim cur_page = cp2; 760127e670aSJaegeuk Kim else 761127e670aSJaegeuk Kim cur_page = cp1; 762127e670aSJaegeuk Kim } else if (cp1) { 763127e670aSJaegeuk Kim cur_page = cp1; 764127e670aSJaegeuk Kim } else if (cp2) { 765127e670aSJaegeuk Kim cur_page = cp2; 766127e670aSJaegeuk Kim } else { 767127e670aSJaegeuk Kim goto fail_no_cp; 768127e670aSJaegeuk Kim } 769127e670aSJaegeuk Kim 770127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 771127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 772127e670aSJaegeuk Kim 773984ec63cSShawn Lin /* Sanity checking of checkpoint */ 774984ec63cSShawn Lin if (sanity_check_ckpt(sbi)) 775a2125ff7SJaegeuk Kim goto free_fail_no_cp; 776984ec63cSShawn Lin 7778508e44aSJaegeuk Kim if (cur_page == cp1) 7788508e44aSJaegeuk Kim sbi->cur_cp_pack = 1; 7798508e44aSJaegeuk Kim else 7808508e44aSJaegeuk Kim sbi->cur_cp_pack = 2; 7811dbe4152SChangman Lee 7821dbe4152SChangman Lee if (cp_blks <= 1) 7831dbe4152SChangman Lee goto done; 7841dbe4152SChangman Lee 7851dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 7861dbe4152SChangman Lee if (cur_page == cp2) 7871dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 7881dbe4152SChangman Lee 7891dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 7901dbe4152SChangman Lee void *sit_bitmap_ptr; 7911dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 7921dbe4152SChangman Lee 7931dbe4152SChangman Lee cur_page = get_meta_page(sbi, cp_blk_no + i); 7941dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 795127e670aSJaegeuk Kim memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 796127e670aSJaegeuk Kim f2fs_put_page(cur_page, 1); 797127e670aSJaegeuk Kim } 798127e670aSJaegeuk Kim done: 799127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 800127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 801127e670aSJaegeuk Kim return 0; 802127e670aSJaegeuk Kim 803a2125ff7SJaegeuk Kim free_fail_no_cp: 804a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1); 805a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1); 806127e670aSJaegeuk Kim fail_no_cp: 807127e670aSJaegeuk Kim kfree(sbi->ckpt); 808127e670aSJaegeuk Kim return -EINVAL; 809127e670aSJaegeuk Kim } 810127e670aSJaegeuk Kim 811c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type) 812127e670aSJaegeuk Kim { 8134081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 814c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 8152d7b822aSChao Yu 81691942321SJaegeuk Kim if (is_inode_flag_set(inode, flag)) 8172710fd7eSChao Yu return; 8182d7b822aSChao Yu 81991942321SJaegeuk Kim set_inode_flag(inode, flag); 82099f4b917SChao Yu if (!f2fs_is_volatile_file(inode)) 82199f4b917SChao Yu list_add_tail(&F2FS_I(inode)->dirty_list, 82299f4b917SChao Yu &sbi->inode_list[type]); 82333fbd510SChao Yu stat_inc_dirty_inode(sbi, type); 8245deb8267SJaegeuk Kim } 8255deb8267SJaegeuk Kim 826c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type) 8276ad7609aSChao Yu { 828c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; 8296ad7609aSChao Yu 83091942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) 8316ad7609aSChao Yu return; 8326ad7609aSChao Yu 83391942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list); 83491942321SJaegeuk Kim clear_inode_flag(inode, flag); 83533fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type); 8366ad7609aSChao Yu } 8376ad7609aSChao Yu 838a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page) 8395deb8267SJaegeuk Kim { 8404081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 841c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 8425deb8267SJaegeuk Kim 8435ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 8445ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 845127e670aSJaegeuk Kim return; 8467bd59381SGu Zheng 847c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 8481c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) 849c227f912SChao Yu __add_dirty_inode(inode, type); 850b951a4ecSYunlei He inode_inc_dirty_pages(inode); 8511c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]); 8521c4bf763SJaegeuk Kim 853a7ffdbe2SJaegeuk Kim SetPagePrivate(page); 8549e4ded3fSJaegeuk Kim f2fs_trace_pid(page); 8555deb8267SJaegeuk Kim } 856127e670aSJaegeuk Kim 857c227f912SChao Yu void remove_dirty_inode(struct inode *inode) 858127e670aSJaegeuk Kim { 8594081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 860c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; 861127e670aSJaegeuk Kim 862c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 863c227f912SChao Yu !S_ISLNK(inode->i_mode)) 864127e670aSJaegeuk Kim return; 865127e670aSJaegeuk Kim 86610aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) 86710aa97c3SJaegeuk Kim return; 86810aa97c3SJaegeuk Kim 869c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 870c227f912SChao Yu __remove_dirty_inode(inode, type); 871c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 872afc3eda2SJaegeuk Kim } 87374d0b917SJaegeuk Kim 8746d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) 875127e670aSJaegeuk Kim { 876ce3b7d80SGu Zheng struct list_head *head; 877127e670aSJaegeuk Kim struct inode *inode; 8782710fd7eSChao Yu struct f2fs_inode_info *fi; 8794cf18537SChao Yu bool is_dir = (type == DIR_INODE); 8804cf18537SChao Yu 8814cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, 8824cf18537SChao Yu get_pages(sbi, is_dir ? 8834cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 884127e670aSJaegeuk Kim retry: 885af41d3eeSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 8866d5a1495SChao Yu return -EIO; 887af41d3eeSJaegeuk Kim 888c227f912SChao Yu spin_lock(&sbi->inode_lock[type]); 889ce3b7d80SGu Zheng 890c227f912SChao Yu head = &sbi->inode_list[type]; 891127e670aSJaegeuk Kim if (list_empty(head)) { 892c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 8934cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, 8944cf18537SChao Yu get_pages(sbi, is_dir ? 8954cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); 8966d5a1495SChao Yu return 0; 897127e670aSJaegeuk Kim } 898939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); 8992710fd7eSChao Yu inode = igrab(&fi->vfs_inode); 900c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]); 901127e670aSJaegeuk Kim if (inode) { 90287d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 903127e670aSJaegeuk Kim iput(inode); 904127e670aSJaegeuk Kim } else { 905127e670aSJaegeuk Kim /* 906127e670aSJaegeuk Kim * We should submit bio, since it exists several 907127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 908127e670aSJaegeuk Kim */ 909458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 9107ecebe5eSSebastian Andrzej Siewior cond_resched(); 911127e670aSJaegeuk Kim } 912127e670aSJaegeuk Kim goto retry; 913127e670aSJaegeuk Kim } 914127e670aSJaegeuk Kim 9150f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) 9160f18b462SJaegeuk Kim { 9170f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META]; 9180f18b462SJaegeuk Kim struct inode *inode; 9190f18b462SJaegeuk Kim struct f2fs_inode_info *fi; 9200f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); 9210f18b462SJaegeuk Kim 9220f18b462SJaegeuk Kim while (total--) { 9230f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 9240f18b462SJaegeuk Kim return -EIO; 9250f18b462SJaegeuk Kim 9260f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 9270f18b462SJaegeuk Kim if (list_empty(head)) { 9280f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 9290f18b462SJaegeuk Kim return 0; 9300f18b462SJaegeuk Kim } 931939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, 9320f18b462SJaegeuk Kim gdirty_list); 9330f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode); 9340f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 9350f18b462SJaegeuk Kim if (inode) { 93618340edcSJaegeuk Kim sync_inode_metadata(inode, 0); 93718340edcSJaegeuk Kim 93818340edcSJaegeuk Kim /* it's on eviction */ 93918340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE)) 9400f18b462SJaegeuk Kim update_inode_page(inode); 9410f18b462SJaegeuk Kim iput(inode); 9420f18b462SJaegeuk Kim } 9430f18b462SJaegeuk Kim }; 9440f18b462SJaegeuk Kim return 0; 9450f18b462SJaegeuk Kim } 9460f18b462SJaegeuk Kim 94759c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi) 94859c9081bSYunlei He { 94959c9081bSYunlei He struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 95059c9081bSYunlei He struct f2fs_nm_info *nm_i = NM_I(sbi); 95159c9081bSYunlei He nid_t last_nid = nm_i->next_scan_nid; 95259c9081bSYunlei He 95359c9081bSYunlei He next_free_nid(sbi, &last_nid); 95459c9081bSYunlei He ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 95559c9081bSYunlei He ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 95659c9081bSYunlei He ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 95759c9081bSYunlei He ckpt->next_free_nid = cpu_to_le32(last_nid); 95859c9081bSYunlei He } 95959c9081bSYunlei He 9600a8165d7SJaegeuk Kim /* 961127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 962127e670aSJaegeuk Kim */ 963cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 964127e670aSJaegeuk Kim { 965127e670aSJaegeuk Kim struct writeback_control wbc = { 966127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 967127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 968127e670aSJaegeuk Kim .for_reclaim = 0, 969127e670aSJaegeuk Kim }; 970c718379bSJaegeuk Kim struct blk_plug plug; 971cf779cabSJaegeuk Kim int err = 0; 972c718379bSJaegeuk Kim 973c718379bSJaegeuk Kim blk_start_plug(&plug); 974c718379bSJaegeuk Kim 97539936837SJaegeuk Kim retry_flush_dents: 976e479556bSGu Zheng f2fs_lock_all(sbi); 977127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 978127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 979e479556bSGu Zheng f2fs_unlock_all(sbi); 9806d5a1495SChao Yu err = sync_dirty_inodes(sbi, DIR_INODE); 9816d5a1495SChao Yu if (err) 982cf779cabSJaegeuk Kim goto out; 98330973883SJaegeuk Kim cond_resched(); 98439936837SJaegeuk Kim goto retry_flush_dents; 985127e670aSJaegeuk Kim } 986127e670aSJaegeuk Kim 98759c9081bSYunlei He /* 98859c9081bSYunlei He * POR: we should ensure that there are no dirty node pages 98959c9081bSYunlei He * until finishing nat/sit flush. inode->i_blocks can be updated. 99059c9081bSYunlei He */ 99159c9081bSYunlei He down_write(&sbi->node_change); 99259c9081bSYunlei He 9930f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 99459c9081bSYunlei He up_write(&sbi->node_change); 9950f18b462SJaegeuk Kim f2fs_unlock_all(sbi); 9960f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi); 9970f18b462SJaegeuk Kim if (err) 9980f18b462SJaegeuk Kim goto out; 99930973883SJaegeuk Kim cond_resched(); 10000f18b462SJaegeuk Kim goto retry_flush_dents; 10010f18b462SJaegeuk Kim } 10020f18b462SJaegeuk Kim 100339936837SJaegeuk Kim retry_flush_nodes: 1004b3582c68SChao Yu down_write(&sbi->node_write); 1005127e670aSJaegeuk Kim 1006127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1007b3582c68SChao Yu up_write(&sbi->node_write); 100852681375SJaegeuk Kim err = sync_node_pages(sbi, &wbc); 10096d5a1495SChao Yu if (err) { 101059c9081bSYunlei He up_write(&sbi->node_change); 1011cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 1012cf779cabSJaegeuk Kim goto out; 1013cf779cabSJaegeuk Kim } 101430973883SJaegeuk Kim cond_resched(); 101539936837SJaegeuk Kim goto retry_flush_nodes; 1016127e670aSJaegeuk Kim } 101759c9081bSYunlei He 101859c9081bSYunlei He /* 101959c9081bSYunlei He * sbi->node_change is used only for AIO write_begin path which produces 102059c9081bSYunlei He * dirty node blocks and some checkpoint values by block allocation. 102159c9081bSYunlei He */ 102259c9081bSYunlei He __prepare_cp_block(sbi); 102359c9081bSYunlei He up_write(&sbi->node_change); 1024cf779cabSJaegeuk Kim out: 1025c718379bSJaegeuk Kim blk_finish_plug(&plug); 1026cf779cabSJaegeuk Kim return err; 1027127e670aSJaegeuk Kim } 1028127e670aSJaegeuk Kim 1029127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 1030127e670aSJaegeuk Kim { 1031b3582c68SChao Yu up_write(&sbi->node_write); 1032e479556bSGu Zheng f2fs_unlock_all(sbi); 1033127e670aSJaegeuk Kim } 1034127e670aSJaegeuk Kim 1035fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 1036fb51b5efSChangman Lee { 1037fb51b5efSChangman Lee DEFINE_WAIT(wait); 1038fb51b5efSChangman Lee 1039fb51b5efSChangman Lee for (;;) { 1040fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 1041fb51b5efSChangman Lee 104236951b38SChao Yu if (!get_pages(sbi, F2FS_WB_CP_DATA)) 1043fb51b5efSChangman Lee break; 1044fb51b5efSChangman Lee 10450ff21646SYunlei He io_schedule_timeout(5*HZ); 1046fb51b5efSChangman Lee } 1047fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 1048fb51b5efSChangman Lee } 1049fb51b5efSChangman Lee 1050e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1051e4c5d848SJaegeuk Kim { 1052e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1053e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1054e4c5d848SJaegeuk Kim 1055e4c5d848SJaegeuk Kim spin_lock(&sbi->cp_lock); 1056e4c5d848SJaegeuk Kim 1057*c473f1a9SChao Yu if ((cpc->reason & CP_UMOUNT) && 105810047f53SKinglong Mee le32_to_cpu(ckpt->cp_pack_total_block_count) > 105922ad0b6aSJaegeuk Kim sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks) 106022ad0b6aSJaegeuk Kim disable_nat_bits(sbi, false); 106122ad0b6aSJaegeuk Kim 1062*c473f1a9SChao Yu if (cpc->reason & CP_UMOUNT) 1063e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1064e4c5d848SJaegeuk Kim else 1065e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 1066e4c5d848SJaegeuk Kim 1067*c473f1a9SChao Yu if (cpc->reason & CP_FASTBOOT) 1068e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1069e4c5d848SJaegeuk Kim else 1070e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); 1071e4c5d848SJaegeuk Kim 1072e4c5d848SJaegeuk Kim if (orphan_num) 1073e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1074e4c5d848SJaegeuk Kim else 1075e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 1076e4c5d848SJaegeuk Kim 1077e4c5d848SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1078e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG); 1079e4c5d848SJaegeuk Kim 1080e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */ 1081e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1082e4c5d848SJaegeuk Kim 1083e4c5d848SJaegeuk Kim spin_unlock(&sbi->cp_lock); 1084e4c5d848SJaegeuk Kim } 1085e4c5d848SJaegeuk Kim 1086c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1087127e670aSJaegeuk Kim { 1088127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 108977041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi); 109067298804SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; 1091127e670aSJaegeuk Kim block_t start_blk; 1092127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 10937e586fa0SJaegeuk Kim __u32 crc32 = 0; 1094127e670aSJaegeuk Kim int i; 109555141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi); 10968f1dbbbbSShuoran Liu struct super_block *sb = sbi->sb; 10978f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 10988f1dbbbbSShuoran Liu u64 kbytes_written; 1099127e670aSJaegeuk Kim 1100127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 1101cf779cabSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) { 1102127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 1103cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1104c34f42e2SChao Yu return -EIO; 1105cf779cabSJaegeuk Kim } 1106127e670aSJaegeuk Kim 1107127e670aSJaegeuk Kim /* 1108127e670aSJaegeuk Kim * modify checkpoint 1109127e670aSJaegeuk Kim * version number is already updated 1110127e670aSJaegeuk Kim */ 1111127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 1112127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 1113b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 1114127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 1115127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 1116127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 1117127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 1118127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 1119127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 1120127e670aSJaegeuk Kim } 1121b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 1122127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 1123127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 1124127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 1125127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 1126127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 1127127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 1128127e670aSJaegeuk Kim } 1129127e670aSJaegeuk Kim 1130127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 11313fa06d7bSChao Yu data_sum_blocks = npages_for_summary_flush(sbi, false); 1132aaec2b1dSChao Yu spin_lock(&sbi->cp_lock); 1133b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 1134aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1135127e670aSJaegeuk Kim else 1136aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 1137aaec2b1dSChao Yu spin_unlock(&sbi->cp_lock); 1138127e670aSJaegeuk Kim 113967298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); 11401dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 11411dbe4152SChangman Lee orphan_blocks); 1142127e670aSJaegeuk Kim 1143119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) 1144b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+ 11451dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 11461dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 1147119ee914SJaegeuk Kim else 1148b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 11491dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 11501dbe4152SChangman Lee orphan_blocks); 1151119ee914SJaegeuk Kim 1152e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */ 1153e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc); 1154a468f0efSJaegeuk Kim 1155127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 1156127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 1157127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 1158127e670aSJaegeuk Kim 115943b6573bSKeith Mok crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset)); 11607e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 11617e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 1162127e670aSJaegeuk Kim = cpu_to_le32(crc32); 1163127e670aSJaegeuk Kim 11648508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi); 1165127e670aSJaegeuk Kim 116622ad0b6aSJaegeuk Kim /* write nat bits */ 116722ad0b6aSJaegeuk Kim if (enabled_nat_bits(sbi, cpc)) { 116822ad0b6aSJaegeuk Kim __u64 cp_ver = cur_cp_version(ckpt); 116922ad0b6aSJaegeuk Kim block_t blk; 117022ad0b6aSJaegeuk Kim 117122ad0b6aSJaegeuk Kim cp_ver |= ((__u64)crc32 << 32); 117222ad0b6aSJaegeuk Kim *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); 117322ad0b6aSJaegeuk Kim 117422ad0b6aSJaegeuk Kim blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks; 117522ad0b6aSJaegeuk Kim for (i = 0; i < nm_i->nat_bits_blocks; i++) 117622ad0b6aSJaegeuk Kim update_meta_page(sbi, nm_i->nat_bits + 117722ad0b6aSJaegeuk Kim (i << F2FS_BLKSIZE_BITS), blk + i); 117822ad0b6aSJaegeuk Kim 117922ad0b6aSJaegeuk Kim /* Flush all the NAT BITS pages */ 118022ad0b6aSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) { 118122ad0b6aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 118222ad0b6aSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 118322ad0b6aSJaegeuk Kim return -EIO; 118422ad0b6aSJaegeuk Kim } 118522ad0b6aSJaegeuk Kim } 118622ad0b6aSJaegeuk Kim 1187a7230d16SJaegeuk Kim /* need to wait for end_io results */ 1188a7230d16SJaegeuk Kim wait_on_all_pages_writeback(sbi); 1189a7230d16SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1190c34f42e2SChao Yu return -EIO; 1191a7230d16SJaegeuk Kim 1192127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 1193381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk++); 1194127e670aSJaegeuk Kim 1195381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++) 1196381722d2SChao Yu update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, 1197381722d2SChao Yu start_blk++); 11981dbe4152SChangman Lee 119967298804SChao Yu if (orphan_num) { 1200127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 1201127e670aSJaegeuk Kim start_blk += orphan_blocks; 1202127e670aSJaegeuk Kim } 1203127e670aSJaegeuk Kim 1204127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 1205127e670aSJaegeuk Kim start_blk += data_sum_blocks; 12068f1dbbbbSShuoran Liu 12078f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */ 12088f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written; 12098f1dbbbbSShuoran Liu if (sb->s_bdev->bd_part) 12108f1dbbbbSShuoran Liu kbytes_written += BD_PART_WRITTEN(sbi); 12118f1dbbbbSShuoran Liu 1212b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 12138f1dbbbbSShuoran Liu 1214119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) { 1215127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 1216127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 1217127e670aSJaegeuk Kim } 1218127e670aSJaegeuk Kim 1219127e670aSJaegeuk Kim /* writeout checkpoint block */ 1220381722d2SChao Yu update_meta_page(sbi, ckpt, start_blk); 1221127e670aSJaegeuk Kim 1222127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 1223fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 1224127e670aSJaegeuk Kim 1225cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1226c34f42e2SChao Yu return -EIO; 1227cf779cabSJaegeuk Kim 122880dd9c0eSChao Yu filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX); 122980dd9c0eSChao Yu filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX); 1230127e670aSJaegeuk Kim 1231127e670aSJaegeuk Kim /* update user_block_counts */ 1232127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 123341382ec4SJaegeuk Kim percpu_counter_set(&sbi->alloc_valid_block_count, 0); 1234127e670aSJaegeuk Kim 1235127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 1236127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 1237127e670aSJaegeuk Kim 12386a8f8ca5SJaegeuk Kim /* wait for previous submitted meta pages writeback */ 12396a8f8ca5SJaegeuk Kim wait_on_all_pages_writeback(sbi); 12406a8f8ca5SJaegeuk Kim 124174ef9241SJaegeuk Kim release_ino_entry(sbi, false); 1242cf779cabSJaegeuk Kim 1243cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 1244c34f42e2SChao Yu return -EIO; 1245cf779cabSJaegeuk Kim 1246caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY); 1247bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP); 12488508e44aSJaegeuk Kim __set_cp_next_pack(sbi); 1249c34f42e2SChao Yu 1250c2a080aeSChao Yu /* 1251c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is 1252c2a080aeSChao Yu * updated during writing checkpoint. 1253c2a080aeSChao Yu */ 1254c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) || 1255c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA)) 1256c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 1257c2a080aeSChao Yu 1258c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); 1259c2a080aeSChao Yu 1260c34f42e2SChao Yu return 0; 1261127e670aSJaegeuk Kim } 1262127e670aSJaegeuk Kim 12630a8165d7SJaegeuk Kim /* 1264e1c42045Sarter97 * We guarantee that this checkpoint procedure will not fail. 1265127e670aSJaegeuk Kim */ 1266c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1267127e670aSJaegeuk Kim { 1268127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1269127e670aSJaegeuk Kim unsigned long long ckpt_ver; 1270c34f42e2SChao Yu int err = 0; 1271127e670aSJaegeuk Kim 1272127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 12738501017eSJaegeuk Kim 1274caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1275*c473f1a9SChao Yu ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1276*c473f1a9SChao Yu ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) 12778501017eSJaegeuk Kim goto out; 1278c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) { 1279c34f42e2SChao Yu err = -EIO; 1280cf779cabSJaegeuk Kim goto out; 1281c34f42e2SChao Yu } 1282c34f42e2SChao Yu if (f2fs_readonly(sbi->sb)) { 1283c34f42e2SChao Yu err = -EROFS; 128411504a8eSJaegeuk Kim goto out; 1285c34f42e2SChao Yu } 12862bda542dSWanpeng Li 12872bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); 12882bda542dSWanpeng Li 1289c34f42e2SChao Yu err = block_operations(sbi); 1290c34f42e2SChao Yu if (err) 1291cf779cabSJaegeuk Kim goto out; 1292127e670aSJaegeuk Kim 129375ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); 12942af4bd6cSNamjae Jeon 1295406657ddSChao Yu f2fs_flush_merged_bios(sbi); 1296127e670aSJaegeuk Kim 129758cce381SYunlei He /* this is the case of multiple fstrims without any changes */ 1298*c473f1a9SChao Yu if (cpc->reason & CP_DISCARD) { 129925290fa5SJaegeuk Kim if (!exist_trim_candidates(sbi, cpc)) { 130025290fa5SJaegeuk Kim unblock_operations(sbi); 130125290fa5SJaegeuk Kim goto out; 130225290fa5SJaegeuk Kim } 130325290fa5SJaegeuk Kim 13040333ad4eSJaegeuk Kim if (NM_I(sbi)->dirty_nat_cnt == 0 && 13050333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 && 13060333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) { 130758cce381SYunlei He flush_sit_entries(sbi, cpc); 130858cce381SYunlei He clear_prefree_segments(sbi, cpc); 130958cce381SYunlei He unblock_operations(sbi); 131058cce381SYunlei He goto out; 131158cce381SYunlei He } 13120333ad4eSJaegeuk Kim } 131358cce381SYunlei He 1314127e670aSJaegeuk Kim /* 1315127e670aSJaegeuk Kim * update checkpoint pack index 1316127e670aSJaegeuk Kim * Increase the version number so that 1317127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 1318127e670aSJaegeuk Kim */ 1319d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 1320127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 1321127e670aSJaegeuk Kim 1322127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 132322ad0b6aSJaegeuk Kim flush_nat_entries(sbi, cpc); 13244b2fecc8SJaegeuk Kim flush_sit_entries(sbi, cpc); 1325127e670aSJaegeuk Kim 1326127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 1327c34f42e2SChao Yu err = do_checkpoint(sbi, cpc); 13284e6a8d9bSJaegeuk Kim if (err) 13292dd15654SChao Yu release_discard_addrs(sbi); 13304e6a8d9bSJaegeuk Kim else 13312dd15654SChao Yu clear_prefree_segments(sbi, cpc); 1332275b66b0SChao Yu 1333127e670aSJaegeuk Kim unblock_operations(sbi); 1334942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 133510027551SJaegeuk Kim 1336*c473f1a9SChao Yu if (cpc->reason & CP_RECOVERY) 133710027551SJaegeuk Kim f2fs_msg(sbi->sb, KERN_NOTICE, 133810027551SJaegeuk Kim "checkpoint: version = %llx", ckpt_ver); 133960b99b48SJaegeuk Kim 134060b99b48SJaegeuk Kim /* do checkpoint periodically */ 13416beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME); 134255d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 13438501017eSJaegeuk Kim out: 13448501017eSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 1345c34f42e2SChao Yu return err; 1346127e670aSJaegeuk Kim } 1347127e670aSJaegeuk Kim 13486451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi) 1349127e670aSJaegeuk Kim { 13506451e041SJaegeuk Kim int i; 13516451e041SJaegeuk Kim 13526451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 135367298804SChao Yu struct inode_management *im = &sbi->im[i]; 135467298804SChao Yu 135567298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); 135667298804SChao Yu spin_lock_init(&im->ino_lock); 135767298804SChao Yu INIT_LIST_HEAD(&im->ino_list); 135867298804SChao Yu im->ino_num = 0; 13596451e041SJaegeuk Kim } 13606451e041SJaegeuk Kim 1361b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 136214b42817SWanpeng Li NR_CURSEG_TYPE - __cp_payload(sbi)) * 136314b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK; 1364127e670aSJaegeuk Kim } 1365127e670aSJaegeuk Kim 13666e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 1367127e670aSJaegeuk Kim { 13686451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 13696451e041SJaegeuk Kim sizeof(struct ino_entry)); 13706451e041SJaegeuk Kim if (!ino_entry_slab) 1371127e670aSJaegeuk Kim return -ENOMEM; 137206292073SChao Yu inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", 137306292073SChao Yu sizeof(struct inode_entry)); 13746bacf52fSJaegeuk Kim if (!inode_entry_slab) { 13756451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1376127e670aSJaegeuk Kim return -ENOMEM; 1377127e670aSJaegeuk Kim } 1378127e670aSJaegeuk Kim return 0; 1379127e670aSJaegeuk Kim } 1380127e670aSJaegeuk Kim 1381127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 1382127e670aSJaegeuk Kim { 13836451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1384127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 1385127e670aSJaegeuk Kim } 1386