segment.c 50 KB

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