segment.c 46 KB

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