segment.c 59 KB

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