segment.c 50 KB

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