gc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * fs/f2fs/gc.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/module.h>
  13. #include <linux/backing-dev.h>
  14. #include <linux/init.h>
  15. #include <linux/f2fs_fs.h>
  16. #include <linux/kthread.h>
  17. #include <linux/delay.h>
  18. #include <linux/freezer.h>
  19. #include "f2fs.h"
  20. #include "node.h"
  21. #include "segment.h"
  22. #include "gc.h"
  23. #include <trace/events/f2fs.h>
  24. static int gc_thread_func(void *data)
  25. {
  26. struct f2fs_sb_info *sbi = data;
  27. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  28. wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
  29. long wait_ms;
  30. wait_ms = gc_th->min_sleep_time;
  31. do {
  32. if (try_to_freeze())
  33. continue;
  34. else
  35. wait_event_interruptible_timeout(*wq,
  36. kthread_should_stop(),
  37. msecs_to_jiffies(wait_ms));
  38. if (kthread_should_stop())
  39. break;
  40. if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) {
  41. increase_sleep_time(gc_th, &wait_ms);
  42. continue;
  43. }
  44. #ifdef CONFIG_F2FS_FAULT_INJECTION
  45. if (time_to_inject(sbi, FAULT_CHECKPOINT))
  46. f2fs_stop_checkpoint(sbi, false);
  47. #endif
  48. /*
  49. * [GC triggering condition]
  50. * 0. GC is not conducted currently.
  51. * 1. There are enough dirty segments.
  52. * 2. IO subsystem is idle by checking the # of writeback pages.
  53. * 3. IO subsystem is idle by checking the # of requests in
  54. * bdev's request list.
  55. *
  56. * Note) We have to avoid triggering GCs frequently.
  57. * Because it is possible that some segments can be
  58. * invalidated soon after by user update or deletion.
  59. * So, I'd like to wait some time to collect dirty segments.
  60. */
  61. if (!mutex_trylock(&sbi->gc_mutex))
  62. continue;
  63. if (!is_idle(sbi)) {
  64. increase_sleep_time(gc_th, &wait_ms);
  65. mutex_unlock(&sbi->gc_mutex);
  66. continue;
  67. }
  68. if (has_enough_invalid_blocks(sbi))
  69. decrease_sleep_time(gc_th, &wait_ms);
  70. else
  71. increase_sleep_time(gc_th, &wait_ms);
  72. stat_inc_bggc_count(sbi);
  73. /* if return value is not zero, no victim was selected */
  74. if (f2fs_gc(sbi, test_opt(sbi, FORCE_FG_GC)))
  75. wait_ms = gc_th->no_gc_sleep_time;
  76. trace_f2fs_background_gc(sbi->sb, wait_ms,
  77. prefree_segments(sbi), free_segments(sbi));
  78. /* balancing f2fs's metadata periodically */
  79. f2fs_balance_fs_bg(sbi);
  80. } while (!kthread_should_stop());
  81. return 0;
  82. }
  83. int start_gc_thread(struct f2fs_sb_info *sbi)
  84. {
  85. struct f2fs_gc_kthread *gc_th;
  86. dev_t dev = sbi->sb->s_bdev->bd_dev;
  87. int err = 0;
  88. gc_th = f2fs_kmalloc(sbi, sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
  89. if (!gc_th) {
  90. err = -ENOMEM;
  91. goto out;
  92. }
  93. gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME;
  94. gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
  95. gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
  96. gc_th->gc_idle = 0;
  97. sbi->gc_thread = gc_th;
  98. init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
  99. sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
  100. "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
  101. if (IS_ERR(gc_th->f2fs_gc_task)) {
  102. err = PTR_ERR(gc_th->f2fs_gc_task);
  103. kfree(gc_th);
  104. sbi->gc_thread = NULL;
  105. }
  106. out:
  107. return err;
  108. }
  109. void stop_gc_thread(struct f2fs_sb_info *sbi)
  110. {
  111. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  112. if (!gc_th)
  113. return;
  114. kthread_stop(gc_th->f2fs_gc_task);
  115. kfree(gc_th);
  116. sbi->gc_thread = NULL;
  117. }
  118. static int select_gc_type(struct f2fs_gc_kthread *gc_th, int gc_type)
  119. {
  120. int gc_mode = (gc_type == BG_GC) ? GC_CB : GC_GREEDY;
  121. if (gc_th && gc_th->gc_idle) {
  122. if (gc_th->gc_idle == 1)
  123. gc_mode = GC_CB;
  124. else if (gc_th->gc_idle == 2)
  125. gc_mode = GC_GREEDY;
  126. }
  127. return gc_mode;
  128. }
  129. static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
  130. int type, struct victim_sel_policy *p)
  131. {
  132. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  133. if (p->alloc_mode == SSR) {
  134. p->gc_mode = GC_GREEDY;
  135. p->dirty_segmap = dirty_i->dirty_segmap[type];
  136. p->max_search = dirty_i->nr_dirty[type];
  137. p->ofs_unit = 1;
  138. } else {
  139. p->gc_mode = select_gc_type(sbi->gc_thread, gc_type);
  140. p->dirty_segmap = dirty_i->dirty_segmap[DIRTY];
  141. p->max_search = dirty_i->nr_dirty[DIRTY];
  142. p->ofs_unit = sbi->segs_per_sec;
  143. }
  144. /* we need to check every dirty segments in the FG_GC case */
  145. if (gc_type != FG_GC && p->max_search > sbi->max_victim_search)
  146. p->max_search = sbi->max_victim_search;
  147. p->offset = sbi->last_victim[p->gc_mode];
  148. }
  149. static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
  150. struct victim_sel_policy *p)
  151. {
  152. /* SSR allocates in a segment unit */
  153. if (p->alloc_mode == SSR)
  154. return sbi->blocks_per_seg;
  155. if (p->gc_mode == GC_GREEDY)
  156. return sbi->blocks_per_seg * p->ofs_unit;
  157. else if (p->gc_mode == GC_CB)
  158. return UINT_MAX;
  159. else /* No other gc_mode */
  160. return 0;
  161. }
  162. static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
  163. {
  164. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  165. unsigned int secno;
  166. /*
  167. * If the gc_type is FG_GC, we can select victim segments
  168. * selected by background GC before.
  169. * Those segments guarantee they have small valid blocks.
  170. */
  171. for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
  172. if (sec_usage_check(sbi, secno))
  173. continue;
  174. if (no_fggc_candidate(sbi, secno))
  175. continue;
  176. clear_bit(secno, dirty_i->victim_secmap);
  177. return secno * sbi->segs_per_sec;
  178. }
  179. return NULL_SEGNO;
  180. }
  181. static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
  182. {
  183. struct sit_info *sit_i = SIT_I(sbi);
  184. unsigned int secno = GET_SECNO(sbi, segno);
  185. unsigned int start = secno * sbi->segs_per_sec;
  186. unsigned long long mtime = 0;
  187. unsigned int vblocks;
  188. unsigned char age = 0;
  189. unsigned char u;
  190. unsigned int i;
  191. for (i = 0; i < sbi->segs_per_sec; i++)
  192. mtime += get_seg_entry(sbi, start + i)->mtime;
  193. vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec);
  194. mtime = div_u64(mtime, sbi->segs_per_sec);
  195. vblocks = div_u64(vblocks, sbi->segs_per_sec);
  196. u = (vblocks * 100) >> sbi->log_blocks_per_seg;
  197. /* Handle if the system time has changed by the user */
  198. if (mtime < sit_i->min_mtime)
  199. sit_i->min_mtime = mtime;
  200. if (mtime > sit_i->max_mtime)
  201. sit_i->max_mtime = mtime;
  202. if (sit_i->max_mtime != sit_i->min_mtime)
  203. age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime),
  204. sit_i->max_mtime - sit_i->min_mtime);
  205. return UINT_MAX - ((100 * (100 - u) * age) / (100 + u));
  206. }
  207. static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi,
  208. unsigned int segno, struct victim_sel_policy *p)
  209. {
  210. if (p->alloc_mode == SSR)
  211. return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  212. /* alloc_mode == LFS */
  213. if (p->gc_mode == GC_GREEDY)
  214. return get_valid_blocks(sbi, segno, sbi->segs_per_sec);
  215. else
  216. return get_cb_cost(sbi, segno);
  217. }
  218. static unsigned int count_bits(const unsigned long *addr,
  219. unsigned int offset, unsigned int len)
  220. {
  221. unsigned int end = offset + len, sum = 0;
  222. while (offset < end) {
  223. if (test_bit(offset++, addr))
  224. ++sum;
  225. }
  226. return sum;
  227. }
  228. /*
  229. * This function is called from two paths.
  230. * One is garbage collection and the other is SSR segment selection.
  231. * When it is called during GC, it just gets a victim segment
  232. * and it does not remove it from dirty seglist.
  233. * When it is called from SSR segment selection, it finds a segment
  234. * which has minimum valid blocks and removes it from dirty seglist.
  235. */
  236. static int get_victim_by_default(struct f2fs_sb_info *sbi,
  237. unsigned int *result, int gc_type, int type, char alloc_mode)
  238. {
  239. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  240. struct victim_sel_policy p;
  241. unsigned int secno, last_victim;
  242. unsigned int last_segment = MAIN_SEGS(sbi);
  243. unsigned int nsearched = 0;
  244. mutex_lock(&dirty_i->seglist_lock);
  245. p.alloc_mode = alloc_mode;
  246. select_policy(sbi, gc_type, type, &p);
  247. p.min_segno = NULL_SEGNO;
  248. p.min_cost = get_max_cost(sbi, &p);
  249. if (p.max_search == 0)
  250. goto out;
  251. last_victim = sbi->last_victim[p.gc_mode];
  252. if (p.alloc_mode == LFS && gc_type == FG_GC) {
  253. p.min_segno = check_bg_victims(sbi);
  254. if (p.min_segno != NULL_SEGNO)
  255. goto got_it;
  256. }
  257. while (1) {
  258. unsigned long cost;
  259. unsigned int segno;
  260. segno = find_next_bit(p.dirty_segmap, last_segment, p.offset);
  261. if (segno >= last_segment) {
  262. if (sbi->last_victim[p.gc_mode]) {
  263. last_segment = sbi->last_victim[p.gc_mode];
  264. sbi->last_victim[p.gc_mode] = 0;
  265. p.offset = 0;
  266. continue;
  267. }
  268. break;
  269. }
  270. p.offset = segno + p.ofs_unit;
  271. if (p.ofs_unit > 1) {
  272. p.offset -= segno % p.ofs_unit;
  273. nsearched += count_bits(p.dirty_segmap,
  274. p.offset - p.ofs_unit,
  275. p.ofs_unit);
  276. } else {
  277. nsearched++;
  278. }
  279. secno = GET_SECNO(sbi, segno);
  280. if (sec_usage_check(sbi, secno))
  281. goto next;
  282. if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
  283. goto next;
  284. if (gc_type == FG_GC && p.alloc_mode == LFS &&
  285. no_fggc_candidate(sbi, secno))
  286. goto next;
  287. cost = get_gc_cost(sbi, segno, &p);
  288. if (p.min_cost > cost) {
  289. p.min_segno = segno;
  290. p.min_cost = cost;
  291. }
  292. next:
  293. if (nsearched >= p.max_search) {
  294. if (!sbi->last_victim[p.gc_mode] && segno <= last_victim)
  295. sbi->last_victim[p.gc_mode] = last_victim + 1;
  296. else
  297. sbi->last_victim[p.gc_mode] = segno + 1;
  298. break;
  299. }
  300. }
  301. if (p.min_segno != NULL_SEGNO) {
  302. got_it:
  303. if (p.alloc_mode == LFS) {
  304. secno = GET_SECNO(sbi, p.min_segno);
  305. if (gc_type == FG_GC)
  306. sbi->cur_victim_sec = secno;
  307. else
  308. set_bit(secno, dirty_i->victim_secmap);
  309. }
  310. *result = (p.min_segno / p.ofs_unit) * p.ofs_unit;
  311. trace_f2fs_get_victim(sbi->sb, type, gc_type, &p,
  312. sbi->cur_victim_sec,
  313. prefree_segments(sbi), free_segments(sbi));
  314. }
  315. out:
  316. mutex_unlock(&dirty_i->seglist_lock);
  317. return (p.min_segno == NULL_SEGNO) ? 0 : 1;
  318. }
  319. static const struct victim_selection default_v_ops = {
  320. .get_victim = get_victim_by_default,
  321. };
  322. static struct inode *find_gc_inode(struct gc_inode_list *gc_list, nid_t ino)
  323. {
  324. struct inode_entry *ie;
  325. ie = radix_tree_lookup(&gc_list->iroot, ino);
  326. if (ie)
  327. return ie->inode;
  328. return NULL;
  329. }
  330. static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode)
  331. {
  332. struct inode_entry *new_ie;
  333. if (inode == find_gc_inode(gc_list, inode->i_ino)) {
  334. iput(inode);
  335. return;
  336. }
  337. new_ie = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
  338. new_ie->inode = inode;
  339. f2fs_radix_tree_insert(&gc_list->iroot, inode->i_ino, new_ie);
  340. list_add_tail(&new_ie->list, &gc_list->ilist);
  341. }
  342. static void put_gc_inode(struct gc_inode_list *gc_list)
  343. {
  344. struct inode_entry *ie, *next_ie;
  345. list_for_each_entry_safe(ie, next_ie, &gc_list->ilist, list) {
  346. radix_tree_delete(&gc_list->iroot, ie->inode->i_ino);
  347. iput(ie->inode);
  348. list_del(&ie->list);
  349. kmem_cache_free(inode_entry_slab, ie);
  350. }
  351. }
  352. static int check_valid_map(struct f2fs_sb_info *sbi,
  353. unsigned int segno, int offset)
  354. {
  355. struct sit_info *sit_i = SIT_I(sbi);
  356. struct seg_entry *sentry;
  357. int ret;
  358. mutex_lock(&sit_i->sentry_lock);
  359. sentry = get_seg_entry(sbi, segno);
  360. ret = f2fs_test_bit(offset, sentry->cur_valid_map);
  361. mutex_unlock(&sit_i->sentry_lock);
  362. return ret;
  363. }
  364. /*
  365. * This function compares node address got in summary with that in NAT.
  366. * On validity, copy that node with cold status, otherwise (invalid node)
  367. * ignore that.
  368. */
  369. static void gc_node_segment(struct f2fs_sb_info *sbi,
  370. struct f2fs_summary *sum, unsigned int segno, int gc_type)
  371. {
  372. struct f2fs_summary *entry;
  373. block_t start_addr;
  374. int off;
  375. int phase = 0;
  376. start_addr = START_BLOCK(sbi, segno);
  377. next_step:
  378. entry = sum;
  379. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  380. nid_t nid = le32_to_cpu(entry->nid);
  381. struct page *node_page;
  382. struct node_info ni;
  383. /* stop BG_GC if there is not enough free sections. */
  384. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
  385. return;
  386. if (check_valid_map(sbi, segno, off) == 0)
  387. continue;
  388. if (phase == 0) {
  389. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
  390. META_NAT, true);
  391. continue;
  392. }
  393. if (phase == 1) {
  394. ra_node_page(sbi, nid);
  395. continue;
  396. }
  397. /* phase == 2 */
  398. node_page = get_node_page(sbi, nid);
  399. if (IS_ERR(node_page))
  400. continue;
  401. /* block may become invalid during get_node_page */
  402. if (check_valid_map(sbi, segno, off) == 0) {
  403. f2fs_put_page(node_page, 1);
  404. continue;
  405. }
  406. get_node_info(sbi, nid, &ni);
  407. if (ni.blk_addr != start_addr + off) {
  408. f2fs_put_page(node_page, 1);
  409. continue;
  410. }
  411. move_node_page(node_page, gc_type);
  412. stat_inc_node_blk_count(sbi, 1, gc_type);
  413. }
  414. if (++phase < 3)
  415. goto next_step;
  416. }
  417. /*
  418. * Calculate start block index indicating the given node offset.
  419. * Be careful, caller should give this node offset only indicating direct node
  420. * blocks. If any node offsets, which point the other types of node blocks such
  421. * as indirect or double indirect node blocks, are given, it must be a caller's
  422. * bug.
  423. */
  424. block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode)
  425. {
  426. unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
  427. unsigned int bidx;
  428. if (node_ofs == 0)
  429. return 0;
  430. if (node_ofs <= 2) {
  431. bidx = node_ofs - 1;
  432. } else if (node_ofs <= indirect_blks) {
  433. int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
  434. bidx = node_ofs - 2 - dec;
  435. } else {
  436. int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
  437. bidx = node_ofs - 5 - dec;
  438. }
  439. return bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE(inode);
  440. }
  441. static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  442. struct node_info *dni, block_t blkaddr, unsigned int *nofs)
  443. {
  444. struct page *node_page;
  445. nid_t nid;
  446. unsigned int ofs_in_node;
  447. block_t source_blkaddr;
  448. nid = le32_to_cpu(sum->nid);
  449. ofs_in_node = le16_to_cpu(sum->ofs_in_node);
  450. node_page = get_node_page(sbi, nid);
  451. if (IS_ERR(node_page))
  452. return false;
  453. get_node_info(sbi, nid, dni);
  454. if (sum->version != dni->version) {
  455. f2fs_msg(sbi->sb, KERN_WARNING,
  456. "%s: valid data with mismatched node version.",
  457. __func__);
  458. set_sbi_flag(sbi, SBI_NEED_FSCK);
  459. }
  460. *nofs = ofs_of_node(node_page);
  461. source_blkaddr = datablock_addr(node_page, ofs_in_node);
  462. f2fs_put_page(node_page, 1);
  463. if (source_blkaddr != blkaddr)
  464. return false;
  465. return true;
  466. }
  467. static void move_encrypted_block(struct inode *inode, block_t bidx)
  468. {
  469. struct f2fs_io_info fio = {
  470. .sbi = F2FS_I_SB(inode),
  471. .type = DATA,
  472. .op = REQ_OP_READ,
  473. .op_flags = READ_SYNC,
  474. .encrypted_page = NULL,
  475. };
  476. struct dnode_of_data dn;
  477. struct f2fs_summary sum;
  478. struct node_info ni;
  479. struct page *page;
  480. block_t newaddr;
  481. int err;
  482. /* do not read out */
  483. page = f2fs_grab_cache_page(inode->i_mapping, bidx, false);
  484. if (!page)
  485. return;
  486. set_new_dnode(&dn, inode, NULL, NULL, 0);
  487. err = get_dnode_of_data(&dn, bidx, LOOKUP_NODE);
  488. if (err)
  489. goto out;
  490. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  491. ClearPageUptodate(page);
  492. goto put_out;
  493. }
  494. /*
  495. * don't cache encrypted data into meta inode until previous dirty
  496. * data were writebacked to avoid racing between GC and flush.
  497. */
  498. f2fs_wait_on_page_writeback(page, DATA, true);
  499. get_node_info(fio.sbi, dn.nid, &ni);
  500. set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
  501. /* read page */
  502. fio.page = page;
  503. fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
  504. allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
  505. &sum, CURSEG_COLD_DATA);
  506. fio.encrypted_page = pagecache_get_page(META_MAPPING(fio.sbi), newaddr,
  507. FGP_LOCK | FGP_CREAT, GFP_NOFS);
  508. if (!fio.encrypted_page) {
  509. err = -ENOMEM;
  510. goto recover_block;
  511. }
  512. err = f2fs_submit_page_bio(&fio);
  513. if (err)
  514. goto put_page_out;
  515. /* write page */
  516. lock_page(fio.encrypted_page);
  517. if (unlikely(fio.encrypted_page->mapping != META_MAPPING(fio.sbi))) {
  518. err = -EIO;
  519. goto put_page_out;
  520. }
  521. if (unlikely(!PageUptodate(fio.encrypted_page))) {
  522. err = -EIO;
  523. goto put_page_out;
  524. }
  525. set_page_dirty(fio.encrypted_page);
  526. f2fs_wait_on_page_writeback(fio.encrypted_page, DATA, true);
  527. if (clear_page_dirty_for_io(fio.encrypted_page))
  528. dec_page_count(fio.sbi, F2FS_DIRTY_META);
  529. set_page_writeback(fio.encrypted_page);
  530. /* allocate block address */
  531. f2fs_wait_on_page_writeback(dn.node_page, NODE, true);
  532. fio.op = REQ_OP_WRITE;
  533. fio.op_flags = WRITE_SYNC;
  534. fio.new_blkaddr = newaddr;
  535. f2fs_submit_page_mbio(&fio);
  536. f2fs_update_data_blkaddr(&dn, newaddr);
  537. set_inode_flag(inode, FI_APPEND_WRITE);
  538. if (page->index == 0)
  539. set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
  540. put_page_out:
  541. f2fs_put_page(fio.encrypted_page, 1);
  542. recover_block:
  543. if (err)
  544. __f2fs_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
  545. true, true);
  546. put_out:
  547. f2fs_put_dnode(&dn);
  548. out:
  549. f2fs_put_page(page, 1);
  550. }
  551. static void move_data_page(struct inode *inode, block_t bidx, int gc_type)
  552. {
  553. struct page *page;
  554. page = get_lock_data_page(inode, bidx, true);
  555. if (IS_ERR(page))
  556. return;
  557. if (gc_type == BG_GC) {
  558. if (PageWriteback(page))
  559. goto out;
  560. set_page_dirty(page);
  561. set_cold_data(page);
  562. } else {
  563. struct f2fs_io_info fio = {
  564. .sbi = F2FS_I_SB(inode),
  565. .type = DATA,
  566. .op = REQ_OP_WRITE,
  567. .op_flags = WRITE_SYNC,
  568. .page = page,
  569. .encrypted_page = NULL,
  570. };
  571. bool is_dirty = PageDirty(page);
  572. int err;
  573. retry:
  574. set_page_dirty(page);
  575. f2fs_wait_on_page_writeback(page, DATA, true);
  576. if (clear_page_dirty_for_io(page))
  577. inode_dec_dirty_pages(inode);
  578. set_cold_data(page);
  579. err = do_write_data_page(&fio);
  580. if (err) {
  581. clear_cold_data(page);
  582. if (err == -ENOMEM) {
  583. congestion_wait(BLK_RW_ASYNC, HZ/50);
  584. goto retry;
  585. }
  586. if (is_dirty)
  587. set_page_dirty(page);
  588. }
  589. clear_cold_data(page);
  590. }
  591. out:
  592. f2fs_put_page(page, 1);
  593. }
  594. /*
  595. * This function tries to get parent node of victim data block, and identifies
  596. * data block validity. If the block is valid, copy that with cold status and
  597. * modify parent node.
  598. * If the parent node is not valid or the data block address is different,
  599. * the victim data block is ignored.
  600. */
  601. static void gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  602. struct gc_inode_list *gc_list, unsigned int segno, int gc_type)
  603. {
  604. struct super_block *sb = sbi->sb;
  605. struct f2fs_summary *entry;
  606. block_t start_addr;
  607. int off;
  608. int phase = 0;
  609. start_addr = START_BLOCK(sbi, segno);
  610. next_step:
  611. entry = sum;
  612. for (off = 0; off < sbi->blocks_per_seg; off++, entry++) {
  613. struct page *data_page;
  614. struct inode *inode;
  615. struct node_info dni; /* dnode info for the data */
  616. unsigned int ofs_in_node, nofs;
  617. block_t start_bidx;
  618. nid_t nid = le32_to_cpu(entry->nid);
  619. /* stop BG_GC if there is not enough free sections. */
  620. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
  621. return;
  622. if (check_valid_map(sbi, segno, off) == 0)
  623. continue;
  624. if (phase == 0) {
  625. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
  626. META_NAT, true);
  627. continue;
  628. }
  629. if (phase == 1) {
  630. ra_node_page(sbi, nid);
  631. continue;
  632. }
  633. /* Get an inode by ino with checking validity */
  634. if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs))
  635. continue;
  636. if (phase == 2) {
  637. ra_node_page(sbi, dni.ino);
  638. continue;
  639. }
  640. ofs_in_node = le16_to_cpu(entry->ofs_in_node);
  641. if (phase == 3) {
  642. inode = f2fs_iget(sb, dni.ino);
  643. if (IS_ERR(inode) || is_bad_inode(inode))
  644. continue;
  645. /* if encrypted inode, let's go phase 3 */
  646. if (f2fs_encrypted_inode(inode) &&
  647. S_ISREG(inode->i_mode)) {
  648. add_gc_inode(gc_list, inode);
  649. continue;
  650. }
  651. start_bidx = start_bidx_of_node(nofs, inode);
  652. data_page = get_read_data_page(inode,
  653. start_bidx + ofs_in_node, REQ_RAHEAD,
  654. true);
  655. if (IS_ERR(data_page)) {
  656. iput(inode);
  657. continue;
  658. }
  659. f2fs_put_page(data_page, 0);
  660. add_gc_inode(gc_list, inode);
  661. continue;
  662. }
  663. /* phase 4 */
  664. inode = find_gc_inode(gc_list, dni.ino);
  665. if (inode) {
  666. struct f2fs_inode_info *fi = F2FS_I(inode);
  667. bool locked = false;
  668. if (S_ISREG(inode->i_mode)) {
  669. if (!down_write_trylock(&fi->dio_rwsem[READ]))
  670. continue;
  671. if (!down_write_trylock(
  672. &fi->dio_rwsem[WRITE])) {
  673. up_write(&fi->dio_rwsem[READ]);
  674. continue;
  675. }
  676. locked = true;
  677. }
  678. start_bidx = start_bidx_of_node(nofs, inode)
  679. + ofs_in_node;
  680. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  681. move_encrypted_block(inode, start_bidx);
  682. else
  683. move_data_page(inode, start_bidx, gc_type);
  684. if (locked) {
  685. up_write(&fi->dio_rwsem[WRITE]);
  686. up_write(&fi->dio_rwsem[READ]);
  687. }
  688. stat_inc_data_blk_count(sbi, 1, gc_type);
  689. }
  690. }
  691. if (++phase < 5)
  692. goto next_step;
  693. }
  694. static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
  695. int gc_type)
  696. {
  697. struct sit_info *sit_i = SIT_I(sbi);
  698. int ret;
  699. mutex_lock(&sit_i->sentry_lock);
  700. ret = DIRTY_I(sbi)->v_ops->get_victim(sbi, victim, gc_type,
  701. NO_CHECK_TYPE, LFS);
  702. mutex_unlock(&sit_i->sentry_lock);
  703. return ret;
  704. }
  705. static int do_garbage_collect(struct f2fs_sb_info *sbi,
  706. unsigned int start_segno,
  707. struct gc_inode_list *gc_list, int gc_type)
  708. {
  709. struct page *sum_page;
  710. struct f2fs_summary_block *sum;
  711. struct blk_plug plug;
  712. unsigned int segno = start_segno;
  713. unsigned int end_segno = start_segno + sbi->segs_per_sec;
  714. int sec_freed = 0;
  715. unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
  716. SUM_TYPE_DATA : SUM_TYPE_NODE;
  717. /* readahead multi ssa blocks those have contiguous address */
  718. if (sbi->segs_per_sec > 1)
  719. ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno),
  720. sbi->segs_per_sec, META_SSA, true);
  721. /* reference all summary page */
  722. while (segno < end_segno) {
  723. sum_page = get_sum_page(sbi, segno++);
  724. unlock_page(sum_page);
  725. }
  726. blk_start_plug(&plug);
  727. for (segno = start_segno; segno < end_segno; segno++) {
  728. /* find segment summary of victim */
  729. sum_page = find_get_page(META_MAPPING(sbi),
  730. GET_SUM_BLOCK(sbi, segno));
  731. f2fs_put_page(sum_page, 0);
  732. if (get_valid_blocks(sbi, segno, 1) == 0 ||
  733. !PageUptodate(sum_page) ||
  734. unlikely(f2fs_cp_error(sbi)))
  735. goto next;
  736. sum = page_address(sum_page);
  737. f2fs_bug_on(sbi, type != GET_SUM_TYPE((&sum->footer)));
  738. /*
  739. * this is to avoid deadlock:
  740. * - lock_page(sum_page) - f2fs_replace_block
  741. * - check_valid_map() - mutex_lock(sentry_lock)
  742. * - mutex_lock(sentry_lock) - change_curseg()
  743. * - lock_page(sum_page)
  744. */
  745. if (type == SUM_TYPE_NODE)
  746. gc_node_segment(sbi, sum->entries, segno, gc_type);
  747. else
  748. gc_data_segment(sbi, sum->entries, gc_list, segno,
  749. gc_type);
  750. stat_inc_seg_count(sbi, type, gc_type);
  751. next:
  752. f2fs_put_page(sum_page, 0);
  753. }
  754. if (gc_type == FG_GC)
  755. f2fs_submit_merged_bio(sbi,
  756. (type == SUM_TYPE_NODE) ? NODE : DATA, WRITE);
  757. blk_finish_plug(&plug);
  758. if (gc_type == FG_GC &&
  759. get_valid_blocks(sbi, start_segno, sbi->segs_per_sec) == 0)
  760. sec_freed = 1;
  761. stat_inc_call_count(sbi->stat_info);
  762. return sec_freed;
  763. }
  764. int f2fs_gc(struct f2fs_sb_info *sbi, bool sync)
  765. {
  766. unsigned int segno;
  767. int gc_type = sync ? FG_GC : BG_GC;
  768. int sec_freed = 0;
  769. int ret = -EINVAL;
  770. struct cp_control cpc;
  771. struct gc_inode_list gc_list = {
  772. .ilist = LIST_HEAD_INIT(gc_list.ilist),
  773. .iroot = RADIX_TREE_INIT(GFP_NOFS),
  774. };
  775. cpc.reason = __get_cp_reason(sbi);
  776. gc_more:
  777. segno = NULL_SEGNO;
  778. if (unlikely(!(sbi->sb->s_flags & MS_ACTIVE)))
  779. goto stop;
  780. if (unlikely(f2fs_cp_error(sbi))) {
  781. ret = -EIO;
  782. goto stop;
  783. }
  784. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, sec_freed, 0)) {
  785. gc_type = FG_GC;
  786. /*
  787. * If there is no victim and no prefree segment but still not
  788. * enough free sections, we should flush dent/node blocks and do
  789. * garbage collections.
  790. */
  791. if (__get_victim(sbi, &segno, gc_type) ||
  792. prefree_segments(sbi)) {
  793. ret = write_checkpoint(sbi, &cpc);
  794. if (ret)
  795. goto stop;
  796. segno = NULL_SEGNO;
  797. } else if (has_not_enough_free_secs(sbi, 0, 0)) {
  798. ret = write_checkpoint(sbi, &cpc);
  799. if (ret)
  800. goto stop;
  801. }
  802. }
  803. if (segno == NULL_SEGNO && !__get_victim(sbi, &segno, gc_type))
  804. goto stop;
  805. ret = 0;
  806. if (do_garbage_collect(sbi, segno, &gc_list, gc_type) &&
  807. gc_type == FG_GC)
  808. sec_freed++;
  809. if (gc_type == FG_GC)
  810. sbi->cur_victim_sec = NULL_SEGNO;
  811. if (!sync) {
  812. if (has_not_enough_free_secs(sbi, sec_freed, 0))
  813. goto gc_more;
  814. if (gc_type == FG_GC)
  815. ret = write_checkpoint(sbi, &cpc);
  816. }
  817. stop:
  818. mutex_unlock(&sbi->gc_mutex);
  819. put_gc_inode(&gc_list);
  820. if (sync)
  821. ret = sec_freed ? 0 : -EAGAIN;
  822. return ret;
  823. }
  824. void build_gc_manager(struct f2fs_sb_info *sbi)
  825. {
  826. u64 main_count, resv_count, ovp_count, blocks_per_sec;
  827. DIRTY_I(sbi)->v_ops = &default_v_ops;
  828. /* threshold of # of valid blocks in a section for victims of FG_GC */
  829. main_count = SM_I(sbi)->main_segments << sbi->log_blocks_per_seg;
  830. resv_count = SM_I(sbi)->reserved_segments << sbi->log_blocks_per_seg;
  831. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  832. blocks_per_sec = sbi->blocks_per_seg * sbi->segs_per_sec;
  833. sbi->fggc_threshold = div_u64((main_count - ovp_count) * blocks_per_sec,
  834. (main_count - resv_count));
  835. }