checkpoint.c 23 KB

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