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