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