extent-tree.c 236 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895
  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. path = btrfs_alloc_path();
  1782. if (!path)
  1783. return -ENOMEM;
  1784. path->reada = 1;
  1785. path->leave_spinning = 1;
  1786. /* this will setup the path even if it fails to insert the back ref */
  1787. ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
  1788. path, bytenr, num_bytes, parent,
  1789. root_objectid, owner, offset,
  1790. refs_to_add, extent_op);
  1791. if (ret != -EAGAIN)
  1792. goto out;
  1793. leaf = path->nodes[0];
  1794. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1795. refs = btrfs_extent_refs(leaf, item);
  1796. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  1797. if (extent_op)
  1798. __run_delayed_extent_op(extent_op, leaf, item);
  1799. btrfs_mark_buffer_dirty(leaf);
  1800. btrfs_release_path(path);
  1801. path->reada = 1;
  1802. path->leave_spinning = 1;
  1803. /* now insert the actual backref */
  1804. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1805. path, bytenr, parent, root_objectid,
  1806. owner, offset, refs_to_add);
  1807. if (ret)
  1808. btrfs_abort_transaction(trans, root, ret);
  1809. out:
  1810. btrfs_free_path(path);
  1811. return ret;
  1812. }
  1813. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  1814. struct btrfs_root *root,
  1815. struct btrfs_delayed_ref_node *node,
  1816. struct btrfs_delayed_extent_op *extent_op,
  1817. int insert_reserved)
  1818. {
  1819. int ret = 0;
  1820. struct btrfs_delayed_data_ref *ref;
  1821. struct btrfs_key ins;
  1822. u64 parent = 0;
  1823. u64 ref_root = 0;
  1824. u64 flags = 0;
  1825. ins.objectid = node->bytenr;
  1826. ins.offset = node->num_bytes;
  1827. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1828. ref = btrfs_delayed_node_to_data_ref(node);
  1829. trace_run_delayed_data_ref(node, ref, node->action);
  1830. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1831. parent = ref->parent;
  1832. else
  1833. ref_root = ref->root;
  1834. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1835. if (extent_op)
  1836. flags |= extent_op->flags_to_set;
  1837. ret = alloc_reserved_file_extent(trans, root,
  1838. parent, ref_root, flags,
  1839. ref->objectid, ref->offset,
  1840. &ins, node->ref_mod);
  1841. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1842. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1843. node->num_bytes, parent,
  1844. ref_root, ref->objectid,
  1845. ref->offset, node->ref_mod,
  1846. extent_op);
  1847. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1848. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1849. node->num_bytes, parent,
  1850. ref_root, ref->objectid,
  1851. ref->offset, node->ref_mod,
  1852. extent_op);
  1853. } else {
  1854. BUG();
  1855. }
  1856. return ret;
  1857. }
  1858. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1859. struct extent_buffer *leaf,
  1860. struct btrfs_extent_item *ei)
  1861. {
  1862. u64 flags = btrfs_extent_flags(leaf, ei);
  1863. if (extent_op->update_flags) {
  1864. flags |= extent_op->flags_to_set;
  1865. btrfs_set_extent_flags(leaf, ei, flags);
  1866. }
  1867. if (extent_op->update_key) {
  1868. struct btrfs_tree_block_info *bi;
  1869. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1870. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1871. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1872. }
  1873. }
  1874. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1875. struct btrfs_root *root,
  1876. struct btrfs_delayed_ref_node *node,
  1877. struct btrfs_delayed_extent_op *extent_op)
  1878. {
  1879. struct btrfs_key key;
  1880. struct btrfs_path *path;
  1881. struct btrfs_extent_item *ei;
  1882. struct extent_buffer *leaf;
  1883. u32 item_size;
  1884. int ret;
  1885. int err = 0;
  1886. int metadata = !extent_op->is_data;
  1887. if (trans->aborted)
  1888. return 0;
  1889. if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  1890. metadata = 0;
  1891. path = btrfs_alloc_path();
  1892. if (!path)
  1893. return -ENOMEM;
  1894. key.objectid = node->bytenr;
  1895. if (metadata) {
  1896. key.type = BTRFS_METADATA_ITEM_KEY;
  1897. key.offset = extent_op->level;
  1898. } else {
  1899. key.type = BTRFS_EXTENT_ITEM_KEY;
  1900. key.offset = node->num_bytes;
  1901. }
  1902. again:
  1903. path->reada = 1;
  1904. path->leave_spinning = 1;
  1905. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  1906. path, 0, 1);
  1907. if (ret < 0) {
  1908. err = ret;
  1909. goto out;
  1910. }
  1911. if (ret > 0) {
  1912. if (metadata) {
  1913. btrfs_release_path(path);
  1914. metadata = 0;
  1915. key.offset = node->num_bytes;
  1916. key.type = BTRFS_EXTENT_ITEM_KEY;
  1917. goto again;
  1918. }
  1919. err = -EIO;
  1920. goto out;
  1921. }
  1922. leaf = path->nodes[0];
  1923. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1924. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1925. if (item_size < sizeof(*ei)) {
  1926. ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
  1927. path, (u64)-1, 0);
  1928. if (ret < 0) {
  1929. err = ret;
  1930. goto out;
  1931. }
  1932. leaf = path->nodes[0];
  1933. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1934. }
  1935. #endif
  1936. BUG_ON(item_size < sizeof(*ei));
  1937. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1938. __run_delayed_extent_op(extent_op, leaf, ei);
  1939. btrfs_mark_buffer_dirty(leaf);
  1940. out:
  1941. btrfs_free_path(path);
  1942. return err;
  1943. }
  1944. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1945. struct btrfs_root *root,
  1946. struct btrfs_delayed_ref_node *node,
  1947. struct btrfs_delayed_extent_op *extent_op,
  1948. int insert_reserved)
  1949. {
  1950. int ret = 0;
  1951. struct btrfs_delayed_tree_ref *ref;
  1952. struct btrfs_key ins;
  1953. u64 parent = 0;
  1954. u64 ref_root = 0;
  1955. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  1956. SKINNY_METADATA);
  1957. ref = btrfs_delayed_node_to_tree_ref(node);
  1958. trace_run_delayed_tree_ref(node, ref, node->action);
  1959. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1960. parent = ref->parent;
  1961. else
  1962. ref_root = ref->root;
  1963. ins.objectid = node->bytenr;
  1964. if (skinny_metadata) {
  1965. ins.offset = ref->level;
  1966. ins.type = BTRFS_METADATA_ITEM_KEY;
  1967. } else {
  1968. ins.offset = node->num_bytes;
  1969. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1970. }
  1971. BUG_ON(node->ref_mod != 1);
  1972. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1973. BUG_ON(!extent_op || !extent_op->update_flags);
  1974. ret = alloc_reserved_tree_block(trans, root,
  1975. parent, ref_root,
  1976. extent_op->flags_to_set,
  1977. &extent_op->key,
  1978. ref->level, &ins);
  1979. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1980. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1981. node->num_bytes, parent, ref_root,
  1982. ref->level, 0, 1, extent_op);
  1983. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1984. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1985. node->num_bytes, parent, ref_root,
  1986. ref->level, 0, 1, extent_op);
  1987. } else {
  1988. BUG();
  1989. }
  1990. return ret;
  1991. }
  1992. /* helper function to actually process a single delayed ref entry */
  1993. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  1994. struct btrfs_root *root,
  1995. struct btrfs_delayed_ref_node *node,
  1996. struct btrfs_delayed_extent_op *extent_op,
  1997. int insert_reserved)
  1998. {
  1999. int ret = 0;
  2000. if (trans->aborted) {
  2001. if (insert_reserved)
  2002. btrfs_pin_extent(root, node->bytenr,
  2003. node->num_bytes, 1);
  2004. return 0;
  2005. }
  2006. if (btrfs_delayed_ref_is_head(node)) {
  2007. struct btrfs_delayed_ref_head *head;
  2008. /*
  2009. * we've hit the end of the chain and we were supposed
  2010. * to insert this extent into the tree. But, it got
  2011. * deleted before we ever needed to insert it, so all
  2012. * we have to do is clean up the accounting
  2013. */
  2014. BUG_ON(extent_op);
  2015. head = btrfs_delayed_node_to_head(node);
  2016. trace_run_delayed_ref_head(node, head, node->action);
  2017. if (insert_reserved) {
  2018. btrfs_pin_extent(root, node->bytenr,
  2019. node->num_bytes, 1);
  2020. if (head->is_data) {
  2021. ret = btrfs_del_csums(trans, root,
  2022. node->bytenr,
  2023. node->num_bytes);
  2024. }
  2025. }
  2026. return ret;
  2027. }
  2028. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  2029. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  2030. ret = run_delayed_tree_ref(trans, root, node, extent_op,
  2031. insert_reserved);
  2032. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  2033. node->type == BTRFS_SHARED_DATA_REF_KEY)
  2034. ret = run_delayed_data_ref(trans, root, node, extent_op,
  2035. insert_reserved);
  2036. else
  2037. BUG();
  2038. return ret;
  2039. }
  2040. static noinline struct btrfs_delayed_ref_node *
  2041. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  2042. {
  2043. struct rb_node *node;
  2044. struct btrfs_delayed_ref_node *ref;
  2045. int action = BTRFS_ADD_DELAYED_REF;
  2046. again:
  2047. /*
  2048. * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
  2049. * this prevents ref count from going down to zero when
  2050. * there still are pending delayed ref.
  2051. */
  2052. node = rb_prev(&head->node.rb_node);
  2053. while (1) {
  2054. if (!node)
  2055. break;
  2056. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2057. rb_node);
  2058. if (ref->bytenr != head->node.bytenr)
  2059. break;
  2060. if (ref->action == action)
  2061. return ref;
  2062. node = rb_prev(node);
  2063. }
  2064. if (action == BTRFS_ADD_DELAYED_REF) {
  2065. action = BTRFS_DROP_DELAYED_REF;
  2066. goto again;
  2067. }
  2068. return NULL;
  2069. }
  2070. /*
  2071. * Returns 0 on success or if called with an already aborted transaction.
  2072. * Returns -ENOMEM or -EIO on failure and will abort the transaction.
  2073. */
  2074. static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
  2075. struct btrfs_root *root,
  2076. struct list_head *cluster)
  2077. {
  2078. struct btrfs_delayed_ref_root *delayed_refs;
  2079. struct btrfs_delayed_ref_node *ref;
  2080. struct btrfs_delayed_ref_head *locked_ref = NULL;
  2081. struct btrfs_delayed_extent_op *extent_op;
  2082. struct btrfs_fs_info *fs_info = root->fs_info;
  2083. int ret;
  2084. int count = 0;
  2085. int must_insert_reserved = 0;
  2086. delayed_refs = &trans->transaction->delayed_refs;
  2087. while (1) {
  2088. if (!locked_ref) {
  2089. /* pick a new head ref from the cluster list */
  2090. if (list_empty(cluster))
  2091. break;
  2092. locked_ref = list_entry(cluster->next,
  2093. struct btrfs_delayed_ref_head, cluster);
  2094. /* grab the lock that says we are going to process
  2095. * all the refs for this head */
  2096. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  2097. /*
  2098. * we may have dropped the spin lock to get the head
  2099. * mutex lock, and that might have given someone else
  2100. * time to free the head. If that's true, it has been
  2101. * removed from our list and we can move on.
  2102. */
  2103. if (ret == -EAGAIN) {
  2104. locked_ref = NULL;
  2105. count++;
  2106. continue;
  2107. }
  2108. }
  2109. /*
  2110. * We need to try and merge add/drops of the same ref since we
  2111. * can run into issues with relocate dropping the implicit ref
  2112. * and then it being added back again before the drop can
  2113. * finish. If we merged anything we need to re-loop so we can
  2114. * get a good ref.
  2115. */
  2116. btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
  2117. locked_ref);
  2118. /*
  2119. * locked_ref is the head node, so we have to go one
  2120. * node back for any delayed ref updates
  2121. */
  2122. ref = select_delayed_ref(locked_ref);
  2123. if (ref && ref->seq &&
  2124. btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
  2125. /*
  2126. * there are still refs with lower seq numbers in the
  2127. * process of being added. Don't run this ref yet.
  2128. */
  2129. list_del_init(&locked_ref->cluster);
  2130. btrfs_delayed_ref_unlock(locked_ref);
  2131. locked_ref = NULL;
  2132. delayed_refs->num_heads_ready++;
  2133. spin_unlock(&delayed_refs->lock);
  2134. cond_resched();
  2135. spin_lock(&delayed_refs->lock);
  2136. continue;
  2137. }
  2138. /*
  2139. * record the must insert reserved flag before we
  2140. * drop the spin lock.
  2141. */
  2142. must_insert_reserved = locked_ref->must_insert_reserved;
  2143. locked_ref->must_insert_reserved = 0;
  2144. extent_op = locked_ref->extent_op;
  2145. locked_ref->extent_op = NULL;
  2146. if (!ref) {
  2147. /* All delayed refs have been processed, Go ahead
  2148. * and send the head node to run_one_delayed_ref,
  2149. * so that any accounting fixes can happen
  2150. */
  2151. ref = &locked_ref->node;
  2152. if (extent_op && must_insert_reserved) {
  2153. btrfs_free_delayed_extent_op(extent_op);
  2154. extent_op = NULL;
  2155. }
  2156. if (extent_op) {
  2157. spin_unlock(&delayed_refs->lock);
  2158. ret = run_delayed_extent_op(trans, root,
  2159. ref, extent_op);
  2160. btrfs_free_delayed_extent_op(extent_op);
  2161. if (ret) {
  2162. /*
  2163. * Need to reset must_insert_reserved if
  2164. * there was an error so the abort stuff
  2165. * can cleanup the reserved space
  2166. * properly.
  2167. */
  2168. if (must_insert_reserved)
  2169. locked_ref->must_insert_reserved = 1;
  2170. btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
  2171. spin_lock(&delayed_refs->lock);
  2172. btrfs_delayed_ref_unlock(locked_ref);
  2173. return ret;
  2174. }
  2175. goto next;
  2176. }
  2177. }
  2178. ref->in_tree = 0;
  2179. rb_erase(&ref->rb_node, &delayed_refs->root);
  2180. delayed_refs->num_entries--;
  2181. if (!btrfs_delayed_ref_is_head(ref)) {
  2182. /*
  2183. * when we play the delayed ref, also correct the
  2184. * ref_mod on head
  2185. */
  2186. switch (ref->action) {
  2187. case BTRFS_ADD_DELAYED_REF:
  2188. case BTRFS_ADD_DELAYED_EXTENT:
  2189. locked_ref->node.ref_mod -= ref->ref_mod;
  2190. break;
  2191. case BTRFS_DROP_DELAYED_REF:
  2192. locked_ref->node.ref_mod += ref->ref_mod;
  2193. break;
  2194. default:
  2195. WARN_ON(1);
  2196. }
  2197. } else {
  2198. list_del_init(&locked_ref->cluster);
  2199. }
  2200. spin_unlock(&delayed_refs->lock);
  2201. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2202. must_insert_reserved);
  2203. btrfs_free_delayed_extent_op(extent_op);
  2204. if (ret) {
  2205. btrfs_delayed_ref_unlock(locked_ref);
  2206. btrfs_put_delayed_ref(ref);
  2207. btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
  2208. spin_lock(&delayed_refs->lock);
  2209. return ret;
  2210. }
  2211. /*
  2212. * If this node is a head, that means all the refs in this head
  2213. * have been dealt with, and we will pick the next head to deal
  2214. * with, so we must unlock the head and drop it from the cluster
  2215. * list before we release it.
  2216. */
  2217. if (btrfs_delayed_ref_is_head(ref)) {
  2218. btrfs_delayed_ref_unlock(locked_ref);
  2219. locked_ref = NULL;
  2220. }
  2221. btrfs_put_delayed_ref(ref);
  2222. count++;
  2223. next:
  2224. cond_resched();
  2225. spin_lock(&delayed_refs->lock);
  2226. }
  2227. return count;
  2228. }
  2229. #ifdef SCRAMBLE_DELAYED_REFS
  2230. /*
  2231. * Normally delayed refs get processed in ascending bytenr order. This
  2232. * correlates in most cases to the order added. To expose dependencies on this
  2233. * order, we start to process the tree in the middle instead of the beginning
  2234. */
  2235. static u64 find_middle(struct rb_root *root)
  2236. {
  2237. struct rb_node *n = root->rb_node;
  2238. struct btrfs_delayed_ref_node *entry;
  2239. int alt = 1;
  2240. u64 middle;
  2241. u64 first = 0, last = 0;
  2242. n = rb_first(root);
  2243. if (n) {
  2244. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2245. first = entry->bytenr;
  2246. }
  2247. n = rb_last(root);
  2248. if (n) {
  2249. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2250. last = entry->bytenr;
  2251. }
  2252. n = root->rb_node;
  2253. while (n) {
  2254. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2255. WARN_ON(!entry->in_tree);
  2256. middle = entry->bytenr;
  2257. if (alt)
  2258. n = n->rb_left;
  2259. else
  2260. n = n->rb_right;
  2261. alt = 1 - alt;
  2262. }
  2263. return middle;
  2264. }
  2265. #endif
  2266. int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
  2267. struct btrfs_fs_info *fs_info)
  2268. {
  2269. struct qgroup_update *qgroup_update;
  2270. int ret = 0;
  2271. if (list_empty(&trans->qgroup_ref_list) !=
  2272. !trans->delayed_ref_elem.seq) {
  2273. /* list without seq or seq without list */
  2274. btrfs_err(fs_info,
  2275. "qgroup accounting update error, list is%s empty, seq is %#x.%x",
  2276. list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2277. (u32)(trans->delayed_ref_elem.seq >> 32),
  2278. (u32)trans->delayed_ref_elem.seq);
  2279. BUG();
  2280. }
  2281. if (!trans->delayed_ref_elem.seq)
  2282. return 0;
  2283. while (!list_empty(&trans->qgroup_ref_list)) {
  2284. qgroup_update = list_first_entry(&trans->qgroup_ref_list,
  2285. struct qgroup_update, list);
  2286. list_del(&qgroup_update->list);
  2287. if (!ret)
  2288. ret = btrfs_qgroup_account_ref(
  2289. trans, fs_info, qgroup_update->node,
  2290. qgroup_update->extent_op);
  2291. kfree(qgroup_update);
  2292. }
  2293. btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  2294. return ret;
  2295. }
  2296. static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
  2297. int count)
  2298. {
  2299. int val = atomic_read(&delayed_refs->ref_seq);
  2300. if (val < seq || val >= seq + count)
  2301. return 1;
  2302. return 0;
  2303. }
  2304. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2305. {
  2306. u64 num_bytes;
  2307. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2308. sizeof(struct btrfs_extent_inline_ref));
  2309. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2310. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2311. /*
  2312. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2313. * closer to what we're really going to want to ouse.
  2314. */
  2315. return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2316. }
  2317. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2318. struct btrfs_root *root)
  2319. {
  2320. struct btrfs_block_rsv *global_rsv;
  2321. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2322. u64 num_bytes;
  2323. int ret = 0;
  2324. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2325. num_heads = heads_to_leaves(root, num_heads);
  2326. if (num_heads > 1)
  2327. num_bytes += (num_heads - 1) * root->leafsize;
  2328. num_bytes <<= 1;
  2329. global_rsv = &root->fs_info->global_block_rsv;
  2330. /*
  2331. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2332. * wiggle room since running delayed refs can create more delayed refs.
  2333. */
  2334. if (global_rsv->space_info->full)
  2335. num_bytes <<= 1;
  2336. spin_lock(&global_rsv->lock);
  2337. if (global_rsv->reserved <= num_bytes)
  2338. ret = 1;
  2339. spin_unlock(&global_rsv->lock);
  2340. return ret;
  2341. }
  2342. /*
  2343. * this starts processing the delayed reference count updates and
  2344. * extent insertions we have queued up so far. count can be
  2345. * 0, which means to process everything in the tree at the start
  2346. * of the run (but not newly added entries), or it can be some target
  2347. * number you'd like to process.
  2348. *
  2349. * Returns 0 on success or if called with an aborted transaction
  2350. * Returns <0 on error and aborts the transaction
  2351. */
  2352. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2353. struct btrfs_root *root, unsigned long count)
  2354. {
  2355. struct rb_node *node;
  2356. struct btrfs_delayed_ref_root *delayed_refs;
  2357. struct btrfs_delayed_ref_node *ref;
  2358. struct list_head cluster;
  2359. int ret;
  2360. u64 delayed_start;
  2361. int run_all = count == (unsigned long)-1;
  2362. int run_most = 0;
  2363. int loops;
  2364. /* We'll clean this up in btrfs_cleanup_transaction */
  2365. if (trans->aborted)
  2366. return 0;
  2367. if (root == root->fs_info->extent_root)
  2368. root = root->fs_info->tree_root;
  2369. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  2370. delayed_refs = &trans->transaction->delayed_refs;
  2371. INIT_LIST_HEAD(&cluster);
  2372. if (count == 0) {
  2373. count = delayed_refs->num_entries * 2;
  2374. run_most = 1;
  2375. }
  2376. if (!run_all && !run_most) {
  2377. int old;
  2378. int seq = atomic_read(&delayed_refs->ref_seq);
  2379. progress:
  2380. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2381. if (old) {
  2382. DEFINE_WAIT(__wait);
  2383. if (delayed_refs->flushing ||
  2384. !btrfs_should_throttle_delayed_refs(trans, root))
  2385. return 0;
  2386. prepare_to_wait(&delayed_refs->wait, &__wait,
  2387. TASK_UNINTERRUPTIBLE);
  2388. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2389. if (old) {
  2390. schedule();
  2391. finish_wait(&delayed_refs->wait, &__wait);
  2392. if (!refs_newer(delayed_refs, seq, 256))
  2393. goto progress;
  2394. else
  2395. return 0;
  2396. } else {
  2397. finish_wait(&delayed_refs->wait, &__wait);
  2398. goto again;
  2399. }
  2400. }
  2401. } else {
  2402. atomic_inc(&delayed_refs->procs_running_refs);
  2403. }
  2404. again:
  2405. loops = 0;
  2406. spin_lock(&delayed_refs->lock);
  2407. #ifdef SCRAMBLE_DELAYED_REFS
  2408. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2409. #endif
  2410. while (1) {
  2411. if (!(run_all || run_most) &&
  2412. !btrfs_should_throttle_delayed_refs(trans, root))
  2413. break;
  2414. /*
  2415. * go find something we can process in the rbtree. We start at
  2416. * the beginning of the tree, and then build a cluster
  2417. * of refs to process starting at the first one we are able to
  2418. * lock
  2419. */
  2420. delayed_start = delayed_refs->run_delayed_start;
  2421. ret = btrfs_find_ref_cluster(trans, &cluster,
  2422. delayed_refs->run_delayed_start);
  2423. if (ret)
  2424. break;
  2425. ret = run_clustered_refs(trans, root, &cluster);
  2426. if (ret < 0) {
  2427. btrfs_release_ref_cluster(&cluster);
  2428. spin_unlock(&delayed_refs->lock);
  2429. btrfs_abort_transaction(trans, root, ret);
  2430. atomic_dec(&delayed_refs->procs_running_refs);
  2431. wake_up(&delayed_refs->wait);
  2432. return ret;
  2433. }
  2434. atomic_add(ret, &delayed_refs->ref_seq);
  2435. count -= min_t(unsigned long, ret, count);
  2436. if (count == 0)
  2437. break;
  2438. if (delayed_start >= delayed_refs->run_delayed_start) {
  2439. if (loops == 0) {
  2440. /*
  2441. * btrfs_find_ref_cluster looped. let's do one
  2442. * more cycle. if we don't run any delayed ref
  2443. * during that cycle (because we can't because
  2444. * all of them are blocked), bail out.
  2445. */
  2446. loops = 1;
  2447. } else {
  2448. /*
  2449. * no runnable refs left, stop trying
  2450. */
  2451. BUG_ON(run_all);
  2452. break;
  2453. }
  2454. }
  2455. if (ret) {
  2456. /* refs were run, let's reset staleness detection */
  2457. loops = 0;
  2458. }
  2459. }
  2460. if (run_all) {
  2461. if (!list_empty(&trans->new_bgs)) {
  2462. spin_unlock(&delayed_refs->lock);
  2463. btrfs_create_pending_block_groups(trans, root);
  2464. spin_lock(&delayed_refs->lock);
  2465. }
  2466. node = rb_first(&delayed_refs->root);
  2467. if (!node)
  2468. goto out;
  2469. count = (unsigned long)-1;
  2470. while (node) {
  2471. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2472. rb_node);
  2473. if (btrfs_delayed_ref_is_head(ref)) {
  2474. struct btrfs_delayed_ref_head *head;
  2475. head = btrfs_delayed_node_to_head(ref);
  2476. atomic_inc(&ref->refs);
  2477. spin_unlock(&delayed_refs->lock);
  2478. /*
  2479. * Mutex was contended, block until it's
  2480. * released and try again
  2481. */
  2482. mutex_lock(&head->mutex);
  2483. mutex_unlock(&head->mutex);
  2484. btrfs_put_delayed_ref(ref);
  2485. cond_resched();
  2486. goto again;
  2487. }
  2488. node = rb_next(node);
  2489. }
  2490. spin_unlock(&delayed_refs->lock);
  2491. schedule_timeout(1);
  2492. goto again;
  2493. }
  2494. out:
  2495. atomic_dec(&delayed_refs->procs_running_refs);
  2496. smp_mb();
  2497. if (waitqueue_active(&delayed_refs->wait))
  2498. wake_up(&delayed_refs->wait);
  2499. spin_unlock(&delayed_refs->lock);
  2500. assert_qgroups_uptodate(trans);
  2501. return 0;
  2502. }
  2503. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2504. struct btrfs_root *root,
  2505. u64 bytenr, u64 num_bytes, u64 flags,
  2506. int level, int is_data)
  2507. {
  2508. struct btrfs_delayed_extent_op *extent_op;
  2509. int ret;
  2510. extent_op = btrfs_alloc_delayed_extent_op();
  2511. if (!extent_op)
  2512. return -ENOMEM;
  2513. extent_op->flags_to_set = flags;
  2514. extent_op->update_flags = 1;
  2515. extent_op->update_key = 0;
  2516. extent_op->is_data = is_data ? 1 : 0;
  2517. extent_op->level = level;
  2518. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2519. num_bytes, extent_op);
  2520. if (ret)
  2521. btrfs_free_delayed_extent_op(extent_op);
  2522. return ret;
  2523. }
  2524. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2525. struct btrfs_root *root,
  2526. struct btrfs_path *path,
  2527. u64 objectid, u64 offset, u64 bytenr)
  2528. {
  2529. struct btrfs_delayed_ref_head *head;
  2530. struct btrfs_delayed_ref_node *ref;
  2531. struct btrfs_delayed_data_ref *data_ref;
  2532. struct btrfs_delayed_ref_root *delayed_refs;
  2533. struct rb_node *node;
  2534. int ret = 0;
  2535. ret = -ENOENT;
  2536. delayed_refs = &trans->transaction->delayed_refs;
  2537. spin_lock(&delayed_refs->lock);
  2538. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2539. if (!head)
  2540. goto out;
  2541. if (!mutex_trylock(&head->mutex)) {
  2542. atomic_inc(&head->node.refs);
  2543. spin_unlock(&delayed_refs->lock);
  2544. btrfs_release_path(path);
  2545. /*
  2546. * Mutex was contended, block until it's released and let
  2547. * caller try again
  2548. */
  2549. mutex_lock(&head->mutex);
  2550. mutex_unlock(&head->mutex);
  2551. btrfs_put_delayed_ref(&head->node);
  2552. return -EAGAIN;
  2553. }
  2554. node = rb_prev(&head->node.rb_node);
  2555. if (!node)
  2556. goto out_unlock;
  2557. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2558. if (ref->bytenr != bytenr)
  2559. goto out_unlock;
  2560. ret = 1;
  2561. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2562. goto out_unlock;
  2563. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2564. node = rb_prev(node);
  2565. if (node) {
  2566. int seq = ref->seq;
  2567. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2568. if (ref->bytenr == bytenr && ref->seq == seq)
  2569. goto out_unlock;
  2570. }
  2571. if (data_ref->root != root->root_key.objectid ||
  2572. data_ref->objectid != objectid || data_ref->offset != offset)
  2573. goto out_unlock;
  2574. ret = 0;
  2575. out_unlock:
  2576. mutex_unlock(&head->mutex);
  2577. out:
  2578. spin_unlock(&delayed_refs->lock);
  2579. return ret;
  2580. }
  2581. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2582. struct btrfs_root *root,
  2583. struct btrfs_path *path,
  2584. u64 objectid, u64 offset, u64 bytenr)
  2585. {
  2586. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2587. struct extent_buffer *leaf;
  2588. struct btrfs_extent_data_ref *ref;
  2589. struct btrfs_extent_inline_ref *iref;
  2590. struct btrfs_extent_item *ei;
  2591. struct btrfs_key key;
  2592. u32 item_size;
  2593. int ret;
  2594. key.objectid = bytenr;
  2595. key.offset = (u64)-1;
  2596. key.type = BTRFS_EXTENT_ITEM_KEY;
  2597. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2598. if (ret < 0)
  2599. goto out;
  2600. BUG_ON(ret == 0); /* Corruption */
  2601. ret = -ENOENT;
  2602. if (path->slots[0] == 0)
  2603. goto out;
  2604. path->slots[0]--;
  2605. leaf = path->nodes[0];
  2606. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2607. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2608. goto out;
  2609. ret = 1;
  2610. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2611. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2612. if (item_size < sizeof(*ei)) {
  2613. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2614. goto out;
  2615. }
  2616. #endif
  2617. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2618. if (item_size != sizeof(*ei) +
  2619. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2620. goto out;
  2621. if (btrfs_extent_generation(leaf, ei) <=
  2622. btrfs_root_last_snapshot(&root->root_item))
  2623. goto out;
  2624. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2625. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2626. BTRFS_EXTENT_DATA_REF_KEY)
  2627. goto out;
  2628. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2629. if (btrfs_extent_refs(leaf, ei) !=
  2630. btrfs_extent_data_ref_count(leaf, ref) ||
  2631. btrfs_extent_data_ref_root(leaf, ref) !=
  2632. root->root_key.objectid ||
  2633. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2634. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2635. goto out;
  2636. ret = 0;
  2637. out:
  2638. return ret;
  2639. }
  2640. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2641. struct btrfs_root *root,
  2642. u64 objectid, u64 offset, u64 bytenr)
  2643. {
  2644. struct btrfs_path *path;
  2645. int ret;
  2646. int ret2;
  2647. path = btrfs_alloc_path();
  2648. if (!path)
  2649. return -ENOENT;
  2650. do {
  2651. ret = check_committed_ref(trans, root, path, objectid,
  2652. offset, bytenr);
  2653. if (ret && ret != -ENOENT)
  2654. goto out;
  2655. ret2 = check_delayed_ref(trans, root, path, objectid,
  2656. offset, bytenr);
  2657. } while (ret2 == -EAGAIN);
  2658. if (ret2 && ret2 != -ENOENT) {
  2659. ret = ret2;
  2660. goto out;
  2661. }
  2662. if (ret != -ENOENT || ret2 != -ENOENT)
  2663. ret = 0;
  2664. out:
  2665. btrfs_free_path(path);
  2666. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2667. WARN_ON(ret > 0);
  2668. return ret;
  2669. }
  2670. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2671. struct btrfs_root *root,
  2672. struct extent_buffer *buf,
  2673. int full_backref, int inc, int for_cow)
  2674. {
  2675. u64 bytenr;
  2676. u64 num_bytes;
  2677. u64 parent;
  2678. u64 ref_root;
  2679. u32 nritems;
  2680. struct btrfs_key key;
  2681. struct btrfs_file_extent_item *fi;
  2682. int i;
  2683. int level;
  2684. int ret = 0;
  2685. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2686. u64, u64, u64, u64, u64, u64, int);
  2687. ref_root = btrfs_header_owner(buf);
  2688. nritems = btrfs_header_nritems(buf);
  2689. level = btrfs_header_level(buf);
  2690. if (!root->ref_cows && level == 0)
  2691. return 0;
  2692. if (inc)
  2693. process_func = btrfs_inc_extent_ref;
  2694. else
  2695. process_func = btrfs_free_extent;
  2696. if (full_backref)
  2697. parent = buf->start;
  2698. else
  2699. parent = 0;
  2700. for (i = 0; i < nritems; i++) {
  2701. if (level == 0) {
  2702. btrfs_item_key_to_cpu(buf, &key, i);
  2703. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2704. continue;
  2705. fi = btrfs_item_ptr(buf, i,
  2706. struct btrfs_file_extent_item);
  2707. if (btrfs_file_extent_type(buf, fi) ==
  2708. BTRFS_FILE_EXTENT_INLINE)
  2709. continue;
  2710. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2711. if (bytenr == 0)
  2712. continue;
  2713. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2714. key.offset -= btrfs_file_extent_offset(buf, fi);
  2715. ret = process_func(trans, root, bytenr, num_bytes,
  2716. parent, ref_root, key.objectid,
  2717. key.offset, for_cow);
  2718. if (ret)
  2719. goto fail;
  2720. } else {
  2721. bytenr = btrfs_node_blockptr(buf, i);
  2722. num_bytes = btrfs_level_size(root, level - 1);
  2723. ret = process_func(trans, root, bytenr, num_bytes,
  2724. parent, ref_root, level - 1, 0,
  2725. for_cow);
  2726. if (ret)
  2727. goto fail;
  2728. }
  2729. }
  2730. return 0;
  2731. fail:
  2732. return ret;
  2733. }
  2734. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2735. struct extent_buffer *buf, int full_backref, int for_cow)
  2736. {
  2737. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2738. }
  2739. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2740. struct extent_buffer *buf, int full_backref, int for_cow)
  2741. {
  2742. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2743. }
  2744. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2745. struct btrfs_root *root,
  2746. struct btrfs_path *path,
  2747. struct btrfs_block_group_cache *cache)
  2748. {
  2749. int ret;
  2750. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2751. unsigned long bi;
  2752. struct extent_buffer *leaf;
  2753. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2754. if (ret < 0)
  2755. goto fail;
  2756. BUG_ON(ret); /* Corruption */
  2757. leaf = path->nodes[0];
  2758. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2759. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2760. btrfs_mark_buffer_dirty(leaf);
  2761. btrfs_release_path(path);
  2762. fail:
  2763. if (ret) {
  2764. btrfs_abort_transaction(trans, root, ret);
  2765. return ret;
  2766. }
  2767. return 0;
  2768. }
  2769. static struct btrfs_block_group_cache *
  2770. next_block_group(struct btrfs_root *root,
  2771. struct btrfs_block_group_cache *cache)
  2772. {
  2773. struct rb_node *node;
  2774. spin_lock(&root->fs_info->block_group_cache_lock);
  2775. node = rb_next(&cache->cache_node);
  2776. btrfs_put_block_group(cache);
  2777. if (node) {
  2778. cache = rb_entry(node, struct btrfs_block_group_cache,
  2779. cache_node);
  2780. btrfs_get_block_group(cache);
  2781. } else
  2782. cache = NULL;
  2783. spin_unlock(&root->fs_info->block_group_cache_lock);
  2784. return cache;
  2785. }
  2786. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2787. struct btrfs_trans_handle *trans,
  2788. struct btrfs_path *path)
  2789. {
  2790. struct btrfs_root *root = block_group->fs_info->tree_root;
  2791. struct inode *inode = NULL;
  2792. u64 alloc_hint = 0;
  2793. int dcs = BTRFS_DC_ERROR;
  2794. int num_pages = 0;
  2795. int retries = 0;
  2796. int ret = 0;
  2797. /*
  2798. * If this block group is smaller than 100 megs don't bother caching the
  2799. * block group.
  2800. */
  2801. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2802. spin_lock(&block_group->lock);
  2803. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2804. spin_unlock(&block_group->lock);
  2805. return 0;
  2806. }
  2807. again:
  2808. inode = lookup_free_space_inode(root, block_group, path);
  2809. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2810. ret = PTR_ERR(inode);
  2811. btrfs_release_path(path);
  2812. goto out;
  2813. }
  2814. if (IS_ERR(inode)) {
  2815. BUG_ON(retries);
  2816. retries++;
  2817. if (block_group->ro)
  2818. goto out_free;
  2819. ret = create_free_space_inode(root, trans, block_group, path);
  2820. if (ret)
  2821. goto out_free;
  2822. goto again;
  2823. }
  2824. /* We've already setup this transaction, go ahead and exit */
  2825. if (block_group->cache_generation == trans->transid &&
  2826. i_size_read(inode)) {
  2827. dcs = BTRFS_DC_SETUP;
  2828. goto out_put;
  2829. }
  2830. /*
  2831. * We want to set the generation to 0, that way if anything goes wrong
  2832. * from here on out we know not to trust this cache when we load up next
  2833. * time.
  2834. */
  2835. BTRFS_I(inode)->generation = 0;
  2836. ret = btrfs_update_inode(trans, root, inode);
  2837. WARN_ON(ret);
  2838. if (i_size_read(inode) > 0) {
  2839. ret = btrfs_check_trunc_cache_free_space(root,
  2840. &root->fs_info->global_block_rsv);
  2841. if (ret)
  2842. goto out_put;
  2843. ret = btrfs_truncate_free_space_cache(root, trans, inode);
  2844. if (ret)
  2845. goto out_put;
  2846. }
  2847. spin_lock(&block_group->lock);
  2848. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2849. !btrfs_test_opt(root, SPACE_CACHE)) {
  2850. /*
  2851. * don't bother trying to write stuff out _if_
  2852. * a) we're not cached,
  2853. * b) we're with nospace_cache mount option.
  2854. */
  2855. dcs = BTRFS_DC_WRITTEN;
  2856. spin_unlock(&block_group->lock);
  2857. goto out_put;
  2858. }
  2859. spin_unlock(&block_group->lock);
  2860. /*
  2861. * Try to preallocate enough space based on how big the block group is.
  2862. * Keep in mind this has to include any pinned space which could end up
  2863. * taking up quite a bit since it's not folded into the other space
  2864. * cache.
  2865. */
  2866. num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
  2867. if (!num_pages)
  2868. num_pages = 1;
  2869. num_pages *= 16;
  2870. num_pages *= PAGE_CACHE_SIZE;
  2871. ret = btrfs_check_data_free_space(inode, num_pages);
  2872. if (ret)
  2873. goto out_put;
  2874. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2875. num_pages, num_pages,
  2876. &alloc_hint);
  2877. if (!ret)
  2878. dcs = BTRFS_DC_SETUP;
  2879. btrfs_free_reserved_data_space(inode, num_pages);
  2880. out_put:
  2881. iput(inode);
  2882. out_free:
  2883. btrfs_release_path(path);
  2884. out:
  2885. spin_lock(&block_group->lock);
  2886. if (!ret && dcs == BTRFS_DC_SETUP)
  2887. block_group->cache_generation = trans->transid;
  2888. block_group->disk_cache_state = dcs;
  2889. spin_unlock(&block_group->lock);
  2890. return ret;
  2891. }
  2892. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2893. struct btrfs_root *root)
  2894. {
  2895. struct btrfs_block_group_cache *cache;
  2896. int err = 0;
  2897. struct btrfs_path *path;
  2898. u64 last = 0;
  2899. path = btrfs_alloc_path();
  2900. if (!path)
  2901. return -ENOMEM;
  2902. again:
  2903. while (1) {
  2904. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2905. while (cache) {
  2906. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2907. break;
  2908. cache = next_block_group(root, cache);
  2909. }
  2910. if (!cache) {
  2911. if (last == 0)
  2912. break;
  2913. last = 0;
  2914. continue;
  2915. }
  2916. err = cache_save_setup(cache, trans, path);
  2917. last = cache->key.objectid + cache->key.offset;
  2918. btrfs_put_block_group(cache);
  2919. }
  2920. while (1) {
  2921. if (last == 0) {
  2922. err = btrfs_run_delayed_refs(trans, root,
  2923. (unsigned long)-1);
  2924. if (err) /* File system offline */
  2925. goto out;
  2926. }
  2927. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2928. while (cache) {
  2929. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2930. btrfs_put_block_group(cache);
  2931. goto again;
  2932. }
  2933. if (cache->dirty)
  2934. break;
  2935. cache = next_block_group(root, cache);
  2936. }
  2937. if (!cache) {
  2938. if (last == 0)
  2939. break;
  2940. last = 0;
  2941. continue;
  2942. }
  2943. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2944. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2945. cache->dirty = 0;
  2946. last = cache->key.objectid + cache->key.offset;
  2947. err = write_one_cache_group(trans, root, path, cache);
  2948. btrfs_put_block_group(cache);
  2949. if (err) /* File system offline */
  2950. goto out;
  2951. }
  2952. while (1) {
  2953. /*
  2954. * I don't think this is needed since we're just marking our
  2955. * preallocated extent as written, but just in case it can't
  2956. * hurt.
  2957. */
  2958. if (last == 0) {
  2959. err = btrfs_run_delayed_refs(trans, root,
  2960. (unsigned long)-1);
  2961. if (err) /* File system offline */
  2962. goto out;
  2963. }
  2964. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2965. while (cache) {
  2966. /*
  2967. * Really this shouldn't happen, but it could if we
  2968. * couldn't write the entire preallocated extent and
  2969. * splitting the extent resulted in a new block.
  2970. */
  2971. if (cache->dirty) {
  2972. btrfs_put_block_group(cache);
  2973. goto again;
  2974. }
  2975. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2976. break;
  2977. cache = next_block_group(root, cache);
  2978. }
  2979. if (!cache) {
  2980. if (last == 0)
  2981. break;
  2982. last = 0;
  2983. continue;
  2984. }
  2985. err = btrfs_write_out_cache(root, trans, cache, path);
  2986. /*
  2987. * If we didn't have an error then the cache state is still
  2988. * NEED_WRITE, so we can set it to WRITTEN.
  2989. */
  2990. if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2991. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2992. last = cache->key.objectid + cache->key.offset;
  2993. btrfs_put_block_group(cache);
  2994. }
  2995. out:
  2996. btrfs_free_path(path);
  2997. return err;
  2998. }
  2999. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  3000. {
  3001. struct btrfs_block_group_cache *block_group;
  3002. int readonly = 0;
  3003. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  3004. if (!block_group || block_group->ro)
  3005. readonly = 1;
  3006. if (block_group)
  3007. btrfs_put_block_group(block_group);
  3008. return readonly;
  3009. }
  3010. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  3011. u64 total_bytes, u64 bytes_used,
  3012. struct btrfs_space_info **space_info)
  3013. {
  3014. struct btrfs_space_info *found;
  3015. int i;
  3016. int factor;
  3017. int ret;
  3018. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3019. BTRFS_BLOCK_GROUP_RAID10))
  3020. factor = 2;
  3021. else
  3022. factor = 1;
  3023. found = __find_space_info(info, flags);
  3024. if (found) {
  3025. spin_lock(&found->lock);
  3026. found->total_bytes += total_bytes;
  3027. found->disk_total += total_bytes * factor;
  3028. found->bytes_used += bytes_used;
  3029. found->disk_used += bytes_used * factor;
  3030. found->full = 0;
  3031. spin_unlock(&found->lock);
  3032. *space_info = found;
  3033. return 0;
  3034. }
  3035. found = kzalloc(sizeof(*found), GFP_NOFS);
  3036. if (!found)
  3037. return -ENOMEM;
  3038. ret = percpu_counter_init(&found->total_bytes_pinned, 0);
  3039. if (ret) {
  3040. kfree(found);
  3041. return ret;
  3042. }
  3043. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  3044. INIT_LIST_HEAD(&found->block_groups[i]);
  3045. init_rwsem(&found->groups_sem);
  3046. spin_lock_init(&found->lock);
  3047. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3048. found->total_bytes = total_bytes;
  3049. found->disk_total = total_bytes * factor;
  3050. found->bytes_used = bytes_used;
  3051. found->disk_used = bytes_used * factor;
  3052. found->bytes_pinned = 0;
  3053. found->bytes_reserved = 0;
  3054. found->bytes_readonly = 0;
  3055. found->bytes_may_use = 0;
  3056. found->full = 0;
  3057. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3058. found->chunk_alloc = 0;
  3059. found->flush = 0;
  3060. init_waitqueue_head(&found->wait);
  3061. *space_info = found;
  3062. list_add_rcu(&found->list, &info->space_info);
  3063. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3064. info->data_sinfo = found;
  3065. return 0;
  3066. }
  3067. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3068. {
  3069. u64 extra_flags = chunk_to_extended(flags) &
  3070. BTRFS_EXTENDED_PROFILE_MASK;
  3071. write_seqlock(&fs_info->profiles_lock);
  3072. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3073. fs_info->avail_data_alloc_bits |= extra_flags;
  3074. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3075. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3076. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3077. fs_info->avail_system_alloc_bits |= extra_flags;
  3078. write_sequnlock(&fs_info->profiles_lock);
  3079. }
  3080. /*
  3081. * returns target flags in extended format or 0 if restripe for this
  3082. * chunk_type is not in progress
  3083. *
  3084. * should be called with either volume_mutex or balance_lock held
  3085. */
  3086. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3087. {
  3088. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3089. u64 target = 0;
  3090. if (!bctl)
  3091. return 0;
  3092. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3093. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3094. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3095. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3096. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3097. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3098. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3099. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3100. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3101. }
  3102. return target;
  3103. }
  3104. /*
  3105. * @flags: available profiles in extended format (see ctree.h)
  3106. *
  3107. * Returns reduced profile in chunk format. If profile changing is in
  3108. * progress (either running or paused) picks the target profile (if it's
  3109. * already available), otherwise falls back to plain reducing.
  3110. */
  3111. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3112. {
  3113. /*
  3114. * we add in the count of missing devices because we want
  3115. * to make sure that any RAID levels on a degraded FS
  3116. * continue to be honored.
  3117. */
  3118. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  3119. root->fs_info->fs_devices->missing_devices;
  3120. u64 target;
  3121. u64 tmp;
  3122. /*
  3123. * see if restripe for this chunk_type is in progress, if so
  3124. * try to reduce to the target profile
  3125. */
  3126. spin_lock(&root->fs_info->balance_lock);
  3127. target = get_restripe_target(root->fs_info, flags);
  3128. if (target) {
  3129. /* pick target profile only if it's already available */
  3130. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3131. spin_unlock(&root->fs_info->balance_lock);
  3132. return extended_to_chunk(target);
  3133. }
  3134. }
  3135. spin_unlock(&root->fs_info->balance_lock);
  3136. /* First, mask out the RAID levels which aren't possible */
  3137. if (num_devices == 1)
  3138. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3139. BTRFS_BLOCK_GROUP_RAID5);
  3140. if (num_devices < 3)
  3141. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3142. if (num_devices < 4)
  3143. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3144. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3145. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3146. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3147. flags &= ~tmp;
  3148. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3149. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3150. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3151. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3152. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3153. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3154. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3155. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3156. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3157. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3158. return extended_to_chunk(flags | tmp);
  3159. }
  3160. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  3161. {
  3162. unsigned seq;
  3163. do {
  3164. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3165. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3166. flags |= root->fs_info->avail_data_alloc_bits;
  3167. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3168. flags |= root->fs_info->avail_system_alloc_bits;
  3169. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3170. flags |= root->fs_info->avail_metadata_alloc_bits;
  3171. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3172. return btrfs_reduce_alloc_profile(root, flags);
  3173. }
  3174. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3175. {
  3176. u64 flags;
  3177. u64 ret;
  3178. if (data)
  3179. flags = BTRFS_BLOCK_GROUP_DATA;
  3180. else if (root == root->fs_info->chunk_root)
  3181. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3182. else
  3183. flags = BTRFS_BLOCK_GROUP_METADATA;
  3184. ret = get_alloc_profile(root, flags);
  3185. return ret;
  3186. }
  3187. /*
  3188. * This will check the space that the inode allocates from to make sure we have
  3189. * enough space for bytes.
  3190. */
  3191. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  3192. {
  3193. struct btrfs_space_info *data_sinfo;
  3194. struct btrfs_root *root = BTRFS_I(inode)->root;
  3195. struct btrfs_fs_info *fs_info = root->fs_info;
  3196. u64 used;
  3197. int ret = 0, committed = 0, alloc_chunk = 1;
  3198. /* make sure bytes are sectorsize aligned */
  3199. bytes = ALIGN(bytes, root->sectorsize);
  3200. if (root == root->fs_info->tree_root ||
  3201. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  3202. alloc_chunk = 0;
  3203. committed = 1;
  3204. }
  3205. data_sinfo = fs_info->data_sinfo;
  3206. if (!data_sinfo)
  3207. goto alloc;
  3208. again:
  3209. /* make sure we have enough space to handle the data first */
  3210. spin_lock(&data_sinfo->lock);
  3211. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3212. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3213. data_sinfo->bytes_may_use;
  3214. if (used + bytes > data_sinfo->total_bytes) {
  3215. struct btrfs_trans_handle *trans;
  3216. /*
  3217. * if we don't have enough free bytes in this space then we need
  3218. * to alloc a new chunk.
  3219. */
  3220. if (!data_sinfo->full && alloc_chunk) {
  3221. u64 alloc_target;
  3222. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3223. spin_unlock(&data_sinfo->lock);
  3224. alloc:
  3225. alloc_target = btrfs_get_alloc_profile(root, 1);
  3226. trans = btrfs_join_transaction(root);
  3227. if (IS_ERR(trans))
  3228. return PTR_ERR(trans);
  3229. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3230. alloc_target,
  3231. CHUNK_ALLOC_NO_FORCE);
  3232. btrfs_end_transaction(trans, root);
  3233. if (ret < 0) {
  3234. if (ret != -ENOSPC)
  3235. return ret;
  3236. else
  3237. goto commit_trans;
  3238. }
  3239. if (!data_sinfo)
  3240. data_sinfo = fs_info->data_sinfo;
  3241. goto again;
  3242. }
  3243. /*
  3244. * If we don't have enough pinned space to deal with this
  3245. * allocation don't bother committing the transaction.
  3246. */
  3247. if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
  3248. bytes) < 0)
  3249. committed = 1;
  3250. spin_unlock(&data_sinfo->lock);
  3251. /* commit the current transaction and try again */
  3252. commit_trans:
  3253. if (!committed &&
  3254. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3255. committed = 1;
  3256. trans = btrfs_join_transaction(root);
  3257. if (IS_ERR(trans))
  3258. return PTR_ERR(trans);
  3259. ret = btrfs_commit_transaction(trans, root);
  3260. if (ret)
  3261. return ret;
  3262. goto again;
  3263. }
  3264. return -ENOSPC;
  3265. }
  3266. data_sinfo->bytes_may_use += bytes;
  3267. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3268. data_sinfo->flags, bytes, 1);
  3269. spin_unlock(&data_sinfo->lock);
  3270. return 0;
  3271. }
  3272. /*
  3273. * Called if we need to clear a data reservation for this inode.
  3274. */
  3275. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3276. {
  3277. struct btrfs_root *root = BTRFS_I(inode)->root;
  3278. struct btrfs_space_info *data_sinfo;
  3279. /* make sure bytes are sectorsize aligned */
  3280. bytes = ALIGN(bytes, root->sectorsize);
  3281. data_sinfo = root->fs_info->data_sinfo;
  3282. spin_lock(&data_sinfo->lock);
  3283. WARN_ON(data_sinfo->bytes_may_use < bytes);
  3284. data_sinfo->bytes_may_use -= bytes;
  3285. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3286. data_sinfo->flags, bytes, 0);
  3287. spin_unlock(&data_sinfo->lock);
  3288. }
  3289. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3290. {
  3291. struct list_head *head = &info->space_info;
  3292. struct btrfs_space_info *found;
  3293. rcu_read_lock();
  3294. list_for_each_entry_rcu(found, head, list) {
  3295. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3296. found->force_alloc = CHUNK_ALLOC_FORCE;
  3297. }
  3298. rcu_read_unlock();
  3299. }
  3300. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3301. {
  3302. return (global->size << 1);
  3303. }
  3304. static int should_alloc_chunk(struct btrfs_root *root,
  3305. struct btrfs_space_info *sinfo, int force)
  3306. {
  3307. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3308. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3309. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3310. u64 thresh;
  3311. if (force == CHUNK_ALLOC_FORCE)
  3312. return 1;
  3313. /*
  3314. * We need to take into account the global rsv because for all intents
  3315. * and purposes it's used space. Don't worry about locking the
  3316. * global_rsv, it doesn't change except when the transaction commits.
  3317. */
  3318. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3319. num_allocated += calc_global_rsv_need_space(global_rsv);
  3320. /*
  3321. * in limited mode, we want to have some free space up to
  3322. * about 1% of the FS size.
  3323. */
  3324. if (force == CHUNK_ALLOC_LIMITED) {
  3325. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3326. thresh = max_t(u64, 64 * 1024 * 1024,
  3327. div_factor_fine(thresh, 1));
  3328. if (num_bytes - num_allocated < thresh)
  3329. return 1;
  3330. }
  3331. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3332. return 0;
  3333. return 1;
  3334. }
  3335. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3336. {
  3337. u64 num_dev;
  3338. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3339. BTRFS_BLOCK_GROUP_RAID0 |
  3340. BTRFS_BLOCK_GROUP_RAID5 |
  3341. BTRFS_BLOCK_GROUP_RAID6))
  3342. num_dev = root->fs_info->fs_devices->rw_devices;
  3343. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3344. num_dev = 2;
  3345. else
  3346. num_dev = 1; /* DUP or single */
  3347. /* metadata for updaing devices and chunk tree */
  3348. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3349. }
  3350. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3351. struct btrfs_root *root, u64 type)
  3352. {
  3353. struct btrfs_space_info *info;
  3354. u64 left;
  3355. u64 thresh;
  3356. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3357. spin_lock(&info->lock);
  3358. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3359. info->bytes_reserved - info->bytes_readonly;
  3360. spin_unlock(&info->lock);
  3361. thresh = get_system_chunk_thresh(root, type);
  3362. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3363. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3364. left, thresh, type);
  3365. dump_space_info(info, 0, 0);
  3366. }
  3367. if (left < thresh) {
  3368. u64 flags;
  3369. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3370. btrfs_alloc_chunk(trans, root, flags);
  3371. }
  3372. }
  3373. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3374. struct btrfs_root *extent_root, u64 flags, int force)
  3375. {
  3376. struct btrfs_space_info *space_info;
  3377. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3378. int wait_for_alloc = 0;
  3379. int ret = 0;
  3380. /* Don't re-enter if we're already allocating a chunk */
  3381. if (trans->allocating_chunk)
  3382. return -ENOSPC;
  3383. space_info = __find_space_info(extent_root->fs_info, flags);
  3384. if (!space_info) {
  3385. ret = update_space_info(extent_root->fs_info, flags,
  3386. 0, 0, &space_info);
  3387. BUG_ON(ret); /* -ENOMEM */
  3388. }
  3389. BUG_ON(!space_info); /* Logic error */
  3390. again:
  3391. spin_lock(&space_info->lock);
  3392. if (force < space_info->force_alloc)
  3393. force = space_info->force_alloc;
  3394. if (space_info->full) {
  3395. if (should_alloc_chunk(extent_root, space_info, force))
  3396. ret = -ENOSPC;
  3397. else
  3398. ret = 0;
  3399. spin_unlock(&space_info->lock);
  3400. return ret;
  3401. }
  3402. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3403. spin_unlock(&space_info->lock);
  3404. return 0;
  3405. } else if (space_info->chunk_alloc) {
  3406. wait_for_alloc = 1;
  3407. } else {
  3408. space_info->chunk_alloc = 1;
  3409. }
  3410. spin_unlock(&space_info->lock);
  3411. mutex_lock(&fs_info->chunk_mutex);
  3412. /*
  3413. * The chunk_mutex is held throughout the entirety of a chunk
  3414. * allocation, so once we've acquired the chunk_mutex we know that the
  3415. * other guy is done and we need to recheck and see if we should
  3416. * allocate.
  3417. */
  3418. if (wait_for_alloc) {
  3419. mutex_unlock(&fs_info->chunk_mutex);
  3420. wait_for_alloc = 0;
  3421. goto again;
  3422. }
  3423. trans->allocating_chunk = true;
  3424. /*
  3425. * If we have mixed data/metadata chunks we want to make sure we keep
  3426. * allocating mixed chunks instead of individual chunks.
  3427. */
  3428. if (btrfs_mixed_space_info(space_info))
  3429. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3430. /*
  3431. * if we're doing a data chunk, go ahead and make sure that
  3432. * we keep a reasonable number of metadata chunks allocated in the
  3433. * FS as well.
  3434. */
  3435. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3436. fs_info->data_chunk_allocations++;
  3437. if (!(fs_info->data_chunk_allocations %
  3438. fs_info->metadata_ratio))
  3439. force_metadata_allocation(fs_info);
  3440. }
  3441. /*
  3442. * Check if we have enough space in SYSTEM chunk because we may need
  3443. * to update devices.
  3444. */
  3445. check_system_chunk(trans, extent_root, flags);
  3446. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3447. trans->allocating_chunk = false;
  3448. spin_lock(&space_info->lock);
  3449. if (ret < 0 && ret != -ENOSPC)
  3450. goto out;
  3451. if (ret)
  3452. space_info->full = 1;
  3453. else
  3454. ret = 1;
  3455. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3456. out:
  3457. space_info->chunk_alloc = 0;
  3458. spin_unlock(&space_info->lock);
  3459. mutex_unlock(&fs_info->chunk_mutex);
  3460. return ret;
  3461. }
  3462. static int can_overcommit(struct btrfs_root *root,
  3463. struct btrfs_space_info *space_info, u64 bytes,
  3464. enum btrfs_reserve_flush_enum flush)
  3465. {
  3466. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3467. u64 profile = btrfs_get_alloc_profile(root, 0);
  3468. u64 space_size;
  3469. u64 avail;
  3470. u64 used;
  3471. used = space_info->bytes_used + space_info->bytes_reserved +
  3472. space_info->bytes_pinned + space_info->bytes_readonly;
  3473. /*
  3474. * We only want to allow over committing if we have lots of actual space
  3475. * free, but if we don't have enough space to handle the global reserve
  3476. * space then we could end up having a real enospc problem when trying
  3477. * to allocate a chunk or some other such important allocation.
  3478. */
  3479. spin_lock(&global_rsv->lock);
  3480. space_size = calc_global_rsv_need_space(global_rsv);
  3481. spin_unlock(&global_rsv->lock);
  3482. if (used + space_size >= space_info->total_bytes)
  3483. return 0;
  3484. used += space_info->bytes_may_use;
  3485. spin_lock(&root->fs_info->free_chunk_lock);
  3486. avail = root->fs_info->free_chunk_space;
  3487. spin_unlock(&root->fs_info->free_chunk_lock);
  3488. /*
  3489. * If we have dup, raid1 or raid10 then only half of the free
  3490. * space is actually useable. For raid56, the space info used
  3491. * doesn't include the parity drive, so we don't have to
  3492. * change the math
  3493. */
  3494. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3495. BTRFS_BLOCK_GROUP_RAID1 |
  3496. BTRFS_BLOCK_GROUP_RAID10))
  3497. avail >>= 1;
  3498. /*
  3499. * If we aren't flushing all things, let us overcommit up to
  3500. * 1/2th of the space. If we can flush, don't let us overcommit
  3501. * too much, let it overcommit up to 1/8 of the space.
  3502. */
  3503. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3504. avail >>= 3;
  3505. else
  3506. avail >>= 1;
  3507. if (used + bytes < space_info->total_bytes + avail)
  3508. return 1;
  3509. return 0;
  3510. }
  3511. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3512. unsigned long nr_pages)
  3513. {
  3514. struct super_block *sb = root->fs_info->sb;
  3515. if (down_read_trylock(&sb->s_umount)) {
  3516. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  3517. up_read(&sb->s_umount);
  3518. } else {
  3519. /*
  3520. * We needn't worry the filesystem going from r/w to r/o though
  3521. * we don't acquire ->s_umount mutex, because the filesystem
  3522. * should guarantee the delalloc inodes list be empty after
  3523. * the filesystem is readonly(all dirty pages are written to
  3524. * the disk).
  3525. */
  3526. btrfs_start_all_delalloc_inodes(root->fs_info, 0);
  3527. if (!current->journal_info)
  3528. btrfs_wait_all_ordered_extents(root->fs_info);
  3529. }
  3530. }
  3531. /*
  3532. * shrink metadata reservation for delalloc
  3533. */
  3534. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3535. bool wait_ordered)
  3536. {
  3537. struct btrfs_block_rsv *block_rsv;
  3538. struct btrfs_space_info *space_info;
  3539. struct btrfs_trans_handle *trans;
  3540. u64 delalloc_bytes;
  3541. u64 max_reclaim;
  3542. long time_left;
  3543. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3544. int loops = 0;
  3545. enum btrfs_reserve_flush_enum flush;
  3546. trans = (struct btrfs_trans_handle *)current->journal_info;
  3547. block_rsv = &root->fs_info->delalloc_block_rsv;
  3548. space_info = block_rsv->space_info;
  3549. smp_mb();
  3550. delalloc_bytes = percpu_counter_sum_positive(
  3551. &root->fs_info->delalloc_bytes);
  3552. if (delalloc_bytes == 0) {
  3553. if (trans)
  3554. return;
  3555. btrfs_wait_all_ordered_extents(root->fs_info);
  3556. return;
  3557. }
  3558. while (delalloc_bytes && loops < 3) {
  3559. max_reclaim = min(delalloc_bytes, to_reclaim);
  3560. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3561. btrfs_writeback_inodes_sb_nr(root, nr_pages);
  3562. /*
  3563. * We need to wait for the async pages to actually start before
  3564. * we do anything.
  3565. */
  3566. wait_event(root->fs_info->async_submit_wait,
  3567. !atomic_read(&root->fs_info->async_delalloc_pages));
  3568. if (!trans)
  3569. flush = BTRFS_RESERVE_FLUSH_ALL;
  3570. else
  3571. flush = BTRFS_RESERVE_NO_FLUSH;
  3572. spin_lock(&space_info->lock);
  3573. if (can_overcommit(root, space_info, orig, flush)) {
  3574. spin_unlock(&space_info->lock);
  3575. break;
  3576. }
  3577. spin_unlock(&space_info->lock);
  3578. loops++;
  3579. if (wait_ordered && !trans) {
  3580. btrfs_wait_all_ordered_extents(root->fs_info);
  3581. } else {
  3582. time_left = schedule_timeout_killable(1);
  3583. if (time_left)
  3584. break;
  3585. }
  3586. smp_mb();
  3587. delalloc_bytes = percpu_counter_sum_positive(
  3588. &root->fs_info->delalloc_bytes);
  3589. }
  3590. }
  3591. /**
  3592. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3593. * @root - the root we're allocating for
  3594. * @bytes - the number of bytes we want to reserve
  3595. * @force - force the commit
  3596. *
  3597. * This will check to make sure that committing the transaction will actually
  3598. * get us somewhere and then commit the transaction if it does. Otherwise it
  3599. * will return -ENOSPC.
  3600. */
  3601. static int may_commit_transaction(struct btrfs_root *root,
  3602. struct btrfs_space_info *space_info,
  3603. u64 bytes, int force)
  3604. {
  3605. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3606. struct btrfs_trans_handle *trans;
  3607. trans = (struct btrfs_trans_handle *)current->journal_info;
  3608. if (trans)
  3609. return -EAGAIN;
  3610. if (force)
  3611. goto commit;
  3612. /* See if there is enough pinned space to make this reservation */
  3613. spin_lock(&space_info->lock);
  3614. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3615. bytes) >= 0) {
  3616. spin_unlock(&space_info->lock);
  3617. goto commit;
  3618. }
  3619. spin_unlock(&space_info->lock);
  3620. /*
  3621. * See if there is some space in the delayed insertion reservation for
  3622. * this reservation.
  3623. */
  3624. if (space_info != delayed_rsv->space_info)
  3625. return -ENOSPC;
  3626. spin_lock(&space_info->lock);
  3627. spin_lock(&delayed_rsv->lock);
  3628. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3629. bytes - delayed_rsv->size) >= 0) {
  3630. spin_unlock(&delayed_rsv->lock);
  3631. spin_unlock(&space_info->lock);
  3632. return -ENOSPC;
  3633. }
  3634. spin_unlock(&delayed_rsv->lock);
  3635. spin_unlock(&space_info->lock);
  3636. commit:
  3637. trans = btrfs_join_transaction(root);
  3638. if (IS_ERR(trans))
  3639. return -ENOSPC;
  3640. return btrfs_commit_transaction(trans, root);
  3641. }
  3642. enum flush_state {
  3643. FLUSH_DELAYED_ITEMS_NR = 1,
  3644. FLUSH_DELAYED_ITEMS = 2,
  3645. FLUSH_DELALLOC = 3,
  3646. FLUSH_DELALLOC_WAIT = 4,
  3647. ALLOC_CHUNK = 5,
  3648. COMMIT_TRANS = 6,
  3649. };
  3650. static int flush_space(struct btrfs_root *root,
  3651. struct btrfs_space_info *space_info, u64 num_bytes,
  3652. u64 orig_bytes, int state)
  3653. {
  3654. struct btrfs_trans_handle *trans;
  3655. int nr;
  3656. int ret = 0;
  3657. switch (state) {
  3658. case FLUSH_DELAYED_ITEMS_NR:
  3659. case FLUSH_DELAYED_ITEMS:
  3660. if (state == FLUSH_DELAYED_ITEMS_NR) {
  3661. u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
  3662. nr = (int)div64_u64(num_bytes, bytes);
  3663. if (!nr)
  3664. nr = 1;
  3665. nr *= 2;
  3666. } else {
  3667. nr = -1;
  3668. }
  3669. trans = btrfs_join_transaction(root);
  3670. if (IS_ERR(trans)) {
  3671. ret = PTR_ERR(trans);
  3672. break;
  3673. }
  3674. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3675. btrfs_end_transaction(trans, root);
  3676. break;
  3677. case FLUSH_DELALLOC:
  3678. case FLUSH_DELALLOC_WAIT:
  3679. shrink_delalloc(root, num_bytes, orig_bytes,
  3680. state == FLUSH_DELALLOC_WAIT);
  3681. break;
  3682. case ALLOC_CHUNK:
  3683. trans = btrfs_join_transaction(root);
  3684. if (IS_ERR(trans)) {
  3685. ret = PTR_ERR(trans);
  3686. break;
  3687. }
  3688. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3689. btrfs_get_alloc_profile(root, 0),
  3690. CHUNK_ALLOC_NO_FORCE);
  3691. btrfs_end_transaction(trans, root);
  3692. if (ret == -ENOSPC)
  3693. ret = 0;
  3694. break;
  3695. case COMMIT_TRANS:
  3696. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3697. break;
  3698. default:
  3699. ret = -ENOSPC;
  3700. break;
  3701. }
  3702. return ret;
  3703. }
  3704. /**
  3705. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3706. * @root - the root we're allocating for
  3707. * @block_rsv - the block_rsv we're allocating for
  3708. * @orig_bytes - the number of bytes we want
  3709. * @flush - whether or not we can flush to make our reservation
  3710. *
  3711. * This will reserve orgi_bytes number of bytes from the space info associated
  3712. * with the block_rsv. If there is not enough space it will make an attempt to
  3713. * flush out space to make room. It will do this by flushing delalloc if
  3714. * possible or committing the transaction. If flush is 0 then no attempts to
  3715. * regain reservations will be made and this will fail if there is not enough
  3716. * space already.
  3717. */
  3718. static int reserve_metadata_bytes(struct btrfs_root *root,
  3719. struct btrfs_block_rsv *block_rsv,
  3720. u64 orig_bytes,
  3721. enum btrfs_reserve_flush_enum flush)
  3722. {
  3723. struct btrfs_space_info *space_info = block_rsv->space_info;
  3724. u64 used;
  3725. u64 num_bytes = orig_bytes;
  3726. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  3727. int ret = 0;
  3728. bool flushing = false;
  3729. again:
  3730. ret = 0;
  3731. spin_lock(&space_info->lock);
  3732. /*
  3733. * We only want to wait if somebody other than us is flushing and we
  3734. * are actually allowed to flush all things.
  3735. */
  3736. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  3737. space_info->flush) {
  3738. spin_unlock(&space_info->lock);
  3739. /*
  3740. * If we have a trans handle we can't wait because the flusher
  3741. * may have to commit the transaction, which would mean we would
  3742. * deadlock since we are waiting for the flusher to finish, but
  3743. * hold the current transaction open.
  3744. */
  3745. if (current->journal_info)
  3746. return -EAGAIN;
  3747. ret = wait_event_killable(space_info->wait, !space_info->flush);
  3748. /* Must have been killed, return */
  3749. if (ret)
  3750. return -EINTR;
  3751. spin_lock(&space_info->lock);
  3752. }
  3753. ret = -ENOSPC;
  3754. used = space_info->bytes_used + space_info->bytes_reserved +
  3755. space_info->bytes_pinned + space_info->bytes_readonly +
  3756. space_info->bytes_may_use;
  3757. /*
  3758. * The idea here is that we've not already over-reserved the block group
  3759. * then we can go ahead and save our reservation first and then start
  3760. * flushing if we need to. Otherwise if we've already overcommitted
  3761. * lets start flushing stuff first and then come back and try to make
  3762. * our reservation.
  3763. */
  3764. if (used <= space_info->total_bytes) {
  3765. if (used + orig_bytes <= space_info->total_bytes) {
  3766. space_info->bytes_may_use += orig_bytes;
  3767. trace_btrfs_space_reservation(root->fs_info,
  3768. "space_info", space_info->flags, orig_bytes, 1);
  3769. ret = 0;
  3770. } else {
  3771. /*
  3772. * Ok set num_bytes to orig_bytes since we aren't
  3773. * overocmmitted, this way we only try and reclaim what
  3774. * we need.
  3775. */
  3776. num_bytes = orig_bytes;
  3777. }
  3778. } else {
  3779. /*
  3780. * Ok we're over committed, set num_bytes to the overcommitted
  3781. * amount plus the amount of bytes that we need for this
  3782. * reservation.
  3783. */
  3784. num_bytes = used - space_info->total_bytes +
  3785. (orig_bytes * 2);
  3786. }
  3787. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  3788. space_info->bytes_may_use += orig_bytes;
  3789. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3790. space_info->flags, orig_bytes,
  3791. 1);
  3792. ret = 0;
  3793. }
  3794. /*
  3795. * Couldn't make our reservation, save our place so while we're trying
  3796. * to reclaim space we can actually use it instead of somebody else
  3797. * stealing it from us.
  3798. *
  3799. * We make the other tasks wait for the flush only when we can flush
  3800. * all things.
  3801. */
  3802. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  3803. flushing = true;
  3804. space_info->flush = 1;
  3805. }
  3806. spin_unlock(&space_info->lock);
  3807. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  3808. goto out;
  3809. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  3810. flush_state);
  3811. flush_state++;
  3812. /*
  3813. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  3814. * would happen. So skip delalloc flush.
  3815. */
  3816. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3817. (flush_state == FLUSH_DELALLOC ||
  3818. flush_state == FLUSH_DELALLOC_WAIT))
  3819. flush_state = ALLOC_CHUNK;
  3820. if (!ret)
  3821. goto again;
  3822. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3823. flush_state < COMMIT_TRANS)
  3824. goto again;
  3825. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  3826. flush_state <= COMMIT_TRANS)
  3827. goto again;
  3828. out:
  3829. if (ret == -ENOSPC &&
  3830. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  3831. struct btrfs_block_rsv *global_rsv =
  3832. &root->fs_info->global_block_rsv;
  3833. if (block_rsv != global_rsv &&
  3834. !block_rsv_use_bytes(global_rsv, orig_bytes))
  3835. ret = 0;
  3836. }
  3837. if (flushing) {
  3838. spin_lock(&space_info->lock);
  3839. space_info->flush = 0;
  3840. wake_up_all(&space_info->wait);
  3841. spin_unlock(&space_info->lock);
  3842. }
  3843. return ret;
  3844. }
  3845. static struct btrfs_block_rsv *get_block_rsv(
  3846. const struct btrfs_trans_handle *trans,
  3847. const struct btrfs_root *root)
  3848. {
  3849. struct btrfs_block_rsv *block_rsv = NULL;
  3850. if (root->ref_cows)
  3851. block_rsv = trans->block_rsv;
  3852. if (root == root->fs_info->csum_root && trans->adding_csums)
  3853. block_rsv = trans->block_rsv;
  3854. if (root == root->fs_info->uuid_root)
  3855. block_rsv = trans->block_rsv;
  3856. if (!block_rsv)
  3857. block_rsv = root->block_rsv;
  3858. if (!block_rsv)
  3859. block_rsv = &root->fs_info->empty_block_rsv;
  3860. return block_rsv;
  3861. }
  3862. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3863. u64 num_bytes)
  3864. {
  3865. int ret = -ENOSPC;
  3866. spin_lock(&block_rsv->lock);
  3867. if (block_rsv->reserved >= num_bytes) {
  3868. block_rsv->reserved -= num_bytes;
  3869. if (block_rsv->reserved < block_rsv->size)
  3870. block_rsv->full = 0;
  3871. ret = 0;
  3872. }
  3873. spin_unlock(&block_rsv->lock);
  3874. return ret;
  3875. }
  3876. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3877. u64 num_bytes, int update_size)
  3878. {
  3879. spin_lock(&block_rsv->lock);
  3880. block_rsv->reserved += num_bytes;
  3881. if (update_size)
  3882. block_rsv->size += num_bytes;
  3883. else if (block_rsv->reserved >= block_rsv->size)
  3884. block_rsv->full = 1;
  3885. spin_unlock(&block_rsv->lock);
  3886. }
  3887. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  3888. struct btrfs_block_rsv *dest, u64 num_bytes,
  3889. int min_factor)
  3890. {
  3891. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  3892. u64 min_bytes;
  3893. if (global_rsv->space_info != dest->space_info)
  3894. return -ENOSPC;
  3895. spin_lock(&global_rsv->lock);
  3896. min_bytes = div_factor(global_rsv->size, min_factor);
  3897. if (global_rsv->reserved < min_bytes + num_bytes) {
  3898. spin_unlock(&global_rsv->lock);
  3899. return -ENOSPC;
  3900. }
  3901. global_rsv->reserved -= num_bytes;
  3902. if (global_rsv->reserved < global_rsv->size)
  3903. global_rsv->full = 0;
  3904. spin_unlock(&global_rsv->lock);
  3905. block_rsv_add_bytes(dest, num_bytes, 1);
  3906. return 0;
  3907. }
  3908. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3909. struct btrfs_block_rsv *block_rsv,
  3910. struct btrfs_block_rsv *dest, u64 num_bytes)
  3911. {
  3912. struct btrfs_space_info *space_info = block_rsv->space_info;
  3913. spin_lock(&block_rsv->lock);
  3914. if (num_bytes == (u64)-1)
  3915. num_bytes = block_rsv->size;
  3916. block_rsv->size -= num_bytes;
  3917. if (block_rsv->reserved >= block_rsv->size) {
  3918. num_bytes = block_rsv->reserved - block_rsv->size;
  3919. block_rsv->reserved = block_rsv->size;
  3920. block_rsv->full = 1;
  3921. } else {
  3922. num_bytes = 0;
  3923. }
  3924. spin_unlock(&block_rsv->lock);
  3925. if (num_bytes > 0) {
  3926. if (dest) {
  3927. spin_lock(&dest->lock);
  3928. if (!dest->full) {
  3929. u64 bytes_to_add;
  3930. bytes_to_add = dest->size - dest->reserved;
  3931. bytes_to_add = min(num_bytes, bytes_to_add);
  3932. dest->reserved += bytes_to_add;
  3933. if (dest->reserved >= dest->size)
  3934. dest->full = 1;
  3935. num_bytes -= bytes_to_add;
  3936. }
  3937. spin_unlock(&dest->lock);
  3938. }
  3939. if (num_bytes) {
  3940. spin_lock(&space_info->lock);
  3941. space_info->bytes_may_use -= num_bytes;
  3942. trace_btrfs_space_reservation(fs_info, "space_info",
  3943. space_info->flags, num_bytes, 0);
  3944. spin_unlock(&space_info->lock);
  3945. }
  3946. }
  3947. }
  3948. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3949. struct btrfs_block_rsv *dst, u64 num_bytes)
  3950. {
  3951. int ret;
  3952. ret = block_rsv_use_bytes(src, num_bytes);
  3953. if (ret)
  3954. return ret;
  3955. block_rsv_add_bytes(dst, num_bytes, 1);
  3956. return 0;
  3957. }
  3958. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  3959. {
  3960. memset(rsv, 0, sizeof(*rsv));
  3961. spin_lock_init(&rsv->lock);
  3962. rsv->type = type;
  3963. }
  3964. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  3965. unsigned short type)
  3966. {
  3967. struct btrfs_block_rsv *block_rsv;
  3968. struct btrfs_fs_info *fs_info = root->fs_info;
  3969. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3970. if (!block_rsv)
  3971. return NULL;
  3972. btrfs_init_block_rsv(block_rsv, type);
  3973. block_rsv->space_info = __find_space_info(fs_info,
  3974. BTRFS_BLOCK_GROUP_METADATA);
  3975. return block_rsv;
  3976. }
  3977. void btrfs_free_block_rsv(struct btrfs_root *root,
  3978. struct btrfs_block_rsv *rsv)
  3979. {
  3980. if (!rsv)
  3981. return;
  3982. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3983. kfree(rsv);
  3984. }
  3985. int btrfs_block_rsv_add(struct btrfs_root *root,
  3986. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  3987. enum btrfs_reserve_flush_enum flush)
  3988. {
  3989. int ret;
  3990. if (num_bytes == 0)
  3991. return 0;
  3992. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3993. if (!ret) {
  3994. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3995. return 0;
  3996. }
  3997. return ret;
  3998. }
  3999. int btrfs_block_rsv_check(struct btrfs_root *root,
  4000. struct btrfs_block_rsv *block_rsv, int min_factor)
  4001. {
  4002. u64 num_bytes = 0;
  4003. int ret = -ENOSPC;
  4004. if (!block_rsv)
  4005. return 0;
  4006. spin_lock(&block_rsv->lock);
  4007. num_bytes = div_factor(block_rsv->size, min_factor);
  4008. if (block_rsv->reserved >= num_bytes)
  4009. ret = 0;
  4010. spin_unlock(&block_rsv->lock);
  4011. return ret;
  4012. }
  4013. int btrfs_block_rsv_refill(struct btrfs_root *root,
  4014. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  4015. enum btrfs_reserve_flush_enum flush)
  4016. {
  4017. u64 num_bytes = 0;
  4018. int ret = -ENOSPC;
  4019. if (!block_rsv)
  4020. return 0;
  4021. spin_lock(&block_rsv->lock);
  4022. num_bytes = min_reserved;
  4023. if (block_rsv->reserved >= num_bytes)
  4024. ret = 0;
  4025. else
  4026. num_bytes -= block_rsv->reserved;
  4027. spin_unlock(&block_rsv->lock);
  4028. if (!ret)
  4029. return 0;
  4030. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4031. if (!ret) {
  4032. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  4033. return 0;
  4034. }
  4035. return ret;
  4036. }
  4037. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  4038. struct btrfs_block_rsv *dst_rsv,
  4039. u64 num_bytes)
  4040. {
  4041. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4042. }
  4043. void btrfs_block_rsv_release(struct btrfs_root *root,
  4044. struct btrfs_block_rsv *block_rsv,
  4045. u64 num_bytes)
  4046. {
  4047. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4048. if (global_rsv->full || global_rsv == block_rsv ||
  4049. block_rsv->space_info != global_rsv->space_info)
  4050. global_rsv = NULL;
  4051. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4052. num_bytes);
  4053. }
  4054. /*
  4055. * helper to calculate size of global block reservation.
  4056. * the desired value is sum of space used by extent tree,
  4057. * checksum tree and root tree
  4058. */
  4059. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4060. {
  4061. struct btrfs_space_info *sinfo;
  4062. u64 num_bytes;
  4063. u64 meta_used;
  4064. u64 data_used;
  4065. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4066. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4067. spin_lock(&sinfo->lock);
  4068. data_used = sinfo->bytes_used;
  4069. spin_unlock(&sinfo->lock);
  4070. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4071. spin_lock(&sinfo->lock);
  4072. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4073. data_used = 0;
  4074. meta_used = sinfo->bytes_used;
  4075. spin_unlock(&sinfo->lock);
  4076. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4077. csum_size * 2;
  4078. num_bytes += div64_u64(data_used + meta_used, 50);
  4079. if (num_bytes * 3 > meta_used)
  4080. num_bytes = div64_u64(meta_used, 3);
  4081. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  4082. }
  4083. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4084. {
  4085. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4086. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4087. u64 num_bytes;
  4088. num_bytes = calc_global_metadata_size(fs_info);
  4089. spin_lock(&sinfo->lock);
  4090. spin_lock(&block_rsv->lock);
  4091. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4092. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4093. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4094. sinfo->bytes_may_use;
  4095. if (sinfo->total_bytes > num_bytes) {
  4096. num_bytes = sinfo->total_bytes - num_bytes;
  4097. block_rsv->reserved += num_bytes;
  4098. sinfo->bytes_may_use += num_bytes;
  4099. trace_btrfs_space_reservation(fs_info, "space_info",
  4100. sinfo->flags, num_bytes, 1);
  4101. }
  4102. if (block_rsv->reserved >= block_rsv->size) {
  4103. num_bytes = block_rsv->reserved - block_rsv->size;
  4104. sinfo->bytes_may_use -= num_bytes;
  4105. trace_btrfs_space_reservation(fs_info, "space_info",
  4106. sinfo->flags, num_bytes, 0);
  4107. block_rsv->reserved = block_rsv->size;
  4108. block_rsv->full = 1;
  4109. }
  4110. spin_unlock(&block_rsv->lock);
  4111. spin_unlock(&sinfo->lock);
  4112. }
  4113. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4114. {
  4115. struct btrfs_space_info *space_info;
  4116. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4117. fs_info->chunk_block_rsv.space_info = space_info;
  4118. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4119. fs_info->global_block_rsv.space_info = space_info;
  4120. fs_info->delalloc_block_rsv.space_info = space_info;
  4121. fs_info->trans_block_rsv.space_info = space_info;
  4122. fs_info->empty_block_rsv.space_info = space_info;
  4123. fs_info->delayed_block_rsv.space_info = space_info;
  4124. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4125. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4126. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4127. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4128. if (fs_info->quota_root)
  4129. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4130. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4131. update_global_block_rsv(fs_info);
  4132. }
  4133. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4134. {
  4135. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4136. (u64)-1);
  4137. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4138. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4139. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4140. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4141. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4142. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4143. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4144. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4145. }
  4146. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4147. struct btrfs_root *root)
  4148. {
  4149. if (!trans->block_rsv)
  4150. return;
  4151. if (!trans->bytes_reserved)
  4152. return;
  4153. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4154. trans->transid, trans->bytes_reserved, 0);
  4155. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4156. trans->bytes_reserved = 0;
  4157. }
  4158. /* Can only return 0 or -ENOSPC */
  4159. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4160. struct inode *inode)
  4161. {
  4162. struct btrfs_root *root = BTRFS_I(inode)->root;
  4163. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4164. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4165. /*
  4166. * We need to hold space in order to delete our orphan item once we've
  4167. * added it, so this takes the reservation so we can release it later
  4168. * when we are truly done with the orphan item.
  4169. */
  4170. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4171. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4172. btrfs_ino(inode), num_bytes, 1);
  4173. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4174. }
  4175. void btrfs_orphan_release_metadata(struct inode *inode)
  4176. {
  4177. struct btrfs_root *root = BTRFS_I(inode)->root;
  4178. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4179. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4180. btrfs_ino(inode), num_bytes, 0);
  4181. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4182. }
  4183. /*
  4184. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4185. * root: the root of the parent directory
  4186. * rsv: block reservation
  4187. * items: the number of items that we need do reservation
  4188. * qgroup_reserved: used to return the reserved size in qgroup
  4189. *
  4190. * This function is used to reserve the space for snapshot/subvolume
  4191. * creation and deletion. Those operations are different with the
  4192. * common file/directory operations, they change two fs/file trees
  4193. * and root tree, the number of items that the qgroup reserves is
  4194. * different with the free space reservation. So we can not use
  4195. * the space reseravtion mechanism in start_transaction().
  4196. */
  4197. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4198. struct btrfs_block_rsv *rsv,
  4199. int items,
  4200. u64 *qgroup_reserved,
  4201. bool use_global_rsv)
  4202. {
  4203. u64 num_bytes;
  4204. int ret;
  4205. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4206. if (root->fs_info->quota_enabled) {
  4207. /* One for parent inode, two for dir entries */
  4208. num_bytes = 3 * root->leafsize;
  4209. ret = btrfs_qgroup_reserve(root, num_bytes);
  4210. if (ret)
  4211. return ret;
  4212. } else {
  4213. num_bytes = 0;
  4214. }
  4215. *qgroup_reserved = num_bytes;
  4216. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4217. rsv->space_info = __find_space_info(root->fs_info,
  4218. BTRFS_BLOCK_GROUP_METADATA);
  4219. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4220. BTRFS_RESERVE_FLUSH_ALL);
  4221. if (ret == -ENOSPC && use_global_rsv)
  4222. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
  4223. if (ret) {
  4224. if (*qgroup_reserved)
  4225. btrfs_qgroup_free(root, *qgroup_reserved);
  4226. }
  4227. return ret;
  4228. }
  4229. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4230. struct btrfs_block_rsv *rsv,
  4231. u64 qgroup_reserved)
  4232. {
  4233. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4234. if (qgroup_reserved)
  4235. btrfs_qgroup_free(root, qgroup_reserved);
  4236. }
  4237. /**
  4238. * drop_outstanding_extent - drop an outstanding extent
  4239. * @inode: the inode we're dropping the extent for
  4240. *
  4241. * This is called when we are freeing up an outstanding extent, either called
  4242. * after an error or after an extent is written. This will return the number of
  4243. * reserved extents that need to be freed. This must be called with
  4244. * BTRFS_I(inode)->lock held.
  4245. */
  4246. static unsigned drop_outstanding_extent(struct inode *inode)
  4247. {
  4248. unsigned drop_inode_space = 0;
  4249. unsigned dropped_extents = 0;
  4250. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  4251. BTRFS_I(inode)->outstanding_extents--;
  4252. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4253. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4254. &BTRFS_I(inode)->runtime_flags))
  4255. drop_inode_space = 1;
  4256. /*
  4257. * If we have more or the same amount of outsanding extents than we have
  4258. * reserved then we need to leave the reserved extents count alone.
  4259. */
  4260. if (BTRFS_I(inode)->outstanding_extents >=
  4261. BTRFS_I(inode)->reserved_extents)
  4262. return drop_inode_space;
  4263. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4264. BTRFS_I(inode)->outstanding_extents;
  4265. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4266. return dropped_extents + drop_inode_space;
  4267. }
  4268. /**
  4269. * calc_csum_metadata_size - return the amount of metada space that must be
  4270. * reserved/free'd for the given bytes.
  4271. * @inode: the inode we're manipulating
  4272. * @num_bytes: the number of bytes in question
  4273. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4274. *
  4275. * This adjusts the number of csum_bytes in the inode and then returns the
  4276. * correct amount of metadata that must either be reserved or freed. We
  4277. * calculate how many checksums we can fit into one leaf and then divide the
  4278. * number of bytes that will need to be checksumed by this value to figure out
  4279. * how many checksums will be required. If we are adding bytes then the number
  4280. * may go up and we will return the number of additional bytes that must be
  4281. * reserved. If it is going down we will return the number of bytes that must
  4282. * be freed.
  4283. *
  4284. * This must be called with BTRFS_I(inode)->lock held.
  4285. */
  4286. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4287. int reserve)
  4288. {
  4289. struct btrfs_root *root = BTRFS_I(inode)->root;
  4290. u64 csum_size;
  4291. int num_csums_per_leaf;
  4292. int num_csums;
  4293. int old_csums;
  4294. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4295. BTRFS_I(inode)->csum_bytes == 0)
  4296. return 0;
  4297. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4298. if (reserve)
  4299. BTRFS_I(inode)->csum_bytes += num_bytes;
  4300. else
  4301. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4302. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  4303. num_csums_per_leaf = (int)div64_u64(csum_size,
  4304. sizeof(struct btrfs_csum_item) +
  4305. sizeof(struct btrfs_disk_key));
  4306. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4307. num_csums = num_csums + num_csums_per_leaf - 1;
  4308. num_csums = num_csums / num_csums_per_leaf;
  4309. old_csums = old_csums + num_csums_per_leaf - 1;
  4310. old_csums = old_csums / num_csums_per_leaf;
  4311. /* No change, no need to reserve more */
  4312. if (old_csums == num_csums)
  4313. return 0;
  4314. if (reserve)
  4315. return btrfs_calc_trans_metadata_size(root,
  4316. num_csums - old_csums);
  4317. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4318. }
  4319. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4320. {
  4321. struct btrfs_root *root = BTRFS_I(inode)->root;
  4322. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4323. u64 to_reserve = 0;
  4324. u64 csum_bytes;
  4325. unsigned nr_extents = 0;
  4326. int extra_reserve = 0;
  4327. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4328. int ret = 0;
  4329. bool delalloc_lock = true;
  4330. u64 to_free = 0;
  4331. unsigned dropped;
  4332. /* If we are a free space inode we need to not flush since we will be in
  4333. * the middle of a transaction commit. We also don't need the delalloc
  4334. * mutex since we won't race with anybody. We need this mostly to make
  4335. * lockdep shut its filthy mouth.
  4336. */
  4337. if (btrfs_is_free_space_inode(inode)) {
  4338. flush = BTRFS_RESERVE_NO_FLUSH;
  4339. delalloc_lock = false;
  4340. }
  4341. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4342. btrfs_transaction_in_commit(root->fs_info))
  4343. schedule_timeout(1);
  4344. if (delalloc_lock)
  4345. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4346. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4347. spin_lock(&BTRFS_I(inode)->lock);
  4348. BTRFS_I(inode)->outstanding_extents++;
  4349. if (BTRFS_I(inode)->outstanding_extents >
  4350. BTRFS_I(inode)->reserved_extents)
  4351. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4352. BTRFS_I(inode)->reserved_extents;
  4353. /*
  4354. * Add an item to reserve for updating the inode when we complete the
  4355. * delalloc io.
  4356. */
  4357. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4358. &BTRFS_I(inode)->runtime_flags)) {
  4359. nr_extents++;
  4360. extra_reserve = 1;
  4361. }
  4362. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4363. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4364. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4365. spin_unlock(&BTRFS_I(inode)->lock);
  4366. if (root->fs_info->quota_enabled) {
  4367. ret = btrfs_qgroup_reserve(root, num_bytes +
  4368. nr_extents * root->leafsize);
  4369. if (ret)
  4370. goto out_fail;
  4371. }
  4372. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4373. if (unlikely(ret)) {
  4374. if (root->fs_info->quota_enabled)
  4375. btrfs_qgroup_free(root, num_bytes +
  4376. nr_extents * root->leafsize);
  4377. goto out_fail;
  4378. }
  4379. spin_lock(&BTRFS_I(inode)->lock);
  4380. if (extra_reserve) {
  4381. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4382. &BTRFS_I(inode)->runtime_flags);
  4383. nr_extents--;
  4384. }
  4385. BTRFS_I(inode)->reserved_extents += nr_extents;
  4386. spin_unlock(&BTRFS_I(inode)->lock);
  4387. if (delalloc_lock)
  4388. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4389. if (to_reserve)
  4390. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  4391. btrfs_ino(inode), to_reserve, 1);
  4392. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4393. return 0;
  4394. out_fail:
  4395. spin_lock(&BTRFS_I(inode)->lock);
  4396. dropped = drop_outstanding_extent(inode);
  4397. /*
  4398. * If the inodes csum_bytes is the same as the original
  4399. * csum_bytes then we know we haven't raced with any free()ers
  4400. * so we can just reduce our inodes csum bytes and carry on.
  4401. */
  4402. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4403. calc_csum_metadata_size(inode, num_bytes, 0);
  4404. } else {
  4405. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4406. u64 bytes;
  4407. /*
  4408. * This is tricky, but first we need to figure out how much we
  4409. * free'd from any free-ers that occured during this
  4410. * reservation, so we reset ->csum_bytes to the csum_bytes
  4411. * before we dropped our lock, and then call the free for the
  4412. * number of bytes that were freed while we were trying our
  4413. * reservation.
  4414. */
  4415. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4416. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4417. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4418. /*
  4419. * Now we need to see how much we would have freed had we not
  4420. * been making this reservation and our ->csum_bytes were not
  4421. * artificially inflated.
  4422. */
  4423. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4424. bytes = csum_bytes - orig_csum_bytes;
  4425. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4426. /*
  4427. * Now reset ->csum_bytes to what it should be. If bytes is
  4428. * more than to_free then we would have free'd more space had we
  4429. * not had an artificially high ->csum_bytes, so we need to free
  4430. * the remainder. If bytes is the same or less then we don't
  4431. * need to do anything, the other free-ers did the correct
  4432. * thing.
  4433. */
  4434. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4435. if (bytes > to_free)
  4436. to_free = bytes - to_free;
  4437. else
  4438. to_free = 0;
  4439. }
  4440. spin_unlock(&BTRFS_I(inode)->lock);
  4441. if (dropped)
  4442. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4443. if (to_free) {
  4444. btrfs_block_rsv_release(root, block_rsv, to_free);
  4445. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4446. btrfs_ino(inode), to_free, 0);
  4447. }
  4448. if (delalloc_lock)
  4449. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4450. return ret;
  4451. }
  4452. /**
  4453. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4454. * @inode: the inode to release the reservation for
  4455. * @num_bytes: the number of bytes we're releasing
  4456. *
  4457. * This will release the metadata reservation for an inode. This can be called
  4458. * once we complete IO for a given set of bytes to release their metadata
  4459. * reservations.
  4460. */
  4461. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4462. {
  4463. struct btrfs_root *root = BTRFS_I(inode)->root;
  4464. u64 to_free = 0;
  4465. unsigned dropped;
  4466. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4467. spin_lock(&BTRFS_I(inode)->lock);
  4468. dropped = drop_outstanding_extent(inode);
  4469. if (num_bytes)
  4470. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4471. spin_unlock(&BTRFS_I(inode)->lock);
  4472. if (dropped > 0)
  4473. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4474. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4475. btrfs_ino(inode), to_free, 0);
  4476. if (root->fs_info->quota_enabled) {
  4477. btrfs_qgroup_free(root, num_bytes +
  4478. dropped * root->leafsize);
  4479. }
  4480. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4481. to_free);
  4482. }
  4483. /**
  4484. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4485. * @inode: inode we're writing to
  4486. * @num_bytes: the number of bytes we want to allocate
  4487. *
  4488. * This will do the following things
  4489. *
  4490. * o reserve space in the data space info for num_bytes
  4491. * o reserve space in the metadata space info based on number of outstanding
  4492. * extents and how much csums will be needed
  4493. * o add to the inodes ->delalloc_bytes
  4494. * o add it to the fs_info's delalloc inodes list.
  4495. *
  4496. * This will return 0 for success and -ENOSPC if there is no space left.
  4497. */
  4498. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4499. {
  4500. int ret;
  4501. ret = btrfs_check_data_free_space(inode, num_bytes);
  4502. if (ret)
  4503. return ret;
  4504. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4505. if (ret) {
  4506. btrfs_free_reserved_data_space(inode, num_bytes);
  4507. return ret;
  4508. }
  4509. return 0;
  4510. }
  4511. /**
  4512. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4513. * @inode: inode we're releasing space for
  4514. * @num_bytes: the number of bytes we want to free up
  4515. *
  4516. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4517. * called in the case that we don't need the metadata AND data reservations
  4518. * anymore. So if there is an error or we insert an inline extent.
  4519. *
  4520. * This function will release the metadata space that was not used and will
  4521. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4522. * list if there are no delalloc bytes left.
  4523. */
  4524. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4525. {
  4526. btrfs_delalloc_release_metadata(inode, num_bytes);
  4527. btrfs_free_reserved_data_space(inode, num_bytes);
  4528. }
  4529. static int update_block_group(struct btrfs_root *root,
  4530. u64 bytenr, u64 num_bytes, int alloc)
  4531. {
  4532. struct btrfs_block_group_cache *cache = NULL;
  4533. struct btrfs_fs_info *info = root->fs_info;
  4534. u64 total = num_bytes;
  4535. u64 old_val;
  4536. u64 byte_in_group;
  4537. int factor;
  4538. /* block accounting for super block */
  4539. spin_lock(&info->delalloc_root_lock);
  4540. old_val = btrfs_super_bytes_used(info->super_copy);
  4541. if (alloc)
  4542. old_val += num_bytes;
  4543. else
  4544. old_val -= num_bytes;
  4545. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4546. spin_unlock(&info->delalloc_root_lock);
  4547. while (total) {
  4548. cache = btrfs_lookup_block_group(info, bytenr);
  4549. if (!cache)
  4550. return -ENOENT;
  4551. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4552. BTRFS_BLOCK_GROUP_RAID1 |
  4553. BTRFS_BLOCK_GROUP_RAID10))
  4554. factor = 2;
  4555. else
  4556. factor = 1;
  4557. /*
  4558. * If this block group has free space cache written out, we
  4559. * need to make sure to load it if we are removing space. This
  4560. * is because we need the unpinning stage to actually add the
  4561. * space back to the block group, otherwise we will leak space.
  4562. */
  4563. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4564. cache_block_group(cache, 1);
  4565. byte_in_group = bytenr - cache->key.objectid;
  4566. WARN_ON(byte_in_group > cache->key.offset);
  4567. spin_lock(&cache->space_info->lock);
  4568. spin_lock(&cache->lock);
  4569. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4570. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4571. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4572. cache->dirty = 1;
  4573. old_val = btrfs_block_group_used(&cache->item);
  4574. num_bytes = min(total, cache->key.offset - byte_in_group);
  4575. if (alloc) {
  4576. old_val += num_bytes;
  4577. btrfs_set_block_group_used(&cache->item, old_val);
  4578. cache->reserved -= num_bytes;
  4579. cache->space_info->bytes_reserved -= num_bytes;
  4580. cache->space_info->bytes_used += num_bytes;
  4581. cache->space_info->disk_used += num_bytes * factor;
  4582. spin_unlock(&cache->lock);
  4583. spin_unlock(&cache->space_info->lock);
  4584. } else {
  4585. old_val -= num_bytes;
  4586. btrfs_set_block_group_used(&cache->item, old_val);
  4587. cache->pinned += num_bytes;
  4588. cache->space_info->bytes_pinned += num_bytes;
  4589. cache->space_info->bytes_used -= num_bytes;
  4590. cache->space_info->disk_used -= num_bytes * factor;
  4591. spin_unlock(&cache->lock);
  4592. spin_unlock(&cache->space_info->lock);
  4593. set_extent_dirty(info->pinned_extents,
  4594. bytenr, bytenr + num_bytes - 1,
  4595. GFP_NOFS | __GFP_NOFAIL);
  4596. }
  4597. btrfs_put_block_group(cache);
  4598. total -= num_bytes;
  4599. bytenr += num_bytes;
  4600. }
  4601. return 0;
  4602. }
  4603. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4604. {
  4605. struct btrfs_block_group_cache *cache;
  4606. u64 bytenr;
  4607. spin_lock(&root->fs_info->block_group_cache_lock);
  4608. bytenr = root->fs_info->first_logical_byte;
  4609. spin_unlock(&root->fs_info->block_group_cache_lock);
  4610. if (bytenr < (u64)-1)
  4611. return bytenr;
  4612. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4613. if (!cache)
  4614. return 0;
  4615. bytenr = cache->key.objectid;
  4616. btrfs_put_block_group(cache);
  4617. return bytenr;
  4618. }
  4619. static int pin_down_extent(struct btrfs_root *root,
  4620. struct btrfs_block_group_cache *cache,
  4621. u64 bytenr, u64 num_bytes, int reserved)
  4622. {
  4623. spin_lock(&cache->space_info->lock);
  4624. spin_lock(&cache->lock);
  4625. cache->pinned += num_bytes;
  4626. cache->space_info->bytes_pinned += num_bytes;
  4627. if (reserved) {
  4628. cache->reserved -= num_bytes;
  4629. cache->space_info->bytes_reserved -= num_bytes;
  4630. }
  4631. spin_unlock(&cache->lock);
  4632. spin_unlock(&cache->space_info->lock);
  4633. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4634. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4635. if (reserved)
  4636. trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
  4637. return 0;
  4638. }
  4639. /*
  4640. * this function must be called within transaction
  4641. */
  4642. int btrfs_pin_extent(struct btrfs_root *root,
  4643. u64 bytenr, u64 num_bytes, int reserved)
  4644. {
  4645. struct btrfs_block_group_cache *cache;
  4646. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4647. BUG_ON(!cache); /* Logic error */
  4648. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4649. btrfs_put_block_group(cache);
  4650. return 0;
  4651. }
  4652. /*
  4653. * this function must be called within transaction
  4654. */
  4655. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  4656. u64 bytenr, u64 num_bytes)
  4657. {
  4658. struct btrfs_block_group_cache *cache;
  4659. int ret;
  4660. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4661. if (!cache)
  4662. return -EINVAL;
  4663. /*
  4664. * pull in the free space cache (if any) so that our pin
  4665. * removes the free space from the cache. We have load_only set
  4666. * to one because the slow code to read in the free extents does check
  4667. * the pinned extents.
  4668. */
  4669. cache_block_group(cache, 1);
  4670. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4671. /* remove us from the free space cache (if we're there at all) */
  4672. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  4673. btrfs_put_block_group(cache);
  4674. return ret;
  4675. }
  4676. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  4677. {
  4678. int ret;
  4679. struct btrfs_block_group_cache *block_group;
  4680. struct btrfs_caching_control *caching_ctl;
  4681. block_group = btrfs_lookup_block_group(root->fs_info, start);
  4682. if (!block_group)
  4683. return -EINVAL;
  4684. cache_block_group(block_group, 0);
  4685. caching_ctl = get_caching_control(block_group);
  4686. if (!caching_ctl) {
  4687. /* Logic error */
  4688. BUG_ON(!block_group_cache_done(block_group));
  4689. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4690. } else {
  4691. mutex_lock(&caching_ctl->mutex);
  4692. if (start >= caching_ctl->progress) {
  4693. ret = add_excluded_extent(root, start, num_bytes);
  4694. } else if (start + num_bytes <= caching_ctl->progress) {
  4695. ret = btrfs_remove_free_space(block_group,
  4696. start, num_bytes);
  4697. } else {
  4698. num_bytes = caching_ctl->progress - start;
  4699. ret = btrfs_remove_free_space(block_group,
  4700. start, num_bytes);
  4701. if (ret)
  4702. goto out_lock;
  4703. num_bytes = (start + num_bytes) -
  4704. caching_ctl->progress;
  4705. start = caching_ctl->progress;
  4706. ret = add_excluded_extent(root, start, num_bytes);
  4707. }
  4708. out_lock:
  4709. mutex_unlock(&caching_ctl->mutex);
  4710. put_caching_control(caching_ctl);
  4711. }
  4712. btrfs_put_block_group(block_group);
  4713. return ret;
  4714. }
  4715. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  4716. struct extent_buffer *eb)
  4717. {
  4718. struct btrfs_file_extent_item *item;
  4719. struct btrfs_key key;
  4720. int found_type;
  4721. int i;
  4722. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  4723. return 0;
  4724. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  4725. btrfs_item_key_to_cpu(eb, &key, i);
  4726. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4727. continue;
  4728. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  4729. found_type = btrfs_file_extent_type(eb, item);
  4730. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  4731. continue;
  4732. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  4733. continue;
  4734. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  4735. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  4736. __exclude_logged_extent(log, key.objectid, key.offset);
  4737. }
  4738. return 0;
  4739. }
  4740. /**
  4741. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4742. * @cache: The cache we are manipulating
  4743. * @num_bytes: The number of bytes in question
  4744. * @reserve: One of the reservation enums
  4745. *
  4746. * This is called by the allocator when it reserves space, or by somebody who is
  4747. * freeing space that was never actually used on disk. For example if you
  4748. * reserve some space for a new leaf in transaction A and before transaction A
  4749. * commits you free that leaf, you call this with reserve set to 0 in order to
  4750. * clear the reservation.
  4751. *
  4752. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4753. * ENOSPC accounting. For data we handle the reservation through clearing the
  4754. * delalloc bits in the io_tree. We have to do this since we could end up
  4755. * allocating less disk space for the amount of data we have reserved in the
  4756. * case of compression.
  4757. *
  4758. * If this is a reservation and the block group has become read only we cannot
  4759. * make the reservation and return -EAGAIN, otherwise this function always
  4760. * succeeds.
  4761. */
  4762. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4763. u64 num_bytes, int reserve)
  4764. {
  4765. struct btrfs_space_info *space_info = cache->space_info;
  4766. int ret = 0;
  4767. spin_lock(&space_info->lock);
  4768. spin_lock(&cache->lock);
  4769. if (reserve != RESERVE_FREE) {
  4770. if (cache->ro) {
  4771. ret = -EAGAIN;
  4772. } else {
  4773. cache->reserved += num_bytes;
  4774. space_info->bytes_reserved += num_bytes;
  4775. if (reserve == RESERVE_ALLOC) {
  4776. trace_btrfs_space_reservation(cache->fs_info,
  4777. "space_info", space_info->flags,
  4778. num_bytes, 0);
  4779. space_info->bytes_may_use -= num_bytes;
  4780. }
  4781. }
  4782. } else {
  4783. if (cache->ro)
  4784. space_info->bytes_readonly += num_bytes;
  4785. cache->reserved -= num_bytes;
  4786. space_info->bytes_reserved -= num_bytes;
  4787. }
  4788. spin_unlock(&cache->lock);
  4789. spin_unlock(&space_info->lock);
  4790. return ret;
  4791. }
  4792. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4793. struct btrfs_root *root)
  4794. {
  4795. struct btrfs_fs_info *fs_info = root->fs_info;
  4796. struct btrfs_caching_control *next;
  4797. struct btrfs_caching_control *caching_ctl;
  4798. struct btrfs_block_group_cache *cache;
  4799. struct btrfs_space_info *space_info;
  4800. down_write(&fs_info->extent_commit_sem);
  4801. list_for_each_entry_safe(caching_ctl, next,
  4802. &fs_info->caching_block_groups, list) {
  4803. cache = caching_ctl->block_group;
  4804. if (block_group_cache_done(cache)) {
  4805. cache->last_byte_to_unpin = (u64)-1;
  4806. list_del_init(&caching_ctl->list);
  4807. put_caching_control(caching_ctl);
  4808. } else {
  4809. cache->last_byte_to_unpin = caching_ctl->progress;
  4810. }
  4811. }
  4812. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4813. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4814. else
  4815. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4816. up_write(&fs_info->extent_commit_sem);
  4817. list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
  4818. percpu_counter_set(&space_info->total_bytes_pinned, 0);
  4819. update_global_block_rsv(fs_info);
  4820. }
  4821. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4822. {
  4823. struct btrfs_fs_info *fs_info = root->fs_info;
  4824. struct btrfs_block_group_cache *cache = NULL;
  4825. struct btrfs_space_info *space_info;
  4826. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4827. u64 len;
  4828. bool readonly;
  4829. while (start <= end) {
  4830. readonly = false;
  4831. if (!cache ||
  4832. start >= cache->key.objectid + cache->key.offset) {
  4833. if (cache)
  4834. btrfs_put_block_group(cache);
  4835. cache = btrfs_lookup_block_group(fs_info, start);
  4836. BUG_ON(!cache); /* Logic error */
  4837. }
  4838. len = cache->key.objectid + cache->key.offset - start;
  4839. len = min(len, end + 1 - start);
  4840. if (start < cache->last_byte_to_unpin) {
  4841. len = min(len, cache->last_byte_to_unpin - start);
  4842. btrfs_add_free_space(cache, start, len);
  4843. }
  4844. start += len;
  4845. space_info = cache->space_info;
  4846. spin_lock(&space_info->lock);
  4847. spin_lock(&cache->lock);
  4848. cache->pinned -= len;
  4849. space_info->bytes_pinned -= len;
  4850. if (cache->ro) {
  4851. space_info->bytes_readonly += len;
  4852. readonly = true;
  4853. }
  4854. spin_unlock(&cache->lock);
  4855. if (!readonly && global_rsv->space_info == space_info) {
  4856. spin_lock(&global_rsv->lock);
  4857. if (!global_rsv->full) {
  4858. len = min(len, global_rsv->size -
  4859. global_rsv->reserved);
  4860. global_rsv->reserved += len;
  4861. space_info->bytes_may_use += len;
  4862. if (global_rsv->reserved >= global_rsv->size)
  4863. global_rsv->full = 1;
  4864. }
  4865. spin_unlock(&global_rsv->lock);
  4866. }
  4867. spin_unlock(&space_info->lock);
  4868. }
  4869. if (cache)
  4870. btrfs_put_block_group(cache);
  4871. return 0;
  4872. }
  4873. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4874. struct btrfs_root *root)
  4875. {
  4876. struct btrfs_fs_info *fs_info = root->fs_info;
  4877. struct extent_io_tree *unpin;
  4878. u64 start;
  4879. u64 end;
  4880. int ret;
  4881. if (trans->aborted)
  4882. return 0;
  4883. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4884. unpin = &fs_info->freed_extents[1];
  4885. else
  4886. unpin = &fs_info->freed_extents[0];
  4887. while (1) {
  4888. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4889. EXTENT_DIRTY, NULL);
  4890. if (ret)
  4891. break;
  4892. if (btrfs_test_opt(root, DISCARD))
  4893. ret = btrfs_discard_extent(root, start,
  4894. end + 1 - start, NULL);
  4895. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4896. unpin_extent_range(root, start, end);
  4897. cond_resched();
  4898. }
  4899. return 0;
  4900. }
  4901. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
  4902. u64 owner, u64 root_objectid)
  4903. {
  4904. struct btrfs_space_info *space_info;
  4905. u64 flags;
  4906. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4907. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  4908. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  4909. else
  4910. flags = BTRFS_BLOCK_GROUP_METADATA;
  4911. } else {
  4912. flags = BTRFS_BLOCK_GROUP_DATA;
  4913. }
  4914. space_info = __find_space_info(fs_info, flags);
  4915. BUG_ON(!space_info); /* Logic bug */
  4916. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  4917. }
  4918. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4919. struct btrfs_root *root,
  4920. u64 bytenr, u64 num_bytes, u64 parent,
  4921. u64 root_objectid, u64 owner_objectid,
  4922. u64 owner_offset, int refs_to_drop,
  4923. struct btrfs_delayed_extent_op *extent_op)
  4924. {
  4925. struct btrfs_key key;
  4926. struct btrfs_path *path;
  4927. struct btrfs_fs_info *info = root->fs_info;
  4928. struct btrfs_root *extent_root = info->extent_root;
  4929. struct extent_buffer *leaf;
  4930. struct btrfs_extent_item *ei;
  4931. struct btrfs_extent_inline_ref *iref;
  4932. int ret;
  4933. int is_data;
  4934. int extent_slot = 0;
  4935. int found_extent = 0;
  4936. int num_to_del = 1;
  4937. u32 item_size;
  4938. u64 refs;
  4939. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  4940. SKINNY_METADATA);
  4941. path = btrfs_alloc_path();
  4942. if (!path)
  4943. return -ENOMEM;
  4944. path->reada = 1;
  4945. path->leave_spinning = 1;
  4946. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4947. BUG_ON(!is_data && refs_to_drop != 1);
  4948. if (is_data)
  4949. skinny_metadata = 0;
  4950. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4951. bytenr, num_bytes, parent,
  4952. root_objectid, owner_objectid,
  4953. owner_offset);
  4954. if (ret == 0) {
  4955. extent_slot = path->slots[0];
  4956. while (extent_slot >= 0) {
  4957. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4958. extent_slot);
  4959. if (key.objectid != bytenr)
  4960. break;
  4961. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4962. key.offset == num_bytes) {
  4963. found_extent = 1;
  4964. break;
  4965. }
  4966. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  4967. key.offset == owner_objectid) {
  4968. found_extent = 1;
  4969. break;
  4970. }
  4971. if (path->slots[0] - extent_slot > 5)
  4972. break;
  4973. extent_slot--;
  4974. }
  4975. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4976. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4977. if (found_extent && item_size < sizeof(*ei))
  4978. found_extent = 0;
  4979. #endif
  4980. if (!found_extent) {
  4981. BUG_ON(iref);
  4982. ret = remove_extent_backref(trans, extent_root, path,
  4983. NULL, refs_to_drop,
  4984. is_data);
  4985. if (ret) {
  4986. btrfs_abort_transaction(trans, extent_root, ret);
  4987. goto out;
  4988. }
  4989. btrfs_release_path(path);
  4990. path->leave_spinning = 1;
  4991. key.objectid = bytenr;
  4992. key.type = BTRFS_EXTENT_ITEM_KEY;
  4993. key.offset = num_bytes;
  4994. if (!is_data && skinny_metadata) {
  4995. key.type = BTRFS_METADATA_ITEM_KEY;
  4996. key.offset = owner_objectid;
  4997. }
  4998. ret = btrfs_search_slot(trans, extent_root,
  4999. &key, path, -1, 1);
  5000. if (ret > 0 && skinny_metadata && path->slots[0]) {
  5001. /*
  5002. * Couldn't find our skinny metadata item,
  5003. * see if we have ye olde extent item.
  5004. */
  5005. path->slots[0]--;
  5006. btrfs_item_key_to_cpu(path->nodes[0], &key,
  5007. path->slots[0]);
  5008. if (key.objectid == bytenr &&
  5009. key.type == BTRFS_EXTENT_ITEM_KEY &&
  5010. key.offset == num_bytes)
  5011. ret = 0;
  5012. }
  5013. if (ret > 0 && skinny_metadata) {
  5014. skinny_metadata = false;
  5015. key.type = BTRFS_EXTENT_ITEM_KEY;
  5016. key.offset = num_bytes;
  5017. btrfs_release_path(path);
  5018. ret = btrfs_search_slot(trans, extent_root,
  5019. &key, path, -1, 1);
  5020. }
  5021. if (ret) {
  5022. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5023. ret, bytenr);
  5024. if (ret > 0)
  5025. btrfs_print_leaf(extent_root,
  5026. path->nodes[0]);
  5027. }
  5028. if (ret < 0) {
  5029. btrfs_abort_transaction(trans, extent_root, ret);
  5030. goto out;
  5031. }
  5032. extent_slot = path->slots[0];
  5033. }
  5034. } else if (ret == -ENOENT) {
  5035. btrfs_print_leaf(extent_root, path->nodes[0]);
  5036. WARN_ON(1);
  5037. btrfs_err(info,
  5038. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  5039. bytenr, parent, root_objectid, owner_objectid,
  5040. owner_offset);
  5041. } else {
  5042. btrfs_abort_transaction(trans, extent_root, ret);
  5043. goto out;
  5044. }
  5045. leaf = path->nodes[0];
  5046. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5047. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5048. if (item_size < sizeof(*ei)) {
  5049. BUG_ON(found_extent || extent_slot != path->slots[0]);
  5050. ret = convert_extent_item_v0(trans, extent_root, path,
  5051. owner_objectid, 0);
  5052. if (ret < 0) {
  5053. btrfs_abort_transaction(trans, extent_root, ret);
  5054. goto out;
  5055. }
  5056. btrfs_release_path(path);
  5057. path->leave_spinning = 1;
  5058. key.objectid = bytenr;
  5059. key.type = BTRFS_EXTENT_ITEM_KEY;
  5060. key.offset = num_bytes;
  5061. ret = btrfs_search_slot(trans, extent_root, &key, path,
  5062. -1, 1);
  5063. if (ret) {
  5064. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5065. ret, bytenr);
  5066. btrfs_print_leaf(extent_root, path->nodes[0]);
  5067. }
  5068. if (ret < 0) {
  5069. btrfs_abort_transaction(trans, extent_root, ret);
  5070. goto out;
  5071. }
  5072. extent_slot = path->slots[0];
  5073. leaf = path->nodes[0];
  5074. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5075. }
  5076. #endif
  5077. BUG_ON(item_size < sizeof(*ei));
  5078. ei = btrfs_item_ptr(leaf, extent_slot,
  5079. struct btrfs_extent_item);
  5080. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5081. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5082. struct btrfs_tree_block_info *bi;
  5083. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5084. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5085. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5086. }
  5087. refs = btrfs_extent_refs(leaf, ei);
  5088. if (refs < refs_to_drop) {
  5089. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5090. "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
  5091. ret = -EINVAL;
  5092. btrfs_abort_transaction(trans, extent_root, ret);
  5093. goto out;
  5094. }
  5095. refs -= refs_to_drop;
  5096. if (refs > 0) {
  5097. if (extent_op)
  5098. __run_delayed_extent_op(extent_op, leaf, ei);
  5099. /*
  5100. * In the case of inline back ref, reference count will
  5101. * be updated by remove_extent_backref
  5102. */
  5103. if (iref) {
  5104. BUG_ON(!found_extent);
  5105. } else {
  5106. btrfs_set_extent_refs(leaf, ei, refs);
  5107. btrfs_mark_buffer_dirty(leaf);
  5108. }
  5109. if (found_extent) {
  5110. ret = remove_extent_backref(trans, extent_root, path,
  5111. iref, refs_to_drop,
  5112. is_data);
  5113. if (ret) {
  5114. btrfs_abort_transaction(trans, extent_root, ret);
  5115. goto out;
  5116. }
  5117. }
  5118. add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
  5119. root_objectid);
  5120. } else {
  5121. if (found_extent) {
  5122. BUG_ON(is_data && refs_to_drop !=
  5123. extent_data_ref_count(root, path, iref));
  5124. if (iref) {
  5125. BUG_ON(path->slots[0] != extent_slot);
  5126. } else {
  5127. BUG_ON(path->slots[0] != extent_slot + 1);
  5128. path->slots[0] = extent_slot;
  5129. num_to_del = 2;
  5130. }
  5131. }
  5132. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5133. num_to_del);
  5134. if (ret) {
  5135. btrfs_abort_transaction(trans, extent_root, ret);
  5136. goto out;
  5137. }
  5138. btrfs_release_path(path);
  5139. if (is_data) {
  5140. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5141. if (ret) {
  5142. btrfs_abort_transaction(trans, extent_root, ret);
  5143. goto out;
  5144. }
  5145. }
  5146. ret = update_block_group(root, bytenr, num_bytes, 0);
  5147. if (ret) {
  5148. btrfs_abort_transaction(trans, extent_root, ret);
  5149. goto out;
  5150. }
  5151. }
  5152. out:
  5153. btrfs_free_path(path);
  5154. return ret;
  5155. }
  5156. /*
  5157. * when we free an block, it is possible (and likely) that we free the last
  5158. * delayed ref for that extent as well. This searches the delayed ref tree for
  5159. * a given extent, and if there are no other delayed refs to be processed, it
  5160. * removes it from the tree.
  5161. */
  5162. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5163. struct btrfs_root *root, u64 bytenr)
  5164. {
  5165. struct btrfs_delayed_ref_head *head;
  5166. struct btrfs_delayed_ref_root *delayed_refs;
  5167. struct btrfs_delayed_ref_node *ref;
  5168. struct rb_node *node;
  5169. int ret = 0;
  5170. delayed_refs = &trans->transaction->delayed_refs;
  5171. spin_lock(&delayed_refs->lock);
  5172. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5173. if (!head)
  5174. goto out;
  5175. node = rb_prev(&head->node.rb_node);
  5176. if (!node)
  5177. goto out;
  5178. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  5179. /* there are still entries for this ref, we can't drop it */
  5180. if (ref->bytenr == bytenr)
  5181. goto out;
  5182. if (head->extent_op) {
  5183. if (!head->must_insert_reserved)
  5184. goto out;
  5185. btrfs_free_delayed_extent_op(head->extent_op);
  5186. head->extent_op = NULL;
  5187. }
  5188. /*
  5189. * waiting for the lock here would deadlock. If someone else has it
  5190. * locked they are already in the process of dropping it anyway
  5191. */
  5192. if (!mutex_trylock(&head->mutex))
  5193. goto out;
  5194. /*
  5195. * at this point we have a head with no other entries. Go
  5196. * ahead and process it.
  5197. */
  5198. head->node.in_tree = 0;
  5199. rb_erase(&head->node.rb_node, &delayed_refs->root);
  5200. delayed_refs->num_entries--;
  5201. /*
  5202. * we don't take a ref on the node because we're removing it from the
  5203. * tree, so we just steal the ref the tree was holding.
  5204. */
  5205. delayed_refs->num_heads--;
  5206. if (list_empty(&head->cluster))
  5207. delayed_refs->num_heads_ready--;
  5208. list_del_init(&head->cluster);
  5209. spin_unlock(&delayed_refs->lock);
  5210. BUG_ON(head->extent_op);
  5211. if (head->must_insert_reserved)
  5212. ret = 1;
  5213. mutex_unlock(&head->mutex);
  5214. btrfs_put_delayed_ref(&head->node);
  5215. return ret;
  5216. out:
  5217. spin_unlock(&delayed_refs->lock);
  5218. return 0;
  5219. }
  5220. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5221. struct btrfs_root *root,
  5222. struct extent_buffer *buf,
  5223. u64 parent, int last_ref)
  5224. {
  5225. struct btrfs_block_group_cache *cache = NULL;
  5226. int pin = 1;
  5227. int ret;
  5228. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5229. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5230. buf->start, buf->len,
  5231. parent, root->root_key.objectid,
  5232. btrfs_header_level(buf),
  5233. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5234. BUG_ON(ret); /* -ENOMEM */
  5235. }
  5236. if (!last_ref)
  5237. return;
  5238. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5239. if (btrfs_header_generation(buf) == trans->transid) {
  5240. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5241. ret = check_ref_cleanup(trans, root, buf->start);
  5242. if (!ret)
  5243. goto out;
  5244. }
  5245. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5246. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5247. goto out;
  5248. }
  5249. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5250. btrfs_add_free_space(cache, buf->start, buf->len);
  5251. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  5252. trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
  5253. pin = 0;
  5254. }
  5255. out:
  5256. if (pin)
  5257. add_pinned_bytes(root->fs_info, buf->len,
  5258. btrfs_header_level(buf),
  5259. root->root_key.objectid);
  5260. /*
  5261. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5262. * anymore.
  5263. */
  5264. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5265. btrfs_put_block_group(cache);
  5266. }
  5267. /* Can return -ENOMEM */
  5268. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5269. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5270. u64 owner, u64 offset, int for_cow)
  5271. {
  5272. int ret;
  5273. struct btrfs_fs_info *fs_info = root->fs_info;
  5274. add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
  5275. /*
  5276. * tree log blocks never actually go into the extent allocation
  5277. * tree, just update pinning info and exit early.
  5278. */
  5279. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5280. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5281. /* unlocks the pinned mutex */
  5282. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5283. ret = 0;
  5284. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5285. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5286. num_bytes,
  5287. parent, root_objectid, (int)owner,
  5288. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  5289. } else {
  5290. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5291. num_bytes,
  5292. parent, root_objectid, owner,
  5293. offset, BTRFS_DROP_DELAYED_REF,
  5294. NULL, for_cow);
  5295. }
  5296. return ret;
  5297. }
  5298. static u64 stripe_align(struct btrfs_root *root,
  5299. struct btrfs_block_group_cache *cache,
  5300. u64 val, u64 num_bytes)
  5301. {
  5302. u64 ret = ALIGN(val, root->stripesize);
  5303. return ret;
  5304. }
  5305. /*
  5306. * when we wait for progress in the block group caching, its because
  5307. * our allocation attempt failed at least once. So, we must sleep
  5308. * and let some progress happen before we try again.
  5309. *
  5310. * This function will sleep at least once waiting for new free space to
  5311. * show up, and then it will check the block group free space numbers
  5312. * for our min num_bytes. Another option is to have it go ahead
  5313. * and look in the rbtree for a free extent of a given size, but this
  5314. * is a good start.
  5315. *
  5316. * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
  5317. * any of the information in this block group.
  5318. */
  5319. static noinline void
  5320. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5321. u64 num_bytes)
  5322. {
  5323. struct btrfs_caching_control *caching_ctl;
  5324. caching_ctl = get_caching_control(cache);
  5325. if (!caching_ctl)
  5326. return;
  5327. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5328. (cache->free_space_ctl->free_space >= num_bytes));
  5329. put_caching_control(caching_ctl);
  5330. }
  5331. static noinline int
  5332. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5333. {
  5334. struct btrfs_caching_control *caching_ctl;
  5335. int ret = 0;
  5336. caching_ctl = get_caching_control(cache);
  5337. if (!caching_ctl)
  5338. return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
  5339. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5340. if (cache->cached == BTRFS_CACHE_ERROR)
  5341. ret = -EIO;
  5342. put_caching_control(caching_ctl);
  5343. return ret;
  5344. }
  5345. int __get_raid_index(u64 flags)
  5346. {
  5347. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5348. return BTRFS_RAID_RAID10;
  5349. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5350. return BTRFS_RAID_RAID1;
  5351. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5352. return BTRFS_RAID_DUP;
  5353. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5354. return BTRFS_RAID_RAID0;
  5355. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5356. return BTRFS_RAID_RAID5;
  5357. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5358. return BTRFS_RAID_RAID6;
  5359. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5360. }
  5361. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  5362. {
  5363. return __get_raid_index(cache->flags);
  5364. }
  5365. enum btrfs_loop_type {
  5366. LOOP_CACHING_NOWAIT = 0,
  5367. LOOP_CACHING_WAIT = 1,
  5368. LOOP_ALLOC_CHUNK = 2,
  5369. LOOP_NO_EMPTY_SIZE = 3,
  5370. };
  5371. /*
  5372. * walks the btree of allocated extents and find a hole of a given size.
  5373. * The key ins is changed to record the hole:
  5374. * ins->objectid == start position
  5375. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5376. * ins->offset == the size of the hole.
  5377. * Any available blocks before search_start are skipped.
  5378. *
  5379. * If there is no suitable free space, we will record the max size of
  5380. * the free space extent currently.
  5381. */
  5382. static noinline int find_free_extent(struct btrfs_root *orig_root,
  5383. u64 num_bytes, u64 empty_size,
  5384. u64 hint_byte, struct btrfs_key *ins,
  5385. u64 flags)
  5386. {
  5387. int ret = 0;
  5388. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5389. struct btrfs_free_cluster *last_ptr = NULL;
  5390. struct btrfs_block_group_cache *block_group = NULL;
  5391. struct btrfs_block_group_cache *used_block_group;
  5392. u64 search_start = 0;
  5393. u64 max_extent_size = 0;
  5394. int empty_cluster = 2 * 1024 * 1024;
  5395. struct btrfs_space_info *space_info;
  5396. int loop = 0;
  5397. int index = __get_raid_index(flags);
  5398. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5399. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5400. bool found_uncached_bg = false;
  5401. bool failed_cluster_refill = false;
  5402. bool failed_alloc = false;
  5403. bool use_cluster = true;
  5404. bool have_caching_bg = false;
  5405. WARN_ON(num_bytes < root->sectorsize);
  5406. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  5407. ins->objectid = 0;
  5408. ins->offset = 0;
  5409. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5410. space_info = __find_space_info(root->fs_info, flags);
  5411. if (!space_info) {
  5412. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5413. return -ENOSPC;
  5414. }
  5415. /*
  5416. * If the space info is for both data and metadata it means we have a
  5417. * small filesystem and we can't use the clustering stuff.
  5418. */
  5419. if (btrfs_mixed_space_info(space_info))
  5420. use_cluster = false;
  5421. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5422. last_ptr = &root->fs_info->meta_alloc_cluster;
  5423. if (!btrfs_test_opt(root, SSD))
  5424. empty_cluster = 64 * 1024;
  5425. }
  5426. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5427. btrfs_test_opt(root, SSD)) {
  5428. last_ptr = &root->fs_info->data_alloc_cluster;
  5429. }
  5430. if (last_ptr) {
  5431. spin_lock(&last_ptr->lock);
  5432. if (last_ptr->block_group)
  5433. hint_byte = last_ptr->window_start;
  5434. spin_unlock(&last_ptr->lock);
  5435. }
  5436. search_start = max(search_start, first_logical_byte(root, 0));
  5437. search_start = max(search_start, hint_byte);
  5438. if (!last_ptr)
  5439. empty_cluster = 0;
  5440. if (search_start == hint_byte) {
  5441. block_group = btrfs_lookup_block_group(root->fs_info,
  5442. search_start);
  5443. used_block_group = block_group;
  5444. /*
  5445. * we don't want to use the block group if it doesn't match our
  5446. * allocation bits, or if its not cached.
  5447. *
  5448. * However if we are re-searching with an ideal block group
  5449. * picked out then we don't care that the block group is cached.
  5450. */
  5451. if (block_group && block_group_bits(block_group, flags) &&
  5452. block_group->cached != BTRFS_CACHE_NO) {
  5453. down_read(&space_info->groups_sem);
  5454. if (list_empty(&block_group->list) ||
  5455. block_group->ro) {
  5456. /*
  5457. * someone is removing this block group,
  5458. * we can't jump into the have_block_group
  5459. * target because our list pointers are not
  5460. * valid
  5461. */
  5462. btrfs_put_block_group(block_group);
  5463. up_read(&space_info->groups_sem);
  5464. } else {
  5465. index = get_block_group_index(block_group);
  5466. goto have_block_group;
  5467. }
  5468. } else if (block_group) {
  5469. btrfs_put_block_group(block_group);
  5470. }
  5471. }
  5472. search:
  5473. have_caching_bg = false;
  5474. down_read(&space_info->groups_sem);
  5475. list_for_each_entry(block_group, &space_info->block_groups[index],
  5476. list) {
  5477. u64 offset;
  5478. int cached;
  5479. used_block_group = block_group;
  5480. btrfs_get_block_group(block_group);
  5481. search_start = block_group->key.objectid;
  5482. /*
  5483. * this can happen if we end up cycling through all the
  5484. * raid types, but we want to make sure we only allocate
  5485. * for the proper type.
  5486. */
  5487. if (!block_group_bits(block_group, flags)) {
  5488. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  5489. BTRFS_BLOCK_GROUP_RAID1 |
  5490. BTRFS_BLOCK_GROUP_RAID5 |
  5491. BTRFS_BLOCK_GROUP_RAID6 |
  5492. BTRFS_BLOCK_GROUP_RAID10;
  5493. /*
  5494. * if they asked for extra copies and this block group
  5495. * doesn't provide them, bail. This does allow us to
  5496. * fill raid0 from raid1.
  5497. */
  5498. if ((flags & extra) && !(block_group->flags & extra))
  5499. goto loop;
  5500. }
  5501. have_block_group:
  5502. cached = block_group_cache_done(block_group);
  5503. if (unlikely(!cached)) {
  5504. found_uncached_bg = true;
  5505. ret = cache_block_group(block_group, 0);
  5506. BUG_ON(ret < 0);
  5507. ret = 0;
  5508. }
  5509. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
  5510. goto loop;
  5511. if (unlikely(block_group->ro))
  5512. goto loop;
  5513. /*
  5514. * Ok we want to try and use the cluster allocator, so
  5515. * lets look there
  5516. */
  5517. if (last_ptr) {
  5518. unsigned long aligned_cluster;
  5519. /*
  5520. * the refill lock keeps out other
  5521. * people trying to start a new cluster
  5522. */
  5523. spin_lock(&last_ptr->refill_lock);
  5524. used_block_group = last_ptr->block_group;
  5525. if (used_block_group != block_group &&
  5526. (!used_block_group ||
  5527. used_block_group->ro ||
  5528. !block_group_bits(used_block_group, flags))) {
  5529. used_block_group = block_group;
  5530. goto refill_cluster;
  5531. }
  5532. if (used_block_group != block_group)
  5533. btrfs_get_block_group(used_block_group);
  5534. offset = btrfs_alloc_from_cluster(used_block_group,
  5535. last_ptr,
  5536. num_bytes,
  5537. used_block_group->key.objectid,
  5538. &max_extent_size);
  5539. if (offset) {
  5540. /* we have a block, we're done */
  5541. spin_unlock(&last_ptr->refill_lock);
  5542. trace_btrfs_reserve_extent_cluster(root,
  5543. block_group, search_start, num_bytes);
  5544. goto checks;
  5545. }
  5546. WARN_ON(last_ptr->block_group != used_block_group);
  5547. if (used_block_group != block_group) {
  5548. btrfs_put_block_group(used_block_group);
  5549. used_block_group = block_group;
  5550. }
  5551. refill_cluster:
  5552. BUG_ON(used_block_group != block_group);
  5553. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5554. * set up a new clusters, so lets just skip it
  5555. * and let the allocator find whatever block
  5556. * it can find. If we reach this point, we
  5557. * will have tried the cluster allocator
  5558. * plenty of times and not have found
  5559. * anything, so we are likely way too
  5560. * fragmented for the clustering stuff to find
  5561. * anything.
  5562. *
  5563. * However, if the cluster is taken from the
  5564. * current block group, release the cluster
  5565. * first, so that we stand a better chance of
  5566. * succeeding in the unclustered
  5567. * allocation. */
  5568. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5569. last_ptr->block_group != block_group) {
  5570. spin_unlock(&last_ptr->refill_lock);
  5571. goto unclustered_alloc;
  5572. }
  5573. /*
  5574. * this cluster didn't work out, free it and
  5575. * start over
  5576. */
  5577. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5578. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5579. spin_unlock(&last_ptr->refill_lock);
  5580. goto unclustered_alloc;
  5581. }
  5582. aligned_cluster = max_t(unsigned long,
  5583. empty_cluster + empty_size,
  5584. block_group->full_stripe_len);
  5585. /* allocate a cluster in this block group */
  5586. ret = btrfs_find_space_cluster(root, block_group,
  5587. last_ptr, search_start,
  5588. num_bytes,
  5589. aligned_cluster);
  5590. if (ret == 0) {
  5591. /*
  5592. * now pull our allocation out of this
  5593. * cluster
  5594. */
  5595. offset = btrfs_alloc_from_cluster(block_group,
  5596. last_ptr,
  5597. num_bytes,
  5598. search_start,
  5599. &max_extent_size);
  5600. if (offset) {
  5601. /* we found one, proceed */
  5602. spin_unlock(&last_ptr->refill_lock);
  5603. trace_btrfs_reserve_extent_cluster(root,
  5604. block_group, search_start,
  5605. num_bytes);
  5606. goto checks;
  5607. }
  5608. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5609. && !failed_cluster_refill) {
  5610. spin_unlock(&last_ptr->refill_lock);
  5611. failed_cluster_refill = true;
  5612. wait_block_group_cache_progress(block_group,
  5613. num_bytes + empty_cluster + empty_size);
  5614. goto have_block_group;
  5615. }
  5616. /*
  5617. * at this point we either didn't find a cluster
  5618. * or we weren't able to allocate a block from our
  5619. * cluster. Free the cluster we've been trying
  5620. * to use, and go to the next block group
  5621. */
  5622. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5623. spin_unlock(&last_ptr->refill_lock);
  5624. goto loop;
  5625. }
  5626. unclustered_alloc:
  5627. spin_lock(&block_group->free_space_ctl->tree_lock);
  5628. if (cached &&
  5629. block_group->free_space_ctl->free_space <
  5630. num_bytes + empty_cluster + empty_size) {
  5631. if (block_group->free_space_ctl->free_space >
  5632. max_extent_size)
  5633. max_extent_size =
  5634. block_group->free_space_ctl->free_space;
  5635. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5636. goto loop;
  5637. }
  5638. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5639. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5640. num_bytes, empty_size,
  5641. &max_extent_size);
  5642. /*
  5643. * If we didn't find a chunk, and we haven't failed on this
  5644. * block group before, and this block group is in the middle of
  5645. * caching and we are ok with waiting, then go ahead and wait
  5646. * for progress to be made, and set failed_alloc to true.
  5647. *
  5648. * If failed_alloc is true then we've already waited on this
  5649. * block group once and should move on to the next block group.
  5650. */
  5651. if (!offset && !failed_alloc && !cached &&
  5652. loop > LOOP_CACHING_NOWAIT) {
  5653. wait_block_group_cache_progress(block_group,
  5654. num_bytes + empty_size);
  5655. failed_alloc = true;
  5656. goto have_block_group;
  5657. } else if (!offset) {
  5658. if (!cached)
  5659. have_caching_bg = true;
  5660. goto loop;
  5661. }
  5662. checks:
  5663. search_start = stripe_align(root, used_block_group,
  5664. offset, num_bytes);
  5665. /* move on to the next group */
  5666. if (search_start + num_bytes >
  5667. used_block_group->key.objectid + used_block_group->key.offset) {
  5668. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5669. goto loop;
  5670. }
  5671. if (offset < search_start)
  5672. btrfs_add_free_space(used_block_group, offset,
  5673. search_start - offset);
  5674. BUG_ON(offset > search_start);
  5675. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5676. alloc_type);
  5677. if (ret == -EAGAIN) {
  5678. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5679. goto loop;
  5680. }
  5681. /* we are all good, lets return */
  5682. ins->objectid = search_start;
  5683. ins->offset = num_bytes;
  5684. trace_btrfs_reserve_extent(orig_root, block_group,
  5685. search_start, num_bytes);
  5686. if (used_block_group != block_group)
  5687. btrfs_put_block_group(used_block_group);
  5688. btrfs_put_block_group(block_group);
  5689. break;
  5690. loop:
  5691. failed_cluster_refill = false;
  5692. failed_alloc = false;
  5693. BUG_ON(index != get_block_group_index(block_group));
  5694. if (used_block_group != block_group)
  5695. btrfs_put_block_group(used_block_group);
  5696. btrfs_put_block_group(block_group);
  5697. }
  5698. up_read(&space_info->groups_sem);
  5699. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5700. goto search;
  5701. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5702. goto search;
  5703. /*
  5704. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5705. * caching kthreads as we move along
  5706. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5707. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5708. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5709. * again
  5710. */
  5711. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5712. index = 0;
  5713. loop++;
  5714. if (loop == LOOP_ALLOC_CHUNK) {
  5715. struct btrfs_trans_handle *trans;
  5716. trans = btrfs_join_transaction(root);
  5717. if (IS_ERR(trans)) {
  5718. ret = PTR_ERR(trans);
  5719. goto out;
  5720. }
  5721. ret = do_chunk_alloc(trans, root, flags,
  5722. CHUNK_ALLOC_FORCE);
  5723. /*
  5724. * Do not bail out on ENOSPC since we
  5725. * can do more things.
  5726. */
  5727. if (ret < 0 && ret != -ENOSPC)
  5728. btrfs_abort_transaction(trans,
  5729. root, ret);
  5730. else
  5731. ret = 0;
  5732. btrfs_end_transaction(trans, root);
  5733. if (ret)
  5734. goto out;
  5735. }
  5736. if (loop == LOOP_NO_EMPTY_SIZE) {
  5737. empty_size = 0;
  5738. empty_cluster = 0;
  5739. }
  5740. goto search;
  5741. } else if (!ins->objectid) {
  5742. ret = -ENOSPC;
  5743. } else if (ins->objectid) {
  5744. ret = 0;
  5745. }
  5746. out:
  5747. if (ret == -ENOSPC)
  5748. ins->offset = max_extent_size;
  5749. return ret;
  5750. }
  5751. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5752. int dump_block_groups)
  5753. {
  5754. struct btrfs_block_group_cache *cache;
  5755. int index = 0;
  5756. spin_lock(&info->lock);
  5757. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5758. info->flags,
  5759. info->total_bytes - info->bytes_used - info->bytes_pinned -
  5760. info->bytes_reserved - info->bytes_readonly,
  5761. (info->full) ? "" : "not ");
  5762. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5763. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5764. info->total_bytes, info->bytes_used, info->bytes_pinned,
  5765. info->bytes_reserved, info->bytes_may_use,
  5766. info->bytes_readonly);
  5767. spin_unlock(&info->lock);
  5768. if (!dump_block_groups)
  5769. return;
  5770. down_read(&info->groups_sem);
  5771. again:
  5772. list_for_each_entry(cache, &info->block_groups[index], list) {
  5773. spin_lock(&cache->lock);
  5774. printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
  5775. cache->key.objectid, cache->key.offset,
  5776. btrfs_block_group_used(&cache->item), cache->pinned,
  5777. cache->reserved, cache->ro ? "[readonly]" : "");
  5778. btrfs_dump_free_space(cache, bytes);
  5779. spin_unlock(&cache->lock);
  5780. }
  5781. if (++index < BTRFS_NR_RAID_TYPES)
  5782. goto again;
  5783. up_read(&info->groups_sem);
  5784. }
  5785. int btrfs_reserve_extent(struct btrfs_root *root,
  5786. u64 num_bytes, u64 min_alloc_size,
  5787. u64 empty_size, u64 hint_byte,
  5788. struct btrfs_key *ins, int is_data)
  5789. {
  5790. bool final_tried = false;
  5791. u64 flags;
  5792. int ret;
  5793. flags = btrfs_get_alloc_profile(root, is_data);
  5794. again:
  5795. WARN_ON(num_bytes < root->sectorsize);
  5796. ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
  5797. flags);
  5798. if (ret == -ENOSPC) {
  5799. if (!final_tried && ins->offset) {
  5800. num_bytes = min(num_bytes >> 1, ins->offset);
  5801. num_bytes = round_down(num_bytes, root->sectorsize);
  5802. num_bytes = max(num_bytes, min_alloc_size);
  5803. if (num_bytes == min_alloc_size)
  5804. final_tried = true;
  5805. goto again;
  5806. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5807. struct btrfs_space_info *sinfo;
  5808. sinfo = __find_space_info(root->fs_info, flags);
  5809. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  5810. flags, num_bytes);
  5811. if (sinfo)
  5812. dump_space_info(sinfo, num_bytes, 1);
  5813. }
  5814. }
  5815. return ret;
  5816. }
  5817. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5818. u64 start, u64 len, int pin)
  5819. {
  5820. struct btrfs_block_group_cache *cache;
  5821. int ret = 0;
  5822. cache = btrfs_lookup_block_group(root->fs_info, start);
  5823. if (!cache) {
  5824. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  5825. start);
  5826. return -ENOSPC;
  5827. }
  5828. if (btrfs_test_opt(root, DISCARD))
  5829. ret = btrfs_discard_extent(root, start, len, NULL);
  5830. if (pin)
  5831. pin_down_extent(root, cache, start, len, 1);
  5832. else {
  5833. btrfs_add_free_space(cache, start, len);
  5834. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5835. }
  5836. btrfs_put_block_group(cache);
  5837. trace_btrfs_reserved_extent_free(root, start, len);
  5838. return ret;
  5839. }
  5840. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5841. u64 start, u64 len)
  5842. {
  5843. return __btrfs_free_reserved_extent(root, start, len, 0);
  5844. }
  5845. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5846. u64 start, u64 len)
  5847. {
  5848. return __btrfs_free_reserved_extent(root, start, len, 1);
  5849. }
  5850. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5851. struct btrfs_root *root,
  5852. u64 parent, u64 root_objectid,
  5853. u64 flags, u64 owner, u64 offset,
  5854. struct btrfs_key *ins, int ref_mod)
  5855. {
  5856. int ret;
  5857. struct btrfs_fs_info *fs_info = root->fs_info;
  5858. struct btrfs_extent_item *extent_item;
  5859. struct btrfs_extent_inline_ref *iref;
  5860. struct btrfs_path *path;
  5861. struct extent_buffer *leaf;
  5862. int type;
  5863. u32 size;
  5864. if (parent > 0)
  5865. type = BTRFS_SHARED_DATA_REF_KEY;
  5866. else
  5867. type = BTRFS_EXTENT_DATA_REF_KEY;
  5868. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5869. path = btrfs_alloc_path();
  5870. if (!path)
  5871. return -ENOMEM;
  5872. path->leave_spinning = 1;
  5873. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5874. ins, size);
  5875. if (ret) {
  5876. btrfs_free_path(path);
  5877. return ret;
  5878. }
  5879. leaf = path->nodes[0];
  5880. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5881. struct btrfs_extent_item);
  5882. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5883. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5884. btrfs_set_extent_flags(leaf, extent_item,
  5885. flags | BTRFS_EXTENT_FLAG_DATA);
  5886. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5887. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5888. if (parent > 0) {
  5889. struct btrfs_shared_data_ref *ref;
  5890. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5891. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5892. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5893. } else {
  5894. struct btrfs_extent_data_ref *ref;
  5895. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5896. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5897. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5898. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5899. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5900. }
  5901. btrfs_mark_buffer_dirty(path->nodes[0]);
  5902. btrfs_free_path(path);
  5903. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5904. if (ret) { /* -ENOENT, logic error */
  5905. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5906. ins->objectid, ins->offset);
  5907. BUG();
  5908. }
  5909. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5910. return ret;
  5911. }
  5912. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5913. struct btrfs_root *root,
  5914. u64 parent, u64 root_objectid,
  5915. u64 flags, struct btrfs_disk_key *key,
  5916. int level, struct btrfs_key *ins)
  5917. {
  5918. int ret;
  5919. struct btrfs_fs_info *fs_info = root->fs_info;
  5920. struct btrfs_extent_item *extent_item;
  5921. struct btrfs_tree_block_info *block_info;
  5922. struct btrfs_extent_inline_ref *iref;
  5923. struct btrfs_path *path;
  5924. struct extent_buffer *leaf;
  5925. u32 size = sizeof(*extent_item) + sizeof(*iref);
  5926. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5927. SKINNY_METADATA);
  5928. if (!skinny_metadata)
  5929. size += sizeof(*block_info);
  5930. path = btrfs_alloc_path();
  5931. if (!path) {
  5932. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  5933. root->leafsize);
  5934. return -ENOMEM;
  5935. }
  5936. path->leave_spinning = 1;
  5937. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5938. ins, size);
  5939. if (ret) {
  5940. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  5941. root->leafsize);
  5942. btrfs_free_path(path);
  5943. return ret;
  5944. }
  5945. leaf = path->nodes[0];
  5946. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5947. struct btrfs_extent_item);
  5948. btrfs_set_extent_refs(leaf, extent_item, 1);
  5949. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5950. btrfs_set_extent_flags(leaf, extent_item,
  5951. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5952. if (skinny_metadata) {
  5953. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5954. } else {
  5955. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5956. btrfs_set_tree_block_key(leaf, block_info, key);
  5957. btrfs_set_tree_block_level(leaf, block_info, level);
  5958. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5959. }
  5960. if (parent > 0) {
  5961. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5962. btrfs_set_extent_inline_ref_type(leaf, iref,
  5963. BTRFS_SHARED_BLOCK_REF_KEY);
  5964. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5965. } else {
  5966. btrfs_set_extent_inline_ref_type(leaf, iref,
  5967. BTRFS_TREE_BLOCK_REF_KEY);
  5968. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5969. }
  5970. btrfs_mark_buffer_dirty(leaf);
  5971. btrfs_free_path(path);
  5972. ret = update_block_group(root, ins->objectid, root->leafsize, 1);
  5973. if (ret) { /* -ENOENT, logic error */
  5974. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5975. ins->objectid, ins->offset);
  5976. BUG();
  5977. }
  5978. trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize);
  5979. return ret;
  5980. }
  5981. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5982. struct btrfs_root *root,
  5983. u64 root_objectid, u64 owner,
  5984. u64 offset, struct btrfs_key *ins)
  5985. {
  5986. int ret;
  5987. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5988. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5989. ins->offset, 0,
  5990. root_objectid, owner, offset,
  5991. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5992. return ret;
  5993. }
  5994. /*
  5995. * this is used by the tree logging recovery code. It records that
  5996. * an extent has been allocated and makes sure to clear the free
  5997. * space cache bits as well
  5998. */
  5999. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  6000. struct btrfs_root *root,
  6001. u64 root_objectid, u64 owner, u64 offset,
  6002. struct btrfs_key *ins)
  6003. {
  6004. int ret;
  6005. struct btrfs_block_group_cache *block_group;
  6006. /*
  6007. * Mixed block groups will exclude before processing the log so we only
  6008. * need to do the exlude dance if this fs isn't mixed.
  6009. */
  6010. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  6011. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  6012. if (ret)
  6013. return ret;
  6014. }
  6015. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  6016. if (!block_group)
  6017. return -EINVAL;
  6018. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  6019. RESERVE_ALLOC_NO_ACCOUNT);
  6020. BUG_ON(ret); /* logic error */
  6021. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  6022. 0, owner, offset, ins, 1);
  6023. btrfs_put_block_group(block_group);
  6024. return ret;
  6025. }
  6026. static struct extent_buffer *
  6027. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  6028. u64 bytenr, u32 blocksize, int level)
  6029. {
  6030. struct extent_buffer *buf;
  6031. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6032. if (!buf)
  6033. return ERR_PTR(-ENOMEM);
  6034. btrfs_set_header_generation(buf, trans->transid);
  6035. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  6036. btrfs_tree_lock(buf);
  6037. clean_tree_block(trans, root, buf);
  6038. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  6039. btrfs_set_lock_blocking(buf);
  6040. btrfs_set_buffer_uptodate(buf);
  6041. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  6042. /*
  6043. * we allow two log transactions at a time, use different
  6044. * EXENT bit to differentiate dirty pages.
  6045. */
  6046. if (root->log_transid % 2 == 0)
  6047. set_extent_dirty(&root->dirty_log_pages, buf->start,
  6048. buf->start + buf->len - 1, GFP_NOFS);
  6049. else
  6050. set_extent_new(&root->dirty_log_pages, buf->start,
  6051. buf->start + buf->len - 1, GFP_NOFS);
  6052. } else {
  6053. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  6054. buf->start + buf->len - 1, GFP_NOFS);
  6055. }
  6056. trans->blocks_used++;
  6057. /* this returns a buffer locked for blocking */
  6058. return buf;
  6059. }
  6060. static struct btrfs_block_rsv *
  6061. use_block_rsv(struct btrfs_trans_handle *trans,
  6062. struct btrfs_root *root, u32 blocksize)
  6063. {
  6064. struct btrfs_block_rsv *block_rsv;
  6065. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  6066. int ret;
  6067. bool global_updated = false;
  6068. block_rsv = get_block_rsv(trans, root);
  6069. if (unlikely(block_rsv->size == 0))
  6070. goto try_reserve;
  6071. again:
  6072. ret = block_rsv_use_bytes(block_rsv, blocksize);
  6073. if (!ret)
  6074. return block_rsv;
  6075. if (block_rsv->failfast)
  6076. return ERR_PTR(ret);
  6077. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  6078. global_updated = true;
  6079. update_global_block_rsv(root->fs_info);
  6080. goto again;
  6081. }
  6082. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6083. static DEFINE_RATELIMIT_STATE(_rs,
  6084. DEFAULT_RATELIMIT_INTERVAL * 10,
  6085. /*DEFAULT_RATELIMIT_BURST*/ 1);
  6086. if (__ratelimit(&_rs))
  6087. WARN(1, KERN_DEBUG
  6088. "btrfs: block rsv returned %d\n", ret);
  6089. }
  6090. try_reserve:
  6091. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  6092. BTRFS_RESERVE_NO_FLUSH);
  6093. if (!ret)
  6094. return block_rsv;
  6095. /*
  6096. * If we couldn't reserve metadata bytes try and use some from
  6097. * the global reserve if its space type is the same as the global
  6098. * reservation.
  6099. */
  6100. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  6101. block_rsv->space_info == global_rsv->space_info) {
  6102. ret = block_rsv_use_bytes(global_rsv, blocksize);
  6103. if (!ret)
  6104. return global_rsv;
  6105. }
  6106. return ERR_PTR(ret);
  6107. }
  6108. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6109. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6110. {
  6111. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6112. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6113. }
  6114. /*
  6115. * finds a free extent and does all the dirty work required for allocation
  6116. * returns the key for the extent through ins, and a tree buffer for
  6117. * the first block of the extent through buf.
  6118. *
  6119. * returns the tree buffer or NULL.
  6120. */
  6121. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  6122. struct btrfs_root *root, u32 blocksize,
  6123. u64 parent, u64 root_objectid,
  6124. struct btrfs_disk_key *key, int level,
  6125. u64 hint, u64 empty_size)
  6126. {
  6127. struct btrfs_key ins;
  6128. struct btrfs_block_rsv *block_rsv;
  6129. struct extent_buffer *buf;
  6130. u64 flags = 0;
  6131. int ret;
  6132. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6133. SKINNY_METADATA);
  6134. block_rsv = use_block_rsv(trans, root, blocksize);
  6135. if (IS_ERR(block_rsv))
  6136. return ERR_CAST(block_rsv);
  6137. ret = btrfs_reserve_extent(root, blocksize, blocksize,
  6138. empty_size, hint, &ins, 0);
  6139. if (ret) {
  6140. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6141. return ERR_PTR(ret);
  6142. }
  6143. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  6144. blocksize, level);
  6145. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  6146. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6147. if (parent == 0)
  6148. parent = ins.objectid;
  6149. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6150. } else
  6151. BUG_ON(parent > 0);
  6152. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6153. struct btrfs_delayed_extent_op *extent_op;
  6154. extent_op = btrfs_alloc_delayed_extent_op();
  6155. BUG_ON(!extent_op); /* -ENOMEM */
  6156. if (key)
  6157. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6158. else
  6159. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6160. extent_op->flags_to_set = flags;
  6161. if (skinny_metadata)
  6162. extent_op->update_key = 0;
  6163. else
  6164. extent_op->update_key = 1;
  6165. extent_op->update_flags = 1;
  6166. extent_op->is_data = 0;
  6167. extent_op->level = level;
  6168. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6169. ins.objectid,
  6170. ins.offset, parent, root_objectid,
  6171. level, BTRFS_ADD_DELAYED_EXTENT,
  6172. extent_op, 0);
  6173. BUG_ON(ret); /* -ENOMEM */
  6174. }
  6175. return buf;
  6176. }
  6177. struct walk_control {
  6178. u64 refs[BTRFS_MAX_LEVEL];
  6179. u64 flags[BTRFS_MAX_LEVEL];
  6180. struct btrfs_key update_progress;
  6181. int stage;
  6182. int level;
  6183. int shared_level;
  6184. int update_ref;
  6185. int keep_locks;
  6186. int reada_slot;
  6187. int reada_count;
  6188. int for_reloc;
  6189. };
  6190. #define DROP_REFERENCE 1
  6191. #define UPDATE_BACKREF 2
  6192. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6193. struct btrfs_root *root,
  6194. struct walk_control *wc,
  6195. struct btrfs_path *path)
  6196. {
  6197. u64 bytenr;
  6198. u64 generation;
  6199. u64 refs;
  6200. u64 flags;
  6201. u32 nritems;
  6202. u32 blocksize;
  6203. struct btrfs_key key;
  6204. struct extent_buffer *eb;
  6205. int ret;
  6206. int slot;
  6207. int nread = 0;
  6208. if (path->slots[wc->level] < wc->reada_slot) {
  6209. wc->reada_count = wc->reada_count * 2 / 3;
  6210. wc->reada_count = max(wc->reada_count, 2);
  6211. } else {
  6212. wc->reada_count = wc->reada_count * 3 / 2;
  6213. wc->reada_count = min_t(int, wc->reada_count,
  6214. BTRFS_NODEPTRS_PER_BLOCK(root));
  6215. }
  6216. eb = path->nodes[wc->level];
  6217. nritems = btrfs_header_nritems(eb);
  6218. blocksize = btrfs_level_size(root, wc->level - 1);
  6219. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6220. if (nread >= wc->reada_count)
  6221. break;
  6222. cond_resched();
  6223. bytenr = btrfs_node_blockptr(eb, slot);
  6224. generation = btrfs_node_ptr_generation(eb, slot);
  6225. if (slot == path->slots[wc->level])
  6226. goto reada;
  6227. if (wc->stage == UPDATE_BACKREF &&
  6228. generation <= root->root_key.offset)
  6229. continue;
  6230. /* We don't lock the tree block, it's OK to be racy here */
  6231. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6232. wc->level - 1, 1, &refs,
  6233. &flags);
  6234. /* We don't care about errors in readahead. */
  6235. if (ret < 0)
  6236. continue;
  6237. BUG_ON(refs == 0);
  6238. if (wc->stage == DROP_REFERENCE) {
  6239. if (refs == 1)
  6240. goto reada;
  6241. if (wc->level == 1 &&
  6242. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6243. continue;
  6244. if (!wc->update_ref ||
  6245. generation <= root->root_key.offset)
  6246. continue;
  6247. btrfs_node_key_to_cpu(eb, &key, slot);
  6248. ret = btrfs_comp_cpu_keys(&key,
  6249. &wc->update_progress);
  6250. if (ret < 0)
  6251. continue;
  6252. } else {
  6253. if (wc->level == 1 &&
  6254. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6255. continue;
  6256. }
  6257. reada:
  6258. ret = readahead_tree_block(root, bytenr, blocksize,
  6259. generation);
  6260. if (ret)
  6261. break;
  6262. nread++;
  6263. }
  6264. wc->reada_slot = slot;
  6265. }
  6266. /*
  6267. * helper to process tree block while walking down the tree.
  6268. *
  6269. * when wc->stage == UPDATE_BACKREF, this function updates
  6270. * back refs for pointers in the block.
  6271. *
  6272. * NOTE: return value 1 means we should stop walking down.
  6273. */
  6274. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  6275. struct btrfs_root *root,
  6276. struct btrfs_path *path,
  6277. struct walk_control *wc, int lookup_info)
  6278. {
  6279. int level = wc->level;
  6280. struct extent_buffer *eb = path->nodes[level];
  6281. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6282. int ret;
  6283. if (wc->stage == UPDATE_BACKREF &&
  6284. btrfs_header_owner(eb) != root->root_key.objectid)
  6285. return 1;
  6286. /*
  6287. * when reference count of tree block is 1, it won't increase
  6288. * again. once full backref flag is set, we never clear it.
  6289. */
  6290. if (lookup_info &&
  6291. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  6292. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  6293. BUG_ON(!path->locks[level]);
  6294. ret = btrfs_lookup_extent_info(trans, root,
  6295. eb->start, level, 1,
  6296. &wc->refs[level],
  6297. &wc->flags[level]);
  6298. BUG_ON(ret == -ENOMEM);
  6299. if (ret)
  6300. return ret;
  6301. BUG_ON(wc->refs[level] == 0);
  6302. }
  6303. if (wc->stage == DROP_REFERENCE) {
  6304. if (wc->refs[level] > 1)
  6305. return 1;
  6306. if (path->locks[level] && !wc->keep_locks) {
  6307. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6308. path->locks[level] = 0;
  6309. }
  6310. return 0;
  6311. }
  6312. /* wc->stage == UPDATE_BACKREF */
  6313. if (!(wc->flags[level] & flag)) {
  6314. BUG_ON(!path->locks[level]);
  6315. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  6316. BUG_ON(ret); /* -ENOMEM */
  6317. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  6318. BUG_ON(ret); /* -ENOMEM */
  6319. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  6320. eb->len, flag,
  6321. btrfs_header_level(eb), 0);
  6322. BUG_ON(ret); /* -ENOMEM */
  6323. wc->flags[level] |= flag;
  6324. }
  6325. /*
  6326. * the block is shared by multiple trees, so it's not good to
  6327. * keep the tree lock
  6328. */
  6329. if (path->locks[level] && level > 0) {
  6330. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6331. path->locks[level] = 0;
  6332. }
  6333. return 0;
  6334. }
  6335. /*
  6336. * helper to process tree block pointer.
  6337. *
  6338. * when wc->stage == DROP_REFERENCE, this function checks
  6339. * reference count of the block pointed to. if the block
  6340. * is shared and we need update back refs for the subtree
  6341. * rooted at the block, this function changes wc->stage to
  6342. * UPDATE_BACKREF. if the block is shared and there is no
  6343. * need to update back, this function drops the reference
  6344. * to the block.
  6345. *
  6346. * NOTE: return value 1 means we should stop walking down.
  6347. */
  6348. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  6349. struct btrfs_root *root,
  6350. struct btrfs_path *path,
  6351. struct walk_control *wc, int *lookup_info)
  6352. {
  6353. u64 bytenr;
  6354. u64 generation;
  6355. u64 parent;
  6356. u32 blocksize;
  6357. struct btrfs_key key;
  6358. struct extent_buffer *next;
  6359. int level = wc->level;
  6360. int reada = 0;
  6361. int ret = 0;
  6362. generation = btrfs_node_ptr_generation(path->nodes[level],
  6363. path->slots[level]);
  6364. /*
  6365. * if the lower level block was created before the snapshot
  6366. * was created, we know there is no need to update back refs
  6367. * for the subtree
  6368. */
  6369. if (wc->stage == UPDATE_BACKREF &&
  6370. generation <= root->root_key.offset) {
  6371. *lookup_info = 1;
  6372. return 1;
  6373. }
  6374. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  6375. blocksize = btrfs_level_size(root, level - 1);
  6376. next = btrfs_find_tree_block(root, bytenr, blocksize);
  6377. if (!next) {
  6378. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6379. if (!next)
  6380. return -ENOMEM;
  6381. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  6382. level - 1);
  6383. reada = 1;
  6384. }
  6385. btrfs_tree_lock(next);
  6386. btrfs_set_lock_blocking(next);
  6387. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  6388. &wc->refs[level - 1],
  6389. &wc->flags[level - 1]);
  6390. if (ret < 0) {
  6391. btrfs_tree_unlock(next);
  6392. return ret;
  6393. }
  6394. if (unlikely(wc->refs[level - 1] == 0)) {
  6395. btrfs_err(root->fs_info, "Missing references.");
  6396. BUG();
  6397. }
  6398. *lookup_info = 0;
  6399. if (wc->stage == DROP_REFERENCE) {
  6400. if (wc->refs[level - 1] > 1) {
  6401. if (level == 1 &&
  6402. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6403. goto skip;
  6404. if (!wc->update_ref ||
  6405. generation <= root->root_key.offset)
  6406. goto skip;
  6407. btrfs_node_key_to_cpu(path->nodes[level], &key,
  6408. path->slots[level]);
  6409. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  6410. if (ret < 0)
  6411. goto skip;
  6412. wc->stage = UPDATE_BACKREF;
  6413. wc->shared_level = level - 1;
  6414. }
  6415. } else {
  6416. if (level == 1 &&
  6417. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6418. goto skip;
  6419. }
  6420. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  6421. btrfs_tree_unlock(next);
  6422. free_extent_buffer(next);
  6423. next = NULL;
  6424. *lookup_info = 1;
  6425. }
  6426. if (!next) {
  6427. if (reada && level == 1)
  6428. reada_walk_down(trans, root, wc, path);
  6429. next = read_tree_block(root, bytenr, blocksize, generation);
  6430. if (!next || !extent_buffer_uptodate(next)) {
  6431. free_extent_buffer(next);
  6432. return -EIO;
  6433. }
  6434. btrfs_tree_lock(next);
  6435. btrfs_set_lock_blocking(next);
  6436. }
  6437. level--;
  6438. BUG_ON(level != btrfs_header_level(next));
  6439. path->nodes[level] = next;
  6440. path->slots[level] = 0;
  6441. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6442. wc->level = level;
  6443. if (wc->level == 1)
  6444. wc->reada_slot = 0;
  6445. return 0;
  6446. skip:
  6447. wc->refs[level - 1] = 0;
  6448. wc->flags[level - 1] = 0;
  6449. if (wc->stage == DROP_REFERENCE) {
  6450. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  6451. parent = path->nodes[level]->start;
  6452. } else {
  6453. BUG_ON(root->root_key.objectid !=
  6454. btrfs_header_owner(path->nodes[level]));
  6455. parent = 0;
  6456. }
  6457. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  6458. root->root_key.objectid, level - 1, 0, 0);
  6459. BUG_ON(ret); /* -ENOMEM */
  6460. }
  6461. btrfs_tree_unlock(next);
  6462. free_extent_buffer(next);
  6463. *lookup_info = 1;
  6464. return 1;
  6465. }
  6466. /*
  6467. * helper to process tree block while walking up the tree.
  6468. *
  6469. * when wc->stage == DROP_REFERENCE, this function drops
  6470. * reference count on the block.
  6471. *
  6472. * when wc->stage == UPDATE_BACKREF, this function changes
  6473. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  6474. * to UPDATE_BACKREF previously while processing the block.
  6475. *
  6476. * NOTE: return value 1 means we should stop walking up.
  6477. */
  6478. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  6479. struct btrfs_root *root,
  6480. struct btrfs_path *path,
  6481. struct walk_control *wc)
  6482. {
  6483. int ret;
  6484. int level = wc->level;
  6485. struct extent_buffer *eb = path->nodes[level];
  6486. u64 parent = 0;
  6487. if (wc->stage == UPDATE_BACKREF) {
  6488. BUG_ON(wc->shared_level < level);
  6489. if (level < wc->shared_level)
  6490. goto out;
  6491. ret = find_next_key(path, level + 1, &wc->update_progress);
  6492. if (ret > 0)
  6493. wc->update_ref = 0;
  6494. wc->stage = DROP_REFERENCE;
  6495. wc->shared_level = -1;
  6496. path->slots[level] = 0;
  6497. /*
  6498. * check reference count again if the block isn't locked.
  6499. * we should start walking down the tree again if reference
  6500. * count is one.
  6501. */
  6502. if (!path->locks[level]) {
  6503. BUG_ON(level == 0);
  6504. btrfs_tree_lock(eb);
  6505. btrfs_set_lock_blocking(eb);
  6506. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6507. ret = btrfs_lookup_extent_info(trans, root,
  6508. eb->start, level, 1,
  6509. &wc->refs[level],
  6510. &wc->flags[level]);
  6511. if (ret < 0) {
  6512. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6513. path->locks[level] = 0;
  6514. return ret;
  6515. }
  6516. BUG_ON(wc->refs[level] == 0);
  6517. if (wc->refs[level] == 1) {
  6518. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6519. path->locks[level] = 0;
  6520. return 1;
  6521. }
  6522. }
  6523. }
  6524. /* wc->stage == DROP_REFERENCE */
  6525. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6526. if (wc->refs[level] == 1) {
  6527. if (level == 0) {
  6528. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6529. ret = btrfs_dec_ref(trans, root, eb, 1,
  6530. wc->for_reloc);
  6531. else
  6532. ret = btrfs_dec_ref(trans, root, eb, 0,
  6533. wc->for_reloc);
  6534. BUG_ON(ret); /* -ENOMEM */
  6535. }
  6536. /* make block locked assertion in clean_tree_block happy */
  6537. if (!path->locks[level] &&
  6538. btrfs_header_generation(eb) == trans->transid) {
  6539. btrfs_tree_lock(eb);
  6540. btrfs_set_lock_blocking(eb);
  6541. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6542. }
  6543. clean_tree_block(trans, root, eb);
  6544. }
  6545. if (eb == root->node) {
  6546. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6547. parent = eb->start;
  6548. else
  6549. BUG_ON(root->root_key.objectid !=
  6550. btrfs_header_owner(eb));
  6551. } else {
  6552. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6553. parent = path->nodes[level + 1]->start;
  6554. else
  6555. BUG_ON(root->root_key.objectid !=
  6556. btrfs_header_owner(path->nodes[level + 1]));
  6557. }
  6558. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6559. out:
  6560. wc->refs[level] = 0;
  6561. wc->flags[level] = 0;
  6562. return 0;
  6563. }
  6564. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6565. struct btrfs_root *root,
  6566. struct btrfs_path *path,
  6567. struct walk_control *wc)
  6568. {
  6569. int level = wc->level;
  6570. int lookup_info = 1;
  6571. int ret;
  6572. while (level >= 0) {
  6573. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6574. if (ret > 0)
  6575. break;
  6576. if (level == 0)
  6577. break;
  6578. if (path->slots[level] >=
  6579. btrfs_header_nritems(path->nodes[level]))
  6580. break;
  6581. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6582. if (ret > 0) {
  6583. path->slots[level]++;
  6584. continue;
  6585. } else if (ret < 0)
  6586. return ret;
  6587. level = wc->level;
  6588. }
  6589. return 0;
  6590. }
  6591. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6592. struct btrfs_root *root,
  6593. struct btrfs_path *path,
  6594. struct walk_control *wc, int max_level)
  6595. {
  6596. int level = wc->level;
  6597. int ret;
  6598. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6599. while (level < max_level && path->nodes[level]) {
  6600. wc->level = level;
  6601. if (path->slots[level] + 1 <
  6602. btrfs_header_nritems(path->nodes[level])) {
  6603. path->slots[level]++;
  6604. return 0;
  6605. } else {
  6606. ret = walk_up_proc(trans, root, path, wc);
  6607. if (ret > 0)
  6608. return 0;
  6609. if (path->locks[level]) {
  6610. btrfs_tree_unlock_rw(path->nodes[level],
  6611. path->locks[level]);
  6612. path->locks[level] = 0;
  6613. }
  6614. free_extent_buffer(path->nodes[level]);
  6615. path->nodes[level] = NULL;
  6616. level++;
  6617. }
  6618. }
  6619. return 1;
  6620. }
  6621. /*
  6622. * drop a subvolume tree.
  6623. *
  6624. * this function traverses the tree freeing any blocks that only
  6625. * referenced by the tree.
  6626. *
  6627. * when a shared tree block is found. this function decreases its
  6628. * reference count by one. if update_ref is true, this function
  6629. * also make sure backrefs for the shared block and all lower level
  6630. * blocks are properly updated.
  6631. *
  6632. * If called with for_reloc == 0, may exit early with -EAGAIN
  6633. */
  6634. int btrfs_drop_snapshot(struct btrfs_root *root,
  6635. struct btrfs_block_rsv *block_rsv, int update_ref,
  6636. int for_reloc)
  6637. {
  6638. struct btrfs_path *path;
  6639. struct btrfs_trans_handle *trans;
  6640. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6641. struct btrfs_root_item *root_item = &root->root_item;
  6642. struct walk_control *wc;
  6643. struct btrfs_key key;
  6644. int err = 0;
  6645. int ret;
  6646. int level;
  6647. bool root_dropped = false;
  6648. path = btrfs_alloc_path();
  6649. if (!path) {
  6650. err = -ENOMEM;
  6651. goto out;
  6652. }
  6653. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6654. if (!wc) {
  6655. btrfs_free_path(path);
  6656. err = -ENOMEM;
  6657. goto out;
  6658. }
  6659. trans = btrfs_start_transaction(tree_root, 0);
  6660. if (IS_ERR(trans)) {
  6661. err = PTR_ERR(trans);
  6662. goto out_free;
  6663. }
  6664. if (block_rsv)
  6665. trans->block_rsv = block_rsv;
  6666. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6667. level = btrfs_header_level(root->node);
  6668. path->nodes[level] = btrfs_lock_root_node(root);
  6669. btrfs_set_lock_blocking(path->nodes[level]);
  6670. path->slots[level] = 0;
  6671. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6672. memset(&wc->update_progress, 0,
  6673. sizeof(wc->update_progress));
  6674. } else {
  6675. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6676. memcpy(&wc->update_progress, &key,
  6677. sizeof(wc->update_progress));
  6678. level = root_item->drop_level;
  6679. BUG_ON(level == 0);
  6680. path->lowest_level = level;
  6681. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6682. path->lowest_level = 0;
  6683. if (ret < 0) {
  6684. err = ret;
  6685. goto out_end_trans;
  6686. }
  6687. WARN_ON(ret > 0);
  6688. /*
  6689. * unlock our path, this is safe because only this
  6690. * function is allowed to delete this snapshot
  6691. */
  6692. btrfs_unlock_up_safe(path, 0);
  6693. level = btrfs_header_level(root->node);
  6694. while (1) {
  6695. btrfs_tree_lock(path->nodes[level]);
  6696. btrfs_set_lock_blocking(path->nodes[level]);
  6697. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6698. ret = btrfs_lookup_extent_info(trans, root,
  6699. path->nodes[level]->start,
  6700. level, 1, &wc->refs[level],
  6701. &wc->flags[level]);
  6702. if (ret < 0) {
  6703. err = ret;
  6704. goto out_end_trans;
  6705. }
  6706. BUG_ON(wc->refs[level] == 0);
  6707. if (level == root_item->drop_level)
  6708. break;
  6709. btrfs_tree_unlock(path->nodes[level]);
  6710. path->locks[level] = 0;
  6711. WARN_ON(wc->refs[level] != 1);
  6712. level--;
  6713. }
  6714. }
  6715. wc->level = level;
  6716. wc->shared_level = -1;
  6717. wc->stage = DROP_REFERENCE;
  6718. wc->update_ref = update_ref;
  6719. wc->keep_locks = 0;
  6720. wc->for_reloc = for_reloc;
  6721. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6722. while (1) {
  6723. ret = walk_down_tree(trans, root, path, wc);
  6724. if (ret < 0) {
  6725. err = ret;
  6726. break;
  6727. }
  6728. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6729. if (ret < 0) {
  6730. err = ret;
  6731. break;
  6732. }
  6733. if (ret > 0) {
  6734. BUG_ON(wc->stage != DROP_REFERENCE);
  6735. break;
  6736. }
  6737. if (wc->stage == DROP_REFERENCE) {
  6738. level = wc->level;
  6739. btrfs_node_key(path->nodes[level],
  6740. &root_item->drop_progress,
  6741. path->slots[level]);
  6742. root_item->drop_level = level;
  6743. }
  6744. BUG_ON(wc->level == 0);
  6745. if (btrfs_should_end_transaction(trans, tree_root) ||
  6746. (!for_reloc && btrfs_need_cleaner_sleep(root))) {
  6747. ret = btrfs_update_root(trans, tree_root,
  6748. &root->root_key,
  6749. root_item);
  6750. if (ret) {
  6751. btrfs_abort_transaction(trans, tree_root, ret);
  6752. err = ret;
  6753. goto out_end_trans;
  6754. }
  6755. btrfs_end_transaction_throttle(trans, tree_root);
  6756. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  6757. pr_debug("btrfs: drop snapshot early exit\n");
  6758. err = -EAGAIN;
  6759. goto out_free;
  6760. }
  6761. trans = btrfs_start_transaction(tree_root, 0);
  6762. if (IS_ERR(trans)) {
  6763. err = PTR_ERR(trans);
  6764. goto out_free;
  6765. }
  6766. if (block_rsv)
  6767. trans->block_rsv = block_rsv;
  6768. }
  6769. }
  6770. btrfs_release_path(path);
  6771. if (err)
  6772. goto out_end_trans;
  6773. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6774. if (ret) {
  6775. btrfs_abort_transaction(trans, tree_root, ret);
  6776. goto out_end_trans;
  6777. }
  6778. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6779. ret = btrfs_find_root(tree_root, &root->root_key, path,
  6780. NULL, NULL);
  6781. if (ret < 0) {
  6782. btrfs_abort_transaction(trans, tree_root, ret);
  6783. err = ret;
  6784. goto out_end_trans;
  6785. } else if (ret > 0) {
  6786. /* if we fail to delete the orphan item this time
  6787. * around, it'll get picked up the next time.
  6788. *
  6789. * The most common failure here is just -ENOENT.
  6790. */
  6791. btrfs_del_orphan_item(trans, tree_root,
  6792. root->root_key.objectid);
  6793. }
  6794. }
  6795. if (root->in_radix) {
  6796. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  6797. } else {
  6798. free_extent_buffer(root->node);
  6799. free_extent_buffer(root->commit_root);
  6800. btrfs_put_fs_root(root);
  6801. }
  6802. root_dropped = true;
  6803. out_end_trans:
  6804. btrfs_end_transaction_throttle(trans, tree_root);
  6805. out_free:
  6806. kfree(wc);
  6807. btrfs_free_path(path);
  6808. out:
  6809. /*
  6810. * So if we need to stop dropping the snapshot for whatever reason we
  6811. * need to make sure to add it back to the dead root list so that we
  6812. * keep trying to do the work later. This also cleans up roots if we
  6813. * don't have it in the radix (like when we recover after a power fail
  6814. * or unmount) so we don't leak memory.
  6815. */
  6816. if (!for_reloc && root_dropped == false)
  6817. btrfs_add_dead_root(root);
  6818. if (err)
  6819. btrfs_std_error(root->fs_info, err);
  6820. return err;
  6821. }
  6822. /*
  6823. * drop subtree rooted at tree block 'node'.
  6824. *
  6825. * NOTE: this function will unlock and release tree block 'node'
  6826. * only used by relocation code
  6827. */
  6828. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6829. struct btrfs_root *root,
  6830. struct extent_buffer *node,
  6831. struct extent_buffer *parent)
  6832. {
  6833. struct btrfs_path *path;
  6834. struct walk_control *wc;
  6835. int level;
  6836. int parent_level;
  6837. int ret = 0;
  6838. int wret;
  6839. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6840. path = btrfs_alloc_path();
  6841. if (!path)
  6842. return -ENOMEM;
  6843. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6844. if (!wc) {
  6845. btrfs_free_path(path);
  6846. return -ENOMEM;
  6847. }
  6848. btrfs_assert_tree_locked(parent);
  6849. parent_level = btrfs_header_level(parent);
  6850. extent_buffer_get(parent);
  6851. path->nodes[parent_level] = parent;
  6852. path->slots[parent_level] = btrfs_header_nritems(parent);
  6853. btrfs_assert_tree_locked(node);
  6854. level = btrfs_header_level(node);
  6855. path->nodes[level] = node;
  6856. path->slots[level] = 0;
  6857. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6858. wc->refs[parent_level] = 1;
  6859. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6860. wc->level = level;
  6861. wc->shared_level = -1;
  6862. wc->stage = DROP_REFERENCE;
  6863. wc->update_ref = 0;
  6864. wc->keep_locks = 1;
  6865. wc->for_reloc = 1;
  6866. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6867. while (1) {
  6868. wret = walk_down_tree(trans, root, path, wc);
  6869. if (wret < 0) {
  6870. ret = wret;
  6871. break;
  6872. }
  6873. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6874. if (wret < 0)
  6875. ret = wret;
  6876. if (wret != 0)
  6877. break;
  6878. }
  6879. kfree(wc);
  6880. btrfs_free_path(path);
  6881. return ret;
  6882. }
  6883. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6884. {
  6885. u64 num_devices;
  6886. u64 stripped;
  6887. /*
  6888. * if restripe for this chunk_type is on pick target profile and
  6889. * return, otherwise do the usual balance
  6890. */
  6891. stripped = get_restripe_target(root->fs_info, flags);
  6892. if (stripped)
  6893. return extended_to_chunk(stripped);
  6894. /*
  6895. * we add in the count of missing devices because we want
  6896. * to make sure that any RAID levels on a degraded FS
  6897. * continue to be honored.
  6898. */
  6899. num_devices = root->fs_info->fs_devices->rw_devices +
  6900. root->fs_info->fs_devices->missing_devices;
  6901. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6902. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  6903. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6904. if (num_devices == 1) {
  6905. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6906. stripped = flags & ~stripped;
  6907. /* turn raid0 into single device chunks */
  6908. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6909. return stripped;
  6910. /* turn mirroring into duplication */
  6911. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6912. BTRFS_BLOCK_GROUP_RAID10))
  6913. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6914. } else {
  6915. /* they already had raid on here, just return */
  6916. if (flags & stripped)
  6917. return flags;
  6918. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6919. stripped = flags & ~stripped;
  6920. /* switch duplicated blocks with raid1 */
  6921. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6922. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6923. /* this is drive concat, leave it alone */
  6924. }
  6925. return flags;
  6926. }
  6927. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6928. {
  6929. struct btrfs_space_info *sinfo = cache->space_info;
  6930. u64 num_bytes;
  6931. u64 min_allocable_bytes;
  6932. int ret = -ENOSPC;
  6933. /*
  6934. * We need some metadata space and system metadata space for
  6935. * allocating chunks in some corner cases until we force to set
  6936. * it to be readonly.
  6937. */
  6938. if ((sinfo->flags &
  6939. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6940. !force)
  6941. min_allocable_bytes = 1 * 1024 * 1024;
  6942. else
  6943. min_allocable_bytes = 0;
  6944. spin_lock(&sinfo->lock);
  6945. spin_lock(&cache->lock);
  6946. if (cache->ro) {
  6947. ret = 0;
  6948. goto out;
  6949. }
  6950. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6951. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6952. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6953. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6954. min_allocable_bytes <= sinfo->total_bytes) {
  6955. sinfo->bytes_readonly += num_bytes;
  6956. cache->ro = 1;
  6957. ret = 0;
  6958. }
  6959. out:
  6960. spin_unlock(&cache->lock);
  6961. spin_unlock(&sinfo->lock);
  6962. return ret;
  6963. }
  6964. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6965. struct btrfs_block_group_cache *cache)
  6966. {
  6967. struct btrfs_trans_handle *trans;
  6968. u64 alloc_flags;
  6969. int ret;
  6970. BUG_ON(cache->ro);
  6971. trans = btrfs_join_transaction(root);
  6972. if (IS_ERR(trans))
  6973. return PTR_ERR(trans);
  6974. alloc_flags = update_block_group_flags(root, cache->flags);
  6975. if (alloc_flags != cache->flags) {
  6976. ret = do_chunk_alloc(trans, root, alloc_flags,
  6977. CHUNK_ALLOC_FORCE);
  6978. if (ret < 0)
  6979. goto out;
  6980. }
  6981. ret = set_block_group_ro(cache, 0);
  6982. if (!ret)
  6983. goto out;
  6984. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6985. ret = do_chunk_alloc(trans, root, alloc_flags,
  6986. CHUNK_ALLOC_FORCE);
  6987. if (ret < 0)
  6988. goto out;
  6989. ret = set_block_group_ro(cache, 0);
  6990. out:
  6991. btrfs_end_transaction(trans, root);
  6992. return ret;
  6993. }
  6994. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6995. struct btrfs_root *root, u64 type)
  6996. {
  6997. u64 alloc_flags = get_alloc_profile(root, type);
  6998. return do_chunk_alloc(trans, root, alloc_flags,
  6999. CHUNK_ALLOC_FORCE);
  7000. }
  7001. /*
  7002. * helper to account the unused space of all the readonly block group in the
  7003. * list. takes mirrors into account.
  7004. */
  7005. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  7006. {
  7007. struct btrfs_block_group_cache *block_group;
  7008. u64 free_bytes = 0;
  7009. int factor;
  7010. list_for_each_entry(block_group, groups_list, list) {
  7011. spin_lock(&block_group->lock);
  7012. if (!block_group->ro) {
  7013. spin_unlock(&block_group->lock);
  7014. continue;
  7015. }
  7016. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7017. BTRFS_BLOCK_GROUP_RAID10 |
  7018. BTRFS_BLOCK_GROUP_DUP))
  7019. factor = 2;
  7020. else
  7021. factor = 1;
  7022. free_bytes += (block_group->key.offset -
  7023. btrfs_block_group_used(&block_group->item)) *
  7024. factor;
  7025. spin_unlock(&block_group->lock);
  7026. }
  7027. return free_bytes;
  7028. }
  7029. /*
  7030. * helper to account the unused space of all the readonly block group in the
  7031. * space_info. takes mirrors into account.
  7032. */
  7033. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  7034. {
  7035. int i;
  7036. u64 free_bytes = 0;
  7037. spin_lock(&sinfo->lock);
  7038. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  7039. if (!list_empty(&sinfo->block_groups[i]))
  7040. free_bytes += __btrfs_get_ro_block_group_free_space(
  7041. &sinfo->block_groups[i]);
  7042. spin_unlock(&sinfo->lock);
  7043. return free_bytes;
  7044. }
  7045. void btrfs_set_block_group_rw(struct btrfs_root *root,
  7046. struct btrfs_block_group_cache *cache)
  7047. {
  7048. struct btrfs_space_info *sinfo = cache->space_info;
  7049. u64 num_bytes;
  7050. BUG_ON(!cache->ro);
  7051. spin_lock(&sinfo->lock);
  7052. spin_lock(&cache->lock);
  7053. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7054. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7055. sinfo->bytes_readonly -= num_bytes;
  7056. cache->ro = 0;
  7057. spin_unlock(&cache->lock);
  7058. spin_unlock(&sinfo->lock);
  7059. }
  7060. /*
  7061. * checks to see if its even possible to relocate this block group.
  7062. *
  7063. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7064. * ok to go ahead and try.
  7065. */
  7066. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7067. {
  7068. struct btrfs_block_group_cache *block_group;
  7069. struct btrfs_space_info *space_info;
  7070. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7071. struct btrfs_device *device;
  7072. struct btrfs_trans_handle *trans;
  7073. u64 min_free;
  7074. u64 dev_min = 1;
  7075. u64 dev_nr = 0;
  7076. u64 target;
  7077. int index;
  7078. int full = 0;
  7079. int ret = 0;
  7080. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7081. /* odd, couldn't find the block group, leave it alone */
  7082. if (!block_group)
  7083. return -1;
  7084. min_free = btrfs_block_group_used(&block_group->item);
  7085. /* no bytes used, we're good */
  7086. if (!min_free)
  7087. goto out;
  7088. space_info = block_group->space_info;
  7089. spin_lock(&space_info->lock);
  7090. full = space_info->full;
  7091. /*
  7092. * if this is the last block group we have in this space, we can't
  7093. * relocate it unless we're able to allocate a new chunk below.
  7094. *
  7095. * Otherwise, we need to make sure we have room in the space to handle
  7096. * all of the extents from this block group. If we can, we're good
  7097. */
  7098. if ((space_info->total_bytes != block_group->key.offset) &&
  7099. (space_info->bytes_used + space_info->bytes_reserved +
  7100. space_info->bytes_pinned + space_info->bytes_readonly +
  7101. min_free < space_info->total_bytes)) {
  7102. spin_unlock(&space_info->lock);
  7103. goto out;
  7104. }
  7105. spin_unlock(&space_info->lock);
  7106. /*
  7107. * ok we don't have enough space, but maybe we have free space on our
  7108. * devices to allocate new chunks for relocation, so loop through our
  7109. * alloc devices and guess if we have enough space. if this block
  7110. * group is going to be restriped, run checks against the target
  7111. * profile instead of the current one.
  7112. */
  7113. ret = -1;
  7114. /*
  7115. * index:
  7116. * 0: raid10
  7117. * 1: raid1
  7118. * 2: dup
  7119. * 3: raid0
  7120. * 4: single
  7121. */
  7122. target = get_restripe_target(root->fs_info, block_group->flags);
  7123. if (target) {
  7124. index = __get_raid_index(extended_to_chunk(target));
  7125. } else {
  7126. /*
  7127. * this is just a balance, so if we were marked as full
  7128. * we know there is no space for a new chunk
  7129. */
  7130. if (full)
  7131. goto out;
  7132. index = get_block_group_index(block_group);
  7133. }
  7134. if (index == BTRFS_RAID_RAID10) {
  7135. dev_min = 4;
  7136. /* Divide by 2 */
  7137. min_free >>= 1;
  7138. } else if (index == BTRFS_RAID_RAID1) {
  7139. dev_min = 2;
  7140. } else if (index == BTRFS_RAID_DUP) {
  7141. /* Multiply by 2 */
  7142. min_free <<= 1;
  7143. } else if (index == BTRFS_RAID_RAID0) {
  7144. dev_min = fs_devices->rw_devices;
  7145. do_div(min_free, dev_min);
  7146. }
  7147. /* We need to do this so that we can look at pending chunks */
  7148. trans = btrfs_join_transaction(root);
  7149. if (IS_ERR(trans)) {
  7150. ret = PTR_ERR(trans);
  7151. goto out;
  7152. }
  7153. mutex_lock(&root->fs_info->chunk_mutex);
  7154. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7155. u64 dev_offset;
  7156. /*
  7157. * check to make sure we can actually find a chunk with enough
  7158. * space to fit our block group in.
  7159. */
  7160. if (device->total_bytes > device->bytes_used + min_free &&
  7161. !device->is_tgtdev_for_dev_replace) {
  7162. ret = find_free_dev_extent(trans, device, min_free,
  7163. &dev_offset, NULL);
  7164. if (!ret)
  7165. dev_nr++;
  7166. if (dev_nr >= dev_min)
  7167. break;
  7168. ret = -1;
  7169. }
  7170. }
  7171. mutex_unlock(&root->fs_info->chunk_mutex);
  7172. btrfs_end_transaction(trans, root);
  7173. out:
  7174. btrfs_put_block_group(block_group);
  7175. return ret;
  7176. }
  7177. static int find_first_block_group(struct btrfs_root *root,
  7178. struct btrfs_path *path, struct btrfs_key *key)
  7179. {
  7180. int ret = 0;
  7181. struct btrfs_key found_key;
  7182. struct extent_buffer *leaf;
  7183. int slot;
  7184. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7185. if (ret < 0)
  7186. goto out;
  7187. while (1) {
  7188. slot = path->slots[0];
  7189. leaf = path->nodes[0];
  7190. if (slot >= btrfs_header_nritems(leaf)) {
  7191. ret = btrfs_next_leaf(root, path);
  7192. if (ret == 0)
  7193. continue;
  7194. if (ret < 0)
  7195. goto out;
  7196. break;
  7197. }
  7198. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7199. if (found_key.objectid >= key->objectid &&
  7200. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7201. ret = 0;
  7202. goto out;
  7203. }
  7204. path->slots[0]++;
  7205. }
  7206. out:
  7207. return ret;
  7208. }
  7209. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7210. {
  7211. struct btrfs_block_group_cache *block_group;
  7212. u64 last = 0;
  7213. while (1) {
  7214. struct inode *inode;
  7215. block_group = btrfs_lookup_first_block_group(info, last);
  7216. while (block_group) {
  7217. spin_lock(&block_group->lock);
  7218. if (block_group->iref)
  7219. break;
  7220. spin_unlock(&block_group->lock);
  7221. block_group = next_block_group(info->tree_root,
  7222. block_group);
  7223. }
  7224. if (!block_group) {
  7225. if (last == 0)
  7226. break;
  7227. last = 0;
  7228. continue;
  7229. }
  7230. inode = block_group->inode;
  7231. block_group->iref = 0;
  7232. block_group->inode = NULL;
  7233. spin_unlock(&block_group->lock);
  7234. iput(inode);
  7235. last = block_group->key.objectid + block_group->key.offset;
  7236. btrfs_put_block_group(block_group);
  7237. }
  7238. }
  7239. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7240. {
  7241. struct btrfs_block_group_cache *block_group;
  7242. struct btrfs_space_info *space_info;
  7243. struct btrfs_caching_control *caching_ctl;
  7244. struct rb_node *n;
  7245. down_write(&info->extent_commit_sem);
  7246. while (!list_empty(&info->caching_block_groups)) {
  7247. caching_ctl = list_entry(info->caching_block_groups.next,
  7248. struct btrfs_caching_control, list);
  7249. list_del(&caching_ctl->list);
  7250. put_caching_control(caching_ctl);
  7251. }
  7252. up_write(&info->extent_commit_sem);
  7253. spin_lock(&info->block_group_cache_lock);
  7254. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7255. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7256. cache_node);
  7257. rb_erase(&block_group->cache_node,
  7258. &info->block_group_cache_tree);
  7259. spin_unlock(&info->block_group_cache_lock);
  7260. down_write(&block_group->space_info->groups_sem);
  7261. list_del(&block_group->list);
  7262. up_write(&block_group->space_info->groups_sem);
  7263. if (block_group->cached == BTRFS_CACHE_STARTED)
  7264. wait_block_group_cache_done(block_group);
  7265. /*
  7266. * We haven't cached this block group, which means we could
  7267. * possibly have excluded extents on this block group.
  7268. */
  7269. if (block_group->cached == BTRFS_CACHE_NO ||
  7270. block_group->cached == BTRFS_CACHE_ERROR)
  7271. free_excluded_extents(info->extent_root, block_group);
  7272. btrfs_remove_free_space_cache(block_group);
  7273. btrfs_put_block_group(block_group);
  7274. spin_lock(&info->block_group_cache_lock);
  7275. }
  7276. spin_unlock(&info->block_group_cache_lock);
  7277. /* now that all the block groups are freed, go through and
  7278. * free all the space_info structs. This is only called during
  7279. * the final stages of unmount, and so we know nobody is
  7280. * using them. We call synchronize_rcu() once before we start,
  7281. * just to be on the safe side.
  7282. */
  7283. synchronize_rcu();
  7284. release_global_block_rsv(info);
  7285. while(!list_empty(&info->space_info)) {
  7286. space_info = list_entry(info->space_info.next,
  7287. struct btrfs_space_info,
  7288. list);
  7289. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  7290. if (space_info->bytes_pinned > 0 ||
  7291. space_info->bytes_reserved > 0 ||
  7292. space_info->bytes_may_use > 0) {
  7293. WARN_ON(1);
  7294. dump_space_info(space_info, 0, 0);
  7295. }
  7296. }
  7297. percpu_counter_destroy(&space_info->total_bytes_pinned);
  7298. list_del(&space_info->list);
  7299. kfree(space_info);
  7300. }
  7301. return 0;
  7302. }
  7303. static void __link_block_group(struct btrfs_space_info *space_info,
  7304. struct btrfs_block_group_cache *cache)
  7305. {
  7306. int index = get_block_group_index(cache);
  7307. down_write(&space_info->groups_sem);
  7308. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7309. up_write(&space_info->groups_sem);
  7310. }
  7311. int btrfs_read_block_groups(struct btrfs_root *root)
  7312. {
  7313. struct btrfs_path *path;
  7314. int ret;
  7315. struct btrfs_block_group_cache *cache;
  7316. struct btrfs_fs_info *info = root->fs_info;
  7317. struct btrfs_space_info *space_info;
  7318. struct btrfs_key key;
  7319. struct btrfs_key found_key;
  7320. struct extent_buffer *leaf;
  7321. int need_clear = 0;
  7322. u64 cache_gen;
  7323. root = info->extent_root;
  7324. key.objectid = 0;
  7325. key.offset = 0;
  7326. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7327. path = btrfs_alloc_path();
  7328. if (!path)
  7329. return -ENOMEM;
  7330. path->reada = 1;
  7331. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  7332. if (btrfs_test_opt(root, SPACE_CACHE) &&
  7333. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  7334. need_clear = 1;
  7335. if (btrfs_test_opt(root, CLEAR_CACHE))
  7336. need_clear = 1;
  7337. while (1) {
  7338. ret = find_first_block_group(root, path, &key);
  7339. if (ret > 0)
  7340. break;
  7341. if (ret != 0)
  7342. goto error;
  7343. leaf = path->nodes[0];
  7344. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7345. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7346. if (!cache) {
  7347. ret = -ENOMEM;
  7348. goto error;
  7349. }
  7350. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7351. GFP_NOFS);
  7352. if (!cache->free_space_ctl) {
  7353. kfree(cache);
  7354. ret = -ENOMEM;
  7355. goto error;
  7356. }
  7357. atomic_set(&cache->count, 1);
  7358. spin_lock_init(&cache->lock);
  7359. cache->fs_info = info;
  7360. INIT_LIST_HEAD(&cache->list);
  7361. INIT_LIST_HEAD(&cache->cluster_list);
  7362. if (need_clear) {
  7363. /*
  7364. * When we mount with old space cache, we need to
  7365. * set BTRFS_DC_CLEAR and set dirty flag.
  7366. *
  7367. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  7368. * truncate the old free space cache inode and
  7369. * setup a new one.
  7370. * b) Setting 'dirty flag' makes sure that we flush
  7371. * the new space cache info onto disk.
  7372. */
  7373. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7374. if (btrfs_test_opt(root, SPACE_CACHE))
  7375. cache->dirty = 1;
  7376. }
  7377. read_extent_buffer(leaf, &cache->item,
  7378. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7379. sizeof(cache->item));
  7380. memcpy(&cache->key, &found_key, sizeof(found_key));
  7381. key.objectid = found_key.objectid + found_key.offset;
  7382. btrfs_release_path(path);
  7383. cache->flags = btrfs_block_group_flags(&cache->item);
  7384. cache->sectorsize = root->sectorsize;
  7385. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7386. &root->fs_info->mapping_tree,
  7387. found_key.objectid);
  7388. btrfs_init_free_space_ctl(cache);
  7389. /*
  7390. * We need to exclude the super stripes now so that the space
  7391. * info has super bytes accounted for, otherwise we'll think
  7392. * we have more space than we actually do.
  7393. */
  7394. ret = exclude_super_stripes(root, cache);
  7395. if (ret) {
  7396. /*
  7397. * We may have excluded something, so call this just in
  7398. * case.
  7399. */
  7400. free_excluded_extents(root, cache);
  7401. kfree(cache->free_space_ctl);
  7402. kfree(cache);
  7403. goto error;
  7404. }
  7405. /*
  7406. * check for two cases, either we are full, and therefore
  7407. * don't need to bother with the caching work since we won't
  7408. * find any space, or we are empty, and we can just add all
  7409. * the space in and be done with it. This saves us _alot_ of
  7410. * time, particularly in the full case.
  7411. */
  7412. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7413. cache->last_byte_to_unpin = (u64)-1;
  7414. cache->cached = BTRFS_CACHE_FINISHED;
  7415. free_excluded_extents(root, cache);
  7416. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7417. cache->last_byte_to_unpin = (u64)-1;
  7418. cache->cached = BTRFS_CACHE_FINISHED;
  7419. add_new_free_space(cache, root->fs_info,
  7420. found_key.objectid,
  7421. found_key.objectid +
  7422. found_key.offset);
  7423. free_excluded_extents(root, cache);
  7424. }
  7425. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7426. if (ret) {
  7427. btrfs_remove_free_space_cache(cache);
  7428. btrfs_put_block_group(cache);
  7429. goto error;
  7430. }
  7431. ret = update_space_info(info, cache->flags, found_key.offset,
  7432. btrfs_block_group_used(&cache->item),
  7433. &space_info);
  7434. if (ret) {
  7435. btrfs_remove_free_space_cache(cache);
  7436. spin_lock(&info->block_group_cache_lock);
  7437. rb_erase(&cache->cache_node,
  7438. &info->block_group_cache_tree);
  7439. spin_unlock(&info->block_group_cache_lock);
  7440. btrfs_put_block_group(cache);
  7441. goto error;
  7442. }
  7443. cache->space_info = space_info;
  7444. spin_lock(&cache->space_info->lock);
  7445. cache->space_info->bytes_readonly += cache->bytes_super;
  7446. spin_unlock(&cache->space_info->lock);
  7447. __link_block_group(space_info, cache);
  7448. set_avail_alloc_bits(root->fs_info, cache->flags);
  7449. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7450. set_block_group_ro(cache, 1);
  7451. }
  7452. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7453. if (!(get_alloc_profile(root, space_info->flags) &
  7454. (BTRFS_BLOCK_GROUP_RAID10 |
  7455. BTRFS_BLOCK_GROUP_RAID1 |
  7456. BTRFS_BLOCK_GROUP_RAID5 |
  7457. BTRFS_BLOCK_GROUP_RAID6 |
  7458. BTRFS_BLOCK_GROUP_DUP)))
  7459. continue;
  7460. /*
  7461. * avoid allocating from un-mirrored block group if there are
  7462. * mirrored block groups.
  7463. */
  7464. list_for_each_entry(cache,
  7465. &space_info->block_groups[BTRFS_RAID_RAID0],
  7466. list)
  7467. set_block_group_ro(cache, 1);
  7468. list_for_each_entry(cache,
  7469. &space_info->block_groups[BTRFS_RAID_SINGLE],
  7470. list)
  7471. set_block_group_ro(cache, 1);
  7472. }
  7473. init_global_block_rsv(info);
  7474. ret = 0;
  7475. error:
  7476. btrfs_free_path(path);
  7477. return ret;
  7478. }
  7479. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  7480. struct btrfs_root *root)
  7481. {
  7482. struct btrfs_block_group_cache *block_group, *tmp;
  7483. struct btrfs_root *extent_root = root->fs_info->extent_root;
  7484. struct btrfs_block_group_item item;
  7485. struct btrfs_key key;
  7486. int ret = 0;
  7487. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  7488. new_bg_list) {
  7489. list_del_init(&block_group->new_bg_list);
  7490. if (ret)
  7491. continue;
  7492. spin_lock(&block_group->lock);
  7493. memcpy(&item, &block_group->item, sizeof(item));
  7494. memcpy(&key, &block_group->key, sizeof(key));
  7495. spin_unlock(&block_group->lock);
  7496. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  7497. sizeof(item));
  7498. if (ret)
  7499. btrfs_abort_transaction(trans, extent_root, ret);
  7500. ret = btrfs_finish_chunk_alloc(trans, extent_root,
  7501. key.objectid, key.offset);
  7502. if (ret)
  7503. btrfs_abort_transaction(trans, extent_root, ret);
  7504. }
  7505. }
  7506. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7507. struct btrfs_root *root, u64 bytes_used,
  7508. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7509. u64 size)
  7510. {
  7511. int ret;
  7512. struct btrfs_root *extent_root;
  7513. struct btrfs_block_group_cache *cache;
  7514. extent_root = root->fs_info->extent_root;
  7515. root->fs_info->last_trans_log_full_commit = trans->transid;
  7516. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7517. if (!cache)
  7518. return -ENOMEM;
  7519. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7520. GFP_NOFS);
  7521. if (!cache->free_space_ctl) {
  7522. kfree(cache);
  7523. return -ENOMEM;
  7524. }
  7525. cache->key.objectid = chunk_offset;
  7526. cache->key.offset = size;
  7527. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7528. cache->sectorsize = root->sectorsize;
  7529. cache->fs_info = root->fs_info;
  7530. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7531. &root->fs_info->mapping_tree,
  7532. chunk_offset);
  7533. atomic_set(&cache->count, 1);
  7534. spin_lock_init(&cache->lock);
  7535. INIT_LIST_HEAD(&cache->list);
  7536. INIT_LIST_HEAD(&cache->cluster_list);
  7537. INIT_LIST_HEAD(&cache->new_bg_list);
  7538. btrfs_init_free_space_ctl(cache);
  7539. btrfs_set_block_group_used(&cache->item, bytes_used);
  7540. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7541. cache->flags = type;
  7542. btrfs_set_block_group_flags(&cache->item, type);
  7543. cache->last_byte_to_unpin = (u64)-1;
  7544. cache->cached = BTRFS_CACHE_FINISHED;
  7545. ret = exclude_super_stripes(root, cache);
  7546. if (ret) {
  7547. /*
  7548. * We may have excluded something, so call this just in
  7549. * case.
  7550. */
  7551. free_excluded_extents(root, cache);
  7552. kfree(cache->free_space_ctl);
  7553. kfree(cache);
  7554. return ret;
  7555. }
  7556. add_new_free_space(cache, root->fs_info, chunk_offset,
  7557. chunk_offset + size);
  7558. free_excluded_extents(root, cache);
  7559. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7560. if (ret) {
  7561. btrfs_remove_free_space_cache(cache);
  7562. btrfs_put_block_group(cache);
  7563. return ret;
  7564. }
  7565. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7566. &cache->space_info);
  7567. if (ret) {
  7568. btrfs_remove_free_space_cache(cache);
  7569. spin_lock(&root->fs_info->block_group_cache_lock);
  7570. rb_erase(&cache->cache_node,
  7571. &root->fs_info->block_group_cache_tree);
  7572. spin_unlock(&root->fs_info->block_group_cache_lock);
  7573. btrfs_put_block_group(cache);
  7574. return ret;
  7575. }
  7576. update_global_block_rsv(root->fs_info);
  7577. spin_lock(&cache->space_info->lock);
  7578. cache->space_info->bytes_readonly += cache->bytes_super;
  7579. spin_unlock(&cache->space_info->lock);
  7580. __link_block_group(cache->space_info, cache);
  7581. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  7582. set_avail_alloc_bits(extent_root->fs_info, type);
  7583. return 0;
  7584. }
  7585. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  7586. {
  7587. u64 extra_flags = chunk_to_extended(flags) &
  7588. BTRFS_EXTENDED_PROFILE_MASK;
  7589. write_seqlock(&fs_info->profiles_lock);
  7590. if (flags & BTRFS_BLOCK_GROUP_DATA)
  7591. fs_info->avail_data_alloc_bits &= ~extra_flags;
  7592. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  7593. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  7594. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  7595. fs_info->avail_system_alloc_bits &= ~extra_flags;
  7596. write_sequnlock(&fs_info->profiles_lock);
  7597. }
  7598. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7599. struct btrfs_root *root, u64 group_start)
  7600. {
  7601. struct btrfs_path *path;
  7602. struct btrfs_block_group_cache *block_group;
  7603. struct btrfs_free_cluster *cluster;
  7604. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7605. struct btrfs_key key;
  7606. struct inode *inode;
  7607. int ret;
  7608. int index;
  7609. int factor;
  7610. root = root->fs_info->extent_root;
  7611. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7612. BUG_ON(!block_group);
  7613. BUG_ON(!block_group->ro);
  7614. /*
  7615. * Free the reserved super bytes from this block group before
  7616. * remove it.
  7617. */
  7618. free_excluded_extents(root, block_group);
  7619. memcpy(&key, &block_group->key, sizeof(key));
  7620. index = get_block_group_index(block_group);
  7621. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7622. BTRFS_BLOCK_GROUP_RAID1 |
  7623. BTRFS_BLOCK_GROUP_RAID10))
  7624. factor = 2;
  7625. else
  7626. factor = 1;
  7627. /* make sure this block group isn't part of an allocation cluster */
  7628. cluster = &root->fs_info->data_alloc_cluster;
  7629. spin_lock(&cluster->refill_lock);
  7630. btrfs_return_cluster_to_free_space(block_group, cluster);
  7631. spin_unlock(&cluster->refill_lock);
  7632. /*
  7633. * make sure this block group isn't part of a metadata
  7634. * allocation cluster
  7635. */
  7636. cluster = &root->fs_info->meta_alloc_cluster;
  7637. spin_lock(&cluster->refill_lock);
  7638. btrfs_return_cluster_to_free_space(block_group, cluster);
  7639. spin_unlock(&cluster->refill_lock);
  7640. path = btrfs_alloc_path();
  7641. if (!path) {
  7642. ret = -ENOMEM;
  7643. goto out;
  7644. }
  7645. inode = lookup_free_space_inode(tree_root, block_group, path);
  7646. if (!IS_ERR(inode)) {
  7647. ret = btrfs_orphan_add(trans, inode);
  7648. if (ret) {
  7649. btrfs_add_delayed_iput(inode);
  7650. goto out;
  7651. }
  7652. clear_nlink(inode);
  7653. /* One for the block groups ref */
  7654. spin_lock(&block_group->lock);
  7655. if (block_group->iref) {
  7656. block_group->iref = 0;
  7657. block_group->inode = NULL;
  7658. spin_unlock(&block_group->lock);
  7659. iput(inode);
  7660. } else {
  7661. spin_unlock(&block_group->lock);
  7662. }
  7663. /* One for our lookup ref */
  7664. btrfs_add_delayed_iput(inode);
  7665. }
  7666. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7667. key.offset = block_group->key.objectid;
  7668. key.type = 0;
  7669. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7670. if (ret < 0)
  7671. goto out;
  7672. if (ret > 0)
  7673. btrfs_release_path(path);
  7674. if (ret == 0) {
  7675. ret = btrfs_del_item(trans, tree_root, path);
  7676. if (ret)
  7677. goto out;
  7678. btrfs_release_path(path);
  7679. }
  7680. spin_lock(&root->fs_info->block_group_cache_lock);
  7681. rb_erase(&block_group->cache_node,
  7682. &root->fs_info->block_group_cache_tree);
  7683. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  7684. root->fs_info->first_logical_byte = (u64)-1;
  7685. spin_unlock(&root->fs_info->block_group_cache_lock);
  7686. down_write(&block_group->space_info->groups_sem);
  7687. /*
  7688. * we must use list_del_init so people can check to see if they
  7689. * are still on the list after taking the semaphore
  7690. */
  7691. list_del_init(&block_group->list);
  7692. if (list_empty(&block_group->space_info->block_groups[index]))
  7693. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7694. up_write(&block_group->space_info->groups_sem);
  7695. if (block_group->cached == BTRFS_CACHE_STARTED)
  7696. wait_block_group_cache_done(block_group);
  7697. btrfs_remove_free_space_cache(block_group);
  7698. spin_lock(&block_group->space_info->lock);
  7699. block_group->space_info->total_bytes -= block_group->key.offset;
  7700. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7701. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7702. spin_unlock(&block_group->space_info->lock);
  7703. memcpy(&key, &block_group->key, sizeof(key));
  7704. btrfs_clear_space_info_full(root->fs_info);
  7705. btrfs_put_block_group(block_group);
  7706. btrfs_put_block_group(block_group);
  7707. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7708. if (ret > 0)
  7709. ret = -EIO;
  7710. if (ret < 0)
  7711. goto out;
  7712. ret = btrfs_del_item(trans, root, path);
  7713. out:
  7714. btrfs_free_path(path);
  7715. return ret;
  7716. }
  7717. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7718. {
  7719. struct btrfs_space_info *space_info;
  7720. struct btrfs_super_block *disk_super;
  7721. u64 features;
  7722. u64 flags;
  7723. int mixed = 0;
  7724. int ret;
  7725. disk_super = fs_info->super_copy;
  7726. if (!btrfs_super_root(disk_super))
  7727. return 1;
  7728. features = btrfs_super_incompat_flags(disk_super);
  7729. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7730. mixed = 1;
  7731. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7732. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7733. if (ret)
  7734. goto out;
  7735. if (mixed) {
  7736. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7737. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7738. } else {
  7739. flags = BTRFS_BLOCK_GROUP_METADATA;
  7740. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7741. if (ret)
  7742. goto out;
  7743. flags = BTRFS_BLOCK_GROUP_DATA;
  7744. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7745. }
  7746. out:
  7747. return ret;
  7748. }
  7749. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7750. {
  7751. return unpin_extent_range(root, start, end);
  7752. }
  7753. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7754. u64 num_bytes, u64 *actual_bytes)
  7755. {
  7756. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7757. }
  7758. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7759. {
  7760. struct btrfs_fs_info *fs_info = root->fs_info;
  7761. struct btrfs_block_group_cache *cache = NULL;
  7762. u64 group_trimmed;
  7763. u64 start;
  7764. u64 end;
  7765. u64 trimmed = 0;
  7766. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7767. int ret = 0;
  7768. /*
  7769. * try to trim all FS space, our block group may start from non-zero.
  7770. */
  7771. if (range->len == total_bytes)
  7772. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7773. else
  7774. cache = btrfs_lookup_block_group(fs_info, range->start);
  7775. while (cache) {
  7776. if (cache->key.objectid >= (range->start + range->len)) {
  7777. btrfs_put_block_group(cache);
  7778. break;
  7779. }
  7780. start = max(range->start, cache->key.objectid);
  7781. end = min(range->start + range->len,
  7782. cache->key.objectid + cache->key.offset);
  7783. if (end - start >= range->minlen) {
  7784. if (!block_group_cache_done(cache)) {
  7785. ret = cache_block_group(cache, 0);
  7786. if (ret) {
  7787. btrfs_put_block_group(cache);
  7788. break;
  7789. }
  7790. ret = wait_block_group_cache_done(cache);
  7791. if (ret) {
  7792. btrfs_put_block_group(cache);
  7793. break;
  7794. }
  7795. }
  7796. ret = btrfs_trim_block_group(cache,
  7797. &group_trimmed,
  7798. start,
  7799. end,
  7800. range->minlen);
  7801. trimmed += group_trimmed;
  7802. if (ret) {
  7803. btrfs_put_block_group(cache);
  7804. break;
  7805. }
  7806. }
  7807. cache = next_block_group(fs_info->tree_root, cache);
  7808. }
  7809. range->len = trimmed;
  7810. return ret;
  7811. }