checkpoint.c 20 KB

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