segment.c 61 KB

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