gc.c 17 KB

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