segment.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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_BLKOFF_FROM_SEG0(sbi, blkaddr);
  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. void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
  373. {
  374. update_sit_entry(sbi, new, 1);
  375. if (GET_SEGNO(sbi, old) != NULL_SEGNO)
  376. update_sit_entry(sbi, old, -1);
  377. locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
  378. locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
  379. }
  380. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  381. {
  382. unsigned int segno = GET_SEGNO(sbi, addr);
  383. struct sit_info *sit_i = SIT_I(sbi);
  384. f2fs_bug_on(addr == NULL_ADDR);
  385. if (addr == NEW_ADDR)
  386. return;
  387. /* add it into sit main buffer */
  388. mutex_lock(&sit_i->sentry_lock);
  389. update_sit_entry(sbi, addr, -1);
  390. /* add it into dirty seglist */
  391. locate_dirty_segment(sbi, segno);
  392. mutex_unlock(&sit_i->sentry_lock);
  393. }
  394. /*
  395. * This function should be resided under the curseg_mutex lock
  396. */
  397. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  398. struct f2fs_summary *sum)
  399. {
  400. struct curseg_info *curseg = CURSEG_I(sbi, type);
  401. void *addr = curseg->sum_blk;
  402. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  403. memcpy(addr, sum, sizeof(struct f2fs_summary));
  404. }
  405. /*
  406. * Calculate the number of current summary pages for writing
  407. */
  408. int npages_for_summary_flush(struct f2fs_sb_info *sbi)
  409. {
  410. int valid_sum_count = 0;
  411. int i, sum_in_page;
  412. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  413. if (sbi->ckpt->alloc_type[i] == SSR)
  414. valid_sum_count += sbi->blocks_per_seg;
  415. else
  416. valid_sum_count += curseg_blkoff(sbi, i);
  417. }
  418. sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
  419. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  420. if (valid_sum_count <= sum_in_page)
  421. return 1;
  422. else if ((valid_sum_count - sum_in_page) <=
  423. (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  424. return 2;
  425. return 3;
  426. }
  427. /*
  428. * Caller should put this summary page
  429. */
  430. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  431. {
  432. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  433. }
  434. static void write_sum_page(struct f2fs_sb_info *sbi,
  435. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  436. {
  437. struct page *page = grab_meta_page(sbi, blk_addr);
  438. void *kaddr = page_address(page);
  439. memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
  440. set_page_dirty(page);
  441. f2fs_put_page(page, 1);
  442. }
  443. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  444. {
  445. struct curseg_info *curseg = CURSEG_I(sbi, type);
  446. unsigned int segno = curseg->segno + 1;
  447. struct free_segmap_info *free_i = FREE_I(sbi);
  448. if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
  449. return !test_bit(segno, free_i->free_segmap);
  450. return 0;
  451. }
  452. /*
  453. * Find a new segment from the free segments bitmap to right order
  454. * This function should be returned with success, otherwise BUG
  455. */
  456. static void get_new_segment(struct f2fs_sb_info *sbi,
  457. unsigned int *newseg, bool new_sec, int dir)
  458. {
  459. struct free_segmap_info *free_i = FREE_I(sbi);
  460. unsigned int segno, secno, zoneno;
  461. unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
  462. unsigned int hint = *newseg / sbi->segs_per_sec;
  463. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  464. unsigned int left_start = hint;
  465. bool init = true;
  466. int go_left = 0;
  467. int i;
  468. write_lock(&free_i->segmap_lock);
  469. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  470. segno = find_next_zero_bit(free_i->free_segmap,
  471. TOTAL_SEGS(sbi), *newseg + 1);
  472. if (segno - *newseg < sbi->segs_per_sec -
  473. (*newseg % sbi->segs_per_sec))
  474. goto got_it;
  475. }
  476. find_other_zone:
  477. secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
  478. if (secno >= TOTAL_SECS(sbi)) {
  479. if (dir == ALLOC_RIGHT) {
  480. secno = find_next_zero_bit(free_i->free_secmap,
  481. TOTAL_SECS(sbi), 0);
  482. f2fs_bug_on(secno >= TOTAL_SECS(sbi));
  483. } else {
  484. go_left = 1;
  485. left_start = hint - 1;
  486. }
  487. }
  488. if (go_left == 0)
  489. goto skip_left;
  490. while (test_bit(left_start, free_i->free_secmap)) {
  491. if (left_start > 0) {
  492. left_start--;
  493. continue;
  494. }
  495. left_start = find_next_zero_bit(free_i->free_secmap,
  496. TOTAL_SECS(sbi), 0);
  497. f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
  498. break;
  499. }
  500. secno = left_start;
  501. skip_left:
  502. hint = secno;
  503. segno = secno * sbi->segs_per_sec;
  504. zoneno = secno / sbi->secs_per_zone;
  505. /* give up on finding another zone */
  506. if (!init)
  507. goto got_it;
  508. if (sbi->secs_per_zone == 1)
  509. goto got_it;
  510. if (zoneno == old_zoneno)
  511. goto got_it;
  512. if (dir == ALLOC_LEFT) {
  513. if (!go_left && zoneno + 1 >= total_zones)
  514. goto got_it;
  515. if (go_left && zoneno == 0)
  516. goto got_it;
  517. }
  518. for (i = 0; i < NR_CURSEG_TYPE; i++)
  519. if (CURSEG_I(sbi, i)->zone == zoneno)
  520. break;
  521. if (i < NR_CURSEG_TYPE) {
  522. /* zone is in user, try another */
  523. if (go_left)
  524. hint = zoneno * sbi->secs_per_zone - 1;
  525. else if (zoneno + 1 >= total_zones)
  526. hint = 0;
  527. else
  528. hint = (zoneno + 1) * sbi->secs_per_zone;
  529. init = false;
  530. goto find_other_zone;
  531. }
  532. got_it:
  533. /* set it as dirty segment in free segmap */
  534. f2fs_bug_on(test_bit(segno, free_i->free_segmap));
  535. __set_inuse(sbi, segno);
  536. *newseg = segno;
  537. write_unlock(&free_i->segmap_lock);
  538. }
  539. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  540. {
  541. struct curseg_info *curseg = CURSEG_I(sbi, type);
  542. struct summary_footer *sum_footer;
  543. curseg->segno = curseg->next_segno;
  544. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  545. curseg->next_blkoff = 0;
  546. curseg->next_segno = NULL_SEGNO;
  547. sum_footer = &(curseg->sum_blk->footer);
  548. memset(sum_footer, 0, sizeof(struct summary_footer));
  549. if (IS_DATASEG(type))
  550. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  551. if (IS_NODESEG(type))
  552. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  553. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  554. }
  555. /*
  556. * Allocate a current working segment.
  557. * This function always allocates a free segment in LFS manner.
  558. */
  559. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  560. {
  561. struct curseg_info *curseg = CURSEG_I(sbi, type);
  562. unsigned int segno = curseg->segno;
  563. int dir = ALLOC_LEFT;
  564. write_sum_page(sbi, curseg->sum_blk,
  565. GET_SUM_BLOCK(sbi, segno));
  566. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  567. dir = ALLOC_RIGHT;
  568. if (test_opt(sbi, NOHEAP))
  569. dir = ALLOC_RIGHT;
  570. get_new_segment(sbi, &segno, new_sec, dir);
  571. curseg->next_segno = segno;
  572. reset_curseg(sbi, type, 1);
  573. curseg->alloc_type = LFS;
  574. }
  575. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  576. struct curseg_info *seg, block_t start)
  577. {
  578. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  579. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  580. unsigned long target_map[entries];
  581. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  582. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  583. int i, pos;
  584. for (i = 0; i < entries; i++)
  585. target_map[i] = ckpt_map[i] | cur_map[i];
  586. pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
  587. seg->next_blkoff = pos;
  588. }
  589. /*
  590. * If a segment is written by LFS manner, next block offset is just obtained
  591. * by increasing the current block offset. However, if a segment is written by
  592. * SSR manner, next block offset obtained by calling __next_free_blkoff
  593. */
  594. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  595. struct curseg_info *seg)
  596. {
  597. if (seg->alloc_type == SSR)
  598. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  599. else
  600. seg->next_blkoff++;
  601. }
  602. /*
  603. * This function always allocates a used segment (from dirty seglist) by SSR
  604. * manner, so it should recover the existing segment information of valid blocks
  605. */
  606. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  607. {
  608. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  609. struct curseg_info *curseg = CURSEG_I(sbi, type);
  610. unsigned int new_segno = curseg->next_segno;
  611. struct f2fs_summary_block *sum_node;
  612. struct page *sum_page;
  613. write_sum_page(sbi, curseg->sum_blk,
  614. GET_SUM_BLOCK(sbi, curseg->segno));
  615. __set_test_and_inuse(sbi, new_segno);
  616. mutex_lock(&dirty_i->seglist_lock);
  617. __remove_dirty_segment(sbi, new_segno, PRE);
  618. __remove_dirty_segment(sbi, new_segno, DIRTY);
  619. mutex_unlock(&dirty_i->seglist_lock);
  620. reset_curseg(sbi, type, 1);
  621. curseg->alloc_type = SSR;
  622. __next_free_blkoff(sbi, curseg, 0);
  623. if (reuse) {
  624. sum_page = get_sum_page(sbi, new_segno);
  625. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  626. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  627. f2fs_put_page(sum_page, 1);
  628. }
  629. }
  630. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  631. {
  632. struct curseg_info *curseg = CURSEG_I(sbi, type);
  633. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  634. if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
  635. return v_ops->get_victim(sbi,
  636. &(curseg)->next_segno, BG_GC, type, SSR);
  637. /* For data segments, let's do SSR more intensively */
  638. for (; type >= CURSEG_HOT_DATA; type--)
  639. if (v_ops->get_victim(sbi, &(curseg)->next_segno,
  640. BG_GC, type, SSR))
  641. return 1;
  642. return 0;
  643. }
  644. /*
  645. * flush out current segment and replace it with new segment
  646. * This function should be returned with success, otherwise BUG
  647. */
  648. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  649. int type, bool force)
  650. {
  651. struct curseg_info *curseg = CURSEG_I(sbi, type);
  652. if (force)
  653. new_curseg(sbi, type, true);
  654. else if (type == CURSEG_WARM_NODE)
  655. new_curseg(sbi, type, false);
  656. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  657. new_curseg(sbi, type, false);
  658. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  659. change_curseg(sbi, type, true);
  660. else
  661. new_curseg(sbi, type, false);
  662. stat_inc_seg_type(sbi, curseg);
  663. }
  664. void allocate_new_segments(struct f2fs_sb_info *sbi)
  665. {
  666. struct curseg_info *curseg;
  667. unsigned int old_curseg;
  668. int i;
  669. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  670. curseg = CURSEG_I(sbi, i);
  671. old_curseg = curseg->segno;
  672. SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
  673. locate_dirty_segment(sbi, old_curseg);
  674. }
  675. }
  676. static const struct segment_allocation default_salloc_ops = {
  677. .allocate_segment = allocate_segment_by_default,
  678. };
  679. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  680. {
  681. struct curseg_info *curseg = CURSEG_I(sbi, type);
  682. if (curseg->next_blkoff < sbi->blocks_per_seg)
  683. return true;
  684. return false;
  685. }
  686. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  687. {
  688. if (p_type == DATA)
  689. return CURSEG_HOT_DATA;
  690. else
  691. return CURSEG_HOT_NODE;
  692. }
  693. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  694. {
  695. if (p_type == DATA) {
  696. struct inode *inode = page->mapping->host;
  697. if (S_ISDIR(inode->i_mode))
  698. return CURSEG_HOT_DATA;
  699. else
  700. return CURSEG_COLD_DATA;
  701. } else {
  702. if (IS_DNODE(page) && !is_cold_node(page))
  703. return CURSEG_HOT_NODE;
  704. else
  705. return CURSEG_COLD_NODE;
  706. }
  707. }
  708. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  709. {
  710. if (p_type == DATA) {
  711. struct inode *inode = page->mapping->host;
  712. if (S_ISDIR(inode->i_mode))
  713. return CURSEG_HOT_DATA;
  714. else if (is_cold_data(page) || file_is_cold(inode))
  715. return CURSEG_COLD_DATA;
  716. else
  717. return CURSEG_WARM_DATA;
  718. } else {
  719. if (IS_DNODE(page))
  720. return is_cold_node(page) ? CURSEG_WARM_NODE :
  721. CURSEG_HOT_NODE;
  722. else
  723. return CURSEG_COLD_NODE;
  724. }
  725. }
  726. static int __get_segment_type(struct page *page, enum page_type p_type)
  727. {
  728. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  729. switch (sbi->active_logs) {
  730. case 2:
  731. return __get_segment_type_2(page, p_type);
  732. case 4:
  733. return __get_segment_type_4(page, p_type);
  734. }
  735. /* NR_CURSEG_TYPE(6) logs by default */
  736. f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
  737. return __get_segment_type_6(page, p_type);
  738. }
  739. void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  740. block_t old_blkaddr, block_t *new_blkaddr,
  741. struct f2fs_summary *sum, int type)
  742. {
  743. struct sit_info *sit_i = SIT_I(sbi);
  744. struct curseg_info *curseg;
  745. unsigned int old_cursegno;
  746. curseg = CURSEG_I(sbi, type);
  747. mutex_lock(&curseg->curseg_mutex);
  748. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  749. old_cursegno = curseg->segno;
  750. /*
  751. * __add_sum_entry should be resided under the curseg_mutex
  752. * because, this function updates a summary entry in the
  753. * current summary block.
  754. */
  755. __add_sum_entry(sbi, type, sum);
  756. mutex_lock(&sit_i->sentry_lock);
  757. __refresh_next_blkoff(sbi, curseg);
  758. stat_inc_block_count(sbi, curseg);
  759. if (!__has_curseg_space(sbi, type))
  760. sit_i->s_ops->allocate_segment(sbi, type, false);
  761. /*
  762. * SIT information should be updated before segment allocation,
  763. * since SSR needs latest valid block information.
  764. */
  765. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  766. locate_dirty_segment(sbi, old_cursegno);
  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_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  844. __add_sum_entry(sbi, type, sum);
  845. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  846. locate_dirty_segment(sbi, old_cursegno);
  847. mutex_unlock(&sit_i->sentry_lock);
  848. mutex_unlock(&curseg->curseg_mutex);
  849. }
  850. void rewrite_node_page(struct f2fs_sb_info *sbi,
  851. struct page *page, struct f2fs_summary *sum,
  852. block_t old_blkaddr, block_t new_blkaddr)
  853. {
  854. struct sit_info *sit_i = SIT_I(sbi);
  855. int type = CURSEG_WARM_NODE;
  856. struct curseg_info *curseg;
  857. unsigned int segno, old_cursegno;
  858. block_t next_blkaddr = next_blkaddr_of_node(page);
  859. unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
  860. struct f2fs_io_info fio = {
  861. .type = NODE,
  862. .rw = WRITE_SYNC,
  863. };
  864. curseg = CURSEG_I(sbi, type);
  865. mutex_lock(&curseg->curseg_mutex);
  866. mutex_lock(&sit_i->sentry_lock);
  867. segno = GET_SEGNO(sbi, new_blkaddr);
  868. old_cursegno = curseg->segno;
  869. /* change the current segment */
  870. if (segno != curseg->segno) {
  871. curseg->next_segno = segno;
  872. change_curseg(sbi, type, true);
  873. }
  874. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  875. __add_sum_entry(sbi, type, sum);
  876. /* change the current log to the next block addr in advance */
  877. if (next_segno != segno) {
  878. curseg->next_segno = next_segno;
  879. change_curseg(sbi, type, true);
  880. }
  881. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
  882. /* rewrite node page */
  883. set_page_writeback(page);
  884. f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
  885. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  886. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  887. locate_dirty_segment(sbi, old_cursegno);
  888. mutex_unlock(&sit_i->sentry_lock);
  889. mutex_unlock(&curseg->curseg_mutex);
  890. }
  891. static inline bool is_merged_page(struct f2fs_sb_info *sbi,
  892. struct page *page, enum page_type type)
  893. {
  894. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  895. struct f2fs_bio_info *io = &sbi->write_io[btype];
  896. struct bio *bio = io->bio;
  897. struct bio_vec *bvec;
  898. int i;
  899. down_read(&io->io_rwsem);
  900. if (!bio)
  901. goto out;
  902. bio_for_each_segment_all(bvec, bio, i) {
  903. if (page == bvec->bv_page) {
  904. up_read(&io->io_rwsem);
  905. return true;
  906. }
  907. }
  908. out:
  909. up_read(&io->io_rwsem);
  910. return false;
  911. }
  912. void f2fs_wait_on_page_writeback(struct page *page,
  913. enum page_type type)
  914. {
  915. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  916. if (PageWriteback(page)) {
  917. if (is_merged_page(sbi, page, type))
  918. f2fs_submit_merged_bio(sbi, type, WRITE);
  919. wait_on_page_writeback(page);
  920. }
  921. }
  922. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  923. {
  924. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  925. struct curseg_info *seg_i;
  926. unsigned char *kaddr;
  927. struct page *page;
  928. block_t start;
  929. int i, j, offset;
  930. start = start_sum_block(sbi);
  931. page = get_meta_page(sbi, start++);
  932. kaddr = (unsigned char *)page_address(page);
  933. /* Step 1: restore nat cache */
  934. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  935. memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
  936. /* Step 2: restore sit cache */
  937. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  938. memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
  939. SUM_JOURNAL_SIZE);
  940. offset = 2 * SUM_JOURNAL_SIZE;
  941. /* Step 3: restore summary entries */
  942. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  943. unsigned short blk_off;
  944. unsigned int segno;
  945. seg_i = CURSEG_I(sbi, i);
  946. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  947. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  948. seg_i->next_segno = segno;
  949. reset_curseg(sbi, i, 0);
  950. seg_i->alloc_type = ckpt->alloc_type[i];
  951. seg_i->next_blkoff = blk_off;
  952. if (seg_i->alloc_type == SSR)
  953. blk_off = sbi->blocks_per_seg;
  954. for (j = 0; j < blk_off; j++) {
  955. struct f2fs_summary *s;
  956. s = (struct f2fs_summary *)(kaddr + offset);
  957. seg_i->sum_blk->entries[j] = *s;
  958. offset += SUMMARY_SIZE;
  959. if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  960. SUM_FOOTER_SIZE)
  961. continue;
  962. f2fs_put_page(page, 1);
  963. page = NULL;
  964. page = get_meta_page(sbi, start++);
  965. kaddr = (unsigned char *)page_address(page);
  966. offset = 0;
  967. }
  968. }
  969. f2fs_put_page(page, 1);
  970. return 0;
  971. }
  972. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  973. {
  974. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  975. struct f2fs_summary_block *sum;
  976. struct curseg_info *curseg;
  977. struct page *new;
  978. unsigned short blk_off;
  979. unsigned int segno = 0;
  980. block_t blk_addr = 0;
  981. /* get segment number and block addr */
  982. if (IS_DATASEG(type)) {
  983. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  984. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  985. CURSEG_HOT_DATA]);
  986. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  987. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  988. else
  989. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  990. } else {
  991. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  992. CURSEG_HOT_NODE]);
  993. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  994. CURSEG_HOT_NODE]);
  995. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  996. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  997. type - CURSEG_HOT_NODE);
  998. else
  999. blk_addr = GET_SUM_BLOCK(sbi, segno);
  1000. }
  1001. new = get_meta_page(sbi, blk_addr);
  1002. sum = (struct f2fs_summary_block *)page_address(new);
  1003. if (IS_NODESEG(type)) {
  1004. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
  1005. struct f2fs_summary *ns = &sum->entries[0];
  1006. int i;
  1007. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  1008. ns->version = 0;
  1009. ns->ofs_in_node = 0;
  1010. }
  1011. } else {
  1012. int err;
  1013. err = restore_node_summary(sbi, segno, sum);
  1014. if (err) {
  1015. f2fs_put_page(new, 1);
  1016. return err;
  1017. }
  1018. }
  1019. }
  1020. /* set uncompleted segment to curseg */
  1021. curseg = CURSEG_I(sbi, type);
  1022. mutex_lock(&curseg->curseg_mutex);
  1023. memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
  1024. curseg->next_segno = segno;
  1025. reset_curseg(sbi, type, 0);
  1026. curseg->alloc_type = ckpt->alloc_type[type];
  1027. curseg->next_blkoff = blk_off;
  1028. mutex_unlock(&curseg->curseg_mutex);
  1029. f2fs_put_page(new, 1);
  1030. return 0;
  1031. }
  1032. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  1033. {
  1034. int type = CURSEG_HOT_DATA;
  1035. int err;
  1036. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
  1037. /* restore for compacted data summary */
  1038. if (read_compacted_summaries(sbi))
  1039. return -EINVAL;
  1040. type = CURSEG_HOT_NODE;
  1041. }
  1042. for (; type <= CURSEG_COLD_NODE; type++) {
  1043. err = read_normal_summaries(sbi, type);
  1044. if (err)
  1045. return err;
  1046. }
  1047. return 0;
  1048. }
  1049. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  1050. {
  1051. struct page *page;
  1052. unsigned char *kaddr;
  1053. struct f2fs_summary *summary;
  1054. struct curseg_info *seg_i;
  1055. int written_size = 0;
  1056. int i, j;
  1057. page = grab_meta_page(sbi, blkaddr++);
  1058. kaddr = (unsigned char *)page_address(page);
  1059. /* Step 1: write nat cache */
  1060. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1061. memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
  1062. written_size += SUM_JOURNAL_SIZE;
  1063. /* Step 2: write sit cache */
  1064. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1065. memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
  1066. SUM_JOURNAL_SIZE);
  1067. written_size += SUM_JOURNAL_SIZE;
  1068. /* Step 3: write summary entries */
  1069. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1070. unsigned short blkoff;
  1071. seg_i = CURSEG_I(sbi, i);
  1072. if (sbi->ckpt->alloc_type[i] == SSR)
  1073. blkoff = sbi->blocks_per_seg;
  1074. else
  1075. blkoff = curseg_blkoff(sbi, i);
  1076. for (j = 0; j < blkoff; j++) {
  1077. if (!page) {
  1078. page = grab_meta_page(sbi, blkaddr++);
  1079. kaddr = (unsigned char *)page_address(page);
  1080. written_size = 0;
  1081. }
  1082. summary = (struct f2fs_summary *)(kaddr + written_size);
  1083. *summary = seg_i->sum_blk->entries[j];
  1084. written_size += SUMMARY_SIZE;
  1085. if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1086. SUM_FOOTER_SIZE)
  1087. continue;
  1088. set_page_dirty(page);
  1089. f2fs_put_page(page, 1);
  1090. page = NULL;
  1091. }
  1092. }
  1093. if (page) {
  1094. set_page_dirty(page);
  1095. f2fs_put_page(page, 1);
  1096. }
  1097. }
  1098. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  1099. block_t blkaddr, int type)
  1100. {
  1101. int i, end;
  1102. if (IS_DATASEG(type))
  1103. end = type + NR_CURSEG_DATA_TYPE;
  1104. else
  1105. end = type + NR_CURSEG_NODE_TYPE;
  1106. for (i = type; i < end; i++) {
  1107. struct curseg_info *sum = CURSEG_I(sbi, i);
  1108. mutex_lock(&sum->curseg_mutex);
  1109. write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
  1110. mutex_unlock(&sum->curseg_mutex);
  1111. }
  1112. }
  1113. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1114. {
  1115. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
  1116. write_compacted_summaries(sbi, start_blk);
  1117. else
  1118. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1119. }
  1120. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1121. {
  1122. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
  1123. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1124. }
  1125. int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
  1126. unsigned int val, int alloc)
  1127. {
  1128. int i;
  1129. if (type == NAT_JOURNAL) {
  1130. for (i = 0; i < nats_in_cursum(sum); i++) {
  1131. if (le32_to_cpu(nid_in_journal(sum, i)) == val)
  1132. return i;
  1133. }
  1134. if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
  1135. return update_nats_in_cursum(sum, 1);
  1136. } else if (type == SIT_JOURNAL) {
  1137. for (i = 0; i < sits_in_cursum(sum); i++)
  1138. if (le32_to_cpu(segno_in_journal(sum, i)) == val)
  1139. return i;
  1140. if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
  1141. return update_sits_in_cursum(sum, 1);
  1142. }
  1143. return -1;
  1144. }
  1145. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1146. unsigned int segno)
  1147. {
  1148. struct sit_info *sit_i = SIT_I(sbi);
  1149. unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
  1150. block_t blk_addr = sit_i->sit_base_addr + offset;
  1151. check_seg_range(sbi, segno);
  1152. /* calculate sit block address */
  1153. if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  1154. blk_addr += sit_i->sit_blocks;
  1155. return get_meta_page(sbi, blk_addr);
  1156. }
  1157. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1158. unsigned int start)
  1159. {
  1160. struct sit_info *sit_i = SIT_I(sbi);
  1161. struct page *src_page, *dst_page;
  1162. pgoff_t src_off, dst_off;
  1163. void *src_addr, *dst_addr;
  1164. src_off = current_sit_addr(sbi, start);
  1165. dst_off = next_sit_addr(sbi, src_off);
  1166. /* get current sit block page without lock */
  1167. src_page = get_meta_page(sbi, src_off);
  1168. dst_page = grab_meta_page(sbi, dst_off);
  1169. f2fs_bug_on(PageDirty(src_page));
  1170. src_addr = page_address(src_page);
  1171. dst_addr = page_address(dst_page);
  1172. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  1173. set_page_dirty(dst_page);
  1174. f2fs_put_page(src_page, 1);
  1175. set_to_next_sit(sit_i, start);
  1176. return dst_page;
  1177. }
  1178. static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
  1179. {
  1180. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1181. struct f2fs_summary_block *sum = curseg->sum_blk;
  1182. int i;
  1183. /*
  1184. * If the journal area in the current summary is full of sit entries,
  1185. * all the sit entries will be flushed. Otherwise the sit entries
  1186. * are not able to replace with newly hot sit entries.
  1187. */
  1188. if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
  1189. for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
  1190. unsigned int segno;
  1191. segno = le32_to_cpu(segno_in_journal(sum, i));
  1192. __mark_sit_entry_dirty(sbi, segno);
  1193. }
  1194. update_sits_in_cursum(sum, -sits_in_cursum(sum));
  1195. return true;
  1196. }
  1197. return false;
  1198. }
  1199. /*
  1200. * CP calls this function, which flushes SIT entries including sit_journal,
  1201. * and moves prefree segs to free segs.
  1202. */
  1203. void flush_sit_entries(struct f2fs_sb_info *sbi)
  1204. {
  1205. struct sit_info *sit_i = SIT_I(sbi);
  1206. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1207. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1208. struct f2fs_summary_block *sum = curseg->sum_blk;
  1209. unsigned long nsegs = TOTAL_SEGS(sbi);
  1210. struct page *page = NULL;
  1211. struct f2fs_sit_block *raw_sit = NULL;
  1212. unsigned int start = 0, end = 0;
  1213. unsigned int segno = -1;
  1214. bool flushed;
  1215. mutex_lock(&curseg->curseg_mutex);
  1216. mutex_lock(&sit_i->sentry_lock);
  1217. /*
  1218. * "flushed" indicates whether sit entries in journal are flushed
  1219. * to the SIT area or not.
  1220. */
  1221. flushed = flush_sits_in_journal(sbi);
  1222. while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
  1223. struct seg_entry *se = get_seg_entry(sbi, segno);
  1224. int sit_offset, offset;
  1225. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1226. /* add discard candidates */
  1227. if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
  1228. add_discard_addrs(sbi, segno, se);
  1229. if (flushed)
  1230. goto to_sit_page;
  1231. offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
  1232. if (offset >= 0) {
  1233. segno_in_journal(sum, offset) = cpu_to_le32(segno);
  1234. seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
  1235. goto flush_done;
  1236. }
  1237. to_sit_page:
  1238. if (!page || (start > segno) || (segno > end)) {
  1239. if (page) {
  1240. f2fs_put_page(page, 1);
  1241. page = NULL;
  1242. }
  1243. start = START_SEGNO(sit_i, segno);
  1244. end = start + SIT_ENTRY_PER_BLOCK - 1;
  1245. /* read sit block that will be updated */
  1246. page = get_next_sit_page(sbi, start);
  1247. raw_sit = page_address(page);
  1248. }
  1249. /* udpate entry in SIT block */
  1250. seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
  1251. flush_done:
  1252. __clear_bit(segno, bitmap);
  1253. sit_i->dirty_sentries--;
  1254. }
  1255. mutex_unlock(&sit_i->sentry_lock);
  1256. mutex_unlock(&curseg->curseg_mutex);
  1257. /* writeout last modified SIT block */
  1258. f2fs_put_page(page, 1);
  1259. set_prefree_as_free_segments(sbi);
  1260. }
  1261. static int build_sit_info(struct f2fs_sb_info *sbi)
  1262. {
  1263. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1264. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1265. struct sit_info *sit_i;
  1266. unsigned int sit_segs, start;
  1267. char *src_bitmap, *dst_bitmap;
  1268. unsigned int bitmap_size;
  1269. /* allocate memory for SIT information */
  1270. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1271. if (!sit_i)
  1272. return -ENOMEM;
  1273. SM_I(sbi)->sit_info = sit_i;
  1274. sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
  1275. if (!sit_i->sentries)
  1276. return -ENOMEM;
  1277. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1278. sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1279. if (!sit_i->dirty_sentries_bitmap)
  1280. return -ENOMEM;
  1281. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1282. sit_i->sentries[start].cur_valid_map
  1283. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1284. sit_i->sentries[start].ckpt_valid_map
  1285. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1286. if (!sit_i->sentries[start].cur_valid_map
  1287. || !sit_i->sentries[start].ckpt_valid_map)
  1288. return -ENOMEM;
  1289. }
  1290. if (sbi->segs_per_sec > 1) {
  1291. sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
  1292. sizeof(struct sec_entry));
  1293. if (!sit_i->sec_entries)
  1294. return -ENOMEM;
  1295. }
  1296. /* get information related with SIT */
  1297. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1298. /* setup SIT bitmap from ckeckpoint pack */
  1299. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1300. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1301. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1302. if (!dst_bitmap)
  1303. return -ENOMEM;
  1304. /* init SIT information */
  1305. sit_i->s_ops = &default_salloc_ops;
  1306. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1307. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1308. sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
  1309. sit_i->sit_bitmap = dst_bitmap;
  1310. sit_i->bitmap_size = bitmap_size;
  1311. sit_i->dirty_sentries = 0;
  1312. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1313. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1314. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1315. mutex_init(&sit_i->sentry_lock);
  1316. return 0;
  1317. }
  1318. static int build_free_segmap(struct f2fs_sb_info *sbi)
  1319. {
  1320. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1321. struct free_segmap_info *free_i;
  1322. unsigned int bitmap_size, sec_bitmap_size;
  1323. /* allocate memory for free segmap information */
  1324. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  1325. if (!free_i)
  1326. return -ENOMEM;
  1327. SM_I(sbi)->free_info = free_i;
  1328. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1329. free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
  1330. if (!free_i->free_segmap)
  1331. return -ENOMEM;
  1332. sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1333. free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
  1334. if (!free_i->free_secmap)
  1335. return -ENOMEM;
  1336. /* set all segments as dirty temporarily */
  1337. memset(free_i->free_segmap, 0xff, bitmap_size);
  1338. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  1339. /* init free segmap information */
  1340. free_i->start_segno =
  1341. (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
  1342. free_i->free_segments = 0;
  1343. free_i->free_sections = 0;
  1344. rwlock_init(&free_i->segmap_lock);
  1345. return 0;
  1346. }
  1347. static int build_curseg(struct f2fs_sb_info *sbi)
  1348. {
  1349. struct curseg_info *array;
  1350. int i;
  1351. array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
  1352. if (!array)
  1353. return -ENOMEM;
  1354. SM_I(sbi)->curseg_array = array;
  1355. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  1356. mutex_init(&array[i].curseg_mutex);
  1357. array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  1358. if (!array[i].sum_blk)
  1359. return -ENOMEM;
  1360. array[i].segno = NULL_SEGNO;
  1361. array[i].next_blkoff = 0;
  1362. }
  1363. return restore_curseg_summaries(sbi);
  1364. }
  1365. static void build_sit_entries(struct f2fs_sb_info *sbi)
  1366. {
  1367. struct sit_info *sit_i = SIT_I(sbi);
  1368. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1369. struct f2fs_summary_block *sum = curseg->sum_blk;
  1370. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  1371. unsigned int i, start, end;
  1372. unsigned int readed, start_blk = 0;
  1373. int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
  1374. do {
  1375. readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
  1376. start = start_blk * sit_i->sents_per_block;
  1377. end = (start_blk + readed) * sit_i->sents_per_block;
  1378. for (; start < end && start < TOTAL_SEGS(sbi); start++) {
  1379. struct seg_entry *se = &sit_i->sentries[start];
  1380. struct f2fs_sit_block *sit_blk;
  1381. struct f2fs_sit_entry sit;
  1382. struct page *page;
  1383. mutex_lock(&curseg->curseg_mutex);
  1384. for (i = 0; i < sits_in_cursum(sum); i++) {
  1385. if (le32_to_cpu(segno_in_journal(sum, i))
  1386. == start) {
  1387. sit = sit_in_journal(sum, i);
  1388. mutex_unlock(&curseg->curseg_mutex);
  1389. goto got_it;
  1390. }
  1391. }
  1392. mutex_unlock(&curseg->curseg_mutex);
  1393. page = get_current_sit_page(sbi, start);
  1394. sit_blk = (struct f2fs_sit_block *)page_address(page);
  1395. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  1396. f2fs_put_page(page, 1);
  1397. got_it:
  1398. check_block_count(sbi, start, &sit);
  1399. seg_info_from_raw_sit(se, &sit);
  1400. if (sbi->segs_per_sec > 1) {
  1401. struct sec_entry *e = get_sec_entry(sbi, start);
  1402. e->valid_blocks += se->valid_blocks;
  1403. }
  1404. }
  1405. start_blk += readed;
  1406. } while (start_blk < sit_blk_cnt);
  1407. }
  1408. static void init_free_segmap(struct f2fs_sb_info *sbi)
  1409. {
  1410. unsigned int start;
  1411. int type;
  1412. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1413. struct seg_entry *sentry = get_seg_entry(sbi, start);
  1414. if (!sentry->valid_blocks)
  1415. __set_free(sbi, start);
  1416. }
  1417. /* set use the current segments */
  1418. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  1419. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  1420. __set_test_and_inuse(sbi, curseg_t->segno);
  1421. }
  1422. }
  1423. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  1424. {
  1425. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1426. struct free_segmap_info *free_i = FREE_I(sbi);
  1427. unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
  1428. unsigned short valid_blocks;
  1429. while (1) {
  1430. /* find dirty segment based on free segmap */
  1431. segno = find_next_inuse(free_i, total_segs, offset);
  1432. if (segno >= total_segs)
  1433. break;
  1434. offset = segno + 1;
  1435. valid_blocks = get_valid_blocks(sbi, segno, 0);
  1436. if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
  1437. continue;
  1438. mutex_lock(&dirty_i->seglist_lock);
  1439. __locate_dirty_segment(sbi, segno, DIRTY);
  1440. mutex_unlock(&dirty_i->seglist_lock);
  1441. }
  1442. }
  1443. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  1444. {
  1445. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1446. unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1447. dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
  1448. if (!dirty_i->victim_secmap)
  1449. return -ENOMEM;
  1450. return 0;
  1451. }
  1452. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  1453. {
  1454. struct dirty_seglist_info *dirty_i;
  1455. unsigned int bitmap_size, i;
  1456. /* allocate memory for dirty segments list information */
  1457. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  1458. if (!dirty_i)
  1459. return -ENOMEM;
  1460. SM_I(sbi)->dirty_info = dirty_i;
  1461. mutex_init(&dirty_i->seglist_lock);
  1462. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1463. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  1464. dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
  1465. if (!dirty_i->dirty_segmap[i])
  1466. return -ENOMEM;
  1467. }
  1468. init_dirty_segmap(sbi);
  1469. return init_victim_secmap(sbi);
  1470. }
  1471. /*
  1472. * Update min, max modified time for cost-benefit GC algorithm
  1473. */
  1474. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  1475. {
  1476. struct sit_info *sit_i = SIT_I(sbi);
  1477. unsigned int segno;
  1478. mutex_lock(&sit_i->sentry_lock);
  1479. sit_i->min_mtime = LLONG_MAX;
  1480. for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
  1481. unsigned int i;
  1482. unsigned long long mtime = 0;
  1483. for (i = 0; i < sbi->segs_per_sec; i++)
  1484. mtime += get_seg_entry(sbi, segno + i)->mtime;
  1485. mtime = div_u64(mtime, sbi->segs_per_sec);
  1486. if (sit_i->min_mtime > mtime)
  1487. sit_i->min_mtime = mtime;
  1488. }
  1489. sit_i->max_mtime = get_mtime(sbi);
  1490. mutex_unlock(&sit_i->sentry_lock);
  1491. }
  1492. int build_segment_manager(struct f2fs_sb_info *sbi)
  1493. {
  1494. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1495. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1496. struct f2fs_sm_info *sm_info;
  1497. int err;
  1498. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  1499. if (!sm_info)
  1500. return -ENOMEM;
  1501. /* init sm info */
  1502. sbi->sm_info = sm_info;
  1503. INIT_LIST_HEAD(&sm_info->wblist_head);
  1504. spin_lock_init(&sm_info->wblist_lock);
  1505. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1506. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1507. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  1508. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1509. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1510. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  1511. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1512. sm_info->rec_prefree_segments = sm_info->main_segments *
  1513. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  1514. sm_info->ipu_policy = F2FS_IPU_DISABLE;
  1515. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  1516. INIT_LIST_HEAD(&sm_info->discard_list);
  1517. sm_info->nr_discards = 0;
  1518. sm_info->max_discards = 0;
  1519. err = build_sit_info(sbi);
  1520. if (err)
  1521. return err;
  1522. err = build_free_segmap(sbi);
  1523. if (err)
  1524. return err;
  1525. err = build_curseg(sbi);
  1526. if (err)
  1527. return err;
  1528. /* reinit free segmap based on SIT */
  1529. build_sit_entries(sbi);
  1530. init_free_segmap(sbi);
  1531. err = build_dirty_segmap(sbi);
  1532. if (err)
  1533. return err;
  1534. init_min_max_mtime(sbi);
  1535. return 0;
  1536. }
  1537. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  1538. enum dirty_type dirty_type)
  1539. {
  1540. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1541. mutex_lock(&dirty_i->seglist_lock);
  1542. kfree(dirty_i->dirty_segmap[dirty_type]);
  1543. dirty_i->nr_dirty[dirty_type] = 0;
  1544. mutex_unlock(&dirty_i->seglist_lock);
  1545. }
  1546. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  1547. {
  1548. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1549. kfree(dirty_i->victim_secmap);
  1550. }
  1551. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  1552. {
  1553. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1554. int i;
  1555. if (!dirty_i)
  1556. return;
  1557. /* discard pre-free/dirty segments list */
  1558. for (i = 0; i < NR_DIRTY_TYPE; i++)
  1559. discard_dirty_segmap(sbi, i);
  1560. destroy_victim_secmap(sbi);
  1561. SM_I(sbi)->dirty_info = NULL;
  1562. kfree(dirty_i);
  1563. }
  1564. static void destroy_curseg(struct f2fs_sb_info *sbi)
  1565. {
  1566. struct curseg_info *array = SM_I(sbi)->curseg_array;
  1567. int i;
  1568. if (!array)
  1569. return;
  1570. SM_I(sbi)->curseg_array = NULL;
  1571. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1572. kfree(array[i].sum_blk);
  1573. kfree(array);
  1574. }
  1575. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  1576. {
  1577. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  1578. if (!free_i)
  1579. return;
  1580. SM_I(sbi)->free_info = NULL;
  1581. kfree(free_i->free_segmap);
  1582. kfree(free_i->free_secmap);
  1583. kfree(free_i);
  1584. }
  1585. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  1586. {
  1587. struct sit_info *sit_i = SIT_I(sbi);
  1588. unsigned int start;
  1589. if (!sit_i)
  1590. return;
  1591. if (sit_i->sentries) {
  1592. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1593. kfree(sit_i->sentries[start].cur_valid_map);
  1594. kfree(sit_i->sentries[start].ckpt_valid_map);
  1595. }
  1596. }
  1597. vfree(sit_i->sentries);
  1598. vfree(sit_i->sec_entries);
  1599. kfree(sit_i->dirty_sentries_bitmap);
  1600. SM_I(sbi)->sit_info = NULL;
  1601. kfree(sit_i->sit_bitmap);
  1602. kfree(sit_i);
  1603. }
  1604. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  1605. {
  1606. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1607. if (!sm_info)
  1608. return;
  1609. destroy_dirty_segmap(sbi);
  1610. destroy_curseg(sbi);
  1611. destroy_free_segmap(sbi);
  1612. destroy_sit_info(sbi);
  1613. sbi->sm_info = NULL;
  1614. kfree(sm_info);
  1615. }
  1616. int __init create_segment_manager_caches(void)
  1617. {
  1618. discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
  1619. sizeof(struct discard_entry));
  1620. if (!discard_entry_slab)
  1621. return -ENOMEM;
  1622. return 0;
  1623. }
  1624. void destroy_segment_manager_caches(void)
  1625. {
  1626. kmem_cache_destroy(discard_entry_slab);
  1627. }