gc.c 27 KB

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