gc.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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 (!mutex_trylock(&sbi->gc_mutex))
  70. goto next;
  71. if (gc_th->gc_urgent) {
  72. wait_ms = gc_th->urgent_sleep_time;
  73. goto do_gc;
  74. }
  75. if (!is_idle(sbi)) {
  76. increase_sleep_time(gc_th, &wait_ms);
  77. mutex_unlock(&sbi->gc_mutex);
  78. goto next;
  79. }
  80. if (has_enough_invalid_blocks(sbi))
  81. decrease_sleep_time(gc_th, &wait_ms);
  82. else
  83. increase_sleep_time(gc_th, &wait_ms);
  84. do_gc:
  85. stat_inc_bggc_count(sbi);
  86. /* if return value is not zero, no victim was selected */
  87. if (f2fs_gc(sbi, test_opt(sbi, FORCE_FG_GC), true, NULL_SEGNO))
  88. wait_ms = gc_th->no_gc_sleep_time;
  89. trace_f2fs_background_gc(sbi->sb, wait_ms,
  90. prefree_segments(sbi), free_segments(sbi));
  91. /* balancing f2fs's metadata periodically */
  92. f2fs_balance_fs_bg(sbi);
  93. next:
  94. sb_end_write(sbi->sb);
  95. } while (!kthread_should_stop());
  96. return 0;
  97. }
  98. int start_gc_thread(struct f2fs_sb_info *sbi)
  99. {
  100. struct f2fs_gc_kthread *gc_th;
  101. dev_t dev = sbi->sb->s_bdev->bd_dev;
  102. int err = 0;
  103. gc_th = f2fs_kmalloc(sbi, sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
  104. if (!gc_th) {
  105. err = -ENOMEM;
  106. goto out;
  107. }
  108. gc_th->urgent_sleep_time = DEF_GC_THREAD_URGENT_SLEEP_TIME;
  109. gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME;
  110. gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
  111. gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
  112. gc_th->gc_idle = 0;
  113. gc_th->gc_urgent = 0;
  114. gc_th->gc_wake= 0;
  115. sbi->gc_thread = gc_th;
  116. init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
  117. sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
  118. "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
  119. if (IS_ERR(gc_th->f2fs_gc_task)) {
  120. err = PTR_ERR(gc_th->f2fs_gc_task);
  121. kfree(gc_th);
  122. sbi->gc_thread = NULL;
  123. }
  124. out:
  125. return err;
  126. }
  127. void stop_gc_thread(struct f2fs_sb_info *sbi)
  128. {
  129. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  130. if (!gc_th)
  131. return;
  132. kthread_stop(gc_th->f2fs_gc_task);
  133. kfree(gc_th);
  134. sbi->gc_thread = NULL;
  135. }
  136. static int select_gc_type(struct f2fs_gc_kthread *gc_th, int gc_type)
  137. {
  138. int gc_mode = (gc_type == BG_GC) ? GC_CB : GC_GREEDY;
  139. if (gc_th && gc_th->gc_idle) {
  140. if (gc_th->gc_idle == 1)
  141. gc_mode = GC_CB;
  142. else if (gc_th->gc_idle == 2)
  143. gc_mode = GC_GREEDY;
  144. }
  145. return gc_mode;
  146. }
  147. static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
  148. int type, struct victim_sel_policy *p)
  149. {
  150. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  151. if (p->alloc_mode == SSR) {
  152. p->gc_mode = GC_GREEDY;
  153. p->dirty_segmap = dirty_i->dirty_segmap[type];
  154. p->max_search = dirty_i->nr_dirty[type];
  155. p->ofs_unit = 1;
  156. } else {
  157. p->gc_mode = select_gc_type(sbi->gc_thread, gc_type);
  158. p->dirty_segmap = dirty_i->dirty_segmap[DIRTY];
  159. p->max_search = dirty_i->nr_dirty[DIRTY];
  160. p->ofs_unit = sbi->segs_per_sec;
  161. }
  162. /* we need to check every dirty segments in the FG_GC case */
  163. if (gc_type != FG_GC && p->max_search > sbi->max_victim_search)
  164. p->max_search = sbi->max_victim_search;
  165. /* let's select beginning hot/small space first */
  166. if (type == CURSEG_HOT_DATA || IS_NODESEG(type))
  167. p->offset = 0;
  168. else
  169. p->offset = SIT_I(sbi)->last_victim[p->gc_mode];
  170. }
  171. static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
  172. struct victim_sel_policy *p)
  173. {
  174. /* SSR allocates in a segment unit */
  175. if (p->alloc_mode == SSR)
  176. return sbi->blocks_per_seg;
  177. if (p->gc_mode == GC_GREEDY)
  178. return 2 * sbi->blocks_per_seg * p->ofs_unit;
  179. else if (p->gc_mode == GC_CB)
  180. return UINT_MAX;
  181. else /* No other gc_mode */
  182. return 0;
  183. }
  184. static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
  185. {
  186. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  187. unsigned int secno;
  188. /*
  189. * If the gc_type is FG_GC, we can select victim segments
  190. * selected by background GC before.
  191. * Those segments guarantee they have small valid blocks.
  192. */
  193. for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
  194. if (sec_usage_check(sbi, secno))
  195. continue;
  196. if (no_fggc_candidate(sbi, secno))
  197. continue;
  198. clear_bit(secno, dirty_i->victim_secmap);
  199. return GET_SEG_FROM_SEC(sbi, secno);
  200. }
  201. return NULL_SEGNO;
  202. }
  203. static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
  204. {
  205. struct sit_info *sit_i = SIT_I(sbi);
  206. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  207. unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
  208. unsigned long long mtime = 0;
  209. unsigned int vblocks;
  210. unsigned char age = 0;
  211. unsigned char u;
  212. unsigned int i;
  213. for (i = 0; i < sbi->segs_per_sec; i++)
  214. mtime += get_seg_entry(sbi, start + i)->mtime;
  215. vblocks = get_valid_blocks(sbi, segno, true);
  216. mtime = div_u64(mtime, sbi->segs_per_sec);
  217. vblocks = div_u64(vblocks, sbi->segs_per_sec);
  218. u = (vblocks * 100) >> sbi->log_blocks_per_seg;
  219. /* Handle if the system time has changed by the user */
  220. if (mtime < sit_i->min_mtime)
  221. sit_i->min_mtime = mtime;
  222. if (mtime > sit_i->max_mtime)
  223. sit_i->max_mtime = mtime;
  224. if (sit_i->max_mtime != sit_i->min_mtime)
  225. age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime),
  226. sit_i->max_mtime - sit_i->min_mtime);
  227. return UINT_MAX - ((100 * (100 - u) * age) / (100 + u));
  228. }
  229. static unsigned int get_greedy_cost(struct f2fs_sb_info *sbi,
  230. unsigned int segno)
  231. {
  232. unsigned int valid_blocks =
  233. get_valid_blocks(sbi, segno, true);
  234. return IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
  235. valid_blocks * 2 : valid_blocks;
  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_greedy_cost(sbi, segno);
  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. mutex_lock(&sit_i->sentry_lock);
  399. sentry = get_seg_entry(sbi, segno);
  400. ret = f2fs_test_bit(offset, sentry->cur_valid_map);
  401. mutex_unlock(&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. .type = DATA,
  517. .temp = COLD,
  518. .op = REQ_OP_READ,
  519. .op_flags = 0,
  520. .encrypted_page = NULL,
  521. .in_list = false,
  522. };
  523. struct dnode_of_data dn;
  524. struct f2fs_summary sum;
  525. struct node_info ni;
  526. struct page *page;
  527. block_t newaddr;
  528. int err;
  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. goto out;
  537. set_new_dnode(&dn, inode, NULL, NULL, 0);
  538. err = get_dnode_of_data(&dn, bidx, LOOKUP_NODE);
  539. if (err)
  540. goto out;
  541. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  542. ClearPageUptodate(page);
  543. goto put_out;
  544. }
  545. /*
  546. * don't cache encrypted data into meta inode until previous dirty
  547. * data were writebacked to avoid racing between GC and flush.
  548. */
  549. f2fs_wait_on_page_writeback(page, DATA, true);
  550. get_node_info(fio.sbi, dn.nid, &ni);
  551. set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
  552. /* read page */
  553. fio.page = page;
  554. fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
  555. allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
  556. &sum, CURSEG_COLD_DATA, NULL, false);
  557. fio.encrypted_page = pagecache_get_page(META_MAPPING(fio.sbi), newaddr,
  558. FGP_LOCK | FGP_CREAT, GFP_NOFS);
  559. if (!fio.encrypted_page) {
  560. err = -ENOMEM;
  561. goto recover_block;
  562. }
  563. err = f2fs_submit_page_bio(&fio);
  564. if (err)
  565. goto put_page_out;
  566. /* write page */
  567. lock_page(fio.encrypted_page);
  568. if (unlikely(fio.encrypted_page->mapping != META_MAPPING(fio.sbi))) {
  569. err = -EIO;
  570. goto put_page_out;
  571. }
  572. if (unlikely(!PageUptodate(fio.encrypted_page))) {
  573. err = -EIO;
  574. goto put_page_out;
  575. }
  576. set_page_dirty(fio.encrypted_page);
  577. f2fs_wait_on_page_writeback(fio.encrypted_page, DATA, true);
  578. if (clear_page_dirty_for_io(fio.encrypted_page))
  579. dec_page_count(fio.sbi, F2FS_DIRTY_META);
  580. set_page_writeback(fio.encrypted_page);
  581. /* allocate block address */
  582. f2fs_wait_on_page_writeback(dn.node_page, NODE, true);
  583. fio.op = REQ_OP_WRITE;
  584. fio.op_flags = REQ_SYNC;
  585. fio.new_blkaddr = newaddr;
  586. f2fs_submit_page_write(&fio);
  587. f2fs_update_iostat(fio.sbi, FS_GC_DATA_IO, F2FS_BLKSIZE);
  588. f2fs_update_data_blkaddr(&dn, newaddr);
  589. set_inode_flag(inode, FI_APPEND_WRITE);
  590. if (page->index == 0)
  591. set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
  592. put_page_out:
  593. f2fs_put_page(fio.encrypted_page, 1);
  594. recover_block:
  595. if (err)
  596. __f2fs_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
  597. true, true);
  598. put_out:
  599. f2fs_put_dnode(&dn);
  600. out:
  601. f2fs_put_page(page, 1);
  602. }
  603. static void move_data_page(struct inode *inode, block_t bidx, int gc_type,
  604. unsigned int segno, int off)
  605. {
  606. struct page *page;
  607. page = get_lock_data_page(inode, bidx, true);
  608. if (IS_ERR(page))
  609. return;
  610. if (!check_valid_map(F2FS_I_SB(inode), segno, off))
  611. goto out;
  612. if (f2fs_is_atomic_file(inode))
  613. goto out;
  614. if (gc_type == BG_GC) {
  615. if (PageWriteback(page))
  616. goto out;
  617. set_page_dirty(page);
  618. set_cold_data(page);
  619. } else {
  620. struct f2fs_io_info fio = {
  621. .sbi = F2FS_I_SB(inode),
  622. .type = DATA,
  623. .temp = COLD,
  624. .op = REQ_OP_WRITE,
  625. .op_flags = REQ_SYNC,
  626. .old_blkaddr = NULL_ADDR,
  627. .page = page,
  628. .encrypted_page = NULL,
  629. .need_lock = LOCK_REQ,
  630. .io_type = FS_GC_DATA_IO,
  631. };
  632. bool is_dirty = PageDirty(page);
  633. int err;
  634. retry:
  635. set_page_dirty(page);
  636. f2fs_wait_on_page_writeback(page, DATA, true);
  637. if (clear_page_dirty_for_io(page)) {
  638. inode_dec_dirty_pages(inode);
  639. remove_dirty_inode(inode);
  640. }
  641. set_cold_data(page);
  642. err = do_write_data_page(&fio);
  643. if (err == -ENOMEM && is_dirty) {
  644. congestion_wait(BLK_RW_ASYNC, HZ/50);
  645. goto retry;
  646. }
  647. }
  648. out:
  649. f2fs_put_page(page, 1);
  650. }
  651. /*
  652. * This function tries to get parent node of victim data block, and identifies
  653. * data block validity. If the block is valid, copy that with cold status and
  654. * modify parent node.
  655. * If the parent node is not valid or the data block address is different,
  656. * the victim data block is ignored.
  657. */
  658. static void gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  659. struct gc_inode_list *gc_list, unsigned int segno, int gc_type)
  660. {
  661. struct super_block *sb = sbi->sb;
  662. struct f2fs_summary *entry;
  663. block_t start_addr;
  664. int off;
  665. int phase = 0;
  666. start_addr = START_BLOCK(sbi, segno);
  667. next_step:
  668. entry = sum;
  669. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  670. struct page *data_page;
  671. struct inode *inode;
  672. struct node_info dni; /* dnode info for the data */
  673. unsigned int ofs_in_node, nofs;
  674. block_t start_bidx;
  675. nid_t nid = le32_to_cpu(entry->nid);
  676. /* stop BG_GC if there is not enough free sections. */
  677. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
  678. return;
  679. if (check_valid_map(sbi, segno, off) == 0)
  680. continue;
  681. if (phase == 0) {
  682. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
  683. META_NAT, true);
  684. continue;
  685. }
  686. if (phase == 1) {
  687. ra_node_page(sbi, nid);
  688. continue;
  689. }
  690. /* Get an inode by ino with checking validity */
  691. if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs))
  692. continue;
  693. if (phase == 2) {
  694. ra_node_page(sbi, dni.ino);
  695. continue;
  696. }
  697. ofs_in_node = le16_to_cpu(entry->ofs_in_node);
  698. if (phase == 3) {
  699. inode = f2fs_iget(sb, dni.ino);
  700. if (IS_ERR(inode) || is_bad_inode(inode))
  701. continue;
  702. /* if encrypted inode, let's go phase 3 */
  703. if (f2fs_encrypted_file(inode)) {
  704. add_gc_inode(gc_list, inode);
  705. continue;
  706. }
  707. start_bidx = start_bidx_of_node(nofs, inode);
  708. data_page = get_read_data_page(inode,
  709. start_bidx + ofs_in_node, REQ_RAHEAD,
  710. true);
  711. if (IS_ERR(data_page)) {
  712. iput(inode);
  713. continue;
  714. }
  715. f2fs_put_page(data_page, 0);
  716. add_gc_inode(gc_list, inode);
  717. continue;
  718. }
  719. /* phase 4 */
  720. inode = find_gc_inode(gc_list, dni.ino);
  721. if (inode) {
  722. struct f2fs_inode_info *fi = F2FS_I(inode);
  723. bool locked = false;
  724. if (S_ISREG(inode->i_mode)) {
  725. if (!down_write_trylock(&fi->dio_rwsem[READ]))
  726. continue;
  727. if (!down_write_trylock(
  728. &fi->dio_rwsem[WRITE])) {
  729. up_write(&fi->dio_rwsem[READ]);
  730. continue;
  731. }
  732. locked = true;
  733. /* wait for all inflight aio data */
  734. inode_dio_wait(inode);
  735. }
  736. start_bidx = start_bidx_of_node(nofs, inode)
  737. + ofs_in_node;
  738. if (f2fs_encrypted_file(inode))
  739. move_data_block(inode, start_bidx, segno, off);
  740. else
  741. move_data_page(inode, start_bidx, gc_type,
  742. segno, off);
  743. if (locked) {
  744. up_write(&fi->dio_rwsem[WRITE]);
  745. up_write(&fi->dio_rwsem[READ]);
  746. }
  747. stat_inc_data_blk_count(sbi, 1, gc_type);
  748. }
  749. }
  750. if (++phase < 5)
  751. goto next_step;
  752. }
  753. static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
  754. int gc_type)
  755. {
  756. struct sit_info *sit_i = SIT_I(sbi);
  757. int ret;
  758. mutex_lock(&sit_i->sentry_lock);
  759. ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type,
  760. NO_CHECK_TYPE, LFS);
  761. mutex_unlock(&sit_i->sentry_lock);
  762. return ret;
  763. }
  764. static int do_garbage_collect(struct f2fs_sb_info *sbi,
  765. unsigned int start_segno,
  766. struct gc_inode_list *gc_list, int gc_type)
  767. {
  768. struct page *sum_page;
  769. struct f2fs_summary_block *sum;
  770. struct blk_plug plug;
  771. unsigned int segno = start_segno;
  772. unsigned int end_segno = start_segno + sbi->segs_per_sec;
  773. int seg_freed = 0;
  774. unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
  775. SUM_TYPE_DATA : SUM_TYPE_NODE;
  776. /* readahead multi ssa blocks those have contiguous address */
  777. if (sbi->segs_per_sec > 1)
  778. ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno),
  779. sbi->segs_per_sec, META_SSA, true);
  780. /* reference all summary page */
  781. while (segno < end_segno) {
  782. sum_page = get_sum_page(sbi, segno++);
  783. unlock_page(sum_page);
  784. }
  785. blk_start_plug(&plug);
  786. for (segno = start_segno; segno < end_segno; segno++) {
  787. /* find segment summary of victim */
  788. sum_page = find_get_page(META_MAPPING(sbi),
  789. GET_SUM_BLOCK(sbi, segno));
  790. f2fs_put_page(sum_page, 0);
  791. if (get_valid_blocks(sbi, segno, false) == 0 ||
  792. !PageUptodate(sum_page) ||
  793. unlikely(f2fs_cp_error(sbi)))
  794. goto next;
  795. sum = page_address(sum_page);
  796. f2fs_bug_on(sbi, type != GET_SUM_TYPE((&sum->footer)));
  797. /*
  798. * this is to avoid deadlock:
  799. * - lock_page(sum_page) - f2fs_replace_block
  800. * - check_valid_map() - mutex_lock(sentry_lock)
  801. * - mutex_lock(sentry_lock) - change_curseg()
  802. * - lock_page(sum_page)
  803. */
  804. if (type == SUM_TYPE_NODE)
  805. gc_node_segment(sbi, sum->entries, segno, gc_type);
  806. else
  807. gc_data_segment(sbi, sum->entries, gc_list, segno,
  808. gc_type);
  809. stat_inc_seg_count(sbi, type, gc_type);
  810. if (gc_type == FG_GC &&
  811. get_valid_blocks(sbi, segno, false) == 0)
  812. seg_freed++;
  813. next:
  814. f2fs_put_page(sum_page, 0);
  815. }
  816. if (gc_type == FG_GC)
  817. f2fs_submit_merged_write(sbi,
  818. (type == SUM_TYPE_NODE) ? NODE : DATA);
  819. blk_finish_plug(&plug);
  820. stat_inc_call_count(sbi->stat_info);
  821. return seg_freed;
  822. }
  823. int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
  824. bool background, unsigned int segno)
  825. {
  826. int gc_type = sync ? FG_GC : BG_GC;
  827. int sec_freed = 0, seg_freed = 0, total_freed = 0;
  828. int ret = 0;
  829. struct cp_control cpc;
  830. unsigned int init_segno = segno;
  831. struct gc_inode_list gc_list = {
  832. .ilist = LIST_HEAD_INIT(gc_list.ilist),
  833. .iroot = RADIX_TREE_INIT(GFP_NOFS),
  834. };
  835. trace_f2fs_gc_begin(sbi->sb, sync, background,
  836. get_pages(sbi, F2FS_DIRTY_NODES),
  837. get_pages(sbi, F2FS_DIRTY_DENTS),
  838. get_pages(sbi, F2FS_DIRTY_IMETA),
  839. free_sections(sbi),
  840. free_segments(sbi),
  841. reserved_segments(sbi),
  842. prefree_segments(sbi));
  843. cpc.reason = __get_cp_reason(sbi);
  844. gc_more:
  845. if (unlikely(!(sbi->sb->s_flags & MS_ACTIVE))) {
  846. ret = -EINVAL;
  847. goto stop;
  848. }
  849. if (unlikely(f2fs_cp_error(sbi))) {
  850. ret = -EIO;
  851. goto stop;
  852. }
  853. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) {
  854. /*
  855. * For example, if there are many prefree_segments below given
  856. * threshold, we can make them free by checkpoint. Then, we
  857. * secure free segments which doesn't need fggc any more.
  858. */
  859. if (prefree_segments(sbi)) {
  860. ret = write_checkpoint(sbi, &cpc);
  861. if (ret)
  862. goto stop;
  863. }
  864. if (has_not_enough_free_secs(sbi, 0, 0))
  865. gc_type = FG_GC;
  866. }
  867. /* f2fs_balance_fs doesn't need to do BG_GC in critical path. */
  868. if (gc_type == BG_GC && !background) {
  869. ret = -EINVAL;
  870. goto stop;
  871. }
  872. if (!__get_victim(sbi, &segno, gc_type)) {
  873. ret = -ENODATA;
  874. goto stop;
  875. }
  876. seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type);
  877. if (gc_type == FG_GC && seg_freed == sbi->segs_per_sec)
  878. sec_freed++;
  879. total_freed += seg_freed;
  880. if (gc_type == FG_GC)
  881. sbi->cur_victim_sec = NULL_SEGNO;
  882. if (!sync) {
  883. if (has_not_enough_free_secs(sbi, sec_freed, 0)) {
  884. segno = NULL_SEGNO;
  885. goto gc_more;
  886. }
  887. if (gc_type == FG_GC)
  888. ret = write_checkpoint(sbi, &cpc);
  889. }
  890. stop:
  891. SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
  892. SIT_I(sbi)->last_victim[FLUSH_DEVICE] = init_segno;
  893. trace_f2fs_gc_end(sbi->sb, ret, total_freed, sec_freed,
  894. get_pages(sbi, F2FS_DIRTY_NODES),
  895. get_pages(sbi, F2FS_DIRTY_DENTS),
  896. get_pages(sbi, F2FS_DIRTY_IMETA),
  897. free_sections(sbi),
  898. free_segments(sbi),
  899. reserved_segments(sbi),
  900. prefree_segments(sbi));
  901. mutex_unlock(&sbi->gc_mutex);
  902. put_gc_inode(&gc_list);
  903. if (sync)
  904. ret = sec_freed ? 0 : -EAGAIN;
  905. return ret;
  906. }
  907. void build_gc_manager(struct f2fs_sb_info *sbi)
  908. {
  909. u64 main_count, resv_count, ovp_count;
  910. DIRTY_I(sbi)->v_ops = &default_v_ops;
  911. /* threshold of # of valid blocks in a section for victims of FG_GC */
  912. main_count = SM_I(sbi)->main_segments << sbi->log_blocks_per_seg;
  913. resv_count = SM_I(sbi)->reserved_segments << sbi->log_blocks_per_seg;
  914. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  915. sbi->fggc_threshold = div64_u64((main_count - ovp_count) *
  916. BLKS_PER_SEC(sbi), (main_count - resv_count));
  917. /* give warm/cold data area from slower device */
  918. if (sbi->s_ndevs && sbi->segs_per_sec == 1)
  919. SIT_I(sbi)->last_victim[ALLOC_NEXT] =
  920. GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
  921. }