gc.c 17 KB

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