segment.c 46 KB

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