segment.c 62 KB

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