checkpoint.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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.h"
  23. #include <trace/events/f2fs.h>
  24. static struct kmem_cache *ino_entry_slab;
  25. struct kmem_cache *inode_entry_slab;
  26. void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
  27. {
  28. set_ckpt_flags(sbi, CP_ERROR_FLAG);
  29. sbi->sb->s_flags |= MS_RDONLY;
  30. if (!end_io)
  31. f2fs_flush_merged_bios(sbi);
  32. }
  33. /*
  34. * We guarantee no failure on the returned page.
  35. */
  36. struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
  37. {
  38. struct address_space *mapping = META_MAPPING(sbi);
  39. struct page *page = NULL;
  40. repeat:
  41. page = f2fs_grab_cache_page(mapping, index, false);
  42. if (!page) {
  43. cond_resched();
  44. goto repeat;
  45. }
  46. f2fs_wait_on_page_writeback(page, META, true);
  47. if (!PageUptodate(page))
  48. SetPageUptodate(page);
  49. return page;
  50. }
  51. /*
  52. * We guarantee no failure on the returned page.
  53. */
  54. static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
  55. bool is_meta)
  56. {
  57. struct address_space *mapping = META_MAPPING(sbi);
  58. struct page *page;
  59. struct f2fs_io_info fio = {
  60. .sbi = sbi,
  61. .type = META,
  62. .rw = READ_SYNC | REQ_META | REQ_PRIO,
  63. .old_blkaddr = index,
  64. .new_blkaddr = index,
  65. .encrypted_page = NULL,
  66. };
  67. if (unlikely(!is_meta))
  68. fio.rw &= ~REQ_META;
  69. repeat:
  70. page = f2fs_grab_cache_page(mapping, index, false);
  71. if (!page) {
  72. cond_resched();
  73. goto repeat;
  74. }
  75. if (PageUptodate(page))
  76. goto out;
  77. fio.page = page;
  78. if (f2fs_submit_page_bio(&fio)) {
  79. f2fs_put_page(page, 1);
  80. goto repeat;
  81. }
  82. lock_page(page);
  83. if (unlikely(page->mapping != mapping)) {
  84. f2fs_put_page(page, 1);
  85. goto repeat;
  86. }
  87. /*
  88. * if there is any IO error when accessing device, make our filesystem
  89. * readonly and make sure do not write checkpoint with non-uptodate
  90. * meta page.
  91. */
  92. if (unlikely(!PageUptodate(page)))
  93. f2fs_stop_checkpoint(sbi, false);
  94. out:
  95. mark_page_accessed(page);
  96. return page;
  97. }
  98. struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
  99. {
  100. return __get_meta_page(sbi, index, true);
  101. }
  102. /* for POR only */
  103. struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
  104. {
  105. return __get_meta_page(sbi, index, false);
  106. }
  107. bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
  108. {
  109. switch (type) {
  110. case META_NAT:
  111. break;
  112. case META_SIT:
  113. if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
  114. return false;
  115. break;
  116. case META_SSA:
  117. if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
  118. blkaddr < SM_I(sbi)->ssa_blkaddr))
  119. return false;
  120. break;
  121. case META_CP:
  122. if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
  123. blkaddr < __start_cp_addr(sbi)))
  124. return false;
  125. break;
  126. case META_POR:
  127. if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
  128. blkaddr < MAIN_BLKADDR(sbi)))
  129. return false;
  130. break;
  131. default:
  132. BUG();
  133. }
  134. return true;
  135. }
  136. /*
  137. * Readahead CP/NAT/SIT/SSA pages
  138. */
  139. int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
  140. int type, bool sync)
  141. {
  142. struct page *page;
  143. block_t blkno = start;
  144. struct f2fs_io_info fio = {
  145. .sbi = sbi,
  146. .type = META,
  147. .rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA,
  148. .encrypted_page = NULL,
  149. };
  150. struct blk_plug plug;
  151. if (unlikely(type == META_POR))
  152. fio.rw &= ~REQ_META;
  153. blk_start_plug(&plug);
  154. for (; nrpages-- > 0; blkno++) {
  155. if (!is_valid_blkaddr(sbi, blkno, type))
  156. goto out;
  157. switch (type) {
  158. case META_NAT:
  159. if (unlikely(blkno >=
  160. NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
  161. blkno = 0;
  162. /* get nat block addr */
  163. fio.new_blkaddr = current_nat_addr(sbi,
  164. blkno * NAT_ENTRY_PER_BLOCK);
  165. break;
  166. case META_SIT:
  167. /* get sit block addr */
  168. fio.new_blkaddr = current_sit_addr(sbi,
  169. blkno * SIT_ENTRY_PER_BLOCK);
  170. break;
  171. case META_SSA:
  172. case META_CP:
  173. case META_POR:
  174. fio.new_blkaddr = blkno;
  175. break;
  176. default:
  177. BUG();
  178. }
  179. page = f2fs_grab_cache_page(META_MAPPING(sbi),
  180. fio.new_blkaddr, false);
  181. if (!page)
  182. continue;
  183. if (PageUptodate(page)) {
  184. f2fs_put_page(page, 1);
  185. continue;
  186. }
  187. fio.page = page;
  188. fio.old_blkaddr = fio.new_blkaddr;
  189. f2fs_submit_page_mbio(&fio);
  190. f2fs_put_page(page, 0);
  191. }
  192. out:
  193. f2fs_submit_merged_bio(sbi, META, READ);
  194. blk_finish_plug(&plug);
  195. return blkno - start;
  196. }
  197. void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
  198. {
  199. struct page *page;
  200. bool readahead = false;
  201. page = find_get_page(META_MAPPING(sbi), index);
  202. if (!page || !PageUptodate(page))
  203. readahead = true;
  204. f2fs_put_page(page, 0);
  205. if (readahead)
  206. ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
  207. }
  208. static int f2fs_write_meta_page(struct page *page,
  209. struct writeback_control *wbc)
  210. {
  211. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  212. trace_f2fs_writepage(page, META);
  213. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  214. goto redirty_out;
  215. if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
  216. goto redirty_out;
  217. if (unlikely(f2fs_cp_error(sbi)))
  218. goto redirty_out;
  219. write_meta_page(sbi, page);
  220. dec_page_count(sbi, F2FS_DIRTY_META);
  221. if (wbc->for_reclaim)
  222. f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE);
  223. unlock_page(page);
  224. if (unlikely(f2fs_cp_error(sbi)))
  225. f2fs_submit_merged_bio(sbi, META, WRITE);
  226. return 0;
  227. redirty_out:
  228. redirty_page_for_writepage(wbc, page);
  229. return AOP_WRITEPAGE_ACTIVATE;
  230. }
  231. static int f2fs_write_meta_pages(struct address_space *mapping,
  232. struct writeback_control *wbc)
  233. {
  234. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  235. long diff, written;
  236. /* collect a number of dirty meta pages and write together */
  237. if (wbc->for_kupdate ||
  238. get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
  239. goto skip_write;
  240. trace_f2fs_writepages(mapping->host, wbc, META);
  241. /* if mounting is failed, skip writing node pages */
  242. mutex_lock(&sbi->cp_mutex);
  243. diff = nr_pages_to_write(sbi, META, wbc);
  244. written = sync_meta_pages(sbi, META, wbc->nr_to_write);
  245. mutex_unlock(&sbi->cp_mutex);
  246. wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
  247. return 0;
  248. skip_write:
  249. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
  250. trace_f2fs_writepages(mapping->host, wbc, META);
  251. return 0;
  252. }
  253. long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
  254. long nr_to_write)
  255. {
  256. struct address_space *mapping = META_MAPPING(sbi);
  257. pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
  258. struct pagevec pvec;
  259. long nwritten = 0;
  260. struct writeback_control wbc = {
  261. .for_reclaim = 0,
  262. };
  263. struct blk_plug plug;
  264. pagevec_init(&pvec, 0);
  265. blk_start_plug(&plug);
  266. while (index <= end) {
  267. int i, nr_pages;
  268. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  269. PAGECACHE_TAG_DIRTY,
  270. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  271. if (unlikely(nr_pages == 0))
  272. break;
  273. for (i = 0; i < nr_pages; i++) {
  274. struct page *page = pvec.pages[i];
  275. if (prev == ULONG_MAX)
  276. prev = page->index - 1;
  277. if (nr_to_write != LONG_MAX && page->index != prev + 1) {
  278. pagevec_release(&pvec);
  279. goto stop;
  280. }
  281. lock_page(page);
  282. if (unlikely(page->mapping != mapping)) {
  283. continue_unlock:
  284. unlock_page(page);
  285. continue;
  286. }
  287. if (!PageDirty(page)) {
  288. /* someone wrote it for us */
  289. goto continue_unlock;
  290. }
  291. f2fs_wait_on_page_writeback(page, META, true);
  292. BUG_ON(PageWriteback(page));
  293. if (!clear_page_dirty_for_io(page))
  294. goto continue_unlock;
  295. if (mapping->a_ops->writepage(page, &wbc)) {
  296. unlock_page(page);
  297. break;
  298. }
  299. nwritten++;
  300. prev = page->index;
  301. if (unlikely(nwritten >= nr_to_write))
  302. break;
  303. }
  304. pagevec_release(&pvec);
  305. cond_resched();
  306. }
  307. stop:
  308. if (nwritten)
  309. f2fs_submit_merged_bio(sbi, type, WRITE);
  310. blk_finish_plug(&plug);
  311. return nwritten;
  312. }
  313. static int f2fs_set_meta_page_dirty(struct page *page)
  314. {
  315. trace_f2fs_set_page_dirty(page, META);
  316. if (!PageUptodate(page))
  317. SetPageUptodate(page);
  318. if (!PageDirty(page)) {
  319. f2fs_set_page_dirty_nobuffers(page);
  320. inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
  321. SetPagePrivate(page);
  322. f2fs_trace_pid(page);
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. const struct address_space_operations f2fs_meta_aops = {
  328. .writepage = f2fs_write_meta_page,
  329. .writepages = f2fs_write_meta_pages,
  330. .set_page_dirty = f2fs_set_meta_page_dirty,
  331. .invalidatepage = f2fs_invalidate_page,
  332. .releasepage = f2fs_release_page,
  333. };
  334. static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
  335. {
  336. struct inode_management *im = &sbi->im[type];
  337. struct ino_entry *e, *tmp;
  338. tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
  339. retry:
  340. radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
  341. spin_lock(&im->ino_lock);
  342. e = radix_tree_lookup(&im->ino_root, ino);
  343. if (!e) {
  344. e = tmp;
  345. if (radix_tree_insert(&im->ino_root, ino, e)) {
  346. spin_unlock(&im->ino_lock);
  347. radix_tree_preload_end();
  348. goto retry;
  349. }
  350. memset(e, 0, sizeof(struct ino_entry));
  351. e->ino = ino;
  352. list_add_tail(&e->list, &im->ino_list);
  353. if (type != ORPHAN_INO)
  354. im->ino_num++;
  355. }
  356. spin_unlock(&im->ino_lock);
  357. radix_tree_preload_end();
  358. if (e != tmp)
  359. kmem_cache_free(ino_entry_slab, tmp);
  360. }
  361. static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
  362. {
  363. struct inode_management *im = &sbi->im[type];
  364. struct ino_entry *e;
  365. spin_lock(&im->ino_lock);
  366. e = radix_tree_lookup(&im->ino_root, ino);
  367. if (e) {
  368. list_del(&e->list);
  369. radix_tree_delete(&im->ino_root, ino);
  370. im->ino_num--;
  371. spin_unlock(&im->ino_lock);
  372. kmem_cache_free(ino_entry_slab, e);
  373. return;
  374. }
  375. spin_unlock(&im->ino_lock);
  376. }
  377. void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
  378. {
  379. /* add new dirty ino entry into list */
  380. __add_ino_entry(sbi, ino, type);
  381. }
  382. void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
  383. {
  384. /* remove dirty ino entry from list */
  385. __remove_ino_entry(sbi, ino, type);
  386. }
  387. /* mode should be APPEND_INO or UPDATE_INO */
  388. bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
  389. {
  390. struct inode_management *im = &sbi->im[mode];
  391. struct ino_entry *e;
  392. spin_lock(&im->ino_lock);
  393. e = radix_tree_lookup(&im->ino_root, ino);
  394. spin_unlock(&im->ino_lock);
  395. return e ? true : false;
  396. }
  397. void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
  398. {
  399. struct ino_entry *e, *tmp;
  400. int i;
  401. for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) {
  402. struct inode_management *im = &sbi->im[i];
  403. spin_lock(&im->ino_lock);
  404. list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
  405. list_del(&e->list);
  406. radix_tree_delete(&im->ino_root, e->ino);
  407. kmem_cache_free(ino_entry_slab, e);
  408. im->ino_num--;
  409. }
  410. spin_unlock(&im->ino_lock);
  411. }
  412. }
  413. int acquire_orphan_inode(struct f2fs_sb_info *sbi)
  414. {
  415. struct inode_management *im = &sbi->im[ORPHAN_INO];
  416. int err = 0;
  417. spin_lock(&im->ino_lock);
  418. #ifdef CONFIG_F2FS_FAULT_INJECTION
  419. if (time_to_inject(sbi, FAULT_ORPHAN)) {
  420. spin_unlock(&im->ino_lock);
  421. return -ENOSPC;
  422. }
  423. #endif
  424. if (unlikely(im->ino_num >= sbi->max_orphans))
  425. err = -ENOSPC;
  426. else
  427. im->ino_num++;
  428. spin_unlock(&im->ino_lock);
  429. return err;
  430. }
  431. void release_orphan_inode(struct f2fs_sb_info *sbi)
  432. {
  433. struct inode_management *im = &sbi->im[ORPHAN_INO];
  434. spin_lock(&im->ino_lock);
  435. f2fs_bug_on(sbi, im->ino_num == 0);
  436. im->ino_num--;
  437. spin_unlock(&im->ino_lock);
  438. }
  439. void add_orphan_inode(struct inode *inode)
  440. {
  441. /* add new orphan ino entry into list */
  442. __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO);
  443. update_inode_page(inode);
  444. }
  445. void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  446. {
  447. /* remove orphan entry from orphan list */
  448. __remove_ino_entry(sbi, ino, ORPHAN_INO);
  449. }
  450. static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  451. {
  452. struct inode *inode;
  453. struct node_info ni;
  454. int err = acquire_orphan_inode(sbi);
  455. if (err) {
  456. set_sbi_flag(sbi, SBI_NEED_FSCK);
  457. f2fs_msg(sbi->sb, KERN_WARNING,
  458. "%s: orphan failed (ino=%x), run fsck to fix.",
  459. __func__, ino);
  460. return err;
  461. }
  462. __add_ino_entry(sbi, ino, ORPHAN_INO);
  463. inode = f2fs_iget_retry(sbi->sb, ino);
  464. if (IS_ERR(inode)) {
  465. /*
  466. * there should be a bug that we can't find the entry
  467. * to orphan inode.
  468. */
  469. f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
  470. return PTR_ERR(inode);
  471. }
  472. clear_nlink(inode);
  473. /* truncate all the data during iput */
  474. iput(inode);
  475. get_node_info(sbi, ino, &ni);
  476. /* ENOMEM was fully retried in f2fs_evict_inode. */
  477. if (ni.blk_addr != NULL_ADDR) {
  478. set_sbi_flag(sbi, SBI_NEED_FSCK);
  479. f2fs_msg(sbi->sb, KERN_WARNING,
  480. "%s: orphan failed (ino=%x), run fsck to fix.",
  481. __func__, ino);
  482. return -EIO;
  483. }
  484. __remove_ino_entry(sbi, ino, ORPHAN_INO);
  485. return 0;
  486. }
  487. int recover_orphan_inodes(struct f2fs_sb_info *sbi)
  488. {
  489. block_t start_blk, orphan_blocks, i, j;
  490. int err;
  491. if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
  492. return 0;
  493. start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
  494. orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
  495. ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
  496. for (i = 0; i < orphan_blocks; i++) {
  497. struct page *page = get_meta_page(sbi, start_blk + i);
  498. struct f2fs_orphan_block *orphan_blk;
  499. orphan_blk = (struct f2fs_orphan_block *)page_address(page);
  500. for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
  501. nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
  502. err = recover_orphan_inode(sbi, ino);
  503. if (err) {
  504. f2fs_put_page(page, 1);
  505. return err;
  506. }
  507. }
  508. f2fs_put_page(page, 1);
  509. }
  510. /* clear Orphan Flag */
  511. clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
  512. return 0;
  513. }
  514. static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
  515. {
  516. struct list_head *head;
  517. struct f2fs_orphan_block *orphan_blk = NULL;
  518. unsigned int nentries = 0;
  519. unsigned short index = 1;
  520. unsigned short orphan_blocks;
  521. struct page *page = NULL;
  522. struct ino_entry *orphan = NULL;
  523. struct inode_management *im = &sbi->im[ORPHAN_INO];
  524. orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
  525. /*
  526. * we don't need to do spin_lock(&im->ino_lock) here, since all the
  527. * orphan inode operations are covered under f2fs_lock_op().
  528. * And, spin_lock should be avoided due to page operations below.
  529. */
  530. head = &im->ino_list;
  531. /* loop for each orphan inode entry and write them in Jornal block */
  532. list_for_each_entry(orphan, head, list) {
  533. if (!page) {
  534. page = grab_meta_page(sbi, start_blk++);
  535. orphan_blk =
  536. (struct f2fs_orphan_block *)page_address(page);
  537. memset(orphan_blk, 0, sizeof(*orphan_blk));
  538. }
  539. orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
  540. if (nentries == F2FS_ORPHANS_PER_BLOCK) {
  541. /*
  542. * an orphan block is full of 1020 entries,
  543. * then we need to flush current orphan blocks
  544. * and bring another one in memory
  545. */
  546. orphan_blk->blk_addr = cpu_to_le16(index);
  547. orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
  548. orphan_blk->entry_count = cpu_to_le32(nentries);
  549. set_page_dirty(page);
  550. f2fs_put_page(page, 1);
  551. index++;
  552. nentries = 0;
  553. page = NULL;
  554. }
  555. }
  556. if (page) {
  557. orphan_blk->blk_addr = cpu_to_le16(index);
  558. orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
  559. orphan_blk->entry_count = cpu_to_le32(nentries);
  560. set_page_dirty(page);
  561. f2fs_put_page(page, 1);
  562. }
  563. }
  564. static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
  565. struct f2fs_checkpoint **cp_block, struct page **cp_page,
  566. unsigned long long *version)
  567. {
  568. unsigned long blk_size = sbi->blocksize;
  569. size_t crc_offset = 0;
  570. __u32 crc = 0;
  571. *cp_page = get_meta_page(sbi, cp_addr);
  572. *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
  573. crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
  574. if (crc_offset >= blk_size) {
  575. f2fs_msg(sbi->sb, KERN_WARNING,
  576. "invalid crc_offset: %zu", crc_offset);
  577. return -EINVAL;
  578. }
  579. crc = le32_to_cpu(*((__le32 *)((unsigned char *)*cp_block
  580. + crc_offset)));
  581. if (!f2fs_crc_valid(crc, *cp_block, crc_offset)) {
  582. f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
  583. return -EINVAL;
  584. }
  585. *version = cur_cp_version(*cp_block);
  586. return 0;
  587. }
  588. static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
  589. block_t cp_addr, unsigned long long *version)
  590. {
  591. struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
  592. struct f2fs_checkpoint *cp_block = NULL;
  593. unsigned long long cur_version = 0, pre_version = 0;
  594. int err;
  595. err = get_checkpoint_version(sbi, cp_addr, &cp_block,
  596. &cp_page_1, version);
  597. if (err)
  598. goto invalid_cp1;
  599. pre_version = *version;
  600. cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
  601. err = get_checkpoint_version(sbi, cp_addr, &cp_block,
  602. &cp_page_2, version);
  603. if (err)
  604. goto invalid_cp2;
  605. cur_version = *version;
  606. if (cur_version == pre_version) {
  607. *version = cur_version;
  608. f2fs_put_page(cp_page_2, 1);
  609. return cp_page_1;
  610. }
  611. invalid_cp2:
  612. f2fs_put_page(cp_page_2, 1);
  613. invalid_cp1:
  614. f2fs_put_page(cp_page_1, 1);
  615. return NULL;
  616. }
  617. int get_valid_checkpoint(struct f2fs_sb_info *sbi)
  618. {
  619. struct f2fs_checkpoint *cp_block;
  620. struct f2fs_super_block *fsb = sbi->raw_super;
  621. struct page *cp1, *cp2, *cur_page;
  622. unsigned long blk_size = sbi->blocksize;
  623. unsigned long long cp1_version = 0, cp2_version = 0;
  624. unsigned long long cp_start_blk_no;
  625. unsigned int cp_blks = 1 + __cp_payload(sbi);
  626. block_t cp_blk_no;
  627. int i;
  628. sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
  629. if (!sbi->ckpt)
  630. return -ENOMEM;
  631. /*
  632. * Finding out valid cp block involves read both
  633. * sets( cp pack1 and cp pack 2)
  634. */
  635. cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
  636. cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
  637. /* The second checkpoint pack should start at the next segment */
  638. cp_start_blk_no += ((unsigned long long)1) <<
  639. le32_to_cpu(fsb->log_blocks_per_seg);
  640. cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
  641. if (cp1 && cp2) {
  642. if (ver_after(cp2_version, cp1_version))
  643. cur_page = cp2;
  644. else
  645. cur_page = cp1;
  646. } else if (cp1) {
  647. cur_page = cp1;
  648. } else if (cp2) {
  649. cur_page = cp2;
  650. } else {
  651. goto fail_no_cp;
  652. }
  653. cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
  654. memcpy(sbi->ckpt, cp_block, blk_size);
  655. /* Sanity checking of checkpoint */
  656. if (sanity_check_ckpt(sbi))
  657. goto free_fail_no_cp;
  658. if (cur_page == cp1)
  659. sbi->cur_cp_pack = 1;
  660. else
  661. sbi->cur_cp_pack = 2;
  662. if (cp_blks <= 1)
  663. goto done;
  664. cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
  665. if (cur_page == cp2)
  666. cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
  667. for (i = 1; i < cp_blks; i++) {
  668. void *sit_bitmap_ptr;
  669. unsigned char *ckpt = (unsigned char *)sbi->ckpt;
  670. cur_page = get_meta_page(sbi, cp_blk_no + i);
  671. sit_bitmap_ptr = page_address(cur_page);
  672. memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
  673. f2fs_put_page(cur_page, 1);
  674. }
  675. done:
  676. f2fs_put_page(cp1, 1);
  677. f2fs_put_page(cp2, 1);
  678. return 0;
  679. free_fail_no_cp:
  680. f2fs_put_page(cp1, 1);
  681. f2fs_put_page(cp2, 1);
  682. fail_no_cp:
  683. kfree(sbi->ckpt);
  684. return -EINVAL;
  685. }
  686. static void __add_dirty_inode(struct inode *inode, enum inode_type type)
  687. {
  688. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  689. int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
  690. if (is_inode_flag_set(inode, flag))
  691. return;
  692. set_inode_flag(inode, flag);
  693. list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
  694. stat_inc_dirty_inode(sbi, type);
  695. }
  696. static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
  697. {
  698. int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
  699. if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
  700. return;
  701. list_del_init(&F2FS_I(inode)->dirty_list);
  702. clear_inode_flag(inode, flag);
  703. stat_dec_dirty_inode(F2FS_I_SB(inode), type);
  704. }
  705. void update_dirty_page(struct inode *inode, struct page *page)
  706. {
  707. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  708. enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
  709. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  710. !S_ISLNK(inode->i_mode))
  711. return;
  712. spin_lock(&sbi->inode_lock[type]);
  713. if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
  714. __add_dirty_inode(inode, type);
  715. inode_inc_dirty_pages(inode);
  716. spin_unlock(&sbi->inode_lock[type]);
  717. SetPagePrivate(page);
  718. f2fs_trace_pid(page);
  719. }
  720. void remove_dirty_inode(struct inode *inode)
  721. {
  722. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  723. enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
  724. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  725. !S_ISLNK(inode->i_mode))
  726. return;
  727. if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
  728. return;
  729. spin_lock(&sbi->inode_lock[type]);
  730. __remove_dirty_inode(inode, type);
  731. spin_unlock(&sbi->inode_lock[type]);
  732. }
  733. int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
  734. {
  735. struct list_head *head;
  736. struct inode *inode;
  737. struct f2fs_inode_info *fi;
  738. bool is_dir = (type == DIR_INODE);
  739. trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
  740. get_pages(sbi, is_dir ?
  741. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
  742. retry:
  743. if (unlikely(f2fs_cp_error(sbi)))
  744. return -EIO;
  745. spin_lock(&sbi->inode_lock[type]);
  746. head = &sbi->inode_list[type];
  747. if (list_empty(head)) {
  748. spin_unlock(&sbi->inode_lock[type]);
  749. trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
  750. get_pages(sbi, is_dir ?
  751. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
  752. return 0;
  753. }
  754. fi = list_entry(head->next, struct f2fs_inode_info, dirty_list);
  755. inode = igrab(&fi->vfs_inode);
  756. spin_unlock(&sbi->inode_lock[type]);
  757. if (inode) {
  758. filemap_fdatawrite(inode->i_mapping);
  759. iput(inode);
  760. } else {
  761. /*
  762. * We should submit bio, since it exists several
  763. * wribacking dentry pages in the freeing inode.
  764. */
  765. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  766. cond_resched();
  767. }
  768. goto retry;
  769. }
  770. int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
  771. {
  772. struct list_head *head = &sbi->inode_list[DIRTY_META];
  773. struct inode *inode;
  774. struct f2fs_inode_info *fi;
  775. s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
  776. while (total--) {
  777. if (unlikely(f2fs_cp_error(sbi)))
  778. return -EIO;
  779. spin_lock(&sbi->inode_lock[DIRTY_META]);
  780. if (list_empty(head)) {
  781. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  782. return 0;
  783. }
  784. fi = list_entry(head->next, struct f2fs_inode_info,
  785. gdirty_list);
  786. inode = igrab(&fi->vfs_inode);
  787. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  788. if (inode) {
  789. sync_inode_metadata(inode, 0);
  790. /* it's on eviction */
  791. if (is_inode_flag_set(inode, FI_DIRTY_INODE))
  792. update_inode_page(inode);
  793. iput(inode);
  794. }
  795. };
  796. return 0;
  797. }
  798. /*
  799. * Freeze all the FS-operations for checkpoint.
  800. */
  801. static int block_operations(struct f2fs_sb_info *sbi)
  802. {
  803. struct writeback_control wbc = {
  804. .sync_mode = WB_SYNC_ALL,
  805. .nr_to_write = LONG_MAX,
  806. .for_reclaim = 0,
  807. };
  808. struct blk_plug plug;
  809. int err = 0;
  810. blk_start_plug(&plug);
  811. retry_flush_dents:
  812. f2fs_lock_all(sbi);
  813. /* write all the dirty dentry pages */
  814. if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
  815. f2fs_unlock_all(sbi);
  816. err = sync_dirty_inodes(sbi, DIR_INODE);
  817. if (err)
  818. goto out;
  819. goto retry_flush_dents;
  820. }
  821. if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
  822. f2fs_unlock_all(sbi);
  823. err = f2fs_sync_inode_meta(sbi);
  824. if (err)
  825. goto out;
  826. goto retry_flush_dents;
  827. }
  828. /*
  829. * POR: we should ensure that there are no dirty node pages
  830. * until finishing nat/sit flush.
  831. */
  832. retry_flush_nodes:
  833. down_write(&sbi->node_write);
  834. if (get_pages(sbi, F2FS_DIRTY_NODES)) {
  835. up_write(&sbi->node_write);
  836. err = sync_node_pages(sbi, &wbc);
  837. if (err) {
  838. f2fs_unlock_all(sbi);
  839. goto out;
  840. }
  841. goto retry_flush_nodes;
  842. }
  843. out:
  844. blk_finish_plug(&plug);
  845. return err;
  846. }
  847. static void unblock_operations(struct f2fs_sb_info *sbi)
  848. {
  849. up_write(&sbi->node_write);
  850. build_free_nids(sbi, false);
  851. f2fs_unlock_all(sbi);
  852. }
  853. static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
  854. {
  855. DEFINE_WAIT(wait);
  856. for (;;) {
  857. prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
  858. if (!get_pages(sbi, F2FS_WB_CP_DATA))
  859. break;
  860. io_schedule_timeout(5*HZ);
  861. }
  862. finish_wait(&sbi->cp_wait, &wait);
  863. }
  864. static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  865. {
  866. unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
  867. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  868. spin_lock(&sbi->cp_lock);
  869. if (cpc->reason == CP_UMOUNT)
  870. __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
  871. else
  872. __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
  873. if (cpc->reason == CP_FASTBOOT)
  874. __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
  875. else
  876. __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
  877. if (orphan_num)
  878. __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
  879. else
  880. __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
  881. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
  882. __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
  883. /* set this flag to activate crc|cp_ver for recovery */
  884. __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
  885. spin_unlock(&sbi->cp_lock);
  886. }
  887. static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  888. {
  889. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  890. struct f2fs_nm_info *nm_i = NM_I(sbi);
  891. unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
  892. nid_t last_nid = nm_i->next_scan_nid;
  893. block_t start_blk;
  894. unsigned int data_sum_blocks, orphan_blocks;
  895. __u32 crc32 = 0;
  896. int i;
  897. int cp_payload_blks = __cp_payload(sbi);
  898. struct super_block *sb = sbi->sb;
  899. struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
  900. u64 kbytes_written;
  901. /* Flush all the NAT/SIT pages */
  902. while (get_pages(sbi, F2FS_DIRTY_META)) {
  903. sync_meta_pages(sbi, META, LONG_MAX);
  904. if (unlikely(f2fs_cp_error(sbi)))
  905. return -EIO;
  906. }
  907. next_free_nid(sbi, &last_nid);
  908. /*
  909. * modify checkpoint
  910. * version number is already updated
  911. */
  912. ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
  913. ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
  914. ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
  915. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  916. ckpt->cur_node_segno[i] =
  917. cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
  918. ckpt->cur_node_blkoff[i] =
  919. cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
  920. ckpt->alloc_type[i + CURSEG_HOT_NODE] =
  921. curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
  922. }
  923. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  924. ckpt->cur_data_segno[i] =
  925. cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
  926. ckpt->cur_data_blkoff[i] =
  927. cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
  928. ckpt->alloc_type[i + CURSEG_HOT_DATA] =
  929. curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
  930. }
  931. ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
  932. ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
  933. ckpt->next_free_nid = cpu_to_le32(last_nid);
  934. /* 2 cp + n data seg summary + orphan inode blocks */
  935. data_sum_blocks = npages_for_summary_flush(sbi, false);
  936. spin_lock(&sbi->cp_lock);
  937. if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
  938. __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
  939. else
  940. __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
  941. spin_unlock(&sbi->cp_lock);
  942. orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
  943. ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
  944. orphan_blocks);
  945. if (__remain_node_summaries(cpc->reason))
  946. ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
  947. cp_payload_blks + data_sum_blocks +
  948. orphan_blocks + NR_CURSEG_NODE_TYPE);
  949. else
  950. ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
  951. cp_payload_blks + data_sum_blocks +
  952. orphan_blocks);
  953. /* update ckpt flag for checkpoint */
  954. update_ckpt_flags(sbi, cpc);
  955. /* update SIT/NAT bitmap */
  956. get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
  957. get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
  958. crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
  959. *((__le32 *)((unsigned char *)ckpt +
  960. le32_to_cpu(ckpt->checksum_offset)))
  961. = cpu_to_le32(crc32);
  962. start_blk = __start_cp_next_addr(sbi);
  963. /* need to wait for end_io results */
  964. wait_on_all_pages_writeback(sbi);
  965. if (unlikely(f2fs_cp_error(sbi)))
  966. return -EIO;
  967. /* write out checkpoint buffer at block 0 */
  968. update_meta_page(sbi, ckpt, start_blk++);
  969. for (i = 1; i < 1 + cp_payload_blks; i++)
  970. update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
  971. start_blk++);
  972. if (orphan_num) {
  973. write_orphan_inodes(sbi, start_blk);
  974. start_blk += orphan_blocks;
  975. }
  976. write_data_summaries(sbi, start_blk);
  977. start_blk += data_sum_blocks;
  978. /* Record write statistics in the hot node summary */
  979. kbytes_written = sbi->kbytes_written;
  980. if (sb->s_bdev->bd_part)
  981. kbytes_written += BD_PART_WRITTEN(sbi);
  982. seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
  983. if (__remain_node_summaries(cpc->reason)) {
  984. write_node_summaries(sbi, start_blk);
  985. start_blk += NR_CURSEG_NODE_TYPE;
  986. }
  987. /* writeout checkpoint block */
  988. update_meta_page(sbi, ckpt, start_blk);
  989. /* wait for previous submitted node/meta pages writeback */
  990. wait_on_all_pages_writeback(sbi);
  991. if (unlikely(f2fs_cp_error(sbi)))
  992. return -EIO;
  993. filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX);
  994. filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX);
  995. /* update user_block_counts */
  996. sbi->last_valid_block_count = sbi->total_valid_block_count;
  997. percpu_counter_set(&sbi->alloc_valid_block_count, 0);
  998. /* Here, we only have one bio having CP pack */
  999. sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
  1000. /* wait for previous submitted meta pages writeback */
  1001. wait_on_all_pages_writeback(sbi);
  1002. release_ino_entry(sbi, false);
  1003. if (unlikely(f2fs_cp_error(sbi)))
  1004. return -EIO;
  1005. clear_sbi_flag(sbi, SBI_IS_DIRTY);
  1006. clear_sbi_flag(sbi, SBI_NEED_CP);
  1007. __set_cp_next_pack(sbi);
  1008. /*
  1009. * redirty superblock if metadata like node page or inode cache is
  1010. * updated during writing checkpoint.
  1011. */
  1012. if (get_pages(sbi, F2FS_DIRTY_NODES) ||
  1013. get_pages(sbi, F2FS_DIRTY_IMETA))
  1014. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1015. f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
  1016. return 0;
  1017. }
  1018. /*
  1019. * We guarantee that this checkpoint procedure will not fail.
  1020. */
  1021. int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  1022. {
  1023. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1024. unsigned long long ckpt_ver;
  1025. int err = 0;
  1026. mutex_lock(&sbi->cp_mutex);
  1027. if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
  1028. (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
  1029. (cpc->reason == CP_DISCARD && !sbi->discard_blks)))
  1030. goto out;
  1031. if (unlikely(f2fs_cp_error(sbi))) {
  1032. err = -EIO;
  1033. goto out;
  1034. }
  1035. if (f2fs_readonly(sbi->sb)) {
  1036. err = -EROFS;
  1037. goto out;
  1038. }
  1039. trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
  1040. err = block_operations(sbi);
  1041. if (err)
  1042. goto out;
  1043. trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
  1044. f2fs_flush_merged_bios(sbi);
  1045. /* this is the case of multiple fstrims without any changes */
  1046. if (cpc->reason == CP_DISCARD && !is_sbi_flag_set(sbi, SBI_IS_DIRTY)) {
  1047. f2fs_bug_on(sbi, NM_I(sbi)->dirty_nat_cnt);
  1048. f2fs_bug_on(sbi, SIT_I(sbi)->dirty_sentries);
  1049. f2fs_bug_on(sbi, prefree_segments(sbi));
  1050. flush_sit_entries(sbi, cpc);
  1051. clear_prefree_segments(sbi, cpc);
  1052. unblock_operations(sbi);
  1053. goto out;
  1054. }
  1055. /*
  1056. * update checkpoint pack index
  1057. * Increase the version number so that
  1058. * SIT entries and seg summaries are written at correct place
  1059. */
  1060. ckpt_ver = cur_cp_version(ckpt);
  1061. ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
  1062. /* write cached NAT/SIT entries to NAT/SIT area */
  1063. flush_nat_entries(sbi);
  1064. flush_sit_entries(sbi, cpc);
  1065. /* unlock all the fs_lock[] in do_checkpoint() */
  1066. err = do_checkpoint(sbi, cpc);
  1067. if (err)
  1068. release_discard_addrs(sbi);
  1069. else
  1070. clear_prefree_segments(sbi, cpc);
  1071. unblock_operations(sbi);
  1072. stat_inc_cp_count(sbi->stat_info);
  1073. if (cpc->reason == CP_RECOVERY)
  1074. f2fs_msg(sbi->sb, KERN_NOTICE,
  1075. "checkpoint: version = %llx", ckpt_ver);
  1076. /* do checkpoint periodically */
  1077. f2fs_update_time(sbi, CP_TIME);
  1078. trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
  1079. out:
  1080. mutex_unlock(&sbi->cp_mutex);
  1081. return err;
  1082. }
  1083. void init_ino_entry_info(struct f2fs_sb_info *sbi)
  1084. {
  1085. int i;
  1086. for (i = 0; i < MAX_INO_ENTRY; i++) {
  1087. struct inode_management *im = &sbi->im[i];
  1088. INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
  1089. spin_lock_init(&im->ino_lock);
  1090. INIT_LIST_HEAD(&im->ino_list);
  1091. im->ino_num = 0;
  1092. }
  1093. sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
  1094. NR_CURSEG_TYPE - __cp_payload(sbi)) *
  1095. F2FS_ORPHANS_PER_BLOCK;
  1096. }
  1097. int __init create_checkpoint_caches(void)
  1098. {
  1099. ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
  1100. sizeof(struct ino_entry));
  1101. if (!ino_entry_slab)
  1102. return -ENOMEM;
  1103. inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
  1104. sizeof(struct inode_entry));
  1105. if (!inode_entry_slab) {
  1106. kmem_cache_destroy(ino_entry_slab);
  1107. return -ENOMEM;
  1108. }
  1109. return 0;
  1110. }
  1111. void destroy_checkpoint_caches(void)
  1112. {
  1113. kmem_cache_destroy(ino_entry_slab);
  1114. kmem_cache_destroy(inode_entry_slab);
  1115. }