segment.c 63 KB

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