extent-tree.c 236 KB

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