segment.c 61 KB

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