checkpoint.c 21 KB

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