gc.c 17 KB

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