extent-tree.c 236 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/writeback.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/sort.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/kthread.h>
  25. #include <linux/slab.h>
  26. #include <linux/ratelimit.h>
  27. #include <linux/percpu_counter.h>
  28. #include "compat.h"
  29. #include "hash.h"
  30. #include "ctree.h"
  31. #include "disk-io.h"
  32. #include "print-tree.h"
  33. #include "transaction.h"
  34. #include "volumes.h"
  35. #include "raid56.h"
  36. #include "locking.h"
  37. #include "free-space-cache.h"
  38. #include "math.h"
  39. #undef SCRAMBLE_DELAYED_REFS
  40. /*
  41. * control flags for do_chunk_alloc's force field
  42. * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
  43. * if we really need one.
  44. *
  45. * CHUNK_ALLOC_LIMITED means to only try and allocate one
  46. * if we have very few chunks already allocated. This is
  47. * used as part of the clustering code to help make sure
  48. * we have a good pool of storage to cluster in, without
  49. * filling the FS with empty chunks
  50. *
  51. * CHUNK_ALLOC_FORCE means it must try to allocate one
  52. *
  53. */
  54. enum {
  55. CHUNK_ALLOC_NO_FORCE = 0,
  56. CHUNK_ALLOC_LIMITED = 1,
  57. CHUNK_ALLOC_FORCE = 2,
  58. };
  59. /*
  60. * Control how reservations are dealt with.
  61. *
  62. * RESERVE_FREE - freeing a reservation.
  63. * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
  64. * ENOSPC accounting
  65. * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
  66. * bytes_may_use as the ENOSPC accounting is done elsewhere
  67. */
  68. enum {
  69. RESERVE_FREE = 0,
  70. RESERVE_ALLOC = 1,
  71. RESERVE_ALLOC_NO_ACCOUNT = 2,
  72. };
  73. static int update_block_group(struct btrfs_root *root,
  74. u64 bytenr, u64 num_bytes, int alloc);
  75. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  76. struct btrfs_root *root,
  77. u64 bytenr, u64 num_bytes, u64 parent,
  78. u64 root_objectid, u64 owner_objectid,
  79. u64 owner_offset, int refs_to_drop,
  80. struct btrfs_delayed_extent_op *extra_op);
  81. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  82. struct extent_buffer *leaf,
  83. struct btrfs_extent_item *ei);
  84. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  85. struct btrfs_root *root,
  86. u64 parent, u64 root_objectid,
  87. u64 flags, u64 owner, u64 offset,
  88. struct btrfs_key *ins, int ref_mod);
  89. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  90. struct btrfs_root *root,
  91. u64 parent, u64 root_objectid,
  92. u64 flags, struct btrfs_disk_key *key,
  93. int level, struct btrfs_key *ins);
  94. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  95. struct btrfs_root *extent_root, u64 flags,
  96. int force);
  97. static int find_next_key(struct btrfs_path *path, int level,
  98. struct btrfs_key *key);
  99. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  100. int dump_block_groups);
  101. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  102. u64 num_bytes, int reserve);
  103. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  104. u64 num_bytes);
  105. int btrfs_pin_extent(struct btrfs_root *root,
  106. u64 bytenr, u64 num_bytes, int reserved);
  107. static noinline int
  108. block_group_cache_done(struct btrfs_block_group_cache *cache)
  109. {
  110. smp_mb();
  111. return cache->cached == BTRFS_CACHE_FINISHED ||
  112. cache->cached == BTRFS_CACHE_ERROR;
  113. }
  114. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  115. {
  116. return (cache->flags & bits) == bits;
  117. }
  118. static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
  119. {
  120. atomic_inc(&cache->count);
  121. }
  122. void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
  123. {
  124. if (atomic_dec_and_test(&cache->count)) {
  125. WARN_ON(cache->pinned > 0);
  126. WARN_ON(cache->reserved > 0);
  127. kfree(cache->free_space_ctl);
  128. kfree(cache);
  129. }
  130. }
  131. /*
  132. * this adds the block group to the fs_info rb tree for the block group
  133. * cache
  134. */
  135. static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  136. struct btrfs_block_group_cache *block_group)
  137. {
  138. struct rb_node **p;
  139. struct rb_node *parent = NULL;
  140. struct btrfs_block_group_cache *cache;
  141. spin_lock(&info->block_group_cache_lock);
  142. p = &info->block_group_cache_tree.rb_node;
  143. while (*p) {
  144. parent = *p;
  145. cache = rb_entry(parent, struct btrfs_block_group_cache,
  146. cache_node);
  147. if (block_group->key.objectid < cache->key.objectid) {
  148. p = &(*p)->rb_left;
  149. } else if (block_group->key.objectid > cache->key.objectid) {
  150. p = &(*p)->rb_right;
  151. } else {
  152. spin_unlock(&info->block_group_cache_lock);
  153. return -EEXIST;
  154. }
  155. }
  156. rb_link_node(&block_group->cache_node, parent, p);
  157. rb_insert_color(&block_group->cache_node,
  158. &info->block_group_cache_tree);
  159. if (info->first_logical_byte > block_group->key.objectid)
  160. info->first_logical_byte = block_group->key.objectid;
  161. spin_unlock(&info->block_group_cache_lock);
  162. return 0;
  163. }
  164. /*
  165. * This will return the block group at or after bytenr if contains is 0, else
  166. * it will return the block group that contains the bytenr
  167. */
  168. static struct btrfs_block_group_cache *
  169. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  170. int contains)
  171. {
  172. struct btrfs_block_group_cache *cache, *ret = NULL;
  173. struct rb_node *n;
  174. u64 end, start;
  175. spin_lock(&info->block_group_cache_lock);
  176. n = info->block_group_cache_tree.rb_node;
  177. while (n) {
  178. cache = rb_entry(n, struct btrfs_block_group_cache,
  179. cache_node);
  180. end = cache->key.objectid + cache->key.offset - 1;
  181. start = cache->key.objectid;
  182. if (bytenr < start) {
  183. if (!contains && (!ret || start < ret->key.objectid))
  184. ret = cache;
  185. n = n->rb_left;
  186. } else if (bytenr > start) {
  187. if (contains && bytenr <= end) {
  188. ret = cache;
  189. break;
  190. }
  191. n = n->rb_right;
  192. } else {
  193. ret = cache;
  194. break;
  195. }
  196. }
  197. if (ret) {
  198. btrfs_get_block_group(ret);
  199. if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
  200. info->first_logical_byte = ret->key.objectid;
  201. }
  202. spin_unlock(&info->block_group_cache_lock);
  203. return ret;
  204. }
  205. static int add_excluded_extent(struct btrfs_root *root,
  206. u64 start, u64 num_bytes)
  207. {
  208. u64 end = start + num_bytes - 1;
  209. set_extent_bits(&root->fs_info->freed_extents[0],
  210. start, end, EXTENT_UPTODATE, GFP_NOFS);
  211. set_extent_bits(&root->fs_info->freed_extents[1],
  212. start, end, EXTENT_UPTODATE, GFP_NOFS);
  213. return 0;
  214. }
  215. static void free_excluded_extents(struct btrfs_root *root,
  216. struct btrfs_block_group_cache *cache)
  217. {
  218. u64 start, end;
  219. start = cache->key.objectid;
  220. end = start + cache->key.offset - 1;
  221. clear_extent_bits(&root->fs_info->freed_extents[0],
  222. start, end, EXTENT_UPTODATE, GFP_NOFS);
  223. clear_extent_bits(&root->fs_info->freed_extents[1],
  224. start, end, EXTENT_UPTODATE, GFP_NOFS);
  225. }
  226. static int exclude_super_stripes(struct btrfs_root *root,
  227. struct btrfs_block_group_cache *cache)
  228. {
  229. u64 bytenr;
  230. u64 *logical;
  231. int stripe_len;
  232. int i, nr, ret;
  233. if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
  234. stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
  235. cache->bytes_super += stripe_len;
  236. ret = add_excluded_extent(root, cache->key.objectid,
  237. stripe_len);
  238. if (ret)
  239. return ret;
  240. }
  241. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  242. bytenr = btrfs_sb_offset(i);
  243. ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
  244. cache->key.objectid, bytenr,
  245. 0, &logical, &nr, &stripe_len);
  246. if (ret)
  247. return ret;
  248. while (nr--) {
  249. u64 start, len;
  250. if (logical[nr] > cache->key.objectid +
  251. cache->key.offset)
  252. continue;
  253. if (logical[nr] + stripe_len <= cache->key.objectid)
  254. continue;
  255. start = logical[nr];
  256. if (start < cache->key.objectid) {
  257. start = cache->key.objectid;
  258. len = (logical[nr] + stripe_len) - start;
  259. } else {
  260. len = min_t(u64, stripe_len,
  261. cache->key.objectid +
  262. cache->key.offset - start);
  263. }
  264. cache->bytes_super += len;
  265. ret = add_excluded_extent(root, start, len);
  266. if (ret) {
  267. kfree(logical);
  268. return ret;
  269. }
  270. }
  271. kfree(logical);
  272. }
  273. return 0;
  274. }
  275. static struct btrfs_caching_control *
  276. get_caching_control(struct btrfs_block_group_cache *cache)
  277. {
  278. struct btrfs_caching_control *ctl;
  279. spin_lock(&cache->lock);
  280. if (cache->cached != BTRFS_CACHE_STARTED) {
  281. spin_unlock(&cache->lock);
  282. return NULL;
  283. }
  284. /* We're loading it the fast way, so we don't have a caching_ctl. */
  285. if (!cache->caching_ctl) {
  286. spin_unlock(&cache->lock);
  287. return NULL;
  288. }
  289. ctl = cache->caching_ctl;
  290. atomic_inc(&ctl->count);
  291. spin_unlock(&cache->lock);
  292. return ctl;
  293. }
  294. static void put_caching_control(struct btrfs_caching_control *ctl)
  295. {
  296. if (atomic_dec_and_test(&ctl->count))
  297. kfree(ctl);
  298. }
  299. /*
  300. * this is only called by cache_block_group, since we could have freed extents
  301. * we need to check the pinned_extents for any extents that can't be used yet
  302. * since their free space will be released as soon as the transaction commits.
  303. */
  304. static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
  305. struct btrfs_fs_info *info, u64 start, u64 end)
  306. {
  307. u64 extent_start, extent_end, size, total_added = 0;
  308. int ret;
  309. while (start < end) {
  310. ret = find_first_extent_bit(info->pinned_extents, start,
  311. &extent_start, &extent_end,
  312. EXTENT_DIRTY | EXTENT_UPTODATE,
  313. NULL);
  314. if (ret)
  315. break;
  316. if (extent_start <= start) {
  317. start = extent_end + 1;
  318. } else if (extent_start > start && extent_start < end) {
  319. size = extent_start - start;
  320. total_added += size;
  321. ret = btrfs_add_free_space(block_group, start,
  322. size);
  323. BUG_ON(ret); /* -ENOMEM or logic error */
  324. start = extent_end + 1;
  325. } else {
  326. break;
  327. }
  328. }
  329. if (start < end) {
  330. size = end - start;
  331. total_added += size;
  332. ret = btrfs_add_free_space(block_group, start, size);
  333. BUG_ON(ret); /* -ENOMEM or logic error */
  334. }
  335. return total_added;
  336. }
  337. static noinline void caching_thread(struct btrfs_work *work)
  338. {
  339. struct btrfs_block_group_cache *block_group;
  340. struct btrfs_fs_info *fs_info;
  341. struct btrfs_caching_control *caching_ctl;
  342. struct btrfs_root *extent_root;
  343. struct btrfs_path *path;
  344. struct extent_buffer *leaf;
  345. struct btrfs_key key;
  346. u64 total_found = 0;
  347. u64 last = 0;
  348. u32 nritems;
  349. int ret = -ENOMEM;
  350. caching_ctl = container_of(work, struct btrfs_caching_control, work);
  351. block_group = caching_ctl->block_group;
  352. fs_info = block_group->fs_info;
  353. extent_root = fs_info->extent_root;
  354. path = btrfs_alloc_path();
  355. if (!path)
  356. goto out;
  357. last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
  358. /*
  359. * We don't want to deadlock with somebody trying to allocate a new
  360. * extent for the extent root while also trying to search the extent
  361. * root to add free space. So we skip locking and search the commit
  362. * root, since its read-only
  363. */
  364. path->skip_locking = 1;
  365. path->search_commit_root = 1;
  366. path->reada = 1;
  367. key.objectid = last;
  368. key.offset = 0;
  369. key.type = BTRFS_EXTENT_ITEM_KEY;
  370. again:
  371. mutex_lock(&caching_ctl->mutex);
  372. /* need to make sure the commit_root doesn't disappear */
  373. down_read(&fs_info->extent_commit_sem);
  374. next:
  375. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  376. if (ret < 0)
  377. goto err;
  378. leaf = path->nodes[0];
  379. nritems = btrfs_header_nritems(leaf);
  380. while (1) {
  381. if (btrfs_fs_closing(fs_info) > 1) {
  382. last = (u64)-1;
  383. break;
  384. }
  385. if (path->slots[0] < nritems) {
  386. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  387. } else {
  388. ret = find_next_key(path, 0, &key);
  389. if (ret)
  390. break;
  391. if (need_resched()) {
  392. caching_ctl->progress = last;
  393. btrfs_release_path(path);
  394. up_read(&fs_info->extent_commit_sem);
  395. mutex_unlock(&caching_ctl->mutex);
  396. cond_resched();
  397. goto again;
  398. }
  399. ret = btrfs_next_leaf(extent_root, path);
  400. if (ret < 0)
  401. goto err;
  402. if (ret)
  403. break;
  404. leaf = path->nodes[0];
  405. nritems = btrfs_header_nritems(leaf);
  406. continue;
  407. }
  408. if (key.objectid < last) {
  409. key.objectid = last;
  410. key.offset = 0;
  411. key.type = BTRFS_EXTENT_ITEM_KEY;
  412. caching_ctl->progress = last;
  413. btrfs_release_path(path);
  414. goto next;
  415. }
  416. if (key.objectid < block_group->key.objectid) {
  417. path->slots[0]++;
  418. continue;
  419. }
  420. if (key.objectid >= block_group->key.objectid +
  421. block_group->key.offset)
  422. break;
  423. if (key.type == BTRFS_EXTENT_ITEM_KEY ||
  424. key.type == BTRFS_METADATA_ITEM_KEY) {
  425. total_found += add_new_free_space(block_group,
  426. fs_info, last,
  427. key.objectid);
  428. if (key.type == BTRFS_METADATA_ITEM_KEY)
  429. last = key.objectid +
  430. fs_info->tree_root->leafsize;
  431. else
  432. last = key.objectid + key.offset;
  433. if (total_found > (1024 * 1024 * 2)) {
  434. total_found = 0;
  435. wake_up(&caching_ctl->wait);
  436. }
  437. }
  438. path->slots[0]++;
  439. }
  440. ret = 0;
  441. total_found += add_new_free_space(block_group, fs_info, last,
  442. block_group->key.objectid +
  443. block_group->key.offset);
  444. caching_ctl->progress = (u64)-1;
  445. spin_lock(&block_group->lock);
  446. block_group->caching_ctl = NULL;
  447. block_group->cached = BTRFS_CACHE_FINISHED;
  448. spin_unlock(&block_group->lock);
  449. err:
  450. btrfs_free_path(path);
  451. up_read(&fs_info->extent_commit_sem);
  452. free_excluded_extents(extent_root, block_group);
  453. mutex_unlock(&caching_ctl->mutex);
  454. out:
  455. if (ret) {
  456. spin_lock(&block_group->lock);
  457. block_group->caching_ctl = NULL;
  458. block_group->cached = BTRFS_CACHE_ERROR;
  459. spin_unlock(&block_group->lock);
  460. }
  461. wake_up(&caching_ctl->wait);
  462. put_caching_control(caching_ctl);
  463. btrfs_put_block_group(block_group);
  464. }
  465. static int cache_block_group(struct btrfs_block_group_cache *cache,
  466. int load_cache_only)
  467. {
  468. DEFINE_WAIT(wait);
  469. struct btrfs_fs_info *fs_info = cache->fs_info;
  470. struct btrfs_caching_control *caching_ctl;
  471. int ret = 0;
  472. caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
  473. if (!caching_ctl)
  474. return -ENOMEM;
  475. INIT_LIST_HEAD(&caching_ctl->list);
  476. mutex_init(&caching_ctl->mutex);
  477. init_waitqueue_head(&caching_ctl->wait);
  478. caching_ctl->block_group = cache;
  479. caching_ctl->progress = cache->key.objectid;
  480. atomic_set(&caching_ctl->count, 1);
  481. caching_ctl->work.func = caching_thread;
  482. spin_lock(&cache->lock);
  483. /*
  484. * This should be a rare occasion, but this could happen I think in the
  485. * case where one thread starts to load the space cache info, and then
  486. * some other thread starts a transaction commit which tries to do an
  487. * allocation while the other thread is still loading the space cache
  488. * info. The previous loop should have kept us from choosing this block
  489. * group, but if we've moved to the state where we will wait on caching
  490. * block groups we need to first check if we're doing a fast load here,
  491. * so we can wait for it to finish, otherwise we could end up allocating
  492. * from a block group who's cache gets evicted for one reason or
  493. * another.
  494. */
  495. while (cache->cached == BTRFS_CACHE_FAST) {
  496. struct btrfs_caching_control *ctl;
  497. ctl = cache->caching_ctl;
  498. atomic_inc(&ctl->count);
  499. prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
  500. spin_unlock(&cache->lock);
  501. schedule();
  502. finish_wait(&ctl->wait, &wait);
  503. put_caching_control(ctl);
  504. spin_lock(&cache->lock);
  505. }
  506. if (cache->cached != BTRFS_CACHE_NO) {
  507. spin_unlock(&cache->lock);
  508. kfree(caching_ctl);
  509. return 0;
  510. }
  511. WARN_ON(cache->caching_ctl);
  512. cache->caching_ctl = caching_ctl;
  513. cache->cached = BTRFS_CACHE_FAST;
  514. spin_unlock(&cache->lock);
  515. if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
  516. ret = load_free_space_cache(fs_info, cache);
  517. spin_lock(&cache->lock);
  518. if (ret == 1) {
  519. cache->caching_ctl = NULL;
  520. cache->cached = BTRFS_CACHE_FINISHED;
  521. cache->last_byte_to_unpin = (u64)-1;
  522. } else {
  523. if (load_cache_only) {
  524. cache->caching_ctl = NULL;
  525. cache->cached = BTRFS_CACHE_NO;
  526. } else {
  527. cache->cached = BTRFS_CACHE_STARTED;
  528. }
  529. }
  530. spin_unlock(&cache->lock);
  531. wake_up(&caching_ctl->wait);
  532. if (ret == 1) {
  533. put_caching_control(caching_ctl);
  534. free_excluded_extents(fs_info->extent_root, cache);
  535. return 0;
  536. }
  537. } else {
  538. /*
  539. * We are not going to do the fast caching, set cached to the
  540. * appropriate value and wakeup any waiters.
  541. */
  542. spin_lock(&cache->lock);
  543. if (load_cache_only) {
  544. cache->caching_ctl = NULL;
  545. cache->cached = BTRFS_CACHE_NO;
  546. } else {
  547. cache->cached = BTRFS_CACHE_STARTED;
  548. }
  549. spin_unlock(&cache->lock);
  550. wake_up(&caching_ctl->wait);
  551. }
  552. if (load_cache_only) {
  553. put_caching_control(caching_ctl);
  554. return 0;
  555. }
  556. down_write(&fs_info->extent_commit_sem);
  557. atomic_inc(&caching_ctl->count);
  558. list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
  559. up_write(&fs_info->extent_commit_sem);
  560. btrfs_get_block_group(cache);
  561. btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
  562. return ret;
  563. }
  564. /*
  565. * return the block group that starts at or after bytenr
  566. */
  567. static struct btrfs_block_group_cache *
  568. btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
  569. {
  570. struct btrfs_block_group_cache *cache;
  571. cache = block_group_cache_tree_search(info, bytenr, 0);
  572. return cache;
  573. }
  574. /*
  575. * return the block group that contains the given bytenr
  576. */
  577. struct btrfs_block_group_cache *btrfs_lookup_block_group(
  578. struct btrfs_fs_info *info,
  579. u64 bytenr)
  580. {
  581. struct btrfs_block_group_cache *cache;
  582. cache = block_group_cache_tree_search(info, bytenr, 1);
  583. return cache;
  584. }
  585. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  586. u64 flags)
  587. {
  588. struct list_head *head = &info->space_info;
  589. struct btrfs_space_info *found;
  590. flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
  591. rcu_read_lock();
  592. list_for_each_entry_rcu(found, head, list) {
  593. if (found->flags & flags) {
  594. rcu_read_unlock();
  595. return found;
  596. }
  597. }
  598. rcu_read_unlock();
  599. return NULL;
  600. }
  601. /*
  602. * after adding space to the filesystem, we need to clear the full flags
  603. * on all the space infos.
  604. */
  605. void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
  606. {
  607. struct list_head *head = &info->space_info;
  608. struct btrfs_space_info *found;
  609. rcu_read_lock();
  610. list_for_each_entry_rcu(found, head, list)
  611. found->full = 0;
  612. rcu_read_unlock();
  613. }
  614. /* simple helper to search for an existing extent at a given offset */
  615. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  616. {
  617. int ret;
  618. struct btrfs_key key;
  619. struct btrfs_path *path;
  620. path = btrfs_alloc_path();
  621. if (!path)
  622. return -ENOMEM;
  623. key.objectid = start;
  624. key.offset = len;
  625. key.type = BTRFS_EXTENT_ITEM_KEY;
  626. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  627. 0, 0);
  628. if (ret > 0) {
  629. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  630. if (key.objectid == start &&
  631. key.type == BTRFS_METADATA_ITEM_KEY)
  632. ret = 0;
  633. }
  634. btrfs_free_path(path);
  635. return ret;
  636. }
  637. /*
  638. * helper function to lookup reference count and flags of a tree block.
  639. *
  640. * the head node for delayed ref is used to store the sum of all the
  641. * reference count modifications queued up in the rbtree. the head
  642. * node may also store the extent flags to set. This way you can check
  643. * to see what the reference count and extent flags would be if all of
  644. * the delayed refs are not processed.
  645. */
  646. int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
  647. struct btrfs_root *root, u64 bytenr,
  648. u64 offset, int metadata, u64 *refs, u64 *flags)
  649. {
  650. struct btrfs_delayed_ref_head *head;
  651. struct btrfs_delayed_ref_root *delayed_refs;
  652. struct btrfs_path *path;
  653. struct btrfs_extent_item *ei;
  654. struct extent_buffer *leaf;
  655. struct btrfs_key key;
  656. u32 item_size;
  657. u64 num_refs;
  658. u64 extent_flags;
  659. int ret;
  660. /*
  661. * If we don't have skinny metadata, don't bother doing anything
  662. * different
  663. */
  664. if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
  665. offset = root->leafsize;
  666. metadata = 0;
  667. }
  668. path = btrfs_alloc_path();
  669. if (!path)
  670. return -ENOMEM;
  671. if (metadata) {
  672. key.objectid = bytenr;
  673. key.type = BTRFS_METADATA_ITEM_KEY;
  674. key.offset = offset;
  675. } else {
  676. key.objectid = bytenr;
  677. key.type = BTRFS_EXTENT_ITEM_KEY;
  678. key.offset = offset;
  679. }
  680. if (!trans) {
  681. path->skip_locking = 1;
  682. path->search_commit_root = 1;
  683. }
  684. again:
  685. ret = btrfs_search_slot(trans, root->fs_info->extent_root,
  686. &key, path, 0, 0);
  687. if (ret < 0)
  688. goto out_free;
  689. if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
  690. metadata = 0;
  691. if (path->slots[0]) {
  692. path->slots[0]--;
  693. btrfs_item_key_to_cpu(path->nodes[0], &key,
  694. path->slots[0]);
  695. if (key.objectid == bytenr &&
  696. key.type == BTRFS_EXTENT_ITEM_KEY &&
  697. key.offset == root->leafsize)
  698. ret = 0;
  699. }
  700. if (ret) {
  701. key.objectid = bytenr;
  702. key.type = BTRFS_EXTENT_ITEM_KEY;
  703. key.offset = root->leafsize;
  704. btrfs_release_path(path);
  705. goto again;
  706. }
  707. }
  708. if (ret == 0) {
  709. leaf = path->nodes[0];
  710. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  711. if (item_size >= sizeof(*ei)) {
  712. ei = btrfs_item_ptr(leaf, path->slots[0],
  713. struct btrfs_extent_item);
  714. num_refs = btrfs_extent_refs(leaf, ei);
  715. extent_flags = btrfs_extent_flags(leaf, ei);
  716. } else {
  717. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  718. struct btrfs_extent_item_v0 *ei0;
  719. BUG_ON(item_size != sizeof(*ei0));
  720. ei0 = btrfs_item_ptr(leaf, path->slots[0],
  721. struct btrfs_extent_item_v0);
  722. num_refs = btrfs_extent_refs_v0(leaf, ei0);
  723. /* FIXME: this isn't correct for data */
  724. extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  725. #else
  726. BUG();
  727. #endif
  728. }
  729. BUG_ON(num_refs == 0);
  730. } else {
  731. num_refs = 0;
  732. extent_flags = 0;
  733. ret = 0;
  734. }
  735. if (!trans)
  736. goto out;
  737. delayed_refs = &trans->transaction->delayed_refs;
  738. spin_lock(&delayed_refs->lock);
  739. head = btrfs_find_delayed_ref_head(trans, bytenr);
  740. if (head) {
  741. if (!mutex_trylock(&head->mutex)) {
  742. atomic_inc(&head->node.refs);
  743. spin_unlock(&delayed_refs->lock);
  744. btrfs_release_path(path);
  745. /*
  746. * Mutex was contended, block until it's released and try
  747. * again
  748. */
  749. mutex_lock(&head->mutex);
  750. mutex_unlock(&head->mutex);
  751. btrfs_put_delayed_ref(&head->node);
  752. goto again;
  753. }
  754. if (head->extent_op && head->extent_op->update_flags)
  755. extent_flags |= head->extent_op->flags_to_set;
  756. else
  757. BUG_ON(num_refs == 0);
  758. num_refs += head->node.ref_mod;
  759. mutex_unlock(&head->mutex);
  760. }
  761. spin_unlock(&delayed_refs->lock);
  762. out:
  763. WARN_ON(num_refs == 0);
  764. if (refs)
  765. *refs = num_refs;
  766. if (flags)
  767. *flags = extent_flags;
  768. out_free:
  769. btrfs_free_path(path);
  770. return ret;
  771. }
  772. /*
  773. * Back reference rules. Back refs have three main goals:
  774. *
  775. * 1) differentiate between all holders of references to an extent so that
  776. * when a reference is dropped we can make sure it was a valid reference
  777. * before freeing the extent.
  778. *
  779. * 2) Provide enough information to quickly find the holders of an extent
  780. * if we notice a given block is corrupted or bad.
  781. *
  782. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  783. * maintenance. This is actually the same as #2, but with a slightly
  784. * different use case.
  785. *
  786. * There are two kinds of back refs. The implicit back refs is optimized
  787. * for pointers in non-shared tree blocks. For a given pointer in a block,
  788. * back refs of this kind provide information about the block's owner tree
  789. * and the pointer's key. These information allow us to find the block by
  790. * b-tree searching. The full back refs is for pointers in tree blocks not
  791. * referenced by their owner trees. The location of tree block is recorded
  792. * in the back refs. Actually the full back refs is generic, and can be
  793. * used in all cases the implicit back refs is used. The major shortcoming
  794. * of the full back refs is its overhead. Every time a tree block gets
  795. * COWed, we have to update back refs entry for all pointers in it.
  796. *
  797. * For a newly allocated tree block, we use implicit back refs for
  798. * pointers in it. This means most tree related operations only involve
  799. * implicit back refs. For a tree block created in old transaction, the
  800. * only way to drop a reference to it is COW it. So we can detect the
  801. * event that tree block loses its owner tree's reference and do the
  802. * back refs conversion.
  803. *
  804. * When a tree block is COW'd through a tree, there are four cases:
  805. *
  806. * The reference count of the block is one and the tree is the block's
  807. * owner tree. Nothing to do in this case.
  808. *
  809. * The reference count of the block is one and the tree is not the
  810. * block's owner tree. In this case, full back refs is used for pointers
  811. * in the block. Remove these full back refs, add implicit back refs for
  812. * every pointers in the new block.
  813. *
  814. * The reference count of the block is greater than one and the tree is
  815. * the block's owner tree. In this case, implicit back refs is used for
  816. * pointers in the block. Add full back refs for every pointers in the
  817. * block, increase lower level extents' reference counts. The original
  818. * implicit back refs are entailed to the new block.
  819. *
  820. * The reference count of the block is greater than one and the tree is
  821. * not the block's owner tree. Add implicit back refs for every pointer in
  822. * the new block, increase lower level extents' reference count.
  823. *
  824. * Back Reference Key composing:
  825. *
  826. * The key objectid corresponds to the first byte in the extent,
  827. * The key type is used to differentiate between types of back refs.
  828. * There are different meanings of the key offset for different types
  829. * of back refs.
  830. *
  831. * File extents can be referenced by:
  832. *
  833. * - multiple snapshots, subvolumes, or different generations in one subvol
  834. * - different files inside a single subvolume
  835. * - different offsets inside a file (bookend extents in file.c)
  836. *
  837. * The extent ref structure for the implicit back refs has fields for:
  838. *
  839. * - Objectid of the subvolume root
  840. * - objectid of the file holding the reference
  841. * - original offset in the file
  842. * - how many bookend extents
  843. *
  844. * The key offset for the implicit back refs is hash of the first
  845. * three fields.
  846. *
  847. * The extent ref structure for the full back refs has field for:
  848. *
  849. * - number of pointers in the tree leaf
  850. *
  851. * The key offset for the implicit back refs is the first byte of
  852. * the tree leaf
  853. *
  854. * When a file extent is allocated, The implicit back refs is used.
  855. * the fields are filled in:
  856. *
  857. * (root_key.objectid, inode objectid, offset in file, 1)
  858. *
  859. * When a file extent is removed file truncation, we find the
  860. * corresponding implicit back refs and check the following fields:
  861. *
  862. * (btrfs_header_owner(leaf), inode objectid, offset in file)
  863. *
  864. * Btree extents can be referenced by:
  865. *
  866. * - Different subvolumes
  867. *
  868. * Both the implicit back refs and the full back refs for tree blocks
  869. * only consist of key. The key offset for the implicit back refs is
  870. * objectid of block's owner tree. The key offset for the full back refs
  871. * is the first byte of parent block.
  872. *
  873. * When implicit back refs is used, information about the lowest key and
  874. * level of the tree block are required. These information are stored in
  875. * tree block info structure.
  876. */
  877. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  878. static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
  879. struct btrfs_root *root,
  880. struct btrfs_path *path,
  881. u64 owner, u32 extra_size)
  882. {
  883. struct btrfs_extent_item *item;
  884. struct btrfs_extent_item_v0 *ei0;
  885. struct btrfs_extent_ref_v0 *ref0;
  886. struct btrfs_tree_block_info *bi;
  887. struct extent_buffer *leaf;
  888. struct btrfs_key key;
  889. struct btrfs_key found_key;
  890. u32 new_size = sizeof(*item);
  891. u64 refs;
  892. int ret;
  893. leaf = path->nodes[0];
  894. BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
  895. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  896. ei0 = btrfs_item_ptr(leaf, path->slots[0],
  897. struct btrfs_extent_item_v0);
  898. refs = btrfs_extent_refs_v0(leaf, ei0);
  899. if (owner == (u64)-1) {
  900. while (1) {
  901. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  902. ret = btrfs_next_leaf(root, path);
  903. if (ret < 0)
  904. return ret;
  905. BUG_ON(ret > 0); /* Corruption */
  906. leaf = path->nodes[0];
  907. }
  908. btrfs_item_key_to_cpu(leaf, &found_key,
  909. path->slots[0]);
  910. BUG_ON(key.objectid != found_key.objectid);
  911. if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
  912. path->slots[0]++;
  913. continue;
  914. }
  915. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  916. struct btrfs_extent_ref_v0);
  917. owner = btrfs_ref_objectid_v0(leaf, ref0);
  918. break;
  919. }
  920. }
  921. btrfs_release_path(path);
  922. if (owner < BTRFS_FIRST_FREE_OBJECTID)
  923. new_size += sizeof(*bi);
  924. new_size -= sizeof(*ei0);
  925. ret = btrfs_search_slot(trans, root, &key, path,
  926. new_size + extra_size, 1);
  927. if (ret < 0)
  928. return ret;
  929. BUG_ON(ret); /* Corruption */
  930. btrfs_extend_item(root, path, new_size);
  931. leaf = path->nodes[0];
  932. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  933. btrfs_set_extent_refs(leaf, item, refs);
  934. /* FIXME: get real generation */
  935. btrfs_set_extent_generation(leaf, item, 0);
  936. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  937. btrfs_set_extent_flags(leaf, item,
  938. BTRFS_EXTENT_FLAG_TREE_BLOCK |
  939. BTRFS_BLOCK_FLAG_FULL_BACKREF);
  940. bi = (struct btrfs_tree_block_info *)(item + 1);
  941. /* FIXME: get first key of the block */
  942. memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
  943. btrfs_set_tree_block_level(leaf, bi, (int)owner);
  944. } else {
  945. btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
  946. }
  947. btrfs_mark_buffer_dirty(leaf);
  948. return 0;
  949. }
  950. #endif
  951. static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
  952. {
  953. u32 high_crc = ~(u32)0;
  954. u32 low_crc = ~(u32)0;
  955. __le64 lenum;
  956. lenum = cpu_to_le64(root_objectid);
  957. high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
  958. lenum = cpu_to_le64(owner);
  959. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  960. lenum = cpu_to_le64(offset);
  961. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  962. return ((u64)high_crc << 31) ^ (u64)low_crc;
  963. }
  964. static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
  965. struct btrfs_extent_data_ref *ref)
  966. {
  967. return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
  968. btrfs_extent_data_ref_objectid(leaf, ref),
  969. btrfs_extent_data_ref_offset(leaf, ref));
  970. }
  971. static int match_extent_data_ref(struct extent_buffer *leaf,
  972. struct btrfs_extent_data_ref *ref,
  973. u64 root_objectid, u64 owner, u64 offset)
  974. {
  975. if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
  976. btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
  977. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  978. return 0;
  979. return 1;
  980. }
  981. static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
  982. struct btrfs_root *root,
  983. struct btrfs_path *path,
  984. u64 bytenr, u64 parent,
  985. u64 root_objectid,
  986. u64 owner, u64 offset)
  987. {
  988. struct btrfs_key key;
  989. struct btrfs_extent_data_ref *ref;
  990. struct extent_buffer *leaf;
  991. u32 nritems;
  992. int ret;
  993. int recow;
  994. int err = -ENOENT;
  995. key.objectid = bytenr;
  996. if (parent) {
  997. key.type = BTRFS_SHARED_DATA_REF_KEY;
  998. key.offset = parent;
  999. } else {
  1000. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  1001. key.offset = hash_extent_data_ref(root_objectid,
  1002. owner, offset);
  1003. }
  1004. again:
  1005. recow = 0;
  1006. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1007. if (ret < 0) {
  1008. err = ret;
  1009. goto fail;
  1010. }
  1011. if (parent) {
  1012. if (!ret)
  1013. return 0;
  1014. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1015. key.type = BTRFS_EXTENT_REF_V0_KEY;
  1016. btrfs_release_path(path);
  1017. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1018. if (ret < 0) {
  1019. err = ret;
  1020. goto fail;
  1021. }
  1022. if (!ret)
  1023. return 0;
  1024. #endif
  1025. goto fail;
  1026. }
  1027. leaf = path->nodes[0];
  1028. nritems = btrfs_header_nritems(leaf);
  1029. while (1) {
  1030. if (path->slots[0] >= nritems) {
  1031. ret = btrfs_next_leaf(root, path);
  1032. if (ret < 0)
  1033. err = ret;
  1034. if (ret)
  1035. goto fail;
  1036. leaf = path->nodes[0];
  1037. nritems = btrfs_header_nritems(leaf);
  1038. recow = 1;
  1039. }
  1040. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1041. if (key.objectid != bytenr ||
  1042. key.type != BTRFS_EXTENT_DATA_REF_KEY)
  1043. goto fail;
  1044. ref = btrfs_item_ptr(leaf, path->slots[0],
  1045. struct btrfs_extent_data_ref);
  1046. if (match_extent_data_ref(leaf, ref, root_objectid,
  1047. owner, offset)) {
  1048. if (recow) {
  1049. btrfs_release_path(path);
  1050. goto again;
  1051. }
  1052. err = 0;
  1053. break;
  1054. }
  1055. path->slots[0]++;
  1056. }
  1057. fail:
  1058. return err;
  1059. }
  1060. static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
  1061. struct btrfs_root *root,
  1062. struct btrfs_path *path,
  1063. u64 bytenr, u64 parent,
  1064. u64 root_objectid, u64 owner,
  1065. u64 offset, int refs_to_add)
  1066. {
  1067. struct btrfs_key key;
  1068. struct extent_buffer *leaf;
  1069. u32 size;
  1070. u32 num_refs;
  1071. int ret;
  1072. key.objectid = bytenr;
  1073. if (parent) {
  1074. key.type = BTRFS_SHARED_DATA_REF_KEY;
  1075. key.offset = parent;
  1076. size = sizeof(struct btrfs_shared_data_ref);
  1077. } else {
  1078. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  1079. key.offset = hash_extent_data_ref(root_objectid,
  1080. owner, offset);
  1081. size = sizeof(struct btrfs_extent_data_ref);
  1082. }
  1083. ret = btrfs_insert_empty_item(trans, root, path, &key, size);
  1084. if (ret && ret != -EEXIST)
  1085. goto fail;
  1086. leaf = path->nodes[0];
  1087. if (parent) {
  1088. struct btrfs_shared_data_ref *ref;
  1089. ref = btrfs_item_ptr(leaf, path->slots[0],
  1090. struct btrfs_shared_data_ref);
  1091. if (ret == 0) {
  1092. btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
  1093. } else {
  1094. num_refs = btrfs_shared_data_ref_count(leaf, ref);
  1095. num_refs += refs_to_add;
  1096. btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
  1097. }
  1098. } else {
  1099. struct btrfs_extent_data_ref *ref;
  1100. while (ret == -EEXIST) {
  1101. ref = btrfs_item_ptr(leaf, path->slots[0],
  1102. struct btrfs_extent_data_ref);
  1103. if (match_extent_data_ref(leaf, ref, root_objectid,
  1104. owner, offset))
  1105. break;
  1106. btrfs_release_path(path);
  1107. key.offset++;
  1108. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1109. size);
  1110. if (ret && ret != -EEXIST)
  1111. goto fail;
  1112. leaf = path->nodes[0];
  1113. }
  1114. ref = btrfs_item_ptr(leaf, path->slots[0],
  1115. struct btrfs_extent_data_ref);
  1116. if (ret == 0) {
  1117. btrfs_set_extent_data_ref_root(leaf, ref,
  1118. root_objectid);
  1119. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  1120. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  1121. btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
  1122. } else {
  1123. num_refs = btrfs_extent_data_ref_count(leaf, ref);
  1124. num_refs += refs_to_add;
  1125. btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
  1126. }
  1127. }
  1128. btrfs_mark_buffer_dirty(leaf);
  1129. ret = 0;
  1130. fail:
  1131. btrfs_release_path(path);
  1132. return ret;
  1133. }
  1134. static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
  1135. struct btrfs_root *root,
  1136. struct btrfs_path *path,
  1137. int refs_to_drop)
  1138. {
  1139. struct btrfs_key key;
  1140. struct btrfs_extent_data_ref *ref1 = NULL;
  1141. struct btrfs_shared_data_ref *ref2 = NULL;
  1142. struct extent_buffer *leaf;
  1143. u32 num_refs = 0;
  1144. int ret = 0;
  1145. leaf = path->nodes[0];
  1146. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1147. if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  1148. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  1149. struct btrfs_extent_data_ref);
  1150. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1151. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  1152. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  1153. struct btrfs_shared_data_ref);
  1154. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1155. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1156. } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
  1157. struct btrfs_extent_ref_v0 *ref0;
  1158. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1159. struct btrfs_extent_ref_v0);
  1160. num_refs = btrfs_ref_count_v0(leaf, ref0);
  1161. #endif
  1162. } else {
  1163. BUG();
  1164. }
  1165. BUG_ON(num_refs < refs_to_drop);
  1166. num_refs -= refs_to_drop;
  1167. if (num_refs == 0) {
  1168. ret = btrfs_del_item(trans, root, path);
  1169. } else {
  1170. if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
  1171. btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
  1172. else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
  1173. btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
  1174. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1175. else {
  1176. struct btrfs_extent_ref_v0 *ref0;
  1177. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1178. struct btrfs_extent_ref_v0);
  1179. btrfs_set_ref_count_v0(leaf, ref0, num_refs);
  1180. }
  1181. #endif
  1182. btrfs_mark_buffer_dirty(leaf);
  1183. }
  1184. return ret;
  1185. }
  1186. static noinline u32 extent_data_ref_count(struct btrfs_root *root,
  1187. struct btrfs_path *path,
  1188. struct btrfs_extent_inline_ref *iref)
  1189. {
  1190. struct btrfs_key key;
  1191. struct extent_buffer *leaf;
  1192. struct btrfs_extent_data_ref *ref1;
  1193. struct btrfs_shared_data_ref *ref2;
  1194. u32 num_refs = 0;
  1195. leaf = path->nodes[0];
  1196. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1197. if (iref) {
  1198. if (btrfs_extent_inline_ref_type(leaf, iref) ==
  1199. BTRFS_EXTENT_DATA_REF_KEY) {
  1200. ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
  1201. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1202. } else {
  1203. ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
  1204. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1205. }
  1206. } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  1207. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  1208. struct btrfs_extent_data_ref);
  1209. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1210. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  1211. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  1212. struct btrfs_shared_data_ref);
  1213. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1214. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1215. } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
  1216. struct btrfs_extent_ref_v0 *ref0;
  1217. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1218. struct btrfs_extent_ref_v0);
  1219. num_refs = btrfs_ref_count_v0(leaf, ref0);
  1220. #endif
  1221. } else {
  1222. WARN_ON(1);
  1223. }
  1224. return num_refs;
  1225. }
  1226. static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
  1227. struct btrfs_root *root,
  1228. struct btrfs_path *path,
  1229. u64 bytenr, u64 parent,
  1230. u64 root_objectid)
  1231. {
  1232. struct btrfs_key key;
  1233. int ret;
  1234. key.objectid = bytenr;
  1235. if (parent) {
  1236. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  1237. key.offset = parent;
  1238. } else {
  1239. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  1240. key.offset = root_objectid;
  1241. }
  1242. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1243. if (ret > 0)
  1244. ret = -ENOENT;
  1245. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1246. if (ret == -ENOENT && parent) {
  1247. btrfs_release_path(path);
  1248. key.type = BTRFS_EXTENT_REF_V0_KEY;
  1249. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1250. if (ret > 0)
  1251. ret = -ENOENT;
  1252. }
  1253. #endif
  1254. return ret;
  1255. }
  1256. static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
  1257. struct btrfs_root *root,
  1258. struct btrfs_path *path,
  1259. u64 bytenr, u64 parent,
  1260. u64 root_objectid)
  1261. {
  1262. struct btrfs_key key;
  1263. int ret;
  1264. key.objectid = bytenr;
  1265. if (parent) {
  1266. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  1267. key.offset = parent;
  1268. } else {
  1269. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  1270. key.offset = root_objectid;
  1271. }
  1272. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1273. btrfs_release_path(path);
  1274. return ret;
  1275. }
  1276. static inline int extent_ref_type(u64 parent, u64 owner)
  1277. {
  1278. int type;
  1279. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1280. if (parent > 0)
  1281. type = BTRFS_SHARED_BLOCK_REF_KEY;
  1282. else
  1283. type = BTRFS_TREE_BLOCK_REF_KEY;
  1284. } else {
  1285. if (parent > 0)
  1286. type = BTRFS_SHARED_DATA_REF_KEY;
  1287. else
  1288. type = BTRFS_EXTENT_DATA_REF_KEY;
  1289. }
  1290. return type;
  1291. }
  1292. static int find_next_key(struct btrfs_path *path, int level,
  1293. struct btrfs_key *key)
  1294. {
  1295. for (; level < BTRFS_MAX_LEVEL; level++) {
  1296. if (!path->nodes[level])
  1297. break;
  1298. if (path->slots[level] + 1 >=
  1299. btrfs_header_nritems(path->nodes[level]))
  1300. continue;
  1301. if (level == 0)
  1302. btrfs_item_key_to_cpu(path->nodes[level], key,
  1303. path->slots[level] + 1);
  1304. else
  1305. btrfs_node_key_to_cpu(path->nodes[level], key,
  1306. path->slots[level] + 1);
  1307. return 0;
  1308. }
  1309. return 1;
  1310. }
  1311. /*
  1312. * look for inline back ref. if back ref is found, *ref_ret is set
  1313. * to the address of inline back ref, and 0 is returned.
  1314. *
  1315. * if back ref isn't found, *ref_ret is set to the address where it
  1316. * should be inserted, and -ENOENT is returned.
  1317. *
  1318. * if insert is true and there are too many inline back refs, the path
  1319. * points to the extent item, and -EAGAIN is returned.
  1320. *
  1321. * NOTE: inline back refs are ordered in the same way that back ref
  1322. * items in the tree are ordered.
  1323. */
  1324. static noinline_for_stack
  1325. int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
  1326. struct btrfs_root *root,
  1327. struct btrfs_path *path,
  1328. struct btrfs_extent_inline_ref **ref_ret,
  1329. u64 bytenr, u64 num_bytes,
  1330. u64 parent, u64 root_objectid,
  1331. u64 owner, u64 offset, int insert)
  1332. {
  1333. struct btrfs_key key;
  1334. struct extent_buffer *leaf;
  1335. struct btrfs_extent_item *ei;
  1336. struct btrfs_extent_inline_ref *iref;
  1337. u64 flags;
  1338. u64 item_size;
  1339. unsigned long ptr;
  1340. unsigned long end;
  1341. int extra_size;
  1342. int type;
  1343. int want;
  1344. int ret;
  1345. int err = 0;
  1346. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  1347. SKINNY_METADATA);
  1348. key.objectid = bytenr;
  1349. key.type = BTRFS_EXTENT_ITEM_KEY;
  1350. key.offset = num_bytes;
  1351. want = extent_ref_type(parent, owner);
  1352. if (insert) {
  1353. extra_size = btrfs_extent_inline_ref_size(want);
  1354. path->keep_locks = 1;
  1355. } else
  1356. extra_size = -1;
  1357. /*
  1358. * Owner is our parent level, so we can just add one to get the level
  1359. * for the block we are interested in.
  1360. */
  1361. if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
  1362. key.type = BTRFS_METADATA_ITEM_KEY;
  1363. key.offset = owner;
  1364. }
  1365. again:
  1366. ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
  1367. if (ret < 0) {
  1368. err = ret;
  1369. goto out;
  1370. }
  1371. /*
  1372. * We may be a newly converted file system which still has the old fat
  1373. * extent entries for metadata, so try and see if we have one of those.
  1374. */
  1375. if (ret > 0 && skinny_metadata) {
  1376. skinny_metadata = false;
  1377. if (path->slots[0]) {
  1378. path->slots[0]--;
  1379. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1380. path->slots[0]);
  1381. if (key.objectid == bytenr &&
  1382. key.type == BTRFS_EXTENT_ITEM_KEY &&
  1383. key.offset == num_bytes)
  1384. ret = 0;
  1385. }
  1386. if (ret) {
  1387. key.type = BTRFS_EXTENT_ITEM_KEY;
  1388. key.offset = num_bytes;
  1389. btrfs_release_path(path);
  1390. goto again;
  1391. }
  1392. }
  1393. if (ret && !insert) {
  1394. err = -ENOENT;
  1395. goto out;
  1396. } else if (ret) {
  1397. err = -EIO;
  1398. WARN_ON(1);
  1399. goto out;
  1400. }
  1401. leaf = path->nodes[0];
  1402. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1403. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1404. if (item_size < sizeof(*ei)) {
  1405. if (!insert) {
  1406. err = -ENOENT;
  1407. goto out;
  1408. }
  1409. ret = convert_extent_item_v0(trans, root, path, owner,
  1410. extra_size);
  1411. if (ret < 0) {
  1412. err = ret;
  1413. goto out;
  1414. }
  1415. leaf = path->nodes[0];
  1416. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1417. }
  1418. #endif
  1419. BUG_ON(item_size < sizeof(*ei));
  1420. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1421. flags = btrfs_extent_flags(leaf, ei);
  1422. ptr = (unsigned long)(ei + 1);
  1423. end = (unsigned long)ei + item_size;
  1424. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
  1425. ptr += sizeof(struct btrfs_tree_block_info);
  1426. BUG_ON(ptr > end);
  1427. }
  1428. err = -ENOENT;
  1429. while (1) {
  1430. if (ptr >= end) {
  1431. WARN_ON(ptr > end);
  1432. break;
  1433. }
  1434. iref = (struct btrfs_extent_inline_ref *)ptr;
  1435. type = btrfs_extent_inline_ref_type(leaf, iref);
  1436. if (want < type)
  1437. break;
  1438. if (want > type) {
  1439. ptr += btrfs_extent_inline_ref_size(type);
  1440. continue;
  1441. }
  1442. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1443. struct btrfs_extent_data_ref *dref;
  1444. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1445. if (match_extent_data_ref(leaf, dref, root_objectid,
  1446. owner, offset)) {
  1447. err = 0;
  1448. break;
  1449. }
  1450. if (hash_extent_data_ref_item(leaf, dref) <
  1451. hash_extent_data_ref(root_objectid, owner, offset))
  1452. break;
  1453. } else {
  1454. u64 ref_offset;
  1455. ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
  1456. if (parent > 0) {
  1457. if (parent == ref_offset) {
  1458. err = 0;
  1459. break;
  1460. }
  1461. if (ref_offset < parent)
  1462. break;
  1463. } else {
  1464. if (root_objectid == ref_offset) {
  1465. err = 0;
  1466. break;
  1467. }
  1468. if (ref_offset < root_objectid)
  1469. break;
  1470. }
  1471. }
  1472. ptr += btrfs_extent_inline_ref_size(type);
  1473. }
  1474. if (err == -ENOENT && insert) {
  1475. if (item_size + extra_size >=
  1476. BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
  1477. err = -EAGAIN;
  1478. goto out;
  1479. }
  1480. /*
  1481. * To add new inline back ref, we have to make sure
  1482. * there is no corresponding back ref item.
  1483. * For simplicity, we just do not add new inline back
  1484. * ref if there is any kind of item for this block
  1485. */
  1486. if (find_next_key(path, 0, &key) == 0 &&
  1487. key.objectid == bytenr &&
  1488. key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
  1489. err = -EAGAIN;
  1490. goto out;
  1491. }
  1492. }
  1493. *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
  1494. out:
  1495. if (insert) {
  1496. path->keep_locks = 0;
  1497. btrfs_unlock_up_safe(path, 1);
  1498. }
  1499. return err;
  1500. }
  1501. /*
  1502. * helper to add new inline back ref
  1503. */
  1504. static noinline_for_stack
  1505. void setup_inline_extent_backref(struct btrfs_root *root,
  1506. struct btrfs_path *path,
  1507. struct btrfs_extent_inline_ref *iref,
  1508. u64 parent, u64 root_objectid,
  1509. u64 owner, u64 offset, int refs_to_add,
  1510. struct btrfs_delayed_extent_op *extent_op)
  1511. {
  1512. struct extent_buffer *leaf;
  1513. struct btrfs_extent_item *ei;
  1514. unsigned long ptr;
  1515. unsigned long end;
  1516. unsigned long item_offset;
  1517. u64 refs;
  1518. int size;
  1519. int type;
  1520. leaf = path->nodes[0];
  1521. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1522. item_offset = (unsigned long)iref - (unsigned long)ei;
  1523. type = extent_ref_type(parent, owner);
  1524. size = btrfs_extent_inline_ref_size(type);
  1525. btrfs_extend_item(root, path, size);
  1526. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1527. refs = btrfs_extent_refs(leaf, ei);
  1528. refs += refs_to_add;
  1529. btrfs_set_extent_refs(leaf, ei, refs);
  1530. if (extent_op)
  1531. __run_delayed_extent_op(extent_op, leaf, ei);
  1532. ptr = (unsigned long)ei + item_offset;
  1533. end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
  1534. if (ptr < end - size)
  1535. memmove_extent_buffer(leaf, ptr + size, ptr,
  1536. end - size - ptr);
  1537. iref = (struct btrfs_extent_inline_ref *)ptr;
  1538. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  1539. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1540. struct btrfs_extent_data_ref *dref;
  1541. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1542. btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
  1543. btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
  1544. btrfs_set_extent_data_ref_offset(leaf, dref, offset);
  1545. btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
  1546. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1547. struct btrfs_shared_data_ref *sref;
  1548. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1549. btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
  1550. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1551. } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1552. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1553. } else {
  1554. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  1555. }
  1556. btrfs_mark_buffer_dirty(leaf);
  1557. }
  1558. static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  1559. struct btrfs_root *root,
  1560. struct btrfs_path *path,
  1561. struct btrfs_extent_inline_ref **ref_ret,
  1562. u64 bytenr, u64 num_bytes, u64 parent,
  1563. u64 root_objectid, u64 owner, u64 offset)
  1564. {
  1565. int ret;
  1566. ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
  1567. bytenr, num_bytes, parent,
  1568. root_objectid, owner, offset, 0);
  1569. if (ret != -ENOENT)
  1570. return ret;
  1571. btrfs_release_path(path);
  1572. *ref_ret = NULL;
  1573. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1574. ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
  1575. root_objectid);
  1576. } else {
  1577. ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
  1578. root_objectid, owner, offset);
  1579. }
  1580. return ret;
  1581. }
  1582. /*
  1583. * helper to update/remove inline back ref
  1584. */
  1585. static noinline_for_stack
  1586. void update_inline_extent_backref(struct btrfs_root *root,
  1587. struct btrfs_path *path,
  1588. struct btrfs_extent_inline_ref *iref,
  1589. int refs_to_mod,
  1590. struct btrfs_delayed_extent_op *extent_op)
  1591. {
  1592. struct extent_buffer *leaf;
  1593. struct btrfs_extent_item *ei;
  1594. struct btrfs_extent_data_ref *dref = NULL;
  1595. struct btrfs_shared_data_ref *sref = NULL;
  1596. unsigned long ptr;
  1597. unsigned long end;
  1598. u32 item_size;
  1599. int size;
  1600. int type;
  1601. u64 refs;
  1602. leaf = path->nodes[0];
  1603. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1604. refs = btrfs_extent_refs(leaf, ei);
  1605. WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
  1606. refs += refs_to_mod;
  1607. btrfs_set_extent_refs(leaf, ei, refs);
  1608. if (extent_op)
  1609. __run_delayed_extent_op(extent_op, leaf, ei);
  1610. type = btrfs_extent_inline_ref_type(leaf, iref);
  1611. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1612. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1613. refs = btrfs_extent_data_ref_count(leaf, dref);
  1614. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1615. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1616. refs = btrfs_shared_data_ref_count(leaf, sref);
  1617. } else {
  1618. refs = 1;
  1619. BUG_ON(refs_to_mod != -1);
  1620. }
  1621. BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
  1622. refs += refs_to_mod;
  1623. if (refs > 0) {
  1624. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  1625. btrfs_set_extent_data_ref_count(leaf, dref, refs);
  1626. else
  1627. btrfs_set_shared_data_ref_count(leaf, sref, refs);
  1628. } else {
  1629. size = btrfs_extent_inline_ref_size(type);
  1630. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1631. ptr = (unsigned long)iref;
  1632. end = (unsigned long)ei + item_size;
  1633. if (ptr + size < end)
  1634. memmove_extent_buffer(leaf, ptr, ptr + size,
  1635. end - ptr - size);
  1636. item_size -= size;
  1637. btrfs_truncate_item(root, path, item_size, 1);
  1638. }
  1639. btrfs_mark_buffer_dirty(leaf);
  1640. }
  1641. static noinline_for_stack
  1642. int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
  1643. struct btrfs_root *root,
  1644. struct btrfs_path *path,
  1645. u64 bytenr, u64 num_bytes, u64 parent,
  1646. u64 root_objectid, u64 owner,
  1647. u64 offset, int refs_to_add,
  1648. struct btrfs_delayed_extent_op *extent_op)
  1649. {
  1650. struct btrfs_extent_inline_ref *iref;
  1651. int ret;
  1652. ret = lookup_inline_extent_backref(trans, root, path, &iref,
  1653. bytenr, num_bytes, parent,
  1654. root_objectid, owner, offset, 1);
  1655. if (ret == 0) {
  1656. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
  1657. update_inline_extent_backref(root, path, iref,
  1658. refs_to_add, extent_op);
  1659. } else if (ret == -ENOENT) {
  1660. setup_inline_extent_backref(root, path, iref, parent,
  1661. root_objectid, owner, offset,
  1662. refs_to_add, extent_op);
  1663. ret = 0;
  1664. }
  1665. return ret;
  1666. }
  1667. static int insert_extent_backref(struct btrfs_trans_handle *trans,
  1668. struct btrfs_root *root,
  1669. struct btrfs_path *path,
  1670. u64 bytenr, u64 parent, u64 root_objectid,
  1671. u64 owner, u64 offset, int refs_to_add)
  1672. {
  1673. int ret;
  1674. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1675. BUG_ON(refs_to_add != 1);
  1676. ret = insert_tree_block_ref(trans, root, path, bytenr,
  1677. parent, root_objectid);
  1678. } else {
  1679. ret = insert_extent_data_ref(trans, root, path, bytenr,
  1680. parent, root_objectid,
  1681. owner, offset, refs_to_add);
  1682. }
  1683. return ret;
  1684. }
  1685. static int remove_extent_backref(struct btrfs_trans_handle *trans,
  1686. struct btrfs_root *root,
  1687. struct btrfs_path *path,
  1688. struct btrfs_extent_inline_ref *iref,
  1689. int refs_to_drop, int is_data)
  1690. {
  1691. int ret = 0;
  1692. BUG_ON(!is_data && refs_to_drop != 1);
  1693. if (iref) {
  1694. update_inline_extent_backref(root, path, iref,
  1695. -refs_to_drop, NULL);
  1696. } else if (is_data) {
  1697. ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
  1698. } else {
  1699. ret = btrfs_del_item(trans, root, path);
  1700. }
  1701. return ret;
  1702. }
  1703. static int btrfs_issue_discard(struct block_device *bdev,
  1704. u64 start, u64 len)
  1705. {
  1706. return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
  1707. }
  1708. static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
  1709. u64 num_bytes, u64 *actual_bytes)
  1710. {
  1711. int ret;
  1712. u64 discarded_bytes = 0;
  1713. struct btrfs_bio *bbio = NULL;
  1714. /* Tell the block device(s) that the sectors can be discarded */
  1715. ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
  1716. bytenr, &num_bytes, &bbio, 0);
  1717. /* Error condition is -ENOMEM */
  1718. if (!ret) {
  1719. struct btrfs_bio_stripe *stripe = bbio->stripes;
  1720. int i;
  1721. for (i = 0; i < bbio->num_stripes; i++, stripe++) {
  1722. if (!stripe->dev->can_discard)
  1723. continue;
  1724. ret = btrfs_issue_discard(stripe->dev->bdev,
  1725. stripe->physical,
  1726. stripe->length);
  1727. if (!ret)
  1728. discarded_bytes += stripe->length;
  1729. else if (ret != -EOPNOTSUPP)
  1730. break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
  1731. /*
  1732. * Just in case we get back EOPNOTSUPP for some reason,
  1733. * just ignore the return value so we don't screw up
  1734. * people calling discard_extent.
  1735. */
  1736. ret = 0;
  1737. }
  1738. kfree(bbio);
  1739. }
  1740. if (actual_bytes)
  1741. *actual_bytes = discarded_bytes;
  1742. if (ret == -EOPNOTSUPP)
  1743. ret = 0;
  1744. return ret;
  1745. }
  1746. /* Can return -ENOMEM */
  1747. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1748. struct btrfs_root *root,
  1749. u64 bytenr, u64 num_bytes, u64 parent,
  1750. u64 root_objectid, u64 owner, u64 offset, int for_cow)
  1751. {
  1752. int ret;
  1753. struct btrfs_fs_info *fs_info = root->fs_info;
  1754. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
  1755. root_objectid == BTRFS_TREE_LOG_OBJECTID);
  1756. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1757. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  1758. num_bytes,
  1759. parent, root_objectid, (int)owner,
  1760. BTRFS_ADD_DELAYED_REF, NULL, for_cow);
  1761. } else {
  1762. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  1763. num_bytes,
  1764. parent, root_objectid, owner, offset,
  1765. BTRFS_ADD_DELAYED_REF, NULL, for_cow);
  1766. }
  1767. return ret;
  1768. }
  1769. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1770. struct btrfs_root *root,
  1771. u64 bytenr, u64 num_bytes,
  1772. u64 parent, u64 root_objectid,
  1773. u64 owner, u64 offset, int refs_to_add,
  1774. struct btrfs_delayed_extent_op *extent_op)
  1775. {
  1776. struct btrfs_path *path;
  1777. struct extent_buffer *leaf;
  1778. struct btrfs_extent_item *item;
  1779. u64 refs;
  1780. int ret;
  1781. int err = 0;
  1782. path = btrfs_alloc_path();
  1783. if (!path)
  1784. return -ENOMEM;
  1785. path->reada = 1;
  1786. path->leave_spinning = 1;
  1787. /* this will setup the path even if it fails to insert the back ref */
  1788. ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
  1789. path, bytenr, num_bytes, parent,
  1790. root_objectid, owner, offset,
  1791. refs_to_add, extent_op);
  1792. if (ret == 0)
  1793. goto out;
  1794. if (ret != -EAGAIN) {
  1795. err = ret;
  1796. goto out;
  1797. }
  1798. leaf = path->nodes[0];
  1799. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1800. refs = btrfs_extent_refs(leaf, item);
  1801. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  1802. if (extent_op)
  1803. __run_delayed_extent_op(extent_op, leaf, item);
  1804. btrfs_mark_buffer_dirty(leaf);
  1805. btrfs_release_path(path);
  1806. path->reada = 1;
  1807. path->leave_spinning = 1;
  1808. /* now insert the actual backref */
  1809. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1810. path, bytenr, parent, root_objectid,
  1811. owner, offset, refs_to_add);
  1812. if (ret)
  1813. btrfs_abort_transaction(trans, root, ret);
  1814. out:
  1815. btrfs_free_path(path);
  1816. return err;
  1817. }
  1818. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  1819. struct btrfs_root *root,
  1820. struct btrfs_delayed_ref_node *node,
  1821. struct btrfs_delayed_extent_op *extent_op,
  1822. int insert_reserved)
  1823. {
  1824. int ret = 0;
  1825. struct btrfs_delayed_data_ref *ref;
  1826. struct btrfs_key ins;
  1827. u64 parent = 0;
  1828. u64 ref_root = 0;
  1829. u64 flags = 0;
  1830. ins.objectid = node->bytenr;
  1831. ins.offset = node->num_bytes;
  1832. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1833. ref = btrfs_delayed_node_to_data_ref(node);
  1834. trace_run_delayed_data_ref(node, ref, node->action);
  1835. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1836. parent = ref->parent;
  1837. else
  1838. ref_root = ref->root;
  1839. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1840. if (extent_op)
  1841. flags |= extent_op->flags_to_set;
  1842. ret = alloc_reserved_file_extent(trans, root,
  1843. parent, ref_root, flags,
  1844. ref->objectid, ref->offset,
  1845. &ins, node->ref_mod);
  1846. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1847. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1848. node->num_bytes, parent,
  1849. ref_root, ref->objectid,
  1850. ref->offset, node->ref_mod,
  1851. extent_op);
  1852. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1853. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1854. node->num_bytes, parent,
  1855. ref_root, ref->objectid,
  1856. ref->offset, node->ref_mod,
  1857. extent_op);
  1858. } else {
  1859. BUG();
  1860. }
  1861. return ret;
  1862. }
  1863. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1864. struct extent_buffer *leaf,
  1865. struct btrfs_extent_item *ei)
  1866. {
  1867. u64 flags = btrfs_extent_flags(leaf, ei);
  1868. if (extent_op->update_flags) {
  1869. flags |= extent_op->flags_to_set;
  1870. btrfs_set_extent_flags(leaf, ei, flags);
  1871. }
  1872. if (extent_op->update_key) {
  1873. struct btrfs_tree_block_info *bi;
  1874. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1875. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1876. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1877. }
  1878. }
  1879. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1880. struct btrfs_root *root,
  1881. struct btrfs_delayed_ref_node *node,
  1882. struct btrfs_delayed_extent_op *extent_op)
  1883. {
  1884. struct btrfs_key key;
  1885. struct btrfs_path *path;
  1886. struct btrfs_extent_item *ei;
  1887. struct extent_buffer *leaf;
  1888. u32 item_size;
  1889. int ret;
  1890. int err = 0;
  1891. int metadata = !extent_op->is_data;
  1892. if (trans->aborted)
  1893. return 0;
  1894. if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  1895. metadata = 0;
  1896. path = btrfs_alloc_path();
  1897. if (!path)
  1898. return -ENOMEM;
  1899. key.objectid = node->bytenr;
  1900. if (metadata) {
  1901. key.type = BTRFS_METADATA_ITEM_KEY;
  1902. key.offset = extent_op->level;
  1903. } else {
  1904. key.type = BTRFS_EXTENT_ITEM_KEY;
  1905. key.offset = node->num_bytes;
  1906. }
  1907. again:
  1908. path->reada = 1;
  1909. path->leave_spinning = 1;
  1910. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  1911. path, 0, 1);
  1912. if (ret < 0) {
  1913. err = ret;
  1914. goto out;
  1915. }
  1916. if (ret > 0) {
  1917. if (metadata) {
  1918. btrfs_release_path(path);
  1919. metadata = 0;
  1920. key.offset = node->num_bytes;
  1921. key.type = BTRFS_EXTENT_ITEM_KEY;
  1922. goto again;
  1923. }
  1924. err = -EIO;
  1925. goto out;
  1926. }
  1927. leaf = path->nodes[0];
  1928. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1929. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1930. if (item_size < sizeof(*ei)) {
  1931. ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
  1932. path, (u64)-1, 0);
  1933. if (ret < 0) {
  1934. err = ret;
  1935. goto out;
  1936. }
  1937. leaf = path->nodes[0];
  1938. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1939. }
  1940. #endif
  1941. BUG_ON(item_size < sizeof(*ei));
  1942. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1943. __run_delayed_extent_op(extent_op, leaf, ei);
  1944. btrfs_mark_buffer_dirty(leaf);
  1945. out:
  1946. btrfs_free_path(path);
  1947. return err;
  1948. }
  1949. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1950. struct btrfs_root *root,
  1951. struct btrfs_delayed_ref_node *node,
  1952. struct btrfs_delayed_extent_op *extent_op,
  1953. int insert_reserved)
  1954. {
  1955. int ret = 0;
  1956. struct btrfs_delayed_tree_ref *ref;
  1957. struct btrfs_key ins;
  1958. u64 parent = 0;
  1959. u64 ref_root = 0;
  1960. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  1961. SKINNY_METADATA);
  1962. ref = btrfs_delayed_node_to_tree_ref(node);
  1963. trace_run_delayed_tree_ref(node, ref, node->action);
  1964. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1965. parent = ref->parent;
  1966. else
  1967. ref_root = ref->root;
  1968. ins.objectid = node->bytenr;
  1969. if (skinny_metadata) {
  1970. ins.offset = ref->level;
  1971. ins.type = BTRFS_METADATA_ITEM_KEY;
  1972. } else {
  1973. ins.offset = node->num_bytes;
  1974. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1975. }
  1976. BUG_ON(node->ref_mod != 1);
  1977. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1978. BUG_ON(!extent_op || !extent_op->update_flags);
  1979. ret = alloc_reserved_tree_block(trans, root,
  1980. parent, ref_root,
  1981. extent_op->flags_to_set,
  1982. &extent_op->key,
  1983. ref->level, &ins);
  1984. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1985. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1986. node->num_bytes, parent, ref_root,
  1987. ref->level, 0, 1, extent_op);
  1988. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1989. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1990. node->num_bytes, parent, ref_root,
  1991. ref->level, 0, 1, extent_op);
  1992. } else {
  1993. BUG();
  1994. }
  1995. return ret;
  1996. }
  1997. /* helper function to actually process a single delayed ref entry */
  1998. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  1999. struct btrfs_root *root,
  2000. struct btrfs_delayed_ref_node *node,
  2001. struct btrfs_delayed_extent_op *extent_op,
  2002. int insert_reserved)
  2003. {
  2004. int ret = 0;
  2005. if (trans->aborted)
  2006. return 0;
  2007. if (btrfs_delayed_ref_is_head(node)) {
  2008. struct btrfs_delayed_ref_head *head;
  2009. /*
  2010. * we've hit the end of the chain and we were supposed
  2011. * to insert this extent into the tree. But, it got
  2012. * deleted before we ever needed to insert it, so all
  2013. * we have to do is clean up the accounting
  2014. */
  2015. BUG_ON(extent_op);
  2016. head = btrfs_delayed_node_to_head(node);
  2017. trace_run_delayed_ref_head(node, head, node->action);
  2018. if (insert_reserved) {
  2019. btrfs_pin_extent(root, node->bytenr,
  2020. node->num_bytes, 1);
  2021. if (head->is_data) {
  2022. ret = btrfs_del_csums(trans, root,
  2023. node->bytenr,
  2024. node->num_bytes);
  2025. }
  2026. }
  2027. return ret;
  2028. }
  2029. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  2030. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  2031. ret = run_delayed_tree_ref(trans, root, node, extent_op,
  2032. insert_reserved);
  2033. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  2034. node->type == BTRFS_SHARED_DATA_REF_KEY)
  2035. ret = run_delayed_data_ref(trans, root, node, extent_op,
  2036. insert_reserved);
  2037. else
  2038. BUG();
  2039. return ret;
  2040. }
  2041. static noinline struct btrfs_delayed_ref_node *
  2042. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  2043. {
  2044. struct rb_node *node;
  2045. struct btrfs_delayed_ref_node *ref;
  2046. int action = BTRFS_ADD_DELAYED_REF;
  2047. again:
  2048. /*
  2049. * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
  2050. * this prevents ref count from going down to zero when
  2051. * there still are pending delayed ref.
  2052. */
  2053. node = rb_prev(&head->node.rb_node);
  2054. while (1) {
  2055. if (!node)
  2056. break;
  2057. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2058. rb_node);
  2059. if (ref->bytenr != head->node.bytenr)
  2060. break;
  2061. if (ref->action == action)
  2062. return ref;
  2063. node = rb_prev(node);
  2064. }
  2065. if (action == BTRFS_ADD_DELAYED_REF) {
  2066. action = BTRFS_DROP_DELAYED_REF;
  2067. goto again;
  2068. }
  2069. return NULL;
  2070. }
  2071. /*
  2072. * Returns 0 on success or if called with an already aborted transaction.
  2073. * Returns -ENOMEM or -EIO on failure and will abort the transaction.
  2074. */
  2075. static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
  2076. struct btrfs_root *root,
  2077. struct list_head *cluster)
  2078. {
  2079. struct btrfs_delayed_ref_root *delayed_refs;
  2080. struct btrfs_delayed_ref_node *ref;
  2081. struct btrfs_delayed_ref_head *locked_ref = NULL;
  2082. struct btrfs_delayed_extent_op *extent_op;
  2083. struct btrfs_fs_info *fs_info = root->fs_info;
  2084. int ret;
  2085. int count = 0;
  2086. int must_insert_reserved = 0;
  2087. delayed_refs = &trans->transaction->delayed_refs;
  2088. while (1) {
  2089. if (!locked_ref) {
  2090. /* pick a new head ref from the cluster list */
  2091. if (list_empty(cluster))
  2092. break;
  2093. locked_ref = list_entry(cluster->next,
  2094. struct btrfs_delayed_ref_head, cluster);
  2095. /* grab the lock that says we are going to process
  2096. * all the refs for this head */
  2097. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  2098. /*
  2099. * we may have dropped the spin lock to get the head
  2100. * mutex lock, and that might have given someone else
  2101. * time to free the head. If that's true, it has been
  2102. * removed from our list and we can move on.
  2103. */
  2104. if (ret == -EAGAIN) {
  2105. locked_ref = NULL;
  2106. count++;
  2107. continue;
  2108. }
  2109. }
  2110. /*
  2111. * We need to try and merge add/drops of the same ref since we
  2112. * can run into issues with relocate dropping the implicit ref
  2113. * and then it being added back again before the drop can
  2114. * finish. If we merged anything we need to re-loop so we can
  2115. * get a good ref.
  2116. */
  2117. btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
  2118. locked_ref);
  2119. /*
  2120. * locked_ref is the head node, so we have to go one
  2121. * node back for any delayed ref updates
  2122. */
  2123. ref = select_delayed_ref(locked_ref);
  2124. if (ref && ref->seq &&
  2125. btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
  2126. /*
  2127. * there are still refs with lower seq numbers in the
  2128. * process of being added. Don't run this ref yet.
  2129. */
  2130. list_del_init(&locked_ref->cluster);
  2131. btrfs_delayed_ref_unlock(locked_ref);
  2132. locked_ref = NULL;
  2133. delayed_refs->num_heads_ready++;
  2134. spin_unlock(&delayed_refs->lock);
  2135. cond_resched();
  2136. spin_lock(&delayed_refs->lock);
  2137. continue;
  2138. }
  2139. /*
  2140. * record the must insert reserved flag before we
  2141. * drop the spin lock.
  2142. */
  2143. must_insert_reserved = locked_ref->must_insert_reserved;
  2144. locked_ref->must_insert_reserved = 0;
  2145. extent_op = locked_ref->extent_op;
  2146. locked_ref->extent_op = NULL;
  2147. if (!ref) {
  2148. /* All delayed refs have been processed, Go ahead
  2149. * and send the head node to run_one_delayed_ref,
  2150. * so that any accounting fixes can happen
  2151. */
  2152. ref = &locked_ref->node;
  2153. if (extent_op && must_insert_reserved) {
  2154. btrfs_free_delayed_extent_op(extent_op);
  2155. extent_op = NULL;
  2156. }
  2157. if (extent_op) {
  2158. spin_unlock(&delayed_refs->lock);
  2159. ret = run_delayed_extent_op(trans, root,
  2160. ref, extent_op);
  2161. btrfs_free_delayed_extent_op(extent_op);
  2162. if (ret) {
  2163. btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
  2164. spin_lock(&delayed_refs->lock);
  2165. btrfs_delayed_ref_unlock(locked_ref);
  2166. return ret;
  2167. }
  2168. goto next;
  2169. }
  2170. }
  2171. ref->in_tree = 0;
  2172. rb_erase(&ref->rb_node, &delayed_refs->root);
  2173. delayed_refs->num_entries--;
  2174. if (!btrfs_delayed_ref_is_head(ref)) {
  2175. /*
  2176. * when we play the delayed ref, also correct the
  2177. * ref_mod on head
  2178. */
  2179. switch (ref->action) {
  2180. case BTRFS_ADD_DELAYED_REF:
  2181. case BTRFS_ADD_DELAYED_EXTENT:
  2182. locked_ref->node.ref_mod -= ref->ref_mod;
  2183. break;
  2184. case BTRFS_DROP_DELAYED_REF:
  2185. locked_ref->node.ref_mod += ref->ref_mod;
  2186. break;
  2187. default:
  2188. WARN_ON(1);
  2189. }
  2190. } else {
  2191. list_del_init(&locked_ref->cluster);
  2192. }
  2193. spin_unlock(&delayed_refs->lock);
  2194. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2195. must_insert_reserved);
  2196. btrfs_free_delayed_extent_op(extent_op);
  2197. if (ret) {
  2198. btrfs_delayed_ref_unlock(locked_ref);
  2199. btrfs_put_delayed_ref(ref);
  2200. btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
  2201. spin_lock(&delayed_refs->lock);
  2202. return ret;
  2203. }
  2204. /*
  2205. * If this node is a head, that means all the refs in this head
  2206. * have been dealt with, and we will pick the next head to deal
  2207. * with, so we must unlock the head and drop it from the cluster
  2208. * list before we release it.
  2209. */
  2210. if (btrfs_delayed_ref_is_head(ref)) {
  2211. btrfs_delayed_ref_unlock(locked_ref);
  2212. locked_ref = NULL;
  2213. }
  2214. btrfs_put_delayed_ref(ref);
  2215. count++;
  2216. next:
  2217. cond_resched();
  2218. spin_lock(&delayed_refs->lock);
  2219. }
  2220. return count;
  2221. }
  2222. #ifdef SCRAMBLE_DELAYED_REFS
  2223. /*
  2224. * Normally delayed refs get processed in ascending bytenr order. This
  2225. * correlates in most cases to the order added. To expose dependencies on this
  2226. * order, we start to process the tree in the middle instead of the beginning
  2227. */
  2228. static u64 find_middle(struct rb_root *root)
  2229. {
  2230. struct rb_node *n = root->rb_node;
  2231. struct btrfs_delayed_ref_node *entry;
  2232. int alt = 1;
  2233. u64 middle;
  2234. u64 first = 0, last = 0;
  2235. n = rb_first(root);
  2236. if (n) {
  2237. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2238. first = entry->bytenr;
  2239. }
  2240. n = rb_last(root);
  2241. if (n) {
  2242. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2243. last = entry->bytenr;
  2244. }
  2245. n = root->rb_node;
  2246. while (n) {
  2247. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2248. WARN_ON(!entry->in_tree);
  2249. middle = entry->bytenr;
  2250. if (alt)
  2251. n = n->rb_left;
  2252. else
  2253. n = n->rb_right;
  2254. alt = 1 - alt;
  2255. }
  2256. return middle;
  2257. }
  2258. #endif
  2259. int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
  2260. struct btrfs_fs_info *fs_info)
  2261. {
  2262. struct qgroup_update *qgroup_update;
  2263. int ret = 0;
  2264. if (list_empty(&trans->qgroup_ref_list) !=
  2265. !trans->delayed_ref_elem.seq) {
  2266. /* list without seq or seq without list */
  2267. btrfs_err(fs_info,
  2268. "qgroup accounting update error, list is%s empty, seq is %#x.%x",
  2269. list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2270. (u32)(trans->delayed_ref_elem.seq >> 32),
  2271. (u32)trans->delayed_ref_elem.seq);
  2272. BUG();
  2273. }
  2274. if (!trans->delayed_ref_elem.seq)
  2275. return 0;
  2276. while (!list_empty(&trans->qgroup_ref_list)) {
  2277. qgroup_update = list_first_entry(&trans->qgroup_ref_list,
  2278. struct qgroup_update, list);
  2279. list_del(&qgroup_update->list);
  2280. if (!ret)
  2281. ret = btrfs_qgroup_account_ref(
  2282. trans, fs_info, qgroup_update->node,
  2283. qgroup_update->extent_op);
  2284. kfree(qgroup_update);
  2285. }
  2286. btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  2287. return ret;
  2288. }
  2289. static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
  2290. int count)
  2291. {
  2292. int val = atomic_read(&delayed_refs->ref_seq);
  2293. if (val < seq || val >= seq + count)
  2294. return 1;
  2295. return 0;
  2296. }
  2297. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2298. {
  2299. u64 num_bytes;
  2300. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2301. sizeof(struct btrfs_extent_inline_ref));
  2302. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2303. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2304. /*
  2305. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2306. * closer to what we're really going to want to ouse.
  2307. */
  2308. return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2309. }
  2310. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2311. struct btrfs_root *root)
  2312. {
  2313. struct btrfs_block_rsv *global_rsv;
  2314. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2315. u64 num_bytes;
  2316. int ret = 0;
  2317. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2318. num_heads = heads_to_leaves(root, num_heads);
  2319. if (num_heads > 1)
  2320. num_bytes += (num_heads - 1) * root->leafsize;
  2321. num_bytes <<= 1;
  2322. global_rsv = &root->fs_info->global_block_rsv;
  2323. /*
  2324. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2325. * wiggle room since running delayed refs can create more delayed refs.
  2326. */
  2327. if (global_rsv->space_info->full)
  2328. num_bytes <<= 1;
  2329. spin_lock(&global_rsv->lock);
  2330. if (global_rsv->reserved <= num_bytes)
  2331. ret = 1;
  2332. spin_unlock(&global_rsv->lock);
  2333. return ret;
  2334. }
  2335. /*
  2336. * this starts processing the delayed reference count updates and
  2337. * extent insertions we have queued up so far. count can be
  2338. * 0, which means to process everything in the tree at the start
  2339. * of the run (but not newly added entries), or it can be some target
  2340. * number you'd like to process.
  2341. *
  2342. * Returns 0 on success or if called with an aborted transaction
  2343. * Returns <0 on error and aborts the transaction
  2344. */
  2345. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2346. struct btrfs_root *root, unsigned long count)
  2347. {
  2348. struct rb_node *node;
  2349. struct btrfs_delayed_ref_root *delayed_refs;
  2350. struct btrfs_delayed_ref_node *ref;
  2351. struct list_head cluster;
  2352. int ret;
  2353. u64 delayed_start;
  2354. int run_all = count == (unsigned long)-1;
  2355. int run_most = 0;
  2356. int loops;
  2357. /* We'll clean this up in btrfs_cleanup_transaction */
  2358. if (trans->aborted)
  2359. return 0;
  2360. if (root == root->fs_info->extent_root)
  2361. root = root->fs_info->tree_root;
  2362. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  2363. delayed_refs = &trans->transaction->delayed_refs;
  2364. INIT_LIST_HEAD(&cluster);
  2365. if (count == 0) {
  2366. count = delayed_refs->num_entries * 2;
  2367. run_most = 1;
  2368. }
  2369. if (!run_all && !run_most) {
  2370. int old;
  2371. int seq = atomic_read(&delayed_refs->ref_seq);
  2372. progress:
  2373. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2374. if (old) {
  2375. DEFINE_WAIT(__wait);
  2376. if (delayed_refs->flushing ||
  2377. !btrfs_should_throttle_delayed_refs(trans, root))
  2378. return 0;
  2379. prepare_to_wait(&delayed_refs->wait, &__wait,
  2380. TASK_UNINTERRUPTIBLE);
  2381. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2382. if (old) {
  2383. schedule();
  2384. finish_wait(&delayed_refs->wait, &__wait);
  2385. if (!refs_newer(delayed_refs, seq, 256))
  2386. goto progress;
  2387. else
  2388. return 0;
  2389. } else {
  2390. finish_wait(&delayed_refs->wait, &__wait);
  2391. goto again;
  2392. }
  2393. }
  2394. } else {
  2395. atomic_inc(&delayed_refs->procs_running_refs);
  2396. }
  2397. again:
  2398. loops = 0;
  2399. spin_lock(&delayed_refs->lock);
  2400. #ifdef SCRAMBLE_DELAYED_REFS
  2401. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2402. #endif
  2403. while (1) {
  2404. if (!(run_all || run_most) &&
  2405. !btrfs_should_throttle_delayed_refs(trans, root))
  2406. break;
  2407. /*
  2408. * go find something we can process in the rbtree. We start at
  2409. * the beginning of the tree, and then build a cluster
  2410. * of refs to process starting at the first one we are able to
  2411. * lock
  2412. */
  2413. delayed_start = delayed_refs->run_delayed_start;
  2414. ret = btrfs_find_ref_cluster(trans, &cluster,
  2415. delayed_refs->run_delayed_start);
  2416. if (ret)
  2417. break;
  2418. ret = run_clustered_refs(trans, root, &cluster);
  2419. if (ret < 0) {
  2420. btrfs_release_ref_cluster(&cluster);
  2421. spin_unlock(&delayed_refs->lock);
  2422. btrfs_abort_transaction(trans, root, ret);
  2423. atomic_dec(&delayed_refs->procs_running_refs);
  2424. wake_up(&delayed_refs->wait);
  2425. return ret;
  2426. }
  2427. atomic_add(ret, &delayed_refs->ref_seq);
  2428. count -= min_t(unsigned long, ret, count);
  2429. if (count == 0)
  2430. break;
  2431. if (delayed_start >= delayed_refs->run_delayed_start) {
  2432. if (loops == 0) {
  2433. /*
  2434. * btrfs_find_ref_cluster looped. let's do one
  2435. * more cycle. if we don't run any delayed ref
  2436. * during that cycle (because we can't because
  2437. * all of them are blocked), bail out.
  2438. */
  2439. loops = 1;
  2440. } else {
  2441. /*
  2442. * no runnable refs left, stop trying
  2443. */
  2444. BUG_ON(run_all);
  2445. break;
  2446. }
  2447. }
  2448. if (ret) {
  2449. /* refs were run, let's reset staleness detection */
  2450. loops = 0;
  2451. }
  2452. }
  2453. if (run_all) {
  2454. if (!list_empty(&trans->new_bgs)) {
  2455. spin_unlock(&delayed_refs->lock);
  2456. btrfs_create_pending_block_groups(trans, root);
  2457. spin_lock(&delayed_refs->lock);
  2458. }
  2459. node = rb_first(&delayed_refs->root);
  2460. if (!node)
  2461. goto out;
  2462. count = (unsigned long)-1;
  2463. while (node) {
  2464. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2465. rb_node);
  2466. if (btrfs_delayed_ref_is_head(ref)) {
  2467. struct btrfs_delayed_ref_head *head;
  2468. head = btrfs_delayed_node_to_head(ref);
  2469. atomic_inc(&ref->refs);
  2470. spin_unlock(&delayed_refs->lock);
  2471. /*
  2472. * Mutex was contended, block until it's
  2473. * released and try again
  2474. */
  2475. mutex_lock(&head->mutex);
  2476. mutex_unlock(&head->mutex);
  2477. btrfs_put_delayed_ref(ref);
  2478. cond_resched();
  2479. goto again;
  2480. }
  2481. node = rb_next(node);
  2482. }
  2483. spin_unlock(&delayed_refs->lock);
  2484. schedule_timeout(1);
  2485. goto again;
  2486. }
  2487. out:
  2488. atomic_dec(&delayed_refs->procs_running_refs);
  2489. smp_mb();
  2490. if (waitqueue_active(&delayed_refs->wait))
  2491. wake_up(&delayed_refs->wait);
  2492. spin_unlock(&delayed_refs->lock);
  2493. assert_qgroups_uptodate(trans);
  2494. return 0;
  2495. }
  2496. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2497. struct btrfs_root *root,
  2498. u64 bytenr, u64 num_bytes, u64 flags,
  2499. int level, int is_data)
  2500. {
  2501. struct btrfs_delayed_extent_op *extent_op;
  2502. int ret;
  2503. extent_op = btrfs_alloc_delayed_extent_op();
  2504. if (!extent_op)
  2505. return -ENOMEM;
  2506. extent_op->flags_to_set = flags;
  2507. extent_op->update_flags = 1;
  2508. extent_op->update_key = 0;
  2509. extent_op->is_data = is_data ? 1 : 0;
  2510. extent_op->level = level;
  2511. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2512. num_bytes, extent_op);
  2513. if (ret)
  2514. btrfs_free_delayed_extent_op(extent_op);
  2515. return ret;
  2516. }
  2517. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2518. struct btrfs_root *root,
  2519. struct btrfs_path *path,
  2520. u64 objectid, u64 offset, u64 bytenr)
  2521. {
  2522. struct btrfs_delayed_ref_head *head;
  2523. struct btrfs_delayed_ref_node *ref;
  2524. struct btrfs_delayed_data_ref *data_ref;
  2525. struct btrfs_delayed_ref_root *delayed_refs;
  2526. struct rb_node *node;
  2527. int ret = 0;
  2528. ret = -ENOENT;
  2529. delayed_refs = &trans->transaction->delayed_refs;
  2530. spin_lock(&delayed_refs->lock);
  2531. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2532. if (!head)
  2533. goto out;
  2534. if (!mutex_trylock(&head->mutex)) {
  2535. atomic_inc(&head->node.refs);
  2536. spin_unlock(&delayed_refs->lock);
  2537. btrfs_release_path(path);
  2538. /*
  2539. * Mutex was contended, block until it's released and let
  2540. * caller try again
  2541. */
  2542. mutex_lock(&head->mutex);
  2543. mutex_unlock(&head->mutex);
  2544. btrfs_put_delayed_ref(&head->node);
  2545. return -EAGAIN;
  2546. }
  2547. node = rb_prev(&head->node.rb_node);
  2548. if (!node)
  2549. goto out_unlock;
  2550. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2551. if (ref->bytenr != bytenr)
  2552. goto out_unlock;
  2553. ret = 1;
  2554. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2555. goto out_unlock;
  2556. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2557. node = rb_prev(node);
  2558. if (node) {
  2559. int seq = ref->seq;
  2560. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2561. if (ref->bytenr == bytenr && ref->seq == seq)
  2562. goto out_unlock;
  2563. }
  2564. if (data_ref->root != root->root_key.objectid ||
  2565. data_ref->objectid != objectid || data_ref->offset != offset)
  2566. goto out_unlock;
  2567. ret = 0;
  2568. out_unlock:
  2569. mutex_unlock(&head->mutex);
  2570. out:
  2571. spin_unlock(&delayed_refs->lock);
  2572. return ret;
  2573. }
  2574. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2575. struct btrfs_root *root,
  2576. struct btrfs_path *path,
  2577. u64 objectid, u64 offset, u64 bytenr)
  2578. {
  2579. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2580. struct extent_buffer *leaf;
  2581. struct btrfs_extent_data_ref *ref;
  2582. struct btrfs_extent_inline_ref *iref;
  2583. struct btrfs_extent_item *ei;
  2584. struct btrfs_key key;
  2585. u32 item_size;
  2586. int ret;
  2587. key.objectid = bytenr;
  2588. key.offset = (u64)-1;
  2589. key.type = BTRFS_EXTENT_ITEM_KEY;
  2590. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2591. if (ret < 0)
  2592. goto out;
  2593. BUG_ON(ret == 0); /* Corruption */
  2594. ret = -ENOENT;
  2595. if (path->slots[0] == 0)
  2596. goto out;
  2597. path->slots[0]--;
  2598. leaf = path->nodes[0];
  2599. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2600. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2601. goto out;
  2602. ret = 1;
  2603. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2604. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2605. if (item_size < sizeof(*ei)) {
  2606. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2607. goto out;
  2608. }
  2609. #endif
  2610. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2611. if (item_size != sizeof(*ei) +
  2612. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2613. goto out;
  2614. if (btrfs_extent_generation(leaf, ei) <=
  2615. btrfs_root_last_snapshot(&root->root_item))
  2616. goto out;
  2617. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2618. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2619. BTRFS_EXTENT_DATA_REF_KEY)
  2620. goto out;
  2621. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2622. if (btrfs_extent_refs(leaf, ei) !=
  2623. btrfs_extent_data_ref_count(leaf, ref) ||
  2624. btrfs_extent_data_ref_root(leaf, ref) !=
  2625. root->root_key.objectid ||
  2626. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2627. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2628. goto out;
  2629. ret = 0;
  2630. out:
  2631. return ret;
  2632. }
  2633. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2634. struct btrfs_root *root,
  2635. u64 objectid, u64 offset, u64 bytenr)
  2636. {
  2637. struct btrfs_path *path;
  2638. int ret;
  2639. int ret2;
  2640. path = btrfs_alloc_path();
  2641. if (!path)
  2642. return -ENOENT;
  2643. do {
  2644. ret = check_committed_ref(trans, root, path, objectid,
  2645. offset, bytenr);
  2646. if (ret && ret != -ENOENT)
  2647. goto out;
  2648. ret2 = check_delayed_ref(trans, root, path, objectid,
  2649. offset, bytenr);
  2650. } while (ret2 == -EAGAIN);
  2651. if (ret2 && ret2 != -ENOENT) {
  2652. ret = ret2;
  2653. goto out;
  2654. }
  2655. if (ret != -ENOENT || ret2 != -ENOENT)
  2656. ret = 0;
  2657. out:
  2658. btrfs_free_path(path);
  2659. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2660. WARN_ON(ret > 0);
  2661. return ret;
  2662. }
  2663. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2664. struct btrfs_root *root,
  2665. struct extent_buffer *buf,
  2666. int full_backref, int inc, int for_cow)
  2667. {
  2668. u64 bytenr;
  2669. u64 num_bytes;
  2670. u64 parent;
  2671. u64 ref_root;
  2672. u32 nritems;
  2673. struct btrfs_key key;
  2674. struct btrfs_file_extent_item *fi;
  2675. int i;
  2676. int level;
  2677. int ret = 0;
  2678. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2679. u64, u64, u64, u64, u64, u64, int);
  2680. ref_root = btrfs_header_owner(buf);
  2681. nritems = btrfs_header_nritems(buf);
  2682. level = btrfs_header_level(buf);
  2683. if (!root->ref_cows && level == 0)
  2684. return 0;
  2685. if (inc)
  2686. process_func = btrfs_inc_extent_ref;
  2687. else
  2688. process_func = btrfs_free_extent;
  2689. if (full_backref)
  2690. parent = buf->start;
  2691. else
  2692. parent = 0;
  2693. for (i = 0; i < nritems; i++) {
  2694. if (level == 0) {
  2695. btrfs_item_key_to_cpu(buf, &key, i);
  2696. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2697. continue;
  2698. fi = btrfs_item_ptr(buf, i,
  2699. struct btrfs_file_extent_item);
  2700. if (btrfs_file_extent_type(buf, fi) ==
  2701. BTRFS_FILE_EXTENT_INLINE)
  2702. continue;
  2703. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2704. if (bytenr == 0)
  2705. continue;
  2706. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2707. key.offset -= btrfs_file_extent_offset(buf, fi);
  2708. ret = process_func(trans, root, bytenr, num_bytes,
  2709. parent, ref_root, key.objectid,
  2710. key.offset, for_cow);
  2711. if (ret)
  2712. goto fail;
  2713. } else {
  2714. bytenr = btrfs_node_blockptr(buf, i);
  2715. num_bytes = btrfs_level_size(root, level - 1);
  2716. ret = process_func(trans, root, bytenr, num_bytes,
  2717. parent, ref_root, level - 1, 0,
  2718. for_cow);
  2719. if (ret)
  2720. goto fail;
  2721. }
  2722. }
  2723. return 0;
  2724. fail:
  2725. return ret;
  2726. }
  2727. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2728. struct extent_buffer *buf, int full_backref, int for_cow)
  2729. {
  2730. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2731. }
  2732. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2733. struct extent_buffer *buf, int full_backref, int for_cow)
  2734. {
  2735. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2736. }
  2737. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2738. struct btrfs_root *root,
  2739. struct btrfs_path *path,
  2740. struct btrfs_block_group_cache *cache)
  2741. {
  2742. int ret;
  2743. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2744. unsigned long bi;
  2745. struct extent_buffer *leaf;
  2746. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2747. if (ret < 0)
  2748. goto fail;
  2749. BUG_ON(ret); /* Corruption */
  2750. leaf = path->nodes[0];
  2751. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2752. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2753. btrfs_mark_buffer_dirty(leaf);
  2754. btrfs_release_path(path);
  2755. fail:
  2756. if (ret) {
  2757. btrfs_abort_transaction(trans, root, ret);
  2758. return ret;
  2759. }
  2760. return 0;
  2761. }
  2762. static struct btrfs_block_group_cache *
  2763. next_block_group(struct btrfs_root *root,
  2764. struct btrfs_block_group_cache *cache)
  2765. {
  2766. struct rb_node *node;
  2767. spin_lock(&root->fs_info->block_group_cache_lock);
  2768. node = rb_next(&cache->cache_node);
  2769. btrfs_put_block_group(cache);
  2770. if (node) {
  2771. cache = rb_entry(node, struct btrfs_block_group_cache,
  2772. cache_node);
  2773. btrfs_get_block_group(cache);
  2774. } else
  2775. cache = NULL;
  2776. spin_unlock(&root->fs_info->block_group_cache_lock);
  2777. return cache;
  2778. }
  2779. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2780. struct btrfs_trans_handle *trans,
  2781. struct btrfs_path *path)
  2782. {
  2783. struct btrfs_root *root = block_group->fs_info->tree_root;
  2784. struct inode *inode = NULL;
  2785. u64 alloc_hint = 0;
  2786. int dcs = BTRFS_DC_ERROR;
  2787. int num_pages = 0;
  2788. int retries = 0;
  2789. int ret = 0;
  2790. /*
  2791. * If this block group is smaller than 100 megs don't bother caching the
  2792. * block group.
  2793. */
  2794. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2795. spin_lock(&block_group->lock);
  2796. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2797. spin_unlock(&block_group->lock);
  2798. return 0;
  2799. }
  2800. again:
  2801. inode = lookup_free_space_inode(root, block_group, path);
  2802. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2803. ret = PTR_ERR(inode);
  2804. btrfs_release_path(path);
  2805. goto out;
  2806. }
  2807. if (IS_ERR(inode)) {
  2808. BUG_ON(retries);
  2809. retries++;
  2810. if (block_group->ro)
  2811. goto out_free;
  2812. ret = create_free_space_inode(root, trans, block_group, path);
  2813. if (ret)
  2814. goto out_free;
  2815. goto again;
  2816. }
  2817. /* We've already setup this transaction, go ahead and exit */
  2818. if (block_group->cache_generation == trans->transid &&
  2819. i_size_read(inode)) {
  2820. dcs = BTRFS_DC_SETUP;
  2821. goto out_put;
  2822. }
  2823. /*
  2824. * We want to set the generation to 0, that way if anything goes wrong
  2825. * from here on out we know not to trust this cache when we load up next
  2826. * time.
  2827. */
  2828. BTRFS_I(inode)->generation = 0;
  2829. ret = btrfs_update_inode(trans, root, inode);
  2830. WARN_ON(ret);
  2831. if (i_size_read(inode) > 0) {
  2832. ret = btrfs_check_trunc_cache_free_space(root,
  2833. &root->fs_info->global_block_rsv);
  2834. if (ret)
  2835. goto out_put;
  2836. ret = btrfs_truncate_free_space_cache(root, trans, inode);
  2837. if (ret)
  2838. goto out_put;
  2839. }
  2840. spin_lock(&block_group->lock);
  2841. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2842. !btrfs_test_opt(root, SPACE_CACHE)) {
  2843. /*
  2844. * don't bother trying to write stuff out _if_
  2845. * a) we're not cached,
  2846. * b) we're with nospace_cache mount option.
  2847. */
  2848. dcs = BTRFS_DC_WRITTEN;
  2849. spin_unlock(&block_group->lock);
  2850. goto out_put;
  2851. }
  2852. spin_unlock(&block_group->lock);
  2853. /*
  2854. * Try to preallocate enough space based on how big the block group is.
  2855. * Keep in mind this has to include any pinned space which could end up
  2856. * taking up quite a bit since it's not folded into the other space
  2857. * cache.
  2858. */
  2859. num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
  2860. if (!num_pages)
  2861. num_pages = 1;
  2862. num_pages *= 16;
  2863. num_pages *= PAGE_CACHE_SIZE;
  2864. ret = btrfs_check_data_free_space(inode, num_pages);
  2865. if (ret)
  2866. goto out_put;
  2867. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2868. num_pages, num_pages,
  2869. &alloc_hint);
  2870. if (!ret)
  2871. dcs = BTRFS_DC_SETUP;
  2872. btrfs_free_reserved_data_space(inode, num_pages);
  2873. out_put:
  2874. iput(inode);
  2875. out_free:
  2876. btrfs_release_path(path);
  2877. out:
  2878. spin_lock(&block_group->lock);
  2879. if (!ret && dcs == BTRFS_DC_SETUP)
  2880. block_group->cache_generation = trans->transid;
  2881. block_group->disk_cache_state = dcs;
  2882. spin_unlock(&block_group->lock);
  2883. return ret;
  2884. }
  2885. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2886. struct btrfs_root *root)
  2887. {
  2888. struct btrfs_block_group_cache *cache;
  2889. int err = 0;
  2890. struct btrfs_path *path;
  2891. u64 last = 0;
  2892. path = btrfs_alloc_path();
  2893. if (!path)
  2894. return -ENOMEM;
  2895. again:
  2896. while (1) {
  2897. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2898. while (cache) {
  2899. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2900. break;
  2901. cache = next_block_group(root, cache);
  2902. }
  2903. if (!cache) {
  2904. if (last == 0)
  2905. break;
  2906. last = 0;
  2907. continue;
  2908. }
  2909. err = cache_save_setup(cache, trans, path);
  2910. last = cache->key.objectid + cache->key.offset;
  2911. btrfs_put_block_group(cache);
  2912. }
  2913. while (1) {
  2914. if (last == 0) {
  2915. err = btrfs_run_delayed_refs(trans, root,
  2916. (unsigned long)-1);
  2917. if (err) /* File system offline */
  2918. goto out;
  2919. }
  2920. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2921. while (cache) {
  2922. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2923. btrfs_put_block_group(cache);
  2924. goto again;
  2925. }
  2926. if (cache->dirty)
  2927. break;
  2928. cache = next_block_group(root, cache);
  2929. }
  2930. if (!cache) {
  2931. if (last == 0)
  2932. break;
  2933. last = 0;
  2934. continue;
  2935. }
  2936. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2937. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2938. cache->dirty = 0;
  2939. last = cache->key.objectid + cache->key.offset;
  2940. err = write_one_cache_group(trans, root, path, cache);
  2941. btrfs_put_block_group(cache);
  2942. if (err) /* File system offline */
  2943. goto out;
  2944. }
  2945. while (1) {
  2946. /*
  2947. * I don't think this is needed since we're just marking our
  2948. * preallocated extent as written, but just in case it can't
  2949. * hurt.
  2950. */
  2951. if (last == 0) {
  2952. err = btrfs_run_delayed_refs(trans, root,
  2953. (unsigned long)-1);
  2954. if (err) /* File system offline */
  2955. goto out;
  2956. }
  2957. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2958. while (cache) {
  2959. /*
  2960. * Really this shouldn't happen, but it could if we
  2961. * couldn't write the entire preallocated extent and
  2962. * splitting the extent resulted in a new block.
  2963. */
  2964. if (cache->dirty) {
  2965. btrfs_put_block_group(cache);
  2966. goto again;
  2967. }
  2968. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2969. break;
  2970. cache = next_block_group(root, cache);
  2971. }
  2972. if (!cache) {
  2973. if (last == 0)
  2974. break;
  2975. last = 0;
  2976. continue;
  2977. }
  2978. err = btrfs_write_out_cache(root, trans, cache, path);
  2979. /*
  2980. * If we didn't have an error then the cache state is still
  2981. * NEED_WRITE, so we can set it to WRITTEN.
  2982. */
  2983. if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2984. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2985. last = cache->key.objectid + cache->key.offset;
  2986. btrfs_put_block_group(cache);
  2987. }
  2988. out:
  2989. btrfs_free_path(path);
  2990. return err;
  2991. }
  2992. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2993. {
  2994. struct btrfs_block_group_cache *block_group;
  2995. int readonly = 0;
  2996. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2997. if (!block_group || block_group->ro)
  2998. readonly = 1;
  2999. if (block_group)
  3000. btrfs_put_block_group(block_group);
  3001. return readonly;
  3002. }
  3003. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  3004. u64 total_bytes, u64 bytes_used,
  3005. struct btrfs_space_info **space_info)
  3006. {
  3007. struct btrfs_space_info *found;
  3008. int i;
  3009. int factor;
  3010. int ret;
  3011. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3012. BTRFS_BLOCK_GROUP_RAID10))
  3013. factor = 2;
  3014. else
  3015. factor = 1;
  3016. found = __find_space_info(info, flags);
  3017. if (found) {
  3018. spin_lock(&found->lock);
  3019. found->total_bytes += total_bytes;
  3020. found->disk_total += total_bytes * factor;
  3021. found->bytes_used += bytes_used;
  3022. found->disk_used += bytes_used * factor;
  3023. found->full = 0;
  3024. spin_unlock(&found->lock);
  3025. *space_info = found;
  3026. return 0;
  3027. }
  3028. found = kzalloc(sizeof(*found), GFP_NOFS);
  3029. if (!found)
  3030. return -ENOMEM;
  3031. ret = percpu_counter_init(&found->total_bytes_pinned, 0);
  3032. if (ret) {
  3033. kfree(found);
  3034. return ret;
  3035. }
  3036. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  3037. INIT_LIST_HEAD(&found->block_groups[i]);
  3038. init_rwsem(&found->groups_sem);
  3039. spin_lock_init(&found->lock);
  3040. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3041. found->total_bytes = total_bytes;
  3042. found->disk_total = total_bytes * factor;
  3043. found->bytes_used = bytes_used;
  3044. found->disk_used = bytes_used * factor;
  3045. found->bytes_pinned = 0;
  3046. found->bytes_reserved = 0;
  3047. found->bytes_readonly = 0;
  3048. found->bytes_may_use = 0;
  3049. found->full = 0;
  3050. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3051. found->chunk_alloc = 0;
  3052. found->flush = 0;
  3053. init_waitqueue_head(&found->wait);
  3054. *space_info = found;
  3055. list_add_rcu(&found->list, &info->space_info);
  3056. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3057. info->data_sinfo = found;
  3058. return 0;
  3059. }
  3060. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3061. {
  3062. u64 extra_flags = chunk_to_extended(flags) &
  3063. BTRFS_EXTENDED_PROFILE_MASK;
  3064. write_seqlock(&fs_info->profiles_lock);
  3065. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3066. fs_info->avail_data_alloc_bits |= extra_flags;
  3067. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3068. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3069. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3070. fs_info->avail_system_alloc_bits |= extra_flags;
  3071. write_sequnlock(&fs_info->profiles_lock);
  3072. }
  3073. /*
  3074. * returns target flags in extended format or 0 if restripe for this
  3075. * chunk_type is not in progress
  3076. *
  3077. * should be called with either volume_mutex or balance_lock held
  3078. */
  3079. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3080. {
  3081. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3082. u64 target = 0;
  3083. if (!bctl)
  3084. return 0;
  3085. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3086. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3087. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3088. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3089. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3090. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3091. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3092. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3093. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3094. }
  3095. return target;
  3096. }
  3097. /*
  3098. * @flags: available profiles in extended format (see ctree.h)
  3099. *
  3100. * Returns reduced profile in chunk format. If profile changing is in
  3101. * progress (either running or paused) picks the target profile (if it's
  3102. * already available), otherwise falls back to plain reducing.
  3103. */
  3104. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3105. {
  3106. /*
  3107. * we add in the count of missing devices because we want
  3108. * to make sure that any RAID levels on a degraded FS
  3109. * continue to be honored.
  3110. */
  3111. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  3112. root->fs_info->fs_devices->missing_devices;
  3113. u64 target;
  3114. u64 tmp;
  3115. /*
  3116. * see if restripe for this chunk_type is in progress, if so
  3117. * try to reduce to the target profile
  3118. */
  3119. spin_lock(&root->fs_info->balance_lock);
  3120. target = get_restripe_target(root->fs_info, flags);
  3121. if (target) {
  3122. /* pick target profile only if it's already available */
  3123. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3124. spin_unlock(&root->fs_info->balance_lock);
  3125. return extended_to_chunk(target);
  3126. }
  3127. }
  3128. spin_unlock(&root->fs_info->balance_lock);
  3129. /* First, mask out the RAID levels which aren't possible */
  3130. if (num_devices == 1)
  3131. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3132. BTRFS_BLOCK_GROUP_RAID5);
  3133. if (num_devices < 3)
  3134. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3135. if (num_devices < 4)
  3136. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3137. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3138. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3139. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3140. flags &= ~tmp;
  3141. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3142. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3143. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3144. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3145. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3146. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3147. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3148. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3149. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3150. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3151. return extended_to_chunk(flags | tmp);
  3152. }
  3153. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  3154. {
  3155. unsigned seq;
  3156. do {
  3157. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3158. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3159. flags |= root->fs_info->avail_data_alloc_bits;
  3160. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3161. flags |= root->fs_info->avail_system_alloc_bits;
  3162. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3163. flags |= root->fs_info->avail_metadata_alloc_bits;
  3164. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3165. return btrfs_reduce_alloc_profile(root, flags);
  3166. }
  3167. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3168. {
  3169. u64 flags;
  3170. u64 ret;
  3171. if (data)
  3172. flags = BTRFS_BLOCK_GROUP_DATA;
  3173. else if (root == root->fs_info->chunk_root)
  3174. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3175. else
  3176. flags = BTRFS_BLOCK_GROUP_METADATA;
  3177. ret = get_alloc_profile(root, flags);
  3178. return ret;
  3179. }
  3180. /*
  3181. * This will check the space that the inode allocates from to make sure we have
  3182. * enough space for bytes.
  3183. */
  3184. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  3185. {
  3186. struct btrfs_space_info *data_sinfo;
  3187. struct btrfs_root *root = BTRFS_I(inode)->root;
  3188. struct btrfs_fs_info *fs_info = root->fs_info;
  3189. u64 used;
  3190. int ret = 0, committed = 0, alloc_chunk = 1;
  3191. /* make sure bytes are sectorsize aligned */
  3192. bytes = ALIGN(bytes, root->sectorsize);
  3193. if (root == root->fs_info->tree_root ||
  3194. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  3195. alloc_chunk = 0;
  3196. committed = 1;
  3197. }
  3198. data_sinfo = fs_info->data_sinfo;
  3199. if (!data_sinfo)
  3200. goto alloc;
  3201. again:
  3202. /* make sure we have enough space to handle the data first */
  3203. spin_lock(&data_sinfo->lock);
  3204. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3205. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3206. data_sinfo->bytes_may_use;
  3207. if (used + bytes > data_sinfo->total_bytes) {
  3208. struct btrfs_trans_handle *trans;
  3209. /*
  3210. * if we don't have enough free bytes in this space then we need
  3211. * to alloc a new chunk.
  3212. */
  3213. if (!data_sinfo->full && alloc_chunk) {
  3214. u64 alloc_target;
  3215. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3216. spin_unlock(&data_sinfo->lock);
  3217. alloc:
  3218. alloc_target = btrfs_get_alloc_profile(root, 1);
  3219. trans = btrfs_join_transaction(root);
  3220. if (IS_ERR(trans))
  3221. return PTR_ERR(trans);
  3222. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3223. alloc_target,
  3224. CHUNK_ALLOC_NO_FORCE);
  3225. btrfs_end_transaction(trans, root);
  3226. if (ret < 0) {
  3227. if (ret != -ENOSPC)
  3228. return ret;
  3229. else
  3230. goto commit_trans;
  3231. }
  3232. if (!data_sinfo)
  3233. data_sinfo = fs_info->data_sinfo;
  3234. goto again;
  3235. }
  3236. /*
  3237. * If we don't have enough pinned space to deal with this
  3238. * allocation don't bother committing the transaction.
  3239. */
  3240. if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
  3241. bytes) < 0)
  3242. committed = 1;
  3243. spin_unlock(&data_sinfo->lock);
  3244. /* commit the current transaction and try again */
  3245. commit_trans:
  3246. if (!committed &&
  3247. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3248. committed = 1;
  3249. trans = btrfs_join_transaction(root);
  3250. if (IS_ERR(trans))
  3251. return PTR_ERR(trans);
  3252. ret = btrfs_commit_transaction(trans, root);
  3253. if (ret)
  3254. return ret;
  3255. goto again;
  3256. }
  3257. return -ENOSPC;
  3258. }
  3259. data_sinfo->bytes_may_use += bytes;
  3260. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3261. data_sinfo->flags, bytes, 1);
  3262. spin_unlock(&data_sinfo->lock);
  3263. return 0;
  3264. }
  3265. /*
  3266. * Called if we need to clear a data reservation for this inode.
  3267. */
  3268. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3269. {
  3270. struct btrfs_root *root = BTRFS_I(inode)->root;
  3271. struct btrfs_space_info *data_sinfo;
  3272. /* make sure bytes are sectorsize aligned */
  3273. bytes = ALIGN(bytes, root->sectorsize);
  3274. data_sinfo = root->fs_info->data_sinfo;
  3275. spin_lock(&data_sinfo->lock);
  3276. WARN_ON(data_sinfo->bytes_may_use < bytes);
  3277. data_sinfo->bytes_may_use -= bytes;
  3278. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3279. data_sinfo->flags, bytes, 0);
  3280. spin_unlock(&data_sinfo->lock);
  3281. }
  3282. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3283. {
  3284. struct list_head *head = &info->space_info;
  3285. struct btrfs_space_info *found;
  3286. rcu_read_lock();
  3287. list_for_each_entry_rcu(found, head, list) {
  3288. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3289. found->force_alloc = CHUNK_ALLOC_FORCE;
  3290. }
  3291. rcu_read_unlock();
  3292. }
  3293. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3294. {
  3295. return (global->size << 1);
  3296. }
  3297. static int should_alloc_chunk(struct btrfs_root *root,
  3298. struct btrfs_space_info *sinfo, int force)
  3299. {
  3300. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3301. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3302. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3303. u64 thresh;
  3304. if (force == CHUNK_ALLOC_FORCE)
  3305. return 1;
  3306. /*
  3307. * We need to take into account the global rsv because for all intents
  3308. * and purposes it's used space. Don't worry about locking the
  3309. * global_rsv, it doesn't change except when the transaction commits.
  3310. */
  3311. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3312. num_allocated += calc_global_rsv_need_space(global_rsv);
  3313. /*
  3314. * in limited mode, we want to have some free space up to
  3315. * about 1% of the FS size.
  3316. */
  3317. if (force == CHUNK_ALLOC_LIMITED) {
  3318. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3319. thresh = max_t(u64, 64 * 1024 * 1024,
  3320. div_factor_fine(thresh, 1));
  3321. if (num_bytes - num_allocated < thresh)
  3322. return 1;
  3323. }
  3324. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3325. return 0;
  3326. return 1;
  3327. }
  3328. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3329. {
  3330. u64 num_dev;
  3331. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3332. BTRFS_BLOCK_GROUP_RAID0 |
  3333. BTRFS_BLOCK_GROUP_RAID5 |
  3334. BTRFS_BLOCK_GROUP_RAID6))
  3335. num_dev = root->fs_info->fs_devices->rw_devices;
  3336. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3337. num_dev = 2;
  3338. else
  3339. num_dev = 1; /* DUP or single */
  3340. /* metadata for updaing devices and chunk tree */
  3341. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3342. }
  3343. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3344. struct btrfs_root *root, u64 type)
  3345. {
  3346. struct btrfs_space_info *info;
  3347. u64 left;
  3348. u64 thresh;
  3349. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3350. spin_lock(&info->lock);
  3351. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3352. info->bytes_reserved - info->bytes_readonly;
  3353. spin_unlock(&info->lock);
  3354. thresh = get_system_chunk_thresh(root, type);
  3355. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3356. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3357. left, thresh, type);
  3358. dump_space_info(info, 0, 0);
  3359. }
  3360. if (left < thresh) {
  3361. u64 flags;
  3362. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3363. btrfs_alloc_chunk(trans, root, flags);
  3364. }
  3365. }
  3366. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3367. struct btrfs_root *extent_root, u64 flags, int force)
  3368. {
  3369. struct btrfs_space_info *space_info;
  3370. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3371. int wait_for_alloc = 0;
  3372. int ret = 0;
  3373. /* Don't re-enter if we're already allocating a chunk */
  3374. if (trans->allocating_chunk)
  3375. return -ENOSPC;
  3376. space_info = __find_space_info(extent_root->fs_info, flags);
  3377. if (!space_info) {
  3378. ret = update_space_info(extent_root->fs_info, flags,
  3379. 0, 0, &space_info);
  3380. BUG_ON(ret); /* -ENOMEM */
  3381. }
  3382. BUG_ON(!space_info); /* Logic error */
  3383. again:
  3384. spin_lock(&space_info->lock);
  3385. if (force < space_info->force_alloc)
  3386. force = space_info->force_alloc;
  3387. if (space_info->full) {
  3388. if (should_alloc_chunk(extent_root, space_info, force))
  3389. ret = -ENOSPC;
  3390. else
  3391. ret = 0;
  3392. spin_unlock(&space_info->lock);
  3393. return ret;
  3394. }
  3395. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3396. spin_unlock(&space_info->lock);
  3397. return 0;
  3398. } else if (space_info->chunk_alloc) {
  3399. wait_for_alloc = 1;
  3400. } else {
  3401. space_info->chunk_alloc = 1;
  3402. }
  3403. spin_unlock(&space_info->lock);
  3404. mutex_lock(&fs_info->chunk_mutex);
  3405. /*
  3406. * The chunk_mutex is held throughout the entirety of a chunk
  3407. * allocation, so once we've acquired the chunk_mutex we know that the
  3408. * other guy is done and we need to recheck and see if we should
  3409. * allocate.
  3410. */
  3411. if (wait_for_alloc) {
  3412. mutex_unlock(&fs_info->chunk_mutex);
  3413. wait_for_alloc = 0;
  3414. goto again;
  3415. }
  3416. trans->allocating_chunk = true;
  3417. /*
  3418. * If we have mixed data/metadata chunks we want to make sure we keep
  3419. * allocating mixed chunks instead of individual chunks.
  3420. */
  3421. if (btrfs_mixed_space_info(space_info))
  3422. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3423. /*
  3424. * if we're doing a data chunk, go ahead and make sure that
  3425. * we keep a reasonable number of metadata chunks allocated in the
  3426. * FS as well.
  3427. */
  3428. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3429. fs_info->data_chunk_allocations++;
  3430. if (!(fs_info->data_chunk_allocations %
  3431. fs_info->metadata_ratio))
  3432. force_metadata_allocation(fs_info);
  3433. }
  3434. /*
  3435. * Check if we have enough space in SYSTEM chunk because we may need
  3436. * to update devices.
  3437. */
  3438. check_system_chunk(trans, extent_root, flags);
  3439. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3440. trans->allocating_chunk = false;
  3441. spin_lock(&space_info->lock);
  3442. if (ret < 0 && ret != -ENOSPC)
  3443. goto out;
  3444. if (ret)
  3445. space_info->full = 1;
  3446. else
  3447. ret = 1;
  3448. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3449. out:
  3450. space_info->chunk_alloc = 0;
  3451. spin_unlock(&space_info->lock);
  3452. mutex_unlock(&fs_info->chunk_mutex);
  3453. return ret;
  3454. }
  3455. static int can_overcommit(struct btrfs_root *root,
  3456. struct btrfs_space_info *space_info, u64 bytes,
  3457. enum btrfs_reserve_flush_enum flush)
  3458. {
  3459. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3460. u64 profile = btrfs_get_alloc_profile(root, 0);
  3461. u64 space_size;
  3462. u64 avail;
  3463. u64 used;
  3464. used = space_info->bytes_used + space_info->bytes_reserved +
  3465. space_info->bytes_pinned + space_info->bytes_readonly;
  3466. /*
  3467. * We only want to allow over committing if we have lots of actual space
  3468. * free, but if we don't have enough space to handle the global reserve
  3469. * space then we could end up having a real enospc problem when trying
  3470. * to allocate a chunk or some other such important allocation.
  3471. */
  3472. spin_lock(&global_rsv->lock);
  3473. space_size = calc_global_rsv_need_space(global_rsv);
  3474. spin_unlock(&global_rsv->lock);
  3475. if (used + space_size >= space_info->total_bytes)
  3476. return 0;
  3477. used += space_info->bytes_may_use;
  3478. spin_lock(&root->fs_info->free_chunk_lock);
  3479. avail = root->fs_info->free_chunk_space;
  3480. spin_unlock(&root->fs_info->free_chunk_lock);
  3481. /*
  3482. * If we have dup, raid1 or raid10 then only half of the free
  3483. * space is actually useable. For raid56, the space info used
  3484. * doesn't include the parity drive, so we don't have to
  3485. * change the math
  3486. */
  3487. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3488. BTRFS_BLOCK_GROUP_RAID1 |
  3489. BTRFS_BLOCK_GROUP_RAID10))
  3490. avail >>= 1;
  3491. /*
  3492. * If we aren't flushing all things, let us overcommit up to
  3493. * 1/2th of the space. If we can flush, don't let us overcommit
  3494. * too much, let it overcommit up to 1/8 of the space.
  3495. */
  3496. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3497. avail >>= 3;
  3498. else
  3499. avail >>= 1;
  3500. if (used + bytes < space_info->total_bytes + avail)
  3501. return 1;
  3502. return 0;
  3503. }
  3504. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3505. unsigned long nr_pages)
  3506. {
  3507. struct super_block *sb = root->fs_info->sb;
  3508. if (down_read_trylock(&sb->s_umount)) {
  3509. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  3510. up_read(&sb->s_umount);
  3511. } else {
  3512. /*
  3513. * We needn't worry the filesystem going from r/w to r/o though
  3514. * we don't acquire ->s_umount mutex, because the filesystem
  3515. * should guarantee the delalloc inodes list be empty after
  3516. * the filesystem is readonly(all dirty pages are written to
  3517. * the disk).
  3518. */
  3519. btrfs_start_all_delalloc_inodes(root->fs_info, 0);
  3520. if (!current->journal_info)
  3521. btrfs_wait_all_ordered_extents(root->fs_info);
  3522. }
  3523. }
  3524. /*
  3525. * shrink metadata reservation for delalloc
  3526. */
  3527. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3528. bool wait_ordered)
  3529. {
  3530. struct btrfs_block_rsv *block_rsv;
  3531. struct btrfs_space_info *space_info;
  3532. struct btrfs_trans_handle *trans;
  3533. u64 delalloc_bytes;
  3534. u64 max_reclaim;
  3535. long time_left;
  3536. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3537. int loops = 0;
  3538. enum btrfs_reserve_flush_enum flush;
  3539. trans = (struct btrfs_trans_handle *)current->journal_info;
  3540. block_rsv = &root->fs_info->delalloc_block_rsv;
  3541. space_info = block_rsv->space_info;
  3542. smp_mb();
  3543. delalloc_bytes = percpu_counter_sum_positive(
  3544. &root->fs_info->delalloc_bytes);
  3545. if (delalloc_bytes == 0) {
  3546. if (trans)
  3547. return;
  3548. btrfs_wait_all_ordered_extents(root->fs_info);
  3549. return;
  3550. }
  3551. while (delalloc_bytes && loops < 3) {
  3552. max_reclaim = min(delalloc_bytes, to_reclaim);
  3553. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3554. btrfs_writeback_inodes_sb_nr(root, nr_pages);
  3555. /*
  3556. * We need to wait for the async pages to actually start before
  3557. * we do anything.
  3558. */
  3559. wait_event(root->fs_info->async_submit_wait,
  3560. !atomic_read(&root->fs_info->async_delalloc_pages));
  3561. if (!trans)
  3562. flush = BTRFS_RESERVE_FLUSH_ALL;
  3563. else
  3564. flush = BTRFS_RESERVE_NO_FLUSH;
  3565. spin_lock(&space_info->lock);
  3566. if (can_overcommit(root, space_info, orig, flush)) {
  3567. spin_unlock(&space_info->lock);
  3568. break;
  3569. }
  3570. spin_unlock(&space_info->lock);
  3571. loops++;
  3572. if (wait_ordered && !trans) {
  3573. btrfs_wait_all_ordered_extents(root->fs_info);
  3574. } else {
  3575. time_left = schedule_timeout_killable(1);
  3576. if (time_left)
  3577. break;
  3578. }
  3579. smp_mb();
  3580. delalloc_bytes = percpu_counter_sum_positive(
  3581. &root->fs_info->delalloc_bytes);
  3582. }
  3583. }
  3584. /**
  3585. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3586. * @root - the root we're allocating for
  3587. * @bytes - the number of bytes we want to reserve
  3588. * @force - force the commit
  3589. *
  3590. * This will check to make sure that committing the transaction will actually
  3591. * get us somewhere and then commit the transaction if it does. Otherwise it
  3592. * will return -ENOSPC.
  3593. */
  3594. static int may_commit_transaction(struct btrfs_root *root,
  3595. struct btrfs_space_info *space_info,
  3596. u64 bytes, int force)
  3597. {
  3598. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3599. struct btrfs_trans_handle *trans;
  3600. trans = (struct btrfs_trans_handle *)current->journal_info;
  3601. if (trans)
  3602. return -EAGAIN;
  3603. if (force)
  3604. goto commit;
  3605. /* See if there is enough pinned space to make this reservation */
  3606. spin_lock(&space_info->lock);
  3607. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3608. bytes) >= 0) {
  3609. spin_unlock(&space_info->lock);
  3610. goto commit;
  3611. }
  3612. spin_unlock(&space_info->lock);
  3613. /*
  3614. * See if there is some space in the delayed insertion reservation for
  3615. * this reservation.
  3616. */
  3617. if (space_info != delayed_rsv->space_info)
  3618. return -ENOSPC;
  3619. spin_lock(&space_info->lock);
  3620. spin_lock(&delayed_rsv->lock);
  3621. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3622. bytes - delayed_rsv->size) >= 0) {
  3623. spin_unlock(&delayed_rsv->lock);
  3624. spin_unlock(&space_info->lock);
  3625. return -ENOSPC;
  3626. }
  3627. spin_unlock(&delayed_rsv->lock);
  3628. spin_unlock(&space_info->lock);
  3629. commit:
  3630. trans = btrfs_join_transaction(root);
  3631. if (IS_ERR(trans))
  3632. return -ENOSPC;
  3633. return btrfs_commit_transaction(trans, root);
  3634. }
  3635. enum flush_state {
  3636. FLUSH_DELAYED_ITEMS_NR = 1,
  3637. FLUSH_DELAYED_ITEMS = 2,
  3638. FLUSH_DELALLOC = 3,
  3639. FLUSH_DELALLOC_WAIT = 4,
  3640. ALLOC_CHUNK = 5,
  3641. COMMIT_TRANS = 6,
  3642. };
  3643. static int flush_space(struct btrfs_root *root,
  3644. struct btrfs_space_info *space_info, u64 num_bytes,
  3645. u64 orig_bytes, int state)
  3646. {
  3647. struct btrfs_trans_handle *trans;
  3648. int nr;
  3649. int ret = 0;
  3650. switch (state) {
  3651. case FLUSH_DELAYED_ITEMS_NR:
  3652. case FLUSH_DELAYED_ITEMS:
  3653. if (state == FLUSH_DELAYED_ITEMS_NR) {
  3654. u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
  3655. nr = (int)div64_u64(num_bytes, bytes);
  3656. if (!nr)
  3657. nr = 1;
  3658. nr *= 2;
  3659. } else {
  3660. nr = -1;
  3661. }
  3662. trans = btrfs_join_transaction(root);
  3663. if (IS_ERR(trans)) {
  3664. ret = PTR_ERR(trans);
  3665. break;
  3666. }
  3667. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3668. btrfs_end_transaction(trans, root);
  3669. break;
  3670. case FLUSH_DELALLOC:
  3671. case FLUSH_DELALLOC_WAIT:
  3672. shrink_delalloc(root, num_bytes, orig_bytes,
  3673. state == FLUSH_DELALLOC_WAIT);
  3674. break;
  3675. case ALLOC_CHUNK:
  3676. trans = btrfs_join_transaction(root);
  3677. if (IS_ERR(trans)) {
  3678. ret = PTR_ERR(trans);
  3679. break;
  3680. }
  3681. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3682. btrfs_get_alloc_profile(root, 0),
  3683. CHUNK_ALLOC_NO_FORCE);
  3684. btrfs_end_transaction(trans, root);
  3685. if (ret == -ENOSPC)
  3686. ret = 0;
  3687. break;
  3688. case COMMIT_TRANS:
  3689. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3690. break;
  3691. default:
  3692. ret = -ENOSPC;
  3693. break;
  3694. }
  3695. return ret;
  3696. }
  3697. /**
  3698. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3699. * @root - the root we're allocating for
  3700. * @block_rsv - the block_rsv we're allocating for
  3701. * @orig_bytes - the number of bytes we want
  3702. * @flush - whether or not we can flush to make our reservation
  3703. *
  3704. * This will reserve orgi_bytes number of bytes from the space info associated
  3705. * with the block_rsv. If there is not enough space it will make an attempt to
  3706. * flush out space to make room. It will do this by flushing delalloc if
  3707. * possible or committing the transaction. If flush is 0 then no attempts to
  3708. * regain reservations will be made and this will fail if there is not enough
  3709. * space already.
  3710. */
  3711. static int reserve_metadata_bytes(struct btrfs_root *root,
  3712. struct btrfs_block_rsv *block_rsv,
  3713. u64 orig_bytes,
  3714. enum btrfs_reserve_flush_enum flush)
  3715. {
  3716. struct btrfs_space_info *space_info = block_rsv->space_info;
  3717. u64 used;
  3718. u64 num_bytes = orig_bytes;
  3719. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  3720. int ret = 0;
  3721. bool flushing = false;
  3722. again:
  3723. ret = 0;
  3724. spin_lock(&space_info->lock);
  3725. /*
  3726. * We only want to wait if somebody other than us is flushing and we
  3727. * are actually allowed to flush all things.
  3728. */
  3729. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  3730. space_info->flush) {
  3731. spin_unlock(&space_info->lock);
  3732. /*
  3733. * If we have a trans handle we can't wait because the flusher
  3734. * may have to commit the transaction, which would mean we would
  3735. * deadlock since we are waiting for the flusher to finish, but
  3736. * hold the current transaction open.
  3737. */
  3738. if (current->journal_info)
  3739. return -EAGAIN;
  3740. ret = wait_event_killable(space_info->wait, !space_info->flush);
  3741. /* Must have been killed, return */
  3742. if (ret)
  3743. return -EINTR;
  3744. spin_lock(&space_info->lock);
  3745. }
  3746. ret = -ENOSPC;
  3747. used = space_info->bytes_used + space_info->bytes_reserved +
  3748. space_info->bytes_pinned + space_info->bytes_readonly +
  3749. space_info->bytes_may_use;
  3750. /*
  3751. * The idea here is that we've not already over-reserved the block group
  3752. * then we can go ahead and save our reservation first and then start
  3753. * flushing if we need to. Otherwise if we've already overcommitted
  3754. * lets start flushing stuff first and then come back and try to make
  3755. * our reservation.
  3756. */
  3757. if (used <= space_info->total_bytes) {
  3758. if (used + orig_bytes <= space_info->total_bytes) {
  3759. space_info->bytes_may_use += orig_bytes;
  3760. trace_btrfs_space_reservation(root->fs_info,
  3761. "space_info", space_info->flags, orig_bytes, 1);
  3762. ret = 0;
  3763. } else {
  3764. /*
  3765. * Ok set num_bytes to orig_bytes since we aren't
  3766. * overocmmitted, this way we only try and reclaim what
  3767. * we need.
  3768. */
  3769. num_bytes = orig_bytes;
  3770. }
  3771. } else {
  3772. /*
  3773. * Ok we're over committed, set num_bytes to the overcommitted
  3774. * amount plus the amount of bytes that we need for this
  3775. * reservation.
  3776. */
  3777. num_bytes = used - space_info->total_bytes +
  3778. (orig_bytes * 2);
  3779. }
  3780. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  3781. space_info->bytes_may_use += orig_bytes;
  3782. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3783. space_info->flags, orig_bytes,
  3784. 1);
  3785. ret = 0;
  3786. }
  3787. /*
  3788. * Couldn't make our reservation, save our place so while we're trying
  3789. * to reclaim space we can actually use it instead of somebody else
  3790. * stealing it from us.
  3791. *
  3792. * We make the other tasks wait for the flush only when we can flush
  3793. * all things.
  3794. */
  3795. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  3796. flushing = true;
  3797. space_info->flush = 1;
  3798. }
  3799. spin_unlock(&space_info->lock);
  3800. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  3801. goto out;
  3802. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  3803. flush_state);
  3804. flush_state++;
  3805. /*
  3806. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  3807. * would happen. So skip delalloc flush.
  3808. */
  3809. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3810. (flush_state == FLUSH_DELALLOC ||
  3811. flush_state == FLUSH_DELALLOC_WAIT))
  3812. flush_state = ALLOC_CHUNK;
  3813. if (!ret)
  3814. goto again;
  3815. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3816. flush_state < COMMIT_TRANS)
  3817. goto again;
  3818. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  3819. flush_state <= COMMIT_TRANS)
  3820. goto again;
  3821. out:
  3822. if (ret == -ENOSPC &&
  3823. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  3824. struct btrfs_block_rsv *global_rsv =
  3825. &root->fs_info->global_block_rsv;
  3826. if (block_rsv != global_rsv &&
  3827. !block_rsv_use_bytes(global_rsv, orig_bytes))
  3828. ret = 0;
  3829. }
  3830. if (flushing) {
  3831. spin_lock(&space_info->lock);
  3832. space_info->flush = 0;
  3833. wake_up_all(&space_info->wait);
  3834. spin_unlock(&space_info->lock);
  3835. }
  3836. return ret;
  3837. }
  3838. static struct btrfs_block_rsv *get_block_rsv(
  3839. const struct btrfs_trans_handle *trans,
  3840. const struct btrfs_root *root)
  3841. {
  3842. struct btrfs_block_rsv *block_rsv = NULL;
  3843. if (root->ref_cows)
  3844. block_rsv = trans->block_rsv;
  3845. if (root == root->fs_info->csum_root && trans->adding_csums)
  3846. block_rsv = trans->block_rsv;
  3847. if (root == root->fs_info->uuid_root)
  3848. block_rsv = trans->block_rsv;
  3849. if (!block_rsv)
  3850. block_rsv = root->block_rsv;
  3851. if (!block_rsv)
  3852. block_rsv = &root->fs_info->empty_block_rsv;
  3853. return block_rsv;
  3854. }
  3855. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3856. u64 num_bytes)
  3857. {
  3858. int ret = -ENOSPC;
  3859. spin_lock(&block_rsv->lock);
  3860. if (block_rsv->reserved >= num_bytes) {
  3861. block_rsv->reserved -= num_bytes;
  3862. if (block_rsv->reserved < block_rsv->size)
  3863. block_rsv->full = 0;
  3864. ret = 0;
  3865. }
  3866. spin_unlock(&block_rsv->lock);
  3867. return ret;
  3868. }
  3869. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3870. u64 num_bytes, int update_size)
  3871. {
  3872. spin_lock(&block_rsv->lock);
  3873. block_rsv->reserved += num_bytes;
  3874. if (update_size)
  3875. block_rsv->size += num_bytes;
  3876. else if (block_rsv->reserved >= block_rsv->size)
  3877. block_rsv->full = 1;
  3878. spin_unlock(&block_rsv->lock);
  3879. }
  3880. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  3881. struct btrfs_block_rsv *dest, u64 num_bytes,
  3882. int min_factor)
  3883. {
  3884. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  3885. u64 min_bytes;
  3886. if (global_rsv->space_info != dest->space_info)
  3887. return -ENOSPC;
  3888. spin_lock(&global_rsv->lock);
  3889. min_bytes = div_factor(global_rsv->size, min_factor);
  3890. if (global_rsv->reserved < min_bytes + num_bytes) {
  3891. spin_unlock(&global_rsv->lock);
  3892. return -ENOSPC;
  3893. }
  3894. global_rsv->reserved -= num_bytes;
  3895. if (global_rsv->reserved < global_rsv->size)
  3896. global_rsv->full = 0;
  3897. spin_unlock(&global_rsv->lock);
  3898. block_rsv_add_bytes(dest, num_bytes, 1);
  3899. return 0;
  3900. }
  3901. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3902. struct btrfs_block_rsv *block_rsv,
  3903. struct btrfs_block_rsv *dest, u64 num_bytes)
  3904. {
  3905. struct btrfs_space_info *space_info = block_rsv->space_info;
  3906. spin_lock(&block_rsv->lock);
  3907. if (num_bytes == (u64)-1)
  3908. num_bytes = block_rsv->size;
  3909. block_rsv->size -= num_bytes;
  3910. if (block_rsv->reserved >= block_rsv->size) {
  3911. num_bytes = block_rsv->reserved - block_rsv->size;
  3912. block_rsv->reserved = block_rsv->size;
  3913. block_rsv->full = 1;
  3914. } else {
  3915. num_bytes = 0;
  3916. }
  3917. spin_unlock(&block_rsv->lock);
  3918. if (num_bytes > 0) {
  3919. if (dest) {
  3920. spin_lock(&dest->lock);
  3921. if (!dest->full) {
  3922. u64 bytes_to_add;
  3923. bytes_to_add = dest->size - dest->reserved;
  3924. bytes_to_add = min(num_bytes, bytes_to_add);
  3925. dest->reserved += bytes_to_add;
  3926. if (dest->reserved >= dest->size)
  3927. dest->full = 1;
  3928. num_bytes -= bytes_to_add;
  3929. }
  3930. spin_unlock(&dest->lock);
  3931. }
  3932. if (num_bytes) {
  3933. spin_lock(&space_info->lock);
  3934. space_info->bytes_may_use -= num_bytes;
  3935. trace_btrfs_space_reservation(fs_info, "space_info",
  3936. space_info->flags, num_bytes, 0);
  3937. spin_unlock(&space_info->lock);
  3938. }
  3939. }
  3940. }
  3941. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3942. struct btrfs_block_rsv *dst, u64 num_bytes)
  3943. {
  3944. int ret;
  3945. ret = block_rsv_use_bytes(src, num_bytes);
  3946. if (ret)
  3947. return ret;
  3948. block_rsv_add_bytes(dst, num_bytes, 1);
  3949. return 0;
  3950. }
  3951. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  3952. {
  3953. memset(rsv, 0, sizeof(*rsv));
  3954. spin_lock_init(&rsv->lock);
  3955. rsv->type = type;
  3956. }
  3957. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  3958. unsigned short type)
  3959. {
  3960. struct btrfs_block_rsv *block_rsv;
  3961. struct btrfs_fs_info *fs_info = root->fs_info;
  3962. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3963. if (!block_rsv)
  3964. return NULL;
  3965. btrfs_init_block_rsv(block_rsv, type);
  3966. block_rsv->space_info = __find_space_info(fs_info,
  3967. BTRFS_BLOCK_GROUP_METADATA);
  3968. return block_rsv;
  3969. }
  3970. void btrfs_free_block_rsv(struct btrfs_root *root,
  3971. struct btrfs_block_rsv *rsv)
  3972. {
  3973. if (!rsv)
  3974. return;
  3975. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3976. kfree(rsv);
  3977. }
  3978. int btrfs_block_rsv_add(struct btrfs_root *root,
  3979. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  3980. enum btrfs_reserve_flush_enum flush)
  3981. {
  3982. int ret;
  3983. if (num_bytes == 0)
  3984. return 0;
  3985. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3986. if (!ret) {
  3987. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3988. return 0;
  3989. }
  3990. return ret;
  3991. }
  3992. int btrfs_block_rsv_check(struct btrfs_root *root,
  3993. struct btrfs_block_rsv *block_rsv, int min_factor)
  3994. {
  3995. u64 num_bytes = 0;
  3996. int ret = -ENOSPC;
  3997. if (!block_rsv)
  3998. return 0;
  3999. spin_lock(&block_rsv->lock);
  4000. num_bytes = div_factor(block_rsv->size, min_factor);
  4001. if (block_rsv->reserved >= num_bytes)
  4002. ret = 0;
  4003. spin_unlock(&block_rsv->lock);
  4004. return ret;
  4005. }
  4006. int btrfs_block_rsv_refill(struct btrfs_root *root,
  4007. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  4008. enum btrfs_reserve_flush_enum flush)
  4009. {
  4010. u64 num_bytes = 0;
  4011. int ret = -ENOSPC;
  4012. if (!block_rsv)
  4013. return 0;
  4014. spin_lock(&block_rsv->lock);
  4015. num_bytes = min_reserved;
  4016. if (block_rsv->reserved >= num_bytes)
  4017. ret = 0;
  4018. else
  4019. num_bytes -= block_rsv->reserved;
  4020. spin_unlock(&block_rsv->lock);
  4021. if (!ret)
  4022. return 0;
  4023. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4024. if (!ret) {
  4025. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  4026. return 0;
  4027. }
  4028. return ret;
  4029. }
  4030. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  4031. struct btrfs_block_rsv *dst_rsv,
  4032. u64 num_bytes)
  4033. {
  4034. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4035. }
  4036. void btrfs_block_rsv_release(struct btrfs_root *root,
  4037. struct btrfs_block_rsv *block_rsv,
  4038. u64 num_bytes)
  4039. {
  4040. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4041. if (global_rsv->full || global_rsv == block_rsv ||
  4042. block_rsv->space_info != global_rsv->space_info)
  4043. global_rsv = NULL;
  4044. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4045. num_bytes);
  4046. }
  4047. /*
  4048. * helper to calculate size of global block reservation.
  4049. * the desired value is sum of space used by extent tree,
  4050. * checksum tree and root tree
  4051. */
  4052. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4053. {
  4054. struct btrfs_space_info *sinfo;
  4055. u64 num_bytes;
  4056. u64 meta_used;
  4057. u64 data_used;
  4058. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4059. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4060. spin_lock(&sinfo->lock);
  4061. data_used = sinfo->bytes_used;
  4062. spin_unlock(&sinfo->lock);
  4063. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4064. spin_lock(&sinfo->lock);
  4065. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4066. data_used = 0;
  4067. meta_used = sinfo->bytes_used;
  4068. spin_unlock(&sinfo->lock);
  4069. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4070. csum_size * 2;
  4071. num_bytes += div64_u64(data_used + meta_used, 50);
  4072. if (num_bytes * 3 > meta_used)
  4073. num_bytes = div64_u64(meta_used, 3);
  4074. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  4075. }
  4076. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4077. {
  4078. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4079. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4080. u64 num_bytes;
  4081. num_bytes = calc_global_metadata_size(fs_info);
  4082. spin_lock(&sinfo->lock);
  4083. spin_lock(&block_rsv->lock);
  4084. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4085. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4086. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4087. sinfo->bytes_may_use;
  4088. if (sinfo->total_bytes > num_bytes) {
  4089. num_bytes = sinfo->total_bytes - num_bytes;
  4090. block_rsv->reserved += num_bytes;
  4091. sinfo->bytes_may_use += num_bytes;
  4092. trace_btrfs_space_reservation(fs_info, "space_info",
  4093. sinfo->flags, num_bytes, 1);
  4094. }
  4095. if (block_rsv->reserved >= block_rsv->size) {
  4096. num_bytes = block_rsv->reserved - block_rsv->size;
  4097. sinfo->bytes_may_use -= num_bytes;
  4098. trace_btrfs_space_reservation(fs_info, "space_info",
  4099. sinfo->flags, num_bytes, 0);
  4100. block_rsv->reserved = block_rsv->size;
  4101. block_rsv->full = 1;
  4102. }
  4103. spin_unlock(&block_rsv->lock);
  4104. spin_unlock(&sinfo->lock);
  4105. }
  4106. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4107. {
  4108. struct btrfs_space_info *space_info;
  4109. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4110. fs_info->chunk_block_rsv.space_info = space_info;
  4111. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4112. fs_info->global_block_rsv.space_info = space_info;
  4113. fs_info->delalloc_block_rsv.space_info = space_info;
  4114. fs_info->trans_block_rsv.space_info = space_info;
  4115. fs_info->empty_block_rsv.space_info = space_info;
  4116. fs_info->delayed_block_rsv.space_info = space_info;
  4117. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4118. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4119. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4120. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4121. if (fs_info->quota_root)
  4122. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4123. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4124. update_global_block_rsv(fs_info);
  4125. }
  4126. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4127. {
  4128. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4129. (u64)-1);
  4130. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4131. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4132. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4133. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4134. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4135. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4136. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4137. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4138. }
  4139. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4140. struct btrfs_root *root)
  4141. {
  4142. if (!trans->block_rsv)
  4143. return;
  4144. if (!trans->bytes_reserved)
  4145. return;
  4146. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4147. trans->transid, trans->bytes_reserved, 0);
  4148. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4149. trans->bytes_reserved = 0;
  4150. }
  4151. /* Can only return 0 or -ENOSPC */
  4152. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4153. struct inode *inode)
  4154. {
  4155. struct btrfs_root *root = BTRFS_I(inode)->root;
  4156. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4157. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4158. /*
  4159. * We need to hold space in order to delete our orphan item once we've
  4160. * added it, so this takes the reservation so we can release it later
  4161. * when we are truly done with the orphan item.
  4162. */
  4163. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4164. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4165. btrfs_ino(inode), num_bytes, 1);
  4166. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4167. }
  4168. void btrfs_orphan_release_metadata(struct inode *inode)
  4169. {
  4170. struct btrfs_root *root = BTRFS_I(inode)->root;
  4171. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4172. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4173. btrfs_ino(inode), num_bytes, 0);
  4174. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4175. }
  4176. /*
  4177. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4178. * root: the root of the parent directory
  4179. * rsv: block reservation
  4180. * items: the number of items that we need do reservation
  4181. * qgroup_reserved: used to return the reserved size in qgroup
  4182. *
  4183. * This function is used to reserve the space for snapshot/subvolume
  4184. * creation and deletion. Those operations are different with the
  4185. * common file/directory operations, they change two fs/file trees
  4186. * and root tree, the number of items that the qgroup reserves is
  4187. * different with the free space reservation. So we can not use
  4188. * the space reseravtion mechanism in start_transaction().
  4189. */
  4190. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4191. struct btrfs_block_rsv *rsv,
  4192. int items,
  4193. u64 *qgroup_reserved,
  4194. bool use_global_rsv)
  4195. {
  4196. u64 num_bytes;
  4197. int ret;
  4198. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4199. if (root->fs_info->quota_enabled) {
  4200. /* One for parent inode, two for dir entries */
  4201. num_bytes = 3 * root->leafsize;
  4202. ret = btrfs_qgroup_reserve(root, num_bytes);
  4203. if (ret)
  4204. return ret;
  4205. } else {
  4206. num_bytes = 0;
  4207. }
  4208. *qgroup_reserved = num_bytes;
  4209. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4210. rsv->space_info = __find_space_info(root->fs_info,
  4211. BTRFS_BLOCK_GROUP_METADATA);
  4212. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4213. BTRFS_RESERVE_FLUSH_ALL);
  4214. if (ret == -ENOSPC && use_global_rsv)
  4215. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
  4216. if (ret) {
  4217. if (*qgroup_reserved)
  4218. btrfs_qgroup_free(root, *qgroup_reserved);
  4219. }
  4220. return ret;
  4221. }
  4222. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4223. struct btrfs_block_rsv *rsv,
  4224. u64 qgroup_reserved)
  4225. {
  4226. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4227. if (qgroup_reserved)
  4228. btrfs_qgroup_free(root, qgroup_reserved);
  4229. }
  4230. /**
  4231. * drop_outstanding_extent - drop an outstanding extent
  4232. * @inode: the inode we're dropping the extent for
  4233. *
  4234. * This is called when we are freeing up an outstanding extent, either called
  4235. * after an error or after an extent is written. This will return the number of
  4236. * reserved extents that need to be freed. This must be called with
  4237. * BTRFS_I(inode)->lock held.
  4238. */
  4239. static unsigned drop_outstanding_extent(struct inode *inode)
  4240. {
  4241. unsigned drop_inode_space = 0;
  4242. unsigned dropped_extents = 0;
  4243. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  4244. BTRFS_I(inode)->outstanding_extents--;
  4245. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4246. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4247. &BTRFS_I(inode)->runtime_flags))
  4248. drop_inode_space = 1;
  4249. /*
  4250. * If we have more or the same amount of outsanding extents than we have
  4251. * reserved then we need to leave the reserved extents count alone.
  4252. */
  4253. if (BTRFS_I(inode)->outstanding_extents >=
  4254. BTRFS_I(inode)->reserved_extents)
  4255. return drop_inode_space;
  4256. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4257. BTRFS_I(inode)->outstanding_extents;
  4258. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4259. return dropped_extents + drop_inode_space;
  4260. }
  4261. /**
  4262. * calc_csum_metadata_size - return the amount of metada space that must be
  4263. * reserved/free'd for the given bytes.
  4264. * @inode: the inode we're manipulating
  4265. * @num_bytes: the number of bytes in question
  4266. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4267. *
  4268. * This adjusts the number of csum_bytes in the inode and then returns the
  4269. * correct amount of metadata that must either be reserved or freed. We
  4270. * calculate how many checksums we can fit into one leaf and then divide the
  4271. * number of bytes that will need to be checksumed by this value to figure out
  4272. * how many checksums will be required. If we are adding bytes then the number
  4273. * may go up and we will return the number of additional bytes that must be
  4274. * reserved. If it is going down we will return the number of bytes that must
  4275. * be freed.
  4276. *
  4277. * This must be called with BTRFS_I(inode)->lock held.
  4278. */
  4279. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4280. int reserve)
  4281. {
  4282. struct btrfs_root *root = BTRFS_I(inode)->root;
  4283. u64 csum_size;
  4284. int num_csums_per_leaf;
  4285. int num_csums;
  4286. int old_csums;
  4287. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4288. BTRFS_I(inode)->csum_bytes == 0)
  4289. return 0;
  4290. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4291. if (reserve)
  4292. BTRFS_I(inode)->csum_bytes += num_bytes;
  4293. else
  4294. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4295. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  4296. num_csums_per_leaf = (int)div64_u64(csum_size,
  4297. sizeof(struct btrfs_csum_item) +
  4298. sizeof(struct btrfs_disk_key));
  4299. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4300. num_csums = num_csums + num_csums_per_leaf - 1;
  4301. num_csums = num_csums / num_csums_per_leaf;
  4302. old_csums = old_csums + num_csums_per_leaf - 1;
  4303. old_csums = old_csums / num_csums_per_leaf;
  4304. /* No change, no need to reserve more */
  4305. if (old_csums == num_csums)
  4306. return 0;
  4307. if (reserve)
  4308. return btrfs_calc_trans_metadata_size(root,
  4309. num_csums - old_csums);
  4310. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4311. }
  4312. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4313. {
  4314. struct btrfs_root *root = BTRFS_I(inode)->root;
  4315. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4316. u64 to_reserve = 0;
  4317. u64 csum_bytes;
  4318. unsigned nr_extents = 0;
  4319. int extra_reserve = 0;
  4320. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4321. int ret = 0;
  4322. bool delalloc_lock = true;
  4323. u64 to_free = 0;
  4324. unsigned dropped;
  4325. /* If we are a free space inode we need to not flush since we will be in
  4326. * the middle of a transaction commit. We also don't need the delalloc
  4327. * mutex since we won't race with anybody. We need this mostly to make
  4328. * lockdep shut its filthy mouth.
  4329. */
  4330. if (btrfs_is_free_space_inode(inode)) {
  4331. flush = BTRFS_RESERVE_NO_FLUSH;
  4332. delalloc_lock = false;
  4333. }
  4334. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4335. btrfs_transaction_in_commit(root->fs_info))
  4336. schedule_timeout(1);
  4337. if (delalloc_lock)
  4338. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4339. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4340. spin_lock(&BTRFS_I(inode)->lock);
  4341. BTRFS_I(inode)->outstanding_extents++;
  4342. if (BTRFS_I(inode)->outstanding_extents >
  4343. BTRFS_I(inode)->reserved_extents)
  4344. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4345. BTRFS_I(inode)->reserved_extents;
  4346. /*
  4347. * Add an item to reserve for updating the inode when we complete the
  4348. * delalloc io.
  4349. */
  4350. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4351. &BTRFS_I(inode)->runtime_flags)) {
  4352. nr_extents++;
  4353. extra_reserve = 1;
  4354. }
  4355. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4356. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4357. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4358. spin_unlock(&BTRFS_I(inode)->lock);
  4359. if (root->fs_info->quota_enabled) {
  4360. ret = btrfs_qgroup_reserve(root, num_bytes +
  4361. nr_extents * root->leafsize);
  4362. if (ret)
  4363. goto out_fail;
  4364. }
  4365. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4366. if (unlikely(ret)) {
  4367. if (root->fs_info->quota_enabled)
  4368. btrfs_qgroup_free(root, num_bytes +
  4369. nr_extents * root->leafsize);
  4370. goto out_fail;
  4371. }
  4372. spin_lock(&BTRFS_I(inode)->lock);
  4373. if (extra_reserve) {
  4374. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4375. &BTRFS_I(inode)->runtime_flags);
  4376. nr_extents--;
  4377. }
  4378. BTRFS_I(inode)->reserved_extents += nr_extents;
  4379. spin_unlock(&BTRFS_I(inode)->lock);
  4380. if (delalloc_lock)
  4381. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4382. if (to_reserve)
  4383. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  4384. btrfs_ino(inode), to_reserve, 1);
  4385. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4386. return 0;
  4387. out_fail:
  4388. spin_lock(&BTRFS_I(inode)->lock);
  4389. dropped = drop_outstanding_extent(inode);
  4390. /*
  4391. * If the inodes csum_bytes is the same as the original
  4392. * csum_bytes then we know we haven't raced with any free()ers
  4393. * so we can just reduce our inodes csum bytes and carry on.
  4394. */
  4395. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4396. calc_csum_metadata_size(inode, num_bytes, 0);
  4397. } else {
  4398. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4399. u64 bytes;
  4400. /*
  4401. * This is tricky, but first we need to figure out how much we
  4402. * free'd from any free-ers that occured during this
  4403. * reservation, so we reset ->csum_bytes to the csum_bytes
  4404. * before we dropped our lock, and then call the free for the
  4405. * number of bytes that were freed while we were trying our
  4406. * reservation.
  4407. */
  4408. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4409. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4410. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4411. /*
  4412. * Now we need to see how much we would have freed had we not
  4413. * been making this reservation and our ->csum_bytes were not
  4414. * artificially inflated.
  4415. */
  4416. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4417. bytes = csum_bytes - orig_csum_bytes;
  4418. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4419. /*
  4420. * Now reset ->csum_bytes to what it should be. If bytes is
  4421. * more than to_free then we would have free'd more space had we
  4422. * not had an artificially high ->csum_bytes, so we need to free
  4423. * the remainder. If bytes is the same or less then we don't
  4424. * need to do anything, the other free-ers did the correct
  4425. * thing.
  4426. */
  4427. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4428. if (bytes > to_free)
  4429. to_free = bytes - to_free;
  4430. else
  4431. to_free = 0;
  4432. }
  4433. spin_unlock(&BTRFS_I(inode)->lock);
  4434. if (dropped)
  4435. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4436. if (to_free) {
  4437. btrfs_block_rsv_release(root, block_rsv, to_free);
  4438. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4439. btrfs_ino(inode), to_free, 0);
  4440. }
  4441. if (delalloc_lock)
  4442. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4443. return ret;
  4444. }
  4445. /**
  4446. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4447. * @inode: the inode to release the reservation for
  4448. * @num_bytes: the number of bytes we're releasing
  4449. *
  4450. * This will release the metadata reservation for an inode. This can be called
  4451. * once we complete IO for a given set of bytes to release their metadata
  4452. * reservations.
  4453. */
  4454. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4455. {
  4456. struct btrfs_root *root = BTRFS_I(inode)->root;
  4457. u64 to_free = 0;
  4458. unsigned dropped;
  4459. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4460. spin_lock(&BTRFS_I(inode)->lock);
  4461. dropped = drop_outstanding_extent(inode);
  4462. if (num_bytes)
  4463. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4464. spin_unlock(&BTRFS_I(inode)->lock);
  4465. if (dropped > 0)
  4466. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4467. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4468. btrfs_ino(inode), to_free, 0);
  4469. if (root->fs_info->quota_enabled) {
  4470. btrfs_qgroup_free(root, num_bytes +
  4471. dropped * root->leafsize);
  4472. }
  4473. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4474. to_free);
  4475. }
  4476. /**
  4477. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4478. * @inode: inode we're writing to
  4479. * @num_bytes: the number of bytes we want to allocate
  4480. *
  4481. * This will do the following things
  4482. *
  4483. * o reserve space in the data space info for num_bytes
  4484. * o reserve space in the metadata space info based on number of outstanding
  4485. * extents and how much csums will be needed
  4486. * o add to the inodes ->delalloc_bytes
  4487. * o add it to the fs_info's delalloc inodes list.
  4488. *
  4489. * This will return 0 for success and -ENOSPC if there is no space left.
  4490. */
  4491. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4492. {
  4493. int ret;
  4494. ret = btrfs_check_data_free_space(inode, num_bytes);
  4495. if (ret)
  4496. return ret;
  4497. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4498. if (ret) {
  4499. btrfs_free_reserved_data_space(inode, num_bytes);
  4500. return ret;
  4501. }
  4502. return 0;
  4503. }
  4504. /**
  4505. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4506. * @inode: inode we're releasing space for
  4507. * @num_bytes: the number of bytes we want to free up
  4508. *
  4509. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4510. * called in the case that we don't need the metadata AND data reservations
  4511. * anymore. So if there is an error or we insert an inline extent.
  4512. *
  4513. * This function will release the metadata space that was not used and will
  4514. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4515. * list if there are no delalloc bytes left.
  4516. */
  4517. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4518. {
  4519. btrfs_delalloc_release_metadata(inode, num_bytes);
  4520. btrfs_free_reserved_data_space(inode, num_bytes);
  4521. }
  4522. static int update_block_group(struct btrfs_root *root,
  4523. u64 bytenr, u64 num_bytes, int alloc)
  4524. {
  4525. struct btrfs_block_group_cache *cache = NULL;
  4526. struct btrfs_fs_info *info = root->fs_info;
  4527. u64 total = num_bytes;
  4528. u64 old_val;
  4529. u64 byte_in_group;
  4530. int factor;
  4531. /* block accounting for super block */
  4532. spin_lock(&info->delalloc_root_lock);
  4533. old_val = btrfs_super_bytes_used(info->super_copy);
  4534. if (alloc)
  4535. old_val += num_bytes;
  4536. else
  4537. old_val -= num_bytes;
  4538. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4539. spin_unlock(&info->delalloc_root_lock);
  4540. while (total) {
  4541. cache = btrfs_lookup_block_group(info, bytenr);
  4542. if (!cache)
  4543. return -ENOENT;
  4544. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4545. BTRFS_BLOCK_GROUP_RAID1 |
  4546. BTRFS_BLOCK_GROUP_RAID10))
  4547. factor = 2;
  4548. else
  4549. factor = 1;
  4550. /*
  4551. * If this block group has free space cache written out, we
  4552. * need to make sure to load it if we are removing space. This
  4553. * is because we need the unpinning stage to actually add the
  4554. * space back to the block group, otherwise we will leak space.
  4555. */
  4556. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4557. cache_block_group(cache, 1);
  4558. byte_in_group = bytenr - cache->key.objectid;
  4559. WARN_ON(byte_in_group > cache->key.offset);
  4560. spin_lock(&cache->space_info->lock);
  4561. spin_lock(&cache->lock);
  4562. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4563. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4564. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4565. cache->dirty = 1;
  4566. old_val = btrfs_block_group_used(&cache->item);
  4567. num_bytes = min(total, cache->key.offset - byte_in_group);
  4568. if (alloc) {
  4569. old_val += num_bytes;
  4570. btrfs_set_block_group_used(&cache->item, old_val);
  4571. cache->reserved -= num_bytes;
  4572. cache->space_info->bytes_reserved -= num_bytes;
  4573. cache->space_info->bytes_used += num_bytes;
  4574. cache->space_info->disk_used += num_bytes * factor;
  4575. spin_unlock(&cache->lock);
  4576. spin_unlock(&cache->space_info->lock);
  4577. } else {
  4578. old_val -= num_bytes;
  4579. btrfs_set_block_group_used(&cache->item, old_val);
  4580. cache->pinned += num_bytes;
  4581. cache->space_info->bytes_pinned += num_bytes;
  4582. cache->space_info->bytes_used -= num_bytes;
  4583. cache->space_info->disk_used -= num_bytes * factor;
  4584. spin_unlock(&cache->lock);
  4585. spin_unlock(&cache->space_info->lock);
  4586. set_extent_dirty(info->pinned_extents,
  4587. bytenr, bytenr + num_bytes - 1,
  4588. GFP_NOFS | __GFP_NOFAIL);
  4589. }
  4590. btrfs_put_block_group(cache);
  4591. total -= num_bytes;
  4592. bytenr += num_bytes;
  4593. }
  4594. return 0;
  4595. }
  4596. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4597. {
  4598. struct btrfs_block_group_cache *cache;
  4599. u64 bytenr;
  4600. spin_lock(&root->fs_info->block_group_cache_lock);
  4601. bytenr = root->fs_info->first_logical_byte;
  4602. spin_unlock(&root->fs_info->block_group_cache_lock);
  4603. if (bytenr < (u64)-1)
  4604. return bytenr;
  4605. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4606. if (!cache)
  4607. return 0;
  4608. bytenr = cache->key.objectid;
  4609. btrfs_put_block_group(cache);
  4610. return bytenr;
  4611. }
  4612. static int pin_down_extent(struct btrfs_root *root,
  4613. struct btrfs_block_group_cache *cache,
  4614. u64 bytenr, u64 num_bytes, int reserved)
  4615. {
  4616. spin_lock(&cache->space_info->lock);
  4617. spin_lock(&cache->lock);
  4618. cache->pinned += num_bytes;
  4619. cache->space_info->bytes_pinned += num_bytes;
  4620. if (reserved) {
  4621. cache->reserved -= num_bytes;
  4622. cache->space_info->bytes_reserved -= num_bytes;
  4623. }
  4624. spin_unlock(&cache->lock);
  4625. spin_unlock(&cache->space_info->lock);
  4626. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4627. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4628. return 0;
  4629. }
  4630. /*
  4631. * this function must be called within transaction
  4632. */
  4633. int btrfs_pin_extent(struct btrfs_root *root,
  4634. u64 bytenr, u64 num_bytes, int reserved)
  4635. {
  4636. struct btrfs_block_group_cache *cache;
  4637. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4638. BUG_ON(!cache); /* Logic error */
  4639. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4640. btrfs_put_block_group(cache);
  4641. return 0;
  4642. }
  4643. /*
  4644. * this function must be called within transaction
  4645. */
  4646. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  4647. u64 bytenr, u64 num_bytes)
  4648. {
  4649. struct btrfs_block_group_cache *cache;
  4650. int ret;
  4651. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4652. if (!cache)
  4653. return -EINVAL;
  4654. /*
  4655. * pull in the free space cache (if any) so that our pin
  4656. * removes the free space from the cache. We have load_only set
  4657. * to one because the slow code to read in the free extents does check
  4658. * the pinned extents.
  4659. */
  4660. cache_block_group(cache, 1);
  4661. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4662. /* remove us from the free space cache (if we're there at all) */
  4663. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  4664. btrfs_put_block_group(cache);
  4665. return ret;
  4666. }
  4667. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  4668. {
  4669. int ret;
  4670. struct btrfs_block_group_cache *block_group;
  4671. struct btrfs_caching_control *caching_ctl;
  4672. block_group = btrfs_lookup_block_group(root->fs_info, start);
  4673. if (!block_group)
  4674. return -EINVAL;
  4675. cache_block_group(block_group, 0);
  4676. caching_ctl = get_caching_control(block_group);
  4677. if (!caching_ctl) {
  4678. /* Logic error */
  4679. BUG_ON(!block_group_cache_done(block_group));
  4680. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4681. } else {
  4682. mutex_lock(&caching_ctl->mutex);
  4683. if (start >= caching_ctl->progress) {
  4684. ret = add_excluded_extent(root, start, num_bytes);
  4685. } else if (start + num_bytes <= caching_ctl->progress) {
  4686. ret = btrfs_remove_free_space(block_group,
  4687. start, num_bytes);
  4688. } else {
  4689. num_bytes = caching_ctl->progress - start;
  4690. ret = btrfs_remove_free_space(block_group,
  4691. start, num_bytes);
  4692. if (ret)
  4693. goto out_lock;
  4694. num_bytes = (start + num_bytes) -
  4695. caching_ctl->progress;
  4696. start = caching_ctl->progress;
  4697. ret = add_excluded_extent(root, start, num_bytes);
  4698. }
  4699. out_lock:
  4700. mutex_unlock(&caching_ctl->mutex);
  4701. put_caching_control(caching_ctl);
  4702. }
  4703. btrfs_put_block_group(block_group);
  4704. return ret;
  4705. }
  4706. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  4707. struct extent_buffer *eb)
  4708. {
  4709. struct btrfs_file_extent_item *item;
  4710. struct btrfs_key key;
  4711. int found_type;
  4712. int i;
  4713. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  4714. return 0;
  4715. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  4716. btrfs_item_key_to_cpu(eb, &key, i);
  4717. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4718. continue;
  4719. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  4720. found_type = btrfs_file_extent_type(eb, item);
  4721. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  4722. continue;
  4723. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  4724. continue;
  4725. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  4726. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  4727. __exclude_logged_extent(log, key.objectid, key.offset);
  4728. }
  4729. return 0;
  4730. }
  4731. /**
  4732. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4733. * @cache: The cache we are manipulating
  4734. * @num_bytes: The number of bytes in question
  4735. * @reserve: One of the reservation enums
  4736. *
  4737. * This is called by the allocator when it reserves space, or by somebody who is
  4738. * freeing space that was never actually used on disk. For example if you
  4739. * reserve some space for a new leaf in transaction A and before transaction A
  4740. * commits you free that leaf, you call this with reserve set to 0 in order to
  4741. * clear the reservation.
  4742. *
  4743. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4744. * ENOSPC accounting. For data we handle the reservation through clearing the
  4745. * delalloc bits in the io_tree. We have to do this since we could end up
  4746. * allocating less disk space for the amount of data we have reserved in the
  4747. * case of compression.
  4748. *
  4749. * If this is a reservation and the block group has become read only we cannot
  4750. * make the reservation and return -EAGAIN, otherwise this function always
  4751. * succeeds.
  4752. */
  4753. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4754. u64 num_bytes, int reserve)
  4755. {
  4756. struct btrfs_space_info *space_info = cache->space_info;
  4757. int ret = 0;
  4758. spin_lock(&space_info->lock);
  4759. spin_lock(&cache->lock);
  4760. if (reserve != RESERVE_FREE) {
  4761. if (cache->ro) {
  4762. ret = -EAGAIN;
  4763. } else {
  4764. cache->reserved += num_bytes;
  4765. space_info->bytes_reserved += num_bytes;
  4766. if (reserve == RESERVE_ALLOC) {
  4767. trace_btrfs_space_reservation(cache->fs_info,
  4768. "space_info", space_info->flags,
  4769. num_bytes, 0);
  4770. space_info->bytes_may_use -= num_bytes;
  4771. }
  4772. }
  4773. } else {
  4774. if (cache->ro)
  4775. space_info->bytes_readonly += num_bytes;
  4776. cache->reserved -= num_bytes;
  4777. space_info->bytes_reserved -= num_bytes;
  4778. }
  4779. spin_unlock(&cache->lock);
  4780. spin_unlock(&space_info->lock);
  4781. return ret;
  4782. }
  4783. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4784. struct btrfs_root *root)
  4785. {
  4786. struct btrfs_fs_info *fs_info = root->fs_info;
  4787. struct btrfs_caching_control *next;
  4788. struct btrfs_caching_control *caching_ctl;
  4789. struct btrfs_block_group_cache *cache;
  4790. struct btrfs_space_info *space_info;
  4791. down_write(&fs_info->extent_commit_sem);
  4792. list_for_each_entry_safe(caching_ctl, next,
  4793. &fs_info->caching_block_groups, list) {
  4794. cache = caching_ctl->block_group;
  4795. if (block_group_cache_done(cache)) {
  4796. cache->last_byte_to_unpin = (u64)-1;
  4797. list_del_init(&caching_ctl->list);
  4798. put_caching_control(caching_ctl);
  4799. } else {
  4800. cache->last_byte_to_unpin = caching_ctl->progress;
  4801. }
  4802. }
  4803. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4804. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4805. else
  4806. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4807. up_write(&fs_info->extent_commit_sem);
  4808. list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
  4809. percpu_counter_set(&space_info->total_bytes_pinned, 0);
  4810. update_global_block_rsv(fs_info);
  4811. }
  4812. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4813. {
  4814. struct btrfs_fs_info *fs_info = root->fs_info;
  4815. struct btrfs_block_group_cache *cache = NULL;
  4816. struct btrfs_space_info *space_info;
  4817. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4818. u64 len;
  4819. bool readonly;
  4820. while (start <= end) {
  4821. readonly = false;
  4822. if (!cache ||
  4823. start >= cache->key.objectid + cache->key.offset) {
  4824. if (cache)
  4825. btrfs_put_block_group(cache);
  4826. cache = btrfs_lookup_block_group(fs_info, start);
  4827. BUG_ON(!cache); /* Logic error */
  4828. }
  4829. len = cache->key.objectid + cache->key.offset - start;
  4830. len = min(len, end + 1 - start);
  4831. if (start < cache->last_byte_to_unpin) {
  4832. len = min(len, cache->last_byte_to_unpin - start);
  4833. btrfs_add_free_space(cache, start, len);
  4834. }
  4835. start += len;
  4836. space_info = cache->space_info;
  4837. spin_lock(&space_info->lock);
  4838. spin_lock(&cache->lock);
  4839. cache->pinned -= len;
  4840. space_info->bytes_pinned -= len;
  4841. if (cache->ro) {
  4842. space_info->bytes_readonly += len;
  4843. readonly = true;
  4844. }
  4845. spin_unlock(&cache->lock);
  4846. if (!readonly && global_rsv->space_info == space_info) {
  4847. spin_lock(&global_rsv->lock);
  4848. if (!global_rsv->full) {
  4849. len = min(len, global_rsv->size -
  4850. global_rsv->reserved);
  4851. global_rsv->reserved += len;
  4852. space_info->bytes_may_use += len;
  4853. if (global_rsv->reserved >= global_rsv->size)
  4854. global_rsv->full = 1;
  4855. }
  4856. spin_unlock(&global_rsv->lock);
  4857. }
  4858. spin_unlock(&space_info->lock);
  4859. }
  4860. if (cache)
  4861. btrfs_put_block_group(cache);
  4862. return 0;
  4863. }
  4864. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4865. struct btrfs_root *root)
  4866. {
  4867. struct btrfs_fs_info *fs_info = root->fs_info;
  4868. struct extent_io_tree *unpin;
  4869. u64 start;
  4870. u64 end;
  4871. int ret;
  4872. if (trans->aborted)
  4873. return 0;
  4874. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4875. unpin = &fs_info->freed_extents[1];
  4876. else
  4877. unpin = &fs_info->freed_extents[0];
  4878. while (1) {
  4879. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4880. EXTENT_DIRTY, NULL);
  4881. if (ret)
  4882. break;
  4883. if (btrfs_test_opt(root, DISCARD))
  4884. ret = btrfs_discard_extent(root, start,
  4885. end + 1 - start, NULL);
  4886. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4887. unpin_extent_range(root, start, end);
  4888. cond_resched();
  4889. }
  4890. return 0;
  4891. }
  4892. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
  4893. u64 owner, u64 root_objectid)
  4894. {
  4895. struct btrfs_space_info *space_info;
  4896. u64 flags;
  4897. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4898. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  4899. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  4900. else
  4901. flags = BTRFS_BLOCK_GROUP_METADATA;
  4902. } else {
  4903. flags = BTRFS_BLOCK_GROUP_DATA;
  4904. }
  4905. space_info = __find_space_info(fs_info, flags);
  4906. BUG_ON(!space_info); /* Logic bug */
  4907. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  4908. }
  4909. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4910. struct btrfs_root *root,
  4911. u64 bytenr, u64 num_bytes, u64 parent,
  4912. u64 root_objectid, u64 owner_objectid,
  4913. u64 owner_offset, int refs_to_drop,
  4914. struct btrfs_delayed_extent_op *extent_op)
  4915. {
  4916. struct btrfs_key key;
  4917. struct btrfs_path *path;
  4918. struct btrfs_fs_info *info = root->fs_info;
  4919. struct btrfs_root *extent_root = info->extent_root;
  4920. struct extent_buffer *leaf;
  4921. struct btrfs_extent_item *ei;
  4922. struct btrfs_extent_inline_ref *iref;
  4923. int ret;
  4924. int is_data;
  4925. int extent_slot = 0;
  4926. int found_extent = 0;
  4927. int num_to_del = 1;
  4928. u32 item_size;
  4929. u64 refs;
  4930. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  4931. SKINNY_METADATA);
  4932. path = btrfs_alloc_path();
  4933. if (!path)
  4934. return -ENOMEM;
  4935. path->reada = 1;
  4936. path->leave_spinning = 1;
  4937. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4938. BUG_ON(!is_data && refs_to_drop != 1);
  4939. if (is_data)
  4940. skinny_metadata = 0;
  4941. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4942. bytenr, num_bytes, parent,
  4943. root_objectid, owner_objectid,
  4944. owner_offset);
  4945. if (ret == 0) {
  4946. extent_slot = path->slots[0];
  4947. while (extent_slot >= 0) {
  4948. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4949. extent_slot);
  4950. if (key.objectid != bytenr)
  4951. break;
  4952. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4953. key.offset == num_bytes) {
  4954. found_extent = 1;
  4955. break;
  4956. }
  4957. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  4958. key.offset == owner_objectid) {
  4959. found_extent = 1;
  4960. break;
  4961. }
  4962. if (path->slots[0] - extent_slot > 5)
  4963. break;
  4964. extent_slot--;
  4965. }
  4966. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4967. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4968. if (found_extent && item_size < sizeof(*ei))
  4969. found_extent = 0;
  4970. #endif
  4971. if (!found_extent) {
  4972. BUG_ON(iref);
  4973. ret = remove_extent_backref(trans, extent_root, path,
  4974. NULL, refs_to_drop,
  4975. is_data);
  4976. if (ret) {
  4977. btrfs_abort_transaction(trans, extent_root, ret);
  4978. goto out;
  4979. }
  4980. btrfs_release_path(path);
  4981. path->leave_spinning = 1;
  4982. key.objectid = bytenr;
  4983. key.type = BTRFS_EXTENT_ITEM_KEY;
  4984. key.offset = num_bytes;
  4985. if (!is_data && skinny_metadata) {
  4986. key.type = BTRFS_METADATA_ITEM_KEY;
  4987. key.offset = owner_objectid;
  4988. }
  4989. ret = btrfs_search_slot(trans, extent_root,
  4990. &key, path, -1, 1);
  4991. if (ret > 0 && skinny_metadata && path->slots[0]) {
  4992. /*
  4993. * Couldn't find our skinny metadata item,
  4994. * see if we have ye olde extent item.
  4995. */
  4996. path->slots[0]--;
  4997. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4998. path->slots[0]);
  4999. if (key.objectid == bytenr &&
  5000. key.type == BTRFS_EXTENT_ITEM_KEY &&
  5001. key.offset == num_bytes)
  5002. ret = 0;
  5003. }
  5004. if (ret > 0 && skinny_metadata) {
  5005. skinny_metadata = false;
  5006. key.type = BTRFS_EXTENT_ITEM_KEY;
  5007. key.offset = num_bytes;
  5008. btrfs_release_path(path);
  5009. ret = btrfs_search_slot(trans, extent_root,
  5010. &key, path, -1, 1);
  5011. }
  5012. if (ret) {
  5013. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5014. ret, bytenr);
  5015. if (ret > 0)
  5016. btrfs_print_leaf(extent_root,
  5017. path->nodes[0]);
  5018. }
  5019. if (ret < 0) {
  5020. btrfs_abort_transaction(trans, extent_root, ret);
  5021. goto out;
  5022. }
  5023. extent_slot = path->slots[0];
  5024. }
  5025. } else if (ret == -ENOENT) {
  5026. btrfs_print_leaf(extent_root, path->nodes[0]);
  5027. WARN_ON(1);
  5028. btrfs_err(info,
  5029. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  5030. bytenr, parent, root_objectid, owner_objectid,
  5031. owner_offset);
  5032. } else {
  5033. btrfs_abort_transaction(trans, extent_root, ret);
  5034. goto out;
  5035. }
  5036. leaf = path->nodes[0];
  5037. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5038. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5039. if (item_size < sizeof(*ei)) {
  5040. BUG_ON(found_extent || extent_slot != path->slots[0]);
  5041. ret = convert_extent_item_v0(trans, extent_root, path,
  5042. owner_objectid, 0);
  5043. if (ret < 0) {
  5044. btrfs_abort_transaction(trans, extent_root, ret);
  5045. goto out;
  5046. }
  5047. btrfs_release_path(path);
  5048. path->leave_spinning = 1;
  5049. key.objectid = bytenr;
  5050. key.type = BTRFS_EXTENT_ITEM_KEY;
  5051. key.offset = num_bytes;
  5052. ret = btrfs_search_slot(trans, extent_root, &key, path,
  5053. -1, 1);
  5054. if (ret) {
  5055. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5056. ret, bytenr);
  5057. btrfs_print_leaf(extent_root, path->nodes[0]);
  5058. }
  5059. if (ret < 0) {
  5060. btrfs_abort_transaction(trans, extent_root, ret);
  5061. goto out;
  5062. }
  5063. extent_slot = path->slots[0];
  5064. leaf = path->nodes[0];
  5065. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5066. }
  5067. #endif
  5068. BUG_ON(item_size < sizeof(*ei));
  5069. ei = btrfs_item_ptr(leaf, extent_slot,
  5070. struct btrfs_extent_item);
  5071. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5072. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5073. struct btrfs_tree_block_info *bi;
  5074. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5075. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5076. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5077. }
  5078. refs = btrfs_extent_refs(leaf, ei);
  5079. if (refs < refs_to_drop) {
  5080. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5081. "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
  5082. ret = -EINVAL;
  5083. btrfs_abort_transaction(trans, extent_root, ret);
  5084. goto out;
  5085. }
  5086. refs -= refs_to_drop;
  5087. if (refs > 0) {
  5088. if (extent_op)
  5089. __run_delayed_extent_op(extent_op, leaf, ei);
  5090. /*
  5091. * In the case of inline back ref, reference count will
  5092. * be updated by remove_extent_backref
  5093. */
  5094. if (iref) {
  5095. BUG_ON(!found_extent);
  5096. } else {
  5097. btrfs_set_extent_refs(leaf, ei, refs);
  5098. btrfs_mark_buffer_dirty(leaf);
  5099. }
  5100. if (found_extent) {
  5101. ret = remove_extent_backref(trans, extent_root, path,
  5102. iref, refs_to_drop,
  5103. is_data);
  5104. if (ret) {
  5105. btrfs_abort_transaction(trans, extent_root, ret);
  5106. goto out;
  5107. }
  5108. }
  5109. add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
  5110. root_objectid);
  5111. } else {
  5112. if (found_extent) {
  5113. BUG_ON(is_data && refs_to_drop !=
  5114. extent_data_ref_count(root, path, iref));
  5115. if (iref) {
  5116. BUG_ON(path->slots[0] != extent_slot);
  5117. } else {
  5118. BUG_ON(path->slots[0] != extent_slot + 1);
  5119. path->slots[0] = extent_slot;
  5120. num_to_del = 2;
  5121. }
  5122. }
  5123. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5124. num_to_del);
  5125. if (ret) {
  5126. btrfs_abort_transaction(trans, extent_root, ret);
  5127. goto out;
  5128. }
  5129. btrfs_release_path(path);
  5130. if (is_data) {
  5131. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5132. if (ret) {
  5133. btrfs_abort_transaction(trans, extent_root, ret);
  5134. goto out;
  5135. }
  5136. }
  5137. ret = update_block_group(root, bytenr, num_bytes, 0);
  5138. if (ret) {
  5139. btrfs_abort_transaction(trans, extent_root, ret);
  5140. goto out;
  5141. }
  5142. }
  5143. out:
  5144. btrfs_free_path(path);
  5145. return ret;
  5146. }
  5147. /*
  5148. * when we free an block, it is possible (and likely) that we free the last
  5149. * delayed ref for that extent as well. This searches the delayed ref tree for
  5150. * a given extent, and if there are no other delayed refs to be processed, it
  5151. * removes it from the tree.
  5152. */
  5153. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5154. struct btrfs_root *root, u64 bytenr)
  5155. {
  5156. struct btrfs_delayed_ref_head *head;
  5157. struct btrfs_delayed_ref_root *delayed_refs;
  5158. struct btrfs_delayed_ref_node *ref;
  5159. struct rb_node *node;
  5160. int ret = 0;
  5161. delayed_refs = &trans->transaction->delayed_refs;
  5162. spin_lock(&delayed_refs->lock);
  5163. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5164. if (!head)
  5165. goto out;
  5166. node = rb_prev(&head->node.rb_node);
  5167. if (!node)
  5168. goto out;
  5169. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  5170. /* there are still entries for this ref, we can't drop it */
  5171. if (ref->bytenr == bytenr)
  5172. goto out;
  5173. if (head->extent_op) {
  5174. if (!head->must_insert_reserved)
  5175. goto out;
  5176. btrfs_free_delayed_extent_op(head->extent_op);
  5177. head->extent_op = NULL;
  5178. }
  5179. /*
  5180. * waiting for the lock here would deadlock. If someone else has it
  5181. * locked they are already in the process of dropping it anyway
  5182. */
  5183. if (!mutex_trylock(&head->mutex))
  5184. goto out;
  5185. /*
  5186. * at this point we have a head with no other entries. Go
  5187. * ahead and process it.
  5188. */
  5189. head->node.in_tree = 0;
  5190. rb_erase(&head->node.rb_node, &delayed_refs->root);
  5191. delayed_refs->num_entries--;
  5192. /*
  5193. * we don't take a ref on the node because we're removing it from the
  5194. * tree, so we just steal the ref the tree was holding.
  5195. */
  5196. delayed_refs->num_heads--;
  5197. if (list_empty(&head->cluster))
  5198. delayed_refs->num_heads_ready--;
  5199. list_del_init(&head->cluster);
  5200. spin_unlock(&delayed_refs->lock);
  5201. BUG_ON(head->extent_op);
  5202. if (head->must_insert_reserved)
  5203. ret = 1;
  5204. mutex_unlock(&head->mutex);
  5205. btrfs_put_delayed_ref(&head->node);
  5206. return ret;
  5207. out:
  5208. spin_unlock(&delayed_refs->lock);
  5209. return 0;
  5210. }
  5211. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5212. struct btrfs_root *root,
  5213. struct extent_buffer *buf,
  5214. u64 parent, int last_ref)
  5215. {
  5216. struct btrfs_block_group_cache *cache = NULL;
  5217. int pin = 1;
  5218. int ret;
  5219. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5220. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5221. buf->start, buf->len,
  5222. parent, root->root_key.objectid,
  5223. btrfs_header_level(buf),
  5224. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5225. BUG_ON(ret); /* -ENOMEM */
  5226. }
  5227. if (!last_ref)
  5228. return;
  5229. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5230. if (btrfs_header_generation(buf) == trans->transid) {
  5231. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5232. ret = check_ref_cleanup(trans, root, buf->start);
  5233. if (!ret)
  5234. goto out;
  5235. }
  5236. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5237. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5238. goto out;
  5239. }
  5240. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5241. btrfs_add_free_space(cache, buf->start, buf->len);
  5242. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  5243. pin = 0;
  5244. }
  5245. out:
  5246. if (pin)
  5247. add_pinned_bytes(root->fs_info, buf->len,
  5248. btrfs_header_level(buf),
  5249. root->root_key.objectid);
  5250. /*
  5251. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5252. * anymore.
  5253. */
  5254. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5255. btrfs_put_block_group(cache);
  5256. }
  5257. /* Can return -ENOMEM */
  5258. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5259. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5260. u64 owner, u64 offset, int for_cow)
  5261. {
  5262. int ret;
  5263. struct btrfs_fs_info *fs_info = root->fs_info;
  5264. add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
  5265. /*
  5266. * tree log blocks never actually go into the extent allocation
  5267. * tree, just update pinning info and exit early.
  5268. */
  5269. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5270. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5271. /* unlocks the pinned mutex */
  5272. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5273. ret = 0;
  5274. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5275. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5276. num_bytes,
  5277. parent, root_objectid, (int)owner,
  5278. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  5279. } else {
  5280. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5281. num_bytes,
  5282. parent, root_objectid, owner,
  5283. offset, BTRFS_DROP_DELAYED_REF,
  5284. NULL, for_cow);
  5285. }
  5286. return ret;
  5287. }
  5288. static u64 stripe_align(struct btrfs_root *root,
  5289. struct btrfs_block_group_cache *cache,
  5290. u64 val, u64 num_bytes)
  5291. {
  5292. u64 ret = ALIGN(val, root->stripesize);
  5293. return ret;
  5294. }
  5295. /*
  5296. * when we wait for progress in the block group caching, its because
  5297. * our allocation attempt failed at least once. So, we must sleep
  5298. * and let some progress happen before we try again.
  5299. *
  5300. * This function will sleep at least once waiting for new free space to
  5301. * show up, and then it will check the block group free space numbers
  5302. * for our min num_bytes. Another option is to have it go ahead
  5303. * and look in the rbtree for a free extent of a given size, but this
  5304. * is a good start.
  5305. *
  5306. * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
  5307. * any of the information in this block group.
  5308. */
  5309. static noinline void
  5310. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5311. u64 num_bytes)
  5312. {
  5313. struct btrfs_caching_control *caching_ctl;
  5314. caching_ctl = get_caching_control(cache);
  5315. if (!caching_ctl)
  5316. return;
  5317. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5318. (cache->free_space_ctl->free_space >= num_bytes));
  5319. put_caching_control(caching_ctl);
  5320. }
  5321. static noinline int
  5322. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5323. {
  5324. struct btrfs_caching_control *caching_ctl;
  5325. int ret = 0;
  5326. caching_ctl = get_caching_control(cache);
  5327. if (!caching_ctl)
  5328. return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
  5329. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5330. if (cache->cached == BTRFS_CACHE_ERROR)
  5331. ret = -EIO;
  5332. put_caching_control(caching_ctl);
  5333. return ret;
  5334. }
  5335. int __get_raid_index(u64 flags)
  5336. {
  5337. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5338. return BTRFS_RAID_RAID10;
  5339. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5340. return BTRFS_RAID_RAID1;
  5341. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5342. return BTRFS_RAID_DUP;
  5343. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5344. return BTRFS_RAID_RAID0;
  5345. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5346. return BTRFS_RAID_RAID5;
  5347. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5348. return BTRFS_RAID_RAID6;
  5349. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5350. }
  5351. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  5352. {
  5353. return __get_raid_index(cache->flags);
  5354. }
  5355. enum btrfs_loop_type {
  5356. LOOP_CACHING_NOWAIT = 0,
  5357. LOOP_CACHING_WAIT = 1,
  5358. LOOP_ALLOC_CHUNK = 2,
  5359. LOOP_NO_EMPTY_SIZE = 3,
  5360. };
  5361. /*
  5362. * walks the btree of allocated extents and find a hole of a given size.
  5363. * The key ins is changed to record the hole:
  5364. * ins->objectid == start position
  5365. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5366. * ins->offset == the size of the hole.
  5367. * Any available blocks before search_start are skipped.
  5368. *
  5369. * If there is no suitable free space, we will record the max size of
  5370. * the free space extent currently.
  5371. */
  5372. static noinline int find_free_extent(struct btrfs_root *orig_root,
  5373. u64 num_bytes, u64 empty_size,
  5374. u64 hint_byte, struct btrfs_key *ins,
  5375. u64 flags)
  5376. {
  5377. int ret = 0;
  5378. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5379. struct btrfs_free_cluster *last_ptr = NULL;
  5380. struct btrfs_block_group_cache *block_group = NULL;
  5381. struct btrfs_block_group_cache *used_block_group;
  5382. u64 search_start = 0;
  5383. u64 max_extent_size = 0;
  5384. int empty_cluster = 2 * 1024 * 1024;
  5385. struct btrfs_space_info *space_info;
  5386. int loop = 0;
  5387. int index = __get_raid_index(flags);
  5388. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5389. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5390. bool found_uncached_bg = false;
  5391. bool failed_cluster_refill = false;
  5392. bool failed_alloc = false;
  5393. bool use_cluster = true;
  5394. bool have_caching_bg = false;
  5395. WARN_ON(num_bytes < root->sectorsize);
  5396. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  5397. ins->objectid = 0;
  5398. ins->offset = 0;
  5399. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5400. space_info = __find_space_info(root->fs_info, flags);
  5401. if (!space_info) {
  5402. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5403. return -ENOSPC;
  5404. }
  5405. /*
  5406. * If the space info is for both data and metadata it means we have a
  5407. * small filesystem and we can't use the clustering stuff.
  5408. */
  5409. if (btrfs_mixed_space_info(space_info))
  5410. use_cluster = false;
  5411. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5412. last_ptr = &root->fs_info->meta_alloc_cluster;
  5413. if (!btrfs_test_opt(root, SSD))
  5414. empty_cluster = 64 * 1024;
  5415. }
  5416. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5417. btrfs_test_opt(root, SSD)) {
  5418. last_ptr = &root->fs_info->data_alloc_cluster;
  5419. }
  5420. if (last_ptr) {
  5421. spin_lock(&last_ptr->lock);
  5422. if (last_ptr->block_group)
  5423. hint_byte = last_ptr->window_start;
  5424. spin_unlock(&last_ptr->lock);
  5425. }
  5426. search_start = max(search_start, first_logical_byte(root, 0));
  5427. search_start = max(search_start, hint_byte);
  5428. if (!last_ptr)
  5429. empty_cluster = 0;
  5430. if (search_start == hint_byte) {
  5431. block_group = btrfs_lookup_block_group(root->fs_info,
  5432. search_start);
  5433. used_block_group = block_group;
  5434. /*
  5435. * we don't want to use the block group if it doesn't match our
  5436. * allocation bits, or if its not cached.
  5437. *
  5438. * However if we are re-searching with an ideal block group
  5439. * picked out then we don't care that the block group is cached.
  5440. */
  5441. if (block_group && block_group_bits(block_group, flags) &&
  5442. block_group->cached != BTRFS_CACHE_NO) {
  5443. down_read(&space_info->groups_sem);
  5444. if (list_empty(&block_group->list) ||
  5445. block_group->ro) {
  5446. /*
  5447. * someone is removing this block group,
  5448. * we can't jump into the have_block_group
  5449. * target because our list pointers are not
  5450. * valid
  5451. */
  5452. btrfs_put_block_group(block_group);
  5453. up_read(&space_info->groups_sem);
  5454. } else {
  5455. index = get_block_group_index(block_group);
  5456. goto have_block_group;
  5457. }
  5458. } else if (block_group) {
  5459. btrfs_put_block_group(block_group);
  5460. }
  5461. }
  5462. search:
  5463. have_caching_bg = false;
  5464. down_read(&space_info->groups_sem);
  5465. list_for_each_entry(block_group, &space_info->block_groups[index],
  5466. list) {
  5467. u64 offset;
  5468. int cached;
  5469. used_block_group = block_group;
  5470. btrfs_get_block_group(block_group);
  5471. search_start = block_group->key.objectid;
  5472. /*
  5473. * this can happen if we end up cycling through all the
  5474. * raid types, but we want to make sure we only allocate
  5475. * for the proper type.
  5476. */
  5477. if (!block_group_bits(block_group, flags)) {
  5478. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  5479. BTRFS_BLOCK_GROUP_RAID1 |
  5480. BTRFS_BLOCK_GROUP_RAID5 |
  5481. BTRFS_BLOCK_GROUP_RAID6 |
  5482. BTRFS_BLOCK_GROUP_RAID10;
  5483. /*
  5484. * if they asked for extra copies and this block group
  5485. * doesn't provide them, bail. This does allow us to
  5486. * fill raid0 from raid1.
  5487. */
  5488. if ((flags & extra) && !(block_group->flags & extra))
  5489. goto loop;
  5490. }
  5491. have_block_group:
  5492. cached = block_group_cache_done(block_group);
  5493. if (unlikely(!cached)) {
  5494. found_uncached_bg = true;
  5495. ret = cache_block_group(block_group, 0);
  5496. BUG_ON(ret < 0);
  5497. ret = 0;
  5498. }
  5499. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
  5500. goto loop;
  5501. if (unlikely(block_group->ro))
  5502. goto loop;
  5503. /*
  5504. * Ok we want to try and use the cluster allocator, so
  5505. * lets look there
  5506. */
  5507. if (last_ptr) {
  5508. unsigned long aligned_cluster;
  5509. /*
  5510. * the refill lock keeps out other
  5511. * people trying to start a new cluster
  5512. */
  5513. spin_lock(&last_ptr->refill_lock);
  5514. used_block_group = last_ptr->block_group;
  5515. if (used_block_group != block_group &&
  5516. (!used_block_group ||
  5517. used_block_group->ro ||
  5518. !block_group_bits(used_block_group, flags))) {
  5519. used_block_group = block_group;
  5520. goto refill_cluster;
  5521. }
  5522. if (used_block_group != block_group)
  5523. btrfs_get_block_group(used_block_group);
  5524. offset = btrfs_alloc_from_cluster(used_block_group,
  5525. last_ptr,
  5526. num_bytes,
  5527. used_block_group->key.objectid,
  5528. &max_extent_size);
  5529. if (offset) {
  5530. /* we have a block, we're done */
  5531. spin_unlock(&last_ptr->refill_lock);
  5532. trace_btrfs_reserve_extent_cluster(root,
  5533. block_group, search_start, num_bytes);
  5534. goto checks;
  5535. }
  5536. WARN_ON(last_ptr->block_group != used_block_group);
  5537. if (used_block_group != block_group) {
  5538. btrfs_put_block_group(used_block_group);
  5539. used_block_group = block_group;
  5540. }
  5541. refill_cluster:
  5542. BUG_ON(used_block_group != block_group);
  5543. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5544. * set up a new clusters, so lets just skip it
  5545. * and let the allocator find whatever block
  5546. * it can find. If we reach this point, we
  5547. * will have tried the cluster allocator
  5548. * plenty of times and not have found
  5549. * anything, so we are likely way too
  5550. * fragmented for the clustering stuff to find
  5551. * anything.
  5552. *
  5553. * However, if the cluster is taken from the
  5554. * current block group, release the cluster
  5555. * first, so that we stand a better chance of
  5556. * succeeding in the unclustered
  5557. * allocation. */
  5558. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5559. last_ptr->block_group != block_group) {
  5560. spin_unlock(&last_ptr->refill_lock);
  5561. goto unclustered_alloc;
  5562. }
  5563. /*
  5564. * this cluster didn't work out, free it and
  5565. * start over
  5566. */
  5567. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5568. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5569. spin_unlock(&last_ptr->refill_lock);
  5570. goto unclustered_alloc;
  5571. }
  5572. aligned_cluster = max_t(unsigned long,
  5573. empty_cluster + empty_size,
  5574. block_group->full_stripe_len);
  5575. /* allocate a cluster in this block group */
  5576. ret = btrfs_find_space_cluster(root, block_group,
  5577. last_ptr, search_start,
  5578. num_bytes,
  5579. aligned_cluster);
  5580. if (ret == 0) {
  5581. /*
  5582. * now pull our allocation out of this
  5583. * cluster
  5584. */
  5585. offset = btrfs_alloc_from_cluster(block_group,
  5586. last_ptr,
  5587. num_bytes,
  5588. search_start,
  5589. &max_extent_size);
  5590. if (offset) {
  5591. /* we found one, proceed */
  5592. spin_unlock(&last_ptr->refill_lock);
  5593. trace_btrfs_reserve_extent_cluster(root,
  5594. block_group, search_start,
  5595. num_bytes);
  5596. goto checks;
  5597. }
  5598. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5599. && !failed_cluster_refill) {
  5600. spin_unlock(&last_ptr->refill_lock);
  5601. failed_cluster_refill = true;
  5602. wait_block_group_cache_progress(block_group,
  5603. num_bytes + empty_cluster + empty_size);
  5604. goto have_block_group;
  5605. }
  5606. /*
  5607. * at this point we either didn't find a cluster
  5608. * or we weren't able to allocate a block from our
  5609. * cluster. Free the cluster we've been trying
  5610. * to use, and go to the next block group
  5611. */
  5612. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5613. spin_unlock(&last_ptr->refill_lock);
  5614. goto loop;
  5615. }
  5616. unclustered_alloc:
  5617. spin_lock(&block_group->free_space_ctl->tree_lock);
  5618. if (cached &&
  5619. block_group->free_space_ctl->free_space <
  5620. num_bytes + empty_cluster + empty_size) {
  5621. if (block_group->free_space_ctl->free_space >
  5622. max_extent_size)
  5623. max_extent_size =
  5624. block_group->free_space_ctl->free_space;
  5625. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5626. goto loop;
  5627. }
  5628. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5629. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5630. num_bytes, empty_size,
  5631. &max_extent_size);
  5632. /*
  5633. * If we didn't find a chunk, and we haven't failed on this
  5634. * block group before, and this block group is in the middle of
  5635. * caching and we are ok with waiting, then go ahead and wait
  5636. * for progress to be made, and set failed_alloc to true.
  5637. *
  5638. * If failed_alloc is true then we've already waited on this
  5639. * block group once and should move on to the next block group.
  5640. */
  5641. if (!offset && !failed_alloc && !cached &&
  5642. loop > LOOP_CACHING_NOWAIT) {
  5643. wait_block_group_cache_progress(block_group,
  5644. num_bytes + empty_size);
  5645. failed_alloc = true;
  5646. goto have_block_group;
  5647. } else if (!offset) {
  5648. if (!cached)
  5649. have_caching_bg = true;
  5650. goto loop;
  5651. }
  5652. checks:
  5653. search_start = stripe_align(root, used_block_group,
  5654. offset, num_bytes);
  5655. /* move on to the next group */
  5656. if (search_start + num_bytes >
  5657. used_block_group->key.objectid + used_block_group->key.offset) {
  5658. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5659. goto loop;
  5660. }
  5661. if (offset < search_start)
  5662. btrfs_add_free_space(used_block_group, offset,
  5663. search_start - offset);
  5664. BUG_ON(offset > search_start);
  5665. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5666. alloc_type);
  5667. if (ret == -EAGAIN) {
  5668. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5669. goto loop;
  5670. }
  5671. /* we are all good, lets return */
  5672. ins->objectid = search_start;
  5673. ins->offset = num_bytes;
  5674. trace_btrfs_reserve_extent(orig_root, block_group,
  5675. search_start, num_bytes);
  5676. if (used_block_group != block_group)
  5677. btrfs_put_block_group(used_block_group);
  5678. btrfs_put_block_group(block_group);
  5679. break;
  5680. loop:
  5681. failed_cluster_refill = false;
  5682. failed_alloc = false;
  5683. BUG_ON(index != get_block_group_index(block_group));
  5684. if (used_block_group != block_group)
  5685. btrfs_put_block_group(used_block_group);
  5686. btrfs_put_block_group(block_group);
  5687. }
  5688. up_read(&space_info->groups_sem);
  5689. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5690. goto search;
  5691. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5692. goto search;
  5693. /*
  5694. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5695. * caching kthreads as we move along
  5696. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5697. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5698. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5699. * again
  5700. */
  5701. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5702. index = 0;
  5703. loop++;
  5704. if (loop == LOOP_ALLOC_CHUNK) {
  5705. struct btrfs_trans_handle *trans;
  5706. trans = btrfs_join_transaction(root);
  5707. if (IS_ERR(trans)) {
  5708. ret = PTR_ERR(trans);
  5709. goto out;
  5710. }
  5711. ret = do_chunk_alloc(trans, root, flags,
  5712. CHUNK_ALLOC_FORCE);
  5713. /*
  5714. * Do not bail out on ENOSPC since we
  5715. * can do more things.
  5716. */
  5717. if (ret < 0 && ret != -ENOSPC)
  5718. btrfs_abort_transaction(trans,
  5719. root, ret);
  5720. else
  5721. ret = 0;
  5722. btrfs_end_transaction(trans, root);
  5723. if (ret)
  5724. goto out;
  5725. }
  5726. if (loop == LOOP_NO_EMPTY_SIZE) {
  5727. empty_size = 0;
  5728. empty_cluster = 0;
  5729. }
  5730. goto search;
  5731. } else if (!ins->objectid) {
  5732. ret = -ENOSPC;
  5733. } else if (ins->objectid) {
  5734. ret = 0;
  5735. }
  5736. out:
  5737. if (ret == -ENOSPC)
  5738. ins->offset = max_extent_size;
  5739. return ret;
  5740. }
  5741. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5742. int dump_block_groups)
  5743. {
  5744. struct btrfs_block_group_cache *cache;
  5745. int index = 0;
  5746. spin_lock(&info->lock);
  5747. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5748. info->flags,
  5749. info->total_bytes - info->bytes_used - info->bytes_pinned -
  5750. info->bytes_reserved - info->bytes_readonly,
  5751. (info->full) ? "" : "not ");
  5752. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5753. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5754. info->total_bytes, info->bytes_used, info->bytes_pinned,
  5755. info->bytes_reserved, info->bytes_may_use,
  5756. info->bytes_readonly);
  5757. spin_unlock(&info->lock);
  5758. if (!dump_block_groups)
  5759. return;
  5760. down_read(&info->groups_sem);
  5761. again:
  5762. list_for_each_entry(cache, &info->block_groups[index], list) {
  5763. spin_lock(&cache->lock);
  5764. printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
  5765. cache->key.objectid, cache->key.offset,
  5766. btrfs_block_group_used(&cache->item), cache->pinned,
  5767. cache->reserved, cache->ro ? "[readonly]" : "");
  5768. btrfs_dump_free_space(cache, bytes);
  5769. spin_unlock(&cache->lock);
  5770. }
  5771. if (++index < BTRFS_NR_RAID_TYPES)
  5772. goto again;
  5773. up_read(&info->groups_sem);
  5774. }
  5775. int btrfs_reserve_extent(struct btrfs_root *root,
  5776. u64 num_bytes, u64 min_alloc_size,
  5777. u64 empty_size, u64 hint_byte,
  5778. struct btrfs_key *ins, int is_data)
  5779. {
  5780. bool final_tried = false;
  5781. u64 flags;
  5782. int ret;
  5783. flags = btrfs_get_alloc_profile(root, is_data);
  5784. again:
  5785. WARN_ON(num_bytes < root->sectorsize);
  5786. ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
  5787. flags);
  5788. if (ret == -ENOSPC) {
  5789. if (!final_tried && ins->offset) {
  5790. num_bytes = min(num_bytes >> 1, ins->offset);
  5791. num_bytes = round_down(num_bytes, root->sectorsize);
  5792. num_bytes = max(num_bytes, min_alloc_size);
  5793. if (num_bytes == min_alloc_size)
  5794. final_tried = true;
  5795. goto again;
  5796. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5797. struct btrfs_space_info *sinfo;
  5798. sinfo = __find_space_info(root->fs_info, flags);
  5799. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  5800. flags, num_bytes);
  5801. if (sinfo)
  5802. dump_space_info(sinfo, num_bytes, 1);
  5803. }
  5804. }
  5805. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5806. return ret;
  5807. }
  5808. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5809. u64 start, u64 len, int pin)
  5810. {
  5811. struct btrfs_block_group_cache *cache;
  5812. int ret = 0;
  5813. cache = btrfs_lookup_block_group(root->fs_info, start);
  5814. if (!cache) {
  5815. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  5816. start);
  5817. return -ENOSPC;
  5818. }
  5819. if (btrfs_test_opt(root, DISCARD))
  5820. ret = btrfs_discard_extent(root, start, len, NULL);
  5821. if (pin)
  5822. pin_down_extent(root, cache, start, len, 1);
  5823. else {
  5824. btrfs_add_free_space(cache, start, len);
  5825. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5826. }
  5827. btrfs_put_block_group(cache);
  5828. trace_btrfs_reserved_extent_free(root, start, len);
  5829. return ret;
  5830. }
  5831. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5832. u64 start, u64 len)
  5833. {
  5834. return __btrfs_free_reserved_extent(root, start, len, 0);
  5835. }
  5836. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5837. u64 start, u64 len)
  5838. {
  5839. return __btrfs_free_reserved_extent(root, start, len, 1);
  5840. }
  5841. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5842. struct btrfs_root *root,
  5843. u64 parent, u64 root_objectid,
  5844. u64 flags, u64 owner, u64 offset,
  5845. struct btrfs_key *ins, int ref_mod)
  5846. {
  5847. int ret;
  5848. struct btrfs_fs_info *fs_info = root->fs_info;
  5849. struct btrfs_extent_item *extent_item;
  5850. struct btrfs_extent_inline_ref *iref;
  5851. struct btrfs_path *path;
  5852. struct extent_buffer *leaf;
  5853. int type;
  5854. u32 size;
  5855. if (parent > 0)
  5856. type = BTRFS_SHARED_DATA_REF_KEY;
  5857. else
  5858. type = BTRFS_EXTENT_DATA_REF_KEY;
  5859. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5860. path = btrfs_alloc_path();
  5861. if (!path)
  5862. return -ENOMEM;
  5863. path->leave_spinning = 1;
  5864. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5865. ins, size);
  5866. if (ret) {
  5867. btrfs_free_path(path);
  5868. return ret;
  5869. }
  5870. leaf = path->nodes[0];
  5871. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5872. struct btrfs_extent_item);
  5873. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5874. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5875. btrfs_set_extent_flags(leaf, extent_item,
  5876. flags | BTRFS_EXTENT_FLAG_DATA);
  5877. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5878. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5879. if (parent > 0) {
  5880. struct btrfs_shared_data_ref *ref;
  5881. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5882. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5883. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5884. } else {
  5885. struct btrfs_extent_data_ref *ref;
  5886. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5887. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5888. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5889. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5890. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5891. }
  5892. btrfs_mark_buffer_dirty(path->nodes[0]);
  5893. btrfs_free_path(path);
  5894. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5895. if (ret) { /* -ENOENT, logic error */
  5896. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5897. ins->objectid, ins->offset);
  5898. BUG();
  5899. }
  5900. return ret;
  5901. }
  5902. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5903. struct btrfs_root *root,
  5904. u64 parent, u64 root_objectid,
  5905. u64 flags, struct btrfs_disk_key *key,
  5906. int level, struct btrfs_key *ins)
  5907. {
  5908. int ret;
  5909. struct btrfs_fs_info *fs_info = root->fs_info;
  5910. struct btrfs_extent_item *extent_item;
  5911. struct btrfs_tree_block_info *block_info;
  5912. struct btrfs_extent_inline_ref *iref;
  5913. struct btrfs_path *path;
  5914. struct extent_buffer *leaf;
  5915. u32 size = sizeof(*extent_item) + sizeof(*iref);
  5916. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5917. SKINNY_METADATA);
  5918. if (!skinny_metadata)
  5919. size += sizeof(*block_info);
  5920. path = btrfs_alloc_path();
  5921. if (!path)
  5922. return -ENOMEM;
  5923. path->leave_spinning = 1;
  5924. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5925. ins, size);
  5926. if (ret) {
  5927. btrfs_free_path(path);
  5928. return ret;
  5929. }
  5930. leaf = path->nodes[0];
  5931. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5932. struct btrfs_extent_item);
  5933. btrfs_set_extent_refs(leaf, extent_item, 1);
  5934. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5935. btrfs_set_extent_flags(leaf, extent_item,
  5936. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5937. if (skinny_metadata) {
  5938. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5939. } else {
  5940. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5941. btrfs_set_tree_block_key(leaf, block_info, key);
  5942. btrfs_set_tree_block_level(leaf, block_info, level);
  5943. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5944. }
  5945. if (parent > 0) {
  5946. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5947. btrfs_set_extent_inline_ref_type(leaf, iref,
  5948. BTRFS_SHARED_BLOCK_REF_KEY);
  5949. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5950. } else {
  5951. btrfs_set_extent_inline_ref_type(leaf, iref,
  5952. BTRFS_TREE_BLOCK_REF_KEY);
  5953. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5954. }
  5955. btrfs_mark_buffer_dirty(leaf);
  5956. btrfs_free_path(path);
  5957. ret = update_block_group(root, ins->objectid, root->leafsize, 1);
  5958. if (ret) { /* -ENOENT, logic error */
  5959. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5960. ins->objectid, ins->offset);
  5961. BUG();
  5962. }
  5963. return ret;
  5964. }
  5965. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5966. struct btrfs_root *root,
  5967. u64 root_objectid, u64 owner,
  5968. u64 offset, struct btrfs_key *ins)
  5969. {
  5970. int ret;
  5971. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5972. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5973. ins->offset, 0,
  5974. root_objectid, owner, offset,
  5975. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5976. return ret;
  5977. }
  5978. /*
  5979. * this is used by the tree logging recovery code. It records that
  5980. * an extent has been allocated and makes sure to clear the free
  5981. * space cache bits as well
  5982. */
  5983. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  5984. struct btrfs_root *root,
  5985. u64 root_objectid, u64 owner, u64 offset,
  5986. struct btrfs_key *ins)
  5987. {
  5988. int ret;
  5989. struct btrfs_block_group_cache *block_group;
  5990. /*
  5991. * Mixed block groups will exclude before processing the log so we only
  5992. * need to do the exlude dance if this fs isn't mixed.
  5993. */
  5994. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  5995. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  5996. if (ret)
  5997. return ret;
  5998. }
  5999. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  6000. if (!block_group)
  6001. return -EINVAL;
  6002. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  6003. RESERVE_ALLOC_NO_ACCOUNT);
  6004. BUG_ON(ret); /* logic error */
  6005. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  6006. 0, owner, offset, ins, 1);
  6007. btrfs_put_block_group(block_group);
  6008. return ret;
  6009. }
  6010. static struct extent_buffer *
  6011. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  6012. u64 bytenr, u32 blocksize, int level)
  6013. {
  6014. struct extent_buffer *buf;
  6015. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6016. if (!buf)
  6017. return ERR_PTR(-ENOMEM);
  6018. btrfs_set_header_generation(buf, trans->transid);
  6019. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  6020. btrfs_tree_lock(buf);
  6021. clean_tree_block(trans, root, buf);
  6022. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  6023. btrfs_set_lock_blocking(buf);
  6024. btrfs_set_buffer_uptodate(buf);
  6025. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  6026. /*
  6027. * we allow two log transactions at a time, use different
  6028. * EXENT bit to differentiate dirty pages.
  6029. */
  6030. if (root->log_transid % 2 == 0)
  6031. set_extent_dirty(&root->dirty_log_pages, buf->start,
  6032. buf->start + buf->len - 1, GFP_NOFS);
  6033. else
  6034. set_extent_new(&root->dirty_log_pages, buf->start,
  6035. buf->start + buf->len - 1, GFP_NOFS);
  6036. } else {
  6037. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  6038. buf->start + buf->len - 1, GFP_NOFS);
  6039. }
  6040. trans->blocks_used++;
  6041. /* this returns a buffer locked for blocking */
  6042. return buf;
  6043. }
  6044. static struct btrfs_block_rsv *
  6045. use_block_rsv(struct btrfs_trans_handle *trans,
  6046. struct btrfs_root *root, u32 blocksize)
  6047. {
  6048. struct btrfs_block_rsv *block_rsv;
  6049. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  6050. int ret;
  6051. bool global_updated = false;
  6052. block_rsv = get_block_rsv(trans, root);
  6053. if (unlikely(block_rsv->size == 0))
  6054. goto try_reserve;
  6055. again:
  6056. ret = block_rsv_use_bytes(block_rsv, blocksize);
  6057. if (!ret)
  6058. return block_rsv;
  6059. if (block_rsv->failfast)
  6060. return ERR_PTR(ret);
  6061. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  6062. global_updated = true;
  6063. update_global_block_rsv(root->fs_info);
  6064. goto again;
  6065. }
  6066. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6067. static DEFINE_RATELIMIT_STATE(_rs,
  6068. DEFAULT_RATELIMIT_INTERVAL * 10,
  6069. /*DEFAULT_RATELIMIT_BURST*/ 1);
  6070. if (__ratelimit(&_rs))
  6071. WARN(1, KERN_DEBUG
  6072. "btrfs: block rsv returned %d\n", ret);
  6073. }
  6074. try_reserve:
  6075. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  6076. BTRFS_RESERVE_NO_FLUSH);
  6077. if (!ret)
  6078. return block_rsv;
  6079. /*
  6080. * If we couldn't reserve metadata bytes try and use some from
  6081. * the global reserve if its space type is the same as the global
  6082. * reservation.
  6083. */
  6084. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  6085. block_rsv->space_info == global_rsv->space_info) {
  6086. ret = block_rsv_use_bytes(global_rsv, blocksize);
  6087. if (!ret)
  6088. return global_rsv;
  6089. }
  6090. return ERR_PTR(ret);
  6091. }
  6092. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6093. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6094. {
  6095. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6096. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6097. }
  6098. /*
  6099. * finds a free extent and does all the dirty work required for allocation
  6100. * returns the key for the extent through ins, and a tree buffer for
  6101. * the first block of the extent through buf.
  6102. *
  6103. * returns the tree buffer or NULL.
  6104. */
  6105. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  6106. struct btrfs_root *root, u32 blocksize,
  6107. u64 parent, u64 root_objectid,
  6108. struct btrfs_disk_key *key, int level,
  6109. u64 hint, u64 empty_size)
  6110. {
  6111. struct btrfs_key ins;
  6112. struct btrfs_block_rsv *block_rsv;
  6113. struct extent_buffer *buf;
  6114. u64 flags = 0;
  6115. int ret;
  6116. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6117. SKINNY_METADATA);
  6118. block_rsv = use_block_rsv(trans, root, blocksize);
  6119. if (IS_ERR(block_rsv))
  6120. return ERR_CAST(block_rsv);
  6121. ret = btrfs_reserve_extent(root, blocksize, blocksize,
  6122. empty_size, hint, &ins, 0);
  6123. if (ret) {
  6124. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6125. return ERR_PTR(ret);
  6126. }
  6127. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  6128. blocksize, level);
  6129. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  6130. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6131. if (parent == 0)
  6132. parent = ins.objectid;
  6133. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6134. } else
  6135. BUG_ON(parent > 0);
  6136. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6137. struct btrfs_delayed_extent_op *extent_op;
  6138. extent_op = btrfs_alloc_delayed_extent_op();
  6139. BUG_ON(!extent_op); /* -ENOMEM */
  6140. if (key)
  6141. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6142. else
  6143. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6144. extent_op->flags_to_set = flags;
  6145. if (skinny_metadata)
  6146. extent_op->update_key = 0;
  6147. else
  6148. extent_op->update_key = 1;
  6149. extent_op->update_flags = 1;
  6150. extent_op->is_data = 0;
  6151. extent_op->level = level;
  6152. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6153. ins.objectid,
  6154. ins.offset, parent, root_objectid,
  6155. level, BTRFS_ADD_DELAYED_EXTENT,
  6156. extent_op, 0);
  6157. BUG_ON(ret); /* -ENOMEM */
  6158. }
  6159. return buf;
  6160. }
  6161. struct walk_control {
  6162. u64 refs[BTRFS_MAX_LEVEL];
  6163. u64 flags[BTRFS_MAX_LEVEL];
  6164. struct btrfs_key update_progress;
  6165. int stage;
  6166. int level;
  6167. int shared_level;
  6168. int update_ref;
  6169. int keep_locks;
  6170. int reada_slot;
  6171. int reada_count;
  6172. int for_reloc;
  6173. };
  6174. #define DROP_REFERENCE 1
  6175. #define UPDATE_BACKREF 2
  6176. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6177. struct btrfs_root *root,
  6178. struct walk_control *wc,
  6179. struct btrfs_path *path)
  6180. {
  6181. u64 bytenr;
  6182. u64 generation;
  6183. u64 refs;
  6184. u64 flags;
  6185. u32 nritems;
  6186. u32 blocksize;
  6187. struct btrfs_key key;
  6188. struct extent_buffer *eb;
  6189. int ret;
  6190. int slot;
  6191. int nread = 0;
  6192. if (path->slots[wc->level] < wc->reada_slot) {
  6193. wc->reada_count = wc->reada_count * 2 / 3;
  6194. wc->reada_count = max(wc->reada_count, 2);
  6195. } else {
  6196. wc->reada_count = wc->reada_count * 3 / 2;
  6197. wc->reada_count = min_t(int, wc->reada_count,
  6198. BTRFS_NODEPTRS_PER_BLOCK(root));
  6199. }
  6200. eb = path->nodes[wc->level];
  6201. nritems = btrfs_header_nritems(eb);
  6202. blocksize = btrfs_level_size(root, wc->level - 1);
  6203. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6204. if (nread >= wc->reada_count)
  6205. break;
  6206. cond_resched();
  6207. bytenr = btrfs_node_blockptr(eb, slot);
  6208. generation = btrfs_node_ptr_generation(eb, slot);
  6209. if (slot == path->slots[wc->level])
  6210. goto reada;
  6211. if (wc->stage == UPDATE_BACKREF &&
  6212. generation <= root->root_key.offset)
  6213. continue;
  6214. /* We don't lock the tree block, it's OK to be racy here */
  6215. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6216. wc->level - 1, 1, &refs,
  6217. &flags);
  6218. /* We don't care about errors in readahead. */
  6219. if (ret < 0)
  6220. continue;
  6221. BUG_ON(refs == 0);
  6222. if (wc->stage == DROP_REFERENCE) {
  6223. if (refs == 1)
  6224. goto reada;
  6225. if (wc->level == 1 &&
  6226. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6227. continue;
  6228. if (!wc->update_ref ||
  6229. generation <= root->root_key.offset)
  6230. continue;
  6231. btrfs_node_key_to_cpu(eb, &key, slot);
  6232. ret = btrfs_comp_cpu_keys(&key,
  6233. &wc->update_progress);
  6234. if (ret < 0)
  6235. continue;
  6236. } else {
  6237. if (wc->level == 1 &&
  6238. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6239. continue;
  6240. }
  6241. reada:
  6242. ret = readahead_tree_block(root, bytenr, blocksize,
  6243. generation);
  6244. if (ret)
  6245. break;
  6246. nread++;
  6247. }
  6248. wc->reada_slot = slot;
  6249. }
  6250. /*
  6251. * helper to process tree block while walking down the tree.
  6252. *
  6253. * when wc->stage == UPDATE_BACKREF, this function updates
  6254. * back refs for pointers in the block.
  6255. *
  6256. * NOTE: return value 1 means we should stop walking down.
  6257. */
  6258. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  6259. struct btrfs_root *root,
  6260. struct btrfs_path *path,
  6261. struct walk_control *wc, int lookup_info)
  6262. {
  6263. int level = wc->level;
  6264. struct extent_buffer *eb = path->nodes[level];
  6265. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6266. int ret;
  6267. if (wc->stage == UPDATE_BACKREF &&
  6268. btrfs_header_owner(eb) != root->root_key.objectid)
  6269. return 1;
  6270. /*
  6271. * when reference count of tree block is 1, it won't increase
  6272. * again. once full backref flag is set, we never clear it.
  6273. */
  6274. if (lookup_info &&
  6275. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  6276. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  6277. BUG_ON(!path->locks[level]);
  6278. ret = btrfs_lookup_extent_info(trans, root,
  6279. eb->start, level, 1,
  6280. &wc->refs[level],
  6281. &wc->flags[level]);
  6282. BUG_ON(ret == -ENOMEM);
  6283. if (ret)
  6284. return ret;
  6285. BUG_ON(wc->refs[level] == 0);
  6286. }
  6287. if (wc->stage == DROP_REFERENCE) {
  6288. if (wc->refs[level] > 1)
  6289. return 1;
  6290. if (path->locks[level] && !wc->keep_locks) {
  6291. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6292. path->locks[level] = 0;
  6293. }
  6294. return 0;
  6295. }
  6296. /* wc->stage == UPDATE_BACKREF */
  6297. if (!(wc->flags[level] & flag)) {
  6298. BUG_ON(!path->locks[level]);
  6299. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  6300. BUG_ON(ret); /* -ENOMEM */
  6301. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  6302. BUG_ON(ret); /* -ENOMEM */
  6303. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  6304. eb->len, flag,
  6305. btrfs_header_level(eb), 0);
  6306. BUG_ON(ret); /* -ENOMEM */
  6307. wc->flags[level] |= flag;
  6308. }
  6309. /*
  6310. * the block is shared by multiple trees, so it's not good to
  6311. * keep the tree lock
  6312. */
  6313. if (path->locks[level] && level > 0) {
  6314. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6315. path->locks[level] = 0;
  6316. }
  6317. return 0;
  6318. }
  6319. /*
  6320. * helper to process tree block pointer.
  6321. *
  6322. * when wc->stage == DROP_REFERENCE, this function checks
  6323. * reference count of the block pointed to. if the block
  6324. * is shared and we need update back refs for the subtree
  6325. * rooted at the block, this function changes wc->stage to
  6326. * UPDATE_BACKREF. if the block is shared and there is no
  6327. * need to update back, this function drops the reference
  6328. * to the block.
  6329. *
  6330. * NOTE: return value 1 means we should stop walking down.
  6331. */
  6332. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  6333. struct btrfs_root *root,
  6334. struct btrfs_path *path,
  6335. struct walk_control *wc, int *lookup_info)
  6336. {
  6337. u64 bytenr;
  6338. u64 generation;
  6339. u64 parent;
  6340. u32 blocksize;
  6341. struct btrfs_key key;
  6342. struct extent_buffer *next;
  6343. int level = wc->level;
  6344. int reada = 0;
  6345. int ret = 0;
  6346. generation = btrfs_node_ptr_generation(path->nodes[level],
  6347. path->slots[level]);
  6348. /*
  6349. * if the lower level block was created before the snapshot
  6350. * was created, we know there is no need to update back refs
  6351. * for the subtree
  6352. */
  6353. if (wc->stage == UPDATE_BACKREF &&
  6354. generation <= root->root_key.offset) {
  6355. *lookup_info = 1;
  6356. return 1;
  6357. }
  6358. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  6359. blocksize = btrfs_level_size(root, level - 1);
  6360. next = btrfs_find_tree_block(root, bytenr, blocksize);
  6361. if (!next) {
  6362. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6363. if (!next)
  6364. return -ENOMEM;
  6365. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  6366. level - 1);
  6367. reada = 1;
  6368. }
  6369. btrfs_tree_lock(next);
  6370. btrfs_set_lock_blocking(next);
  6371. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  6372. &wc->refs[level - 1],
  6373. &wc->flags[level - 1]);
  6374. if (ret < 0) {
  6375. btrfs_tree_unlock(next);
  6376. return ret;
  6377. }
  6378. if (unlikely(wc->refs[level - 1] == 0)) {
  6379. btrfs_err(root->fs_info, "Missing references.");
  6380. BUG();
  6381. }
  6382. *lookup_info = 0;
  6383. if (wc->stage == DROP_REFERENCE) {
  6384. if (wc->refs[level - 1] > 1) {
  6385. if (level == 1 &&
  6386. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6387. goto skip;
  6388. if (!wc->update_ref ||
  6389. generation <= root->root_key.offset)
  6390. goto skip;
  6391. btrfs_node_key_to_cpu(path->nodes[level], &key,
  6392. path->slots[level]);
  6393. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  6394. if (ret < 0)
  6395. goto skip;
  6396. wc->stage = UPDATE_BACKREF;
  6397. wc->shared_level = level - 1;
  6398. }
  6399. } else {
  6400. if (level == 1 &&
  6401. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6402. goto skip;
  6403. }
  6404. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  6405. btrfs_tree_unlock(next);
  6406. free_extent_buffer(next);
  6407. next = NULL;
  6408. *lookup_info = 1;
  6409. }
  6410. if (!next) {
  6411. if (reada && level == 1)
  6412. reada_walk_down(trans, root, wc, path);
  6413. next = read_tree_block(root, bytenr, blocksize, generation);
  6414. if (!next || !extent_buffer_uptodate(next)) {
  6415. free_extent_buffer(next);
  6416. return -EIO;
  6417. }
  6418. btrfs_tree_lock(next);
  6419. btrfs_set_lock_blocking(next);
  6420. }
  6421. level--;
  6422. BUG_ON(level != btrfs_header_level(next));
  6423. path->nodes[level] = next;
  6424. path->slots[level] = 0;
  6425. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6426. wc->level = level;
  6427. if (wc->level == 1)
  6428. wc->reada_slot = 0;
  6429. return 0;
  6430. skip:
  6431. wc->refs[level - 1] = 0;
  6432. wc->flags[level - 1] = 0;
  6433. if (wc->stage == DROP_REFERENCE) {
  6434. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  6435. parent = path->nodes[level]->start;
  6436. } else {
  6437. BUG_ON(root->root_key.objectid !=
  6438. btrfs_header_owner(path->nodes[level]));
  6439. parent = 0;
  6440. }
  6441. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  6442. root->root_key.objectid, level - 1, 0, 0);
  6443. BUG_ON(ret); /* -ENOMEM */
  6444. }
  6445. btrfs_tree_unlock(next);
  6446. free_extent_buffer(next);
  6447. *lookup_info = 1;
  6448. return 1;
  6449. }
  6450. /*
  6451. * helper to process tree block while walking up the tree.
  6452. *
  6453. * when wc->stage == DROP_REFERENCE, this function drops
  6454. * reference count on the block.
  6455. *
  6456. * when wc->stage == UPDATE_BACKREF, this function changes
  6457. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  6458. * to UPDATE_BACKREF previously while processing the block.
  6459. *
  6460. * NOTE: return value 1 means we should stop walking up.
  6461. */
  6462. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  6463. struct btrfs_root *root,
  6464. struct btrfs_path *path,
  6465. struct walk_control *wc)
  6466. {
  6467. int ret;
  6468. int level = wc->level;
  6469. struct extent_buffer *eb = path->nodes[level];
  6470. u64 parent = 0;
  6471. if (wc->stage == UPDATE_BACKREF) {
  6472. BUG_ON(wc->shared_level < level);
  6473. if (level < wc->shared_level)
  6474. goto out;
  6475. ret = find_next_key(path, level + 1, &wc->update_progress);
  6476. if (ret > 0)
  6477. wc->update_ref = 0;
  6478. wc->stage = DROP_REFERENCE;
  6479. wc->shared_level = -1;
  6480. path->slots[level] = 0;
  6481. /*
  6482. * check reference count again if the block isn't locked.
  6483. * we should start walking down the tree again if reference
  6484. * count is one.
  6485. */
  6486. if (!path->locks[level]) {
  6487. BUG_ON(level == 0);
  6488. btrfs_tree_lock(eb);
  6489. btrfs_set_lock_blocking(eb);
  6490. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6491. ret = btrfs_lookup_extent_info(trans, root,
  6492. eb->start, level, 1,
  6493. &wc->refs[level],
  6494. &wc->flags[level]);
  6495. if (ret < 0) {
  6496. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6497. path->locks[level] = 0;
  6498. return ret;
  6499. }
  6500. BUG_ON(wc->refs[level] == 0);
  6501. if (wc->refs[level] == 1) {
  6502. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6503. path->locks[level] = 0;
  6504. return 1;
  6505. }
  6506. }
  6507. }
  6508. /* wc->stage == DROP_REFERENCE */
  6509. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6510. if (wc->refs[level] == 1) {
  6511. if (level == 0) {
  6512. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6513. ret = btrfs_dec_ref(trans, root, eb, 1,
  6514. wc->for_reloc);
  6515. else
  6516. ret = btrfs_dec_ref(trans, root, eb, 0,
  6517. wc->for_reloc);
  6518. BUG_ON(ret); /* -ENOMEM */
  6519. }
  6520. /* make block locked assertion in clean_tree_block happy */
  6521. if (!path->locks[level] &&
  6522. btrfs_header_generation(eb) == trans->transid) {
  6523. btrfs_tree_lock(eb);
  6524. btrfs_set_lock_blocking(eb);
  6525. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6526. }
  6527. clean_tree_block(trans, root, eb);
  6528. }
  6529. if (eb == root->node) {
  6530. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6531. parent = eb->start;
  6532. else
  6533. BUG_ON(root->root_key.objectid !=
  6534. btrfs_header_owner(eb));
  6535. } else {
  6536. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6537. parent = path->nodes[level + 1]->start;
  6538. else
  6539. BUG_ON(root->root_key.objectid !=
  6540. btrfs_header_owner(path->nodes[level + 1]));
  6541. }
  6542. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6543. out:
  6544. wc->refs[level] = 0;
  6545. wc->flags[level] = 0;
  6546. return 0;
  6547. }
  6548. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6549. struct btrfs_root *root,
  6550. struct btrfs_path *path,
  6551. struct walk_control *wc)
  6552. {
  6553. int level = wc->level;
  6554. int lookup_info = 1;
  6555. int ret;
  6556. while (level >= 0) {
  6557. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6558. if (ret > 0)
  6559. break;
  6560. if (level == 0)
  6561. break;
  6562. if (path->slots[level] >=
  6563. btrfs_header_nritems(path->nodes[level]))
  6564. break;
  6565. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6566. if (ret > 0) {
  6567. path->slots[level]++;
  6568. continue;
  6569. } else if (ret < 0)
  6570. return ret;
  6571. level = wc->level;
  6572. }
  6573. return 0;
  6574. }
  6575. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6576. struct btrfs_root *root,
  6577. struct btrfs_path *path,
  6578. struct walk_control *wc, int max_level)
  6579. {
  6580. int level = wc->level;
  6581. int ret;
  6582. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6583. while (level < max_level && path->nodes[level]) {
  6584. wc->level = level;
  6585. if (path->slots[level] + 1 <
  6586. btrfs_header_nritems(path->nodes[level])) {
  6587. path->slots[level]++;
  6588. return 0;
  6589. } else {
  6590. ret = walk_up_proc(trans, root, path, wc);
  6591. if (ret > 0)
  6592. return 0;
  6593. if (path->locks[level]) {
  6594. btrfs_tree_unlock_rw(path->nodes[level],
  6595. path->locks[level]);
  6596. path->locks[level] = 0;
  6597. }
  6598. free_extent_buffer(path->nodes[level]);
  6599. path->nodes[level] = NULL;
  6600. level++;
  6601. }
  6602. }
  6603. return 1;
  6604. }
  6605. /*
  6606. * drop a subvolume tree.
  6607. *
  6608. * this function traverses the tree freeing any blocks that only
  6609. * referenced by the tree.
  6610. *
  6611. * when a shared tree block is found. this function decreases its
  6612. * reference count by one. if update_ref is true, this function
  6613. * also make sure backrefs for the shared block and all lower level
  6614. * blocks are properly updated.
  6615. *
  6616. * If called with for_reloc == 0, may exit early with -EAGAIN
  6617. */
  6618. int btrfs_drop_snapshot(struct btrfs_root *root,
  6619. struct btrfs_block_rsv *block_rsv, int update_ref,
  6620. int for_reloc)
  6621. {
  6622. struct btrfs_path *path;
  6623. struct btrfs_trans_handle *trans;
  6624. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6625. struct btrfs_root_item *root_item = &root->root_item;
  6626. struct walk_control *wc;
  6627. struct btrfs_key key;
  6628. int err = 0;
  6629. int ret;
  6630. int level;
  6631. bool root_dropped = false;
  6632. path = btrfs_alloc_path();
  6633. if (!path) {
  6634. err = -ENOMEM;
  6635. goto out;
  6636. }
  6637. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6638. if (!wc) {
  6639. btrfs_free_path(path);
  6640. err = -ENOMEM;
  6641. goto out;
  6642. }
  6643. trans = btrfs_start_transaction(tree_root, 0);
  6644. if (IS_ERR(trans)) {
  6645. err = PTR_ERR(trans);
  6646. goto out_free;
  6647. }
  6648. if (block_rsv)
  6649. trans->block_rsv = block_rsv;
  6650. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6651. level = btrfs_header_level(root->node);
  6652. path->nodes[level] = btrfs_lock_root_node(root);
  6653. btrfs_set_lock_blocking(path->nodes[level]);
  6654. path->slots[level] = 0;
  6655. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6656. memset(&wc->update_progress, 0,
  6657. sizeof(wc->update_progress));
  6658. } else {
  6659. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6660. memcpy(&wc->update_progress, &key,
  6661. sizeof(wc->update_progress));
  6662. level = root_item->drop_level;
  6663. BUG_ON(level == 0);
  6664. path->lowest_level = level;
  6665. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6666. path->lowest_level = 0;
  6667. if (ret < 0) {
  6668. err = ret;
  6669. goto out_end_trans;
  6670. }
  6671. WARN_ON(ret > 0);
  6672. /*
  6673. * unlock our path, this is safe because only this
  6674. * function is allowed to delete this snapshot
  6675. */
  6676. btrfs_unlock_up_safe(path, 0);
  6677. level = btrfs_header_level(root->node);
  6678. while (1) {
  6679. btrfs_tree_lock(path->nodes[level]);
  6680. btrfs_set_lock_blocking(path->nodes[level]);
  6681. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6682. ret = btrfs_lookup_extent_info(trans, root,
  6683. path->nodes[level]->start,
  6684. level, 1, &wc->refs[level],
  6685. &wc->flags[level]);
  6686. if (ret < 0) {
  6687. err = ret;
  6688. goto out_end_trans;
  6689. }
  6690. BUG_ON(wc->refs[level] == 0);
  6691. if (level == root_item->drop_level)
  6692. break;
  6693. btrfs_tree_unlock(path->nodes[level]);
  6694. path->locks[level] = 0;
  6695. WARN_ON(wc->refs[level] != 1);
  6696. level--;
  6697. }
  6698. }
  6699. wc->level = level;
  6700. wc->shared_level = -1;
  6701. wc->stage = DROP_REFERENCE;
  6702. wc->update_ref = update_ref;
  6703. wc->keep_locks = 0;
  6704. wc->for_reloc = for_reloc;
  6705. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6706. while (1) {
  6707. ret = walk_down_tree(trans, root, path, wc);
  6708. if (ret < 0) {
  6709. err = ret;
  6710. break;
  6711. }
  6712. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6713. if (ret < 0) {
  6714. err = ret;
  6715. break;
  6716. }
  6717. if (ret > 0) {
  6718. BUG_ON(wc->stage != DROP_REFERENCE);
  6719. break;
  6720. }
  6721. if (wc->stage == DROP_REFERENCE) {
  6722. level = wc->level;
  6723. btrfs_node_key(path->nodes[level],
  6724. &root_item->drop_progress,
  6725. path->slots[level]);
  6726. root_item->drop_level = level;
  6727. }
  6728. BUG_ON(wc->level == 0);
  6729. if (btrfs_should_end_transaction(trans, tree_root) ||
  6730. (!for_reloc && btrfs_need_cleaner_sleep(root))) {
  6731. ret = btrfs_update_root(trans, tree_root,
  6732. &root->root_key,
  6733. root_item);
  6734. if (ret) {
  6735. btrfs_abort_transaction(trans, tree_root, ret);
  6736. err = ret;
  6737. goto out_end_trans;
  6738. }
  6739. btrfs_end_transaction_throttle(trans, tree_root);
  6740. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  6741. pr_debug("btrfs: drop snapshot early exit\n");
  6742. err = -EAGAIN;
  6743. goto out_free;
  6744. }
  6745. trans = btrfs_start_transaction(tree_root, 0);
  6746. if (IS_ERR(trans)) {
  6747. err = PTR_ERR(trans);
  6748. goto out_free;
  6749. }
  6750. if (block_rsv)
  6751. trans->block_rsv = block_rsv;
  6752. }
  6753. }
  6754. btrfs_release_path(path);
  6755. if (err)
  6756. goto out_end_trans;
  6757. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6758. if (ret) {
  6759. btrfs_abort_transaction(trans, tree_root, ret);
  6760. goto out_end_trans;
  6761. }
  6762. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6763. ret = btrfs_find_root(tree_root, &root->root_key, path,
  6764. NULL, NULL);
  6765. if (ret < 0) {
  6766. btrfs_abort_transaction(trans, tree_root, ret);
  6767. err = ret;
  6768. goto out_end_trans;
  6769. } else if (ret > 0) {
  6770. /* if we fail to delete the orphan item this time
  6771. * around, it'll get picked up the next time.
  6772. *
  6773. * The most common failure here is just -ENOENT.
  6774. */
  6775. btrfs_del_orphan_item(trans, tree_root,
  6776. root->root_key.objectid);
  6777. }
  6778. }
  6779. if (root->in_radix) {
  6780. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  6781. } else {
  6782. free_extent_buffer(root->node);
  6783. free_extent_buffer(root->commit_root);
  6784. btrfs_put_fs_root(root);
  6785. }
  6786. root_dropped = true;
  6787. out_end_trans:
  6788. btrfs_end_transaction_throttle(trans, tree_root);
  6789. out_free:
  6790. kfree(wc);
  6791. btrfs_free_path(path);
  6792. out:
  6793. /*
  6794. * So if we need to stop dropping the snapshot for whatever reason we
  6795. * need to make sure to add it back to the dead root list so that we
  6796. * keep trying to do the work later. This also cleans up roots if we
  6797. * don't have it in the radix (like when we recover after a power fail
  6798. * or unmount) so we don't leak memory.
  6799. */
  6800. if (!for_reloc && root_dropped == false)
  6801. btrfs_add_dead_root(root);
  6802. if (err)
  6803. btrfs_std_error(root->fs_info, err);
  6804. return err;
  6805. }
  6806. /*
  6807. * drop subtree rooted at tree block 'node'.
  6808. *
  6809. * NOTE: this function will unlock and release tree block 'node'
  6810. * only used by relocation code
  6811. */
  6812. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6813. struct btrfs_root *root,
  6814. struct extent_buffer *node,
  6815. struct extent_buffer *parent)
  6816. {
  6817. struct btrfs_path *path;
  6818. struct walk_control *wc;
  6819. int level;
  6820. int parent_level;
  6821. int ret = 0;
  6822. int wret;
  6823. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6824. path = btrfs_alloc_path();
  6825. if (!path)
  6826. return -ENOMEM;
  6827. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6828. if (!wc) {
  6829. btrfs_free_path(path);
  6830. return -ENOMEM;
  6831. }
  6832. btrfs_assert_tree_locked(parent);
  6833. parent_level = btrfs_header_level(parent);
  6834. extent_buffer_get(parent);
  6835. path->nodes[parent_level] = parent;
  6836. path->slots[parent_level] = btrfs_header_nritems(parent);
  6837. btrfs_assert_tree_locked(node);
  6838. level = btrfs_header_level(node);
  6839. path->nodes[level] = node;
  6840. path->slots[level] = 0;
  6841. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6842. wc->refs[parent_level] = 1;
  6843. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6844. wc->level = level;
  6845. wc->shared_level = -1;
  6846. wc->stage = DROP_REFERENCE;
  6847. wc->update_ref = 0;
  6848. wc->keep_locks = 1;
  6849. wc->for_reloc = 1;
  6850. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6851. while (1) {
  6852. wret = walk_down_tree(trans, root, path, wc);
  6853. if (wret < 0) {
  6854. ret = wret;
  6855. break;
  6856. }
  6857. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6858. if (wret < 0)
  6859. ret = wret;
  6860. if (wret != 0)
  6861. break;
  6862. }
  6863. kfree(wc);
  6864. btrfs_free_path(path);
  6865. return ret;
  6866. }
  6867. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6868. {
  6869. u64 num_devices;
  6870. u64 stripped;
  6871. /*
  6872. * if restripe for this chunk_type is on pick target profile and
  6873. * return, otherwise do the usual balance
  6874. */
  6875. stripped = get_restripe_target(root->fs_info, flags);
  6876. if (stripped)
  6877. return extended_to_chunk(stripped);
  6878. /*
  6879. * we add in the count of missing devices because we want
  6880. * to make sure that any RAID levels on a degraded FS
  6881. * continue to be honored.
  6882. */
  6883. num_devices = root->fs_info->fs_devices->rw_devices +
  6884. root->fs_info->fs_devices->missing_devices;
  6885. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6886. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  6887. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6888. if (num_devices == 1) {
  6889. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6890. stripped = flags & ~stripped;
  6891. /* turn raid0 into single device chunks */
  6892. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6893. return stripped;
  6894. /* turn mirroring into duplication */
  6895. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6896. BTRFS_BLOCK_GROUP_RAID10))
  6897. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6898. } else {
  6899. /* they already had raid on here, just return */
  6900. if (flags & stripped)
  6901. return flags;
  6902. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6903. stripped = flags & ~stripped;
  6904. /* switch duplicated blocks with raid1 */
  6905. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6906. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6907. /* this is drive concat, leave it alone */
  6908. }
  6909. return flags;
  6910. }
  6911. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6912. {
  6913. struct btrfs_space_info *sinfo = cache->space_info;
  6914. u64 num_bytes;
  6915. u64 min_allocable_bytes;
  6916. int ret = -ENOSPC;
  6917. /*
  6918. * We need some metadata space and system metadata space for
  6919. * allocating chunks in some corner cases until we force to set
  6920. * it to be readonly.
  6921. */
  6922. if ((sinfo->flags &
  6923. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6924. !force)
  6925. min_allocable_bytes = 1 * 1024 * 1024;
  6926. else
  6927. min_allocable_bytes = 0;
  6928. spin_lock(&sinfo->lock);
  6929. spin_lock(&cache->lock);
  6930. if (cache->ro) {
  6931. ret = 0;
  6932. goto out;
  6933. }
  6934. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6935. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6936. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6937. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6938. min_allocable_bytes <= sinfo->total_bytes) {
  6939. sinfo->bytes_readonly += num_bytes;
  6940. cache->ro = 1;
  6941. ret = 0;
  6942. }
  6943. out:
  6944. spin_unlock(&cache->lock);
  6945. spin_unlock(&sinfo->lock);
  6946. return ret;
  6947. }
  6948. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6949. struct btrfs_block_group_cache *cache)
  6950. {
  6951. struct btrfs_trans_handle *trans;
  6952. u64 alloc_flags;
  6953. int ret;
  6954. BUG_ON(cache->ro);
  6955. trans = btrfs_join_transaction(root);
  6956. if (IS_ERR(trans))
  6957. return PTR_ERR(trans);
  6958. alloc_flags = update_block_group_flags(root, cache->flags);
  6959. if (alloc_flags != cache->flags) {
  6960. ret = do_chunk_alloc(trans, root, alloc_flags,
  6961. CHUNK_ALLOC_FORCE);
  6962. if (ret < 0)
  6963. goto out;
  6964. }
  6965. ret = set_block_group_ro(cache, 0);
  6966. if (!ret)
  6967. goto out;
  6968. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6969. ret = do_chunk_alloc(trans, root, alloc_flags,
  6970. CHUNK_ALLOC_FORCE);
  6971. if (ret < 0)
  6972. goto out;
  6973. ret = set_block_group_ro(cache, 0);
  6974. out:
  6975. btrfs_end_transaction(trans, root);
  6976. return ret;
  6977. }
  6978. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6979. struct btrfs_root *root, u64 type)
  6980. {
  6981. u64 alloc_flags = get_alloc_profile(root, type);
  6982. return do_chunk_alloc(trans, root, alloc_flags,
  6983. CHUNK_ALLOC_FORCE);
  6984. }
  6985. /*
  6986. * helper to account the unused space of all the readonly block group in the
  6987. * list. takes mirrors into account.
  6988. */
  6989. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6990. {
  6991. struct btrfs_block_group_cache *block_group;
  6992. u64 free_bytes = 0;
  6993. int factor;
  6994. list_for_each_entry(block_group, groups_list, list) {
  6995. spin_lock(&block_group->lock);
  6996. if (!block_group->ro) {
  6997. spin_unlock(&block_group->lock);
  6998. continue;
  6999. }
  7000. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7001. BTRFS_BLOCK_GROUP_RAID10 |
  7002. BTRFS_BLOCK_GROUP_DUP))
  7003. factor = 2;
  7004. else
  7005. factor = 1;
  7006. free_bytes += (block_group->key.offset -
  7007. btrfs_block_group_used(&block_group->item)) *
  7008. factor;
  7009. spin_unlock(&block_group->lock);
  7010. }
  7011. return free_bytes;
  7012. }
  7013. /*
  7014. * helper to account the unused space of all the readonly block group in the
  7015. * space_info. takes mirrors into account.
  7016. */
  7017. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  7018. {
  7019. int i;
  7020. u64 free_bytes = 0;
  7021. spin_lock(&sinfo->lock);
  7022. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  7023. if (!list_empty(&sinfo->block_groups[i]))
  7024. free_bytes += __btrfs_get_ro_block_group_free_space(
  7025. &sinfo->block_groups[i]);
  7026. spin_unlock(&sinfo->lock);
  7027. return free_bytes;
  7028. }
  7029. void btrfs_set_block_group_rw(struct btrfs_root *root,
  7030. struct btrfs_block_group_cache *cache)
  7031. {
  7032. struct btrfs_space_info *sinfo = cache->space_info;
  7033. u64 num_bytes;
  7034. BUG_ON(!cache->ro);
  7035. spin_lock(&sinfo->lock);
  7036. spin_lock(&cache->lock);
  7037. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7038. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7039. sinfo->bytes_readonly -= num_bytes;
  7040. cache->ro = 0;
  7041. spin_unlock(&cache->lock);
  7042. spin_unlock(&sinfo->lock);
  7043. }
  7044. /*
  7045. * checks to see if its even possible to relocate this block group.
  7046. *
  7047. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7048. * ok to go ahead and try.
  7049. */
  7050. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7051. {
  7052. struct btrfs_block_group_cache *block_group;
  7053. struct btrfs_space_info *space_info;
  7054. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7055. struct btrfs_device *device;
  7056. struct btrfs_trans_handle *trans;
  7057. u64 min_free;
  7058. u64 dev_min = 1;
  7059. u64 dev_nr = 0;
  7060. u64 target;
  7061. int index;
  7062. int full = 0;
  7063. int ret = 0;
  7064. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7065. /* odd, couldn't find the block group, leave it alone */
  7066. if (!block_group)
  7067. return -1;
  7068. min_free = btrfs_block_group_used(&block_group->item);
  7069. /* no bytes used, we're good */
  7070. if (!min_free)
  7071. goto out;
  7072. space_info = block_group->space_info;
  7073. spin_lock(&space_info->lock);
  7074. full = space_info->full;
  7075. /*
  7076. * if this is the last block group we have in this space, we can't
  7077. * relocate it unless we're able to allocate a new chunk below.
  7078. *
  7079. * Otherwise, we need to make sure we have room in the space to handle
  7080. * all of the extents from this block group. If we can, we're good
  7081. */
  7082. if ((space_info->total_bytes != block_group->key.offset) &&
  7083. (space_info->bytes_used + space_info->bytes_reserved +
  7084. space_info->bytes_pinned + space_info->bytes_readonly +
  7085. min_free < space_info->total_bytes)) {
  7086. spin_unlock(&space_info->lock);
  7087. goto out;
  7088. }
  7089. spin_unlock(&space_info->lock);
  7090. /*
  7091. * ok we don't have enough space, but maybe we have free space on our
  7092. * devices to allocate new chunks for relocation, so loop through our
  7093. * alloc devices and guess if we have enough space. if this block
  7094. * group is going to be restriped, run checks against the target
  7095. * profile instead of the current one.
  7096. */
  7097. ret = -1;
  7098. /*
  7099. * index:
  7100. * 0: raid10
  7101. * 1: raid1
  7102. * 2: dup
  7103. * 3: raid0
  7104. * 4: single
  7105. */
  7106. target = get_restripe_target(root->fs_info, block_group->flags);
  7107. if (target) {
  7108. index = __get_raid_index(extended_to_chunk(target));
  7109. } else {
  7110. /*
  7111. * this is just a balance, so if we were marked as full
  7112. * we know there is no space for a new chunk
  7113. */
  7114. if (full)
  7115. goto out;
  7116. index = get_block_group_index(block_group);
  7117. }
  7118. if (index == BTRFS_RAID_RAID10) {
  7119. dev_min = 4;
  7120. /* Divide by 2 */
  7121. min_free >>= 1;
  7122. } else if (index == BTRFS_RAID_RAID1) {
  7123. dev_min = 2;
  7124. } else if (index == BTRFS_RAID_DUP) {
  7125. /* Multiply by 2 */
  7126. min_free <<= 1;
  7127. } else if (index == BTRFS_RAID_RAID0) {
  7128. dev_min = fs_devices->rw_devices;
  7129. do_div(min_free, dev_min);
  7130. }
  7131. /* We need to do this so that we can look at pending chunks */
  7132. trans = btrfs_join_transaction(root);
  7133. if (IS_ERR(trans)) {
  7134. ret = PTR_ERR(trans);
  7135. goto out;
  7136. }
  7137. mutex_lock(&root->fs_info->chunk_mutex);
  7138. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7139. u64 dev_offset;
  7140. /*
  7141. * check to make sure we can actually find a chunk with enough
  7142. * space to fit our block group in.
  7143. */
  7144. if (device->total_bytes > device->bytes_used + min_free &&
  7145. !device->is_tgtdev_for_dev_replace) {
  7146. ret = find_free_dev_extent(trans, device, min_free,
  7147. &dev_offset, NULL);
  7148. if (!ret)
  7149. dev_nr++;
  7150. if (dev_nr >= dev_min)
  7151. break;
  7152. ret = -1;
  7153. }
  7154. }
  7155. mutex_unlock(&root->fs_info->chunk_mutex);
  7156. btrfs_end_transaction(trans, root);
  7157. out:
  7158. btrfs_put_block_group(block_group);
  7159. return ret;
  7160. }
  7161. static int find_first_block_group(struct btrfs_root *root,
  7162. struct btrfs_path *path, struct btrfs_key *key)
  7163. {
  7164. int ret = 0;
  7165. struct btrfs_key found_key;
  7166. struct extent_buffer *leaf;
  7167. int slot;
  7168. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7169. if (ret < 0)
  7170. goto out;
  7171. while (1) {
  7172. slot = path->slots[0];
  7173. leaf = path->nodes[0];
  7174. if (slot >= btrfs_header_nritems(leaf)) {
  7175. ret = btrfs_next_leaf(root, path);
  7176. if (ret == 0)
  7177. continue;
  7178. if (ret < 0)
  7179. goto out;
  7180. break;
  7181. }
  7182. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7183. if (found_key.objectid >= key->objectid &&
  7184. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7185. ret = 0;
  7186. goto out;
  7187. }
  7188. path->slots[0]++;
  7189. }
  7190. out:
  7191. return ret;
  7192. }
  7193. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7194. {
  7195. struct btrfs_block_group_cache *block_group;
  7196. u64 last = 0;
  7197. while (1) {
  7198. struct inode *inode;
  7199. block_group = btrfs_lookup_first_block_group(info, last);
  7200. while (block_group) {
  7201. spin_lock(&block_group->lock);
  7202. if (block_group->iref)
  7203. break;
  7204. spin_unlock(&block_group->lock);
  7205. block_group = next_block_group(info->tree_root,
  7206. block_group);
  7207. }
  7208. if (!block_group) {
  7209. if (last == 0)
  7210. break;
  7211. last = 0;
  7212. continue;
  7213. }
  7214. inode = block_group->inode;
  7215. block_group->iref = 0;
  7216. block_group->inode = NULL;
  7217. spin_unlock(&block_group->lock);
  7218. iput(inode);
  7219. last = block_group->key.objectid + block_group->key.offset;
  7220. btrfs_put_block_group(block_group);
  7221. }
  7222. }
  7223. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7224. {
  7225. struct btrfs_block_group_cache *block_group;
  7226. struct btrfs_space_info *space_info;
  7227. struct btrfs_caching_control *caching_ctl;
  7228. struct rb_node *n;
  7229. down_write(&info->extent_commit_sem);
  7230. while (!list_empty(&info->caching_block_groups)) {
  7231. caching_ctl = list_entry(info->caching_block_groups.next,
  7232. struct btrfs_caching_control, list);
  7233. list_del(&caching_ctl->list);
  7234. put_caching_control(caching_ctl);
  7235. }
  7236. up_write(&info->extent_commit_sem);
  7237. spin_lock(&info->block_group_cache_lock);
  7238. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7239. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7240. cache_node);
  7241. rb_erase(&block_group->cache_node,
  7242. &info->block_group_cache_tree);
  7243. spin_unlock(&info->block_group_cache_lock);
  7244. down_write(&block_group->space_info->groups_sem);
  7245. list_del(&block_group->list);
  7246. up_write(&block_group->space_info->groups_sem);
  7247. if (block_group->cached == BTRFS_CACHE_STARTED)
  7248. wait_block_group_cache_done(block_group);
  7249. /*
  7250. * We haven't cached this block group, which means we could
  7251. * possibly have excluded extents on this block group.
  7252. */
  7253. if (block_group->cached == BTRFS_CACHE_NO ||
  7254. block_group->cached == BTRFS_CACHE_ERROR)
  7255. free_excluded_extents(info->extent_root, block_group);
  7256. btrfs_remove_free_space_cache(block_group);
  7257. btrfs_put_block_group(block_group);
  7258. spin_lock(&info->block_group_cache_lock);
  7259. }
  7260. spin_unlock(&info->block_group_cache_lock);
  7261. /* now that all the block groups are freed, go through and
  7262. * free all the space_info structs. This is only called during
  7263. * the final stages of unmount, and so we know nobody is
  7264. * using them. We call synchronize_rcu() once before we start,
  7265. * just to be on the safe side.
  7266. */
  7267. synchronize_rcu();
  7268. release_global_block_rsv(info);
  7269. while(!list_empty(&info->space_info)) {
  7270. space_info = list_entry(info->space_info.next,
  7271. struct btrfs_space_info,
  7272. list);
  7273. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  7274. if (space_info->bytes_pinned > 0 ||
  7275. space_info->bytes_reserved > 0 ||
  7276. space_info->bytes_may_use > 0) {
  7277. WARN_ON(1);
  7278. dump_space_info(space_info, 0, 0);
  7279. }
  7280. }
  7281. percpu_counter_destroy(&space_info->total_bytes_pinned);
  7282. list_del(&space_info->list);
  7283. kfree(space_info);
  7284. }
  7285. return 0;
  7286. }
  7287. static void __link_block_group(struct btrfs_space_info *space_info,
  7288. struct btrfs_block_group_cache *cache)
  7289. {
  7290. int index = get_block_group_index(cache);
  7291. down_write(&space_info->groups_sem);
  7292. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7293. up_write(&space_info->groups_sem);
  7294. }
  7295. int btrfs_read_block_groups(struct btrfs_root *root)
  7296. {
  7297. struct btrfs_path *path;
  7298. int ret;
  7299. struct btrfs_block_group_cache *cache;
  7300. struct btrfs_fs_info *info = root->fs_info;
  7301. struct btrfs_space_info *space_info;
  7302. struct btrfs_key key;
  7303. struct btrfs_key found_key;
  7304. struct extent_buffer *leaf;
  7305. int need_clear = 0;
  7306. u64 cache_gen;
  7307. root = info->extent_root;
  7308. key.objectid = 0;
  7309. key.offset = 0;
  7310. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7311. path = btrfs_alloc_path();
  7312. if (!path)
  7313. return -ENOMEM;
  7314. path->reada = 1;
  7315. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  7316. if (btrfs_test_opt(root, SPACE_CACHE) &&
  7317. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  7318. need_clear = 1;
  7319. if (btrfs_test_opt(root, CLEAR_CACHE))
  7320. need_clear = 1;
  7321. while (1) {
  7322. ret = find_first_block_group(root, path, &key);
  7323. if (ret > 0)
  7324. break;
  7325. if (ret != 0)
  7326. goto error;
  7327. leaf = path->nodes[0];
  7328. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7329. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7330. if (!cache) {
  7331. ret = -ENOMEM;
  7332. goto error;
  7333. }
  7334. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7335. GFP_NOFS);
  7336. if (!cache->free_space_ctl) {
  7337. kfree(cache);
  7338. ret = -ENOMEM;
  7339. goto error;
  7340. }
  7341. atomic_set(&cache->count, 1);
  7342. spin_lock_init(&cache->lock);
  7343. cache->fs_info = info;
  7344. INIT_LIST_HEAD(&cache->list);
  7345. INIT_LIST_HEAD(&cache->cluster_list);
  7346. if (need_clear) {
  7347. /*
  7348. * When we mount with old space cache, we need to
  7349. * set BTRFS_DC_CLEAR and set dirty flag.
  7350. *
  7351. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  7352. * truncate the old free space cache inode and
  7353. * setup a new one.
  7354. * b) Setting 'dirty flag' makes sure that we flush
  7355. * the new space cache info onto disk.
  7356. */
  7357. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7358. if (btrfs_test_opt(root, SPACE_CACHE))
  7359. cache->dirty = 1;
  7360. }
  7361. read_extent_buffer(leaf, &cache->item,
  7362. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7363. sizeof(cache->item));
  7364. memcpy(&cache->key, &found_key, sizeof(found_key));
  7365. key.objectid = found_key.objectid + found_key.offset;
  7366. btrfs_release_path(path);
  7367. cache->flags = btrfs_block_group_flags(&cache->item);
  7368. cache->sectorsize = root->sectorsize;
  7369. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7370. &root->fs_info->mapping_tree,
  7371. found_key.objectid);
  7372. btrfs_init_free_space_ctl(cache);
  7373. /*
  7374. * We need to exclude the super stripes now so that the space
  7375. * info has super bytes accounted for, otherwise we'll think
  7376. * we have more space than we actually do.
  7377. */
  7378. ret = exclude_super_stripes(root, cache);
  7379. if (ret) {
  7380. /*
  7381. * We may have excluded something, so call this just in
  7382. * case.
  7383. */
  7384. free_excluded_extents(root, cache);
  7385. kfree(cache->free_space_ctl);
  7386. kfree(cache);
  7387. goto error;
  7388. }
  7389. /*
  7390. * check for two cases, either we are full, and therefore
  7391. * don't need to bother with the caching work since we won't
  7392. * find any space, or we are empty, and we can just add all
  7393. * the space in and be done with it. This saves us _alot_ of
  7394. * time, particularly in the full case.
  7395. */
  7396. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7397. cache->last_byte_to_unpin = (u64)-1;
  7398. cache->cached = BTRFS_CACHE_FINISHED;
  7399. free_excluded_extents(root, cache);
  7400. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7401. cache->last_byte_to_unpin = (u64)-1;
  7402. cache->cached = BTRFS_CACHE_FINISHED;
  7403. add_new_free_space(cache, root->fs_info,
  7404. found_key.objectid,
  7405. found_key.objectid +
  7406. found_key.offset);
  7407. free_excluded_extents(root, cache);
  7408. }
  7409. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7410. if (ret) {
  7411. btrfs_remove_free_space_cache(cache);
  7412. btrfs_put_block_group(cache);
  7413. goto error;
  7414. }
  7415. ret = update_space_info(info, cache->flags, found_key.offset,
  7416. btrfs_block_group_used(&cache->item),
  7417. &space_info);
  7418. if (ret) {
  7419. btrfs_remove_free_space_cache(cache);
  7420. spin_lock(&info->block_group_cache_lock);
  7421. rb_erase(&cache->cache_node,
  7422. &info->block_group_cache_tree);
  7423. spin_unlock(&info->block_group_cache_lock);
  7424. btrfs_put_block_group(cache);
  7425. goto error;
  7426. }
  7427. cache->space_info = space_info;
  7428. spin_lock(&cache->space_info->lock);
  7429. cache->space_info->bytes_readonly += cache->bytes_super;
  7430. spin_unlock(&cache->space_info->lock);
  7431. __link_block_group(space_info, cache);
  7432. set_avail_alloc_bits(root->fs_info, cache->flags);
  7433. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7434. set_block_group_ro(cache, 1);
  7435. }
  7436. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7437. if (!(get_alloc_profile(root, space_info->flags) &
  7438. (BTRFS_BLOCK_GROUP_RAID10 |
  7439. BTRFS_BLOCK_GROUP_RAID1 |
  7440. BTRFS_BLOCK_GROUP_RAID5 |
  7441. BTRFS_BLOCK_GROUP_RAID6 |
  7442. BTRFS_BLOCK_GROUP_DUP)))
  7443. continue;
  7444. /*
  7445. * avoid allocating from un-mirrored block group if there are
  7446. * mirrored block groups.
  7447. */
  7448. list_for_each_entry(cache,
  7449. &space_info->block_groups[BTRFS_RAID_RAID0],
  7450. list)
  7451. set_block_group_ro(cache, 1);
  7452. list_for_each_entry(cache,
  7453. &space_info->block_groups[BTRFS_RAID_SINGLE],
  7454. list)
  7455. set_block_group_ro(cache, 1);
  7456. }
  7457. init_global_block_rsv(info);
  7458. ret = 0;
  7459. error:
  7460. btrfs_free_path(path);
  7461. return ret;
  7462. }
  7463. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  7464. struct btrfs_root *root)
  7465. {
  7466. struct btrfs_block_group_cache *block_group, *tmp;
  7467. struct btrfs_root *extent_root = root->fs_info->extent_root;
  7468. struct btrfs_block_group_item item;
  7469. struct btrfs_key key;
  7470. int ret = 0;
  7471. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  7472. new_bg_list) {
  7473. list_del_init(&block_group->new_bg_list);
  7474. if (ret)
  7475. continue;
  7476. spin_lock(&block_group->lock);
  7477. memcpy(&item, &block_group->item, sizeof(item));
  7478. memcpy(&key, &block_group->key, sizeof(key));
  7479. spin_unlock(&block_group->lock);
  7480. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  7481. sizeof(item));
  7482. if (ret)
  7483. btrfs_abort_transaction(trans, extent_root, ret);
  7484. ret = btrfs_finish_chunk_alloc(trans, extent_root,
  7485. key.objectid, key.offset);
  7486. if (ret)
  7487. btrfs_abort_transaction(trans, extent_root, ret);
  7488. }
  7489. }
  7490. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7491. struct btrfs_root *root, u64 bytes_used,
  7492. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7493. u64 size)
  7494. {
  7495. int ret;
  7496. struct btrfs_root *extent_root;
  7497. struct btrfs_block_group_cache *cache;
  7498. extent_root = root->fs_info->extent_root;
  7499. root->fs_info->last_trans_log_full_commit = trans->transid;
  7500. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7501. if (!cache)
  7502. return -ENOMEM;
  7503. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7504. GFP_NOFS);
  7505. if (!cache->free_space_ctl) {
  7506. kfree(cache);
  7507. return -ENOMEM;
  7508. }
  7509. cache->key.objectid = chunk_offset;
  7510. cache->key.offset = size;
  7511. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7512. cache->sectorsize = root->sectorsize;
  7513. cache->fs_info = root->fs_info;
  7514. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7515. &root->fs_info->mapping_tree,
  7516. chunk_offset);
  7517. atomic_set(&cache->count, 1);
  7518. spin_lock_init(&cache->lock);
  7519. INIT_LIST_HEAD(&cache->list);
  7520. INIT_LIST_HEAD(&cache->cluster_list);
  7521. INIT_LIST_HEAD(&cache->new_bg_list);
  7522. btrfs_init_free_space_ctl(cache);
  7523. btrfs_set_block_group_used(&cache->item, bytes_used);
  7524. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7525. cache->flags = type;
  7526. btrfs_set_block_group_flags(&cache->item, type);
  7527. cache->last_byte_to_unpin = (u64)-1;
  7528. cache->cached = BTRFS_CACHE_FINISHED;
  7529. ret = exclude_super_stripes(root, cache);
  7530. if (ret) {
  7531. /*
  7532. * We may have excluded something, so call this just in
  7533. * case.
  7534. */
  7535. free_excluded_extents(root, cache);
  7536. kfree(cache->free_space_ctl);
  7537. kfree(cache);
  7538. return ret;
  7539. }
  7540. add_new_free_space(cache, root->fs_info, chunk_offset,
  7541. chunk_offset + size);
  7542. free_excluded_extents(root, cache);
  7543. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7544. if (ret) {
  7545. btrfs_remove_free_space_cache(cache);
  7546. btrfs_put_block_group(cache);
  7547. return ret;
  7548. }
  7549. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7550. &cache->space_info);
  7551. if (ret) {
  7552. btrfs_remove_free_space_cache(cache);
  7553. spin_lock(&root->fs_info->block_group_cache_lock);
  7554. rb_erase(&cache->cache_node,
  7555. &root->fs_info->block_group_cache_tree);
  7556. spin_unlock(&root->fs_info->block_group_cache_lock);
  7557. btrfs_put_block_group(cache);
  7558. return ret;
  7559. }
  7560. update_global_block_rsv(root->fs_info);
  7561. spin_lock(&cache->space_info->lock);
  7562. cache->space_info->bytes_readonly += cache->bytes_super;
  7563. spin_unlock(&cache->space_info->lock);
  7564. __link_block_group(cache->space_info, cache);
  7565. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  7566. set_avail_alloc_bits(extent_root->fs_info, type);
  7567. return 0;
  7568. }
  7569. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  7570. {
  7571. u64 extra_flags = chunk_to_extended(flags) &
  7572. BTRFS_EXTENDED_PROFILE_MASK;
  7573. write_seqlock(&fs_info->profiles_lock);
  7574. if (flags & BTRFS_BLOCK_GROUP_DATA)
  7575. fs_info->avail_data_alloc_bits &= ~extra_flags;
  7576. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  7577. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  7578. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  7579. fs_info->avail_system_alloc_bits &= ~extra_flags;
  7580. write_sequnlock(&fs_info->profiles_lock);
  7581. }
  7582. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7583. struct btrfs_root *root, u64 group_start)
  7584. {
  7585. struct btrfs_path *path;
  7586. struct btrfs_block_group_cache *block_group;
  7587. struct btrfs_free_cluster *cluster;
  7588. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7589. struct btrfs_key key;
  7590. struct inode *inode;
  7591. int ret;
  7592. int index;
  7593. int factor;
  7594. root = root->fs_info->extent_root;
  7595. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7596. BUG_ON(!block_group);
  7597. BUG_ON(!block_group->ro);
  7598. /*
  7599. * Free the reserved super bytes from this block group before
  7600. * remove it.
  7601. */
  7602. free_excluded_extents(root, block_group);
  7603. memcpy(&key, &block_group->key, sizeof(key));
  7604. index = get_block_group_index(block_group);
  7605. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7606. BTRFS_BLOCK_GROUP_RAID1 |
  7607. BTRFS_BLOCK_GROUP_RAID10))
  7608. factor = 2;
  7609. else
  7610. factor = 1;
  7611. /* make sure this block group isn't part of an allocation cluster */
  7612. cluster = &root->fs_info->data_alloc_cluster;
  7613. spin_lock(&cluster->refill_lock);
  7614. btrfs_return_cluster_to_free_space(block_group, cluster);
  7615. spin_unlock(&cluster->refill_lock);
  7616. /*
  7617. * make sure this block group isn't part of a metadata
  7618. * allocation cluster
  7619. */
  7620. cluster = &root->fs_info->meta_alloc_cluster;
  7621. spin_lock(&cluster->refill_lock);
  7622. btrfs_return_cluster_to_free_space(block_group, cluster);
  7623. spin_unlock(&cluster->refill_lock);
  7624. path = btrfs_alloc_path();
  7625. if (!path) {
  7626. ret = -ENOMEM;
  7627. goto out;
  7628. }
  7629. inode = lookup_free_space_inode(tree_root, block_group, path);
  7630. if (!IS_ERR(inode)) {
  7631. ret = btrfs_orphan_add(trans, inode);
  7632. if (ret) {
  7633. btrfs_add_delayed_iput(inode);
  7634. goto out;
  7635. }
  7636. clear_nlink(inode);
  7637. /* One for the block groups ref */
  7638. spin_lock(&block_group->lock);
  7639. if (block_group->iref) {
  7640. block_group->iref = 0;
  7641. block_group->inode = NULL;
  7642. spin_unlock(&block_group->lock);
  7643. iput(inode);
  7644. } else {
  7645. spin_unlock(&block_group->lock);
  7646. }
  7647. /* One for our lookup ref */
  7648. btrfs_add_delayed_iput(inode);
  7649. }
  7650. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7651. key.offset = block_group->key.objectid;
  7652. key.type = 0;
  7653. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7654. if (ret < 0)
  7655. goto out;
  7656. if (ret > 0)
  7657. btrfs_release_path(path);
  7658. if (ret == 0) {
  7659. ret = btrfs_del_item(trans, tree_root, path);
  7660. if (ret)
  7661. goto out;
  7662. btrfs_release_path(path);
  7663. }
  7664. spin_lock(&root->fs_info->block_group_cache_lock);
  7665. rb_erase(&block_group->cache_node,
  7666. &root->fs_info->block_group_cache_tree);
  7667. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  7668. root->fs_info->first_logical_byte = (u64)-1;
  7669. spin_unlock(&root->fs_info->block_group_cache_lock);
  7670. down_write(&block_group->space_info->groups_sem);
  7671. /*
  7672. * we must use list_del_init so people can check to see if they
  7673. * are still on the list after taking the semaphore
  7674. */
  7675. list_del_init(&block_group->list);
  7676. if (list_empty(&block_group->space_info->block_groups[index]))
  7677. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7678. up_write(&block_group->space_info->groups_sem);
  7679. if (block_group->cached == BTRFS_CACHE_STARTED)
  7680. wait_block_group_cache_done(block_group);
  7681. btrfs_remove_free_space_cache(block_group);
  7682. spin_lock(&block_group->space_info->lock);
  7683. block_group->space_info->total_bytes -= block_group->key.offset;
  7684. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7685. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7686. spin_unlock(&block_group->space_info->lock);
  7687. memcpy(&key, &block_group->key, sizeof(key));
  7688. btrfs_clear_space_info_full(root->fs_info);
  7689. btrfs_put_block_group(block_group);
  7690. btrfs_put_block_group(block_group);
  7691. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7692. if (ret > 0)
  7693. ret = -EIO;
  7694. if (ret < 0)
  7695. goto out;
  7696. ret = btrfs_del_item(trans, root, path);
  7697. out:
  7698. btrfs_free_path(path);
  7699. return ret;
  7700. }
  7701. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7702. {
  7703. struct btrfs_space_info *space_info;
  7704. struct btrfs_super_block *disk_super;
  7705. u64 features;
  7706. u64 flags;
  7707. int mixed = 0;
  7708. int ret;
  7709. disk_super = fs_info->super_copy;
  7710. if (!btrfs_super_root(disk_super))
  7711. return 1;
  7712. features = btrfs_super_incompat_flags(disk_super);
  7713. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7714. mixed = 1;
  7715. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7716. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7717. if (ret)
  7718. goto out;
  7719. if (mixed) {
  7720. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7721. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7722. } else {
  7723. flags = BTRFS_BLOCK_GROUP_METADATA;
  7724. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7725. if (ret)
  7726. goto out;
  7727. flags = BTRFS_BLOCK_GROUP_DATA;
  7728. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7729. }
  7730. out:
  7731. return ret;
  7732. }
  7733. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7734. {
  7735. return unpin_extent_range(root, start, end);
  7736. }
  7737. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7738. u64 num_bytes, u64 *actual_bytes)
  7739. {
  7740. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7741. }
  7742. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7743. {
  7744. struct btrfs_fs_info *fs_info = root->fs_info;
  7745. struct btrfs_block_group_cache *cache = NULL;
  7746. u64 group_trimmed;
  7747. u64 start;
  7748. u64 end;
  7749. u64 trimmed = 0;
  7750. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7751. int ret = 0;
  7752. /*
  7753. * try to trim all FS space, our block group may start from non-zero.
  7754. */
  7755. if (range->len == total_bytes)
  7756. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7757. else
  7758. cache = btrfs_lookup_block_group(fs_info, range->start);
  7759. while (cache) {
  7760. if (cache->key.objectid >= (range->start + range->len)) {
  7761. btrfs_put_block_group(cache);
  7762. break;
  7763. }
  7764. start = max(range->start, cache->key.objectid);
  7765. end = min(range->start + range->len,
  7766. cache->key.objectid + cache->key.offset);
  7767. if (end - start >= range->minlen) {
  7768. if (!block_group_cache_done(cache)) {
  7769. ret = cache_block_group(cache, 0);
  7770. if (ret) {
  7771. btrfs_put_block_group(cache);
  7772. break;
  7773. }
  7774. ret = wait_block_group_cache_done(cache);
  7775. if (ret) {
  7776. btrfs_put_block_group(cache);
  7777. break;
  7778. }
  7779. }
  7780. ret = btrfs_trim_block_group(cache,
  7781. &group_trimmed,
  7782. start,
  7783. end,
  7784. range->minlen);
  7785. trimmed += group_trimmed;
  7786. if (ret) {
  7787. btrfs_put_block_group(cache);
  7788. break;
  7789. }
  7790. }
  7791. cache = next_block_group(fs_info->tree_root, cache);
  7792. }
  7793. range->len = trimmed;
  7794. return ret;
  7795. }