segment.c 47 KB

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