checkpoint.c 32 KB

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