segment.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  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/swap.h>
  18. #include <linux/timer.h>
  19. #include "f2fs.h"
  20. #include "segment.h"
  21. #include "node.h"
  22. #include "trace.h"
  23. #include <trace/events/f2fs.h>
  24. #define __reverse_ffz(x) __reverse_ffs(~(x))
  25. static struct kmem_cache *discard_entry_slab;
  26. static struct kmem_cache *sit_entry_set_slab;
  27. static struct kmem_cache *inmem_entry_slab;
  28. static unsigned long __reverse_ulong(unsigned char *str)
  29. {
  30. unsigned long tmp = 0;
  31. int shift = 24, idx = 0;
  32. #if BITS_PER_LONG == 64
  33. shift = 56;
  34. #endif
  35. while (shift >= 0) {
  36. tmp |= (unsigned long)str[idx++] << shift;
  37. shift -= BITS_PER_BYTE;
  38. }
  39. return tmp;
  40. }
  41. /*
  42. * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
  43. * MSB and LSB are reversed in a byte by f2fs_set_bit.
  44. */
  45. static inline unsigned long __reverse_ffs(unsigned long word)
  46. {
  47. int num = 0;
  48. #if BITS_PER_LONG == 64
  49. if ((word & 0xffffffff00000000UL) == 0)
  50. num += 32;
  51. else
  52. word >>= 32;
  53. #endif
  54. if ((word & 0xffff0000) == 0)
  55. num += 16;
  56. else
  57. word >>= 16;
  58. if ((word & 0xff00) == 0)
  59. num += 8;
  60. else
  61. word >>= 8;
  62. if ((word & 0xf0) == 0)
  63. num += 4;
  64. else
  65. word >>= 4;
  66. if ((word & 0xc) == 0)
  67. num += 2;
  68. else
  69. word >>= 2;
  70. if ((word & 0x2) == 0)
  71. num += 1;
  72. return num;
  73. }
  74. /*
  75. * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
  76. * f2fs_set_bit makes MSB and LSB reversed in a byte.
  77. * @size must be integral times of unsigned long.
  78. * Example:
  79. * MSB <--> LSB
  80. * f2fs_set_bit(0, bitmap) => 1000 0000
  81. * f2fs_set_bit(7, bitmap) => 0000 0001
  82. */
  83. static unsigned long __find_rev_next_bit(const unsigned long *addr,
  84. unsigned long size, unsigned long offset)
  85. {
  86. const unsigned long *p = addr + BIT_WORD(offset);
  87. unsigned long result = size;
  88. unsigned long tmp;
  89. if (offset >= size)
  90. return size;
  91. size -= (offset & ~(BITS_PER_LONG - 1));
  92. offset %= BITS_PER_LONG;
  93. while (1) {
  94. if (*p == 0)
  95. goto pass;
  96. tmp = __reverse_ulong((unsigned char *)p);
  97. tmp &= ~0UL >> offset;
  98. if (size < BITS_PER_LONG)
  99. tmp &= (~0UL << (BITS_PER_LONG - size));
  100. if (tmp)
  101. goto found;
  102. pass:
  103. if (size <= BITS_PER_LONG)
  104. break;
  105. size -= BITS_PER_LONG;
  106. offset = 0;
  107. p++;
  108. }
  109. return result;
  110. found:
  111. return result - size + __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 = size;
  118. unsigned long tmp;
  119. if (offset >= size)
  120. return size;
  121. size -= (offset & ~(BITS_PER_LONG - 1));
  122. offset %= BITS_PER_LONG;
  123. while (1) {
  124. if (*p == ~0UL)
  125. goto pass;
  126. tmp = __reverse_ulong((unsigned char *)p);
  127. if (offset)
  128. tmp |= ~0UL << (BITS_PER_LONG - offset);
  129. if (size < BITS_PER_LONG)
  130. tmp |= ~0UL >> size;
  131. if (tmp != ~0UL)
  132. goto found;
  133. pass:
  134. if (size <= BITS_PER_LONG)
  135. break;
  136. size -= BITS_PER_LONG;
  137. offset = 0;
  138. p++;
  139. }
  140. return result;
  141. found:
  142. return result - size + __reverse_ffz(tmp);
  143. }
  144. void register_inmem_page(struct inode *inode, struct page *page)
  145. {
  146. struct f2fs_inode_info *fi = F2FS_I(inode);
  147. struct inmem_pages *new;
  148. f2fs_trace_pid(page);
  149. set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
  150. SetPagePrivate(page);
  151. new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
  152. /* add atomic page indices to the list */
  153. new->page = page;
  154. INIT_LIST_HEAD(&new->list);
  155. /* increase reference count with clean state */
  156. mutex_lock(&fi->inmem_lock);
  157. get_page(page);
  158. list_add_tail(&new->list, &fi->inmem_pages);
  159. inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  160. mutex_unlock(&fi->inmem_lock);
  161. trace_f2fs_register_inmem_page(page, INMEM);
  162. }
  163. int commit_inmem_pages(struct inode *inode, bool abort)
  164. {
  165. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  166. struct f2fs_inode_info *fi = F2FS_I(inode);
  167. struct inmem_pages *cur, *tmp;
  168. bool submit_bio = false;
  169. struct f2fs_io_info fio = {
  170. .sbi = sbi,
  171. .type = DATA,
  172. .rw = WRITE_SYNC | REQ_PRIO,
  173. .encrypted_page = NULL,
  174. };
  175. int err = 0;
  176. /*
  177. * The abort is true only when f2fs_evict_inode is called.
  178. * Basically, the f2fs_evict_inode doesn't produce any data writes, so
  179. * that we don't need to call f2fs_balance_fs.
  180. * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
  181. * inode becomes free by iget_locked in f2fs_iget.
  182. */
  183. if (!abort) {
  184. f2fs_balance_fs(sbi, true);
  185. f2fs_lock_op(sbi);
  186. }
  187. mutex_lock(&fi->inmem_lock);
  188. list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
  189. lock_page(cur->page);
  190. if (!abort) {
  191. if (cur->page->mapping == inode->i_mapping) {
  192. set_page_dirty(cur->page);
  193. f2fs_wait_on_page_writeback(cur->page, DATA);
  194. if (clear_page_dirty_for_io(cur->page))
  195. inode_dec_dirty_pages(inode);
  196. trace_f2fs_commit_inmem_page(cur->page, INMEM);
  197. fio.page = cur->page;
  198. err = do_write_data_page(&fio);
  199. if (err) {
  200. unlock_page(cur->page);
  201. break;
  202. }
  203. clear_cold_data(cur->page);
  204. submit_bio = true;
  205. }
  206. } else {
  207. ClearPageUptodate(cur->page);
  208. trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP);
  209. }
  210. set_page_private(cur->page, 0);
  211. ClearPagePrivate(cur->page);
  212. f2fs_put_page(cur->page, 1);
  213. list_del(&cur->list);
  214. kmem_cache_free(inmem_entry_slab, cur);
  215. dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  216. }
  217. mutex_unlock(&fi->inmem_lock);
  218. if (!abort) {
  219. f2fs_unlock_op(sbi);
  220. if (submit_bio)
  221. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  222. }
  223. return err;
  224. }
  225. /*
  226. * This function balances dirty node and dentry pages.
  227. * In addition, it controls garbage collection.
  228. */
  229. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
  230. {
  231. if (!need)
  232. return;
  233. /*
  234. * We should do GC or end up with checkpoint, if there are so many dirty
  235. * dir/node pages without enough free segments.
  236. */
  237. if (has_not_enough_free_secs(sbi, 0)) {
  238. mutex_lock(&sbi->gc_mutex);
  239. f2fs_gc(sbi, false);
  240. }
  241. }
  242. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
  243. {
  244. /* try to shrink extent cache when there is no enough memory */
  245. if (!available_free_memory(sbi, EXTENT_CACHE))
  246. f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
  247. /* check the # of cached NAT entries */
  248. if (!available_free_memory(sbi, NAT_ENTRIES))
  249. try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
  250. if (!available_free_memory(sbi, FREE_NIDS))
  251. try_to_free_nids(sbi, NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES);
  252. /* checkpoint is the only way to shrink partial cached entries */
  253. if (!available_free_memory(sbi, NAT_ENTRIES) ||
  254. excess_prefree_segs(sbi) ||
  255. !available_free_memory(sbi, INO_ENTRIES) ||
  256. (is_idle(sbi) && f2fs_time_over(sbi, CP_TIME))) {
  257. if (test_opt(sbi, DATA_FLUSH))
  258. sync_dirty_inodes(sbi, FILE_INODE);
  259. f2fs_sync_fs(sbi->sb, true);
  260. stat_inc_bg_cp_count(sbi->stat_info);
  261. }
  262. }
  263. static int issue_flush_thread(void *data)
  264. {
  265. struct f2fs_sb_info *sbi = data;
  266. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  267. wait_queue_head_t *q = &fcc->flush_wait_queue;
  268. repeat:
  269. if (kthread_should_stop())
  270. return 0;
  271. if (!llist_empty(&fcc->issue_list)) {
  272. struct bio *bio;
  273. struct flush_cmd *cmd, *next;
  274. int ret;
  275. bio = f2fs_bio_alloc(0);
  276. fcc->dispatch_list = llist_del_all(&fcc->issue_list);
  277. fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
  278. bio->bi_bdev = sbi->sb->s_bdev;
  279. ret = submit_bio_wait(WRITE_FLUSH, bio);
  280. llist_for_each_entry_safe(cmd, next,
  281. fcc->dispatch_list, llnode) {
  282. cmd->ret = ret;
  283. complete(&cmd->wait);
  284. }
  285. bio_put(bio);
  286. fcc->dispatch_list = NULL;
  287. }
  288. wait_event_interruptible(*q,
  289. kthread_should_stop() || !llist_empty(&fcc->issue_list));
  290. goto repeat;
  291. }
  292. int f2fs_issue_flush(struct f2fs_sb_info *sbi)
  293. {
  294. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  295. struct flush_cmd cmd;
  296. trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
  297. test_opt(sbi, FLUSH_MERGE));
  298. if (test_opt(sbi, NOBARRIER))
  299. return 0;
  300. if (!test_opt(sbi, FLUSH_MERGE)) {
  301. struct bio *bio = f2fs_bio_alloc(0);
  302. int ret;
  303. bio->bi_bdev = sbi->sb->s_bdev;
  304. ret = submit_bio_wait(WRITE_FLUSH, bio);
  305. bio_put(bio);
  306. return ret;
  307. }
  308. init_completion(&cmd.wait);
  309. llist_add(&cmd.llnode, &fcc->issue_list);
  310. if (!fcc->dispatch_list)
  311. wake_up(&fcc->flush_wait_queue);
  312. wait_for_completion(&cmd.wait);
  313. return cmd.ret;
  314. }
  315. int create_flush_cmd_control(struct f2fs_sb_info *sbi)
  316. {
  317. dev_t dev = sbi->sb->s_bdev->bd_dev;
  318. struct flush_cmd_control *fcc;
  319. int err = 0;
  320. fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
  321. if (!fcc)
  322. return -ENOMEM;
  323. init_waitqueue_head(&fcc->flush_wait_queue);
  324. init_llist_head(&fcc->issue_list);
  325. SM_I(sbi)->cmd_control_info = fcc;
  326. fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
  327. "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
  328. if (IS_ERR(fcc->f2fs_issue_flush)) {
  329. err = PTR_ERR(fcc->f2fs_issue_flush);
  330. kfree(fcc);
  331. SM_I(sbi)->cmd_control_info = NULL;
  332. return err;
  333. }
  334. return err;
  335. }
  336. void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
  337. {
  338. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  339. if (fcc && fcc->f2fs_issue_flush)
  340. kthread_stop(fcc->f2fs_issue_flush);
  341. kfree(fcc);
  342. SM_I(sbi)->cmd_control_info = NULL;
  343. }
  344. static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  345. enum dirty_type dirty_type)
  346. {
  347. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  348. /* need not be added */
  349. if (IS_CURSEG(sbi, segno))
  350. return;
  351. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  352. dirty_i->nr_dirty[dirty_type]++;
  353. if (dirty_type == DIRTY) {
  354. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  355. enum dirty_type t = sentry->type;
  356. if (unlikely(t >= DIRTY)) {
  357. f2fs_bug_on(sbi, 1);
  358. return;
  359. }
  360. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
  361. dirty_i->nr_dirty[t]++;
  362. }
  363. }
  364. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  365. enum dirty_type dirty_type)
  366. {
  367. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  368. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  369. dirty_i->nr_dirty[dirty_type]--;
  370. if (dirty_type == DIRTY) {
  371. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  372. enum dirty_type t = sentry->type;
  373. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  374. dirty_i->nr_dirty[t]--;
  375. if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
  376. clear_bit(GET_SECNO(sbi, segno),
  377. dirty_i->victim_secmap);
  378. }
  379. }
  380. /*
  381. * Should not occur error such as -ENOMEM.
  382. * Adding dirty entry into seglist is not critical operation.
  383. * If a given segment is one of current working segments, it won't be added.
  384. */
  385. static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  386. {
  387. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  388. unsigned short valid_blocks;
  389. if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
  390. return;
  391. mutex_lock(&dirty_i->seglist_lock);
  392. valid_blocks = get_valid_blocks(sbi, segno, 0);
  393. if (valid_blocks == 0) {
  394. __locate_dirty_segment(sbi, segno, PRE);
  395. __remove_dirty_segment(sbi, segno, DIRTY);
  396. } else if (valid_blocks < sbi->blocks_per_seg) {
  397. __locate_dirty_segment(sbi, segno, DIRTY);
  398. } else {
  399. /* Recovery routine with SSR needs this */
  400. __remove_dirty_segment(sbi, segno, DIRTY);
  401. }
  402. mutex_unlock(&dirty_i->seglist_lock);
  403. }
  404. static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
  405. block_t blkstart, block_t blklen)
  406. {
  407. sector_t start = SECTOR_FROM_BLOCK(blkstart);
  408. sector_t len = SECTOR_FROM_BLOCK(blklen);
  409. struct seg_entry *se;
  410. unsigned int offset;
  411. block_t i;
  412. for (i = blkstart; i < blkstart + blklen; i++) {
  413. se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
  414. offset = GET_BLKOFF_FROM_SEG0(sbi, i);
  415. if (!f2fs_test_and_set_bit(offset, se->discard_map))
  416. sbi->discard_blks--;
  417. }
  418. trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
  419. return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
  420. }
  421. bool discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
  422. {
  423. int err = -ENOTSUPP;
  424. if (test_opt(sbi, DISCARD)) {
  425. struct seg_entry *se = get_seg_entry(sbi,
  426. GET_SEGNO(sbi, blkaddr));
  427. unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  428. if (f2fs_test_bit(offset, se->discard_map))
  429. return false;
  430. err = f2fs_issue_discard(sbi, blkaddr, 1);
  431. }
  432. if (err) {
  433. update_meta_page(sbi, NULL, blkaddr);
  434. return true;
  435. }
  436. return false;
  437. }
  438. static void __add_discard_entry(struct f2fs_sb_info *sbi,
  439. struct cp_control *cpc, struct seg_entry *se,
  440. unsigned int start, unsigned int end)
  441. {
  442. struct list_head *head = &SM_I(sbi)->discard_list;
  443. struct discard_entry *new, *last;
  444. if (!list_empty(head)) {
  445. last = list_last_entry(head, struct discard_entry, list);
  446. if (START_BLOCK(sbi, cpc->trim_start) + start ==
  447. last->blkaddr + last->len) {
  448. last->len += end - start;
  449. goto done;
  450. }
  451. }
  452. new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
  453. INIT_LIST_HEAD(&new->list);
  454. new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
  455. new->len = end - start;
  456. list_add_tail(&new->list, head);
  457. done:
  458. SM_I(sbi)->nr_discards += end - start;
  459. }
  460. static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  461. {
  462. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  463. int max_blocks = sbi->blocks_per_seg;
  464. struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
  465. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  466. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  467. unsigned long *discard_map = (unsigned long *)se->discard_map;
  468. unsigned long *dmap = SIT_I(sbi)->tmp_map;
  469. unsigned int start = 0, end = -1;
  470. bool force = (cpc->reason == CP_DISCARD);
  471. int i;
  472. if (se->valid_blocks == max_blocks)
  473. return;
  474. if (!force) {
  475. if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
  476. SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
  477. return;
  478. }
  479. /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
  480. for (i = 0; i < entries; i++)
  481. dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
  482. (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
  483. while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
  484. start = __find_rev_next_bit(dmap, max_blocks, end + 1);
  485. if (start >= max_blocks)
  486. break;
  487. end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
  488. __add_discard_entry(sbi, cpc, se, start, end);
  489. }
  490. }
  491. void release_discard_addrs(struct f2fs_sb_info *sbi)
  492. {
  493. struct list_head *head = &(SM_I(sbi)->discard_list);
  494. struct discard_entry *entry, *this;
  495. /* drop caches */
  496. list_for_each_entry_safe(entry, this, head, list) {
  497. list_del(&entry->list);
  498. kmem_cache_free(discard_entry_slab, entry);
  499. }
  500. }
  501. /*
  502. * Should call clear_prefree_segments after checkpoint is done.
  503. */
  504. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  505. {
  506. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  507. unsigned int segno;
  508. mutex_lock(&dirty_i->seglist_lock);
  509. for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
  510. __set_test_and_free(sbi, segno);
  511. mutex_unlock(&dirty_i->seglist_lock);
  512. }
  513. void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  514. {
  515. struct list_head *head = &(SM_I(sbi)->discard_list);
  516. struct discard_entry *entry, *this;
  517. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  518. unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
  519. unsigned int start = 0, end = -1;
  520. mutex_lock(&dirty_i->seglist_lock);
  521. while (1) {
  522. int i;
  523. start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
  524. if (start >= MAIN_SEGS(sbi))
  525. break;
  526. end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
  527. start + 1);
  528. for (i = start; i < end; i++)
  529. clear_bit(i, prefree_map);
  530. dirty_i->nr_dirty[PRE] -= end - start;
  531. if (!test_opt(sbi, DISCARD))
  532. continue;
  533. f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
  534. (end - start) << sbi->log_blocks_per_seg);
  535. }
  536. mutex_unlock(&dirty_i->seglist_lock);
  537. /* send small discards */
  538. list_for_each_entry_safe(entry, this, head, list) {
  539. if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen)
  540. goto skip;
  541. f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
  542. cpc->trimmed += entry->len;
  543. skip:
  544. list_del(&entry->list);
  545. SM_I(sbi)->nr_discards -= entry->len;
  546. kmem_cache_free(discard_entry_slab, entry);
  547. }
  548. }
  549. static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  550. {
  551. struct sit_info *sit_i = SIT_I(sbi);
  552. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
  553. sit_i->dirty_sentries++;
  554. return false;
  555. }
  556. return true;
  557. }
  558. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  559. unsigned int segno, int modified)
  560. {
  561. struct seg_entry *se = get_seg_entry(sbi, segno);
  562. se->type = type;
  563. if (modified)
  564. __mark_sit_entry_dirty(sbi, segno);
  565. }
  566. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  567. {
  568. struct seg_entry *se;
  569. unsigned int segno, offset;
  570. long int new_vblocks;
  571. segno = GET_SEGNO(sbi, blkaddr);
  572. se = get_seg_entry(sbi, segno);
  573. new_vblocks = se->valid_blocks + del;
  574. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  575. f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
  576. (new_vblocks > sbi->blocks_per_seg)));
  577. se->valid_blocks = new_vblocks;
  578. se->mtime = get_mtime(sbi);
  579. SIT_I(sbi)->max_mtime = se->mtime;
  580. /* Update valid block bitmap */
  581. if (del > 0) {
  582. if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
  583. f2fs_bug_on(sbi, 1);
  584. if (!f2fs_test_and_set_bit(offset, se->discard_map))
  585. sbi->discard_blks--;
  586. } else {
  587. if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
  588. f2fs_bug_on(sbi, 1);
  589. if (f2fs_test_and_clear_bit(offset, se->discard_map))
  590. sbi->discard_blks++;
  591. }
  592. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  593. se->ckpt_valid_blocks += del;
  594. __mark_sit_entry_dirty(sbi, segno);
  595. /* update total number of valid blocks to be written in ckpt area */
  596. SIT_I(sbi)->written_valid_blocks += del;
  597. if (sbi->segs_per_sec > 1)
  598. get_sec_entry(sbi, segno)->valid_blocks += del;
  599. }
  600. void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
  601. {
  602. update_sit_entry(sbi, new, 1);
  603. if (GET_SEGNO(sbi, old) != NULL_SEGNO)
  604. update_sit_entry(sbi, old, -1);
  605. locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
  606. locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
  607. }
  608. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  609. {
  610. unsigned int segno = GET_SEGNO(sbi, addr);
  611. struct sit_info *sit_i = SIT_I(sbi);
  612. f2fs_bug_on(sbi, addr == NULL_ADDR);
  613. if (addr == NEW_ADDR)
  614. return;
  615. /* add it into sit main buffer */
  616. mutex_lock(&sit_i->sentry_lock);
  617. update_sit_entry(sbi, addr, -1);
  618. /* add it into dirty seglist */
  619. locate_dirty_segment(sbi, segno);
  620. mutex_unlock(&sit_i->sentry_lock);
  621. }
  622. bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
  623. {
  624. struct sit_info *sit_i = SIT_I(sbi);
  625. unsigned int segno, offset;
  626. struct seg_entry *se;
  627. bool is_cp = false;
  628. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
  629. return true;
  630. mutex_lock(&sit_i->sentry_lock);
  631. segno = GET_SEGNO(sbi, blkaddr);
  632. se = get_seg_entry(sbi, segno);
  633. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  634. if (f2fs_test_bit(offset, se->ckpt_valid_map))
  635. is_cp = true;
  636. mutex_unlock(&sit_i->sentry_lock);
  637. return is_cp;
  638. }
  639. /*
  640. * This function should be resided under the curseg_mutex lock
  641. */
  642. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  643. struct f2fs_summary *sum)
  644. {
  645. struct curseg_info *curseg = CURSEG_I(sbi, type);
  646. void *addr = curseg->sum_blk;
  647. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  648. memcpy(addr, sum, sizeof(struct f2fs_summary));
  649. }
  650. /*
  651. * Calculate the number of current summary pages for writing
  652. */
  653. int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
  654. {
  655. int valid_sum_count = 0;
  656. int i, sum_in_page;
  657. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  658. if (sbi->ckpt->alloc_type[i] == SSR)
  659. valid_sum_count += sbi->blocks_per_seg;
  660. else {
  661. if (for_ra)
  662. valid_sum_count += le16_to_cpu(
  663. F2FS_CKPT(sbi)->cur_data_blkoff[i]);
  664. else
  665. valid_sum_count += curseg_blkoff(sbi, i);
  666. }
  667. }
  668. sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
  669. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  670. if (valid_sum_count <= sum_in_page)
  671. return 1;
  672. else if ((valid_sum_count - sum_in_page) <=
  673. (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  674. return 2;
  675. return 3;
  676. }
  677. /*
  678. * Caller should put this summary page
  679. */
  680. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  681. {
  682. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  683. }
  684. void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
  685. {
  686. struct page *page = grab_meta_page(sbi, blk_addr);
  687. void *dst = page_address(page);
  688. if (src)
  689. memcpy(dst, src, PAGE_CACHE_SIZE);
  690. else
  691. memset(dst, 0, PAGE_CACHE_SIZE);
  692. set_page_dirty(page);
  693. f2fs_put_page(page, 1);
  694. }
  695. static void write_sum_page(struct f2fs_sb_info *sbi,
  696. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  697. {
  698. update_meta_page(sbi, (void *)sum_blk, blk_addr);
  699. }
  700. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  701. {
  702. struct curseg_info *curseg = CURSEG_I(sbi, type);
  703. unsigned int segno = curseg->segno + 1;
  704. struct free_segmap_info *free_i = FREE_I(sbi);
  705. if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
  706. return !test_bit(segno, free_i->free_segmap);
  707. return 0;
  708. }
  709. /*
  710. * Find a new segment from the free segments bitmap to right order
  711. * This function should be returned with success, otherwise BUG
  712. */
  713. static void get_new_segment(struct f2fs_sb_info *sbi,
  714. unsigned int *newseg, bool new_sec, int dir)
  715. {
  716. struct free_segmap_info *free_i = FREE_I(sbi);
  717. unsigned int segno, secno, zoneno;
  718. unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
  719. unsigned int hint = *newseg / sbi->segs_per_sec;
  720. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  721. unsigned int left_start = hint;
  722. bool init = true;
  723. int go_left = 0;
  724. int i;
  725. spin_lock(&free_i->segmap_lock);
  726. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  727. segno = find_next_zero_bit(free_i->free_segmap,
  728. MAIN_SEGS(sbi), *newseg + 1);
  729. if (segno - *newseg < sbi->segs_per_sec -
  730. (*newseg % sbi->segs_per_sec))
  731. goto got_it;
  732. }
  733. find_other_zone:
  734. secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
  735. if (secno >= MAIN_SECS(sbi)) {
  736. if (dir == ALLOC_RIGHT) {
  737. secno = find_next_zero_bit(free_i->free_secmap,
  738. MAIN_SECS(sbi), 0);
  739. f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
  740. } else {
  741. go_left = 1;
  742. left_start = hint - 1;
  743. }
  744. }
  745. if (go_left == 0)
  746. goto skip_left;
  747. while (test_bit(left_start, free_i->free_secmap)) {
  748. if (left_start > 0) {
  749. left_start--;
  750. continue;
  751. }
  752. left_start = find_next_zero_bit(free_i->free_secmap,
  753. MAIN_SECS(sbi), 0);
  754. f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
  755. break;
  756. }
  757. secno = left_start;
  758. skip_left:
  759. hint = secno;
  760. segno = secno * sbi->segs_per_sec;
  761. zoneno = secno / sbi->secs_per_zone;
  762. /* give up on finding another zone */
  763. if (!init)
  764. goto got_it;
  765. if (sbi->secs_per_zone == 1)
  766. goto got_it;
  767. if (zoneno == old_zoneno)
  768. goto got_it;
  769. if (dir == ALLOC_LEFT) {
  770. if (!go_left && zoneno + 1 >= total_zones)
  771. goto got_it;
  772. if (go_left && zoneno == 0)
  773. goto got_it;
  774. }
  775. for (i = 0; i < NR_CURSEG_TYPE; i++)
  776. if (CURSEG_I(sbi, i)->zone == zoneno)
  777. break;
  778. if (i < NR_CURSEG_TYPE) {
  779. /* zone is in user, try another */
  780. if (go_left)
  781. hint = zoneno * sbi->secs_per_zone - 1;
  782. else if (zoneno + 1 >= total_zones)
  783. hint = 0;
  784. else
  785. hint = (zoneno + 1) * sbi->secs_per_zone;
  786. init = false;
  787. goto find_other_zone;
  788. }
  789. got_it:
  790. /* set it as dirty segment in free segmap */
  791. f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
  792. __set_inuse(sbi, segno);
  793. *newseg = segno;
  794. spin_unlock(&free_i->segmap_lock);
  795. }
  796. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  797. {
  798. struct curseg_info *curseg = CURSEG_I(sbi, type);
  799. struct summary_footer *sum_footer;
  800. curseg->segno = curseg->next_segno;
  801. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  802. curseg->next_blkoff = 0;
  803. curseg->next_segno = NULL_SEGNO;
  804. sum_footer = &(curseg->sum_blk->footer);
  805. memset(sum_footer, 0, sizeof(struct summary_footer));
  806. if (IS_DATASEG(type))
  807. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  808. if (IS_NODESEG(type))
  809. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  810. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  811. }
  812. /*
  813. * Allocate a current working segment.
  814. * This function always allocates a free segment in LFS manner.
  815. */
  816. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  817. {
  818. struct curseg_info *curseg = CURSEG_I(sbi, type);
  819. unsigned int segno = curseg->segno;
  820. int dir = ALLOC_LEFT;
  821. write_sum_page(sbi, curseg->sum_blk,
  822. GET_SUM_BLOCK(sbi, segno));
  823. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  824. dir = ALLOC_RIGHT;
  825. if (test_opt(sbi, NOHEAP))
  826. dir = ALLOC_RIGHT;
  827. get_new_segment(sbi, &segno, new_sec, dir);
  828. curseg->next_segno = segno;
  829. reset_curseg(sbi, type, 1);
  830. curseg->alloc_type = LFS;
  831. }
  832. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  833. struct curseg_info *seg, block_t start)
  834. {
  835. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  836. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  837. unsigned long *target_map = SIT_I(sbi)->tmp_map;
  838. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  839. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  840. int i, pos;
  841. for (i = 0; i < entries; i++)
  842. target_map[i] = ckpt_map[i] | cur_map[i];
  843. pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
  844. seg->next_blkoff = pos;
  845. }
  846. /*
  847. * If a segment is written by LFS manner, next block offset is just obtained
  848. * by increasing the current block offset. However, if a segment is written by
  849. * SSR manner, next block offset obtained by calling __next_free_blkoff
  850. */
  851. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  852. struct curseg_info *seg)
  853. {
  854. if (seg->alloc_type == SSR)
  855. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  856. else
  857. seg->next_blkoff++;
  858. }
  859. /*
  860. * This function always allocates a used segment(from dirty seglist) by SSR
  861. * manner, so it should recover the existing segment information of valid blocks
  862. */
  863. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  864. {
  865. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  866. struct curseg_info *curseg = CURSEG_I(sbi, type);
  867. unsigned int new_segno = curseg->next_segno;
  868. struct f2fs_summary_block *sum_node;
  869. struct page *sum_page;
  870. write_sum_page(sbi, curseg->sum_blk,
  871. GET_SUM_BLOCK(sbi, curseg->segno));
  872. __set_test_and_inuse(sbi, new_segno);
  873. mutex_lock(&dirty_i->seglist_lock);
  874. __remove_dirty_segment(sbi, new_segno, PRE);
  875. __remove_dirty_segment(sbi, new_segno, DIRTY);
  876. mutex_unlock(&dirty_i->seglist_lock);
  877. reset_curseg(sbi, type, 1);
  878. curseg->alloc_type = SSR;
  879. __next_free_blkoff(sbi, curseg, 0);
  880. if (reuse) {
  881. sum_page = get_sum_page(sbi, new_segno);
  882. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  883. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  884. f2fs_put_page(sum_page, 1);
  885. }
  886. }
  887. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  888. {
  889. struct curseg_info *curseg = CURSEG_I(sbi, type);
  890. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  891. if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
  892. return v_ops->get_victim(sbi,
  893. &(curseg)->next_segno, BG_GC, type, SSR);
  894. /* For data segments, let's do SSR more intensively */
  895. for (; type >= CURSEG_HOT_DATA; type--)
  896. if (v_ops->get_victim(sbi, &(curseg)->next_segno,
  897. BG_GC, type, SSR))
  898. return 1;
  899. return 0;
  900. }
  901. /*
  902. * flush out current segment and replace it with new segment
  903. * This function should be returned with success, otherwise BUG
  904. */
  905. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  906. int type, bool force)
  907. {
  908. struct curseg_info *curseg = CURSEG_I(sbi, type);
  909. if (force)
  910. new_curseg(sbi, type, true);
  911. else if (type == CURSEG_WARM_NODE)
  912. new_curseg(sbi, type, false);
  913. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  914. new_curseg(sbi, type, false);
  915. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  916. change_curseg(sbi, type, true);
  917. else
  918. new_curseg(sbi, type, false);
  919. stat_inc_seg_type(sbi, curseg);
  920. }
  921. static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
  922. {
  923. struct curseg_info *curseg = CURSEG_I(sbi, type);
  924. unsigned int old_segno;
  925. old_segno = curseg->segno;
  926. SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
  927. locate_dirty_segment(sbi, old_segno);
  928. }
  929. void allocate_new_segments(struct f2fs_sb_info *sbi)
  930. {
  931. int i;
  932. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
  933. __allocate_new_segments(sbi, i);
  934. }
  935. static const struct segment_allocation default_salloc_ops = {
  936. .allocate_segment = allocate_segment_by_default,
  937. };
  938. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
  939. {
  940. __u64 start = F2FS_BYTES_TO_BLK(range->start);
  941. __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
  942. unsigned int start_segno, end_segno;
  943. struct cp_control cpc;
  944. int err = 0;
  945. if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
  946. return -EINVAL;
  947. cpc.trimmed = 0;
  948. if (end <= MAIN_BLKADDR(sbi))
  949. goto out;
  950. /* start/end segment number in main_area */
  951. start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
  952. end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
  953. GET_SEGNO(sbi, end);
  954. cpc.reason = CP_DISCARD;
  955. cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
  956. /* do checkpoint to issue discard commands safely */
  957. for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
  958. cpc.trim_start = start_segno;
  959. if (sbi->discard_blks == 0)
  960. break;
  961. else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
  962. cpc.trim_end = end_segno;
  963. else
  964. cpc.trim_end = min_t(unsigned int,
  965. rounddown(start_segno +
  966. BATCHED_TRIM_SEGMENTS(sbi),
  967. sbi->segs_per_sec) - 1, end_segno);
  968. mutex_lock(&sbi->gc_mutex);
  969. err = write_checkpoint(sbi, &cpc);
  970. mutex_unlock(&sbi->gc_mutex);
  971. }
  972. out:
  973. range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
  974. return err;
  975. }
  976. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  977. {
  978. struct curseg_info *curseg = CURSEG_I(sbi, type);
  979. if (curseg->next_blkoff < sbi->blocks_per_seg)
  980. return true;
  981. return false;
  982. }
  983. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  984. {
  985. if (p_type == DATA)
  986. return CURSEG_HOT_DATA;
  987. else
  988. return CURSEG_HOT_NODE;
  989. }
  990. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  991. {
  992. if (p_type == DATA) {
  993. struct inode *inode = page->mapping->host;
  994. if (S_ISDIR(inode->i_mode))
  995. return CURSEG_HOT_DATA;
  996. else
  997. return CURSEG_COLD_DATA;
  998. } else {
  999. if (IS_DNODE(page) && is_cold_node(page))
  1000. return CURSEG_WARM_NODE;
  1001. else
  1002. return CURSEG_COLD_NODE;
  1003. }
  1004. }
  1005. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  1006. {
  1007. if (p_type == DATA) {
  1008. struct inode *inode = page->mapping->host;
  1009. if (S_ISDIR(inode->i_mode))
  1010. return CURSEG_HOT_DATA;
  1011. else if (is_cold_data(page) || file_is_cold(inode))
  1012. return CURSEG_COLD_DATA;
  1013. else
  1014. return CURSEG_WARM_DATA;
  1015. } else {
  1016. if (IS_DNODE(page))
  1017. return is_cold_node(page) ? CURSEG_WARM_NODE :
  1018. CURSEG_HOT_NODE;
  1019. else
  1020. return CURSEG_COLD_NODE;
  1021. }
  1022. }
  1023. static int __get_segment_type(struct page *page, enum page_type p_type)
  1024. {
  1025. switch (F2FS_P_SB(page)->active_logs) {
  1026. case 2:
  1027. return __get_segment_type_2(page, p_type);
  1028. case 4:
  1029. return __get_segment_type_4(page, p_type);
  1030. }
  1031. /* NR_CURSEG_TYPE(6) logs by default */
  1032. f2fs_bug_on(F2FS_P_SB(page),
  1033. F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
  1034. return __get_segment_type_6(page, p_type);
  1035. }
  1036. void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  1037. block_t old_blkaddr, block_t *new_blkaddr,
  1038. struct f2fs_summary *sum, int type)
  1039. {
  1040. struct sit_info *sit_i = SIT_I(sbi);
  1041. struct curseg_info *curseg;
  1042. bool direct_io = (type == CURSEG_DIRECT_IO);
  1043. type = direct_io ? CURSEG_WARM_DATA : type;
  1044. curseg = CURSEG_I(sbi, type);
  1045. mutex_lock(&curseg->curseg_mutex);
  1046. mutex_lock(&sit_i->sentry_lock);
  1047. /* direct_io'ed data is aligned to the segment for better performance */
  1048. if (direct_io && curseg->next_blkoff &&
  1049. !has_not_enough_free_secs(sbi, 0))
  1050. __allocate_new_segments(sbi, type);
  1051. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  1052. /*
  1053. * __add_sum_entry should be resided under the curseg_mutex
  1054. * because, this function updates a summary entry in the
  1055. * current summary block.
  1056. */
  1057. __add_sum_entry(sbi, type, sum);
  1058. __refresh_next_blkoff(sbi, curseg);
  1059. stat_inc_block_count(sbi, curseg);
  1060. if (!__has_curseg_space(sbi, type))
  1061. sit_i->s_ops->allocate_segment(sbi, type, false);
  1062. /*
  1063. * SIT information should be updated before segment allocation,
  1064. * since SSR needs latest valid block information.
  1065. */
  1066. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  1067. mutex_unlock(&sit_i->sentry_lock);
  1068. if (page && IS_NODESEG(type))
  1069. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  1070. mutex_unlock(&curseg->curseg_mutex);
  1071. }
  1072. static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
  1073. {
  1074. int type = __get_segment_type(fio->page, fio->type);
  1075. allocate_data_block(fio->sbi, fio->page, fio->blk_addr,
  1076. &fio->blk_addr, sum, type);
  1077. /* writeout dirty page into bdev */
  1078. f2fs_submit_page_mbio(fio);
  1079. }
  1080. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
  1081. {
  1082. struct f2fs_io_info fio = {
  1083. .sbi = sbi,
  1084. .type = META,
  1085. .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
  1086. .blk_addr = page->index,
  1087. .page = page,
  1088. .encrypted_page = NULL,
  1089. };
  1090. if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
  1091. fio.rw &= ~REQ_META;
  1092. set_page_writeback(page);
  1093. f2fs_submit_page_mbio(&fio);
  1094. }
  1095. void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
  1096. {
  1097. struct f2fs_summary sum;
  1098. set_summary(&sum, nid, 0, 0);
  1099. do_write_page(&sum, fio);
  1100. }
  1101. void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
  1102. {
  1103. struct f2fs_sb_info *sbi = fio->sbi;
  1104. struct f2fs_summary sum;
  1105. struct node_info ni;
  1106. f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
  1107. get_node_info(sbi, dn->nid, &ni);
  1108. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  1109. do_write_page(&sum, fio);
  1110. dn->data_blkaddr = fio->blk_addr;
  1111. }
  1112. void rewrite_data_page(struct f2fs_io_info *fio)
  1113. {
  1114. stat_inc_inplace_blocks(fio->sbi);
  1115. f2fs_submit_page_mbio(fio);
  1116. }
  1117. static void __f2fs_replace_block(struct f2fs_sb_info *sbi,
  1118. struct f2fs_summary *sum,
  1119. block_t old_blkaddr, block_t new_blkaddr,
  1120. bool recover_curseg)
  1121. {
  1122. struct sit_info *sit_i = SIT_I(sbi);
  1123. struct curseg_info *curseg;
  1124. unsigned int segno, old_cursegno;
  1125. struct seg_entry *se;
  1126. int type;
  1127. unsigned short old_blkoff;
  1128. segno = GET_SEGNO(sbi, new_blkaddr);
  1129. se = get_seg_entry(sbi, segno);
  1130. type = se->type;
  1131. if (!recover_curseg) {
  1132. /* for recovery flow */
  1133. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  1134. if (old_blkaddr == NULL_ADDR)
  1135. type = CURSEG_COLD_DATA;
  1136. else
  1137. type = CURSEG_WARM_DATA;
  1138. }
  1139. } else {
  1140. if (!IS_CURSEG(sbi, segno))
  1141. type = CURSEG_WARM_DATA;
  1142. }
  1143. curseg = CURSEG_I(sbi, type);
  1144. mutex_lock(&curseg->curseg_mutex);
  1145. mutex_lock(&sit_i->sentry_lock);
  1146. old_cursegno = curseg->segno;
  1147. old_blkoff = curseg->next_blkoff;
  1148. /* change the current segment */
  1149. if (segno != curseg->segno) {
  1150. curseg->next_segno = segno;
  1151. change_curseg(sbi, type, true);
  1152. }
  1153. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  1154. __add_sum_entry(sbi, type, sum);
  1155. if (!recover_curseg)
  1156. update_sit_entry(sbi, new_blkaddr, 1);
  1157. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  1158. update_sit_entry(sbi, old_blkaddr, -1);
  1159. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  1160. locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
  1161. locate_dirty_segment(sbi, old_cursegno);
  1162. if (recover_curseg) {
  1163. if (old_cursegno != curseg->segno) {
  1164. curseg->next_segno = old_cursegno;
  1165. change_curseg(sbi, type, true);
  1166. }
  1167. curseg->next_blkoff = old_blkoff;
  1168. }
  1169. mutex_unlock(&sit_i->sentry_lock);
  1170. mutex_unlock(&curseg->curseg_mutex);
  1171. }
  1172. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  1173. block_t old_addr, block_t new_addr,
  1174. unsigned char version, bool recover_curseg)
  1175. {
  1176. struct f2fs_summary sum;
  1177. set_summary(&sum, dn->nid, dn->ofs_in_node, version);
  1178. __f2fs_replace_block(sbi, &sum, old_addr, new_addr, recover_curseg);
  1179. dn->data_blkaddr = new_addr;
  1180. set_data_blkaddr(dn);
  1181. f2fs_update_extent_cache(dn);
  1182. }
  1183. static inline bool is_merged_page(struct f2fs_sb_info *sbi,
  1184. struct page *page, enum page_type type)
  1185. {
  1186. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  1187. struct f2fs_bio_info *io = &sbi->write_io[btype];
  1188. struct bio_vec *bvec;
  1189. struct page *target;
  1190. int i;
  1191. down_read(&io->io_rwsem);
  1192. if (!io->bio) {
  1193. up_read(&io->io_rwsem);
  1194. return false;
  1195. }
  1196. bio_for_each_segment_all(bvec, io->bio, i) {
  1197. if (bvec->bv_page->mapping) {
  1198. target = bvec->bv_page;
  1199. } else {
  1200. struct f2fs_crypto_ctx *ctx;
  1201. /* encrypted page */
  1202. ctx = (struct f2fs_crypto_ctx *)page_private(
  1203. bvec->bv_page);
  1204. target = ctx->w.control_page;
  1205. }
  1206. if (page == target) {
  1207. up_read(&io->io_rwsem);
  1208. return true;
  1209. }
  1210. }
  1211. up_read(&io->io_rwsem);
  1212. return false;
  1213. }
  1214. void f2fs_wait_on_page_writeback(struct page *page,
  1215. enum page_type type)
  1216. {
  1217. if (PageWriteback(page)) {
  1218. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1219. if (is_merged_page(sbi, page, type))
  1220. f2fs_submit_merged_bio(sbi, type, WRITE);
  1221. wait_on_page_writeback(page);
  1222. }
  1223. }
  1224. void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
  1225. block_t blkaddr)
  1226. {
  1227. struct page *cpage;
  1228. if (blkaddr == NEW_ADDR)
  1229. return;
  1230. f2fs_bug_on(sbi, blkaddr == NULL_ADDR);
  1231. cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
  1232. if (cpage) {
  1233. f2fs_wait_on_page_writeback(cpage, DATA);
  1234. f2fs_put_page(cpage, 1);
  1235. }
  1236. }
  1237. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  1238. {
  1239. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1240. struct curseg_info *seg_i;
  1241. unsigned char *kaddr;
  1242. struct page *page;
  1243. block_t start;
  1244. int i, j, offset;
  1245. start = start_sum_block(sbi);
  1246. page = get_meta_page(sbi, start++);
  1247. kaddr = (unsigned char *)page_address(page);
  1248. /* Step 1: restore nat cache */
  1249. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1250. memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
  1251. /* Step 2: restore sit cache */
  1252. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1253. memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
  1254. SUM_JOURNAL_SIZE);
  1255. offset = 2 * SUM_JOURNAL_SIZE;
  1256. /* Step 3: restore summary entries */
  1257. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1258. unsigned short blk_off;
  1259. unsigned int segno;
  1260. seg_i = CURSEG_I(sbi, i);
  1261. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  1262. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  1263. seg_i->next_segno = segno;
  1264. reset_curseg(sbi, i, 0);
  1265. seg_i->alloc_type = ckpt->alloc_type[i];
  1266. seg_i->next_blkoff = blk_off;
  1267. if (seg_i->alloc_type == SSR)
  1268. blk_off = sbi->blocks_per_seg;
  1269. for (j = 0; j < blk_off; j++) {
  1270. struct f2fs_summary *s;
  1271. s = (struct f2fs_summary *)(kaddr + offset);
  1272. seg_i->sum_blk->entries[j] = *s;
  1273. offset += SUMMARY_SIZE;
  1274. if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1275. SUM_FOOTER_SIZE)
  1276. continue;
  1277. f2fs_put_page(page, 1);
  1278. page = NULL;
  1279. page = get_meta_page(sbi, start++);
  1280. kaddr = (unsigned char *)page_address(page);
  1281. offset = 0;
  1282. }
  1283. }
  1284. f2fs_put_page(page, 1);
  1285. return 0;
  1286. }
  1287. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  1288. {
  1289. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1290. struct f2fs_summary_block *sum;
  1291. struct curseg_info *curseg;
  1292. struct page *new;
  1293. unsigned short blk_off;
  1294. unsigned int segno = 0;
  1295. block_t blk_addr = 0;
  1296. /* get segment number and block addr */
  1297. if (IS_DATASEG(type)) {
  1298. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  1299. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  1300. CURSEG_HOT_DATA]);
  1301. if (__exist_node_summaries(sbi))
  1302. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  1303. else
  1304. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  1305. } else {
  1306. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  1307. CURSEG_HOT_NODE]);
  1308. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  1309. CURSEG_HOT_NODE]);
  1310. if (__exist_node_summaries(sbi))
  1311. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  1312. type - CURSEG_HOT_NODE);
  1313. else
  1314. blk_addr = GET_SUM_BLOCK(sbi, segno);
  1315. }
  1316. new = get_meta_page(sbi, blk_addr);
  1317. sum = (struct f2fs_summary_block *)page_address(new);
  1318. if (IS_NODESEG(type)) {
  1319. if (__exist_node_summaries(sbi)) {
  1320. struct f2fs_summary *ns = &sum->entries[0];
  1321. int i;
  1322. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  1323. ns->version = 0;
  1324. ns->ofs_in_node = 0;
  1325. }
  1326. } else {
  1327. int err;
  1328. err = restore_node_summary(sbi, segno, sum);
  1329. if (err) {
  1330. f2fs_put_page(new, 1);
  1331. return err;
  1332. }
  1333. }
  1334. }
  1335. /* set uncompleted segment to curseg */
  1336. curseg = CURSEG_I(sbi, type);
  1337. mutex_lock(&curseg->curseg_mutex);
  1338. memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
  1339. curseg->next_segno = segno;
  1340. reset_curseg(sbi, type, 0);
  1341. curseg->alloc_type = ckpt->alloc_type[type];
  1342. curseg->next_blkoff = blk_off;
  1343. mutex_unlock(&curseg->curseg_mutex);
  1344. f2fs_put_page(new, 1);
  1345. return 0;
  1346. }
  1347. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  1348. {
  1349. int type = CURSEG_HOT_DATA;
  1350. int err;
  1351. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
  1352. int npages = npages_for_summary_flush(sbi, true);
  1353. if (npages >= 2)
  1354. ra_meta_pages(sbi, start_sum_block(sbi), npages,
  1355. META_CP, true);
  1356. /* restore for compacted data summary */
  1357. if (read_compacted_summaries(sbi))
  1358. return -EINVAL;
  1359. type = CURSEG_HOT_NODE;
  1360. }
  1361. if (__exist_node_summaries(sbi))
  1362. ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
  1363. NR_CURSEG_TYPE - type, META_CP, true);
  1364. for (; type <= CURSEG_COLD_NODE; type++) {
  1365. err = read_normal_summaries(sbi, type);
  1366. if (err)
  1367. return err;
  1368. }
  1369. return 0;
  1370. }
  1371. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  1372. {
  1373. struct page *page;
  1374. unsigned char *kaddr;
  1375. struct f2fs_summary *summary;
  1376. struct curseg_info *seg_i;
  1377. int written_size = 0;
  1378. int i, j;
  1379. page = grab_meta_page(sbi, blkaddr++);
  1380. kaddr = (unsigned char *)page_address(page);
  1381. /* Step 1: write nat cache */
  1382. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1383. memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
  1384. written_size += SUM_JOURNAL_SIZE;
  1385. /* Step 2: write sit cache */
  1386. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1387. memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
  1388. SUM_JOURNAL_SIZE);
  1389. written_size += SUM_JOURNAL_SIZE;
  1390. /* Step 3: write summary entries */
  1391. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1392. unsigned short blkoff;
  1393. seg_i = CURSEG_I(sbi, i);
  1394. if (sbi->ckpt->alloc_type[i] == SSR)
  1395. blkoff = sbi->blocks_per_seg;
  1396. else
  1397. blkoff = curseg_blkoff(sbi, i);
  1398. for (j = 0; j < blkoff; j++) {
  1399. if (!page) {
  1400. page = grab_meta_page(sbi, blkaddr++);
  1401. kaddr = (unsigned char *)page_address(page);
  1402. written_size = 0;
  1403. }
  1404. summary = (struct f2fs_summary *)(kaddr + written_size);
  1405. *summary = seg_i->sum_blk->entries[j];
  1406. written_size += SUMMARY_SIZE;
  1407. if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1408. SUM_FOOTER_SIZE)
  1409. continue;
  1410. set_page_dirty(page);
  1411. f2fs_put_page(page, 1);
  1412. page = NULL;
  1413. }
  1414. }
  1415. if (page) {
  1416. set_page_dirty(page);
  1417. f2fs_put_page(page, 1);
  1418. }
  1419. }
  1420. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  1421. block_t blkaddr, int type)
  1422. {
  1423. int i, end;
  1424. if (IS_DATASEG(type))
  1425. end = type + NR_CURSEG_DATA_TYPE;
  1426. else
  1427. end = type + NR_CURSEG_NODE_TYPE;
  1428. for (i = type; i < end; i++) {
  1429. struct curseg_info *sum = CURSEG_I(sbi, i);
  1430. mutex_lock(&sum->curseg_mutex);
  1431. write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
  1432. mutex_unlock(&sum->curseg_mutex);
  1433. }
  1434. }
  1435. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1436. {
  1437. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
  1438. write_compacted_summaries(sbi, start_blk);
  1439. else
  1440. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1441. }
  1442. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1443. {
  1444. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1445. }
  1446. int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
  1447. unsigned int val, int alloc)
  1448. {
  1449. int i;
  1450. if (type == NAT_JOURNAL) {
  1451. for (i = 0; i < nats_in_cursum(sum); i++) {
  1452. if (le32_to_cpu(nid_in_journal(sum, i)) == val)
  1453. return i;
  1454. }
  1455. if (alloc && __has_cursum_space(sum, 1, NAT_JOURNAL))
  1456. return update_nats_in_cursum(sum, 1);
  1457. } else if (type == SIT_JOURNAL) {
  1458. for (i = 0; i < sits_in_cursum(sum); i++)
  1459. if (le32_to_cpu(segno_in_journal(sum, i)) == val)
  1460. return i;
  1461. if (alloc && __has_cursum_space(sum, 1, SIT_JOURNAL))
  1462. return update_sits_in_cursum(sum, 1);
  1463. }
  1464. return -1;
  1465. }
  1466. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1467. unsigned int segno)
  1468. {
  1469. return get_meta_page(sbi, current_sit_addr(sbi, segno));
  1470. }
  1471. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1472. unsigned int start)
  1473. {
  1474. struct sit_info *sit_i = SIT_I(sbi);
  1475. struct page *src_page, *dst_page;
  1476. pgoff_t src_off, dst_off;
  1477. void *src_addr, *dst_addr;
  1478. src_off = current_sit_addr(sbi, start);
  1479. dst_off = next_sit_addr(sbi, src_off);
  1480. /* get current sit block page without lock */
  1481. src_page = get_meta_page(sbi, src_off);
  1482. dst_page = grab_meta_page(sbi, dst_off);
  1483. f2fs_bug_on(sbi, PageDirty(src_page));
  1484. src_addr = page_address(src_page);
  1485. dst_addr = page_address(dst_page);
  1486. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  1487. set_page_dirty(dst_page);
  1488. f2fs_put_page(src_page, 1);
  1489. set_to_next_sit(sit_i, start);
  1490. return dst_page;
  1491. }
  1492. static struct sit_entry_set *grab_sit_entry_set(void)
  1493. {
  1494. struct sit_entry_set *ses =
  1495. f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
  1496. ses->entry_cnt = 0;
  1497. INIT_LIST_HEAD(&ses->set_list);
  1498. return ses;
  1499. }
  1500. static void release_sit_entry_set(struct sit_entry_set *ses)
  1501. {
  1502. list_del(&ses->set_list);
  1503. kmem_cache_free(sit_entry_set_slab, ses);
  1504. }
  1505. static void adjust_sit_entry_set(struct sit_entry_set *ses,
  1506. struct list_head *head)
  1507. {
  1508. struct sit_entry_set *next = ses;
  1509. if (list_is_last(&ses->set_list, head))
  1510. return;
  1511. list_for_each_entry_continue(next, head, set_list)
  1512. if (ses->entry_cnt <= next->entry_cnt)
  1513. break;
  1514. list_move_tail(&ses->set_list, &next->set_list);
  1515. }
  1516. static void add_sit_entry(unsigned int segno, struct list_head *head)
  1517. {
  1518. struct sit_entry_set *ses;
  1519. unsigned int start_segno = START_SEGNO(segno);
  1520. list_for_each_entry(ses, head, set_list) {
  1521. if (ses->start_segno == start_segno) {
  1522. ses->entry_cnt++;
  1523. adjust_sit_entry_set(ses, head);
  1524. return;
  1525. }
  1526. }
  1527. ses = grab_sit_entry_set();
  1528. ses->start_segno = start_segno;
  1529. ses->entry_cnt++;
  1530. list_add(&ses->set_list, head);
  1531. }
  1532. static void add_sits_in_set(struct f2fs_sb_info *sbi)
  1533. {
  1534. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1535. struct list_head *set_list = &sm_info->sit_entry_set;
  1536. unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
  1537. unsigned int segno;
  1538. for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
  1539. add_sit_entry(segno, set_list);
  1540. }
  1541. static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
  1542. {
  1543. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1544. struct f2fs_summary_block *sum = curseg->sum_blk;
  1545. int i;
  1546. for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
  1547. unsigned int segno;
  1548. bool dirtied;
  1549. segno = le32_to_cpu(segno_in_journal(sum, i));
  1550. dirtied = __mark_sit_entry_dirty(sbi, segno);
  1551. if (!dirtied)
  1552. add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
  1553. }
  1554. update_sits_in_cursum(sum, -sits_in_cursum(sum));
  1555. }
  1556. /*
  1557. * CP calls this function, which flushes SIT entries including sit_journal,
  1558. * and moves prefree segs to free segs.
  1559. */
  1560. void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  1561. {
  1562. struct sit_info *sit_i = SIT_I(sbi);
  1563. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1564. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1565. struct f2fs_summary_block *sum = curseg->sum_blk;
  1566. struct sit_entry_set *ses, *tmp;
  1567. struct list_head *head = &SM_I(sbi)->sit_entry_set;
  1568. bool to_journal = true;
  1569. struct seg_entry *se;
  1570. mutex_lock(&curseg->curseg_mutex);
  1571. mutex_lock(&sit_i->sentry_lock);
  1572. if (!sit_i->dirty_sentries)
  1573. goto out;
  1574. /*
  1575. * add and account sit entries of dirty bitmap in sit entry
  1576. * set temporarily
  1577. */
  1578. add_sits_in_set(sbi);
  1579. /*
  1580. * if there are no enough space in journal to store dirty sit
  1581. * entries, remove all entries from journal and add and account
  1582. * them in sit entry set.
  1583. */
  1584. if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
  1585. remove_sits_in_journal(sbi);
  1586. /*
  1587. * there are two steps to flush sit entries:
  1588. * #1, flush sit entries to journal in current cold data summary block.
  1589. * #2, flush sit entries to sit page.
  1590. */
  1591. list_for_each_entry_safe(ses, tmp, head, set_list) {
  1592. struct page *page = NULL;
  1593. struct f2fs_sit_block *raw_sit = NULL;
  1594. unsigned int start_segno = ses->start_segno;
  1595. unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
  1596. (unsigned long)MAIN_SEGS(sbi));
  1597. unsigned int segno = start_segno;
  1598. if (to_journal &&
  1599. !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
  1600. to_journal = false;
  1601. if (!to_journal) {
  1602. page = get_next_sit_page(sbi, start_segno);
  1603. raw_sit = page_address(page);
  1604. }
  1605. /* flush dirty sit entries in region of current sit set */
  1606. for_each_set_bit_from(segno, bitmap, end) {
  1607. int offset, sit_offset;
  1608. se = get_seg_entry(sbi, segno);
  1609. /* add discard candidates */
  1610. if (cpc->reason != CP_DISCARD) {
  1611. cpc->trim_start = segno;
  1612. add_discard_addrs(sbi, cpc);
  1613. }
  1614. if (to_journal) {
  1615. offset = lookup_journal_in_cursum(sum,
  1616. SIT_JOURNAL, segno, 1);
  1617. f2fs_bug_on(sbi, offset < 0);
  1618. segno_in_journal(sum, offset) =
  1619. cpu_to_le32(segno);
  1620. seg_info_to_raw_sit(se,
  1621. &sit_in_journal(sum, offset));
  1622. } else {
  1623. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1624. seg_info_to_raw_sit(se,
  1625. &raw_sit->entries[sit_offset]);
  1626. }
  1627. __clear_bit(segno, bitmap);
  1628. sit_i->dirty_sentries--;
  1629. ses->entry_cnt--;
  1630. }
  1631. if (!to_journal)
  1632. f2fs_put_page(page, 1);
  1633. f2fs_bug_on(sbi, ses->entry_cnt);
  1634. release_sit_entry_set(ses);
  1635. }
  1636. f2fs_bug_on(sbi, !list_empty(head));
  1637. f2fs_bug_on(sbi, sit_i->dirty_sentries);
  1638. out:
  1639. if (cpc->reason == CP_DISCARD) {
  1640. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
  1641. add_discard_addrs(sbi, cpc);
  1642. }
  1643. mutex_unlock(&sit_i->sentry_lock);
  1644. mutex_unlock(&curseg->curseg_mutex);
  1645. set_prefree_as_free_segments(sbi);
  1646. }
  1647. static int build_sit_info(struct f2fs_sb_info *sbi)
  1648. {
  1649. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1650. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1651. struct sit_info *sit_i;
  1652. unsigned int sit_segs, start;
  1653. char *src_bitmap, *dst_bitmap;
  1654. unsigned int bitmap_size;
  1655. /* allocate memory for SIT information */
  1656. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1657. if (!sit_i)
  1658. return -ENOMEM;
  1659. SM_I(sbi)->sit_info = sit_i;
  1660. sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
  1661. sizeof(struct seg_entry), GFP_KERNEL);
  1662. if (!sit_i->sentries)
  1663. return -ENOMEM;
  1664. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1665. sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  1666. if (!sit_i->dirty_sentries_bitmap)
  1667. return -ENOMEM;
  1668. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1669. sit_i->sentries[start].cur_valid_map
  1670. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1671. sit_i->sentries[start].ckpt_valid_map
  1672. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1673. sit_i->sentries[start].discard_map
  1674. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1675. if (!sit_i->sentries[start].cur_valid_map ||
  1676. !sit_i->sentries[start].ckpt_valid_map ||
  1677. !sit_i->sentries[start].discard_map)
  1678. return -ENOMEM;
  1679. }
  1680. sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1681. if (!sit_i->tmp_map)
  1682. return -ENOMEM;
  1683. if (sbi->segs_per_sec > 1) {
  1684. sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
  1685. sizeof(struct sec_entry), GFP_KERNEL);
  1686. if (!sit_i->sec_entries)
  1687. return -ENOMEM;
  1688. }
  1689. /* get information related with SIT */
  1690. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1691. /* setup SIT bitmap from ckeckpoint pack */
  1692. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1693. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1694. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1695. if (!dst_bitmap)
  1696. return -ENOMEM;
  1697. /* init SIT information */
  1698. sit_i->s_ops = &default_salloc_ops;
  1699. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1700. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1701. sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
  1702. sit_i->sit_bitmap = dst_bitmap;
  1703. sit_i->bitmap_size = bitmap_size;
  1704. sit_i->dirty_sentries = 0;
  1705. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1706. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1707. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1708. mutex_init(&sit_i->sentry_lock);
  1709. return 0;
  1710. }
  1711. static int build_free_segmap(struct f2fs_sb_info *sbi)
  1712. {
  1713. struct free_segmap_info *free_i;
  1714. unsigned int bitmap_size, sec_bitmap_size;
  1715. /* allocate memory for free segmap information */
  1716. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  1717. if (!free_i)
  1718. return -ENOMEM;
  1719. SM_I(sbi)->free_info = free_i;
  1720. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1721. free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
  1722. if (!free_i->free_segmap)
  1723. return -ENOMEM;
  1724. sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  1725. free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
  1726. if (!free_i->free_secmap)
  1727. return -ENOMEM;
  1728. /* set all segments as dirty temporarily */
  1729. memset(free_i->free_segmap, 0xff, bitmap_size);
  1730. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  1731. /* init free segmap information */
  1732. free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
  1733. free_i->free_segments = 0;
  1734. free_i->free_sections = 0;
  1735. spin_lock_init(&free_i->segmap_lock);
  1736. return 0;
  1737. }
  1738. static int build_curseg(struct f2fs_sb_info *sbi)
  1739. {
  1740. struct curseg_info *array;
  1741. int i;
  1742. array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
  1743. if (!array)
  1744. return -ENOMEM;
  1745. SM_I(sbi)->curseg_array = array;
  1746. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  1747. mutex_init(&array[i].curseg_mutex);
  1748. array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  1749. if (!array[i].sum_blk)
  1750. return -ENOMEM;
  1751. array[i].segno = NULL_SEGNO;
  1752. array[i].next_blkoff = 0;
  1753. }
  1754. return restore_curseg_summaries(sbi);
  1755. }
  1756. static void build_sit_entries(struct f2fs_sb_info *sbi)
  1757. {
  1758. struct sit_info *sit_i = SIT_I(sbi);
  1759. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1760. struct f2fs_summary_block *sum = curseg->sum_blk;
  1761. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  1762. unsigned int i, start, end;
  1763. unsigned int readed, start_blk = 0;
  1764. int nrpages = MAX_BIO_BLOCKS(sbi);
  1765. do {
  1766. readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT, true);
  1767. start = start_blk * sit_i->sents_per_block;
  1768. end = (start_blk + readed) * sit_i->sents_per_block;
  1769. for (; start < end && start < MAIN_SEGS(sbi); start++) {
  1770. struct seg_entry *se = &sit_i->sentries[start];
  1771. struct f2fs_sit_block *sit_blk;
  1772. struct f2fs_sit_entry sit;
  1773. struct page *page;
  1774. mutex_lock(&curseg->curseg_mutex);
  1775. for (i = 0; i < sits_in_cursum(sum); i++) {
  1776. if (le32_to_cpu(segno_in_journal(sum, i))
  1777. == start) {
  1778. sit = sit_in_journal(sum, i);
  1779. mutex_unlock(&curseg->curseg_mutex);
  1780. goto got_it;
  1781. }
  1782. }
  1783. mutex_unlock(&curseg->curseg_mutex);
  1784. page = get_current_sit_page(sbi, start);
  1785. sit_blk = (struct f2fs_sit_block *)page_address(page);
  1786. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  1787. f2fs_put_page(page, 1);
  1788. got_it:
  1789. check_block_count(sbi, start, &sit);
  1790. seg_info_from_raw_sit(se, &sit);
  1791. /* build discard map only one time */
  1792. memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
  1793. sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks;
  1794. if (sbi->segs_per_sec > 1) {
  1795. struct sec_entry *e = get_sec_entry(sbi, start);
  1796. e->valid_blocks += se->valid_blocks;
  1797. }
  1798. }
  1799. start_blk += readed;
  1800. } while (start_blk < sit_blk_cnt);
  1801. }
  1802. static void init_free_segmap(struct f2fs_sb_info *sbi)
  1803. {
  1804. unsigned int start;
  1805. int type;
  1806. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1807. struct seg_entry *sentry = get_seg_entry(sbi, start);
  1808. if (!sentry->valid_blocks)
  1809. __set_free(sbi, start);
  1810. }
  1811. /* set use the current segments */
  1812. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  1813. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  1814. __set_test_and_inuse(sbi, curseg_t->segno);
  1815. }
  1816. }
  1817. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  1818. {
  1819. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1820. struct free_segmap_info *free_i = FREE_I(sbi);
  1821. unsigned int segno = 0, offset = 0;
  1822. unsigned short valid_blocks;
  1823. while (1) {
  1824. /* find dirty segment based on free segmap */
  1825. segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
  1826. if (segno >= MAIN_SEGS(sbi))
  1827. break;
  1828. offset = segno + 1;
  1829. valid_blocks = get_valid_blocks(sbi, segno, 0);
  1830. if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
  1831. continue;
  1832. if (valid_blocks > sbi->blocks_per_seg) {
  1833. f2fs_bug_on(sbi, 1);
  1834. continue;
  1835. }
  1836. mutex_lock(&dirty_i->seglist_lock);
  1837. __locate_dirty_segment(sbi, segno, DIRTY);
  1838. mutex_unlock(&dirty_i->seglist_lock);
  1839. }
  1840. }
  1841. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  1842. {
  1843. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1844. unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  1845. dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  1846. if (!dirty_i->victim_secmap)
  1847. return -ENOMEM;
  1848. return 0;
  1849. }
  1850. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  1851. {
  1852. struct dirty_seglist_info *dirty_i;
  1853. unsigned int bitmap_size, i;
  1854. /* allocate memory for dirty segments list information */
  1855. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  1856. if (!dirty_i)
  1857. return -ENOMEM;
  1858. SM_I(sbi)->dirty_info = dirty_i;
  1859. mutex_init(&dirty_i->seglist_lock);
  1860. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1861. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  1862. dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  1863. if (!dirty_i->dirty_segmap[i])
  1864. return -ENOMEM;
  1865. }
  1866. init_dirty_segmap(sbi);
  1867. return init_victim_secmap(sbi);
  1868. }
  1869. /*
  1870. * Update min, max modified time for cost-benefit GC algorithm
  1871. */
  1872. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  1873. {
  1874. struct sit_info *sit_i = SIT_I(sbi);
  1875. unsigned int segno;
  1876. mutex_lock(&sit_i->sentry_lock);
  1877. sit_i->min_mtime = LLONG_MAX;
  1878. for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
  1879. unsigned int i;
  1880. unsigned long long mtime = 0;
  1881. for (i = 0; i < sbi->segs_per_sec; i++)
  1882. mtime += get_seg_entry(sbi, segno + i)->mtime;
  1883. mtime = div_u64(mtime, sbi->segs_per_sec);
  1884. if (sit_i->min_mtime > mtime)
  1885. sit_i->min_mtime = mtime;
  1886. }
  1887. sit_i->max_mtime = get_mtime(sbi);
  1888. mutex_unlock(&sit_i->sentry_lock);
  1889. }
  1890. int build_segment_manager(struct f2fs_sb_info *sbi)
  1891. {
  1892. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1893. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1894. struct f2fs_sm_info *sm_info;
  1895. int err;
  1896. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  1897. if (!sm_info)
  1898. return -ENOMEM;
  1899. /* init sm info */
  1900. sbi->sm_info = sm_info;
  1901. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1902. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1903. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  1904. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1905. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1906. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  1907. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1908. sm_info->rec_prefree_segments = sm_info->main_segments *
  1909. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  1910. sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
  1911. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  1912. sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
  1913. INIT_LIST_HEAD(&sm_info->discard_list);
  1914. sm_info->nr_discards = 0;
  1915. sm_info->max_discards = 0;
  1916. sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
  1917. INIT_LIST_HEAD(&sm_info->sit_entry_set);
  1918. if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
  1919. err = create_flush_cmd_control(sbi);
  1920. if (err)
  1921. return err;
  1922. }
  1923. err = build_sit_info(sbi);
  1924. if (err)
  1925. return err;
  1926. err = build_free_segmap(sbi);
  1927. if (err)
  1928. return err;
  1929. err = build_curseg(sbi);
  1930. if (err)
  1931. return err;
  1932. /* reinit free segmap based on SIT */
  1933. build_sit_entries(sbi);
  1934. init_free_segmap(sbi);
  1935. err = build_dirty_segmap(sbi);
  1936. if (err)
  1937. return err;
  1938. init_min_max_mtime(sbi);
  1939. return 0;
  1940. }
  1941. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  1942. enum dirty_type dirty_type)
  1943. {
  1944. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1945. mutex_lock(&dirty_i->seglist_lock);
  1946. kvfree(dirty_i->dirty_segmap[dirty_type]);
  1947. dirty_i->nr_dirty[dirty_type] = 0;
  1948. mutex_unlock(&dirty_i->seglist_lock);
  1949. }
  1950. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  1951. {
  1952. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1953. kvfree(dirty_i->victim_secmap);
  1954. }
  1955. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  1956. {
  1957. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1958. int i;
  1959. if (!dirty_i)
  1960. return;
  1961. /* discard pre-free/dirty segments list */
  1962. for (i = 0; i < NR_DIRTY_TYPE; i++)
  1963. discard_dirty_segmap(sbi, i);
  1964. destroy_victim_secmap(sbi);
  1965. SM_I(sbi)->dirty_info = NULL;
  1966. kfree(dirty_i);
  1967. }
  1968. static void destroy_curseg(struct f2fs_sb_info *sbi)
  1969. {
  1970. struct curseg_info *array = SM_I(sbi)->curseg_array;
  1971. int i;
  1972. if (!array)
  1973. return;
  1974. SM_I(sbi)->curseg_array = NULL;
  1975. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1976. kfree(array[i].sum_blk);
  1977. kfree(array);
  1978. }
  1979. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  1980. {
  1981. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  1982. if (!free_i)
  1983. return;
  1984. SM_I(sbi)->free_info = NULL;
  1985. kvfree(free_i->free_segmap);
  1986. kvfree(free_i->free_secmap);
  1987. kfree(free_i);
  1988. }
  1989. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  1990. {
  1991. struct sit_info *sit_i = SIT_I(sbi);
  1992. unsigned int start;
  1993. if (!sit_i)
  1994. return;
  1995. if (sit_i->sentries) {
  1996. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1997. kfree(sit_i->sentries[start].cur_valid_map);
  1998. kfree(sit_i->sentries[start].ckpt_valid_map);
  1999. kfree(sit_i->sentries[start].discard_map);
  2000. }
  2001. }
  2002. kfree(sit_i->tmp_map);
  2003. kvfree(sit_i->sentries);
  2004. kvfree(sit_i->sec_entries);
  2005. kvfree(sit_i->dirty_sentries_bitmap);
  2006. SM_I(sbi)->sit_info = NULL;
  2007. kfree(sit_i->sit_bitmap);
  2008. kfree(sit_i);
  2009. }
  2010. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  2011. {
  2012. struct f2fs_sm_info *sm_info = SM_I(sbi);
  2013. if (!sm_info)
  2014. return;
  2015. destroy_flush_cmd_control(sbi);
  2016. destroy_dirty_segmap(sbi);
  2017. destroy_curseg(sbi);
  2018. destroy_free_segmap(sbi);
  2019. destroy_sit_info(sbi);
  2020. sbi->sm_info = NULL;
  2021. kfree(sm_info);
  2022. }
  2023. int __init create_segment_manager_caches(void)
  2024. {
  2025. discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
  2026. sizeof(struct discard_entry));
  2027. if (!discard_entry_slab)
  2028. goto fail;
  2029. sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
  2030. sizeof(struct sit_entry_set));
  2031. if (!sit_entry_set_slab)
  2032. goto destory_discard_entry;
  2033. inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
  2034. sizeof(struct inmem_pages));
  2035. if (!inmem_entry_slab)
  2036. goto destroy_sit_entry_set;
  2037. return 0;
  2038. destroy_sit_entry_set:
  2039. kmem_cache_destroy(sit_entry_set_slab);
  2040. destory_discard_entry:
  2041. kmem_cache_destroy(discard_entry_slab);
  2042. fail:
  2043. return -ENOMEM;
  2044. }
  2045. void destroy_segment_manager_caches(void)
  2046. {
  2047. kmem_cache_destroy(sit_entry_set_slab);
  2048. kmem_cache_destroy(discard_entry_slab);
  2049. kmem_cache_destroy(inmem_entry_slab);
  2050. }