segment.c 46 KB

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