segment.c 46 KB

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