segment.c 61 KB

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