gc.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/gc.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/f2fs_fs.h>
  12. #include <linux/kthread.h>
  13. #include <linux/delay.h>
  14. #include <linux/freezer.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/random.h>
  17. #include <linux/sched/mm.h>
  18. #include "f2fs.h"
  19. #include "node.h"
  20. #include "segment.h"
  21. #include "gc.h"
  22. #include "iostat.h"
  23. #include <trace/events/f2fs.h>
  24. static struct kmem_cache *victim_entry_slab;
  25. static unsigned int count_bits(const unsigned long *addr,
  26. unsigned int offset, unsigned int len);
  27. static int gc_thread_func(void *data)
  28. {
  29. struct f2fs_sb_info *sbi = data;
  30. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  31. wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
  32. wait_queue_head_t *fggc_wq = &sbi->gc_thread->fggc_wq;
  33. unsigned int wait_ms;
  34. struct f2fs_gc_control gc_control = {
  35. .victim_segno = NULL_SEGNO,
  36. .should_migrate_blocks = false,
  37. .err_gc_skipped = false };
  38. wait_ms = gc_th->min_sleep_time;
  39. set_freezable();
  40. do {
  41. bool sync_mode, foreground = false;
  42. wait_event_interruptible_timeout(*wq,
  43. kthread_should_stop() || freezing(current) ||
  44. waitqueue_active(fggc_wq) ||
  45. gc_th->gc_wake,
  46. msecs_to_jiffies(wait_ms));
  47. if (test_opt(sbi, GC_MERGE) && waitqueue_active(fggc_wq))
  48. foreground = true;
  49. /* give it a try one time */
  50. if (gc_th->gc_wake)
  51. gc_th->gc_wake = false;
  52. if (try_to_freeze()) {
  53. stat_other_skip_bggc_count(sbi);
  54. continue;
  55. }
  56. if (kthread_should_stop())
  57. break;
  58. if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) {
  59. increase_sleep_time(gc_th, &wait_ms);
  60. stat_other_skip_bggc_count(sbi);
  61. continue;
  62. }
  63. if (time_to_inject(sbi, FAULT_CHECKPOINT))
  64. f2fs_stop_checkpoint(sbi, false,
  65. STOP_CP_REASON_FAULT_INJECT);
  66. if (!sb_start_write_trylock(sbi->sb)) {
  67. stat_other_skip_bggc_count(sbi);
  68. continue;
  69. }
  70. /*
  71. * [GC triggering condition]
  72. * 0. GC is not conducted currently.
  73. * 1. There are enough dirty segments.
  74. * 2. IO subsystem is idle by checking the # of writeback pages.
  75. * 3. IO subsystem is idle by checking the # of requests in
  76. * bdev's request list.
  77. *
  78. * Note) We have to avoid triggering GCs frequently.
  79. * Because it is possible that some segments can be
  80. * invalidated soon after by user update or deletion.
  81. * So, I'd like to wait some time to collect dirty segments.
  82. */
  83. if (sbi->gc_mode == GC_URGENT_HIGH ||
  84. sbi->gc_mode == GC_URGENT_MID) {
  85. wait_ms = gc_th->urgent_sleep_time;
  86. f2fs_down_write(&sbi->gc_lock);
  87. goto do_gc;
  88. }
  89. if (foreground) {
  90. f2fs_down_write(&sbi->gc_lock);
  91. goto do_gc;
  92. } else if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
  93. stat_other_skip_bggc_count(sbi);
  94. goto next;
  95. }
  96. if (!is_idle(sbi, GC_TIME)) {
  97. increase_sleep_time(gc_th, &wait_ms);
  98. f2fs_up_write(&sbi->gc_lock);
  99. stat_io_skip_bggc_count(sbi);
  100. goto next;
  101. }
  102. if (has_enough_invalid_blocks(sbi))
  103. decrease_sleep_time(gc_th, &wait_ms);
  104. else
  105. increase_sleep_time(gc_th, &wait_ms);
  106. do_gc:
  107. if (!foreground)
  108. stat_inc_bggc_count(sbi->stat_info);
  109. sync_mode = F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC;
  110. /* foreground GC was been triggered via f2fs_balance_fs() */
  111. if (foreground)
  112. sync_mode = false;
  113. gc_control.init_gc_type = sync_mode ? FG_GC : BG_GC;
  114. gc_control.no_bg_gc = foreground;
  115. gc_control.nr_free_secs = foreground ? 1 : 0;
  116. /* if return value is not zero, no victim was selected */
  117. if (f2fs_gc(sbi, &gc_control)) {
  118. /* don't bother wait_ms by foreground gc */
  119. if (!foreground)
  120. wait_ms = gc_th->no_gc_sleep_time;
  121. } else {
  122. /* reset wait_ms to default sleep time */
  123. if (wait_ms == gc_th->no_gc_sleep_time)
  124. wait_ms = gc_th->min_sleep_time;
  125. }
  126. if (foreground)
  127. wake_up_all(&gc_th->fggc_wq);
  128. trace_f2fs_background_gc(sbi->sb, wait_ms,
  129. prefree_segments(sbi), free_segments(sbi));
  130. /* balancing f2fs's metadata periodically */
  131. f2fs_balance_fs_bg(sbi, true);
  132. next:
  133. if (sbi->gc_mode != GC_NORMAL) {
  134. spin_lock(&sbi->gc_remaining_trials_lock);
  135. if (sbi->gc_remaining_trials) {
  136. sbi->gc_remaining_trials--;
  137. if (!sbi->gc_remaining_trials)
  138. sbi->gc_mode = GC_NORMAL;
  139. }
  140. spin_unlock(&sbi->gc_remaining_trials_lock);
  141. }
  142. sb_end_write(sbi->sb);
  143. } while (!kthread_should_stop());
  144. return 0;
  145. }
  146. int f2fs_start_gc_thread(struct f2fs_sb_info *sbi)
  147. {
  148. struct f2fs_gc_kthread *gc_th;
  149. dev_t dev = sbi->sb->s_bdev->bd_dev;
  150. gc_th = f2fs_kmalloc(sbi, sizeof(struct f2fs_gc_kthread), GFP_KERNEL);
  151. if (!gc_th)
  152. return -ENOMEM;
  153. gc_th->urgent_sleep_time = DEF_GC_THREAD_URGENT_SLEEP_TIME;
  154. gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME;
  155. gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
  156. gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
  157. gc_th->gc_wake = false;
  158. sbi->gc_thread = gc_th;
  159. init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
  160. init_waitqueue_head(&sbi->gc_thread->fggc_wq);
  161. sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
  162. "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev));
  163. if (IS_ERR(gc_th->f2fs_gc_task)) {
  164. int err = PTR_ERR(gc_th->f2fs_gc_task);
  165. kfree(gc_th);
  166. sbi->gc_thread = NULL;
  167. return err;
  168. }
  169. return 0;
  170. }
  171. void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi)
  172. {
  173. struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
  174. if (!gc_th)
  175. return;
  176. kthread_stop(gc_th->f2fs_gc_task);
  177. wake_up_all(&gc_th->fggc_wq);
  178. kfree(gc_th);
  179. sbi->gc_thread = NULL;
  180. }
  181. static int select_gc_type(struct f2fs_sb_info *sbi, int gc_type)
  182. {
  183. int gc_mode;
  184. if (gc_type == BG_GC) {
  185. if (sbi->am.atgc_enabled)
  186. gc_mode = GC_AT;
  187. else
  188. gc_mode = GC_CB;
  189. } else {
  190. gc_mode = GC_GREEDY;
  191. }
  192. switch (sbi->gc_mode) {
  193. case GC_IDLE_CB:
  194. gc_mode = GC_CB;
  195. break;
  196. case GC_IDLE_GREEDY:
  197. case GC_URGENT_HIGH:
  198. gc_mode = GC_GREEDY;
  199. break;
  200. case GC_IDLE_AT:
  201. gc_mode = GC_AT;
  202. break;
  203. }
  204. return gc_mode;
  205. }
  206. static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
  207. int type, struct victim_sel_policy *p)
  208. {
  209. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  210. if (p->alloc_mode == SSR) {
  211. p->gc_mode = GC_GREEDY;
  212. p->dirty_bitmap = dirty_i->dirty_segmap[type];
  213. p->max_search = dirty_i->nr_dirty[type];
  214. p->ofs_unit = 1;
  215. } else if (p->alloc_mode == AT_SSR) {
  216. p->gc_mode = GC_GREEDY;
  217. p->dirty_bitmap = dirty_i->dirty_segmap[type];
  218. p->max_search = dirty_i->nr_dirty[type];
  219. p->ofs_unit = 1;
  220. } else {
  221. p->gc_mode = select_gc_type(sbi, gc_type);
  222. p->ofs_unit = sbi->segs_per_sec;
  223. if (__is_large_section(sbi)) {
  224. p->dirty_bitmap = dirty_i->dirty_secmap;
  225. p->max_search = count_bits(p->dirty_bitmap,
  226. 0, MAIN_SECS(sbi));
  227. } else {
  228. p->dirty_bitmap = dirty_i->dirty_segmap[DIRTY];
  229. p->max_search = dirty_i->nr_dirty[DIRTY];
  230. }
  231. }
  232. /*
  233. * adjust candidates range, should select all dirty segments for
  234. * foreground GC and urgent GC cases.
  235. */
  236. if (gc_type != FG_GC &&
  237. (sbi->gc_mode != GC_URGENT_HIGH) &&
  238. (p->gc_mode != GC_AT && p->alloc_mode != AT_SSR) &&
  239. p->max_search > sbi->max_victim_search)
  240. p->max_search = sbi->max_victim_search;
  241. /* let's select beginning hot/small space first in no_heap mode*/
  242. if (f2fs_need_rand_seg(sbi))
  243. p->offset = prandom_u32_max(MAIN_SECS(sbi) * sbi->segs_per_sec);
  244. else if (test_opt(sbi, NOHEAP) &&
  245. (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
  246. p->offset = 0;
  247. else
  248. p->offset = SIT_I(sbi)->last_victim[p->gc_mode];
  249. }
  250. static unsigned int get_max_cost(struct f2fs_sb_info *sbi,
  251. struct victim_sel_policy *p)
  252. {
  253. /* SSR allocates in a segment unit */
  254. if (p->alloc_mode == SSR)
  255. return sbi->blocks_per_seg;
  256. else if (p->alloc_mode == AT_SSR)
  257. return UINT_MAX;
  258. /* LFS */
  259. if (p->gc_mode == GC_GREEDY)
  260. return 2 * sbi->blocks_per_seg * p->ofs_unit;
  261. else if (p->gc_mode == GC_CB)
  262. return UINT_MAX;
  263. else if (p->gc_mode == GC_AT)
  264. return UINT_MAX;
  265. else /* No other gc_mode */
  266. return 0;
  267. }
  268. static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
  269. {
  270. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  271. unsigned int secno;
  272. /*
  273. * If the gc_type is FG_GC, we can select victim segments
  274. * selected by background GC before.
  275. * Those segments guarantee they have small valid blocks.
  276. */
  277. for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
  278. if (sec_usage_check(sbi, secno))
  279. continue;
  280. clear_bit(secno, dirty_i->victim_secmap);
  281. return GET_SEG_FROM_SEC(sbi, secno);
  282. }
  283. return NULL_SEGNO;
  284. }
  285. static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno)
  286. {
  287. struct sit_info *sit_i = SIT_I(sbi);
  288. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  289. unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
  290. unsigned long long mtime = 0;
  291. unsigned int vblocks;
  292. unsigned char age = 0;
  293. unsigned char u;
  294. unsigned int i;
  295. unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi, segno);
  296. for (i = 0; i < usable_segs_per_sec; i++)
  297. mtime += get_seg_entry(sbi, start + i)->mtime;
  298. vblocks = get_valid_blocks(sbi, segno, true);
  299. mtime = div_u64(mtime, usable_segs_per_sec);
  300. vblocks = div_u64(vblocks, usable_segs_per_sec);
  301. u = (vblocks * 100) >> sbi->log_blocks_per_seg;
  302. /* Handle if the system time has changed by the user */
  303. if (mtime < sit_i->min_mtime)
  304. sit_i->min_mtime = mtime;
  305. if (mtime > sit_i->max_mtime)
  306. sit_i->max_mtime = mtime;
  307. if (sit_i->max_mtime != sit_i->min_mtime)
  308. age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime),
  309. sit_i->max_mtime - sit_i->min_mtime);
  310. return UINT_MAX - ((100 * (100 - u) * age) / (100 + u));
  311. }
  312. static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi,
  313. unsigned int segno, struct victim_sel_policy *p)
  314. {
  315. if (p->alloc_mode == SSR)
  316. return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  317. /* alloc_mode == LFS */
  318. if (p->gc_mode == GC_GREEDY)
  319. return get_valid_blocks(sbi, segno, true);
  320. else if (p->gc_mode == GC_CB)
  321. return get_cb_cost(sbi, segno);
  322. f2fs_bug_on(sbi, 1);
  323. return 0;
  324. }
  325. static unsigned int count_bits(const unsigned long *addr,
  326. unsigned int offset, unsigned int len)
  327. {
  328. unsigned int end = offset + len, sum = 0;
  329. while (offset < end) {
  330. if (test_bit(offset++, addr))
  331. ++sum;
  332. }
  333. return sum;
  334. }
  335. static bool f2fs_check_victim_tree(struct f2fs_sb_info *sbi,
  336. struct rb_root_cached *root)
  337. {
  338. #ifdef CONFIG_F2FS_CHECK_FS
  339. struct rb_node *cur = rb_first_cached(root), *next;
  340. struct victim_entry *cur_ve, *next_ve;
  341. while (cur) {
  342. next = rb_next(cur);
  343. if (!next)
  344. return true;
  345. cur_ve = rb_entry(cur, struct victim_entry, rb_node);
  346. next_ve = rb_entry(next, struct victim_entry, rb_node);
  347. if (cur_ve->mtime > next_ve->mtime) {
  348. f2fs_info(sbi, "broken victim_rbtree, "
  349. "cur_mtime(%llu) next_mtime(%llu)",
  350. cur_ve->mtime, next_ve->mtime);
  351. return false;
  352. }
  353. cur = next;
  354. }
  355. #endif
  356. return true;
  357. }
  358. static struct victim_entry *__lookup_victim_entry(struct f2fs_sb_info *sbi,
  359. unsigned long long mtime)
  360. {
  361. struct atgc_management *am = &sbi->am;
  362. struct rb_node *node = am->root.rb_root.rb_node;
  363. struct victim_entry *ve = NULL;
  364. while (node) {
  365. ve = rb_entry(node, struct victim_entry, rb_node);
  366. if (mtime < ve->mtime)
  367. node = node->rb_left;
  368. else
  369. node = node->rb_right;
  370. }
  371. return ve;
  372. }
  373. static struct victim_entry *__create_victim_entry(struct f2fs_sb_info *sbi,
  374. unsigned long long mtime, unsigned int segno)
  375. {
  376. struct atgc_management *am = &sbi->am;
  377. struct victim_entry *ve;
  378. ve = f2fs_kmem_cache_alloc(victim_entry_slab, GFP_NOFS, true, NULL);
  379. ve->mtime = mtime;
  380. ve->segno = segno;
  381. list_add_tail(&ve->list, &am->victim_list);
  382. am->victim_count++;
  383. return ve;
  384. }
  385. static void __insert_victim_entry(struct f2fs_sb_info *sbi,
  386. unsigned long long mtime, unsigned int segno)
  387. {
  388. struct atgc_management *am = &sbi->am;
  389. struct rb_root_cached *root = &am->root;
  390. struct rb_node **p = &root->rb_root.rb_node;
  391. struct rb_node *parent = NULL;
  392. struct victim_entry *ve;
  393. bool left_most = true;
  394. /* look up rb tree to find parent node */
  395. while (*p) {
  396. parent = *p;
  397. ve = rb_entry(parent, struct victim_entry, rb_node);
  398. if (mtime < ve->mtime) {
  399. p = &(*p)->rb_left;
  400. } else {
  401. p = &(*p)->rb_right;
  402. left_most = false;
  403. }
  404. }
  405. ve = __create_victim_entry(sbi, mtime, segno);
  406. rb_link_node(&ve->rb_node, parent, p);
  407. rb_insert_color_cached(&ve->rb_node, root, left_most);
  408. }
  409. static void add_victim_entry(struct f2fs_sb_info *sbi,
  410. struct victim_sel_policy *p, unsigned int segno)
  411. {
  412. struct sit_info *sit_i = SIT_I(sbi);
  413. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  414. unsigned int start = GET_SEG_FROM_SEC(sbi, secno);
  415. unsigned long long mtime = 0;
  416. unsigned int i;
  417. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
  418. if (p->gc_mode == GC_AT &&
  419. get_valid_blocks(sbi, segno, true) == 0)
  420. return;
  421. }
  422. for (i = 0; i < sbi->segs_per_sec; i++)
  423. mtime += get_seg_entry(sbi, start + i)->mtime;
  424. mtime = div_u64(mtime, sbi->segs_per_sec);
  425. /* Handle if the system time has changed by the user */
  426. if (mtime < sit_i->min_mtime)
  427. sit_i->min_mtime = mtime;
  428. if (mtime > sit_i->max_mtime)
  429. sit_i->max_mtime = mtime;
  430. if (mtime < sit_i->dirty_min_mtime)
  431. sit_i->dirty_min_mtime = mtime;
  432. if (mtime > sit_i->dirty_max_mtime)
  433. sit_i->dirty_max_mtime = mtime;
  434. /* don't choose young section as candidate */
  435. if (sit_i->dirty_max_mtime - mtime < p->age_threshold)
  436. return;
  437. __insert_victim_entry(sbi, mtime, segno);
  438. }
  439. static void atgc_lookup_victim(struct f2fs_sb_info *sbi,
  440. struct victim_sel_policy *p)
  441. {
  442. struct sit_info *sit_i = SIT_I(sbi);
  443. struct atgc_management *am = &sbi->am;
  444. struct rb_root_cached *root = &am->root;
  445. struct rb_node *node;
  446. struct victim_entry *ve;
  447. unsigned long long total_time;
  448. unsigned long long age, u, accu;
  449. unsigned long long max_mtime = sit_i->dirty_max_mtime;
  450. unsigned long long min_mtime = sit_i->dirty_min_mtime;
  451. unsigned int sec_blocks = CAP_BLKS_PER_SEC(sbi);
  452. unsigned int vblocks;
  453. unsigned int dirty_threshold = max(am->max_candidate_count,
  454. am->candidate_ratio *
  455. am->victim_count / 100);
  456. unsigned int age_weight = am->age_weight;
  457. unsigned int cost;
  458. unsigned int iter = 0;
  459. if (max_mtime < min_mtime)
  460. return;
  461. max_mtime += 1;
  462. total_time = max_mtime - min_mtime;
  463. accu = div64_u64(ULLONG_MAX, total_time);
  464. accu = min_t(unsigned long long, div_u64(accu, 100),
  465. DEFAULT_ACCURACY_CLASS);
  466. node = rb_first_cached(root);
  467. next:
  468. ve = rb_entry_safe(node, struct victim_entry, rb_node);
  469. if (!ve)
  470. return;
  471. if (ve->mtime >= max_mtime || ve->mtime < min_mtime)
  472. goto skip;
  473. /* age = 10000 * x% * 60 */
  474. age = div64_u64(accu * (max_mtime - ve->mtime), total_time) *
  475. age_weight;
  476. vblocks = get_valid_blocks(sbi, ve->segno, true);
  477. f2fs_bug_on(sbi, !vblocks || vblocks == sec_blocks);
  478. /* u = 10000 * x% * 40 */
  479. u = div64_u64(accu * (sec_blocks - vblocks), sec_blocks) *
  480. (100 - age_weight);
  481. f2fs_bug_on(sbi, age + u >= UINT_MAX);
  482. cost = UINT_MAX - (age + u);
  483. iter++;
  484. if (cost < p->min_cost ||
  485. (cost == p->min_cost && age > p->oldest_age)) {
  486. p->min_cost = cost;
  487. p->oldest_age = age;
  488. p->min_segno = ve->segno;
  489. }
  490. skip:
  491. if (iter < dirty_threshold) {
  492. node = rb_next(node);
  493. goto next;
  494. }
  495. }
  496. /*
  497. * select candidates around source section in range of
  498. * [target - dirty_threshold, target + dirty_threshold]
  499. */
  500. static void atssr_lookup_victim(struct f2fs_sb_info *sbi,
  501. struct victim_sel_policy *p)
  502. {
  503. struct sit_info *sit_i = SIT_I(sbi);
  504. struct atgc_management *am = &sbi->am;
  505. struct victim_entry *ve;
  506. unsigned long long age;
  507. unsigned long long max_mtime = sit_i->dirty_max_mtime;
  508. unsigned long long min_mtime = sit_i->dirty_min_mtime;
  509. unsigned int seg_blocks = sbi->blocks_per_seg;
  510. unsigned int vblocks;
  511. unsigned int dirty_threshold = max(am->max_candidate_count,
  512. am->candidate_ratio *
  513. am->victim_count / 100);
  514. unsigned int cost, iter;
  515. int stage = 0;
  516. if (max_mtime < min_mtime)
  517. return;
  518. max_mtime += 1;
  519. next_stage:
  520. iter = 0;
  521. ve = __lookup_victim_entry(sbi, p->age);
  522. next_node:
  523. if (!ve) {
  524. if (stage++ == 0)
  525. goto next_stage;
  526. return;
  527. }
  528. if (ve->mtime >= max_mtime || ve->mtime < min_mtime)
  529. goto skip_node;
  530. age = max_mtime - ve->mtime;
  531. vblocks = get_seg_entry(sbi, ve->segno)->ckpt_valid_blocks;
  532. f2fs_bug_on(sbi, !vblocks);
  533. /* rare case */
  534. if (vblocks == seg_blocks)
  535. goto skip_node;
  536. iter++;
  537. age = max_mtime - abs(p->age - age);
  538. cost = UINT_MAX - vblocks;
  539. if (cost < p->min_cost ||
  540. (cost == p->min_cost && age > p->oldest_age)) {
  541. p->min_cost = cost;
  542. p->oldest_age = age;
  543. p->min_segno = ve->segno;
  544. }
  545. skip_node:
  546. if (iter < dirty_threshold) {
  547. ve = rb_entry(stage == 0 ? rb_prev(&ve->rb_node) :
  548. rb_next(&ve->rb_node),
  549. struct victim_entry, rb_node);
  550. goto next_node;
  551. }
  552. if (stage++ == 0)
  553. goto next_stage;
  554. }
  555. static void lookup_victim_by_age(struct f2fs_sb_info *sbi,
  556. struct victim_sel_policy *p)
  557. {
  558. f2fs_bug_on(sbi, !f2fs_check_victim_tree(sbi, &sbi->am.root));
  559. if (p->gc_mode == GC_AT)
  560. atgc_lookup_victim(sbi, p);
  561. else if (p->alloc_mode == AT_SSR)
  562. atssr_lookup_victim(sbi, p);
  563. else
  564. f2fs_bug_on(sbi, 1);
  565. }
  566. static void release_victim_entry(struct f2fs_sb_info *sbi)
  567. {
  568. struct atgc_management *am = &sbi->am;
  569. struct victim_entry *ve, *tmp;
  570. list_for_each_entry_safe(ve, tmp, &am->victim_list, list) {
  571. list_del(&ve->list);
  572. kmem_cache_free(victim_entry_slab, ve);
  573. am->victim_count--;
  574. }
  575. am->root = RB_ROOT_CACHED;
  576. f2fs_bug_on(sbi, am->victim_count);
  577. f2fs_bug_on(sbi, !list_empty(&am->victim_list));
  578. }
  579. static bool f2fs_pin_section(struct f2fs_sb_info *sbi, unsigned int segno)
  580. {
  581. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  582. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  583. if (!dirty_i->enable_pin_section)
  584. return false;
  585. if (!test_and_set_bit(secno, dirty_i->pinned_secmap))
  586. dirty_i->pinned_secmap_cnt++;
  587. return true;
  588. }
  589. static bool f2fs_pinned_section_exists(struct dirty_seglist_info *dirty_i)
  590. {
  591. return dirty_i->pinned_secmap_cnt;
  592. }
  593. static bool f2fs_section_is_pinned(struct dirty_seglist_info *dirty_i,
  594. unsigned int secno)
  595. {
  596. return dirty_i->enable_pin_section &&
  597. f2fs_pinned_section_exists(dirty_i) &&
  598. test_bit(secno, dirty_i->pinned_secmap);
  599. }
  600. static void f2fs_unpin_all_sections(struct f2fs_sb_info *sbi, bool enable)
  601. {
  602. unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  603. if (f2fs_pinned_section_exists(DIRTY_I(sbi))) {
  604. memset(DIRTY_I(sbi)->pinned_secmap, 0, bitmap_size);
  605. DIRTY_I(sbi)->pinned_secmap_cnt = 0;
  606. }
  607. DIRTY_I(sbi)->enable_pin_section = enable;
  608. }
  609. static int f2fs_gc_pinned_control(struct inode *inode, int gc_type,
  610. unsigned int segno)
  611. {
  612. if (!f2fs_is_pinned_file(inode))
  613. return 0;
  614. if (gc_type != FG_GC)
  615. return -EBUSY;
  616. if (!f2fs_pin_section(F2FS_I_SB(inode), segno))
  617. f2fs_pin_file_control(inode, true);
  618. return -EAGAIN;
  619. }
  620. /*
  621. * This function is called from two paths.
  622. * One is garbage collection and the other is SSR segment selection.
  623. * When it is called during GC, it just gets a victim segment
  624. * and it does not remove it from dirty seglist.
  625. * When it is called from SSR segment selection, it finds a segment
  626. * which has minimum valid blocks and removes it from dirty seglist.
  627. */
  628. int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
  629. int gc_type, int type, char alloc_mode,
  630. unsigned long long age)
  631. {
  632. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  633. struct sit_info *sm = SIT_I(sbi);
  634. struct victim_sel_policy p;
  635. unsigned int secno, last_victim;
  636. unsigned int last_segment;
  637. unsigned int nsearched;
  638. bool is_atgc;
  639. int ret = 0;
  640. mutex_lock(&dirty_i->seglist_lock);
  641. last_segment = MAIN_SECS(sbi) * sbi->segs_per_sec;
  642. p.alloc_mode = alloc_mode;
  643. p.age = age;
  644. p.age_threshold = sbi->am.age_threshold;
  645. retry:
  646. select_policy(sbi, gc_type, type, &p);
  647. p.min_segno = NULL_SEGNO;
  648. p.oldest_age = 0;
  649. p.min_cost = get_max_cost(sbi, &p);
  650. is_atgc = (p.gc_mode == GC_AT || p.alloc_mode == AT_SSR);
  651. nsearched = 0;
  652. if (is_atgc)
  653. SIT_I(sbi)->dirty_min_mtime = ULLONG_MAX;
  654. if (*result != NULL_SEGNO) {
  655. if (!get_valid_blocks(sbi, *result, false)) {
  656. ret = -ENODATA;
  657. goto out;
  658. }
  659. if (sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result)))
  660. ret = -EBUSY;
  661. else
  662. p.min_segno = *result;
  663. goto out;
  664. }
  665. ret = -ENODATA;
  666. if (p.max_search == 0)
  667. goto out;
  668. if (__is_large_section(sbi) && p.alloc_mode == LFS) {
  669. if (sbi->next_victim_seg[BG_GC] != NULL_SEGNO) {
  670. p.min_segno = sbi->next_victim_seg[BG_GC];
  671. *result = p.min_segno;
  672. sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
  673. goto got_result;
  674. }
  675. if (gc_type == FG_GC &&
  676. sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
  677. p.min_segno = sbi->next_victim_seg[FG_GC];
  678. *result = p.min_segno;
  679. sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
  680. goto got_result;
  681. }
  682. }
  683. last_victim = sm->last_victim[p.gc_mode];
  684. if (p.alloc_mode == LFS && gc_type == FG_GC) {
  685. p.min_segno = check_bg_victims(sbi);
  686. if (p.min_segno != NULL_SEGNO)
  687. goto got_it;
  688. }
  689. while (1) {
  690. unsigned long cost, *dirty_bitmap;
  691. unsigned int unit_no, segno;
  692. dirty_bitmap = p.dirty_bitmap;
  693. unit_no = find_next_bit(dirty_bitmap,
  694. last_segment / p.ofs_unit,
  695. p.offset / p.ofs_unit);
  696. segno = unit_no * p.ofs_unit;
  697. if (segno >= last_segment) {
  698. if (sm->last_victim[p.gc_mode]) {
  699. last_segment =
  700. sm->last_victim[p.gc_mode];
  701. sm->last_victim[p.gc_mode] = 0;
  702. p.offset = 0;
  703. continue;
  704. }
  705. break;
  706. }
  707. p.offset = segno + p.ofs_unit;
  708. nsearched++;
  709. #ifdef CONFIG_F2FS_CHECK_FS
  710. /*
  711. * skip selecting the invalid segno (that is failed due to block
  712. * validity check failure during GC) to avoid endless GC loop in
  713. * such cases.
  714. */
  715. if (test_bit(segno, sm->invalid_segmap))
  716. goto next;
  717. #endif
  718. secno = GET_SEC_FROM_SEG(sbi, segno);
  719. if (sec_usage_check(sbi, secno))
  720. goto next;
  721. /* Don't touch checkpointed data */
  722. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
  723. if (p.alloc_mode == LFS) {
  724. /*
  725. * LFS is set to find source section during GC.
  726. * The victim should have no checkpointed data.
  727. */
  728. if (get_ckpt_valid_blocks(sbi, segno, true))
  729. goto next;
  730. } else {
  731. /*
  732. * SSR | AT_SSR are set to find target segment
  733. * for writes which can be full by checkpointed
  734. * and newly written blocks.
  735. */
  736. if (!f2fs_segment_has_free_slot(sbi, segno))
  737. goto next;
  738. }
  739. }
  740. if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
  741. goto next;
  742. if (gc_type == FG_GC && f2fs_section_is_pinned(dirty_i, secno))
  743. goto next;
  744. if (is_atgc) {
  745. add_victim_entry(sbi, &p, segno);
  746. goto next;
  747. }
  748. cost = get_gc_cost(sbi, segno, &p);
  749. if (p.min_cost > cost) {
  750. p.min_segno = segno;
  751. p.min_cost = cost;
  752. }
  753. next:
  754. if (nsearched >= p.max_search) {
  755. if (!sm->last_victim[p.gc_mode] && segno <= last_victim)
  756. sm->last_victim[p.gc_mode] =
  757. last_victim + p.ofs_unit;
  758. else
  759. sm->last_victim[p.gc_mode] = segno + p.ofs_unit;
  760. sm->last_victim[p.gc_mode] %=
  761. (MAIN_SECS(sbi) * sbi->segs_per_sec);
  762. break;
  763. }
  764. }
  765. /* get victim for GC_AT/AT_SSR */
  766. if (is_atgc) {
  767. lookup_victim_by_age(sbi, &p);
  768. release_victim_entry(sbi);
  769. }
  770. if (is_atgc && p.min_segno == NULL_SEGNO &&
  771. sm->elapsed_time < p.age_threshold) {
  772. p.age_threshold = 0;
  773. goto retry;
  774. }
  775. if (p.min_segno != NULL_SEGNO) {
  776. got_it:
  777. *result = (p.min_segno / p.ofs_unit) * p.ofs_unit;
  778. got_result:
  779. if (p.alloc_mode == LFS) {
  780. secno = GET_SEC_FROM_SEG(sbi, p.min_segno);
  781. if (gc_type == FG_GC)
  782. sbi->cur_victim_sec = secno;
  783. else
  784. set_bit(secno, dirty_i->victim_secmap);
  785. }
  786. ret = 0;
  787. }
  788. out:
  789. if (p.min_segno != NULL_SEGNO)
  790. trace_f2fs_get_victim(sbi->sb, type, gc_type, &p,
  791. sbi->cur_victim_sec,
  792. prefree_segments(sbi), free_segments(sbi));
  793. mutex_unlock(&dirty_i->seglist_lock);
  794. return ret;
  795. }
  796. static struct inode *find_gc_inode(struct gc_inode_list *gc_list, nid_t ino)
  797. {
  798. struct inode_entry *ie;
  799. ie = radix_tree_lookup(&gc_list->iroot, ino);
  800. if (ie)
  801. return ie->inode;
  802. return NULL;
  803. }
  804. static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode)
  805. {
  806. struct inode_entry *new_ie;
  807. if (inode == find_gc_inode(gc_list, inode->i_ino)) {
  808. iput(inode);
  809. return;
  810. }
  811. new_ie = f2fs_kmem_cache_alloc(f2fs_inode_entry_slab,
  812. GFP_NOFS, true, NULL);
  813. new_ie->inode = inode;
  814. f2fs_radix_tree_insert(&gc_list->iroot, inode->i_ino, new_ie);
  815. list_add_tail(&new_ie->list, &gc_list->ilist);
  816. }
  817. static void put_gc_inode(struct gc_inode_list *gc_list)
  818. {
  819. struct inode_entry *ie, *next_ie;
  820. list_for_each_entry_safe(ie, next_ie, &gc_list->ilist, list) {
  821. radix_tree_delete(&gc_list->iroot, ie->inode->i_ino);
  822. iput(ie->inode);
  823. list_del(&ie->list);
  824. kmem_cache_free(f2fs_inode_entry_slab, ie);
  825. }
  826. }
  827. static int check_valid_map(struct f2fs_sb_info *sbi,
  828. unsigned int segno, int offset)
  829. {
  830. struct sit_info *sit_i = SIT_I(sbi);
  831. struct seg_entry *sentry;
  832. int ret;
  833. down_read(&sit_i->sentry_lock);
  834. sentry = get_seg_entry(sbi, segno);
  835. ret = f2fs_test_bit(offset, sentry->cur_valid_map);
  836. up_read(&sit_i->sentry_lock);
  837. return ret;
  838. }
  839. /*
  840. * This function compares node address got in summary with that in NAT.
  841. * On validity, copy that node with cold status, otherwise (invalid node)
  842. * ignore that.
  843. */
  844. static int gc_node_segment(struct f2fs_sb_info *sbi,
  845. struct f2fs_summary *sum, unsigned int segno, int gc_type)
  846. {
  847. struct f2fs_summary *entry;
  848. block_t start_addr;
  849. int off;
  850. int phase = 0;
  851. bool fggc = (gc_type == FG_GC);
  852. int submitted = 0;
  853. unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
  854. start_addr = START_BLOCK(sbi, segno);
  855. next_step:
  856. entry = sum;
  857. if (fggc && phase == 2)
  858. atomic_inc(&sbi->wb_sync_req[NODE]);
  859. for (off = 0; off < usable_blks_in_seg; off++, entry++) {
  860. nid_t nid = le32_to_cpu(entry->nid);
  861. struct page *node_page;
  862. struct node_info ni;
  863. int err;
  864. /* stop BG_GC if there is not enough free sections. */
  865. if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0))
  866. return submitted;
  867. if (check_valid_map(sbi, segno, off) == 0)
  868. continue;
  869. if (phase == 0) {
  870. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
  871. META_NAT, true);
  872. continue;
  873. }
  874. if (phase == 1) {
  875. f2fs_ra_node_page(sbi, nid);
  876. continue;
  877. }
  878. /* phase == 2 */
  879. node_page = f2fs_get_node_page(sbi, nid);
  880. if (IS_ERR(node_page))
  881. continue;
  882. /* block may become invalid during f2fs_get_node_page */
  883. if (check_valid_map(sbi, segno, off) == 0) {
  884. f2fs_put_page(node_page, 1);
  885. continue;
  886. }
  887. if (f2fs_get_node_info(sbi, nid, &ni, false)) {
  888. f2fs_put_page(node_page, 1);
  889. continue;
  890. }
  891. if (ni.blk_addr != start_addr + off) {
  892. f2fs_put_page(node_page, 1);
  893. continue;
  894. }
  895. err = f2fs_move_node_page(node_page, gc_type);
  896. if (!err && gc_type == FG_GC)
  897. submitted++;
  898. stat_inc_node_blk_count(sbi, 1, gc_type);
  899. }
  900. if (++phase < 3)
  901. goto next_step;
  902. if (fggc)
  903. atomic_dec(&sbi->wb_sync_req[NODE]);
  904. return submitted;
  905. }
  906. /*
  907. * Calculate start block index indicating the given node offset.
  908. * Be careful, caller should give this node offset only indicating direct node
  909. * blocks. If any node offsets, which point the other types of node blocks such
  910. * as indirect or double indirect node blocks, are given, it must be a caller's
  911. * bug.
  912. */
  913. block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode)
  914. {
  915. unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
  916. unsigned int bidx;
  917. if (node_ofs == 0)
  918. return 0;
  919. if (node_ofs <= 2) {
  920. bidx = node_ofs - 1;
  921. } else if (node_ofs <= indirect_blks) {
  922. int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
  923. bidx = node_ofs - 2 - dec;
  924. } else {
  925. int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
  926. bidx = node_ofs - 5 - dec;
  927. }
  928. return bidx * ADDRS_PER_BLOCK(inode) + ADDRS_PER_INODE(inode);
  929. }
  930. static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  931. struct node_info *dni, block_t blkaddr, unsigned int *nofs)
  932. {
  933. struct page *node_page;
  934. nid_t nid;
  935. unsigned int ofs_in_node, max_addrs, base;
  936. block_t source_blkaddr;
  937. nid = le32_to_cpu(sum->nid);
  938. ofs_in_node = le16_to_cpu(sum->ofs_in_node);
  939. node_page = f2fs_get_node_page(sbi, nid);
  940. if (IS_ERR(node_page))
  941. return false;
  942. if (f2fs_get_node_info(sbi, nid, dni, false)) {
  943. f2fs_put_page(node_page, 1);
  944. return false;
  945. }
  946. if (sum->version != dni->version) {
  947. f2fs_warn(sbi, "%s: valid data with mismatched node version.",
  948. __func__);
  949. set_sbi_flag(sbi, SBI_NEED_FSCK);
  950. }
  951. if (f2fs_check_nid_range(sbi, dni->ino)) {
  952. f2fs_put_page(node_page, 1);
  953. return false;
  954. }
  955. if (IS_INODE(node_page)) {
  956. base = offset_in_addr(F2FS_INODE(node_page));
  957. max_addrs = DEF_ADDRS_PER_INODE;
  958. } else {
  959. base = 0;
  960. max_addrs = DEF_ADDRS_PER_BLOCK;
  961. }
  962. if (base + ofs_in_node >= max_addrs) {
  963. f2fs_err(sbi, "Inconsistent blkaddr offset: base:%u, ofs_in_node:%u, max:%u, ino:%u, nid:%u",
  964. base, ofs_in_node, max_addrs, dni->ino, dni->nid);
  965. f2fs_put_page(node_page, 1);
  966. return false;
  967. }
  968. *nofs = ofs_of_node(node_page);
  969. source_blkaddr = data_blkaddr(NULL, node_page, ofs_in_node);
  970. f2fs_put_page(node_page, 1);
  971. if (source_blkaddr != blkaddr) {
  972. #ifdef CONFIG_F2FS_CHECK_FS
  973. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  974. unsigned long offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  975. if (unlikely(check_valid_map(sbi, segno, offset))) {
  976. if (!test_and_set_bit(segno, SIT_I(sbi)->invalid_segmap)) {
  977. f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u",
  978. blkaddr, source_blkaddr, segno);
  979. set_sbi_flag(sbi, SBI_NEED_FSCK);
  980. }
  981. }
  982. #endif
  983. return false;
  984. }
  985. return true;
  986. }
  987. static int ra_data_block(struct inode *inode, pgoff_t index)
  988. {
  989. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  990. struct address_space *mapping = inode->i_mapping;
  991. struct dnode_of_data dn;
  992. struct page *page;
  993. struct f2fs_io_info fio = {
  994. .sbi = sbi,
  995. .ino = inode->i_ino,
  996. .type = DATA,
  997. .temp = COLD,
  998. .op = REQ_OP_READ,
  999. .op_flags = 0,
  1000. .encrypted_page = NULL,
  1001. .in_list = 0,
  1002. .retry = 0,
  1003. };
  1004. int err;
  1005. page = f2fs_grab_cache_page(mapping, index, true);
  1006. if (!page)
  1007. return -ENOMEM;
  1008. if (f2fs_lookup_read_extent_cache_block(inode, index,
  1009. &dn.data_blkaddr)) {
  1010. if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr,
  1011. DATA_GENERIC_ENHANCE_READ))) {
  1012. err = -EFSCORRUPTED;
  1013. f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
  1014. goto put_page;
  1015. }
  1016. goto got_it;
  1017. }
  1018. set_new_dnode(&dn, inode, NULL, NULL, 0);
  1019. err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
  1020. if (err)
  1021. goto put_page;
  1022. f2fs_put_dnode(&dn);
  1023. if (!__is_valid_data_blkaddr(dn.data_blkaddr)) {
  1024. err = -ENOENT;
  1025. goto put_page;
  1026. }
  1027. if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr,
  1028. DATA_GENERIC_ENHANCE))) {
  1029. err = -EFSCORRUPTED;
  1030. f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
  1031. goto put_page;
  1032. }
  1033. got_it:
  1034. /* read page */
  1035. fio.page = page;
  1036. fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
  1037. /*
  1038. * don't cache encrypted data into meta inode until previous dirty
  1039. * data were writebacked to avoid racing between GC and flush.
  1040. */
  1041. f2fs_wait_on_page_writeback(page, DATA, true, true);
  1042. f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
  1043. fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(sbi),
  1044. dn.data_blkaddr,
  1045. FGP_LOCK | FGP_CREAT, GFP_NOFS);
  1046. if (!fio.encrypted_page) {
  1047. err = -ENOMEM;
  1048. goto put_page;
  1049. }
  1050. err = f2fs_submit_page_bio(&fio);
  1051. if (err)
  1052. goto put_encrypted_page;
  1053. f2fs_put_page(fio.encrypted_page, 0);
  1054. f2fs_put_page(page, 1);
  1055. f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE);
  1056. f2fs_update_iostat(sbi, NULL, FS_GDATA_READ_IO, F2FS_BLKSIZE);
  1057. return 0;
  1058. put_encrypted_page:
  1059. f2fs_put_page(fio.encrypted_page, 1);
  1060. put_page:
  1061. f2fs_put_page(page, 1);
  1062. return err;
  1063. }
  1064. /*
  1065. * Move data block via META_MAPPING while keeping locked data page.
  1066. * This can be used to move blocks, aka LBAs, directly on disk.
  1067. */
  1068. static int move_data_block(struct inode *inode, block_t bidx,
  1069. int gc_type, unsigned int segno, int off)
  1070. {
  1071. struct f2fs_io_info fio = {
  1072. .sbi = F2FS_I_SB(inode),
  1073. .ino = inode->i_ino,
  1074. .type = DATA,
  1075. .temp = COLD,
  1076. .op = REQ_OP_READ,
  1077. .op_flags = 0,
  1078. .encrypted_page = NULL,
  1079. .in_list = 0,
  1080. .retry = 0,
  1081. };
  1082. struct dnode_of_data dn;
  1083. struct f2fs_summary sum;
  1084. struct node_info ni;
  1085. struct page *page, *mpage;
  1086. block_t newaddr;
  1087. int err = 0;
  1088. bool lfs_mode = f2fs_lfs_mode(fio.sbi);
  1089. int type = fio.sbi->am.atgc_enabled && (gc_type == BG_GC) &&
  1090. (fio.sbi->gc_mode != GC_URGENT_HIGH) ?
  1091. CURSEG_ALL_DATA_ATGC : CURSEG_COLD_DATA;
  1092. /* do not read out */
  1093. page = f2fs_grab_cache_page(inode->i_mapping, bidx, false);
  1094. if (!page)
  1095. return -ENOMEM;
  1096. if (!check_valid_map(F2FS_I_SB(inode), segno, off)) {
  1097. err = -ENOENT;
  1098. goto out;
  1099. }
  1100. err = f2fs_gc_pinned_control(inode, gc_type, segno);
  1101. if (err)
  1102. goto out;
  1103. set_new_dnode(&dn, inode, NULL, NULL, 0);
  1104. err = f2fs_get_dnode_of_data(&dn, bidx, LOOKUP_NODE);
  1105. if (err)
  1106. goto out;
  1107. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  1108. ClearPageUptodate(page);
  1109. err = -ENOENT;
  1110. goto put_out;
  1111. }
  1112. /*
  1113. * don't cache encrypted data into meta inode until previous dirty
  1114. * data were writebacked to avoid racing between GC and flush.
  1115. */
  1116. f2fs_wait_on_page_writeback(page, DATA, true, true);
  1117. f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
  1118. err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
  1119. if (err)
  1120. goto put_out;
  1121. /* read page */
  1122. fio.page = page;
  1123. fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
  1124. if (lfs_mode)
  1125. f2fs_down_write(&fio.sbi->io_order_lock);
  1126. mpage = f2fs_grab_cache_page(META_MAPPING(fio.sbi),
  1127. fio.old_blkaddr, false);
  1128. if (!mpage) {
  1129. err = -ENOMEM;
  1130. goto up_out;
  1131. }
  1132. fio.encrypted_page = mpage;
  1133. /* read source block in mpage */
  1134. if (!PageUptodate(mpage)) {
  1135. err = f2fs_submit_page_bio(&fio);
  1136. if (err) {
  1137. f2fs_put_page(mpage, 1);
  1138. goto up_out;
  1139. }
  1140. f2fs_update_iostat(fio.sbi, inode, FS_DATA_READ_IO,
  1141. F2FS_BLKSIZE);
  1142. f2fs_update_iostat(fio.sbi, NULL, FS_GDATA_READ_IO,
  1143. F2FS_BLKSIZE);
  1144. lock_page(mpage);
  1145. if (unlikely(mpage->mapping != META_MAPPING(fio.sbi) ||
  1146. !PageUptodate(mpage))) {
  1147. err = -EIO;
  1148. f2fs_put_page(mpage, 1);
  1149. goto up_out;
  1150. }
  1151. }
  1152. set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
  1153. /* allocate block address */
  1154. f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
  1155. &sum, type, NULL);
  1156. fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(fio.sbi),
  1157. newaddr, FGP_LOCK | FGP_CREAT, GFP_NOFS);
  1158. if (!fio.encrypted_page) {
  1159. err = -ENOMEM;
  1160. f2fs_put_page(mpage, 1);
  1161. goto recover_block;
  1162. }
  1163. /* write target block */
  1164. f2fs_wait_on_page_writeback(fio.encrypted_page, DATA, true, true);
  1165. memcpy(page_address(fio.encrypted_page),
  1166. page_address(mpage), PAGE_SIZE);
  1167. f2fs_put_page(mpage, 1);
  1168. invalidate_mapping_pages(META_MAPPING(fio.sbi),
  1169. fio.old_blkaddr, fio.old_blkaddr);
  1170. f2fs_invalidate_compress_page(fio.sbi, fio.old_blkaddr);
  1171. set_page_dirty(fio.encrypted_page);
  1172. if (clear_page_dirty_for_io(fio.encrypted_page))
  1173. dec_page_count(fio.sbi, F2FS_DIRTY_META);
  1174. set_page_writeback(fio.encrypted_page);
  1175. fio.op = REQ_OP_WRITE;
  1176. fio.op_flags = REQ_SYNC;
  1177. fio.new_blkaddr = newaddr;
  1178. f2fs_submit_page_write(&fio);
  1179. if (fio.retry) {
  1180. err = -EAGAIN;
  1181. if (PageWriteback(fio.encrypted_page))
  1182. end_page_writeback(fio.encrypted_page);
  1183. goto put_page_out;
  1184. }
  1185. f2fs_update_iostat(fio.sbi, NULL, FS_GC_DATA_IO, F2FS_BLKSIZE);
  1186. f2fs_update_data_blkaddr(&dn, newaddr);
  1187. set_inode_flag(inode, FI_APPEND_WRITE);
  1188. if (page->index == 0)
  1189. set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
  1190. put_page_out:
  1191. f2fs_put_page(fio.encrypted_page, 1);
  1192. recover_block:
  1193. if (err)
  1194. f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
  1195. true, true, true);
  1196. up_out:
  1197. if (lfs_mode)
  1198. f2fs_up_write(&fio.sbi->io_order_lock);
  1199. put_out:
  1200. f2fs_put_dnode(&dn);
  1201. out:
  1202. f2fs_put_page(page, 1);
  1203. return err;
  1204. }
  1205. static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
  1206. unsigned int segno, int off)
  1207. {
  1208. struct page *page;
  1209. int err = 0;
  1210. page = f2fs_get_lock_data_page(inode, bidx, true);
  1211. if (IS_ERR(page))
  1212. return PTR_ERR(page);
  1213. if (!check_valid_map(F2FS_I_SB(inode), segno, off)) {
  1214. err = -ENOENT;
  1215. goto out;
  1216. }
  1217. err = f2fs_gc_pinned_control(inode, gc_type, segno);
  1218. if (err)
  1219. goto out;
  1220. if (gc_type == BG_GC) {
  1221. if (PageWriteback(page)) {
  1222. err = -EAGAIN;
  1223. goto out;
  1224. }
  1225. set_page_dirty(page);
  1226. set_page_private_gcing(page);
  1227. } else {
  1228. struct f2fs_io_info fio = {
  1229. .sbi = F2FS_I_SB(inode),
  1230. .ino = inode->i_ino,
  1231. .type = DATA,
  1232. .temp = COLD,
  1233. .op = REQ_OP_WRITE,
  1234. .op_flags = REQ_SYNC,
  1235. .old_blkaddr = NULL_ADDR,
  1236. .page = page,
  1237. .encrypted_page = NULL,
  1238. .need_lock = LOCK_REQ,
  1239. .io_type = FS_GC_DATA_IO,
  1240. };
  1241. bool is_dirty = PageDirty(page);
  1242. retry:
  1243. f2fs_wait_on_page_writeback(page, DATA, true, true);
  1244. set_page_dirty(page);
  1245. if (clear_page_dirty_for_io(page)) {
  1246. inode_dec_dirty_pages(inode);
  1247. f2fs_remove_dirty_inode(inode);
  1248. }
  1249. set_page_private_gcing(page);
  1250. err = f2fs_do_write_data_page(&fio);
  1251. if (err) {
  1252. clear_page_private_gcing(page);
  1253. if (err == -ENOMEM) {
  1254. memalloc_retry_wait(GFP_NOFS);
  1255. goto retry;
  1256. }
  1257. if (is_dirty)
  1258. set_page_dirty(page);
  1259. }
  1260. }
  1261. out:
  1262. f2fs_put_page(page, 1);
  1263. return err;
  1264. }
  1265. /*
  1266. * This function tries to get parent node of victim data block, and identifies
  1267. * data block validity. If the block is valid, copy that with cold status and
  1268. * modify parent node.
  1269. * If the parent node is not valid or the data block address is different,
  1270. * the victim data block is ignored.
  1271. */
  1272. static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  1273. struct gc_inode_list *gc_list, unsigned int segno, int gc_type,
  1274. bool force_migrate)
  1275. {
  1276. struct super_block *sb = sbi->sb;
  1277. struct f2fs_summary *entry;
  1278. block_t start_addr;
  1279. int off;
  1280. int phase = 0;
  1281. int submitted = 0;
  1282. unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
  1283. start_addr = START_BLOCK(sbi, segno);
  1284. next_step:
  1285. entry = sum;
  1286. for (off = 0; off < usable_blks_in_seg; off++, entry++) {
  1287. struct page *data_page;
  1288. struct inode *inode;
  1289. struct node_info dni; /* dnode info for the data */
  1290. unsigned int ofs_in_node, nofs;
  1291. block_t start_bidx;
  1292. nid_t nid = le32_to_cpu(entry->nid);
  1293. /*
  1294. * stop BG_GC if there is not enough free sections.
  1295. * Or, stop GC if the segment becomes fully valid caused by
  1296. * race condition along with SSR block allocation.
  1297. */
  1298. if ((gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) ||
  1299. (!force_migrate && get_valid_blocks(sbi, segno, true) ==
  1300. CAP_BLKS_PER_SEC(sbi)))
  1301. return submitted;
  1302. if (check_valid_map(sbi, segno, off) == 0)
  1303. continue;
  1304. if (phase == 0) {
  1305. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1,
  1306. META_NAT, true);
  1307. continue;
  1308. }
  1309. if (phase == 1) {
  1310. f2fs_ra_node_page(sbi, nid);
  1311. continue;
  1312. }
  1313. /* Get an inode by ino with checking validity */
  1314. if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs))
  1315. continue;
  1316. if (phase == 2) {
  1317. f2fs_ra_node_page(sbi, dni.ino);
  1318. continue;
  1319. }
  1320. ofs_in_node = le16_to_cpu(entry->ofs_in_node);
  1321. if (phase == 3) {
  1322. int err;
  1323. inode = f2fs_iget(sb, dni.ino);
  1324. if (IS_ERR(inode) || is_bad_inode(inode) ||
  1325. special_file(inode->i_mode))
  1326. continue;
  1327. err = f2fs_gc_pinned_control(inode, gc_type, segno);
  1328. if (err == -EAGAIN) {
  1329. iput(inode);
  1330. return submitted;
  1331. }
  1332. if (!f2fs_down_write_trylock(
  1333. &F2FS_I(inode)->i_gc_rwsem[WRITE])) {
  1334. iput(inode);
  1335. sbi->skipped_gc_rwsem++;
  1336. continue;
  1337. }
  1338. start_bidx = f2fs_start_bidx_of_node(nofs, inode) +
  1339. ofs_in_node;
  1340. if (f2fs_post_read_required(inode)) {
  1341. int err = ra_data_block(inode, start_bidx);
  1342. f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1343. if (err) {
  1344. iput(inode);
  1345. continue;
  1346. }
  1347. add_gc_inode(gc_list, inode);
  1348. continue;
  1349. }
  1350. data_page = f2fs_get_read_data_page(inode, start_bidx,
  1351. REQ_RAHEAD, true, NULL);
  1352. f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1353. if (IS_ERR(data_page)) {
  1354. iput(inode);
  1355. continue;
  1356. }
  1357. f2fs_put_page(data_page, 0);
  1358. add_gc_inode(gc_list, inode);
  1359. continue;
  1360. }
  1361. /* phase 4 */
  1362. inode = find_gc_inode(gc_list, dni.ino);
  1363. if (inode) {
  1364. struct f2fs_inode_info *fi = F2FS_I(inode);
  1365. bool locked = false;
  1366. int err;
  1367. if (S_ISREG(inode->i_mode)) {
  1368. if (!f2fs_down_write_trylock(&fi->i_gc_rwsem[WRITE])) {
  1369. sbi->skipped_gc_rwsem++;
  1370. continue;
  1371. }
  1372. if (!f2fs_down_write_trylock(
  1373. &fi->i_gc_rwsem[READ])) {
  1374. sbi->skipped_gc_rwsem++;
  1375. f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
  1376. continue;
  1377. }
  1378. locked = true;
  1379. /* wait for all inflight aio data */
  1380. inode_dio_wait(inode);
  1381. }
  1382. start_bidx = f2fs_start_bidx_of_node(nofs, inode)
  1383. + ofs_in_node;
  1384. if (f2fs_post_read_required(inode))
  1385. err = move_data_block(inode, start_bidx,
  1386. gc_type, segno, off);
  1387. else
  1388. err = move_data_page(inode, start_bidx, gc_type,
  1389. segno, off);
  1390. if (!err && (gc_type == FG_GC ||
  1391. f2fs_post_read_required(inode)))
  1392. submitted++;
  1393. if (locked) {
  1394. f2fs_up_write(&fi->i_gc_rwsem[READ]);
  1395. f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
  1396. }
  1397. stat_inc_data_blk_count(sbi, 1, gc_type);
  1398. }
  1399. }
  1400. if (++phase < 5)
  1401. goto next_step;
  1402. return submitted;
  1403. }
  1404. static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim,
  1405. int gc_type)
  1406. {
  1407. struct sit_info *sit_i = SIT_I(sbi);
  1408. int ret;
  1409. down_write(&sit_i->sentry_lock);
  1410. ret = f2fs_get_victim(sbi, victim, gc_type, NO_CHECK_TYPE, LFS, 0);
  1411. up_write(&sit_i->sentry_lock);
  1412. return ret;
  1413. }
  1414. static int do_garbage_collect(struct f2fs_sb_info *sbi,
  1415. unsigned int start_segno,
  1416. struct gc_inode_list *gc_list, int gc_type,
  1417. bool force_migrate)
  1418. {
  1419. struct page *sum_page;
  1420. struct f2fs_summary_block *sum;
  1421. struct blk_plug plug;
  1422. unsigned int segno = start_segno;
  1423. unsigned int end_segno = start_segno + sbi->segs_per_sec;
  1424. int seg_freed = 0, migrated = 0;
  1425. unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
  1426. SUM_TYPE_DATA : SUM_TYPE_NODE;
  1427. int submitted = 0;
  1428. if (__is_large_section(sbi))
  1429. end_segno = rounddown(end_segno, sbi->segs_per_sec);
  1430. /*
  1431. * zone-capacity can be less than zone-size in zoned devices,
  1432. * resulting in less than expected usable segments in the zone,
  1433. * calculate the end segno in the zone which can be garbage collected
  1434. */
  1435. if (f2fs_sb_has_blkzoned(sbi))
  1436. end_segno -= sbi->segs_per_sec -
  1437. f2fs_usable_segs_in_sec(sbi, segno);
  1438. sanity_check_seg_type(sbi, get_seg_entry(sbi, segno)->type);
  1439. /* readahead multi ssa blocks those have contiguous address */
  1440. if (__is_large_section(sbi))
  1441. f2fs_ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno),
  1442. end_segno - segno, META_SSA, true);
  1443. /* reference all summary page */
  1444. while (segno < end_segno) {
  1445. sum_page = f2fs_get_sum_page(sbi, segno++);
  1446. if (IS_ERR(sum_page)) {
  1447. int err = PTR_ERR(sum_page);
  1448. end_segno = segno - 1;
  1449. for (segno = start_segno; segno < end_segno; segno++) {
  1450. sum_page = find_get_page(META_MAPPING(sbi),
  1451. GET_SUM_BLOCK(sbi, segno));
  1452. f2fs_put_page(sum_page, 0);
  1453. f2fs_put_page(sum_page, 0);
  1454. }
  1455. return err;
  1456. }
  1457. unlock_page(sum_page);
  1458. }
  1459. blk_start_plug(&plug);
  1460. for (segno = start_segno; segno < end_segno; segno++) {
  1461. /* find segment summary of victim */
  1462. sum_page = find_get_page(META_MAPPING(sbi),
  1463. GET_SUM_BLOCK(sbi, segno));
  1464. f2fs_put_page(sum_page, 0);
  1465. if (get_valid_blocks(sbi, segno, false) == 0)
  1466. goto freed;
  1467. if (gc_type == BG_GC && __is_large_section(sbi) &&
  1468. migrated >= sbi->migration_granularity)
  1469. goto skip;
  1470. if (!PageUptodate(sum_page) || unlikely(f2fs_cp_error(sbi)))
  1471. goto skip;
  1472. sum = page_address(sum_page);
  1473. if (type != GET_SUM_TYPE((&sum->footer))) {
  1474. f2fs_err(sbi, "Inconsistent segment (%u) type [%d, %d] in SSA and SIT",
  1475. segno, type, GET_SUM_TYPE((&sum->footer)));
  1476. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1477. f2fs_stop_checkpoint(sbi, false,
  1478. STOP_CP_REASON_CORRUPTED_SUMMARY);
  1479. goto skip;
  1480. }
  1481. /*
  1482. * this is to avoid deadlock:
  1483. * - lock_page(sum_page) - f2fs_replace_block
  1484. * - check_valid_map() - down_write(sentry_lock)
  1485. * - down_read(sentry_lock) - change_curseg()
  1486. * - lock_page(sum_page)
  1487. */
  1488. if (type == SUM_TYPE_NODE)
  1489. submitted += gc_node_segment(sbi, sum->entries, segno,
  1490. gc_type);
  1491. else
  1492. submitted += gc_data_segment(sbi, sum->entries, gc_list,
  1493. segno, gc_type,
  1494. force_migrate);
  1495. stat_inc_seg_count(sbi, type, gc_type);
  1496. sbi->gc_reclaimed_segs[sbi->gc_mode]++;
  1497. migrated++;
  1498. freed:
  1499. if (gc_type == FG_GC &&
  1500. get_valid_blocks(sbi, segno, false) == 0)
  1501. seg_freed++;
  1502. if (__is_large_section(sbi))
  1503. sbi->next_victim_seg[gc_type] =
  1504. (segno + 1 < end_segno) ? segno + 1 : NULL_SEGNO;
  1505. skip:
  1506. f2fs_put_page(sum_page, 0);
  1507. }
  1508. if (submitted)
  1509. f2fs_submit_merged_write(sbi,
  1510. (type == SUM_TYPE_NODE) ? NODE : DATA);
  1511. blk_finish_plug(&plug);
  1512. stat_inc_call_count(sbi->stat_info);
  1513. return seg_freed;
  1514. }
  1515. int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
  1516. {
  1517. int gc_type = gc_control->init_gc_type;
  1518. unsigned int segno = gc_control->victim_segno;
  1519. int sec_freed = 0, seg_freed = 0, total_freed = 0;
  1520. int ret = 0;
  1521. struct cp_control cpc;
  1522. struct gc_inode_list gc_list = {
  1523. .ilist = LIST_HEAD_INIT(gc_list.ilist),
  1524. .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
  1525. };
  1526. unsigned int skipped_round = 0, round = 0;
  1527. unsigned int upper_secs;
  1528. trace_f2fs_gc_begin(sbi->sb, gc_type, gc_control->no_bg_gc,
  1529. gc_control->nr_free_secs,
  1530. get_pages(sbi, F2FS_DIRTY_NODES),
  1531. get_pages(sbi, F2FS_DIRTY_DENTS),
  1532. get_pages(sbi, F2FS_DIRTY_IMETA),
  1533. free_sections(sbi),
  1534. free_segments(sbi),
  1535. reserved_segments(sbi),
  1536. prefree_segments(sbi));
  1537. cpc.reason = __get_cp_reason(sbi);
  1538. gc_more:
  1539. sbi->skipped_gc_rwsem = 0;
  1540. if (unlikely(!(sbi->sb->s_flags & SB_ACTIVE))) {
  1541. ret = -EINVAL;
  1542. goto stop;
  1543. }
  1544. if (unlikely(f2fs_cp_error(sbi))) {
  1545. ret = -EIO;
  1546. goto stop;
  1547. }
  1548. /* Let's run FG_GC, if we don't have enough space. */
  1549. if (has_not_enough_free_secs(sbi, 0, 0)) {
  1550. gc_type = FG_GC;
  1551. /*
  1552. * For example, if there are many prefree_segments below given
  1553. * threshold, we can make them free by checkpoint. Then, we
  1554. * secure free segments which doesn't need fggc any more.
  1555. */
  1556. if (prefree_segments(sbi)) {
  1557. ret = f2fs_write_checkpoint(sbi, &cpc);
  1558. if (ret)
  1559. goto stop;
  1560. }
  1561. }
  1562. /* f2fs_balance_fs doesn't need to do BG_GC in critical path. */
  1563. if (gc_type == BG_GC && gc_control->no_bg_gc) {
  1564. ret = -EINVAL;
  1565. goto stop;
  1566. }
  1567. retry:
  1568. ret = __get_victim(sbi, &segno, gc_type);
  1569. if (ret) {
  1570. /* allow to search victim from sections has pinned data */
  1571. if (ret == -ENODATA && gc_type == FG_GC &&
  1572. f2fs_pinned_section_exists(DIRTY_I(sbi))) {
  1573. f2fs_unpin_all_sections(sbi, false);
  1574. goto retry;
  1575. }
  1576. goto stop;
  1577. }
  1578. seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
  1579. gc_control->should_migrate_blocks);
  1580. total_freed += seg_freed;
  1581. if (seg_freed == f2fs_usable_segs_in_sec(sbi, segno))
  1582. sec_freed++;
  1583. if (gc_type == FG_GC) {
  1584. sbi->cur_victim_sec = NULL_SEGNO;
  1585. if (has_enough_free_secs(sbi, sec_freed, 0)) {
  1586. if (!gc_control->no_bg_gc &&
  1587. sec_freed < gc_control->nr_free_secs)
  1588. goto go_gc_more;
  1589. goto stop;
  1590. }
  1591. if (sbi->skipped_gc_rwsem)
  1592. skipped_round++;
  1593. round++;
  1594. if (skipped_round > MAX_SKIP_GC_COUNT &&
  1595. skipped_round * 2 >= round) {
  1596. ret = f2fs_write_checkpoint(sbi, &cpc);
  1597. goto stop;
  1598. }
  1599. } else if (has_enough_free_secs(sbi, 0, 0)) {
  1600. goto stop;
  1601. }
  1602. __get_secs_required(sbi, NULL, &upper_secs, NULL);
  1603. /*
  1604. * Write checkpoint to reclaim prefree segments.
  1605. * We need more three extra sections for writer's data/node/dentry.
  1606. */
  1607. if (free_sections(sbi) <= upper_secs + NR_GC_CHECKPOINT_SECS &&
  1608. prefree_segments(sbi)) {
  1609. ret = f2fs_write_checkpoint(sbi, &cpc);
  1610. if (ret)
  1611. goto stop;
  1612. }
  1613. go_gc_more:
  1614. segno = NULL_SEGNO;
  1615. goto gc_more;
  1616. stop:
  1617. SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
  1618. SIT_I(sbi)->last_victim[FLUSH_DEVICE] = gc_control->victim_segno;
  1619. if (gc_type == FG_GC)
  1620. f2fs_unpin_all_sections(sbi, true);
  1621. trace_f2fs_gc_end(sbi->sb, ret, total_freed, sec_freed,
  1622. get_pages(sbi, F2FS_DIRTY_NODES),
  1623. get_pages(sbi, F2FS_DIRTY_DENTS),
  1624. get_pages(sbi, F2FS_DIRTY_IMETA),
  1625. free_sections(sbi),
  1626. free_segments(sbi),
  1627. reserved_segments(sbi),
  1628. prefree_segments(sbi));
  1629. f2fs_up_write(&sbi->gc_lock);
  1630. put_gc_inode(&gc_list);
  1631. if (gc_control->err_gc_skipped && !ret)
  1632. ret = sec_freed ? 0 : -EAGAIN;
  1633. return ret;
  1634. }
  1635. int __init f2fs_create_garbage_collection_cache(void)
  1636. {
  1637. victim_entry_slab = f2fs_kmem_cache_create("f2fs_victim_entry",
  1638. sizeof(struct victim_entry));
  1639. return victim_entry_slab ? 0 : -ENOMEM;
  1640. }
  1641. void f2fs_destroy_garbage_collection_cache(void)
  1642. {
  1643. kmem_cache_destroy(victim_entry_slab);
  1644. }
  1645. static void init_atgc_management(struct f2fs_sb_info *sbi)
  1646. {
  1647. struct atgc_management *am = &sbi->am;
  1648. if (test_opt(sbi, ATGC) &&
  1649. SIT_I(sbi)->elapsed_time >= DEF_GC_THREAD_AGE_THRESHOLD)
  1650. am->atgc_enabled = true;
  1651. am->root = RB_ROOT_CACHED;
  1652. INIT_LIST_HEAD(&am->victim_list);
  1653. am->victim_count = 0;
  1654. am->candidate_ratio = DEF_GC_THREAD_CANDIDATE_RATIO;
  1655. am->max_candidate_count = DEF_GC_THREAD_MAX_CANDIDATE_COUNT;
  1656. am->age_weight = DEF_GC_THREAD_AGE_WEIGHT;
  1657. am->age_threshold = DEF_GC_THREAD_AGE_THRESHOLD;
  1658. }
  1659. void f2fs_build_gc_manager(struct f2fs_sb_info *sbi)
  1660. {
  1661. sbi->gc_pin_file_threshold = DEF_GC_FAILED_PINNED_FILES;
  1662. /* give warm/cold data area from slower device */
  1663. if (f2fs_is_multi_device(sbi) && !__is_large_section(sbi))
  1664. SIT_I(sbi)->last_victim[ALLOC_NEXT] =
  1665. GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
  1666. init_atgc_management(sbi);
  1667. }
  1668. static int free_segment_range(struct f2fs_sb_info *sbi,
  1669. unsigned int secs, bool gc_only)
  1670. {
  1671. unsigned int segno, next_inuse, start, end;
  1672. struct cp_control cpc = { CP_RESIZE, 0, 0, 0 };
  1673. int gc_mode, gc_type;
  1674. int err = 0;
  1675. int type;
  1676. /* Force block allocation for GC */
  1677. MAIN_SECS(sbi) -= secs;
  1678. start = MAIN_SECS(sbi) * sbi->segs_per_sec;
  1679. end = MAIN_SEGS(sbi) - 1;
  1680. mutex_lock(&DIRTY_I(sbi)->seglist_lock);
  1681. for (gc_mode = 0; gc_mode < MAX_GC_POLICY; gc_mode++)
  1682. if (SIT_I(sbi)->last_victim[gc_mode] >= start)
  1683. SIT_I(sbi)->last_victim[gc_mode] = 0;
  1684. for (gc_type = BG_GC; gc_type <= FG_GC; gc_type++)
  1685. if (sbi->next_victim_seg[gc_type] >= start)
  1686. sbi->next_victim_seg[gc_type] = NULL_SEGNO;
  1687. mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
  1688. /* Move out cursegs from the target range */
  1689. for (type = CURSEG_HOT_DATA; type < NR_CURSEG_PERSIST_TYPE; type++)
  1690. f2fs_allocate_segment_for_resize(sbi, type, start, end);
  1691. /* do GC to move out valid blocks in the range */
  1692. for (segno = start; segno <= end; segno += sbi->segs_per_sec) {
  1693. struct gc_inode_list gc_list = {
  1694. .ilist = LIST_HEAD_INIT(gc_list.ilist),
  1695. .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
  1696. };
  1697. do_garbage_collect(sbi, segno, &gc_list, FG_GC, true);
  1698. put_gc_inode(&gc_list);
  1699. if (!gc_only && get_valid_blocks(sbi, segno, true)) {
  1700. err = -EAGAIN;
  1701. goto out;
  1702. }
  1703. if (fatal_signal_pending(current)) {
  1704. err = -ERESTARTSYS;
  1705. goto out;
  1706. }
  1707. }
  1708. if (gc_only)
  1709. goto out;
  1710. err = f2fs_write_checkpoint(sbi, &cpc);
  1711. if (err)
  1712. goto out;
  1713. next_inuse = find_next_inuse(FREE_I(sbi), end + 1, start);
  1714. if (next_inuse <= end) {
  1715. f2fs_err(sbi, "segno %u should be free but still inuse!",
  1716. next_inuse);
  1717. f2fs_bug_on(sbi, 1);
  1718. }
  1719. out:
  1720. MAIN_SECS(sbi) += secs;
  1721. return err;
  1722. }
  1723. static void update_sb_metadata(struct f2fs_sb_info *sbi, int secs)
  1724. {
  1725. struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi);
  1726. int section_count;
  1727. int segment_count;
  1728. int segment_count_main;
  1729. long long block_count;
  1730. int segs = secs * sbi->segs_per_sec;
  1731. f2fs_down_write(&sbi->sb_lock);
  1732. section_count = le32_to_cpu(raw_sb->section_count);
  1733. segment_count = le32_to_cpu(raw_sb->segment_count);
  1734. segment_count_main = le32_to_cpu(raw_sb->segment_count_main);
  1735. block_count = le64_to_cpu(raw_sb->block_count);
  1736. raw_sb->section_count = cpu_to_le32(section_count + secs);
  1737. raw_sb->segment_count = cpu_to_le32(segment_count + segs);
  1738. raw_sb->segment_count_main = cpu_to_le32(segment_count_main + segs);
  1739. raw_sb->block_count = cpu_to_le64(block_count +
  1740. (long long)segs * sbi->blocks_per_seg);
  1741. if (f2fs_is_multi_device(sbi)) {
  1742. int last_dev = sbi->s_ndevs - 1;
  1743. int dev_segs =
  1744. le32_to_cpu(raw_sb->devs[last_dev].total_segments);
  1745. raw_sb->devs[last_dev].total_segments =
  1746. cpu_to_le32(dev_segs + segs);
  1747. }
  1748. f2fs_up_write(&sbi->sb_lock);
  1749. }
  1750. static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
  1751. {
  1752. int segs = secs * sbi->segs_per_sec;
  1753. long long blks = (long long)segs * sbi->blocks_per_seg;
  1754. long long user_block_count =
  1755. le64_to_cpu(F2FS_CKPT(sbi)->user_block_count);
  1756. SM_I(sbi)->segment_count = (int)SM_I(sbi)->segment_count + segs;
  1757. MAIN_SEGS(sbi) = (int)MAIN_SEGS(sbi) + segs;
  1758. MAIN_SECS(sbi) += secs;
  1759. FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs;
  1760. FREE_I(sbi)->free_segments = (int)FREE_I(sbi)->free_segments + segs;
  1761. F2FS_CKPT(sbi)->user_block_count = cpu_to_le64(user_block_count + blks);
  1762. if (f2fs_is_multi_device(sbi)) {
  1763. int last_dev = sbi->s_ndevs - 1;
  1764. FDEV(last_dev).total_segments =
  1765. (int)FDEV(last_dev).total_segments + segs;
  1766. FDEV(last_dev).end_blk =
  1767. (long long)FDEV(last_dev).end_blk + blks;
  1768. #ifdef CONFIG_BLK_DEV_ZONED
  1769. FDEV(last_dev).nr_blkz = FDEV(last_dev).nr_blkz +
  1770. div_u64(blks, sbi->blocks_per_blkz);
  1771. #endif
  1772. }
  1773. }
  1774. int f2fs_resize_fs(struct file *filp, __u64 block_count)
  1775. {
  1776. struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
  1777. __u64 old_block_count, shrunk_blocks;
  1778. struct cp_control cpc = { CP_RESIZE, 0, 0, 0 };
  1779. unsigned int secs;
  1780. int err = 0;
  1781. __u32 rem;
  1782. old_block_count = le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
  1783. if (block_count > old_block_count)
  1784. return -EINVAL;
  1785. if (f2fs_is_multi_device(sbi)) {
  1786. int last_dev = sbi->s_ndevs - 1;
  1787. __u64 last_segs = FDEV(last_dev).total_segments;
  1788. if (block_count + last_segs * sbi->blocks_per_seg <=
  1789. old_block_count)
  1790. return -EINVAL;
  1791. }
  1792. /* new fs size should align to section size */
  1793. div_u64_rem(block_count, BLKS_PER_SEC(sbi), &rem);
  1794. if (rem)
  1795. return -EINVAL;
  1796. if (block_count == old_block_count)
  1797. return 0;
  1798. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
  1799. f2fs_err(sbi, "Should run fsck to repair first.");
  1800. return -EFSCORRUPTED;
  1801. }
  1802. if (test_opt(sbi, DISABLE_CHECKPOINT)) {
  1803. f2fs_err(sbi, "Checkpoint should be enabled.");
  1804. return -EINVAL;
  1805. }
  1806. err = mnt_want_write_file(filp);
  1807. if (err)
  1808. return err;
  1809. shrunk_blocks = old_block_count - block_count;
  1810. secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
  1811. /* stop other GC */
  1812. if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
  1813. err = -EAGAIN;
  1814. goto out_drop_write;
  1815. }
  1816. /* stop CP to protect MAIN_SEC in free_segment_range */
  1817. f2fs_lock_op(sbi);
  1818. spin_lock(&sbi->stat_lock);
  1819. if (shrunk_blocks + valid_user_blocks(sbi) +
  1820. sbi->current_reserved_blocks + sbi->unusable_block_count +
  1821. F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
  1822. err = -ENOSPC;
  1823. spin_unlock(&sbi->stat_lock);
  1824. if (err)
  1825. goto out_unlock;
  1826. err = free_segment_range(sbi, secs, true);
  1827. out_unlock:
  1828. f2fs_unlock_op(sbi);
  1829. f2fs_up_write(&sbi->gc_lock);
  1830. out_drop_write:
  1831. mnt_drop_write_file(filp);
  1832. if (err)
  1833. return err;
  1834. err = freeze_super(sbi->sb);
  1835. if (err)
  1836. return err;
  1837. if (f2fs_readonly(sbi->sb)) {
  1838. thaw_super(sbi->sb);
  1839. return -EROFS;
  1840. }
  1841. f2fs_down_write(&sbi->gc_lock);
  1842. f2fs_down_write(&sbi->cp_global_sem);
  1843. spin_lock(&sbi->stat_lock);
  1844. if (shrunk_blocks + valid_user_blocks(sbi) +
  1845. sbi->current_reserved_blocks + sbi->unusable_block_count +
  1846. F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
  1847. err = -ENOSPC;
  1848. else
  1849. sbi->user_block_count -= shrunk_blocks;
  1850. spin_unlock(&sbi->stat_lock);
  1851. if (err)
  1852. goto out_err;
  1853. set_sbi_flag(sbi, SBI_IS_RESIZEFS);
  1854. err = free_segment_range(sbi, secs, false);
  1855. if (err)
  1856. goto recover_out;
  1857. update_sb_metadata(sbi, -secs);
  1858. err = f2fs_commit_super(sbi, false);
  1859. if (err) {
  1860. update_sb_metadata(sbi, secs);
  1861. goto recover_out;
  1862. }
  1863. update_fs_metadata(sbi, -secs);
  1864. clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
  1865. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1866. err = f2fs_write_checkpoint(sbi, &cpc);
  1867. if (err) {
  1868. update_fs_metadata(sbi, secs);
  1869. update_sb_metadata(sbi, secs);
  1870. f2fs_commit_super(sbi, false);
  1871. }
  1872. recover_out:
  1873. clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
  1874. if (err) {
  1875. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1876. f2fs_err(sbi, "resize_fs failed, should run fsck to repair!");
  1877. spin_lock(&sbi->stat_lock);
  1878. sbi->user_block_count += shrunk_blocks;
  1879. spin_unlock(&sbi->stat_lock);
  1880. }
  1881. out_err:
  1882. f2fs_up_write(&sbi->cp_global_sem);
  1883. f2fs_up_write(&sbi->gc_lock);
  1884. thaw_super(sbi->sb);
  1885. return err;
  1886. }