segment.c 46 KB

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