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 { 303127e670aSJaegeuk Kim struct list_head *head, *this, *next; 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; 309127e670aSJaegeuk Kim 310127e670aSJaegeuk Kim orphan_blocks = (unsigned short)((sbi->n_orphans + 311127e670aSJaegeuk Kim (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK); 312127e670aSJaegeuk Kim 313127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 314127e670aSJaegeuk Kim head = &sbi->orphan_inode_list; 315127e670aSJaegeuk Kim 316127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 317127e670aSJaegeuk Kim list_for_each_safe(this, next, head) { 318127e670aSJaegeuk Kim struct orphan_inode_entry *orphan; 319127e670aSJaegeuk Kim 320127e670aSJaegeuk Kim orphan = list_entry(this, struct orphan_inode_entry, list); 321127e670aSJaegeuk Kim 322127e670aSJaegeuk Kim if (nentries == F2FS_ORPHANS_PER_BLOCK) { 323127e670aSJaegeuk Kim /* 324127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 325127e670aSJaegeuk Kim * then we need to flush current orphan blocks 326127e670aSJaegeuk Kim * and bring another one in memory 327127e670aSJaegeuk Kim */ 328127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 329127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 330127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 331127e670aSJaegeuk Kim set_page_dirty(page); 332127e670aSJaegeuk Kim f2fs_put_page(page, 1); 333127e670aSJaegeuk Kim index++; 334127e670aSJaegeuk Kim start_blk++; 335127e670aSJaegeuk Kim nentries = 0; 336127e670aSJaegeuk Kim page = NULL; 337127e670aSJaegeuk Kim } 338127e670aSJaegeuk Kim if (page) 339127e670aSJaegeuk Kim goto page_exist; 340127e670aSJaegeuk Kim 341127e670aSJaegeuk Kim page = grab_meta_page(sbi, start_blk); 342127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 343127e670aSJaegeuk Kim memset(orphan_blk, 0, sizeof(*orphan_blk)); 344127e670aSJaegeuk Kim page_exist: 345127e670aSJaegeuk Kim orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 346127e670aSJaegeuk Kim } 347127e670aSJaegeuk Kim if (!page) 348127e670aSJaegeuk Kim goto end; 349127e670aSJaegeuk Kim 350127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 351127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 352127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 353127e670aSJaegeuk Kim set_page_dirty(page); 354127e670aSJaegeuk Kim f2fs_put_page(page, 1); 355127e670aSJaegeuk Kim end: 356127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 357127e670aSJaegeuk Kim } 358127e670aSJaegeuk Kim 359127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 360127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 361127e670aSJaegeuk Kim { 362127e670aSJaegeuk Kim struct page *cp_page_1, *cp_page_2 = NULL; 363127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 364127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 365127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 366127e670aSJaegeuk Kim size_t crc_offset; 3677e586fa0SJaegeuk Kim __u32 crc = 0; 368127e670aSJaegeuk Kim 369127e670aSJaegeuk Kim /* Read the 1st cp block in this CP pack */ 370127e670aSJaegeuk Kim cp_page_1 = get_meta_page(sbi, cp_addr); 371127e670aSJaegeuk Kim 372127e670aSJaegeuk Kim /* get the version number */ 373127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); 374127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 375127e670aSJaegeuk Kim if (crc_offset >= blk_size) 376127e670aSJaegeuk Kim goto invalid_cp1; 377127e670aSJaegeuk Kim 3787e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 379127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 380127e670aSJaegeuk Kim goto invalid_cp1; 381127e670aSJaegeuk Kim 382d71b5564SJaegeuk Kim pre_version = cur_cp_version(cp_block); 383127e670aSJaegeuk Kim 384127e670aSJaegeuk Kim /* Read the 2nd cp block in this CP pack */ 38525ca923bSJaegeuk Kim cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 386127e670aSJaegeuk Kim cp_page_2 = get_meta_page(sbi, cp_addr); 387127e670aSJaegeuk Kim 388127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); 389127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 390127e670aSJaegeuk Kim if (crc_offset >= blk_size) 391127e670aSJaegeuk Kim goto invalid_cp2; 392127e670aSJaegeuk Kim 3937e586fa0SJaegeuk Kim crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset))); 394127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 395127e670aSJaegeuk Kim goto invalid_cp2; 396127e670aSJaegeuk Kim 397d71b5564SJaegeuk Kim cur_version = cur_cp_version(cp_block); 398127e670aSJaegeuk Kim 399127e670aSJaegeuk Kim if (cur_version == pre_version) { 400127e670aSJaegeuk Kim *version = cur_version; 401127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 402127e670aSJaegeuk Kim return cp_page_1; 403127e670aSJaegeuk Kim } 404127e670aSJaegeuk Kim invalid_cp2: 405127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 406127e670aSJaegeuk Kim invalid_cp1: 407127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 408127e670aSJaegeuk Kim return NULL; 409127e670aSJaegeuk Kim } 410127e670aSJaegeuk Kim 411127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 412127e670aSJaegeuk Kim { 413127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 414127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 415127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 416127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 417127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 418127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 419127e670aSJaegeuk Kim 420127e670aSJaegeuk Kim sbi->ckpt = kzalloc(blk_size, GFP_KERNEL); 421127e670aSJaegeuk Kim if (!sbi->ckpt) 422127e670aSJaegeuk Kim return -ENOMEM; 423127e670aSJaegeuk Kim /* 424127e670aSJaegeuk Kim * Finding out valid cp block involves read both 425127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 426127e670aSJaegeuk Kim */ 427127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 428127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 429127e670aSJaegeuk Kim 430127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 431127e670aSJaegeuk Kim cp_start_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 432127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 433127e670aSJaegeuk Kim 434127e670aSJaegeuk Kim if (cp1 && cp2) { 435127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 436127e670aSJaegeuk Kim cur_page = cp2; 437127e670aSJaegeuk Kim else 438127e670aSJaegeuk Kim cur_page = cp1; 439127e670aSJaegeuk Kim } else if (cp1) { 440127e670aSJaegeuk Kim cur_page = cp1; 441127e670aSJaegeuk Kim } else if (cp2) { 442127e670aSJaegeuk Kim cur_page = cp2; 443127e670aSJaegeuk Kim } else { 444127e670aSJaegeuk Kim goto fail_no_cp; 445127e670aSJaegeuk Kim } 446127e670aSJaegeuk Kim 447127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 448127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 449127e670aSJaegeuk Kim 450127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 451127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 452127e670aSJaegeuk Kim return 0; 453127e670aSJaegeuk Kim 454127e670aSJaegeuk Kim fail_no_cp: 455127e670aSJaegeuk Kim kfree(sbi->ckpt); 456127e670aSJaegeuk Kim return -EINVAL; 457127e670aSJaegeuk Kim } 458127e670aSJaegeuk Kim 4595deb8267SJaegeuk Kim static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new) 460127e670aSJaegeuk Kim { 461127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 462127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 463127e670aSJaegeuk Kim struct list_head *this; 464127e670aSJaegeuk Kim 4655deb8267SJaegeuk Kim list_for_each(this, head) { 4665deb8267SJaegeuk Kim struct dir_inode_entry *entry; 4675deb8267SJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 4685deb8267SJaegeuk Kim if (entry->inode == inode) 4695deb8267SJaegeuk Kim return -EEXIST; 4705deb8267SJaegeuk Kim } 4715deb8267SJaegeuk Kim list_add_tail(&new->list, head); 472dcdfff65SJaegeuk Kim stat_inc_dirty_dir(sbi); 4735deb8267SJaegeuk Kim return 0; 4745deb8267SJaegeuk Kim } 4755deb8267SJaegeuk Kim 4765deb8267SJaegeuk Kim void set_dirty_dir_page(struct inode *inode, struct page *page) 4775deb8267SJaegeuk Kim { 4785deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 4795deb8267SJaegeuk Kim struct dir_inode_entry *new; 4805deb8267SJaegeuk Kim 481127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 482127e670aSJaegeuk Kim return; 4837bd59381SGu Zheng 4847bd59381SGu Zheng new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 485127e670aSJaegeuk Kim new->inode = inode; 486127e670aSJaegeuk Kim INIT_LIST_HEAD(&new->list); 487127e670aSJaegeuk Kim 488127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 4895deb8267SJaegeuk Kim if (__add_dirty_inode(inode, new)) 490127e670aSJaegeuk Kim kmem_cache_free(inode_entry_slab, new); 491127e670aSJaegeuk Kim 492127e670aSJaegeuk Kim inc_page_count(sbi, F2FS_DIRTY_DENTS); 493127e670aSJaegeuk Kim inode_inc_dirty_dents(inode); 494127e670aSJaegeuk Kim SetPagePrivate(page); 4955deb8267SJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 4965deb8267SJaegeuk Kim } 497127e670aSJaegeuk Kim 4985deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode) 4995deb8267SJaegeuk Kim { 5005deb8267SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 5017bd59381SGu Zheng struct dir_inode_entry *new = 5027bd59381SGu Zheng f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 5037bd59381SGu Zheng 5045deb8267SJaegeuk Kim new->inode = inode; 5055deb8267SJaegeuk Kim INIT_LIST_HEAD(&new->list); 5065deb8267SJaegeuk Kim 5075deb8267SJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 5085deb8267SJaegeuk Kim if (__add_dirty_inode(inode, new)) 5095deb8267SJaegeuk Kim kmem_cache_free(inode_entry_slab, new); 510127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 511127e670aSJaegeuk Kim } 512127e670aSJaegeuk Kim 513127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode) 514127e670aSJaegeuk Kim { 515127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 516127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 517127e670aSJaegeuk Kim struct list_head *this; 518127e670aSJaegeuk Kim 519127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 520127e670aSJaegeuk Kim return; 521127e670aSJaegeuk Kim 522127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 5233b10b1fdSJaegeuk Kim if (atomic_read(&F2FS_I(inode)->dirty_dents)) { 5243b10b1fdSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 5253b10b1fdSJaegeuk Kim return; 5263b10b1fdSJaegeuk Kim } 527127e670aSJaegeuk Kim 528127e670aSJaegeuk Kim list_for_each(this, head) { 529127e670aSJaegeuk Kim struct dir_inode_entry *entry; 530127e670aSJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 531127e670aSJaegeuk Kim if (entry->inode == inode) { 532127e670aSJaegeuk Kim list_del(&entry->list); 533127e670aSJaegeuk Kim kmem_cache_free(inode_entry_slab, entry); 534dcdfff65SJaegeuk Kim stat_dec_dirty_dir(sbi); 535127e670aSJaegeuk Kim break; 536127e670aSJaegeuk Kim } 537127e670aSJaegeuk Kim } 538127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 53974d0b917SJaegeuk Kim 54074d0b917SJaegeuk Kim /* Only from the recovery routine */ 541afc3eda2SJaegeuk Kim if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) { 542afc3eda2SJaegeuk Kim clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT); 54374d0b917SJaegeuk Kim iput(inode); 54474d0b917SJaegeuk Kim } 545afc3eda2SJaegeuk Kim } 54674d0b917SJaegeuk Kim 54774d0b917SJaegeuk Kim struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino) 54874d0b917SJaegeuk Kim { 54974d0b917SJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 55074d0b917SJaegeuk Kim struct list_head *this; 55174d0b917SJaegeuk Kim struct inode *inode = NULL; 55274d0b917SJaegeuk Kim 55374d0b917SJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 55474d0b917SJaegeuk Kim list_for_each(this, head) { 55574d0b917SJaegeuk Kim struct dir_inode_entry *entry; 55674d0b917SJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 55774d0b917SJaegeuk Kim if (entry->inode->i_ino == ino) { 55874d0b917SJaegeuk Kim inode = entry->inode; 55974d0b917SJaegeuk Kim break; 56074d0b917SJaegeuk Kim } 56174d0b917SJaegeuk Kim } 56274d0b917SJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 56374d0b917SJaegeuk Kim return inode; 564127e670aSJaegeuk Kim } 565127e670aSJaegeuk Kim 566127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi) 567127e670aSJaegeuk Kim { 568127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 569127e670aSJaegeuk Kim struct dir_inode_entry *entry; 570127e670aSJaegeuk Kim struct inode *inode; 571127e670aSJaegeuk Kim retry: 572127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 573127e670aSJaegeuk Kim if (list_empty(head)) { 574127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 575127e670aSJaegeuk Kim return; 576127e670aSJaegeuk Kim } 577127e670aSJaegeuk Kim entry = list_entry(head->next, struct dir_inode_entry, list); 578127e670aSJaegeuk Kim inode = igrab(entry->inode); 579127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 580127e670aSJaegeuk Kim if (inode) { 581127e670aSJaegeuk Kim filemap_flush(inode->i_mapping); 582127e670aSJaegeuk Kim iput(inode); 583127e670aSJaegeuk Kim } else { 584127e670aSJaegeuk Kim /* 585127e670aSJaegeuk Kim * We should submit bio, since it exists several 586127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 587127e670aSJaegeuk Kim */ 588127e670aSJaegeuk Kim f2fs_submit_bio(sbi, DATA, true); 589127e670aSJaegeuk Kim } 590127e670aSJaegeuk Kim goto retry; 591127e670aSJaegeuk Kim } 592127e670aSJaegeuk Kim 5930a8165d7SJaegeuk Kim /* 594127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 595127e670aSJaegeuk Kim */ 59643727527SJaegeuk Kim static void block_operations(struct f2fs_sb_info *sbi) 597127e670aSJaegeuk Kim { 598127e670aSJaegeuk Kim struct writeback_control wbc = { 599127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 600127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 601127e670aSJaegeuk Kim .for_reclaim = 0, 602127e670aSJaegeuk Kim }; 603c718379bSJaegeuk Kim struct blk_plug plug; 604c718379bSJaegeuk Kim 605c718379bSJaegeuk Kim blk_start_plug(&plug); 606c718379bSJaegeuk Kim 60739936837SJaegeuk Kim retry_flush_dents: 608e479556bSGu Zheng f2fs_lock_all(sbi); 609127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 610127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 611e479556bSGu Zheng f2fs_unlock_all(sbi); 61239936837SJaegeuk Kim sync_dirty_dir_inodes(sbi); 61339936837SJaegeuk Kim goto retry_flush_dents; 614127e670aSJaegeuk Kim } 615127e670aSJaegeuk Kim 616127e670aSJaegeuk Kim /* 617127e670aSJaegeuk Kim * POR: we should ensure that there is no dirty node pages 618127e670aSJaegeuk Kim * until finishing nat/sit flush. 619127e670aSJaegeuk Kim */ 62039936837SJaegeuk Kim retry_flush_nodes: 62139936837SJaegeuk Kim mutex_lock(&sbi->node_write); 622127e670aSJaegeuk Kim 623127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 62439936837SJaegeuk Kim mutex_unlock(&sbi->node_write); 62539936837SJaegeuk Kim sync_node_pages(sbi, 0, &wbc); 62639936837SJaegeuk Kim goto retry_flush_nodes; 627127e670aSJaegeuk Kim } 628c718379bSJaegeuk Kim blk_finish_plug(&plug); 629127e670aSJaegeuk Kim } 630127e670aSJaegeuk Kim 631127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 632127e670aSJaegeuk Kim { 63339936837SJaegeuk Kim mutex_unlock(&sbi->node_write); 634e479556bSGu Zheng f2fs_unlock_all(sbi); 635127e670aSJaegeuk Kim } 636127e670aSJaegeuk Kim 637*fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi) 638*fb51b5efSChangman Lee { 639*fb51b5efSChangman Lee DEFINE_WAIT(wait); 640*fb51b5efSChangman Lee 641*fb51b5efSChangman Lee for (;;) { 642*fb51b5efSChangman Lee prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); 643*fb51b5efSChangman Lee 644*fb51b5efSChangman Lee if (!get_pages(sbi, F2FS_WRITEBACK)) 645*fb51b5efSChangman Lee break; 646*fb51b5efSChangman Lee 647*fb51b5efSChangman Lee io_schedule(); 648*fb51b5efSChangman Lee } 649*fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait); 650*fb51b5efSChangman Lee } 651*fb51b5efSChangman Lee 652127e670aSJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 653127e670aSJaegeuk Kim { 654127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 655127e670aSJaegeuk Kim nid_t last_nid = 0; 656127e670aSJaegeuk Kim block_t start_blk; 657127e670aSJaegeuk Kim struct page *cp_page; 658127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 6597e586fa0SJaegeuk Kim __u32 crc32 = 0; 660127e670aSJaegeuk Kim void *kaddr; 661127e670aSJaegeuk Kim int i; 662127e670aSJaegeuk Kim 663127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 664127e670aSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) 665127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 666127e670aSJaegeuk Kim 667127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 668127e670aSJaegeuk Kim 669127e670aSJaegeuk Kim /* 670127e670aSJaegeuk Kim * modify checkpoint 671127e670aSJaegeuk Kim * version number is already updated 672127e670aSJaegeuk Kim */ 673127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 674127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 675127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 676127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 677127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 678127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 679127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 680127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 681127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 682127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 683127e670aSJaegeuk Kim } 684127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 685127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 686127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 687127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 688127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 689127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 690127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 691127e670aSJaegeuk Kim } 692127e670aSJaegeuk Kim 693127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 694127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 695127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 696127e670aSJaegeuk Kim 697127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 698127e670aSJaegeuk Kim data_sum_blocks = npages_for_summary_flush(sbi); 699127e670aSJaegeuk Kim if (data_sum_blocks < 3) 70025ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 701127e670aSJaegeuk Kim else 70225ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); 703127e670aSJaegeuk Kim 704127e670aSJaegeuk Kim orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1) 705127e670aSJaegeuk Kim / F2FS_ORPHANS_PER_BLOCK; 70625ca923bSJaegeuk Kim ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks); 707127e670aSJaegeuk Kim 708127e670aSJaegeuk Kim if (is_umount) { 70925ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 71025ca923bSJaegeuk Kim ckpt->cp_pack_total_block_count = cpu_to_le32(2 + 71125ca923bSJaegeuk Kim data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE); 712127e670aSJaegeuk Kim } else { 71325ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); 71425ca923bSJaegeuk Kim ckpt->cp_pack_total_block_count = cpu_to_le32(2 + 71525ca923bSJaegeuk Kim data_sum_blocks + orphan_blocks); 716127e670aSJaegeuk Kim } 717127e670aSJaegeuk Kim 718127e670aSJaegeuk Kim if (sbi->n_orphans) 71925ca923bSJaegeuk Kim set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 720127e670aSJaegeuk Kim else 72125ca923bSJaegeuk Kim clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); 722127e670aSJaegeuk Kim 723127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 724127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 725127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 726127e670aSJaegeuk Kim 727127e670aSJaegeuk Kim crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); 7287e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt + 7297e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset))) 730127e670aSJaegeuk Kim = cpu_to_le32(crc32); 731127e670aSJaegeuk Kim 732127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi); 733127e670aSJaegeuk Kim 734127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 735127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk++); 736127e670aSJaegeuk Kim kaddr = page_address(cp_page); 737127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 738127e670aSJaegeuk Kim set_page_dirty(cp_page); 739127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 740127e670aSJaegeuk Kim 741127e670aSJaegeuk Kim if (sbi->n_orphans) { 742127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 743127e670aSJaegeuk Kim start_blk += orphan_blocks; 744127e670aSJaegeuk Kim } 745127e670aSJaegeuk Kim 746127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 747127e670aSJaegeuk Kim start_blk += data_sum_blocks; 748127e670aSJaegeuk Kim if (is_umount) { 749127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 750127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 751127e670aSJaegeuk Kim } 752127e670aSJaegeuk Kim 753127e670aSJaegeuk Kim /* writeout checkpoint block */ 754127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk); 755127e670aSJaegeuk Kim kaddr = page_address(cp_page); 756127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 757127e670aSJaegeuk Kim set_page_dirty(cp_page); 758127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 759127e670aSJaegeuk Kim 760127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 761*fb51b5efSChangman Lee wait_on_all_pages_writeback(sbi); 762127e670aSJaegeuk Kim 763127e670aSJaegeuk Kim filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX); 764127e670aSJaegeuk Kim filemap_fdatawait_range(sbi->meta_inode->i_mapping, 0, LONG_MAX); 765127e670aSJaegeuk Kim 766127e670aSJaegeuk Kim /* update user_block_counts */ 767127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 768127e670aSJaegeuk Kim sbi->alloc_valid_block_count = 0; 769127e670aSJaegeuk Kim 770127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 771127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 772127e670aSJaegeuk Kim 773577e3495SJaegeuk Kim if (!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) { 774127e670aSJaegeuk Kim clear_prefree_segments(sbi); 775127e670aSJaegeuk Kim F2FS_RESET_SB_DIRT(sbi); 776127e670aSJaegeuk Kim } 777577e3495SJaegeuk Kim } 778127e670aSJaegeuk Kim 7790a8165d7SJaegeuk Kim /* 780127e670aSJaegeuk Kim * We guarantee that this checkpoint procedure should not fail. 781127e670aSJaegeuk Kim */ 78243727527SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 783127e670aSJaegeuk Kim { 784127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 785127e670aSJaegeuk Kim unsigned long long ckpt_ver; 786127e670aSJaegeuk Kim 7872af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops"); 7882af4bd6cSNamjae Jeon 789127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 790127e670aSJaegeuk Kim block_operations(sbi); 791127e670aSJaegeuk Kim 7922af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops"); 7932af4bd6cSNamjae Jeon 794127e670aSJaegeuk Kim f2fs_submit_bio(sbi, DATA, true); 795127e670aSJaegeuk Kim f2fs_submit_bio(sbi, NODE, true); 796127e670aSJaegeuk Kim f2fs_submit_bio(sbi, META, true); 797127e670aSJaegeuk Kim 798127e670aSJaegeuk Kim /* 799127e670aSJaegeuk Kim * update checkpoint pack index 800127e670aSJaegeuk Kim * Increase the version number so that 801127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 802127e670aSJaegeuk Kim */ 803d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt); 804127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 805127e670aSJaegeuk Kim 806127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 807127e670aSJaegeuk Kim flush_nat_entries(sbi); 808127e670aSJaegeuk Kim flush_sit_entries(sbi); 809127e670aSJaegeuk Kim 810127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 811127e670aSJaegeuk Kim do_checkpoint(sbi, is_umount); 812127e670aSJaegeuk Kim 813127e670aSJaegeuk Kim unblock_operations(sbi); 814127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 8152af4bd6cSNamjae Jeon 8162af4bd6cSNamjae Jeon trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint"); 817127e670aSJaegeuk Kim } 818127e670aSJaegeuk Kim 819127e670aSJaegeuk Kim void init_orphan_info(struct f2fs_sb_info *sbi) 820127e670aSJaegeuk Kim { 821127e670aSJaegeuk Kim mutex_init(&sbi->orphan_inode_mutex); 822127e670aSJaegeuk Kim INIT_LIST_HEAD(&sbi->orphan_inode_list); 823127e670aSJaegeuk Kim sbi->n_orphans = 0; 824127e670aSJaegeuk Kim } 825127e670aSJaegeuk Kim 8266e6093a8SNamjae Jeon int __init create_checkpoint_caches(void) 827127e670aSJaegeuk Kim { 828127e670aSJaegeuk Kim orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry", 829127e670aSJaegeuk Kim sizeof(struct orphan_inode_entry), NULL); 830127e670aSJaegeuk Kim if (unlikely(!orphan_entry_slab)) 831127e670aSJaegeuk Kim return -ENOMEM; 832127e670aSJaegeuk Kim inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry", 833127e670aSJaegeuk Kim sizeof(struct dir_inode_entry), NULL); 834127e670aSJaegeuk Kim if (unlikely(!inode_entry_slab)) { 835127e670aSJaegeuk Kim kmem_cache_destroy(orphan_entry_slab); 836127e670aSJaegeuk Kim return -ENOMEM; 837127e670aSJaegeuk Kim } 838127e670aSJaegeuk Kim return 0; 839127e670aSJaegeuk Kim } 840127e670aSJaegeuk Kim 841127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 842127e670aSJaegeuk Kim { 843127e670aSJaegeuk Kim kmem_cache_destroy(orphan_entry_slab); 844127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 845127e670aSJaegeuk Kim } 846