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