checkpoint.c 21 KB

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