segment.c 47 KB

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