segment.c 52 KB

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