segment.c 47 KB

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