gc.c 17 KB

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