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