gc.c 18 KB

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