segment.c 49 KB

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