segment.c 45 KB

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