segment.c 63 KB

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