segment.c 52 KB

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