segment.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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 (sbi->bio[btype]) {
  592. struct bio_private *p = sbi->bio[btype]->bi_private;
  593. p->sbi = sbi;
  594. sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
  595. trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
  596. if (type == META_FLUSH) {
  597. DECLARE_COMPLETION_ONSTACK(wait);
  598. p->is_sync = true;
  599. p->wait = &wait;
  600. submit_bio(rw, sbi->bio[btype]);
  601. wait_for_completion(&wait);
  602. } else {
  603. p->is_sync = false;
  604. submit_bio(rw, sbi->bio[btype]);
  605. }
  606. sbi->bio[btype] = NULL;
  607. }
  608. }
  609. void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
  610. {
  611. down_write(&sbi->bio_sem);
  612. do_submit_bio(sbi, type, sync);
  613. up_write(&sbi->bio_sem);
  614. }
  615. static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
  616. block_t blk_addr, enum page_type type)
  617. {
  618. struct block_device *bdev = sbi->sb->s_bdev;
  619. verify_block_addr(sbi, blk_addr);
  620. down_write(&sbi->bio_sem);
  621. inc_page_count(sbi, F2FS_WRITEBACK);
  622. if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
  623. do_submit_bio(sbi, type, false);
  624. alloc_new:
  625. if (sbi->bio[type] == NULL) {
  626. sbi->bio[type] = f2fs_bio_alloc(bdev, bio_get_nr_vecs(bdev));
  627. sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
  628. /*
  629. * The end_io will be assigned at the sumbission phase.
  630. * Until then, let bio_add_page() merge consecutive IOs as much
  631. * as possible.
  632. */
  633. }
  634. if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
  635. PAGE_CACHE_SIZE) {
  636. do_submit_bio(sbi, type, false);
  637. goto alloc_new;
  638. }
  639. sbi->last_block_in_bio[type] = blk_addr;
  640. up_write(&sbi->bio_sem);
  641. trace_f2fs_submit_write_page(page, blk_addr, type);
  642. }
  643. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  644. {
  645. struct curseg_info *curseg = CURSEG_I(sbi, type);
  646. if (curseg->next_blkoff < sbi->blocks_per_seg)
  647. return true;
  648. return false;
  649. }
  650. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  651. {
  652. if (p_type == DATA)
  653. return CURSEG_HOT_DATA;
  654. else
  655. return CURSEG_HOT_NODE;
  656. }
  657. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  658. {
  659. if (p_type == DATA) {
  660. struct inode *inode = page->mapping->host;
  661. if (S_ISDIR(inode->i_mode))
  662. return CURSEG_HOT_DATA;
  663. else
  664. return CURSEG_COLD_DATA;
  665. } else {
  666. if (IS_DNODE(page) && !is_cold_node(page))
  667. return CURSEG_HOT_NODE;
  668. else
  669. return CURSEG_COLD_NODE;
  670. }
  671. }
  672. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  673. {
  674. if (p_type == DATA) {
  675. struct inode *inode = page->mapping->host;
  676. if (S_ISDIR(inode->i_mode))
  677. return CURSEG_HOT_DATA;
  678. else if (is_cold_data(page) || is_cold_file(inode))
  679. return CURSEG_COLD_DATA;
  680. else
  681. return CURSEG_WARM_DATA;
  682. } else {
  683. if (IS_DNODE(page))
  684. return is_cold_node(page) ? CURSEG_WARM_NODE :
  685. CURSEG_HOT_NODE;
  686. else
  687. return CURSEG_COLD_NODE;
  688. }
  689. }
  690. static int __get_segment_type(struct page *page, enum page_type p_type)
  691. {
  692. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  693. switch (sbi->active_logs) {
  694. case 2:
  695. return __get_segment_type_2(page, p_type);
  696. case 4:
  697. return __get_segment_type_4(page, p_type);
  698. }
  699. /* NR_CURSEG_TYPE(6) logs by default */
  700. BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
  701. return __get_segment_type_6(page, p_type);
  702. }
  703. static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
  704. block_t old_blkaddr, block_t *new_blkaddr,
  705. struct f2fs_summary *sum, enum page_type p_type)
  706. {
  707. struct sit_info *sit_i = SIT_I(sbi);
  708. struct curseg_info *curseg;
  709. unsigned int old_cursegno;
  710. int type;
  711. type = __get_segment_type(page, p_type);
  712. curseg = CURSEG_I(sbi, type);
  713. mutex_lock(&curseg->curseg_mutex);
  714. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  715. old_cursegno = curseg->segno;
  716. /*
  717. * __add_sum_entry should be resided under the curseg_mutex
  718. * because, this function updates a summary entry in the
  719. * current summary block.
  720. */
  721. __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
  722. mutex_lock(&sit_i->sentry_lock);
  723. __refresh_next_blkoff(sbi, curseg);
  724. sbi->block_count[curseg->alloc_type]++;
  725. /*
  726. * SIT information should be updated before segment allocation,
  727. * since SSR needs latest valid block information.
  728. */
  729. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  730. if (!__has_curseg_space(sbi, type))
  731. sit_i->s_ops->allocate_segment(sbi, type, false);
  732. locate_dirty_segment(sbi, old_cursegno);
  733. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  734. mutex_unlock(&sit_i->sentry_lock);
  735. if (p_type == NODE)
  736. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  737. /* writeout dirty page into bdev */
  738. submit_write_page(sbi, page, *new_blkaddr, p_type);
  739. mutex_unlock(&curseg->curseg_mutex);
  740. }
  741. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
  742. {
  743. set_page_writeback(page);
  744. submit_write_page(sbi, page, page->index, META);
  745. }
  746. void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
  747. unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
  748. {
  749. struct f2fs_summary sum;
  750. set_summary(&sum, nid, 0, 0);
  751. do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
  752. }
  753. void write_data_page(struct inode *inode, struct page *page,
  754. struct dnode_of_data *dn, block_t old_blkaddr,
  755. block_t *new_blkaddr)
  756. {
  757. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  758. struct f2fs_summary sum;
  759. struct node_info ni;
  760. BUG_ON(old_blkaddr == NULL_ADDR);
  761. get_node_info(sbi, dn->nid, &ni);
  762. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  763. do_write_page(sbi, page, old_blkaddr,
  764. new_blkaddr, &sum, DATA);
  765. }
  766. void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
  767. block_t old_blk_addr)
  768. {
  769. submit_write_page(sbi, page, old_blk_addr, DATA);
  770. }
  771. void recover_data_page(struct f2fs_sb_info *sbi,
  772. struct page *page, struct f2fs_summary *sum,
  773. block_t old_blkaddr, block_t new_blkaddr)
  774. {
  775. struct sit_info *sit_i = SIT_I(sbi);
  776. struct curseg_info *curseg;
  777. unsigned int segno, old_cursegno;
  778. struct seg_entry *se;
  779. int type;
  780. segno = GET_SEGNO(sbi, new_blkaddr);
  781. se = get_seg_entry(sbi, segno);
  782. type = se->type;
  783. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  784. if (old_blkaddr == NULL_ADDR)
  785. type = CURSEG_COLD_DATA;
  786. else
  787. type = CURSEG_WARM_DATA;
  788. }
  789. curseg = CURSEG_I(sbi, type);
  790. mutex_lock(&curseg->curseg_mutex);
  791. mutex_lock(&sit_i->sentry_lock);
  792. old_cursegno = curseg->segno;
  793. /* change the current segment */
  794. if (segno != curseg->segno) {
  795. curseg->next_segno = segno;
  796. change_curseg(sbi, type, true);
  797. }
  798. curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
  799. (sbi->blocks_per_seg - 1);
  800. __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
  801. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  802. locate_dirty_segment(sbi, old_cursegno);
  803. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  804. mutex_unlock(&sit_i->sentry_lock);
  805. mutex_unlock(&curseg->curseg_mutex);
  806. }
  807. void rewrite_node_page(struct f2fs_sb_info *sbi,
  808. struct page *page, struct f2fs_summary *sum,
  809. block_t old_blkaddr, block_t new_blkaddr)
  810. {
  811. struct sit_info *sit_i = SIT_I(sbi);
  812. int type = CURSEG_WARM_NODE;
  813. struct curseg_info *curseg;
  814. unsigned int segno, old_cursegno;
  815. block_t next_blkaddr = next_blkaddr_of_node(page);
  816. unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
  817. curseg = CURSEG_I(sbi, type);
  818. mutex_lock(&curseg->curseg_mutex);
  819. mutex_lock(&sit_i->sentry_lock);
  820. segno = GET_SEGNO(sbi, new_blkaddr);
  821. old_cursegno = curseg->segno;
  822. /* change the current segment */
  823. if (segno != curseg->segno) {
  824. curseg->next_segno = segno;
  825. change_curseg(sbi, type, true);
  826. }
  827. curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
  828. (sbi->blocks_per_seg - 1);
  829. __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
  830. /* change the current log to the next block addr in advance */
  831. if (next_segno != segno) {
  832. curseg->next_segno = next_segno;
  833. change_curseg(sbi, type, true);
  834. }
  835. curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
  836. (sbi->blocks_per_seg - 1);
  837. /* rewrite node page */
  838. set_page_writeback(page);
  839. submit_write_page(sbi, page, new_blkaddr, NODE);
  840. f2fs_submit_bio(sbi, NODE, true);
  841. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  842. locate_dirty_segment(sbi, old_cursegno);
  843. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  844. mutex_unlock(&sit_i->sentry_lock);
  845. mutex_unlock(&curseg->curseg_mutex);
  846. }
  847. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  848. {
  849. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  850. struct curseg_info *seg_i;
  851. unsigned char *kaddr;
  852. struct page *page;
  853. block_t start;
  854. int i, j, offset;
  855. start = start_sum_block(sbi);
  856. page = get_meta_page(sbi, start++);
  857. kaddr = (unsigned char *)page_address(page);
  858. /* Step 1: restore nat cache */
  859. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  860. memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
  861. /* Step 2: restore sit cache */
  862. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  863. memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
  864. SUM_JOURNAL_SIZE);
  865. offset = 2 * SUM_JOURNAL_SIZE;
  866. /* Step 3: restore summary entries */
  867. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  868. unsigned short blk_off;
  869. unsigned int segno;
  870. seg_i = CURSEG_I(sbi, i);
  871. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  872. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  873. seg_i->next_segno = segno;
  874. reset_curseg(sbi, i, 0);
  875. seg_i->alloc_type = ckpt->alloc_type[i];
  876. seg_i->next_blkoff = blk_off;
  877. if (seg_i->alloc_type == SSR)
  878. blk_off = sbi->blocks_per_seg;
  879. for (j = 0; j < blk_off; j++) {
  880. struct f2fs_summary *s;
  881. s = (struct f2fs_summary *)(kaddr + offset);
  882. seg_i->sum_blk->entries[j] = *s;
  883. offset += SUMMARY_SIZE;
  884. if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  885. SUM_FOOTER_SIZE)
  886. continue;
  887. f2fs_put_page(page, 1);
  888. page = NULL;
  889. page = get_meta_page(sbi, start++);
  890. kaddr = (unsigned char *)page_address(page);
  891. offset = 0;
  892. }
  893. }
  894. f2fs_put_page(page, 1);
  895. return 0;
  896. }
  897. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  898. {
  899. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  900. struct f2fs_summary_block *sum;
  901. struct curseg_info *curseg;
  902. struct page *new;
  903. unsigned short blk_off;
  904. unsigned int segno = 0;
  905. block_t blk_addr = 0;
  906. /* get segment number and block addr */
  907. if (IS_DATASEG(type)) {
  908. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  909. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  910. CURSEG_HOT_DATA]);
  911. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  912. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  913. else
  914. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  915. } else {
  916. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  917. CURSEG_HOT_NODE]);
  918. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  919. CURSEG_HOT_NODE]);
  920. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  921. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  922. type - CURSEG_HOT_NODE);
  923. else
  924. blk_addr = GET_SUM_BLOCK(sbi, segno);
  925. }
  926. new = get_meta_page(sbi, blk_addr);
  927. sum = (struct f2fs_summary_block *)page_address(new);
  928. if (IS_NODESEG(type)) {
  929. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
  930. struct f2fs_summary *ns = &sum->entries[0];
  931. int i;
  932. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  933. ns->version = 0;
  934. ns->ofs_in_node = 0;
  935. }
  936. } else {
  937. if (restore_node_summary(sbi, segno, sum)) {
  938. f2fs_put_page(new, 1);
  939. return -EINVAL;
  940. }
  941. }
  942. }
  943. /* set uncompleted segment to curseg */
  944. curseg = CURSEG_I(sbi, type);
  945. mutex_lock(&curseg->curseg_mutex);
  946. memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
  947. curseg->next_segno = segno;
  948. reset_curseg(sbi, type, 0);
  949. curseg->alloc_type = ckpt->alloc_type[type];
  950. curseg->next_blkoff = blk_off;
  951. mutex_unlock(&curseg->curseg_mutex);
  952. f2fs_put_page(new, 1);
  953. return 0;
  954. }
  955. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  956. {
  957. int type = CURSEG_HOT_DATA;
  958. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
  959. /* restore for compacted data summary */
  960. if (read_compacted_summaries(sbi))
  961. return -EINVAL;
  962. type = CURSEG_HOT_NODE;
  963. }
  964. for (; type <= CURSEG_COLD_NODE; type++)
  965. if (read_normal_summaries(sbi, type))
  966. return -EINVAL;
  967. return 0;
  968. }
  969. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  970. {
  971. struct page *page;
  972. unsigned char *kaddr;
  973. struct f2fs_summary *summary;
  974. struct curseg_info *seg_i;
  975. int written_size = 0;
  976. int i, j;
  977. page = grab_meta_page(sbi, blkaddr++);
  978. kaddr = (unsigned char *)page_address(page);
  979. /* Step 1: write nat cache */
  980. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  981. memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
  982. written_size += SUM_JOURNAL_SIZE;
  983. /* Step 2: write sit cache */
  984. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  985. memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
  986. SUM_JOURNAL_SIZE);
  987. written_size += SUM_JOURNAL_SIZE;
  988. set_page_dirty(page);
  989. /* Step 3: write summary entries */
  990. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  991. unsigned short blkoff;
  992. seg_i = CURSEG_I(sbi, i);
  993. if (sbi->ckpt->alloc_type[i] == SSR)
  994. blkoff = sbi->blocks_per_seg;
  995. else
  996. blkoff = curseg_blkoff(sbi, i);
  997. for (j = 0; j < blkoff; j++) {
  998. if (!page) {
  999. page = grab_meta_page(sbi, blkaddr++);
  1000. kaddr = (unsigned char *)page_address(page);
  1001. written_size = 0;
  1002. }
  1003. summary = (struct f2fs_summary *)(kaddr + written_size);
  1004. *summary = seg_i->sum_blk->entries[j];
  1005. written_size += SUMMARY_SIZE;
  1006. set_page_dirty(page);
  1007. if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1008. SUM_FOOTER_SIZE)
  1009. continue;
  1010. f2fs_put_page(page, 1);
  1011. page = NULL;
  1012. }
  1013. }
  1014. if (page)
  1015. f2fs_put_page(page, 1);
  1016. }
  1017. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  1018. block_t blkaddr, int type)
  1019. {
  1020. int i, end;
  1021. if (IS_DATASEG(type))
  1022. end = type + NR_CURSEG_DATA_TYPE;
  1023. else
  1024. end = type + NR_CURSEG_NODE_TYPE;
  1025. for (i = type; i < end; i++) {
  1026. struct curseg_info *sum = CURSEG_I(sbi, i);
  1027. mutex_lock(&sum->curseg_mutex);
  1028. write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
  1029. mutex_unlock(&sum->curseg_mutex);
  1030. }
  1031. }
  1032. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1033. {
  1034. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
  1035. write_compacted_summaries(sbi, start_blk);
  1036. else
  1037. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1038. }
  1039. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1040. {
  1041. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
  1042. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1043. return;
  1044. }
  1045. int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
  1046. unsigned int val, int alloc)
  1047. {
  1048. int i;
  1049. if (type == NAT_JOURNAL) {
  1050. for (i = 0; i < nats_in_cursum(sum); i++) {
  1051. if (le32_to_cpu(nid_in_journal(sum, i)) == val)
  1052. return i;
  1053. }
  1054. if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
  1055. return update_nats_in_cursum(sum, 1);
  1056. } else if (type == SIT_JOURNAL) {
  1057. for (i = 0; i < sits_in_cursum(sum); i++)
  1058. if (le32_to_cpu(segno_in_journal(sum, i)) == val)
  1059. return i;
  1060. if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
  1061. return update_sits_in_cursum(sum, 1);
  1062. }
  1063. return -1;
  1064. }
  1065. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1066. unsigned int segno)
  1067. {
  1068. struct sit_info *sit_i = SIT_I(sbi);
  1069. unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
  1070. block_t blk_addr = sit_i->sit_base_addr + offset;
  1071. check_seg_range(sbi, segno);
  1072. /* calculate sit block address */
  1073. if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  1074. blk_addr += sit_i->sit_blocks;
  1075. return get_meta_page(sbi, blk_addr);
  1076. }
  1077. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1078. unsigned int start)
  1079. {
  1080. struct sit_info *sit_i = SIT_I(sbi);
  1081. struct page *src_page, *dst_page;
  1082. pgoff_t src_off, dst_off;
  1083. void *src_addr, *dst_addr;
  1084. src_off = current_sit_addr(sbi, start);
  1085. dst_off = next_sit_addr(sbi, src_off);
  1086. /* get current sit block page without lock */
  1087. src_page = get_meta_page(sbi, src_off);
  1088. dst_page = grab_meta_page(sbi, dst_off);
  1089. BUG_ON(PageDirty(src_page));
  1090. src_addr = page_address(src_page);
  1091. dst_addr = page_address(dst_page);
  1092. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  1093. set_page_dirty(dst_page);
  1094. f2fs_put_page(src_page, 1);
  1095. set_to_next_sit(sit_i, start);
  1096. return dst_page;
  1097. }
  1098. static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
  1099. {
  1100. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1101. struct f2fs_summary_block *sum = curseg->sum_blk;
  1102. int i;
  1103. /*
  1104. * If the journal area in the current summary is full of sit entries,
  1105. * all the sit entries will be flushed. Otherwise the sit entries
  1106. * are not able to replace with newly hot sit entries.
  1107. */
  1108. if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
  1109. for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
  1110. unsigned int segno;
  1111. segno = le32_to_cpu(segno_in_journal(sum, i));
  1112. __mark_sit_entry_dirty(sbi, segno);
  1113. }
  1114. update_sits_in_cursum(sum, -sits_in_cursum(sum));
  1115. return 1;
  1116. }
  1117. return 0;
  1118. }
  1119. /*
  1120. * CP calls this function, which flushes SIT entries including sit_journal,
  1121. * and moves prefree segs to free segs.
  1122. */
  1123. void flush_sit_entries(struct f2fs_sb_info *sbi)
  1124. {
  1125. struct sit_info *sit_i = SIT_I(sbi);
  1126. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1127. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1128. struct f2fs_summary_block *sum = curseg->sum_blk;
  1129. unsigned long nsegs = TOTAL_SEGS(sbi);
  1130. struct page *page = NULL;
  1131. struct f2fs_sit_block *raw_sit = NULL;
  1132. unsigned int start = 0, end = 0;
  1133. unsigned int segno = -1;
  1134. bool flushed;
  1135. mutex_lock(&curseg->curseg_mutex);
  1136. mutex_lock(&sit_i->sentry_lock);
  1137. /*
  1138. * "flushed" indicates whether sit entries in journal are flushed
  1139. * to the SIT area or not.
  1140. */
  1141. flushed = flush_sits_in_journal(sbi);
  1142. while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
  1143. struct seg_entry *se = get_seg_entry(sbi, segno);
  1144. int sit_offset, offset;
  1145. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1146. if (flushed)
  1147. goto to_sit_page;
  1148. offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
  1149. if (offset >= 0) {
  1150. segno_in_journal(sum, offset) = cpu_to_le32(segno);
  1151. seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
  1152. goto flush_done;
  1153. }
  1154. to_sit_page:
  1155. if (!page || (start > segno) || (segno > end)) {
  1156. if (page) {
  1157. f2fs_put_page(page, 1);
  1158. page = NULL;
  1159. }
  1160. start = START_SEGNO(sit_i, segno);
  1161. end = start + SIT_ENTRY_PER_BLOCK - 1;
  1162. /* read sit block that will be updated */
  1163. page = get_next_sit_page(sbi, start);
  1164. raw_sit = page_address(page);
  1165. }
  1166. /* udpate entry in SIT block */
  1167. seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
  1168. flush_done:
  1169. __clear_bit(segno, bitmap);
  1170. sit_i->dirty_sentries--;
  1171. }
  1172. mutex_unlock(&sit_i->sentry_lock);
  1173. mutex_unlock(&curseg->curseg_mutex);
  1174. /* writeout last modified SIT block */
  1175. f2fs_put_page(page, 1);
  1176. set_prefree_as_free_segments(sbi);
  1177. }
  1178. static int build_sit_info(struct f2fs_sb_info *sbi)
  1179. {
  1180. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1181. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1182. struct sit_info *sit_i;
  1183. unsigned int sit_segs, start;
  1184. char *src_bitmap, *dst_bitmap;
  1185. unsigned int bitmap_size;
  1186. /* allocate memory for SIT information */
  1187. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1188. if (!sit_i)
  1189. return -ENOMEM;
  1190. SM_I(sbi)->sit_info = sit_i;
  1191. sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
  1192. if (!sit_i->sentries)
  1193. return -ENOMEM;
  1194. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1195. sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1196. if (!sit_i->dirty_sentries_bitmap)
  1197. return -ENOMEM;
  1198. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1199. sit_i->sentries[start].cur_valid_map
  1200. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1201. sit_i->sentries[start].ckpt_valid_map
  1202. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1203. if (!sit_i->sentries[start].cur_valid_map
  1204. || !sit_i->sentries[start].ckpt_valid_map)
  1205. return -ENOMEM;
  1206. }
  1207. if (sbi->segs_per_sec > 1) {
  1208. sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
  1209. sizeof(struct sec_entry));
  1210. if (!sit_i->sec_entries)
  1211. return -ENOMEM;
  1212. }
  1213. /* get information related with SIT */
  1214. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1215. /* setup SIT bitmap from ckeckpoint pack */
  1216. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1217. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1218. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1219. if (!dst_bitmap)
  1220. return -ENOMEM;
  1221. /* init SIT information */
  1222. sit_i->s_ops = &default_salloc_ops;
  1223. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1224. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1225. sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
  1226. sit_i->sit_bitmap = dst_bitmap;
  1227. sit_i->bitmap_size = bitmap_size;
  1228. sit_i->dirty_sentries = 0;
  1229. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1230. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1231. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1232. mutex_init(&sit_i->sentry_lock);
  1233. return 0;
  1234. }
  1235. static int build_free_segmap(struct f2fs_sb_info *sbi)
  1236. {
  1237. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1238. struct free_segmap_info *free_i;
  1239. unsigned int bitmap_size, sec_bitmap_size;
  1240. /* allocate memory for free segmap information */
  1241. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  1242. if (!free_i)
  1243. return -ENOMEM;
  1244. SM_I(sbi)->free_info = free_i;
  1245. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1246. free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
  1247. if (!free_i->free_segmap)
  1248. return -ENOMEM;
  1249. sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1250. free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
  1251. if (!free_i->free_secmap)
  1252. return -ENOMEM;
  1253. /* set all segments as dirty temporarily */
  1254. memset(free_i->free_segmap, 0xff, bitmap_size);
  1255. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  1256. /* init free segmap information */
  1257. free_i->start_segno =
  1258. (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
  1259. free_i->free_segments = 0;
  1260. free_i->free_sections = 0;
  1261. rwlock_init(&free_i->segmap_lock);
  1262. return 0;
  1263. }
  1264. static int build_curseg(struct f2fs_sb_info *sbi)
  1265. {
  1266. struct curseg_info *array;
  1267. int i;
  1268. array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
  1269. if (!array)
  1270. return -ENOMEM;
  1271. SM_I(sbi)->curseg_array = array;
  1272. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  1273. mutex_init(&array[i].curseg_mutex);
  1274. array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  1275. if (!array[i].sum_blk)
  1276. return -ENOMEM;
  1277. array[i].segno = NULL_SEGNO;
  1278. array[i].next_blkoff = 0;
  1279. }
  1280. return restore_curseg_summaries(sbi);
  1281. }
  1282. static void build_sit_entries(struct f2fs_sb_info *sbi)
  1283. {
  1284. struct sit_info *sit_i = SIT_I(sbi);
  1285. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1286. struct f2fs_summary_block *sum = curseg->sum_blk;
  1287. unsigned int start;
  1288. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1289. struct seg_entry *se = &sit_i->sentries[start];
  1290. struct f2fs_sit_block *sit_blk;
  1291. struct f2fs_sit_entry sit;
  1292. struct page *page;
  1293. int i;
  1294. mutex_lock(&curseg->curseg_mutex);
  1295. for (i = 0; i < sits_in_cursum(sum); i++) {
  1296. if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
  1297. sit = sit_in_journal(sum, i);
  1298. mutex_unlock(&curseg->curseg_mutex);
  1299. goto got_it;
  1300. }
  1301. }
  1302. mutex_unlock(&curseg->curseg_mutex);
  1303. page = get_current_sit_page(sbi, start);
  1304. sit_blk = (struct f2fs_sit_block *)page_address(page);
  1305. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  1306. f2fs_put_page(page, 1);
  1307. got_it:
  1308. check_block_count(sbi, start, &sit);
  1309. seg_info_from_raw_sit(se, &sit);
  1310. if (sbi->segs_per_sec > 1) {
  1311. struct sec_entry *e = get_sec_entry(sbi, start);
  1312. e->valid_blocks += se->valid_blocks;
  1313. }
  1314. }
  1315. }
  1316. static void init_free_segmap(struct f2fs_sb_info *sbi)
  1317. {
  1318. unsigned int start;
  1319. int type;
  1320. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1321. struct seg_entry *sentry = get_seg_entry(sbi, start);
  1322. if (!sentry->valid_blocks)
  1323. __set_free(sbi, start);
  1324. }
  1325. /* set use the current segments */
  1326. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  1327. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  1328. __set_test_and_inuse(sbi, curseg_t->segno);
  1329. }
  1330. }
  1331. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  1332. {
  1333. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1334. struct free_segmap_info *free_i = FREE_I(sbi);
  1335. unsigned int segno = 0, offset = 0;
  1336. unsigned short valid_blocks;
  1337. while (segno < TOTAL_SEGS(sbi)) {
  1338. /* find dirty segment based on free segmap */
  1339. segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
  1340. if (segno >= TOTAL_SEGS(sbi))
  1341. break;
  1342. offset = segno + 1;
  1343. valid_blocks = get_valid_blocks(sbi, segno, 0);
  1344. if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
  1345. continue;
  1346. mutex_lock(&dirty_i->seglist_lock);
  1347. __locate_dirty_segment(sbi, segno, DIRTY);
  1348. mutex_unlock(&dirty_i->seglist_lock);
  1349. }
  1350. }
  1351. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  1352. {
  1353. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1354. unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1355. dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
  1356. if (!dirty_i->victim_secmap)
  1357. return -ENOMEM;
  1358. return 0;
  1359. }
  1360. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  1361. {
  1362. struct dirty_seglist_info *dirty_i;
  1363. unsigned int bitmap_size, i;
  1364. /* allocate memory for dirty segments list information */
  1365. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  1366. if (!dirty_i)
  1367. return -ENOMEM;
  1368. SM_I(sbi)->dirty_info = dirty_i;
  1369. mutex_init(&dirty_i->seglist_lock);
  1370. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1371. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  1372. dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
  1373. if (!dirty_i->dirty_segmap[i])
  1374. return -ENOMEM;
  1375. }
  1376. init_dirty_segmap(sbi);
  1377. return init_victim_secmap(sbi);
  1378. }
  1379. /*
  1380. * Update min, max modified time for cost-benefit GC algorithm
  1381. */
  1382. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  1383. {
  1384. struct sit_info *sit_i = SIT_I(sbi);
  1385. unsigned int segno;
  1386. mutex_lock(&sit_i->sentry_lock);
  1387. sit_i->min_mtime = LLONG_MAX;
  1388. for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
  1389. unsigned int i;
  1390. unsigned long long mtime = 0;
  1391. for (i = 0; i < sbi->segs_per_sec; i++)
  1392. mtime += get_seg_entry(sbi, segno + i)->mtime;
  1393. mtime = div_u64(mtime, sbi->segs_per_sec);
  1394. if (sit_i->min_mtime > mtime)
  1395. sit_i->min_mtime = mtime;
  1396. }
  1397. sit_i->max_mtime = get_mtime(sbi);
  1398. mutex_unlock(&sit_i->sentry_lock);
  1399. }
  1400. int build_segment_manager(struct f2fs_sb_info *sbi)
  1401. {
  1402. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1403. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1404. struct f2fs_sm_info *sm_info;
  1405. int err;
  1406. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  1407. if (!sm_info)
  1408. return -ENOMEM;
  1409. /* init sm info */
  1410. sbi->sm_info = sm_info;
  1411. INIT_LIST_HEAD(&sm_info->wblist_head);
  1412. spin_lock_init(&sm_info->wblist_lock);
  1413. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1414. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1415. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  1416. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1417. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1418. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  1419. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1420. err = build_sit_info(sbi);
  1421. if (err)
  1422. return err;
  1423. err = build_free_segmap(sbi);
  1424. if (err)
  1425. return err;
  1426. err = build_curseg(sbi);
  1427. if (err)
  1428. return err;
  1429. /* reinit free segmap based on SIT */
  1430. build_sit_entries(sbi);
  1431. init_free_segmap(sbi);
  1432. err = build_dirty_segmap(sbi);
  1433. if (err)
  1434. return err;
  1435. init_min_max_mtime(sbi);
  1436. return 0;
  1437. }
  1438. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  1439. enum dirty_type dirty_type)
  1440. {
  1441. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1442. mutex_lock(&dirty_i->seglist_lock);
  1443. kfree(dirty_i->dirty_segmap[dirty_type]);
  1444. dirty_i->nr_dirty[dirty_type] = 0;
  1445. mutex_unlock(&dirty_i->seglist_lock);
  1446. }
  1447. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  1448. {
  1449. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1450. kfree(dirty_i->victim_secmap);
  1451. }
  1452. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  1453. {
  1454. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1455. int i;
  1456. if (!dirty_i)
  1457. return;
  1458. /* discard pre-free/dirty segments list */
  1459. for (i = 0; i < NR_DIRTY_TYPE; i++)
  1460. discard_dirty_segmap(sbi, i);
  1461. destroy_victim_secmap(sbi);
  1462. SM_I(sbi)->dirty_info = NULL;
  1463. kfree(dirty_i);
  1464. }
  1465. static void destroy_curseg(struct f2fs_sb_info *sbi)
  1466. {
  1467. struct curseg_info *array = SM_I(sbi)->curseg_array;
  1468. int i;
  1469. if (!array)
  1470. return;
  1471. SM_I(sbi)->curseg_array = NULL;
  1472. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1473. kfree(array[i].sum_blk);
  1474. kfree(array);
  1475. }
  1476. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  1477. {
  1478. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  1479. if (!free_i)
  1480. return;
  1481. SM_I(sbi)->free_info = NULL;
  1482. kfree(free_i->free_segmap);
  1483. kfree(free_i->free_secmap);
  1484. kfree(free_i);
  1485. }
  1486. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  1487. {
  1488. struct sit_info *sit_i = SIT_I(sbi);
  1489. unsigned int start;
  1490. if (!sit_i)
  1491. return;
  1492. if (sit_i->sentries) {
  1493. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1494. kfree(sit_i->sentries[start].cur_valid_map);
  1495. kfree(sit_i->sentries[start].ckpt_valid_map);
  1496. }
  1497. }
  1498. vfree(sit_i->sentries);
  1499. vfree(sit_i->sec_entries);
  1500. kfree(sit_i->dirty_sentries_bitmap);
  1501. SM_I(sbi)->sit_info = NULL;
  1502. kfree(sit_i->sit_bitmap);
  1503. kfree(sit_i);
  1504. }
  1505. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  1506. {
  1507. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1508. destroy_dirty_segmap(sbi);
  1509. destroy_curseg(sbi);
  1510. destroy_free_segmap(sbi);
  1511. destroy_sit_info(sbi);
  1512. sbi->sm_info = NULL;
  1513. kfree(sm_info);
  1514. }