extent-tree.c 212 KB

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