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/vmalloc.h>
  16. #include "f2fs.h"
  17. #include "segment.h"
  18. #include "node.h"
  19. static int need_to_flush(struct f2fs_sb_info *sbi)
  20. {
  21. unsigned int pages_per_sec = (1 << sbi->log_blocks_per_seg) *
  22. sbi->segs_per_sec;
  23. int node_secs = ((get_pages(sbi, F2FS_DIRTY_NODES) + pages_per_sec - 1)
  24. >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
  25. int dent_secs = ((get_pages(sbi, F2FS_DIRTY_DENTS) + pages_per_sec - 1)
  26. >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
  27. if (sbi->por_doing)
  28. return 0;
  29. if (free_sections(sbi) <= (node_secs + 2 * dent_secs +
  30. reserved_sections(sbi)))
  31. return 1;
  32. return 0;
  33. }
  34. /*
  35. * This function balances dirty node and dentry pages.
  36. * In addition, it controls garbage collection.
  37. */
  38. void f2fs_balance_fs(struct f2fs_sb_info *sbi)
  39. {
  40. struct writeback_control wbc = {
  41. .sync_mode = WB_SYNC_ALL,
  42. .nr_to_write = LONG_MAX,
  43. .for_reclaim = 0,
  44. };
  45. if (sbi->por_doing)
  46. return;
  47. /*
  48. * We should do checkpoint when there are so many dirty node pages
  49. * with enough free segments. After then, we should do GC.
  50. */
  51. if (need_to_flush(sbi)) {
  52. sync_dirty_dir_inodes(sbi);
  53. sync_node_pages(sbi, 0, &wbc);
  54. }
  55. if (has_not_enough_free_secs(sbi)) {
  56. mutex_lock(&sbi->gc_mutex);
  57. f2fs_gc(sbi, 1);
  58. }
  59. }
  60. static void __locate_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. /* need not be added */
  65. if (IS_CURSEG(sbi, segno))
  66. return;
  67. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  68. dirty_i->nr_dirty[dirty_type]++;
  69. if (dirty_type == DIRTY) {
  70. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  71. dirty_type = sentry->type;
  72. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  73. dirty_i->nr_dirty[dirty_type]++;
  74. }
  75. }
  76. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  77. enum dirty_type dirty_type)
  78. {
  79. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  80. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  81. dirty_i->nr_dirty[dirty_type]--;
  82. if (dirty_type == DIRTY) {
  83. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  84. dirty_type = sentry->type;
  85. if (test_and_clear_bit(segno,
  86. dirty_i->dirty_segmap[dirty_type]))
  87. dirty_i->nr_dirty[dirty_type]--;
  88. clear_bit(segno, dirty_i->victim_segmap[FG_GC]);
  89. clear_bit(segno, dirty_i->victim_segmap[BG_GC]);
  90. }
  91. }
  92. /*
  93. * Should not occur error such as -ENOMEM.
  94. * Adding dirty entry into seglist is not critical operation.
  95. * If a given segment is one of current working segments, it won't be added.
  96. */
  97. void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  98. {
  99. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  100. unsigned short valid_blocks;
  101. if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
  102. return;
  103. mutex_lock(&dirty_i->seglist_lock);
  104. valid_blocks = get_valid_blocks(sbi, segno, 0);
  105. if (valid_blocks == 0) {
  106. __locate_dirty_segment(sbi, segno, PRE);
  107. __remove_dirty_segment(sbi, segno, DIRTY);
  108. } else if (valid_blocks < sbi->blocks_per_seg) {
  109. __locate_dirty_segment(sbi, segno, DIRTY);
  110. } else {
  111. /* Recovery routine with SSR needs this */
  112. __remove_dirty_segment(sbi, segno, DIRTY);
  113. }
  114. mutex_unlock(&dirty_i->seglist_lock);
  115. return;
  116. }
  117. /*
  118. * Should call clear_prefree_segments after checkpoint is done.
  119. */
  120. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  121. {
  122. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  123. unsigned int segno, offset = 0;
  124. unsigned int total_segs = TOTAL_SEGS(sbi);
  125. mutex_lock(&dirty_i->seglist_lock);
  126. while (1) {
  127. segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
  128. offset);
  129. if (segno >= total_segs)
  130. break;
  131. __set_test_and_free(sbi, segno);
  132. offset = segno + 1;
  133. }
  134. mutex_unlock(&dirty_i->seglist_lock);
  135. }
  136. void clear_prefree_segments(struct f2fs_sb_info *sbi)
  137. {
  138. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  139. unsigned int segno, offset = 0;
  140. unsigned int total_segs = TOTAL_SEGS(sbi);
  141. mutex_lock(&dirty_i->seglist_lock);
  142. while (1) {
  143. segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
  144. offset);
  145. if (segno >= total_segs)
  146. break;
  147. offset = segno + 1;
  148. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
  149. dirty_i->nr_dirty[PRE]--;
  150. /* Let's use trim */
  151. if (test_opt(sbi, DISCARD))
  152. blkdev_issue_discard(sbi->sb->s_bdev,
  153. START_BLOCK(sbi, segno) <<
  154. sbi->log_sectors_per_block,
  155. 1 << (sbi->log_sectors_per_block +
  156. sbi->log_blocks_per_seg),
  157. GFP_NOFS, 0);
  158. }
  159. mutex_unlock(&dirty_i->seglist_lock);
  160. }
  161. static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  162. {
  163. struct sit_info *sit_i = SIT_I(sbi);
  164. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
  165. sit_i->dirty_sentries++;
  166. }
  167. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  168. unsigned int segno, int modified)
  169. {
  170. struct seg_entry *se = get_seg_entry(sbi, segno);
  171. se->type = type;
  172. if (modified)
  173. __mark_sit_entry_dirty(sbi, segno);
  174. }
  175. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  176. {
  177. struct seg_entry *se;
  178. unsigned int segno, offset;
  179. long int new_vblocks;
  180. segno = GET_SEGNO(sbi, blkaddr);
  181. se = get_seg_entry(sbi, segno);
  182. new_vblocks = se->valid_blocks + del;
  183. offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
  184. BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
  185. (new_vblocks > sbi->blocks_per_seg)));
  186. se->valid_blocks = new_vblocks;
  187. se->mtime = get_mtime(sbi);
  188. SIT_I(sbi)->max_mtime = se->mtime;
  189. /* Update valid block bitmap */
  190. if (del > 0) {
  191. if (f2fs_set_bit(offset, se->cur_valid_map))
  192. BUG();
  193. } else {
  194. if (!f2fs_clear_bit(offset, se->cur_valid_map))
  195. BUG();
  196. }
  197. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  198. se->ckpt_valid_blocks += del;
  199. __mark_sit_entry_dirty(sbi, segno);
  200. /* update total number of valid blocks to be written in ckpt area */
  201. SIT_I(sbi)->written_valid_blocks += del;
  202. if (sbi->segs_per_sec > 1)
  203. get_sec_entry(sbi, segno)->valid_blocks += del;
  204. }
  205. static void refresh_sit_entry(struct f2fs_sb_info *sbi,
  206. block_t old_blkaddr, block_t new_blkaddr)
  207. {
  208. update_sit_entry(sbi, new_blkaddr, 1);
  209. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  210. update_sit_entry(sbi, old_blkaddr, -1);
  211. }
  212. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  213. {
  214. unsigned int segno = GET_SEGNO(sbi, addr);
  215. struct sit_info *sit_i = SIT_I(sbi);
  216. BUG_ON(addr == NULL_ADDR);
  217. if (addr == NEW_ADDR)
  218. return;
  219. /* add it into sit main buffer */
  220. mutex_lock(&sit_i->sentry_lock);
  221. update_sit_entry(sbi, addr, -1);
  222. /* add it into dirty seglist */
  223. locate_dirty_segment(sbi, segno);
  224. mutex_unlock(&sit_i->sentry_lock);
  225. }
  226. /*
  227. * This function should be resided under the curseg_mutex lock
  228. */
  229. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  230. struct f2fs_summary *sum, unsigned short offset)
  231. {
  232. struct curseg_info *curseg = CURSEG_I(sbi, type);
  233. void *addr = curseg->sum_blk;
  234. addr += offset * sizeof(struct f2fs_summary);
  235. memcpy(addr, sum, sizeof(struct f2fs_summary));
  236. return;
  237. }
  238. /*
  239. * Calculate the number of current summary pages for writing
  240. */
  241. int npages_for_summary_flush(struct f2fs_sb_info *sbi)
  242. {
  243. int total_size_bytes = 0;
  244. int valid_sum_count = 0;
  245. int i, sum_space;
  246. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  247. if (sbi->ckpt->alloc_type[i] == SSR)
  248. valid_sum_count += sbi->blocks_per_seg;
  249. else
  250. valid_sum_count += curseg_blkoff(sbi, i);
  251. }
  252. total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
  253. + sizeof(struct nat_journal) + 2
  254. + sizeof(struct sit_journal) + 2;
  255. sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
  256. if (total_size_bytes < sum_space)
  257. return 1;
  258. else if (total_size_bytes < 2 * sum_space)
  259. return 2;
  260. return 3;
  261. }
  262. /*
  263. * Caller should put this summary page
  264. */
  265. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  266. {
  267. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  268. }
  269. static void write_sum_page(struct f2fs_sb_info *sbi,
  270. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  271. {
  272. struct page *page = grab_meta_page(sbi, blk_addr);
  273. void *kaddr = page_address(page);
  274. memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
  275. set_page_dirty(page);
  276. f2fs_put_page(page, 1);
  277. }
  278. static unsigned int check_prefree_segments(struct f2fs_sb_info *sbi,
  279. int ofs_unit, int type)
  280. {
  281. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  282. unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
  283. unsigned int segno, next_segno, i;
  284. int ofs = 0;
  285. /*
  286. * If there is not enough reserved sections,
  287. * we should not reuse prefree segments.
  288. */
  289. if (has_not_enough_free_secs(sbi))
  290. return NULL_SEGNO;
  291. /*
  292. * NODE page should not reuse prefree segment,
  293. * since those information is used for SPOR.
  294. */
  295. if (IS_NODESEG(type))
  296. return NULL_SEGNO;
  297. next:
  298. segno = find_next_bit(prefree_segmap, TOTAL_SEGS(sbi), ofs++);
  299. ofs = ((segno / ofs_unit) * ofs_unit) + ofs_unit;
  300. if (segno < TOTAL_SEGS(sbi)) {
  301. /* skip intermediate segments in a section */
  302. if (segno % ofs_unit)
  303. goto next;
  304. /* skip if whole section is not prefree */
  305. next_segno = find_next_zero_bit(prefree_segmap,
  306. TOTAL_SEGS(sbi), segno + 1);
  307. if (next_segno - segno < ofs_unit)
  308. goto next;
  309. /* skip if whole section was not free at the last checkpoint */
  310. for (i = 0; i < ofs_unit; i++)
  311. if (get_seg_entry(sbi, segno)->ckpt_valid_blocks)
  312. goto next;
  313. return segno;
  314. }
  315. return NULL_SEGNO;
  316. }
  317. /*
  318. * Find a new segment from the free segments bitmap to right order
  319. * This function should be returned with success, otherwise BUG
  320. */
  321. static void get_new_segment(struct f2fs_sb_info *sbi,
  322. unsigned int *newseg, bool new_sec, int dir)
  323. {
  324. struct free_segmap_info *free_i = FREE_I(sbi);
  325. unsigned int total_secs = sbi->total_sections;
  326. unsigned int segno, secno, zoneno;
  327. unsigned int total_zones = sbi->total_sections / sbi->secs_per_zone;
  328. unsigned int hint = *newseg / sbi->segs_per_sec;
  329. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  330. unsigned int left_start = hint;
  331. bool init = true;
  332. int go_left = 0;
  333. int i;
  334. write_lock(&free_i->segmap_lock);
  335. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  336. segno = find_next_zero_bit(free_i->free_segmap,
  337. TOTAL_SEGS(sbi), *newseg + 1);
  338. if (segno < TOTAL_SEGS(sbi))
  339. goto got_it;
  340. }
  341. find_other_zone:
  342. secno = find_next_zero_bit(free_i->free_secmap, total_secs, hint);
  343. if (secno >= total_secs) {
  344. if (dir == ALLOC_RIGHT) {
  345. secno = find_next_zero_bit(free_i->free_secmap,
  346. total_secs, 0);
  347. BUG_ON(secno >= total_secs);
  348. } else {
  349. go_left = 1;
  350. left_start = hint - 1;
  351. }
  352. }
  353. if (go_left == 0)
  354. goto skip_left;
  355. while (test_bit(left_start, free_i->free_secmap)) {
  356. if (left_start > 0) {
  357. left_start--;
  358. continue;
  359. }
  360. left_start = find_next_zero_bit(free_i->free_secmap,
  361. total_secs, 0);
  362. BUG_ON(left_start >= total_secs);
  363. break;
  364. }
  365. secno = left_start;
  366. skip_left:
  367. hint = secno;
  368. segno = secno * sbi->segs_per_sec;
  369. zoneno = secno / sbi->secs_per_zone;
  370. /* give up on finding another zone */
  371. if (!init)
  372. goto got_it;
  373. if (sbi->secs_per_zone == 1)
  374. goto got_it;
  375. if (zoneno == old_zoneno)
  376. goto got_it;
  377. if (dir == ALLOC_LEFT) {
  378. if (!go_left && zoneno + 1 >= total_zones)
  379. goto got_it;
  380. if (go_left && zoneno == 0)
  381. goto got_it;
  382. }
  383. for (i = 0; i < NR_CURSEG_TYPE; i++)
  384. if (CURSEG_I(sbi, i)->zone == zoneno)
  385. break;
  386. if (i < NR_CURSEG_TYPE) {
  387. /* zone is in user, try another */
  388. if (go_left)
  389. hint = zoneno * sbi->secs_per_zone - 1;
  390. else if (zoneno + 1 >= total_zones)
  391. hint = 0;
  392. else
  393. hint = (zoneno + 1) * sbi->secs_per_zone;
  394. init = false;
  395. goto find_other_zone;
  396. }
  397. got_it:
  398. /* set it as dirty segment in free segmap */
  399. BUG_ON(test_bit(segno, free_i->free_segmap));
  400. __set_inuse(sbi, segno);
  401. *newseg = segno;
  402. write_unlock(&free_i->segmap_lock);
  403. }
  404. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  405. {
  406. struct curseg_info *curseg = CURSEG_I(sbi, type);
  407. struct summary_footer *sum_footer;
  408. curseg->segno = curseg->next_segno;
  409. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  410. curseg->next_blkoff = 0;
  411. curseg->next_segno = NULL_SEGNO;
  412. sum_footer = &(curseg->sum_blk->footer);
  413. memset(sum_footer, 0, sizeof(struct summary_footer));
  414. if (IS_DATASEG(type))
  415. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  416. if (IS_NODESEG(type))
  417. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  418. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  419. }
  420. /*
  421. * Allocate a current working segment.
  422. * This function always allocates a free segment in LFS manner.
  423. */
  424. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  425. {
  426. struct curseg_info *curseg = CURSEG_I(sbi, type);
  427. unsigned int segno = curseg->segno;
  428. int dir = ALLOC_LEFT;
  429. write_sum_page(sbi, curseg->sum_blk,
  430. GET_SUM_BLOCK(sbi, curseg->segno));
  431. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  432. dir = ALLOC_RIGHT;
  433. if (test_opt(sbi, NOHEAP))
  434. dir = ALLOC_RIGHT;
  435. get_new_segment(sbi, &segno, new_sec, dir);
  436. curseg->next_segno = segno;
  437. reset_curseg(sbi, type, 1);
  438. curseg->alloc_type = LFS;
  439. }
  440. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  441. struct curseg_info *seg, block_t start)
  442. {
  443. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  444. block_t ofs;
  445. for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
  446. if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
  447. && !f2fs_test_bit(ofs, se->cur_valid_map))
  448. break;
  449. }
  450. seg->next_blkoff = ofs;
  451. }
  452. /*
  453. * If a segment is written by LFS manner, next block offset is just obtained
  454. * by increasing the current block offset. However, if a segment is written by
  455. * SSR manner, next block offset obtained by calling __next_free_blkoff
  456. */
  457. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  458. struct curseg_info *seg)
  459. {
  460. if (seg->alloc_type == SSR)
  461. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  462. else
  463. seg->next_blkoff++;
  464. }
  465. /*
  466. * This function always allocates a used segment (from dirty seglist) by SSR
  467. * manner, so it should recover the existing segment information of valid blocks
  468. */
  469. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  470. {
  471. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  472. struct curseg_info *curseg = CURSEG_I(sbi, type);
  473. unsigned int new_segno = curseg->next_segno;
  474. struct f2fs_summary_block *sum_node;
  475. struct page *sum_page;
  476. write_sum_page(sbi, curseg->sum_blk,
  477. GET_SUM_BLOCK(sbi, curseg->segno));
  478. __set_test_and_inuse(sbi, new_segno);
  479. mutex_lock(&dirty_i->seglist_lock);
  480. __remove_dirty_segment(sbi, new_segno, PRE);
  481. __remove_dirty_segment(sbi, new_segno, DIRTY);
  482. mutex_unlock(&dirty_i->seglist_lock);
  483. reset_curseg(sbi, type, 1);
  484. curseg->alloc_type = SSR;
  485. __next_free_blkoff(sbi, curseg, 0);
  486. if (reuse) {
  487. sum_page = get_sum_page(sbi, new_segno);
  488. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  489. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  490. f2fs_put_page(sum_page, 1);
  491. }
  492. }
  493. /*
  494. * flush out current segment and replace it with new segment
  495. * This function should be returned with success, otherwise BUG
  496. */
  497. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  498. int type, bool force)
  499. {
  500. struct curseg_info *curseg = CURSEG_I(sbi, type);
  501. unsigned int ofs_unit;
  502. if (force) {
  503. new_curseg(sbi, type, true);
  504. goto out;
  505. }
  506. ofs_unit = need_SSR(sbi) ? 1 : sbi->segs_per_sec;
  507. curseg->next_segno = check_prefree_segments(sbi, ofs_unit, type);
  508. if (curseg->next_segno != NULL_SEGNO)
  509. change_curseg(sbi, type, false);
  510. else if (type == CURSEG_WARM_NODE)
  511. new_curseg(sbi, type, false);
  512. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  513. change_curseg(sbi, type, true);
  514. else
  515. new_curseg(sbi, type, false);
  516. out:
  517. sbi->segment_count[curseg->alloc_type]++;
  518. }
  519. void allocate_new_segments(struct f2fs_sb_info *sbi)
  520. {
  521. struct curseg_info *curseg;
  522. unsigned int old_curseg;
  523. int i;
  524. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  525. curseg = CURSEG_I(sbi, i);
  526. old_curseg = curseg->segno;
  527. SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
  528. locate_dirty_segment(sbi, old_curseg);
  529. }
  530. }
  531. static const struct segment_allocation default_salloc_ops = {
  532. .allocate_segment = allocate_segment_by_default,
  533. };
  534. static void f2fs_end_io_write(struct bio *bio, int err)
  535. {
  536. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  537. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  538. struct bio_private *p = bio->bi_private;
  539. do {
  540. struct page *page = bvec->bv_page;
  541. if (--bvec >= bio->bi_io_vec)
  542. prefetchw(&bvec->bv_page->flags);
  543. if (!uptodate) {
  544. SetPageError(page);
  545. if (page->mapping)
  546. set_bit(AS_EIO, &page->mapping->flags);
  547. set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
  548. }
  549. end_page_writeback(page);
  550. dec_page_count(p->sbi, F2FS_WRITEBACK);
  551. } while (bvec >= bio->bi_io_vec);
  552. if (p->is_sync)
  553. complete(p->wait);
  554. kfree(p);
  555. bio_put(bio);
  556. }
  557. struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
  558. {
  559. struct bio *bio;
  560. struct bio_private *priv;
  561. retry:
  562. priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
  563. if (!priv) {
  564. cond_resched();
  565. goto retry;
  566. }
  567. /* No failure on bio allocation */
  568. bio = bio_alloc(GFP_NOIO, npages);
  569. bio->bi_bdev = bdev;
  570. bio->bi_private = priv;
  571. return bio;
  572. }
  573. static void do_submit_bio(struct f2fs_sb_info *sbi,
  574. enum page_type type, bool sync)
  575. {
  576. int rw = sync ? WRITE_SYNC : WRITE;
  577. enum page_type btype = type > META ? META : type;
  578. if (type >= META_FLUSH)
  579. rw = WRITE_FLUSH_FUA;
  580. if (sbi->bio[btype]) {
  581. struct bio_private *p = sbi->bio[btype]->bi_private;
  582. p->sbi = sbi;
  583. sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
  584. if (type == META_FLUSH) {
  585. DECLARE_COMPLETION_ONSTACK(wait);
  586. p->is_sync = true;
  587. p->wait = &wait;
  588. submit_bio(rw, sbi->bio[btype]);
  589. wait_for_completion(&wait);
  590. } else {
  591. p->is_sync = false;
  592. submit_bio(rw, sbi->bio[btype]);
  593. }
  594. sbi->bio[btype] = NULL;
  595. }
  596. }
  597. void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
  598. {
  599. down_write(&sbi->bio_sem);
  600. do_submit_bio(sbi, type, sync);
  601. up_write(&sbi->bio_sem);
  602. }
  603. static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
  604. block_t blk_addr, enum page_type type)
  605. {
  606. struct block_device *bdev = sbi->sb->s_bdev;
  607. verify_block_addr(sbi, blk_addr);
  608. down_write(&sbi->bio_sem);
  609. inc_page_count(sbi, F2FS_WRITEBACK);
  610. if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
  611. do_submit_bio(sbi, type, false);
  612. alloc_new:
  613. if (sbi->bio[type] == NULL) {
  614. sbi->bio[type] = f2fs_bio_alloc(bdev, bio_get_nr_vecs(bdev));
  615. sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
  616. /*
  617. * The end_io will be assigned at the sumbission phase.
  618. * Until then, let bio_add_page() merge consecutive IOs as much
  619. * as possible.
  620. */
  621. }
  622. if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
  623. PAGE_CACHE_SIZE) {
  624. do_submit_bio(sbi, type, false);
  625. goto alloc_new;
  626. }
  627. sbi->last_block_in_bio[type] = blk_addr;
  628. up_write(&sbi->bio_sem);
  629. }
  630. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  631. {
  632. struct curseg_info *curseg = CURSEG_I(sbi, type);
  633. if (curseg->next_blkoff < sbi->blocks_per_seg)
  634. return true;
  635. return false;
  636. }
  637. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  638. {
  639. if (p_type == DATA)
  640. return CURSEG_HOT_DATA;
  641. else
  642. return CURSEG_HOT_NODE;
  643. }
  644. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  645. {
  646. if (p_type == DATA) {
  647. struct inode *inode = page->mapping->host;
  648. if (S_ISDIR(inode->i_mode))
  649. return CURSEG_HOT_DATA;
  650. else
  651. return CURSEG_COLD_DATA;
  652. } else {
  653. if (IS_DNODE(page) && !is_cold_node(page))
  654. return CURSEG_HOT_NODE;
  655. else
  656. return CURSEG_COLD_NODE;
  657. }
  658. }
  659. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  660. {
  661. if (p_type == DATA) {
  662. struct inode *inode = page->mapping->host;
  663. if (S_ISDIR(inode->i_mode))
  664. return CURSEG_HOT_DATA;
  665. else if (is_cold_data(page) || is_cold_file(inode))
  666. return CURSEG_COLD_DATA;
  667. else
  668. return CURSEG_WARM_DATA;
  669. } else {
  670. if (IS_DNODE(page))
  671. return is_cold_node(page) ? CURSEG_WARM_NODE :
  672. CURSEG_HOT_NODE;
  673. else
  674. return CURSEG_COLD_NODE;
  675. }
  676. }
  677. static int __get_segment_type(struct page *page, enum page_type p_type)
  678. {
  679. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  680. switch (sbi->active_logs) {
  681. case 2:
  682. return __get_segment_type_2(page, p_type);
  683. case 4:
  684. return __get_segment_type_4(page, p_type);
  685. case 6:
  686. return __get_segment_type_6(page, p_type);
  687. default:
  688. BUG();
  689. }
  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. }