segment.c 46 KB

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