checkpoint.c 20 KB

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