gc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. unsigned int wait_ms;
  30. wait_ms = gc_th->min_sleep_time;
  31. set_freezable();
  32. do {
  33. wait_event_interruptible_timeout(*wq,
  34. kthread_should_stop() || freezing(current) ||
  35. gc_th->gc_wake,
  36. msecs_to_jiffies(wait_ms));
  37. /* give it a try one time */
  38. if (gc_th->gc_wake)
  39. gc_th->gc_wake = 0;
  40. if (try_to_freeze())
  41. continue;
  42. if (kthread_should_stop())
  43. break;
  44. if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) {
  45. increase_sleep_time(gc_th, &wait_ms);
  46. continue;
  47. }
  48. #ifdef CONFIG_F2FS_FAULT_INJECTION
  49. if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
  50. f2fs_show_injection_info(FAULT_CHECKPOINT);
  51. f2fs_stop_checkpoint(sbi, false);
  52. }
  53. #endif
  54. if (!sb_start_write_trylock(sbi->sb))
  55. continue;
  56. /*
  57. * [GC triggering condition]
  58. * 0. GC is not conducted currently.
  59. * 1. There are enough dirty segments.
  60. * 2. IO subsystem is idle by checking the # of writeback pages.
  61. * 3. IO subsystem is idle by checking the # of requests in
  62. * bdev's request list.
  63. *
  64. * Note) We have to avoid triggering GCs frequently.
  65. * Because it is possible that some segments can be
  66. * invalidated soon after by user update or deletion.
  67. * So, I'd like to wait some time to collect dirty segments.
  68. */
  69. if (sbi->gc_mode == GC_URGENT) {
  70. wait_ms = gc_th->urgent_sleep_time;
  71. mutex_lock(&sbi->gc_mutex);
  72. goto do_gc;
  73. }
  74. if (!mutex_trylock(&sbi->gc_mutex))
  75. goto next;
  76. if (!is_idle(sbi)) {
  77. increase_sleep_time(gc_th, &wait_ms);
  78. mutex_unlock(&sbi->gc_mutex);
  79. goto next;
  80. }
  81. if (has_enough_invalid_blocks(sbi))
  82. decrease_sleep_time(gc_th, &wait_ms);
  83. else
  84. increase_sleep_time(gc_th, &wait_ms);
  85. do_gc:
  86. stat_inc_bggc_count(sbi);
  87. /* if return value is not zero, no victim was selected */
  88. if (f2fs_gc(sbi, test_opt(sbi, FORCE_FG_GC), true, NULL_SEGNO))
  89. wait_ms = gc_th->no_gc_sleep_time;
  90. trace_f2fs_background_gc(sbi->sb, wait_ms,
  91. prefree_segments(sbi), free_segments(sbi));
  92. /* balancing f2fs's metadata periodically */
  93. f2fs_balance_fs_bg(sbi);
  94. next:
  95. sb_end_write(sbi->sb);
  96. } while (!kthread_should_stop());
  97. return 0;
  98. }
  99. int f2fs_start_gc_thread(struct f2fs_sb_info *sbi)
  100. {
  101. struct f2fs_gc_kthread *gc_th;
  102. dev_t dev = sbi->sb->s_bdev->bd_dev;
  103. int err = 0;
  104. gc_th = f2fs_kmalloc(sbi, sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
  105. if (!gc_th) {
  106. err = -ENOMEM;
  107. goto out;
  108. }
  109. gc_th->urgent_sleep_time = DEF_GC_THREAD_URGENT_SLEEP_TIME;
  110. gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME;
  111. gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
  112. gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
  113. gc_th->gc_wake= 0;
  114. sbi->gc_thread = gc_th;
  115. init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
  116. sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
  117. "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
  118. if (IS_ERR(gc_th->f2fs_gc_task)) {
  119. err = PTR_ERR(gc_th->f2fs_gc_task);
  120. kfree(gc_th);
  121. sbi->gc_thread = NULL;
  122. }
  123. out:
  124. return err;
  125. }
  126. void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi)
  127. {
  128. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  129. if (!gc_th)
  130. return;
  131. kthread_stop(gc_th->f2fs_gc_task);
  132. kfree(gc_th);
  133. sbi->gc_thread = NULL;
  134. }
  135. static int select_gc_type(struct f2fs_sb_info *sbi, int gc_type)
  136. {
  137. int gc_mode = (gc_type == BG_GC) ? GC_CB : GC_GREEDY;
  138. switch (sbi->gc_mode) {
  139. case GC_IDLE_CB:
  140. gc_mode = GC_CB;
  141. break;
  142. case GC_IDLE_GREEDY:
  143. case GC_URGENT:
  144. gc_mode = GC_GREEDY;
  145. break;
  146. }
  147. return gc_mode;
  148. }
  149. static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
  150. int type, struct victim_sel_policy *p)
  151. {
  152. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  153. if (p->alloc_mode == SSR) {
  154. p->gc_mode = GC_GREEDY;
  155. p->dirty_segmap = dirty_i->dirty_segmap[type];
  156. p->max_search = dirty_i->nr_dirty[type];
  157. p->ofs_unit = 1;
  158. } else {
  159. p->gc_mode = select_gc_type(sbi, gc_type);
  160. p->dirty_segmap = dirty_i->dirty_segmap[DIRTY];
  161. p->max_search = dirty_i->nr_dirty[DIRTY];
  162. p->ofs_unit = sbi->segs_per_sec;
  163. }
  164. /* we need to check every dirty segments in the FG_GC case */
  165. if (gc_type != FG_GC &&
  166. (sbi->gc_mode != GC_URGENT) &&
  167. p->max_search > sbi->max_victim_search)
  168. p->max_search = sbi->max_victim_search;
  169. /* let's select beginning hot/small space first in no_heap mode*/
  170. if (test_opt(sbi, NOHEAP) &&
  171. (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
  172. p->offset = 0;
  173. else
  174. p->offset = SIT_I(sbi)->last_victim[p->gc_mode];
  175. }
  176. static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
  177. struct victim_sel_policy *p)
  178. {
  179. /* SSR allocates in a segment unit */
  180. if (p->alloc_mode == SSR)
  181. return sbi->blocks_per_seg;
  182. if (p->gc_mode == GC_GREEDY)
  183. return 2 * sbi->blocks_per_seg * p->ofs_unit;
  184. else if (p->gc_mode == GC_CB)
  185. return UINT_MAX;
  186. else /* No other gc_mode */
  187. return 0;
  188. }
  189. static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
  190. {
  191. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  192. unsigned int secno;
  193. /*
  194. * If the gc_type is FG_GC, we can select victim segments
  195. * selected by background GC before.
  196. * Those segments guarantee they have small valid blocks.
  197. */
  198. for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
  199. if (sec_usage_check(sbi, secno))
  200. continue;
  201. clear_bit(secno, dirty_i->victim_secmap);
  202. return GET_SEG_FROM_SEC(sbi, secno);
  203. }
  204. return NULL_SEGNO;
  205. }
  206. static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
  207. {
  208. struct sit_info *sit_i = SIT_I(sbi);
  209. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  210. unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
  211. unsigned long long mtime = 0;
  212. unsigned int vblocks;
  213. unsigned char age = 0;
  214. unsigned char u;
  215. unsigned int i;
  216. for (i = 0; i < sbi->segs_per_sec; i++)
  217. mtime += get_seg_entry(sbi, start + i)->mtime;
  218. vblocks = get_valid_blocks(sbi, segno, true);
  219. mtime = div_u64(mtime, sbi->segs_per_sec);
  220. vblocks = div_u64(vblocks, sbi->segs_per_sec);
  221. u = (vblocks * 100) >> sbi->log_blocks_per_seg;
  222. /* Handle if the system time has changed by the user */
  223. if (mtime < sit_i->min_mtime)
  224. sit_i->min_mtime = mtime;
  225. if (mtime > sit_i->max_mtime)
  226. sit_i->max_mtime = mtime;
  227. if (sit_i->max_mtime != sit_i->min_mtime)
  228. age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime),
  229. sit_i->max_mtime - sit_i->min_mtime);
  230. return UINT_MAX - ((100 * (100 - u) * age) / (100 + u));
  231. }
  232. static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi,
  233. unsigned int segno, struct victim_sel_policy *p)
  234. {
  235. if (p->alloc_mode == SSR)
  236. return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  237. /* alloc_mode == LFS */
  238. if (p->gc_mode == GC_GREEDY)
  239. return get_valid_blocks(sbi, segno, true);
  240. else
  241. return get_cb_cost(sbi, segno);
  242. }
  243. static unsigned int count_bits(const unsigned long *addr,
  244. unsigned int offset, unsigned int len)
  245. {
  246. unsigned int end = offset + len, sum = 0;
  247. while (offset < end) {
  248. if (test_bit(offset++, addr))
  249. ++sum;
  250. }
  251. return sum;
  252. }
  253. /*
  254. * This function is called from two paths.
  255. * One is garbage collection and the other is SSR segment selection.
  256. * When it is called during GC, it just gets a victim segment
  257. * and it does not remove it from dirty seglist.
  258. * When it is called from SSR segment selection, it finds a segment
  259. * which has minimum valid blocks and removes it from dirty seglist.
  260. */
  261. static int get_victim_by_default(struct f2fs_sb_info *sbi,
  262. unsigned int *result, int gc_type, int type, char alloc_mode)
  263. {
  264. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  265. struct sit_info *sm = SIT_I(sbi);
  266. struct victim_sel_policy p;
  267. unsigned int secno, last_victim;
  268. unsigned int last_segment = MAIN_SEGS(sbi);
  269. unsigned int nsearched = 0;
  270. mutex_lock(&dirty_i->seglist_lock);
  271. p.alloc_mode = alloc_mode;
  272. select_policy(sbi, gc_type, type, &p);
  273. p.min_segno = NULL_SEGNO;
  274. p.min_cost = get_max_cost(sbi, &p);
  275. if (*result != NULL_SEGNO) {
  276. if (IS_DATASEG(get_seg_entry(sbi, *result)->type) &&
  277. get_valid_blocks(sbi, *result, false) &&
  278. !sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result)))
  279. p.min_segno = *result;
  280. goto out;
  281. }
  282. if (p.max_search == 0)
  283. goto out;
  284. last_victim = sm->last_victim[p.gc_mode];
  285. if (p.alloc_mode == LFS && gc_type == FG_GC) {
  286. p.min_segno = check_bg_victims(sbi);
  287. if (p.min_segno != NULL_SEGNO)
  288. goto got_it;
  289. }
  290. while (1) {
  291. unsigned long cost;
  292. unsigned int segno;
  293. segno = find_next_bit(p.dirty_segmap, last_segment, p.offset);
  294. if (segno >= last_segment) {
  295. if (sm->last_victim[p.gc_mode]) {
  296. last_segment =
  297. sm->last_victim[p.gc_mode];
  298. sm->last_victim[p.gc_mode] = 0;
  299. p.offset = 0;
  300. continue;
  301. }
  302. break;
  303. }
  304. p.offset = segno + p.ofs_unit;
  305. if (p.ofs_unit > 1) {
  306. p.offset -= segno % p.ofs_unit;
  307. nsearched += count_bits(p.dirty_segmap,
  308. p.offset - p.ofs_unit,
  309. p.ofs_unit);
  310. } else {
  311. nsearched++;
  312. }
  313. secno = GET_SEC_FROM_SEG(sbi, segno);
  314. if (sec_usage_check(sbi, secno))
  315. goto next;
  316. if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
  317. goto next;
  318. cost = get_gc_cost(sbi, segno, &p);
  319. if (p.min_cost > cost) {
  320. p.min_segno = segno;
  321. p.min_cost = cost;
  322. }
  323. next:
  324. if (nsearched >= p.max_search) {
  325. if (!sm->last_victim[p.gc_mode] && segno <= last_victim)
  326. sm->last_victim[p.gc_mode] = last_victim + 1;
  327. else
  328. sm->last_victim[p.gc_mode] = segno + 1;
  329. sm->last_victim[p.gc_mode] %= MAIN_SEGS(sbi);
  330. break;
  331. }
  332. }
  333. if (p.min_segno != NULL_SEGNO) {
  334. got_it:
  335. if (p.alloc_mode == LFS) {
  336. secno = GET_SEC_FROM_SEG(sbi, p.min_segno);
  337. if (gc_type == FG_GC)
  338. sbi->cur_victim_sec = secno;
  339. else
  340. set_bit(secno, dirty_i->victim_secmap);
  341. }
  342. *result = (p.min_segno / p.ofs_unit) * p.ofs_unit;
  343. trace_f2fs_get_victim(sbi->sb, type, gc_type, &p,
  344. sbi->cur_victim_sec,
  345. prefree_segments(sbi), free_segments(sbi));
  346. }
  347. out:
  348. mutex_unlock(&dirty_i->seglist_lock);
  349. return (p.min_segno == NULL_SEGNO) ? 0 : 1;
  350. }
  351. static const struct victim_selection default_v_ops = {
  352. .get_victim = get_victim_by_default,
  353. };
  354. static struct inode *find_gc_inode(struct gc_inode_list *gc_list, nid_t ino)
  355. {
  356. struct inode_entry *ie;
  357. ie = radix_tree_lookup(&gc_list->iroot, ino);
  358. if (ie)
  359. return ie->inode;
  360. return NULL;
  361. }
  362. static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode)
  363. {
  364. struct inode_entry *new_ie;
  365. if (inode == find_gc_inode(gc_list, inode->i_ino)) {
  366. iput(inode);
  367. return;
  368. }
  369. new_ie = f2fs_kmem_cache_alloc(f2fs_inode_entry_slab, GFP_NOFS);
  370. new_ie->inode = inode;
  371. f2fs_radix_tree_insert(&gc_list->iroot, inode->i_ino, new_ie);
  372. list_add_tail(&new_ie->list, &gc_list->ilist);
  373. }
  374. static void put_gc_inode(struct gc_inode_list *gc_list)
  375. {
  376. struct inode_entry *ie, *next_ie;
  377. list_for_each_entry_safe(ie, next_ie, &gc_list->ilist, list) {
  378. radix_tree_delete(&gc_list->iroot, ie->inode->i_ino);
  379. iput(ie->inode);
  380. list_del(&ie->list);
  381. kmem_cache_free(f2fs_inode_entry_slab, ie);
  382. }
  383. }
  384. static int check_valid_map(struct f2fs_sb_info *sbi,
  385. unsigned int segno, int offset)
  386. {
  387. struct sit_info *sit_i = SIT_I(sbi);
  388. struct seg_entry *sentry;
  389. int ret;
  390. down_read(&sit_i->sentry_lock);
  391. sentry = get_seg_entry(sbi, segno);
  392. ret = f2fs_test_bit(offset, sentry->cur_valid_map);
  393. up_read(&sit_i->sentry_lock);
  394. return ret;
  395. }
  396. /*
  397. * This function compares node address got in summary with that in NAT.
  398. * On validity, copy that node with cold status, otherwise (invalid node)
  399. * ignore that.
  400. */
  401. static void gc_node_segment(struct f2fs_sb_info *sbi,
  402. struct f2fs_summary *sum, unsigned int segno, int gc_type)
  403. {
  404. struct f2fs_summary *entry;
  405. block_t start_addr;
  406. int off;
  407. int phase = 0;
  408. bool fggc = (gc_type == FG_GC);
  409. start_addr = START_BLOCK(sbi, segno);
  410. next_step:
  411. entry = sum;
  412. if (fggc && phase == 2)
  413. atomic_inc(&sbi->wb_sync_req[NODE]);
  414. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  415. nid_t nid = le32_to_cpu(entry->nid);
  416. struct page *node_page;
  417. struct node_info ni;
  418. /* stop BG_GC if there is not enough free sections. */
  419. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
  420. return;
  421. if (check_valid_map(sbi, segno, off) == 0)
  422. continue;
  423. if (phase == 0) {
  424. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
  425. META_NAT, true);
  426. continue;
  427. }
  428. if (phase == 1) {
  429. f2fs_ra_node_page(sbi, nid);
  430. continue;
  431. }
  432. /* phase == 2 */
  433. node_page = f2fs_get_node_page(sbi, nid);
  434. if (IS_ERR(node_page))
  435. continue;
  436. /* block may become invalid during f2fs_get_node_page */
  437. if (check_valid_map(sbi, segno, off) == 0) {
  438. f2fs_put_page(node_page, 1);
  439. continue;
  440. }
  441. f2fs_get_node_info(sbi, nid, &ni);
  442. if (ni.blk_addr != start_addr + off) {
  443. f2fs_put_page(node_page, 1);
  444. continue;
  445. }
  446. f2fs_move_node_page(node_page, gc_type);
  447. stat_inc_node_blk_count(sbi, 1, gc_type);
  448. }
  449. if (++phase < 3)
  450. goto next_step;
  451. if (fggc)
  452. atomic_dec(&sbi->wb_sync_req[NODE]);
  453. }
  454. /*
  455. * Calculate start block index indicating the given node offset.
  456. * Be careful, caller should give this node offset only indicating direct node
  457. * blocks. If any node offsets, which point the other types of node blocks such
  458. * as indirect or double indirect node blocks, are given, it must be a caller's
  459. * bug.
  460. */
  461. block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode)
  462. {
  463. unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
  464. unsigned int bidx;
  465. if (node_ofs == 0)
  466. return 0;
  467. if (node_ofs <= 2) {
  468. bidx = node_ofs - 1;
  469. } else if (node_ofs <= indirect_blks) {
  470. int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
  471. bidx = node_ofs - 2 - dec;
  472. } else {
  473. int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
  474. bidx = node_ofs - 5 - dec;
  475. }
  476. return bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE(inode);
  477. }
  478. static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  479. struct node_info *dni, block_t blkaddr, unsigned int *nofs)
  480. {
  481. struct page *node_page;
  482. nid_t nid;
  483. unsigned int ofs_in_node;
  484. block_t source_blkaddr;
  485. nid = le32_to_cpu(sum->nid);
  486. ofs_in_node = le16_to_cpu(sum->ofs_in_node);
  487. node_page = f2fs_get_node_page(sbi, nid);
  488. if (IS_ERR(node_page))
  489. return false;
  490. f2fs_get_node_info(sbi, nid, dni);
  491. if (sum->version != dni->version) {
  492. f2fs_msg(sbi->sb, KERN_WARNING,
  493. "%s: valid data with mismatched node version.",
  494. __func__);
  495. set_sbi_flag(sbi, SBI_NEED_FSCK);
  496. }
  497. *nofs = ofs_of_node(node_page);
  498. source_blkaddr = datablock_addr(NULL, node_page, ofs_in_node);
  499. f2fs_put_page(node_page, 1);
  500. if (source_blkaddr != blkaddr)
  501. return false;
  502. return true;
  503. }
  504. /*
  505. * Move data block via META_MAPPING while keeping locked data page.
  506. * This can be used to move blocks, aka LBAs, directly on disk.
  507. */
  508. static void move_data_block(struct inode *inode, block_t bidx,
  509. int gc_type, unsigned int segno, int off)
  510. {
  511. struct f2fs_io_info fio = {
  512. .sbi = F2FS_I_SB(inode),
  513. .ino = inode->i_ino,
  514. .type = DATA,
  515. .temp = COLD,
  516. .op = REQ_OP_READ,
  517. .op_flags = 0,
  518. .encrypted_page = NULL,
  519. .in_list = false,
  520. .retry = false,
  521. };
  522. struct dnode_of_data dn;
  523. struct f2fs_summary sum;
  524. struct node_info ni;
  525. struct page *page;
  526. block_t newaddr;
  527. int err;
  528. bool lfs_mode = test_opt(fio.sbi, LFS);
  529. /* do not read out */
  530. page = f2fs_grab_cache_page(inode->i_mapping, bidx, false);
  531. if (!page)
  532. return;
  533. if (!check_valid_map(F2FS_I_SB(inode), segno, off))
  534. goto out;
  535. if (f2fs_is_atomic_file(inode)) {
  536. F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC]++;
  537. F2FS_I_SB(inode)->skipped_atomic_files[gc_type]++;
  538. goto out;
  539. }
  540. if (f2fs_is_pinned_file(inode)) {
  541. f2fs_pin_file_control(inode, true);
  542. goto out;
  543. }
  544. set_new_dnode(&dn, inode, NULL, NULL, 0);
  545. err = f2fs_get_dnode_of_data(&dn, bidx, LOOKUP_NODE);
  546. if (err)
  547. goto out;
  548. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  549. ClearPageUptodate(page);
  550. goto put_out;
  551. }
  552. /*
  553. * don't cache encrypted data into meta inode until previous dirty
  554. * data were writebacked to avoid racing between GC and flush.
  555. */
  556. f2fs_wait_on_page_writeback(page, DATA, true);
  557. f2fs_get_node_info(fio.sbi, dn.nid, &ni);
  558. set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
  559. /* read page */
  560. fio.page = page;
  561. fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
  562. if (lfs_mode)
  563. down_write(&fio.sbi->io_order_lock);
  564. f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
  565. &sum, CURSEG_COLD_DATA, NULL, false);
  566. fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(fio.sbi),
  567. newaddr, FGP_LOCK | FGP_CREAT, GFP_NOFS);
  568. if (!fio.encrypted_page) {
  569. err = -ENOMEM;
  570. goto recover_block;
  571. }
  572. err = f2fs_submit_page_bio(&fio);
  573. if (err)
  574. goto put_page_out;
  575. /* write page */
  576. lock_page(fio.encrypted_page);
  577. if (unlikely(fio.encrypted_page->mapping != META_MAPPING(fio.sbi))) {
  578. err = -EIO;
  579. goto put_page_out;
  580. }
  581. if (unlikely(!PageUptodate(fio.encrypted_page))) {
  582. err = -EIO;
  583. goto put_page_out;
  584. }
  585. set_page_dirty(fio.encrypted_page);
  586. f2fs_wait_on_page_writeback(fio.encrypted_page, DATA, true);
  587. if (clear_page_dirty_for_io(fio.encrypted_page))
  588. dec_page_count(fio.sbi, F2FS_DIRTY_META);
  589. set_page_writeback(fio.encrypted_page);
  590. ClearPageError(page);
  591. /* allocate block address */
  592. f2fs_wait_on_page_writeback(dn.node_page, NODE, true);
  593. fio.op = REQ_OP_WRITE;
  594. fio.op_flags = REQ_SYNC;
  595. fio.new_blkaddr = newaddr;
  596. f2fs_submit_page_write(&fio);
  597. if (fio.retry) {
  598. if (PageWriteback(fio.encrypted_page))
  599. end_page_writeback(fio.encrypted_page);
  600. goto put_page_out;
  601. }
  602. f2fs_update_iostat(fio.sbi, FS_GC_DATA_IO, F2FS_BLKSIZE);
  603. f2fs_update_data_blkaddr(&dn, newaddr);
  604. set_inode_flag(inode, FI_APPEND_WRITE);
  605. if (page->index == 0)
  606. set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
  607. put_page_out:
  608. f2fs_put_page(fio.encrypted_page, 1);
  609. recover_block:
  610. if (lfs_mode)
  611. up_write(&fio.sbi->io_order_lock);
  612. if (err)
  613. f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
  614. true, true);
  615. put_out:
  616. f2fs_put_dnode(&dn);
  617. out:
  618. f2fs_put_page(page, 1);
  619. }
  620. static void move_data_page(struct inode *inode, block_t bidx, int gc_type,
  621. unsigned int segno, int off)
  622. {
  623. struct page *page;
  624. page = f2fs_get_lock_data_page(inode, bidx, true);
  625. if (IS_ERR(page))
  626. return;
  627. if (!check_valid_map(F2FS_I_SB(inode), segno, off))
  628. goto out;
  629. if (f2fs_is_atomic_file(inode)) {
  630. F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC]++;
  631. F2FS_I_SB(inode)->skipped_atomic_files[gc_type]++;
  632. goto out;
  633. }
  634. if (f2fs_is_pinned_file(inode)) {
  635. if (gc_type == FG_GC)
  636. f2fs_pin_file_control(inode, true);
  637. goto out;
  638. }
  639. if (gc_type == BG_GC) {
  640. if (PageWriteback(page))
  641. goto out;
  642. set_page_dirty(page);
  643. set_cold_data(page);
  644. } else {
  645. struct f2fs_io_info fio = {
  646. .sbi = F2FS_I_SB(inode),
  647. .ino = inode->i_ino,
  648. .type = DATA,
  649. .temp = COLD,
  650. .op = REQ_OP_WRITE,
  651. .op_flags = REQ_SYNC,
  652. .old_blkaddr = NULL_ADDR,
  653. .page = page,
  654. .encrypted_page = NULL,
  655. .need_lock = LOCK_REQ,
  656. .io_type = FS_GC_DATA_IO,
  657. };
  658. bool is_dirty = PageDirty(page);
  659. int err;
  660. retry:
  661. set_page_dirty(page);
  662. f2fs_wait_on_page_writeback(page, DATA, true);
  663. if (clear_page_dirty_for_io(page)) {
  664. inode_dec_dirty_pages(inode);
  665. f2fs_remove_dirty_inode(inode);
  666. }
  667. set_cold_data(page);
  668. err = f2fs_do_write_data_page(&fio);
  669. if (err) {
  670. clear_cold_data(page);
  671. if (err == -ENOMEM) {
  672. congestion_wait(BLK_RW_ASYNC, HZ/50);
  673. goto retry;
  674. }
  675. if (is_dirty)
  676. set_page_dirty(page);
  677. }
  678. }
  679. out:
  680. f2fs_put_page(page, 1);
  681. }
  682. /*
  683. * This function tries to get parent node of victim data block, and identifies
  684. * data block validity. If the block is valid, copy that with cold status and
  685. * modify parent node.
  686. * If the parent node is not valid or the data block address is different,
  687. * the victim data block is ignored.
  688. */
  689. static void gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  690. struct gc_inode_list *gc_list, unsigned int segno, int gc_type)
  691. {
  692. struct super_block *sb = sbi->sb;
  693. struct f2fs_summary *entry;
  694. block_t start_addr;
  695. int off;
  696. int phase = 0;
  697. start_addr = START_BLOCK(sbi, segno);
  698. next_step:
  699. entry = sum;
  700. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  701. struct page *data_page;
  702. struct inode *inode;
  703. struct node_info dni; /* dnode info for the data */
  704. unsigned int ofs_in_node, nofs;
  705. block_t start_bidx;
  706. nid_t nid = le32_to_cpu(entry->nid);
  707. /* stop BG_GC if there is not enough free sections. */
  708. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
  709. return;
  710. if (check_valid_map(sbi, segno, off) == 0)
  711. continue;
  712. if (phase == 0) {
  713. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
  714. META_NAT, true);
  715. continue;
  716. }
  717. if (phase == 1) {
  718. f2fs_ra_node_page(sbi, nid);
  719. continue;
  720. }
  721. /* Get an inode by ino with checking validity */
  722. if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs))
  723. continue;
  724. if (phase == 2) {
  725. f2fs_ra_node_page(sbi, dni.ino);
  726. continue;
  727. }
  728. ofs_in_node = le16_to_cpu(entry->ofs_in_node);
  729. if (phase == 3) {
  730. inode = f2fs_iget(sb, dni.ino);
  731. if (IS_ERR(inode) || is_bad_inode(inode))
  732. continue;
  733. /* if inode uses special I/O path, let's go phase 3 */
  734. if (f2fs_post_read_required(inode)) {
  735. add_gc_inode(gc_list, inode);
  736. continue;
  737. }
  738. if (!down_write_trylock(
  739. &F2FS_I(inode)->i_gc_rwsem[WRITE])) {
  740. iput(inode);
  741. continue;
  742. }
  743. start_bidx = f2fs_start_bidx_of_node(nofs, inode);
  744. data_page = f2fs_get_read_data_page(inode,
  745. start_bidx + ofs_in_node, REQ_RAHEAD,
  746. true);
  747. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  748. if (IS_ERR(data_page)) {
  749. iput(inode);
  750. continue;
  751. }
  752. f2fs_put_page(data_page, 0);
  753. add_gc_inode(gc_list, inode);
  754. continue;
  755. }
  756. /* phase 4 */
  757. inode = find_gc_inode(gc_list, dni.ino);
  758. if (inode) {
  759. struct f2fs_inode_info *fi = F2FS_I(inode);
  760. bool locked = false;
  761. if (S_ISREG(inode->i_mode)) {
  762. if (!down_write_trylock(&fi->i_gc_rwsem[READ]))
  763. continue;
  764. if (!down_write_trylock(
  765. &fi->i_gc_rwsem[WRITE])) {
  766. up_write(&fi->i_gc_rwsem[READ]);
  767. continue;
  768. }
  769. locked = true;
  770. /* wait for all inflight aio data */
  771. inode_dio_wait(inode);
  772. }
  773. start_bidx = f2fs_start_bidx_of_node(nofs, inode)
  774. + ofs_in_node;
  775. if (f2fs_post_read_required(inode))
  776. move_data_block(inode, start_bidx, gc_type,
  777. segno, off);
  778. else
  779. move_data_page(inode, start_bidx, gc_type,
  780. segno, off);
  781. if (locked) {
  782. up_write(&fi->i_gc_rwsem[WRITE]);
  783. up_write(&fi->i_gc_rwsem[READ]);
  784. }
  785. stat_inc_data_blk_count(sbi, 1, gc_type);
  786. }
  787. }
  788. if (++phase < 5)
  789. goto next_step;
  790. }
  791. static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
  792. int gc_type)
  793. {
  794. struct sit_info *sit_i = SIT_I(sbi);
  795. int ret;
  796. down_write(&sit_i->sentry_lock);
  797. ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type,
  798. NO_CHECK_TYPE, LFS);
  799. up_write(&sit_i->sentry_lock);
  800. return ret;
  801. }
  802. static int do_garbage_collect(struct f2fs_sb_info *sbi,
  803. unsigned int start_segno,
  804. struct gc_inode_list *gc_list, int gc_type)
  805. {
  806. struct page *sum_page;
  807. struct f2fs_summary_block *sum;
  808. struct blk_plug plug;
  809. unsigned int segno = start_segno;
  810. unsigned int end_segno = start_segno + sbi->segs_per_sec;
  811. int seg_freed = 0;
  812. unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
  813. SUM_TYPE_DATA : SUM_TYPE_NODE;
  814. /* readahead multi ssa blocks those have contiguous address */
  815. if (sbi->segs_per_sec > 1)
  816. f2fs_ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno),
  817. sbi->segs_per_sec, META_SSA, true);
  818. /* reference all summary page */
  819. while (segno < end_segno) {
  820. sum_page = f2fs_get_sum_page(sbi, segno++);
  821. unlock_page(sum_page);
  822. }
  823. blk_start_plug(&plug);
  824. for (segno = start_segno; segno < end_segno; segno++) {
  825. /* find segment summary of victim */
  826. sum_page = find_get_page(META_MAPPING(sbi),
  827. GET_SUM_BLOCK(sbi, segno));
  828. f2fs_put_page(sum_page, 0);
  829. if (get_valid_blocks(sbi, segno, false) == 0 ||
  830. !PageUptodate(sum_page) ||
  831. unlikely(f2fs_cp_error(sbi)))
  832. goto next;
  833. sum = page_address(sum_page);
  834. f2fs_bug_on(sbi, type != GET_SUM_TYPE((&sum->footer)));
  835. /*
  836. * this is to avoid deadlock:
  837. * - lock_page(sum_page) - f2fs_replace_block
  838. * - check_valid_map() - down_write(sentry_lock)
  839. * - down_read(sentry_lock) - change_curseg()
  840. * - lock_page(sum_page)
  841. */
  842. if (type == SUM_TYPE_NODE)
  843. gc_node_segment(sbi, sum->entries, segno, gc_type);
  844. else
  845. gc_data_segment(sbi, sum->entries, gc_list, segno,
  846. gc_type);
  847. stat_inc_seg_count(sbi, type, gc_type);
  848. if (gc_type == FG_GC &&
  849. get_valid_blocks(sbi, segno, false) == 0)
  850. seg_freed++;
  851. next:
  852. f2fs_put_page(sum_page, 0);
  853. }
  854. if (gc_type == FG_GC)
  855. f2fs_submit_merged_write(sbi,
  856. (type == SUM_TYPE_NODE) ? NODE : DATA);
  857. blk_finish_plug(&plug);
  858. stat_inc_call_count(sbi->stat_info);
  859. return seg_freed;
  860. }
  861. int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
  862. bool background, unsigned int segno)
  863. {
  864. int gc_type = sync ? FG_GC : BG_GC;
  865. int sec_freed = 0, seg_freed = 0, total_freed = 0;
  866. int ret = 0;
  867. struct cp_control cpc;
  868. unsigned int init_segno = segno;
  869. struct gc_inode_list gc_list = {
  870. .ilist = LIST_HEAD_INIT(gc_list.ilist),
  871. .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
  872. };
  873. unsigned long long last_skipped = sbi->skipped_atomic_files[FG_GC];
  874. unsigned int skipped_round = 0, round = 0;
  875. trace_f2fs_gc_begin(sbi->sb, sync, background,
  876. get_pages(sbi, F2FS_DIRTY_NODES),
  877. get_pages(sbi, F2FS_DIRTY_DENTS),
  878. get_pages(sbi, F2FS_DIRTY_IMETA),
  879. free_sections(sbi),
  880. free_segments(sbi),
  881. reserved_segments(sbi),
  882. prefree_segments(sbi));
  883. cpc.reason = __get_cp_reason(sbi);
  884. gc_more:
  885. if (unlikely(!(sbi->sb->s_flags & SB_ACTIVE))) {
  886. ret = -EINVAL;
  887. goto stop;
  888. }
  889. if (unlikely(f2fs_cp_error(sbi))) {
  890. ret = -EIO;
  891. goto stop;
  892. }
  893. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) {
  894. /*
  895. * For example, if there are many prefree_segments below given
  896. * threshold, we can make them free by checkpoint. Then, we
  897. * secure free segments which doesn't need fggc any more.
  898. */
  899. if (prefree_segments(sbi)) {
  900. ret = f2fs_write_checkpoint(sbi, &cpc);
  901. if (ret)
  902. goto stop;
  903. }
  904. if (has_not_enough_free_secs(sbi, 0, 0))
  905. gc_type = FG_GC;
  906. }
  907. /* f2fs_balance_fs doesn't need to do BG_GC in critical path. */
  908. if (gc_type == BG_GC && !background) {
  909. ret = -EINVAL;
  910. goto stop;
  911. }
  912. if (!__get_victim(sbi, &segno, gc_type)) {
  913. ret = -ENODATA;
  914. goto stop;
  915. }
  916. seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type);
  917. if (gc_type == FG_GC && seg_freed == sbi->segs_per_sec)
  918. sec_freed++;
  919. total_freed += seg_freed;
  920. if (gc_type == FG_GC) {
  921. if (sbi->skipped_atomic_files[FG_GC] > last_skipped)
  922. skipped_round++;
  923. last_skipped = sbi->skipped_atomic_files[FG_GC];
  924. round++;
  925. }
  926. if (gc_type == FG_GC)
  927. sbi->cur_victim_sec = NULL_SEGNO;
  928. if (!sync) {
  929. if (has_not_enough_free_secs(sbi, sec_freed, 0)) {
  930. if (skipped_round > MAX_SKIP_ATOMIC_COUNT &&
  931. skipped_round * 2 >= round)
  932. f2fs_drop_inmem_pages_all(sbi, true);
  933. segno = NULL_SEGNO;
  934. goto gc_more;
  935. }
  936. if (gc_type == FG_GC)
  937. ret = f2fs_write_checkpoint(sbi, &cpc);
  938. }
  939. stop:
  940. SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
  941. SIT_I(sbi)->last_victim[FLUSH_DEVICE] = init_segno;
  942. trace_f2fs_gc_end(sbi->sb, ret, total_freed, sec_freed,
  943. get_pages(sbi, F2FS_DIRTY_NODES),
  944. get_pages(sbi, F2FS_DIRTY_DENTS),
  945. get_pages(sbi, F2FS_DIRTY_IMETA),
  946. free_sections(sbi),
  947. free_segments(sbi),
  948. reserved_segments(sbi),
  949. prefree_segments(sbi));
  950. mutex_unlock(&sbi->gc_mutex);
  951. put_gc_inode(&gc_list);
  952. if (sync)
  953. ret = sec_freed ? 0 : -EAGAIN;
  954. return ret;
  955. }
  956. void f2fs_build_gc_manager(struct f2fs_sb_info *sbi)
  957. {
  958. DIRTY_I(sbi)->v_ops = &default_v_ops;
  959. sbi->gc_pin_file_threshold = DEF_GC_FAILED_PINNED_FILES;
  960. /* give warm/cold data area from slower device */
  961. if (sbi->s_ndevs && sbi->segs_per_sec == 1)
  962. SIT_I(sbi)->last_victim[ALLOC_NEXT] =
  963. GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
  964. }