1*127e670aSJaegeuk Kim /** 2*127e670aSJaegeuk Kim * fs/f2fs/checkpoint.c 3*127e670aSJaegeuk Kim * 4*127e670aSJaegeuk Kim * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5*127e670aSJaegeuk Kim * http://www.samsung.com/ 6*127e670aSJaegeuk Kim * 7*127e670aSJaegeuk Kim * This program is free software; you can redistribute it and/or modify 8*127e670aSJaegeuk Kim * it under the terms of the GNU General Public License version 2 as 9*127e670aSJaegeuk Kim * published by the Free Software Foundation. 10*127e670aSJaegeuk Kim */ 11*127e670aSJaegeuk Kim #include <linux/fs.h> 12*127e670aSJaegeuk Kim #include <linux/bio.h> 13*127e670aSJaegeuk Kim #include <linux/mpage.h> 14*127e670aSJaegeuk Kim #include <linux/writeback.h> 15*127e670aSJaegeuk Kim #include <linux/blkdev.h> 16*127e670aSJaegeuk Kim #include <linux/f2fs_fs.h> 17*127e670aSJaegeuk Kim #include <linux/pagevec.h> 18*127e670aSJaegeuk Kim #include <linux/swap.h> 19*127e670aSJaegeuk Kim 20*127e670aSJaegeuk Kim #include "f2fs.h" 21*127e670aSJaegeuk Kim #include "node.h" 22*127e670aSJaegeuk Kim #include "segment.h" 23*127e670aSJaegeuk Kim 24*127e670aSJaegeuk Kim static struct kmem_cache *orphan_entry_slab; 25*127e670aSJaegeuk Kim static struct kmem_cache *inode_entry_slab; 26*127e670aSJaegeuk Kim 27*127e670aSJaegeuk Kim /** 28*127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 29*127e670aSJaegeuk Kim */ 30*127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 31*127e670aSJaegeuk Kim { 32*127e670aSJaegeuk Kim struct address_space *mapping = sbi->meta_inode->i_mapping; 33*127e670aSJaegeuk Kim struct page *page = NULL; 34*127e670aSJaegeuk Kim repeat: 35*127e670aSJaegeuk Kim page = grab_cache_page(mapping, index); 36*127e670aSJaegeuk Kim if (!page) { 37*127e670aSJaegeuk Kim cond_resched(); 38*127e670aSJaegeuk Kim goto repeat; 39*127e670aSJaegeuk Kim } 40*127e670aSJaegeuk Kim 41*127e670aSJaegeuk Kim /* We wait writeback only inside grab_meta_page() */ 42*127e670aSJaegeuk Kim wait_on_page_writeback(page); 43*127e670aSJaegeuk Kim SetPageUptodate(page); 44*127e670aSJaegeuk Kim return page; 45*127e670aSJaegeuk Kim } 46*127e670aSJaegeuk Kim 47*127e670aSJaegeuk Kim /** 48*127e670aSJaegeuk Kim * We guarantee no failure on the returned page. 49*127e670aSJaegeuk Kim */ 50*127e670aSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 51*127e670aSJaegeuk Kim { 52*127e670aSJaegeuk Kim struct address_space *mapping = sbi->meta_inode->i_mapping; 53*127e670aSJaegeuk Kim struct page *page; 54*127e670aSJaegeuk Kim repeat: 55*127e670aSJaegeuk Kim page = grab_cache_page(mapping, index); 56*127e670aSJaegeuk Kim if (!page) { 57*127e670aSJaegeuk Kim cond_resched(); 58*127e670aSJaegeuk Kim goto repeat; 59*127e670aSJaegeuk Kim } 60*127e670aSJaegeuk Kim if (f2fs_readpage(sbi, page, index, READ_SYNC)) { 61*127e670aSJaegeuk Kim f2fs_put_page(page, 1); 62*127e670aSJaegeuk Kim goto repeat; 63*127e670aSJaegeuk Kim } 64*127e670aSJaegeuk Kim mark_page_accessed(page); 65*127e670aSJaegeuk Kim 66*127e670aSJaegeuk Kim /* We do not allow returning an errorneous page */ 67*127e670aSJaegeuk Kim return page; 68*127e670aSJaegeuk Kim } 69*127e670aSJaegeuk Kim 70*127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page, 71*127e670aSJaegeuk Kim struct writeback_control *wbc) 72*127e670aSJaegeuk Kim { 73*127e670aSJaegeuk Kim struct inode *inode = page->mapping->host; 74*127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 75*127e670aSJaegeuk Kim int err; 76*127e670aSJaegeuk Kim 77*127e670aSJaegeuk Kim wait_on_page_writeback(page); 78*127e670aSJaegeuk Kim 79*127e670aSJaegeuk Kim err = write_meta_page(sbi, page, wbc); 80*127e670aSJaegeuk Kim if (err) { 81*127e670aSJaegeuk Kim wbc->pages_skipped++; 82*127e670aSJaegeuk Kim set_page_dirty(page); 83*127e670aSJaegeuk Kim } 84*127e670aSJaegeuk Kim 85*127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META); 86*127e670aSJaegeuk Kim 87*127e670aSJaegeuk Kim /* In this case, we should not unlock this page */ 88*127e670aSJaegeuk Kim if (err != AOP_WRITEPAGE_ACTIVATE) 89*127e670aSJaegeuk Kim unlock_page(page); 90*127e670aSJaegeuk Kim return err; 91*127e670aSJaegeuk Kim } 92*127e670aSJaegeuk Kim 93*127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping, 94*127e670aSJaegeuk Kim struct writeback_control *wbc) 95*127e670aSJaegeuk Kim { 96*127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 97*127e670aSJaegeuk Kim struct block_device *bdev = sbi->sb->s_bdev; 98*127e670aSJaegeuk Kim long written; 99*127e670aSJaegeuk Kim 100*127e670aSJaegeuk Kim if (wbc->for_kupdate) 101*127e670aSJaegeuk Kim return 0; 102*127e670aSJaegeuk Kim 103*127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_META) == 0) 104*127e670aSJaegeuk Kim return 0; 105*127e670aSJaegeuk Kim 106*127e670aSJaegeuk Kim /* if mounting is failed, skip writing node pages */ 107*127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 108*127e670aSJaegeuk Kim written = sync_meta_pages(sbi, META, bio_get_nr_vecs(bdev)); 109*127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 110*127e670aSJaegeuk Kim wbc->nr_to_write -= written; 111*127e670aSJaegeuk Kim return 0; 112*127e670aSJaegeuk Kim } 113*127e670aSJaegeuk Kim 114*127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 115*127e670aSJaegeuk Kim long nr_to_write) 116*127e670aSJaegeuk Kim { 117*127e670aSJaegeuk Kim struct address_space *mapping = sbi->meta_inode->i_mapping; 118*127e670aSJaegeuk Kim pgoff_t index = 0, end = LONG_MAX; 119*127e670aSJaegeuk Kim struct pagevec pvec; 120*127e670aSJaegeuk Kim long nwritten = 0; 121*127e670aSJaegeuk Kim struct writeback_control wbc = { 122*127e670aSJaegeuk Kim .for_reclaim = 0, 123*127e670aSJaegeuk Kim }; 124*127e670aSJaegeuk Kim 125*127e670aSJaegeuk Kim pagevec_init(&pvec, 0); 126*127e670aSJaegeuk Kim 127*127e670aSJaegeuk Kim while (index <= end) { 128*127e670aSJaegeuk Kim int i, nr_pages; 129*127e670aSJaegeuk Kim nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 130*127e670aSJaegeuk Kim PAGECACHE_TAG_DIRTY, 131*127e670aSJaegeuk Kim min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 132*127e670aSJaegeuk Kim if (nr_pages == 0) 133*127e670aSJaegeuk Kim break; 134*127e670aSJaegeuk Kim 135*127e670aSJaegeuk Kim for (i = 0; i < nr_pages; i++) { 136*127e670aSJaegeuk Kim struct page *page = pvec.pages[i]; 137*127e670aSJaegeuk Kim lock_page(page); 138*127e670aSJaegeuk Kim BUG_ON(page->mapping != mapping); 139*127e670aSJaegeuk Kim BUG_ON(!PageDirty(page)); 140*127e670aSJaegeuk Kim clear_page_dirty_for_io(page); 141*127e670aSJaegeuk Kim f2fs_write_meta_page(page, &wbc); 142*127e670aSJaegeuk Kim if (nwritten++ >= nr_to_write) 143*127e670aSJaegeuk Kim break; 144*127e670aSJaegeuk Kim } 145*127e670aSJaegeuk Kim pagevec_release(&pvec); 146*127e670aSJaegeuk Kim cond_resched(); 147*127e670aSJaegeuk Kim } 148*127e670aSJaegeuk Kim 149*127e670aSJaegeuk Kim if (nwritten) 150*127e670aSJaegeuk Kim f2fs_submit_bio(sbi, type, nr_to_write == LONG_MAX); 151*127e670aSJaegeuk Kim 152*127e670aSJaegeuk Kim return nwritten; 153*127e670aSJaegeuk Kim } 154*127e670aSJaegeuk Kim 155*127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page) 156*127e670aSJaegeuk Kim { 157*127e670aSJaegeuk Kim struct address_space *mapping = page->mapping; 158*127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 159*127e670aSJaegeuk Kim 160*127e670aSJaegeuk Kim SetPageUptodate(page); 161*127e670aSJaegeuk Kim if (!PageDirty(page)) { 162*127e670aSJaegeuk Kim __set_page_dirty_nobuffers(page); 163*127e670aSJaegeuk Kim inc_page_count(sbi, F2FS_DIRTY_META); 164*127e670aSJaegeuk Kim F2FS_SET_SB_DIRT(sbi); 165*127e670aSJaegeuk Kim return 1; 166*127e670aSJaegeuk Kim } 167*127e670aSJaegeuk Kim return 0; 168*127e670aSJaegeuk Kim } 169*127e670aSJaegeuk Kim 170*127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = { 171*127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page, 172*127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages, 173*127e670aSJaegeuk Kim .set_page_dirty = f2fs_set_meta_page_dirty, 174*127e670aSJaegeuk Kim }; 175*127e670aSJaegeuk Kim 176*127e670aSJaegeuk Kim int check_orphan_space(struct f2fs_sb_info *sbi) 177*127e670aSJaegeuk Kim { 178*127e670aSJaegeuk Kim unsigned int max_orphans; 179*127e670aSJaegeuk Kim int err = 0; 180*127e670aSJaegeuk Kim 181*127e670aSJaegeuk Kim /* 182*127e670aSJaegeuk Kim * considering 512 blocks in a segment 5 blocks are needed for cp 183*127e670aSJaegeuk Kim * and log segment summaries. Remaining blocks are used to keep 184*127e670aSJaegeuk Kim * orphan entries with the limitation one reserved segment 185*127e670aSJaegeuk Kim * for cp pack we can have max 1020*507 orphan entries 186*127e670aSJaegeuk Kim */ 187*127e670aSJaegeuk Kim max_orphans = (sbi->blocks_per_seg - 5) * F2FS_ORPHANS_PER_BLOCK; 188*127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 189*127e670aSJaegeuk Kim if (sbi->n_orphans >= max_orphans) 190*127e670aSJaegeuk Kim err = -ENOSPC; 191*127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 192*127e670aSJaegeuk Kim return err; 193*127e670aSJaegeuk Kim } 194*127e670aSJaegeuk Kim 195*127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 196*127e670aSJaegeuk Kim { 197*127e670aSJaegeuk Kim struct list_head *head, *this; 198*127e670aSJaegeuk Kim struct orphan_inode_entry *new = NULL, *orphan = NULL; 199*127e670aSJaegeuk Kim 200*127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 201*127e670aSJaegeuk Kim head = &sbi->orphan_inode_list; 202*127e670aSJaegeuk Kim list_for_each(this, head) { 203*127e670aSJaegeuk Kim orphan = list_entry(this, struct orphan_inode_entry, list); 204*127e670aSJaegeuk Kim if (orphan->ino == ino) 205*127e670aSJaegeuk Kim goto out; 206*127e670aSJaegeuk Kim if (orphan->ino > ino) 207*127e670aSJaegeuk Kim break; 208*127e670aSJaegeuk Kim orphan = NULL; 209*127e670aSJaegeuk Kim } 210*127e670aSJaegeuk Kim retry: 211*127e670aSJaegeuk Kim new = kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC); 212*127e670aSJaegeuk Kim if (!new) { 213*127e670aSJaegeuk Kim cond_resched(); 214*127e670aSJaegeuk Kim goto retry; 215*127e670aSJaegeuk Kim } 216*127e670aSJaegeuk Kim new->ino = ino; 217*127e670aSJaegeuk Kim INIT_LIST_HEAD(&new->list); 218*127e670aSJaegeuk Kim 219*127e670aSJaegeuk Kim /* add new_oentry into list which is sorted by inode number */ 220*127e670aSJaegeuk Kim if (orphan) { 221*127e670aSJaegeuk Kim struct orphan_inode_entry *prev; 222*127e670aSJaegeuk Kim 223*127e670aSJaegeuk Kim /* get previous entry */ 224*127e670aSJaegeuk Kim prev = list_entry(orphan->list.prev, typeof(*prev), list); 225*127e670aSJaegeuk Kim if (&prev->list != head) 226*127e670aSJaegeuk Kim /* insert new orphan inode entry */ 227*127e670aSJaegeuk Kim list_add(&new->list, &prev->list); 228*127e670aSJaegeuk Kim else 229*127e670aSJaegeuk Kim list_add(&new->list, head); 230*127e670aSJaegeuk Kim } else { 231*127e670aSJaegeuk Kim list_add_tail(&new->list, head); 232*127e670aSJaegeuk Kim } 233*127e670aSJaegeuk Kim sbi->n_orphans++; 234*127e670aSJaegeuk Kim out: 235*127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 236*127e670aSJaegeuk Kim } 237*127e670aSJaegeuk Kim 238*127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 239*127e670aSJaegeuk Kim { 240*127e670aSJaegeuk Kim struct list_head *this, *next, *head; 241*127e670aSJaegeuk Kim struct orphan_inode_entry *orphan; 242*127e670aSJaegeuk Kim 243*127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 244*127e670aSJaegeuk Kim head = &sbi->orphan_inode_list; 245*127e670aSJaegeuk Kim list_for_each_safe(this, next, head) { 246*127e670aSJaegeuk Kim orphan = list_entry(this, struct orphan_inode_entry, list); 247*127e670aSJaegeuk Kim if (orphan->ino == ino) { 248*127e670aSJaegeuk Kim list_del(&orphan->list); 249*127e670aSJaegeuk Kim kmem_cache_free(orphan_entry_slab, orphan); 250*127e670aSJaegeuk Kim sbi->n_orphans--; 251*127e670aSJaegeuk Kim break; 252*127e670aSJaegeuk Kim } 253*127e670aSJaegeuk Kim } 254*127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 255*127e670aSJaegeuk Kim } 256*127e670aSJaegeuk Kim 257*127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) 258*127e670aSJaegeuk Kim { 259*127e670aSJaegeuk Kim struct inode *inode = f2fs_iget(sbi->sb, ino); 260*127e670aSJaegeuk Kim BUG_ON(IS_ERR(inode)); 261*127e670aSJaegeuk Kim clear_nlink(inode); 262*127e670aSJaegeuk Kim 263*127e670aSJaegeuk Kim /* truncate all the data during iput */ 264*127e670aSJaegeuk Kim iput(inode); 265*127e670aSJaegeuk Kim } 266*127e670aSJaegeuk Kim 267*127e670aSJaegeuk Kim int recover_orphan_inodes(struct f2fs_sb_info *sbi) 268*127e670aSJaegeuk Kim { 269*127e670aSJaegeuk Kim block_t start_blk, orphan_blkaddr, i, j; 270*127e670aSJaegeuk Kim 271*127e670aSJaegeuk Kim if (!(F2FS_CKPT(sbi)->ckpt_flags & CP_ORPHAN_PRESENT_FLAG)) 272*127e670aSJaegeuk Kim return 0; 273*127e670aSJaegeuk Kim 274*127e670aSJaegeuk Kim sbi->por_doing = 1; 275*127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi) + 1; 276*127e670aSJaegeuk Kim orphan_blkaddr = __start_sum_addr(sbi) - 1; 277*127e670aSJaegeuk Kim 278*127e670aSJaegeuk Kim for (i = 0; i < orphan_blkaddr; i++) { 279*127e670aSJaegeuk Kim struct page *page = get_meta_page(sbi, start_blk + i); 280*127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk; 281*127e670aSJaegeuk Kim 282*127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 283*127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 284*127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 285*127e670aSJaegeuk Kim recover_orphan_inode(sbi, ino); 286*127e670aSJaegeuk Kim } 287*127e670aSJaegeuk Kim f2fs_put_page(page, 1); 288*127e670aSJaegeuk Kim } 289*127e670aSJaegeuk Kim /* clear Orphan Flag */ 290*127e670aSJaegeuk Kim F2FS_CKPT(sbi)->ckpt_flags &= (~CP_ORPHAN_PRESENT_FLAG); 291*127e670aSJaegeuk Kim sbi->por_doing = 0; 292*127e670aSJaegeuk Kim return 0; 293*127e670aSJaegeuk Kim } 294*127e670aSJaegeuk Kim 295*127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 296*127e670aSJaegeuk Kim { 297*127e670aSJaegeuk Kim struct list_head *head, *this, *next; 298*127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL; 299*127e670aSJaegeuk Kim struct page *page = NULL; 300*127e670aSJaegeuk Kim unsigned int nentries = 0; 301*127e670aSJaegeuk Kim unsigned short index = 1; 302*127e670aSJaegeuk Kim unsigned short orphan_blocks; 303*127e670aSJaegeuk Kim 304*127e670aSJaegeuk Kim orphan_blocks = (unsigned short)((sbi->n_orphans + 305*127e670aSJaegeuk Kim (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK); 306*127e670aSJaegeuk Kim 307*127e670aSJaegeuk Kim mutex_lock(&sbi->orphan_inode_mutex); 308*127e670aSJaegeuk Kim head = &sbi->orphan_inode_list; 309*127e670aSJaegeuk Kim 310*127e670aSJaegeuk Kim /* loop for each orphan inode entry and write them in Jornal block */ 311*127e670aSJaegeuk Kim list_for_each_safe(this, next, head) { 312*127e670aSJaegeuk Kim struct orphan_inode_entry *orphan; 313*127e670aSJaegeuk Kim 314*127e670aSJaegeuk Kim orphan = list_entry(this, struct orphan_inode_entry, list); 315*127e670aSJaegeuk Kim 316*127e670aSJaegeuk Kim if (nentries == F2FS_ORPHANS_PER_BLOCK) { 317*127e670aSJaegeuk Kim /* 318*127e670aSJaegeuk Kim * an orphan block is full of 1020 entries, 319*127e670aSJaegeuk Kim * then we need to flush current orphan blocks 320*127e670aSJaegeuk Kim * and bring another one in memory 321*127e670aSJaegeuk Kim */ 322*127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 323*127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 324*127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 325*127e670aSJaegeuk Kim set_page_dirty(page); 326*127e670aSJaegeuk Kim f2fs_put_page(page, 1); 327*127e670aSJaegeuk Kim index++; 328*127e670aSJaegeuk Kim start_blk++; 329*127e670aSJaegeuk Kim nentries = 0; 330*127e670aSJaegeuk Kim page = NULL; 331*127e670aSJaegeuk Kim } 332*127e670aSJaegeuk Kim if (page) 333*127e670aSJaegeuk Kim goto page_exist; 334*127e670aSJaegeuk Kim 335*127e670aSJaegeuk Kim page = grab_meta_page(sbi, start_blk); 336*127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page); 337*127e670aSJaegeuk Kim memset(orphan_blk, 0, sizeof(*orphan_blk)); 338*127e670aSJaegeuk Kim page_exist: 339*127e670aSJaegeuk Kim orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); 340*127e670aSJaegeuk Kim } 341*127e670aSJaegeuk Kim if (!page) 342*127e670aSJaegeuk Kim goto end; 343*127e670aSJaegeuk Kim 344*127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index); 345*127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks); 346*127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries); 347*127e670aSJaegeuk Kim set_page_dirty(page); 348*127e670aSJaegeuk Kim f2fs_put_page(page, 1); 349*127e670aSJaegeuk Kim end: 350*127e670aSJaegeuk Kim mutex_unlock(&sbi->orphan_inode_mutex); 351*127e670aSJaegeuk Kim } 352*127e670aSJaegeuk Kim 353*127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi, 354*127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version) 355*127e670aSJaegeuk Kim { 356*127e670aSJaegeuk Kim struct page *cp_page_1, *cp_page_2 = NULL; 357*127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 358*127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 359*127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0; 360*127e670aSJaegeuk Kim unsigned int crc = 0; 361*127e670aSJaegeuk Kim size_t crc_offset; 362*127e670aSJaegeuk Kim 363*127e670aSJaegeuk Kim /* Read the 1st cp block in this CP pack */ 364*127e670aSJaegeuk Kim cp_page_1 = get_meta_page(sbi, cp_addr); 365*127e670aSJaegeuk Kim 366*127e670aSJaegeuk Kim /* get the version number */ 367*127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1); 368*127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 369*127e670aSJaegeuk Kim if (crc_offset >= blk_size) 370*127e670aSJaegeuk Kim goto invalid_cp1; 371*127e670aSJaegeuk Kim 372*127e670aSJaegeuk Kim crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset); 373*127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 374*127e670aSJaegeuk Kim goto invalid_cp1; 375*127e670aSJaegeuk Kim 376*127e670aSJaegeuk Kim pre_version = le64_to_cpu(cp_block->checkpoint_ver); 377*127e670aSJaegeuk Kim 378*127e670aSJaegeuk Kim /* Read the 2nd cp block in this CP pack */ 379*127e670aSJaegeuk Kim cp_addr += le64_to_cpu(cp_block->cp_pack_total_block_count) - 1; 380*127e670aSJaegeuk Kim cp_page_2 = get_meta_page(sbi, cp_addr); 381*127e670aSJaegeuk Kim 382*127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2); 383*127e670aSJaegeuk Kim crc_offset = le32_to_cpu(cp_block->checksum_offset); 384*127e670aSJaegeuk Kim if (crc_offset >= blk_size) 385*127e670aSJaegeuk Kim goto invalid_cp2; 386*127e670aSJaegeuk Kim 387*127e670aSJaegeuk Kim crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset); 388*127e670aSJaegeuk Kim if (!f2fs_crc_valid(crc, cp_block, crc_offset)) 389*127e670aSJaegeuk Kim goto invalid_cp2; 390*127e670aSJaegeuk Kim 391*127e670aSJaegeuk Kim cur_version = le64_to_cpu(cp_block->checkpoint_ver); 392*127e670aSJaegeuk Kim 393*127e670aSJaegeuk Kim if (cur_version == pre_version) { 394*127e670aSJaegeuk Kim *version = cur_version; 395*127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 396*127e670aSJaegeuk Kim return cp_page_1; 397*127e670aSJaegeuk Kim } 398*127e670aSJaegeuk Kim invalid_cp2: 399*127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1); 400*127e670aSJaegeuk Kim invalid_cp1: 401*127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1); 402*127e670aSJaegeuk Kim return NULL; 403*127e670aSJaegeuk Kim } 404*127e670aSJaegeuk Kim 405*127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi) 406*127e670aSJaegeuk Kim { 407*127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block; 408*127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super; 409*127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page; 410*127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize; 411*127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0; 412*127e670aSJaegeuk Kim unsigned long long cp_start_blk_no; 413*127e670aSJaegeuk Kim 414*127e670aSJaegeuk Kim sbi->ckpt = kzalloc(blk_size, GFP_KERNEL); 415*127e670aSJaegeuk Kim if (!sbi->ckpt) 416*127e670aSJaegeuk Kim return -ENOMEM; 417*127e670aSJaegeuk Kim /* 418*127e670aSJaegeuk Kim * Finding out valid cp block involves read both 419*127e670aSJaegeuk Kim * sets( cp pack1 and cp pack 2) 420*127e670aSJaegeuk Kim */ 421*127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); 422*127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 423*127e670aSJaegeuk Kim 424*127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */ 425*127e670aSJaegeuk Kim cp_start_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg); 426*127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 427*127e670aSJaegeuk Kim 428*127e670aSJaegeuk Kim if (cp1 && cp2) { 429*127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version)) 430*127e670aSJaegeuk Kim cur_page = cp2; 431*127e670aSJaegeuk Kim else 432*127e670aSJaegeuk Kim cur_page = cp1; 433*127e670aSJaegeuk Kim } else if (cp1) { 434*127e670aSJaegeuk Kim cur_page = cp1; 435*127e670aSJaegeuk Kim } else if (cp2) { 436*127e670aSJaegeuk Kim cur_page = cp2; 437*127e670aSJaegeuk Kim } else { 438*127e670aSJaegeuk Kim goto fail_no_cp; 439*127e670aSJaegeuk Kim } 440*127e670aSJaegeuk Kim 441*127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page); 442*127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size); 443*127e670aSJaegeuk Kim 444*127e670aSJaegeuk Kim f2fs_put_page(cp1, 1); 445*127e670aSJaegeuk Kim f2fs_put_page(cp2, 1); 446*127e670aSJaegeuk Kim return 0; 447*127e670aSJaegeuk Kim 448*127e670aSJaegeuk Kim fail_no_cp: 449*127e670aSJaegeuk Kim kfree(sbi->ckpt); 450*127e670aSJaegeuk Kim return -EINVAL; 451*127e670aSJaegeuk Kim } 452*127e670aSJaegeuk Kim 453*127e670aSJaegeuk Kim void set_dirty_dir_page(struct inode *inode, struct page *page) 454*127e670aSJaegeuk Kim { 455*127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 456*127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 457*127e670aSJaegeuk Kim struct dir_inode_entry *new; 458*127e670aSJaegeuk Kim struct list_head *this; 459*127e670aSJaegeuk Kim 460*127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 461*127e670aSJaegeuk Kim return; 462*127e670aSJaegeuk Kim retry: 463*127e670aSJaegeuk Kim new = kmem_cache_alloc(inode_entry_slab, GFP_NOFS); 464*127e670aSJaegeuk Kim if (!new) { 465*127e670aSJaegeuk Kim cond_resched(); 466*127e670aSJaegeuk Kim goto retry; 467*127e670aSJaegeuk Kim } 468*127e670aSJaegeuk Kim new->inode = inode; 469*127e670aSJaegeuk Kim INIT_LIST_HEAD(&new->list); 470*127e670aSJaegeuk Kim 471*127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 472*127e670aSJaegeuk Kim list_for_each(this, head) { 473*127e670aSJaegeuk Kim struct dir_inode_entry *entry; 474*127e670aSJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 475*127e670aSJaegeuk Kim if (entry->inode == inode) { 476*127e670aSJaegeuk Kim kmem_cache_free(inode_entry_slab, new); 477*127e670aSJaegeuk Kim goto out; 478*127e670aSJaegeuk Kim } 479*127e670aSJaegeuk Kim } 480*127e670aSJaegeuk Kim list_add_tail(&new->list, head); 481*127e670aSJaegeuk Kim sbi->n_dirty_dirs++; 482*127e670aSJaegeuk Kim 483*127e670aSJaegeuk Kim BUG_ON(!S_ISDIR(inode->i_mode)); 484*127e670aSJaegeuk Kim out: 485*127e670aSJaegeuk Kim inc_page_count(sbi, F2FS_DIRTY_DENTS); 486*127e670aSJaegeuk Kim inode_inc_dirty_dents(inode); 487*127e670aSJaegeuk Kim SetPagePrivate(page); 488*127e670aSJaegeuk Kim 489*127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 490*127e670aSJaegeuk Kim } 491*127e670aSJaegeuk Kim 492*127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode) 493*127e670aSJaegeuk Kim { 494*127e670aSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 495*127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 496*127e670aSJaegeuk Kim struct list_head *this; 497*127e670aSJaegeuk Kim 498*127e670aSJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 499*127e670aSJaegeuk Kim return; 500*127e670aSJaegeuk Kim 501*127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 502*127e670aSJaegeuk Kim if (atomic_read(&F2FS_I(inode)->dirty_dents)) 503*127e670aSJaegeuk Kim goto out; 504*127e670aSJaegeuk Kim 505*127e670aSJaegeuk Kim list_for_each(this, head) { 506*127e670aSJaegeuk Kim struct dir_inode_entry *entry; 507*127e670aSJaegeuk Kim entry = list_entry(this, struct dir_inode_entry, list); 508*127e670aSJaegeuk Kim if (entry->inode == inode) { 509*127e670aSJaegeuk Kim list_del(&entry->list); 510*127e670aSJaegeuk Kim kmem_cache_free(inode_entry_slab, entry); 511*127e670aSJaegeuk Kim sbi->n_dirty_dirs--; 512*127e670aSJaegeuk Kim break; 513*127e670aSJaegeuk Kim } 514*127e670aSJaegeuk Kim } 515*127e670aSJaegeuk Kim out: 516*127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 517*127e670aSJaegeuk Kim } 518*127e670aSJaegeuk Kim 519*127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi) 520*127e670aSJaegeuk Kim { 521*127e670aSJaegeuk Kim struct list_head *head = &sbi->dir_inode_list; 522*127e670aSJaegeuk Kim struct dir_inode_entry *entry; 523*127e670aSJaegeuk Kim struct inode *inode; 524*127e670aSJaegeuk Kim retry: 525*127e670aSJaegeuk Kim spin_lock(&sbi->dir_inode_lock); 526*127e670aSJaegeuk Kim if (list_empty(head)) { 527*127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 528*127e670aSJaegeuk Kim return; 529*127e670aSJaegeuk Kim } 530*127e670aSJaegeuk Kim entry = list_entry(head->next, struct dir_inode_entry, list); 531*127e670aSJaegeuk Kim inode = igrab(entry->inode); 532*127e670aSJaegeuk Kim spin_unlock(&sbi->dir_inode_lock); 533*127e670aSJaegeuk Kim if (inode) { 534*127e670aSJaegeuk Kim filemap_flush(inode->i_mapping); 535*127e670aSJaegeuk Kim iput(inode); 536*127e670aSJaegeuk Kim } else { 537*127e670aSJaegeuk Kim /* 538*127e670aSJaegeuk Kim * We should submit bio, since it exists several 539*127e670aSJaegeuk Kim * wribacking dentry pages in the freeing inode. 540*127e670aSJaegeuk Kim */ 541*127e670aSJaegeuk Kim f2fs_submit_bio(sbi, DATA, true); 542*127e670aSJaegeuk Kim } 543*127e670aSJaegeuk Kim goto retry; 544*127e670aSJaegeuk Kim } 545*127e670aSJaegeuk Kim 546*127e670aSJaegeuk Kim /** 547*127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint. 548*127e670aSJaegeuk Kim */ 549*127e670aSJaegeuk Kim void block_operations(struct f2fs_sb_info *sbi) 550*127e670aSJaegeuk Kim { 551*127e670aSJaegeuk Kim int t; 552*127e670aSJaegeuk Kim struct writeback_control wbc = { 553*127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL, 554*127e670aSJaegeuk Kim .nr_to_write = LONG_MAX, 555*127e670aSJaegeuk Kim .for_reclaim = 0, 556*127e670aSJaegeuk Kim }; 557*127e670aSJaegeuk Kim 558*127e670aSJaegeuk Kim /* Stop renaming operation */ 559*127e670aSJaegeuk Kim mutex_lock_op(sbi, RENAME); 560*127e670aSJaegeuk Kim mutex_lock_op(sbi, DENTRY_OPS); 561*127e670aSJaegeuk Kim 562*127e670aSJaegeuk Kim retry_dents: 563*127e670aSJaegeuk Kim /* write all the dirty dentry pages */ 564*127e670aSJaegeuk Kim sync_dirty_dir_inodes(sbi); 565*127e670aSJaegeuk Kim 566*127e670aSJaegeuk Kim mutex_lock_op(sbi, DATA_WRITE); 567*127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 568*127e670aSJaegeuk Kim mutex_unlock_op(sbi, DATA_WRITE); 569*127e670aSJaegeuk Kim goto retry_dents; 570*127e670aSJaegeuk Kim } 571*127e670aSJaegeuk Kim 572*127e670aSJaegeuk Kim /* block all the operations */ 573*127e670aSJaegeuk Kim for (t = DATA_NEW; t <= NODE_TRUNC; t++) 574*127e670aSJaegeuk Kim mutex_lock_op(sbi, t); 575*127e670aSJaegeuk Kim 576*127e670aSJaegeuk Kim mutex_lock(&sbi->write_inode); 577*127e670aSJaegeuk Kim 578*127e670aSJaegeuk Kim /* 579*127e670aSJaegeuk Kim * POR: we should ensure that there is no dirty node pages 580*127e670aSJaegeuk Kim * until finishing nat/sit flush. 581*127e670aSJaegeuk Kim */ 582*127e670aSJaegeuk Kim retry: 583*127e670aSJaegeuk Kim sync_node_pages(sbi, 0, &wbc); 584*127e670aSJaegeuk Kim 585*127e670aSJaegeuk Kim mutex_lock_op(sbi, NODE_WRITE); 586*127e670aSJaegeuk Kim 587*127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) { 588*127e670aSJaegeuk Kim mutex_unlock_op(sbi, NODE_WRITE); 589*127e670aSJaegeuk Kim goto retry; 590*127e670aSJaegeuk Kim } 591*127e670aSJaegeuk Kim mutex_unlock(&sbi->write_inode); 592*127e670aSJaegeuk Kim } 593*127e670aSJaegeuk Kim 594*127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi) 595*127e670aSJaegeuk Kim { 596*127e670aSJaegeuk Kim int t; 597*127e670aSJaegeuk Kim for (t = NODE_WRITE; t >= RENAME; t--) 598*127e670aSJaegeuk Kim mutex_unlock_op(sbi, t); 599*127e670aSJaegeuk Kim } 600*127e670aSJaegeuk Kim 601*127e670aSJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount) 602*127e670aSJaegeuk Kim { 603*127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 604*127e670aSJaegeuk Kim nid_t last_nid = 0; 605*127e670aSJaegeuk Kim block_t start_blk; 606*127e670aSJaegeuk Kim struct page *cp_page; 607*127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks; 608*127e670aSJaegeuk Kim void *kaddr; 609*127e670aSJaegeuk Kim __u32 crc32 = 0; 610*127e670aSJaegeuk Kim int i; 611*127e670aSJaegeuk Kim 612*127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */ 613*127e670aSJaegeuk Kim while (get_pages(sbi, F2FS_DIRTY_META)) 614*127e670aSJaegeuk Kim sync_meta_pages(sbi, META, LONG_MAX); 615*127e670aSJaegeuk Kim 616*127e670aSJaegeuk Kim next_free_nid(sbi, &last_nid); 617*127e670aSJaegeuk Kim 618*127e670aSJaegeuk Kim /* 619*127e670aSJaegeuk Kim * modify checkpoint 620*127e670aSJaegeuk Kim * version number is already updated 621*127e670aSJaegeuk Kim */ 622*127e670aSJaegeuk Kim ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi)); 623*127e670aSJaegeuk Kim ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); 624*127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); 625*127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 626*127e670aSJaegeuk Kim ckpt->cur_node_segno[i] = 627*127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE)); 628*127e670aSJaegeuk Kim ckpt->cur_node_blkoff[i] = 629*127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE)); 630*127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_NODE] = 631*127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_NODE); 632*127e670aSJaegeuk Kim } 633*127e670aSJaegeuk Kim for (i = 0; i < 3; i++) { 634*127e670aSJaegeuk Kim ckpt->cur_data_segno[i] = 635*127e670aSJaegeuk Kim cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA)); 636*127e670aSJaegeuk Kim ckpt->cur_data_blkoff[i] = 637*127e670aSJaegeuk Kim cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA)); 638*127e670aSJaegeuk Kim ckpt->alloc_type[i + CURSEG_HOT_DATA] = 639*127e670aSJaegeuk Kim curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); 640*127e670aSJaegeuk Kim } 641*127e670aSJaegeuk Kim 642*127e670aSJaegeuk Kim ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); 643*127e670aSJaegeuk Kim ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); 644*127e670aSJaegeuk Kim ckpt->next_free_nid = cpu_to_le32(last_nid); 645*127e670aSJaegeuk Kim 646*127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */ 647*127e670aSJaegeuk Kim data_sum_blocks = npages_for_summary_flush(sbi); 648*127e670aSJaegeuk Kim if (data_sum_blocks < 3) 649*127e670aSJaegeuk Kim ckpt->ckpt_flags |= CP_COMPACT_SUM_FLAG; 650*127e670aSJaegeuk Kim else 651*127e670aSJaegeuk Kim ckpt->ckpt_flags &= (~CP_COMPACT_SUM_FLAG); 652*127e670aSJaegeuk Kim 653*127e670aSJaegeuk Kim orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1) 654*127e670aSJaegeuk Kim / F2FS_ORPHANS_PER_BLOCK; 655*127e670aSJaegeuk Kim ckpt->cp_pack_start_sum = 1 + orphan_blocks; 656*127e670aSJaegeuk Kim ckpt->cp_pack_total_block_count = 2 + data_sum_blocks + orphan_blocks; 657*127e670aSJaegeuk Kim 658*127e670aSJaegeuk Kim if (is_umount) { 659*127e670aSJaegeuk Kim ckpt->ckpt_flags |= CP_UMOUNT_FLAG; 660*127e670aSJaegeuk Kim ckpt->cp_pack_total_block_count += NR_CURSEG_NODE_TYPE; 661*127e670aSJaegeuk Kim } else { 662*127e670aSJaegeuk Kim ckpt->ckpt_flags &= (~CP_UMOUNT_FLAG); 663*127e670aSJaegeuk Kim } 664*127e670aSJaegeuk Kim 665*127e670aSJaegeuk Kim if (sbi->n_orphans) 666*127e670aSJaegeuk Kim ckpt->ckpt_flags |= CP_ORPHAN_PRESENT_FLAG; 667*127e670aSJaegeuk Kim else 668*127e670aSJaegeuk Kim ckpt->ckpt_flags &= (~CP_ORPHAN_PRESENT_FLAG); 669*127e670aSJaegeuk Kim 670*127e670aSJaegeuk Kim /* update SIT/NAT bitmap */ 671*127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); 672*127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); 673*127e670aSJaegeuk Kim 674*127e670aSJaegeuk Kim crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset)); 675*127e670aSJaegeuk Kim *(__u32 *)((unsigned char *)ckpt + 676*127e670aSJaegeuk Kim le32_to_cpu(ckpt->checksum_offset)) 677*127e670aSJaegeuk Kim = cpu_to_le32(crc32); 678*127e670aSJaegeuk Kim 679*127e670aSJaegeuk Kim start_blk = __start_cp_addr(sbi); 680*127e670aSJaegeuk Kim 681*127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */ 682*127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk++); 683*127e670aSJaegeuk Kim kaddr = page_address(cp_page); 684*127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 685*127e670aSJaegeuk Kim set_page_dirty(cp_page); 686*127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 687*127e670aSJaegeuk Kim 688*127e670aSJaegeuk Kim if (sbi->n_orphans) { 689*127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk); 690*127e670aSJaegeuk Kim start_blk += orphan_blocks; 691*127e670aSJaegeuk Kim } 692*127e670aSJaegeuk Kim 693*127e670aSJaegeuk Kim write_data_summaries(sbi, start_blk); 694*127e670aSJaegeuk Kim start_blk += data_sum_blocks; 695*127e670aSJaegeuk Kim if (is_umount) { 696*127e670aSJaegeuk Kim write_node_summaries(sbi, start_blk); 697*127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE; 698*127e670aSJaegeuk Kim } 699*127e670aSJaegeuk Kim 700*127e670aSJaegeuk Kim /* writeout checkpoint block */ 701*127e670aSJaegeuk Kim cp_page = grab_meta_page(sbi, start_blk); 702*127e670aSJaegeuk Kim kaddr = page_address(cp_page); 703*127e670aSJaegeuk Kim memcpy(kaddr, ckpt, (1 << sbi->log_blocksize)); 704*127e670aSJaegeuk Kim set_page_dirty(cp_page); 705*127e670aSJaegeuk Kim f2fs_put_page(cp_page, 1); 706*127e670aSJaegeuk Kim 707*127e670aSJaegeuk Kim /* wait for previous submitted node/meta pages writeback */ 708*127e670aSJaegeuk Kim while (get_pages(sbi, F2FS_WRITEBACK)) 709*127e670aSJaegeuk Kim congestion_wait(BLK_RW_ASYNC, HZ / 50); 710*127e670aSJaegeuk Kim 711*127e670aSJaegeuk Kim filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX); 712*127e670aSJaegeuk Kim filemap_fdatawait_range(sbi->meta_inode->i_mapping, 0, LONG_MAX); 713*127e670aSJaegeuk Kim 714*127e670aSJaegeuk Kim /* update user_block_counts */ 715*127e670aSJaegeuk Kim sbi->last_valid_block_count = sbi->total_valid_block_count; 716*127e670aSJaegeuk Kim sbi->alloc_valid_block_count = 0; 717*127e670aSJaegeuk Kim 718*127e670aSJaegeuk Kim /* Here, we only have one bio having CP pack */ 719*127e670aSJaegeuk Kim if (sbi->ckpt->ckpt_flags & CP_ERROR_FLAG) 720*127e670aSJaegeuk Kim sbi->sb->s_flags |= MS_RDONLY; 721*127e670aSJaegeuk Kim else 722*127e670aSJaegeuk Kim sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 723*127e670aSJaegeuk Kim 724*127e670aSJaegeuk Kim clear_prefree_segments(sbi); 725*127e670aSJaegeuk Kim F2FS_RESET_SB_DIRT(sbi); 726*127e670aSJaegeuk Kim } 727*127e670aSJaegeuk Kim 728*127e670aSJaegeuk Kim /** 729*127e670aSJaegeuk Kim * We guarantee that this checkpoint procedure should not fail. 730*127e670aSJaegeuk Kim */ 731*127e670aSJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, bool blocked, bool is_umount) 732*127e670aSJaegeuk Kim { 733*127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 734*127e670aSJaegeuk Kim unsigned long long ckpt_ver; 735*127e670aSJaegeuk Kim 736*127e670aSJaegeuk Kim if (!blocked) { 737*127e670aSJaegeuk Kim mutex_lock(&sbi->cp_mutex); 738*127e670aSJaegeuk Kim block_operations(sbi); 739*127e670aSJaegeuk Kim } 740*127e670aSJaegeuk Kim 741*127e670aSJaegeuk Kim f2fs_submit_bio(sbi, DATA, true); 742*127e670aSJaegeuk Kim f2fs_submit_bio(sbi, NODE, true); 743*127e670aSJaegeuk Kim f2fs_submit_bio(sbi, META, true); 744*127e670aSJaegeuk Kim 745*127e670aSJaegeuk Kim /* 746*127e670aSJaegeuk Kim * update checkpoint pack index 747*127e670aSJaegeuk Kim * Increase the version number so that 748*127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place 749*127e670aSJaegeuk Kim */ 750*127e670aSJaegeuk Kim ckpt_ver = le64_to_cpu(ckpt->checkpoint_ver); 751*127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); 752*127e670aSJaegeuk Kim 753*127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */ 754*127e670aSJaegeuk Kim flush_nat_entries(sbi); 755*127e670aSJaegeuk Kim flush_sit_entries(sbi); 756*127e670aSJaegeuk Kim 757*127e670aSJaegeuk Kim reset_victim_segmap(sbi); 758*127e670aSJaegeuk Kim 759*127e670aSJaegeuk Kim /* unlock all the fs_lock[] in do_checkpoint() */ 760*127e670aSJaegeuk Kim do_checkpoint(sbi, is_umount); 761*127e670aSJaegeuk Kim 762*127e670aSJaegeuk Kim unblock_operations(sbi); 763*127e670aSJaegeuk Kim mutex_unlock(&sbi->cp_mutex); 764*127e670aSJaegeuk Kim } 765*127e670aSJaegeuk Kim 766*127e670aSJaegeuk Kim void init_orphan_info(struct f2fs_sb_info *sbi) 767*127e670aSJaegeuk Kim { 768*127e670aSJaegeuk Kim mutex_init(&sbi->orphan_inode_mutex); 769*127e670aSJaegeuk Kim INIT_LIST_HEAD(&sbi->orphan_inode_list); 770*127e670aSJaegeuk Kim sbi->n_orphans = 0; 771*127e670aSJaegeuk Kim } 772*127e670aSJaegeuk Kim 773*127e670aSJaegeuk Kim int create_checkpoint_caches(void) 774*127e670aSJaegeuk Kim { 775*127e670aSJaegeuk Kim orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry", 776*127e670aSJaegeuk Kim sizeof(struct orphan_inode_entry), NULL); 777*127e670aSJaegeuk Kim if (unlikely(!orphan_entry_slab)) 778*127e670aSJaegeuk Kim return -ENOMEM; 779*127e670aSJaegeuk Kim inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry", 780*127e670aSJaegeuk Kim sizeof(struct dir_inode_entry), NULL); 781*127e670aSJaegeuk Kim if (unlikely(!inode_entry_slab)) { 782*127e670aSJaegeuk Kim kmem_cache_destroy(orphan_entry_slab); 783*127e670aSJaegeuk Kim return -ENOMEM; 784*127e670aSJaegeuk Kim } 785*127e670aSJaegeuk Kim return 0; 786*127e670aSJaegeuk Kim } 787*127e670aSJaegeuk Kim 788*127e670aSJaegeuk Kim void destroy_checkpoint_caches(void) 789*127e670aSJaegeuk Kim { 790*127e670aSJaegeuk Kim kmem_cache_destroy(orphan_entry_slab); 791*127e670aSJaegeuk Kim kmem_cache_destroy(inode_entry_slab); 792*127e670aSJaegeuk Kim } 793