segment.c 47 KB

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