checkpoint.c 20 KB

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