extent-tree.c 213 KB

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