extent-tree.c 234 KB

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