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 25127e670aSJaegeuk Kim static struct kmem_cache *orphan_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 { 33127e670aSJaegeuk Kim struct address_space *mapping = sbi->meta_inode->i_mapping; 34127e670aSJaegeuk Kim struct page *page = NULL; 35127e670aSJaegeuk Kim repeat: 36127e670aSJaegeuk Kim page = grab_cache_page(mapping, index); 37127e670aSJaegeuk Kim if (!page) { 38127e670aSJaegeuk Kim cond_resched(); 39127e670aSJaegeuk Kim goto repeat; 40127e670aSJaegeuk Kim } 41127e670aSJaegeuk Kim 42127e670aSJaegeuk Kim /* We wait writeback only inside grab_meta_page() */ 43127e670aSJaegeuk Kim wait_on_page_writeback(page); 44127e670aSJaegeuk Kim SetPageUptodate(page); 45127e670aSJaegeuk Kim return page; 46127e670aSJaegeuk Kim } 47127e670aSJaegeuk Kim 480a8165d7SJaegeuk Kim /* 49127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 50127e670aSJaegeuk Kim */ 51127e670aSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 52127e670aSJaegeuk Kim { 53127e670aSJaegeuk Kim struct address_space *mapping = sbi->meta_inode->i_mapping; 54127e670aSJaegeuk Kim struct page *page; 55127e670aSJaegeuk Kim repeat: 56127e670aSJaegeuk Kim page = grab_cache_page(mapping, index); 57127e670aSJaegeuk Kim if (!page) { 58127e670aSJaegeuk Kim cond_resched(); 59127e670aSJaegeuk Kim goto repeat; 60127e670aSJaegeuk Kim } 61393ff91fSJaegeuk Kim if (PageUptodate(page)) 62393ff91fSJaegeuk Kim goto out; 63127e670aSJaegeuk Kim 64393ff91fSJaegeuk Kim if (f2fs_readpage(sbi, page, index, READ_SYNC)) 65393ff91fSJaegeuk Kim goto repeat; 66393ff91fSJaegeuk Kim 67393ff91fSJaegeuk Kim lock_page(page); 68afcb7ca0SJaegeuk Kim if (page->mapping != mapping) { 69afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1); 70afcb7ca0SJaegeuk Kim goto repeat; 71afcb7ca0SJaegeuk Kim } 72393ff91fSJaegeuk Kim out: 73393ff91fSJaegeuk Kim mark_page_accessed(page); 74127e670aSJaegeuk Kim return page; 75127e670aSJaegeuk Kim } 76127e670aSJaegeuk Kim 77127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page, 78127e670aSJaegeuk Kim struct writeback_control *wbc) 79127e670aSJaegeuk Kim { 80127e670aSJaegeuk Kim struct inode *inode = page->mapping->host; 81127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 82577e3495SJaegeuk Kim 83577e3495SJaegeuk Kim /* Should not write any meta pages, if any IO error was occurred */ 8487a9bd26SJaegeuk Kim if (wbc->for_reclaim || sbi->por_doing || 85577e3495SJaegeuk Kim is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG)) { 86577e3495SJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 87577e3495SJaegeuk Kim wbc->pages_skipped++; 88577e3495SJaegeuk Kim set_page_dirty(page); 89577e3495SJaegeuk Kim return AOP_WRITEPAGE_ACTIVATE; 90577e3495SJaegeuk Kim } 91127e670aSJaegeuk Kim 92127e670aSJaegeuk Kim wait_on_page_writeback(page); 93127e670aSJaegeuk Kim 94577e3495SJaegeuk Kim write_meta_page(sbi, page); 95127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 96127e670aSJaegeuk Kim unlock_page(page); 97577e3495SJaegeuk Kim return 0; 98127e670aSJaegeuk Kim } 99127e670aSJaegeuk Kim 100127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 101127e670aSJaegeuk Kim struct writeback_control *wbc) 102127e670aSJaegeuk Kim { 103127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 104127e670aSJaegeuk Kim struct block_device *bdev = sbi->sb->s_bdev; 105127e670aSJaegeuk Kim long written; 106127e670aSJaegeuk Kim 107127e670aSJaegeuk Kim if (wbc->for_kupdate) 108127e670aSJaegeuk Kim return 0; 109127e670aSJaegeuk Kim 110127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_META) == 0) 111127e670aSJaegeuk Kim return 0; 112127e670aSJaegeuk Kim 113127e670aSJaegeuk Kim /* if mounting is failed, skip writing node pages */ 114127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 115127e670aSJaegeuk Kim written = sync_meta_pages(sbi, META, bio_get_nr_vecs(bdev)); 116127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 117127e670aSJaegeuk Kim wbc->nr_to_write -= written; 118127e670aSJaegeuk Kim return 0; 119127e670aSJaegeuk Kim } 120127e670aSJaegeuk Kim 121127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 122127e670aSJaegeuk Kim long nr_to_write) 123127e670aSJaegeuk Kim { 124127e670aSJaegeuk Kim struct address_space *mapping = sbi->meta_inode->i_mapping; 125127e670aSJaegeuk Kim pgoff_t index = 0, end = LONG_MAX; 126127e670aSJaegeuk Kim struct pagevec pvec; 127127e670aSJaegeuk Kim long nwritten = 0; 128127e670aSJaegeuk Kim struct writeback_control wbc = { 129127e670aSJaegeuk Kim .for_reclaim = 0, 130127e670aSJaegeuk Kim }; 131127e670aSJaegeuk Kim 132127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 133127e670aSJaegeuk Kim 134127e670aSJaegeuk Kim while (index <= end) { 135127e670aSJaegeuk Kim int i, nr_pages; 136127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 137127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 138127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 139127e670aSJaegeuk Kim if (nr_pages == 0) 140127e670aSJaegeuk Kim break; 141127e670aSJaegeuk Kim 142127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 143127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 144127e670aSJaegeuk Kim lock_page(page); 1455d56b671SJaegeuk Kim f2fs_bug_on(page->mapping != mapping); 1465d56b671SJaegeuk Kim f2fs_bug_on(!PageDirty(page)); 147127e670aSJaegeuk Kim clear_page_dirty_for_io(page); 148577e3495SJaegeuk Kim if (f2fs_write_meta_page(page, &wbc)) { 149577e3495SJaegeuk Kim unlock_page(page); 150577e3495SJaegeuk Kim break; 151577e3495SJaegeuk Kim } 152127e670aSJaegeuk Kim if (nwritten++ >= nr_to_write) 153127e670aSJaegeuk Kim break; 154127e670aSJaegeuk Kim } 155127e670aSJaegeuk Kim pagevec_release(&pvec); 156127e670aSJaegeuk Kim cond_resched(); 157127e670aSJaegeuk Kim } 158127e670aSJaegeuk Kim 159127e670aSJaegeuk Kim if (nwritten) 160127e670aSJaegeuk Kim f2fs_submit_bio(sbi, type, nr_to_write == LONG_MAX); 161127e670aSJaegeuk Kim 162127e670aSJaegeuk Kim return nwritten; 163127e670aSJaegeuk Kim } 164127e670aSJaegeuk Kim 165127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 166127e670aSJaegeuk Kim { 167127e670aSJaegeuk Kim struct address_space *mapping = page->mapping; 168127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 169127e670aSJaegeuk Kim 17026c6b887SJaegeuk Kim trace_f2fs_set_page_dirty(page, META); 17126c6b887SJaegeuk Kim 172127e670aSJaegeuk Kim SetPageUptodate(page); 173127e670aSJaegeuk Kim if (!PageDirty(page)) { 174127e670aSJaegeuk Kim __set_page_dirty_nobuffers(page); 175127e670aSJaegeuk Kim inc_page_count(sbi, F2FS_DIRTY_META); 176127e670aSJaegeuk Kim return 1; 177127e670aSJaegeuk Kim } 178127e670aSJaegeuk Kim return 0; 179127e670aSJaegeuk Kim } 180127e670aSJaegeuk Kim 181127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 182127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 183127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 184127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 185127e670aSJaegeuk Kim }; 186127e670aSJaegeuk Kim 187cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi) 188127e670aSJaegeuk Kim { 189127e670aSJaegeuk Kim unsigned int max_orphans; 190127e670aSJaegeuk Kim int err = 0; 191127e670aSJaegeuk Kim 192127e670aSJaegeuk Kim /* 193127e670aSJaegeuk Kim * considering 512 blocks in a segment 5 blocks are needed for cp 194127e670aSJaegeuk Kim * and log segment summaries. Remaining blocks are used to keep 195127e670aSJaegeuk Kim * orphan entries with the limitation one reserved segment 196127e670aSJaegeuk Kim * for cp pack we can have max 1020*507 orphan entries 197127e670aSJaegeuk Kim */ 198127e670aSJaegeuk Kim max_orphans = (sbi->blocks_per_seg - 5) * F2FS_ORPHANS_PER_BLOCK; 199127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 200127e670aSJaegeuk Kim if (sbi->n_orphans >= max_orphans) 201127e670aSJaegeuk Kim err = -ENOSPC; 202cbd56e7dSJaegeuk Kim else 203cbd56e7dSJaegeuk Kim sbi->n_orphans++; 204127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 205127e670aSJaegeuk Kim return err; 206127e670aSJaegeuk Kim } 207127e670aSJaegeuk Kim 208cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi) 209cbd56e7dSJaegeuk Kim { 210cbd56e7dSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 2115d56b671SJaegeuk Kim f2fs_bug_on(sbi->n_orphans == 0); 212cbd56e7dSJaegeuk Kim sbi->n_orphans--; 213cbd56e7dSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 214cbd56e7dSJaegeuk Kim } 215cbd56e7dSJaegeuk Kim 216127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 217127e670aSJaegeuk Kim { 218127e670aSJaegeuk Kim struct list_head *head, *this; 219127e670aSJaegeuk Kim struct orphan_inode_entry *new = NULL, *orphan = NULL; 220127e670aSJaegeuk Kim 221127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 222127e670aSJaegeuk Kim head = &sbi->orphan_inode_list; 223127e670aSJaegeuk Kim list_for_each(this, head) { 224127e670aSJaegeuk Kim orphan = list_entry(this, struct orphan_inode_entry, list); 225127e670aSJaegeuk Kim if (orphan->ino == ino) 226127e670aSJaegeuk Kim goto out; 227127e670aSJaegeuk Kim if (orphan->ino > ino) 228127e670aSJaegeuk Kim break; 229127e670aSJaegeuk Kim orphan = NULL; 230127e670aSJaegeuk Kim } 2317bd59381SGu Zheng 2327bd59381SGu Zheng new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC); 233127e670aSJaegeuk Kim new->ino = ino; 234127e670aSJaegeuk Kim 235127e670aSJaegeuk Kim /* add new_oentry into list which is sorted by inode number */ 236a2617dc6Smajianpeng if (orphan) 237a2617dc6Smajianpeng list_add(&new->list, this->prev); 238127e670aSJaegeuk Kim else 239127e670aSJaegeuk Kim list_add_tail(&new->list, head); 240127e670aSJaegeuk Kim out: 241127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 242127e670aSJaegeuk Kim } 243127e670aSJaegeuk Kim 244127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 245127e670aSJaegeuk Kim { 24660ed9a0fSGu Zheng struct list_head *head; 247127e670aSJaegeuk Kim struct orphan_inode_entry *orphan; 248127e670aSJaegeuk Kim 249127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 250127e670aSJaegeuk Kim head = &sbi->orphan_inode_list; 25160ed9a0fSGu Zheng list_for_each_entry(orphan, head, list) { 252127e670aSJaegeuk Kim if (orphan->ino == ino) { 253127e670aSJaegeuk Kim list_del(&orphan->list); 254127e670aSJaegeuk Kim kmem_cache_free(orphan_entry_slab, orphan); 2555d56b671SJaegeuk Kim f2fs_bug_on(sbi->n_orphans == 0); 256127e670aSJaegeuk Kim sbi->n_orphans--; 257127e670aSJaegeuk Kim break; 258127e670aSJaegeuk Kim } 259127e670aSJaegeuk Kim } 260127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 261127e670aSJaegeuk Kim } 262127e670aSJaegeuk Kim 263127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 264127e670aSJaegeuk Kim { 265127e670aSJaegeuk Kim struct inode *inode = f2fs_iget(sbi->sb, ino); 2665d56b671SJaegeuk Kim f2fs_bug_on(IS_ERR(inode)); 267127e670aSJaegeuk Kim clear_nlink(inode); 268127e670aSJaegeuk Kim 269127e670aSJaegeuk Kim /* truncate all the data during iput */ 270127e670aSJaegeuk Kim iput(inode); 271127e670aSJaegeuk Kim } 272127e670aSJaegeuk Kim 273127e670aSJaegeuk Kim int recover_orphan_inodes(struct f2fs_sb_info *sbi) 274127e670aSJaegeuk Kim { 275127e670aSJaegeuk Kim block_t start_blk, orphan_blkaddr, i, j; 276127e670aSJaegeuk Kim 27725ca923bSJaegeuk Kim if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG)) 278127e670aSJaegeuk Kim return 0; 279127e670aSJaegeuk Kim 280aabe5136SHaicheng Li sbi->por_doing = true; 281127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi) + 1; 282127e670aSJaegeuk Kim orphan_blkaddr = __start_sum_addr(sbi) - 1; 283127e670aSJaegeuk Kim 284127e670aSJaegeuk Kim for (i = 0; i < orphan_blkaddr; i++) { 285127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 286127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 287127e670aSJaegeuk Kim 288127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 289127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 290127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 291127e670aSJaegeuk Kim recover_orphan_inode(sbi, ino); 292127e670aSJaegeuk Kim } 293127e670aSJaegeuk Kim f2fs_put_page(page, 1); 294127e670aSJaegeuk Kim } 295127e670aSJaegeuk Kim /* clear Orphan Flag */ 29625ca923bSJaegeuk Kim clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG); 297aabe5136SHaicheng Li sbi->por_doing = false; 298127e670aSJaegeuk Kim return 0; 299127e670aSJaegeuk Kim } 300127e670aSJaegeuk Kim 301127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 302127e670aSJaegeuk Kim { 303*502c6e0bSGu Zheng struct list_head *head; 304127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 305127e670aSJaegeuk Kim struct page *page = NULL; 306127e670aSJaegeuk Kim unsigned int nentries = 0; 307127e670aSJaegeuk Kim unsigned short index = 1; 308127e670aSJaegeuk Kim unsigned short orphan_blocks; 309*502c6e0bSGu Zheng struct orphan_inode_entry *orphan = NULL; 310127e670aSJaegeuk Kim 311127e670aSJaegeuk Kim orphan_blocks = (unsigned short)((sbi->n_orphans + 312127e670aSJaegeuk Kim (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK); 313127e670aSJaegeuk Kim 314127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 315127e670aSJaegeuk Kim head = &sbi->orphan_inode_list; 316127e670aSJaegeuk Kim 317127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 318*502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) { 319*502c6e0bSGu Zheng if (!page) { 320*502c6e0bSGu Zheng page = grab_meta_page(sbi, start_blk); 321*502c6e0bSGu Zheng orphan_blk = 322*502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page); 323*502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk)); 324*502c6e0bSGu Zheng } 325127e670aSJaegeuk Kim 326*502c6e0bSGu Zheng orphan_blk->ino[nentries] = cpu_to_le32(orphan->ino); 327127e670aSJaegeuk Kim 328*502c6e0bSGu Zheng if (nentries++ == F2FS_ORPHANS_PER_BLOCK) { 329127e670aSJaegeuk Kim /* 330127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 331127e670aSJaegeuk Kim * then we need to flush current orphan blocks 332127e670aSJaegeuk Kim * and bring another one in memory 333127e670aSJaegeuk Kim */ 334127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 335127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 336127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 337127e670aSJaegeuk Kim set_page_dirty(page); 338127e670aSJaegeuk Kim f2fs_put_page(page, 1); 339127e670aSJaegeuk Kim index++; 340127e670aSJaegeuk Kim start_blk++; 341127e670aSJaegeuk Kim nentries = 0; 342127e670aSJaegeuk Kim page = NULL; 343127e670aSJaegeuk Kim } 344127e670aSJaegeuk Kim } 345127e670aSJaegeuk Kim 346*502c6e0bSGu Zheng if (page) { 347127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 348127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 349127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 350127e670aSJaegeuk Kim set_page_dirty(page); 351127e670aSJaegeuk Kim f2fs_put_page(page, 1); 352*502c6e0bSGu Zheng } 353*502c6e0bSGu Zheng 354127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 355127e670aSJaegeuk Kim } 356127e670aSJaegeuk Kim 357127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 358127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 359127e670aSJaegeuk Kim { 360127e670aSJaegeuk Kim struct page *cp_page_1, *cp_page_2 = NULL; 361127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 362127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 363127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 364127e670aSJaegeuk Kim size_t crc_offset; 3657e586fa0SJaegeuk Kim __u32 crc = 0; 366127e670aSJaegeuk Kim 367127e670aSJaegeuk Kim /* Read the 1st cp block in this CP pack */ 368127e670aSJaegeuk Kim cp_page_1 = get_meta_page(sbi, cp_addr); 369127e670aSJaegeuk Kim 370127e670aSJaegeuk Kim /* get the version number */ 371127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); 372127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 373127e670aSJaegeuk Kim if (crc_offset >= blk_size) 374127e670aSJaegeuk Kim goto invalid_cp1; 375127e670aSJaegeuk Kim 3767e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 377127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 378127e670aSJaegeuk Kim goto invalid_cp1; 379127e670aSJaegeuk Kim 380d71b5564SJaegeuk Kim pre_version = cur_cp_version(cp_block); 381127e670aSJaegeuk Kim 382127e670aSJaegeuk Kim /* Read the 2nd cp block in this CP pack */ 38325ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 384127e670aSJaegeuk Kim cp_page_2 = get_meta_page(sbi, cp_addr); 385127e670aSJaegeuk Kim 386127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); 387127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 388127e670aSJaegeuk Kim if (crc_offset >= blk_size) 389127e670aSJaegeuk Kim goto invalid_cp2; 390127e670aSJaegeuk Kim 3917e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 392127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 393127e670aSJaegeuk Kim goto invalid_cp2; 394127e670aSJaegeuk Kim 395d71b5564SJaegeuk Kim cur_version = cur_cp_version(cp_block); 396127e670aSJaegeuk Kim 397127e670aSJaegeuk Kim if (cur_version == pre_version) { 398127e670aSJaegeuk Kim *version = cur_version; 399127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 400127e670aSJaegeuk Kim return cp_page_1; 401127e670aSJaegeuk Kim } 402127e670aSJaegeuk Kim invalid_cp2: 403127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 404127e670aSJaegeuk Kim invalid_cp1: 405127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 406127e670aSJaegeuk Kim return NULL; 407127e670aSJaegeuk Kim } 408127e670aSJaegeuk Kim 409127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 410127e670aSJaegeuk Kim { 411127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 412127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 413127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 414127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 415127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 416127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 417127e670aSJaegeuk Kim 418127e670aSJaegeuk Kim sbi->ckpt = kzalloc(blk_size, GFP_KERNEL); 419127e670aSJaegeuk Kim if (!sbi->ckpt) 420127e670aSJaegeuk Kim return -ENOMEM; 421127e670aSJaegeuk Kim /* 422127e670aSJaegeuk Kim * Finding out valid cp block involves read both 423127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 424127e670aSJaegeuk Kim */ 425127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 426127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 427127e670aSJaegeuk Kim 428127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 429127e670aSJaegeuk Kim cp_start_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 430127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 431127e670aSJaegeuk Kim 432127e670aSJaegeuk Kim if (cp1 && cp2) { 433127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 434127e670aSJaegeuk Kim cur_page = cp2; 435127e670aSJaegeuk Kim else 436127e670aSJaegeuk Kim cur_page = cp1; 437127e670aSJaegeuk Kim } else if (cp1) { 438127e670aSJaegeuk Kim cur_page = cp1; 439127e670aSJaegeuk Kim } else if (cp2) { 440127e670aSJaegeuk Kim cur_page = cp2; 441127e670aSJaegeuk Kim } else { 442127e670aSJaegeuk Kim goto fail_no_cp; 443127e670aSJaegeuk Kim } 444127e670aSJaegeuk Kim 445127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 446127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 447127e670aSJaegeuk Kim 448127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 449127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 450127e670aSJaegeuk Kim return 0; 451127e670aSJaegeuk Kim 452127e670aSJaegeuk Kim fail_no_cp: 453127e670aSJaegeuk Kim kfree(sbi->ckpt); 454127e670aSJaegeuk Kim return -EINVAL; 455127e670aSJaegeuk Kim } 456127e670aSJaegeuk Kim 4575deb8267SJaegeuk Kim static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new) 458127e670aSJaegeuk Kim { 459127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 460127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 461127e670aSJaegeuk Kim struct list_head *this; 462127e670aSJaegeuk Kim 4635deb8267SJaegeuk Kim list_for_each(this, head) { 4645deb8267SJaegeuk Kim struct dir_inode_entry *entry; 4655deb8267SJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 4665deb8267SJaegeuk Kim if (entry->inode == inode) 4675deb8267SJaegeuk Kim return -EEXIST; 4685deb8267SJaegeuk Kim } 4695deb8267SJaegeuk Kim list_add_tail(&new->list, head); 470dcdfff65SJaegeuk Kim stat_inc_dirty_dir(sbi); 4715deb8267SJaegeuk Kim return 0; 4725deb8267SJaegeuk Kim } 4735deb8267SJaegeuk Kim 4745deb8267SJaegeuk Kim void set_dirty_dir_page(struct inode *inode, struct page *page) 4755deb8267SJaegeuk Kim { 4765deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 4775deb8267SJaegeuk Kim struct dir_inode_entry *new; 4785deb8267SJaegeuk Kim 479127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 480127e670aSJaegeuk Kim return; 4817bd59381SGu Zheng 4827bd59381SGu Zheng new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 483127e670aSJaegeuk Kim new->inode = inode; 484127e670aSJaegeuk Kim INIT_LIST_HEAD(&new->list); 485127e670aSJaegeuk Kim 486127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 4875deb8267SJaegeuk Kim if (__add_dirty_inode(inode, new)) 488127e670aSJaegeuk Kim kmem_cache_free(inode_entry_slab, new); 489127e670aSJaegeuk Kim 490127e670aSJaegeuk Kim inc_page_count(sbi, F2FS_DIRTY_DENTS); 491127e670aSJaegeuk Kim inode_inc_dirty_dents(inode); 492127e670aSJaegeuk Kim SetPagePrivate(page); 4935deb8267SJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 4945deb8267SJaegeuk Kim } 495127e670aSJaegeuk Kim 4965deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode) 4975deb8267SJaegeuk Kim { 4985deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 4997bd59381SGu Zheng struct dir_inode_entry *new = 5007bd59381SGu Zheng f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 5017bd59381SGu Zheng 5025deb8267SJaegeuk Kim new->inode = inode; 5035deb8267SJaegeuk Kim INIT_LIST_HEAD(&new->list); 5045deb8267SJaegeuk Kim 5055deb8267SJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 5065deb8267SJaegeuk Kim if (__add_dirty_inode(inode, new)) 5075deb8267SJaegeuk Kim kmem_cache_free(inode_entry_slab, new); 508127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 509127e670aSJaegeuk Kim } 510127e670aSJaegeuk Kim 511127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode) 512127e670aSJaegeuk Kim { 513127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 514127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 515127e670aSJaegeuk Kim struct list_head *this; 516127e670aSJaegeuk Kim 517127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 518127e670aSJaegeuk Kim return; 519127e670aSJaegeuk Kim 520127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 5213b10b1fdSJaegeuk Kim if (atomic_read(&F2FS_I(inode)->dirty_dents)) { 5223b10b1fdSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 5233b10b1fdSJaegeuk Kim return; 5243b10b1fdSJaegeuk Kim } 525127e670aSJaegeuk Kim 526127e670aSJaegeuk Kim list_for_each(this, head) { 527127e670aSJaegeuk Kim struct dir_inode_entry *entry; 528127e670aSJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 529127e670aSJaegeuk Kim if (entry->inode == inode) { 530127e670aSJaegeuk Kim list_del(&entry->list); 531127e670aSJaegeuk Kim kmem_cache_free(inode_entry_slab, entry); 532dcdfff65SJaegeuk Kim stat_dec_dirty_dir(sbi); 533127e670aSJaegeuk Kim break; 534127e670aSJaegeuk Kim } 535127e670aSJaegeuk Kim } 536127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 53774d0b917SJaegeuk Kim 53874d0b917SJaegeuk Kim /* Only from the recovery routine */ 539afc3eda2SJaegeuk Kim if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) { 540afc3eda2SJaegeuk Kim clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT); 54174d0b917SJaegeuk Kim iput(inode); 54274d0b917SJaegeuk Kim } 543afc3eda2SJaegeuk Kim } 54474d0b917SJaegeuk Kim 54574d0b917SJaegeuk Kim struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino) 54674d0b917SJaegeuk Kim { 54774d0b917SJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 54874d0b917SJaegeuk Kim struct list_head *this; 54974d0b917SJaegeuk Kim struct inode *inode = NULL; 55074d0b917SJaegeuk Kim 55174d0b917SJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 55274d0b917SJaegeuk Kim list_for_each(this, head) { 55374d0b917SJaegeuk Kim struct dir_inode_entry *entry; 55474d0b917SJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 55574d0b917SJaegeuk Kim if (entry->inode->i_ino == ino) { 55674d0b917SJaegeuk Kim inode = entry->inode; 55774d0b917SJaegeuk Kim break; 55874d0b917SJaegeuk Kim } 55974d0b917SJaegeuk Kim } 56074d0b917SJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 56174d0b917SJaegeuk Kim return inode; 562127e670aSJaegeuk Kim } 563127e670aSJaegeuk Kim 564127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi) 565127e670aSJaegeuk Kim { 566127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 567127e670aSJaegeuk Kim struct dir_inode_entry *entry; 568127e670aSJaegeuk Kim struct inode *inode; 569127e670aSJaegeuk Kim retry: 570127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 571127e670aSJaegeuk Kim if (list_empty(head)) { 572127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 573127e670aSJaegeuk Kim return; 574127e670aSJaegeuk Kim } 575127e670aSJaegeuk Kim entry = list_entry(head->next, struct dir_inode_entry, list); 576127e670aSJaegeuk Kim inode = igrab(entry->inode); 577127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 578127e670aSJaegeuk Kim if (inode) { 579127e670aSJaegeuk Kim filemap_flush(inode->i_mapping); 580127e670aSJaegeuk Kim iput(inode); 581127e670aSJaegeuk Kim } else { 582127e670aSJaegeuk Kim /* 583127e670aSJaegeuk Kim * We should submit bio, since it exists several 584127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 585127e670aSJaegeuk Kim */ 586127e670aSJaegeuk Kim f2fs_submit_bio(sbi, DATA, true); 587127e670aSJaegeuk Kim } 588127e670aSJaegeuk Kim goto retry; 589127e670aSJaegeuk Kim } 590127e670aSJaegeuk Kim 5910a8165d7SJaegeuk Kim /* 592127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 593127e670aSJaegeuk Kim */ 59443727527SJaegeuk Kim static void block_operations(struct f2fs_sb_info *sbi) 595127e670aSJaegeuk Kim { 596127e670aSJaegeuk Kim struct writeback_control wbc = { 597127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 598127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 599127e670aSJaegeuk Kim .for_reclaim = 0, 600127e670aSJaegeuk Kim }; 601c718379bSJaegeuk Kim struct blk_plug plug; 602c718379bSJaegeuk Kim 603c718379bSJaegeuk Kim blk_start_plug(&plug); 604c718379bSJaegeuk Kim 60539936837SJaegeuk Kim retry_flush_dents: 606e479556bSGu Zheng f2fs_lock_all(sbi); 607127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 608127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 609e479556bSGu Zheng f2fs_unlock_all(sbi); 61039936837SJaegeuk Kim sync_dirty_dir_inodes(sbi); 61139936837SJaegeuk Kim goto retry_flush_dents; 612127e670aSJaegeuk Kim } 613127e670aSJaegeuk Kim 614127e670aSJaegeuk Kim /* 615127e670aSJaegeuk Kim * POR: we should ensure that there is no dirty node pages 616127e670aSJaegeuk Kim * until finishing nat/sit flush. 617127e670aSJaegeuk Kim */ 61839936837SJaegeuk Kim retry_flush_nodes: 61939936837SJaegeuk Kim mutex_lock(&sbi->node_write); 620127e670aSJaegeuk Kim 621127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 62239936837SJaegeuk Kim mutex_unlock(&sbi->node_write); 62339936837SJaegeuk Kim sync_node_pages(sbi, 0, &wbc); 62439936837SJaegeuk Kim goto retry_flush_nodes; 625127e670aSJaegeuk Kim } 626c718379bSJaegeuk Kim blk_finish_plug(&plug); 627127e670aSJaegeuk Kim } 628127e670aSJaegeuk Kim 629127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 630127e670aSJaegeuk Kim { 63139936837SJaegeuk Kim mutex_unlock(&sbi->node_write); 632e479556bSGu Zheng f2fs_unlock_all(sbi); 633127e670aSJaegeuk Kim } 634127e670aSJaegeuk Kim 635fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 636fb51b5efSChangman Lee { 637fb51b5efSChangman Lee DEFINE_WAIT(wait); 638fb51b5efSChangman Lee 639fb51b5efSChangman Lee for (;;) { 640fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 641fb51b5efSChangman Lee 642fb51b5efSChangman Lee if (!get_pages(sbi, F2FS_WRITEBACK)) 643fb51b5efSChangman Lee break; 644fb51b5efSChangman Lee 645fb51b5efSChangman Lee io_schedule(); 646fb51b5efSChangman Lee } 647fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 648fb51b5efSChangman Lee } 649fb51b5efSChangman Lee 650127e670aSJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 651127e670aSJaegeuk Kim { 652127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 653127e670aSJaegeuk Kim nid_t last_nid = 0; 654127e670aSJaegeuk Kim block_t start_blk; 655127e670aSJaegeuk Kim struct page *cp_page; 656127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 6577e586fa0SJaegeuk Kim __u32 crc32 = 0; 658127e670aSJaegeuk Kim void *kaddr; 659127e670aSJaegeuk Kim int i; 660127e670aSJaegeuk Kim 661127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 662127e670aSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) 663127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 664127e670aSJaegeuk Kim 665127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 666127e670aSJaegeuk Kim 667127e670aSJaegeuk Kim /* 668127e670aSJaegeuk Kim * modify checkpoint 669127e670aSJaegeuk Kim * version number is already updated 670127e670aSJaegeuk Kim */ 671127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 672127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 673127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 674127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 675127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 676127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 677127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 678127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 679127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 680127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 681127e670aSJaegeuk Kim } 682127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 683127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 684127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 685127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 686127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 687127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 688127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 689127e670aSJaegeuk Kim } 690127e670aSJaegeuk Kim 691127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 692127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 693127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 694127e670aSJaegeuk Kim 695127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 696127e670aSJaegeuk Kim data_sum_blocks = npages_for_summary_flush(sbi); 697127e670aSJaegeuk Kim if (data_sum_blocks < 3) 69825ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 699127e670aSJaegeuk Kim else 70025ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 701127e670aSJaegeuk Kim 702127e670aSJaegeuk Kim orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1) 703127e670aSJaegeuk Kim / F2FS_ORPHANS_PER_BLOCK; 70425ca923bSJaegeuk Kim ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks); 705127e670aSJaegeuk Kim 706127e670aSJaegeuk Kim if (is_umount) { 70725ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 70825ca923bSJaegeuk Kim ckpt->cp_pack_total_block_count = cpu_to_le32(2 + 70925ca923bSJaegeuk Kim data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE); 710127e670aSJaegeuk Kim } else { 71125ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 71225ca923bSJaegeuk Kim ckpt->cp_pack_total_block_count = cpu_to_le32(2 + 71325ca923bSJaegeuk Kim data_sum_blocks + orphan_blocks); 714127e670aSJaegeuk Kim } 715127e670aSJaegeuk Kim 716127e670aSJaegeuk Kim if (sbi->n_orphans) 71725ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 718127e670aSJaegeuk Kim else 71925ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 720127e670aSJaegeuk Kim 721127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 722127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 723127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 724127e670aSJaegeuk Kim 725127e670aSJaegeuk Kim crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); 7267e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 7277e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 728127e670aSJaegeuk Kim = cpu_to_le32(crc32); 729127e670aSJaegeuk Kim 730127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi); 731127e670aSJaegeuk Kim 732127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 733127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk++); 734127e670aSJaegeuk Kim kaddr = page_address(cp_page); 735127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 736127e670aSJaegeuk Kim set_page_dirty(cp_page); 737127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 738127e670aSJaegeuk Kim 739127e670aSJaegeuk Kim if (sbi->n_orphans) { 740127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 741127e670aSJaegeuk Kim start_blk += orphan_blocks; 742127e670aSJaegeuk Kim } 743127e670aSJaegeuk Kim 744127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 745127e670aSJaegeuk Kim start_blk += data_sum_blocks; 746127e670aSJaegeuk Kim if (is_umount) { 747127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 748127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 749127e670aSJaegeuk Kim } 750127e670aSJaegeuk Kim 751127e670aSJaegeuk Kim /* writeout checkpoint block */ 752127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk); 753127e670aSJaegeuk Kim kaddr = page_address(cp_page); 754127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 755127e670aSJaegeuk Kim set_page_dirty(cp_page); 756127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 757127e670aSJaegeuk Kim 758127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 759fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 760127e670aSJaegeuk Kim 761127e670aSJaegeuk Kim filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX); 762127e670aSJaegeuk Kim filemap_fdatawait_range(sbi->meta_inode->i_mapping, 0, LONG_MAX); 763127e670aSJaegeuk Kim 764127e670aSJaegeuk Kim /* update user_block_counts */ 765127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 766127e670aSJaegeuk Kim sbi->alloc_valid_block_count = 0; 767127e670aSJaegeuk Kim 768127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 769127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 770127e670aSJaegeuk Kim 771577e3495SJaegeuk Kim if (!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) { 772127e670aSJaegeuk Kim clear_prefree_segments(sbi); 773127e670aSJaegeuk Kim F2FS_RESET_SB_DIRT(sbi); 774127e670aSJaegeuk Kim } 775577e3495SJaegeuk Kim } 776127e670aSJaegeuk Kim 7770a8165d7SJaegeuk Kim /* 778127e670aSJaegeuk Kim * We guarantee that this checkpoint procedure should not fail. 779127e670aSJaegeuk Kim */ 78043727527SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 781127e670aSJaegeuk Kim { 782127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 783127e670aSJaegeuk Kim unsigned long long ckpt_ver; 784127e670aSJaegeuk Kim 7852af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops"); 7862af4bd6cSNamjae Jeon 787127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 788127e670aSJaegeuk Kim block_operations(sbi); 789127e670aSJaegeuk Kim 7902af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops"); 7912af4bd6cSNamjae Jeon 792127e670aSJaegeuk Kim f2fs_submit_bio(sbi, DATA, true); 793127e670aSJaegeuk Kim f2fs_submit_bio(sbi, NODE, true); 794127e670aSJaegeuk Kim f2fs_submit_bio(sbi, META, true); 795127e670aSJaegeuk Kim 796127e670aSJaegeuk Kim /* 797127e670aSJaegeuk Kim * update checkpoint pack index 798127e670aSJaegeuk Kim * Increase the version number so that 799127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 800127e670aSJaegeuk Kim */ 801d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 802127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 803127e670aSJaegeuk Kim 804127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 805127e670aSJaegeuk Kim flush_nat_entries(sbi); 806127e670aSJaegeuk Kim flush_sit_entries(sbi); 807127e670aSJaegeuk Kim 808127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 809127e670aSJaegeuk Kim do_checkpoint(sbi, is_umount); 810127e670aSJaegeuk Kim 811127e670aSJaegeuk Kim unblock_operations(sbi); 812127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 8132af4bd6cSNamjae Jeon 8142af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint"); 815127e670aSJaegeuk Kim } 816127e670aSJaegeuk Kim 817127e670aSJaegeuk Kim void init_orphan_info(struct f2fs_sb_info *sbi) 818127e670aSJaegeuk Kim { 819127e670aSJaegeuk Kim mutex_init(&sbi->orphan_inode_mutex); 820127e670aSJaegeuk Kim INIT_LIST_HEAD(&sbi->orphan_inode_list); 821127e670aSJaegeuk Kim sbi->n_orphans = 0; 822127e670aSJaegeuk Kim } 823127e670aSJaegeuk Kim 8246e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 825127e670aSJaegeuk Kim { 826127e670aSJaegeuk Kim orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry", 827127e670aSJaegeuk Kim sizeof(struct orphan_inode_entry), NULL); 828127e670aSJaegeuk Kim if (unlikely(!orphan_entry_slab)) 829127e670aSJaegeuk Kim return -ENOMEM; 830127e670aSJaegeuk Kim inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry", 831127e670aSJaegeuk Kim sizeof(struct dir_inode_entry), NULL); 832127e670aSJaegeuk Kim if (unlikely(!inode_entry_slab)) { 833127e670aSJaegeuk Kim kmem_cache_destroy(orphan_entry_slab); 834127e670aSJaegeuk Kim return -ENOMEM; 835127e670aSJaegeuk Kim } 836127e670aSJaegeuk Kim return 0; 837127e670aSJaegeuk Kim } 838127e670aSJaegeuk Kim 839127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 840127e670aSJaegeuk Kim { 841127e670aSJaegeuk Kim kmem_cache_destroy(orphan_entry_slab); 842127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 843127e670aSJaegeuk Kim } 844