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