segment.c 61 KB

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