checkpoint.c 20 KB

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