segment.c 47 KB

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