gc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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 int gc_thread_func(void *data)
  26. {
  27. struct f2fs_sb_info *sbi = data;
  28. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  29. wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
  30. long wait_ms;
  31. wait_ms = gc_th->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. increase_sleep_time(gc_th, &wait_ms);
  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 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. increase_sleep_time(gc_th, &wait_ms);
  62. mutex_unlock(&sbi->gc_mutex);
  63. continue;
  64. }
  65. if (has_enough_invalid_blocks(sbi))
  66. decrease_sleep_time(gc_th, &wait_ms);
  67. else
  68. increase_sleep_time(gc_th, &wait_ms);
  69. stat_inc_bggc_count(sbi);
  70. /* if return value is not zero, no victim was selected */
  71. if (f2fs_gc(sbi, test_opt(sbi, FORCE_FG_GC)))
  72. wait_ms = gc_th->no_gc_sleep_time;
  73. trace_f2fs_background_gc(sbi->sb, wait_ms,
  74. prefree_segments(sbi), free_segments(sbi));
  75. /* balancing f2fs's metadata periodically */
  76. f2fs_balance_fs_bg(sbi);
  77. } while (!kthread_should_stop());
  78. return 0;
  79. }
  80. int start_gc_thread(struct f2fs_sb_info *sbi)
  81. {
  82. struct f2fs_gc_kthread *gc_th;
  83. dev_t dev = sbi->sb->s_bdev->bd_dev;
  84. int err = 0;
  85. gc_th = kmalloc(sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
  86. if (!gc_th) {
  87. err = -ENOMEM;
  88. goto out;
  89. }
  90. gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME;
  91. gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
  92. gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
  93. gc_th->gc_idle = 0;
  94. sbi->gc_thread = gc_th;
  95. init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
  96. sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
  97. "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
  98. if (IS_ERR(gc_th->f2fs_gc_task)) {
  99. err = PTR_ERR(gc_th->f2fs_gc_task);
  100. kfree(gc_th);
  101. sbi->gc_thread = NULL;
  102. }
  103. out:
  104. return err;
  105. }
  106. void stop_gc_thread(struct f2fs_sb_info *sbi)
  107. {
  108. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  109. if (!gc_th)
  110. return;
  111. kthread_stop(gc_th->f2fs_gc_task);
  112. kfree(gc_th);
  113. sbi->gc_thread = NULL;
  114. }
  115. static int select_gc_type(struct f2fs_gc_kthread *gc_th, int gc_type)
  116. {
  117. int gc_mode = (gc_type == BG_GC) ? GC_CB : GC_GREEDY;
  118. if (gc_th && gc_th->gc_idle) {
  119. if (gc_th->gc_idle == 1)
  120. gc_mode = GC_CB;
  121. else if (gc_th->gc_idle == 2)
  122. gc_mode = GC_GREEDY;
  123. }
  124. return gc_mode;
  125. }
  126. static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
  127. int type, struct victim_sel_policy *p)
  128. {
  129. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  130. if (p->alloc_mode == SSR) {
  131. p->gc_mode = GC_GREEDY;
  132. p->dirty_segmap = dirty_i->dirty_segmap[type];
  133. p->max_search = dirty_i->nr_dirty[type];
  134. p->ofs_unit = 1;
  135. } else {
  136. p->gc_mode = select_gc_type(sbi->gc_thread, gc_type);
  137. p->dirty_segmap = dirty_i->dirty_segmap[DIRTY];
  138. p->max_search = dirty_i->nr_dirty[DIRTY];
  139. p->ofs_unit = sbi->segs_per_sec;
  140. }
  141. if (p->max_search > sbi->max_victim_search)
  142. p->max_search = sbi->max_victim_search;
  143. p->offset = sbi->last_victim[p->gc_mode];
  144. }
  145. static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
  146. struct victim_sel_policy *p)
  147. {
  148. /* SSR allocates in a segment unit */
  149. if (p->alloc_mode == SSR)
  150. return 1 << sbi->log_blocks_per_seg;
  151. if (p->gc_mode == GC_GREEDY)
  152. return (1 << sbi->log_blocks_per_seg) * p->ofs_unit;
  153. else if (p->gc_mode == GC_CB)
  154. return UINT_MAX;
  155. else /* No other gc_mode */
  156. return 0;
  157. }
  158. static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
  159. {
  160. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  161. unsigned int secno;
  162. /*
  163. * If the gc_type is FG_GC, we can select victim segments
  164. * selected by background GC before.
  165. * Those segments guarantee they have small valid blocks.
  166. */
  167. for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
  168. if (sec_usage_check(sbi, secno))
  169. continue;
  170. clear_bit(secno, dirty_i->victim_secmap);
  171. return secno * sbi->segs_per_sec;
  172. }
  173. return NULL_SEGNO;
  174. }
  175. static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
  176. {
  177. struct sit_info *sit_i = SIT_I(sbi);
  178. unsigned int secno = GET_SECNO(sbi, segno);
  179. unsigned int start = secno * sbi->segs_per_sec;
  180. unsigned long long mtime = 0;
  181. unsigned int vblocks;
  182. unsigned char age = 0;
  183. unsigned char u;
  184. unsigned int i;
  185. for (i = 0; i < sbi->segs_per_sec; i++)
  186. mtime += get_seg_entry(sbi, start + i)->mtime;
  187. vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec);
  188. mtime = div_u64(mtime, sbi->segs_per_sec);
  189. vblocks = div_u64(vblocks, sbi->segs_per_sec);
  190. u = (vblocks * 100) >> sbi->log_blocks_per_seg;
  191. /* Handle if the system time has changed by the user */
  192. if (mtime < sit_i->min_mtime)
  193. sit_i->min_mtime = mtime;
  194. if (mtime > sit_i->max_mtime)
  195. sit_i->max_mtime = mtime;
  196. if (sit_i->max_mtime != sit_i->min_mtime)
  197. age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime),
  198. sit_i->max_mtime - sit_i->min_mtime);
  199. return UINT_MAX - ((100 * (100 - u) * age) / (100 + u));
  200. }
  201. static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi,
  202. unsigned int segno, struct victim_sel_policy *p)
  203. {
  204. if (p->alloc_mode == SSR)
  205. return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  206. /* alloc_mode == LFS */
  207. if (p->gc_mode == GC_GREEDY)
  208. return get_valid_blocks(sbi, segno, sbi->segs_per_sec);
  209. else
  210. return get_cb_cost(sbi, segno);
  211. }
  212. /*
  213. * This function is called from two paths.
  214. * One is garbage collection and the other is SSR segment selection.
  215. * When it is called during GC, it just gets a victim segment
  216. * and it does not remove it from dirty seglist.
  217. * When it is called from SSR segment selection, it finds a segment
  218. * which has minimum valid blocks and removes it from dirty seglist.
  219. */
  220. static int get_victim_by_default(struct f2fs_sb_info *sbi,
  221. unsigned int *result, int gc_type, int type, char alloc_mode)
  222. {
  223. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  224. struct victim_sel_policy p;
  225. unsigned int secno, max_cost;
  226. unsigned int last_segment = MAIN_SEGS(sbi);
  227. int nsearched = 0;
  228. mutex_lock(&dirty_i->seglist_lock);
  229. p.alloc_mode = alloc_mode;
  230. select_policy(sbi, gc_type, type, &p);
  231. p.min_segno = NULL_SEGNO;
  232. p.min_cost = max_cost = get_max_cost(sbi, &p);
  233. if (p.max_search == 0)
  234. goto out;
  235. if (p.alloc_mode == LFS && gc_type == FG_GC) {
  236. p.min_segno = check_bg_victims(sbi);
  237. if (p.min_segno != NULL_SEGNO)
  238. goto got_it;
  239. }
  240. while (1) {
  241. unsigned long cost;
  242. unsigned int segno;
  243. segno = find_next_bit(p.dirty_segmap, last_segment, p.offset);
  244. if (segno >= last_segment) {
  245. if (sbi->last_victim[p.gc_mode]) {
  246. last_segment = sbi->last_victim[p.gc_mode];
  247. sbi->last_victim[p.gc_mode] = 0;
  248. p.offset = 0;
  249. continue;
  250. }
  251. break;
  252. }
  253. p.offset = segno + p.ofs_unit;
  254. if (p.ofs_unit > 1)
  255. p.offset -= segno % p.ofs_unit;
  256. secno = GET_SECNO(sbi, segno);
  257. if (sec_usage_check(sbi, secno))
  258. continue;
  259. if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
  260. continue;
  261. cost = get_gc_cost(sbi, segno, &p);
  262. if (p.min_cost > cost) {
  263. p.min_segno = segno;
  264. p.min_cost = cost;
  265. } else if (unlikely(cost == max_cost)) {
  266. continue;
  267. }
  268. if (nsearched++ >= p.max_search) {
  269. sbi->last_victim[p.gc_mode] = segno;
  270. break;
  271. }
  272. }
  273. if (p.min_segno != NULL_SEGNO) {
  274. got_it:
  275. if (p.alloc_mode == LFS) {
  276. secno = GET_SECNO(sbi, p.min_segno);
  277. if (gc_type == FG_GC)
  278. sbi->cur_victim_sec = secno;
  279. else
  280. set_bit(secno, dirty_i->victim_secmap);
  281. }
  282. *result = (p.min_segno / p.ofs_unit) * p.ofs_unit;
  283. trace_f2fs_get_victim(sbi->sb, type, gc_type, &p,
  284. sbi->cur_victim_sec,
  285. prefree_segments(sbi), free_segments(sbi));
  286. }
  287. out:
  288. mutex_unlock(&dirty_i->seglist_lock);
  289. return (p.min_segno == NULL_SEGNO) ? 0 : 1;
  290. }
  291. static const struct victim_selection default_v_ops = {
  292. .get_victim = get_victim_by_default,
  293. };
  294. static struct inode *find_gc_inode(struct gc_inode_list *gc_list, nid_t ino)
  295. {
  296. struct inode_entry *ie;
  297. ie = radix_tree_lookup(&gc_list->iroot, ino);
  298. if (ie)
  299. return ie->inode;
  300. return NULL;
  301. }
  302. static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode)
  303. {
  304. struct inode_entry *new_ie;
  305. if (inode == find_gc_inode(gc_list, inode->i_ino)) {
  306. iput(inode);
  307. return;
  308. }
  309. new_ie = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
  310. new_ie->inode = inode;
  311. f2fs_radix_tree_insert(&gc_list->iroot, inode->i_ino, new_ie);
  312. list_add_tail(&new_ie->list, &gc_list->ilist);
  313. }
  314. static void put_gc_inode(struct gc_inode_list *gc_list)
  315. {
  316. struct inode_entry *ie, *next_ie;
  317. list_for_each_entry_safe(ie, next_ie, &gc_list->ilist, list) {
  318. radix_tree_delete(&gc_list->iroot, ie->inode->i_ino);
  319. iput(ie->inode);
  320. list_del(&ie->list);
  321. kmem_cache_free(inode_entry_slab, ie);
  322. }
  323. }
  324. static int check_valid_map(struct f2fs_sb_info *sbi,
  325. unsigned int segno, int offset)
  326. {
  327. struct sit_info *sit_i = SIT_I(sbi);
  328. struct seg_entry *sentry;
  329. int ret;
  330. mutex_lock(&sit_i->sentry_lock);
  331. sentry = get_seg_entry(sbi, segno);
  332. ret = f2fs_test_bit(offset, sentry->cur_valid_map);
  333. mutex_unlock(&sit_i->sentry_lock);
  334. return ret;
  335. }
  336. /*
  337. * This function compares node address got in summary with that in NAT.
  338. * On validity, copy that node with cold status, otherwise (invalid node)
  339. * ignore that.
  340. */
  341. static int gc_node_segment(struct f2fs_sb_info *sbi,
  342. struct f2fs_summary *sum, unsigned int segno, int gc_type)
  343. {
  344. bool initial = true;
  345. struct f2fs_summary *entry;
  346. block_t start_addr;
  347. int off;
  348. start_addr = START_BLOCK(sbi, segno);
  349. next_step:
  350. entry = sum;
  351. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  352. nid_t nid = le32_to_cpu(entry->nid);
  353. struct page *node_page;
  354. struct node_info ni;
  355. /* stop BG_GC if there is not enough free sections. */
  356. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0))
  357. return 0;
  358. if (check_valid_map(sbi, segno, off) == 0)
  359. continue;
  360. if (initial) {
  361. ra_node_page(sbi, nid);
  362. continue;
  363. }
  364. node_page = get_node_page(sbi, nid);
  365. if (IS_ERR(node_page))
  366. continue;
  367. /* block may become invalid during get_node_page */
  368. if (check_valid_map(sbi, segno, off) == 0) {
  369. f2fs_put_page(node_page, 1);
  370. continue;
  371. }
  372. get_node_info(sbi, nid, &ni);
  373. if (ni.blk_addr != start_addr + off) {
  374. f2fs_put_page(node_page, 1);
  375. continue;
  376. }
  377. /* set page dirty and write it */
  378. if (gc_type == FG_GC) {
  379. f2fs_wait_on_page_writeback(node_page, NODE);
  380. set_page_dirty(node_page);
  381. } else {
  382. if (!PageWriteback(node_page))
  383. set_page_dirty(node_page);
  384. }
  385. f2fs_put_page(node_page, 1);
  386. stat_inc_node_blk_count(sbi, 1, gc_type);
  387. }
  388. if (initial) {
  389. initial = false;
  390. goto next_step;
  391. }
  392. if (gc_type == FG_GC) {
  393. struct writeback_control wbc = {
  394. .sync_mode = WB_SYNC_ALL,
  395. .nr_to_write = LONG_MAX,
  396. .for_reclaim = 0,
  397. };
  398. sync_node_pages(sbi, 0, &wbc);
  399. /* return 1 only if FG_GC succefully reclaimed one */
  400. if (get_valid_blocks(sbi, segno, 1) == 0)
  401. return 1;
  402. }
  403. return 0;
  404. }
  405. /*
  406. * Calculate start block index indicating the given node offset.
  407. * Be careful, caller should give this node offset only indicating direct node
  408. * blocks. If any node offsets, which point the other types of node blocks such
  409. * as indirect or double indirect node blocks, are given, it must be a caller's
  410. * bug.
  411. */
  412. block_t start_bidx_of_node(unsigned int node_ofs, struct f2fs_inode_info *fi)
  413. {
  414. unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
  415. unsigned int bidx;
  416. if (node_ofs == 0)
  417. return 0;
  418. if (node_ofs <= 2) {
  419. bidx = node_ofs - 1;
  420. } else if (node_ofs <= indirect_blks) {
  421. int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
  422. bidx = node_ofs - 2 - dec;
  423. } else {
  424. int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
  425. bidx = node_ofs - 5 - dec;
  426. }
  427. return bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE(fi);
  428. }
  429. static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  430. struct node_info *dni, block_t blkaddr, unsigned int *nofs)
  431. {
  432. struct page *node_page;
  433. nid_t nid;
  434. unsigned int ofs_in_node;
  435. block_t source_blkaddr;
  436. nid = le32_to_cpu(sum->nid);
  437. ofs_in_node = le16_to_cpu(sum->ofs_in_node);
  438. node_page = get_node_page(sbi, nid);
  439. if (IS_ERR(node_page))
  440. return false;
  441. get_node_info(sbi, nid, dni);
  442. if (sum->version != dni->version) {
  443. f2fs_msg(sbi->sb, KERN_WARNING,
  444. "%s: valid data with mismatched node version.",
  445. __func__);
  446. set_sbi_flag(sbi, SBI_NEED_FSCK);
  447. }
  448. *nofs = ofs_of_node(node_page);
  449. source_blkaddr = datablock_addr(node_page, ofs_in_node);
  450. f2fs_put_page(node_page, 1);
  451. if (source_blkaddr != blkaddr)
  452. return false;
  453. return true;
  454. }
  455. static void move_encrypted_block(struct inode *inode, block_t bidx)
  456. {
  457. struct f2fs_io_info fio = {
  458. .sbi = F2FS_I_SB(inode),
  459. .type = DATA,
  460. .rw = READ_SYNC,
  461. .encrypted_page = NULL,
  462. };
  463. struct dnode_of_data dn;
  464. struct f2fs_summary sum;
  465. struct node_info ni;
  466. struct page *page;
  467. int err;
  468. /* do not read out */
  469. page = f2fs_grab_cache_page(inode->i_mapping, bidx, false);
  470. if (!page)
  471. return;
  472. set_new_dnode(&dn, inode, NULL, NULL, 0);
  473. err = get_dnode_of_data(&dn, bidx, LOOKUP_NODE);
  474. if (err)
  475. goto out;
  476. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  477. ClearPageUptodate(page);
  478. goto put_out;
  479. }
  480. /*
  481. * don't cache encrypted data into meta inode until previous dirty
  482. * data were writebacked to avoid racing between GC and flush.
  483. */
  484. f2fs_wait_on_page_writeback(page, DATA);
  485. get_node_info(fio.sbi, dn.nid, &ni);
  486. set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
  487. /* read page */
  488. fio.page = page;
  489. fio.blk_addr = dn.data_blkaddr;
  490. fio.encrypted_page = pagecache_get_page(META_MAPPING(fio.sbi),
  491. fio.blk_addr,
  492. FGP_LOCK|FGP_CREAT,
  493. GFP_NOFS);
  494. if (!fio.encrypted_page)
  495. goto put_out;
  496. err = f2fs_submit_page_bio(&fio);
  497. if (err)
  498. goto put_page_out;
  499. /* write page */
  500. lock_page(fio.encrypted_page);
  501. if (unlikely(!PageUptodate(fio.encrypted_page)))
  502. goto put_page_out;
  503. if (unlikely(fio.encrypted_page->mapping != META_MAPPING(fio.sbi)))
  504. goto put_page_out;
  505. set_page_dirty(fio.encrypted_page);
  506. f2fs_wait_on_page_writeback(fio.encrypted_page, DATA);
  507. if (clear_page_dirty_for_io(fio.encrypted_page))
  508. dec_page_count(fio.sbi, F2FS_DIRTY_META);
  509. set_page_writeback(fio.encrypted_page);
  510. /* allocate block address */
  511. f2fs_wait_on_page_writeback(dn.node_page, NODE);
  512. allocate_data_block(fio.sbi, NULL, fio.blk_addr,
  513. &fio.blk_addr, &sum, CURSEG_COLD_DATA);
  514. fio.rw = WRITE_SYNC;
  515. f2fs_submit_page_mbio(&fio);
  516. dn.data_blkaddr = fio.blk_addr;
  517. set_data_blkaddr(&dn);
  518. f2fs_update_extent_cache(&dn);
  519. set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
  520. if (page->index == 0)
  521. set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
  522. put_page_out:
  523. f2fs_put_page(fio.encrypted_page, 1);
  524. put_out:
  525. f2fs_put_dnode(&dn);
  526. out:
  527. f2fs_put_page(page, 1);
  528. }
  529. static void move_data_page(struct inode *inode, block_t bidx, int gc_type)
  530. {
  531. struct page *page;
  532. page = get_lock_data_page(inode, bidx, true);
  533. if (IS_ERR(page))
  534. return;
  535. if (gc_type == BG_GC) {
  536. if (PageWriteback(page))
  537. goto out;
  538. set_page_dirty(page);
  539. set_cold_data(page);
  540. } else {
  541. struct f2fs_io_info fio = {
  542. .sbi = F2FS_I_SB(inode),
  543. .type = DATA,
  544. .rw = WRITE_SYNC,
  545. .page = page,
  546. .encrypted_page = NULL,
  547. };
  548. set_page_dirty(page);
  549. f2fs_wait_on_page_writeback(page, DATA);
  550. if (clear_page_dirty_for_io(page))
  551. inode_dec_dirty_pages(inode);
  552. set_cold_data(page);
  553. do_write_data_page(&fio);
  554. clear_cold_data(page);
  555. }
  556. out:
  557. f2fs_put_page(page, 1);
  558. }
  559. /*
  560. * This function tries to get parent node of victim data block, and identifies
  561. * data block validity. If the block is valid, copy that with cold status and
  562. * modify parent node.
  563. * If the parent node is not valid or the data block address is different,
  564. * the victim data block is ignored.
  565. */
  566. static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  567. struct gc_inode_list *gc_list, unsigned int segno, int gc_type)
  568. {
  569. struct super_block *sb = sbi->sb;
  570. struct f2fs_summary *entry;
  571. block_t start_addr;
  572. int off;
  573. int phase = 0;
  574. start_addr = START_BLOCK(sbi, segno);
  575. next_step:
  576. entry = sum;
  577. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  578. struct page *data_page;
  579. struct inode *inode;
  580. struct node_info dni; /* dnode info for the data */
  581. unsigned int ofs_in_node, nofs;
  582. block_t start_bidx;
  583. /* stop BG_GC if there is not enough free sections. */
  584. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0))
  585. return 0;
  586. if (check_valid_map(sbi, segno, off) == 0)
  587. continue;
  588. if (phase == 0) {
  589. ra_node_page(sbi, le32_to_cpu(entry->nid));
  590. continue;
  591. }
  592. /* Get an inode by ino with checking validity */
  593. if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs))
  594. continue;
  595. if (phase == 1) {
  596. ra_node_page(sbi, dni.ino);
  597. continue;
  598. }
  599. ofs_in_node = le16_to_cpu(entry->ofs_in_node);
  600. if (phase == 2) {
  601. inode = f2fs_iget(sb, dni.ino);
  602. if (IS_ERR(inode) || is_bad_inode(inode))
  603. continue;
  604. /* if encrypted inode, let's go phase 3 */
  605. if (f2fs_encrypted_inode(inode) &&
  606. S_ISREG(inode->i_mode)) {
  607. add_gc_inode(gc_list, inode);
  608. continue;
  609. }
  610. start_bidx = start_bidx_of_node(nofs, F2FS_I(inode));
  611. data_page = get_read_data_page(inode,
  612. start_bidx + ofs_in_node, READA, true);
  613. if (IS_ERR(data_page)) {
  614. iput(inode);
  615. continue;
  616. }
  617. f2fs_put_page(data_page, 0);
  618. add_gc_inode(gc_list, inode);
  619. continue;
  620. }
  621. /* phase 3 */
  622. inode = find_gc_inode(gc_list, dni.ino);
  623. if (inode) {
  624. start_bidx = start_bidx_of_node(nofs, F2FS_I(inode))
  625. + ofs_in_node;
  626. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  627. move_encrypted_block(inode, start_bidx);
  628. else
  629. move_data_page(inode, start_bidx, gc_type);
  630. stat_inc_data_blk_count(sbi, 1, gc_type);
  631. }
  632. }
  633. if (++phase < 4)
  634. goto next_step;
  635. if (gc_type == FG_GC) {
  636. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  637. /* return 1 only if FG_GC succefully reclaimed one */
  638. if (get_valid_blocks(sbi, segno, 1) == 0)
  639. return 1;
  640. }
  641. return 0;
  642. }
  643. static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
  644. int gc_type)
  645. {
  646. struct sit_info *sit_i = SIT_I(sbi);
  647. int ret;
  648. mutex_lock(&sit_i->sentry_lock);
  649. ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type,
  650. NO_CHECK_TYPE, LFS);
  651. mutex_unlock(&sit_i->sentry_lock);
  652. return ret;
  653. }
  654. static int do_garbage_collect(struct f2fs_sb_info *sbi, unsigned int segno,
  655. struct gc_inode_list *gc_list, int gc_type)
  656. {
  657. struct page *sum_page;
  658. struct f2fs_summary_block *sum;
  659. struct blk_plug plug;
  660. int nfree = 0;
  661. /* read segment summary of victim */
  662. sum_page = get_sum_page(sbi, segno);
  663. blk_start_plug(&plug);
  664. sum = page_address(sum_page);
  665. /*
  666. * this is to avoid deadlock:
  667. * - lock_page(sum_page) - f2fs_replace_block
  668. * - check_valid_map() - mutex_lock(sentry_lock)
  669. * - mutex_lock(sentry_lock) - change_curseg()
  670. * - lock_page(sum_page)
  671. */
  672. unlock_page(sum_page);
  673. switch (GET_SUM_TYPE((&sum->footer))) {
  674. case SUM_TYPE_NODE:
  675. nfree = gc_node_segment(sbi, sum->entries, segno, gc_type);
  676. break;
  677. case SUM_TYPE_DATA:
  678. nfree = gc_data_segment(sbi, sum->entries, gc_list,
  679. segno, gc_type);
  680. break;
  681. }
  682. blk_finish_plug(&plug);
  683. stat_inc_seg_count(sbi, GET_SUM_TYPE((&sum->footer)), gc_type);
  684. stat_inc_call_count(sbi->stat_info);
  685. f2fs_put_page(sum_page, 0);
  686. return nfree;
  687. }
  688. int f2fs_gc(struct f2fs_sb_info *sbi, bool sync)
  689. {
  690. unsigned int segno, i;
  691. int gc_type = sync ? FG_GC : BG_GC;
  692. int sec_freed = 0;
  693. int ret = -EINVAL;
  694. struct cp_control cpc;
  695. struct gc_inode_list gc_list = {
  696. .ilist = LIST_HEAD_INIT(gc_list.ilist),
  697. .iroot = RADIX_TREE_INIT(GFP_NOFS),
  698. };
  699. cpc.reason = __get_cp_reason(sbi);
  700. gc_more:
  701. segno = NULL_SEGNO;
  702. if (unlikely(!(sbi->sb->s_flags & MS_ACTIVE)))
  703. goto stop;
  704. if (unlikely(f2fs_cp_error(sbi)))
  705. goto stop;
  706. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, sec_freed)) {
  707. gc_type = FG_GC;
  708. if (__get_victim(sbi, &segno, gc_type) || prefree_segments(sbi))
  709. write_checkpoint(sbi, &cpc);
  710. }
  711. if (segno == NULL_SEGNO && !__get_victim(sbi, &segno, gc_type))
  712. goto stop;
  713. ret = 0;
  714. /* readahead multi ssa blocks those have contiguous address */
  715. if (sbi->segs_per_sec > 1)
  716. ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno), sbi->segs_per_sec,
  717. META_SSA, true);
  718. for (i = 0; i < sbi->segs_per_sec; i++) {
  719. /*
  720. * for FG_GC case, halt gcing left segments once failed one
  721. * of segments in selected section to avoid long latency.
  722. */
  723. if (!do_garbage_collect(sbi, segno + i, &gc_list, gc_type) &&
  724. gc_type == FG_GC)
  725. break;
  726. }
  727. if (i == sbi->segs_per_sec && gc_type == FG_GC)
  728. sec_freed++;
  729. if (gc_type == FG_GC)
  730. sbi->cur_victim_sec = NULL_SEGNO;
  731. if (!sync) {
  732. if (has_not_enough_free_secs(sbi, sec_freed))
  733. goto gc_more;
  734. if (gc_type == FG_GC)
  735. write_checkpoint(sbi, &cpc);
  736. }
  737. stop:
  738. mutex_unlock(&sbi->gc_mutex);
  739. put_gc_inode(&gc_list);
  740. if (sync)
  741. ret = sec_freed ? 0 : -EAGAIN;
  742. return ret;
  743. }
  744. void build_gc_manager(struct f2fs_sb_info *sbi)
  745. {
  746. DIRTY_I(sbi)->v_ops = &default_v_ops;
  747. }