gc.c 22 KB

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