segment.c 46 KB

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