segment.c 46 KB

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