gc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * fs/f2fs/gc.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/module.h>
  13. #include <linux/backing-dev.h>
  14. #include <linux/init.h>
  15. #include <linux/f2fs_fs.h>
  16. #include <linux/kthread.h>
  17. #include <linux/delay.h>
  18. #include <linux/freezer.h>
  19. #include <linux/blkdev.h>
  20. #include "f2fs.h"
  21. #include "node.h"
  22. #include "segment.h"
  23. #include "gc.h"
  24. #include <trace/events/f2fs.h>
  25. static struct kmem_cache *winode_slab;
  26. static int gc_thread_func(void *data)
  27. {
  28. struct f2fs_sb_info *sbi = data;
  29. wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
  30. long wait_ms;
  31. wait_ms = GC_THREAD_MIN_SLEEP_TIME;
  32. do {
  33. if (try_to_freeze())
  34. continue;
  35. else
  36. wait_event_interruptible_timeout(*wq,
  37. kthread_should_stop(),
  38. msecs_to_jiffies(wait_ms));
  39. if (kthread_should_stop())
  40. break;
  41. if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) {
  42. wait_ms = GC_THREAD_MAX_SLEEP_TIME;
  43. continue;
  44. }
  45. /*
  46. * [GC triggering condition]
  47. * 0. GC is not conducted currently.
  48. * 1. There are enough dirty segments.
  49. * 2. IO subsystem is idle by checking the # of writeback pages.
  50. * 3. IO subsystem is idle by checking the # of requests in
  51. * bdev's request list.
  52. *
  53. * Note) We have to avoid triggering GCs too much frequently.
  54. * Because it is possible that some segments can be
  55. * invalidated soon after by user update or deletion.
  56. * So, I'd like to wait some time to collect dirty segments.
  57. */
  58. if (!mutex_trylock(&sbi->gc_mutex))
  59. continue;
  60. if (!is_idle(sbi)) {
  61. wait_ms = increase_sleep_time(wait_ms);
  62. mutex_unlock(&sbi->gc_mutex);
  63. continue;
  64. }
  65. if (has_enough_invalid_blocks(sbi))
  66. wait_ms = decrease_sleep_time(wait_ms);
  67. else
  68. wait_ms = increase_sleep_time(wait_ms);
  69. #ifdef CONFIG_F2FS_STAT_FS
  70. sbi->bg_gc++;
  71. #endif
  72. /* if return value is not zero, no victim was selected */
  73. if (f2fs_gc(sbi))
  74. wait_ms = GC_THREAD_NOGC_SLEEP_TIME;
  75. } while (!kthread_should_stop());
  76. return 0;
  77. }
  78. int start_gc_thread(struct f2fs_sb_info *sbi)
  79. {
  80. struct f2fs_gc_kthread *gc_th;
  81. dev_t dev = sbi->sb->s_bdev->bd_dev;
  82. int err = 0;
  83. if (!test_opt(sbi, BG_GC))
  84. goto out;
  85. gc_th = kmalloc(sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
  86. if (!gc_th) {
  87. err = -ENOMEM;
  88. goto out;
  89. }
  90. sbi->gc_thread = gc_th;
  91. init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
  92. sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
  93. "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
  94. if (IS_ERR(gc_th->f2fs_gc_task)) {
  95. err = PTR_ERR(gc_th->f2fs_gc_task);
  96. kfree(gc_th);
  97. sbi->gc_thread = NULL;
  98. }
  99. out:
  100. return err;
  101. }
  102. void stop_gc_thread(struct f2fs_sb_info *sbi)
  103. {
  104. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  105. if (!gc_th)
  106. return;
  107. kthread_stop(gc_th->f2fs_gc_task);
  108. kfree(gc_th);
  109. sbi->gc_thread = NULL;
  110. }
  111. static int select_gc_type(int gc_type)
  112. {
  113. return (gc_type == BG_GC) ? GC_CB : GC_GREEDY;
  114. }
  115. static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
  116. int type, struct victim_sel_policy *p)
  117. {
  118. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  119. if (p->alloc_mode == SSR) {
  120. p->gc_mode = GC_GREEDY;
  121. p->dirty_segmap = dirty_i->dirty_segmap[type];
  122. p->ofs_unit = 1;
  123. } else {
  124. p->gc_mode = select_gc_type(gc_type);
  125. p->dirty_segmap = dirty_i->dirty_segmap[DIRTY];
  126. p->ofs_unit = sbi->segs_per_sec;
  127. }
  128. p->offset = sbi->last_victim[p->gc_mode];
  129. }
  130. static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
  131. struct victim_sel_policy *p)
  132. {
  133. /* SSR allocates in a segment unit */
  134. if (p->alloc_mode == SSR)
  135. return 1 << sbi->log_blocks_per_seg;
  136. if (p->gc_mode == GC_GREEDY)
  137. return (1 << sbi->log_blocks_per_seg) * p->ofs_unit;
  138. else if (p->gc_mode == GC_CB)
  139. return UINT_MAX;
  140. else /* No other gc_mode */
  141. return 0;
  142. }
  143. static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
  144. {
  145. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  146. unsigned int hint = 0;
  147. unsigned int secno;
  148. /*
  149. * If the gc_type is FG_GC, we can select victim segments
  150. * selected by background GC before.
  151. * Those segments guarantee they have small valid blocks.
  152. */
  153. next:
  154. secno = find_next_bit(dirty_i->victim_secmap, TOTAL_SECS(sbi), hint++);
  155. if (secno < TOTAL_SECS(sbi)) {
  156. if (sec_usage_check(sbi, secno))
  157. goto next;
  158. clear_bit(secno, dirty_i->victim_secmap);
  159. return secno * sbi->segs_per_sec;
  160. }
  161. return NULL_SEGNO;
  162. }
  163. static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
  164. {
  165. struct sit_info *sit_i = SIT_I(sbi);
  166. unsigned int secno = GET_SECNO(sbi, segno);
  167. unsigned int start = secno * sbi->segs_per_sec;
  168. unsigned long long mtime = 0;
  169. unsigned int vblocks;
  170. unsigned char age = 0;
  171. unsigned char u;
  172. unsigned int i;
  173. for (i = 0; i < sbi->segs_per_sec; i++)
  174. mtime += get_seg_entry(sbi, start + i)->mtime;
  175. vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec);
  176. mtime = div_u64(mtime, sbi->segs_per_sec);
  177. vblocks = div_u64(vblocks, sbi->segs_per_sec);
  178. u = (vblocks * 100) >> sbi->log_blocks_per_seg;
  179. /* Handle if the system time is changed by user */
  180. if (mtime < sit_i->min_mtime)
  181. sit_i->min_mtime = mtime;
  182. if (mtime > sit_i->max_mtime)
  183. sit_i->max_mtime = mtime;
  184. if (sit_i->max_mtime != sit_i->min_mtime)
  185. age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime),
  186. sit_i->max_mtime - sit_i->min_mtime);
  187. return UINT_MAX - ((100 * (100 - u) * age) / (100 + u));
  188. }
  189. static unsigned int get_gc_cost(struct f2fs_sb_info *sbi, unsigned int segno,
  190. struct victim_sel_policy *p)
  191. {
  192. if (p->alloc_mode == SSR)
  193. return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  194. /* alloc_mode == LFS */
  195. if (p->gc_mode == GC_GREEDY)
  196. return get_valid_blocks(sbi, segno, sbi->segs_per_sec);
  197. else
  198. return get_cb_cost(sbi, segno);
  199. }
  200. /*
  201. * This function is called from two paths.
  202. * One is garbage collection and the other is SSR segment selection.
  203. * When it is called during GC, it just gets a victim segment
  204. * and it does not remove it from dirty seglist.
  205. * When it is called from SSR segment selection, it finds a segment
  206. * which has minimum valid blocks and removes it from dirty seglist.
  207. */
  208. static int get_victim_by_default(struct f2fs_sb_info *sbi,
  209. unsigned int *result, int gc_type, int type, char alloc_mode)
  210. {
  211. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  212. struct victim_sel_policy p;
  213. unsigned int secno, max_cost;
  214. int nsearched = 0;
  215. p.alloc_mode = alloc_mode;
  216. select_policy(sbi, gc_type, type, &p);
  217. p.min_segno = NULL_SEGNO;
  218. p.min_cost = max_cost = get_max_cost(sbi, &p);
  219. mutex_lock(&dirty_i->seglist_lock);
  220. if (p.alloc_mode == LFS && gc_type == FG_GC) {
  221. p.min_segno = check_bg_victims(sbi);
  222. if (p.min_segno != NULL_SEGNO)
  223. goto got_it;
  224. }
  225. while (1) {
  226. unsigned long cost;
  227. unsigned int segno;
  228. segno = find_next_bit(p.dirty_segmap,
  229. TOTAL_SEGS(sbi), p.offset);
  230. if (segno >= TOTAL_SEGS(sbi)) {
  231. if (sbi->last_victim[p.gc_mode]) {
  232. sbi->last_victim[p.gc_mode] = 0;
  233. p.offset = 0;
  234. continue;
  235. }
  236. break;
  237. }
  238. p.offset = ((segno / p.ofs_unit) * p.ofs_unit) + p.ofs_unit;
  239. secno = GET_SECNO(sbi, segno);
  240. if (sec_usage_check(sbi, secno))
  241. continue;
  242. if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
  243. continue;
  244. cost = get_gc_cost(sbi, segno, &p);
  245. if (p.min_cost > cost) {
  246. p.min_segno = segno;
  247. p.min_cost = cost;
  248. }
  249. if (cost == max_cost)
  250. continue;
  251. if (nsearched++ >= MAX_VICTIM_SEARCH) {
  252. sbi->last_victim[p.gc_mode] = segno;
  253. break;
  254. }
  255. }
  256. if (p.min_segno != NULL_SEGNO) {
  257. got_it:
  258. if (p.alloc_mode == LFS) {
  259. secno = GET_SECNO(sbi, p.min_segno);
  260. if (gc_type == FG_GC)
  261. sbi->cur_victim_sec = secno;
  262. else
  263. set_bit(secno, dirty_i->victim_secmap);
  264. }
  265. *result = (p.min_segno / p.ofs_unit) * p.ofs_unit;
  266. trace_f2fs_get_victim(sbi->sb, type, gc_type, &p,
  267. sbi->cur_victim_sec,
  268. prefree_segments(sbi), free_segments(sbi));
  269. }
  270. mutex_unlock(&dirty_i->seglist_lock);
  271. return (p.min_segno == NULL_SEGNO) ? 0 : 1;
  272. }
  273. static const struct victim_selection default_v_ops = {
  274. .get_victim = get_victim_by_default,
  275. };
  276. static struct inode *find_gc_inode(nid_t ino, struct list_head *ilist)
  277. {
  278. struct list_head *this;
  279. struct inode_entry *ie;
  280. list_for_each(this, ilist) {
  281. ie = list_entry(this, struct inode_entry, list);
  282. if (ie->inode->i_ino == ino)
  283. return ie->inode;
  284. }
  285. return NULL;
  286. }
  287. static void add_gc_inode(struct inode *inode, struct list_head *ilist)
  288. {
  289. struct list_head *this;
  290. struct inode_entry *new_ie, *ie;
  291. list_for_each(this, ilist) {
  292. ie = list_entry(this, struct inode_entry, list);
  293. if (ie->inode == inode) {
  294. iput(inode);
  295. return;
  296. }
  297. }
  298. repeat:
  299. new_ie = kmem_cache_alloc(winode_slab, GFP_NOFS);
  300. if (!new_ie) {
  301. cond_resched();
  302. goto repeat;
  303. }
  304. new_ie->inode = inode;
  305. list_add_tail(&new_ie->list, ilist);
  306. }
  307. static void put_gc_inode(struct list_head *ilist)
  308. {
  309. struct inode_entry *ie, *next_ie;
  310. list_for_each_entry_safe(ie, next_ie, ilist, list) {
  311. iput(ie->inode);
  312. list_del(&ie->list);
  313. kmem_cache_free(winode_slab, ie);
  314. }
  315. }
  316. static int check_valid_map(struct f2fs_sb_info *sbi,
  317. unsigned int segno, int offset)
  318. {
  319. struct sit_info *sit_i = SIT_I(sbi);
  320. struct seg_entry *sentry;
  321. int ret;
  322. mutex_lock(&sit_i->sentry_lock);
  323. sentry = get_seg_entry(sbi, segno);
  324. ret = f2fs_test_bit(offset, sentry->cur_valid_map);
  325. mutex_unlock(&sit_i->sentry_lock);
  326. return ret;
  327. }
  328. /*
  329. * This function compares node address got in summary with that in NAT.
  330. * On validity, copy that node with cold status, otherwise (invalid node)
  331. * ignore that.
  332. */
  333. static void gc_node_segment(struct f2fs_sb_info *sbi,
  334. struct f2fs_summary *sum, unsigned int segno, int gc_type)
  335. {
  336. bool initial = true;
  337. struct f2fs_summary *entry;
  338. int off;
  339. next_step:
  340. entry = sum;
  341. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  342. nid_t nid = le32_to_cpu(entry->nid);
  343. struct page *node_page;
  344. /* stop BG_GC if there is not enough free sections. */
  345. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0))
  346. return;
  347. if (check_valid_map(sbi, segno, off) == 0)
  348. continue;
  349. if (initial) {
  350. ra_node_page(sbi, nid);
  351. continue;
  352. }
  353. node_page = get_node_page(sbi, nid);
  354. if (IS_ERR(node_page))
  355. continue;
  356. /* set page dirty and write it */
  357. if (gc_type == FG_GC) {
  358. f2fs_submit_bio(sbi, NODE, true);
  359. wait_on_page_writeback(node_page);
  360. set_page_dirty(node_page);
  361. } else {
  362. if (!PageWriteback(node_page))
  363. set_page_dirty(node_page);
  364. }
  365. f2fs_put_page(node_page, 1);
  366. stat_inc_node_blk_count(sbi, 1);
  367. }
  368. if (initial) {
  369. initial = false;
  370. goto next_step;
  371. }
  372. if (gc_type == FG_GC) {
  373. struct writeback_control wbc = {
  374. .sync_mode = WB_SYNC_ALL,
  375. .nr_to_write = LONG_MAX,
  376. .for_reclaim = 0,
  377. };
  378. sync_node_pages(sbi, 0, &wbc);
  379. /*
  380. * In the case of FG_GC, it'd be better to reclaim this victim
  381. * completely.
  382. */
  383. if (get_valid_blocks(sbi, segno, 1) != 0)
  384. goto next_step;
  385. }
  386. }
  387. /*
  388. * Calculate start block index indicating the given node offset.
  389. * Be careful, caller should give this node offset only indicating direct node
  390. * blocks. If any node offsets, which point the other types of node blocks such
  391. * as indirect or double indirect node blocks, are given, it must be a caller's
  392. * bug.
  393. */
  394. block_t start_bidx_of_node(unsigned int node_ofs)
  395. {
  396. unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
  397. unsigned int bidx;
  398. if (node_ofs == 0)
  399. return 0;
  400. if (node_ofs <= 2) {
  401. bidx = node_ofs - 1;
  402. } else if (node_ofs <= indirect_blks) {
  403. int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
  404. bidx = node_ofs - 2 - dec;
  405. } else {
  406. int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
  407. bidx = node_ofs - 5 - dec;
  408. }
  409. return bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE;
  410. }
  411. static int check_dnode(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  412. struct node_info *dni, block_t blkaddr, unsigned int *nofs)
  413. {
  414. struct page *node_page;
  415. nid_t nid;
  416. unsigned int ofs_in_node;
  417. block_t source_blkaddr;
  418. nid = le32_to_cpu(sum->nid);
  419. ofs_in_node = le16_to_cpu(sum->ofs_in_node);
  420. node_page = get_node_page(sbi, nid);
  421. if (IS_ERR(node_page))
  422. return 0;
  423. get_node_info(sbi, nid, dni);
  424. if (sum->version != dni->version) {
  425. f2fs_put_page(node_page, 1);
  426. return 0;
  427. }
  428. *nofs = ofs_of_node(node_page);
  429. source_blkaddr = datablock_addr(node_page, ofs_in_node);
  430. f2fs_put_page(node_page, 1);
  431. if (source_blkaddr != blkaddr)
  432. return 0;
  433. return 1;
  434. }
  435. static void move_data_page(struct inode *inode, struct page *page, int gc_type)
  436. {
  437. if (gc_type == BG_GC) {
  438. if (PageWriteback(page))
  439. goto out;
  440. set_page_dirty(page);
  441. set_cold_data(page);
  442. } else {
  443. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  444. if (PageWriteback(page)) {
  445. f2fs_submit_bio(sbi, DATA, true);
  446. wait_on_page_writeback(page);
  447. }
  448. if (clear_page_dirty_for_io(page) &&
  449. S_ISDIR(inode->i_mode)) {
  450. dec_page_count(sbi, F2FS_DIRTY_DENTS);
  451. inode_dec_dirty_dents(inode);
  452. }
  453. set_cold_data(page);
  454. do_write_data_page(page);
  455. clear_cold_data(page);
  456. }
  457. out:
  458. f2fs_put_page(page, 1);
  459. }
  460. /*
  461. * This function tries to get parent node of victim data block, and identifies
  462. * data block validity. If the block is valid, copy that with cold status and
  463. * modify parent node.
  464. * If the parent node is not valid or the data block address is different,
  465. * the victim data block is ignored.
  466. */
  467. static void gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  468. struct list_head *ilist, unsigned int segno, int gc_type)
  469. {
  470. struct super_block *sb = sbi->sb;
  471. struct f2fs_summary *entry;
  472. block_t start_addr;
  473. int off;
  474. int phase = 0;
  475. start_addr = START_BLOCK(sbi, segno);
  476. next_step:
  477. entry = sum;
  478. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  479. struct page *data_page;
  480. struct inode *inode;
  481. struct node_info dni; /* dnode info for the data */
  482. unsigned int ofs_in_node, nofs;
  483. block_t start_bidx;
  484. /* stop BG_GC if there is not enough free sections. */
  485. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0))
  486. return;
  487. if (check_valid_map(sbi, segno, off) == 0)
  488. continue;
  489. if (phase == 0) {
  490. ra_node_page(sbi, le32_to_cpu(entry->nid));
  491. continue;
  492. }
  493. /* Get an inode by ino with checking validity */
  494. if (check_dnode(sbi, entry, &dni, start_addr + off, &nofs) == 0)
  495. continue;
  496. if (phase == 1) {
  497. ra_node_page(sbi, dni.ino);
  498. continue;
  499. }
  500. start_bidx = start_bidx_of_node(nofs);
  501. ofs_in_node = le16_to_cpu(entry->ofs_in_node);
  502. if (phase == 2) {
  503. inode = f2fs_iget(sb, dni.ino);
  504. if (IS_ERR(inode))
  505. continue;
  506. data_page = find_data_page(inode,
  507. start_bidx + ofs_in_node, false);
  508. if (IS_ERR(data_page))
  509. goto next_iput;
  510. f2fs_put_page(data_page, 0);
  511. add_gc_inode(inode, ilist);
  512. } else {
  513. inode = find_gc_inode(dni.ino, ilist);
  514. if (inode) {
  515. data_page = get_lock_data_page(inode,
  516. start_bidx + ofs_in_node);
  517. if (IS_ERR(data_page))
  518. continue;
  519. move_data_page(inode, data_page, gc_type);
  520. stat_inc_data_blk_count(sbi, 1);
  521. }
  522. }
  523. continue;
  524. next_iput:
  525. iput(inode);
  526. }
  527. if (++phase < 4)
  528. goto next_step;
  529. if (gc_type == FG_GC) {
  530. f2fs_submit_bio(sbi, DATA, true);
  531. /*
  532. * In the case of FG_GC, it'd be better to reclaim this victim
  533. * completely.
  534. */
  535. if (get_valid_blocks(sbi, segno, 1) != 0) {
  536. phase = 2;
  537. goto next_step;
  538. }
  539. }
  540. }
  541. static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
  542. int gc_type, int type)
  543. {
  544. struct sit_info *sit_i = SIT_I(sbi);
  545. int ret;
  546. mutex_lock(&sit_i->sentry_lock);
  547. ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type, type, LFS);
  548. mutex_unlock(&sit_i->sentry_lock);
  549. return ret;
  550. }
  551. static void do_garbage_collect(struct f2fs_sb_info *sbi, unsigned int segno,
  552. struct list_head *ilist, int gc_type)
  553. {
  554. struct page *sum_page;
  555. struct f2fs_summary_block *sum;
  556. struct blk_plug plug;
  557. /* read segment summary of victim */
  558. sum_page = get_sum_page(sbi, segno);
  559. if (IS_ERR(sum_page))
  560. return;
  561. blk_start_plug(&plug);
  562. sum = page_address(sum_page);
  563. switch (GET_SUM_TYPE((&sum->footer))) {
  564. case SUM_TYPE_NODE:
  565. gc_node_segment(sbi, sum->entries, segno, gc_type);
  566. break;
  567. case SUM_TYPE_DATA:
  568. gc_data_segment(sbi, sum->entries, ilist, segno, gc_type);
  569. break;
  570. }
  571. blk_finish_plug(&plug);
  572. stat_inc_seg_count(sbi, GET_SUM_TYPE((&sum->footer)));
  573. stat_inc_call_count(sbi->stat_info);
  574. f2fs_put_page(sum_page, 1);
  575. }
  576. int f2fs_gc(struct f2fs_sb_info *sbi)
  577. {
  578. struct list_head ilist;
  579. unsigned int segno, i;
  580. int gc_type = BG_GC;
  581. int nfree = 0;
  582. int ret = -1;
  583. INIT_LIST_HEAD(&ilist);
  584. gc_more:
  585. if (!(sbi->sb->s_flags & MS_ACTIVE))
  586. goto stop;
  587. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, nfree)) {
  588. gc_type = FG_GC;
  589. write_checkpoint(sbi, false);
  590. }
  591. if (!__get_victim(sbi, &segno, gc_type, NO_CHECK_TYPE))
  592. goto stop;
  593. ret = 0;
  594. for (i = 0; i < sbi->segs_per_sec; i++)
  595. do_garbage_collect(sbi, segno + i, &ilist, gc_type);
  596. if (gc_type == FG_GC) {
  597. sbi->cur_victim_sec = NULL_SEGNO;
  598. nfree++;
  599. WARN_ON(get_valid_blocks(sbi, segno, sbi->segs_per_sec));
  600. }
  601. if (has_not_enough_free_secs(sbi, nfree))
  602. goto gc_more;
  603. if (gc_type == FG_GC)
  604. write_checkpoint(sbi, false);
  605. stop:
  606. mutex_unlock(&sbi->gc_mutex);
  607. put_gc_inode(&ilist);
  608. return ret;
  609. }
  610. void build_gc_manager(struct f2fs_sb_info *sbi)
  611. {
  612. DIRTY_I(sbi)->v_ops = &default_v_ops;
  613. }
  614. int __init create_gc_caches(void)
  615. {
  616. winode_slab = f2fs_kmem_cache_create("f2fs_gc_inodes",
  617. sizeof(struct inode_entry), NULL);
  618. if (!winode_slab)
  619. return -ENOMEM;
  620. return 0;
  621. }
  622. void destroy_gc_caches(void)
  623. {
  624. kmem_cache_destroy(winode_slab);
  625. }