segment.c 50 KB

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