segment.c 63 KB

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