checkpoint.c 22 KB

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