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; 1631e968fdfSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 164cf779cabSJaegeuk Kim goto redirty_out; 165127e670aSJaegeuk Kim 1663cb5ad15SJaegeuk Kim f2fs_wait_on_page_writeback(page, META); 167577e3495SJaegeuk Kim write_meta_page(sbi, page); 168127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 169127e670aSJaegeuk Kim unlock_page(page); 170577e3495SJaegeuk Kim return 0; 171cfb271d4SChao Yu 172cfb271d4SChao Yu redirty_out: 17376f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page); 174cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE; 175127e670aSJaegeuk Kim } 176127e670aSJaegeuk Kim 177127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 178127e670aSJaegeuk Kim struct writeback_control *wbc) 179127e670aSJaegeuk Kim { 180127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 18150c8cdb3SJaegeuk Kim long diff, written; 182127e670aSJaegeuk Kim 183e5748434SChao Yu trace_f2fs_writepages(mapping->host, wbc, META); 184e5748434SChao Yu 1855459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */ 18650c8cdb3SJaegeuk Kim if (wbc->for_kupdate || 18750c8cdb3SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META)) 188d3baf95dSJaegeuk Kim goto skip_write; 189127e670aSJaegeuk Kim 190127e670aSJaegeuk Kim /* if mounting is failed, skip writing node pages */ 191127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 19250c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc); 19350c8cdb3SJaegeuk Kim written = sync_meta_pages(sbi, META, wbc->nr_to_write); 194127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 19550c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 196127e670aSJaegeuk Kim return 0; 197d3baf95dSJaegeuk Kim 198d3baf95dSJaegeuk Kim skip_write: 199d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); 200d3baf95dSJaegeuk Kim return 0; 201127e670aSJaegeuk Kim } 202127e670aSJaegeuk Kim 203127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 204127e670aSJaegeuk Kim long nr_to_write) 205127e670aSJaegeuk Kim { 2069df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi); 207127e670aSJaegeuk Kim pgoff_t index = 0, end = LONG_MAX; 208127e670aSJaegeuk Kim struct pagevec pvec; 209127e670aSJaegeuk Kim long nwritten = 0; 210127e670aSJaegeuk Kim struct writeback_control wbc = { 211127e670aSJaegeuk Kim .for_reclaim = 0, 212127e670aSJaegeuk Kim }; 213127e670aSJaegeuk Kim 214127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 215127e670aSJaegeuk Kim 216127e670aSJaegeuk Kim while (index <= end) { 217127e670aSJaegeuk Kim int i, nr_pages; 218127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 219127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 220127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 221cfb271d4SChao Yu if (unlikely(nr_pages == 0)) 222127e670aSJaegeuk Kim break; 223127e670aSJaegeuk Kim 224127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 225127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 226203681f6SJaegeuk Kim 227127e670aSJaegeuk Kim lock_page(page); 228203681f6SJaegeuk Kim 229203681f6SJaegeuk Kim if (unlikely(page->mapping != mapping)) { 230203681f6SJaegeuk Kim continue_unlock: 231203681f6SJaegeuk Kim unlock_page(page); 232203681f6SJaegeuk Kim continue; 233203681f6SJaegeuk Kim } 234203681f6SJaegeuk Kim if (!PageDirty(page)) { 235203681f6SJaegeuk Kim /* someone wrote it for us */ 236203681f6SJaegeuk Kim goto continue_unlock; 237203681f6SJaegeuk Kim } 238203681f6SJaegeuk Kim 239203681f6SJaegeuk Kim if (!clear_page_dirty_for_io(page)) 240203681f6SJaegeuk Kim goto continue_unlock; 241203681f6SJaegeuk Kim 242577e3495SJaegeuk Kim if (f2fs_write_meta_page(page, &wbc)) { 243577e3495SJaegeuk Kim unlock_page(page); 244577e3495SJaegeuk Kim break; 245577e3495SJaegeuk Kim } 246cfb271d4SChao Yu nwritten++; 247cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write)) 248127e670aSJaegeuk Kim break; 249127e670aSJaegeuk Kim } 250127e670aSJaegeuk Kim pagevec_release(&pvec); 251127e670aSJaegeuk Kim cond_resched(); 252127e670aSJaegeuk Kim } 253127e670aSJaegeuk Kim 254127e670aSJaegeuk Kim if (nwritten) 255458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, type, WRITE); 256127e670aSJaegeuk Kim 257127e670aSJaegeuk Kim return nwritten; 258127e670aSJaegeuk Kim } 259127e670aSJaegeuk Kim 260127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 261127e670aSJaegeuk Kim { 262127e670aSJaegeuk Kim struct address_space *mapping = page->mapping; 263127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 264127e670aSJaegeuk Kim 26526c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 26626c6b887SJaegeuk Kim 267127e670aSJaegeuk Kim SetPageUptodate(page); 268127e670aSJaegeuk Kim if (!PageDirty(page)) { 269127e670aSJaegeuk Kim __set_page_dirty_nobuffers(page); 270127e670aSJaegeuk Kim inc_page_count(sbi, F2FS_DIRTY_META); 271127e670aSJaegeuk Kim return 1; 272127e670aSJaegeuk Kim } 273127e670aSJaegeuk Kim return 0; 274127e670aSJaegeuk Kim } 275127e670aSJaegeuk Kim 276127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 277127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 278127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 279127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 280127e670aSJaegeuk Kim }; 281127e670aSJaegeuk Kim 2826451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 283953e6cc6SJaegeuk Kim { 28439efac41SJaegeuk Kim struct ino_entry *e; 28539efac41SJaegeuk Kim retry: 2866451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[type]); 287953e6cc6SJaegeuk Kim 28839efac41SJaegeuk Kim e = radix_tree_lookup(&sbi->ino_root[type], ino); 28939efac41SJaegeuk Kim if (!e) { 29039efac41SJaegeuk Kim e = kmem_cache_alloc(ino_entry_slab, GFP_ATOMIC); 29139efac41SJaegeuk Kim if (!e) { 29239efac41SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 29339efac41SJaegeuk Kim goto retry; 29439efac41SJaegeuk Kim } 29539efac41SJaegeuk Kim if (radix_tree_insert(&sbi->ino_root[type], ino, e)) { 29639efac41SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 29739efac41SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 29839efac41SJaegeuk Kim goto retry; 29939efac41SJaegeuk Kim } 30039efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry)); 30139efac41SJaegeuk Kim e->ino = ino; 30239efac41SJaegeuk Kim 30339efac41SJaegeuk Kim list_add_tail(&e->list, &sbi->ino_list[type]); 30439efac41SJaegeuk Kim } 3056451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 306953e6cc6SJaegeuk Kim } 307953e6cc6SJaegeuk Kim 3086451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) 309953e6cc6SJaegeuk Kim { 3106451e041SJaegeuk Kim struct ino_entry *e; 311953e6cc6SJaegeuk Kim 3126451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[type]); 31339efac41SJaegeuk Kim e = radix_tree_lookup(&sbi->ino_root[type], ino); 31439efac41SJaegeuk Kim if (e) { 315953e6cc6SJaegeuk Kim list_del(&e->list); 31639efac41SJaegeuk Kim radix_tree_delete(&sbi->ino_root[type], ino); 31739efac41SJaegeuk Kim if (type == ORPHAN_INO) 318953e6cc6SJaegeuk Kim sbi->n_orphans--; 3196451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 3206451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 321953e6cc6SJaegeuk Kim return; 322953e6cc6SJaegeuk Kim } 3236451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[type]); 324953e6cc6SJaegeuk Kim } 325953e6cc6SJaegeuk Kim 326fff04f90SJaegeuk Kim void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type) 327fff04f90SJaegeuk Kim { 328fff04f90SJaegeuk Kim /* add new dirty ino entry into list */ 329fff04f90SJaegeuk Kim __add_ino_entry(sbi, ino, type); 330fff04f90SJaegeuk Kim } 331fff04f90SJaegeuk Kim 332fff04f90SJaegeuk Kim void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type) 333fff04f90SJaegeuk Kim { 334fff04f90SJaegeuk Kim /* remove dirty ino entry from list */ 335fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type); 336fff04f90SJaegeuk Kim } 337fff04f90SJaegeuk Kim 338fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */ 339fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) 340fff04f90SJaegeuk Kim { 341fff04f90SJaegeuk Kim struct ino_entry *e; 342fff04f90SJaegeuk Kim spin_lock(&sbi->ino_lock[mode]); 343fff04f90SJaegeuk Kim e = radix_tree_lookup(&sbi->ino_root[mode], ino); 344fff04f90SJaegeuk Kim spin_unlock(&sbi->ino_lock[mode]); 345fff04f90SJaegeuk Kim return e ? true : false; 346fff04f90SJaegeuk Kim } 347fff04f90SJaegeuk Kim 3486f12ac25SJaegeuk Kim void release_dirty_inode(struct f2fs_sb_info *sbi) 349fff04f90SJaegeuk Kim { 350fff04f90SJaegeuk Kim struct ino_entry *e, *tmp; 351fff04f90SJaegeuk Kim int i; 352fff04f90SJaegeuk Kim 353fff04f90SJaegeuk Kim for (i = APPEND_INO; i <= UPDATE_INO; i++) { 354fff04f90SJaegeuk Kim spin_lock(&sbi->ino_lock[i]); 355fff04f90SJaegeuk Kim list_for_each_entry_safe(e, tmp, &sbi->ino_list[i], list) { 356fff04f90SJaegeuk Kim list_del(&e->list); 357fff04f90SJaegeuk Kim radix_tree_delete(&sbi->ino_root[i], e->ino); 358fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e); 359fff04f90SJaegeuk Kim } 360fff04f90SJaegeuk Kim spin_unlock(&sbi->ino_lock[i]); 361fff04f90SJaegeuk Kim } 362fff04f90SJaegeuk Kim } 363fff04f90SJaegeuk Kim 364cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi) 365127e670aSJaegeuk Kim { 366127e670aSJaegeuk Kim int err = 0; 367127e670aSJaegeuk Kim 3686451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[ORPHAN_INO]); 3690d47c1adSGu Zheng if (unlikely(sbi->n_orphans >= sbi->max_orphans)) 370127e670aSJaegeuk Kim err = -ENOSPC; 371cbd56e7dSJaegeuk Kim else 372cbd56e7dSJaegeuk Kim sbi->n_orphans++; 3736451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[ORPHAN_INO]); 3740d47c1adSGu Zheng 375127e670aSJaegeuk Kim return err; 376127e670aSJaegeuk Kim } 377127e670aSJaegeuk Kim 378cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi) 379cbd56e7dSJaegeuk Kim { 3806451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[ORPHAN_INO]); 3815d56b671SJaegeuk Kim f2fs_bug_on(sbi->n_orphans == 0); 382cbd56e7dSJaegeuk Kim sbi->n_orphans--; 3836451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[ORPHAN_INO]); 384cbd56e7dSJaegeuk Kim } 385cbd56e7dSJaegeuk Kim 386127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 387127e670aSJaegeuk Kim { 38839efac41SJaegeuk Kim /* add new orphan ino entry into list */ 3896451e041SJaegeuk Kim __add_ino_entry(sbi, ino, ORPHAN_INO); 390127e670aSJaegeuk Kim } 391127e670aSJaegeuk Kim 392127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 393127e670aSJaegeuk Kim { 394953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */ 3956451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO); 396127e670aSJaegeuk Kim } 397127e670aSJaegeuk Kim 398127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 399127e670aSJaegeuk Kim { 400127e670aSJaegeuk Kim struct inode *inode = f2fs_iget(sbi->sb, ino); 4015d56b671SJaegeuk Kim f2fs_bug_on(IS_ERR(inode)); 402127e670aSJaegeuk Kim clear_nlink(inode); 403127e670aSJaegeuk Kim 404127e670aSJaegeuk Kim /* truncate all the data during iput */ 405127e670aSJaegeuk Kim iput(inode); 406127e670aSJaegeuk Kim } 407127e670aSJaegeuk Kim 4088f99a946SChao Yu void recover_orphan_inodes(struct f2fs_sb_info *sbi) 409127e670aSJaegeuk Kim { 410127e670aSJaegeuk Kim block_t start_blk, orphan_blkaddr, i, j; 411127e670aSJaegeuk Kim 41225ca923bSJaegeuk Kim if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG)) 4138f99a946SChao Yu return; 414127e670aSJaegeuk Kim 415aabe5136SHaicheng Li sbi->por_doing = true; 4161dbe4152SChangman Lee 4171dbe4152SChangman Lee start_blk = __start_cp_addr(sbi) + 1 + 4181dbe4152SChangman Lee le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 419127e670aSJaegeuk Kim orphan_blkaddr = __start_sum_addr(sbi) - 1; 420127e670aSJaegeuk Kim 421662befdaSChao Yu ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP); 422662befdaSChao Yu 423127e670aSJaegeuk Kim for (i = 0; i < orphan_blkaddr; i++) { 424127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 425127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 426127e670aSJaegeuk Kim 427127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 428127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 429127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 430127e670aSJaegeuk Kim recover_orphan_inode(sbi, ino); 431127e670aSJaegeuk Kim } 432127e670aSJaegeuk Kim f2fs_put_page(page, 1); 433127e670aSJaegeuk Kim } 434127e670aSJaegeuk Kim /* clear Orphan Flag */ 43525ca923bSJaegeuk Kim clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG); 436aabe5136SHaicheng Li sbi->por_doing = false; 4378f99a946SChao Yu return; 438127e670aSJaegeuk Kim } 439127e670aSJaegeuk Kim 440127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 441127e670aSJaegeuk Kim { 442502c6e0bSGu Zheng struct list_head *head; 443127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 444127e670aSJaegeuk Kim unsigned int nentries = 0; 4454531929eSGu Zheng unsigned short index; 446*b5b82205SChao Yu unsigned short orphan_blocks = 447*b5b82205SChao Yu (unsigned short)GET_ORPHAN_BLOCKS(sbi->n_orphans); 4484531929eSGu Zheng struct page *page = NULL; 4496451e041SJaegeuk Kim struct ino_entry *orphan = NULL; 450127e670aSJaegeuk Kim 4514531929eSGu Zheng for (index = 0; index < orphan_blocks; index++) 45263f5384cSGu Zheng grab_meta_page(sbi, start_blk + index); 453127e670aSJaegeuk Kim 4544531929eSGu Zheng index = 1; 4556451e041SJaegeuk Kim spin_lock(&sbi->ino_lock[ORPHAN_INO]); 4566451e041SJaegeuk Kim head = &sbi->ino_list[ORPHAN_INO]; 457127e670aSJaegeuk Kim 458127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 459502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 460502c6e0bSGu Zheng if (!page) { 46163f5384cSGu Zheng page = find_get_page(META_MAPPING(sbi), start_blk++); 46263f5384cSGu Zheng f2fs_bug_on(!page); 463502c6e0bSGu Zheng orphan_blk = 464502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 465502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 46663f5384cSGu Zheng f2fs_put_page(page, 0); 467502c6e0bSGu Zheng } 468127e670aSJaegeuk Kim 46936795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 470127e670aSJaegeuk Kim 47136795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) { 472127e670aSJaegeuk Kim /* 473127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 474127e670aSJaegeuk Kim * then we need to flush current orphan blocks 475127e670aSJaegeuk Kim * and bring another one in memory 476127e670aSJaegeuk Kim */ 477127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 478127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 479127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 480127e670aSJaegeuk Kim set_page_dirty(page); 481127e670aSJaegeuk Kim f2fs_put_page(page, 1); 482127e670aSJaegeuk Kim index++; 483127e670aSJaegeuk Kim nentries = 0; 484127e670aSJaegeuk Kim page = NULL; 485127e670aSJaegeuk Kim } 486127e670aSJaegeuk Kim } 487127e670aSJaegeuk Kim 488502c6e0bSGu Zheng if (page) { 489127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 490127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 491127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 492127e670aSJaegeuk Kim set_page_dirty(page); 493127e670aSJaegeuk Kim f2fs_put_page(page, 1); 494502c6e0bSGu Zheng } 495502c6e0bSGu Zheng 4966451e041SJaegeuk Kim spin_unlock(&sbi->ino_lock[ORPHAN_INO]); 497127e670aSJaegeuk Kim } 498127e670aSJaegeuk Kim 499127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 500127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 501127e670aSJaegeuk Kim { 502127e670aSJaegeuk Kim struct page *cp_page_1, *cp_page_2 = NULL; 503127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 504127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 505127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 506127e670aSJaegeuk Kim size_t crc_offset; 5077e586fa0SJaegeuk Kim __u32 crc = 0; 508127e670aSJaegeuk Kim 509127e670aSJaegeuk Kim /* Read the 1st cp block in this CP pack */ 510127e670aSJaegeuk Kim cp_page_1 = get_meta_page(sbi, cp_addr); 511127e670aSJaegeuk Kim 512127e670aSJaegeuk Kim /* get the version number */ 513127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); 514127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 515127e670aSJaegeuk Kim if (crc_offset >= blk_size) 516127e670aSJaegeuk Kim goto invalid_cp1; 517127e670aSJaegeuk Kim 5187e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 519127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 520127e670aSJaegeuk Kim goto invalid_cp1; 521127e670aSJaegeuk Kim 522d71b5564SJaegeuk Kim pre_version = cur_cp_version(cp_block); 523127e670aSJaegeuk Kim 524127e670aSJaegeuk Kim /* Read the 2nd cp block in this CP pack */ 52525ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 526127e670aSJaegeuk Kim cp_page_2 = get_meta_page(sbi, cp_addr); 527127e670aSJaegeuk Kim 528127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); 529127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 530127e670aSJaegeuk Kim if (crc_offset >= blk_size) 531127e670aSJaegeuk Kim goto invalid_cp2; 532127e670aSJaegeuk Kim 5337e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 534127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 535127e670aSJaegeuk Kim goto invalid_cp2; 536127e670aSJaegeuk Kim 537d71b5564SJaegeuk Kim cur_version = cur_cp_version(cp_block); 538127e670aSJaegeuk Kim 539127e670aSJaegeuk Kim if (cur_version == pre_version) { 540127e670aSJaegeuk Kim *version = cur_version; 541127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 542127e670aSJaegeuk Kim return cp_page_1; 543127e670aSJaegeuk Kim } 544127e670aSJaegeuk Kim invalid_cp2: 545127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 546127e670aSJaegeuk Kim invalid_cp1: 547127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 548127e670aSJaegeuk Kim return NULL; 549127e670aSJaegeuk Kim } 550127e670aSJaegeuk Kim 551127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 552127e670aSJaegeuk Kim { 553127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 554127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 555127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 556127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 557127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 558127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 5591dbe4152SChangman Lee unsigned int cp_blks = 1 + le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 5601dbe4152SChangman Lee block_t cp_blk_no; 5611dbe4152SChangman Lee int i; 562127e670aSJaegeuk Kim 5631dbe4152SChangman Lee sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL); 564127e670aSJaegeuk Kim if (!sbi->ckpt) 565127e670aSJaegeuk Kim return -ENOMEM; 566127e670aSJaegeuk Kim /* 567127e670aSJaegeuk Kim * Finding out valid cp block involves read both 568127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 569127e670aSJaegeuk Kim */ 570127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 571127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 572127e670aSJaegeuk Kim 573127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 574f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) << 575f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg); 576127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 577127e670aSJaegeuk Kim 578127e670aSJaegeuk Kim if (cp1 && cp2) { 579127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 580127e670aSJaegeuk Kim cur_page = cp2; 581127e670aSJaegeuk Kim else 582127e670aSJaegeuk Kim cur_page = cp1; 583127e670aSJaegeuk Kim } else if (cp1) { 584127e670aSJaegeuk Kim cur_page = cp1; 585127e670aSJaegeuk Kim } else if (cp2) { 586127e670aSJaegeuk Kim cur_page = cp2; 587127e670aSJaegeuk Kim } else { 588127e670aSJaegeuk Kim goto fail_no_cp; 589127e670aSJaegeuk Kim } 590127e670aSJaegeuk Kim 591127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 592127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 593127e670aSJaegeuk Kim 5941dbe4152SChangman Lee if (cp_blks <= 1) 5951dbe4152SChangman Lee goto done; 5961dbe4152SChangman Lee 5971dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); 5981dbe4152SChangman Lee if (cur_page == cp2) 5991dbe4152SChangman Lee cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 6001dbe4152SChangman Lee 6011dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) { 6021dbe4152SChangman Lee void *sit_bitmap_ptr; 6031dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt; 6041dbe4152SChangman Lee 6051dbe4152SChangman Lee cur_page = get_meta_page(sbi, cp_blk_no + i); 6061dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page); 6071dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); 6081dbe4152SChangman Lee f2fs_put_page(cur_page, 1); 6091dbe4152SChangman Lee } 6101dbe4152SChangman Lee done: 611127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 612127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 613127e670aSJaegeuk Kim return 0; 614127e670aSJaegeuk Kim 615127e670aSJaegeuk Kim fail_no_cp: 616127e670aSJaegeuk Kim kfree(sbi->ckpt); 617127e670aSJaegeuk Kim return -EINVAL; 618127e670aSJaegeuk Kim } 619127e670aSJaegeuk Kim 6205deb8267SJaegeuk Kim static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new) 621127e670aSJaegeuk Kim { 622127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6232d7b822aSChao Yu 624ed57c27fSJaegeuk Kim if (is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) 6255deb8267SJaegeuk Kim return -EEXIST; 6262d7b822aSChao Yu 627ed57c27fSJaegeuk Kim set_inode_flag(F2FS_I(inode), FI_DIRTY_DIR); 628ed57c27fSJaegeuk Kim F2FS_I(inode)->dirty_dir = new; 629ed57c27fSJaegeuk Kim list_add_tail(&new->list, &sbi->dir_inode_list); 630dcdfff65SJaegeuk Kim stat_inc_dirty_dir(sbi); 6315deb8267SJaegeuk Kim return 0; 6325deb8267SJaegeuk Kim } 6335deb8267SJaegeuk Kim 6345deb8267SJaegeuk Kim void set_dirty_dir_page(struct inode *inode, struct page *page) 6355deb8267SJaegeuk Kim { 6365deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6375deb8267SJaegeuk Kim struct dir_inode_entry *new; 638cf0ee0f0SChao Yu int ret = 0; 6395deb8267SJaegeuk Kim 640127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 641127e670aSJaegeuk Kim return; 6427bd59381SGu Zheng 6437bd59381SGu Zheng new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 644127e670aSJaegeuk Kim new->inode = inode; 645127e670aSJaegeuk Kim INIT_LIST_HEAD(&new->list); 646127e670aSJaegeuk Kim 647127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 648cf0ee0f0SChao Yu ret = __add_dirty_inode(inode, new); 649127e670aSJaegeuk Kim inode_inc_dirty_dents(inode); 650127e670aSJaegeuk Kim SetPagePrivate(page); 6515deb8267SJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 652cf0ee0f0SChao Yu 653cf0ee0f0SChao Yu if (ret) 654cf0ee0f0SChao Yu kmem_cache_free(inode_entry_slab, new); 6555deb8267SJaegeuk Kim } 656127e670aSJaegeuk Kim 6575deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode) 6585deb8267SJaegeuk Kim { 6595deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6607bd59381SGu Zheng struct dir_inode_entry *new = 6617bd59381SGu Zheng f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 662cf0ee0f0SChao Yu int ret = 0; 6637bd59381SGu Zheng 6645deb8267SJaegeuk Kim new->inode = inode; 6655deb8267SJaegeuk Kim INIT_LIST_HEAD(&new->list); 6665deb8267SJaegeuk Kim 6675deb8267SJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 668cf0ee0f0SChao Yu ret = __add_dirty_inode(inode, new); 669127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 670cf0ee0f0SChao Yu 671cf0ee0f0SChao Yu if (ret) 672cf0ee0f0SChao Yu kmem_cache_free(inode_entry_slab, new); 673127e670aSJaegeuk Kim } 674127e670aSJaegeuk Kim 675127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode) 676127e670aSJaegeuk Kim { 677127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 6782d7b822aSChao Yu struct dir_inode_entry *entry; 679127e670aSJaegeuk Kim 680127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 681127e670aSJaegeuk Kim return; 682127e670aSJaegeuk Kim 683127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 684ed57c27fSJaegeuk Kim if (get_dirty_dents(inode) || 685ed57c27fSJaegeuk Kim !is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) { 6863b10b1fdSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 6873b10b1fdSJaegeuk Kim return; 6883b10b1fdSJaegeuk Kim } 689127e670aSJaegeuk Kim 690ed57c27fSJaegeuk Kim entry = F2FS_I(inode)->dirty_dir; 691127e670aSJaegeuk Kim list_del(&entry->list); 692ed57c27fSJaegeuk Kim F2FS_I(inode)->dirty_dir = NULL; 693ed57c27fSJaegeuk Kim clear_inode_flag(F2FS_I(inode), FI_DIRTY_DIR); 694dcdfff65SJaegeuk Kim stat_dec_dirty_dir(sbi); 695cf0ee0f0SChao Yu spin_unlock(&sbi->dir_inode_lock); 696cf0ee0f0SChao Yu kmem_cache_free(inode_entry_slab, entry); 69774d0b917SJaegeuk Kim 69874d0b917SJaegeuk Kim /* Only from the recovery routine */ 699afc3eda2SJaegeuk Kim if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) { 700afc3eda2SJaegeuk Kim clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT); 70174d0b917SJaegeuk Kim iput(inode); 70274d0b917SJaegeuk Kim } 703afc3eda2SJaegeuk Kim } 70474d0b917SJaegeuk Kim 705127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi) 706127e670aSJaegeuk Kim { 707ce3b7d80SGu Zheng struct list_head *head; 708127e670aSJaegeuk Kim struct dir_inode_entry *entry; 709127e670aSJaegeuk Kim struct inode *inode; 710127e670aSJaegeuk Kim retry: 711127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 712ce3b7d80SGu Zheng 713ce3b7d80SGu Zheng head = &sbi->dir_inode_list; 714127e670aSJaegeuk Kim if (list_empty(head)) { 715127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 716127e670aSJaegeuk Kim return; 717127e670aSJaegeuk Kim } 718127e670aSJaegeuk Kim entry = list_entry(head->next, struct dir_inode_entry, list); 719127e670aSJaegeuk Kim inode = igrab(entry->inode); 720127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 721127e670aSJaegeuk Kim if (inode) { 72287d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping); 723127e670aSJaegeuk Kim iput(inode); 724127e670aSJaegeuk Kim } else { 725127e670aSJaegeuk Kim /* 726127e670aSJaegeuk Kim * We should submit bio, since it exists several 727127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 728127e670aSJaegeuk Kim */ 729458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 730127e670aSJaegeuk Kim } 731127e670aSJaegeuk Kim goto retry; 732127e670aSJaegeuk Kim } 733127e670aSJaegeuk Kim 7340a8165d7SJaegeuk Kim /* 735127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 736127e670aSJaegeuk Kim */ 737cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi) 738127e670aSJaegeuk Kim { 739127e670aSJaegeuk Kim struct writeback_control wbc = { 740127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 741127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 742127e670aSJaegeuk Kim .for_reclaim = 0, 743127e670aSJaegeuk Kim }; 744c718379bSJaegeuk Kim struct blk_plug plug; 745cf779cabSJaegeuk Kim int err = 0; 746c718379bSJaegeuk Kim 747c718379bSJaegeuk Kim blk_start_plug(&plug); 748c718379bSJaegeuk Kim 74939936837SJaegeuk Kim retry_flush_dents: 750e479556bSGu Zheng f2fs_lock_all(sbi); 751127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 752127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 753e479556bSGu Zheng f2fs_unlock_all(sbi); 75439936837SJaegeuk Kim sync_dirty_dir_inodes(sbi); 755cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) { 756cf779cabSJaegeuk Kim err = -EIO; 757cf779cabSJaegeuk Kim goto out; 758cf779cabSJaegeuk Kim } 75939936837SJaegeuk Kim goto retry_flush_dents; 760127e670aSJaegeuk Kim } 761127e670aSJaegeuk Kim 762127e670aSJaegeuk Kim /* 763e1c42045Sarter97 * POR: we should ensure that there are no dirty node pages 764127e670aSJaegeuk Kim * until finishing nat/sit flush. 765127e670aSJaegeuk Kim */ 76639936837SJaegeuk Kim retry_flush_nodes: 767b3582c68SChao Yu down_write(&sbi->node_write); 768127e670aSJaegeuk Kim 769127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 770b3582c68SChao Yu up_write(&sbi->node_write); 77139936837SJaegeuk Kim sync_node_pages(sbi, 0, &wbc); 772cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) { 773cf779cabSJaegeuk Kim f2fs_unlock_all(sbi); 774cf779cabSJaegeuk Kim err = -EIO; 775cf779cabSJaegeuk Kim goto out; 776cf779cabSJaegeuk Kim } 77739936837SJaegeuk Kim goto retry_flush_nodes; 778127e670aSJaegeuk Kim } 779cf779cabSJaegeuk Kim out: 780c718379bSJaegeuk Kim blk_finish_plug(&plug); 781cf779cabSJaegeuk Kim return err; 782127e670aSJaegeuk Kim } 783127e670aSJaegeuk Kim 784127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 785127e670aSJaegeuk Kim { 786b3582c68SChao Yu up_write(&sbi->node_write); 787e479556bSGu Zheng f2fs_unlock_all(sbi); 788127e670aSJaegeuk Kim } 789127e670aSJaegeuk Kim 790fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 791fb51b5efSChangman Lee { 792fb51b5efSChangman Lee DEFINE_WAIT(wait); 793fb51b5efSChangman Lee 794fb51b5efSChangman Lee for (;;) { 795fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 796fb51b5efSChangman Lee 797fb51b5efSChangman Lee if (!get_pages(sbi, F2FS_WRITEBACK)) 798fb51b5efSChangman Lee break; 799fb51b5efSChangman Lee 800fb51b5efSChangman Lee io_schedule(); 801fb51b5efSChangman Lee } 802fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 803fb51b5efSChangman Lee } 804fb51b5efSChangman Lee 805127e670aSJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 806127e670aSJaegeuk Kim { 807127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 808cf2271e7SJaegeuk Kim struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE); 809127e670aSJaegeuk Kim nid_t last_nid = 0; 810127e670aSJaegeuk Kim block_t start_blk; 811127e670aSJaegeuk Kim struct page *cp_page; 812127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 8137e586fa0SJaegeuk Kim __u32 crc32 = 0; 814127e670aSJaegeuk Kim void *kaddr; 815127e670aSJaegeuk Kim int i; 8161dbe4152SChangman Lee int cp_payload_blks = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 817127e670aSJaegeuk Kim 8181e87a78dSJaegeuk Kim /* 8191e87a78dSJaegeuk Kim * This avoids to conduct wrong roll-forward operations and uses 8201e87a78dSJaegeuk Kim * metapages, so should be called prior to sync_meta_pages below. 8211e87a78dSJaegeuk Kim */ 822cf2271e7SJaegeuk Kim discard_next_dnode(sbi, NEXT_FREE_BLKADDR(sbi, curseg)); 823127e670aSJaegeuk Kim 824127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 825cf779cabSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) { 826127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 827cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 828cf779cabSJaegeuk Kim return; 829cf779cabSJaegeuk Kim } 830127e670aSJaegeuk Kim 831127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 832127e670aSJaegeuk Kim 833127e670aSJaegeuk Kim /* 834127e670aSJaegeuk Kim * modify checkpoint 835127e670aSJaegeuk Kim * version number is already updated 836127e670aSJaegeuk Kim */ 837127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 838127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 839127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 840*b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 841127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 842127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 843127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 844127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 845127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 846127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 847127e670aSJaegeuk Kim } 848*b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 849127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 850127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 851127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 852127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 853127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 854127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 855127e670aSJaegeuk Kim } 856127e670aSJaegeuk Kim 857127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 858127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 859127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 860127e670aSJaegeuk Kim 861127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 862127e670aSJaegeuk Kim data_sum_blocks = npages_for_summary_flush(sbi); 863*b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE) 86425ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 865127e670aSJaegeuk Kim else 86625ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 867127e670aSJaegeuk Kim 868*b5b82205SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(sbi->n_orphans); 8691dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + 8701dbe4152SChangman Lee orphan_blocks); 871127e670aSJaegeuk Kim 872127e670aSJaegeuk Kim if (is_umount) { 87325ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 874*b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+ 8751dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 8761dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE); 877127e670aSJaegeuk Kim } else { 87825ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 879*b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + 8801dbe4152SChangman Lee cp_payload_blks + data_sum_blocks + 8811dbe4152SChangman Lee orphan_blocks); 882127e670aSJaegeuk Kim } 883127e670aSJaegeuk Kim 884127e670aSJaegeuk Kim if (sbi->n_orphans) 88525ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 886127e670aSJaegeuk Kim else 88725ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 888127e670aSJaegeuk Kim 889127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 890127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 891127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 892127e670aSJaegeuk Kim 893127e670aSJaegeuk Kim crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); 8947e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 8957e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 896127e670aSJaegeuk Kim = cpu_to_le32(crc32); 897127e670aSJaegeuk Kim 898127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi); 899127e670aSJaegeuk Kim 900127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 901127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk++); 902127e670aSJaegeuk Kim kaddr = page_address(cp_page); 903127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 904127e670aSJaegeuk Kim set_page_dirty(cp_page); 905127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 906127e670aSJaegeuk Kim 9071dbe4152SChangman Lee for (i = 1; i < 1 + cp_payload_blks; i++) { 9081dbe4152SChangman Lee cp_page = grab_meta_page(sbi, start_blk++); 9091dbe4152SChangman Lee kaddr = page_address(cp_page); 9101dbe4152SChangman Lee memcpy(kaddr, (char *)ckpt + i * F2FS_BLKSIZE, 9111dbe4152SChangman Lee (1 << sbi->log_blocksize)); 9121dbe4152SChangman Lee set_page_dirty(cp_page); 9131dbe4152SChangman Lee f2fs_put_page(cp_page, 1); 9141dbe4152SChangman Lee } 9151dbe4152SChangman Lee 916127e670aSJaegeuk Kim if (sbi->n_orphans) { 917127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 918127e670aSJaegeuk Kim start_blk += orphan_blocks; 919127e670aSJaegeuk Kim } 920127e670aSJaegeuk Kim 921127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 922127e670aSJaegeuk Kim start_blk += data_sum_blocks; 923127e670aSJaegeuk Kim if (is_umount) { 924127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 925127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 926127e670aSJaegeuk Kim } 927127e670aSJaegeuk Kim 928127e670aSJaegeuk Kim /* writeout checkpoint block */ 929127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk); 930127e670aSJaegeuk Kim kaddr = page_address(cp_page); 931127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 932127e670aSJaegeuk Kim set_page_dirty(cp_page); 933127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 934127e670aSJaegeuk Kim 935127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 936fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 937127e670aSJaegeuk Kim 938cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 939cf779cabSJaegeuk Kim return; 940cf779cabSJaegeuk Kim 9414ef51a8fSJaegeuk Kim filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX); 9429df27d98SGu Zheng filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX); 943127e670aSJaegeuk Kim 944127e670aSJaegeuk Kim /* update user_block_counts */ 945127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 946127e670aSJaegeuk Kim sbi->alloc_valid_block_count = 0; 947127e670aSJaegeuk Kim 948127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 949127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 950127e670aSJaegeuk Kim 951fff04f90SJaegeuk Kim release_dirty_inode(sbi); 952cf779cabSJaegeuk Kim 953cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 954cf779cabSJaegeuk Kim return; 955cf779cabSJaegeuk Kim 956cf779cabSJaegeuk Kim clear_prefree_segments(sbi); 957127e670aSJaegeuk Kim F2FS_RESET_SB_DIRT(sbi); 958127e670aSJaegeuk Kim } 959127e670aSJaegeuk Kim 9600a8165d7SJaegeuk Kim /* 961e1c42045Sarter97 * We guarantee that this checkpoint procedure will not fail. 962127e670aSJaegeuk Kim */ 96343727527SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 964127e670aSJaegeuk Kim { 965127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 966127e670aSJaegeuk Kim unsigned long long ckpt_ver; 967127e670aSJaegeuk Kim 9682af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops"); 9692af4bd6cSNamjae Jeon 970127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 9718501017eSJaegeuk Kim 9728501017eSJaegeuk Kim if (!sbi->s_dirty) 9738501017eSJaegeuk Kim goto out; 974cf779cabSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi))) 975cf779cabSJaegeuk Kim goto out; 976cf779cabSJaegeuk Kim if (block_operations(sbi)) 977cf779cabSJaegeuk Kim goto out; 978127e670aSJaegeuk Kim 9792af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops"); 9802af4bd6cSNamjae Jeon 981458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, DATA, WRITE); 982458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, NODE, WRITE); 983458e6197SJaegeuk Kim f2fs_submit_merged_bio(sbi, META, WRITE); 984127e670aSJaegeuk Kim 985127e670aSJaegeuk Kim /* 986127e670aSJaegeuk Kim * update checkpoint pack index 987127e670aSJaegeuk Kim * Increase the version number so that 988127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 989127e670aSJaegeuk Kim */ 990d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 991127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 992127e670aSJaegeuk Kim 993127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 994127e670aSJaegeuk Kim flush_nat_entries(sbi); 995127e670aSJaegeuk Kim flush_sit_entries(sbi); 996127e670aSJaegeuk Kim 997127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 998127e670aSJaegeuk Kim do_checkpoint(sbi, is_umount); 999127e670aSJaegeuk Kim 1000127e670aSJaegeuk Kim unblock_operations(sbi); 1001942e0be6SChangman Lee stat_inc_cp_count(sbi->stat_info); 10028501017eSJaegeuk Kim out: 10038501017eSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 10042af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint"); 1005127e670aSJaegeuk Kim } 1006127e670aSJaegeuk Kim 10076451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi) 1008127e670aSJaegeuk Kim { 10096451e041SJaegeuk Kim int i; 10106451e041SJaegeuk Kim 10116451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) { 101239efac41SJaegeuk Kim INIT_RADIX_TREE(&sbi->ino_root[i], GFP_ATOMIC); 10136451e041SJaegeuk Kim spin_lock_init(&sbi->ino_lock[i]); 10146451e041SJaegeuk Kim INIT_LIST_HEAD(&sbi->ino_list[i]); 10156451e041SJaegeuk Kim } 10166451e041SJaegeuk Kim 10170d47c1adSGu Zheng /* 10180d47c1adSGu Zheng * considering 512 blocks in a segment 8 blocks are needed for cp 10190d47c1adSGu Zheng * and log segment summaries. Remaining blocks are used to keep 10200d47c1adSGu Zheng * orphan entries with the limitation one reserved segment 10210d47c1adSGu Zheng * for cp pack we can have max 1020*504 orphan entries 10220d47c1adSGu Zheng */ 10236451e041SJaegeuk Kim sbi->n_orphans = 0; 1024*b5b82205SChao Yu sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - 1025*b5b82205SChao Yu NR_CURSEG_TYPE) * F2FS_ORPHANS_PER_BLOCK; 1026127e670aSJaegeuk Kim } 1027127e670aSJaegeuk Kim 10286e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 1029127e670aSJaegeuk Kim { 10306451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", 10316451e041SJaegeuk Kim sizeof(struct ino_entry)); 10326451e041SJaegeuk Kim if (!ino_entry_slab) 1033127e670aSJaegeuk Kim return -ENOMEM; 1034127e670aSJaegeuk Kim inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry", 1035e8512d2eSGu Zheng sizeof(struct dir_inode_entry)); 10366bacf52fSJaegeuk Kim if (!inode_entry_slab) { 10376451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1038127e670aSJaegeuk Kim return -ENOMEM; 1039127e670aSJaegeuk Kim } 1040127e670aSJaegeuk Kim return 0; 1041127e670aSJaegeuk Kim } 1042127e670aSJaegeuk Kim 1043127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 1044127e670aSJaegeuk Kim { 10456451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab); 1046127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 1047127e670aSJaegeuk Kim } 1048