extent-tree.c 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/sched/signal.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/writeback.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/sort.h>
  11. #include <linux/rcupdate.h>
  12. #include <linux/kthread.h>
  13. #include <linux/slab.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/percpu_counter.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/crc32c.h>
  18. #include "ctree.h"
  19. #include "extent-tree.h"
  20. #include "transaction.h"
  21. #include "disk-io.h"
  22. #include "print-tree.h"
  23. #include "volumes.h"
  24. #include "raid56.h"
  25. #include "locking.h"
  26. #include "free-space-cache.h"
  27. #include "free-space-tree.h"
  28. #include "qgroup.h"
  29. #include "ref-verify.h"
  30. #include "space-info.h"
  31. #include "block-rsv.h"
  32. #include "discard.h"
  33. #include "zoned.h"
  34. #include "dev-replace.h"
  35. #include "fs.h"
  36. #include "accessors.h"
  37. #include "root-tree.h"
  38. #include "file-item.h"
  39. #include "orphan.h"
  40. #include "tree-checker.h"
  41. #include "raid-stripe-tree.h"
  42. #include "delayed-inode.h"
  43. #include "relocation.h"
  44. #undef SCRAMBLE_DELAYED_REFS
  45. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  46. struct btrfs_delayed_ref_head *href,
  47. const struct btrfs_delayed_ref_node *node,
  48. struct btrfs_delayed_extent_op *extra_op);
  49. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  50. struct extent_buffer *leaf,
  51. struct btrfs_extent_item *ei);
  52. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  53. u64 parent, u64 root_objectid,
  54. u64 flags, u64 owner, u64 offset,
  55. struct btrfs_key *ins, int ref_mod, u64 oref_root);
  56. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  57. const struct btrfs_delayed_ref_node *node,
  58. struct btrfs_delayed_extent_op *extent_op);
  59. static int find_next_key(const struct btrfs_path *path, int level,
  60. struct btrfs_key *key);
  61. static int block_group_bits(const struct btrfs_block_group *cache, u64 bits)
  62. {
  63. return (cache->flags & bits) == bits;
  64. }
  65. /* simple helper to search for an existing data extent at a given offset */
  66. int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
  67. {
  68. struct btrfs_root *root = btrfs_extent_root(fs_info, start);
  69. struct btrfs_key key;
  70. BTRFS_PATH_AUTO_FREE(path);
  71. if (unlikely(!root)) {
  72. btrfs_err(fs_info,
  73. "missing extent root for extent at bytenr %llu", start);
  74. return -EUCLEAN;
  75. }
  76. path = btrfs_alloc_path();
  77. if (!path)
  78. return -ENOMEM;
  79. key.objectid = start;
  80. key.type = BTRFS_EXTENT_ITEM_KEY;
  81. key.offset = len;
  82. return btrfs_search_slot(NULL, root, &key, path, 0, 0);
  83. }
  84. /*
  85. * helper function to lookup reference count and flags of a tree block.
  86. *
  87. * the head node for delayed ref is used to store the sum of all the
  88. * reference count modifications queued up in the rbtree. the head
  89. * node may also store the extent flags to set. This way you can check
  90. * to see what the reference count and extent flags would be if all of
  91. * the delayed refs are not processed.
  92. */
  93. int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
  94. struct btrfs_fs_info *fs_info, u64 bytenr,
  95. u64 offset, int metadata, u64 *refs, u64 *flags,
  96. u64 *owning_root)
  97. {
  98. struct btrfs_root *extent_root;
  99. struct btrfs_delayed_ref_head *head;
  100. struct btrfs_delayed_ref_root *delayed_refs;
  101. BTRFS_PATH_AUTO_FREE(path);
  102. struct btrfs_key key;
  103. u64 num_refs;
  104. u64 extent_flags;
  105. u64 owner = 0;
  106. int ret;
  107. /*
  108. * If we don't have skinny metadata, don't bother doing anything
  109. * different
  110. */
  111. if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
  112. offset = fs_info->nodesize;
  113. metadata = 0;
  114. }
  115. path = btrfs_alloc_path();
  116. if (!path)
  117. return -ENOMEM;
  118. search_again:
  119. key.objectid = bytenr;
  120. if (metadata)
  121. key.type = BTRFS_METADATA_ITEM_KEY;
  122. else
  123. key.type = BTRFS_EXTENT_ITEM_KEY;
  124. key.offset = offset;
  125. extent_root = btrfs_extent_root(fs_info, bytenr);
  126. if (unlikely(!extent_root)) {
  127. btrfs_err(fs_info,
  128. "missing extent root for extent at bytenr %llu", bytenr);
  129. return -EUCLEAN;
  130. }
  131. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  132. if (ret < 0)
  133. return ret;
  134. if (ret > 0 && key.type == BTRFS_METADATA_ITEM_KEY) {
  135. if (path->slots[0]) {
  136. path->slots[0]--;
  137. btrfs_item_key_to_cpu(path->nodes[0], &key,
  138. path->slots[0]);
  139. if (key.objectid == bytenr &&
  140. key.type == BTRFS_EXTENT_ITEM_KEY &&
  141. key.offset == fs_info->nodesize)
  142. ret = 0;
  143. }
  144. }
  145. if (ret == 0) {
  146. struct extent_buffer *leaf = path->nodes[0];
  147. struct btrfs_extent_item *ei;
  148. const u32 item_size = btrfs_item_size(leaf, path->slots[0]);
  149. if (unlikely(item_size < sizeof(*ei))) {
  150. ret = -EUCLEAN;
  151. btrfs_err(fs_info,
  152. "unexpected extent item size, has %u expect >= %zu",
  153. item_size, sizeof(*ei));
  154. btrfs_abort_transaction(trans, ret);
  155. return ret;
  156. }
  157. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  158. num_refs = btrfs_extent_refs(leaf, ei);
  159. if (unlikely(num_refs == 0)) {
  160. ret = -EUCLEAN;
  161. btrfs_err(fs_info,
  162. "unexpected zero reference count for extent item " BTRFS_KEY_FMT,
  163. BTRFS_KEY_FMT_VALUE(&key));
  164. btrfs_abort_transaction(trans, ret);
  165. return ret;
  166. }
  167. extent_flags = btrfs_extent_flags(leaf, ei);
  168. owner = btrfs_get_extent_owner_root(fs_info, leaf, path->slots[0]);
  169. } else {
  170. num_refs = 0;
  171. extent_flags = 0;
  172. ret = 0;
  173. }
  174. delayed_refs = &trans->transaction->delayed_refs;
  175. spin_lock(&delayed_refs->lock);
  176. head = btrfs_find_delayed_ref_head(fs_info, delayed_refs, bytenr);
  177. if (head) {
  178. if (!mutex_trylock(&head->mutex)) {
  179. refcount_inc(&head->refs);
  180. spin_unlock(&delayed_refs->lock);
  181. btrfs_release_path(path);
  182. /*
  183. * Mutex was contended, block until it's released and try
  184. * again
  185. */
  186. mutex_lock(&head->mutex);
  187. mutex_unlock(&head->mutex);
  188. btrfs_put_delayed_ref_head(head);
  189. goto search_again;
  190. }
  191. spin_lock(&head->lock);
  192. if (head->extent_op && head->extent_op->update_flags)
  193. extent_flags |= head->extent_op->flags_to_set;
  194. num_refs += head->ref_mod;
  195. spin_unlock(&head->lock);
  196. mutex_unlock(&head->mutex);
  197. }
  198. spin_unlock(&delayed_refs->lock);
  199. WARN_ON(num_refs == 0);
  200. if (refs)
  201. *refs = num_refs;
  202. if (flags)
  203. *flags = extent_flags;
  204. if (owning_root)
  205. *owning_root = owner;
  206. return ret;
  207. }
  208. /*
  209. * Back reference rules. Back refs have three main goals:
  210. *
  211. * 1) differentiate between all holders of references to an extent so that
  212. * when a reference is dropped we can make sure it was a valid reference
  213. * before freeing the extent.
  214. *
  215. * 2) Provide enough information to quickly find the holders of an extent
  216. * if we notice a given block is corrupted or bad.
  217. *
  218. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  219. * maintenance. This is actually the same as #2, but with a slightly
  220. * different use case.
  221. *
  222. * There are two kinds of back refs. The implicit back refs is optimized
  223. * for pointers in non-shared tree blocks. For a given pointer in a block,
  224. * back refs of this kind provide information about the block's owner tree
  225. * and the pointer's key. These information allow us to find the block by
  226. * b-tree searching. The full back refs is for pointers in tree blocks not
  227. * referenced by their owner trees. The location of tree block is recorded
  228. * in the back refs. Actually the full back refs is generic, and can be
  229. * used in all cases the implicit back refs is used. The major shortcoming
  230. * of the full back refs is its overhead. Every time a tree block gets
  231. * COWed, we have to update back refs entry for all pointers in it.
  232. *
  233. * For a newly allocated tree block, we use implicit back refs for
  234. * pointers in it. This means most tree related operations only involve
  235. * implicit back refs. For a tree block created in old transaction, the
  236. * only way to drop a reference to it is COW it. So we can detect the
  237. * event that tree block loses its owner tree's reference and do the
  238. * back refs conversion.
  239. *
  240. * When a tree block is COWed through a tree, there are four cases:
  241. *
  242. * The reference count of the block is one and the tree is the block's
  243. * owner tree. Nothing to do in this case.
  244. *
  245. * The reference count of the block is one and the tree is not the
  246. * block's owner tree. In this case, full back refs is used for pointers
  247. * in the block. Remove these full back refs, add implicit back refs for
  248. * every pointers in the new block.
  249. *
  250. * The reference count of the block is greater than one and the tree is
  251. * the block's owner tree. In this case, implicit back refs is used for
  252. * pointers in the block. Add full back refs for every pointers in the
  253. * block, increase lower level extents' reference counts. The original
  254. * implicit back refs are entailed to the new block.
  255. *
  256. * The reference count of the block is greater than one and the tree is
  257. * not the block's owner tree. Add implicit back refs for every pointer in
  258. * the new block, increase lower level extents' reference count.
  259. *
  260. * Back Reference Key composing:
  261. *
  262. * The key objectid corresponds to the first byte in the extent,
  263. * The key type is used to differentiate between types of back refs.
  264. * There are different meanings of the key offset for different types
  265. * of back refs.
  266. *
  267. * File extents can be referenced by:
  268. *
  269. * - multiple snapshots, subvolumes, or different generations in one subvol
  270. * - different files inside a single subvolume
  271. * - different offsets inside a file (bookend extents in file.c)
  272. *
  273. * The extent ref structure for the implicit back refs has fields for:
  274. *
  275. * - Objectid of the subvolume root
  276. * - objectid of the file holding the reference
  277. * - original offset in the file
  278. * - how many bookend extents
  279. *
  280. * The key offset for the implicit back refs is hash of the first
  281. * three fields.
  282. *
  283. * The extent ref structure for the full back refs has field for:
  284. *
  285. * - number of pointers in the tree leaf
  286. *
  287. * The key offset for the implicit back refs is the first byte of
  288. * the tree leaf
  289. *
  290. * When a file extent is allocated, The implicit back refs is used.
  291. * the fields are filled in:
  292. *
  293. * (root_key.objectid, inode objectid, offset in file, 1)
  294. *
  295. * When a file extent is removed file truncation, we find the
  296. * corresponding implicit back refs and check the following fields:
  297. *
  298. * (btrfs_header_owner(leaf), inode objectid, offset in file)
  299. *
  300. * Btree extents can be referenced by:
  301. *
  302. * - Different subvolumes
  303. *
  304. * Both the implicit back refs and the full back refs for tree blocks
  305. * only consist of key. The key offset for the implicit back refs is
  306. * objectid of block's owner tree. The key offset for the full back refs
  307. * is the first byte of parent block.
  308. *
  309. * When implicit back refs is used, information about the lowest key and
  310. * level of the tree block are required. These information are stored in
  311. * tree block info structure.
  312. */
  313. /*
  314. * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
  315. * is_data == BTRFS_REF_TYPE_DATA, data type is required,
  316. * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
  317. */
  318. int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
  319. const struct btrfs_extent_inline_ref *iref,
  320. enum btrfs_inline_ref_type is_data)
  321. {
  322. struct btrfs_fs_info *fs_info = eb->fs_info;
  323. int type = btrfs_extent_inline_ref_type(eb, iref);
  324. u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
  325. if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
  326. ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
  327. return type;
  328. }
  329. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  330. type == BTRFS_SHARED_BLOCK_REF_KEY ||
  331. type == BTRFS_SHARED_DATA_REF_KEY ||
  332. type == BTRFS_EXTENT_DATA_REF_KEY) {
  333. if (is_data == BTRFS_REF_TYPE_BLOCK) {
  334. if (type == BTRFS_TREE_BLOCK_REF_KEY)
  335. return type;
  336. if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
  337. ASSERT(fs_info);
  338. /*
  339. * Every shared one has parent tree block,
  340. * which must be aligned to sector size.
  341. */
  342. if (offset && IS_ALIGNED(offset, fs_info->sectorsize))
  343. return type;
  344. }
  345. } else if (is_data == BTRFS_REF_TYPE_DATA) {
  346. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  347. return type;
  348. if (type == BTRFS_SHARED_DATA_REF_KEY) {
  349. ASSERT(fs_info);
  350. /*
  351. * Every shared one has parent tree block,
  352. * which must be aligned to sector size.
  353. */
  354. if (offset &&
  355. IS_ALIGNED(offset, fs_info->sectorsize))
  356. return type;
  357. }
  358. } else {
  359. ASSERT(is_data == BTRFS_REF_TYPE_ANY);
  360. return type;
  361. }
  362. }
  363. WARN_ON(1);
  364. btrfs_print_leaf(eb);
  365. btrfs_err(fs_info,
  366. "eb %llu iref 0x%lx invalid extent inline ref type %d",
  367. eb->start, (unsigned long)iref, type);
  368. return BTRFS_REF_TYPE_INVALID;
  369. }
  370. u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
  371. {
  372. u32 high_crc = ~(u32)0;
  373. u32 low_crc = ~(u32)0;
  374. __le64 lenum;
  375. lenum = cpu_to_le64(root_objectid);
  376. high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
  377. lenum = cpu_to_le64(owner);
  378. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  379. lenum = cpu_to_le64(offset);
  380. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  381. return ((u64)high_crc << 31) ^ (u64)low_crc;
  382. }
  383. static u64 hash_extent_data_ref_item(const struct extent_buffer *leaf,
  384. const struct btrfs_extent_data_ref *ref)
  385. {
  386. return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
  387. btrfs_extent_data_ref_objectid(leaf, ref),
  388. btrfs_extent_data_ref_offset(leaf, ref));
  389. }
  390. static bool match_extent_data_ref(const struct extent_buffer *leaf,
  391. const struct btrfs_extent_data_ref *ref,
  392. u64 root_objectid, u64 owner, u64 offset)
  393. {
  394. if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
  395. btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
  396. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  397. return false;
  398. return true;
  399. }
  400. static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
  401. struct btrfs_path *path,
  402. u64 bytenr, u64 parent,
  403. u64 root_objectid,
  404. u64 owner, u64 offset)
  405. {
  406. struct btrfs_root *root = btrfs_extent_root(trans->fs_info, bytenr);
  407. struct btrfs_key key;
  408. struct btrfs_extent_data_ref *ref;
  409. struct extent_buffer *leaf;
  410. u32 nritems;
  411. int recow;
  412. int ret;
  413. if (unlikely(!root)) {
  414. btrfs_err(trans->fs_info,
  415. "missing extent root for extent at bytenr %llu", bytenr);
  416. return -EUCLEAN;
  417. }
  418. key.objectid = bytenr;
  419. if (parent) {
  420. key.type = BTRFS_SHARED_DATA_REF_KEY;
  421. key.offset = parent;
  422. } else {
  423. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  424. key.offset = hash_extent_data_ref(root_objectid,
  425. owner, offset);
  426. }
  427. again:
  428. recow = 0;
  429. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  430. if (ret < 0)
  431. return ret;
  432. if (parent) {
  433. if (ret)
  434. return -ENOENT;
  435. return 0;
  436. }
  437. ret = -ENOENT;
  438. leaf = path->nodes[0];
  439. nritems = btrfs_header_nritems(leaf);
  440. while (1) {
  441. if (path->slots[0] >= nritems) {
  442. ret = btrfs_next_leaf(root, path);
  443. if (ret) {
  444. if (ret > 0)
  445. return -ENOENT;
  446. return ret;
  447. }
  448. leaf = path->nodes[0];
  449. nritems = btrfs_header_nritems(leaf);
  450. recow = 1;
  451. }
  452. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  453. if (key.objectid != bytenr ||
  454. key.type != BTRFS_EXTENT_DATA_REF_KEY)
  455. return -ENOENT;
  456. ref = btrfs_item_ptr(leaf, path->slots[0],
  457. struct btrfs_extent_data_ref);
  458. if (match_extent_data_ref(leaf, ref, root_objectid,
  459. owner, offset)) {
  460. if (recow) {
  461. btrfs_release_path(path);
  462. goto again;
  463. }
  464. return 0;
  465. }
  466. path->slots[0]++;
  467. }
  468. return ret;
  469. }
  470. static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
  471. struct btrfs_path *path,
  472. const struct btrfs_delayed_ref_node *node,
  473. u64 bytenr)
  474. {
  475. struct btrfs_root *root = btrfs_extent_root(trans->fs_info, bytenr);
  476. struct btrfs_key key;
  477. struct extent_buffer *leaf;
  478. u64 owner = btrfs_delayed_ref_owner(node);
  479. u64 offset = btrfs_delayed_ref_offset(node);
  480. u32 size;
  481. u32 num_refs;
  482. int ret;
  483. if (unlikely(!root)) {
  484. btrfs_err(trans->fs_info,
  485. "missing extent root for extent at bytenr %llu", bytenr);
  486. return -EUCLEAN;
  487. }
  488. key.objectid = bytenr;
  489. if (node->parent) {
  490. key.type = BTRFS_SHARED_DATA_REF_KEY;
  491. key.offset = node->parent;
  492. size = sizeof(struct btrfs_shared_data_ref);
  493. } else {
  494. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  495. key.offset = hash_extent_data_ref(node->ref_root, owner, offset);
  496. size = sizeof(struct btrfs_extent_data_ref);
  497. }
  498. ret = btrfs_insert_empty_item(trans, root, path, &key, size);
  499. if (ret && ret != -EEXIST)
  500. goto fail;
  501. leaf = path->nodes[0];
  502. if (node->parent) {
  503. struct btrfs_shared_data_ref *ref;
  504. ref = btrfs_item_ptr(leaf, path->slots[0],
  505. struct btrfs_shared_data_ref);
  506. if (ret == 0) {
  507. btrfs_set_shared_data_ref_count(leaf, ref, node->ref_mod);
  508. } else {
  509. num_refs = btrfs_shared_data_ref_count(leaf, ref);
  510. num_refs += node->ref_mod;
  511. btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
  512. }
  513. } else {
  514. struct btrfs_extent_data_ref *ref;
  515. while (ret == -EEXIST) {
  516. ref = btrfs_item_ptr(leaf, path->slots[0],
  517. struct btrfs_extent_data_ref);
  518. if (match_extent_data_ref(leaf, ref, node->ref_root,
  519. owner, offset))
  520. break;
  521. btrfs_release_path(path);
  522. key.offset++;
  523. ret = btrfs_insert_empty_item(trans, root, path, &key,
  524. size);
  525. if (ret && ret != -EEXIST)
  526. goto fail;
  527. leaf = path->nodes[0];
  528. }
  529. ref = btrfs_item_ptr(leaf, path->slots[0],
  530. struct btrfs_extent_data_ref);
  531. if (ret == 0) {
  532. btrfs_set_extent_data_ref_root(leaf, ref, node->ref_root);
  533. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  534. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  535. btrfs_set_extent_data_ref_count(leaf, ref, node->ref_mod);
  536. } else {
  537. num_refs = btrfs_extent_data_ref_count(leaf, ref);
  538. num_refs += node->ref_mod;
  539. btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
  540. }
  541. }
  542. ret = 0;
  543. fail:
  544. btrfs_release_path(path);
  545. return ret;
  546. }
  547. static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
  548. struct btrfs_root *root,
  549. struct btrfs_path *path,
  550. int refs_to_drop)
  551. {
  552. struct btrfs_key key;
  553. struct btrfs_extent_data_ref *ref1 = NULL;
  554. struct btrfs_shared_data_ref *ref2 = NULL;
  555. struct extent_buffer *leaf;
  556. u32 num_refs = 0;
  557. int ret = 0;
  558. leaf = path->nodes[0];
  559. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  560. if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  561. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  562. struct btrfs_extent_data_ref);
  563. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  564. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  565. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  566. struct btrfs_shared_data_ref);
  567. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  568. } else {
  569. btrfs_err(trans->fs_info,
  570. "unrecognized backref key " BTRFS_KEY_FMT,
  571. BTRFS_KEY_FMT_VALUE(&key));
  572. btrfs_abort_transaction(trans, -EUCLEAN);
  573. return -EUCLEAN;
  574. }
  575. BUG_ON(num_refs < refs_to_drop);
  576. num_refs -= refs_to_drop;
  577. if (num_refs == 0) {
  578. ret = btrfs_del_item(trans, root, path);
  579. } else {
  580. if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
  581. btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
  582. else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
  583. btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
  584. }
  585. return ret;
  586. }
  587. static noinline u32 extent_data_ref_count(const struct btrfs_path *path,
  588. const struct btrfs_extent_inline_ref *iref)
  589. {
  590. struct btrfs_key key;
  591. struct extent_buffer *leaf;
  592. const struct btrfs_extent_data_ref *ref1;
  593. const struct btrfs_shared_data_ref *ref2;
  594. u32 num_refs = 0;
  595. int type;
  596. leaf = path->nodes[0];
  597. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  598. if (iref) {
  599. /*
  600. * If type is invalid, we should have bailed out earlier than
  601. * this call.
  602. */
  603. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
  604. ASSERT(type != BTRFS_REF_TYPE_INVALID);
  605. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  606. ref1 = (const struct btrfs_extent_data_ref *)(&iref->offset);
  607. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  608. } else {
  609. ref2 = (const struct btrfs_shared_data_ref *)(iref + 1);
  610. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  611. }
  612. } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  613. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  614. struct btrfs_extent_data_ref);
  615. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  616. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  617. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  618. struct btrfs_shared_data_ref);
  619. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  620. } else {
  621. WARN_ON(1);
  622. }
  623. return num_refs;
  624. }
  625. static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
  626. struct btrfs_path *path,
  627. u64 bytenr, u64 parent,
  628. u64 root_objectid)
  629. {
  630. struct btrfs_root *root = btrfs_extent_root(trans->fs_info, bytenr);
  631. struct btrfs_key key;
  632. int ret;
  633. if (unlikely(!root)) {
  634. btrfs_err(trans->fs_info,
  635. "missing extent root for extent at bytenr %llu", bytenr);
  636. return -EUCLEAN;
  637. }
  638. key.objectid = bytenr;
  639. if (parent) {
  640. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  641. key.offset = parent;
  642. } else {
  643. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  644. key.offset = root_objectid;
  645. }
  646. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  647. if (ret > 0)
  648. ret = -ENOENT;
  649. return ret;
  650. }
  651. static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
  652. struct btrfs_path *path,
  653. const struct btrfs_delayed_ref_node *node,
  654. u64 bytenr)
  655. {
  656. struct btrfs_root *root = btrfs_extent_root(trans->fs_info, bytenr);
  657. struct btrfs_key key;
  658. int ret;
  659. if (unlikely(!root)) {
  660. btrfs_err(trans->fs_info,
  661. "missing extent root for extent at bytenr %llu", bytenr);
  662. return -EUCLEAN;
  663. }
  664. key.objectid = bytenr;
  665. if (node->parent) {
  666. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  667. key.offset = node->parent;
  668. } else {
  669. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  670. key.offset = node->ref_root;
  671. }
  672. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  673. btrfs_release_path(path);
  674. return ret;
  675. }
  676. static inline int extent_ref_type(u64 parent, u64 owner)
  677. {
  678. int type;
  679. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  680. if (parent > 0)
  681. type = BTRFS_SHARED_BLOCK_REF_KEY;
  682. else
  683. type = BTRFS_TREE_BLOCK_REF_KEY;
  684. } else {
  685. if (parent > 0)
  686. type = BTRFS_SHARED_DATA_REF_KEY;
  687. else
  688. type = BTRFS_EXTENT_DATA_REF_KEY;
  689. }
  690. return type;
  691. }
  692. static int find_next_key(const struct btrfs_path *path, int level,
  693. struct btrfs_key *key)
  694. {
  695. for (; level < BTRFS_MAX_LEVEL; level++) {
  696. if (!path->nodes[level])
  697. break;
  698. if (path->slots[level] + 1 >=
  699. btrfs_header_nritems(path->nodes[level]))
  700. continue;
  701. if (level == 0)
  702. btrfs_item_key_to_cpu(path->nodes[level], key,
  703. path->slots[level] + 1);
  704. else
  705. btrfs_node_key_to_cpu(path->nodes[level], key,
  706. path->slots[level] + 1);
  707. return 0;
  708. }
  709. return 1;
  710. }
  711. /*
  712. * look for inline back ref. if back ref is found, *ref_ret is set
  713. * to the address of inline back ref, and 0 is returned.
  714. *
  715. * if back ref isn't found, *ref_ret is set to the address where it
  716. * should be inserted, and -ENOENT is returned.
  717. *
  718. * if insert is true and there are too many inline back refs, the path
  719. * points to the extent item, and -EAGAIN is returned.
  720. *
  721. * NOTE: inline back refs are ordered in the same way that back ref
  722. * items in the tree are ordered.
  723. */
  724. static noinline_for_stack
  725. int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
  726. struct btrfs_path *path,
  727. struct btrfs_extent_inline_ref **ref_ret,
  728. u64 bytenr, u64 num_bytes,
  729. u64 parent, u64 root_objectid,
  730. u64 owner, u64 offset, int insert)
  731. {
  732. struct btrfs_fs_info *fs_info = trans->fs_info;
  733. struct btrfs_root *root = btrfs_extent_root(fs_info, bytenr);
  734. struct btrfs_key key;
  735. struct extent_buffer *leaf;
  736. struct btrfs_extent_item *ei;
  737. struct btrfs_extent_inline_ref *iref;
  738. u64 flags;
  739. u64 item_size;
  740. unsigned long ptr;
  741. unsigned long end;
  742. int extra_size;
  743. int type;
  744. int want;
  745. int ret;
  746. bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  747. int needed;
  748. if (unlikely(!root)) {
  749. btrfs_err(fs_info,
  750. "missing extent root for extent at bytenr %llu", bytenr);
  751. return -EUCLEAN;
  752. }
  753. key.objectid = bytenr;
  754. key.type = BTRFS_EXTENT_ITEM_KEY;
  755. key.offset = num_bytes;
  756. want = extent_ref_type(parent, owner);
  757. if (insert) {
  758. extra_size = btrfs_extent_inline_ref_size(want);
  759. path->search_for_extension = true;
  760. } else
  761. extra_size = -1;
  762. /*
  763. * Owner is our level, so we can just add one to get the level for the
  764. * block we are interested in.
  765. */
  766. if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
  767. key.type = BTRFS_METADATA_ITEM_KEY;
  768. key.offset = owner;
  769. }
  770. again:
  771. ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
  772. if (ret < 0)
  773. goto out;
  774. /*
  775. * We may be a newly converted file system which still has the old fat
  776. * extent entries for metadata, so try and see if we have one of those.
  777. */
  778. if (ret > 0 && skinny_metadata) {
  779. skinny_metadata = false;
  780. if (path->slots[0]) {
  781. path->slots[0]--;
  782. btrfs_item_key_to_cpu(path->nodes[0], &key,
  783. path->slots[0]);
  784. if (key.objectid == bytenr &&
  785. key.type == BTRFS_EXTENT_ITEM_KEY &&
  786. key.offset == num_bytes)
  787. ret = 0;
  788. }
  789. if (ret) {
  790. key.objectid = bytenr;
  791. key.type = BTRFS_EXTENT_ITEM_KEY;
  792. key.offset = num_bytes;
  793. btrfs_release_path(path);
  794. goto again;
  795. }
  796. }
  797. if (ret && !insert) {
  798. ret = -ENOENT;
  799. goto out;
  800. } else if (WARN_ON(ret)) {
  801. btrfs_print_leaf(path->nodes[0]);
  802. btrfs_err(fs_info,
  803. "extent item not found for insert, bytenr %llu num_bytes %llu parent %llu root_objectid %llu owner %llu offset %llu",
  804. bytenr, num_bytes, parent, root_objectid, owner,
  805. offset);
  806. ret = -EUCLEAN;
  807. goto out;
  808. }
  809. leaf = path->nodes[0];
  810. item_size = btrfs_item_size(leaf, path->slots[0]);
  811. if (unlikely(item_size < sizeof(*ei))) {
  812. ret = -EUCLEAN;
  813. btrfs_err(fs_info,
  814. "unexpected extent item size, has %llu expect >= %zu",
  815. item_size, sizeof(*ei));
  816. btrfs_abort_transaction(trans, ret);
  817. goto out;
  818. }
  819. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  820. flags = btrfs_extent_flags(leaf, ei);
  821. ptr = (unsigned long)(ei + 1);
  822. end = (unsigned long)ei + item_size;
  823. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
  824. ptr += sizeof(struct btrfs_tree_block_info);
  825. BUG_ON(ptr > end);
  826. }
  827. if (owner >= BTRFS_FIRST_FREE_OBJECTID)
  828. needed = BTRFS_REF_TYPE_DATA;
  829. else
  830. needed = BTRFS_REF_TYPE_BLOCK;
  831. ret = -ENOENT;
  832. while (ptr < end) {
  833. iref = (struct btrfs_extent_inline_ref *)ptr;
  834. type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
  835. if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
  836. ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
  837. ptr += btrfs_extent_inline_ref_size(type);
  838. continue;
  839. }
  840. if (unlikely(type == BTRFS_REF_TYPE_INVALID)) {
  841. ret = -EUCLEAN;
  842. goto out;
  843. }
  844. if (want < type)
  845. break;
  846. if (want > type) {
  847. ptr += btrfs_extent_inline_ref_size(type);
  848. continue;
  849. }
  850. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  851. struct btrfs_extent_data_ref *dref;
  852. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  853. if (match_extent_data_ref(leaf, dref, root_objectid,
  854. owner, offset)) {
  855. ret = 0;
  856. break;
  857. }
  858. if (hash_extent_data_ref_item(leaf, dref) <
  859. hash_extent_data_ref(root_objectid, owner, offset))
  860. break;
  861. } else {
  862. u64 ref_offset;
  863. ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
  864. if (parent > 0) {
  865. if (parent == ref_offset) {
  866. ret = 0;
  867. break;
  868. }
  869. if (ref_offset < parent)
  870. break;
  871. } else {
  872. if (root_objectid == ref_offset) {
  873. ret = 0;
  874. break;
  875. }
  876. if (ref_offset < root_objectid)
  877. break;
  878. }
  879. }
  880. ptr += btrfs_extent_inline_ref_size(type);
  881. }
  882. if (unlikely(ptr > end)) {
  883. ret = -EUCLEAN;
  884. btrfs_print_leaf(path->nodes[0]);
  885. btrfs_crit(fs_info,
  886. "overrun extent record at slot %d while looking for inline extent for root %llu owner %llu offset %llu parent %llu",
  887. path->slots[0], root_objectid, owner, offset, parent);
  888. goto out;
  889. }
  890. if (ret == -ENOENT && insert) {
  891. if (item_size + extra_size >=
  892. BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
  893. ret = -EAGAIN;
  894. goto out;
  895. }
  896. if (path->slots[0] + 1 < btrfs_header_nritems(path->nodes[0])) {
  897. struct btrfs_key tmp_key;
  898. btrfs_item_key_to_cpu(path->nodes[0], &tmp_key, path->slots[0] + 1);
  899. if (tmp_key.objectid == bytenr &&
  900. tmp_key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
  901. ret = -EAGAIN;
  902. goto out;
  903. }
  904. goto out_no_entry;
  905. }
  906. if (!path->keep_locks) {
  907. btrfs_release_path(path);
  908. path->keep_locks = true;
  909. goto again;
  910. }
  911. /*
  912. * To add new inline back ref, we have to make sure
  913. * there is no corresponding back ref item.
  914. * For simplicity, we just do not add new inline back
  915. * ref if there is any kind of item for this block
  916. */
  917. if (find_next_key(path, 0, &key) == 0 &&
  918. key.objectid == bytenr &&
  919. key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
  920. ret = -EAGAIN;
  921. goto out;
  922. }
  923. }
  924. out_no_entry:
  925. *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
  926. out:
  927. if (path->keep_locks) {
  928. path->keep_locks = false;
  929. btrfs_unlock_up_safe(path, 1);
  930. }
  931. if (insert)
  932. path->search_for_extension = false;
  933. return ret;
  934. }
  935. /*
  936. * helper to add new inline back ref
  937. */
  938. static noinline_for_stack
  939. void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
  940. struct btrfs_path *path,
  941. struct btrfs_extent_inline_ref *iref,
  942. u64 parent, u64 root_objectid,
  943. u64 owner, u64 offset, int refs_to_add,
  944. struct btrfs_delayed_extent_op *extent_op)
  945. {
  946. struct extent_buffer *leaf;
  947. struct btrfs_extent_item *ei;
  948. unsigned long ptr;
  949. unsigned long end;
  950. unsigned long item_offset;
  951. u64 refs;
  952. int size;
  953. int type;
  954. leaf = path->nodes[0];
  955. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  956. item_offset = (unsigned long)iref - (unsigned long)ei;
  957. type = extent_ref_type(parent, owner);
  958. size = btrfs_extent_inline_ref_size(type);
  959. btrfs_extend_item(trans, path, size);
  960. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  961. refs = btrfs_extent_refs(leaf, ei);
  962. refs += refs_to_add;
  963. btrfs_set_extent_refs(leaf, ei, refs);
  964. if (extent_op)
  965. __run_delayed_extent_op(extent_op, leaf, ei);
  966. ptr = (unsigned long)ei + item_offset;
  967. end = (unsigned long)ei + btrfs_item_size(leaf, path->slots[0]);
  968. if (ptr < end - size)
  969. memmove_extent_buffer(leaf, ptr + size, ptr,
  970. end - size - ptr);
  971. iref = (struct btrfs_extent_inline_ref *)ptr;
  972. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  973. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  974. struct btrfs_extent_data_ref *dref;
  975. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  976. btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
  977. btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
  978. btrfs_set_extent_data_ref_offset(leaf, dref, offset);
  979. btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
  980. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  981. struct btrfs_shared_data_ref *sref;
  982. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  983. btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
  984. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  985. } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
  986. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  987. } else {
  988. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  989. }
  990. }
  991. static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  992. struct btrfs_path *path,
  993. struct btrfs_extent_inline_ref **ref_ret,
  994. u64 bytenr, u64 num_bytes, u64 parent,
  995. u64 root_objectid, u64 owner, u64 offset)
  996. {
  997. int ret;
  998. ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
  999. num_bytes, parent, root_objectid,
  1000. owner, offset, 0);
  1001. if (ret != -ENOENT)
  1002. return ret;
  1003. btrfs_release_path(path);
  1004. *ref_ret = NULL;
  1005. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1006. ret = lookup_tree_block_ref(trans, path, bytenr, parent,
  1007. root_objectid);
  1008. } else {
  1009. ret = lookup_extent_data_ref(trans, path, bytenr, parent,
  1010. root_objectid, owner, offset);
  1011. }
  1012. return ret;
  1013. }
  1014. /*
  1015. * helper to update/remove inline back ref
  1016. */
  1017. static noinline_for_stack int update_inline_extent_backref(
  1018. struct btrfs_trans_handle *trans,
  1019. struct btrfs_path *path,
  1020. struct btrfs_extent_inline_ref *iref,
  1021. int refs_to_mod,
  1022. struct btrfs_delayed_extent_op *extent_op)
  1023. {
  1024. struct extent_buffer *leaf = path->nodes[0];
  1025. struct btrfs_fs_info *fs_info = leaf->fs_info;
  1026. struct btrfs_extent_item *ei;
  1027. struct btrfs_extent_data_ref *dref = NULL;
  1028. struct btrfs_shared_data_ref *sref = NULL;
  1029. unsigned long ptr;
  1030. unsigned long end;
  1031. u32 item_size;
  1032. int size;
  1033. int type;
  1034. u64 refs;
  1035. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1036. refs = btrfs_extent_refs(leaf, ei);
  1037. if (unlikely(refs_to_mod < 0 && refs + refs_to_mod <= 0)) {
  1038. struct btrfs_key key;
  1039. u32 extent_size;
  1040. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1041. if (key.type == BTRFS_METADATA_ITEM_KEY)
  1042. extent_size = fs_info->nodesize;
  1043. else
  1044. extent_size = key.offset;
  1045. btrfs_print_leaf(leaf);
  1046. btrfs_err(fs_info,
  1047. "invalid refs_to_mod for extent %llu num_bytes %u, has %d expect >= -%llu",
  1048. key.objectid, extent_size, refs_to_mod, refs);
  1049. return -EUCLEAN;
  1050. }
  1051. refs += refs_to_mod;
  1052. btrfs_set_extent_refs(leaf, ei, refs);
  1053. if (extent_op)
  1054. __run_delayed_extent_op(extent_op, leaf, ei);
  1055. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
  1056. /*
  1057. * Function btrfs_get_extent_inline_ref_type() has already printed
  1058. * error messages.
  1059. */
  1060. if (unlikely(type == BTRFS_REF_TYPE_INVALID))
  1061. return -EUCLEAN;
  1062. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1063. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1064. refs = btrfs_extent_data_ref_count(leaf, dref);
  1065. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1066. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1067. refs = btrfs_shared_data_ref_count(leaf, sref);
  1068. } else {
  1069. refs = 1;
  1070. /*
  1071. * For tree blocks we can only drop one ref for it, and tree
  1072. * blocks should not have refs > 1.
  1073. *
  1074. * Furthermore if we're inserting a new inline backref, we
  1075. * won't reach this path either. That would be
  1076. * setup_inline_extent_backref().
  1077. */
  1078. if (unlikely(refs_to_mod != -1)) {
  1079. struct btrfs_key key;
  1080. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1081. btrfs_print_leaf(leaf);
  1082. btrfs_err(fs_info,
  1083. "invalid refs_to_mod for tree block %llu, has %d expect -1",
  1084. key.objectid, refs_to_mod);
  1085. return -EUCLEAN;
  1086. }
  1087. }
  1088. if (unlikely(refs_to_mod < 0 && refs < -refs_to_mod)) {
  1089. struct btrfs_key key;
  1090. u32 extent_size;
  1091. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1092. if (key.type == BTRFS_METADATA_ITEM_KEY)
  1093. extent_size = fs_info->nodesize;
  1094. else
  1095. extent_size = key.offset;
  1096. btrfs_print_leaf(leaf);
  1097. btrfs_err(fs_info,
  1098. "invalid refs_to_mod for backref entry, iref %lu extent %llu num_bytes %u, has %d expect >= -%llu",
  1099. (unsigned long)iref, key.objectid, extent_size,
  1100. refs_to_mod, refs);
  1101. return -EUCLEAN;
  1102. }
  1103. refs += refs_to_mod;
  1104. if (refs > 0) {
  1105. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  1106. btrfs_set_extent_data_ref_count(leaf, dref, refs);
  1107. else
  1108. btrfs_set_shared_data_ref_count(leaf, sref, refs);
  1109. } else {
  1110. size = btrfs_extent_inline_ref_size(type);
  1111. item_size = btrfs_item_size(leaf, path->slots[0]);
  1112. ptr = (unsigned long)iref;
  1113. end = (unsigned long)ei + item_size;
  1114. if (ptr + size < end)
  1115. memmove_extent_buffer(leaf, ptr, ptr + size,
  1116. end - ptr - size);
  1117. item_size -= size;
  1118. btrfs_truncate_item(trans, path, item_size, 1);
  1119. }
  1120. return 0;
  1121. }
  1122. static noinline_for_stack
  1123. int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
  1124. struct btrfs_path *path,
  1125. u64 bytenr, u64 num_bytes, u64 parent,
  1126. u64 root_objectid, u64 owner,
  1127. u64 offset, int refs_to_add,
  1128. struct btrfs_delayed_extent_op *extent_op)
  1129. {
  1130. struct btrfs_extent_inline_ref *iref;
  1131. int ret;
  1132. ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
  1133. num_bytes, parent, root_objectid,
  1134. owner, offset, 1);
  1135. if (ret == 0) {
  1136. /*
  1137. * We're adding refs to a tree block we already own, this
  1138. * should not happen at all.
  1139. */
  1140. if (unlikely(owner < BTRFS_FIRST_FREE_OBJECTID)) {
  1141. btrfs_print_leaf(path->nodes[0]);
  1142. btrfs_crit(trans->fs_info,
  1143. "adding refs to an existing tree ref, bytenr %llu num_bytes %llu root_objectid %llu slot %u",
  1144. bytenr, num_bytes, root_objectid, path->slots[0]);
  1145. return -EUCLEAN;
  1146. }
  1147. ret = update_inline_extent_backref(trans, path, iref,
  1148. refs_to_add, extent_op);
  1149. } else if (ret == -ENOENT) {
  1150. setup_inline_extent_backref(trans, path, iref, parent,
  1151. root_objectid, owner, offset,
  1152. refs_to_add, extent_op);
  1153. ret = 0;
  1154. }
  1155. return ret;
  1156. }
  1157. static int remove_extent_backref(struct btrfs_trans_handle *trans,
  1158. struct btrfs_root *root,
  1159. struct btrfs_path *path,
  1160. struct btrfs_extent_inline_ref *iref,
  1161. int refs_to_drop, int is_data)
  1162. {
  1163. int ret = 0;
  1164. BUG_ON(!is_data && refs_to_drop != 1);
  1165. if (iref)
  1166. ret = update_inline_extent_backref(trans, path, iref,
  1167. -refs_to_drop, NULL);
  1168. else if (is_data)
  1169. ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
  1170. else
  1171. ret = btrfs_del_item(trans, root, path);
  1172. return ret;
  1173. }
  1174. static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  1175. u64 *discarded_bytes)
  1176. {
  1177. int j, ret = 0;
  1178. u64 bytes_left, end;
  1179. u64 aligned_start = ALIGN(start, SECTOR_SIZE);
  1180. /* Adjust the range to be aligned to 512B sectors if necessary. */
  1181. if (start != aligned_start) {
  1182. len -= aligned_start - start;
  1183. len = round_down(len, SECTOR_SIZE);
  1184. start = aligned_start;
  1185. }
  1186. *discarded_bytes = 0;
  1187. if (!len)
  1188. return 0;
  1189. end = start + len;
  1190. bytes_left = len;
  1191. /* Skip any superblocks on this device. */
  1192. for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
  1193. u64 sb_start = btrfs_sb_offset(j);
  1194. u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
  1195. u64 size = sb_start - start;
  1196. if (!in_range(sb_start, start, bytes_left) &&
  1197. !in_range(sb_end, start, bytes_left) &&
  1198. !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
  1199. continue;
  1200. /*
  1201. * Superblock spans beginning of range. Adjust start and
  1202. * try again.
  1203. */
  1204. if (sb_start <= start) {
  1205. start += sb_end - start;
  1206. if (start > end) {
  1207. bytes_left = 0;
  1208. break;
  1209. }
  1210. bytes_left = end - start;
  1211. continue;
  1212. }
  1213. if (size) {
  1214. ret = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
  1215. size >> SECTOR_SHIFT,
  1216. GFP_NOFS);
  1217. if (!ret)
  1218. *discarded_bytes += size;
  1219. else if (ret != -EOPNOTSUPP)
  1220. return ret;
  1221. }
  1222. start = sb_end;
  1223. if (start > end) {
  1224. bytes_left = 0;
  1225. break;
  1226. }
  1227. bytes_left = end - start;
  1228. }
  1229. while (bytes_left) {
  1230. u64 bytes_to_discard = min(BTRFS_MAX_DISCARD_CHUNK_SIZE, bytes_left);
  1231. ret = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
  1232. bytes_to_discard >> SECTOR_SHIFT,
  1233. GFP_NOFS);
  1234. if (ret) {
  1235. if (ret != -EOPNOTSUPP)
  1236. break;
  1237. continue;
  1238. }
  1239. start += bytes_to_discard;
  1240. bytes_left -= bytes_to_discard;
  1241. *discarded_bytes += bytes_to_discard;
  1242. if (btrfs_trim_interrupted()) {
  1243. ret = -ERESTARTSYS;
  1244. break;
  1245. }
  1246. }
  1247. return ret;
  1248. }
  1249. static int do_discard_extent(struct btrfs_discard_stripe *stripe, u64 *bytes)
  1250. {
  1251. struct btrfs_device *dev = stripe->dev;
  1252. struct btrfs_fs_info *fs_info = dev->fs_info;
  1253. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  1254. u64 phys = stripe->physical;
  1255. u64 len = stripe->length;
  1256. u64 discarded = 0;
  1257. int ret = 0;
  1258. /* Zone reset on a zoned filesystem */
  1259. if (btrfs_can_zone_reset(dev, phys, len)) {
  1260. u64 src_disc;
  1261. ret = btrfs_reset_device_zone(dev, phys, len, &discarded);
  1262. if (ret)
  1263. goto out;
  1264. if (!btrfs_dev_replace_is_ongoing(dev_replace) ||
  1265. dev != dev_replace->srcdev)
  1266. goto out;
  1267. src_disc = discarded;
  1268. /* Send to replace target as well */
  1269. ret = btrfs_reset_device_zone(dev_replace->tgtdev, phys, len,
  1270. &discarded);
  1271. discarded += src_disc;
  1272. } else if (bdev_max_discard_sectors(stripe->dev->bdev)) {
  1273. ret = btrfs_issue_discard(dev->bdev, phys, len, &discarded);
  1274. } else {
  1275. ret = 0;
  1276. *bytes = 0;
  1277. }
  1278. out:
  1279. *bytes = discarded;
  1280. return ret;
  1281. }
  1282. int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
  1283. u64 num_bytes, u64 *actual_bytes, bool do_remap)
  1284. {
  1285. int ret = 0;
  1286. u64 discarded_bytes = 0;
  1287. u64 end = bytenr + num_bytes;
  1288. u64 cur = bytenr;
  1289. /*
  1290. * Avoid races with device replace and make sure the devices in the
  1291. * stripes don't go away while we are discarding.
  1292. */
  1293. btrfs_bio_counter_inc_blocked(fs_info);
  1294. while (cur < end) {
  1295. struct btrfs_discard_stripe *stripes;
  1296. unsigned int num_stripes;
  1297. int i;
  1298. num_bytes = end - cur;
  1299. stripes = btrfs_map_discard(fs_info, cur, &num_bytes, &num_stripes,
  1300. do_remap);
  1301. if (IS_ERR(stripes)) {
  1302. ret = PTR_ERR(stripes);
  1303. if (ret == -EOPNOTSUPP)
  1304. ret = 0;
  1305. break;
  1306. }
  1307. for (i = 0; i < num_stripes; i++) {
  1308. struct btrfs_discard_stripe *stripe = stripes + i;
  1309. u64 bytes;
  1310. if (!stripe->dev->bdev) {
  1311. ASSERT(btrfs_test_opt(fs_info, DEGRADED));
  1312. continue;
  1313. }
  1314. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
  1315. &stripe->dev->dev_state))
  1316. continue;
  1317. ret = do_discard_extent(stripe, &bytes);
  1318. if (ret) {
  1319. /*
  1320. * Keep going if discard is not supported by the
  1321. * device.
  1322. */
  1323. if (ret != -EOPNOTSUPP)
  1324. break;
  1325. ret = 0;
  1326. } else {
  1327. discarded_bytes += bytes;
  1328. }
  1329. }
  1330. kfree(stripes);
  1331. if (ret)
  1332. break;
  1333. cur += num_bytes;
  1334. }
  1335. btrfs_bio_counter_dec(fs_info);
  1336. if (actual_bytes)
  1337. *actual_bytes = discarded_bytes;
  1338. return ret;
  1339. }
  1340. /* Can return -ENOMEM */
  1341. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1342. struct btrfs_ref *generic_ref)
  1343. {
  1344. struct btrfs_fs_info *fs_info = trans->fs_info;
  1345. int ret;
  1346. ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
  1347. generic_ref->action);
  1348. BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
  1349. generic_ref->ref_root == BTRFS_TREE_LOG_OBJECTID);
  1350. if (generic_ref->type == BTRFS_REF_METADATA)
  1351. ret = btrfs_add_delayed_tree_ref(trans, generic_ref, NULL);
  1352. else
  1353. ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0);
  1354. btrfs_ref_tree_mod(fs_info, generic_ref);
  1355. return ret;
  1356. }
  1357. /*
  1358. * Insert backreference for a given extent.
  1359. *
  1360. * The counterpart is in __btrfs_free_extent(), with examples and more details
  1361. * how it works.
  1362. *
  1363. * @trans: Handle of transaction
  1364. *
  1365. * @node: The delayed ref node used to get the bytenr/length for
  1366. * extent whose references are incremented.
  1367. *
  1368. * @extent_op Pointer to a structure, holding information necessary when
  1369. * updating a tree block's flags
  1370. *
  1371. */
  1372. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1373. const struct btrfs_delayed_ref_node *node,
  1374. struct btrfs_delayed_extent_op *extent_op)
  1375. {
  1376. BTRFS_PATH_AUTO_FREE(path);
  1377. struct extent_buffer *leaf;
  1378. struct btrfs_extent_item *item;
  1379. struct btrfs_key key;
  1380. u64 bytenr = node->bytenr;
  1381. u64 num_bytes = node->num_bytes;
  1382. u64 owner = btrfs_delayed_ref_owner(node);
  1383. u64 offset = btrfs_delayed_ref_offset(node);
  1384. u64 refs;
  1385. int refs_to_add = node->ref_mod;
  1386. int ret;
  1387. path = btrfs_alloc_path();
  1388. if (!path)
  1389. return -ENOMEM;
  1390. /* this will setup the path even if it fails to insert the back ref */
  1391. ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
  1392. node->parent, node->ref_root, owner,
  1393. offset, refs_to_add, extent_op);
  1394. if ((ret < 0 && ret != -EAGAIN) || !ret)
  1395. return ret;
  1396. /*
  1397. * Ok we had -EAGAIN which means we didn't have space to insert and
  1398. * inline extent ref, so just update the reference count and add a
  1399. * normal backref.
  1400. */
  1401. leaf = path->nodes[0];
  1402. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1403. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1404. refs = btrfs_extent_refs(leaf, item);
  1405. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  1406. if (extent_op)
  1407. __run_delayed_extent_op(extent_op, leaf, item);
  1408. btrfs_release_path(path);
  1409. /* now insert the actual backref */
  1410. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1411. ret = insert_tree_block_ref(trans, path, node, bytenr);
  1412. if (ret)
  1413. btrfs_abort_transaction(trans, ret);
  1414. } else {
  1415. ret = insert_extent_data_ref(trans, path, node, bytenr);
  1416. if (ret)
  1417. btrfs_abort_transaction(trans, ret);
  1418. }
  1419. return ret;
  1420. }
  1421. static void free_head_ref_squota_rsv(struct btrfs_fs_info *fs_info,
  1422. const struct btrfs_delayed_ref_head *href)
  1423. {
  1424. u64 root = href->owning_root;
  1425. /*
  1426. * Don't check must_insert_reserved, as this is called from contexts
  1427. * where it has already been unset.
  1428. */
  1429. if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE ||
  1430. !href->is_data || !btrfs_is_fstree(root))
  1431. return;
  1432. btrfs_qgroup_free_refroot(fs_info, root, href->reserved_bytes,
  1433. BTRFS_QGROUP_RSV_DATA);
  1434. }
  1435. static int drop_remap_tree_ref(struct btrfs_trans_handle *trans,
  1436. const struct btrfs_delayed_ref_node *node)
  1437. {
  1438. u64 bytenr = node->bytenr;
  1439. u64 num_bytes = node->num_bytes;
  1440. int ret;
  1441. ret = btrfs_add_to_free_space_tree(trans, bytenr, num_bytes);
  1442. if (unlikely(ret)) {
  1443. btrfs_abort_transaction(trans, ret);
  1444. return ret;
  1445. }
  1446. ret = btrfs_update_block_group(trans, bytenr, num_bytes, false);
  1447. if (unlikely(ret)) {
  1448. btrfs_abort_transaction(trans, ret);
  1449. return ret;
  1450. }
  1451. return 0;
  1452. }
  1453. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  1454. struct btrfs_delayed_ref_head *href,
  1455. const struct btrfs_delayed_ref_node *node,
  1456. struct btrfs_delayed_extent_op *extent_op,
  1457. bool insert_reserved)
  1458. {
  1459. int ret = 0;
  1460. u64 parent = 0;
  1461. u64 flags = 0;
  1462. trace_run_delayed_data_ref(trans->fs_info, node);
  1463. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1464. parent = node->parent;
  1465. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1466. struct btrfs_key key;
  1467. struct btrfs_squota_delta delta = {
  1468. .root = href->owning_root,
  1469. .num_bytes = node->num_bytes,
  1470. .is_data = true,
  1471. .is_inc = true,
  1472. .generation = trans->transid,
  1473. };
  1474. u64 owner = btrfs_delayed_ref_owner(node);
  1475. u64 offset = btrfs_delayed_ref_offset(node);
  1476. if (extent_op)
  1477. flags |= extent_op->flags_to_set;
  1478. key.objectid = node->bytenr;
  1479. key.type = BTRFS_EXTENT_ITEM_KEY;
  1480. key.offset = node->num_bytes;
  1481. ret = alloc_reserved_file_extent(trans, parent, node->ref_root,
  1482. flags, owner, offset, &key,
  1483. node->ref_mod,
  1484. href->owning_root);
  1485. free_head_ref_squota_rsv(trans->fs_info, href);
  1486. if (!ret)
  1487. ret = btrfs_record_squota_delta(trans->fs_info, &delta);
  1488. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1489. ret = __btrfs_inc_extent_ref(trans, node, extent_op);
  1490. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1491. ret = __btrfs_free_extent(trans, href, node, extent_op);
  1492. } else {
  1493. BUG();
  1494. }
  1495. return ret;
  1496. }
  1497. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1498. struct extent_buffer *leaf,
  1499. struct btrfs_extent_item *ei)
  1500. {
  1501. u64 flags = btrfs_extent_flags(leaf, ei);
  1502. if (extent_op->update_flags) {
  1503. flags |= extent_op->flags_to_set;
  1504. btrfs_set_extent_flags(leaf, ei, flags);
  1505. }
  1506. if (extent_op->update_key) {
  1507. struct btrfs_tree_block_info *bi;
  1508. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1509. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1510. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1511. }
  1512. }
  1513. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1514. const struct btrfs_delayed_ref_head *head,
  1515. struct btrfs_delayed_extent_op *extent_op)
  1516. {
  1517. struct btrfs_fs_info *fs_info = trans->fs_info;
  1518. struct btrfs_root *root;
  1519. struct btrfs_key key;
  1520. BTRFS_PATH_AUTO_FREE(path);
  1521. struct btrfs_extent_item *ei;
  1522. struct extent_buffer *leaf;
  1523. u32 item_size;
  1524. int ret;
  1525. int metadata = 1;
  1526. if (TRANS_ABORTED(trans))
  1527. return 0;
  1528. if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1529. metadata = 0;
  1530. path = btrfs_alloc_path();
  1531. if (!path)
  1532. return -ENOMEM;
  1533. key.objectid = head->bytenr;
  1534. if (metadata) {
  1535. key.type = BTRFS_METADATA_ITEM_KEY;
  1536. key.offset = head->level;
  1537. } else {
  1538. key.type = BTRFS_EXTENT_ITEM_KEY;
  1539. key.offset = head->num_bytes;
  1540. }
  1541. root = btrfs_extent_root(fs_info, key.objectid);
  1542. if (unlikely(!root)) {
  1543. btrfs_err(fs_info,
  1544. "missing extent root for extent at bytenr %llu",
  1545. key.objectid);
  1546. return -EUCLEAN;
  1547. }
  1548. again:
  1549. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1550. if (ret < 0) {
  1551. return ret;
  1552. } else if (ret > 0) {
  1553. if (metadata) {
  1554. if (path->slots[0] > 0) {
  1555. path->slots[0]--;
  1556. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1557. path->slots[0]);
  1558. if (key.objectid == head->bytenr &&
  1559. key.type == BTRFS_EXTENT_ITEM_KEY &&
  1560. key.offset == head->num_bytes)
  1561. ret = 0;
  1562. }
  1563. if (ret > 0) {
  1564. btrfs_release_path(path);
  1565. metadata = 0;
  1566. key.objectid = head->bytenr;
  1567. key.type = BTRFS_EXTENT_ITEM_KEY;
  1568. key.offset = head->num_bytes;
  1569. goto again;
  1570. }
  1571. } else {
  1572. ret = -EUCLEAN;
  1573. btrfs_err(fs_info,
  1574. "missing extent item for extent %llu num_bytes %llu level %d",
  1575. head->bytenr, head->num_bytes, head->level);
  1576. return ret;
  1577. }
  1578. }
  1579. leaf = path->nodes[0];
  1580. item_size = btrfs_item_size(leaf, path->slots[0]);
  1581. if (unlikely(item_size < sizeof(*ei))) {
  1582. ret = -EUCLEAN;
  1583. btrfs_err(fs_info,
  1584. "unexpected extent item size, has %u expect >= %zu",
  1585. item_size, sizeof(*ei));
  1586. btrfs_abort_transaction(trans, ret);
  1587. return ret;
  1588. }
  1589. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1590. __run_delayed_extent_op(extent_op, leaf, ei);
  1591. return ret;
  1592. }
  1593. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1594. struct btrfs_delayed_ref_head *href,
  1595. const struct btrfs_delayed_ref_node *node,
  1596. struct btrfs_delayed_extent_op *extent_op,
  1597. bool insert_reserved)
  1598. {
  1599. int ret = 0;
  1600. struct btrfs_fs_info *fs_info = trans->fs_info;
  1601. u64 parent = 0;
  1602. u64 ref_root = 0;
  1603. trace_run_delayed_tree_ref(trans->fs_info, node);
  1604. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1605. parent = node->parent;
  1606. ref_root = node->ref_root;
  1607. if (unlikely(node->ref_mod != 1)) {
  1608. btrfs_err(trans->fs_info,
  1609. "btree block %llu has %d references rather than 1: action %d ref_root %llu parent %llu",
  1610. node->bytenr, node->ref_mod, node->action, ref_root,
  1611. parent);
  1612. return -EUCLEAN;
  1613. }
  1614. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1615. struct btrfs_squota_delta delta = {
  1616. .root = href->owning_root,
  1617. .num_bytes = fs_info->nodesize,
  1618. .is_data = false,
  1619. .is_inc = true,
  1620. .generation = trans->transid,
  1621. };
  1622. ret = alloc_reserved_tree_block(trans, node, extent_op);
  1623. if (!ret)
  1624. btrfs_record_squota_delta(fs_info, &delta);
  1625. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1626. ret = __btrfs_inc_extent_ref(trans, node, extent_op);
  1627. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1628. if (node->ref_root == BTRFS_REMAP_TREE_OBJECTID)
  1629. ret = drop_remap_tree_ref(trans, node);
  1630. else
  1631. ret = __btrfs_free_extent(trans, href, node, extent_op);
  1632. } else {
  1633. BUG();
  1634. }
  1635. return ret;
  1636. }
  1637. /* helper function to actually process a single delayed ref entry */
  1638. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  1639. struct btrfs_delayed_ref_head *href,
  1640. const struct btrfs_delayed_ref_node *node,
  1641. struct btrfs_delayed_extent_op *extent_op,
  1642. bool insert_reserved)
  1643. {
  1644. struct btrfs_fs_info *fs_info = trans->fs_info;
  1645. int ret = 0;
  1646. if (TRANS_ABORTED(trans)) {
  1647. if (insert_reserved) {
  1648. btrfs_pin_extent(trans, node->bytenr, node->num_bytes);
  1649. free_head_ref_squota_rsv(fs_info, href);
  1650. }
  1651. return 0;
  1652. }
  1653. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  1654. node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1655. ret = run_delayed_tree_ref(trans, href, node, extent_op,
  1656. insert_reserved);
  1657. } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  1658. node->type == BTRFS_SHARED_DATA_REF_KEY) {
  1659. ret = run_delayed_data_ref(trans, href, node, extent_op,
  1660. insert_reserved);
  1661. } else if (unlikely(node->type != BTRFS_EXTENT_OWNER_REF_KEY)) {
  1662. ret = -EUCLEAN;
  1663. btrfs_err(fs_info, "unexpected delayed ref node type: %u", node->type);
  1664. }
  1665. if (unlikely(ret)) {
  1666. if (insert_reserved)
  1667. btrfs_pin_extent(trans, node->bytenr, node->num_bytes);
  1668. btrfs_err(fs_info,
  1669. "failed to run delayed ref for logical %llu num_bytes %llu type %u action %u ref_mod %d: %d",
  1670. node->bytenr, node->num_bytes, node->type,
  1671. node->action, node->ref_mod, ret);
  1672. }
  1673. return ret;
  1674. }
  1675. static struct btrfs_delayed_extent_op *cleanup_extent_op(
  1676. struct btrfs_delayed_ref_head *head)
  1677. {
  1678. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  1679. if (!extent_op)
  1680. return NULL;
  1681. if (head->must_insert_reserved) {
  1682. head->extent_op = NULL;
  1683. btrfs_free_delayed_extent_op(extent_op);
  1684. return NULL;
  1685. }
  1686. return extent_op;
  1687. }
  1688. static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
  1689. struct btrfs_delayed_ref_head *head)
  1690. {
  1691. struct btrfs_delayed_extent_op *extent_op;
  1692. int ret;
  1693. extent_op = cleanup_extent_op(head);
  1694. if (!extent_op)
  1695. return 0;
  1696. head->extent_op = NULL;
  1697. spin_unlock(&head->lock);
  1698. ret = run_delayed_extent_op(trans, head, extent_op);
  1699. btrfs_free_delayed_extent_op(extent_op);
  1700. return ret ? ret : 1;
  1701. }
  1702. u64 btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
  1703. struct btrfs_delayed_ref_root *delayed_refs,
  1704. struct btrfs_delayed_ref_head *head)
  1705. {
  1706. u64 ret = 0;
  1707. /*
  1708. * We had csum deletions accounted for in our delayed refs rsv, we need
  1709. * to drop the csum leaves for this update from our delayed_refs_rsv.
  1710. */
  1711. if (head->total_ref_mod < 0 && head->is_data) {
  1712. int nr_csums;
  1713. spin_lock(&delayed_refs->lock);
  1714. delayed_refs->pending_csums -= head->num_bytes;
  1715. spin_unlock(&delayed_refs->lock);
  1716. nr_csums = btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes);
  1717. btrfs_delayed_refs_rsv_release(fs_info, 0, nr_csums);
  1718. ret = btrfs_calc_delayed_ref_csum_bytes(fs_info, nr_csums);
  1719. }
  1720. /* must_insert_reserved can be set only if we didn't run the head ref. */
  1721. if (head->must_insert_reserved)
  1722. free_head_ref_squota_rsv(fs_info, head);
  1723. return ret;
  1724. }
  1725. static int cleanup_ref_head(struct btrfs_trans_handle *trans,
  1726. struct btrfs_delayed_ref_head *head,
  1727. u64 *bytes_released)
  1728. {
  1729. struct btrfs_fs_info *fs_info = trans->fs_info;
  1730. struct btrfs_delayed_ref_root *delayed_refs;
  1731. int ret;
  1732. delayed_refs = &trans->transaction->delayed_refs;
  1733. ret = run_and_cleanup_extent_op(trans, head);
  1734. if (ret < 0) {
  1735. btrfs_unselect_ref_head(delayed_refs, head);
  1736. btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
  1737. return ret;
  1738. } else if (ret) {
  1739. return ret;
  1740. }
  1741. /*
  1742. * Need to drop our head ref lock and re-acquire the delayed ref lock
  1743. * and then re-check to make sure nobody got added.
  1744. */
  1745. spin_unlock(&head->lock);
  1746. spin_lock(&delayed_refs->lock);
  1747. spin_lock(&head->lock);
  1748. if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
  1749. spin_unlock(&head->lock);
  1750. spin_unlock(&delayed_refs->lock);
  1751. return 1;
  1752. }
  1753. btrfs_delete_ref_head(fs_info, delayed_refs, head);
  1754. spin_unlock(&head->lock);
  1755. spin_unlock(&delayed_refs->lock);
  1756. if (head->must_insert_reserved) {
  1757. btrfs_pin_extent(trans, head->bytenr, head->num_bytes);
  1758. if (head->is_data) {
  1759. struct btrfs_root *csum_root;
  1760. csum_root = btrfs_csum_root(fs_info, head->bytenr);
  1761. if (unlikely(!csum_root)) {
  1762. btrfs_err(fs_info,
  1763. "missing csum root for extent at bytenr %llu",
  1764. head->bytenr);
  1765. ret = -EUCLEAN;
  1766. } else {
  1767. ret = btrfs_del_csums(trans, csum_root, head->bytenr,
  1768. head->num_bytes);
  1769. }
  1770. }
  1771. }
  1772. *bytes_released += btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
  1773. trace_run_delayed_ref_head(fs_info, head, 0);
  1774. btrfs_delayed_ref_unlock(head);
  1775. btrfs_put_delayed_ref_head(head);
  1776. return ret;
  1777. }
  1778. static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
  1779. struct btrfs_delayed_ref_head *locked_ref,
  1780. u64 *bytes_released)
  1781. {
  1782. struct btrfs_fs_info *fs_info = trans->fs_info;
  1783. struct btrfs_delayed_ref_root *delayed_refs;
  1784. struct btrfs_delayed_extent_op *extent_op;
  1785. struct btrfs_delayed_ref_node *ref;
  1786. bool must_insert_reserved;
  1787. int ret;
  1788. delayed_refs = &trans->transaction->delayed_refs;
  1789. lockdep_assert_held(&locked_ref->mutex);
  1790. lockdep_assert_held(&locked_ref->lock);
  1791. while ((ref = btrfs_select_delayed_ref(locked_ref))) {
  1792. if (ref->seq &&
  1793. btrfs_check_delayed_seq(fs_info, ref->seq)) {
  1794. spin_unlock(&locked_ref->lock);
  1795. btrfs_unselect_ref_head(delayed_refs, locked_ref);
  1796. return -EAGAIN;
  1797. }
  1798. rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
  1799. RB_CLEAR_NODE(&ref->ref_node);
  1800. if (!list_empty(&ref->add_list))
  1801. list_del(&ref->add_list);
  1802. /*
  1803. * When we play the delayed ref, also correct the ref_mod on
  1804. * head
  1805. */
  1806. switch (ref->action) {
  1807. case BTRFS_ADD_DELAYED_REF:
  1808. case BTRFS_ADD_DELAYED_EXTENT:
  1809. locked_ref->ref_mod -= ref->ref_mod;
  1810. break;
  1811. case BTRFS_DROP_DELAYED_REF:
  1812. locked_ref->ref_mod += ref->ref_mod;
  1813. break;
  1814. default:
  1815. WARN_ON(1);
  1816. }
  1817. /*
  1818. * Record the must_insert_reserved flag before we drop the
  1819. * spin lock.
  1820. */
  1821. must_insert_reserved = locked_ref->must_insert_reserved;
  1822. /*
  1823. * Unsetting this on the head ref relinquishes ownership of
  1824. * the rsv_bytes, so it is critical that every possible code
  1825. * path from here forward frees all reserves including qgroup
  1826. * reserve.
  1827. */
  1828. locked_ref->must_insert_reserved = false;
  1829. extent_op = locked_ref->extent_op;
  1830. locked_ref->extent_op = NULL;
  1831. spin_unlock(&locked_ref->lock);
  1832. ret = run_one_delayed_ref(trans, locked_ref, ref, extent_op,
  1833. must_insert_reserved);
  1834. btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
  1835. *bytes_released += btrfs_calc_delayed_ref_bytes(fs_info, 1);
  1836. btrfs_free_delayed_extent_op(extent_op);
  1837. if (ret) {
  1838. btrfs_unselect_ref_head(delayed_refs, locked_ref);
  1839. btrfs_put_delayed_ref(ref);
  1840. return ret;
  1841. }
  1842. btrfs_put_delayed_ref(ref);
  1843. cond_resched();
  1844. spin_lock(&locked_ref->lock);
  1845. btrfs_merge_delayed_refs(fs_info, delayed_refs, locked_ref);
  1846. }
  1847. return 0;
  1848. }
  1849. /*
  1850. * Returns 0 on success or if called with an already aborted transaction.
  1851. * Returns -ENOMEM or -EIO on failure and will abort the transaction.
  1852. */
  1853. static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  1854. u64 min_bytes)
  1855. {
  1856. struct btrfs_fs_info *fs_info = trans->fs_info;
  1857. struct btrfs_delayed_ref_root *delayed_refs;
  1858. struct btrfs_delayed_ref_head *locked_ref = NULL;
  1859. int ret;
  1860. unsigned long count = 0;
  1861. unsigned long max_count = 0;
  1862. u64 bytes_processed = 0;
  1863. delayed_refs = &trans->transaction->delayed_refs;
  1864. if (min_bytes == 0) {
  1865. /*
  1866. * We may be subject to a harmless race if some task is
  1867. * concurrently adding or removing a delayed ref, so silence
  1868. * KCSAN and similar tools.
  1869. */
  1870. max_count = data_race(delayed_refs->num_heads_ready);
  1871. min_bytes = U64_MAX;
  1872. }
  1873. do {
  1874. if (!locked_ref) {
  1875. locked_ref = btrfs_select_ref_head(fs_info, delayed_refs);
  1876. if (IS_ERR_OR_NULL(locked_ref)) {
  1877. if (PTR_ERR(locked_ref) == -EAGAIN) {
  1878. continue;
  1879. } else {
  1880. break;
  1881. }
  1882. }
  1883. count++;
  1884. }
  1885. /*
  1886. * We need to try and merge add/drops of the same ref since we
  1887. * can run into issues with relocate dropping the implicit ref
  1888. * and then it being added back again before the drop can
  1889. * finish. If we merged anything we need to re-loop so we can
  1890. * get a good ref.
  1891. * Or we can get node references of the same type that weren't
  1892. * merged when created due to bumps in the tree mod seq, and
  1893. * we need to merge them to prevent adding an inline extent
  1894. * backref before dropping it (triggering a BUG_ON at
  1895. * insert_inline_extent_backref()).
  1896. */
  1897. spin_lock(&locked_ref->lock);
  1898. btrfs_merge_delayed_refs(fs_info, delayed_refs, locked_ref);
  1899. ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, &bytes_processed);
  1900. if (ret < 0 && ret != -EAGAIN) {
  1901. /*
  1902. * Error, btrfs_run_delayed_refs_for_head already
  1903. * unlocked everything so just bail out
  1904. */
  1905. return ret;
  1906. } else if (!ret) {
  1907. /*
  1908. * Success, perform the usual cleanup of a processed
  1909. * head
  1910. */
  1911. ret = cleanup_ref_head(trans, locked_ref, &bytes_processed);
  1912. if (ret > 0 ) {
  1913. /* We dropped our lock, we need to loop. */
  1914. ret = 0;
  1915. continue;
  1916. } else if (ret) {
  1917. return ret;
  1918. }
  1919. }
  1920. /*
  1921. * Either success case or btrfs_run_delayed_refs_for_head
  1922. * returned -EAGAIN, meaning we need to select another head
  1923. */
  1924. locked_ref = NULL;
  1925. cond_resched();
  1926. } while ((min_bytes != U64_MAX && bytes_processed < min_bytes) ||
  1927. (max_count > 0 && count < max_count) ||
  1928. locked_ref);
  1929. return 0;
  1930. }
  1931. #ifdef SCRAMBLE_DELAYED_REFS
  1932. /*
  1933. * Normally delayed refs get processed in ascending bytenr order. This
  1934. * correlates in most cases to the order added. To expose dependencies on this
  1935. * order, we start to process the tree in the middle instead of the beginning
  1936. */
  1937. static u64 find_middle(struct rb_root *root)
  1938. {
  1939. struct rb_node *n = root->rb_node;
  1940. struct btrfs_delayed_ref_node *entry;
  1941. int alt = 1;
  1942. u64 middle;
  1943. u64 first = 0, last = 0;
  1944. n = rb_first(root);
  1945. if (n) {
  1946. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  1947. first = entry->bytenr;
  1948. }
  1949. n = rb_last(root);
  1950. if (n) {
  1951. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  1952. last = entry->bytenr;
  1953. }
  1954. n = root->rb_node;
  1955. while (n) {
  1956. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  1957. WARN_ON(!entry->in_tree);
  1958. middle = entry->bytenr;
  1959. if (alt)
  1960. n = n->rb_left;
  1961. else
  1962. n = n->rb_right;
  1963. alt = 1 - alt;
  1964. }
  1965. return middle;
  1966. }
  1967. #endif
  1968. /*
  1969. * Start processing the delayed reference count updates and extent insertions
  1970. * we have queued up so far.
  1971. *
  1972. * @trans: Transaction handle.
  1973. * @min_bytes: How many bytes of delayed references to process. After this
  1974. * many bytes we stop processing delayed references if there are
  1975. * any more. If 0 it means to run all existing delayed references,
  1976. * but not new ones added after running all existing ones.
  1977. * Use (u64)-1 (U64_MAX) to run all existing delayed references
  1978. * plus any new ones that are added.
  1979. *
  1980. * Returns 0 on success or if called with an aborted transaction
  1981. * Returns <0 on error and aborts the transaction
  1982. */
  1983. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, u64 min_bytes)
  1984. {
  1985. struct btrfs_fs_info *fs_info = trans->fs_info;
  1986. struct btrfs_delayed_ref_root *delayed_refs;
  1987. int ret;
  1988. /* We'll clean this up in btrfs_cleanup_transaction */
  1989. if (TRANS_ABORTED(trans))
  1990. return 0;
  1991. if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
  1992. return 0;
  1993. delayed_refs = &trans->transaction->delayed_refs;
  1994. again:
  1995. #ifdef SCRAMBLE_DELAYED_REFS
  1996. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  1997. #endif
  1998. ret = __btrfs_run_delayed_refs(trans, min_bytes);
  1999. if (unlikely(ret < 0)) {
  2000. btrfs_abort_transaction(trans, ret);
  2001. return ret;
  2002. }
  2003. if (min_bytes == U64_MAX) {
  2004. btrfs_create_pending_block_groups(trans);
  2005. spin_lock(&delayed_refs->lock);
  2006. if (xa_empty(&delayed_refs->head_refs)) {
  2007. spin_unlock(&delayed_refs->lock);
  2008. return 0;
  2009. }
  2010. spin_unlock(&delayed_refs->lock);
  2011. cond_resched();
  2012. goto again;
  2013. }
  2014. return 0;
  2015. }
  2016. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2017. struct extent_buffer *eb, u64 flags)
  2018. {
  2019. struct btrfs_delayed_extent_op *extent_op;
  2020. int ret;
  2021. extent_op = btrfs_alloc_delayed_extent_op();
  2022. if (!extent_op)
  2023. return -ENOMEM;
  2024. extent_op->flags_to_set = flags;
  2025. extent_op->update_flags = true;
  2026. extent_op->update_key = false;
  2027. ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len,
  2028. btrfs_header_level(eb), extent_op);
  2029. if (ret)
  2030. btrfs_free_delayed_extent_op(extent_op);
  2031. return ret;
  2032. }
  2033. static noinline int check_delayed_ref(struct btrfs_inode *inode,
  2034. struct btrfs_path *path,
  2035. u64 offset, u64 bytenr)
  2036. {
  2037. struct btrfs_root *root = inode->root;
  2038. struct btrfs_delayed_ref_head *head;
  2039. struct btrfs_delayed_ref_node *ref;
  2040. struct btrfs_delayed_ref_root *delayed_refs;
  2041. struct btrfs_transaction *cur_trans;
  2042. struct rb_node *node;
  2043. int ret = 0;
  2044. spin_lock(&root->fs_info->trans_lock);
  2045. cur_trans = root->fs_info->running_transaction;
  2046. if (cur_trans)
  2047. refcount_inc(&cur_trans->use_count);
  2048. spin_unlock(&root->fs_info->trans_lock);
  2049. if (!cur_trans)
  2050. return 0;
  2051. delayed_refs = &cur_trans->delayed_refs;
  2052. spin_lock(&delayed_refs->lock);
  2053. head = btrfs_find_delayed_ref_head(root->fs_info, delayed_refs, bytenr);
  2054. if (!head) {
  2055. spin_unlock(&delayed_refs->lock);
  2056. btrfs_put_transaction(cur_trans);
  2057. return 0;
  2058. }
  2059. if (!mutex_trylock(&head->mutex)) {
  2060. if (path->nowait) {
  2061. spin_unlock(&delayed_refs->lock);
  2062. btrfs_put_transaction(cur_trans);
  2063. return -EAGAIN;
  2064. }
  2065. refcount_inc(&head->refs);
  2066. spin_unlock(&delayed_refs->lock);
  2067. btrfs_release_path(path);
  2068. /*
  2069. * Mutex was contended, block until it's released and let
  2070. * caller try again
  2071. */
  2072. mutex_lock(&head->mutex);
  2073. mutex_unlock(&head->mutex);
  2074. btrfs_put_delayed_ref_head(head);
  2075. btrfs_put_transaction(cur_trans);
  2076. return -EAGAIN;
  2077. }
  2078. spin_unlock(&delayed_refs->lock);
  2079. spin_lock(&head->lock);
  2080. /*
  2081. * XXX: We should replace this with a proper search function in the
  2082. * future.
  2083. */
  2084. for (node = rb_first_cached(&head->ref_tree); node;
  2085. node = rb_next(node)) {
  2086. u64 ref_owner;
  2087. u64 ref_offset;
  2088. ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
  2089. /* If it's a shared ref we know a cross reference exists */
  2090. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
  2091. ret = 1;
  2092. break;
  2093. }
  2094. ref_owner = btrfs_delayed_ref_owner(ref);
  2095. ref_offset = btrfs_delayed_ref_offset(ref);
  2096. /*
  2097. * If our ref doesn't match the one we're currently looking at
  2098. * then we have a cross reference.
  2099. */
  2100. if (ref->ref_root != btrfs_root_id(root) ||
  2101. ref_owner != btrfs_ino(inode) || ref_offset != offset) {
  2102. ret = 1;
  2103. break;
  2104. }
  2105. }
  2106. spin_unlock(&head->lock);
  2107. mutex_unlock(&head->mutex);
  2108. btrfs_put_transaction(cur_trans);
  2109. return ret;
  2110. }
  2111. /*
  2112. * Check if there are references for a data extent other than the one belonging
  2113. * to the given inode and offset.
  2114. *
  2115. * @inode: The only inode we expect to find associated with the data extent.
  2116. * @path: A path to use for searching the extent tree.
  2117. * @offset: The only offset we expect to find associated with the data extent.
  2118. * @bytenr: The logical address of the data extent.
  2119. *
  2120. * When the extent does not have any other references other than the one we
  2121. * expect to find, we always return a value of 0 with the path having a locked
  2122. * leaf that contains the extent's extent item - this is necessary to ensure
  2123. * we don't race with a task running delayed references, and our caller must
  2124. * have such a path when calling check_delayed_ref() - it must lock a delayed
  2125. * ref head while holding the leaf locked. In case the extent item is not found
  2126. * in the extent tree, we return -ENOENT with the path having the leaf (locked)
  2127. * where the extent item should be, in order to prevent races with another task
  2128. * running delayed references, so that we don't miss any reference when calling
  2129. * check_delayed_ref().
  2130. *
  2131. * Note: this may return false positives, and this is because we want to be
  2132. * quick here as we're called in write paths (when flushing delalloc and
  2133. * in the direct IO write path). For example we can have an extent with
  2134. * a single reference but that reference is not inlined, or we may have
  2135. * many references in the extent tree but we also have delayed references
  2136. * that cancel all the reference except the one for our inode and offset,
  2137. * but it would be expensive to do such checks and complex due to all
  2138. * locking to avoid races between the checks and flushing delayed refs,
  2139. * plus non-inline references may be located on leaves other than the one
  2140. * that contains the extent item in the extent tree. The important thing
  2141. * here is to not return false negatives and that the false positives are
  2142. * not very common.
  2143. *
  2144. * Returns: 0 if there are no cross references and with the path having a locked
  2145. * leaf from the extent tree that contains the extent's extent item.
  2146. *
  2147. * 1 if there are cross references (false positives can happen).
  2148. *
  2149. * < 0 in case of an error. In case of -ENOENT the leaf in the extent
  2150. * tree where the extent item should be located at is read locked and
  2151. * accessible in the given path.
  2152. */
  2153. static noinline int check_committed_ref(struct btrfs_inode *inode,
  2154. struct btrfs_path *path,
  2155. u64 offset, u64 bytenr)
  2156. {
  2157. struct btrfs_root *root = inode->root;
  2158. struct btrfs_fs_info *fs_info = root->fs_info;
  2159. struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr);
  2160. struct extent_buffer *leaf;
  2161. struct btrfs_extent_data_ref *ref;
  2162. struct btrfs_extent_inline_ref *iref;
  2163. struct btrfs_extent_item *ei;
  2164. struct btrfs_key key;
  2165. u32 item_size;
  2166. u32 expected_size;
  2167. int type;
  2168. int ret;
  2169. if (unlikely(!extent_root)) {
  2170. btrfs_err(fs_info,
  2171. "missing extent root for extent at bytenr %llu", bytenr);
  2172. return -EUCLEAN;
  2173. }
  2174. key.objectid = bytenr;
  2175. key.type = BTRFS_EXTENT_ITEM_KEY;
  2176. key.offset = (u64)-1;
  2177. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2178. if (ret < 0)
  2179. return ret;
  2180. if (unlikely(ret == 0)) {
  2181. /*
  2182. * Key with offset -1 found, there would have to exist an extent
  2183. * item with such offset, but this is out of the valid range.
  2184. */
  2185. return -EUCLEAN;
  2186. }
  2187. if (path->slots[0] == 0)
  2188. return -ENOENT;
  2189. path->slots[0]--;
  2190. leaf = path->nodes[0];
  2191. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2192. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2193. return -ENOENT;
  2194. item_size = btrfs_item_size(leaf, path->slots[0]);
  2195. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2196. expected_size = sizeof(*ei) + btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY);
  2197. /* No inline refs; we need to bail before checking for owner ref. */
  2198. if (item_size == sizeof(*ei))
  2199. return 1;
  2200. /* Check for an owner ref; skip over it to the real inline refs. */
  2201. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2202. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
  2203. if (btrfs_fs_incompat(fs_info, SIMPLE_QUOTA) && type == BTRFS_EXTENT_OWNER_REF_KEY) {
  2204. expected_size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY);
  2205. iref = (struct btrfs_extent_inline_ref *)(iref + 1);
  2206. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
  2207. }
  2208. /* If extent item has more than 1 inline ref then it's shared */
  2209. if (item_size != expected_size)
  2210. return 1;
  2211. /* If this extent has SHARED_DATA_REF then it's shared */
  2212. if (type != BTRFS_EXTENT_DATA_REF_KEY)
  2213. return 1;
  2214. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2215. if (btrfs_extent_refs(leaf, ei) !=
  2216. btrfs_extent_data_ref_count(leaf, ref) ||
  2217. btrfs_extent_data_ref_root(leaf, ref) != btrfs_root_id(root) ||
  2218. btrfs_extent_data_ref_objectid(leaf, ref) != btrfs_ino(inode) ||
  2219. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2220. return 1;
  2221. return 0;
  2222. }
  2223. int btrfs_cross_ref_exist(struct btrfs_inode *inode, u64 offset,
  2224. u64 bytenr, struct btrfs_path *path)
  2225. {
  2226. int ret;
  2227. do {
  2228. ret = check_committed_ref(inode, path, offset, bytenr);
  2229. if (ret && ret != -ENOENT)
  2230. goto out;
  2231. /*
  2232. * The path must have a locked leaf from the extent tree where
  2233. * the extent item for our extent is located, in case it exists,
  2234. * or where it should be located in case it doesn't exist yet
  2235. * because it's new and its delayed ref was not yet flushed.
  2236. * We need to lock the delayed ref head at check_delayed_ref(),
  2237. * if one exists, while holding the leaf locked in order to not
  2238. * race with delayed ref flushing, missing references and
  2239. * incorrectly reporting that the extent is not shared.
  2240. */
  2241. if (IS_ENABLED(CONFIG_BTRFS_ASSERT)) {
  2242. struct extent_buffer *leaf = path->nodes[0];
  2243. ASSERT(leaf != NULL);
  2244. btrfs_assert_tree_read_locked(leaf);
  2245. if (ret != -ENOENT) {
  2246. struct btrfs_key key;
  2247. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2248. ASSERT(key.objectid == bytenr);
  2249. ASSERT(key.type == BTRFS_EXTENT_ITEM_KEY);
  2250. }
  2251. }
  2252. ret = check_delayed_ref(inode, path, offset, bytenr);
  2253. } while (ret == -EAGAIN && !path->nowait);
  2254. out:
  2255. btrfs_release_path(path);
  2256. if (btrfs_is_data_reloc_root(inode->root))
  2257. WARN_ON(ret > 0);
  2258. return ret;
  2259. }
  2260. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2261. struct btrfs_root *root,
  2262. struct extent_buffer *buf,
  2263. bool full_backref, bool inc)
  2264. {
  2265. struct btrfs_fs_info *fs_info = root->fs_info;
  2266. u64 parent;
  2267. u64 ref_root;
  2268. u32 nritems;
  2269. struct btrfs_key key;
  2270. struct btrfs_file_extent_item *fi;
  2271. bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
  2272. int i;
  2273. int action;
  2274. int level;
  2275. int ret;
  2276. if (btrfs_is_testing(fs_info))
  2277. return 0;
  2278. ref_root = btrfs_header_owner(buf);
  2279. nritems = btrfs_header_nritems(buf);
  2280. level = btrfs_header_level(buf);
  2281. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0)
  2282. return 0;
  2283. if (full_backref)
  2284. parent = buf->start;
  2285. else
  2286. parent = 0;
  2287. if (inc)
  2288. action = BTRFS_ADD_DELAYED_REF;
  2289. else
  2290. action = BTRFS_DROP_DELAYED_REF;
  2291. for (i = 0; i < nritems; i++) {
  2292. struct btrfs_ref ref = {
  2293. .action = action,
  2294. .parent = parent,
  2295. .ref_root = ref_root,
  2296. };
  2297. if (level == 0) {
  2298. btrfs_item_key_to_cpu(buf, &key, i);
  2299. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2300. continue;
  2301. fi = btrfs_item_ptr(buf, i,
  2302. struct btrfs_file_extent_item);
  2303. if (btrfs_file_extent_type(buf, fi) ==
  2304. BTRFS_FILE_EXTENT_INLINE)
  2305. continue;
  2306. ref.bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2307. if (ref.bytenr == 0)
  2308. continue;
  2309. ref.num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2310. ref.owning_root = ref_root;
  2311. key.offset -= btrfs_file_extent_offset(buf, fi);
  2312. btrfs_init_data_ref(&ref, key.objectid, key.offset,
  2313. btrfs_root_id(root), for_reloc);
  2314. if (inc)
  2315. ret = btrfs_inc_extent_ref(trans, &ref);
  2316. else
  2317. ret = btrfs_free_extent(trans, &ref);
  2318. if (ret)
  2319. return ret;
  2320. } else {
  2321. /* We don't know the owning_root, leave as 0. */
  2322. ref.bytenr = btrfs_node_blockptr(buf, i);
  2323. ref.num_bytes = fs_info->nodesize;
  2324. btrfs_init_tree_ref(&ref, level - 1,
  2325. btrfs_root_id(root), for_reloc);
  2326. if (inc)
  2327. ret = btrfs_inc_extent_ref(trans, &ref);
  2328. else
  2329. ret = btrfs_free_extent(trans, &ref);
  2330. if (ret)
  2331. return ret;
  2332. }
  2333. }
  2334. return 0;
  2335. }
  2336. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2337. struct extent_buffer *buf, bool full_backref)
  2338. {
  2339. return __btrfs_mod_ref(trans, root, buf, full_backref, true);
  2340. }
  2341. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2342. struct extent_buffer *buf, bool full_backref)
  2343. {
  2344. return __btrfs_mod_ref(trans, root, buf, full_backref, false);
  2345. }
  2346. static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
  2347. {
  2348. struct btrfs_fs_info *fs_info = root->fs_info;
  2349. u64 flags;
  2350. if (data)
  2351. flags = BTRFS_BLOCK_GROUP_DATA;
  2352. else if (root == fs_info->chunk_root)
  2353. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  2354. else if (root == fs_info->remap_root)
  2355. flags = BTRFS_BLOCK_GROUP_METADATA_REMAP;
  2356. else
  2357. flags = BTRFS_BLOCK_GROUP_METADATA;
  2358. return btrfs_get_alloc_profile(fs_info, flags);
  2359. }
  2360. static u64 first_logical_byte(struct btrfs_fs_info *fs_info)
  2361. {
  2362. struct rb_node *leftmost;
  2363. u64 bytenr = 0;
  2364. read_lock(&fs_info->block_group_cache_lock);
  2365. /* Get the block group with the lowest logical start address. */
  2366. leftmost = rb_first_cached(&fs_info->block_group_cache_tree);
  2367. if (leftmost) {
  2368. struct btrfs_block_group *bg;
  2369. bg = rb_entry(leftmost, struct btrfs_block_group, cache_node);
  2370. bytenr = bg->start;
  2371. }
  2372. read_unlock(&fs_info->block_group_cache_lock);
  2373. return bytenr;
  2374. }
  2375. static int pin_down_extent(struct btrfs_trans_handle *trans,
  2376. struct btrfs_block_group *bg,
  2377. u64 bytenr, u64 num_bytes, bool reserved)
  2378. {
  2379. struct btrfs_space_info *space_info = bg->space_info;
  2380. const u64 reserved_bytes = (reserved ? num_bytes : 0);
  2381. spin_lock(&space_info->lock);
  2382. spin_lock(&bg->lock);
  2383. bg->pinned += num_bytes;
  2384. bg->reserved -= reserved_bytes;
  2385. spin_unlock(&bg->lock);
  2386. space_info->bytes_reserved -= reserved_bytes;
  2387. btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
  2388. spin_unlock(&space_info->lock);
  2389. btrfs_set_extent_bit(&trans->transaction->pinned_extents, bytenr,
  2390. bytenr + num_bytes - 1, EXTENT_DIRTY, NULL);
  2391. return 0;
  2392. }
  2393. int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num_bytes)
  2394. {
  2395. struct btrfs_block_group *cache;
  2396. cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
  2397. BUG_ON(!cache); /* Logic error */
  2398. pin_down_extent(trans, cache, bytenr, num_bytes, true);
  2399. btrfs_put_block_group(cache);
  2400. return 0;
  2401. }
  2402. int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
  2403. const struct extent_buffer *eb)
  2404. {
  2405. struct btrfs_block_group *cache;
  2406. int ret;
  2407. cache = btrfs_lookup_block_group(trans->fs_info, eb->start);
  2408. if (!cache)
  2409. return -EINVAL;
  2410. /*
  2411. * Fully cache the free space first so that our pin removes the free space
  2412. * from the cache.
  2413. */
  2414. ret = btrfs_cache_block_group(cache, true);
  2415. if (ret)
  2416. goto out;
  2417. pin_down_extent(trans, cache, eb->start, eb->len, false);
  2418. /* remove us from the free space cache (if we're there at all) */
  2419. ret = btrfs_remove_free_space(cache, eb->start, eb->len);
  2420. out:
  2421. btrfs_put_block_group(cache);
  2422. return ret;
  2423. }
  2424. static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
  2425. u64 start, u64 num_bytes)
  2426. {
  2427. int ret;
  2428. struct btrfs_block_group *block_group;
  2429. block_group = btrfs_lookup_block_group(fs_info, start);
  2430. if (!block_group)
  2431. return -EINVAL;
  2432. ret = btrfs_cache_block_group(block_group, true);
  2433. if (ret)
  2434. goto out;
  2435. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  2436. out:
  2437. btrfs_put_block_group(block_group);
  2438. return ret;
  2439. }
  2440. int btrfs_exclude_logged_extents(struct extent_buffer *eb)
  2441. {
  2442. struct btrfs_fs_info *fs_info = eb->fs_info;
  2443. struct btrfs_file_extent_item *item;
  2444. struct btrfs_key key;
  2445. int found_type;
  2446. int i;
  2447. int ret = 0;
  2448. if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
  2449. return 0;
  2450. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  2451. btrfs_item_key_to_cpu(eb, &key, i);
  2452. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2453. continue;
  2454. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  2455. found_type = btrfs_file_extent_type(eb, item);
  2456. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  2457. continue;
  2458. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  2459. continue;
  2460. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  2461. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  2462. ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
  2463. if (ret)
  2464. break;
  2465. }
  2466. return ret;
  2467. }
  2468. static void
  2469. btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
  2470. {
  2471. atomic_inc(&bg->reservations);
  2472. }
  2473. /*
  2474. * Returns the free cluster for the given space info and sets empty_cluster to
  2475. * what it should be based on the mount options.
  2476. */
  2477. static struct btrfs_free_cluster *
  2478. fetch_cluster_info(struct btrfs_fs_info *fs_info,
  2479. struct btrfs_space_info *space_info, u64 *empty_cluster)
  2480. {
  2481. struct btrfs_free_cluster *ret = NULL;
  2482. *empty_cluster = 0;
  2483. if (btrfs_mixed_space_info(space_info))
  2484. return ret;
  2485. if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
  2486. ret = &fs_info->meta_alloc_cluster;
  2487. if (btrfs_test_opt(fs_info, SSD))
  2488. *empty_cluster = SZ_2M;
  2489. else
  2490. *empty_cluster = SZ_64K;
  2491. } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
  2492. btrfs_test_opt(fs_info, SSD_SPREAD)) {
  2493. *empty_cluster = SZ_2M;
  2494. ret = &fs_info->data_alloc_cluster;
  2495. }
  2496. return ret;
  2497. }
  2498. static int unpin_extent_range(struct btrfs_fs_info *fs_info,
  2499. u64 start, u64 end,
  2500. const bool return_free_space)
  2501. {
  2502. struct btrfs_block_group *cache = NULL;
  2503. struct btrfs_space_info *space_info;
  2504. struct btrfs_free_cluster *cluster = NULL;
  2505. u64 total_unpinned = 0;
  2506. u64 empty_cluster = 0;
  2507. while (start <= end) {
  2508. u64 len;
  2509. bool readonly;
  2510. if (!cache || start >= btrfs_block_group_end(cache)) {
  2511. if (cache)
  2512. btrfs_put_block_group(cache);
  2513. total_unpinned = 0;
  2514. cache = btrfs_lookup_block_group(fs_info, start);
  2515. if (unlikely(cache == NULL)) {
  2516. /* Logic error, something removed the block group. */
  2517. return -EUCLEAN;
  2518. }
  2519. cluster = fetch_cluster_info(fs_info,
  2520. cache->space_info,
  2521. &empty_cluster);
  2522. empty_cluster <<= 1;
  2523. }
  2524. len = btrfs_block_group_end(cache) - start;
  2525. len = min(len, end + 1 - start);
  2526. if (return_free_space)
  2527. btrfs_add_free_space(cache, start, len);
  2528. start += len;
  2529. total_unpinned += len;
  2530. space_info = cache->space_info;
  2531. /*
  2532. * If this space cluster has been marked as fragmented and we've
  2533. * unpinned enough in this block group to potentially allow a
  2534. * cluster to be created inside of it go ahead and clear the
  2535. * fragmented check.
  2536. */
  2537. if (cluster && cluster->fragmented &&
  2538. total_unpinned > empty_cluster) {
  2539. spin_lock(&cluster->lock);
  2540. cluster->fragmented = 0;
  2541. spin_unlock(&cluster->lock);
  2542. }
  2543. spin_lock(&space_info->lock);
  2544. spin_lock(&cache->lock);
  2545. readonly = cache->ro;
  2546. cache->pinned -= len;
  2547. spin_unlock(&cache->lock);
  2548. btrfs_space_info_update_bytes_pinned(space_info, -len);
  2549. space_info->max_extent_size = 0;
  2550. if (readonly) {
  2551. space_info->bytes_readonly += len;
  2552. } else if (btrfs_is_zoned(fs_info)) {
  2553. /* Need reset before reusing in a zoned block group */
  2554. btrfs_space_info_update_bytes_zone_unusable(space_info, len);
  2555. } else if (return_free_space) {
  2556. btrfs_return_free_space(space_info, len);
  2557. }
  2558. spin_unlock(&space_info->lock);
  2559. }
  2560. if (cache)
  2561. btrfs_put_block_group(cache);
  2562. return 0;
  2563. }
  2564. /*
  2565. * Complete the remapping of a block group by removing its chunk stripes and
  2566. * device extents, and adding it to the unused list if there's no longer any
  2567. * extents nominally within it.
  2568. */
  2569. int btrfs_complete_bg_remapping(struct btrfs_block_group *bg)
  2570. {
  2571. struct btrfs_fs_info *fs_info = bg->fs_info;
  2572. struct btrfs_chunk_map *map;
  2573. int ret;
  2574. map = btrfs_get_chunk_map(fs_info, bg->start, 1);
  2575. if (IS_ERR(map))
  2576. return PTR_ERR(map);
  2577. ret = btrfs_last_identity_remap_gone(map, bg);
  2578. if (ret) {
  2579. btrfs_free_chunk_map(map);
  2580. return ret;
  2581. }
  2582. /*
  2583. * Set num_stripes to 0, so that btrfs_remove_dev_extents() won't run a
  2584. * second time.
  2585. */
  2586. map->num_stripes = 0;
  2587. btrfs_free_chunk_map(map);
  2588. if (bg->used == 0) {
  2589. spin_lock(&fs_info->unused_bgs_lock);
  2590. if (!list_empty(&bg->bg_list)) {
  2591. list_del_init(&bg->bg_list);
  2592. btrfs_put_block_group(bg);
  2593. }
  2594. spin_unlock(&fs_info->unused_bgs_lock);
  2595. btrfs_mark_bg_unused(bg);
  2596. }
  2597. return 0;
  2598. }
  2599. void btrfs_handle_fully_remapped_bgs(struct btrfs_fs_info *fs_info)
  2600. {
  2601. struct btrfs_block_group *bg;
  2602. int ret;
  2603. spin_lock(&fs_info->unused_bgs_lock);
  2604. while (!list_empty(&fs_info->fully_remapped_bgs)) {
  2605. bg = list_first_entry(&fs_info->fully_remapped_bgs,
  2606. struct btrfs_block_group, bg_list);
  2607. list_del_init(&bg->bg_list);
  2608. spin_unlock(&fs_info->unused_bgs_lock);
  2609. btrfs_discard_extent(fs_info, bg->start, bg->length, NULL, false);
  2610. ret = btrfs_complete_bg_remapping(bg);
  2611. if (ret) {
  2612. btrfs_put_block_group(bg);
  2613. return;
  2614. }
  2615. btrfs_put_block_group(bg);
  2616. spin_lock(&fs_info->unused_bgs_lock);
  2617. }
  2618. spin_unlock(&fs_info->unused_bgs_lock);
  2619. }
  2620. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
  2621. {
  2622. struct btrfs_fs_info *fs_info = trans->fs_info;
  2623. struct btrfs_block_group *block_group, *tmp;
  2624. struct list_head *deleted_bgs;
  2625. struct extent_io_tree *unpin = &trans->transaction->pinned_extents;
  2626. struct extent_state *cached_state = NULL;
  2627. u64 start;
  2628. u64 end;
  2629. int unpin_error = 0;
  2630. int ret;
  2631. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  2632. btrfs_find_first_extent_bit(unpin, 0, &start, &end, EXTENT_DIRTY, &cached_state);
  2633. while (!TRANS_ABORTED(trans) && cached_state) {
  2634. struct extent_state *next_state;
  2635. if (btrfs_test_opt(fs_info, DISCARD_SYNC)) {
  2636. ret = btrfs_discard_extent(fs_info, start,
  2637. end + 1 - start, NULL, true);
  2638. if (ret) {
  2639. btrfs_warn(fs_info,
  2640. "discard failed for extent [%llu, %llu]: errno=%d %s",
  2641. start, end, ret, btrfs_decode_error(ret));
  2642. }
  2643. }
  2644. next_state = btrfs_next_extent_state(unpin, cached_state);
  2645. btrfs_clear_extent_dirty(unpin, start, end, &cached_state);
  2646. ret = unpin_extent_range(fs_info, start, end, true);
  2647. /*
  2648. * If we get an error unpinning an extent range, store the first
  2649. * error to return later after trying to unpin all ranges and do
  2650. * the sync discards. Our caller will abort the transaction
  2651. * (which already wrote new superblocks) and on the next mount
  2652. * the space will be available as it was pinned by in-memory
  2653. * only structures in this phase.
  2654. */
  2655. if (ret) {
  2656. btrfs_err_rl(fs_info,
  2657. "failed to unpin extent range [%llu, %llu] when committing transaction %llu: %s (%d)",
  2658. start, end, trans->transid,
  2659. btrfs_decode_error(ret), ret);
  2660. if (!unpin_error)
  2661. unpin_error = ret;
  2662. }
  2663. btrfs_free_extent_state(cached_state);
  2664. if (need_resched()) {
  2665. btrfs_free_extent_state(next_state);
  2666. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  2667. cond_resched();
  2668. cached_state = NULL;
  2669. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  2670. btrfs_find_first_extent_bit(unpin, 0, &start, &end,
  2671. EXTENT_DIRTY, &cached_state);
  2672. } else {
  2673. cached_state = next_state;
  2674. if (cached_state) {
  2675. start = cached_state->start;
  2676. end = cached_state->end;
  2677. }
  2678. }
  2679. }
  2680. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  2681. btrfs_free_extent_state(cached_state);
  2682. if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
  2683. btrfs_discard_calc_delay(&fs_info->discard_ctl);
  2684. btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
  2685. }
  2686. /*
  2687. * Transaction is finished. We don't need the lock anymore. We
  2688. * do need to clean up the block groups in case of a transaction
  2689. * abort.
  2690. */
  2691. deleted_bgs = &trans->transaction->deleted_bgs;
  2692. list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
  2693. ret = -EROFS;
  2694. if (!TRANS_ABORTED(trans))
  2695. ret = btrfs_discard_extent(fs_info, block_group->start,
  2696. block_group->length, NULL, true);
  2697. /*
  2698. * Not strictly necessary to lock, as the block_group should be
  2699. * read-only from btrfs_delete_unused_bgs().
  2700. */
  2701. ASSERT(block_group->ro);
  2702. spin_lock(&fs_info->unused_bgs_lock);
  2703. list_del_init(&block_group->bg_list);
  2704. spin_unlock(&fs_info->unused_bgs_lock);
  2705. btrfs_unfreeze_block_group(block_group);
  2706. btrfs_put_block_group(block_group);
  2707. if (ret) {
  2708. const char *errstr = btrfs_decode_error(ret);
  2709. btrfs_warn(fs_info,
  2710. "discard failed while removing blockgroup: errno=%d %s",
  2711. ret, errstr);
  2712. }
  2713. }
  2714. return unpin_error;
  2715. }
  2716. /*
  2717. * Parse an extent item's inline extents looking for a simple quotas owner ref.
  2718. *
  2719. * @fs_info: the btrfs_fs_info for this mount
  2720. * @leaf: a leaf in the extent tree containing the extent item
  2721. * @slot: the slot in the leaf where the extent item is found
  2722. *
  2723. * Returns the objectid of the root that originally allocated the extent item
  2724. * if the inline owner ref is expected and present, otherwise 0.
  2725. *
  2726. * If an extent item has an owner ref item, it will be the first inline ref
  2727. * item. Therefore the logic is to check whether there are any inline ref
  2728. * items, then check the type of the first one.
  2729. */
  2730. u64 btrfs_get_extent_owner_root(struct btrfs_fs_info *fs_info,
  2731. struct extent_buffer *leaf, int slot)
  2732. {
  2733. struct btrfs_extent_item *ei;
  2734. struct btrfs_extent_inline_ref *iref;
  2735. struct btrfs_extent_owner_ref *oref;
  2736. unsigned long ptr;
  2737. unsigned long end;
  2738. int type;
  2739. if (!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA))
  2740. return 0;
  2741. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  2742. ptr = (unsigned long)(ei + 1);
  2743. end = (unsigned long)ei + btrfs_item_size(leaf, slot);
  2744. /* No inline ref items of any kind, can't check type. */
  2745. if (ptr == end)
  2746. return 0;
  2747. iref = (struct btrfs_extent_inline_ref *)ptr;
  2748. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
  2749. /* We found an owner ref, get the root out of it. */
  2750. if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
  2751. oref = (struct btrfs_extent_owner_ref *)(&iref->offset);
  2752. return btrfs_extent_owner_ref_root_id(leaf, oref);
  2753. }
  2754. /* We have inline refs, but not an owner ref. */
  2755. return 0;
  2756. }
  2757. static int do_free_extent_accounting(struct btrfs_trans_handle *trans,
  2758. u64 bytenr, struct btrfs_squota_delta *delta,
  2759. struct btrfs_path *path)
  2760. {
  2761. int ret;
  2762. bool remapped = false;
  2763. u64 num_bytes = delta->num_bytes;
  2764. /* Returns 1 on success and 0 on no-op. */
  2765. ret = btrfs_remove_extent_from_remap_tree(trans, path, bytenr, num_bytes);
  2766. if (unlikely(ret < 0)) {
  2767. btrfs_abort_transaction(trans, ret);
  2768. return ret;
  2769. } else if (ret == 1) {
  2770. remapped = true;
  2771. }
  2772. if (delta->is_data) {
  2773. struct btrfs_root *csum_root;
  2774. csum_root = btrfs_csum_root(trans->fs_info, bytenr);
  2775. if (unlikely(!csum_root)) {
  2776. ret = -EUCLEAN;
  2777. btrfs_abort_transaction(trans, ret);
  2778. btrfs_err(trans->fs_info,
  2779. "missing csum root for extent at bytenr %llu",
  2780. bytenr);
  2781. return ret;
  2782. }
  2783. ret = btrfs_del_csums(trans, csum_root, bytenr, num_bytes);
  2784. if (unlikely(ret)) {
  2785. btrfs_abort_transaction(trans, ret);
  2786. return ret;
  2787. }
  2788. ret = btrfs_delete_raid_extent(trans, bytenr, num_bytes);
  2789. if (unlikely(ret)) {
  2790. btrfs_abort_transaction(trans, ret);
  2791. return ret;
  2792. }
  2793. }
  2794. ret = btrfs_record_squota_delta(trans->fs_info, delta);
  2795. if (unlikely(ret)) {
  2796. btrfs_abort_transaction(trans, ret);
  2797. return ret;
  2798. }
  2799. /* If remapped, FST has already been taken care of in remove_range_from_remap_tree(). */
  2800. if (!remapped) {
  2801. ret = btrfs_add_to_free_space_tree(trans, bytenr, num_bytes);
  2802. if (unlikely(ret)) {
  2803. btrfs_abort_transaction(trans, ret);
  2804. return ret;
  2805. }
  2806. }
  2807. ret = btrfs_update_block_group(trans, bytenr, num_bytes, false);
  2808. if (ret)
  2809. btrfs_abort_transaction(trans, ret);
  2810. return ret;
  2811. }
  2812. #define abort_and_dump(trans, path, fmt, args...) \
  2813. ({ \
  2814. btrfs_abort_transaction(trans, -EUCLEAN); \
  2815. btrfs_print_leaf(path->nodes[0]); \
  2816. btrfs_crit(trans->fs_info, fmt, ##args); \
  2817. })
  2818. /*
  2819. * Drop one or more refs of @node.
  2820. *
  2821. * 1. Locate the extent refs.
  2822. * It's either inline in EXTENT/METADATA_ITEM or in keyed SHARED_* item.
  2823. * Locate it, then reduce the refs number or remove the ref line completely.
  2824. *
  2825. * 2. Update the refs count in EXTENT/METADATA_ITEM
  2826. *
  2827. * Inline backref case:
  2828. *
  2829. * in extent tree we have:
  2830. *
  2831. * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
  2832. * refs 2 gen 6 flags DATA
  2833. * extent data backref root FS_TREE objectid 258 offset 0 count 1
  2834. * extent data backref root FS_TREE objectid 257 offset 0 count 1
  2835. *
  2836. * This function gets called with:
  2837. *
  2838. * node->bytenr = 13631488
  2839. * node->num_bytes = 1048576
  2840. * root_objectid = FS_TREE
  2841. * owner_objectid = 257
  2842. * owner_offset = 0
  2843. * refs_to_drop = 1
  2844. *
  2845. * Then we should get some like:
  2846. *
  2847. * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
  2848. * refs 1 gen 6 flags DATA
  2849. * extent data backref root FS_TREE objectid 258 offset 0 count 1
  2850. *
  2851. * Keyed backref case:
  2852. *
  2853. * in extent tree we have:
  2854. *
  2855. * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
  2856. * refs 754 gen 6 flags DATA
  2857. * [...]
  2858. * item 2 key (13631488 EXTENT_DATA_REF <HASH>) itemoff 3915 itemsize 28
  2859. * extent data backref root FS_TREE objectid 866 offset 0 count 1
  2860. *
  2861. * This function get called with:
  2862. *
  2863. * node->bytenr = 13631488
  2864. * node->num_bytes = 1048576
  2865. * root_objectid = FS_TREE
  2866. * owner_objectid = 866
  2867. * owner_offset = 0
  2868. * refs_to_drop = 1
  2869. *
  2870. * Then we should get some like:
  2871. *
  2872. * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
  2873. * refs 753 gen 6 flags DATA
  2874. *
  2875. * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed.
  2876. */
  2877. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  2878. struct btrfs_delayed_ref_head *href,
  2879. const struct btrfs_delayed_ref_node *node,
  2880. struct btrfs_delayed_extent_op *extent_op)
  2881. {
  2882. struct btrfs_fs_info *info = trans->fs_info;
  2883. struct btrfs_key key;
  2884. BTRFS_PATH_AUTO_FREE(path);
  2885. struct btrfs_root *extent_root;
  2886. struct extent_buffer *leaf;
  2887. struct btrfs_extent_item *ei;
  2888. struct btrfs_extent_inline_ref *iref;
  2889. int ret;
  2890. int is_data;
  2891. int extent_slot = 0;
  2892. int found_extent = 0;
  2893. int num_to_del = 1;
  2894. int refs_to_drop = node->ref_mod;
  2895. u32 item_size;
  2896. u64 refs;
  2897. u64 bytenr = node->bytenr;
  2898. u64 num_bytes = node->num_bytes;
  2899. u64 owner_objectid = btrfs_delayed_ref_owner(node);
  2900. u64 owner_offset = btrfs_delayed_ref_offset(node);
  2901. bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
  2902. u64 delayed_ref_root = href->owning_root;
  2903. extent_root = btrfs_extent_root(info, bytenr);
  2904. if (unlikely(!extent_root)) {
  2905. btrfs_err(info,
  2906. "missing extent root for extent at bytenr %llu", bytenr);
  2907. return -EUCLEAN;
  2908. }
  2909. path = btrfs_alloc_path();
  2910. if (!path)
  2911. return -ENOMEM;
  2912. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  2913. if (unlikely(!is_data && refs_to_drop != 1)) {
  2914. btrfs_crit(info,
  2915. "invalid refs_to_drop, dropping more than 1 refs for tree block %llu refs_to_drop %u",
  2916. node->bytenr, refs_to_drop);
  2917. ret = -EINVAL;
  2918. btrfs_abort_transaction(trans, ret);
  2919. return ret;
  2920. }
  2921. if (is_data)
  2922. skinny_metadata = false;
  2923. ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
  2924. node->parent, node->ref_root, owner_objectid,
  2925. owner_offset);
  2926. if (ret == 0) {
  2927. /*
  2928. * Either the inline backref or the SHARED_DATA_REF/
  2929. * SHARED_BLOCK_REF is found
  2930. *
  2931. * Here is a quick path to locate EXTENT/METADATA_ITEM.
  2932. * It's possible the EXTENT/METADATA_ITEM is near current slot.
  2933. */
  2934. extent_slot = path->slots[0];
  2935. while (extent_slot >= 0) {
  2936. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2937. extent_slot);
  2938. if (key.objectid != bytenr)
  2939. break;
  2940. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  2941. key.offset == num_bytes) {
  2942. found_extent = 1;
  2943. break;
  2944. }
  2945. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  2946. key.offset == owner_objectid) {
  2947. found_extent = 1;
  2948. break;
  2949. }
  2950. /* Quick path didn't find the EXTENT/METADATA_ITEM */
  2951. if (path->slots[0] - extent_slot > 5)
  2952. break;
  2953. extent_slot--;
  2954. }
  2955. if (!found_extent) {
  2956. if (unlikely(iref)) {
  2957. abort_and_dump(trans, path,
  2958. "invalid iref slot %u, no EXTENT/METADATA_ITEM found but has inline extent ref",
  2959. path->slots[0]);
  2960. return -EUCLEAN;
  2961. }
  2962. /* Must be SHARED_* item, remove the backref first */
  2963. ret = remove_extent_backref(trans, extent_root, path,
  2964. NULL, refs_to_drop, is_data);
  2965. if (unlikely(ret)) {
  2966. btrfs_abort_transaction(trans, ret);
  2967. return ret;
  2968. }
  2969. btrfs_release_path(path);
  2970. /* Slow path to locate EXTENT/METADATA_ITEM */
  2971. key.objectid = bytenr;
  2972. key.type = BTRFS_EXTENT_ITEM_KEY;
  2973. key.offset = num_bytes;
  2974. if (!is_data && skinny_metadata) {
  2975. key.type = BTRFS_METADATA_ITEM_KEY;
  2976. key.offset = owner_objectid;
  2977. }
  2978. ret = btrfs_search_slot(trans, extent_root,
  2979. &key, path, -1, 1);
  2980. if (ret > 0 && skinny_metadata && path->slots[0]) {
  2981. /*
  2982. * Couldn't find our skinny metadata item,
  2983. * see if we have ye olde extent item.
  2984. */
  2985. path->slots[0]--;
  2986. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2987. path->slots[0]);
  2988. if (key.objectid == bytenr &&
  2989. key.type == BTRFS_EXTENT_ITEM_KEY &&
  2990. key.offset == num_bytes)
  2991. ret = 0;
  2992. }
  2993. if (ret > 0 && skinny_metadata) {
  2994. skinny_metadata = false;
  2995. key.objectid = bytenr;
  2996. key.type = BTRFS_EXTENT_ITEM_KEY;
  2997. key.offset = num_bytes;
  2998. btrfs_release_path(path);
  2999. ret = btrfs_search_slot(trans, extent_root,
  3000. &key, path, -1, 1);
  3001. }
  3002. if (ret) {
  3003. if (ret > 0)
  3004. btrfs_print_leaf(path->nodes[0]);
  3005. btrfs_err(info,
  3006. "umm, got %d back from search, was looking for %llu, slot %d",
  3007. ret, bytenr, path->slots[0]);
  3008. }
  3009. if (unlikely(ret < 0)) {
  3010. btrfs_abort_transaction(trans, ret);
  3011. return ret;
  3012. }
  3013. extent_slot = path->slots[0];
  3014. }
  3015. } else if (WARN_ON(ret == -ENOENT)) {
  3016. abort_and_dump(trans, path,
  3017. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu slot %d",
  3018. bytenr, node->parent, node->ref_root, owner_objectid,
  3019. owner_offset, path->slots[0]);
  3020. return ret;
  3021. } else {
  3022. btrfs_abort_transaction(trans, ret);
  3023. return ret;
  3024. }
  3025. leaf = path->nodes[0];
  3026. item_size = btrfs_item_size(leaf, extent_slot);
  3027. if (unlikely(item_size < sizeof(*ei))) {
  3028. ret = -EUCLEAN;
  3029. btrfs_err(trans->fs_info,
  3030. "unexpected extent item size, has %u expect >= %zu",
  3031. item_size, sizeof(*ei));
  3032. btrfs_abort_transaction(trans, ret);
  3033. return ret;
  3034. }
  3035. ei = btrfs_item_ptr(leaf, extent_slot,
  3036. struct btrfs_extent_item);
  3037. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  3038. key.type == BTRFS_EXTENT_ITEM_KEY) {
  3039. struct btrfs_tree_block_info *bi;
  3040. if (unlikely(item_size < sizeof(*ei) + sizeof(*bi))) {
  3041. abort_and_dump(trans, path,
  3042. "invalid extent item size for key (%llu, %u, %llu) slot %u owner %llu, has %u expect >= %zu",
  3043. key.objectid, key.type, key.offset,
  3044. path->slots[0], owner_objectid, item_size,
  3045. sizeof(*ei) + sizeof(*bi));
  3046. return -EUCLEAN;
  3047. }
  3048. bi = (struct btrfs_tree_block_info *)(ei + 1);
  3049. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  3050. }
  3051. refs = btrfs_extent_refs(leaf, ei);
  3052. if (unlikely(refs < refs_to_drop)) {
  3053. abort_and_dump(trans, path,
  3054. "trying to drop %d refs but we only have %llu for bytenr %llu slot %u",
  3055. refs_to_drop, refs, bytenr, path->slots[0]);
  3056. return -EUCLEAN;
  3057. }
  3058. refs -= refs_to_drop;
  3059. if (refs > 0) {
  3060. if (extent_op)
  3061. __run_delayed_extent_op(extent_op, leaf, ei);
  3062. /*
  3063. * In the case of inline back ref, reference count will
  3064. * be updated by remove_extent_backref
  3065. */
  3066. if (iref) {
  3067. if (unlikely(!found_extent)) {
  3068. abort_and_dump(trans, path,
  3069. "invalid iref, got inlined extent ref but no EXTENT/METADATA_ITEM found, slot %u",
  3070. path->slots[0]);
  3071. return -EUCLEAN;
  3072. }
  3073. } else {
  3074. btrfs_set_extent_refs(leaf, ei, refs);
  3075. }
  3076. if (found_extent) {
  3077. ret = remove_extent_backref(trans, extent_root, path,
  3078. iref, refs_to_drop, is_data);
  3079. if (unlikely(ret)) {
  3080. btrfs_abort_transaction(trans, ret);
  3081. return ret;
  3082. }
  3083. }
  3084. } else {
  3085. struct btrfs_squota_delta delta = {
  3086. .root = delayed_ref_root,
  3087. .num_bytes = num_bytes,
  3088. .is_data = is_data,
  3089. .is_inc = false,
  3090. .generation = btrfs_extent_generation(leaf, ei),
  3091. };
  3092. /* In this branch refs == 1 */
  3093. if (found_extent) {
  3094. if (unlikely(is_data && refs_to_drop !=
  3095. extent_data_ref_count(path, iref))) {
  3096. abort_and_dump(trans, path,
  3097. "invalid refs_to_drop, current refs %u refs_to_drop %u slot %u",
  3098. extent_data_ref_count(path, iref),
  3099. refs_to_drop, path->slots[0]);
  3100. return -EUCLEAN;
  3101. }
  3102. if (iref) {
  3103. if (unlikely(path->slots[0] != extent_slot)) {
  3104. abort_and_dump(trans, path,
  3105. "invalid iref, extent item key " BTRFS_KEY_FMT " slot %u doesn't have wanted iref",
  3106. BTRFS_KEY_FMT_VALUE(&key),
  3107. path->slots[0]);
  3108. return -EUCLEAN;
  3109. }
  3110. } else {
  3111. /*
  3112. * No inline ref, we must be at SHARED_* item,
  3113. * And it's single ref, it must be:
  3114. * | extent_slot ||extent_slot + 1|
  3115. * [ EXTENT/METADATA_ITEM ][ SHARED_* ITEM ]
  3116. */
  3117. if (unlikely(path->slots[0] != extent_slot + 1)) {
  3118. abort_and_dump(trans, path,
  3119. "invalid SHARED_* item slot %u, previous item is not EXTENT/METADATA_ITEM",
  3120. path->slots[0]);
  3121. return -EUCLEAN;
  3122. }
  3123. path->slots[0] = extent_slot;
  3124. num_to_del = 2;
  3125. }
  3126. }
  3127. /*
  3128. * We can't infer the data owner from the delayed ref, so we need
  3129. * to try to get it from the owning ref item.
  3130. *
  3131. * If it is not present, then that extent was not written under
  3132. * simple quotas mode, so we don't need to account for its deletion.
  3133. */
  3134. if (is_data)
  3135. delta.root = btrfs_get_extent_owner_root(trans->fs_info,
  3136. leaf, extent_slot);
  3137. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  3138. num_to_del);
  3139. if (unlikely(ret)) {
  3140. btrfs_abort_transaction(trans, ret);
  3141. return ret;
  3142. }
  3143. btrfs_release_path(path);
  3144. ret = do_free_extent_accounting(trans, bytenr, &delta, path);
  3145. }
  3146. btrfs_release_path(path);
  3147. return ret;
  3148. }
  3149. /*
  3150. * when we free an block, it is possible (and likely) that we free the last
  3151. * delayed ref for that extent as well. This searches the delayed ref tree for
  3152. * a given extent, and if there are no other delayed refs to be processed, it
  3153. * removes it from the tree.
  3154. */
  3155. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  3156. u64 bytenr)
  3157. {
  3158. struct btrfs_fs_info *fs_info = trans->fs_info;
  3159. struct btrfs_delayed_ref_head *head;
  3160. struct btrfs_delayed_ref_root *delayed_refs;
  3161. int ret = 0;
  3162. delayed_refs = &trans->transaction->delayed_refs;
  3163. spin_lock(&delayed_refs->lock);
  3164. head = btrfs_find_delayed_ref_head(fs_info, delayed_refs, bytenr);
  3165. if (!head)
  3166. goto out_delayed_unlock;
  3167. spin_lock(&head->lock);
  3168. if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
  3169. goto out;
  3170. if (cleanup_extent_op(head) != NULL)
  3171. goto out;
  3172. /*
  3173. * waiting for the lock here would deadlock. If someone else has it
  3174. * locked they are already in the process of dropping it anyway
  3175. */
  3176. if (!mutex_trylock(&head->mutex))
  3177. goto out;
  3178. btrfs_delete_ref_head(fs_info, delayed_refs, head);
  3179. head->processing = false;
  3180. spin_unlock(&head->lock);
  3181. spin_unlock(&delayed_refs->lock);
  3182. BUG_ON(head->extent_op);
  3183. if (head->must_insert_reserved)
  3184. ret = 1;
  3185. btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
  3186. mutex_unlock(&head->mutex);
  3187. btrfs_put_delayed_ref_head(head);
  3188. return ret;
  3189. out:
  3190. spin_unlock(&head->lock);
  3191. out_delayed_unlock:
  3192. spin_unlock(&delayed_refs->lock);
  3193. return 0;
  3194. }
  3195. int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  3196. u64 root_id,
  3197. struct extent_buffer *buf,
  3198. u64 parent, int last_ref)
  3199. {
  3200. struct btrfs_fs_info *fs_info = trans->fs_info;
  3201. struct btrfs_block_group *bg;
  3202. int ret;
  3203. if (root_id != BTRFS_TREE_LOG_OBJECTID) {
  3204. struct btrfs_ref generic_ref = {
  3205. .action = BTRFS_DROP_DELAYED_REF,
  3206. .bytenr = buf->start,
  3207. .num_bytes = buf->len,
  3208. .parent = parent,
  3209. .owning_root = btrfs_header_owner(buf),
  3210. .ref_root = root_id,
  3211. };
  3212. /*
  3213. * Assert that the extent buffer is not cleared due to
  3214. * EXTENT_BUFFER_ZONED_ZEROOUT. Please refer
  3215. * btrfs_clear_buffer_dirty() and btree_csum_one_bio() for
  3216. * detail.
  3217. */
  3218. ASSERT(btrfs_header_bytenr(buf) != 0);
  3219. btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf), 0, false);
  3220. btrfs_ref_tree_mod(fs_info, &generic_ref);
  3221. ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL);
  3222. if (ret < 0)
  3223. return ret;
  3224. }
  3225. if (!last_ref)
  3226. return 0;
  3227. if (btrfs_header_generation(buf) != trans->transid)
  3228. return 0;
  3229. if (root_id != BTRFS_TREE_LOG_OBJECTID) {
  3230. ret = check_ref_cleanup(trans, buf->start);
  3231. if (!ret)
  3232. return 0;
  3233. }
  3234. bg = btrfs_lookup_block_group(fs_info, buf->start);
  3235. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  3236. pin_down_extent(trans, bg, buf->start, buf->len, true);
  3237. btrfs_put_block_group(bg);
  3238. return 0;
  3239. }
  3240. /*
  3241. * If there are tree mod log users we may have recorded mod log
  3242. * operations for this node. If we re-allocate this node we
  3243. * could replay operations on this node that happened when it
  3244. * existed in a completely different root. For example if it
  3245. * was part of root A, then was reallocated to root B, and we
  3246. * are doing a btrfs_old_search_slot(root b), we could replay
  3247. * operations that happened when the block was part of root A,
  3248. * giving us an inconsistent view of the btree.
  3249. *
  3250. * We are safe from races here because at this point no other
  3251. * node or root points to this extent buffer, so if after this
  3252. * check a new tree mod log user joins we will not have an
  3253. * existing log of operations on this node that we have to
  3254. * contend with.
  3255. */
  3256. if (test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags)
  3257. || btrfs_is_zoned(fs_info)) {
  3258. pin_down_extent(trans, bg, buf->start, buf->len, true);
  3259. btrfs_put_block_group(bg);
  3260. return 0;
  3261. }
  3262. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  3263. btrfs_add_free_space(bg, buf->start, buf->len);
  3264. btrfs_free_reserved_bytes(bg, buf->len, false);
  3265. btrfs_put_block_group(bg);
  3266. trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
  3267. return 0;
  3268. }
  3269. /* Can return -ENOMEM */
  3270. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
  3271. {
  3272. struct btrfs_fs_info *fs_info = trans->fs_info;
  3273. int ret;
  3274. if (btrfs_is_testing(fs_info))
  3275. return 0;
  3276. /*
  3277. * tree log blocks never actually go into the extent allocation
  3278. * tree, just update pinning info and exit early.
  3279. */
  3280. if (ref->ref_root == BTRFS_TREE_LOG_OBJECTID) {
  3281. btrfs_pin_extent(trans, ref->bytenr, ref->num_bytes);
  3282. ret = 0;
  3283. } else if (ref->type == BTRFS_REF_METADATA) {
  3284. ret = btrfs_add_delayed_tree_ref(trans, ref, NULL);
  3285. } else {
  3286. ret = btrfs_add_delayed_data_ref(trans, ref, 0);
  3287. }
  3288. if (ref->ref_root != BTRFS_TREE_LOG_OBJECTID)
  3289. btrfs_ref_tree_mod(fs_info, ref);
  3290. return ret;
  3291. }
  3292. enum btrfs_loop_type {
  3293. /*
  3294. * Start caching block groups but do not wait for progress or for them
  3295. * to be done.
  3296. */
  3297. LOOP_CACHING_NOWAIT,
  3298. /*
  3299. * Wait for the block group free_space >= the space we're waiting for if
  3300. * the block group isn't cached.
  3301. */
  3302. LOOP_CACHING_WAIT,
  3303. /*
  3304. * Allow allocations to happen from block groups that do not yet have a
  3305. * size classification.
  3306. */
  3307. LOOP_UNSET_SIZE_CLASS,
  3308. /*
  3309. * Allocate a chunk and then retry the allocation.
  3310. */
  3311. LOOP_ALLOC_CHUNK,
  3312. /*
  3313. * Ignore the size class restrictions for this allocation.
  3314. */
  3315. LOOP_WRONG_SIZE_CLASS,
  3316. /*
  3317. * Ignore the empty size, only try to allocate the number of bytes
  3318. * needed for this allocation.
  3319. */
  3320. LOOP_NO_EMPTY_SIZE,
  3321. };
  3322. static inline void
  3323. btrfs_lock_block_group(struct btrfs_block_group *cache, bool delalloc)
  3324. {
  3325. if (delalloc)
  3326. down_read(&cache->data_rwsem);
  3327. }
  3328. static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
  3329. bool delalloc)
  3330. {
  3331. btrfs_get_block_group(cache);
  3332. if (delalloc)
  3333. down_read(&cache->data_rwsem);
  3334. }
  3335. static struct btrfs_block_group *btrfs_lock_cluster(
  3336. struct btrfs_block_group *block_group,
  3337. struct btrfs_free_cluster *cluster,
  3338. bool delalloc)
  3339. __acquires(&cluster->refill_lock)
  3340. {
  3341. struct btrfs_block_group *used_bg = NULL;
  3342. spin_lock(&cluster->refill_lock);
  3343. while (1) {
  3344. used_bg = cluster->block_group;
  3345. if (!used_bg)
  3346. return NULL;
  3347. if (used_bg == block_group)
  3348. return used_bg;
  3349. btrfs_get_block_group(used_bg);
  3350. if (!delalloc)
  3351. return used_bg;
  3352. if (down_read_trylock(&used_bg->data_rwsem))
  3353. return used_bg;
  3354. spin_unlock(&cluster->refill_lock);
  3355. /* We should only have one-level nested. */
  3356. down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
  3357. spin_lock(&cluster->refill_lock);
  3358. if (used_bg == cluster->block_group)
  3359. return used_bg;
  3360. up_read(&used_bg->data_rwsem);
  3361. btrfs_put_block_group(used_bg);
  3362. }
  3363. }
  3364. static inline void
  3365. btrfs_release_block_group(struct btrfs_block_group *cache, bool delalloc)
  3366. {
  3367. if (delalloc)
  3368. up_read(&cache->data_rwsem);
  3369. btrfs_put_block_group(cache);
  3370. }
  3371. static bool find_free_extent_check_size_class(const struct find_free_extent_ctl *ffe_ctl,
  3372. const struct btrfs_block_group *bg)
  3373. {
  3374. if (ffe_ctl->policy == BTRFS_EXTENT_ALLOC_ZONED)
  3375. return true;
  3376. if (!btrfs_block_group_should_use_size_class(bg))
  3377. return true;
  3378. if (ffe_ctl->loop >= LOOP_WRONG_SIZE_CLASS)
  3379. return true;
  3380. if (ffe_ctl->loop >= LOOP_UNSET_SIZE_CLASS &&
  3381. bg->size_class == BTRFS_BG_SZ_NONE)
  3382. return true;
  3383. return ffe_ctl->size_class == bg->size_class;
  3384. }
  3385. /*
  3386. * Helper function for find_free_extent().
  3387. *
  3388. * Return -ENOENT to inform caller that we need fallback to unclustered mode.
  3389. * Return >0 to inform caller that we find nothing
  3390. * Return 0 means we have found a location and set ffe_ctl->found_offset.
  3391. */
  3392. static int find_free_extent_clustered(struct btrfs_block_group *bg,
  3393. struct find_free_extent_ctl *ffe_ctl,
  3394. struct btrfs_block_group **cluster_bg_ret)
  3395. {
  3396. struct btrfs_block_group *cluster_bg;
  3397. struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
  3398. u64 aligned_cluster;
  3399. u64 offset;
  3400. int ret;
  3401. cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
  3402. if (!cluster_bg)
  3403. goto refill_cluster;
  3404. if (cluster_bg != bg && (cluster_bg->ro ||
  3405. !block_group_bits(cluster_bg, ffe_ctl->flags) ||
  3406. !find_free_extent_check_size_class(ffe_ctl, cluster_bg)))
  3407. goto release_cluster;
  3408. offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
  3409. ffe_ctl->num_bytes, cluster_bg->start,
  3410. &ffe_ctl->max_extent_size);
  3411. if (offset) {
  3412. /* We have a block, we're done */
  3413. spin_unlock(&last_ptr->refill_lock);
  3414. trace_btrfs_reserve_extent_cluster(cluster_bg, ffe_ctl);
  3415. *cluster_bg_ret = cluster_bg;
  3416. ffe_ctl->found_offset = offset;
  3417. return 0;
  3418. }
  3419. WARN_ON(last_ptr->block_group != cluster_bg);
  3420. release_cluster:
  3421. /*
  3422. * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
  3423. * lets just skip it and let the allocator find whatever block it can
  3424. * find. If we reach this point, we will have tried the cluster
  3425. * allocator plenty of times and not have found anything, so we are
  3426. * likely way too fragmented for the clustering stuff to find anything.
  3427. *
  3428. * However, if the cluster is taken from the current block group,
  3429. * release the cluster first, so that we stand a better chance of
  3430. * succeeding in the unclustered allocation.
  3431. */
  3432. if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
  3433. spin_unlock(&last_ptr->refill_lock);
  3434. btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
  3435. return -ENOENT;
  3436. }
  3437. /* This cluster didn't work out, free it and start over */
  3438. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  3439. if (cluster_bg != bg)
  3440. btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
  3441. refill_cluster:
  3442. if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
  3443. spin_unlock(&last_ptr->refill_lock);
  3444. return -ENOENT;
  3445. }
  3446. aligned_cluster = max_t(u64,
  3447. ffe_ctl->empty_cluster + ffe_ctl->empty_size,
  3448. bg->full_stripe_len);
  3449. ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
  3450. ffe_ctl->num_bytes, aligned_cluster);
  3451. if (ret == 0) {
  3452. /* Now pull our allocation out of this cluster */
  3453. offset = btrfs_alloc_from_cluster(bg, last_ptr,
  3454. ffe_ctl->num_bytes, ffe_ctl->search_start,
  3455. &ffe_ctl->max_extent_size);
  3456. if (offset) {
  3457. /* We found one, proceed */
  3458. spin_unlock(&last_ptr->refill_lock);
  3459. ffe_ctl->found_offset = offset;
  3460. trace_btrfs_reserve_extent_cluster(bg, ffe_ctl);
  3461. return 0;
  3462. }
  3463. }
  3464. /*
  3465. * At this point we either didn't find a cluster or we weren't able to
  3466. * allocate a block from our cluster. Free the cluster we've been
  3467. * trying to use, and go to the next block group.
  3468. */
  3469. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  3470. spin_unlock(&last_ptr->refill_lock);
  3471. return 1;
  3472. }
  3473. /*
  3474. * Return >0 to inform caller that we find nothing
  3475. * Return 0 when we found an free extent and set ffe_ctrl->found_offset
  3476. */
  3477. static int find_free_extent_unclustered(struct btrfs_block_group *bg,
  3478. struct find_free_extent_ctl *ffe_ctl)
  3479. {
  3480. struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
  3481. u64 offset;
  3482. /*
  3483. * We are doing an unclustered allocation, set the fragmented flag so
  3484. * we don't bother trying to setup a cluster again until we get more
  3485. * space.
  3486. */
  3487. if (unlikely(last_ptr)) {
  3488. spin_lock(&last_ptr->lock);
  3489. last_ptr->fragmented = 1;
  3490. spin_unlock(&last_ptr->lock);
  3491. }
  3492. if (ffe_ctl->cached) {
  3493. struct btrfs_free_space_ctl *free_space_ctl;
  3494. free_space_ctl = bg->free_space_ctl;
  3495. spin_lock(&free_space_ctl->tree_lock);
  3496. if (free_space_ctl->free_space <
  3497. ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
  3498. ffe_ctl->empty_size) {
  3499. ffe_ctl->total_free_space = max_t(u64,
  3500. ffe_ctl->total_free_space,
  3501. free_space_ctl->free_space);
  3502. spin_unlock(&free_space_ctl->tree_lock);
  3503. return 1;
  3504. }
  3505. spin_unlock(&free_space_ctl->tree_lock);
  3506. }
  3507. offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
  3508. ffe_ctl->num_bytes, ffe_ctl->empty_size,
  3509. &ffe_ctl->max_extent_size);
  3510. if (!offset)
  3511. return 1;
  3512. ffe_ctl->found_offset = offset;
  3513. return 0;
  3514. }
  3515. static int do_allocation_clustered(struct btrfs_block_group *block_group,
  3516. struct find_free_extent_ctl *ffe_ctl,
  3517. struct btrfs_block_group **bg_ret)
  3518. {
  3519. int ret;
  3520. /* We want to try and use the cluster allocator, so lets look there */
  3521. if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) {
  3522. ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret);
  3523. if (ret >= 0)
  3524. return ret;
  3525. /* ret == -ENOENT case falls through */
  3526. }
  3527. return find_free_extent_unclustered(block_group, ffe_ctl);
  3528. }
  3529. /*
  3530. * Tree-log block group locking
  3531. * ============================
  3532. *
  3533. * fs_info::treelog_bg_lock protects the fs_info::treelog_bg which
  3534. * indicates the starting address of a block group, which is reserved only
  3535. * for tree-log metadata.
  3536. *
  3537. * Lock nesting
  3538. * ============
  3539. *
  3540. * space_info::lock
  3541. * block_group::lock
  3542. * fs_info::treelog_bg_lock
  3543. */
  3544. /*
  3545. * Simple allocator for sequential-only block group. It only allows sequential
  3546. * allocation. No need to play with trees. This function also reserves the
  3547. * bytes as in btrfs_add_reserved_bytes.
  3548. */
  3549. static int do_allocation_zoned(struct btrfs_block_group *block_group,
  3550. struct find_free_extent_ctl *ffe_ctl,
  3551. struct btrfs_block_group **bg_ret)
  3552. {
  3553. struct btrfs_fs_info *fs_info = block_group->fs_info;
  3554. struct btrfs_space_info *space_info = block_group->space_info;
  3555. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  3556. u64 start = block_group->start;
  3557. u64 num_bytes = ffe_ctl->num_bytes;
  3558. u64 avail;
  3559. u64 bytenr = block_group->start;
  3560. u64 log_bytenr;
  3561. u64 data_reloc_bytenr;
  3562. int ret = 0;
  3563. bool skip = false;
  3564. ASSERT(btrfs_is_zoned(block_group->fs_info));
  3565. /*
  3566. * Do not allow non-tree-log blocks in the dedicated tree-log block
  3567. * group, and vice versa.
  3568. */
  3569. spin_lock(&fs_info->treelog_bg_lock);
  3570. log_bytenr = fs_info->treelog_bg;
  3571. if (log_bytenr && ((ffe_ctl->for_treelog && bytenr != log_bytenr) ||
  3572. (!ffe_ctl->for_treelog && bytenr == log_bytenr)))
  3573. skip = true;
  3574. spin_unlock(&fs_info->treelog_bg_lock);
  3575. if (skip)
  3576. return 1;
  3577. /*
  3578. * Do not allow non-relocation blocks in the dedicated relocation block
  3579. * group, and vice versa.
  3580. */
  3581. spin_lock(&fs_info->relocation_bg_lock);
  3582. data_reloc_bytenr = fs_info->data_reloc_bg;
  3583. if (data_reloc_bytenr &&
  3584. ((ffe_ctl->for_data_reloc && bytenr != data_reloc_bytenr) ||
  3585. (!ffe_ctl->for_data_reloc && bytenr == data_reloc_bytenr)))
  3586. skip = true;
  3587. spin_unlock(&fs_info->relocation_bg_lock);
  3588. if (skip)
  3589. return 1;
  3590. /* Check RO and no space case before trying to activate it */
  3591. spin_lock(&block_group->lock);
  3592. if (block_group->ro || btrfs_zoned_bg_is_full(block_group)) {
  3593. ret = 1;
  3594. /*
  3595. * May need to clear fs_info->{treelog,data_reloc}_bg.
  3596. * Return the error after taking the locks.
  3597. */
  3598. }
  3599. spin_unlock(&block_group->lock);
  3600. /* Metadata block group is activated at write time. */
  3601. if (!ret && (block_group->flags & BTRFS_BLOCK_GROUP_DATA) &&
  3602. !btrfs_zone_activate(block_group)) {
  3603. ret = 1;
  3604. /*
  3605. * May need to clear fs_info->{treelog,data_reloc}_bg.
  3606. * Return the error after taking the locks.
  3607. */
  3608. }
  3609. spin_lock(&space_info->lock);
  3610. spin_lock(&block_group->lock);
  3611. spin_lock(&fs_info->treelog_bg_lock);
  3612. spin_lock(&fs_info->relocation_bg_lock);
  3613. if (ret)
  3614. goto out;
  3615. ASSERT(!ffe_ctl->for_treelog ||
  3616. block_group->start == fs_info->treelog_bg ||
  3617. fs_info->treelog_bg == 0);
  3618. ASSERT(!ffe_ctl->for_data_reloc ||
  3619. block_group->start == fs_info->data_reloc_bg ||
  3620. fs_info->data_reloc_bg == 0);
  3621. if (block_group->ro ||
  3622. (!ffe_ctl->for_data_reloc &&
  3623. test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))) {
  3624. ret = 1;
  3625. goto out;
  3626. }
  3627. /*
  3628. * Do not allow currently using block group to be tree-log dedicated
  3629. * block group.
  3630. */
  3631. if (ffe_ctl->for_treelog && !fs_info->treelog_bg &&
  3632. (block_group->used || block_group->reserved)) {
  3633. ret = 1;
  3634. goto out;
  3635. }
  3636. /*
  3637. * Do not allow currently used block group to be the data relocation
  3638. * dedicated block group.
  3639. */
  3640. if (ffe_ctl->for_data_reloc && !fs_info->data_reloc_bg &&
  3641. (block_group->used || block_group->reserved)) {
  3642. ret = 1;
  3643. goto out;
  3644. }
  3645. WARN_ON_ONCE(block_group->alloc_offset > block_group->zone_capacity);
  3646. avail = block_group->zone_capacity - block_group->alloc_offset;
  3647. if (avail < num_bytes) {
  3648. if (ffe_ctl->max_extent_size < avail) {
  3649. /*
  3650. * With sequential allocator, free space is always
  3651. * contiguous
  3652. */
  3653. ffe_ctl->max_extent_size = avail;
  3654. ffe_ctl->total_free_space = avail;
  3655. }
  3656. ret = 1;
  3657. goto out;
  3658. }
  3659. if (ffe_ctl->for_treelog && !fs_info->treelog_bg)
  3660. fs_info->treelog_bg = block_group->start;
  3661. if (ffe_ctl->for_data_reloc) {
  3662. if (!fs_info->data_reloc_bg)
  3663. fs_info->data_reloc_bg = block_group->start;
  3664. /*
  3665. * Do not allow allocations from this block group, unless it is
  3666. * for data relocation. Compared to increasing the ->ro, setting
  3667. * the ->zoned_data_reloc_ongoing flag still allows nocow
  3668. * writers to come in. See btrfs_inc_nocow_writers().
  3669. *
  3670. * We need to disable an allocation to avoid an allocation of
  3671. * regular (non-relocation data) extent. With mix of relocation
  3672. * extents and regular extents, we can dispatch WRITE commands
  3673. * (for relocation extents) and ZONE APPEND commands (for
  3674. * regular extents) at the same time to the same zone, which
  3675. * easily break the write pointer.
  3676. *
  3677. * Also, this flag avoids this block group to be zone finished.
  3678. */
  3679. set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags);
  3680. }
  3681. ffe_ctl->found_offset = start + block_group->alloc_offset;
  3682. block_group->alloc_offset += num_bytes;
  3683. spin_lock(&ctl->tree_lock);
  3684. ctl->free_space -= num_bytes;
  3685. spin_unlock(&ctl->tree_lock);
  3686. /*
  3687. * We do not check if found_offset is aligned to stripesize. The
  3688. * address is anyway rewritten when using zone append writing.
  3689. */
  3690. ffe_ctl->search_start = ffe_ctl->found_offset;
  3691. out:
  3692. if (ret && ffe_ctl->for_treelog)
  3693. fs_info->treelog_bg = 0;
  3694. if (ret && ffe_ctl->for_data_reloc)
  3695. fs_info->data_reloc_bg = 0;
  3696. spin_unlock(&fs_info->relocation_bg_lock);
  3697. spin_unlock(&fs_info->treelog_bg_lock);
  3698. spin_unlock(&block_group->lock);
  3699. spin_unlock(&space_info->lock);
  3700. return ret;
  3701. }
  3702. static int do_allocation(struct btrfs_block_group *block_group,
  3703. struct find_free_extent_ctl *ffe_ctl,
  3704. struct btrfs_block_group **bg_ret)
  3705. {
  3706. switch (ffe_ctl->policy) {
  3707. case BTRFS_EXTENT_ALLOC_CLUSTERED:
  3708. return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
  3709. case BTRFS_EXTENT_ALLOC_ZONED:
  3710. return do_allocation_zoned(block_group, ffe_ctl, bg_ret);
  3711. default:
  3712. BUG();
  3713. }
  3714. }
  3715. static void release_block_group(struct btrfs_block_group *block_group,
  3716. struct find_free_extent_ctl *ffe_ctl,
  3717. bool delalloc)
  3718. {
  3719. switch (ffe_ctl->policy) {
  3720. case BTRFS_EXTENT_ALLOC_CLUSTERED:
  3721. ffe_ctl->retry_uncached = false;
  3722. break;
  3723. case BTRFS_EXTENT_ALLOC_ZONED:
  3724. /* Nothing to do */
  3725. break;
  3726. default:
  3727. BUG();
  3728. }
  3729. BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
  3730. ffe_ctl->index);
  3731. btrfs_release_block_group(block_group, delalloc);
  3732. }
  3733. static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl,
  3734. struct btrfs_key *ins)
  3735. {
  3736. struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
  3737. if (!ffe_ctl->use_cluster && last_ptr) {
  3738. spin_lock(&last_ptr->lock);
  3739. last_ptr->window_start = ins->objectid;
  3740. spin_unlock(&last_ptr->lock);
  3741. }
  3742. }
  3743. static void found_extent(struct find_free_extent_ctl *ffe_ctl,
  3744. struct btrfs_key *ins)
  3745. {
  3746. switch (ffe_ctl->policy) {
  3747. case BTRFS_EXTENT_ALLOC_CLUSTERED:
  3748. found_extent_clustered(ffe_ctl, ins);
  3749. break;
  3750. case BTRFS_EXTENT_ALLOC_ZONED:
  3751. /* Nothing to do */
  3752. break;
  3753. default:
  3754. BUG();
  3755. }
  3756. }
  3757. static int can_allocate_chunk_zoned(struct btrfs_fs_info *fs_info,
  3758. struct find_free_extent_ctl *ffe_ctl)
  3759. {
  3760. /* Block group's activeness is not a requirement for METADATA block groups. */
  3761. if (!(ffe_ctl->flags & BTRFS_BLOCK_GROUP_DATA))
  3762. return 0;
  3763. /* If we can activate new zone, just allocate a chunk and use it */
  3764. if (btrfs_can_activate_zone(fs_info->fs_devices, ffe_ctl->flags))
  3765. return 0;
  3766. /*
  3767. * We already reached the max active zones. Try to finish one block
  3768. * group to make a room for a new block group. This is only possible
  3769. * for a data block group because btrfs_zone_finish() may need to wait
  3770. * for a running transaction which can cause a deadlock for metadata
  3771. * allocation.
  3772. */
  3773. if (ffe_ctl->flags & BTRFS_BLOCK_GROUP_DATA) {
  3774. int ret = btrfs_zone_finish_one_bg(fs_info);
  3775. if (ret == 1)
  3776. return 0;
  3777. else if (ret < 0)
  3778. return ret;
  3779. }
  3780. /*
  3781. * If we have enough free space left in an already active block group
  3782. * and we can't activate any other zone now, do not allow allocating a
  3783. * new chunk and let find_free_extent() retry with a smaller size.
  3784. */
  3785. if (ffe_ctl->max_extent_size >= ffe_ctl->min_alloc_size)
  3786. return -ENOSPC;
  3787. /*
  3788. * Even min_alloc_size is not left in any block groups. Since we cannot
  3789. * activate a new block group, allocating it may not help. Let's tell a
  3790. * caller to try again and hope it progress something by writing some
  3791. * parts of the region. That is only possible for data block groups,
  3792. * where a part of the region can be written.
  3793. */
  3794. if (ffe_ctl->flags & BTRFS_BLOCK_GROUP_DATA)
  3795. return -EAGAIN;
  3796. /*
  3797. * We cannot activate a new block group and no enough space left in any
  3798. * block groups. So, allocating a new block group may not help. But,
  3799. * there is nothing to do anyway, so let's go with it.
  3800. */
  3801. return 0;
  3802. }
  3803. static int can_allocate_chunk(struct btrfs_fs_info *fs_info,
  3804. struct find_free_extent_ctl *ffe_ctl)
  3805. {
  3806. switch (ffe_ctl->policy) {
  3807. case BTRFS_EXTENT_ALLOC_CLUSTERED:
  3808. return 0;
  3809. case BTRFS_EXTENT_ALLOC_ZONED:
  3810. return can_allocate_chunk_zoned(fs_info, ffe_ctl);
  3811. default:
  3812. BUG();
  3813. }
  3814. }
  3815. /*
  3816. * Return >0 means caller needs to re-search for free extent
  3817. * Return 0 means we have the needed free extent.
  3818. * Return <0 means we failed to locate any free extent.
  3819. */
  3820. static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
  3821. struct btrfs_key *ins,
  3822. struct find_free_extent_ctl *ffe_ctl,
  3823. struct btrfs_space_info *space_info,
  3824. bool full_search)
  3825. {
  3826. struct btrfs_root *root = fs_info->chunk_root;
  3827. int ret;
  3828. if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
  3829. ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
  3830. ffe_ctl->orig_have_caching_bg = true;
  3831. if (ins->objectid) {
  3832. found_extent(ffe_ctl, ins);
  3833. return 0;
  3834. }
  3835. if (ffe_ctl->loop >= LOOP_CACHING_WAIT && ffe_ctl->have_caching_bg)
  3836. return 1;
  3837. ffe_ctl->index++;
  3838. if (ffe_ctl->index < BTRFS_NR_RAID_TYPES)
  3839. return 1;
  3840. /* See the comments for btrfs_loop_type for an explanation of the phases. */
  3841. if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
  3842. ffe_ctl->index = 0;
  3843. /*
  3844. * We want to skip the LOOP_CACHING_WAIT step if we don't have
  3845. * any uncached bgs and we've already done a full search
  3846. * through.
  3847. */
  3848. if (ffe_ctl->loop == LOOP_CACHING_NOWAIT &&
  3849. (!ffe_ctl->orig_have_caching_bg && full_search))
  3850. ffe_ctl->loop++;
  3851. ffe_ctl->loop++;
  3852. if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
  3853. struct btrfs_trans_handle *trans;
  3854. int exist = 0;
  3855. /* Check if allocation policy allows to create a new chunk */
  3856. ret = can_allocate_chunk(fs_info, ffe_ctl);
  3857. if (ret)
  3858. return ret;
  3859. trans = current->journal_info;
  3860. if (trans)
  3861. exist = 1;
  3862. else
  3863. trans = btrfs_join_transaction(root);
  3864. if (IS_ERR(trans))
  3865. return PTR_ERR(trans);
  3866. ret = btrfs_chunk_alloc(trans, space_info, ffe_ctl->flags,
  3867. CHUNK_ALLOC_FORCE_FOR_EXTENT);
  3868. /* Do not bail out on ENOSPC since we can do more. */
  3869. if (ret == -ENOSPC) {
  3870. ret = 0;
  3871. ffe_ctl->loop++;
  3872. }
  3873. else if (ret < 0)
  3874. btrfs_abort_transaction(trans, ret);
  3875. else
  3876. ret = 0;
  3877. if (!exist)
  3878. btrfs_end_transaction(trans);
  3879. if (ret)
  3880. return ret;
  3881. }
  3882. if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
  3883. if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED)
  3884. return -ENOSPC;
  3885. /*
  3886. * Don't loop again if we already have no empty_size and
  3887. * no empty_cluster.
  3888. */
  3889. if (ffe_ctl->empty_size == 0 &&
  3890. ffe_ctl->empty_cluster == 0)
  3891. return -ENOSPC;
  3892. ffe_ctl->empty_size = 0;
  3893. ffe_ctl->empty_cluster = 0;
  3894. }
  3895. return 1;
  3896. }
  3897. return -ENOSPC;
  3898. }
  3899. static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info,
  3900. struct find_free_extent_ctl *ffe_ctl,
  3901. struct btrfs_space_info *space_info,
  3902. struct btrfs_key *ins)
  3903. {
  3904. /*
  3905. * If our free space is heavily fragmented we may not be able to make
  3906. * big contiguous allocations, so instead of doing the expensive search
  3907. * for free space, simply return ENOSPC with our max_extent_size so we
  3908. * can go ahead and search for a more manageable chunk.
  3909. *
  3910. * If our max_extent_size is large enough for our allocation simply
  3911. * disable clustering since we will likely not be able to find enough
  3912. * space to create a cluster and induce latency trying.
  3913. */
  3914. if (space_info->max_extent_size) {
  3915. spin_lock(&space_info->lock);
  3916. if (space_info->max_extent_size &&
  3917. ffe_ctl->num_bytes > space_info->max_extent_size) {
  3918. ins->offset = space_info->max_extent_size;
  3919. spin_unlock(&space_info->lock);
  3920. return -ENOSPC;
  3921. } else if (space_info->max_extent_size) {
  3922. ffe_ctl->use_cluster = false;
  3923. }
  3924. spin_unlock(&space_info->lock);
  3925. }
  3926. ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info,
  3927. &ffe_ctl->empty_cluster);
  3928. if (ffe_ctl->last_ptr) {
  3929. struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
  3930. spin_lock(&last_ptr->lock);
  3931. if (last_ptr->block_group)
  3932. ffe_ctl->hint_byte = last_ptr->window_start;
  3933. if (last_ptr->fragmented) {
  3934. /*
  3935. * We still set window_start so we can keep track of the
  3936. * last place we found an allocation to try and save
  3937. * some time.
  3938. */
  3939. ffe_ctl->hint_byte = last_ptr->window_start;
  3940. ffe_ctl->use_cluster = false;
  3941. }
  3942. spin_unlock(&last_ptr->lock);
  3943. }
  3944. return 0;
  3945. }
  3946. static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,
  3947. struct find_free_extent_ctl *ffe_ctl,
  3948. struct btrfs_space_info *space_info)
  3949. {
  3950. struct btrfs_block_group *block_group;
  3951. if (ffe_ctl->for_treelog) {
  3952. spin_lock(&fs_info->treelog_bg_lock);
  3953. if (fs_info->treelog_bg)
  3954. ffe_ctl->hint_byte = fs_info->treelog_bg;
  3955. spin_unlock(&fs_info->treelog_bg_lock);
  3956. return 0;
  3957. }
  3958. if (ffe_ctl->for_data_reloc) {
  3959. spin_lock(&fs_info->relocation_bg_lock);
  3960. if (fs_info->data_reloc_bg)
  3961. ffe_ctl->hint_byte = fs_info->data_reloc_bg;
  3962. spin_unlock(&fs_info->relocation_bg_lock);
  3963. return 0;
  3964. }
  3965. if (!(ffe_ctl->flags & BTRFS_BLOCK_GROUP_DATA))
  3966. return 0;
  3967. spin_lock(&fs_info->zone_active_bgs_lock);
  3968. list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
  3969. /*
  3970. * No lock is OK here because avail is monotonically
  3971. * decreasing, and this is just a hint.
  3972. */
  3973. u64 avail = block_group->zone_capacity - block_group->alloc_offset;
  3974. if (block_group_bits(block_group, ffe_ctl->flags) &&
  3975. block_group->space_info == space_info &&
  3976. avail >= ffe_ctl->num_bytes) {
  3977. ffe_ctl->hint_byte = block_group->start;
  3978. break;
  3979. }
  3980. }
  3981. spin_unlock(&fs_info->zone_active_bgs_lock);
  3982. return 0;
  3983. }
  3984. static int prepare_allocation(struct btrfs_fs_info *fs_info,
  3985. struct find_free_extent_ctl *ffe_ctl,
  3986. struct btrfs_space_info *space_info,
  3987. struct btrfs_key *ins)
  3988. {
  3989. switch (ffe_ctl->policy) {
  3990. case BTRFS_EXTENT_ALLOC_CLUSTERED:
  3991. return prepare_allocation_clustered(fs_info, ffe_ctl,
  3992. space_info, ins);
  3993. case BTRFS_EXTENT_ALLOC_ZONED:
  3994. return prepare_allocation_zoned(fs_info, ffe_ctl, space_info);
  3995. default:
  3996. BUG();
  3997. }
  3998. }
  3999. /*
  4000. * walks the btree of allocated extents and find a hole of a given size.
  4001. * The key ins is changed to record the hole:
  4002. * ins->objectid == start position
  4003. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  4004. * ins->offset == the size of the hole.
  4005. * Any available blocks before search_start are skipped.
  4006. *
  4007. * If there is no suitable free space, we will record the max size of
  4008. * the free space extent currently.
  4009. *
  4010. * The overall logic and call chain:
  4011. *
  4012. * find_free_extent()
  4013. * |- Iterate through all block groups
  4014. * | |- Get a valid block group
  4015. * | |- Try to do clustered allocation in that block group
  4016. * | |- Try to do unclustered allocation in that block group
  4017. * | |- Check if the result is valid
  4018. * | | |- If valid, then exit
  4019. * | |- Jump to next block group
  4020. * |
  4021. * |- Push harder to find free extents
  4022. * |- If not found, re-iterate all block groups
  4023. */
  4024. static noinline int find_free_extent(struct btrfs_root *root,
  4025. struct btrfs_key *ins,
  4026. struct find_free_extent_ctl *ffe_ctl)
  4027. {
  4028. struct btrfs_fs_info *fs_info = root->fs_info;
  4029. int ret = 0;
  4030. int cache_block_group_error = 0;
  4031. struct btrfs_block_group *block_group = NULL;
  4032. struct btrfs_space_info *space_info;
  4033. bool full_search = false;
  4034. WARN_ON(ffe_ctl->num_bytes < fs_info->sectorsize);
  4035. ffe_ctl->search_start = 0;
  4036. /* For clustered allocation */
  4037. ffe_ctl->empty_cluster = 0;
  4038. ffe_ctl->last_ptr = NULL;
  4039. ffe_ctl->use_cluster = true;
  4040. ffe_ctl->have_caching_bg = false;
  4041. ffe_ctl->orig_have_caching_bg = false;
  4042. ffe_ctl->index = btrfs_bg_flags_to_raid_index(ffe_ctl->flags);
  4043. ffe_ctl->loop = 0;
  4044. ffe_ctl->retry_uncached = false;
  4045. ffe_ctl->cached = 0;
  4046. ffe_ctl->max_extent_size = 0;
  4047. ffe_ctl->total_free_space = 0;
  4048. ffe_ctl->found_offset = 0;
  4049. ffe_ctl->policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
  4050. ffe_ctl->size_class = btrfs_calc_block_group_size_class(ffe_ctl->num_bytes);
  4051. if (btrfs_is_zoned(fs_info))
  4052. ffe_ctl->policy = BTRFS_EXTENT_ALLOC_ZONED;
  4053. ins->type = BTRFS_EXTENT_ITEM_KEY;
  4054. ins->objectid = 0;
  4055. ins->offset = 0;
  4056. trace_btrfs_find_free_extent(root, ffe_ctl);
  4057. space_info = btrfs_find_space_info(fs_info, ffe_ctl->flags);
  4058. if (btrfs_is_zoned(fs_info) && space_info) {
  4059. /* Use dedicated sub-space_info for dedicated block group users. */
  4060. if (ffe_ctl->for_data_reloc) {
  4061. space_info = space_info->sub_group[0];
  4062. ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC);
  4063. } else if (ffe_ctl->for_treelog) {
  4064. space_info = space_info->sub_group[0];
  4065. ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_TREELOG);
  4066. }
  4067. }
  4068. if (!space_info) {
  4069. btrfs_err(fs_info, "no space info for %llu, tree-log %d, relocation %d",
  4070. ffe_ctl->flags, ffe_ctl->for_treelog, ffe_ctl->for_data_reloc);
  4071. return -ENOSPC;
  4072. }
  4073. ret = prepare_allocation(fs_info, ffe_ctl, space_info, ins);
  4074. if (ret < 0)
  4075. return ret;
  4076. ffe_ctl->search_start = max(ffe_ctl->search_start,
  4077. first_logical_byte(fs_info));
  4078. ffe_ctl->search_start = max(ffe_ctl->search_start, ffe_ctl->hint_byte);
  4079. if (ffe_ctl->search_start == ffe_ctl->hint_byte) {
  4080. block_group = btrfs_lookup_block_group(fs_info,
  4081. ffe_ctl->search_start);
  4082. /*
  4083. * we don't want to use the block group if it doesn't match our
  4084. * allocation bits, or if its not cached.
  4085. *
  4086. * However if we are re-searching with an ideal block group
  4087. * picked out then we don't care that the block group is cached.
  4088. */
  4089. if (block_group && block_group_bits(block_group, ffe_ctl->flags) &&
  4090. block_group->space_info == space_info &&
  4091. block_group->cached != BTRFS_CACHE_NO) {
  4092. down_read(&space_info->groups_sem);
  4093. if (list_empty(&block_group->list) ||
  4094. block_group->ro ||
  4095. (block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) {
  4096. /*
  4097. * someone is removing this block group,
  4098. * we can't jump into the have_block_group
  4099. * target because our list pointers are not
  4100. * valid
  4101. */
  4102. btrfs_put_block_group(block_group);
  4103. up_read(&space_info->groups_sem);
  4104. } else {
  4105. ffe_ctl->index = btrfs_bg_flags_to_raid_index(
  4106. block_group->flags);
  4107. btrfs_lock_block_group(block_group,
  4108. ffe_ctl->delalloc);
  4109. ffe_ctl->hinted = true;
  4110. goto have_block_group;
  4111. }
  4112. } else if (block_group) {
  4113. btrfs_put_block_group(block_group);
  4114. }
  4115. }
  4116. search:
  4117. trace_btrfs_find_free_extent_search_loop(root, ffe_ctl);
  4118. ffe_ctl->have_caching_bg = false;
  4119. if (ffe_ctl->index == btrfs_bg_flags_to_raid_index(ffe_ctl->flags) ||
  4120. ffe_ctl->index == 0)
  4121. full_search = true;
  4122. down_read(&space_info->groups_sem);
  4123. list_for_each_entry(block_group,
  4124. &space_info->block_groups[ffe_ctl->index], list) {
  4125. struct btrfs_block_group *bg_ret;
  4126. ffe_ctl->hinted = false;
  4127. /* If the block group is read-only, we can skip it entirely. */
  4128. if (unlikely(block_group->ro ||
  4129. (block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED))) {
  4130. if (ffe_ctl->for_treelog)
  4131. btrfs_clear_treelog_bg(block_group);
  4132. if (ffe_ctl->for_data_reloc)
  4133. btrfs_clear_data_reloc_bg(block_group);
  4134. continue;
  4135. }
  4136. btrfs_grab_block_group(block_group, ffe_ctl->delalloc);
  4137. ffe_ctl->search_start = block_group->start;
  4138. /*
  4139. * this can happen if we end up cycling through all the
  4140. * raid types, but we want to make sure we only allocate
  4141. * for the proper type.
  4142. */
  4143. if (!block_group_bits(block_group, ffe_ctl->flags)) {
  4144. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  4145. BTRFS_BLOCK_GROUP_RAID1_MASK |
  4146. BTRFS_BLOCK_GROUP_RAID56_MASK |
  4147. BTRFS_BLOCK_GROUP_RAID10;
  4148. /*
  4149. * if they asked for extra copies and this block group
  4150. * doesn't provide them, bail. This does allow us to
  4151. * fill raid0 from raid1.
  4152. */
  4153. if ((ffe_ctl->flags & extra) && !(block_group->flags & extra))
  4154. goto loop;
  4155. /*
  4156. * This block group has different flags than we want.
  4157. * It's possible that we have MIXED_GROUP flag but no
  4158. * block group is mixed. Just skip such block group.
  4159. */
  4160. btrfs_release_block_group(block_group, ffe_ctl->delalloc);
  4161. continue;
  4162. }
  4163. have_block_group:
  4164. trace_btrfs_find_free_extent_have_block_group(root, ffe_ctl, block_group);
  4165. ffe_ctl->cached = btrfs_block_group_done(block_group);
  4166. if (unlikely(!ffe_ctl->cached)) {
  4167. ffe_ctl->have_caching_bg = true;
  4168. ret = btrfs_cache_block_group(block_group, false);
  4169. /*
  4170. * If we get ENOMEM here or something else we want to
  4171. * try other block groups, because it may not be fatal.
  4172. * However if we can't find anything else we need to
  4173. * save our return here so that we return the actual
  4174. * error that caused problems, not ENOSPC.
  4175. */
  4176. if (ret < 0) {
  4177. if (!cache_block_group_error)
  4178. cache_block_group_error = ret;
  4179. ret = 0;
  4180. goto loop;
  4181. }
  4182. ret = 0;
  4183. }
  4184. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) {
  4185. if (!cache_block_group_error)
  4186. cache_block_group_error = -EIO;
  4187. goto loop;
  4188. }
  4189. if (!find_free_extent_check_size_class(ffe_ctl, block_group))
  4190. goto loop;
  4191. bg_ret = NULL;
  4192. ret = do_allocation(block_group, ffe_ctl, &bg_ret);
  4193. if (ret > 0)
  4194. goto loop;
  4195. if (bg_ret && bg_ret != block_group) {
  4196. btrfs_release_block_group(block_group, ffe_ctl->delalloc);
  4197. block_group = bg_ret;
  4198. }
  4199. /* Checks */
  4200. ffe_ctl->search_start = round_up(ffe_ctl->found_offset,
  4201. fs_info->stripesize);
  4202. /* move on to the next group */
  4203. if (ffe_ctl->search_start + ffe_ctl->num_bytes >
  4204. btrfs_block_group_end(block_group)) {
  4205. btrfs_add_free_space_unused(block_group,
  4206. ffe_ctl->found_offset,
  4207. ffe_ctl->num_bytes);
  4208. goto loop;
  4209. }
  4210. if (ffe_ctl->found_offset < ffe_ctl->search_start)
  4211. btrfs_add_free_space_unused(block_group,
  4212. ffe_ctl->found_offset,
  4213. ffe_ctl->search_start - ffe_ctl->found_offset);
  4214. ret = btrfs_add_reserved_bytes(block_group, ffe_ctl->ram_bytes,
  4215. ffe_ctl->num_bytes,
  4216. ffe_ctl->delalloc,
  4217. ffe_ctl->loop >= LOOP_WRONG_SIZE_CLASS);
  4218. if (ret == -EAGAIN) {
  4219. btrfs_add_free_space_unused(block_group,
  4220. ffe_ctl->found_offset,
  4221. ffe_ctl->num_bytes);
  4222. goto loop;
  4223. }
  4224. btrfs_inc_block_group_reservations(block_group);
  4225. /* we are all good, lets return */
  4226. ins->objectid = ffe_ctl->search_start;
  4227. ins->offset = ffe_ctl->num_bytes;
  4228. trace_btrfs_reserve_extent(block_group, ffe_ctl);
  4229. btrfs_release_block_group(block_group, ffe_ctl->delalloc);
  4230. break;
  4231. loop:
  4232. if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
  4233. !ffe_ctl->retry_uncached) {
  4234. ffe_ctl->retry_uncached = true;
  4235. btrfs_wait_block_group_cache_progress(block_group,
  4236. ffe_ctl->num_bytes +
  4237. ffe_ctl->empty_cluster +
  4238. ffe_ctl->empty_size);
  4239. goto have_block_group;
  4240. }
  4241. release_block_group(block_group, ffe_ctl, ffe_ctl->delalloc);
  4242. cond_resched();
  4243. }
  4244. up_read(&space_info->groups_sem);
  4245. ret = find_free_extent_update_loop(fs_info, ins, ffe_ctl, space_info,
  4246. full_search);
  4247. if (ret > 0)
  4248. goto search;
  4249. if (ret == -ENOSPC && !cache_block_group_error) {
  4250. /*
  4251. * Use ffe_ctl->total_free_space as fallback if we can't find
  4252. * any contiguous hole.
  4253. */
  4254. if (!ffe_ctl->max_extent_size)
  4255. ffe_ctl->max_extent_size = ffe_ctl->total_free_space;
  4256. spin_lock(&space_info->lock);
  4257. space_info->max_extent_size = ffe_ctl->max_extent_size;
  4258. spin_unlock(&space_info->lock);
  4259. ins->offset = ffe_ctl->max_extent_size;
  4260. } else if (ret == -ENOSPC) {
  4261. ret = cache_block_group_error;
  4262. }
  4263. return ret;
  4264. }
  4265. /*
  4266. * Entry point to the extent allocator. Tries to find a hole that is at least
  4267. * as big as @num_bytes.
  4268. *
  4269. * @root - The root that will contain this extent
  4270. *
  4271. * @ram_bytes - The amount of space in ram that @num_bytes take. This
  4272. * is used for accounting purposes. This value differs
  4273. * from @num_bytes only in the case of compressed extents.
  4274. *
  4275. * @num_bytes - Number of bytes to allocate on-disk.
  4276. *
  4277. * @min_alloc_size - Indicates the minimum amount of space that the
  4278. * allocator should try to satisfy. In some cases
  4279. * @num_bytes may be larger than what is required and if
  4280. * the filesystem is fragmented then allocation fails.
  4281. * However, the presence of @min_alloc_size gives a
  4282. * chance to try and satisfy the smaller allocation.
  4283. *
  4284. * @empty_size - A hint that you plan on doing more COW. This is the
  4285. * size in bytes the allocator should try to find free
  4286. * next to the block it returns. This is just a hint and
  4287. * may be ignored by the allocator.
  4288. *
  4289. * @hint_byte - Hint to the allocator to start searching above the byte
  4290. * address passed. It might be ignored.
  4291. *
  4292. * @ins - This key is modified to record the found hole. It will
  4293. * have the following values:
  4294. * ins->objectid == start position
  4295. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  4296. * ins->offset == the size of the hole.
  4297. *
  4298. * @is_data - Boolean flag indicating whether an extent is
  4299. * allocated for data (true) or metadata (false)
  4300. *
  4301. * @delalloc - Boolean flag indicating whether this allocation is for
  4302. * delalloc or not. If 'true' data_rwsem of block groups
  4303. * is going to be acquired.
  4304. *
  4305. *
  4306. * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
  4307. * case -ENOSPC is returned then @ins->offset will contain the size of the
  4308. * largest available hole the allocator managed to find.
  4309. */
  4310. int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
  4311. u64 num_bytes, u64 min_alloc_size,
  4312. u64 empty_size, u64 hint_byte,
  4313. struct btrfs_key *ins, bool is_data, bool delalloc)
  4314. {
  4315. struct btrfs_fs_info *fs_info = root->fs_info;
  4316. struct find_free_extent_ctl ffe_ctl = {};
  4317. bool final_tried = num_bytes == min_alloc_size;
  4318. u64 flags;
  4319. int ret;
  4320. bool for_treelog = (btrfs_root_id(root) == BTRFS_TREE_LOG_OBJECTID);
  4321. bool for_data_reloc = (btrfs_is_data_reloc_root(root) && is_data);
  4322. flags = get_alloc_profile_by_root(root, is_data);
  4323. again:
  4324. WARN_ON(num_bytes < fs_info->sectorsize);
  4325. ffe_ctl.ram_bytes = ram_bytes;
  4326. ffe_ctl.num_bytes = num_bytes;
  4327. ffe_ctl.min_alloc_size = min_alloc_size;
  4328. ffe_ctl.empty_size = empty_size;
  4329. ffe_ctl.flags = flags;
  4330. ffe_ctl.delalloc = delalloc;
  4331. ffe_ctl.hint_byte = hint_byte;
  4332. ffe_ctl.for_treelog = for_treelog;
  4333. ffe_ctl.for_data_reloc = for_data_reloc;
  4334. ret = find_free_extent(root, ins, &ffe_ctl);
  4335. if (!ret && !is_data) {
  4336. btrfs_dec_block_group_reservations(fs_info, ins->objectid);
  4337. } else if (ret == -ENOSPC) {
  4338. if (!final_tried && ins->offset) {
  4339. num_bytes = min(num_bytes >> 1, ins->offset);
  4340. num_bytes = round_down(num_bytes,
  4341. fs_info->sectorsize);
  4342. num_bytes = max(num_bytes, min_alloc_size);
  4343. ram_bytes = num_bytes;
  4344. if (num_bytes == min_alloc_size)
  4345. final_tried = true;
  4346. goto again;
  4347. } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
  4348. struct btrfs_space_info *sinfo;
  4349. sinfo = btrfs_find_space_info(fs_info, flags);
  4350. btrfs_err(fs_info,
  4351. "allocation failed flags %llu, wanted %llu tree-log %d, relocation: %d",
  4352. flags, num_bytes, for_treelog, for_data_reloc);
  4353. if (sinfo)
  4354. btrfs_dump_space_info(sinfo, num_bytes, 1);
  4355. }
  4356. }
  4357. return ret;
  4358. }
  4359. int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len,
  4360. bool is_delalloc)
  4361. {
  4362. struct btrfs_block_group *cache;
  4363. cache = btrfs_lookup_block_group(fs_info, start);
  4364. if (!cache) {
  4365. btrfs_err(fs_info, "Unable to find block group for %llu",
  4366. start);
  4367. return -ENOSPC;
  4368. }
  4369. btrfs_add_free_space(cache, start, len);
  4370. btrfs_free_reserved_bytes(cache, len, is_delalloc);
  4371. trace_btrfs_reserved_extent_free(fs_info, start, len);
  4372. btrfs_put_block_group(cache);
  4373. return 0;
  4374. }
  4375. int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans,
  4376. const struct extent_buffer *eb)
  4377. {
  4378. struct btrfs_block_group *cache;
  4379. int ret = 0;
  4380. cache = btrfs_lookup_block_group(trans->fs_info, eb->start);
  4381. if (!cache) {
  4382. btrfs_err(trans->fs_info, "unable to find block group for %llu",
  4383. eb->start);
  4384. return -ENOSPC;
  4385. }
  4386. ret = pin_down_extent(trans, cache, eb->start, eb->len, true);
  4387. btrfs_put_block_group(cache);
  4388. return ret;
  4389. }
  4390. static int alloc_reserved_extent(struct btrfs_trans_handle *trans, u64 bytenr,
  4391. u64 num_bytes)
  4392. {
  4393. struct btrfs_fs_info *fs_info = trans->fs_info;
  4394. int ret;
  4395. ret = btrfs_remove_from_free_space_tree(trans, bytenr, num_bytes);
  4396. if (ret)
  4397. return ret;
  4398. ret = btrfs_update_block_group(trans, bytenr, num_bytes, true);
  4399. if (ret) {
  4400. ASSERT(!ret);
  4401. btrfs_err(fs_info, "update block group failed for %llu %llu",
  4402. bytenr, num_bytes);
  4403. return ret;
  4404. }
  4405. trace_btrfs_reserved_extent_alloc(fs_info, bytenr, num_bytes);
  4406. return 0;
  4407. }
  4408. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4409. u64 parent, u64 root_objectid,
  4410. u64 flags, u64 owner, u64 offset,
  4411. struct btrfs_key *ins, int ref_mod, u64 oref_root)
  4412. {
  4413. struct btrfs_fs_info *fs_info = trans->fs_info;
  4414. struct btrfs_root *extent_root;
  4415. int ret;
  4416. struct btrfs_extent_item *extent_item;
  4417. struct btrfs_extent_owner_ref *oref;
  4418. struct btrfs_extent_inline_ref *iref;
  4419. struct btrfs_path *path;
  4420. struct extent_buffer *leaf;
  4421. int type;
  4422. u32 size;
  4423. const bool simple_quota = (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE);
  4424. if (parent > 0)
  4425. type = BTRFS_SHARED_DATA_REF_KEY;
  4426. else
  4427. type = BTRFS_EXTENT_DATA_REF_KEY;
  4428. size = sizeof(*extent_item);
  4429. if (simple_quota)
  4430. size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY);
  4431. size += btrfs_extent_inline_ref_size(type);
  4432. extent_root = btrfs_extent_root(fs_info, ins->objectid);
  4433. if (unlikely(!extent_root)) {
  4434. btrfs_err(fs_info,
  4435. "missing extent root for extent at bytenr %llu",
  4436. ins->objectid);
  4437. return -EUCLEAN;
  4438. }
  4439. path = btrfs_alloc_path();
  4440. if (!path)
  4441. return -ENOMEM;
  4442. ret = btrfs_insert_empty_item(trans, extent_root, path, ins, size);
  4443. if (ret) {
  4444. btrfs_free_path(path);
  4445. return ret;
  4446. }
  4447. leaf = path->nodes[0];
  4448. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4449. struct btrfs_extent_item);
  4450. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  4451. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4452. btrfs_set_extent_flags(leaf, extent_item,
  4453. flags | BTRFS_EXTENT_FLAG_DATA);
  4454. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  4455. if (simple_quota) {
  4456. btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_EXTENT_OWNER_REF_KEY);
  4457. oref = (struct btrfs_extent_owner_ref *)(&iref->offset);
  4458. btrfs_set_extent_owner_ref_root_id(leaf, oref, oref_root);
  4459. iref = (struct btrfs_extent_inline_ref *)(oref + 1);
  4460. }
  4461. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  4462. if (parent > 0) {
  4463. struct btrfs_shared_data_ref *ref;
  4464. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  4465. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  4466. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  4467. } else {
  4468. struct btrfs_extent_data_ref *ref;
  4469. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  4470. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  4471. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  4472. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  4473. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  4474. }
  4475. btrfs_free_path(path);
  4476. return alloc_reserved_extent(trans, ins->objectid, ins->offset);
  4477. }
  4478. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  4479. const struct btrfs_delayed_ref_node *node,
  4480. struct btrfs_delayed_extent_op *extent_op)
  4481. {
  4482. struct btrfs_fs_info *fs_info = trans->fs_info;
  4483. struct btrfs_root *extent_root;
  4484. int ret;
  4485. struct btrfs_extent_item *extent_item;
  4486. struct btrfs_key extent_key;
  4487. struct btrfs_tree_block_info *block_info;
  4488. struct btrfs_extent_inline_ref *iref;
  4489. struct btrfs_path *path;
  4490. struct extent_buffer *leaf;
  4491. u32 size = sizeof(*extent_item) + sizeof(*iref);
  4492. const u64 flags = (extent_op ? extent_op->flags_to_set : 0);
  4493. /* The owner of a tree block is the level. */
  4494. int level = btrfs_delayed_ref_owner(node);
  4495. bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  4496. if (unlikely(node->ref_root == BTRFS_REMAP_TREE_OBJECTID))
  4497. goto skip;
  4498. extent_key.objectid = node->bytenr;
  4499. if (skinny_metadata) {
  4500. /* The owner of a tree block is the level. */
  4501. extent_key.offset = level;
  4502. extent_key.type = BTRFS_METADATA_ITEM_KEY;
  4503. } else {
  4504. extent_key.offset = node->num_bytes;
  4505. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  4506. size += sizeof(*block_info);
  4507. }
  4508. extent_root = btrfs_extent_root(fs_info, extent_key.objectid);
  4509. if (unlikely(!extent_root)) {
  4510. btrfs_err(fs_info,
  4511. "missing extent root for extent at bytenr %llu",
  4512. extent_key.objectid);
  4513. return -EUCLEAN;
  4514. }
  4515. path = btrfs_alloc_path();
  4516. if (!path)
  4517. return -ENOMEM;
  4518. ret = btrfs_insert_empty_item(trans, extent_root, path, &extent_key,
  4519. size);
  4520. if (ret) {
  4521. btrfs_free_path(path);
  4522. return ret;
  4523. }
  4524. leaf = path->nodes[0];
  4525. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4526. struct btrfs_extent_item);
  4527. btrfs_set_extent_refs(leaf, extent_item, 1);
  4528. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4529. btrfs_set_extent_flags(leaf, extent_item,
  4530. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  4531. if (skinny_metadata) {
  4532. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  4533. } else {
  4534. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  4535. btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
  4536. btrfs_set_tree_block_level(leaf, block_info, level);
  4537. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  4538. }
  4539. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
  4540. btrfs_set_extent_inline_ref_type(leaf, iref,
  4541. BTRFS_SHARED_BLOCK_REF_KEY);
  4542. btrfs_set_extent_inline_ref_offset(leaf, iref, node->parent);
  4543. } else {
  4544. btrfs_set_extent_inline_ref_type(leaf, iref,
  4545. BTRFS_TREE_BLOCK_REF_KEY);
  4546. btrfs_set_extent_inline_ref_offset(leaf, iref, node->ref_root);
  4547. }
  4548. btrfs_free_path(path);
  4549. skip:
  4550. return alloc_reserved_extent(trans, node->bytenr, fs_info->nodesize);
  4551. }
  4552. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4553. struct btrfs_root *root, u64 owner,
  4554. u64 offset, u64 ram_bytes,
  4555. struct btrfs_key *ins)
  4556. {
  4557. struct btrfs_ref generic_ref = {
  4558. .action = BTRFS_ADD_DELAYED_EXTENT,
  4559. .bytenr = ins->objectid,
  4560. .num_bytes = ins->offset,
  4561. .owning_root = btrfs_root_id(root),
  4562. .ref_root = btrfs_root_id(root),
  4563. };
  4564. ASSERT(generic_ref.ref_root != BTRFS_TREE_LOG_OBJECTID);
  4565. if (btrfs_is_data_reloc_root(root) && btrfs_is_fstree(root->relocation_src_root))
  4566. generic_ref.owning_root = root->relocation_src_root;
  4567. btrfs_init_data_ref(&generic_ref, owner, offset, 0, false);
  4568. btrfs_ref_tree_mod(root->fs_info, &generic_ref);
  4569. return btrfs_add_delayed_data_ref(trans, &generic_ref, ram_bytes);
  4570. }
  4571. /*
  4572. * this is used by the tree logging recovery code. It records that
  4573. * an extent has been allocated and makes sure to clear the free
  4574. * space cache bits as well
  4575. */
  4576. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  4577. u64 root_objectid, u64 owner, u64 offset,
  4578. struct btrfs_key *ins)
  4579. {
  4580. struct btrfs_fs_info *fs_info = trans->fs_info;
  4581. int ret;
  4582. struct btrfs_block_group *block_group;
  4583. struct btrfs_space_info *space_info;
  4584. const struct btrfs_squota_delta delta = {
  4585. .root = root_objectid,
  4586. .num_bytes = ins->offset,
  4587. .generation = trans->transid,
  4588. .is_data = true,
  4589. .is_inc = true,
  4590. };
  4591. /*
  4592. * Mixed block groups will exclude before processing the log so we only
  4593. * need to do the exclude dance if this fs isn't mixed.
  4594. */
  4595. if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  4596. ret = __exclude_logged_extent(fs_info, ins->objectid,
  4597. ins->offset);
  4598. if (ret)
  4599. return ret;
  4600. }
  4601. block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
  4602. if (!block_group)
  4603. return -EINVAL;
  4604. space_info = block_group->space_info;
  4605. spin_lock(&space_info->lock);
  4606. spin_lock(&block_group->lock);
  4607. space_info->bytes_reserved += ins->offset;
  4608. block_group->reserved += ins->offset;
  4609. spin_unlock(&block_group->lock);
  4610. spin_unlock(&space_info->lock);
  4611. ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
  4612. offset, ins, 1, root_objectid);
  4613. if (ret)
  4614. btrfs_pin_extent(trans, ins->objectid, ins->offset);
  4615. ret = btrfs_record_squota_delta(fs_info, &delta);
  4616. btrfs_put_block_group(block_group);
  4617. return ret;
  4618. }
  4619. #ifdef CONFIG_BTRFS_DEBUG
  4620. /*
  4621. * Extra safety check in case the extent tree is corrupted and extent allocator
  4622. * chooses to use a tree block which is already used and locked.
  4623. */
  4624. static bool check_eb_lock_owner(const struct extent_buffer *eb)
  4625. {
  4626. if (eb->lock_owner == current->pid) {
  4627. btrfs_err_rl(eb->fs_info,
  4628. "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
  4629. eb->start, btrfs_header_owner(eb), current->pid);
  4630. return true;
  4631. }
  4632. return false;
  4633. }
  4634. #else
  4635. static bool check_eb_lock_owner(struct extent_buffer *eb)
  4636. {
  4637. return false;
  4638. }
  4639. #endif
  4640. static struct extent_buffer *
  4641. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4642. u64 bytenr, int level, u64 owner,
  4643. enum btrfs_lock_nesting nest)
  4644. {
  4645. struct btrfs_fs_info *fs_info = root->fs_info;
  4646. struct extent_buffer *buf;
  4647. u64 lockdep_owner = owner;
  4648. buf = btrfs_find_create_tree_block(fs_info, bytenr, owner, level);
  4649. if (IS_ERR(buf))
  4650. return buf;
  4651. if (unlikely(check_eb_lock_owner(buf))) {
  4652. free_extent_buffer(buf);
  4653. return ERR_PTR(-EUCLEAN);
  4654. }
  4655. /*
  4656. * The reloc trees are just snapshots, so we need them to appear to be
  4657. * just like any other fs tree WRT lockdep.
  4658. *
  4659. * The exception however is in replace_path() in relocation, where we
  4660. * hold the lock on the original fs root and then search for the reloc
  4661. * root. At that point we need to make sure any reloc root buffers are
  4662. * set to the BTRFS_TREE_RELOC_OBJECTID lockdep class in order to make
  4663. * lockdep happy.
  4664. */
  4665. if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID &&
  4666. !test_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &root->state))
  4667. lockdep_owner = BTRFS_FS_TREE_OBJECTID;
  4668. /* btrfs_clear_buffer_dirty() accesses generation field. */
  4669. btrfs_set_header_generation(buf, trans->transid);
  4670. /*
  4671. * This needs to stay, because we could allocate a freed block from an
  4672. * old tree into a new tree, so we need to make sure this new block is
  4673. * set to the appropriate level and owner.
  4674. */
  4675. btrfs_set_buffer_lockdep_class(lockdep_owner, buf, level);
  4676. btrfs_tree_lock_nested(buf, nest);
  4677. btrfs_clear_buffer_dirty(trans, buf);
  4678. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  4679. clear_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &buf->bflags);
  4680. set_extent_buffer_uptodate(buf);
  4681. memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
  4682. btrfs_set_header_level(buf, level);
  4683. btrfs_set_header_bytenr(buf, buf->start);
  4684. btrfs_set_header_generation(buf, trans->transid);
  4685. btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
  4686. btrfs_set_header_owner(buf, owner);
  4687. write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
  4688. write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
  4689. if (btrfs_root_id(root) == BTRFS_TREE_LOG_OBJECTID) {
  4690. buf->log_index = root->log_transid % 2;
  4691. /*
  4692. * we allow two log transactions at a time, use different
  4693. * EXTENT bit to differentiate dirty pages.
  4694. */
  4695. if (buf->log_index == 0)
  4696. btrfs_set_extent_bit(&root->dirty_log_pages, buf->start,
  4697. buf->start + buf->len - 1,
  4698. EXTENT_DIRTY_LOG1, NULL);
  4699. else
  4700. btrfs_set_extent_bit(&root->dirty_log_pages, buf->start,
  4701. buf->start + buf->len - 1,
  4702. EXTENT_DIRTY_LOG2, NULL);
  4703. } else {
  4704. buf->log_index = -1;
  4705. btrfs_set_extent_bit(&trans->transaction->dirty_pages, buf->start,
  4706. buf->start + buf->len - 1, EXTENT_DIRTY, NULL);
  4707. }
  4708. /* this returns a buffer locked for blocking */
  4709. return buf;
  4710. }
  4711. /*
  4712. * finds a free extent and does all the dirty work required for allocation
  4713. * returns the tree buffer or an ERR_PTR on error.
  4714. */
  4715. struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
  4716. struct btrfs_root *root,
  4717. u64 parent, u64 root_objectid,
  4718. const struct btrfs_disk_key *key,
  4719. int level, u64 hint,
  4720. u64 empty_size,
  4721. u64 reloc_src_root,
  4722. enum btrfs_lock_nesting nest)
  4723. {
  4724. struct btrfs_fs_info *fs_info = root->fs_info;
  4725. struct btrfs_key ins;
  4726. struct btrfs_block_rsv *block_rsv;
  4727. struct extent_buffer *buf;
  4728. u64 flags = 0;
  4729. int ret;
  4730. u32 blocksize = fs_info->nodesize;
  4731. bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  4732. u64 owning_root;
  4733. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4734. if (btrfs_is_testing(fs_info)) {
  4735. buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
  4736. level, root_objectid, nest);
  4737. if (!IS_ERR(buf))
  4738. root->alloc_bytenr += blocksize;
  4739. return buf;
  4740. }
  4741. #endif
  4742. block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
  4743. if (IS_ERR(block_rsv))
  4744. return ERR_CAST(block_rsv);
  4745. ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
  4746. empty_size, hint, &ins, false, false);
  4747. if (ret)
  4748. goto out_unuse;
  4749. buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
  4750. root_objectid, nest);
  4751. if (IS_ERR(buf)) {
  4752. ret = PTR_ERR(buf);
  4753. goto out_free_reserved;
  4754. }
  4755. owning_root = btrfs_header_owner(buf);
  4756. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  4757. if (parent == 0)
  4758. parent = ins.objectid;
  4759. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4760. owning_root = reloc_src_root;
  4761. } else
  4762. BUG_ON(parent > 0);
  4763. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  4764. struct btrfs_delayed_extent_op *extent_op;
  4765. struct btrfs_ref generic_ref = {
  4766. .action = BTRFS_ADD_DELAYED_EXTENT,
  4767. .bytenr = ins.objectid,
  4768. .num_bytes = ins.offset,
  4769. .parent = parent,
  4770. .owning_root = owning_root,
  4771. .ref_root = root_objectid,
  4772. };
  4773. if (!skinny_metadata || flags != 0) {
  4774. extent_op = btrfs_alloc_delayed_extent_op();
  4775. if (!extent_op) {
  4776. ret = -ENOMEM;
  4777. goto out_free_buf;
  4778. }
  4779. if (key)
  4780. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  4781. else
  4782. memset(&extent_op->key, 0, sizeof(extent_op->key));
  4783. extent_op->flags_to_set = flags;
  4784. extent_op->update_key = (skinny_metadata ? false : true);
  4785. extent_op->update_flags = (flags != 0);
  4786. } else {
  4787. extent_op = NULL;
  4788. }
  4789. btrfs_init_tree_ref(&generic_ref, level, btrfs_root_id(root), false);
  4790. btrfs_ref_tree_mod(fs_info, &generic_ref);
  4791. ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, extent_op);
  4792. if (ret) {
  4793. btrfs_free_delayed_extent_op(extent_op);
  4794. goto out_free_buf;
  4795. }
  4796. }
  4797. return buf;
  4798. out_free_buf:
  4799. btrfs_tree_unlock(buf);
  4800. free_extent_buffer(buf);
  4801. out_free_reserved:
  4802. btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, false);
  4803. out_unuse:
  4804. btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
  4805. return ERR_PTR(ret);
  4806. }
  4807. struct walk_control {
  4808. u64 refs[BTRFS_MAX_LEVEL];
  4809. u64 flags[BTRFS_MAX_LEVEL];
  4810. struct btrfs_key update_progress;
  4811. struct btrfs_key drop_progress;
  4812. int drop_level;
  4813. int stage;
  4814. int level;
  4815. int shared_level;
  4816. int update_ref;
  4817. int keep_locks;
  4818. int reada_slot;
  4819. int reada_count;
  4820. int restarted;
  4821. /* Indicate that extent info needs to be looked up when walking the tree. */
  4822. int lookup_info;
  4823. };
  4824. /*
  4825. * This is our normal stage. We are traversing blocks the current snapshot owns
  4826. * and we are dropping any of our references to any children we are able to, and
  4827. * then freeing the block once we've processed all of the children.
  4828. */
  4829. #define DROP_REFERENCE 1
  4830. /*
  4831. * We enter this stage when we have to walk into a child block (meaning we can't
  4832. * simply drop our reference to it from our current parent node) and there are
  4833. * more than one reference on it. If we are the owner of any of the children
  4834. * blocks from the current parent node then we have to do the FULL_BACKREF dance
  4835. * on them in order to drop our normal ref and add the shared ref.
  4836. */
  4837. #define UPDATE_BACKREF 2
  4838. /*
  4839. * Decide if we need to walk down into this node to adjust the references.
  4840. *
  4841. * @root: the root we are currently deleting
  4842. * @wc: the walk control for this deletion
  4843. * @eb: the parent eb that we're currently visiting
  4844. * @flags: the flags for wc->level - 1
  4845. * @slot: the slot in the eb that we're currently checking
  4846. *
  4847. * This is meant to be called when we're evaluating if a node we point to at
  4848. * wc->level should be read and walked into, or if we can simply delete our
  4849. * reference to it. We return true if we should walk into the node, false if we
  4850. * can skip it.
  4851. *
  4852. * We have assertions in here to make sure this is called correctly. We assume
  4853. * that sanity checking on the blocks read to this point has been done, so any
  4854. * corrupted file systems must have been caught before calling this function.
  4855. */
  4856. static bool visit_node_for_delete(struct btrfs_root *root, struct walk_control *wc,
  4857. struct extent_buffer *eb, u64 flags, int slot)
  4858. {
  4859. struct btrfs_key key;
  4860. u64 generation;
  4861. int level = wc->level;
  4862. ASSERT(level > 0);
  4863. ASSERT(wc->refs[level - 1] > 0);
  4864. /*
  4865. * The update backref stage we only want to skip if we already have
  4866. * FULL_BACKREF set, otherwise we need to read.
  4867. */
  4868. if (wc->stage == UPDATE_BACKREF) {
  4869. if (level == 1 && flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4870. return false;
  4871. return true;
  4872. }
  4873. /*
  4874. * We're the last ref on this block, we must walk into it and process
  4875. * any refs it's pointing at.
  4876. */
  4877. if (wc->refs[level - 1] == 1)
  4878. return true;
  4879. /*
  4880. * If we're already FULL_BACKREF then we know we can just drop our
  4881. * current reference.
  4882. */
  4883. if (level == 1 && flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4884. return false;
  4885. /*
  4886. * This block is older than our creation generation, we can drop our
  4887. * reference to it.
  4888. */
  4889. generation = btrfs_node_ptr_generation(eb, slot);
  4890. if (!wc->update_ref || generation <= btrfs_root_origin_generation(root))
  4891. return false;
  4892. /*
  4893. * This block was processed from a previous snapshot deletion run, we
  4894. * can skip it.
  4895. */
  4896. btrfs_node_key_to_cpu(eb, &key, slot);
  4897. if (btrfs_comp_cpu_keys(&key, &wc->update_progress) < 0)
  4898. return false;
  4899. /* All other cases we need to wander into the node. */
  4900. return true;
  4901. }
  4902. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  4903. struct btrfs_root *root,
  4904. struct walk_control *wc,
  4905. struct btrfs_path *path)
  4906. {
  4907. struct btrfs_fs_info *fs_info = root->fs_info;
  4908. u64 bytenr;
  4909. u64 generation;
  4910. u64 refs;
  4911. u64 flags;
  4912. u32 nritems;
  4913. struct extent_buffer *eb;
  4914. int ret;
  4915. int slot;
  4916. int nread = 0;
  4917. if (path->slots[wc->level] < wc->reada_slot) {
  4918. wc->reada_count = wc->reada_count * 2 / 3;
  4919. wc->reada_count = max(wc->reada_count, 2);
  4920. } else {
  4921. wc->reada_count = wc->reada_count * 3 / 2;
  4922. wc->reada_count = min_t(int, wc->reada_count,
  4923. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  4924. }
  4925. eb = path->nodes[wc->level];
  4926. nritems = btrfs_header_nritems(eb);
  4927. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  4928. if (nread >= wc->reada_count)
  4929. break;
  4930. cond_resched();
  4931. bytenr = btrfs_node_blockptr(eb, slot);
  4932. generation = btrfs_node_ptr_generation(eb, slot);
  4933. if (slot == path->slots[wc->level])
  4934. goto reada;
  4935. if (wc->stage == UPDATE_BACKREF &&
  4936. generation <= btrfs_root_origin_generation(root))
  4937. continue;
  4938. /* We don't lock the tree block, it's OK to be racy here */
  4939. ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
  4940. wc->level - 1, 1, &refs,
  4941. &flags, NULL);
  4942. /* We don't care about errors in readahead. */
  4943. if (ret < 0)
  4944. continue;
  4945. /*
  4946. * This could be racey, it's conceivable that we raced and end
  4947. * up with a bogus refs count, if that's the case just skip, if
  4948. * we are actually corrupt we will notice when we look up
  4949. * everything again with our locks.
  4950. */
  4951. if (refs == 0)
  4952. continue;
  4953. /* If we don't need to visit this node don't reada. */
  4954. if (!visit_node_for_delete(root, wc, eb, flags, slot))
  4955. continue;
  4956. reada:
  4957. btrfs_readahead_node_child(eb, slot);
  4958. nread++;
  4959. }
  4960. wc->reada_slot = slot;
  4961. }
  4962. /*
  4963. * helper to process tree block while walking down the tree.
  4964. *
  4965. * when wc->stage == UPDATE_BACKREF, this function updates
  4966. * back refs for pointers in the block.
  4967. *
  4968. * NOTE: return value 1 means we should stop walking down.
  4969. */
  4970. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  4971. struct btrfs_root *root,
  4972. struct btrfs_path *path,
  4973. struct walk_control *wc)
  4974. {
  4975. struct btrfs_fs_info *fs_info = root->fs_info;
  4976. int level = wc->level;
  4977. struct extent_buffer *eb = path->nodes[level];
  4978. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4979. int ret;
  4980. if (wc->stage == UPDATE_BACKREF && btrfs_header_owner(eb) != btrfs_root_id(root))
  4981. return 1;
  4982. /*
  4983. * when reference count of tree block is 1, it won't increase
  4984. * again. once full backref flag is set, we never clear it.
  4985. */
  4986. if (wc->lookup_info &&
  4987. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  4988. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  4989. ASSERT(path->locks[level]);
  4990. ret = btrfs_lookup_extent_info(trans, fs_info,
  4991. eb->start, level, 1,
  4992. &wc->refs[level],
  4993. &wc->flags[level],
  4994. NULL);
  4995. if (ret)
  4996. return ret;
  4997. if (unlikely(wc->refs[level] == 0)) {
  4998. btrfs_err(fs_info, "bytenr %llu has 0 references, expect > 0",
  4999. eb->start);
  5000. return -EUCLEAN;
  5001. }
  5002. }
  5003. if (wc->stage == DROP_REFERENCE) {
  5004. if (wc->refs[level] > 1)
  5005. return 1;
  5006. if (path->locks[level] && !wc->keep_locks) {
  5007. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5008. path->locks[level] = 0;
  5009. }
  5010. return 0;
  5011. }
  5012. /* wc->stage == UPDATE_BACKREF */
  5013. if (!(wc->flags[level] & flag)) {
  5014. ASSERT(path->locks[level]);
  5015. ret = btrfs_inc_ref(trans, root, eb, true);
  5016. if (unlikely(ret)) {
  5017. btrfs_abort_transaction(trans, ret);
  5018. return ret;
  5019. }
  5020. ret = btrfs_dec_ref(trans, root, eb, false);
  5021. if (unlikely(ret)) {
  5022. btrfs_abort_transaction(trans, ret);
  5023. return ret;
  5024. }
  5025. ret = btrfs_set_disk_extent_flags(trans, eb, flag);
  5026. if (unlikely(ret)) {
  5027. btrfs_abort_transaction(trans, ret);
  5028. return ret;
  5029. }
  5030. wc->flags[level] |= flag;
  5031. }
  5032. /*
  5033. * the block is shared by multiple trees, so it's not good to
  5034. * keep the tree lock
  5035. */
  5036. if (path->locks[level] && level > 0) {
  5037. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5038. path->locks[level] = 0;
  5039. }
  5040. return 0;
  5041. }
  5042. /*
  5043. * This is used to verify a ref exists for this root to deal with a bug where we
  5044. * would have a drop_progress key that hadn't been updated properly.
  5045. */
  5046. static int check_ref_exists(struct btrfs_trans_handle *trans,
  5047. struct btrfs_root *root, u64 bytenr, u64 parent,
  5048. int level)
  5049. {
  5050. struct btrfs_delayed_ref_root *delayed_refs;
  5051. struct btrfs_delayed_ref_head *head;
  5052. BTRFS_PATH_AUTO_FREE(path);
  5053. struct btrfs_extent_inline_ref *iref;
  5054. int ret;
  5055. bool exists = false;
  5056. path = btrfs_alloc_path();
  5057. if (!path)
  5058. return -ENOMEM;
  5059. again:
  5060. ret = lookup_extent_backref(trans, path, &iref, bytenr,
  5061. root->fs_info->nodesize, parent,
  5062. btrfs_root_id(root), level, 0);
  5063. if (ret != -ENOENT) {
  5064. /*
  5065. * If we get 0 then we found our reference, return 1, else
  5066. * return the error if it's not -ENOENT;
  5067. */
  5068. return (ret < 0 ) ? ret : 1;
  5069. }
  5070. /*
  5071. * We could have a delayed ref with this reference, so look it up while
  5072. * we're holding the path open to make sure we don't race with the
  5073. * delayed ref running.
  5074. */
  5075. delayed_refs = &trans->transaction->delayed_refs;
  5076. spin_lock(&delayed_refs->lock);
  5077. head = btrfs_find_delayed_ref_head(root->fs_info, delayed_refs, bytenr);
  5078. if (!head)
  5079. goto out;
  5080. if (!mutex_trylock(&head->mutex)) {
  5081. /*
  5082. * We're contended, means that the delayed ref is running, get a
  5083. * reference and wait for the ref head to be complete and then
  5084. * try again.
  5085. */
  5086. refcount_inc(&head->refs);
  5087. spin_unlock(&delayed_refs->lock);
  5088. btrfs_release_path(path);
  5089. mutex_lock(&head->mutex);
  5090. mutex_unlock(&head->mutex);
  5091. btrfs_put_delayed_ref_head(head);
  5092. goto again;
  5093. }
  5094. exists = btrfs_find_delayed_tree_ref(head, btrfs_root_id(root), parent);
  5095. mutex_unlock(&head->mutex);
  5096. out:
  5097. spin_unlock(&delayed_refs->lock);
  5098. return exists ? 1 : 0;
  5099. }
  5100. /*
  5101. * We may not have an uptodate block, so if we are going to walk down into this
  5102. * block we need to drop the lock, read it off of the disk, re-lock it and
  5103. * return to continue dropping the snapshot.
  5104. */
  5105. static int check_next_block_uptodate(struct btrfs_trans_handle *trans,
  5106. struct btrfs_root *root,
  5107. struct btrfs_path *path,
  5108. struct walk_control *wc,
  5109. struct extent_buffer *next)
  5110. {
  5111. struct btrfs_tree_parent_check check = { 0 };
  5112. u64 generation;
  5113. int level = wc->level;
  5114. int ret;
  5115. btrfs_assert_tree_write_locked(next);
  5116. generation = btrfs_node_ptr_generation(path->nodes[level], path->slots[level]);
  5117. if (btrfs_buffer_uptodate(next, generation, false))
  5118. return 0;
  5119. check.level = level - 1;
  5120. check.transid = generation;
  5121. check.owner_root = btrfs_root_id(root);
  5122. check.has_first_key = true;
  5123. btrfs_node_key_to_cpu(path->nodes[level], &check.first_key, path->slots[level]);
  5124. btrfs_tree_unlock(next);
  5125. if (level == 1)
  5126. reada_walk_down(trans, root, wc, path);
  5127. ret = btrfs_read_extent_buffer(next, &check);
  5128. if (ret) {
  5129. free_extent_buffer(next);
  5130. return ret;
  5131. }
  5132. btrfs_tree_lock(next);
  5133. wc->lookup_info = 1;
  5134. return 0;
  5135. }
  5136. /*
  5137. * If we determine that we don't have to visit wc->level - 1 then we need to
  5138. * determine if we can drop our reference.
  5139. *
  5140. * If we are UPDATE_BACKREF then we will not, we need to update our backrefs.
  5141. *
  5142. * If we are DROP_REFERENCE this will figure out if we need to drop our current
  5143. * reference, skipping it if we dropped it from a previous uncompleted drop, or
  5144. * dropping it if we still have a reference to it.
  5145. */
  5146. static int maybe_drop_reference(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5147. struct btrfs_path *path, struct walk_control *wc,
  5148. struct extent_buffer *next, u64 owner_root)
  5149. {
  5150. struct btrfs_ref ref = {
  5151. .action = BTRFS_DROP_DELAYED_REF,
  5152. .bytenr = next->start,
  5153. .num_bytes = root->fs_info->nodesize,
  5154. .owning_root = owner_root,
  5155. .ref_root = btrfs_root_id(root),
  5156. };
  5157. int level = wc->level;
  5158. int ret;
  5159. /* We are UPDATE_BACKREF, we're not dropping anything. */
  5160. if (wc->stage == UPDATE_BACKREF)
  5161. return 0;
  5162. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  5163. ref.parent = path->nodes[level]->start;
  5164. } else {
  5165. ASSERT(btrfs_root_id(root) == btrfs_header_owner(path->nodes[level]));
  5166. if (unlikely(btrfs_root_id(root) != btrfs_header_owner(path->nodes[level]))) {
  5167. btrfs_err(root->fs_info, "mismatched block owner");
  5168. return -EIO;
  5169. }
  5170. }
  5171. /*
  5172. * If we had a drop_progress we need to verify the refs are set as
  5173. * expected. If we find our ref then we know that from here on out
  5174. * everything should be correct, and we can clear the
  5175. * ->restarted flag.
  5176. */
  5177. if (wc->restarted) {
  5178. ret = check_ref_exists(trans, root, next->start, ref.parent,
  5179. level - 1);
  5180. if (ret <= 0)
  5181. return ret;
  5182. ret = 0;
  5183. wc->restarted = 0;
  5184. }
  5185. /*
  5186. * Reloc tree doesn't contribute to qgroup numbers, and we have already
  5187. * accounted them at merge time (replace_path), thus we could skip
  5188. * expensive subtree trace here.
  5189. */
  5190. if (btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
  5191. wc->refs[level - 1] > 1) {
  5192. u64 generation = btrfs_node_ptr_generation(path->nodes[level],
  5193. path->slots[level]);
  5194. ret = btrfs_qgroup_trace_subtree(trans, next, generation, level - 1);
  5195. if (ret) {
  5196. btrfs_err_rl(root->fs_info,
  5197. "error %d accounting shared subtree, quota is out of sync, rescan required",
  5198. ret);
  5199. }
  5200. }
  5201. /*
  5202. * We need to update the next key in our walk control so we can update
  5203. * the drop_progress key accordingly. We don't care if find_next_key
  5204. * doesn't find a key because that means we're at the end and are going
  5205. * to clean up now.
  5206. */
  5207. wc->drop_level = level;
  5208. find_next_key(path, level, &wc->drop_progress);
  5209. btrfs_init_tree_ref(&ref, level - 1, 0, false);
  5210. return btrfs_free_extent(trans, &ref);
  5211. }
  5212. /*
  5213. * helper to process tree block pointer.
  5214. *
  5215. * when wc->stage == DROP_REFERENCE, this function checks
  5216. * reference count of the block pointed to. if the block
  5217. * is shared and we need update back refs for the subtree
  5218. * rooted at the block, this function changes wc->stage to
  5219. * UPDATE_BACKREF. if the block is shared and there is no
  5220. * need to update back, this function drops the reference
  5221. * to the block.
  5222. *
  5223. * NOTE: return value 1 means we should stop walking down.
  5224. */
  5225. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  5226. struct btrfs_root *root,
  5227. struct btrfs_path *path,
  5228. struct walk_control *wc)
  5229. {
  5230. struct btrfs_fs_info *fs_info = root->fs_info;
  5231. u64 bytenr;
  5232. u64 generation;
  5233. u64 owner_root = 0;
  5234. struct extent_buffer *next;
  5235. int level = wc->level;
  5236. int ret = 0;
  5237. generation = btrfs_node_ptr_generation(path->nodes[level],
  5238. path->slots[level]);
  5239. /*
  5240. * if the lower level block was created before the snapshot
  5241. * was created, we know there is no need to update back refs
  5242. * for the subtree
  5243. */
  5244. if (wc->stage == UPDATE_BACKREF &&
  5245. generation <= btrfs_root_origin_generation(root)) {
  5246. wc->lookup_info = 1;
  5247. return 1;
  5248. }
  5249. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  5250. next = btrfs_find_create_tree_block(fs_info, bytenr, btrfs_root_id(root),
  5251. level - 1);
  5252. if (IS_ERR(next))
  5253. return PTR_ERR(next);
  5254. btrfs_tree_lock(next);
  5255. ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
  5256. &wc->refs[level - 1],
  5257. &wc->flags[level - 1],
  5258. &owner_root);
  5259. if (ret < 0)
  5260. goto out_unlock;
  5261. if (unlikely(wc->refs[level - 1] == 0)) {
  5262. btrfs_err(fs_info, "bytenr %llu has 0 references, expect > 0",
  5263. bytenr);
  5264. ret = -EUCLEAN;
  5265. goto out_unlock;
  5266. }
  5267. wc->lookup_info = 0;
  5268. /* If we don't have to walk into this node skip it. */
  5269. if (!visit_node_for_delete(root, wc, path->nodes[level],
  5270. wc->flags[level - 1], path->slots[level]))
  5271. goto skip;
  5272. /*
  5273. * We have to walk down into this node, and if we're currently at the
  5274. * DROP_REFERENCE stage and this block is shared then we need to switch
  5275. * to the UPDATE_BACKREF stage in order to convert to FULL_BACKREF.
  5276. */
  5277. if (wc->stage == DROP_REFERENCE && wc->refs[level - 1] > 1) {
  5278. wc->stage = UPDATE_BACKREF;
  5279. wc->shared_level = level - 1;
  5280. }
  5281. ret = check_next_block_uptodate(trans, root, path, wc, next);
  5282. if (ret)
  5283. return ret;
  5284. level--;
  5285. ASSERT(level == btrfs_header_level(next));
  5286. if (unlikely(level != btrfs_header_level(next))) {
  5287. btrfs_err(root->fs_info, "mismatched level");
  5288. ret = -EIO;
  5289. goto out_unlock;
  5290. }
  5291. path->nodes[level] = next;
  5292. path->slots[level] = 0;
  5293. path->locks[level] = BTRFS_WRITE_LOCK;
  5294. wc->level = level;
  5295. if (wc->level == 1)
  5296. wc->reada_slot = 0;
  5297. return 0;
  5298. skip:
  5299. ret = maybe_drop_reference(trans, root, path, wc, next, owner_root);
  5300. if (ret)
  5301. goto out_unlock;
  5302. wc->refs[level - 1] = 0;
  5303. wc->flags[level - 1] = 0;
  5304. wc->lookup_info = 1;
  5305. ret = 1;
  5306. out_unlock:
  5307. btrfs_tree_unlock(next);
  5308. free_extent_buffer(next);
  5309. return ret;
  5310. }
  5311. /*
  5312. * helper to process tree block while walking up the tree.
  5313. *
  5314. * when wc->stage == DROP_REFERENCE, this function drops
  5315. * reference count on the block.
  5316. *
  5317. * when wc->stage == UPDATE_BACKREF, this function changes
  5318. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  5319. * to UPDATE_BACKREF previously while processing the block.
  5320. *
  5321. * NOTE: return value 1 means we should stop walking up.
  5322. */
  5323. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  5324. struct btrfs_root *root,
  5325. struct btrfs_path *path,
  5326. struct walk_control *wc)
  5327. {
  5328. struct btrfs_fs_info *fs_info = root->fs_info;
  5329. int ret = 0;
  5330. int level = wc->level;
  5331. struct extent_buffer *eb = path->nodes[level];
  5332. u64 parent = 0;
  5333. if (wc->stage == UPDATE_BACKREF) {
  5334. ASSERT(wc->shared_level >= level);
  5335. if (level < wc->shared_level)
  5336. goto out;
  5337. ret = find_next_key(path, level + 1, &wc->update_progress);
  5338. if (ret > 0)
  5339. wc->update_ref = 0;
  5340. wc->stage = DROP_REFERENCE;
  5341. wc->shared_level = -1;
  5342. path->slots[level] = 0;
  5343. /*
  5344. * check reference count again if the block isn't locked.
  5345. * we should start walking down the tree again if reference
  5346. * count is one.
  5347. */
  5348. if (!path->locks[level]) {
  5349. ASSERT(level > 0);
  5350. btrfs_tree_lock(eb);
  5351. path->locks[level] = BTRFS_WRITE_LOCK;
  5352. ret = btrfs_lookup_extent_info(trans, fs_info,
  5353. eb->start, level, 1,
  5354. &wc->refs[level],
  5355. &wc->flags[level],
  5356. NULL);
  5357. if (ret < 0) {
  5358. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5359. path->locks[level] = 0;
  5360. return ret;
  5361. }
  5362. if (unlikely(wc->refs[level] == 0)) {
  5363. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5364. btrfs_err(fs_info, "bytenr %llu has 0 references, expect > 0",
  5365. eb->start);
  5366. return -EUCLEAN;
  5367. }
  5368. if (wc->refs[level] == 1) {
  5369. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5370. path->locks[level] = 0;
  5371. return 1;
  5372. }
  5373. }
  5374. }
  5375. /* wc->stage == DROP_REFERENCE */
  5376. ASSERT(path->locks[level] || wc->refs[level] == 1);
  5377. if (wc->refs[level] == 1) {
  5378. if (level == 0) {
  5379. const bool full_backref = (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF);
  5380. ret = btrfs_dec_ref(trans, root, eb, full_backref);
  5381. if (unlikely(ret)) {
  5382. btrfs_abort_transaction(trans, ret);
  5383. return ret;
  5384. }
  5385. if (btrfs_is_fstree(btrfs_root_id(root))) {
  5386. ret = btrfs_qgroup_trace_leaf_items(trans, eb);
  5387. if (ret) {
  5388. btrfs_err_rl(fs_info,
  5389. "error %d accounting leaf items, quota is out of sync, rescan required",
  5390. ret);
  5391. }
  5392. }
  5393. }
  5394. /* Make block locked assertion in btrfs_clear_buffer_dirty happy. */
  5395. if (!path->locks[level]) {
  5396. btrfs_tree_lock(eb);
  5397. path->locks[level] = BTRFS_WRITE_LOCK;
  5398. }
  5399. btrfs_clear_buffer_dirty(trans, eb);
  5400. }
  5401. if (eb == root->node) {
  5402. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5403. parent = eb->start;
  5404. else if (unlikely(btrfs_root_id(root) != btrfs_header_owner(eb)))
  5405. goto owner_mismatch;
  5406. } else {
  5407. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5408. parent = path->nodes[level + 1]->start;
  5409. else if (unlikely(btrfs_root_id(root) !=
  5410. btrfs_header_owner(path->nodes[level + 1])))
  5411. goto owner_mismatch;
  5412. }
  5413. ret = btrfs_free_tree_block(trans, btrfs_root_id(root), eb, parent,
  5414. wc->refs[level] == 1);
  5415. if (ret < 0)
  5416. btrfs_abort_transaction(trans, ret);
  5417. out:
  5418. wc->refs[level] = 0;
  5419. wc->flags[level] = 0;
  5420. return ret;
  5421. owner_mismatch:
  5422. btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
  5423. btrfs_header_owner(eb), btrfs_root_id(root));
  5424. return -EUCLEAN;
  5425. }
  5426. /*
  5427. * walk_down_tree consists of two steps.
  5428. *
  5429. * walk_down_proc(). Look up the reference count and reference of our current
  5430. * wc->level. At this point path->nodes[wc->level] should be populated and
  5431. * uptodate, and in most cases should already be locked. If we are in
  5432. * DROP_REFERENCE and our refcount is > 1 then we've entered a shared node and
  5433. * we can walk back up the tree. If we are UPDATE_BACKREF we have to set
  5434. * FULL_BACKREF on this node if it's not already set, and then do the
  5435. * FULL_BACKREF conversion dance, which is to drop the root reference and add
  5436. * the shared reference to all of this nodes children.
  5437. *
  5438. * do_walk_down(). This is where we actually start iterating on the children of
  5439. * our current path->nodes[wc->level]. For DROP_REFERENCE that means dropping
  5440. * our reference to the children that return false from visit_node_for_delete(),
  5441. * which has various conditions where we know we can just drop our reference
  5442. * without visiting the node. For UPDATE_BACKREF we will skip any children that
  5443. * visit_node_for_delete() returns false for, only walking down when necessary.
  5444. * The bulk of the work for UPDATE_BACKREF occurs in the walk_up_tree() part of
  5445. * snapshot deletion.
  5446. */
  5447. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  5448. struct btrfs_root *root,
  5449. struct btrfs_path *path,
  5450. struct walk_control *wc)
  5451. {
  5452. int level = wc->level;
  5453. int ret = 0;
  5454. wc->lookup_info = 1;
  5455. while (level >= 0) {
  5456. ret = walk_down_proc(trans, root, path, wc);
  5457. if (ret)
  5458. break;
  5459. if (level == 0)
  5460. break;
  5461. if (path->slots[level] >=
  5462. btrfs_header_nritems(path->nodes[level]))
  5463. break;
  5464. ret = do_walk_down(trans, root, path, wc);
  5465. if (ret > 0) {
  5466. path->slots[level]++;
  5467. continue;
  5468. } else if (ret < 0)
  5469. break;
  5470. level = wc->level;
  5471. }
  5472. return (ret == 1) ? 0 : ret;
  5473. }
  5474. /*
  5475. * walk_up_tree() is responsible for making sure we visit every slot on our
  5476. * current node, and if we're at the end of that node then we call
  5477. * walk_up_proc() on our current node which will do one of a few things based on
  5478. * our stage.
  5479. *
  5480. * UPDATE_BACKREF. If we wc->level is currently less than our wc->shared_level
  5481. * then we need to walk back up the tree, and then going back down into the
  5482. * other slots via walk_down_tree to update any other children from our original
  5483. * wc->shared_level. Once we're at or above our wc->shared_level we can switch
  5484. * back to DROP_REFERENCE, lookup the current nodes refs and flags, and carry on.
  5485. *
  5486. * DROP_REFERENCE. If our refs == 1 then we're going to free this tree block.
  5487. * If we're level 0 then we need to btrfs_dec_ref() on all of the data extents
  5488. * in our current leaf. After that we call btrfs_free_tree_block() on the
  5489. * current node and walk up to the next node to walk down the next slot.
  5490. */
  5491. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  5492. struct btrfs_root *root,
  5493. struct btrfs_path *path,
  5494. struct walk_control *wc, int max_level)
  5495. {
  5496. int level = wc->level;
  5497. int ret;
  5498. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  5499. while (level < max_level && path->nodes[level]) {
  5500. wc->level = level;
  5501. if (path->slots[level] + 1 <
  5502. btrfs_header_nritems(path->nodes[level])) {
  5503. path->slots[level]++;
  5504. return 0;
  5505. } else {
  5506. ret = walk_up_proc(trans, root, path, wc);
  5507. if (ret > 0)
  5508. return 0;
  5509. if (ret < 0)
  5510. return ret;
  5511. if (path->locks[level]) {
  5512. btrfs_tree_unlock_rw(path->nodes[level],
  5513. path->locks[level]);
  5514. path->locks[level] = 0;
  5515. }
  5516. free_extent_buffer(path->nodes[level]);
  5517. path->nodes[level] = NULL;
  5518. level++;
  5519. }
  5520. }
  5521. return 1;
  5522. }
  5523. /*
  5524. * drop a subvolume tree.
  5525. *
  5526. * this function traverses the tree freeing any blocks that only
  5527. * referenced by the tree.
  5528. *
  5529. * when a shared tree block is found. this function decreases its
  5530. * reference count by one. if update_ref is true, this function
  5531. * also make sure backrefs for the shared block and all lower level
  5532. * blocks are properly updated.
  5533. *
  5534. * If called with for_reloc set, may exit early with -EAGAIN
  5535. */
  5536. int btrfs_drop_snapshot(struct btrfs_root *root, bool update_ref, bool for_reloc)
  5537. {
  5538. const bool is_reloc_root = (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID);
  5539. struct btrfs_fs_info *fs_info = root->fs_info;
  5540. struct btrfs_path *path;
  5541. struct btrfs_trans_handle *trans;
  5542. struct btrfs_root *tree_root = fs_info->tree_root;
  5543. struct btrfs_root_item *root_item = &root->root_item;
  5544. struct walk_control AUTO_KFREE(wc);
  5545. struct btrfs_key key;
  5546. const u64 rootid = btrfs_root_id(root);
  5547. int ret = 0;
  5548. int level;
  5549. bool root_dropped = false;
  5550. bool unfinished_drop = false;
  5551. btrfs_debug(fs_info, "Drop subvolume %llu", btrfs_root_id(root));
  5552. path = btrfs_alloc_path();
  5553. if (!path) {
  5554. ret = -ENOMEM;
  5555. goto out;
  5556. }
  5557. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5558. if (!wc) {
  5559. ret = -ENOMEM;
  5560. goto out_free;
  5561. }
  5562. /*
  5563. * Use join to avoid potential EINTR from transaction start. See
  5564. * wait_reserve_ticket and the whole reservation callchain.
  5565. */
  5566. if (for_reloc)
  5567. trans = btrfs_join_transaction(tree_root);
  5568. else
  5569. trans = btrfs_start_transaction(tree_root, 0);
  5570. if (IS_ERR(trans)) {
  5571. ret = PTR_ERR(trans);
  5572. goto out_free;
  5573. }
  5574. ret = btrfs_run_delayed_items(trans);
  5575. if (ret)
  5576. goto out_end_trans;
  5577. /*
  5578. * This will help us catch people modifying the fs tree while we're
  5579. * dropping it. It is unsafe to mess with the fs tree while it's being
  5580. * dropped as we unlock the root node and parent nodes as we walk down
  5581. * the tree, assuming nothing will change. If something does change
  5582. * then we'll have stale information and drop references to blocks we've
  5583. * already dropped.
  5584. */
  5585. set_bit(BTRFS_ROOT_DELETING, &root->state);
  5586. unfinished_drop = test_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state);
  5587. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  5588. level = btrfs_header_level(root->node);
  5589. path->nodes[level] = btrfs_lock_root_node(root);
  5590. path->slots[level] = 0;
  5591. path->locks[level] = BTRFS_WRITE_LOCK;
  5592. memset(&wc->update_progress, 0,
  5593. sizeof(wc->update_progress));
  5594. } else {
  5595. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  5596. memcpy(&wc->update_progress, &key,
  5597. sizeof(wc->update_progress));
  5598. level = btrfs_root_drop_level(root_item);
  5599. BUG_ON(level == 0);
  5600. path->lowest_level = level;
  5601. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5602. path->lowest_level = 0;
  5603. if (ret < 0)
  5604. goto out_end_trans;
  5605. WARN_ON(ret > 0);
  5606. ret = 0;
  5607. /*
  5608. * unlock our path, this is safe because only this
  5609. * function is allowed to delete this snapshot
  5610. */
  5611. btrfs_unlock_up_safe(path, 0);
  5612. level = btrfs_header_level(root->node);
  5613. while (1) {
  5614. btrfs_tree_lock(path->nodes[level]);
  5615. path->locks[level] = BTRFS_WRITE_LOCK;
  5616. /*
  5617. * btrfs_lookup_extent_info() returns 0 for success,
  5618. * or < 0 for error.
  5619. */
  5620. ret = btrfs_lookup_extent_info(trans, fs_info,
  5621. path->nodes[level]->start,
  5622. level, 1, &wc->refs[level],
  5623. &wc->flags[level], NULL);
  5624. if (ret < 0)
  5625. goto out_end_trans;
  5626. BUG_ON(wc->refs[level] == 0);
  5627. if (level == btrfs_root_drop_level(root_item))
  5628. break;
  5629. btrfs_tree_unlock(path->nodes[level]);
  5630. path->locks[level] = 0;
  5631. WARN_ON(wc->refs[level] != 1);
  5632. level--;
  5633. }
  5634. }
  5635. wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
  5636. wc->level = level;
  5637. wc->shared_level = -1;
  5638. wc->stage = DROP_REFERENCE;
  5639. wc->update_ref = update_ref;
  5640. wc->keep_locks = 0;
  5641. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
  5642. while (1) {
  5643. ret = walk_down_tree(trans, root, path, wc);
  5644. if (unlikely(ret < 0)) {
  5645. btrfs_abort_transaction(trans, ret);
  5646. break;
  5647. }
  5648. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  5649. if (unlikely(ret < 0)) {
  5650. btrfs_abort_transaction(trans, ret);
  5651. break;
  5652. }
  5653. if (ret > 0) {
  5654. BUG_ON(wc->stage != DROP_REFERENCE);
  5655. ret = 0;
  5656. break;
  5657. }
  5658. if (wc->stage == DROP_REFERENCE) {
  5659. wc->drop_level = wc->level;
  5660. btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
  5661. &wc->drop_progress,
  5662. path->slots[wc->drop_level]);
  5663. }
  5664. btrfs_cpu_key_to_disk(&root_item->drop_progress,
  5665. &wc->drop_progress);
  5666. btrfs_set_root_drop_level(root_item, wc->drop_level);
  5667. BUG_ON(wc->level == 0);
  5668. if (btrfs_should_end_transaction(trans) ||
  5669. (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
  5670. ret = btrfs_update_root(trans, tree_root,
  5671. &root->root_key,
  5672. root_item);
  5673. if (unlikely(ret)) {
  5674. btrfs_abort_transaction(trans, ret);
  5675. goto out_end_trans;
  5676. }
  5677. if (!is_reloc_root)
  5678. btrfs_set_last_root_drop_gen(fs_info, trans->transid);
  5679. btrfs_end_transaction_throttle(trans);
  5680. if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
  5681. btrfs_debug(fs_info,
  5682. "drop snapshot early exit");
  5683. ret = -EAGAIN;
  5684. goto out_free;
  5685. }
  5686. /*
  5687. * Use join to avoid potential EINTR from transaction
  5688. * start. See wait_reserve_ticket and the whole
  5689. * reservation callchain.
  5690. */
  5691. if (for_reloc)
  5692. trans = btrfs_join_transaction(tree_root);
  5693. else
  5694. trans = btrfs_start_transaction(tree_root, 0);
  5695. if (IS_ERR(trans)) {
  5696. ret = PTR_ERR(trans);
  5697. goto out_free;
  5698. }
  5699. }
  5700. }
  5701. btrfs_release_path(path);
  5702. if (ret)
  5703. goto out_end_trans;
  5704. ret = btrfs_del_root(trans, &root->root_key);
  5705. if (unlikely(ret)) {
  5706. btrfs_abort_transaction(trans, ret);
  5707. goto out_end_trans;
  5708. }
  5709. if (!is_reloc_root) {
  5710. ret = btrfs_find_root(tree_root, &root->root_key, path,
  5711. NULL, NULL);
  5712. if (unlikely(ret < 0)) {
  5713. btrfs_abort_transaction(trans, ret);
  5714. goto out_end_trans;
  5715. } else if (ret > 0) {
  5716. ret = 0;
  5717. /*
  5718. * If we fail to delete the orphan item this time
  5719. * around, it'll get picked up the next time.
  5720. *
  5721. * The most common failure here is just -ENOENT.
  5722. */
  5723. btrfs_del_orphan_item(trans, tree_root, btrfs_root_id(root));
  5724. }
  5725. }
  5726. /*
  5727. * This subvolume is going to be completely dropped, and won't be
  5728. * recorded as dirty roots, thus pertrans meta rsv will not be freed at
  5729. * commit transaction time. So free it here manually.
  5730. */
  5731. btrfs_qgroup_convert_reserved_meta(root, INT_MAX);
  5732. btrfs_qgroup_free_meta_all_pertrans(root);
  5733. if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state))
  5734. btrfs_add_dropped_root(trans, root);
  5735. else
  5736. btrfs_put_root(root);
  5737. root_dropped = true;
  5738. out_end_trans:
  5739. if (!is_reloc_root)
  5740. btrfs_set_last_root_drop_gen(fs_info, trans->transid);
  5741. btrfs_end_transaction_throttle(trans);
  5742. out_free:
  5743. btrfs_free_path(path);
  5744. out:
  5745. if (!ret && root_dropped) {
  5746. ret = btrfs_qgroup_cleanup_dropped_subvolume(fs_info, rootid);
  5747. if (ret < 0)
  5748. btrfs_warn_rl(fs_info,
  5749. "failed to cleanup qgroup 0/%llu: %d",
  5750. rootid, ret);
  5751. ret = 0;
  5752. }
  5753. /*
  5754. * We were an unfinished drop root, check to see if there are any
  5755. * pending, and if not clear and wake up any waiters.
  5756. */
  5757. if (!ret && unfinished_drop)
  5758. btrfs_maybe_wake_unfinished_drop(fs_info);
  5759. /*
  5760. * So if we need to stop dropping the snapshot for whatever reason we
  5761. * need to make sure to add it back to the dead root list so that we
  5762. * keep trying to do the work later. This also cleans up roots if we
  5763. * don't have it in the radix (like when we recover after a power fail
  5764. * or unmount) so we don't leak memory.
  5765. */
  5766. if (!for_reloc && !root_dropped)
  5767. btrfs_add_dead_root(root);
  5768. return ret;
  5769. }
  5770. /*
  5771. * drop subtree rooted at tree block 'node'.
  5772. *
  5773. * NOTE: this function will unlock and release tree block 'node'
  5774. * only used by relocation code
  5775. */
  5776. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  5777. struct btrfs_root *root,
  5778. struct extent_buffer *node,
  5779. struct extent_buffer *parent)
  5780. {
  5781. struct btrfs_fs_info *fs_info = root->fs_info;
  5782. BTRFS_PATH_AUTO_FREE(path);
  5783. struct walk_control AUTO_KFREE(wc);
  5784. int level;
  5785. int parent_level;
  5786. int ret = 0;
  5787. BUG_ON(btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID);
  5788. path = btrfs_alloc_path();
  5789. if (!path)
  5790. return -ENOMEM;
  5791. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5792. if (!wc)
  5793. return -ENOMEM;
  5794. btrfs_assert_tree_write_locked(parent);
  5795. parent_level = btrfs_header_level(parent);
  5796. refcount_inc(&parent->refs);
  5797. path->nodes[parent_level] = parent;
  5798. path->slots[parent_level] = btrfs_header_nritems(parent);
  5799. btrfs_assert_tree_write_locked(node);
  5800. level = btrfs_header_level(node);
  5801. path->nodes[level] = node;
  5802. path->slots[level] = 0;
  5803. path->locks[level] = BTRFS_WRITE_LOCK;
  5804. wc->refs[parent_level] = 1;
  5805. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5806. wc->level = level;
  5807. wc->shared_level = -1;
  5808. wc->stage = DROP_REFERENCE;
  5809. wc->update_ref = 0;
  5810. wc->keep_locks = 1;
  5811. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
  5812. while (1) {
  5813. ret = walk_down_tree(trans, root, path, wc);
  5814. if (ret < 0)
  5815. return ret;
  5816. ret = walk_up_tree(trans, root, path, wc, parent_level);
  5817. if (ret) {
  5818. if (ret < 0)
  5819. return ret;
  5820. break;
  5821. }
  5822. }
  5823. return 0;
  5824. }
  5825. /*
  5826. * Unpin the extent range in an error context and don't add the space back.
  5827. * Errors are not propagated further.
  5828. */
  5829. void btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, u64 start, u64 end)
  5830. {
  5831. unpin_extent_range(fs_info, start, end, false);
  5832. }
  5833. /*
  5834. * It used to be that old block groups would be left around forever.
  5835. * Iterating over them would be enough to trim unused space. Since we
  5836. * now automatically remove them, we also need to iterate over unallocated
  5837. * space.
  5838. *
  5839. * We don't want a transaction for this since the discard may take a
  5840. * substantial amount of time. We don't require that a transaction be
  5841. * running, but we do need to take a running transaction into account
  5842. * to ensure that we're not discarding chunks that were released or
  5843. * allocated in the current transaction.
  5844. *
  5845. * Holding the chunks lock will prevent other threads from allocating
  5846. * or releasing chunks, but it won't prevent a running transaction
  5847. * from committing and releasing the memory that the pending chunks
  5848. * list head uses. For that, we need to take a reference to the
  5849. * transaction and hold the commit root sem. We only need to hold
  5850. * it while performing the free space search since we have already
  5851. * held back allocations.
  5852. */
  5853. static int btrfs_trim_free_extents_throttle(struct btrfs_device *device,
  5854. u64 *trimmed, u64 pos, u64 *ret_next_pos)
  5855. {
  5856. int ret;
  5857. u64 start = pos;
  5858. u64 trim_len = 0;
  5859. *trimmed = 0;
  5860. /* Discard not supported = nothing to do. */
  5861. if (!bdev_max_discard_sectors(device->bdev))
  5862. return 0;
  5863. /* Not writable = nothing to do. */
  5864. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
  5865. return 0;
  5866. /* No free space = nothing to do. */
  5867. if (device->total_bytes <= device->bytes_used)
  5868. return 0;
  5869. ret = 0;
  5870. while (1) {
  5871. struct btrfs_fs_info *fs_info = device->fs_info;
  5872. u64 cur_start;
  5873. u64 end;
  5874. u64 len;
  5875. u64 bytes;
  5876. ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
  5877. if (ret)
  5878. break;
  5879. cur_start = start;
  5880. btrfs_find_first_clear_extent_bit(&device->alloc_state, start,
  5881. &start, &end,
  5882. CHUNK_TRIMMED | CHUNK_ALLOCATED);
  5883. start = max(start, cur_start);
  5884. /* Check if there are any CHUNK_* bits left */
  5885. if (start > device->total_bytes) {
  5886. DEBUG_WARN();
  5887. btrfs_warn(fs_info,
  5888. "ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu",
  5889. start, end - start + 1,
  5890. btrfs_dev_name(device),
  5891. device->total_bytes);
  5892. mutex_unlock(&fs_info->chunk_mutex);
  5893. ret = 0;
  5894. break;
  5895. }
  5896. /* Ensure we skip the reserved space on each device. */
  5897. start = max_t(u64, start, BTRFS_DEVICE_RANGE_RESERVED);
  5898. /*
  5899. * If find_first_clear_extent_bit find a range that spans the
  5900. * end of the device it will set end to -1, in this case it's up
  5901. * to the caller to trim the value to the size of the device.
  5902. */
  5903. end = min(end, device->total_bytes - 1);
  5904. len = end - start + 1;
  5905. len = min(len, BTRFS_MAX_TRIM_LENGTH);
  5906. /* We didn't find any extents */
  5907. if (!len) {
  5908. mutex_unlock(&fs_info->chunk_mutex);
  5909. ret = 0;
  5910. break;
  5911. }
  5912. ret = btrfs_issue_discard(device->bdev, start, len,
  5913. &bytes);
  5914. if (!ret)
  5915. btrfs_set_extent_bit(&device->alloc_state, start,
  5916. start + bytes - 1, CHUNK_TRIMMED, NULL);
  5917. mutex_unlock(&fs_info->chunk_mutex);
  5918. if (ret)
  5919. break;
  5920. start += len;
  5921. *trimmed += bytes;
  5922. trim_len += len;
  5923. if (trim_len >= BTRFS_MAX_TRIM_LENGTH) {
  5924. *ret_next_pos = start;
  5925. ret = -EAGAIN;
  5926. break;
  5927. }
  5928. if (btrfs_trim_interrupted()) {
  5929. ret = -ERESTARTSYS;
  5930. break;
  5931. }
  5932. cond_resched();
  5933. }
  5934. return ret;
  5935. }
  5936. static int btrfs_trim_free_extents(struct btrfs_fs_info *fs_info, u64 *trimmed,
  5937. u64 *dev_failed, int *dev_ret)
  5938. {
  5939. struct btrfs_device *dev;
  5940. struct btrfs_device *working_dev = NULL;
  5941. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5942. u8 uuid[BTRFS_UUID_SIZE];
  5943. u64 start = BTRFS_DEVICE_RANGE_RESERVED;
  5944. *trimmed = 0;
  5945. *dev_failed = 0;
  5946. *dev_ret = 0;
  5947. /* Find the device with the smallest UUID to start. */
  5948. mutex_lock(&fs_devices->device_list_mutex);
  5949. list_for_each_entry(dev, &fs_devices->devices, dev_list) {
  5950. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  5951. continue;
  5952. if (!working_dev ||
  5953. memcmp(dev->uuid, working_dev->uuid, BTRFS_UUID_SIZE) < 0)
  5954. working_dev = dev;
  5955. }
  5956. if (working_dev)
  5957. memcpy(uuid, working_dev->uuid, BTRFS_UUID_SIZE);
  5958. mutex_unlock(&fs_devices->device_list_mutex);
  5959. if (!working_dev)
  5960. return 0;
  5961. while (1) {
  5962. u64 group_trimmed = 0;
  5963. u64 next_pos = 0;
  5964. int ret = 0;
  5965. mutex_lock(&fs_devices->device_list_mutex);
  5966. /* Find and trim the current device. */
  5967. list_for_each_entry(dev, &fs_devices->devices, dev_list) {
  5968. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  5969. continue;
  5970. if (dev == working_dev) {
  5971. ret = btrfs_trim_free_extents_throttle(working_dev,
  5972. &group_trimmed, start, &next_pos);
  5973. break;
  5974. }
  5975. }
  5976. /* Throttle: continue the same device from the new position. */
  5977. if (ret == -EAGAIN && next_pos > start) {
  5978. mutex_unlock(&fs_devices->device_list_mutex);
  5979. *trimmed += group_trimmed;
  5980. start = next_pos;
  5981. cond_resched();
  5982. continue;
  5983. }
  5984. /* User interrupted. */
  5985. if (ret == -ERESTARTSYS || ret == -EINTR) {
  5986. mutex_unlock(&fs_devices->device_list_mutex);
  5987. *trimmed += group_trimmed;
  5988. return ret;
  5989. }
  5990. /*
  5991. * Device completed (ret == 0), failed, or EAGAIN with no progress.
  5992. * Record error if any, then move to next device.
  5993. */
  5994. if (ret == -EAGAIN) {
  5995. /* No progress - log and skip device. */
  5996. btrfs_warn(fs_info,
  5997. "trim throttle: no progress, offset=%llu device %s, skipping",
  5998. start, btrfs_dev_name(working_dev));
  5999. (*dev_failed)++;
  6000. if (!*dev_ret)
  6001. *dev_ret = ret;
  6002. } else if (ret) {
  6003. /* Device failed with error. */
  6004. (*dev_failed)++;
  6005. if (!*dev_ret)
  6006. *dev_ret = ret;
  6007. }
  6008. /*
  6009. * Find next device: smallest UUID larger than current.
  6010. * Devices added during trim with smaller UUID will be skipped.
  6011. */
  6012. working_dev = NULL;
  6013. list_for_each_entry(dev, &fs_devices->devices, dev_list) {
  6014. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  6015. continue;
  6016. /* Must larger than current UUID. */
  6017. if (memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE) <= 0)
  6018. continue;
  6019. /* Find the smallest. */
  6020. if (!working_dev ||
  6021. memcmp(dev->uuid, working_dev->uuid, BTRFS_UUID_SIZE) < 0)
  6022. working_dev = dev;
  6023. }
  6024. if (working_dev)
  6025. memcpy(uuid, working_dev->uuid, BTRFS_UUID_SIZE);
  6026. mutex_unlock(&fs_devices->device_list_mutex);
  6027. *trimmed += group_trimmed;
  6028. start = BTRFS_DEVICE_RANGE_RESERVED;
  6029. /* No more devices. */
  6030. if (!working_dev)
  6031. break;
  6032. cond_resched();
  6033. }
  6034. return 0;
  6035. }
  6036. /*
  6037. * Trim the whole filesystem by:
  6038. * 1) trimming the free space in each block group
  6039. * 2) trimming the unallocated space on each device
  6040. *
  6041. * This will also continue trimming even if a block group or device encounters
  6042. * an error. The return value will be the first error, or 0 if nothing bad
  6043. * happens.
  6044. */
  6045. int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
  6046. {
  6047. struct btrfs_block_group *cache = NULL;
  6048. u64 group_trimmed;
  6049. u64 range_end = U64_MAX;
  6050. u64 start;
  6051. u64 end;
  6052. u64 trimmed = 0;
  6053. u64 bg_failed = 0;
  6054. u64 dev_failed = 0;
  6055. int bg_ret = 0;
  6056. int dev_ret = 0;
  6057. int ret = 0;
  6058. if (range->start == U64_MAX)
  6059. return -EINVAL;
  6060. /*
  6061. * Check range overflow if range->len is set.
  6062. * The default range->len is U64_MAX.
  6063. */
  6064. if (range->len != U64_MAX &&
  6065. check_add_overflow(range->start, range->len, &range_end))
  6066. return -EINVAL;
  6067. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  6068. for (; cache; cache = btrfs_next_block_group(cache)) {
  6069. if (cache->start >= range_end) {
  6070. btrfs_put_block_group(cache);
  6071. break;
  6072. }
  6073. start = max(range->start, cache->start);
  6074. end = min(range_end, btrfs_block_group_end(cache));
  6075. if (end - start >= range->minlen) {
  6076. if (!btrfs_block_group_done(cache)) {
  6077. ret = btrfs_cache_block_group(cache, true);
  6078. if (ret) {
  6079. bg_failed++;
  6080. if (!bg_ret)
  6081. bg_ret = ret;
  6082. continue;
  6083. }
  6084. }
  6085. ret = btrfs_trim_block_group(cache,
  6086. &group_trimmed,
  6087. start,
  6088. end,
  6089. range->minlen);
  6090. trimmed += group_trimmed;
  6091. if (ret == -ERESTARTSYS || ret == -EINTR) {
  6092. btrfs_put_block_group(cache);
  6093. break;
  6094. }
  6095. if (ret) {
  6096. bg_failed++;
  6097. if (!bg_ret)
  6098. bg_ret = ret;
  6099. continue;
  6100. }
  6101. }
  6102. }
  6103. if (bg_failed)
  6104. btrfs_warn(fs_info,
  6105. "failed to trim %llu block group(s), first error %d",
  6106. bg_failed, bg_ret);
  6107. if (ret == -ERESTARTSYS || ret == -EINTR)
  6108. return ret;
  6109. ret = btrfs_trim_free_extents(fs_info, &group_trimmed, &dev_failed, &dev_ret);
  6110. trimmed += group_trimmed;
  6111. if (dev_failed)
  6112. btrfs_warn(fs_info,
  6113. "failed to trim %llu device(s), first error %d",
  6114. dev_failed, dev_ret);
  6115. range->len = trimmed;
  6116. if (ret == -ERESTARTSYS || ret == -EINTR)
  6117. return ret;
  6118. if (bg_ret)
  6119. return bg_ret;
  6120. return dev_ret;
  6121. }