segment.c 58 KB

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