segment.c 52 KB

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