segment.c 47 KB

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