segment.c 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/segment.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/f2fs_fs.h>
  10. #include <linux/bio.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/sched/mm.h>
  13. #include <linux/prefetch.h>
  14. #include <linux/kthread.h>
  15. #include <linux/swap.h>
  16. #include <linux/timer.h>
  17. #include <linux/freezer.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/random.h>
  20. #include "f2fs.h"
  21. #include "segment.h"
  22. #include "node.h"
  23. #include "gc.h"
  24. #include "iostat.h"
  25. #include <trace/events/f2fs.h>
  26. #define __reverse_ffz(x) __reverse_ffs(~(x))
  27. static struct kmem_cache *discard_entry_slab;
  28. static struct kmem_cache *discard_cmd_slab;
  29. static struct kmem_cache *sit_entry_set_slab;
  30. static struct kmem_cache *revoke_entry_slab;
  31. static unsigned long __reverse_ulong(unsigned char *str)
  32. {
  33. unsigned long tmp = 0;
  34. int shift = 24, idx = 0;
  35. #if BITS_PER_LONG == 64
  36. shift = 56;
  37. #endif
  38. while (shift >= 0) {
  39. tmp |= (unsigned long)str[idx++] << shift;
  40. shift -= BITS_PER_BYTE;
  41. }
  42. return tmp;
  43. }
  44. /*
  45. * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
  46. * MSB and LSB are reversed in a byte by f2fs_set_bit.
  47. */
  48. static inline unsigned long __reverse_ffs(unsigned long word)
  49. {
  50. int num = 0;
  51. #if BITS_PER_LONG == 64
  52. if ((word & 0xffffffff00000000UL) == 0)
  53. num += 32;
  54. else
  55. word >>= 32;
  56. #endif
  57. if ((word & 0xffff0000) == 0)
  58. num += 16;
  59. else
  60. word >>= 16;
  61. if ((word & 0xff00) == 0)
  62. num += 8;
  63. else
  64. word >>= 8;
  65. if ((word & 0xf0) == 0)
  66. num += 4;
  67. else
  68. word >>= 4;
  69. if ((word & 0xc) == 0)
  70. num += 2;
  71. else
  72. word >>= 2;
  73. if ((word & 0x2) == 0)
  74. num += 1;
  75. return num;
  76. }
  77. /*
  78. * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
  79. * f2fs_set_bit makes MSB and LSB reversed in a byte.
  80. * @size must be integral times of unsigned long.
  81. * Example:
  82. * MSB <--> LSB
  83. * f2fs_set_bit(0, bitmap) => 1000 0000
  84. * f2fs_set_bit(7, bitmap) => 0000 0001
  85. */
  86. static unsigned long __find_rev_next_bit(const unsigned long *addr,
  87. unsigned long size, unsigned long offset)
  88. {
  89. const unsigned long *p = addr + BIT_WORD(offset);
  90. unsigned long result = size;
  91. unsigned long tmp;
  92. if (offset >= size)
  93. return size;
  94. size -= (offset & ~(BITS_PER_LONG - 1));
  95. offset %= BITS_PER_LONG;
  96. while (1) {
  97. if (*p == 0)
  98. goto pass;
  99. tmp = __reverse_ulong((unsigned char *)p);
  100. tmp &= ~0UL >> offset;
  101. if (size < BITS_PER_LONG)
  102. tmp &= (~0UL << (BITS_PER_LONG - size));
  103. if (tmp)
  104. goto found;
  105. pass:
  106. if (size <= BITS_PER_LONG)
  107. break;
  108. size -= BITS_PER_LONG;
  109. offset = 0;
  110. p++;
  111. }
  112. return result;
  113. found:
  114. return result - size + __reverse_ffs(tmp);
  115. }
  116. static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
  117. unsigned long size, unsigned long offset)
  118. {
  119. const unsigned long *p = addr + BIT_WORD(offset);
  120. unsigned long result = size;
  121. unsigned long tmp;
  122. if (offset >= size)
  123. return size;
  124. size -= (offset & ~(BITS_PER_LONG - 1));
  125. offset %= BITS_PER_LONG;
  126. while (1) {
  127. if (*p == ~0UL)
  128. goto pass;
  129. tmp = __reverse_ulong((unsigned char *)p);
  130. if (offset)
  131. tmp |= ~0UL << (BITS_PER_LONG - offset);
  132. if (size < BITS_PER_LONG)
  133. tmp |= ~0UL >> size;
  134. if (tmp != ~0UL)
  135. goto found;
  136. pass:
  137. if (size <= BITS_PER_LONG)
  138. break;
  139. size -= BITS_PER_LONG;
  140. offset = 0;
  141. p++;
  142. }
  143. return result;
  144. found:
  145. return result - size + __reverse_ffz(tmp);
  146. }
  147. bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
  148. {
  149. int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
  150. int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
  151. int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
  152. if (f2fs_lfs_mode(sbi))
  153. return false;
  154. if (sbi->gc_mode == GC_URGENT_HIGH)
  155. return true;
  156. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  157. return true;
  158. return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
  159. SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
  160. }
  161. void f2fs_abort_atomic_write(struct inode *inode, bool clean)
  162. {
  163. struct f2fs_inode_info *fi = F2FS_I(inode);
  164. if (!f2fs_is_atomic_file(inode))
  165. return;
  166. if (clean)
  167. truncate_inode_pages_final(inode->i_mapping);
  168. release_atomic_write_cnt(inode);
  169. clear_inode_flag(inode, FI_ATOMIC_COMMITTED);
  170. clear_inode_flag(inode, FI_ATOMIC_REPLACE);
  171. clear_inode_flag(inode, FI_ATOMIC_FILE);
  172. if (is_inode_flag_set(inode, FI_ATOMIC_DIRTIED)) {
  173. clear_inode_flag(inode, FI_ATOMIC_DIRTIED);
  174. /*
  175. * The vfs inode keeps clean during commit, but the f2fs inode
  176. * doesn't. So clear the dirty state after commit and let
  177. * f2fs_mark_inode_dirty_sync ensure a consistent dirty state.
  178. */
  179. f2fs_inode_synced(inode);
  180. f2fs_mark_inode_dirty_sync(inode, true);
  181. }
  182. stat_dec_atomic_inode(inode);
  183. F2FS_I(inode)->atomic_write_task = NULL;
  184. if (clean) {
  185. f2fs_i_size_write(inode, fi->original_i_size);
  186. fi->original_i_size = 0;
  187. }
  188. /* avoid stale dirty inode during eviction */
  189. sync_inode_metadata(inode, 0);
  190. }
  191. static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
  192. block_t new_addr, block_t *old_addr, bool recover)
  193. {
  194. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  195. struct dnode_of_data dn;
  196. struct node_info ni;
  197. int err;
  198. retry:
  199. set_new_dnode(&dn, inode, NULL, NULL, 0);
  200. err = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
  201. if (err) {
  202. if (err == -ENOMEM) {
  203. memalloc_retry_wait(GFP_NOFS);
  204. goto retry;
  205. }
  206. return err;
  207. }
  208. err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
  209. if (err) {
  210. f2fs_put_dnode(&dn);
  211. return err;
  212. }
  213. if (recover) {
  214. /* dn.data_blkaddr is always valid */
  215. if (!__is_valid_data_blkaddr(new_addr)) {
  216. if (new_addr == NULL_ADDR)
  217. dec_valid_block_count(sbi, inode, 1);
  218. f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
  219. f2fs_update_data_blkaddr(&dn, new_addr);
  220. } else {
  221. f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
  222. new_addr, ni.version, true, true);
  223. }
  224. } else {
  225. blkcnt_t count = 1;
  226. err = inc_valid_block_count(sbi, inode, &count, true);
  227. if (err) {
  228. f2fs_put_dnode(&dn);
  229. return err;
  230. }
  231. *old_addr = dn.data_blkaddr;
  232. f2fs_truncate_data_blocks_range(&dn, 1);
  233. dec_valid_block_count(sbi, F2FS_I(inode)->cow_inode, count);
  234. f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr,
  235. ni.version, true, false);
  236. }
  237. f2fs_put_dnode(&dn);
  238. trace_f2fs_replace_atomic_write_block(inode, F2FS_I(inode)->cow_inode,
  239. index, old_addr ? *old_addr : 0, new_addr, recover);
  240. return 0;
  241. }
  242. static void __complete_revoke_list(struct inode *inode, struct list_head *head,
  243. bool revoke)
  244. {
  245. struct revoke_entry *cur, *tmp;
  246. pgoff_t start_index = 0;
  247. bool truncate = is_inode_flag_set(inode, FI_ATOMIC_REPLACE);
  248. list_for_each_entry_safe(cur, tmp, head, list) {
  249. if (revoke) {
  250. __replace_atomic_write_block(inode, cur->index,
  251. cur->old_addr, NULL, true);
  252. } else if (truncate) {
  253. f2fs_truncate_hole(inode, start_index, cur->index);
  254. start_index = cur->index + 1;
  255. }
  256. list_del(&cur->list);
  257. kmem_cache_free(revoke_entry_slab, cur);
  258. }
  259. if (!revoke && truncate)
  260. f2fs_do_truncate_blocks(inode, start_index * PAGE_SIZE, false);
  261. }
  262. static int __f2fs_commit_atomic_write(struct inode *inode)
  263. {
  264. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  265. struct f2fs_inode_info *fi = F2FS_I(inode);
  266. struct inode *cow_inode = fi->cow_inode;
  267. struct revoke_entry *new;
  268. struct list_head revoke_list;
  269. block_t blkaddr;
  270. struct dnode_of_data dn;
  271. pgoff_t len = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  272. pgoff_t off = 0, blen, index;
  273. int ret = 0, i;
  274. INIT_LIST_HEAD(&revoke_list);
  275. while (len) {
  276. blen = min_t(pgoff_t, ADDRS_PER_BLOCK(cow_inode), len);
  277. set_new_dnode(&dn, cow_inode, NULL, NULL, 0);
  278. ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
  279. if (ret && ret != -ENOENT) {
  280. goto out;
  281. } else if (ret == -ENOENT) {
  282. ret = 0;
  283. if (dn.max_level == 0)
  284. goto out;
  285. goto next;
  286. }
  287. blen = min((pgoff_t)ADDRS_PER_PAGE(dn.node_folio, cow_inode),
  288. len);
  289. index = off;
  290. for (i = 0; i < blen; i++, dn.ofs_in_node++, index++) {
  291. blkaddr = f2fs_data_blkaddr(&dn);
  292. if (!__is_valid_data_blkaddr(blkaddr)) {
  293. continue;
  294. } else if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
  295. DATA_GENERIC_ENHANCE)) {
  296. f2fs_put_dnode(&dn);
  297. ret = -EFSCORRUPTED;
  298. goto out;
  299. }
  300. new = f2fs_kmem_cache_alloc(revoke_entry_slab, GFP_NOFS,
  301. true, NULL);
  302. ret = __replace_atomic_write_block(inode, index, blkaddr,
  303. &new->old_addr, false);
  304. if (ret) {
  305. f2fs_put_dnode(&dn);
  306. kmem_cache_free(revoke_entry_slab, new);
  307. goto out;
  308. }
  309. f2fs_update_data_blkaddr(&dn, NULL_ADDR);
  310. new->index = index;
  311. list_add_tail(&new->list, &revoke_list);
  312. }
  313. f2fs_put_dnode(&dn);
  314. next:
  315. off += blen;
  316. len -= blen;
  317. }
  318. out:
  319. if (time_to_inject(sbi, FAULT_ATOMIC_TIMEOUT))
  320. f2fs_schedule_timeout_killable(DEFAULT_FAULT_TIMEOUT, true);
  321. if (ret) {
  322. sbi->revoked_atomic_block += fi->atomic_write_cnt;
  323. } else {
  324. sbi->committed_atomic_block += fi->atomic_write_cnt;
  325. set_inode_flag(inode, FI_ATOMIC_COMMITTED);
  326. /*
  327. * inode may has no FI_ATOMIC_DIRTIED flag due to no write
  328. * before commit.
  329. */
  330. if (is_inode_flag_set(inode, FI_ATOMIC_DIRTIED)) {
  331. /* clear atomic dirty status and set vfs dirty status */
  332. clear_inode_flag(inode, FI_ATOMIC_DIRTIED);
  333. f2fs_mark_inode_dirty_sync(inode, true);
  334. }
  335. }
  336. __complete_revoke_list(inode, &revoke_list, ret ? true : false);
  337. return ret;
  338. }
  339. int f2fs_commit_atomic_write(struct inode *inode)
  340. {
  341. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  342. struct f2fs_inode_info *fi = F2FS_I(inode);
  343. struct f2fs_lock_context lc;
  344. int err;
  345. err = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
  346. if (err)
  347. return err;
  348. f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
  349. f2fs_lock_op(sbi, &lc);
  350. err = __f2fs_commit_atomic_write(inode);
  351. f2fs_unlock_op(sbi, &lc);
  352. f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
  353. return err;
  354. }
  355. /*
  356. * This function balances dirty node and dentry pages.
  357. * In addition, it controls garbage collection.
  358. */
  359. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
  360. {
  361. if (f2fs_cp_error(sbi))
  362. return;
  363. if (time_to_inject(sbi, FAULT_CHECKPOINT))
  364. f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_FAULT_INJECT);
  365. /* balance_fs_bg is able to be pending */
  366. if (need && excess_cached_nats(sbi))
  367. f2fs_balance_fs_bg(sbi, false);
  368. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  369. return;
  370. /*
  371. * We should do GC or end up with checkpoint, if there are so many dirty
  372. * dir/node pages without enough free segments.
  373. */
  374. if (has_enough_free_secs(sbi, 0, 0))
  375. return;
  376. if (test_opt(sbi, GC_MERGE) && sbi->gc_thread &&
  377. sbi->gc_thread->f2fs_gc_task) {
  378. DEFINE_WAIT(wait);
  379. prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait,
  380. TASK_UNINTERRUPTIBLE);
  381. wake_up(&sbi->gc_thread->gc_wait_queue_head);
  382. io_schedule();
  383. finish_wait(&sbi->gc_thread->fggc_wq, &wait);
  384. } else {
  385. struct f2fs_gc_control gc_control = {
  386. .victim_segno = NULL_SEGNO,
  387. .init_gc_type = f2fs_sb_has_blkzoned(sbi) ?
  388. FG_GC : BG_GC,
  389. .no_bg_gc = true,
  390. .should_migrate_blocks = false,
  391. .err_gc_skipped = false,
  392. .nr_free_secs = 1 };
  393. f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
  394. stat_inc_gc_call_count(sbi, FOREGROUND);
  395. f2fs_gc(sbi, &gc_control);
  396. }
  397. }
  398. static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi)
  399. {
  400. int factor = f2fs_rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2;
  401. unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS);
  402. unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA);
  403. unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES);
  404. unsigned int meta = get_pages(sbi, F2FS_DIRTY_META);
  405. unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
  406. unsigned int threshold =
  407. SEGS_TO_BLKS(sbi, (factor * DEFAULT_DIRTY_THRESHOLD));
  408. unsigned int global_threshold = threshold * 3 / 2;
  409. if (dents >= threshold || qdata >= threshold ||
  410. nodes >= threshold || meta >= threshold ||
  411. imeta >= threshold)
  412. return true;
  413. return dents + qdata + nodes + meta + imeta > global_threshold;
  414. }
  415. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
  416. {
  417. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  418. return;
  419. /* try to shrink extent cache when there is no enough memory */
  420. if (!f2fs_available_free_memory(sbi, READ_EXTENT_CACHE))
  421. f2fs_shrink_read_extent_tree(sbi,
  422. READ_EXTENT_CACHE_SHRINK_NUMBER);
  423. /* try to shrink age extent cache when there is no enough memory */
  424. if (!f2fs_available_free_memory(sbi, AGE_EXTENT_CACHE))
  425. f2fs_shrink_age_extent_tree(sbi,
  426. AGE_EXTENT_CACHE_SHRINK_NUMBER);
  427. /* check the # of cached NAT entries */
  428. if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
  429. f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
  430. if (!f2fs_available_free_memory(sbi, FREE_NIDS))
  431. f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
  432. else
  433. f2fs_build_free_nids(sbi, false, false);
  434. if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) ||
  435. excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi))
  436. goto do_sync;
  437. /* there is background inflight IO or foreground operation recently */
  438. if (is_inflight_io(sbi, REQ_TIME) ||
  439. (!f2fs_time_over(sbi, REQ_TIME) && f2fs_rwsem_is_locked(&sbi->cp_rwsem)))
  440. return;
  441. /* exceed periodical checkpoint timeout threshold */
  442. if (f2fs_time_over(sbi, CP_TIME))
  443. goto do_sync;
  444. /* checkpoint is the only way to shrink partial cached entries */
  445. if (f2fs_available_free_memory(sbi, NAT_ENTRIES) &&
  446. f2fs_available_free_memory(sbi, INO_ENTRIES))
  447. return;
  448. do_sync:
  449. if (test_opt(sbi, DATA_FLUSH) && from_bg) {
  450. struct blk_plug plug;
  451. mutex_lock(&sbi->flush_lock);
  452. blk_start_plug(&plug);
  453. f2fs_sync_dirty_inodes(sbi, FILE_INODE, false);
  454. blk_finish_plug(&plug);
  455. mutex_unlock(&sbi->flush_lock);
  456. }
  457. stat_inc_cp_call_count(sbi, BACKGROUND);
  458. f2fs_sync_fs(sbi->sb, 1);
  459. }
  460. static int __submit_flush_wait(struct f2fs_sb_info *sbi,
  461. struct block_device *bdev)
  462. {
  463. int ret = blkdev_issue_flush(bdev);
  464. trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
  465. test_opt(sbi, FLUSH_MERGE), ret);
  466. if (!ret)
  467. f2fs_update_iostat(sbi, NULL, FS_FLUSH_IO, 0);
  468. return ret;
  469. }
  470. static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
  471. {
  472. int ret = 0;
  473. int i;
  474. if (!f2fs_is_multi_device(sbi))
  475. return __submit_flush_wait(sbi, sbi->sb->s_bdev);
  476. for (i = 0; i < sbi->s_ndevs; i++) {
  477. if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
  478. continue;
  479. ret = __submit_flush_wait(sbi, FDEV(i).bdev);
  480. if (ret)
  481. break;
  482. }
  483. return ret;
  484. }
  485. static int issue_flush_thread(void *data)
  486. {
  487. struct f2fs_sb_info *sbi = data;
  488. struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
  489. wait_queue_head_t *q = &fcc->flush_wait_queue;
  490. repeat:
  491. if (kthread_should_stop())
  492. return 0;
  493. if (!llist_empty(&fcc->issue_list)) {
  494. struct flush_cmd *cmd, *next;
  495. int ret;
  496. fcc->dispatch_list = llist_del_all(&fcc->issue_list);
  497. fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
  498. cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
  499. ret = submit_flush_wait(sbi, cmd->ino);
  500. atomic_inc(&fcc->issued_flush);
  501. llist_for_each_entry_safe(cmd, next,
  502. fcc->dispatch_list, llnode) {
  503. cmd->ret = ret;
  504. complete(&cmd->wait);
  505. }
  506. fcc->dispatch_list = NULL;
  507. }
  508. wait_event_interruptible(*q,
  509. kthread_should_stop() || !llist_empty(&fcc->issue_list));
  510. goto repeat;
  511. }
  512. int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
  513. {
  514. struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
  515. struct flush_cmd cmd;
  516. int ret;
  517. if (test_opt(sbi, NOBARRIER))
  518. return 0;
  519. if (!test_opt(sbi, FLUSH_MERGE)) {
  520. atomic_inc(&fcc->queued_flush);
  521. ret = submit_flush_wait(sbi, ino);
  522. atomic_dec(&fcc->queued_flush);
  523. atomic_inc(&fcc->issued_flush);
  524. return ret;
  525. }
  526. if (atomic_inc_return(&fcc->queued_flush) == 1 ||
  527. f2fs_is_multi_device(sbi)) {
  528. ret = submit_flush_wait(sbi, ino);
  529. atomic_dec(&fcc->queued_flush);
  530. atomic_inc(&fcc->issued_flush);
  531. return ret;
  532. }
  533. cmd.ino = ino;
  534. init_completion(&cmd.wait);
  535. llist_add(&cmd.llnode, &fcc->issue_list);
  536. /*
  537. * update issue_list before we wake up issue_flush thread, this
  538. * smp_mb() pairs with another barrier in ___wait_event(), see
  539. * more details in comments of waitqueue_active().
  540. */
  541. smp_mb();
  542. if (waitqueue_active(&fcc->flush_wait_queue))
  543. wake_up(&fcc->flush_wait_queue);
  544. if (fcc->f2fs_issue_flush) {
  545. wait_for_completion(&cmd.wait);
  546. atomic_dec(&fcc->queued_flush);
  547. } else {
  548. struct llist_node *list;
  549. list = llist_del_all(&fcc->issue_list);
  550. if (!list) {
  551. wait_for_completion(&cmd.wait);
  552. atomic_dec(&fcc->queued_flush);
  553. } else {
  554. struct flush_cmd *tmp, *next;
  555. ret = submit_flush_wait(sbi, ino);
  556. llist_for_each_entry_safe(tmp, next, list, llnode) {
  557. if (tmp == &cmd) {
  558. cmd.ret = ret;
  559. atomic_dec(&fcc->queued_flush);
  560. continue;
  561. }
  562. tmp->ret = ret;
  563. complete(&tmp->wait);
  564. }
  565. }
  566. }
  567. return cmd.ret;
  568. }
  569. int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
  570. {
  571. dev_t dev = sbi->sb->s_bdev->bd_dev;
  572. struct flush_cmd_control *fcc;
  573. if (SM_I(sbi)->fcc_info) {
  574. fcc = SM_I(sbi)->fcc_info;
  575. if (fcc->f2fs_issue_flush)
  576. return 0;
  577. goto init_thread;
  578. }
  579. fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
  580. if (!fcc)
  581. return -ENOMEM;
  582. atomic_set(&fcc->issued_flush, 0);
  583. atomic_set(&fcc->queued_flush, 0);
  584. init_waitqueue_head(&fcc->flush_wait_queue);
  585. init_llist_head(&fcc->issue_list);
  586. SM_I(sbi)->fcc_info = fcc;
  587. if (!test_opt(sbi, FLUSH_MERGE))
  588. return 0;
  589. init_thread:
  590. fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
  591. "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
  592. if (IS_ERR(fcc->f2fs_issue_flush)) {
  593. int err = PTR_ERR(fcc->f2fs_issue_flush);
  594. fcc->f2fs_issue_flush = NULL;
  595. return err;
  596. }
  597. return 0;
  598. }
  599. void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
  600. {
  601. struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
  602. if (fcc && fcc->f2fs_issue_flush) {
  603. struct task_struct *flush_thread = fcc->f2fs_issue_flush;
  604. fcc->f2fs_issue_flush = NULL;
  605. kthread_stop(flush_thread);
  606. }
  607. if (free) {
  608. kfree(fcc);
  609. SM_I(sbi)->fcc_info = NULL;
  610. }
  611. }
  612. int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
  613. {
  614. int ret = 0, i;
  615. if (!f2fs_is_multi_device(sbi))
  616. return 0;
  617. if (test_opt(sbi, NOBARRIER))
  618. return 0;
  619. for (i = 1; i < sbi->s_ndevs; i++) {
  620. int count = DEFAULT_RETRY_IO_COUNT;
  621. if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
  622. continue;
  623. do {
  624. ret = __submit_flush_wait(sbi, FDEV(i).bdev);
  625. if (ret)
  626. f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
  627. } while (ret && --count);
  628. if (ret) {
  629. f2fs_stop_checkpoint(sbi, false,
  630. STOP_CP_REASON_FLUSH_FAIL);
  631. break;
  632. }
  633. spin_lock(&sbi->dev_lock);
  634. f2fs_clear_bit(i, (char *)&sbi->dirty_device);
  635. spin_unlock(&sbi->dev_lock);
  636. }
  637. return ret;
  638. }
  639. static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  640. enum dirty_type dirty_type)
  641. {
  642. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  643. /* need not be added */
  644. if (is_curseg(sbi, segno))
  645. return;
  646. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  647. dirty_i->nr_dirty[dirty_type]++;
  648. if (dirty_type == DIRTY) {
  649. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  650. enum dirty_type t = sentry->type;
  651. if (unlikely(t >= DIRTY)) {
  652. f2fs_bug_on(sbi, 1);
  653. return;
  654. }
  655. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
  656. dirty_i->nr_dirty[t]++;
  657. if (__is_large_section(sbi)) {
  658. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  659. block_t valid_blocks =
  660. get_valid_blocks(sbi, segno, true);
  661. f2fs_bug_on(sbi,
  662. (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
  663. !valid_blocks) ||
  664. valid_blocks == CAP_BLKS_PER_SEC(sbi));
  665. if (!is_cursec(sbi, secno))
  666. set_bit(secno, dirty_i->dirty_secmap);
  667. }
  668. }
  669. }
  670. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  671. enum dirty_type dirty_type)
  672. {
  673. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  674. block_t valid_blocks;
  675. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  676. dirty_i->nr_dirty[dirty_type]--;
  677. if (dirty_type == DIRTY) {
  678. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  679. enum dirty_type t = sentry->type;
  680. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  681. dirty_i->nr_dirty[t]--;
  682. valid_blocks = get_valid_blocks(sbi, segno, true);
  683. if (valid_blocks == 0) {
  684. clear_bit(GET_SEC_FROM_SEG(sbi, segno),
  685. dirty_i->victim_secmap);
  686. #ifdef CONFIG_F2FS_CHECK_FS
  687. clear_bit(segno, SIT_I(sbi)->invalid_segmap);
  688. #endif
  689. }
  690. if (__is_large_section(sbi)) {
  691. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  692. if (!valid_blocks ||
  693. valid_blocks == CAP_BLKS_PER_SEC(sbi)) {
  694. clear_bit(secno, dirty_i->dirty_secmap);
  695. return;
  696. }
  697. if (!is_cursec(sbi, secno))
  698. set_bit(secno, dirty_i->dirty_secmap);
  699. }
  700. }
  701. }
  702. /*
  703. * Should not occur error such as -ENOMEM.
  704. * Adding dirty entry into seglist is not critical operation.
  705. * If a given segment is one of current working segments, it won't be added.
  706. */
  707. static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  708. {
  709. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  710. unsigned short valid_blocks, ckpt_valid_blocks;
  711. unsigned int usable_blocks;
  712. if (segno == NULL_SEGNO || is_curseg(sbi, segno))
  713. return;
  714. usable_blocks = f2fs_usable_blks_in_seg(sbi, segno);
  715. mutex_lock(&dirty_i->seglist_lock);
  716. valid_blocks = get_valid_blocks(sbi, segno, false);
  717. ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false);
  718. if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) ||
  719. ckpt_valid_blocks == usable_blocks)) {
  720. __locate_dirty_segment(sbi, segno, PRE);
  721. __remove_dirty_segment(sbi, segno, DIRTY);
  722. } else if (valid_blocks < usable_blocks) {
  723. __locate_dirty_segment(sbi, segno, DIRTY);
  724. } else {
  725. /* Recovery routine with SSR needs this */
  726. __remove_dirty_segment(sbi, segno, DIRTY);
  727. }
  728. mutex_unlock(&dirty_i->seglist_lock);
  729. }
  730. /* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */
  731. void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi)
  732. {
  733. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  734. unsigned int segno;
  735. mutex_lock(&dirty_i->seglist_lock);
  736. for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
  737. if (get_valid_blocks(sbi, segno, false))
  738. continue;
  739. if (is_curseg(sbi, segno))
  740. continue;
  741. __locate_dirty_segment(sbi, segno, PRE);
  742. __remove_dirty_segment(sbi, segno, DIRTY);
  743. }
  744. mutex_unlock(&dirty_i->seglist_lock);
  745. }
  746. block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi)
  747. {
  748. int ovp_hole_segs =
  749. (overprovision_segments(sbi) - reserved_segments(sbi));
  750. block_t ovp_holes = SEGS_TO_BLKS(sbi, ovp_hole_segs);
  751. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  752. block_t holes[2] = {0, 0}; /* DATA and NODE */
  753. block_t unusable;
  754. struct seg_entry *se;
  755. unsigned int segno;
  756. mutex_lock(&dirty_i->seglist_lock);
  757. for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
  758. se = get_seg_entry(sbi, segno);
  759. if (IS_NODESEG(se->type))
  760. holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) -
  761. se->valid_blocks;
  762. else
  763. holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) -
  764. se->valid_blocks;
  765. }
  766. mutex_unlock(&dirty_i->seglist_lock);
  767. unusable = max(holes[DATA], holes[NODE]);
  768. if (unusable > ovp_holes)
  769. return unusable - ovp_holes;
  770. return 0;
  771. }
  772. int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable)
  773. {
  774. int ovp_hole_segs =
  775. (overprovision_segments(sbi) - reserved_segments(sbi));
  776. if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
  777. return 0;
  778. if (unusable > F2FS_OPTION(sbi).unusable_cap)
  779. return -EAGAIN;
  780. if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) &&
  781. dirty_segments(sbi) > ovp_hole_segs)
  782. return -EAGAIN;
  783. if (has_not_enough_free_secs(sbi, 0, 0))
  784. return -EAGAIN;
  785. return 0;
  786. }
  787. /* This is only used by SBI_CP_DISABLED */
  788. static unsigned int get_free_segment(struct f2fs_sb_info *sbi)
  789. {
  790. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  791. unsigned int segno = 0;
  792. mutex_lock(&dirty_i->seglist_lock);
  793. for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
  794. if (get_valid_blocks(sbi, segno, false))
  795. continue;
  796. if (get_ckpt_valid_blocks(sbi, segno, false))
  797. continue;
  798. mutex_unlock(&dirty_i->seglist_lock);
  799. return segno;
  800. }
  801. mutex_unlock(&dirty_i->seglist_lock);
  802. return NULL_SEGNO;
  803. }
  804. static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
  805. struct block_device *bdev, block_t lstart,
  806. block_t start, block_t len)
  807. {
  808. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  809. struct list_head *pend_list;
  810. struct discard_cmd *dc;
  811. f2fs_bug_on(sbi, !len);
  812. pend_list = &dcc->pend_list[plist_idx(len)];
  813. dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL);
  814. INIT_LIST_HEAD(&dc->list);
  815. dc->bdev = bdev;
  816. dc->di.lstart = lstart;
  817. dc->di.start = start;
  818. dc->di.len = len;
  819. dc->ref = 0;
  820. dc->state = D_PREP;
  821. dc->queued = 0;
  822. dc->error = 0;
  823. init_completion(&dc->wait);
  824. list_add_tail(&dc->list, pend_list);
  825. spin_lock_init(&dc->lock);
  826. dc->bio_ref = 0;
  827. atomic_inc(&dcc->discard_cmd_cnt);
  828. dcc->undiscard_blks += len;
  829. return dc;
  830. }
  831. static bool f2fs_check_discard_tree(struct f2fs_sb_info *sbi)
  832. {
  833. #ifdef CONFIG_F2FS_CHECK_FS
  834. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  835. struct rb_node *cur = rb_first_cached(&dcc->root), *next;
  836. struct discard_cmd *cur_dc, *next_dc;
  837. while (cur) {
  838. next = rb_next(cur);
  839. if (!next)
  840. return true;
  841. cur_dc = rb_entry(cur, struct discard_cmd, rb_node);
  842. next_dc = rb_entry(next, struct discard_cmd, rb_node);
  843. if (cur_dc->di.lstart + cur_dc->di.len > next_dc->di.lstart) {
  844. f2fs_info(sbi, "broken discard_rbtree, "
  845. "cur(%u, %u) next(%u, %u)",
  846. cur_dc->di.lstart, cur_dc->di.len,
  847. next_dc->di.lstart, next_dc->di.len);
  848. return false;
  849. }
  850. cur = next;
  851. }
  852. #endif
  853. return true;
  854. }
  855. static struct discard_cmd *__lookup_discard_cmd(struct f2fs_sb_info *sbi,
  856. block_t blkaddr)
  857. {
  858. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  859. struct rb_node *node = dcc->root.rb_root.rb_node;
  860. struct discard_cmd *dc;
  861. while (node) {
  862. dc = rb_entry(node, struct discard_cmd, rb_node);
  863. if (blkaddr < dc->di.lstart)
  864. node = node->rb_left;
  865. else if (blkaddr >= dc->di.lstart + dc->di.len)
  866. node = node->rb_right;
  867. else
  868. return dc;
  869. }
  870. return NULL;
  871. }
  872. static struct discard_cmd *__lookup_discard_cmd_ret(struct rb_root_cached *root,
  873. block_t blkaddr,
  874. struct discard_cmd **prev_entry,
  875. struct discard_cmd **next_entry,
  876. struct rb_node ***insert_p,
  877. struct rb_node **insert_parent)
  878. {
  879. struct rb_node **pnode = &root->rb_root.rb_node;
  880. struct rb_node *parent = NULL, *tmp_node;
  881. struct discard_cmd *dc;
  882. *insert_p = NULL;
  883. *insert_parent = NULL;
  884. *prev_entry = NULL;
  885. *next_entry = NULL;
  886. if (RB_EMPTY_ROOT(&root->rb_root))
  887. return NULL;
  888. while (*pnode) {
  889. parent = *pnode;
  890. dc = rb_entry(*pnode, struct discard_cmd, rb_node);
  891. if (blkaddr < dc->di.lstart)
  892. pnode = &(*pnode)->rb_left;
  893. else if (blkaddr >= dc->di.lstart + dc->di.len)
  894. pnode = &(*pnode)->rb_right;
  895. else
  896. goto lookup_neighbors;
  897. }
  898. *insert_p = pnode;
  899. *insert_parent = parent;
  900. dc = rb_entry(parent, struct discard_cmd, rb_node);
  901. tmp_node = parent;
  902. if (parent && blkaddr > dc->di.lstart)
  903. tmp_node = rb_next(parent);
  904. *next_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
  905. tmp_node = parent;
  906. if (parent && blkaddr < dc->di.lstart)
  907. tmp_node = rb_prev(parent);
  908. *prev_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
  909. return NULL;
  910. lookup_neighbors:
  911. /* lookup prev node for merging backward later */
  912. tmp_node = rb_prev(&dc->rb_node);
  913. *prev_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
  914. /* lookup next node for merging frontward later */
  915. tmp_node = rb_next(&dc->rb_node);
  916. *next_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
  917. return dc;
  918. }
  919. static void __detach_discard_cmd(struct discard_cmd_control *dcc,
  920. struct discard_cmd *dc)
  921. {
  922. if (dc->state == D_DONE)
  923. atomic_sub(dc->queued, &dcc->queued_discard);
  924. list_del(&dc->list);
  925. rb_erase_cached(&dc->rb_node, &dcc->root);
  926. dcc->undiscard_blks -= dc->di.len;
  927. kmem_cache_free(discard_cmd_slab, dc);
  928. atomic_dec(&dcc->discard_cmd_cnt);
  929. }
  930. static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
  931. struct discard_cmd *dc)
  932. {
  933. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  934. unsigned long flags;
  935. trace_f2fs_remove_discard(dc->bdev, dc->di.start, dc->di.len);
  936. spin_lock_irqsave(&dc->lock, flags);
  937. if (dc->bio_ref) {
  938. spin_unlock_irqrestore(&dc->lock, flags);
  939. return;
  940. }
  941. spin_unlock_irqrestore(&dc->lock, flags);
  942. f2fs_bug_on(sbi, dc->ref);
  943. if (dc->error == -EOPNOTSUPP)
  944. dc->error = 0;
  945. if (dc->error)
  946. f2fs_info_ratelimited(sbi,
  947. "Issue discard(%u, %u, %u) failed, ret: %d",
  948. dc->di.lstart, dc->di.start, dc->di.len, dc->error);
  949. __detach_discard_cmd(dcc, dc);
  950. }
  951. static void f2fs_submit_discard_endio(struct bio *bio)
  952. {
  953. struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
  954. unsigned long flags;
  955. spin_lock_irqsave(&dc->lock, flags);
  956. if (!dc->error)
  957. dc->error = blk_status_to_errno(bio->bi_status);
  958. dc->bio_ref--;
  959. if (!dc->bio_ref && dc->state == D_SUBMIT) {
  960. dc->state = D_DONE;
  961. complete_all(&dc->wait);
  962. }
  963. spin_unlock_irqrestore(&dc->lock, flags);
  964. bio_put(bio);
  965. }
  966. static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
  967. block_t start, block_t end)
  968. {
  969. #ifdef CONFIG_F2FS_CHECK_FS
  970. struct seg_entry *sentry;
  971. unsigned int segno;
  972. block_t blk = start;
  973. unsigned long offset, size, *map;
  974. while (blk < end) {
  975. segno = GET_SEGNO(sbi, blk);
  976. sentry = get_seg_entry(sbi, segno);
  977. offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
  978. if (end < START_BLOCK(sbi, segno + 1))
  979. size = GET_BLKOFF_FROM_SEG0(sbi, end);
  980. else
  981. size = BLKS_PER_SEG(sbi);
  982. map = (unsigned long *)(sentry->cur_valid_map);
  983. offset = __find_rev_next_bit(map, size, offset);
  984. f2fs_bug_on(sbi, offset != size);
  985. blk = START_BLOCK(sbi, segno + 1);
  986. }
  987. #endif
  988. }
  989. static void __init_discard_policy(struct f2fs_sb_info *sbi,
  990. struct discard_policy *dpolicy,
  991. int discard_type, unsigned int granularity)
  992. {
  993. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  994. /* common policy */
  995. dpolicy->type = discard_type;
  996. dpolicy->sync = true;
  997. dpolicy->ordered = false;
  998. dpolicy->granularity = granularity;
  999. dpolicy->max_requests = dcc->max_discard_request;
  1000. dpolicy->io_aware_gran = dcc->discard_io_aware_gran;
  1001. dpolicy->timeout = false;
  1002. if (discard_type == DPOLICY_BG) {
  1003. dpolicy->min_interval = dcc->min_discard_issue_time;
  1004. dpolicy->mid_interval = dcc->mid_discard_issue_time;
  1005. dpolicy->max_interval = dcc->max_discard_issue_time;
  1006. if (dcc->discard_io_aware == DPOLICY_IO_AWARE_ENABLE)
  1007. dpolicy->io_aware = true;
  1008. else if (dcc->discard_io_aware == DPOLICY_IO_AWARE_DISABLE)
  1009. dpolicy->io_aware = false;
  1010. dpolicy->sync = false;
  1011. dpolicy->ordered = true;
  1012. if (utilization(sbi) > dcc->discard_urgent_util) {
  1013. dpolicy->granularity = MIN_DISCARD_GRANULARITY;
  1014. if (atomic_read(&dcc->discard_cmd_cnt))
  1015. dpolicy->max_interval =
  1016. dcc->min_discard_issue_time;
  1017. }
  1018. } else if (discard_type == DPOLICY_FORCE) {
  1019. dpolicy->min_interval = dcc->min_discard_issue_time;
  1020. dpolicy->mid_interval = dcc->mid_discard_issue_time;
  1021. dpolicy->max_interval = dcc->max_discard_issue_time;
  1022. dpolicy->io_aware = false;
  1023. } else if (discard_type == DPOLICY_FSTRIM) {
  1024. dpolicy->io_aware = false;
  1025. } else if (discard_type == DPOLICY_UMOUNT) {
  1026. dpolicy->io_aware = false;
  1027. /* we need to issue all to keep CP_TRIMMED_FLAG */
  1028. dpolicy->granularity = MIN_DISCARD_GRANULARITY;
  1029. dpolicy->timeout = true;
  1030. }
  1031. }
  1032. static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
  1033. struct block_device *bdev, block_t lstart,
  1034. block_t start, block_t len);
  1035. #ifdef CONFIG_BLK_DEV_ZONED
  1036. static void __submit_zone_reset_cmd(struct f2fs_sb_info *sbi,
  1037. struct discard_cmd *dc, blk_opf_t flag,
  1038. struct list_head *wait_list,
  1039. unsigned int *issued)
  1040. {
  1041. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1042. struct block_device *bdev = dc->bdev;
  1043. struct bio *bio = bio_alloc(bdev, 0, REQ_OP_ZONE_RESET | flag, GFP_NOFS);
  1044. unsigned long flags;
  1045. trace_f2fs_issue_reset_zone(bdev, dc->di.start);
  1046. spin_lock_irqsave(&dc->lock, flags);
  1047. dc->state = D_SUBMIT;
  1048. dc->bio_ref++;
  1049. spin_unlock_irqrestore(&dc->lock, flags);
  1050. if (issued)
  1051. (*issued)++;
  1052. atomic_inc(&dcc->queued_discard);
  1053. dc->queued++;
  1054. list_move_tail(&dc->list, wait_list);
  1055. /* sanity check on discard range */
  1056. __check_sit_bitmap(sbi, dc->di.lstart, dc->di.lstart + dc->di.len);
  1057. bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(dc->di.start);
  1058. bio->bi_private = dc;
  1059. bio->bi_end_io = f2fs_submit_discard_endio;
  1060. submit_bio(bio);
  1061. atomic_inc(&dcc->issued_discard);
  1062. f2fs_update_iostat(sbi, NULL, FS_ZONE_RESET_IO, dc->di.len * F2FS_BLKSIZE);
  1063. }
  1064. #endif
  1065. /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
  1066. static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
  1067. struct discard_policy *dpolicy,
  1068. struct discard_cmd *dc, int *issued)
  1069. {
  1070. struct block_device *bdev = dc->bdev;
  1071. unsigned int max_discard_blocks =
  1072. SECTOR_TO_BLOCK(bdev_max_discard_sectors(bdev));
  1073. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1074. struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
  1075. &(dcc->fstrim_list) : &(dcc->wait_list);
  1076. blk_opf_t flag = dpolicy->sync ? REQ_SYNC : 0;
  1077. block_t lstart, start, len, total_len;
  1078. if (dc->state != D_PREP)
  1079. return 0;
  1080. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
  1081. return 0;
  1082. #ifdef CONFIG_BLK_DEV_ZONED
  1083. if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev)) {
  1084. int devi = f2fs_bdev_index(sbi, bdev);
  1085. if (devi < 0)
  1086. return -EINVAL;
  1087. if (f2fs_blkz_is_seq(sbi, devi, dc->di.start)) {
  1088. __submit_zone_reset_cmd(sbi, dc, flag,
  1089. wait_list, issued);
  1090. return 0;
  1091. }
  1092. }
  1093. #endif
  1094. /*
  1095. * stop issuing discard for any of below cases:
  1096. * 1. device is conventional zone, but it doesn't support discard.
  1097. * 2. device is regulare device, after snapshot it doesn't support
  1098. * discard.
  1099. */
  1100. if (!bdev_max_discard_sectors(bdev))
  1101. return -EOPNOTSUPP;
  1102. trace_f2fs_issue_discard(bdev, dc->di.start, dc->di.len);
  1103. lstart = dc->di.lstart;
  1104. start = dc->di.start;
  1105. len = dc->di.len;
  1106. total_len = len;
  1107. dc->di.len = 0;
  1108. while (total_len && *issued < dpolicy->max_requests) {
  1109. struct bio *bio = NULL;
  1110. unsigned long flags;
  1111. bool last = true;
  1112. if (len > max_discard_blocks) {
  1113. len = max_discard_blocks;
  1114. last = false;
  1115. }
  1116. (*issued)++;
  1117. if (*issued == dpolicy->max_requests)
  1118. last = true;
  1119. dc->di.len += len;
  1120. __blkdev_issue_discard(bdev, SECTOR_FROM_BLOCK(start),
  1121. SECTOR_FROM_BLOCK(len), GFP_NOFS, &bio);
  1122. f2fs_bug_on(sbi, !bio);
  1123. /*
  1124. * should keep before submission to avoid D_DONE
  1125. * right away
  1126. */
  1127. spin_lock_irqsave(&dc->lock, flags);
  1128. if (last)
  1129. dc->state = D_SUBMIT;
  1130. else
  1131. dc->state = D_PARTIAL;
  1132. dc->bio_ref++;
  1133. spin_unlock_irqrestore(&dc->lock, flags);
  1134. atomic_inc(&dcc->queued_discard);
  1135. dc->queued++;
  1136. list_move_tail(&dc->list, wait_list);
  1137. /* sanity check on discard range */
  1138. __check_sit_bitmap(sbi, lstart, lstart + len);
  1139. bio->bi_private = dc;
  1140. bio->bi_end_io = f2fs_submit_discard_endio;
  1141. bio->bi_opf |= flag;
  1142. submit_bio(bio);
  1143. atomic_inc(&dcc->issued_discard);
  1144. f2fs_update_iostat(sbi, NULL, FS_DISCARD_IO, len * F2FS_BLKSIZE);
  1145. lstart += len;
  1146. start += len;
  1147. total_len -= len;
  1148. len = total_len;
  1149. }
  1150. if (len) {
  1151. dcc->undiscard_blks -= len;
  1152. __update_discard_tree_range(sbi, bdev, lstart, start, len);
  1153. }
  1154. return 0;
  1155. }
  1156. static void __insert_discard_cmd(struct f2fs_sb_info *sbi,
  1157. struct block_device *bdev, block_t lstart,
  1158. block_t start, block_t len)
  1159. {
  1160. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1161. struct rb_node **p = &dcc->root.rb_root.rb_node;
  1162. struct rb_node *parent = NULL;
  1163. struct discard_cmd *dc;
  1164. bool leftmost = true;
  1165. /* look up rb tree to find parent node */
  1166. while (*p) {
  1167. parent = *p;
  1168. dc = rb_entry(parent, struct discard_cmd, rb_node);
  1169. if (lstart < dc->di.lstart) {
  1170. p = &(*p)->rb_left;
  1171. } else if (lstart >= dc->di.lstart + dc->di.len) {
  1172. p = &(*p)->rb_right;
  1173. leftmost = false;
  1174. } else {
  1175. /* Let's skip to add, if exists */
  1176. return;
  1177. }
  1178. }
  1179. dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
  1180. rb_link_node(&dc->rb_node, parent, p);
  1181. rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost);
  1182. }
  1183. static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
  1184. struct discard_cmd *dc)
  1185. {
  1186. list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->di.len)]);
  1187. }
  1188. static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
  1189. struct discard_cmd *dc, block_t blkaddr)
  1190. {
  1191. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1192. struct discard_info di = dc->di;
  1193. bool modified = false;
  1194. if (dc->state == D_DONE || dc->di.len == 1) {
  1195. __remove_discard_cmd(sbi, dc);
  1196. return;
  1197. }
  1198. dcc->undiscard_blks -= di.len;
  1199. if (blkaddr > di.lstart) {
  1200. dc->di.len = blkaddr - dc->di.lstart;
  1201. dcc->undiscard_blks += dc->di.len;
  1202. __relocate_discard_cmd(dcc, dc);
  1203. modified = true;
  1204. }
  1205. if (blkaddr < di.lstart + di.len - 1) {
  1206. if (modified) {
  1207. __insert_discard_cmd(sbi, dc->bdev, blkaddr + 1,
  1208. di.start + blkaddr + 1 - di.lstart,
  1209. di.lstart + di.len - 1 - blkaddr);
  1210. } else {
  1211. dc->di.lstart++;
  1212. dc->di.len--;
  1213. dc->di.start++;
  1214. dcc->undiscard_blks += dc->di.len;
  1215. __relocate_discard_cmd(dcc, dc);
  1216. }
  1217. }
  1218. }
  1219. static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
  1220. struct block_device *bdev, block_t lstart,
  1221. block_t start, block_t len)
  1222. {
  1223. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1224. struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
  1225. struct discard_cmd *dc;
  1226. struct discard_info di = {0};
  1227. struct rb_node **insert_p = NULL, *insert_parent = NULL;
  1228. unsigned int max_discard_blocks =
  1229. SECTOR_TO_BLOCK(bdev_max_discard_sectors(bdev));
  1230. block_t end = lstart + len;
  1231. dc = __lookup_discard_cmd_ret(&dcc->root, lstart,
  1232. &prev_dc, &next_dc, &insert_p, &insert_parent);
  1233. if (dc)
  1234. prev_dc = dc;
  1235. if (!prev_dc) {
  1236. di.lstart = lstart;
  1237. di.len = next_dc ? next_dc->di.lstart - lstart : len;
  1238. di.len = min(di.len, len);
  1239. di.start = start;
  1240. }
  1241. while (1) {
  1242. struct rb_node *node;
  1243. bool merged = false;
  1244. struct discard_cmd *tdc = NULL;
  1245. if (prev_dc) {
  1246. di.lstart = prev_dc->di.lstart + prev_dc->di.len;
  1247. if (di.lstart < lstart)
  1248. di.lstart = lstart;
  1249. if (di.lstart >= end)
  1250. break;
  1251. if (!next_dc || next_dc->di.lstart > end)
  1252. di.len = end - di.lstart;
  1253. else
  1254. di.len = next_dc->di.lstart - di.lstart;
  1255. di.start = start + di.lstart - lstart;
  1256. }
  1257. if (!di.len)
  1258. goto next;
  1259. if (prev_dc && prev_dc->state == D_PREP &&
  1260. prev_dc->bdev == bdev &&
  1261. __is_discard_back_mergeable(&di, &prev_dc->di,
  1262. max_discard_blocks)) {
  1263. prev_dc->di.len += di.len;
  1264. dcc->undiscard_blks += di.len;
  1265. __relocate_discard_cmd(dcc, prev_dc);
  1266. di = prev_dc->di;
  1267. tdc = prev_dc;
  1268. merged = true;
  1269. }
  1270. if (next_dc && next_dc->state == D_PREP &&
  1271. next_dc->bdev == bdev &&
  1272. __is_discard_front_mergeable(&di, &next_dc->di,
  1273. max_discard_blocks)) {
  1274. next_dc->di.lstart = di.lstart;
  1275. next_dc->di.len += di.len;
  1276. next_dc->di.start = di.start;
  1277. dcc->undiscard_blks += di.len;
  1278. __relocate_discard_cmd(dcc, next_dc);
  1279. if (tdc)
  1280. __remove_discard_cmd(sbi, tdc);
  1281. merged = true;
  1282. }
  1283. if (!merged)
  1284. __insert_discard_cmd(sbi, bdev,
  1285. di.lstart, di.start, di.len);
  1286. next:
  1287. prev_dc = next_dc;
  1288. if (!prev_dc)
  1289. break;
  1290. node = rb_next(&prev_dc->rb_node);
  1291. next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
  1292. }
  1293. }
  1294. #ifdef CONFIG_BLK_DEV_ZONED
  1295. static void __queue_zone_reset_cmd(struct f2fs_sb_info *sbi,
  1296. struct block_device *bdev, block_t blkstart, block_t lblkstart,
  1297. block_t blklen)
  1298. {
  1299. trace_f2fs_queue_reset_zone(bdev, blkstart);
  1300. mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
  1301. __insert_discard_cmd(sbi, bdev, lblkstart, blkstart, blklen);
  1302. mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
  1303. }
  1304. #endif
  1305. static void __queue_discard_cmd(struct f2fs_sb_info *sbi,
  1306. struct block_device *bdev, block_t blkstart, block_t blklen)
  1307. {
  1308. block_t lblkstart = blkstart;
  1309. if (!f2fs_bdev_support_discard(bdev))
  1310. return;
  1311. trace_f2fs_queue_discard(bdev, blkstart, blklen);
  1312. if (f2fs_is_multi_device(sbi)) {
  1313. int devi = f2fs_target_device_index(sbi, blkstart);
  1314. blkstart -= FDEV(devi).start_blk;
  1315. }
  1316. mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
  1317. __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
  1318. mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
  1319. }
  1320. static void __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
  1321. struct discard_policy *dpolicy, int *issued)
  1322. {
  1323. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1324. struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
  1325. struct rb_node **insert_p = NULL, *insert_parent = NULL;
  1326. struct discard_cmd *dc;
  1327. struct blk_plug plug;
  1328. bool io_interrupted = false;
  1329. mutex_lock(&dcc->cmd_lock);
  1330. dc = __lookup_discard_cmd_ret(&dcc->root, dcc->next_pos,
  1331. &prev_dc, &next_dc, &insert_p, &insert_parent);
  1332. if (!dc)
  1333. dc = next_dc;
  1334. blk_start_plug(&plug);
  1335. while (dc) {
  1336. struct rb_node *node;
  1337. int err = 0;
  1338. if (dc->state != D_PREP)
  1339. goto next;
  1340. if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) {
  1341. io_interrupted = true;
  1342. break;
  1343. }
  1344. dcc->next_pos = dc->di.lstart + dc->di.len;
  1345. err = __submit_discard_cmd(sbi, dpolicy, dc, issued);
  1346. if (*issued >= dpolicy->max_requests)
  1347. break;
  1348. next:
  1349. node = rb_next(&dc->rb_node);
  1350. if (err)
  1351. __remove_discard_cmd(sbi, dc);
  1352. dc = rb_entry_safe(node, struct discard_cmd, rb_node);
  1353. }
  1354. blk_finish_plug(&plug);
  1355. if (!dc)
  1356. dcc->next_pos = 0;
  1357. mutex_unlock(&dcc->cmd_lock);
  1358. if (!(*issued) && io_interrupted)
  1359. *issued = -1;
  1360. }
  1361. static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
  1362. struct discard_policy *dpolicy);
  1363. static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
  1364. struct discard_policy *dpolicy)
  1365. {
  1366. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1367. struct list_head *pend_list;
  1368. struct discard_cmd *dc, *tmp;
  1369. struct blk_plug plug;
  1370. int i, issued;
  1371. bool io_interrupted = false;
  1372. if (dpolicy->timeout)
  1373. f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT);
  1374. retry:
  1375. issued = 0;
  1376. for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  1377. if (dpolicy->timeout &&
  1378. f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
  1379. break;
  1380. if (i + 1 < dpolicy->granularity)
  1381. break;
  1382. if (i + 1 < dcc->max_ordered_discard && dpolicy->ordered) {
  1383. __issue_discard_cmd_orderly(sbi, dpolicy, &issued);
  1384. return issued;
  1385. }
  1386. pend_list = &dcc->pend_list[i];
  1387. mutex_lock(&dcc->cmd_lock);
  1388. if (list_empty(pend_list))
  1389. goto next;
  1390. if (unlikely(dcc->rbtree_check))
  1391. f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi));
  1392. blk_start_plug(&plug);
  1393. list_for_each_entry_safe(dc, tmp, pend_list, list) {
  1394. f2fs_bug_on(sbi, dc->state != D_PREP);
  1395. if (dpolicy->timeout &&
  1396. f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
  1397. break;
  1398. if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
  1399. !is_idle(sbi, DISCARD_TIME)) {
  1400. io_interrupted = true;
  1401. break;
  1402. }
  1403. __submit_discard_cmd(sbi, dpolicy, dc, &issued);
  1404. if (issued >= dpolicy->max_requests)
  1405. break;
  1406. }
  1407. blk_finish_plug(&plug);
  1408. next:
  1409. mutex_unlock(&dcc->cmd_lock);
  1410. if (issued >= dpolicy->max_requests || io_interrupted)
  1411. break;
  1412. }
  1413. if (dpolicy->type == DPOLICY_UMOUNT && issued) {
  1414. __wait_all_discard_cmd(sbi, dpolicy);
  1415. goto retry;
  1416. }
  1417. if (!issued && io_interrupted)
  1418. issued = -1;
  1419. return issued;
  1420. }
  1421. static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
  1422. {
  1423. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1424. struct list_head *pend_list;
  1425. struct discard_cmd *dc, *tmp;
  1426. int i;
  1427. bool dropped = false;
  1428. mutex_lock(&dcc->cmd_lock);
  1429. for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  1430. pend_list = &dcc->pend_list[i];
  1431. list_for_each_entry_safe(dc, tmp, pend_list, list) {
  1432. f2fs_bug_on(sbi, dc->state != D_PREP);
  1433. __remove_discard_cmd(sbi, dc);
  1434. dropped = true;
  1435. }
  1436. }
  1437. mutex_unlock(&dcc->cmd_lock);
  1438. return dropped;
  1439. }
  1440. void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
  1441. {
  1442. __drop_discard_cmd(sbi);
  1443. }
  1444. static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
  1445. struct discard_cmd *dc)
  1446. {
  1447. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1448. unsigned int len = 0;
  1449. wait_for_completion_io(&dc->wait);
  1450. mutex_lock(&dcc->cmd_lock);
  1451. f2fs_bug_on(sbi, dc->state != D_DONE);
  1452. dc->ref--;
  1453. if (!dc->ref) {
  1454. if (!dc->error)
  1455. len = dc->di.len;
  1456. __remove_discard_cmd(sbi, dc);
  1457. }
  1458. mutex_unlock(&dcc->cmd_lock);
  1459. return len;
  1460. }
  1461. static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
  1462. struct discard_policy *dpolicy,
  1463. block_t start, block_t end)
  1464. {
  1465. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1466. struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
  1467. &(dcc->fstrim_list) : &(dcc->wait_list);
  1468. struct discard_cmd *dc = NULL, *iter, *tmp;
  1469. unsigned int trimmed = 0;
  1470. next:
  1471. dc = NULL;
  1472. mutex_lock(&dcc->cmd_lock);
  1473. list_for_each_entry_safe(iter, tmp, wait_list, list) {
  1474. if (iter->di.lstart + iter->di.len <= start ||
  1475. end <= iter->di.lstart)
  1476. continue;
  1477. if (iter->di.len < dpolicy->granularity)
  1478. continue;
  1479. if (iter->state == D_DONE && !iter->ref) {
  1480. wait_for_completion_io(&iter->wait);
  1481. if (!iter->error)
  1482. trimmed += iter->di.len;
  1483. __remove_discard_cmd(sbi, iter);
  1484. } else {
  1485. iter->ref++;
  1486. dc = iter;
  1487. break;
  1488. }
  1489. }
  1490. mutex_unlock(&dcc->cmd_lock);
  1491. if (dc) {
  1492. trimmed += __wait_one_discard_bio(sbi, dc);
  1493. goto next;
  1494. }
  1495. return trimmed;
  1496. }
  1497. static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
  1498. struct discard_policy *dpolicy)
  1499. {
  1500. struct discard_policy dp;
  1501. unsigned int discard_blks;
  1502. if (dpolicy)
  1503. return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
  1504. /* wait all */
  1505. __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, MIN_DISCARD_GRANULARITY);
  1506. discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
  1507. __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, MIN_DISCARD_GRANULARITY);
  1508. discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
  1509. return discard_blks;
  1510. }
  1511. /* This should be covered by global mutex, &sit_i->sentry_lock */
  1512. static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
  1513. {
  1514. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1515. struct discard_cmd *dc;
  1516. bool need_wait = false;
  1517. mutex_lock(&dcc->cmd_lock);
  1518. dc = __lookup_discard_cmd(sbi, blkaddr);
  1519. #ifdef CONFIG_BLK_DEV_ZONED
  1520. if (dc && f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(dc->bdev)) {
  1521. int devi = f2fs_bdev_index(sbi, dc->bdev);
  1522. if (devi < 0) {
  1523. mutex_unlock(&dcc->cmd_lock);
  1524. return;
  1525. }
  1526. if (f2fs_blkz_is_seq(sbi, devi, dc->di.start)) {
  1527. /* force submit zone reset */
  1528. if (dc->state == D_PREP)
  1529. __submit_zone_reset_cmd(sbi, dc, REQ_SYNC,
  1530. &dcc->wait_list, NULL);
  1531. dc->ref++;
  1532. mutex_unlock(&dcc->cmd_lock);
  1533. /* wait zone reset */
  1534. __wait_one_discard_bio(sbi, dc);
  1535. return;
  1536. }
  1537. }
  1538. #endif
  1539. if (dc) {
  1540. if (dc->state == D_PREP) {
  1541. __punch_discard_cmd(sbi, dc, blkaddr);
  1542. } else {
  1543. dc->ref++;
  1544. need_wait = true;
  1545. }
  1546. }
  1547. mutex_unlock(&dcc->cmd_lock);
  1548. if (need_wait)
  1549. __wait_one_discard_bio(sbi, dc);
  1550. }
  1551. void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
  1552. {
  1553. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1554. if (dcc && dcc->f2fs_issue_discard) {
  1555. struct task_struct *discard_thread = dcc->f2fs_issue_discard;
  1556. dcc->f2fs_issue_discard = NULL;
  1557. kthread_stop(discard_thread);
  1558. }
  1559. }
  1560. /**
  1561. * f2fs_issue_discard_timeout() - Issue all discard cmd within UMOUNT_DISCARD_TIMEOUT
  1562. * @sbi: the f2fs_sb_info data for discard cmd to issue
  1563. *
  1564. * When UMOUNT_DISCARD_TIMEOUT is exceeded, all remaining discard commands will be dropped
  1565. *
  1566. * Return true if issued all discard cmd or no discard cmd need issue, otherwise return false.
  1567. */
  1568. bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi)
  1569. {
  1570. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1571. struct discard_policy dpolicy;
  1572. bool dropped;
  1573. if (!atomic_read(&dcc->discard_cmd_cnt))
  1574. return true;
  1575. __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
  1576. dcc->discard_granularity);
  1577. __issue_discard_cmd(sbi, &dpolicy);
  1578. dropped = __drop_discard_cmd(sbi);
  1579. /* just to make sure there is no pending discard commands */
  1580. __wait_all_discard_cmd(sbi, NULL);
  1581. f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
  1582. return !dropped;
  1583. }
  1584. static int issue_discard_thread(void *data)
  1585. {
  1586. struct f2fs_sb_info *sbi = data;
  1587. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1588. wait_queue_head_t *q = &dcc->discard_wait_queue;
  1589. struct discard_policy dpolicy;
  1590. unsigned int wait_ms = dcc->min_discard_issue_time;
  1591. int issued;
  1592. set_freezable();
  1593. do {
  1594. wait_event_freezable_timeout(*q,
  1595. kthread_should_stop() || dcc->discard_wake,
  1596. msecs_to_jiffies(wait_ms));
  1597. if (sbi->gc_mode == GC_URGENT_HIGH ||
  1598. !f2fs_available_free_memory(sbi, DISCARD_CACHE))
  1599. __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE,
  1600. MIN_DISCARD_GRANULARITY);
  1601. else
  1602. __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
  1603. dcc->discard_granularity);
  1604. if (dcc->discard_wake)
  1605. dcc->discard_wake = false;
  1606. /* clean up pending candidates before going to sleep */
  1607. if (atomic_read(&dcc->queued_discard))
  1608. __wait_all_discard_cmd(sbi, NULL);
  1609. if (f2fs_readonly(sbi->sb))
  1610. continue;
  1611. if (kthread_should_stop())
  1612. return 0;
  1613. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK) ||
  1614. !atomic_read(&dcc->discard_cmd_cnt)) {
  1615. wait_ms = dpolicy.max_interval;
  1616. continue;
  1617. }
  1618. sb_start_intwrite(sbi->sb);
  1619. issued = __issue_discard_cmd(sbi, &dpolicy);
  1620. if (issued > 0) {
  1621. __wait_all_discard_cmd(sbi, &dpolicy);
  1622. wait_ms = dpolicy.min_interval;
  1623. } else if (issued == -1) {
  1624. wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME);
  1625. if (!wait_ms)
  1626. wait_ms = dpolicy.mid_interval;
  1627. } else {
  1628. wait_ms = dpolicy.max_interval;
  1629. }
  1630. if (!atomic_read(&dcc->discard_cmd_cnt))
  1631. wait_ms = dpolicy.max_interval;
  1632. sb_end_intwrite(sbi->sb);
  1633. } while (!kthread_should_stop());
  1634. return 0;
  1635. }
  1636. #ifdef CONFIG_BLK_DEV_ZONED
  1637. static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
  1638. struct block_device *bdev, block_t blkstart, block_t blklen)
  1639. {
  1640. sector_t sector, nr_sects;
  1641. block_t lblkstart = blkstart;
  1642. int devi = 0;
  1643. u64 remainder = 0;
  1644. if (f2fs_is_multi_device(sbi)) {
  1645. devi = f2fs_target_device_index(sbi, blkstart);
  1646. if (blkstart < FDEV(devi).start_blk ||
  1647. blkstart > FDEV(devi).end_blk) {
  1648. f2fs_err(sbi, "Invalid block %x", blkstart);
  1649. return -EIO;
  1650. }
  1651. blkstart -= FDEV(devi).start_blk;
  1652. }
  1653. /* For sequential zones, reset the zone write pointer */
  1654. if (f2fs_blkz_is_seq(sbi, devi, blkstart)) {
  1655. sector = SECTOR_FROM_BLOCK(blkstart);
  1656. nr_sects = SECTOR_FROM_BLOCK(blklen);
  1657. div64_u64_rem(sector, bdev_zone_sectors(bdev), &remainder);
  1658. if (remainder || nr_sects != bdev_zone_sectors(bdev)) {
  1659. f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)",
  1660. devi, sbi->s_ndevs ? FDEV(devi).path : "",
  1661. blkstart, blklen);
  1662. return -EIO;
  1663. }
  1664. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) {
  1665. unsigned int nofs_flags;
  1666. int ret;
  1667. trace_f2fs_issue_reset_zone(bdev, blkstart);
  1668. nofs_flags = memalloc_nofs_save();
  1669. ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
  1670. sector, nr_sects);
  1671. memalloc_nofs_restore(nofs_flags);
  1672. return ret;
  1673. }
  1674. __queue_zone_reset_cmd(sbi, bdev, blkstart, lblkstart, blklen);
  1675. return 0;
  1676. }
  1677. /* For conventional zones, use regular discard if supported */
  1678. __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
  1679. return 0;
  1680. }
  1681. #endif
  1682. static int __issue_discard_async(struct f2fs_sb_info *sbi,
  1683. struct block_device *bdev, block_t blkstart, block_t blklen)
  1684. {
  1685. #ifdef CONFIG_BLK_DEV_ZONED
  1686. if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev))
  1687. return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
  1688. #endif
  1689. __queue_discard_cmd(sbi, bdev, blkstart, blklen);
  1690. return 0;
  1691. }
  1692. static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
  1693. block_t blkstart, block_t blklen)
  1694. {
  1695. sector_t start = blkstart, len = 0;
  1696. struct block_device *bdev;
  1697. struct seg_entry *se;
  1698. unsigned int offset;
  1699. block_t i;
  1700. int err = 0;
  1701. bdev = f2fs_target_device(sbi, blkstart, NULL);
  1702. for (i = blkstart; i < blkstart + blklen; i++, len++) {
  1703. if (i != start) {
  1704. struct block_device *bdev2 =
  1705. f2fs_target_device(sbi, i, NULL);
  1706. if (bdev2 != bdev) {
  1707. err = __issue_discard_async(sbi, bdev,
  1708. start, len);
  1709. if (err)
  1710. return err;
  1711. bdev = bdev2;
  1712. start = i;
  1713. len = 0;
  1714. }
  1715. }
  1716. se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
  1717. offset = GET_BLKOFF_FROM_SEG0(sbi, i);
  1718. if (f2fs_block_unit_discard(sbi) &&
  1719. !f2fs_test_and_set_bit(offset, se->discard_map))
  1720. sbi->discard_blks--;
  1721. }
  1722. if (len)
  1723. err = __issue_discard_async(sbi, bdev, start, len);
  1724. return err;
  1725. }
  1726. static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
  1727. bool check_only)
  1728. {
  1729. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  1730. struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
  1731. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  1732. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  1733. unsigned long *discard_map = (unsigned long *)se->discard_map;
  1734. unsigned long *dmap = SIT_I(sbi)->tmp_map;
  1735. unsigned int start = 0, end = -1;
  1736. bool force = (cpc->reason & CP_DISCARD);
  1737. struct discard_entry *de = NULL;
  1738. struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
  1739. int i;
  1740. if (se->valid_blocks == BLKS_PER_SEG(sbi) ||
  1741. !f2fs_hw_support_discard(sbi) ||
  1742. !f2fs_block_unit_discard(sbi))
  1743. return false;
  1744. if (!force) {
  1745. if (!f2fs_realtime_discard_enable(sbi) ||
  1746. (!se->valid_blocks &&
  1747. !is_curseg(sbi, cpc->trim_start)) ||
  1748. SM_I(sbi)->dcc_info->nr_discards >=
  1749. SM_I(sbi)->dcc_info->max_discards)
  1750. return false;
  1751. }
  1752. /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
  1753. for (i = 0; i < entries; i++)
  1754. dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
  1755. (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
  1756. while (force || SM_I(sbi)->dcc_info->nr_discards <=
  1757. SM_I(sbi)->dcc_info->max_discards) {
  1758. start = __find_rev_next_bit(dmap, BLKS_PER_SEG(sbi), end + 1);
  1759. if (start >= BLKS_PER_SEG(sbi))
  1760. break;
  1761. end = __find_rev_next_zero_bit(dmap,
  1762. BLKS_PER_SEG(sbi), start + 1);
  1763. if (force && start && end != BLKS_PER_SEG(sbi) &&
  1764. (end - start) < cpc->trim_minlen)
  1765. continue;
  1766. if (check_only)
  1767. return true;
  1768. if (!de) {
  1769. de = f2fs_kmem_cache_alloc(discard_entry_slab,
  1770. GFP_F2FS_ZERO, true, NULL);
  1771. de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
  1772. list_add_tail(&de->list, head);
  1773. }
  1774. for (i = start; i < end; i++)
  1775. __set_bit_le(i, (void *)de->discard_map);
  1776. SM_I(sbi)->dcc_info->nr_discards += end - start;
  1777. }
  1778. return false;
  1779. }
  1780. static void release_discard_addr(struct discard_entry *entry)
  1781. {
  1782. list_del(&entry->list);
  1783. kmem_cache_free(discard_entry_slab, entry);
  1784. }
  1785. void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
  1786. {
  1787. struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
  1788. struct discard_entry *entry, *this;
  1789. /* drop caches */
  1790. list_for_each_entry_safe(entry, this, head, list)
  1791. release_discard_addr(entry);
  1792. }
  1793. /*
  1794. * Should call f2fs_clear_prefree_segments after checkpoint is done.
  1795. */
  1796. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  1797. {
  1798. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1799. unsigned int segno;
  1800. mutex_lock(&dirty_i->seglist_lock);
  1801. for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
  1802. __set_test_and_free(sbi, segno, false);
  1803. mutex_unlock(&dirty_i->seglist_lock);
  1804. }
  1805. void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
  1806. struct cp_control *cpc)
  1807. {
  1808. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1809. struct list_head *head = &dcc->entry_list;
  1810. struct discard_entry *entry, *this;
  1811. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1812. unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
  1813. unsigned int start = 0, end = -1;
  1814. unsigned int secno, start_segno;
  1815. bool force = (cpc->reason & CP_DISCARD);
  1816. bool section_alignment = F2FS_OPTION(sbi).discard_unit ==
  1817. DISCARD_UNIT_SECTION;
  1818. if (f2fs_lfs_mode(sbi) && __is_large_section(sbi))
  1819. section_alignment = true;
  1820. mutex_lock(&dirty_i->seglist_lock);
  1821. while (1) {
  1822. int i;
  1823. if (section_alignment && end != -1)
  1824. end--;
  1825. start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
  1826. if (start >= MAIN_SEGS(sbi))
  1827. break;
  1828. end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
  1829. start + 1);
  1830. if (section_alignment) {
  1831. start = rounddown(start, SEGS_PER_SEC(sbi));
  1832. end = roundup(end, SEGS_PER_SEC(sbi));
  1833. }
  1834. for (i = start; i < end; i++) {
  1835. if (test_and_clear_bit(i, prefree_map))
  1836. dirty_i->nr_dirty[PRE]--;
  1837. }
  1838. if (!f2fs_realtime_discard_enable(sbi))
  1839. continue;
  1840. if (force && start >= cpc->trim_start &&
  1841. (end - 1) <= cpc->trim_end)
  1842. continue;
  1843. /* Should cover 2MB zoned device for zone-based reset */
  1844. if (!f2fs_sb_has_blkzoned(sbi) &&
  1845. (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi))) {
  1846. f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
  1847. SEGS_TO_BLKS(sbi, end - start));
  1848. continue;
  1849. }
  1850. next:
  1851. secno = GET_SEC_FROM_SEG(sbi, start);
  1852. start_segno = GET_SEG_FROM_SEC(sbi, secno);
  1853. if (!is_cursec(sbi, secno) &&
  1854. !get_valid_blocks(sbi, start, true))
  1855. f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
  1856. BLKS_PER_SEC(sbi));
  1857. start = start_segno + SEGS_PER_SEC(sbi);
  1858. if (start < end)
  1859. goto next;
  1860. else
  1861. end = start - 1;
  1862. }
  1863. mutex_unlock(&dirty_i->seglist_lock);
  1864. if (!f2fs_block_unit_discard(sbi))
  1865. goto wakeup;
  1866. /* send small discards */
  1867. list_for_each_entry_safe(entry, this, head, list) {
  1868. unsigned int cur_pos = 0, next_pos, len, total_len = 0;
  1869. bool is_valid = test_bit_le(0, entry->discard_map);
  1870. find_next:
  1871. if (is_valid) {
  1872. next_pos = find_next_zero_bit_le(entry->discard_map,
  1873. BLKS_PER_SEG(sbi), cur_pos);
  1874. len = next_pos - cur_pos;
  1875. if (f2fs_sb_has_blkzoned(sbi) ||
  1876. (force && len < cpc->trim_minlen))
  1877. goto skip;
  1878. f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
  1879. len);
  1880. total_len += len;
  1881. } else {
  1882. next_pos = find_next_bit_le(entry->discard_map,
  1883. BLKS_PER_SEG(sbi), cur_pos);
  1884. }
  1885. skip:
  1886. cur_pos = next_pos;
  1887. is_valid = !is_valid;
  1888. if (cur_pos < BLKS_PER_SEG(sbi))
  1889. goto find_next;
  1890. release_discard_addr(entry);
  1891. dcc->nr_discards -= total_len;
  1892. }
  1893. wakeup:
  1894. wake_up_discard_thread(sbi, false);
  1895. }
  1896. int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)
  1897. {
  1898. dev_t dev = sbi->sb->s_bdev->bd_dev;
  1899. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1900. int err = 0;
  1901. if (f2fs_sb_has_readonly(sbi)) {
  1902. f2fs_info(sbi,
  1903. "Skip to start discard thread for readonly image");
  1904. return 0;
  1905. }
  1906. if (!f2fs_realtime_discard_enable(sbi))
  1907. return 0;
  1908. dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
  1909. "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
  1910. if (IS_ERR(dcc->f2fs_issue_discard)) {
  1911. err = PTR_ERR(dcc->f2fs_issue_discard);
  1912. dcc->f2fs_issue_discard = NULL;
  1913. }
  1914. return err;
  1915. }
  1916. static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
  1917. {
  1918. struct discard_cmd_control *dcc;
  1919. int err = 0, i;
  1920. if (SM_I(sbi)->dcc_info) {
  1921. dcc = SM_I(sbi)->dcc_info;
  1922. goto init_thread;
  1923. }
  1924. dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
  1925. if (!dcc)
  1926. return -ENOMEM;
  1927. dcc->discard_io_aware_gran = MAX_PLIST_NUM;
  1928. dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
  1929. dcc->max_ordered_discard = DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY;
  1930. dcc->discard_io_aware = DPOLICY_IO_AWARE_ENABLE;
  1931. if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT ||
  1932. F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
  1933. dcc->discard_granularity = BLKS_PER_SEG(sbi);
  1934. INIT_LIST_HEAD(&dcc->entry_list);
  1935. for (i = 0; i < MAX_PLIST_NUM; i++)
  1936. INIT_LIST_HEAD(&dcc->pend_list[i]);
  1937. INIT_LIST_HEAD(&dcc->wait_list);
  1938. INIT_LIST_HEAD(&dcc->fstrim_list);
  1939. mutex_init(&dcc->cmd_lock);
  1940. atomic_set(&dcc->issued_discard, 0);
  1941. atomic_set(&dcc->queued_discard, 0);
  1942. atomic_set(&dcc->discard_cmd_cnt, 0);
  1943. dcc->nr_discards = 0;
  1944. dcc->max_discards = SEGS_TO_BLKS(sbi, MAIN_SEGS(sbi));
  1945. dcc->max_discard_request = DEF_MAX_DISCARD_REQUEST;
  1946. dcc->min_discard_issue_time = DEF_MIN_DISCARD_ISSUE_TIME;
  1947. dcc->mid_discard_issue_time = DEF_MID_DISCARD_ISSUE_TIME;
  1948. dcc->max_discard_issue_time = DEF_MAX_DISCARD_ISSUE_TIME;
  1949. dcc->discard_urgent_util = DEF_DISCARD_URGENT_UTIL;
  1950. dcc->undiscard_blks = 0;
  1951. dcc->next_pos = 0;
  1952. dcc->root = RB_ROOT_CACHED;
  1953. dcc->rbtree_check = false;
  1954. init_waitqueue_head(&dcc->discard_wait_queue);
  1955. SM_I(sbi)->dcc_info = dcc;
  1956. init_thread:
  1957. err = f2fs_start_discard_thread(sbi);
  1958. if (err) {
  1959. kfree(dcc);
  1960. SM_I(sbi)->dcc_info = NULL;
  1961. }
  1962. return err;
  1963. }
  1964. static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
  1965. {
  1966. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1967. if (!dcc)
  1968. return;
  1969. f2fs_stop_discard_thread(sbi);
  1970. /*
  1971. * Recovery can cache discard commands, so in error path of
  1972. * fill_super(), it needs to give a chance to handle them.
  1973. */
  1974. f2fs_issue_discard_timeout(sbi);
  1975. kfree(dcc);
  1976. SM_I(sbi)->dcc_info = NULL;
  1977. }
  1978. static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  1979. {
  1980. struct sit_info *sit_i = SIT_I(sbi);
  1981. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
  1982. sit_i->dirty_sentries++;
  1983. return false;
  1984. }
  1985. return true;
  1986. }
  1987. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  1988. unsigned int segno, int modified)
  1989. {
  1990. struct seg_entry *se = get_seg_entry(sbi, segno);
  1991. se->type = type;
  1992. if (modified)
  1993. __mark_sit_entry_dirty(sbi, segno);
  1994. }
  1995. static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi,
  1996. block_t blkaddr)
  1997. {
  1998. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  1999. if (segno == NULL_SEGNO)
  2000. return 0;
  2001. return get_seg_entry(sbi, segno)->mtime;
  2002. }
  2003. static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
  2004. unsigned long long old_mtime)
  2005. {
  2006. struct seg_entry *se;
  2007. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  2008. unsigned long long ctime = get_mtime(sbi, false);
  2009. unsigned long long mtime = old_mtime ? old_mtime : ctime;
  2010. if (segno == NULL_SEGNO)
  2011. return;
  2012. se = get_seg_entry(sbi, segno);
  2013. if (!se->mtime)
  2014. se->mtime = mtime;
  2015. else
  2016. se->mtime = div_u64(se->mtime * se->valid_blocks + mtime,
  2017. se->valid_blocks + 1);
  2018. if (ctime > SIT_I(sbi)->max_mtime)
  2019. SIT_I(sbi)->max_mtime = ctime;
  2020. }
  2021. /*
  2022. * NOTE: when updating multiple blocks at the same time, please ensure
  2023. * that the consecutive input blocks belong to the same segment.
  2024. */
  2025. static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_entry *se,
  2026. unsigned int segno, block_t blkaddr, unsigned int offset, int del)
  2027. {
  2028. bool exist;
  2029. #ifdef CONFIG_F2FS_CHECK_FS
  2030. bool mir_exist;
  2031. #endif
  2032. int i;
  2033. int del_count = -del;
  2034. f2fs_bug_on(sbi, GET_SEGNO(sbi, blkaddr) != GET_SEGNO(sbi, blkaddr + del_count - 1));
  2035. for (i = 0; i < del_count; i++) {
  2036. exist = f2fs_test_and_clear_bit(offset + i, se->cur_valid_map);
  2037. #ifdef CONFIG_F2FS_CHECK_FS
  2038. mir_exist = f2fs_test_and_clear_bit(offset + i,
  2039. se->cur_valid_map_mir);
  2040. if (unlikely(exist != mir_exist)) {
  2041. f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d",
  2042. blkaddr + i, exist);
  2043. f2fs_bug_on(sbi, 1);
  2044. }
  2045. #endif
  2046. if (unlikely(!exist)) {
  2047. f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u", blkaddr + i);
  2048. f2fs_bug_on(sbi, 1);
  2049. se->valid_blocks++;
  2050. del += 1;
  2051. } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
  2052. /*
  2053. * If checkpoints are off, we must not reuse data that
  2054. * was used in the previous checkpoint. If it was used
  2055. * before, we must track that to know how much space we
  2056. * really have.
  2057. */
  2058. if (f2fs_test_bit(offset + i, se->ckpt_valid_map)) {
  2059. spin_lock(&sbi->stat_lock);
  2060. sbi->unusable_block_count++;
  2061. spin_unlock(&sbi->stat_lock);
  2062. }
  2063. }
  2064. if (f2fs_block_unit_discard(sbi) &&
  2065. f2fs_test_and_clear_bit(offset + i, se->discard_map))
  2066. sbi->discard_blks++;
  2067. if (!f2fs_test_bit(offset + i, se->ckpt_valid_map)) {
  2068. se->ckpt_valid_blocks -= 1;
  2069. if (__is_large_section(sbi))
  2070. get_sec_entry(sbi, segno)->ckpt_valid_blocks -= 1;
  2071. }
  2072. }
  2073. if (__is_large_section(sbi))
  2074. sanity_check_valid_blocks(sbi, segno);
  2075. return del;
  2076. }
  2077. static int update_sit_entry_for_alloc(struct f2fs_sb_info *sbi, struct seg_entry *se,
  2078. unsigned int segno, block_t blkaddr, unsigned int offset, int del)
  2079. {
  2080. bool exist;
  2081. #ifdef CONFIG_F2FS_CHECK_FS
  2082. bool mir_exist;
  2083. #endif
  2084. exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
  2085. #ifdef CONFIG_F2FS_CHECK_FS
  2086. mir_exist = f2fs_test_and_set_bit(offset,
  2087. se->cur_valid_map_mir);
  2088. if (unlikely(exist != mir_exist)) {
  2089. f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d",
  2090. blkaddr, exist);
  2091. f2fs_bug_on(sbi, 1);
  2092. }
  2093. #endif
  2094. if (unlikely(exist)) {
  2095. f2fs_err(sbi, "Bitmap was wrongly set, blk:%u", blkaddr);
  2096. f2fs_bug_on(sbi, 1);
  2097. se->valid_blocks--;
  2098. del = 0;
  2099. }
  2100. if (f2fs_block_unit_discard(sbi) &&
  2101. !f2fs_test_and_set_bit(offset, se->discard_map))
  2102. sbi->discard_blks--;
  2103. /*
  2104. * SSR should never reuse block which is checkpointed
  2105. * or newly invalidated.
  2106. */
  2107. if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
  2108. if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map)) {
  2109. se->ckpt_valid_blocks++;
  2110. if (__is_large_section(sbi))
  2111. get_sec_entry(sbi, segno)->ckpt_valid_blocks++;
  2112. }
  2113. }
  2114. if (!f2fs_test_bit(offset, se->ckpt_valid_map)) {
  2115. se->ckpt_valid_blocks += del;
  2116. if (__is_large_section(sbi))
  2117. get_sec_entry(sbi, segno)->ckpt_valid_blocks += del;
  2118. }
  2119. if (__is_large_section(sbi))
  2120. sanity_check_valid_blocks(sbi, segno);
  2121. return del;
  2122. }
  2123. /*
  2124. * If releasing blocks, this function supports updating multiple consecutive blocks
  2125. * at one time, but please note that these consecutive blocks need to belong to the
  2126. * same segment.
  2127. */
  2128. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  2129. {
  2130. struct seg_entry *se;
  2131. unsigned int segno, offset;
  2132. long int new_vblocks;
  2133. segno = GET_SEGNO(sbi, blkaddr);
  2134. if (segno == NULL_SEGNO)
  2135. return;
  2136. se = get_seg_entry(sbi, segno);
  2137. new_vblocks = se->valid_blocks + del;
  2138. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  2139. f2fs_bug_on(sbi, (new_vblocks < 0 ||
  2140. (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno))));
  2141. se->valid_blocks = new_vblocks;
  2142. /* Update valid block bitmap */
  2143. if (del > 0) {
  2144. del = update_sit_entry_for_alloc(sbi, se, segno, blkaddr, offset, del);
  2145. } else {
  2146. del = update_sit_entry_for_release(sbi, se, segno, blkaddr, offset, del);
  2147. }
  2148. __mark_sit_entry_dirty(sbi, segno);
  2149. /* update total number of valid blocks to be written in ckpt area */
  2150. SIT_I(sbi)->written_valid_blocks += del;
  2151. if (__is_large_section(sbi))
  2152. get_sec_entry(sbi, segno)->valid_blocks += del;
  2153. }
  2154. void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
  2155. unsigned int len)
  2156. {
  2157. unsigned int segno = GET_SEGNO(sbi, addr);
  2158. struct sit_info *sit_i = SIT_I(sbi);
  2159. block_t addr_start = addr, addr_end = addr + len - 1;
  2160. unsigned int seg_num = GET_SEGNO(sbi, addr_end) - segno + 1;
  2161. unsigned int i = 1, max_blocks = sbi->blocks_per_seg, cnt;
  2162. f2fs_bug_on(sbi, addr == NULL_ADDR);
  2163. if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
  2164. return;
  2165. f2fs_invalidate_internal_cache(sbi, addr, len);
  2166. /* add it into sit main buffer */
  2167. down_write(&sit_i->sentry_lock);
  2168. if (seg_num == 1)
  2169. cnt = len;
  2170. else
  2171. cnt = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
  2172. do {
  2173. update_segment_mtime(sbi, addr_start, 0);
  2174. update_sit_entry(sbi, addr_start, -cnt);
  2175. /* add it into dirty seglist */
  2176. locate_dirty_segment(sbi, segno);
  2177. /* update @addr_start and @cnt and @segno */
  2178. addr_start = START_BLOCK(sbi, ++segno);
  2179. if (++i == seg_num)
  2180. cnt = GET_BLKOFF_FROM_SEG0(sbi, addr_end) + 1;
  2181. else
  2182. cnt = max_blocks;
  2183. } while (i <= seg_num);
  2184. up_write(&sit_i->sentry_lock);
  2185. }
  2186. bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
  2187. {
  2188. struct sit_info *sit_i = SIT_I(sbi);
  2189. unsigned int segno, offset;
  2190. struct seg_entry *se;
  2191. bool is_cp = false;
  2192. if (!__is_valid_data_blkaddr(blkaddr))
  2193. return true;
  2194. down_read(&sit_i->sentry_lock);
  2195. segno = GET_SEGNO(sbi, blkaddr);
  2196. se = get_seg_entry(sbi, segno);
  2197. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  2198. if (f2fs_test_bit(offset, se->ckpt_valid_map))
  2199. is_cp = true;
  2200. up_read(&sit_i->sentry_lock);
  2201. return is_cp;
  2202. }
  2203. static unsigned short f2fs_curseg_valid_blocks(struct f2fs_sb_info *sbi, int type)
  2204. {
  2205. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2206. if (sbi->ckpt->alloc_type[type] == SSR)
  2207. return BLKS_PER_SEG(sbi);
  2208. return curseg->next_blkoff;
  2209. }
  2210. /*
  2211. * Calculate the number of current summary pages for writing
  2212. */
  2213. int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
  2214. {
  2215. int valid_sum_count = 0;
  2216. int i, sum_in_page;
  2217. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  2218. if (sbi->ckpt->alloc_type[i] != SSR && for_ra)
  2219. valid_sum_count +=
  2220. le16_to_cpu(F2FS_CKPT(sbi)->cur_data_blkoff[i]);
  2221. else
  2222. valid_sum_count += f2fs_curseg_valid_blocks(sbi, i);
  2223. }
  2224. sum_in_page = (sbi->blocksize - 2 * sbi->sum_journal_size -
  2225. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  2226. if (valid_sum_count <= sum_in_page)
  2227. return 1;
  2228. else if ((valid_sum_count - sum_in_page) <=
  2229. (sbi->blocksize - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  2230. return 2;
  2231. return 3;
  2232. }
  2233. /*
  2234. * Caller should put this summary folio
  2235. */
  2236. struct folio *f2fs_get_sum_folio(struct f2fs_sb_info *sbi, unsigned int segno)
  2237. {
  2238. if (unlikely(f2fs_cp_error(sbi)))
  2239. return ERR_PTR(-EIO);
  2240. return f2fs_get_meta_folio_retry(sbi, GET_SUM_BLOCK(sbi, segno));
  2241. }
  2242. void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
  2243. void *src, block_t blk_addr)
  2244. {
  2245. struct folio *folio;
  2246. if (!f2fs_sb_has_packed_ssa(sbi))
  2247. folio = f2fs_grab_meta_folio(sbi, blk_addr);
  2248. else
  2249. folio = f2fs_get_meta_folio_retry(sbi, blk_addr);
  2250. if (IS_ERR(folio))
  2251. return;
  2252. memcpy(folio_address(folio), src, PAGE_SIZE);
  2253. folio_mark_dirty(folio);
  2254. f2fs_folio_put(folio, true);
  2255. }
  2256. static void write_sum_page(struct f2fs_sb_info *sbi,
  2257. struct f2fs_summary_block *sum_blk, unsigned int segno)
  2258. {
  2259. struct folio *folio;
  2260. if (!f2fs_sb_has_packed_ssa(sbi))
  2261. return f2fs_update_meta_page(sbi, (void *)sum_blk,
  2262. GET_SUM_BLOCK(sbi, segno));
  2263. folio = f2fs_get_sum_folio(sbi, segno);
  2264. if (IS_ERR(folio))
  2265. return;
  2266. memcpy(SUM_BLK_PAGE_ADDR(sbi, folio, segno), sum_blk,
  2267. sbi->sum_blocksize);
  2268. folio_mark_dirty(folio);
  2269. f2fs_folio_put(folio, true);
  2270. }
  2271. static void write_current_sum_page(struct f2fs_sb_info *sbi,
  2272. int type, block_t blk_addr)
  2273. {
  2274. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2275. struct folio *folio = f2fs_grab_meta_folio(sbi, blk_addr);
  2276. struct f2fs_summary_block *src = curseg->sum_blk;
  2277. struct f2fs_summary_block *dst;
  2278. dst = folio_address(folio);
  2279. memset(dst, 0, PAGE_SIZE);
  2280. mutex_lock(&curseg->curseg_mutex);
  2281. down_read(&curseg->journal_rwsem);
  2282. memcpy(sum_journal(sbi, dst), curseg->journal, sbi->sum_journal_size);
  2283. up_read(&curseg->journal_rwsem);
  2284. memcpy(sum_entries(dst), sum_entries(src), sbi->sum_entry_size);
  2285. memcpy(sum_footer(sbi, dst), sum_footer(sbi, src), SUM_FOOTER_SIZE);
  2286. mutex_unlock(&curseg->curseg_mutex);
  2287. folio_mark_dirty(folio);
  2288. f2fs_folio_put(folio, true);
  2289. }
  2290. static int is_next_segment_free(struct f2fs_sb_info *sbi,
  2291. struct curseg_info *curseg)
  2292. {
  2293. unsigned int segno = curseg->segno + 1;
  2294. struct free_segmap_info *free_i = FREE_I(sbi);
  2295. if (segno < MAIN_SEGS(sbi) && segno % SEGS_PER_SEC(sbi))
  2296. return !test_bit(segno, free_i->free_segmap);
  2297. return 0;
  2298. }
  2299. /*
  2300. * Find a new segment from the free segments bitmap to right order
  2301. * This function should be returned with success, otherwise BUG
  2302. */
  2303. static int get_new_segment(struct f2fs_sb_info *sbi,
  2304. unsigned int *newseg, bool new_sec, bool pinning)
  2305. {
  2306. struct free_segmap_info *free_i = FREE_I(sbi);
  2307. unsigned int segno, secno, zoneno;
  2308. unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
  2309. unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
  2310. unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
  2311. unsigned int alloc_policy = sbi->allocate_section_policy;
  2312. unsigned int alloc_hint = sbi->allocate_section_hint;
  2313. bool init = true;
  2314. int i;
  2315. int ret = 0;
  2316. spin_lock(&free_i->segmap_lock);
  2317. if (time_to_inject(sbi, FAULT_NO_SEGMENT)) {
  2318. ret = -ENOSPC;
  2319. goto out_unlock;
  2320. }
  2321. if (!new_sec && ((*newseg + 1) % SEGS_PER_SEC(sbi))) {
  2322. segno = find_next_zero_bit(free_i->free_segmap,
  2323. GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
  2324. if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
  2325. goto got_it;
  2326. }
  2327. #ifdef CONFIG_BLK_DEV_ZONED
  2328. /*
  2329. * If we format f2fs on zoned storage, let's try to get pinned sections
  2330. * from beginning of the storage, which should be a conventional one.
  2331. */
  2332. if (f2fs_sb_has_blkzoned(sbi)) {
  2333. /* Prioritize writing to conventional zones */
  2334. if (sbi->blkzone_alloc_policy == BLKZONE_ALLOC_PRIOR_CONV || pinning)
  2335. segno = 0;
  2336. else
  2337. segno = max(sbi->first_seq_zone_segno, *newseg);
  2338. hint = GET_SEC_FROM_SEG(sbi, segno);
  2339. }
  2340. #endif
  2341. /*
  2342. * Prevent allocate_section_hint from exceeding MAIN_SECS()
  2343. * due to desynchronization.
  2344. */
  2345. if (alloc_policy != ALLOCATE_FORWARD_NOHINT &&
  2346. alloc_hint > MAIN_SECS(sbi))
  2347. alloc_hint = MAIN_SECS(sbi);
  2348. if (alloc_policy == ALLOCATE_FORWARD_FROM_HINT &&
  2349. hint < alloc_hint)
  2350. hint = alloc_hint;
  2351. else if (alloc_policy == ALLOCATE_FORWARD_WITHIN_HINT &&
  2352. hint >= alloc_hint)
  2353. hint = 0;
  2354. find_other_zone:
  2355. secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
  2356. #ifdef CONFIG_BLK_DEV_ZONED
  2357. if (secno >= MAIN_SECS(sbi) && f2fs_sb_has_blkzoned(sbi)) {
  2358. /* Write only to sequential zones */
  2359. if (sbi->blkzone_alloc_policy == BLKZONE_ALLOC_ONLY_SEQ) {
  2360. hint = GET_SEC_FROM_SEG(sbi, sbi->first_seq_zone_segno);
  2361. secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
  2362. } else
  2363. secno = find_first_zero_bit(free_i->free_secmap,
  2364. MAIN_SECS(sbi));
  2365. if (secno >= MAIN_SECS(sbi)) {
  2366. ret = -ENOSPC;
  2367. f2fs_bug_on(sbi, 1);
  2368. goto out_unlock;
  2369. }
  2370. }
  2371. #endif
  2372. if (secno >= MAIN_SECS(sbi)) {
  2373. secno = find_first_zero_bit(free_i->free_secmap,
  2374. MAIN_SECS(sbi));
  2375. if (secno >= MAIN_SECS(sbi)) {
  2376. ret = -ENOSPC;
  2377. f2fs_bug_on(sbi, !pinning);
  2378. goto out_unlock;
  2379. }
  2380. }
  2381. segno = GET_SEG_FROM_SEC(sbi, secno);
  2382. zoneno = GET_ZONE_FROM_SEC(sbi, secno);
  2383. /* give up on finding another zone */
  2384. if (!init)
  2385. goto got_it;
  2386. if (sbi->secs_per_zone == 1)
  2387. goto got_it;
  2388. if (zoneno == old_zoneno)
  2389. goto got_it;
  2390. for (i = 0; i < NR_CURSEG_TYPE; i++)
  2391. if (CURSEG_I(sbi, i)->zone == zoneno)
  2392. break;
  2393. if (i < NR_CURSEG_TYPE) {
  2394. /* zone is in user, try another */
  2395. if (zoneno + 1 >= total_zones)
  2396. hint = 0;
  2397. else
  2398. hint = (zoneno + 1) * sbi->secs_per_zone;
  2399. init = false;
  2400. goto find_other_zone;
  2401. }
  2402. got_it:
  2403. /* set it as dirty segment in free segmap */
  2404. if (test_bit(segno, free_i->free_segmap)) {
  2405. ret = -EFSCORRUPTED;
  2406. f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_CORRUPTED_FREE_BITMAP);
  2407. goto out_unlock;
  2408. }
  2409. /* no free section in conventional device or conventional zone */
  2410. if (new_sec && pinning &&
  2411. f2fs_is_sequential_zone_area(sbi, START_BLOCK(sbi, segno))) {
  2412. ret = -EAGAIN;
  2413. goto out_unlock;
  2414. }
  2415. __set_inuse(sbi, segno);
  2416. *newseg = segno;
  2417. out_unlock:
  2418. spin_unlock(&free_i->segmap_lock);
  2419. if (ret == -ENOSPC && !pinning)
  2420. f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_NO_SEGMENT);
  2421. return ret;
  2422. }
  2423. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  2424. {
  2425. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2426. struct summary_footer *sum_footer;
  2427. unsigned short seg_type = curseg->seg_type;
  2428. /* only happen when get_new_segment() fails */
  2429. if (curseg->next_segno == NULL_SEGNO)
  2430. return;
  2431. curseg->inited = true;
  2432. curseg->segno = curseg->next_segno;
  2433. curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
  2434. curseg->next_blkoff = 0;
  2435. curseg->next_segno = NULL_SEGNO;
  2436. sum_footer = sum_footer(sbi, curseg->sum_blk);
  2437. memset(sum_footer, 0, sizeof(struct summary_footer));
  2438. sanity_check_seg_type(sbi, seg_type);
  2439. if (IS_DATASEG(seg_type))
  2440. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  2441. if (IS_NODESEG(seg_type))
  2442. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  2443. __set_sit_entry_type(sbi, seg_type, curseg->segno, modified);
  2444. }
  2445. static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
  2446. {
  2447. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2448. unsigned short seg_type = curseg->seg_type;
  2449. sanity_check_seg_type(sbi, seg_type);
  2450. if (__is_large_section(sbi)) {
  2451. if (f2fs_need_rand_seg(sbi)) {
  2452. unsigned int hint = GET_SEC_FROM_SEG(sbi, curseg->segno);
  2453. if (GET_SEC_FROM_SEG(sbi, curseg->segno + 1) != hint)
  2454. return curseg->segno;
  2455. return get_random_u32_inclusive(curseg->segno + 1,
  2456. GET_SEG_FROM_SEC(sbi, hint + 1) - 1);
  2457. }
  2458. return curseg->segno;
  2459. } else if (f2fs_need_rand_seg(sbi)) {
  2460. return get_random_u32_below(MAIN_SECS(sbi) * SEGS_PER_SEC(sbi));
  2461. }
  2462. /* inmem log may not locate on any segment after mount */
  2463. if (!curseg->inited)
  2464. return 0;
  2465. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  2466. return 0;
  2467. if (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type))
  2468. return 0;
  2469. if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
  2470. return SIT_I(sbi)->last_victim[ALLOC_NEXT];
  2471. /* find segments from 0 to reuse freed segments */
  2472. if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
  2473. return 0;
  2474. return curseg->segno;
  2475. }
  2476. static void reset_curseg_fields(struct curseg_info *curseg)
  2477. {
  2478. curseg->inited = false;
  2479. curseg->segno = NULL_SEGNO;
  2480. curseg->next_segno = 0;
  2481. }
  2482. /*
  2483. * Allocate a current working segment.
  2484. * This function always allocates a free segment in LFS manner.
  2485. */
  2486. static int new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  2487. {
  2488. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2489. unsigned int segno = curseg->segno;
  2490. bool pinning = type == CURSEG_COLD_DATA_PINNED;
  2491. int ret;
  2492. if (curseg->inited)
  2493. write_sum_page(sbi, curseg->sum_blk, segno);
  2494. segno = __get_next_segno(sbi, type);
  2495. ret = get_new_segment(sbi, &segno, new_sec, pinning);
  2496. if (ret) {
  2497. if (ret == -ENOSPC)
  2498. reset_curseg_fields(curseg);
  2499. return ret;
  2500. }
  2501. curseg->next_segno = segno;
  2502. reset_curseg(sbi, type, 1);
  2503. curseg->alloc_type = LFS;
  2504. if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
  2505. curseg->fragment_remained_chunk =
  2506. get_random_u32_inclusive(1, sbi->max_fragment_chunk);
  2507. return 0;
  2508. }
  2509. static int __next_free_blkoff(struct f2fs_sb_info *sbi,
  2510. int segno, block_t start)
  2511. {
  2512. struct seg_entry *se = get_seg_entry(sbi, segno);
  2513. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  2514. unsigned long *target_map = SIT_I(sbi)->tmp_map;
  2515. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  2516. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  2517. int i;
  2518. for (i = 0; i < entries; i++)
  2519. target_map[i] = ckpt_map[i] | cur_map[i];
  2520. return __find_rev_next_zero_bit(target_map, BLKS_PER_SEG(sbi), start);
  2521. }
  2522. static int f2fs_find_next_ssr_block(struct f2fs_sb_info *sbi,
  2523. struct curseg_info *seg)
  2524. {
  2525. return __next_free_blkoff(sbi, seg->segno, seg->next_blkoff + 1);
  2526. }
  2527. bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)
  2528. {
  2529. return __next_free_blkoff(sbi, segno, 0) < BLKS_PER_SEG(sbi);
  2530. }
  2531. /*
  2532. * This function always allocates a used segment(from dirty seglist) by SSR
  2533. * manner, so it should recover the existing segment information of valid blocks
  2534. */
  2535. static int change_curseg(struct f2fs_sb_info *sbi, int type)
  2536. {
  2537. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2538. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2539. unsigned int new_segno = curseg->next_segno;
  2540. struct f2fs_summary_block *sum_node;
  2541. struct folio *sum_folio;
  2542. if (curseg->inited)
  2543. write_sum_page(sbi, curseg->sum_blk, curseg->segno);
  2544. __set_test_and_inuse(sbi, new_segno);
  2545. mutex_lock(&dirty_i->seglist_lock);
  2546. __remove_dirty_segment(sbi, new_segno, PRE);
  2547. __remove_dirty_segment(sbi, new_segno, DIRTY);
  2548. mutex_unlock(&dirty_i->seglist_lock);
  2549. reset_curseg(sbi, type, 1);
  2550. curseg->alloc_type = SSR;
  2551. curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0);
  2552. sum_folio = f2fs_get_sum_folio(sbi, new_segno);
  2553. if (IS_ERR(sum_folio)) {
  2554. /* GC won't be able to use stale summary pages by cp_error */
  2555. memset(curseg->sum_blk, 0, sbi->sum_entry_size);
  2556. return PTR_ERR(sum_folio);
  2557. }
  2558. sum_node = SUM_BLK_PAGE_ADDR(sbi, sum_folio, new_segno);
  2559. memcpy(curseg->sum_blk, sum_node, sbi->sum_entry_size);
  2560. f2fs_folio_put(sum_folio, true);
  2561. return 0;
  2562. }
  2563. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
  2564. int alloc_mode, unsigned long long age);
  2565. static int get_atssr_segment(struct f2fs_sb_info *sbi, int type,
  2566. int target_type, int alloc_mode,
  2567. unsigned long long age)
  2568. {
  2569. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2570. int ret = 0;
  2571. curseg->seg_type = target_type;
  2572. if (get_ssr_segment(sbi, type, alloc_mode, age)) {
  2573. struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno);
  2574. curseg->seg_type = se->type;
  2575. ret = change_curseg(sbi, type);
  2576. } else {
  2577. /* allocate cold segment by default */
  2578. curseg->seg_type = CURSEG_COLD_DATA;
  2579. ret = new_curseg(sbi, type, true);
  2580. }
  2581. stat_inc_seg_type(sbi, curseg);
  2582. return ret;
  2583. }
  2584. static int __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi, bool force)
  2585. {
  2586. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC);
  2587. int ret = 0;
  2588. if (!sbi->am.atgc_enabled && !force)
  2589. return 0;
  2590. f2fs_down_read(&SM_I(sbi)->curseg_lock);
  2591. mutex_lock(&curseg->curseg_mutex);
  2592. down_write(&SIT_I(sbi)->sentry_lock);
  2593. ret = get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC,
  2594. CURSEG_COLD_DATA, SSR, 0);
  2595. up_write(&SIT_I(sbi)->sentry_lock);
  2596. mutex_unlock(&curseg->curseg_mutex);
  2597. f2fs_up_read(&SM_I(sbi)->curseg_lock);
  2598. return ret;
  2599. }
  2600. int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
  2601. {
  2602. return __f2fs_init_atgc_curseg(sbi, false);
  2603. }
  2604. int f2fs_reinit_atgc_curseg(struct f2fs_sb_info *sbi)
  2605. {
  2606. int ret;
  2607. if (!test_opt(sbi, ATGC))
  2608. return 0;
  2609. if (sbi->am.atgc_enabled)
  2610. return 0;
  2611. if (le64_to_cpu(F2FS_CKPT(sbi)->elapsed_time) <
  2612. sbi->am.age_threshold)
  2613. return 0;
  2614. ret = __f2fs_init_atgc_curseg(sbi, true);
  2615. if (!ret) {
  2616. sbi->am.atgc_enabled = true;
  2617. f2fs_info(sbi, "reenabled age threshold GC");
  2618. }
  2619. return ret;
  2620. }
  2621. static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
  2622. {
  2623. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2624. mutex_lock(&curseg->curseg_mutex);
  2625. if (!curseg->inited)
  2626. goto out;
  2627. if (get_valid_blocks(sbi, curseg->segno, false)) {
  2628. write_sum_page(sbi, curseg->sum_blk, curseg->segno);
  2629. } else {
  2630. mutex_lock(&DIRTY_I(sbi)->seglist_lock);
  2631. __set_test_and_free(sbi, curseg->segno, true);
  2632. mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
  2633. }
  2634. out:
  2635. mutex_unlock(&curseg->curseg_mutex);
  2636. }
  2637. void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi)
  2638. {
  2639. __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
  2640. if (sbi->am.atgc_enabled)
  2641. __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
  2642. }
  2643. static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type)
  2644. {
  2645. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2646. mutex_lock(&curseg->curseg_mutex);
  2647. if (!curseg->inited)
  2648. goto out;
  2649. if (get_valid_blocks(sbi, curseg->segno, false))
  2650. goto out;
  2651. mutex_lock(&DIRTY_I(sbi)->seglist_lock);
  2652. __set_test_and_inuse(sbi, curseg->segno);
  2653. mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
  2654. out:
  2655. mutex_unlock(&curseg->curseg_mutex);
  2656. }
  2657. void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi)
  2658. {
  2659. __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
  2660. if (sbi->am.atgc_enabled)
  2661. __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
  2662. }
  2663. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
  2664. int alloc_mode, unsigned long long age)
  2665. {
  2666. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2667. unsigned segno = NULL_SEGNO;
  2668. unsigned short seg_type = curseg->seg_type;
  2669. int i, cnt;
  2670. bool reversed = false;
  2671. sanity_check_seg_type(sbi, seg_type);
  2672. /* f2fs_need_SSR() already forces to do this */
  2673. if (!f2fs_get_victim(sbi, &segno, BG_GC, seg_type,
  2674. alloc_mode, age, false)) {
  2675. curseg->next_segno = segno;
  2676. return 1;
  2677. }
  2678. /* For node segments, let's do SSR more intensively */
  2679. if (IS_NODESEG(seg_type)) {
  2680. if (seg_type >= CURSEG_WARM_NODE) {
  2681. reversed = true;
  2682. i = CURSEG_COLD_NODE;
  2683. } else {
  2684. i = CURSEG_HOT_NODE;
  2685. }
  2686. cnt = NR_CURSEG_NODE_TYPE;
  2687. } else {
  2688. if (seg_type >= CURSEG_WARM_DATA) {
  2689. reversed = true;
  2690. i = CURSEG_COLD_DATA;
  2691. } else {
  2692. i = CURSEG_HOT_DATA;
  2693. }
  2694. cnt = NR_CURSEG_DATA_TYPE;
  2695. }
  2696. for (; cnt-- > 0; reversed ? i-- : i++) {
  2697. if (i == seg_type)
  2698. continue;
  2699. if (!f2fs_get_victim(sbi, &segno, BG_GC, i,
  2700. alloc_mode, age, false)) {
  2701. curseg->next_segno = segno;
  2702. return 1;
  2703. }
  2704. }
  2705. /* find valid_blocks=0 in dirty list */
  2706. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
  2707. segno = get_free_segment(sbi);
  2708. if (segno != NULL_SEGNO) {
  2709. curseg->next_segno = segno;
  2710. return 1;
  2711. }
  2712. }
  2713. return 0;
  2714. }
  2715. static bool need_new_seg(struct f2fs_sb_info *sbi, int type)
  2716. {
  2717. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2718. if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
  2719. curseg->seg_type == CURSEG_WARM_NODE)
  2720. return true;
  2721. if (curseg->alloc_type == LFS && is_next_segment_free(sbi, curseg) &&
  2722. likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  2723. return true;
  2724. if (!f2fs_need_SSR(sbi) || !get_ssr_segment(sbi, type, SSR, 0))
  2725. return true;
  2726. return false;
  2727. }
  2728. int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
  2729. unsigned int start, unsigned int end)
  2730. {
  2731. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2732. unsigned int segno;
  2733. int ret = 0;
  2734. f2fs_down_read(&SM_I(sbi)->curseg_lock);
  2735. mutex_lock(&curseg->curseg_mutex);
  2736. down_write(&SIT_I(sbi)->sentry_lock);
  2737. segno = CURSEG_I(sbi, type)->segno;
  2738. if (segno < start || segno > end)
  2739. goto unlock;
  2740. if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0))
  2741. ret = change_curseg(sbi, type);
  2742. else
  2743. ret = new_curseg(sbi, type, true);
  2744. stat_inc_seg_type(sbi, curseg);
  2745. locate_dirty_segment(sbi, segno);
  2746. unlock:
  2747. up_write(&SIT_I(sbi)->sentry_lock);
  2748. if (segno != curseg->segno)
  2749. f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u",
  2750. type, segno, curseg->segno);
  2751. mutex_unlock(&curseg->curseg_mutex);
  2752. f2fs_up_read(&SM_I(sbi)->curseg_lock);
  2753. return ret;
  2754. }
  2755. static int __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
  2756. bool new_sec, bool force)
  2757. {
  2758. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2759. unsigned int old_segno;
  2760. int err = 0;
  2761. if (type == CURSEG_COLD_DATA_PINNED && !curseg->inited)
  2762. goto allocate;
  2763. if (!force && curseg->inited &&
  2764. !curseg->next_blkoff &&
  2765. !get_valid_blocks(sbi, curseg->segno, new_sec) &&
  2766. !get_ckpt_valid_blocks(sbi, curseg->segno, new_sec))
  2767. return 0;
  2768. allocate:
  2769. old_segno = curseg->segno;
  2770. err = new_curseg(sbi, type, true);
  2771. if (err)
  2772. return err;
  2773. stat_inc_seg_type(sbi, curseg);
  2774. locate_dirty_segment(sbi, old_segno);
  2775. return 0;
  2776. }
  2777. int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force)
  2778. {
  2779. int ret;
  2780. f2fs_down_read(&SM_I(sbi)->curseg_lock);
  2781. down_write(&SIT_I(sbi)->sentry_lock);
  2782. ret = __allocate_new_segment(sbi, type, true, force);
  2783. up_write(&SIT_I(sbi)->sentry_lock);
  2784. f2fs_up_read(&SM_I(sbi)->curseg_lock);
  2785. return ret;
  2786. }
  2787. int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi)
  2788. {
  2789. struct f2fs_lock_context lc;
  2790. int err;
  2791. bool gc_required = true;
  2792. retry:
  2793. f2fs_lock_op(sbi, &lc);
  2794. err = f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
  2795. f2fs_unlock_op(sbi, &lc);
  2796. if (f2fs_sb_has_blkzoned(sbi) && err == -EAGAIN && gc_required) {
  2797. f2fs_down_write_trace(&sbi->gc_lock, &lc);
  2798. err = f2fs_gc_range(sbi, 0, sbi->first_seq_zone_segno - 1,
  2799. true, ZONED_PIN_SEC_REQUIRED_COUNT);
  2800. f2fs_up_write_trace(&sbi->gc_lock, &lc);
  2801. gc_required = false;
  2802. if (!err)
  2803. goto retry;
  2804. }
  2805. return err;
  2806. }
  2807. int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
  2808. {
  2809. int i;
  2810. int err = 0;
  2811. f2fs_down_read(&SM_I(sbi)->curseg_lock);
  2812. down_write(&SIT_I(sbi)->sentry_lock);
  2813. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
  2814. err += __allocate_new_segment(sbi, i, false, false);
  2815. up_write(&SIT_I(sbi)->sentry_lock);
  2816. f2fs_up_read(&SM_I(sbi)->curseg_lock);
  2817. return err;
  2818. }
  2819. bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
  2820. struct cp_control *cpc)
  2821. {
  2822. __u64 trim_start = cpc->trim_start;
  2823. bool has_candidate = false;
  2824. down_write(&SIT_I(sbi)->sentry_lock);
  2825. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
  2826. if (add_discard_addrs(sbi, cpc, true)) {
  2827. has_candidate = true;
  2828. break;
  2829. }
  2830. }
  2831. up_write(&SIT_I(sbi)->sentry_lock);
  2832. cpc->trim_start = trim_start;
  2833. return has_candidate;
  2834. }
  2835. static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
  2836. struct discard_policy *dpolicy,
  2837. unsigned int start, unsigned int end)
  2838. {
  2839. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  2840. struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
  2841. struct rb_node **insert_p = NULL, *insert_parent = NULL;
  2842. struct discard_cmd *dc;
  2843. struct blk_plug plug;
  2844. int issued;
  2845. unsigned int trimmed = 0;
  2846. next:
  2847. issued = 0;
  2848. mutex_lock(&dcc->cmd_lock);
  2849. if (unlikely(dcc->rbtree_check))
  2850. f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi));
  2851. dc = __lookup_discard_cmd_ret(&dcc->root, start,
  2852. &prev_dc, &next_dc, &insert_p, &insert_parent);
  2853. if (!dc)
  2854. dc = next_dc;
  2855. blk_start_plug(&plug);
  2856. while (dc && dc->di.lstart <= end) {
  2857. struct rb_node *node;
  2858. int err = 0;
  2859. if (dc->di.len < dpolicy->granularity)
  2860. goto skip;
  2861. if (dc->state != D_PREP) {
  2862. list_move_tail(&dc->list, &dcc->fstrim_list);
  2863. goto skip;
  2864. }
  2865. err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
  2866. if (issued >= dpolicy->max_requests) {
  2867. start = dc->di.lstart + dc->di.len;
  2868. if (err)
  2869. __remove_discard_cmd(sbi, dc);
  2870. blk_finish_plug(&plug);
  2871. mutex_unlock(&dcc->cmd_lock);
  2872. trimmed += __wait_all_discard_cmd(sbi, NULL);
  2873. f2fs_schedule_timeout(DEFAULT_DISCARD_INTERVAL);
  2874. goto next;
  2875. }
  2876. skip:
  2877. node = rb_next(&dc->rb_node);
  2878. if (err)
  2879. __remove_discard_cmd(sbi, dc);
  2880. dc = rb_entry_safe(node, struct discard_cmd, rb_node);
  2881. if (fatal_signal_pending(current))
  2882. break;
  2883. }
  2884. blk_finish_plug(&plug);
  2885. mutex_unlock(&dcc->cmd_lock);
  2886. return trimmed;
  2887. }
  2888. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
  2889. {
  2890. __u64 start = F2FS_BYTES_TO_BLK(range->start);
  2891. __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
  2892. unsigned int start_segno, end_segno;
  2893. block_t start_block, end_block;
  2894. struct cp_control cpc;
  2895. struct discard_policy dpolicy;
  2896. struct f2fs_lock_context lc;
  2897. unsigned long long trimmed = 0;
  2898. int err = 0;
  2899. bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
  2900. if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
  2901. return -EINVAL;
  2902. if (end < MAIN_BLKADDR(sbi))
  2903. goto out;
  2904. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
  2905. f2fs_warn(sbi, "Found FS corruption, run fsck to fix.");
  2906. return -EFSCORRUPTED;
  2907. }
  2908. /* start/end segment number in main_area */
  2909. start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
  2910. end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
  2911. GET_SEGNO(sbi, end);
  2912. if (need_align) {
  2913. start_segno = rounddown(start_segno, SEGS_PER_SEC(sbi));
  2914. end_segno = roundup(end_segno + 1, SEGS_PER_SEC(sbi)) - 1;
  2915. }
  2916. cpc.reason = CP_DISCARD;
  2917. cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
  2918. cpc.trim_start = start_segno;
  2919. cpc.trim_end = end_segno;
  2920. if (sbi->discard_blks == 0)
  2921. goto out;
  2922. f2fs_down_write_trace(&sbi->gc_lock, &lc);
  2923. stat_inc_cp_call_count(sbi, TOTAL_CALL);
  2924. err = f2fs_write_checkpoint(sbi, &cpc);
  2925. f2fs_up_write_trace(&sbi->gc_lock, &lc);
  2926. if (err)
  2927. goto out;
  2928. /*
  2929. * We filed discard candidates, but actually we don't need to wait for
  2930. * all of them, since they'll be issued in idle time along with runtime
  2931. * discard option. User configuration looks like using runtime discard
  2932. * or periodic fstrim instead of it.
  2933. */
  2934. if (f2fs_realtime_discard_enable(sbi))
  2935. goto out;
  2936. start_block = START_BLOCK(sbi, start_segno);
  2937. end_block = START_BLOCK(sbi, end_segno + 1);
  2938. __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
  2939. trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
  2940. start_block, end_block);
  2941. trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
  2942. start_block, end_block);
  2943. out:
  2944. if (!err)
  2945. range->len = F2FS_BLK_TO_BYTES(trimmed);
  2946. return err;
  2947. }
  2948. int f2fs_rw_hint_to_seg_type(struct f2fs_sb_info *sbi, enum rw_hint hint)
  2949. {
  2950. if (F2FS_OPTION(sbi).active_logs == 2)
  2951. return CURSEG_HOT_DATA;
  2952. else if (F2FS_OPTION(sbi).active_logs == 4)
  2953. return CURSEG_COLD_DATA;
  2954. /* active_log == 6 */
  2955. switch (hint) {
  2956. case WRITE_LIFE_SHORT:
  2957. return CURSEG_HOT_DATA;
  2958. case WRITE_LIFE_EXTREME:
  2959. return CURSEG_COLD_DATA;
  2960. default:
  2961. return CURSEG_WARM_DATA;
  2962. }
  2963. }
  2964. /*
  2965. * This returns write hints for each segment type. This hints will be
  2966. * passed down to block layer as below by default.
  2967. *
  2968. * User F2FS Block
  2969. * ---- ---- -----
  2970. * META WRITE_LIFE_NONE|REQ_META
  2971. * HOT_NODE WRITE_LIFE_NONE
  2972. * WARM_NODE WRITE_LIFE_MEDIUM
  2973. * COLD_NODE WRITE_LIFE_LONG
  2974. * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
  2975. * extension list " "
  2976. *
  2977. * -- buffered io
  2978. * COLD_DATA WRITE_LIFE_EXTREME
  2979. * HOT_DATA WRITE_LIFE_SHORT
  2980. * WARM_DATA WRITE_LIFE_NOT_SET
  2981. *
  2982. * -- direct io
  2983. * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
  2984. * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
  2985. * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
  2986. * WRITE_LIFE_NONE " WRITE_LIFE_NONE
  2987. * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
  2988. * WRITE_LIFE_LONG " WRITE_LIFE_LONG
  2989. */
  2990. enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
  2991. enum page_type type, enum temp_type temp)
  2992. {
  2993. switch (type) {
  2994. case DATA:
  2995. switch (temp) {
  2996. case WARM:
  2997. return WRITE_LIFE_NOT_SET;
  2998. case HOT:
  2999. return WRITE_LIFE_SHORT;
  3000. case COLD:
  3001. return WRITE_LIFE_EXTREME;
  3002. default:
  3003. return WRITE_LIFE_NONE;
  3004. }
  3005. case NODE:
  3006. switch (temp) {
  3007. case WARM:
  3008. return WRITE_LIFE_MEDIUM;
  3009. case HOT:
  3010. return WRITE_LIFE_NONE;
  3011. case COLD:
  3012. return WRITE_LIFE_LONG;
  3013. default:
  3014. return WRITE_LIFE_NONE;
  3015. }
  3016. case META:
  3017. return WRITE_LIFE_NONE;
  3018. default:
  3019. return WRITE_LIFE_NONE;
  3020. }
  3021. }
  3022. static int __get_segment_type_2(struct f2fs_io_info *fio)
  3023. {
  3024. if (fio->type == DATA)
  3025. return CURSEG_HOT_DATA;
  3026. else
  3027. return CURSEG_HOT_NODE;
  3028. }
  3029. static int __get_segment_type_4(struct f2fs_io_info *fio)
  3030. {
  3031. if (fio->type == DATA) {
  3032. struct inode *inode = fio_inode(fio);
  3033. if (S_ISDIR(inode->i_mode))
  3034. return CURSEG_HOT_DATA;
  3035. else
  3036. return CURSEG_COLD_DATA;
  3037. } else {
  3038. if (IS_DNODE(fio->folio) && is_cold_node(fio->folio))
  3039. return CURSEG_WARM_NODE;
  3040. else
  3041. return CURSEG_COLD_NODE;
  3042. }
  3043. }
  3044. static int __get_age_segment_type(struct inode *inode, pgoff_t pgofs)
  3045. {
  3046. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3047. struct extent_info ei = {};
  3048. if (f2fs_lookup_age_extent_cache(inode, pgofs, &ei)) {
  3049. if (!ei.age)
  3050. return NO_CHECK_TYPE;
  3051. if (ei.age <= sbi->hot_data_age_threshold)
  3052. return CURSEG_HOT_DATA;
  3053. if (ei.age <= sbi->warm_data_age_threshold)
  3054. return CURSEG_WARM_DATA;
  3055. return CURSEG_COLD_DATA;
  3056. }
  3057. return NO_CHECK_TYPE;
  3058. }
  3059. static int __get_segment_type_6(struct f2fs_io_info *fio)
  3060. {
  3061. if (fio->type == DATA) {
  3062. struct inode *inode = fio_inode(fio);
  3063. int type;
  3064. if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
  3065. return CURSEG_COLD_DATA_PINNED;
  3066. if (page_private_gcing(fio->page)) {
  3067. if (fio->sbi->am.atgc_enabled &&
  3068. (fio->io_type == FS_DATA_IO) &&
  3069. (fio->sbi->gc_mode != GC_URGENT_HIGH) &&
  3070. __is_valid_data_blkaddr(fio->old_blkaddr) &&
  3071. !is_inode_flag_set(inode, FI_OPU_WRITE))
  3072. return CURSEG_ALL_DATA_ATGC;
  3073. else
  3074. return CURSEG_COLD_DATA;
  3075. }
  3076. if (file_is_cold(inode) || f2fs_need_compress_data(inode))
  3077. return CURSEG_COLD_DATA;
  3078. type = __get_age_segment_type(inode, fio->folio->index);
  3079. if (type != NO_CHECK_TYPE)
  3080. return type;
  3081. if (file_is_hot(inode) ||
  3082. is_inode_flag_set(inode, FI_HOT_DATA) ||
  3083. f2fs_is_cow_file(inode) ||
  3084. is_inode_flag_set(inode, FI_NEED_IPU))
  3085. return CURSEG_HOT_DATA;
  3086. return f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode),
  3087. inode->i_write_hint);
  3088. } else {
  3089. if (IS_DNODE(fio->folio))
  3090. return is_cold_node(fio->folio) ? CURSEG_WARM_NODE :
  3091. CURSEG_HOT_NODE;
  3092. return CURSEG_COLD_NODE;
  3093. }
  3094. }
  3095. enum temp_type f2fs_get_segment_temp(struct f2fs_sb_info *sbi,
  3096. enum log_type type)
  3097. {
  3098. struct curseg_info *curseg = CURSEG_I(sbi, type);
  3099. enum temp_type temp = COLD;
  3100. switch (curseg->seg_type) {
  3101. case CURSEG_HOT_NODE:
  3102. case CURSEG_HOT_DATA:
  3103. temp = HOT;
  3104. break;
  3105. case CURSEG_WARM_NODE:
  3106. case CURSEG_WARM_DATA:
  3107. temp = WARM;
  3108. break;
  3109. case CURSEG_COLD_NODE:
  3110. case CURSEG_COLD_DATA:
  3111. temp = COLD;
  3112. break;
  3113. default:
  3114. f2fs_bug_on(sbi, 1);
  3115. }
  3116. return temp;
  3117. }
  3118. static int __get_segment_type(struct f2fs_io_info *fio)
  3119. {
  3120. enum log_type type = CURSEG_HOT_DATA;
  3121. switch (F2FS_OPTION(fio->sbi).active_logs) {
  3122. case 2:
  3123. type = __get_segment_type_2(fio);
  3124. break;
  3125. case 4:
  3126. type = __get_segment_type_4(fio);
  3127. break;
  3128. case 6:
  3129. type = __get_segment_type_6(fio);
  3130. break;
  3131. default:
  3132. f2fs_bug_on(fio->sbi, true);
  3133. }
  3134. fio->temp = f2fs_get_segment_temp(fio->sbi, type);
  3135. return type;
  3136. }
  3137. static void f2fs_randomize_chunk(struct f2fs_sb_info *sbi,
  3138. struct curseg_info *seg)
  3139. {
  3140. /* To allocate block chunks in different sizes, use random number */
  3141. if (--seg->fragment_remained_chunk > 0)
  3142. return;
  3143. seg->fragment_remained_chunk =
  3144. get_random_u32_inclusive(1, sbi->max_fragment_chunk);
  3145. seg->next_blkoff +=
  3146. get_random_u32_inclusive(1, sbi->max_fragment_hole);
  3147. }
  3148. int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio,
  3149. block_t old_blkaddr, block_t *new_blkaddr,
  3150. struct f2fs_summary *sum, int type,
  3151. struct f2fs_io_info *fio)
  3152. {
  3153. struct sit_info *sit_i = SIT_I(sbi);
  3154. struct curseg_info *curseg = CURSEG_I(sbi, type);
  3155. unsigned long long old_mtime;
  3156. bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
  3157. struct seg_entry *se = NULL;
  3158. bool segment_full = false;
  3159. int ret = 0;
  3160. f2fs_down_read(&SM_I(sbi)->curseg_lock);
  3161. mutex_lock(&curseg->curseg_mutex);
  3162. down_write(&sit_i->sentry_lock);
  3163. if (curseg->segno == NULL_SEGNO) {
  3164. ret = -ENOSPC;
  3165. goto out_err;
  3166. }
  3167. if (from_gc) {
  3168. f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO);
  3169. se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
  3170. sanity_check_seg_type(sbi, se->type);
  3171. f2fs_bug_on(sbi, IS_NODESEG(se->type));
  3172. }
  3173. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  3174. f2fs_bug_on(sbi, curseg->next_blkoff >= BLKS_PER_SEG(sbi));
  3175. f2fs_wait_discard_bio(sbi, *new_blkaddr);
  3176. sum_entries(curseg->sum_blk)[curseg->next_blkoff] = *sum;
  3177. if (curseg->alloc_type == SSR) {
  3178. curseg->next_blkoff = f2fs_find_next_ssr_block(sbi, curseg);
  3179. } else {
  3180. curseg->next_blkoff++;
  3181. if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
  3182. f2fs_randomize_chunk(sbi, curseg);
  3183. }
  3184. if (curseg->next_blkoff >= f2fs_usable_blks_in_seg(sbi, curseg->segno))
  3185. segment_full = true;
  3186. stat_inc_block_count(sbi, curseg);
  3187. if (from_gc) {
  3188. old_mtime = get_segment_mtime(sbi, old_blkaddr);
  3189. } else {
  3190. update_segment_mtime(sbi, old_blkaddr, 0);
  3191. old_mtime = 0;
  3192. }
  3193. update_segment_mtime(sbi, *new_blkaddr, old_mtime);
  3194. /*
  3195. * SIT information should be updated before segment allocation,
  3196. * since SSR needs latest valid block information.
  3197. */
  3198. update_sit_entry(sbi, *new_blkaddr, 1);
  3199. update_sit_entry(sbi, old_blkaddr, -1);
  3200. /*
  3201. * If the current segment is full, flush it out and replace it with a
  3202. * new segment.
  3203. */
  3204. if (segment_full) {
  3205. if (type == CURSEG_COLD_DATA_PINNED &&
  3206. !((curseg->segno + 1) % sbi->segs_per_sec)) {
  3207. write_sum_page(sbi, curseg->sum_blk, curseg->segno);
  3208. reset_curseg_fields(curseg);
  3209. goto skip_new_segment;
  3210. }
  3211. if (from_gc) {
  3212. ret = get_atssr_segment(sbi, type, se->type,
  3213. AT_SSR, se->mtime);
  3214. } else {
  3215. if (need_new_seg(sbi, type))
  3216. ret = new_curseg(sbi, type, false);
  3217. else
  3218. ret = change_curseg(sbi, type);
  3219. stat_inc_seg_type(sbi, curseg);
  3220. }
  3221. if (ret)
  3222. goto out_err;
  3223. }
  3224. skip_new_segment:
  3225. /*
  3226. * segment dirty status should be updated after segment allocation,
  3227. * so we just need to update status only one time after previous
  3228. * segment being closed.
  3229. */
  3230. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  3231. locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
  3232. if (IS_DATASEG(curseg->seg_type)) {
  3233. unsigned long long new_val;
  3234. new_val = atomic64_inc_return(&sbi->allocated_data_blocks);
  3235. if (unlikely(new_val == ULLONG_MAX))
  3236. atomic64_set(&sbi->allocated_data_blocks, 0);
  3237. }
  3238. up_write(&sit_i->sentry_lock);
  3239. if (folio && IS_NODESEG(curseg->seg_type)) {
  3240. fill_node_footer_blkaddr(folio, NEXT_FREE_BLKADDR(sbi, curseg));
  3241. f2fs_inode_chksum_set(sbi, folio);
  3242. }
  3243. if (fio) {
  3244. struct f2fs_bio_info *io;
  3245. INIT_LIST_HEAD(&fio->list);
  3246. fio->in_list = 1;
  3247. io = sbi->write_io[fio->type] + fio->temp;
  3248. spin_lock(&io->io_lock);
  3249. list_add_tail(&fio->list, &io->io_list);
  3250. spin_unlock(&io->io_lock);
  3251. }
  3252. mutex_unlock(&curseg->curseg_mutex);
  3253. f2fs_up_read(&SM_I(sbi)->curseg_lock);
  3254. return 0;
  3255. out_err:
  3256. *new_blkaddr = NULL_ADDR;
  3257. up_write(&sit_i->sentry_lock);
  3258. mutex_unlock(&curseg->curseg_mutex);
  3259. f2fs_up_read(&SM_I(sbi)->curseg_lock);
  3260. return ret;
  3261. }
  3262. void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
  3263. block_t blkaddr, unsigned int blkcnt)
  3264. {
  3265. if (!f2fs_is_multi_device(sbi))
  3266. return;
  3267. while (1) {
  3268. unsigned int devidx = f2fs_target_device_index(sbi, blkaddr);
  3269. unsigned int blks = FDEV(devidx).end_blk - blkaddr + 1;
  3270. /* update device state for fsync */
  3271. f2fs_set_dirty_device(sbi, ino, devidx, FLUSH_INO);
  3272. /* update device state for checkpoint */
  3273. if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
  3274. spin_lock(&sbi->dev_lock);
  3275. f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
  3276. spin_unlock(&sbi->dev_lock);
  3277. }
  3278. if (blkcnt <= blks)
  3279. break;
  3280. blkcnt -= blks;
  3281. blkaddr += blks;
  3282. }
  3283. }
  3284. static int log_type_to_seg_type(enum log_type type)
  3285. {
  3286. int seg_type = CURSEG_COLD_DATA;
  3287. switch (type) {
  3288. case CURSEG_HOT_DATA:
  3289. case CURSEG_WARM_DATA:
  3290. case CURSEG_COLD_DATA:
  3291. case CURSEG_HOT_NODE:
  3292. case CURSEG_WARM_NODE:
  3293. case CURSEG_COLD_NODE:
  3294. seg_type = (int)type;
  3295. break;
  3296. case CURSEG_COLD_DATA_PINNED:
  3297. case CURSEG_ALL_DATA_ATGC:
  3298. seg_type = CURSEG_COLD_DATA;
  3299. break;
  3300. default:
  3301. break;
  3302. }
  3303. return seg_type;
  3304. }
  3305. static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
  3306. {
  3307. struct folio *folio = fio->folio;
  3308. enum log_type type = __get_segment_type(fio);
  3309. int seg_type = log_type_to_seg_type(type);
  3310. bool keep_order = (f2fs_lfs_mode(fio->sbi) &&
  3311. seg_type == CURSEG_COLD_DATA);
  3312. int err;
  3313. if (keep_order)
  3314. f2fs_down_read(&fio->sbi->io_order_lock);
  3315. err = f2fs_allocate_data_block(fio->sbi, folio, fio->old_blkaddr,
  3316. &fio->new_blkaddr, sum, type, fio);
  3317. if (unlikely(err)) {
  3318. f2fs_err_ratelimited(fio->sbi,
  3319. "%s Failed to allocate data block, ino:%u, index:%lu, type:%d, old_blkaddr:0x%x, new_blkaddr:0x%x, err:%d",
  3320. __func__, fio->ino, folio->index, type,
  3321. fio->old_blkaddr, fio->new_blkaddr, err);
  3322. if (fscrypt_inode_uses_fs_layer_crypto(folio->mapping->host))
  3323. fscrypt_finalize_bounce_page(&fio->encrypted_page);
  3324. folio_end_writeback(folio);
  3325. if (f2fs_in_warm_node_list(fio->sbi, folio))
  3326. f2fs_del_fsync_node_entry(fio->sbi, folio);
  3327. f2fs_bug_on(fio->sbi, !is_set_ckpt_flags(fio->sbi,
  3328. CP_ERROR_FLAG));
  3329. goto out;
  3330. }
  3331. f2fs_bug_on(fio->sbi, !f2fs_is_valid_blkaddr_raw(fio->sbi,
  3332. fio->new_blkaddr, DATA_GENERIC_ENHANCE));
  3333. if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO)
  3334. f2fs_invalidate_internal_cache(fio->sbi, fio->old_blkaddr, 1);
  3335. /* writeout dirty page into bdev */
  3336. f2fs_submit_page_write(fio);
  3337. f2fs_update_device_state(fio->sbi, fio->ino, fio->new_blkaddr, 1);
  3338. out:
  3339. if (keep_order)
  3340. f2fs_up_read(&fio->sbi->io_order_lock);
  3341. }
  3342. void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio,
  3343. enum iostat_type io_type)
  3344. {
  3345. struct f2fs_io_info fio = {
  3346. .sbi = sbi,
  3347. .type = META,
  3348. .temp = HOT,
  3349. .op = REQ_OP_WRITE,
  3350. .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
  3351. .old_blkaddr = folio->index,
  3352. .new_blkaddr = folio->index,
  3353. .folio = folio,
  3354. .encrypted_page = NULL,
  3355. .in_list = 0,
  3356. };
  3357. if (unlikely(folio->index >= MAIN_BLKADDR(sbi)))
  3358. fio.op_flags &= ~REQ_META;
  3359. folio_start_writeback(folio);
  3360. f2fs_submit_page_write(&fio);
  3361. stat_inc_meta_count(sbi, folio->index);
  3362. f2fs_update_iostat(sbi, NULL, io_type, F2FS_BLKSIZE);
  3363. }
  3364. void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
  3365. {
  3366. struct f2fs_summary sum;
  3367. set_summary(&sum, nid, 0, 0);
  3368. do_write_page(&sum, fio);
  3369. f2fs_update_iostat(fio->sbi, NULL, fio->io_type, F2FS_BLKSIZE);
  3370. }
  3371. void f2fs_outplace_write_data(struct dnode_of_data *dn,
  3372. struct f2fs_io_info *fio)
  3373. {
  3374. struct f2fs_sb_info *sbi = fio->sbi;
  3375. struct f2fs_summary sum;
  3376. f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
  3377. if (fio->io_type == FS_DATA_IO || fio->io_type == FS_CP_DATA_IO)
  3378. f2fs_update_age_extent_cache(dn);
  3379. set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
  3380. do_write_page(&sum, fio);
  3381. f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
  3382. f2fs_update_iostat(sbi, dn->inode, fio->io_type, F2FS_BLKSIZE);
  3383. }
  3384. int f2fs_inplace_write_data(struct f2fs_io_info *fio)
  3385. {
  3386. int err;
  3387. struct f2fs_sb_info *sbi = fio->sbi;
  3388. unsigned int segno;
  3389. fio->new_blkaddr = fio->old_blkaddr;
  3390. /* i/o temperature is needed for passing down write hints */
  3391. __get_segment_type(fio);
  3392. segno = GET_SEGNO(sbi, fio->new_blkaddr);
  3393. if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
  3394. set_sbi_flag(sbi, SBI_NEED_FSCK);
  3395. f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.",
  3396. __func__, segno);
  3397. err = -EFSCORRUPTED;
  3398. f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE);
  3399. goto drop_bio;
  3400. }
  3401. if (f2fs_cp_error(sbi)) {
  3402. err = -EIO;
  3403. goto drop_bio;
  3404. }
  3405. if (fio->meta_gc)
  3406. f2fs_truncate_meta_inode_pages(sbi, fio->new_blkaddr, 1);
  3407. stat_inc_inplace_blocks(fio->sbi);
  3408. if (fio->bio && !IS_F2FS_IPU_NOCACHE(sbi))
  3409. err = f2fs_merge_page_bio(fio);
  3410. else
  3411. err = f2fs_submit_page_bio(fio);
  3412. if (!err) {
  3413. f2fs_update_device_state(fio->sbi, fio->ino,
  3414. fio->new_blkaddr, 1);
  3415. f2fs_update_iostat(fio->sbi, fio_inode(fio),
  3416. fio->io_type, F2FS_BLKSIZE);
  3417. }
  3418. return err;
  3419. drop_bio:
  3420. if (fio->bio && *(fio->bio)) {
  3421. struct bio *bio = *(fio->bio);
  3422. bio->bi_status = BLK_STS_IOERR;
  3423. bio_endio(bio);
  3424. *(fio->bio) = NULL;
  3425. }
  3426. return err;
  3427. }
  3428. static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
  3429. unsigned int segno)
  3430. {
  3431. int i;
  3432. for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
  3433. if (CURSEG_I(sbi, i)->segno == segno)
  3434. break;
  3435. }
  3436. return i;
  3437. }
  3438. void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  3439. block_t old_blkaddr, block_t new_blkaddr,
  3440. bool recover_curseg, bool recover_newaddr,
  3441. bool from_gc)
  3442. {
  3443. struct sit_info *sit_i = SIT_I(sbi);
  3444. struct curseg_info *curseg;
  3445. unsigned int segno, old_cursegno;
  3446. struct seg_entry *se;
  3447. int type;
  3448. unsigned short old_blkoff;
  3449. unsigned char old_alloc_type;
  3450. segno = GET_SEGNO(sbi, new_blkaddr);
  3451. se = get_seg_entry(sbi, segno);
  3452. type = se->type;
  3453. f2fs_down_write(&SM_I(sbi)->curseg_lock);
  3454. if (!recover_curseg) {
  3455. /* for recovery flow */
  3456. if (se->valid_blocks == 0 && !is_curseg(sbi, segno)) {
  3457. if (old_blkaddr == NULL_ADDR)
  3458. type = CURSEG_COLD_DATA;
  3459. else
  3460. type = CURSEG_WARM_DATA;
  3461. }
  3462. } else {
  3463. if (is_curseg(sbi, segno)) {
  3464. /* se->type is volatile as SSR allocation */
  3465. type = __f2fs_get_curseg(sbi, segno);
  3466. f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
  3467. } else {
  3468. type = CURSEG_WARM_DATA;
  3469. }
  3470. }
  3471. curseg = CURSEG_I(sbi, type);
  3472. f2fs_bug_on(sbi, !IS_DATASEG(curseg->seg_type));
  3473. mutex_lock(&curseg->curseg_mutex);
  3474. down_write(&sit_i->sentry_lock);
  3475. old_cursegno = curseg->segno;
  3476. old_blkoff = curseg->next_blkoff;
  3477. old_alloc_type = curseg->alloc_type;
  3478. /* change the current segment */
  3479. if (segno != curseg->segno) {
  3480. curseg->next_segno = segno;
  3481. if (change_curseg(sbi, type))
  3482. goto out_unlock;
  3483. }
  3484. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  3485. sum_entries(curseg->sum_blk)[curseg->next_blkoff] = *sum;
  3486. if (!recover_curseg || recover_newaddr) {
  3487. if (!from_gc)
  3488. update_segment_mtime(sbi, new_blkaddr, 0);
  3489. update_sit_entry(sbi, new_blkaddr, 1);
  3490. }
  3491. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
  3492. f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
  3493. if (!from_gc)
  3494. update_segment_mtime(sbi, old_blkaddr, 0);
  3495. update_sit_entry(sbi, old_blkaddr, -1);
  3496. }
  3497. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  3498. locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
  3499. locate_dirty_segment(sbi, old_cursegno);
  3500. if (recover_curseg) {
  3501. if (old_cursegno != curseg->segno) {
  3502. curseg->next_segno = old_cursegno;
  3503. if (change_curseg(sbi, type))
  3504. goto out_unlock;
  3505. }
  3506. curseg->next_blkoff = old_blkoff;
  3507. curseg->alloc_type = old_alloc_type;
  3508. }
  3509. out_unlock:
  3510. up_write(&sit_i->sentry_lock);
  3511. mutex_unlock(&curseg->curseg_mutex);
  3512. f2fs_up_write(&SM_I(sbi)->curseg_lock);
  3513. }
  3514. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  3515. block_t old_addr, block_t new_addr,
  3516. unsigned char version, bool recover_curseg,
  3517. bool recover_newaddr)
  3518. {
  3519. struct f2fs_summary sum;
  3520. set_summary(&sum, dn->nid, dn->ofs_in_node, version);
  3521. f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
  3522. recover_curseg, recover_newaddr, false);
  3523. f2fs_update_data_blkaddr(dn, new_addr);
  3524. }
  3525. void f2fs_folio_wait_writeback(struct folio *folio, enum page_type type,
  3526. bool ordered, bool locked)
  3527. {
  3528. if (folio_test_writeback(folio)) {
  3529. struct f2fs_sb_info *sbi = F2FS_F_SB(folio);
  3530. /* submit cached LFS IO */
  3531. f2fs_submit_merged_write_folio(sbi, folio, type);
  3532. /* submit cached IPU IO */
  3533. f2fs_submit_merged_ipu_write(sbi, NULL, folio);
  3534. if (ordered) {
  3535. folio_wait_writeback(folio);
  3536. f2fs_bug_on(sbi, locked && folio_test_writeback(folio));
  3537. } else {
  3538. folio_wait_stable(folio);
  3539. }
  3540. }
  3541. }
  3542. void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
  3543. {
  3544. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3545. struct folio *cfolio;
  3546. if (!f2fs_meta_inode_gc_required(inode))
  3547. return;
  3548. if (!__is_valid_data_blkaddr(blkaddr))
  3549. return;
  3550. cfolio = filemap_lock_folio(META_MAPPING(sbi), blkaddr);
  3551. if (!IS_ERR(cfolio)) {
  3552. f2fs_folio_wait_writeback(cfolio, DATA, true, true);
  3553. f2fs_folio_put(cfolio, true);
  3554. }
  3555. }
  3556. void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
  3557. block_t len)
  3558. {
  3559. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3560. block_t i;
  3561. if (!f2fs_meta_inode_gc_required(inode))
  3562. return;
  3563. for (i = 0; i < len; i++)
  3564. f2fs_wait_on_block_writeback(inode, blkaddr + i);
  3565. f2fs_truncate_meta_inode_pages(sbi, blkaddr, len);
  3566. }
  3567. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  3568. {
  3569. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  3570. struct curseg_info *seg_i;
  3571. unsigned char *kaddr;
  3572. struct folio *folio;
  3573. block_t start;
  3574. int i, j, offset;
  3575. start = start_sum_block(sbi);
  3576. folio = f2fs_get_meta_folio(sbi, start++);
  3577. if (IS_ERR(folio))
  3578. return PTR_ERR(folio);
  3579. kaddr = folio_address(folio);
  3580. /* Step 1: restore nat cache */
  3581. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  3582. memcpy(seg_i->journal, kaddr, sbi->sum_journal_size);
  3583. /* Step 2: restore sit cache */
  3584. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  3585. memcpy(seg_i->journal, kaddr + sbi->sum_journal_size, sbi->sum_journal_size);
  3586. offset = 2 * sbi->sum_journal_size;
  3587. /* Step 3: restore summary entries */
  3588. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  3589. unsigned short blk_off;
  3590. unsigned int segno;
  3591. seg_i = CURSEG_I(sbi, i);
  3592. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  3593. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  3594. seg_i->next_segno = segno;
  3595. reset_curseg(sbi, i, 0);
  3596. seg_i->alloc_type = ckpt->alloc_type[i];
  3597. seg_i->next_blkoff = blk_off;
  3598. if (seg_i->alloc_type == SSR)
  3599. blk_off = BLKS_PER_SEG(sbi);
  3600. for (j = 0; j < blk_off; j++) {
  3601. struct f2fs_summary *s;
  3602. s = (struct f2fs_summary *)(kaddr + offset);
  3603. sum_entries(seg_i->sum_blk)[j] = *s;
  3604. offset += SUMMARY_SIZE;
  3605. if (offset + SUMMARY_SIZE <= sbi->blocksize -
  3606. SUM_FOOTER_SIZE)
  3607. continue;
  3608. f2fs_folio_put(folio, true);
  3609. folio = f2fs_get_meta_folio(sbi, start++);
  3610. if (IS_ERR(folio))
  3611. return PTR_ERR(folio);
  3612. kaddr = folio_address(folio);
  3613. offset = 0;
  3614. }
  3615. }
  3616. f2fs_folio_put(folio, true);
  3617. return 0;
  3618. }
  3619. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  3620. {
  3621. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  3622. struct f2fs_summary_block *sum;
  3623. struct curseg_info *curseg;
  3624. struct folio *new;
  3625. unsigned short blk_off;
  3626. unsigned int segno = 0;
  3627. block_t blk_addr = 0;
  3628. int err = 0;
  3629. /* get segment number and block addr */
  3630. if (IS_DATASEG(type)) {
  3631. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  3632. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  3633. CURSEG_HOT_DATA]);
  3634. if (__exist_node_summaries(sbi))
  3635. blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type);
  3636. else
  3637. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  3638. } else {
  3639. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  3640. CURSEG_HOT_NODE]);
  3641. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  3642. CURSEG_HOT_NODE]);
  3643. if (__exist_node_summaries(sbi))
  3644. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  3645. type - CURSEG_HOT_NODE);
  3646. else
  3647. blk_addr = GET_SUM_BLOCK(sbi, segno);
  3648. }
  3649. new = f2fs_get_meta_folio(sbi, blk_addr);
  3650. if (IS_ERR(new))
  3651. return PTR_ERR(new);
  3652. sum = folio_address(new);
  3653. if (IS_NODESEG(type)) {
  3654. if (__exist_node_summaries(sbi)) {
  3655. struct f2fs_summary *ns = sum_entries(sum);
  3656. int i;
  3657. for (i = 0; i < BLKS_PER_SEG(sbi); i++, ns++) {
  3658. ns->version = 0;
  3659. ns->ofs_in_node = 0;
  3660. }
  3661. } else {
  3662. err = f2fs_restore_node_summary(sbi, segno, sum);
  3663. if (err)
  3664. goto out;
  3665. }
  3666. }
  3667. /* set uncompleted segment to curseg */
  3668. curseg = CURSEG_I(sbi, type);
  3669. mutex_lock(&curseg->curseg_mutex);
  3670. /* update journal info */
  3671. down_write(&curseg->journal_rwsem);
  3672. memcpy(curseg->journal, sum_journal(sbi, sum), sbi->sum_journal_size);
  3673. up_write(&curseg->journal_rwsem);
  3674. memcpy(sum_entries(curseg->sum_blk), sum_entries(sum),
  3675. sbi->sum_entry_size);
  3676. memcpy(sum_footer(sbi, curseg->sum_blk), sum_footer(sbi, sum),
  3677. SUM_FOOTER_SIZE);
  3678. curseg->next_segno = segno;
  3679. reset_curseg(sbi, type, 0);
  3680. curseg->alloc_type = ckpt->alloc_type[type];
  3681. curseg->next_blkoff = blk_off;
  3682. mutex_unlock(&curseg->curseg_mutex);
  3683. out:
  3684. f2fs_folio_put(new, true);
  3685. return err;
  3686. }
  3687. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  3688. {
  3689. struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
  3690. struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
  3691. int type = CURSEG_HOT_DATA;
  3692. int err;
  3693. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
  3694. int npages = f2fs_npages_for_summary_flush(sbi, true);
  3695. if (npages >= 2)
  3696. f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
  3697. META_CP, true);
  3698. /* restore for compacted data summary */
  3699. err = read_compacted_summaries(sbi);
  3700. if (err)
  3701. return err;
  3702. type = CURSEG_HOT_NODE;
  3703. }
  3704. if (__exist_node_summaries(sbi))
  3705. f2fs_ra_meta_pages(sbi,
  3706. sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type),
  3707. NR_CURSEG_PERSIST_TYPE - type, META_CP, true);
  3708. for (; type <= CURSEG_COLD_NODE; type++) {
  3709. err = read_normal_summaries(sbi, type);
  3710. if (err)
  3711. return err;
  3712. }
  3713. /* sanity check for summary blocks */
  3714. if (nats_in_cursum(nat_j) > sbi->nat_journal_entries ||
  3715. sits_in_cursum(sit_j) > sbi->sit_journal_entries) {
  3716. f2fs_err(sbi, "invalid journal entries nats %u sits %u",
  3717. nats_in_cursum(nat_j), sits_in_cursum(sit_j));
  3718. return -EINVAL;
  3719. }
  3720. return 0;
  3721. }
  3722. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  3723. {
  3724. struct folio *folio;
  3725. unsigned char *kaddr;
  3726. struct f2fs_summary *summary;
  3727. struct curseg_info *seg_i;
  3728. int written_size = 0;
  3729. int i, j;
  3730. folio = f2fs_grab_meta_folio(sbi, blkaddr++);
  3731. kaddr = folio_address(folio);
  3732. memset(kaddr, 0, PAGE_SIZE);
  3733. /* Step 1: write nat cache */
  3734. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  3735. memcpy(kaddr, seg_i->journal, sbi->sum_journal_size);
  3736. written_size += sbi->sum_journal_size;
  3737. /* Step 2: write sit cache */
  3738. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  3739. memcpy(kaddr + written_size, seg_i->journal, sbi->sum_journal_size);
  3740. written_size += sbi->sum_journal_size;
  3741. /* Step 3: write summary entries */
  3742. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  3743. seg_i = CURSEG_I(sbi, i);
  3744. for (j = 0; j < f2fs_curseg_valid_blocks(sbi, i); j++) {
  3745. if (!folio) {
  3746. folio = f2fs_grab_meta_folio(sbi, blkaddr++);
  3747. kaddr = folio_address(folio);
  3748. memset(kaddr, 0, PAGE_SIZE);
  3749. written_size = 0;
  3750. }
  3751. summary = (struct f2fs_summary *)(kaddr + written_size);
  3752. *summary = sum_entries(seg_i->sum_blk)[j];
  3753. written_size += SUMMARY_SIZE;
  3754. if (written_size + SUMMARY_SIZE <= sbi->blocksize -
  3755. SUM_FOOTER_SIZE)
  3756. continue;
  3757. folio_mark_dirty(folio);
  3758. f2fs_folio_put(folio, true);
  3759. folio = NULL;
  3760. }
  3761. }
  3762. if (folio) {
  3763. folio_mark_dirty(folio);
  3764. f2fs_folio_put(folio, true);
  3765. }
  3766. }
  3767. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  3768. block_t blkaddr, int type)
  3769. {
  3770. int i, end;
  3771. if (IS_DATASEG(type))
  3772. end = type + NR_CURSEG_DATA_TYPE;
  3773. else
  3774. end = type + NR_CURSEG_NODE_TYPE;
  3775. for (i = type; i < end; i++)
  3776. write_current_sum_page(sbi, i, blkaddr + (i - type));
  3777. }
  3778. void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  3779. {
  3780. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
  3781. write_compacted_summaries(sbi, start_blk);
  3782. else
  3783. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  3784. }
  3785. void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  3786. {
  3787. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  3788. }
  3789. int f2fs_lookup_journal_in_cursum(struct f2fs_sb_info *sbi,
  3790. struct f2fs_journal *journal, int type,
  3791. unsigned int val, int alloc)
  3792. {
  3793. int i;
  3794. if (type == NAT_JOURNAL) {
  3795. for (i = 0; i < nats_in_cursum(journal); i++) {
  3796. if (le32_to_cpu(nid_in_journal(journal, i)) == val)
  3797. return i;
  3798. }
  3799. if (alloc && __has_cursum_space(sbi, journal, 1, NAT_JOURNAL))
  3800. return update_nats_in_cursum(journal, 1);
  3801. } else if (type == SIT_JOURNAL) {
  3802. for (i = 0; i < sits_in_cursum(journal); i++)
  3803. if (le32_to_cpu(segno_in_journal(journal, i)) == val)
  3804. return i;
  3805. if (alloc && __has_cursum_space(sbi, journal, 1, SIT_JOURNAL))
  3806. return update_sits_in_cursum(journal, 1);
  3807. }
  3808. return -1;
  3809. }
  3810. static struct folio *get_current_sit_folio(struct f2fs_sb_info *sbi,
  3811. unsigned int segno)
  3812. {
  3813. return f2fs_get_meta_folio(sbi, current_sit_addr(sbi, segno));
  3814. }
  3815. static struct folio *get_next_sit_folio(struct f2fs_sb_info *sbi,
  3816. unsigned int start)
  3817. {
  3818. struct sit_info *sit_i = SIT_I(sbi);
  3819. struct folio *folio;
  3820. pgoff_t src_off, dst_off;
  3821. src_off = current_sit_addr(sbi, start);
  3822. dst_off = next_sit_addr(sbi, src_off);
  3823. folio = f2fs_grab_meta_folio(sbi, dst_off);
  3824. seg_info_to_sit_folio(sbi, folio, start);
  3825. folio_mark_dirty(folio);
  3826. set_to_next_sit(sit_i, start);
  3827. return folio;
  3828. }
  3829. static struct sit_entry_set *grab_sit_entry_set(void)
  3830. {
  3831. struct sit_entry_set *ses =
  3832. f2fs_kmem_cache_alloc(sit_entry_set_slab,
  3833. GFP_NOFS, true, NULL);
  3834. ses->entry_cnt = 0;
  3835. INIT_LIST_HEAD(&ses->set_list);
  3836. return ses;
  3837. }
  3838. static void release_sit_entry_set(struct sit_entry_set *ses)
  3839. {
  3840. list_del(&ses->set_list);
  3841. kmem_cache_free(sit_entry_set_slab, ses);
  3842. }
  3843. static void adjust_sit_entry_set(struct sit_entry_set *ses,
  3844. struct list_head *head)
  3845. {
  3846. struct sit_entry_set *next = ses;
  3847. if (list_is_last(&ses->set_list, head))
  3848. return;
  3849. list_for_each_entry_continue(next, head, set_list)
  3850. if (ses->entry_cnt <= next->entry_cnt) {
  3851. list_move_tail(&ses->set_list, &next->set_list);
  3852. return;
  3853. }
  3854. list_move_tail(&ses->set_list, head);
  3855. }
  3856. static void add_sit_entry(unsigned int segno, struct list_head *head)
  3857. {
  3858. struct sit_entry_set *ses;
  3859. unsigned int start_segno = START_SEGNO(segno);
  3860. list_for_each_entry(ses, head, set_list) {
  3861. if (ses->start_segno == start_segno) {
  3862. ses->entry_cnt++;
  3863. adjust_sit_entry_set(ses, head);
  3864. return;
  3865. }
  3866. }
  3867. ses = grab_sit_entry_set();
  3868. ses->start_segno = start_segno;
  3869. ses->entry_cnt++;
  3870. list_add(&ses->set_list, head);
  3871. }
  3872. static void add_sits_in_set(struct f2fs_sb_info *sbi)
  3873. {
  3874. struct f2fs_sm_info *sm_info = SM_I(sbi);
  3875. struct list_head *set_list = &sm_info->sit_entry_set;
  3876. unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
  3877. unsigned int segno;
  3878. for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
  3879. add_sit_entry(segno, set_list);
  3880. }
  3881. static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
  3882. {
  3883. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  3884. struct f2fs_journal *journal = curseg->journal;
  3885. int i;
  3886. down_write(&curseg->journal_rwsem);
  3887. for (i = 0; i < sits_in_cursum(journal); i++) {
  3888. unsigned int segno;
  3889. bool dirtied;
  3890. segno = le32_to_cpu(segno_in_journal(journal, i));
  3891. dirtied = __mark_sit_entry_dirty(sbi, segno);
  3892. if (!dirtied)
  3893. add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
  3894. }
  3895. update_sits_in_cursum(journal, -i);
  3896. up_write(&curseg->journal_rwsem);
  3897. }
  3898. /*
  3899. * CP calls this function, which flushes SIT entries including sit_journal,
  3900. * and moves prefree segs to free segs.
  3901. */
  3902. void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  3903. {
  3904. struct sit_info *sit_i = SIT_I(sbi);
  3905. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  3906. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  3907. struct f2fs_journal *journal = curseg->journal;
  3908. struct sit_entry_set *ses, *tmp;
  3909. struct list_head *head = &SM_I(sbi)->sit_entry_set;
  3910. bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
  3911. struct seg_entry *se;
  3912. down_write(&sit_i->sentry_lock);
  3913. if (!sit_i->dirty_sentries)
  3914. goto out;
  3915. /*
  3916. * add and account sit entries of dirty bitmap in sit entry
  3917. * set temporarily
  3918. */
  3919. add_sits_in_set(sbi);
  3920. /*
  3921. * if there are no enough space in journal to store dirty sit
  3922. * entries, remove all entries from journal and add and account
  3923. * them in sit entry set.
  3924. */
  3925. if (!__has_cursum_space(sbi, journal,
  3926. sit_i->dirty_sentries, SIT_JOURNAL) || !to_journal)
  3927. remove_sits_in_journal(sbi);
  3928. /*
  3929. * there are two steps to flush sit entries:
  3930. * #1, flush sit entries to journal in current cold data summary block.
  3931. * #2, flush sit entries to sit page.
  3932. */
  3933. list_for_each_entry_safe(ses, tmp, head, set_list) {
  3934. struct folio *folio = NULL;
  3935. struct f2fs_sit_block *raw_sit = NULL;
  3936. unsigned int start_segno = ses->start_segno;
  3937. unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
  3938. (unsigned long)MAIN_SEGS(sbi));
  3939. unsigned int segno = start_segno;
  3940. if (to_journal &&
  3941. !__has_cursum_space(sbi, journal, ses->entry_cnt,
  3942. SIT_JOURNAL))
  3943. to_journal = false;
  3944. if (to_journal) {
  3945. down_write(&curseg->journal_rwsem);
  3946. } else {
  3947. folio = get_next_sit_folio(sbi, start_segno);
  3948. raw_sit = folio_address(folio);
  3949. }
  3950. /* flush dirty sit entries in region of current sit set */
  3951. for_each_set_bit_from(segno, bitmap, end) {
  3952. int offset, sit_offset;
  3953. se = get_seg_entry(sbi, segno);
  3954. #ifdef CONFIG_F2FS_CHECK_FS
  3955. if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
  3956. SIT_VBLOCK_MAP_SIZE))
  3957. f2fs_bug_on(sbi, 1);
  3958. #endif
  3959. /* add discard candidates */
  3960. if (!(cpc->reason & CP_DISCARD)) {
  3961. cpc->trim_start = segno;
  3962. add_discard_addrs(sbi, cpc, false);
  3963. }
  3964. if (to_journal) {
  3965. offset = f2fs_lookup_journal_in_cursum(sbi, journal,
  3966. SIT_JOURNAL, segno, 1);
  3967. f2fs_bug_on(sbi, offset < 0);
  3968. segno_in_journal(journal, offset) =
  3969. cpu_to_le32(segno);
  3970. seg_info_to_raw_sit(se,
  3971. &sit_in_journal(journal, offset));
  3972. check_block_count(sbi, segno,
  3973. &sit_in_journal(journal, offset));
  3974. } else {
  3975. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  3976. seg_info_to_raw_sit(se,
  3977. &raw_sit->entries[sit_offset]);
  3978. check_block_count(sbi, segno,
  3979. &raw_sit->entries[sit_offset]);
  3980. }
  3981. /* update ckpt_valid_block */
  3982. if (__is_large_section(sbi)) {
  3983. set_ckpt_valid_blocks(sbi, segno);
  3984. sanity_check_valid_blocks(sbi, segno);
  3985. }
  3986. __clear_bit(segno, bitmap);
  3987. sit_i->dirty_sentries--;
  3988. ses->entry_cnt--;
  3989. }
  3990. if (to_journal)
  3991. up_write(&curseg->journal_rwsem);
  3992. else
  3993. f2fs_folio_put(folio, true);
  3994. f2fs_bug_on(sbi, ses->entry_cnt);
  3995. release_sit_entry_set(ses);
  3996. }
  3997. f2fs_bug_on(sbi, !list_empty(head));
  3998. f2fs_bug_on(sbi, sit_i->dirty_sentries);
  3999. out:
  4000. if (cpc->reason & CP_DISCARD) {
  4001. __u64 trim_start = cpc->trim_start;
  4002. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
  4003. add_discard_addrs(sbi, cpc, false);
  4004. cpc->trim_start = trim_start;
  4005. }
  4006. up_write(&sit_i->sentry_lock);
  4007. set_prefree_as_free_segments(sbi);
  4008. }
  4009. static int build_sit_info(struct f2fs_sb_info *sbi)
  4010. {
  4011. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  4012. struct sit_info *sit_i;
  4013. unsigned int sit_segs, start;
  4014. char *src_bitmap, *bitmap;
  4015. unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
  4016. unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0;
  4017. /* allocate memory for SIT information */
  4018. sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
  4019. if (!sit_i)
  4020. return -ENOMEM;
  4021. SM_I(sbi)->sit_info = sit_i;
  4022. sit_i->sentries =
  4023. f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
  4024. MAIN_SEGS(sbi)),
  4025. GFP_KERNEL);
  4026. if (!sit_i->sentries)
  4027. return -ENOMEM;
  4028. main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  4029. sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
  4030. GFP_KERNEL);
  4031. if (!sit_i->dirty_sentries_bitmap)
  4032. return -ENOMEM;
  4033. #ifdef CONFIG_F2FS_CHECK_FS
  4034. bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map);
  4035. #else
  4036. bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map);
  4037. #endif
  4038. sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
  4039. if (!sit_i->bitmap)
  4040. return -ENOMEM;
  4041. bitmap = sit_i->bitmap;
  4042. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  4043. sit_i->sentries[start].cur_valid_map = bitmap;
  4044. bitmap += SIT_VBLOCK_MAP_SIZE;
  4045. sit_i->sentries[start].ckpt_valid_map = bitmap;
  4046. bitmap += SIT_VBLOCK_MAP_SIZE;
  4047. #ifdef CONFIG_F2FS_CHECK_FS
  4048. sit_i->sentries[start].cur_valid_map_mir = bitmap;
  4049. bitmap += SIT_VBLOCK_MAP_SIZE;
  4050. #endif
  4051. if (discard_map) {
  4052. sit_i->sentries[start].discard_map = bitmap;
  4053. bitmap += SIT_VBLOCK_MAP_SIZE;
  4054. }
  4055. }
  4056. sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  4057. if (!sit_i->tmp_map)
  4058. return -ENOMEM;
  4059. if (__is_large_section(sbi)) {
  4060. sit_i->sec_entries =
  4061. f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
  4062. MAIN_SECS(sbi)),
  4063. GFP_KERNEL);
  4064. if (!sit_i->sec_entries)
  4065. return -ENOMEM;
  4066. }
  4067. /* get information related with SIT */
  4068. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  4069. /* setup SIT bitmap from ckeckpoint pack */
  4070. sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  4071. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  4072. sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
  4073. if (!sit_i->sit_bitmap)
  4074. return -ENOMEM;
  4075. #ifdef CONFIG_F2FS_CHECK_FS
  4076. sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
  4077. sit_bitmap_size, GFP_KERNEL);
  4078. if (!sit_i->sit_bitmap_mir)
  4079. return -ENOMEM;
  4080. sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
  4081. main_bitmap_size, GFP_KERNEL);
  4082. if (!sit_i->invalid_segmap)
  4083. return -ENOMEM;
  4084. #endif
  4085. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  4086. sit_i->sit_blocks = SEGS_TO_BLKS(sbi, sit_segs);
  4087. sit_i->written_valid_blocks = 0;
  4088. sit_i->bitmap_size = sit_bitmap_size;
  4089. sit_i->dirty_sentries = 0;
  4090. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  4091. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  4092. sit_i->mounted_time = ktime_get_boottime_seconds();
  4093. init_rwsem(&sit_i->sentry_lock);
  4094. return 0;
  4095. }
  4096. static int build_free_segmap(struct f2fs_sb_info *sbi)
  4097. {
  4098. struct free_segmap_info *free_i;
  4099. unsigned int bitmap_size, sec_bitmap_size;
  4100. /* allocate memory for free segmap information */
  4101. free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
  4102. if (!free_i)
  4103. return -ENOMEM;
  4104. SM_I(sbi)->free_info = free_i;
  4105. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  4106. free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
  4107. if (!free_i->free_segmap)
  4108. return -ENOMEM;
  4109. sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  4110. free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
  4111. if (!free_i->free_secmap)
  4112. return -ENOMEM;
  4113. /* set all segments as dirty temporarily */
  4114. memset(free_i->free_segmap, 0xff, bitmap_size);
  4115. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  4116. /* init free segmap information */
  4117. free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
  4118. free_i->free_segments = 0;
  4119. free_i->free_sections = 0;
  4120. spin_lock_init(&free_i->segmap_lock);
  4121. return 0;
  4122. }
  4123. static int build_curseg(struct f2fs_sb_info *sbi)
  4124. {
  4125. struct curseg_info *array;
  4126. int i;
  4127. array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE,
  4128. sizeof(*array)), GFP_KERNEL);
  4129. if (!array)
  4130. return -ENOMEM;
  4131. SM_I(sbi)->curseg_array = array;
  4132. for (i = 0; i < NO_CHECK_TYPE; i++) {
  4133. mutex_init(&array[i].curseg_mutex);
  4134. array[i].sum_blk = f2fs_kzalloc(sbi, sbi->sum_blocksize,
  4135. GFP_KERNEL);
  4136. if (!array[i].sum_blk)
  4137. return -ENOMEM;
  4138. init_rwsem(&array[i].journal_rwsem);
  4139. array[i].journal = f2fs_kzalloc(sbi,
  4140. sbi->sum_journal_size, GFP_KERNEL);
  4141. if (!array[i].journal)
  4142. return -ENOMEM;
  4143. array[i].seg_type = log_type_to_seg_type(i);
  4144. reset_curseg_fields(&array[i]);
  4145. }
  4146. return restore_curseg_summaries(sbi);
  4147. }
  4148. static int build_sit_entries(struct f2fs_sb_info *sbi)
  4149. {
  4150. struct sit_info *sit_i = SIT_I(sbi);
  4151. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  4152. struct f2fs_journal *journal = curseg->journal;
  4153. struct seg_entry *se;
  4154. struct f2fs_sit_entry sit;
  4155. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  4156. unsigned int i, start, end;
  4157. unsigned int readed, start_blk = 0;
  4158. int err = 0;
  4159. block_t sit_valid_blocks[2] = {0, 0};
  4160. do {
  4161. readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
  4162. META_SIT, true);
  4163. start = start_blk * sit_i->sents_per_block;
  4164. end = (start_blk + readed) * sit_i->sents_per_block;
  4165. for (; start < end && start < MAIN_SEGS(sbi); start++) {
  4166. struct f2fs_sit_block *sit_blk;
  4167. struct folio *folio;
  4168. se = &sit_i->sentries[start];
  4169. folio = get_current_sit_folio(sbi, start);
  4170. if (IS_ERR(folio))
  4171. return PTR_ERR(folio);
  4172. sit_blk = folio_address(folio);
  4173. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  4174. f2fs_folio_put(folio, true);
  4175. err = check_block_count(sbi, start, &sit);
  4176. if (err)
  4177. return err;
  4178. seg_info_from_raw_sit(se, &sit);
  4179. if (se->type >= NR_PERSISTENT_LOG) {
  4180. f2fs_err(sbi, "Invalid segment type: %u, segno: %u",
  4181. se->type, start);
  4182. f2fs_handle_error(sbi,
  4183. ERROR_INCONSISTENT_SUM_TYPE);
  4184. return -EFSCORRUPTED;
  4185. }
  4186. sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
  4187. if (!f2fs_block_unit_discard(sbi))
  4188. goto init_discard_map_done;
  4189. /* build discard map only one time */
  4190. if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
  4191. memset(se->discard_map, 0xff,
  4192. SIT_VBLOCK_MAP_SIZE);
  4193. goto init_discard_map_done;
  4194. }
  4195. memcpy(se->discard_map, se->cur_valid_map,
  4196. SIT_VBLOCK_MAP_SIZE);
  4197. sbi->discard_blks += BLKS_PER_SEG(sbi) -
  4198. se->valid_blocks;
  4199. init_discard_map_done:
  4200. if (__is_large_section(sbi))
  4201. get_sec_entry(sbi, start)->valid_blocks +=
  4202. se->valid_blocks;
  4203. }
  4204. start_blk += readed;
  4205. } while (start_blk < sit_blk_cnt);
  4206. down_read(&curseg->journal_rwsem);
  4207. for (i = 0; i < sits_in_cursum(journal); i++) {
  4208. unsigned int old_valid_blocks;
  4209. start = le32_to_cpu(segno_in_journal(journal, i));
  4210. if (start >= MAIN_SEGS(sbi)) {
  4211. f2fs_err(sbi, "Wrong journal entry on segno %u",
  4212. start);
  4213. err = -EFSCORRUPTED;
  4214. f2fs_handle_error(sbi, ERROR_CORRUPTED_JOURNAL);
  4215. break;
  4216. }
  4217. se = &sit_i->sentries[start];
  4218. sit = sit_in_journal(journal, i);
  4219. old_valid_blocks = se->valid_blocks;
  4220. sit_valid_blocks[SE_PAGETYPE(se)] -= old_valid_blocks;
  4221. err = check_block_count(sbi, start, &sit);
  4222. if (err)
  4223. break;
  4224. seg_info_from_raw_sit(se, &sit);
  4225. if (se->type >= NR_PERSISTENT_LOG) {
  4226. f2fs_err(sbi, "Invalid segment type: %u, segno: %u",
  4227. se->type, start);
  4228. err = -EFSCORRUPTED;
  4229. f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE);
  4230. break;
  4231. }
  4232. sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
  4233. if (f2fs_block_unit_discard(sbi)) {
  4234. if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
  4235. memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
  4236. } else {
  4237. memcpy(se->discard_map, se->cur_valid_map,
  4238. SIT_VBLOCK_MAP_SIZE);
  4239. sbi->discard_blks += old_valid_blocks;
  4240. sbi->discard_blks -= se->valid_blocks;
  4241. }
  4242. }
  4243. if (__is_large_section(sbi)) {
  4244. get_sec_entry(sbi, start)->valid_blocks +=
  4245. se->valid_blocks;
  4246. get_sec_entry(sbi, start)->valid_blocks -=
  4247. old_valid_blocks;
  4248. }
  4249. }
  4250. up_read(&curseg->journal_rwsem);
  4251. /* update ckpt_valid_block */
  4252. if (__is_large_section(sbi)) {
  4253. unsigned int segno;
  4254. for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) {
  4255. set_ckpt_valid_blocks(sbi, segno);
  4256. sanity_check_valid_blocks(sbi, segno);
  4257. }
  4258. }
  4259. if (err)
  4260. return err;
  4261. if (sit_valid_blocks[NODE] != valid_node_count(sbi)) {
  4262. f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
  4263. sit_valid_blocks[NODE], valid_node_count(sbi));
  4264. f2fs_handle_error(sbi, ERROR_INCONSISTENT_NODE_COUNT);
  4265. return -EFSCORRUPTED;
  4266. }
  4267. if (sit_valid_blocks[DATA] + sit_valid_blocks[NODE] >
  4268. valid_user_blocks(sbi)) {
  4269. f2fs_err(sbi, "SIT is corrupted data# %u %u vs %u",
  4270. sit_valid_blocks[DATA], sit_valid_blocks[NODE],
  4271. valid_user_blocks(sbi));
  4272. f2fs_handle_error(sbi, ERROR_INCONSISTENT_BLOCK_COUNT);
  4273. return -EFSCORRUPTED;
  4274. }
  4275. return 0;
  4276. }
  4277. static void init_free_segmap(struct f2fs_sb_info *sbi)
  4278. {
  4279. unsigned int start;
  4280. int type;
  4281. struct seg_entry *sentry;
  4282. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  4283. if (f2fs_usable_blks_in_seg(sbi, start) == 0)
  4284. continue;
  4285. sentry = get_seg_entry(sbi, start);
  4286. if (!sentry->valid_blocks)
  4287. __set_free(sbi, start);
  4288. else
  4289. SIT_I(sbi)->written_valid_blocks +=
  4290. sentry->valid_blocks;
  4291. }
  4292. /* set use the current segments */
  4293. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  4294. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  4295. __set_test_and_inuse(sbi, curseg_t->segno);
  4296. }
  4297. }
  4298. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  4299. {
  4300. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  4301. struct free_segmap_info *free_i = FREE_I(sbi);
  4302. unsigned int segno = 0, offset = 0, secno;
  4303. block_t valid_blocks, usable_blks_in_seg;
  4304. while (1) {
  4305. /* find dirty segment based on free segmap */
  4306. segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
  4307. if (segno >= MAIN_SEGS(sbi))
  4308. break;
  4309. offset = segno + 1;
  4310. valid_blocks = get_valid_blocks(sbi, segno, false);
  4311. usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
  4312. if (valid_blocks == usable_blks_in_seg || !valid_blocks)
  4313. continue;
  4314. if (valid_blocks > usable_blks_in_seg) {
  4315. f2fs_bug_on(sbi, 1);
  4316. continue;
  4317. }
  4318. mutex_lock(&dirty_i->seglist_lock);
  4319. __locate_dirty_segment(sbi, segno, DIRTY);
  4320. mutex_unlock(&dirty_i->seglist_lock);
  4321. }
  4322. if (!__is_large_section(sbi))
  4323. return;
  4324. mutex_lock(&dirty_i->seglist_lock);
  4325. for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) {
  4326. valid_blocks = get_valid_blocks(sbi, segno, true);
  4327. secno = GET_SEC_FROM_SEG(sbi, segno);
  4328. if (!valid_blocks || valid_blocks == CAP_BLKS_PER_SEC(sbi))
  4329. continue;
  4330. if (is_cursec(sbi, secno))
  4331. continue;
  4332. set_bit(secno, dirty_i->dirty_secmap);
  4333. }
  4334. mutex_unlock(&dirty_i->seglist_lock);
  4335. }
  4336. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  4337. {
  4338. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  4339. unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  4340. dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
  4341. if (!dirty_i->victim_secmap)
  4342. return -ENOMEM;
  4343. dirty_i->pinned_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
  4344. if (!dirty_i->pinned_secmap)
  4345. return -ENOMEM;
  4346. dirty_i->pinned_secmap_cnt = 0;
  4347. dirty_i->enable_pin_section = true;
  4348. return 0;
  4349. }
  4350. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  4351. {
  4352. struct dirty_seglist_info *dirty_i;
  4353. unsigned int bitmap_size, i;
  4354. /* allocate memory for dirty segments list information */
  4355. dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
  4356. GFP_KERNEL);
  4357. if (!dirty_i)
  4358. return -ENOMEM;
  4359. SM_I(sbi)->dirty_info = dirty_i;
  4360. mutex_init(&dirty_i->seglist_lock);
  4361. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  4362. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  4363. dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
  4364. GFP_KERNEL);
  4365. if (!dirty_i->dirty_segmap[i])
  4366. return -ENOMEM;
  4367. }
  4368. if (__is_large_section(sbi)) {
  4369. bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  4370. dirty_i->dirty_secmap = f2fs_kvzalloc(sbi,
  4371. bitmap_size, GFP_KERNEL);
  4372. if (!dirty_i->dirty_secmap)
  4373. return -ENOMEM;
  4374. }
  4375. init_dirty_segmap(sbi);
  4376. return init_victim_secmap(sbi);
  4377. }
  4378. static int sanity_check_curseg(struct f2fs_sb_info *sbi)
  4379. {
  4380. int i;
  4381. /*
  4382. * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
  4383. * In LFS curseg, all blkaddr after .next_blkoff should be unused.
  4384. */
  4385. for (i = 0; i < NR_PERSISTENT_LOG; i++) {
  4386. struct curseg_info *curseg = CURSEG_I(sbi, i);
  4387. struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
  4388. unsigned int blkofs = curseg->next_blkoff;
  4389. if (f2fs_sb_has_readonly(sbi) &&
  4390. i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
  4391. continue;
  4392. sanity_check_seg_type(sbi, curseg->seg_type);
  4393. if (curseg->alloc_type != LFS && curseg->alloc_type != SSR) {
  4394. f2fs_err(sbi,
  4395. "Current segment has invalid alloc_type:%d",
  4396. curseg->alloc_type);
  4397. f2fs_handle_error(sbi, ERROR_INVALID_CURSEG);
  4398. return -EFSCORRUPTED;
  4399. }
  4400. if (f2fs_test_bit(blkofs, se->cur_valid_map))
  4401. goto out;
  4402. if (curseg->alloc_type == SSR)
  4403. continue;
  4404. for (blkofs += 1; blkofs < BLKS_PER_SEG(sbi); blkofs++) {
  4405. if (!f2fs_test_bit(blkofs, se->cur_valid_map))
  4406. continue;
  4407. out:
  4408. f2fs_err(sbi,
  4409. "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
  4410. i, curseg->segno, curseg->alloc_type,
  4411. curseg->next_blkoff, blkofs);
  4412. f2fs_handle_error(sbi, ERROR_INVALID_CURSEG);
  4413. return -EFSCORRUPTED;
  4414. }
  4415. }
  4416. return 0;
  4417. }
  4418. #ifdef CONFIG_BLK_DEV_ZONED
  4419. static int check_zone_write_pointer(struct f2fs_sb_info *sbi,
  4420. struct f2fs_dev_info *fdev,
  4421. struct blk_zone *zone)
  4422. {
  4423. unsigned int zone_segno;
  4424. block_t zone_block, valid_block_cnt;
  4425. unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
  4426. int ret;
  4427. unsigned int nofs_flags;
  4428. if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
  4429. return 0;
  4430. zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block);
  4431. zone_segno = GET_SEGNO(sbi, zone_block);
  4432. /*
  4433. * Skip check of zones cursegs point to, since
  4434. * fix_curseg_write_pointer() checks them.
  4435. */
  4436. if (zone_segno >= MAIN_SEGS(sbi))
  4437. return 0;
  4438. /*
  4439. * Get # of valid block of the zone.
  4440. */
  4441. valid_block_cnt = get_valid_blocks(sbi, zone_segno, true);
  4442. if (is_cursec(sbi, GET_SEC_FROM_SEG(sbi, zone_segno))) {
  4443. f2fs_notice(sbi, "Open zones: valid block[0x%x,0x%x] cond[%s]",
  4444. zone_segno, valid_block_cnt,
  4445. blk_zone_cond_str(zone->cond));
  4446. return 0;
  4447. }
  4448. if ((!valid_block_cnt && zone->cond == BLK_ZONE_COND_EMPTY) ||
  4449. (valid_block_cnt && zone->cond == BLK_ZONE_COND_FULL))
  4450. return 0;
  4451. if (!valid_block_cnt) {
  4452. f2fs_notice(sbi, "Zone without valid block has non-zero write "
  4453. "pointer. Reset the write pointer: cond[%s]",
  4454. blk_zone_cond_str(zone->cond));
  4455. ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block,
  4456. zone->len >> log_sectors_per_block);
  4457. if (ret)
  4458. f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
  4459. fdev->path, ret);
  4460. return ret;
  4461. }
  4462. /*
  4463. * If there are valid blocks and the write pointer doesn't match
  4464. * with them, we need to report the inconsistency and fill
  4465. * the zone till the end to close the zone. This inconsistency
  4466. * does not cause write error because the zone will not be
  4467. * selected for write operation until it get discarded.
  4468. */
  4469. f2fs_notice(sbi, "Valid blocks are not aligned with write "
  4470. "pointer: valid block[0x%x,0x%x] cond[%s]",
  4471. zone_segno, valid_block_cnt, blk_zone_cond_str(zone->cond));
  4472. nofs_flags = memalloc_nofs_save();
  4473. ret = blkdev_zone_mgmt(fdev->bdev, REQ_OP_ZONE_FINISH,
  4474. zone->start, zone->len);
  4475. memalloc_nofs_restore(nofs_flags);
  4476. if (ret == -EOPNOTSUPP) {
  4477. ret = blkdev_issue_zeroout(fdev->bdev, zone->wp,
  4478. zone->len - (zone->wp - zone->start),
  4479. GFP_NOFS, 0);
  4480. if (ret)
  4481. f2fs_err(sbi, "Fill up zone failed: %s (errno=%d)",
  4482. fdev->path, ret);
  4483. } else if (ret) {
  4484. f2fs_err(sbi, "Finishing zone failed: %s (errno=%d)",
  4485. fdev->path, ret);
  4486. }
  4487. return ret;
  4488. }
  4489. static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi,
  4490. block_t zone_blkaddr)
  4491. {
  4492. int i;
  4493. for (i = 0; i < sbi->s_ndevs; i++) {
  4494. if (!bdev_is_zoned(FDEV(i).bdev))
  4495. continue;
  4496. if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr &&
  4497. zone_blkaddr <= FDEV(i).end_blk))
  4498. return &FDEV(i);
  4499. }
  4500. return NULL;
  4501. }
  4502. static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
  4503. void *data)
  4504. {
  4505. memcpy(data, zone, sizeof(struct blk_zone));
  4506. return 0;
  4507. }
  4508. static int do_fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
  4509. {
  4510. struct curseg_info *cs = CURSEG_I(sbi, type);
  4511. struct f2fs_dev_info *zbd;
  4512. struct blk_zone zone;
  4513. unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off;
  4514. block_t cs_zone_block, wp_block;
  4515. unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
  4516. sector_t zone_sector;
  4517. int err;
  4518. cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
  4519. cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
  4520. zbd = get_target_zoned_dev(sbi, cs_zone_block);
  4521. if (!zbd)
  4522. return 0;
  4523. /* report zone for the sector the curseg points to */
  4524. zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
  4525. << log_sectors_per_block;
  4526. err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
  4527. report_one_zone_cb, &zone);
  4528. if (err != 1) {
  4529. f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
  4530. zbd->path, err);
  4531. return err;
  4532. }
  4533. if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
  4534. return 0;
  4535. /*
  4536. * When safely unmounted in the previous mount, we could use current
  4537. * segments. Otherwise, allocate new sections.
  4538. */
  4539. if (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  4540. wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block);
  4541. wp_segno = GET_SEGNO(sbi, wp_block);
  4542. wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
  4543. wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0);
  4544. if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff &&
  4545. wp_sector_off == 0)
  4546. return 0;
  4547. f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: "
  4548. "curseg[0x%x,0x%x] wp[0x%x,0x%x]", type, cs->segno,
  4549. cs->next_blkoff, wp_segno, wp_blkoff);
  4550. }
  4551. /* Allocate a new section if it's not new. */
  4552. if (cs->next_blkoff ||
  4553. cs->segno != GET_SEG_FROM_SEC(sbi, GET_ZONE_FROM_SEC(sbi, cs_section))) {
  4554. unsigned int old_segno = cs->segno, old_blkoff = cs->next_blkoff;
  4555. f2fs_allocate_new_section(sbi, type, true);
  4556. f2fs_notice(sbi, "Assign new section to curseg[%d]: "
  4557. "[0x%x,0x%x] -> [0x%x,0x%x]",
  4558. type, old_segno, old_blkoff,
  4559. cs->segno, cs->next_blkoff);
  4560. }
  4561. /* check consistency of the zone curseg pointed to */
  4562. if (check_zone_write_pointer(sbi, zbd, &zone))
  4563. return -EIO;
  4564. /* check newly assigned zone */
  4565. cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
  4566. cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
  4567. zbd = get_target_zoned_dev(sbi, cs_zone_block);
  4568. if (!zbd)
  4569. return 0;
  4570. zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
  4571. << log_sectors_per_block;
  4572. err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
  4573. report_one_zone_cb, &zone);
  4574. if (err != 1) {
  4575. f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
  4576. zbd->path, err);
  4577. return err;
  4578. }
  4579. if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
  4580. return 0;
  4581. if (zone.wp != zone.start) {
  4582. f2fs_notice(sbi,
  4583. "New zone for curseg[%d] is not yet discarded. "
  4584. "Reset the zone: curseg[0x%x,0x%x]",
  4585. type, cs->segno, cs->next_blkoff);
  4586. err = __f2fs_issue_discard_zone(sbi, zbd->bdev, cs_zone_block,
  4587. zone.len >> log_sectors_per_block);
  4588. if (err) {
  4589. f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
  4590. zbd->path, err);
  4591. return err;
  4592. }
  4593. }
  4594. return 0;
  4595. }
  4596. static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
  4597. {
  4598. int i, ret;
  4599. for (i = 0; i < NR_PERSISTENT_LOG; i++) {
  4600. ret = do_fix_curseg_write_pointer(sbi, i);
  4601. if (ret)
  4602. return ret;
  4603. }
  4604. return 0;
  4605. }
  4606. struct check_zone_write_pointer_args {
  4607. struct f2fs_sb_info *sbi;
  4608. struct f2fs_dev_info *fdev;
  4609. };
  4610. static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx,
  4611. void *data)
  4612. {
  4613. struct check_zone_write_pointer_args *args;
  4614. args = (struct check_zone_write_pointer_args *)data;
  4615. return check_zone_write_pointer(args->sbi, args->fdev, zone);
  4616. }
  4617. static int check_write_pointer(struct f2fs_sb_info *sbi)
  4618. {
  4619. int i, ret;
  4620. struct check_zone_write_pointer_args args;
  4621. for (i = 0; i < sbi->s_ndevs; i++) {
  4622. if (!bdev_is_zoned(FDEV(i).bdev))
  4623. continue;
  4624. args.sbi = sbi;
  4625. args.fdev = &FDEV(i);
  4626. ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES,
  4627. check_zone_write_pointer_cb, &args);
  4628. if (ret < 0)
  4629. return ret;
  4630. }
  4631. return 0;
  4632. }
  4633. int f2fs_check_and_fix_write_pointer(struct f2fs_sb_info *sbi)
  4634. {
  4635. int ret;
  4636. if (!f2fs_sb_has_blkzoned(sbi) || f2fs_readonly(sbi->sb) ||
  4637. f2fs_hw_is_readonly(sbi))
  4638. return 0;
  4639. f2fs_notice(sbi, "Checking entire write pointers");
  4640. ret = fix_curseg_write_pointer(sbi);
  4641. if (!ret)
  4642. ret = check_write_pointer(sbi);
  4643. return ret;
  4644. }
  4645. /*
  4646. * Return the number of usable blocks in a segment. The number of blocks
  4647. * returned is always equal to the number of blocks in a segment for
  4648. * segments fully contained within a sequential zone capacity or a
  4649. * conventional zone. For segments partially contained in a sequential
  4650. * zone capacity, the number of usable blocks up to the zone capacity
  4651. * is returned. 0 is returned in all other cases.
  4652. */
  4653. static inline unsigned int f2fs_usable_zone_blks_in_seg(
  4654. struct f2fs_sb_info *sbi, unsigned int segno)
  4655. {
  4656. block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr;
  4657. unsigned int secno;
  4658. if (!sbi->unusable_blocks_per_sec)
  4659. return BLKS_PER_SEG(sbi);
  4660. secno = GET_SEC_FROM_SEG(sbi, segno);
  4661. seg_start = START_BLOCK(sbi, segno);
  4662. sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
  4663. sec_cap_blkaddr = sec_start_blkaddr + CAP_BLKS_PER_SEC(sbi);
  4664. /*
  4665. * If segment starts before zone capacity and spans beyond
  4666. * zone capacity, then usable blocks are from seg start to
  4667. * zone capacity. If the segment starts after the zone capacity,
  4668. * then there are no usable blocks.
  4669. */
  4670. if (seg_start >= sec_cap_blkaddr)
  4671. return 0;
  4672. if (seg_start + BLKS_PER_SEG(sbi) > sec_cap_blkaddr)
  4673. return sec_cap_blkaddr - seg_start;
  4674. return BLKS_PER_SEG(sbi);
  4675. }
  4676. #else
  4677. int f2fs_check_and_fix_write_pointer(struct f2fs_sb_info *sbi)
  4678. {
  4679. return 0;
  4680. }
  4681. static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi,
  4682. unsigned int segno)
  4683. {
  4684. return 0;
  4685. }
  4686. #endif
  4687. unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
  4688. unsigned int segno)
  4689. {
  4690. if (f2fs_sb_has_blkzoned(sbi))
  4691. return f2fs_usable_zone_blks_in_seg(sbi, segno);
  4692. return BLKS_PER_SEG(sbi);
  4693. }
  4694. unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi)
  4695. {
  4696. if (f2fs_sb_has_blkzoned(sbi))
  4697. return CAP_SEGS_PER_SEC(sbi);
  4698. return SEGS_PER_SEC(sbi);
  4699. }
  4700. unsigned long long f2fs_get_section_mtime(struct f2fs_sb_info *sbi,
  4701. unsigned int segno)
  4702. {
  4703. unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi);
  4704. unsigned int secno = 0, start = 0;
  4705. unsigned int total_valid_blocks = 0;
  4706. unsigned long long mtime = 0;
  4707. unsigned int i = 0;
  4708. secno = GET_SEC_FROM_SEG(sbi, segno);
  4709. start = GET_SEG_FROM_SEC(sbi, secno);
  4710. if (!__is_large_section(sbi)) {
  4711. mtime = get_seg_entry(sbi, start + i)->mtime;
  4712. goto out;
  4713. }
  4714. for (i = 0; i < usable_segs_per_sec; i++) {
  4715. /* for large section, only check the mtime of valid segments */
  4716. struct seg_entry *se = get_seg_entry(sbi, start+i);
  4717. mtime += se->mtime * se->valid_blocks;
  4718. total_valid_blocks += se->valid_blocks;
  4719. }
  4720. if (total_valid_blocks == 0)
  4721. return INVALID_MTIME;
  4722. mtime = div_u64(mtime, total_valid_blocks);
  4723. out:
  4724. if (unlikely(mtime == INVALID_MTIME))
  4725. mtime -= 1;
  4726. return mtime;
  4727. }
  4728. /*
  4729. * Update min, max modified time for cost-benefit GC algorithm
  4730. */
  4731. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  4732. {
  4733. struct sit_info *sit_i = SIT_I(sbi);
  4734. unsigned int segno;
  4735. down_write(&sit_i->sentry_lock);
  4736. sit_i->min_mtime = ULLONG_MAX;
  4737. for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) {
  4738. unsigned long long mtime = 0;
  4739. mtime = f2fs_get_section_mtime(sbi, segno);
  4740. if (sit_i->min_mtime > mtime)
  4741. sit_i->min_mtime = mtime;
  4742. }
  4743. sit_i->max_mtime = get_mtime(sbi, false);
  4744. sit_i->dirty_max_mtime = 0;
  4745. up_write(&sit_i->sentry_lock);
  4746. }
  4747. int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
  4748. {
  4749. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  4750. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  4751. struct f2fs_sm_info *sm_info;
  4752. int err;
  4753. sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
  4754. if (!sm_info)
  4755. return -ENOMEM;
  4756. /* init sm info */
  4757. sbi->sm_info = sm_info;
  4758. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  4759. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  4760. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  4761. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  4762. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  4763. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  4764. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  4765. sm_info->rec_prefree_segments = sm_info->main_segments *
  4766. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  4767. if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
  4768. sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
  4769. if (!f2fs_lfs_mode(sbi))
  4770. sm_info->ipu_policy = BIT(F2FS_IPU_FSYNC);
  4771. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  4772. sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
  4773. sm_info->min_seq_blocks = BLKS_PER_SEG(sbi);
  4774. sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
  4775. sm_info->min_ssr_sections = reserved_sections(sbi);
  4776. INIT_LIST_HEAD(&sm_info->sit_entry_set);
  4777. init_f2fs_rwsem(&sm_info->curseg_lock);
  4778. err = f2fs_create_flush_cmd_control(sbi);
  4779. if (err)
  4780. return err;
  4781. err = create_discard_cmd_control(sbi);
  4782. if (err)
  4783. return err;
  4784. err = build_sit_info(sbi);
  4785. if (err)
  4786. return err;
  4787. err = build_free_segmap(sbi);
  4788. if (err)
  4789. return err;
  4790. err = build_curseg(sbi);
  4791. if (err)
  4792. return err;
  4793. /* reinit free segmap based on SIT */
  4794. err = build_sit_entries(sbi);
  4795. if (err)
  4796. return err;
  4797. init_free_segmap(sbi);
  4798. err = build_dirty_segmap(sbi);
  4799. if (err)
  4800. return err;
  4801. err = sanity_check_curseg(sbi);
  4802. if (err)
  4803. return err;
  4804. init_min_max_mtime(sbi);
  4805. return 0;
  4806. }
  4807. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  4808. enum dirty_type dirty_type)
  4809. {
  4810. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  4811. mutex_lock(&dirty_i->seglist_lock);
  4812. kvfree(dirty_i->dirty_segmap[dirty_type]);
  4813. dirty_i->nr_dirty[dirty_type] = 0;
  4814. mutex_unlock(&dirty_i->seglist_lock);
  4815. }
  4816. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  4817. {
  4818. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  4819. kvfree(dirty_i->pinned_secmap);
  4820. kvfree(dirty_i->victim_secmap);
  4821. }
  4822. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  4823. {
  4824. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  4825. int i;
  4826. if (!dirty_i)
  4827. return;
  4828. /* discard pre-free/dirty segments list */
  4829. for (i = 0; i < NR_DIRTY_TYPE; i++)
  4830. discard_dirty_segmap(sbi, i);
  4831. if (__is_large_section(sbi)) {
  4832. mutex_lock(&dirty_i->seglist_lock);
  4833. kvfree(dirty_i->dirty_secmap);
  4834. mutex_unlock(&dirty_i->seglist_lock);
  4835. }
  4836. destroy_victim_secmap(sbi);
  4837. SM_I(sbi)->dirty_info = NULL;
  4838. kfree(dirty_i);
  4839. }
  4840. static void destroy_curseg(struct f2fs_sb_info *sbi)
  4841. {
  4842. struct curseg_info *array = SM_I(sbi)->curseg_array;
  4843. int i;
  4844. if (!array)
  4845. return;
  4846. SM_I(sbi)->curseg_array = NULL;
  4847. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  4848. kfree(array[i].sum_blk);
  4849. kfree(array[i].journal);
  4850. }
  4851. kfree(array);
  4852. }
  4853. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  4854. {
  4855. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  4856. if (!free_i)
  4857. return;
  4858. SM_I(sbi)->free_info = NULL;
  4859. kvfree(free_i->free_segmap);
  4860. kvfree(free_i->free_secmap);
  4861. kfree(free_i);
  4862. }
  4863. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  4864. {
  4865. struct sit_info *sit_i = SIT_I(sbi);
  4866. if (!sit_i)
  4867. return;
  4868. if (sit_i->sentries)
  4869. kvfree(sit_i->bitmap);
  4870. kfree(sit_i->tmp_map);
  4871. kvfree(sit_i->sentries);
  4872. kvfree(sit_i->sec_entries);
  4873. kvfree(sit_i->dirty_sentries_bitmap);
  4874. SM_I(sbi)->sit_info = NULL;
  4875. kfree(sit_i->sit_bitmap);
  4876. #ifdef CONFIG_F2FS_CHECK_FS
  4877. kfree(sit_i->sit_bitmap_mir);
  4878. kvfree(sit_i->invalid_segmap);
  4879. #endif
  4880. kfree(sit_i);
  4881. }
  4882. void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
  4883. {
  4884. struct f2fs_sm_info *sm_info = SM_I(sbi);
  4885. if (!sm_info)
  4886. return;
  4887. f2fs_destroy_flush_cmd_control(sbi, true);
  4888. destroy_discard_cmd_control(sbi);
  4889. destroy_dirty_segmap(sbi);
  4890. destroy_curseg(sbi);
  4891. destroy_free_segmap(sbi);
  4892. destroy_sit_info(sbi);
  4893. sbi->sm_info = NULL;
  4894. kfree(sm_info);
  4895. }
  4896. int __init f2fs_create_segment_manager_caches(void)
  4897. {
  4898. discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry",
  4899. sizeof(struct discard_entry));
  4900. if (!discard_entry_slab)
  4901. goto fail;
  4902. discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd",
  4903. sizeof(struct discard_cmd));
  4904. if (!discard_cmd_slab)
  4905. goto destroy_discard_entry;
  4906. sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set",
  4907. sizeof(struct sit_entry_set));
  4908. if (!sit_entry_set_slab)
  4909. goto destroy_discard_cmd;
  4910. revoke_entry_slab = f2fs_kmem_cache_create("f2fs_revoke_entry",
  4911. sizeof(struct revoke_entry));
  4912. if (!revoke_entry_slab)
  4913. goto destroy_sit_entry_set;
  4914. return 0;
  4915. destroy_sit_entry_set:
  4916. kmem_cache_destroy(sit_entry_set_slab);
  4917. destroy_discard_cmd:
  4918. kmem_cache_destroy(discard_cmd_slab);
  4919. destroy_discard_entry:
  4920. kmem_cache_destroy(discard_entry_slab);
  4921. fail:
  4922. return -ENOMEM;
  4923. }
  4924. void f2fs_destroy_segment_manager_caches(void)
  4925. {
  4926. kmem_cache_destroy(sit_entry_set_slab);
  4927. kmem_cache_destroy(discard_cmd_slab);
  4928. kmem_cache_destroy(discard_entry_slab);
  4929. kmem_cache_destroy(revoke_entry_slab);
  4930. }