checkpoint.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. static struct kmem_cache *orphan_entry_slab;
  23. static struct kmem_cache *inode_entry_slab;
  24. /*
  25. * We guarantee no failure on the returned page.
  26. */
  27. struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
  28. {
  29. struct address_space *mapping = sbi->meta_inode->i_mapping;
  30. struct page *page = NULL;
  31. repeat:
  32. page = grab_cache_page(mapping, index);
  33. if (!page) {
  34. cond_resched();
  35. goto repeat;
  36. }
  37. /* We wait writeback only inside grab_meta_page() */
  38. wait_on_page_writeback(page);
  39. SetPageUptodate(page);
  40. return page;
  41. }
  42. /*
  43. * We guarantee no failure on the returned page.
  44. */
  45. struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
  46. {
  47. struct address_space *mapping = sbi->meta_inode->i_mapping;
  48. struct page *page;
  49. repeat:
  50. page = grab_cache_page(mapping, index);
  51. if (!page) {
  52. cond_resched();
  53. goto repeat;
  54. }
  55. if (f2fs_readpage(sbi, page, index, READ_SYNC)) {
  56. f2fs_put_page(page, 1);
  57. goto repeat;
  58. }
  59. mark_page_accessed(page);
  60. /* We do not allow returning an errorneous page */
  61. return page;
  62. }
  63. static int f2fs_write_meta_page(struct page *page,
  64. struct writeback_control *wbc)
  65. {
  66. struct inode *inode = page->mapping->host;
  67. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  68. /* Should not write any meta pages, if any IO error was occurred */
  69. if (wbc->for_reclaim ||
  70. is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG)) {
  71. dec_page_count(sbi, F2FS_DIRTY_META);
  72. wbc->pages_skipped++;
  73. set_page_dirty(page);
  74. return AOP_WRITEPAGE_ACTIVATE;
  75. }
  76. wait_on_page_writeback(page);
  77. write_meta_page(sbi, page);
  78. dec_page_count(sbi, F2FS_DIRTY_META);
  79. unlock_page(page);
  80. return 0;
  81. }
  82. static int f2fs_write_meta_pages(struct address_space *mapping,
  83. struct writeback_control *wbc)
  84. {
  85. struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
  86. struct block_device *bdev = sbi->sb->s_bdev;
  87. long written;
  88. if (wbc->for_kupdate)
  89. return 0;
  90. if (get_pages(sbi, F2FS_DIRTY_META) == 0)
  91. return 0;
  92. /* if mounting is failed, skip writing node pages */
  93. mutex_lock(&sbi->cp_mutex);
  94. written = sync_meta_pages(sbi, META, bio_get_nr_vecs(bdev));
  95. mutex_unlock(&sbi->cp_mutex);
  96. wbc->nr_to_write -= written;
  97. return 0;
  98. }
  99. long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
  100. long nr_to_write)
  101. {
  102. struct address_space *mapping = sbi->meta_inode->i_mapping;
  103. pgoff_t index = 0, end = LONG_MAX;
  104. struct pagevec pvec;
  105. long nwritten = 0;
  106. struct writeback_control wbc = {
  107. .for_reclaim = 0,
  108. };
  109. pagevec_init(&pvec, 0);
  110. while (index <= end) {
  111. int i, nr_pages;
  112. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  113. PAGECACHE_TAG_DIRTY,
  114. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  115. if (nr_pages == 0)
  116. break;
  117. for (i = 0; i < nr_pages; i++) {
  118. struct page *page = pvec.pages[i];
  119. lock_page(page);
  120. BUG_ON(page->mapping != mapping);
  121. BUG_ON(!PageDirty(page));
  122. clear_page_dirty_for_io(page);
  123. if (f2fs_write_meta_page(page, &wbc)) {
  124. unlock_page(page);
  125. break;
  126. }
  127. if (nwritten++ >= nr_to_write)
  128. break;
  129. }
  130. pagevec_release(&pvec);
  131. cond_resched();
  132. }
  133. if (nwritten)
  134. f2fs_submit_bio(sbi, type, nr_to_write == LONG_MAX);
  135. return nwritten;
  136. }
  137. static int f2fs_set_meta_page_dirty(struct page *page)
  138. {
  139. struct address_space *mapping = page->mapping;
  140. struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
  141. SetPageUptodate(page);
  142. if (!PageDirty(page)) {
  143. __set_page_dirty_nobuffers(page);
  144. inc_page_count(sbi, F2FS_DIRTY_META);
  145. F2FS_SET_SB_DIRT(sbi);
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. const struct address_space_operations f2fs_meta_aops = {
  151. .writepage = f2fs_write_meta_page,
  152. .writepages = f2fs_write_meta_pages,
  153. .set_page_dirty = f2fs_set_meta_page_dirty,
  154. };
  155. int check_orphan_space(struct f2fs_sb_info *sbi)
  156. {
  157. unsigned int max_orphans;
  158. int err = 0;
  159. /*
  160. * considering 512 blocks in a segment 5 blocks are needed for cp
  161. * and log segment summaries. Remaining blocks are used to keep
  162. * orphan entries with the limitation one reserved segment
  163. * for cp pack we can have max 1020*507 orphan entries
  164. */
  165. max_orphans = (sbi->blocks_per_seg - 5) * F2FS_ORPHANS_PER_BLOCK;
  166. mutex_lock(&sbi->orphan_inode_mutex);
  167. if (sbi->n_orphans >= max_orphans)
  168. err = -ENOSPC;
  169. mutex_unlock(&sbi->orphan_inode_mutex);
  170. return err;
  171. }
  172. void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  173. {
  174. struct list_head *head, *this;
  175. struct orphan_inode_entry *new = NULL, *orphan = NULL;
  176. mutex_lock(&sbi->orphan_inode_mutex);
  177. head = &sbi->orphan_inode_list;
  178. list_for_each(this, head) {
  179. orphan = list_entry(this, struct orphan_inode_entry, list);
  180. if (orphan->ino == ino)
  181. goto out;
  182. if (orphan->ino > ino)
  183. break;
  184. orphan = NULL;
  185. }
  186. retry:
  187. new = kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC);
  188. if (!new) {
  189. cond_resched();
  190. goto retry;
  191. }
  192. new->ino = ino;
  193. /* add new_oentry into list which is sorted by inode number */
  194. if (orphan) {
  195. struct orphan_inode_entry *prev;
  196. /* get previous entry */
  197. prev = list_entry(orphan->list.prev, typeof(*prev), list);
  198. if (&prev->list != head)
  199. /* insert new orphan inode entry */
  200. list_add(&new->list, &prev->list);
  201. else
  202. list_add(&new->list, head);
  203. } else {
  204. list_add_tail(&new->list, head);
  205. }
  206. sbi->n_orphans++;
  207. out:
  208. mutex_unlock(&sbi->orphan_inode_mutex);
  209. }
  210. void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  211. {
  212. struct list_head *this, *next, *head;
  213. struct orphan_inode_entry *orphan;
  214. mutex_lock(&sbi->orphan_inode_mutex);
  215. head = &sbi->orphan_inode_list;
  216. list_for_each_safe(this, next, head) {
  217. orphan = list_entry(this, struct orphan_inode_entry, list);
  218. if (orphan->ino == ino) {
  219. list_del(&orphan->list);
  220. kmem_cache_free(orphan_entry_slab, orphan);
  221. sbi->n_orphans--;
  222. break;
  223. }
  224. }
  225. mutex_unlock(&sbi->orphan_inode_mutex);
  226. }
  227. static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  228. {
  229. struct inode *inode = f2fs_iget(sbi->sb, ino);
  230. BUG_ON(IS_ERR(inode));
  231. clear_nlink(inode);
  232. /* truncate all the data during iput */
  233. iput(inode);
  234. }
  235. int recover_orphan_inodes(struct f2fs_sb_info *sbi)
  236. {
  237. block_t start_blk, orphan_blkaddr, i, j;
  238. if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
  239. return 0;
  240. sbi->por_doing = 1;
  241. start_blk = __start_cp_addr(sbi) + 1;
  242. orphan_blkaddr = __start_sum_addr(sbi) - 1;
  243. for (i = 0; i < orphan_blkaddr; i++) {
  244. struct page *page = get_meta_page(sbi, start_blk + i);
  245. struct f2fs_orphan_block *orphan_blk;
  246. orphan_blk = (struct f2fs_orphan_block *)page_address(page);
  247. for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
  248. nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
  249. recover_orphan_inode(sbi, ino);
  250. }
  251. f2fs_put_page(page, 1);
  252. }
  253. /* clear Orphan Flag */
  254. clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
  255. sbi->por_doing = 0;
  256. return 0;
  257. }
  258. static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
  259. {
  260. struct list_head *head, *this, *next;
  261. struct f2fs_orphan_block *orphan_blk = NULL;
  262. struct page *page = NULL;
  263. unsigned int nentries = 0;
  264. unsigned short index = 1;
  265. unsigned short orphan_blocks;
  266. orphan_blocks = (unsigned short)((sbi->n_orphans +
  267. (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
  268. mutex_lock(&sbi->orphan_inode_mutex);
  269. head = &sbi->orphan_inode_list;
  270. /* loop for each orphan inode entry and write them in Jornal block */
  271. list_for_each_safe(this, next, head) {
  272. struct orphan_inode_entry *orphan;
  273. orphan = list_entry(this, struct orphan_inode_entry, list);
  274. if (nentries == F2FS_ORPHANS_PER_BLOCK) {
  275. /*
  276. * an orphan block is full of 1020 entries,
  277. * then we need to flush current orphan blocks
  278. * and bring another one in memory
  279. */
  280. orphan_blk->blk_addr = cpu_to_le16(index);
  281. orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
  282. orphan_blk->entry_count = cpu_to_le32(nentries);
  283. set_page_dirty(page);
  284. f2fs_put_page(page, 1);
  285. index++;
  286. start_blk++;
  287. nentries = 0;
  288. page = NULL;
  289. }
  290. if (page)
  291. goto page_exist;
  292. page = grab_meta_page(sbi, start_blk);
  293. orphan_blk = (struct f2fs_orphan_block *)page_address(page);
  294. memset(orphan_blk, 0, sizeof(*orphan_blk));
  295. page_exist:
  296. orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
  297. }
  298. if (!page)
  299. goto end;
  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. end:
  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. unsigned int crc = 0;
  316. size_t crc_offset;
  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 = *(unsigned int *)((unsigned char *)cp_block + crc_offset);
  325. if (!f2fs_crc_valid(crc, cp_block, crc_offset))
  326. goto invalid_cp1;
  327. pre_version = le64_to_cpu(cp_block->checkpoint_ver);
  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 = *(unsigned int *)((unsigned char *)cp_block + crc_offset);
  336. if (!f2fs_crc_valid(crc, cp_block, crc_offset))
  337. goto invalid_cp2;
  338. cur_version = le64_to_cpu(cp_block->checkpoint_ver);
  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 += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
  369. cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
  370. if (cp1 && cp2) {
  371. if (ver_after(cp2_version, cp1_version))
  372. cur_page = cp2;
  373. else
  374. cur_page = cp1;
  375. } else if (cp1) {
  376. cur_page = cp1;
  377. } else if (cp2) {
  378. cur_page = cp2;
  379. } else {
  380. goto fail_no_cp;
  381. }
  382. cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
  383. memcpy(sbi->ckpt, cp_block, blk_size);
  384. f2fs_put_page(cp1, 1);
  385. f2fs_put_page(cp2, 1);
  386. return 0;
  387. fail_no_cp:
  388. kfree(sbi->ckpt);
  389. return -EINVAL;
  390. }
  391. void set_dirty_dir_page(struct inode *inode, struct page *page)
  392. {
  393. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  394. struct list_head *head = &sbi->dir_inode_list;
  395. struct dir_inode_entry *new;
  396. struct list_head *this;
  397. if (!S_ISDIR(inode->i_mode))
  398. return;
  399. retry:
  400. new = kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
  401. if (!new) {
  402. cond_resched();
  403. goto retry;
  404. }
  405. new->inode = inode;
  406. INIT_LIST_HEAD(&new->list);
  407. spin_lock(&sbi->dir_inode_lock);
  408. list_for_each(this, head) {
  409. struct dir_inode_entry *entry;
  410. entry = list_entry(this, struct dir_inode_entry, list);
  411. if (entry->inode == inode) {
  412. kmem_cache_free(inode_entry_slab, new);
  413. goto out;
  414. }
  415. }
  416. list_add_tail(&new->list, head);
  417. sbi->n_dirty_dirs++;
  418. BUG_ON(!S_ISDIR(inode->i_mode));
  419. out:
  420. inc_page_count(sbi, F2FS_DIRTY_DENTS);
  421. inode_inc_dirty_dents(inode);
  422. SetPagePrivate(page);
  423. spin_unlock(&sbi->dir_inode_lock);
  424. }
  425. void remove_dirty_dir_inode(struct inode *inode)
  426. {
  427. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  428. struct list_head *head = &sbi->dir_inode_list;
  429. struct list_head *this;
  430. if (!S_ISDIR(inode->i_mode))
  431. return;
  432. spin_lock(&sbi->dir_inode_lock);
  433. if (atomic_read(&F2FS_I(inode)->dirty_dents))
  434. goto out;
  435. list_for_each(this, head) {
  436. struct dir_inode_entry *entry;
  437. entry = list_entry(this, struct dir_inode_entry, list);
  438. if (entry->inode == inode) {
  439. list_del(&entry->list);
  440. kmem_cache_free(inode_entry_slab, entry);
  441. sbi->n_dirty_dirs--;
  442. break;
  443. }
  444. }
  445. out:
  446. spin_unlock(&sbi->dir_inode_lock);
  447. }
  448. void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
  449. {
  450. struct list_head *head = &sbi->dir_inode_list;
  451. struct dir_inode_entry *entry;
  452. struct inode *inode;
  453. retry:
  454. spin_lock(&sbi->dir_inode_lock);
  455. if (list_empty(head)) {
  456. spin_unlock(&sbi->dir_inode_lock);
  457. return;
  458. }
  459. entry = list_entry(head->next, struct dir_inode_entry, list);
  460. inode = igrab(entry->inode);
  461. spin_unlock(&sbi->dir_inode_lock);
  462. if (inode) {
  463. filemap_flush(inode->i_mapping);
  464. iput(inode);
  465. } else {
  466. /*
  467. * We should submit bio, since it exists several
  468. * wribacking dentry pages in the freeing inode.
  469. */
  470. f2fs_submit_bio(sbi, DATA, true);
  471. }
  472. goto retry;
  473. }
  474. /*
  475. * Freeze all the FS-operations for checkpoint.
  476. */
  477. void block_operations(struct f2fs_sb_info *sbi)
  478. {
  479. int t;
  480. struct writeback_control wbc = {
  481. .sync_mode = WB_SYNC_ALL,
  482. .nr_to_write = LONG_MAX,
  483. .for_reclaim = 0,
  484. };
  485. /* Stop renaming operation */
  486. mutex_lock_op(sbi, RENAME);
  487. mutex_lock_op(sbi, DENTRY_OPS);
  488. retry_dents:
  489. /* write all the dirty dentry pages */
  490. sync_dirty_dir_inodes(sbi);
  491. mutex_lock_op(sbi, DATA_WRITE);
  492. if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
  493. mutex_unlock_op(sbi, DATA_WRITE);
  494. goto retry_dents;
  495. }
  496. /* block all the operations */
  497. for (t = DATA_NEW; t <= NODE_TRUNC; t++)
  498. mutex_lock_op(sbi, t);
  499. mutex_lock(&sbi->write_inode);
  500. /*
  501. * POR: we should ensure that there is no dirty node pages
  502. * until finishing nat/sit flush.
  503. */
  504. retry:
  505. sync_node_pages(sbi, 0, &wbc);
  506. mutex_lock_op(sbi, NODE_WRITE);
  507. if (get_pages(sbi, F2FS_DIRTY_NODES)) {
  508. mutex_unlock_op(sbi, NODE_WRITE);
  509. goto retry;
  510. }
  511. mutex_unlock(&sbi->write_inode);
  512. }
  513. static void unblock_operations(struct f2fs_sb_info *sbi)
  514. {
  515. int t;
  516. for (t = NODE_WRITE; t >= RENAME; t--)
  517. mutex_unlock_op(sbi, t);
  518. }
  519. static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
  520. {
  521. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  522. nid_t last_nid = 0;
  523. block_t start_blk;
  524. struct page *cp_page;
  525. unsigned int data_sum_blocks, orphan_blocks;
  526. unsigned int crc32 = 0;
  527. void *kaddr;
  528. int i;
  529. /* Flush all the NAT/SIT pages */
  530. while (get_pages(sbi, F2FS_DIRTY_META))
  531. sync_meta_pages(sbi, META, LONG_MAX);
  532. next_free_nid(sbi, &last_nid);
  533. /*
  534. * modify checkpoint
  535. * version number is already updated
  536. */
  537. ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
  538. ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
  539. ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
  540. for (i = 0; i < 3; i++) {
  541. ckpt->cur_node_segno[i] =
  542. cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
  543. ckpt->cur_node_blkoff[i] =
  544. cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
  545. ckpt->alloc_type[i + CURSEG_HOT_NODE] =
  546. curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
  547. }
  548. for (i = 0; i < 3; i++) {
  549. ckpt->cur_data_segno[i] =
  550. cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
  551. ckpt->cur_data_blkoff[i] =
  552. cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
  553. ckpt->alloc_type[i + CURSEG_HOT_DATA] =
  554. curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
  555. }
  556. ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
  557. ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
  558. ckpt->next_free_nid = cpu_to_le32(last_nid);
  559. /* 2 cp + n data seg summary + orphan inode blocks */
  560. data_sum_blocks = npages_for_summary_flush(sbi);
  561. if (data_sum_blocks < 3)
  562. set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
  563. else
  564. clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
  565. orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
  566. / F2FS_ORPHANS_PER_BLOCK;
  567. ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks);
  568. if (is_umount) {
  569. set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
  570. ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
  571. data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE);
  572. } else {
  573. clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
  574. ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
  575. data_sum_blocks + orphan_blocks);
  576. }
  577. if (sbi->n_orphans)
  578. set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
  579. else
  580. clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
  581. /* update SIT/NAT bitmap */
  582. get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
  583. get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
  584. crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
  585. *(__le32 *)((unsigned char *)ckpt +
  586. le32_to_cpu(ckpt->checksum_offset))
  587. = cpu_to_le32(crc32);
  588. start_blk = __start_cp_addr(sbi);
  589. /* write out checkpoint buffer at block 0 */
  590. cp_page = grab_meta_page(sbi, start_blk++);
  591. kaddr = page_address(cp_page);
  592. memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
  593. set_page_dirty(cp_page);
  594. f2fs_put_page(cp_page, 1);
  595. if (sbi->n_orphans) {
  596. write_orphan_inodes(sbi, start_blk);
  597. start_blk += orphan_blocks;
  598. }
  599. write_data_summaries(sbi, start_blk);
  600. start_blk += data_sum_blocks;
  601. if (is_umount) {
  602. write_node_summaries(sbi, start_blk);
  603. start_blk += NR_CURSEG_NODE_TYPE;
  604. }
  605. /* writeout checkpoint block */
  606. cp_page = grab_meta_page(sbi, start_blk);
  607. kaddr = page_address(cp_page);
  608. memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
  609. set_page_dirty(cp_page);
  610. f2fs_put_page(cp_page, 1);
  611. /* wait for previous submitted node/meta pages writeback */
  612. while (get_pages(sbi, F2FS_WRITEBACK))
  613. congestion_wait(BLK_RW_ASYNC, HZ / 50);
  614. filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX);
  615. filemap_fdatawait_range(sbi->meta_inode->i_mapping, 0, LONG_MAX);
  616. /* update user_block_counts */
  617. sbi->last_valid_block_count = sbi->total_valid_block_count;
  618. sbi->alloc_valid_block_count = 0;
  619. /* Here, we only have one bio having CP pack */
  620. sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
  621. if (!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
  622. clear_prefree_segments(sbi);
  623. F2FS_RESET_SB_DIRT(sbi);
  624. }
  625. }
  626. /*
  627. * We guarantee that this checkpoint procedure should not fail.
  628. */
  629. void write_checkpoint(struct f2fs_sb_info *sbi, bool blocked, bool is_umount)
  630. {
  631. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  632. unsigned long long ckpt_ver;
  633. if (!blocked) {
  634. mutex_lock(&sbi->cp_mutex);
  635. block_operations(sbi);
  636. }
  637. f2fs_submit_bio(sbi, DATA, true);
  638. f2fs_submit_bio(sbi, NODE, true);
  639. f2fs_submit_bio(sbi, META, true);
  640. /*
  641. * update checkpoint pack index
  642. * Increase the version number so that
  643. * SIT entries and seg summaries are written at correct place
  644. */
  645. ckpt_ver = le64_to_cpu(ckpt->checkpoint_ver);
  646. ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
  647. /* write cached NAT/SIT entries to NAT/SIT area */
  648. flush_nat_entries(sbi);
  649. flush_sit_entries(sbi);
  650. reset_victim_segmap(sbi);
  651. /* unlock all the fs_lock[] in do_checkpoint() */
  652. do_checkpoint(sbi, is_umount);
  653. unblock_operations(sbi);
  654. mutex_unlock(&sbi->cp_mutex);
  655. }
  656. void init_orphan_info(struct f2fs_sb_info *sbi)
  657. {
  658. mutex_init(&sbi->orphan_inode_mutex);
  659. INIT_LIST_HEAD(&sbi->orphan_inode_list);
  660. sbi->n_orphans = 0;
  661. }
  662. int __init create_checkpoint_caches(void)
  663. {
  664. orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
  665. sizeof(struct orphan_inode_entry), NULL);
  666. if (unlikely(!orphan_entry_slab))
  667. return -ENOMEM;
  668. inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
  669. sizeof(struct dir_inode_entry), NULL);
  670. if (unlikely(!inode_entry_slab)) {
  671. kmem_cache_destroy(orphan_entry_slab);
  672. return -ENOMEM;
  673. }
  674. return 0;
  675. }
  676. void destroy_checkpoint_caches(void)
  677. {
  678. kmem_cache_destroy(orphan_entry_slab);
  679. kmem_cache_destroy(inode_entry_slab);
  680. }