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" 232af4bd6cSNamjae Jeon #include <trace/events/f2fs.h> 24127e670aSJaegeuk Kim 256451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab; 26127e670aSJaegeuk Kim static struct kmem_cache *inode_entry_slab; 27127e670aSJaegeuk Kim 280a8165d7SJaegeuk Kim /* 29127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 30127e670aSJaegeuk Kim */ 31127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 32127e670aSJaegeuk Kim { 339df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 34127e670aSJaegeuk Kim struct page *page = NULL; 35127e670aSJaegeuk Kim repeat: 36bde44686SJaegeuk Kim page = grab_cache_page(mapping, index); 37127e670aSJaegeuk Kim if (!page) { 38127e670aSJaegeuk Kim cond_resched(); 39127e670aSJaegeuk Kim goto repeat; 40127e670aSJaegeuk Kim } 41bde44686SJaegeuk Kim f2fs_wait_on_page_writeback(page, META); 42127e670aSJaegeuk Kim SetPageUptodate(page); 43127e670aSJaegeuk Kim return page; 44127e670aSJaegeuk Kim } 45127e670aSJaegeuk Kim 460a8165d7SJaegeuk Kim /* 47127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 48127e670aSJaegeuk Kim */ 49127e670aSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 50127e670aSJaegeuk Kim { 519df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 52127e670aSJaegeuk Kim struct page *page; 53127e670aSJaegeuk Kim repeat: 54127e670aSJaegeuk Kim page = grab_cache_page(mapping, index); 55127e670aSJaegeuk Kim if (!page) { 56127e670aSJaegeuk Kim cond_resched(); 57127e670aSJaegeuk Kim goto repeat; 58127e670aSJaegeuk Kim } 59393ff91fSJaegeuk Kim if (PageUptodate(page)) 60393ff91fSJaegeuk Kim goto out; 61127e670aSJaegeuk Kim 6293dfe2acSJaegeuk Kim if (f2fs_submit_page_bio(sbi, page, index, 6393dfe2acSJaegeuk Kim READ_SYNC | REQ_META | REQ_PRIO)) 64393ff91fSJaegeuk Kim goto repeat; 65393ff91fSJaegeuk Kim 66393ff91fSJaegeuk Kim lock_page(page); 676bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) { 68afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 69afcb7ca0SJaegeuk Kim goto repeat; 70afcb7ca0SJaegeuk Kim } 71393ff91fSJaegeuk Kim out: 72127e670aSJaegeuk Kim return page; 73127e670aSJaegeuk Kim } 74127e670aSJaegeuk Kim 75b49ad51eSFabian Frederick static inline int get_max_meta_blks(struct f2fs_sb_info *sbi, int type) 76662befdaSChao Yu { 77662befdaSChao Yu switch (type) { 78662befdaSChao Yu case META_NAT: 79662befdaSChao Yu return NM_I(sbi)->max_nid / NAT_ENTRY_PER_BLOCK; 80662befdaSChao Yu case META_SIT: 81662befdaSChao Yu return SIT_BLK_CNT(sbi); 8281c1a0f1SChao Yu case META_SSA: 83662befdaSChao Yu case META_CP: 84662befdaSChao Yu return 0; 85662befdaSChao Yu default: 86662befdaSChao Yu BUG(); 87662befdaSChao Yu } 88662befdaSChao Yu } 89662befdaSChao Yu 90662befdaSChao Yu /* 9181c1a0f1SChao Yu * Readahead CP/NAT/SIT/SSA pages 92662befdaSChao Yu */ 93662befdaSChao Yu int ra_meta_pages(struct f2fs_sb_info *sbi, int start, int nrpages, int type) 94662befdaSChao Yu { 95662befdaSChao Yu block_t prev_blk_addr = 0; 96662befdaSChao Yu struct page *page; 97662befdaSChao Yu int blkno = start; 98662befdaSChao Yu int max_blks = get_max_meta_blks(sbi, type); 99662befdaSChao Yu 100662befdaSChao Yu struct f2fs_io_info fio = { 101662befdaSChao Yu .type = META, 102662befdaSChao Yu .rw = READ_SYNC | REQ_META | REQ_PRIO 103662befdaSChao Yu }; 104662befdaSChao Yu 105662befdaSChao Yu for (; nrpages-- > 0; blkno++) { 106662befdaSChao Yu block_t blk_addr; 107662befdaSChao Yu 108662befdaSChao Yu switch (type) { 109662befdaSChao Yu case META_NAT: 110662befdaSChao Yu /* get nat block addr */ 111662befdaSChao Yu if (unlikely(blkno >= max_blks)) 112662befdaSChao Yu blkno = 0; 113662befdaSChao Yu blk_addr = current_nat_addr(sbi, 114662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK); 115662befdaSChao Yu break; 116662befdaSChao Yu case META_SIT: 117662befdaSChao Yu /* get sit block addr */ 118662befdaSChao Yu if (unlikely(blkno >= max_blks)) 119662befdaSChao Yu goto out; 120662befdaSChao Yu blk_addr = current_sit_addr(sbi, 121662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK); 122662befdaSChao Yu if (blkno != start && prev_blk_addr + 1 != blk_addr) 123662befdaSChao Yu goto out; 124662befdaSChao Yu prev_blk_addr = blk_addr; 125662befdaSChao Yu break; 12681c1a0f1SChao Yu case META_SSA: 127662befdaSChao Yu case META_CP: 12881c1a0f1SChao Yu /* get ssa/cp block addr */ 129662befdaSChao Yu blk_addr = blkno; 130662befdaSChao Yu break; 131662befdaSChao Yu default: 132662befdaSChao Yu BUG(); 133662befdaSChao Yu } 134662befdaSChao Yu 135662befdaSChao Yu page = grab_cache_page(META_MAPPING(sbi), blk_addr); 136662befdaSChao Yu if (!page) 137662befdaSChao Yu continue; 138662befdaSChao Yu if (PageUptodate(page)) { 139662befdaSChao Yu f2fs_put_page(page, 1); 140662befdaSChao Yu continue; 141662befdaSChao Yu } 142662befdaSChao Yu 143662befdaSChao Yu f2fs_submit_page_mbio(sbi, page, blk_addr, &fio); 144662befdaSChao Yu f2fs_put_page(page, 0); 145662befdaSChao Yu } 146662befdaSChao Yu out: 147662befdaSChao Yu f2fs_submit_merged_bio(sbi, META, READ); 148662befdaSChao Yu return blkno - start; 149662befdaSChao Yu } 150662befdaSChao Yu 151127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page, 152127e670aSJaegeuk Kim struct writeback_control *wbc) 153127e670aSJaegeuk Kim { 154127e670aSJaegeuk Kim struct inode *inode = page->mapping->host; 155127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 156577e3495SJaegeuk Kim 157ecda0de3SChao Yu trace_f2fs_writepage(page, META); 158ecda0de3SChao Yu 159203681f6SJaegeuk Kim if (unlikely(sbi->por_doing)) 160cfb271d4SChao Yu goto redirty_out; 161cfb271d4SChao Yu if (wbc->for_reclaim) 162cfb271d4SChao Yu goto redirty_out; 163127e670aSJaegeuk Kim 164203681f6SJaegeuk Kim /* Should not write any meta pages, if any IO error was occurred */ 165203681f6SJaegeuk Kim if (unlikely(is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG))) 166203681f6SJaegeuk Kim goto no_write; 167127e670aSJaegeuk Kim 1683cb5ad15SJaegeuk Kim f2fs_wait_on_page_writeback(page, META); 169577e3495SJaegeuk Kim write_meta_page(sbi, page); 170203681f6SJaegeuk Kim no_write: 171127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 172127e670aSJaegeuk Kim unlock_page(page); 173577e3495SJaegeuk Kim return 0; 174cfb271d4SChao Yu 175cfb271d4SChao Yu redirty_out: 17676f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 177cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 178127e670aSJaegeuk Kim } 179127e670aSJaegeuk Kim 180127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 181127e670aSJaegeuk Kim struct writeback_control *wbc) 182127e670aSJaegeuk Kim { 183127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 18450c8cdb3SJaegeuk Kim long diff, written; 185127e670aSJaegeuk Kim 186e5748434SChao Yu trace_f2fs_writepages(mapping->host, wbc, META); 187e5748434SChao Yu 1885459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 18950c8cdb3SJaegeuk Kim if (wbc->for_kupdate || 19050c8cdb3SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META)) 191d3baf95dSJaegeuk Kim goto skip_write; 192127e670aSJaegeuk Kim 193127e670aSJaegeuk Kim /* if mounting is failed, skip writing node pages */ 194127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 19550c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 19650c8cdb3SJaegeuk Kim written = sync_meta_pages(sbi, META, wbc->nr_to_write); 197127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 19850c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 199127e670aSJaegeuk Kim return 0; 200d3baf95dSJaegeuk Kim 201d3baf95dSJaegeuk Kim skip_write: 202d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 203d3baf95dSJaegeuk Kim return 0; 204127e670aSJaegeuk Kim } 205127e670aSJaegeuk Kim 206127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 207127e670aSJaegeuk Kim long nr_to_write) 208127e670aSJaegeuk Kim { 2099df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 210127e670aSJaegeuk Kim pgoff_t index = 0, end = LONG_MAX; 211127e670aSJaegeuk Kim struct pagevec pvec; 212127e670aSJaegeuk Kim long nwritten = 0; 213127e670aSJaegeuk Kim struct writeback_control wbc = { 214127e670aSJaegeuk Kim .for_reclaim = 0, 215127e670aSJaegeuk Kim }; 216127e670aSJaegeuk Kim 217127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 218127e670aSJaegeuk Kim 219127e670aSJaegeuk Kim while (index <= end) { 220127e670aSJaegeuk Kim int i, nr_pages; 221127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 222127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 223127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 224cfb271d4SChao Yu if (unlikely(nr_pages == 0)) 225127e670aSJaegeuk Kim break; 226127e670aSJaegeuk Kim 227127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 228127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 229203681f6SJaegeuk Kim 230127e670aSJaegeuk Kim lock_page(page); 231203681f6SJaegeuk Kim 232203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 233203681f6SJaegeuk Kim continue_unlock: 234203681f6SJaegeuk Kim unlock_page(page); 235203681f6SJaegeuk Kim continue; 236203681f6SJaegeuk Kim } 237203681f6SJaegeuk Kim if (!PageDirty(page)) { 238203681f6SJaegeuk Kim /* someone wrote it for us */ 239203681f6SJaegeuk Kim goto continue_unlock; 240203681f6SJaegeuk Kim } 241203681f6SJaegeuk Kim 242203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 243203681f6SJaegeuk Kim goto continue_unlock; 244203681f6SJaegeuk Kim 245577e3495SJaegeuk Kim if (f2fs_write_meta_page(page, &wbc)) { 246577e3495SJaegeuk Kim unlock_page(page); 247577e3495SJaegeuk Kim break; 248577e3495SJaegeuk Kim } 249cfb271d4SChao Yu nwritten++; 250cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 251127e670aSJaegeuk Kim break; 252127e670aSJaegeuk Kim } 253127e670aSJaegeuk Kim pagevec_release(&pvec); 254127e670aSJaegeuk Kim cond_resched(); 255127e670aSJaegeuk Kim } 256127e670aSJaegeuk Kim 257127e670aSJaegeuk Kim if (nwritten) 258458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, type, WRITE); 259127e670aSJaegeuk Kim 260127e670aSJaegeuk Kim return nwritten; 261127e670aSJaegeuk Kim } 262127e670aSJaegeuk Kim 263127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 264127e670aSJaegeuk Kim { 265127e670aSJaegeuk Kim struct address_space *mapping = page->mapping; 266127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 267127e670aSJaegeuk Kim 26826c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 26926c6b887SJaegeuk Kim 270127e670aSJaegeuk Kim SetPageUptodate(page); 271127e670aSJaegeuk Kim if (!PageDirty(page)) { 272127e670aSJaegeuk Kim __set_page_dirty_nobuffers(page); 273127e670aSJaegeuk Kim inc_page_count(sbi, F2FS_DIRTY_META); 274127e670aSJaegeuk Kim return 1; 275127e670aSJaegeuk Kim } 276127e670aSJaegeuk Kim return 0; 277127e670aSJaegeuk Kim } 278127e670aSJaegeuk Kim 279127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 280127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 281127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 282127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 283127e670aSJaegeuk Kim }; 284127e670aSJaegeuk Kim 2856451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 286953e6cc6SJaegeuk Kim { 28739efac41SJaegeuk Kim struct ino_entry *e; 28839efac41SJaegeuk Kim retry: 2896451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[type]); 290953e6cc6SJaegeuk Kim 29139efac41SJaegeuk Kim e = radix_tree_lookup(&sbi->ino_root[type], ino); 29239efac41SJaegeuk Kim if (!e) { 29339efac41SJaegeuk Kim e = kmem_cache_alloc(ino_entry_slab, GFP_ATOMIC); 29439efac41SJaegeuk Kim if (!e) { 29539efac41SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 29639efac41SJaegeuk Kim goto retry; 29739efac41SJaegeuk Kim } 29839efac41SJaegeuk Kim if (radix_tree_insert(&sbi->ino_root[type], ino, e)) { 29939efac41SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 30039efac41SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 30139efac41SJaegeuk Kim goto retry; 30239efac41SJaegeuk Kim } 30339efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 30439efac41SJaegeuk Kim e->ino = ino; 30539efac41SJaegeuk Kim 30639efac41SJaegeuk Kim list_add_tail(&e->list, &sbi->ino_list[type]); 30739efac41SJaegeuk Kim } 3086451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 309953e6cc6SJaegeuk Kim } 310953e6cc6SJaegeuk Kim 3116451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 312953e6cc6SJaegeuk Kim { 3136451e041SJaegeuk Kim struct ino_entry *e; 314953e6cc6SJaegeuk Kim 3156451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[type]); 31639efac41SJaegeuk Kim e = radix_tree_lookup(&sbi->ino_root[type], ino); 31739efac41SJaegeuk Kim if (e) { 318953e6cc6SJaegeuk Kim list_del(&e->list); 31939efac41SJaegeuk Kim radix_tree_delete(&sbi->ino_root[type], ino); 32039efac41SJaegeuk Kim if (type == ORPHAN_INO) 321953e6cc6SJaegeuk Kim sbi->n_orphans--; 3226451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 3236451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 324953e6cc6SJaegeuk Kim return; 325953e6cc6SJaegeuk Kim } 3266451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 327953e6cc6SJaegeuk Kim } 328953e6cc6SJaegeuk Kim 329*fff04f90SJaegeuk Kim void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type) 330*fff04f90SJaegeuk Kim { 331*fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 332*fff04f90SJaegeuk Kim __add_ino_entry(sbi, ino, type); 333*fff04f90SJaegeuk Kim } 334*fff04f90SJaegeuk Kim 335*fff04f90SJaegeuk Kim void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type) 336*fff04f90SJaegeuk Kim { 337*fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 338*fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 339*fff04f90SJaegeuk Kim } 340*fff04f90SJaegeuk Kim 341*fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */ 342*fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 343*fff04f90SJaegeuk Kim { 344*fff04f90SJaegeuk Kim struct ino_entry *e; 345*fff04f90SJaegeuk Kim spin_lock(&sbi->ino_lock[mode]); 346*fff04f90SJaegeuk Kim e = radix_tree_lookup(&sbi->ino_root[mode], ino); 347*fff04f90SJaegeuk Kim spin_unlock(&sbi->ino_lock[mode]); 348*fff04f90SJaegeuk Kim return e ? true : false; 349*fff04f90SJaegeuk Kim } 350*fff04f90SJaegeuk Kim 351*fff04f90SJaegeuk Kim static void release_dirty_inode(struct f2fs_sb_info *sbi) 352*fff04f90SJaegeuk Kim { 353*fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 354*fff04f90SJaegeuk Kim int i; 355*fff04f90SJaegeuk Kim 356*fff04f90SJaegeuk Kim for (i = APPEND_INO; i <= UPDATE_INO; i++) { 357*fff04f90SJaegeuk Kim spin_lock(&sbi->ino_lock[i]); 358*fff04f90SJaegeuk Kim list_for_each_entry_safe(e, tmp, &sbi->ino_list[i], list) { 359*fff04f90SJaegeuk Kim list_del(&e->list); 360*fff04f90SJaegeuk Kim radix_tree_delete(&sbi->ino_root[i], e->ino); 361*fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 362*fff04f90SJaegeuk Kim } 363*fff04f90SJaegeuk Kim spin_unlock(&sbi->ino_lock[i]); 364*fff04f90SJaegeuk Kim } 365*fff04f90SJaegeuk Kim } 366*fff04f90SJaegeuk Kim 367cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi) 368127e670aSJaegeuk Kim { 369127e670aSJaegeuk Kim int err = 0; 370127e670aSJaegeuk Kim 3716451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[ORPHAN_INO]); 3720d47c1adSGu Zheng if (unlikely(sbi->n_orphans >= sbi->max_orphans)) 373127e670aSJaegeuk Kim err = -ENOSPC; 374cbd56e7dSJaegeuk Kim else 375cbd56e7dSJaegeuk Kim sbi->n_orphans++; 3766451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[ORPHAN_INO]); 3770d47c1adSGu Zheng 378127e670aSJaegeuk Kim return err; 379127e670aSJaegeuk Kim } 380127e670aSJaegeuk Kim 381cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi) 382cbd56e7dSJaegeuk Kim { 3836451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[ORPHAN_INO]); 3845d56b671SJaegeuk Kim f2fs_bug_on(sbi->n_orphans == 0); 385cbd56e7dSJaegeuk Kim sbi->n_orphans--; 3866451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[ORPHAN_INO]); 387cbd56e7dSJaegeuk Kim } 388cbd56e7dSJaegeuk Kim 389127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 390127e670aSJaegeuk Kim { 39139efac41SJaegeuk Kim /* add new orphan ino entry into list */ 3926451e041SJaegeuk Kim __add_ino_entry(sbi, ino, ORPHAN_INO); 393127e670aSJaegeuk Kim } 394127e670aSJaegeuk Kim 395127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 396127e670aSJaegeuk Kim { 397953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 3986451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 399127e670aSJaegeuk Kim } 400127e670aSJaegeuk Kim 401127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 402127e670aSJaegeuk Kim { 403127e670aSJaegeuk Kim struct inode *inode = f2fs_iget(sbi->sb, ino); 4045d56b671SJaegeuk Kim f2fs_bug_on(IS_ERR(inode)); 405127e670aSJaegeuk Kim clear_nlink(inode); 406127e670aSJaegeuk Kim 407127e670aSJaegeuk Kim /* truncate all the data during iput */ 408127e670aSJaegeuk Kim iput(inode); 409127e670aSJaegeuk Kim } 410127e670aSJaegeuk Kim 4118f99a946SChao Yu void recover_orphan_inodes(struct f2fs_sb_info *sbi) 412127e670aSJaegeuk Kim { 413127e670aSJaegeuk Kim block_t start_blk, orphan_blkaddr, i, j; 414127e670aSJaegeuk Kim 41525ca923bSJaegeuk Kim if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG)) 4168f99a946SChao Yu return; 417127e670aSJaegeuk Kim 418aabe5136SHaicheng Li sbi->por_doing = true; 4191dbe4152SChangman Lee 4201dbe4152SChangman Lee start_blk = __start_cp_addr(sbi) + 1 + 4211dbe4152SChangman Lee le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 422127e670aSJaegeuk Kim orphan_blkaddr = __start_sum_addr(sbi) - 1; 423127e670aSJaegeuk Kim 424662befdaSChao Yu ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP); 425662befdaSChao Yu 426127e670aSJaegeuk Kim for (i = 0; i < orphan_blkaddr; i++) { 427127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 428127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 429127e670aSJaegeuk Kim 430127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 431127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 432127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 433127e670aSJaegeuk Kim recover_orphan_inode(sbi, ino); 434127e670aSJaegeuk Kim } 435127e670aSJaegeuk Kim f2fs_put_page(page, 1); 436127e670aSJaegeuk Kim } 437127e670aSJaegeuk Kim /* clear Orphan Flag */ 43825ca923bSJaegeuk Kim clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG); 439aabe5136SHaicheng Li sbi->por_doing = false; 4408f99a946SChao Yu return; 441127e670aSJaegeuk Kim } 442127e670aSJaegeuk Kim 443127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 444127e670aSJaegeuk Kim { 445502c6e0bSGu Zheng struct list_head *head; 446127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 447127e670aSJaegeuk Kim unsigned int nentries = 0; 4484531929eSGu Zheng unsigned short index; 4494531929eSGu Zheng unsigned short orphan_blocks = (unsigned short)((sbi->n_orphans + 4504531929eSGu Zheng (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK); 4514531929eSGu Zheng struct page *page = NULL; 4526451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 453127e670aSJaegeuk Kim 4544531929eSGu Zheng for (index = 0; index < orphan_blocks; index++) 45563f5384cSGu Zheng grab_meta_page(sbi, start_blk + index); 456127e670aSJaegeuk Kim 4574531929eSGu Zheng index = 1; 4586451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[ORPHAN_INO]); 4596451e041SJaegeuk Kim head = &sbi->ino_list[ORPHAN_INO]; 460127e670aSJaegeuk Kim 461127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 462502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 463502c6e0bSGu Zheng if (!page) { 46463f5384cSGu Zheng page = find_get_page(META_MAPPING(sbi), start_blk++); 46563f5384cSGu Zheng f2fs_bug_on(!page); 466502c6e0bSGu Zheng orphan_blk = 467502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 468502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 46963f5384cSGu Zheng f2fs_put_page(page, 0); 470502c6e0bSGu Zheng } 471127e670aSJaegeuk Kim 47236795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 473127e670aSJaegeuk Kim 47436795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 475127e670aSJaegeuk Kim /* 476127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 477127e670aSJaegeuk Kim * then we need to flush current orphan blocks 478127e670aSJaegeuk Kim * and bring another one in memory 479127e670aSJaegeuk Kim */ 480127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 481127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 482127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 483127e670aSJaegeuk Kim set_page_dirty(page); 484127e670aSJaegeuk Kim f2fs_put_page(page, 1); 485127e670aSJaegeuk Kim index++; 486127e670aSJaegeuk Kim nentries = 0; 487127e670aSJaegeuk Kim page = NULL; 488127e670aSJaegeuk Kim } 489127e670aSJaegeuk Kim } 490127e670aSJaegeuk Kim 491502c6e0bSGu Zheng if (page) { 492127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 493127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 494127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 495127e670aSJaegeuk Kim set_page_dirty(page); 496127e670aSJaegeuk Kim f2fs_put_page(page, 1); 497502c6e0bSGu Zheng } 498502c6e0bSGu Zheng 4996451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[ORPHAN_INO]); 500127e670aSJaegeuk Kim } 501127e670aSJaegeuk Kim 502127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 503127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 504127e670aSJaegeuk Kim { 505127e670aSJaegeuk Kim struct page *cp_page_1, *cp_page_2 = NULL; 506127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 507127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 508127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 509127e670aSJaegeuk Kim size_t crc_offset; 5107e586fa0SJaegeuk Kim __u32 crc = 0; 511127e670aSJaegeuk Kim 512127e670aSJaegeuk Kim /* Read the 1st cp block in this CP pack */ 513127e670aSJaegeuk Kim cp_page_1 = get_meta_page(sbi, cp_addr); 514127e670aSJaegeuk Kim 515127e670aSJaegeuk Kim /* get the version number */ 516127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); 517127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 518127e670aSJaegeuk Kim if (crc_offset >= blk_size) 519127e670aSJaegeuk Kim goto invalid_cp1; 520127e670aSJaegeuk Kim 5217e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 522127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 523127e670aSJaegeuk Kim goto invalid_cp1; 524127e670aSJaegeuk Kim 525d71b5564SJaegeuk Kim pre_version = cur_cp_version(cp_block); 526127e670aSJaegeuk Kim 527127e670aSJaegeuk Kim /* Read the 2nd cp block in this CP pack */ 52825ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 529127e670aSJaegeuk Kim cp_page_2 = get_meta_page(sbi, cp_addr); 530127e670aSJaegeuk Kim 531127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); 532127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 533127e670aSJaegeuk Kim if (crc_offset >= blk_size) 534127e670aSJaegeuk Kim goto invalid_cp2; 535127e670aSJaegeuk Kim 5367e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 537127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 538127e670aSJaegeuk Kim goto invalid_cp2; 539127e670aSJaegeuk Kim 540d71b5564SJaegeuk Kim cur_version = cur_cp_version(cp_block); 541127e670aSJaegeuk Kim 542127e670aSJaegeuk Kim if (cur_version == pre_version) { 543127e670aSJaegeuk Kim *version = cur_version; 544127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 545127e670aSJaegeuk Kim return cp_page_1; 546127e670aSJaegeuk Kim } 547127e670aSJaegeuk Kim invalid_cp2: 548127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 549127e670aSJaegeuk Kim invalid_cp1: 550127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 551127e670aSJaegeuk Kim return NULL; 552127e670aSJaegeuk Kim } 553127e670aSJaegeuk Kim 554127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 555127e670aSJaegeuk Kim { 556127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 557127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 558127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 559127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 560127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 561127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 5621dbe4152SChangman Lee unsigned int cp_blks = 1 + le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 5631dbe4152SChangman Lee block_t cp_blk_no; 5641dbe4152SChangman Lee int i; 565127e670aSJaegeuk Kim 5661dbe4152SChangman Lee sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL); 567127e670aSJaegeuk Kim if (!sbi->ckpt) 568127e670aSJaegeuk Kim return -ENOMEM; 569127e670aSJaegeuk Kim /* 570127e670aSJaegeuk Kim * Finding out valid cp block involves read both 571127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 572127e670aSJaegeuk Kim */ 573127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 574127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 575127e670aSJaegeuk Kim 576127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 577f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 578f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 579127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 580127e670aSJaegeuk Kim 581127e670aSJaegeuk Kim if (cp1 && cp2) { 582127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 583127e670aSJaegeuk Kim cur_page = cp2; 584127e670aSJaegeuk Kim else 585127e670aSJaegeuk Kim cur_page = cp1; 586127e670aSJaegeuk Kim } else if (cp1) { 587127e670aSJaegeuk Kim cur_page = cp1; 588127e670aSJaegeuk Kim } else if (cp2) { 589127e670aSJaegeuk Kim cur_page = cp2; 590127e670aSJaegeuk Kim } else { 591127e670aSJaegeuk Kim goto fail_no_cp; 592127e670aSJaegeuk Kim } 593127e670aSJaegeuk Kim 594127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 595127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 596127e670aSJaegeuk Kim 5971dbe4152SChangman Lee if (cp_blks <= 1) 5981dbe4152SChangman Lee goto done; 5991dbe4152SChangman Lee 6001dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 6011dbe4152SChangman Lee if (cur_page == cp2) 6021dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 6031dbe4152SChangman Lee 6041dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 6051dbe4152SChangman Lee void *sit_bitmap_ptr; 6061dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 6071dbe4152SChangman Lee 6081dbe4152SChangman Lee cur_page = get_meta_page(sbi, cp_blk_no + i); 6091dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 6101dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 6111dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 6121dbe4152SChangman Lee } 6131dbe4152SChangman Lee done: 614127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 615127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 616127e670aSJaegeuk Kim return 0; 617127e670aSJaegeuk Kim 618127e670aSJaegeuk Kim fail_no_cp: 619127e670aSJaegeuk Kim kfree(sbi->ckpt); 620127e670aSJaegeuk Kim return -EINVAL; 621127e670aSJaegeuk Kim } 622127e670aSJaegeuk Kim 6235deb8267SJaegeuk Kim static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new) 624127e670aSJaegeuk Kim { 625127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6262d7b822aSChao Yu 627ed57c27fSJaegeuk Kim if (is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) 6285deb8267SJaegeuk Kim return -EEXIST; 6292d7b822aSChao Yu 630ed57c27fSJaegeuk Kim set_inode_flag(F2FS_I(inode), FI_DIRTY_DIR); 631ed57c27fSJaegeuk Kim F2FS_I(inode)->dirty_dir = new; 632ed57c27fSJaegeuk Kim list_add_tail(&new->list, &sbi->dir_inode_list); 633dcdfff65SJaegeuk Kim stat_inc_dirty_dir(sbi); 6345deb8267SJaegeuk Kim return 0; 6355deb8267SJaegeuk Kim } 6365deb8267SJaegeuk Kim 6375deb8267SJaegeuk Kim void set_dirty_dir_page(struct inode *inode, struct page *page) 6385deb8267SJaegeuk Kim { 6395deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6405deb8267SJaegeuk Kim struct dir_inode_entry *new; 641cf0ee0f0SChao Yu int ret = 0; 6425deb8267SJaegeuk Kim 643127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 644127e670aSJaegeuk Kim return; 6457bd59381SGu Zheng 6467bd59381SGu Zheng new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 647127e670aSJaegeuk Kim new->inode = inode; 648127e670aSJaegeuk Kim INIT_LIST_HEAD(&new->list); 649127e670aSJaegeuk Kim 650127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 651cf0ee0f0SChao Yu ret = __add_dirty_inode(inode, new); 652127e670aSJaegeuk Kim inode_inc_dirty_dents(inode); 653127e670aSJaegeuk Kim SetPagePrivate(page); 6545deb8267SJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 655cf0ee0f0SChao Yu 656cf0ee0f0SChao Yu if (ret) 657cf0ee0f0SChao Yu kmem_cache_free(inode_entry_slab, new); 6585deb8267SJaegeuk Kim } 659127e670aSJaegeuk Kim 6605deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode) 6615deb8267SJaegeuk Kim { 6625deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6637bd59381SGu Zheng struct dir_inode_entry *new = 6647bd59381SGu Zheng f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 665cf0ee0f0SChao Yu int ret = 0; 6667bd59381SGu Zheng 6675deb8267SJaegeuk Kim new->inode = inode; 6685deb8267SJaegeuk Kim INIT_LIST_HEAD(&new->list); 6695deb8267SJaegeuk Kim 6705deb8267SJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 671cf0ee0f0SChao Yu ret = __add_dirty_inode(inode, new); 672127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 673cf0ee0f0SChao Yu 674cf0ee0f0SChao Yu if (ret) 675cf0ee0f0SChao Yu kmem_cache_free(inode_entry_slab, new); 676127e670aSJaegeuk Kim } 677127e670aSJaegeuk Kim 678127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode) 679127e670aSJaegeuk Kim { 680127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6812d7b822aSChao Yu struct dir_inode_entry *entry; 682127e670aSJaegeuk Kim 683127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 684127e670aSJaegeuk Kim return; 685127e670aSJaegeuk Kim 686127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 687ed57c27fSJaegeuk Kim if (get_dirty_dents(inode) || 688ed57c27fSJaegeuk Kim !is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) { 6893b10b1fdSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 6903b10b1fdSJaegeuk Kim return; 6913b10b1fdSJaegeuk Kim } 692127e670aSJaegeuk Kim 693ed57c27fSJaegeuk Kim entry = F2FS_I(inode)->dirty_dir; 694127e670aSJaegeuk Kim list_del(&entry->list); 695ed57c27fSJaegeuk Kim F2FS_I(inode)->dirty_dir = NULL; 696ed57c27fSJaegeuk Kim clear_inode_flag(F2FS_I(inode), FI_DIRTY_DIR); 697dcdfff65SJaegeuk Kim stat_dec_dirty_dir(sbi); 698cf0ee0f0SChao Yu spin_unlock(&sbi->dir_inode_lock); 699cf0ee0f0SChao Yu kmem_cache_free(inode_entry_slab, entry); 70074d0b917SJaegeuk Kim 70174d0b917SJaegeuk Kim /* Only from the recovery routine */ 702afc3eda2SJaegeuk Kim if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) { 703afc3eda2SJaegeuk Kim clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT); 70474d0b917SJaegeuk Kim iput(inode); 70574d0b917SJaegeuk Kim } 706afc3eda2SJaegeuk Kim } 70774d0b917SJaegeuk Kim 708127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi) 709127e670aSJaegeuk Kim { 710ce3b7d80SGu Zheng struct list_head *head; 711127e670aSJaegeuk Kim struct dir_inode_entry *entry; 712127e670aSJaegeuk Kim struct inode *inode; 713127e670aSJaegeuk Kim retry: 714127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 715ce3b7d80SGu Zheng 716ce3b7d80SGu Zheng head = &sbi->dir_inode_list; 717127e670aSJaegeuk Kim if (list_empty(head)) { 718127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 719127e670aSJaegeuk Kim return; 720127e670aSJaegeuk Kim } 721127e670aSJaegeuk Kim entry = list_entry(head->next, struct dir_inode_entry, list); 722127e670aSJaegeuk Kim inode = igrab(entry->inode); 723127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 724127e670aSJaegeuk Kim if (inode) { 72587d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 726127e670aSJaegeuk Kim iput(inode); 727127e670aSJaegeuk Kim } else { 728127e670aSJaegeuk Kim /* 729127e670aSJaegeuk Kim * We should submit bio, since it exists several 730127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 731127e670aSJaegeuk Kim */ 732458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 733127e670aSJaegeuk Kim } 734127e670aSJaegeuk Kim goto retry; 735127e670aSJaegeuk Kim } 736127e670aSJaegeuk Kim 7370a8165d7SJaegeuk Kim /* 738127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 739127e670aSJaegeuk Kim */ 74043727527SJaegeuk Kim static void block_operations(struct f2fs_sb_info *sbi) 741127e670aSJaegeuk Kim { 742127e670aSJaegeuk Kim struct writeback_control wbc = { 743127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 744127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 745127e670aSJaegeuk Kim .for_reclaim = 0, 746127e670aSJaegeuk Kim }; 747c718379bSJaegeuk Kim struct blk_plug plug; 748c718379bSJaegeuk Kim 749c718379bSJaegeuk Kim blk_start_plug(&plug); 750c718379bSJaegeuk Kim 75139936837SJaegeuk Kim retry_flush_dents: 752e479556bSGu Zheng f2fs_lock_all(sbi); 753127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 754127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 755e479556bSGu Zheng f2fs_unlock_all(sbi); 75639936837SJaegeuk Kim sync_dirty_dir_inodes(sbi); 75739936837SJaegeuk Kim goto retry_flush_dents; 758127e670aSJaegeuk Kim } 759127e670aSJaegeuk Kim 760127e670aSJaegeuk Kim /* 761127e670aSJaegeuk Kim * POR: we should ensure that there is no dirty node pages 762127e670aSJaegeuk Kim * until finishing nat/sit flush. 763127e670aSJaegeuk Kim */ 76439936837SJaegeuk Kim retry_flush_nodes: 76539936837SJaegeuk Kim mutex_lock(&sbi->node_write); 766127e670aSJaegeuk Kim 767127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 76839936837SJaegeuk Kim mutex_unlock(&sbi->node_write); 76939936837SJaegeuk Kim sync_node_pages(sbi, 0, &wbc); 77039936837SJaegeuk Kim goto retry_flush_nodes; 771127e670aSJaegeuk Kim } 772c718379bSJaegeuk Kim blk_finish_plug(&plug); 773127e670aSJaegeuk Kim } 774127e670aSJaegeuk Kim 775127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 776127e670aSJaegeuk Kim { 77739936837SJaegeuk Kim mutex_unlock(&sbi->node_write); 778e479556bSGu Zheng f2fs_unlock_all(sbi); 779127e670aSJaegeuk Kim } 780127e670aSJaegeuk Kim 781fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 782fb51b5efSChangman Lee { 783fb51b5efSChangman Lee DEFINE_WAIT(wait); 784fb51b5efSChangman Lee 785fb51b5efSChangman Lee for (;;) { 786fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 787fb51b5efSChangman Lee 788fb51b5efSChangman Lee if (!get_pages(sbi, F2FS_WRITEBACK)) 789fb51b5efSChangman Lee break; 790fb51b5efSChangman Lee 791fb51b5efSChangman Lee io_schedule(); 792fb51b5efSChangman Lee } 793fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 794fb51b5efSChangman Lee } 795fb51b5efSChangman Lee 796127e670aSJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 797127e670aSJaegeuk Kim { 798127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 799127e670aSJaegeuk Kim nid_t last_nid = 0; 800127e670aSJaegeuk Kim block_t start_blk; 801127e670aSJaegeuk Kim struct page *cp_page; 802127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 8037e586fa0SJaegeuk Kim __u32 crc32 = 0; 804127e670aSJaegeuk Kim void *kaddr; 805127e670aSJaegeuk Kim int i; 8061dbe4152SChangman Lee int cp_payload_blks = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 807127e670aSJaegeuk Kim 8081e87a78dSJaegeuk Kim /* 8091e87a78dSJaegeuk Kim * This avoids to conduct wrong roll-forward operations and uses 8101e87a78dSJaegeuk Kim * metapages, so should be called prior to sync_meta_pages below. 8111e87a78dSJaegeuk Kim */ 8121e87a78dSJaegeuk Kim discard_next_dnode(sbi); 813127e670aSJaegeuk Kim 814127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 815127e670aSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) 816127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 817127e670aSJaegeuk Kim 818127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 819127e670aSJaegeuk Kim 820127e670aSJaegeuk Kim /* 821127e670aSJaegeuk Kim * modify checkpoint 822127e670aSJaegeuk Kim * version number is already updated 823127e670aSJaegeuk Kim */ 824127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 825127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 826127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 827127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 828127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 829127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 830127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 831127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 832127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 833127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 834127e670aSJaegeuk Kim } 835127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 836127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 837127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 838127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 839127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 840127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 841127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 842127e670aSJaegeuk Kim } 843127e670aSJaegeuk Kim 844127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 845127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 846127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 847127e670aSJaegeuk Kim 848127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 849127e670aSJaegeuk Kim data_sum_blocks = npages_for_summary_flush(sbi); 850127e670aSJaegeuk Kim if (data_sum_blocks < 3) 85125ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 852127e670aSJaegeuk Kim else 85325ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 854127e670aSJaegeuk Kim 855127e670aSJaegeuk Kim orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1) 856127e670aSJaegeuk Kim / F2FS_ORPHANS_PER_BLOCK; 8571dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 8581dbe4152SChangman Lee orphan_blocks); 859127e670aSJaegeuk Kim 860127e670aSJaegeuk Kim if (is_umount) { 86125ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 86225ca923bSJaegeuk Kim ckpt->cp_pack_total_block_count = cpu_to_le32(2 + 8631dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 8641dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 865127e670aSJaegeuk Kim } else { 86625ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 86725ca923bSJaegeuk Kim ckpt->cp_pack_total_block_count = cpu_to_le32(2 + 8681dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 8691dbe4152SChangman Lee orphan_blocks); 870127e670aSJaegeuk Kim } 871127e670aSJaegeuk Kim 872127e670aSJaegeuk Kim if (sbi->n_orphans) 87325ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 874127e670aSJaegeuk Kim else 87525ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 876127e670aSJaegeuk Kim 877127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 878127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 879127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 880127e670aSJaegeuk Kim 881127e670aSJaegeuk Kim crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); 8827e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 8837e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 884127e670aSJaegeuk Kim = cpu_to_le32(crc32); 885127e670aSJaegeuk Kim 886127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi); 887127e670aSJaegeuk Kim 888127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 889127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk++); 890127e670aSJaegeuk Kim kaddr = page_address(cp_page); 891127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 892127e670aSJaegeuk Kim set_page_dirty(cp_page); 893127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 894127e670aSJaegeuk Kim 8951dbe4152SChangman Lee for (i = 1; i < 1 + cp_payload_blks; i++) { 8961dbe4152SChangman Lee cp_page = grab_meta_page(sbi, start_blk++); 8971dbe4152SChangman Lee kaddr = page_address(cp_page); 8981dbe4152SChangman Lee memcpy(kaddr, (char *)ckpt + i * F2FS_BLKSIZE, 8991dbe4152SChangman Lee (1 << sbi->log_blocksize)); 9001dbe4152SChangman Lee set_page_dirty(cp_page); 9011dbe4152SChangman Lee f2fs_put_page(cp_page, 1); 9021dbe4152SChangman Lee } 9031dbe4152SChangman Lee 904127e670aSJaegeuk Kim if (sbi->n_orphans) { 905127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 906127e670aSJaegeuk Kim start_blk += orphan_blocks; 907127e670aSJaegeuk Kim } 908127e670aSJaegeuk Kim 909127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 910127e670aSJaegeuk Kim start_blk += data_sum_blocks; 911127e670aSJaegeuk Kim if (is_umount) { 912127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 913127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 914127e670aSJaegeuk Kim } 915127e670aSJaegeuk Kim 916127e670aSJaegeuk Kim /* writeout checkpoint block */ 917127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk); 918127e670aSJaegeuk Kim kaddr = page_address(cp_page); 919127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 920127e670aSJaegeuk Kim set_page_dirty(cp_page); 921127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 922127e670aSJaegeuk Kim 923127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 924fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 925127e670aSJaegeuk Kim 9264ef51a8fSJaegeuk Kim filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX); 9279df27d98SGu Zheng filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX); 928127e670aSJaegeuk Kim 929127e670aSJaegeuk Kim /* update user_block_counts */ 930127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 931127e670aSJaegeuk Kim sbi->alloc_valid_block_count = 0; 932127e670aSJaegeuk Kim 933127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 934127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 935127e670aSJaegeuk Kim 9366bacf52fSJaegeuk Kim if (unlikely(!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG))) { 937127e670aSJaegeuk Kim clear_prefree_segments(sbi); 938*fff04f90SJaegeuk Kim release_dirty_inode(sbi); 939127e670aSJaegeuk Kim F2FS_RESET_SB_DIRT(sbi); 940127e670aSJaegeuk Kim } 941577e3495SJaegeuk Kim } 942127e670aSJaegeuk Kim 9430a8165d7SJaegeuk Kim /* 944127e670aSJaegeuk Kim * We guarantee that this checkpoint procedure should not fail. 945127e670aSJaegeuk Kim */ 94643727527SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 947127e670aSJaegeuk Kim { 948127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 949127e670aSJaegeuk Kim unsigned long long ckpt_ver; 950127e670aSJaegeuk Kim 9512af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops"); 9522af4bd6cSNamjae Jeon 953127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 954127e670aSJaegeuk Kim block_operations(sbi); 955127e670aSJaegeuk Kim 9562af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops"); 9572af4bd6cSNamjae Jeon 958458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 959458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, NODE, WRITE); 960458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 961127e670aSJaegeuk Kim 962127e670aSJaegeuk Kim /* 963127e670aSJaegeuk Kim * update checkpoint pack index 964127e670aSJaegeuk Kim * Increase the version number so that 965127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 966127e670aSJaegeuk Kim */ 967d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 968127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 969127e670aSJaegeuk Kim 970127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 971127e670aSJaegeuk Kim flush_nat_entries(sbi); 972127e670aSJaegeuk Kim flush_sit_entries(sbi); 973127e670aSJaegeuk Kim 974127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 975127e670aSJaegeuk Kim do_checkpoint(sbi, is_umount); 976127e670aSJaegeuk Kim 977127e670aSJaegeuk Kim unblock_operations(sbi); 978127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 9792af4bd6cSNamjae Jeon 980942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 9812af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint"); 982127e670aSJaegeuk Kim } 983127e670aSJaegeuk Kim 9846451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi) 985127e670aSJaegeuk Kim { 9866451e041SJaegeuk Kim int i; 9876451e041SJaegeuk Kim 9886451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 98939efac41SJaegeuk Kim INIT_RADIX_TREE(&sbi->ino_root[i], GFP_ATOMIC); 9906451e041SJaegeuk Kim spin_lock_init(&sbi->ino_lock[i]); 9916451e041SJaegeuk Kim INIT_LIST_HEAD(&sbi->ino_list[i]); 9926451e041SJaegeuk Kim } 9936451e041SJaegeuk Kim 9940d47c1adSGu Zheng /* 9950d47c1adSGu Zheng * considering 512 blocks in a segment 8 blocks are needed for cp 9960d47c1adSGu Zheng * and log segment summaries. Remaining blocks are used to keep 9970d47c1adSGu Zheng * orphan entries with the limitation one reserved segment 9980d47c1adSGu Zheng * for cp pack we can have max 1020*504 orphan entries 9990d47c1adSGu Zheng */ 10006451e041SJaegeuk Kim sbi->n_orphans = 0; 10010d47c1adSGu Zheng sbi->max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE) 10020d47c1adSGu Zheng * F2FS_ORPHANS_PER_BLOCK; 1003127e670aSJaegeuk Kim } 1004127e670aSJaegeuk Kim 10056e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 1006127e670aSJaegeuk Kim { 10076451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 10086451e041SJaegeuk Kim sizeof(struct ino_entry)); 10096451e041SJaegeuk Kim if (!ino_entry_slab) 1010127e670aSJaegeuk Kim return -ENOMEM; 1011127e670aSJaegeuk Kim inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry", 1012e8512d2eSGu Zheng sizeof(struct dir_inode_entry)); 10136bacf52fSJaegeuk Kim if (!inode_entry_slab) { 10146451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1015127e670aSJaegeuk Kim return -ENOMEM; 1016127e670aSJaegeuk Kim } 1017127e670aSJaegeuk Kim return 0; 1018127e670aSJaegeuk Kim } 1019127e670aSJaegeuk Kim 1020127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 1021127e670aSJaegeuk Kim { 10226451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1023127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 1024127e670aSJaegeuk Kim } 1025