gc.c 60 KB

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