segment.c 62 KB

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