segment.c 59 KB

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