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/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, 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. }