segment.c 46 KB

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