checkpoint.c 21 KB

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