segment.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /*
  2. * fs/f2fs/segment.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/f2fs_fs.h>
  13. #include <linux/bio.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/prefetch.h>
  16. #include <linux/vmalloc.h>
  17. #include "f2fs.h"
  18. #include "segment.h"
  19. #include "node.h"
  20. #include <trace/events/f2fs.h>
  21. /*
  22. * This function balances dirty node and dentry pages.
  23. * In addition, it controls garbage collection.
  24. */
  25. void f2fs_balance_fs(struct f2fs_sb_info *sbi)
  26. {
  27. /*
  28. * We should do GC or end up with checkpoint, if there are so many dirty
  29. * dir/node pages without enough free segments.
  30. */
  31. if (has_not_enough_free_secs(sbi, 0)) {
  32. mutex_lock(&sbi->gc_mutex);
  33. f2fs_gc(sbi);
  34. }
  35. }
  36. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
  37. {
  38. /* check the # of cached NAT entries and prefree segments */
  39. if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
  40. excess_prefree_segs(sbi))
  41. f2fs_sync_fs(sbi->sb, true);
  42. }
  43. static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  44. enum dirty_type dirty_type)
  45. {
  46. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  47. /* need not be added */
  48. if (IS_CURSEG(sbi, segno))
  49. return;
  50. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  51. dirty_i->nr_dirty[dirty_type]++;
  52. if (dirty_type == DIRTY) {
  53. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  54. enum dirty_type t = sentry->type;
  55. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
  56. dirty_i->nr_dirty[t]++;
  57. }
  58. }
  59. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  60. enum dirty_type dirty_type)
  61. {
  62. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  63. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  64. dirty_i->nr_dirty[dirty_type]--;
  65. if (dirty_type == DIRTY) {
  66. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  67. enum dirty_type t = sentry->type;
  68. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  69. dirty_i->nr_dirty[t]--;
  70. if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
  71. clear_bit(GET_SECNO(sbi, segno),
  72. dirty_i->victim_secmap);
  73. }
  74. }
  75. /*
  76. * Should not occur error such as -ENOMEM.
  77. * Adding dirty entry into seglist is not critical operation.
  78. * If a given segment is one of current working segments, it won't be added.
  79. */
  80. static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  81. {
  82. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  83. unsigned short valid_blocks;
  84. if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
  85. return;
  86. mutex_lock(&dirty_i->seglist_lock);
  87. valid_blocks = get_valid_blocks(sbi, segno, 0);
  88. if (valid_blocks == 0) {
  89. __locate_dirty_segment(sbi, segno, PRE);
  90. __remove_dirty_segment(sbi, segno, DIRTY);
  91. } else if (valid_blocks < sbi->blocks_per_seg) {
  92. __locate_dirty_segment(sbi, segno, DIRTY);
  93. } else {
  94. /* Recovery routine with SSR needs this */
  95. __remove_dirty_segment(sbi, segno, DIRTY);
  96. }
  97. mutex_unlock(&dirty_i->seglist_lock);
  98. }
  99. /*
  100. * Should call clear_prefree_segments after checkpoint is done.
  101. */
  102. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  103. {
  104. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  105. unsigned int segno = -1;
  106. unsigned int total_segs = TOTAL_SEGS(sbi);
  107. mutex_lock(&dirty_i->seglist_lock);
  108. while (1) {
  109. segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
  110. segno + 1);
  111. if (segno >= total_segs)
  112. break;
  113. __set_test_and_free(sbi, segno);
  114. }
  115. mutex_unlock(&dirty_i->seglist_lock);
  116. }
  117. void clear_prefree_segments(struct f2fs_sb_info *sbi)
  118. {
  119. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  120. unsigned int segno = -1;
  121. unsigned int total_segs = TOTAL_SEGS(sbi);
  122. mutex_lock(&dirty_i->seglist_lock);
  123. while (1) {
  124. segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
  125. segno + 1);
  126. if (segno >= total_segs)
  127. break;
  128. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
  129. dirty_i->nr_dirty[PRE]--;
  130. /* Let's use trim */
  131. if (test_opt(sbi, DISCARD))
  132. blkdev_issue_discard(sbi->sb->s_bdev,
  133. START_BLOCK(sbi, segno) <<
  134. sbi->log_sectors_per_block,
  135. 1 << (sbi->log_sectors_per_block +
  136. sbi->log_blocks_per_seg),
  137. GFP_NOFS, 0);
  138. }
  139. mutex_unlock(&dirty_i->seglist_lock);
  140. }
  141. static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  142. {
  143. struct sit_info *sit_i = SIT_I(sbi);
  144. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
  145. sit_i->dirty_sentries++;
  146. }
  147. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  148. unsigned int segno, int modified)
  149. {
  150. struct seg_entry *se = get_seg_entry(sbi, segno);
  151. se->type = type;
  152. if (modified)
  153. __mark_sit_entry_dirty(sbi, segno);
  154. }
  155. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  156. {
  157. struct seg_entry *se;
  158. unsigned int segno, offset;
  159. long int new_vblocks;
  160. segno = GET_SEGNO(sbi, blkaddr);
  161. se = get_seg_entry(sbi, segno);
  162. new_vblocks = se->valid_blocks + del;
  163. offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
  164. f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
  165. (new_vblocks > sbi->blocks_per_seg)));
  166. se->valid_blocks = new_vblocks;
  167. se->mtime = get_mtime(sbi);
  168. SIT_I(sbi)->max_mtime = se->mtime;
  169. /* Update valid block bitmap */
  170. if (del > 0) {
  171. if (f2fs_set_bit(offset, se->cur_valid_map))
  172. BUG();
  173. } else {
  174. if (!f2fs_clear_bit(offset, se->cur_valid_map))
  175. BUG();
  176. }
  177. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  178. se->ckpt_valid_blocks += del;
  179. __mark_sit_entry_dirty(sbi, segno);
  180. /* update total number of valid blocks to be written in ckpt area */
  181. SIT_I(sbi)->written_valid_blocks += del;
  182. if (sbi->segs_per_sec > 1)
  183. get_sec_entry(sbi, segno)->valid_blocks += del;
  184. }
  185. static void refresh_sit_entry(struct f2fs_sb_info *sbi,
  186. block_t old_blkaddr, block_t new_blkaddr)
  187. {
  188. update_sit_entry(sbi, new_blkaddr, 1);
  189. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  190. update_sit_entry(sbi, old_blkaddr, -1);
  191. }
  192. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  193. {
  194. unsigned int segno = GET_SEGNO(sbi, addr);
  195. struct sit_info *sit_i = SIT_I(sbi);
  196. f2fs_bug_on(addr == NULL_ADDR);
  197. if (addr == NEW_ADDR)
  198. return;
  199. /* add it into sit main buffer */
  200. mutex_lock(&sit_i->sentry_lock);
  201. update_sit_entry(sbi, addr, -1);
  202. /* add it into dirty seglist */
  203. locate_dirty_segment(sbi, segno);
  204. mutex_unlock(&sit_i->sentry_lock);
  205. }
  206. /*
  207. * This function should be resided under the curseg_mutex lock
  208. */
  209. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  210. struct f2fs_summary *sum)
  211. {
  212. struct curseg_info *curseg = CURSEG_I(sbi, type);
  213. void *addr = curseg->sum_blk;
  214. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  215. memcpy(addr, sum, sizeof(struct f2fs_summary));
  216. }
  217. /*
  218. * Calculate the number of current summary pages for writing
  219. */
  220. int npages_for_summary_flush(struct f2fs_sb_info *sbi)
  221. {
  222. int valid_sum_count = 0;
  223. int i, sum_in_page;
  224. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  225. if (sbi->ckpt->alloc_type[i] == SSR)
  226. valid_sum_count += sbi->blocks_per_seg;
  227. else
  228. valid_sum_count += curseg_blkoff(sbi, i);
  229. }
  230. sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
  231. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  232. if (valid_sum_count <= sum_in_page)
  233. return 1;
  234. else if ((valid_sum_count - sum_in_page) <=
  235. (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  236. return 2;
  237. return 3;
  238. }
  239. /*
  240. * Caller should put this summary page
  241. */
  242. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  243. {
  244. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  245. }
  246. static void write_sum_page(struct f2fs_sb_info *sbi,
  247. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  248. {
  249. struct page *page = grab_meta_page(sbi, blk_addr);
  250. void *kaddr = page_address(page);
  251. memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
  252. set_page_dirty(page);
  253. f2fs_put_page(page, 1);
  254. }
  255. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  256. {
  257. struct curseg_info *curseg = CURSEG_I(sbi, type);
  258. unsigned int segno = curseg->segno + 1;
  259. struct free_segmap_info *free_i = FREE_I(sbi);
  260. if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
  261. return !test_bit(segno, free_i->free_segmap);
  262. return 0;
  263. }
  264. /*
  265. * Find a new segment from the free segments bitmap to right order
  266. * This function should be returned with success, otherwise BUG
  267. */
  268. static void get_new_segment(struct f2fs_sb_info *sbi,
  269. unsigned int *newseg, bool new_sec, int dir)
  270. {
  271. struct free_segmap_info *free_i = FREE_I(sbi);
  272. unsigned int segno, secno, zoneno;
  273. unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
  274. unsigned int hint = *newseg / sbi->segs_per_sec;
  275. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  276. unsigned int left_start = hint;
  277. bool init = true;
  278. int go_left = 0;
  279. int i;
  280. write_lock(&free_i->segmap_lock);
  281. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  282. segno = find_next_zero_bit(free_i->free_segmap,
  283. TOTAL_SEGS(sbi), *newseg + 1);
  284. if (segno - *newseg < sbi->segs_per_sec -
  285. (*newseg % sbi->segs_per_sec))
  286. goto got_it;
  287. }
  288. find_other_zone:
  289. secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
  290. if (secno >= TOTAL_SECS(sbi)) {
  291. if (dir == ALLOC_RIGHT) {
  292. secno = find_next_zero_bit(free_i->free_secmap,
  293. TOTAL_SECS(sbi), 0);
  294. f2fs_bug_on(secno >= TOTAL_SECS(sbi));
  295. } else {
  296. go_left = 1;
  297. left_start = hint - 1;
  298. }
  299. }
  300. if (go_left == 0)
  301. goto skip_left;
  302. while (test_bit(left_start, free_i->free_secmap)) {
  303. if (left_start > 0) {
  304. left_start--;
  305. continue;
  306. }
  307. left_start = find_next_zero_bit(free_i->free_secmap,
  308. TOTAL_SECS(sbi), 0);
  309. f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
  310. break;
  311. }
  312. secno = left_start;
  313. skip_left:
  314. hint = secno;
  315. segno = secno * sbi->segs_per_sec;
  316. zoneno = secno / sbi->secs_per_zone;
  317. /* give up on finding another zone */
  318. if (!init)
  319. goto got_it;
  320. if (sbi->secs_per_zone == 1)
  321. goto got_it;
  322. if (zoneno == old_zoneno)
  323. goto got_it;
  324. if (dir == ALLOC_LEFT) {
  325. if (!go_left && zoneno + 1 >= total_zones)
  326. goto got_it;
  327. if (go_left && zoneno == 0)
  328. goto got_it;
  329. }
  330. for (i = 0; i < NR_CURSEG_TYPE; i++)
  331. if (CURSEG_I(sbi, i)->zone == zoneno)
  332. break;
  333. if (i < NR_CURSEG_TYPE) {
  334. /* zone is in user, try another */
  335. if (go_left)
  336. hint = zoneno * sbi->secs_per_zone - 1;
  337. else if (zoneno + 1 >= total_zones)
  338. hint = 0;
  339. else
  340. hint = (zoneno + 1) * sbi->secs_per_zone;
  341. init = false;
  342. goto find_other_zone;
  343. }
  344. got_it:
  345. /* set it as dirty segment in free segmap */
  346. f2fs_bug_on(test_bit(segno, free_i->free_segmap));
  347. __set_inuse(sbi, segno);
  348. *newseg = segno;
  349. write_unlock(&free_i->segmap_lock);
  350. }
  351. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  352. {
  353. struct curseg_info *curseg = CURSEG_I(sbi, type);
  354. struct summary_footer *sum_footer;
  355. curseg->segno = curseg->next_segno;
  356. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  357. curseg->next_blkoff = 0;
  358. curseg->next_segno = NULL_SEGNO;
  359. sum_footer = &(curseg->sum_blk->footer);
  360. memset(sum_footer, 0, sizeof(struct summary_footer));
  361. if (IS_DATASEG(type))
  362. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  363. if (IS_NODESEG(type))
  364. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  365. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  366. }
  367. /*
  368. * Allocate a current working segment.
  369. * This function always allocates a free segment in LFS manner.
  370. */
  371. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  372. {
  373. struct curseg_info *curseg = CURSEG_I(sbi, type);
  374. unsigned int segno = curseg->segno;
  375. int dir = ALLOC_LEFT;
  376. write_sum_page(sbi, curseg->sum_blk,
  377. GET_SUM_BLOCK(sbi, segno));
  378. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  379. dir = ALLOC_RIGHT;
  380. if (test_opt(sbi, NOHEAP))
  381. dir = ALLOC_RIGHT;
  382. get_new_segment(sbi, &segno, new_sec, dir);
  383. curseg->next_segno = segno;
  384. reset_curseg(sbi, type, 1);
  385. curseg->alloc_type = LFS;
  386. }
  387. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  388. struct curseg_info *seg, block_t start)
  389. {
  390. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  391. block_t ofs;
  392. for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
  393. if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
  394. && !f2fs_test_bit(ofs, se->cur_valid_map))
  395. break;
  396. }
  397. seg->next_blkoff = ofs;
  398. }
  399. /*
  400. * If a segment is written by LFS manner, next block offset is just obtained
  401. * by increasing the current block offset. However, if a segment is written by
  402. * SSR manner, next block offset obtained by calling __next_free_blkoff
  403. */
  404. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  405. struct curseg_info *seg)
  406. {
  407. if (seg->alloc_type == SSR)
  408. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  409. else
  410. seg->next_blkoff++;
  411. }
  412. /*
  413. * This function always allocates a used segment (from dirty seglist) by SSR
  414. * manner, so it should recover the existing segment information of valid blocks
  415. */
  416. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  417. {
  418. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  419. struct curseg_info *curseg = CURSEG_I(sbi, type);
  420. unsigned int new_segno = curseg->next_segno;
  421. struct f2fs_summary_block *sum_node;
  422. struct page *sum_page;
  423. write_sum_page(sbi, curseg->sum_blk,
  424. GET_SUM_BLOCK(sbi, curseg->segno));
  425. __set_test_and_inuse(sbi, new_segno);
  426. mutex_lock(&dirty_i->seglist_lock);
  427. __remove_dirty_segment(sbi, new_segno, PRE);
  428. __remove_dirty_segment(sbi, new_segno, DIRTY);
  429. mutex_unlock(&dirty_i->seglist_lock);
  430. reset_curseg(sbi, type, 1);
  431. curseg->alloc_type = SSR;
  432. __next_free_blkoff(sbi, curseg, 0);
  433. if (reuse) {
  434. sum_page = get_sum_page(sbi, new_segno);
  435. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  436. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  437. f2fs_put_page(sum_page, 1);
  438. }
  439. }
  440. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  441. {
  442. struct curseg_info *curseg = CURSEG_I(sbi, type);
  443. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  444. if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
  445. return v_ops->get_victim(sbi,
  446. &(curseg)->next_segno, BG_GC, type, SSR);
  447. /* For data segments, let's do SSR more intensively */
  448. for (; type >= CURSEG_HOT_DATA; type--)
  449. if (v_ops->get_victim(sbi, &(curseg)->next_segno,
  450. BG_GC, type, SSR))
  451. return 1;
  452. return 0;
  453. }
  454. /*
  455. * flush out current segment and replace it with new segment
  456. * This function should be returned with success, otherwise BUG
  457. */
  458. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  459. int type, bool force)
  460. {
  461. struct curseg_info *curseg = CURSEG_I(sbi, type);
  462. if (force)
  463. new_curseg(sbi, type, true);
  464. else if (type == CURSEG_WARM_NODE)
  465. new_curseg(sbi, type, false);
  466. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  467. new_curseg(sbi, type, false);
  468. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  469. change_curseg(sbi, type, true);
  470. else
  471. new_curseg(sbi, type, false);
  472. stat_inc_seg_type(sbi, curseg);
  473. }
  474. void allocate_new_segments(struct f2fs_sb_info *sbi)
  475. {
  476. struct curseg_info *curseg;
  477. unsigned int old_curseg;
  478. int i;
  479. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  480. curseg = CURSEG_I(sbi, i);
  481. old_curseg = curseg->segno;
  482. SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
  483. locate_dirty_segment(sbi, old_curseg);
  484. }
  485. }
  486. static const struct segment_allocation default_salloc_ops = {
  487. .allocate_segment = allocate_segment_by_default,
  488. };
  489. static void f2fs_end_io_write(struct bio *bio, int err)
  490. {
  491. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  492. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  493. struct bio_private *p = bio->bi_private;
  494. do {
  495. struct page *page = bvec->bv_page;
  496. if (--bvec >= bio->bi_io_vec)
  497. prefetchw(&bvec->bv_page->flags);
  498. if (!uptodate) {
  499. SetPageError(page);
  500. if (page->mapping)
  501. set_bit(AS_EIO, &page->mapping->flags);
  502. set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
  503. p->sbi->sb->s_flags |= MS_RDONLY;
  504. }
  505. end_page_writeback(page);
  506. dec_page_count(p->sbi, F2FS_WRITEBACK);
  507. } while (bvec >= bio->bi_io_vec);
  508. if (p->is_sync)
  509. complete(p->wait);
  510. if (!get_pages(p->sbi, F2FS_WRITEBACK) && p->sbi->cp_task)
  511. wake_up_process(p->sbi->cp_task);
  512. kfree(p);
  513. bio_put(bio);
  514. }
  515. struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
  516. {
  517. struct bio *bio;
  518. /* No failure on bio allocation */
  519. bio = bio_alloc(GFP_NOIO, npages);
  520. bio->bi_bdev = bdev;
  521. bio->bi_private = NULL;
  522. return bio;
  523. }
  524. static void do_submit_bio(struct f2fs_sb_info *sbi,
  525. enum page_type type, bool sync)
  526. {
  527. int rw = sync ? WRITE_SYNC : WRITE;
  528. enum page_type btype = type > META ? META : type;
  529. if (type >= META_FLUSH)
  530. rw = WRITE_FLUSH_FUA;
  531. if (btype == META)
  532. rw |= REQ_META;
  533. if (sbi->bio[btype]) {
  534. struct bio_private *p = sbi->bio[btype]->bi_private;
  535. p->sbi = sbi;
  536. sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
  537. trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
  538. if (type == META_FLUSH) {
  539. DECLARE_COMPLETION_ONSTACK(wait);
  540. p->is_sync = true;
  541. p->wait = &wait;
  542. submit_bio(rw, sbi->bio[btype]);
  543. wait_for_completion(&wait);
  544. } else {
  545. p->is_sync = false;
  546. submit_bio(rw, sbi->bio[btype]);
  547. }
  548. sbi->bio[btype] = NULL;
  549. }
  550. }
  551. void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
  552. {
  553. down_write(&sbi->bio_sem);
  554. do_submit_bio(sbi, type, sync);
  555. up_write(&sbi->bio_sem);
  556. }
  557. static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
  558. block_t blk_addr, enum page_type type)
  559. {
  560. struct block_device *bdev = sbi->sb->s_bdev;
  561. int bio_blocks;
  562. verify_block_addr(sbi, blk_addr);
  563. down_write(&sbi->bio_sem);
  564. inc_page_count(sbi, F2FS_WRITEBACK);
  565. if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
  566. do_submit_bio(sbi, type, false);
  567. alloc_new:
  568. if (sbi->bio[type] == NULL) {
  569. struct bio_private *priv;
  570. retry:
  571. priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
  572. if (!priv) {
  573. cond_resched();
  574. goto retry;
  575. }
  576. bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
  577. sbi->bio[type] = f2fs_bio_alloc(bdev, bio_blocks);
  578. sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
  579. sbi->bio[type]->bi_private = priv;
  580. /*
  581. * The end_io will be assigned at the sumbission phase.
  582. * Until then, let bio_add_page() merge consecutive IOs as much
  583. * as possible.
  584. */
  585. }
  586. if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
  587. PAGE_CACHE_SIZE) {
  588. do_submit_bio(sbi, type, false);
  589. goto alloc_new;
  590. }
  591. sbi->last_block_in_bio[type] = blk_addr;
  592. up_write(&sbi->bio_sem);
  593. trace_f2fs_submit_write_page(page, blk_addr, type);
  594. }
  595. void f2fs_wait_on_page_writeback(struct page *page,
  596. enum page_type type, bool sync)
  597. {
  598. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  599. if (PageWriteback(page)) {
  600. f2fs_submit_bio(sbi, type, sync);
  601. wait_on_page_writeback(page);
  602. }
  603. }
  604. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  605. {
  606. struct curseg_info *curseg = CURSEG_I(sbi, type);
  607. if (curseg->next_blkoff < sbi->blocks_per_seg)
  608. return true;
  609. return false;
  610. }
  611. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  612. {
  613. if (p_type == DATA)
  614. return CURSEG_HOT_DATA;
  615. else
  616. return CURSEG_HOT_NODE;
  617. }
  618. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  619. {
  620. if (p_type == DATA) {
  621. struct inode *inode = page->mapping->host;
  622. if (S_ISDIR(inode->i_mode))
  623. return CURSEG_HOT_DATA;
  624. else
  625. return CURSEG_COLD_DATA;
  626. } else {
  627. if (IS_DNODE(page) && !is_cold_node(page))
  628. return CURSEG_HOT_NODE;
  629. else
  630. return CURSEG_COLD_NODE;
  631. }
  632. }
  633. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  634. {
  635. if (p_type == DATA) {
  636. struct inode *inode = page->mapping->host;
  637. if (S_ISDIR(inode->i_mode))
  638. return CURSEG_HOT_DATA;
  639. else if (is_cold_data(page) || file_is_cold(inode))
  640. return CURSEG_COLD_DATA;
  641. else
  642. return CURSEG_WARM_DATA;
  643. } else {
  644. if (IS_DNODE(page))
  645. return is_cold_node(page) ? CURSEG_WARM_NODE :
  646. CURSEG_HOT_NODE;
  647. else
  648. return CURSEG_COLD_NODE;
  649. }
  650. }
  651. static int __get_segment_type(struct page *page, enum page_type p_type)
  652. {
  653. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  654. switch (sbi->active_logs) {
  655. case 2:
  656. return __get_segment_type_2(page, p_type);
  657. case 4:
  658. return __get_segment_type_4(page, p_type);
  659. }
  660. /* NR_CURSEG_TYPE(6) logs by default */
  661. f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
  662. return __get_segment_type_6(page, p_type);
  663. }
  664. static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
  665. block_t old_blkaddr, block_t *new_blkaddr,
  666. struct f2fs_summary *sum, enum page_type p_type)
  667. {
  668. struct sit_info *sit_i = SIT_I(sbi);
  669. struct curseg_info *curseg;
  670. unsigned int old_cursegno;
  671. int type;
  672. type = __get_segment_type(page, p_type);
  673. curseg = CURSEG_I(sbi, type);
  674. mutex_lock(&curseg->curseg_mutex);
  675. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  676. old_cursegno = curseg->segno;
  677. /*
  678. * __add_sum_entry should be resided under the curseg_mutex
  679. * because, this function updates a summary entry in the
  680. * current summary block.
  681. */
  682. __add_sum_entry(sbi, type, sum);
  683. mutex_lock(&sit_i->sentry_lock);
  684. __refresh_next_blkoff(sbi, curseg);
  685. stat_inc_block_count(sbi, curseg);
  686. /*
  687. * SIT information should be updated before segment allocation,
  688. * since SSR needs latest valid block information.
  689. */
  690. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  691. if (!__has_curseg_space(sbi, type))
  692. sit_i->s_ops->allocate_segment(sbi, type, false);
  693. locate_dirty_segment(sbi, old_cursegno);
  694. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  695. mutex_unlock(&sit_i->sentry_lock);
  696. if (p_type == NODE)
  697. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  698. /* writeout dirty page into bdev */
  699. submit_write_page(sbi, page, *new_blkaddr, p_type);
  700. mutex_unlock(&curseg->curseg_mutex);
  701. }
  702. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
  703. {
  704. set_page_writeback(page);
  705. submit_write_page(sbi, page, page->index, META);
  706. }
  707. void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
  708. unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
  709. {
  710. struct f2fs_summary sum;
  711. set_summary(&sum, nid, 0, 0);
  712. do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
  713. }
  714. void write_data_page(struct inode *inode, struct page *page,
  715. struct dnode_of_data *dn, block_t old_blkaddr,
  716. block_t *new_blkaddr)
  717. {
  718. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  719. struct f2fs_summary sum;
  720. struct node_info ni;
  721. f2fs_bug_on(old_blkaddr == NULL_ADDR);
  722. get_node_info(sbi, dn->nid, &ni);
  723. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  724. do_write_page(sbi, page, old_blkaddr,
  725. new_blkaddr, &sum, DATA);
  726. }
  727. void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
  728. block_t old_blk_addr)
  729. {
  730. submit_write_page(sbi, page, old_blk_addr, DATA);
  731. }
  732. void recover_data_page(struct f2fs_sb_info *sbi,
  733. struct page *page, struct f2fs_summary *sum,
  734. block_t old_blkaddr, block_t new_blkaddr)
  735. {
  736. struct sit_info *sit_i = SIT_I(sbi);
  737. struct curseg_info *curseg;
  738. unsigned int segno, old_cursegno;
  739. struct seg_entry *se;
  740. int type;
  741. segno = GET_SEGNO(sbi, new_blkaddr);
  742. se = get_seg_entry(sbi, segno);
  743. type = se->type;
  744. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  745. if (old_blkaddr == NULL_ADDR)
  746. type = CURSEG_COLD_DATA;
  747. else
  748. type = CURSEG_WARM_DATA;
  749. }
  750. curseg = CURSEG_I(sbi, type);
  751. mutex_lock(&curseg->curseg_mutex);
  752. mutex_lock(&sit_i->sentry_lock);
  753. old_cursegno = curseg->segno;
  754. /* change the current segment */
  755. if (segno != curseg->segno) {
  756. curseg->next_segno = segno;
  757. change_curseg(sbi, type, true);
  758. }
  759. curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
  760. (sbi->blocks_per_seg - 1);
  761. __add_sum_entry(sbi, type, sum);
  762. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  763. locate_dirty_segment(sbi, old_cursegno);
  764. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  765. mutex_unlock(&sit_i->sentry_lock);
  766. mutex_unlock(&curseg->curseg_mutex);
  767. }
  768. void rewrite_node_page(struct f2fs_sb_info *sbi,
  769. struct page *page, struct f2fs_summary *sum,
  770. block_t old_blkaddr, block_t new_blkaddr)
  771. {
  772. struct sit_info *sit_i = SIT_I(sbi);
  773. int type = CURSEG_WARM_NODE;
  774. struct curseg_info *curseg;
  775. unsigned int segno, old_cursegno;
  776. block_t next_blkaddr = next_blkaddr_of_node(page);
  777. unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
  778. curseg = CURSEG_I(sbi, type);
  779. mutex_lock(&curseg->curseg_mutex);
  780. mutex_lock(&sit_i->sentry_lock);
  781. segno = GET_SEGNO(sbi, new_blkaddr);
  782. old_cursegno = curseg->segno;
  783. /* change the current segment */
  784. if (segno != curseg->segno) {
  785. curseg->next_segno = segno;
  786. change_curseg(sbi, type, true);
  787. }
  788. curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
  789. (sbi->blocks_per_seg - 1);
  790. __add_sum_entry(sbi, type, sum);
  791. /* change the current log to the next block addr in advance */
  792. if (next_segno != segno) {
  793. curseg->next_segno = next_segno;
  794. change_curseg(sbi, type, true);
  795. }
  796. curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
  797. (sbi->blocks_per_seg - 1);
  798. /* rewrite node page */
  799. set_page_writeback(page);
  800. submit_write_page(sbi, page, new_blkaddr, NODE);
  801. f2fs_submit_bio(sbi, NODE, true);
  802. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  803. locate_dirty_segment(sbi, old_cursegno);
  804. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  805. mutex_unlock(&sit_i->sentry_lock);
  806. mutex_unlock(&curseg->curseg_mutex);
  807. }
  808. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  809. {
  810. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  811. struct curseg_info *seg_i;
  812. unsigned char *kaddr;
  813. struct page *page;
  814. block_t start;
  815. int i, j, offset;
  816. start = start_sum_block(sbi);
  817. page = get_meta_page(sbi, start++);
  818. kaddr = (unsigned char *)page_address(page);
  819. /* Step 1: restore nat cache */
  820. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  821. memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
  822. /* Step 2: restore sit cache */
  823. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  824. memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
  825. SUM_JOURNAL_SIZE);
  826. offset = 2 * SUM_JOURNAL_SIZE;
  827. /* Step 3: restore summary entries */
  828. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  829. unsigned short blk_off;
  830. unsigned int segno;
  831. seg_i = CURSEG_I(sbi, i);
  832. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  833. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  834. seg_i->next_segno = segno;
  835. reset_curseg(sbi, i, 0);
  836. seg_i->alloc_type = ckpt->alloc_type[i];
  837. seg_i->next_blkoff = blk_off;
  838. if (seg_i->alloc_type == SSR)
  839. blk_off = sbi->blocks_per_seg;
  840. for (j = 0; j < blk_off; j++) {
  841. struct f2fs_summary *s;
  842. s = (struct f2fs_summary *)(kaddr + offset);
  843. seg_i->sum_blk->entries[j] = *s;
  844. offset += SUMMARY_SIZE;
  845. if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  846. SUM_FOOTER_SIZE)
  847. continue;
  848. f2fs_put_page(page, 1);
  849. page = NULL;
  850. page = get_meta_page(sbi, start++);
  851. kaddr = (unsigned char *)page_address(page);
  852. offset = 0;
  853. }
  854. }
  855. f2fs_put_page(page, 1);
  856. return 0;
  857. }
  858. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  859. {
  860. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  861. struct f2fs_summary_block *sum;
  862. struct curseg_info *curseg;
  863. struct page *new;
  864. unsigned short blk_off;
  865. unsigned int segno = 0;
  866. block_t blk_addr = 0;
  867. /* get segment number and block addr */
  868. if (IS_DATASEG(type)) {
  869. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  870. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  871. CURSEG_HOT_DATA]);
  872. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  873. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  874. else
  875. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  876. } else {
  877. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  878. CURSEG_HOT_NODE]);
  879. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  880. CURSEG_HOT_NODE]);
  881. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  882. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  883. type - CURSEG_HOT_NODE);
  884. else
  885. blk_addr = GET_SUM_BLOCK(sbi, segno);
  886. }
  887. new = get_meta_page(sbi, blk_addr);
  888. sum = (struct f2fs_summary_block *)page_address(new);
  889. if (IS_NODESEG(type)) {
  890. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
  891. struct f2fs_summary *ns = &sum->entries[0];
  892. int i;
  893. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  894. ns->version = 0;
  895. ns->ofs_in_node = 0;
  896. }
  897. } else {
  898. if (restore_node_summary(sbi, segno, sum)) {
  899. f2fs_put_page(new, 1);
  900. return -EINVAL;
  901. }
  902. }
  903. }
  904. /* set uncompleted segment to curseg */
  905. curseg = CURSEG_I(sbi, type);
  906. mutex_lock(&curseg->curseg_mutex);
  907. memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
  908. curseg->next_segno = segno;
  909. reset_curseg(sbi, type, 0);
  910. curseg->alloc_type = ckpt->alloc_type[type];
  911. curseg->next_blkoff = blk_off;
  912. mutex_unlock(&curseg->curseg_mutex);
  913. f2fs_put_page(new, 1);
  914. return 0;
  915. }
  916. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  917. {
  918. int type = CURSEG_HOT_DATA;
  919. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
  920. /* restore for compacted data summary */
  921. if (read_compacted_summaries(sbi))
  922. return -EINVAL;
  923. type = CURSEG_HOT_NODE;
  924. }
  925. for (; type <= CURSEG_COLD_NODE; type++)
  926. if (read_normal_summaries(sbi, type))
  927. return -EINVAL;
  928. return 0;
  929. }
  930. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  931. {
  932. struct page *page;
  933. unsigned char *kaddr;
  934. struct f2fs_summary *summary;
  935. struct curseg_info *seg_i;
  936. int written_size = 0;
  937. int i, j;
  938. page = grab_meta_page(sbi, blkaddr++);
  939. kaddr = (unsigned char *)page_address(page);
  940. /* Step 1: write nat cache */
  941. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  942. memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
  943. written_size += SUM_JOURNAL_SIZE;
  944. /* Step 2: write sit cache */
  945. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  946. memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
  947. SUM_JOURNAL_SIZE);
  948. written_size += SUM_JOURNAL_SIZE;
  949. /* Step 3: write summary entries */
  950. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  951. unsigned short blkoff;
  952. seg_i = CURSEG_I(sbi, i);
  953. if (sbi->ckpt->alloc_type[i] == SSR)
  954. blkoff = sbi->blocks_per_seg;
  955. else
  956. blkoff = curseg_blkoff(sbi, i);
  957. for (j = 0; j < blkoff; j++) {
  958. if (!page) {
  959. page = grab_meta_page(sbi, blkaddr++);
  960. kaddr = (unsigned char *)page_address(page);
  961. written_size = 0;
  962. }
  963. summary = (struct f2fs_summary *)(kaddr + written_size);
  964. *summary = seg_i->sum_blk->entries[j];
  965. written_size += SUMMARY_SIZE;
  966. if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  967. SUM_FOOTER_SIZE)
  968. continue;
  969. set_page_dirty(page);
  970. f2fs_put_page(page, 1);
  971. page = NULL;
  972. }
  973. }
  974. if (page) {
  975. set_page_dirty(page);
  976. f2fs_put_page(page, 1);
  977. }
  978. }
  979. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  980. block_t blkaddr, int type)
  981. {
  982. int i, end;
  983. if (IS_DATASEG(type))
  984. end = type + NR_CURSEG_DATA_TYPE;
  985. else
  986. end = type + NR_CURSEG_NODE_TYPE;
  987. for (i = type; i < end; i++) {
  988. struct curseg_info *sum = CURSEG_I(sbi, i);
  989. mutex_lock(&sum->curseg_mutex);
  990. write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
  991. mutex_unlock(&sum->curseg_mutex);
  992. }
  993. }
  994. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  995. {
  996. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
  997. write_compacted_summaries(sbi, start_blk);
  998. else
  999. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1000. }
  1001. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1002. {
  1003. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
  1004. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1005. }
  1006. int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
  1007. unsigned int val, int alloc)
  1008. {
  1009. int i;
  1010. if (type == NAT_JOURNAL) {
  1011. for (i = 0; i < nats_in_cursum(sum); i++) {
  1012. if (le32_to_cpu(nid_in_journal(sum, i)) == val)
  1013. return i;
  1014. }
  1015. if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
  1016. return update_nats_in_cursum(sum, 1);
  1017. } else if (type == SIT_JOURNAL) {
  1018. for (i = 0; i < sits_in_cursum(sum); i++)
  1019. if (le32_to_cpu(segno_in_journal(sum, i)) == val)
  1020. return i;
  1021. if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
  1022. return update_sits_in_cursum(sum, 1);
  1023. }
  1024. return -1;
  1025. }
  1026. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1027. unsigned int segno)
  1028. {
  1029. struct sit_info *sit_i = SIT_I(sbi);
  1030. unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
  1031. block_t blk_addr = sit_i->sit_base_addr + offset;
  1032. check_seg_range(sbi, segno);
  1033. /* calculate sit block address */
  1034. if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  1035. blk_addr += sit_i->sit_blocks;
  1036. return get_meta_page(sbi, blk_addr);
  1037. }
  1038. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1039. unsigned int start)
  1040. {
  1041. struct sit_info *sit_i = SIT_I(sbi);
  1042. struct page *src_page, *dst_page;
  1043. pgoff_t src_off, dst_off;
  1044. void *src_addr, *dst_addr;
  1045. src_off = current_sit_addr(sbi, start);
  1046. dst_off = next_sit_addr(sbi, src_off);
  1047. /* get current sit block page without lock */
  1048. src_page = get_meta_page(sbi, src_off);
  1049. dst_page = grab_meta_page(sbi, dst_off);
  1050. f2fs_bug_on(PageDirty(src_page));
  1051. src_addr = page_address(src_page);
  1052. dst_addr = page_address(dst_page);
  1053. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  1054. set_page_dirty(dst_page);
  1055. f2fs_put_page(src_page, 1);
  1056. set_to_next_sit(sit_i, start);
  1057. return dst_page;
  1058. }
  1059. static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
  1060. {
  1061. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1062. struct f2fs_summary_block *sum = curseg->sum_blk;
  1063. int i;
  1064. /*
  1065. * If the journal area in the current summary is full of sit entries,
  1066. * all the sit entries will be flushed. Otherwise the sit entries
  1067. * are not able to replace with newly hot sit entries.
  1068. */
  1069. if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
  1070. for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
  1071. unsigned int segno;
  1072. segno = le32_to_cpu(segno_in_journal(sum, i));
  1073. __mark_sit_entry_dirty(sbi, segno);
  1074. }
  1075. update_sits_in_cursum(sum, -sits_in_cursum(sum));
  1076. return true;
  1077. }
  1078. return false;
  1079. }
  1080. /*
  1081. * CP calls this function, which flushes SIT entries including sit_journal,
  1082. * and moves prefree segs to free segs.
  1083. */
  1084. void flush_sit_entries(struct f2fs_sb_info *sbi)
  1085. {
  1086. struct sit_info *sit_i = SIT_I(sbi);
  1087. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1088. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1089. struct f2fs_summary_block *sum = curseg->sum_blk;
  1090. unsigned long nsegs = TOTAL_SEGS(sbi);
  1091. struct page *page = NULL;
  1092. struct f2fs_sit_block *raw_sit = NULL;
  1093. unsigned int start = 0, end = 0;
  1094. unsigned int segno = -1;
  1095. bool flushed;
  1096. mutex_lock(&curseg->curseg_mutex);
  1097. mutex_lock(&sit_i->sentry_lock);
  1098. /*
  1099. * "flushed" indicates whether sit entries in journal are flushed
  1100. * to the SIT area or not.
  1101. */
  1102. flushed = flush_sits_in_journal(sbi);
  1103. while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
  1104. struct seg_entry *se = get_seg_entry(sbi, segno);
  1105. int sit_offset, offset;
  1106. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1107. if (flushed)
  1108. goto to_sit_page;
  1109. offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
  1110. if (offset >= 0) {
  1111. segno_in_journal(sum, offset) = cpu_to_le32(segno);
  1112. seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
  1113. goto flush_done;
  1114. }
  1115. to_sit_page:
  1116. if (!page || (start > segno) || (segno > end)) {
  1117. if (page) {
  1118. f2fs_put_page(page, 1);
  1119. page = NULL;
  1120. }
  1121. start = START_SEGNO(sit_i, segno);
  1122. end = start + SIT_ENTRY_PER_BLOCK - 1;
  1123. /* read sit block that will be updated */
  1124. page = get_next_sit_page(sbi, start);
  1125. raw_sit = page_address(page);
  1126. }
  1127. /* udpate entry in SIT block */
  1128. seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
  1129. flush_done:
  1130. __clear_bit(segno, bitmap);
  1131. sit_i->dirty_sentries--;
  1132. }
  1133. mutex_unlock(&sit_i->sentry_lock);
  1134. mutex_unlock(&curseg->curseg_mutex);
  1135. /* writeout last modified SIT block */
  1136. f2fs_put_page(page, 1);
  1137. set_prefree_as_free_segments(sbi);
  1138. }
  1139. static int build_sit_info(struct f2fs_sb_info *sbi)
  1140. {
  1141. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1142. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1143. struct sit_info *sit_i;
  1144. unsigned int sit_segs, start;
  1145. char *src_bitmap, *dst_bitmap;
  1146. unsigned int bitmap_size;
  1147. /* allocate memory for SIT information */
  1148. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1149. if (!sit_i)
  1150. return -ENOMEM;
  1151. SM_I(sbi)->sit_info = sit_i;
  1152. sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
  1153. if (!sit_i->sentries)
  1154. return -ENOMEM;
  1155. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1156. sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1157. if (!sit_i->dirty_sentries_bitmap)
  1158. return -ENOMEM;
  1159. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1160. sit_i->sentries[start].cur_valid_map
  1161. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1162. sit_i->sentries[start].ckpt_valid_map
  1163. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1164. if (!sit_i->sentries[start].cur_valid_map
  1165. || !sit_i->sentries[start].ckpt_valid_map)
  1166. return -ENOMEM;
  1167. }
  1168. if (sbi->segs_per_sec > 1) {
  1169. sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
  1170. sizeof(struct sec_entry));
  1171. if (!sit_i->sec_entries)
  1172. return -ENOMEM;
  1173. }
  1174. /* get information related with SIT */
  1175. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1176. /* setup SIT bitmap from ckeckpoint pack */
  1177. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1178. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1179. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1180. if (!dst_bitmap)
  1181. return -ENOMEM;
  1182. /* init SIT information */
  1183. sit_i->s_ops = &default_salloc_ops;
  1184. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1185. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1186. sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
  1187. sit_i->sit_bitmap = dst_bitmap;
  1188. sit_i->bitmap_size = bitmap_size;
  1189. sit_i->dirty_sentries = 0;
  1190. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1191. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1192. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1193. mutex_init(&sit_i->sentry_lock);
  1194. return 0;
  1195. }
  1196. static int build_free_segmap(struct f2fs_sb_info *sbi)
  1197. {
  1198. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1199. struct free_segmap_info *free_i;
  1200. unsigned int bitmap_size, sec_bitmap_size;
  1201. /* allocate memory for free segmap information */
  1202. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  1203. if (!free_i)
  1204. return -ENOMEM;
  1205. SM_I(sbi)->free_info = free_i;
  1206. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1207. free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
  1208. if (!free_i->free_segmap)
  1209. return -ENOMEM;
  1210. sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1211. free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
  1212. if (!free_i->free_secmap)
  1213. return -ENOMEM;
  1214. /* set all segments as dirty temporarily */
  1215. memset(free_i->free_segmap, 0xff, bitmap_size);
  1216. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  1217. /* init free segmap information */
  1218. free_i->start_segno =
  1219. (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
  1220. free_i->free_segments = 0;
  1221. free_i->free_sections = 0;
  1222. rwlock_init(&free_i->segmap_lock);
  1223. return 0;
  1224. }
  1225. static int build_curseg(struct f2fs_sb_info *sbi)
  1226. {
  1227. struct curseg_info *array;
  1228. int i;
  1229. array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
  1230. if (!array)
  1231. return -ENOMEM;
  1232. SM_I(sbi)->curseg_array = array;
  1233. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  1234. mutex_init(&array[i].curseg_mutex);
  1235. array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  1236. if (!array[i].sum_blk)
  1237. return -ENOMEM;
  1238. array[i].segno = NULL_SEGNO;
  1239. array[i].next_blkoff = 0;
  1240. }
  1241. return restore_curseg_summaries(sbi);
  1242. }
  1243. static void build_sit_entries(struct f2fs_sb_info *sbi)
  1244. {
  1245. struct sit_info *sit_i = SIT_I(sbi);
  1246. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1247. struct f2fs_summary_block *sum = curseg->sum_blk;
  1248. unsigned int start;
  1249. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1250. struct seg_entry *se = &sit_i->sentries[start];
  1251. struct f2fs_sit_block *sit_blk;
  1252. struct f2fs_sit_entry sit;
  1253. struct page *page;
  1254. int i;
  1255. mutex_lock(&curseg->curseg_mutex);
  1256. for (i = 0; i < sits_in_cursum(sum); i++) {
  1257. if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
  1258. sit = sit_in_journal(sum, i);
  1259. mutex_unlock(&curseg->curseg_mutex);
  1260. goto got_it;
  1261. }
  1262. }
  1263. mutex_unlock(&curseg->curseg_mutex);
  1264. page = get_current_sit_page(sbi, start);
  1265. sit_blk = (struct f2fs_sit_block *)page_address(page);
  1266. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  1267. f2fs_put_page(page, 1);
  1268. got_it:
  1269. check_block_count(sbi, start, &sit);
  1270. seg_info_from_raw_sit(se, &sit);
  1271. if (sbi->segs_per_sec > 1) {
  1272. struct sec_entry *e = get_sec_entry(sbi, start);
  1273. e->valid_blocks += se->valid_blocks;
  1274. }
  1275. }
  1276. }
  1277. static void init_free_segmap(struct f2fs_sb_info *sbi)
  1278. {
  1279. unsigned int start;
  1280. int type;
  1281. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1282. struct seg_entry *sentry = get_seg_entry(sbi, start);
  1283. if (!sentry->valid_blocks)
  1284. __set_free(sbi, start);
  1285. }
  1286. /* set use the current segments */
  1287. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  1288. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  1289. __set_test_and_inuse(sbi, curseg_t->segno);
  1290. }
  1291. }
  1292. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  1293. {
  1294. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1295. struct free_segmap_info *free_i = FREE_I(sbi);
  1296. unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
  1297. unsigned short valid_blocks;
  1298. while (1) {
  1299. /* find dirty segment based on free segmap */
  1300. segno = find_next_inuse(free_i, total_segs, offset);
  1301. if (segno >= total_segs)
  1302. break;
  1303. offset = segno + 1;
  1304. valid_blocks = get_valid_blocks(sbi, segno, 0);
  1305. if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
  1306. continue;
  1307. mutex_lock(&dirty_i->seglist_lock);
  1308. __locate_dirty_segment(sbi, segno, DIRTY);
  1309. mutex_unlock(&dirty_i->seglist_lock);
  1310. }
  1311. }
  1312. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  1313. {
  1314. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1315. unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1316. dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
  1317. if (!dirty_i->victim_secmap)
  1318. return -ENOMEM;
  1319. return 0;
  1320. }
  1321. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  1322. {
  1323. struct dirty_seglist_info *dirty_i;
  1324. unsigned int bitmap_size, i;
  1325. /* allocate memory for dirty segments list information */
  1326. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  1327. if (!dirty_i)
  1328. return -ENOMEM;
  1329. SM_I(sbi)->dirty_info = dirty_i;
  1330. mutex_init(&dirty_i->seglist_lock);
  1331. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1332. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  1333. dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
  1334. if (!dirty_i->dirty_segmap[i])
  1335. return -ENOMEM;
  1336. }
  1337. init_dirty_segmap(sbi);
  1338. return init_victim_secmap(sbi);
  1339. }
  1340. /*
  1341. * Update min, max modified time for cost-benefit GC algorithm
  1342. */
  1343. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  1344. {
  1345. struct sit_info *sit_i = SIT_I(sbi);
  1346. unsigned int segno;
  1347. mutex_lock(&sit_i->sentry_lock);
  1348. sit_i->min_mtime = LLONG_MAX;
  1349. for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
  1350. unsigned int i;
  1351. unsigned long long mtime = 0;
  1352. for (i = 0; i < sbi->segs_per_sec; i++)
  1353. mtime += get_seg_entry(sbi, segno + i)->mtime;
  1354. mtime = div_u64(mtime, sbi->segs_per_sec);
  1355. if (sit_i->min_mtime > mtime)
  1356. sit_i->min_mtime = mtime;
  1357. }
  1358. sit_i->max_mtime = get_mtime(sbi);
  1359. mutex_unlock(&sit_i->sentry_lock);
  1360. }
  1361. int build_segment_manager(struct f2fs_sb_info *sbi)
  1362. {
  1363. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1364. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1365. struct f2fs_sm_info *sm_info;
  1366. int err;
  1367. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  1368. if (!sm_info)
  1369. return -ENOMEM;
  1370. /* init sm info */
  1371. sbi->sm_info = sm_info;
  1372. INIT_LIST_HEAD(&sm_info->wblist_head);
  1373. spin_lock_init(&sm_info->wblist_lock);
  1374. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1375. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1376. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  1377. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1378. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1379. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  1380. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1381. sm_info->rec_prefree_segments = DEF_RECLAIM_PREFREE_SEGMENTS;
  1382. err = build_sit_info(sbi);
  1383. if (err)
  1384. return err;
  1385. err = build_free_segmap(sbi);
  1386. if (err)
  1387. return err;
  1388. err = build_curseg(sbi);
  1389. if (err)
  1390. return err;
  1391. /* reinit free segmap based on SIT */
  1392. build_sit_entries(sbi);
  1393. init_free_segmap(sbi);
  1394. err = build_dirty_segmap(sbi);
  1395. if (err)
  1396. return err;
  1397. init_min_max_mtime(sbi);
  1398. return 0;
  1399. }
  1400. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  1401. enum dirty_type dirty_type)
  1402. {
  1403. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1404. mutex_lock(&dirty_i->seglist_lock);
  1405. kfree(dirty_i->dirty_segmap[dirty_type]);
  1406. dirty_i->nr_dirty[dirty_type] = 0;
  1407. mutex_unlock(&dirty_i->seglist_lock);
  1408. }
  1409. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  1410. {
  1411. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1412. kfree(dirty_i->victim_secmap);
  1413. }
  1414. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  1415. {
  1416. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1417. int i;
  1418. if (!dirty_i)
  1419. return;
  1420. /* discard pre-free/dirty segments list */
  1421. for (i = 0; i < NR_DIRTY_TYPE; i++)
  1422. discard_dirty_segmap(sbi, i);
  1423. destroy_victim_secmap(sbi);
  1424. SM_I(sbi)->dirty_info = NULL;
  1425. kfree(dirty_i);
  1426. }
  1427. static void destroy_curseg(struct f2fs_sb_info *sbi)
  1428. {
  1429. struct curseg_info *array = SM_I(sbi)->curseg_array;
  1430. int i;
  1431. if (!array)
  1432. return;
  1433. SM_I(sbi)->curseg_array = NULL;
  1434. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1435. kfree(array[i].sum_blk);
  1436. kfree(array);
  1437. }
  1438. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  1439. {
  1440. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  1441. if (!free_i)
  1442. return;
  1443. SM_I(sbi)->free_info = NULL;
  1444. kfree(free_i->free_segmap);
  1445. kfree(free_i->free_secmap);
  1446. kfree(free_i);
  1447. }
  1448. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  1449. {
  1450. struct sit_info *sit_i = SIT_I(sbi);
  1451. unsigned int start;
  1452. if (!sit_i)
  1453. return;
  1454. if (sit_i->sentries) {
  1455. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1456. kfree(sit_i->sentries[start].cur_valid_map);
  1457. kfree(sit_i->sentries[start].ckpt_valid_map);
  1458. }
  1459. }
  1460. vfree(sit_i->sentries);
  1461. vfree(sit_i->sec_entries);
  1462. kfree(sit_i->dirty_sentries_bitmap);
  1463. SM_I(sbi)->sit_info = NULL;
  1464. kfree(sit_i->sit_bitmap);
  1465. kfree(sit_i);
  1466. }
  1467. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  1468. {
  1469. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1470. if (!sm_info)
  1471. return;
  1472. destroy_dirty_segmap(sbi);
  1473. destroy_curseg(sbi);
  1474. destroy_free_segmap(sbi);
  1475. destroy_sit_info(sbi);
  1476. sbi->sm_info = NULL;
  1477. kfree(sm_info);
  1478. }