segment.c 46 KB

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