segment.c 52 KB

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