checkpoint.c 31 KB

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