extent-tree.c 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297
  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 "compat.h"
  24. #include "hash.h"
  25. #include "crc32c.h"
  26. #include "ctree.h"
  27. #include "disk-io.h"
  28. #include "print-tree.h"
  29. #include "transaction.h"
  30. #include "volumes.h"
  31. #include "locking.h"
  32. #include "ref-cache.h"
  33. #define PENDING_EXTENT_INSERT 0
  34. #define PENDING_EXTENT_DELETE 1
  35. #define PENDING_BACKREF_UPDATE 2
  36. struct pending_extent_op {
  37. int type;
  38. u64 bytenr;
  39. u64 num_bytes;
  40. u64 parent;
  41. u64 orig_parent;
  42. u64 generation;
  43. u64 orig_generation;
  44. int level;
  45. struct list_head list;
  46. int del;
  47. };
  48. static int finish_current_insert(struct btrfs_trans_handle *trans,
  49. struct btrfs_root *extent_root, int all);
  50. static int del_pending_extents(struct btrfs_trans_handle *trans,
  51. struct btrfs_root *extent_root, int all);
  52. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  53. struct btrfs_root *root,
  54. u64 bytenr, u64 num_bytes, int is_data);
  55. static int update_block_group(struct btrfs_trans_handle *trans,
  56. struct btrfs_root *root,
  57. u64 bytenr, u64 num_bytes, int alloc,
  58. int mark_free);
  59. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  60. {
  61. return (cache->flags & bits) == bits;
  62. }
  63. /*
  64. * this adds the block group to the fs_info rb tree for the block group
  65. * cache
  66. */
  67. static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  68. struct btrfs_block_group_cache *block_group)
  69. {
  70. struct rb_node **p;
  71. struct rb_node *parent = NULL;
  72. struct btrfs_block_group_cache *cache;
  73. spin_lock(&info->block_group_cache_lock);
  74. p = &info->block_group_cache_tree.rb_node;
  75. while (*p) {
  76. parent = *p;
  77. cache = rb_entry(parent, struct btrfs_block_group_cache,
  78. cache_node);
  79. if (block_group->key.objectid < cache->key.objectid) {
  80. p = &(*p)->rb_left;
  81. } else if (block_group->key.objectid > cache->key.objectid) {
  82. p = &(*p)->rb_right;
  83. } else {
  84. spin_unlock(&info->block_group_cache_lock);
  85. return -EEXIST;
  86. }
  87. }
  88. rb_link_node(&block_group->cache_node, parent, p);
  89. rb_insert_color(&block_group->cache_node,
  90. &info->block_group_cache_tree);
  91. spin_unlock(&info->block_group_cache_lock);
  92. return 0;
  93. }
  94. /*
  95. * This will return the block group at or after bytenr if contains is 0, else
  96. * it will return the block group that contains the bytenr
  97. */
  98. static struct btrfs_block_group_cache *
  99. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  100. int contains)
  101. {
  102. struct btrfs_block_group_cache *cache, *ret = NULL;
  103. struct rb_node *n;
  104. u64 end, start;
  105. spin_lock(&info->block_group_cache_lock);
  106. n = info->block_group_cache_tree.rb_node;
  107. while (n) {
  108. cache = rb_entry(n, struct btrfs_block_group_cache,
  109. cache_node);
  110. end = cache->key.objectid + cache->key.offset - 1;
  111. start = cache->key.objectid;
  112. if (bytenr < start) {
  113. if (!contains && (!ret || start < ret->key.objectid))
  114. ret = cache;
  115. n = n->rb_left;
  116. } else if (bytenr > start) {
  117. if (contains && bytenr <= end) {
  118. ret = cache;
  119. break;
  120. }
  121. n = n->rb_right;
  122. } else {
  123. ret = cache;
  124. break;
  125. }
  126. }
  127. if (ret)
  128. atomic_inc(&ret->count);
  129. spin_unlock(&info->block_group_cache_lock);
  130. return ret;
  131. }
  132. /*
  133. * this is only called by cache_block_group, since we could have freed extents
  134. * we need to check the pinned_extents for any extents that can't be used yet
  135. * since their free space will be released as soon as the transaction commits.
  136. */
  137. static int add_new_free_space(struct btrfs_block_group_cache *block_group,
  138. struct btrfs_fs_info *info, u64 start, u64 end)
  139. {
  140. u64 extent_start, extent_end, size;
  141. int ret;
  142. mutex_lock(&info->pinned_mutex);
  143. while (start < end) {
  144. ret = find_first_extent_bit(&info->pinned_extents, start,
  145. &extent_start, &extent_end,
  146. EXTENT_DIRTY);
  147. if (ret)
  148. break;
  149. if (extent_start == start) {
  150. start = extent_end + 1;
  151. } else if (extent_start > start && extent_start < end) {
  152. size = extent_start - start;
  153. ret = btrfs_add_free_space(block_group, start,
  154. size);
  155. BUG_ON(ret);
  156. start = extent_end + 1;
  157. } else {
  158. break;
  159. }
  160. }
  161. if (start < end) {
  162. size = end - start;
  163. ret = btrfs_add_free_space(block_group, start, size);
  164. BUG_ON(ret);
  165. }
  166. mutex_unlock(&info->pinned_mutex);
  167. return 0;
  168. }
  169. static int remove_sb_from_cache(struct btrfs_root *root,
  170. struct btrfs_block_group_cache *cache)
  171. {
  172. u64 bytenr;
  173. u64 *logical;
  174. int stripe_len;
  175. int i, nr, ret;
  176. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  177. bytenr = btrfs_sb_offset(i);
  178. ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
  179. cache->key.objectid, bytenr, 0,
  180. &logical, &nr, &stripe_len);
  181. BUG_ON(ret);
  182. while (nr--) {
  183. btrfs_remove_free_space(cache, logical[nr],
  184. stripe_len);
  185. }
  186. kfree(logical);
  187. }
  188. return 0;
  189. }
  190. static int cache_block_group(struct btrfs_root *root,
  191. struct btrfs_block_group_cache *block_group)
  192. {
  193. struct btrfs_path *path;
  194. int ret = 0;
  195. struct btrfs_key key;
  196. struct extent_buffer *leaf;
  197. int slot;
  198. u64 last;
  199. if (!block_group)
  200. return 0;
  201. root = root->fs_info->extent_root;
  202. if (block_group->cached)
  203. return 0;
  204. path = btrfs_alloc_path();
  205. if (!path)
  206. return -ENOMEM;
  207. path->reada = 2;
  208. /*
  209. * we get into deadlocks with paths held by callers of this function.
  210. * since the alloc_mutex is protecting things right now, just
  211. * skip the locking here
  212. */
  213. path->skip_locking = 1;
  214. last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
  215. key.objectid = last;
  216. key.offset = 0;
  217. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  218. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  219. if (ret < 0)
  220. goto err;
  221. while (1) {
  222. leaf = path->nodes[0];
  223. slot = path->slots[0];
  224. if (slot >= btrfs_header_nritems(leaf)) {
  225. ret = btrfs_next_leaf(root, path);
  226. if (ret < 0)
  227. goto err;
  228. if (ret == 0)
  229. continue;
  230. else
  231. break;
  232. }
  233. btrfs_item_key_to_cpu(leaf, &key, slot);
  234. if (key.objectid < block_group->key.objectid)
  235. goto next;
  236. if (key.objectid >= block_group->key.objectid +
  237. block_group->key.offset)
  238. break;
  239. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  240. add_new_free_space(block_group, root->fs_info, last,
  241. key.objectid);
  242. last = key.objectid + key.offset;
  243. }
  244. next:
  245. path->slots[0]++;
  246. }
  247. add_new_free_space(block_group, root->fs_info, last,
  248. block_group->key.objectid +
  249. block_group->key.offset);
  250. remove_sb_from_cache(root, block_group);
  251. block_group->cached = 1;
  252. ret = 0;
  253. err:
  254. btrfs_free_path(path);
  255. return ret;
  256. }
  257. /*
  258. * return the block group that starts at or after bytenr
  259. */
  260. static struct btrfs_block_group_cache *
  261. btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
  262. {
  263. struct btrfs_block_group_cache *cache;
  264. cache = block_group_cache_tree_search(info, bytenr, 0);
  265. return cache;
  266. }
  267. /*
  268. * return the block group that contains teh given bytenr
  269. */
  270. struct btrfs_block_group_cache *btrfs_lookup_block_group(
  271. struct btrfs_fs_info *info,
  272. u64 bytenr)
  273. {
  274. struct btrfs_block_group_cache *cache;
  275. cache = block_group_cache_tree_search(info, bytenr, 1);
  276. return cache;
  277. }
  278. static inline void put_block_group(struct btrfs_block_group_cache *cache)
  279. {
  280. if (atomic_dec_and_test(&cache->count))
  281. kfree(cache);
  282. }
  283. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  284. u64 flags)
  285. {
  286. struct list_head *head = &info->space_info;
  287. struct btrfs_space_info *found;
  288. list_for_each_entry(found, head, list) {
  289. if (found->flags == flags)
  290. return found;
  291. }
  292. return NULL;
  293. }
  294. static u64 div_factor(u64 num, int factor)
  295. {
  296. if (factor == 10)
  297. return num;
  298. num *= factor;
  299. do_div(num, 10);
  300. return num;
  301. }
  302. u64 btrfs_find_block_group(struct btrfs_root *root,
  303. u64 search_start, u64 search_hint, int owner)
  304. {
  305. struct btrfs_block_group_cache *cache;
  306. u64 used;
  307. u64 last = max(search_hint, search_start);
  308. u64 group_start = 0;
  309. int full_search = 0;
  310. int factor = 9;
  311. int wrapped = 0;
  312. again:
  313. while (1) {
  314. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  315. if (!cache)
  316. break;
  317. spin_lock(&cache->lock);
  318. last = cache->key.objectid + cache->key.offset;
  319. used = btrfs_block_group_used(&cache->item);
  320. if ((full_search || !cache->ro) &&
  321. block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
  322. if (used + cache->pinned + cache->reserved <
  323. div_factor(cache->key.offset, factor)) {
  324. group_start = cache->key.objectid;
  325. spin_unlock(&cache->lock);
  326. put_block_group(cache);
  327. goto found;
  328. }
  329. }
  330. spin_unlock(&cache->lock);
  331. put_block_group(cache);
  332. cond_resched();
  333. }
  334. if (!wrapped) {
  335. last = search_start;
  336. wrapped = 1;
  337. goto again;
  338. }
  339. if (!full_search && factor < 10) {
  340. last = search_start;
  341. full_search = 1;
  342. factor = 10;
  343. goto again;
  344. }
  345. found:
  346. return group_start;
  347. }
  348. /* simple helper to search for an existing extent at a given offset */
  349. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  350. {
  351. int ret;
  352. struct btrfs_key key;
  353. struct btrfs_path *path;
  354. path = btrfs_alloc_path();
  355. BUG_ON(!path);
  356. key.objectid = start;
  357. key.offset = len;
  358. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  359. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  360. 0, 0);
  361. btrfs_free_path(path);
  362. return ret;
  363. }
  364. /*
  365. * Back reference rules. Back refs have three main goals:
  366. *
  367. * 1) differentiate between all holders of references to an extent so that
  368. * when a reference is dropped we can make sure it was a valid reference
  369. * before freeing the extent.
  370. *
  371. * 2) Provide enough information to quickly find the holders of an extent
  372. * if we notice a given block is corrupted or bad.
  373. *
  374. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  375. * maintenance. This is actually the same as #2, but with a slightly
  376. * different use case.
  377. *
  378. * File extents can be referenced by:
  379. *
  380. * - multiple snapshots, subvolumes, or different generations in one subvol
  381. * - different files inside a single subvolume
  382. * - different offsets inside a file (bookend extents in file.c)
  383. *
  384. * The extent ref structure has fields for:
  385. *
  386. * - Objectid of the subvolume root
  387. * - Generation number of the tree holding the reference
  388. * - objectid of the file holding the reference
  389. * - number of references holding by parent node (alway 1 for tree blocks)
  390. *
  391. * Btree leaf may hold multiple references to a file extent. In most cases,
  392. * these references are from same file and the corresponding offsets inside
  393. * the file are close together.
  394. *
  395. * When a file extent is allocated the fields are filled in:
  396. * (root_key.objectid, trans->transid, inode objectid, 1)
  397. *
  398. * When a leaf is cow'd new references are added for every file extent found
  399. * in the leaf. It looks similar to the create case, but trans->transid will
  400. * be different when the block is cow'd.
  401. *
  402. * (root_key.objectid, trans->transid, inode objectid,
  403. * number of references in the leaf)
  404. *
  405. * When a file extent is removed either during snapshot deletion or
  406. * file truncation, we find the corresponding back reference and check
  407. * the following fields:
  408. *
  409. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  410. * inode objectid)
  411. *
  412. * Btree extents can be referenced by:
  413. *
  414. * - Different subvolumes
  415. * - Different generations of the same subvolume
  416. *
  417. * When a tree block is created, back references are inserted:
  418. *
  419. * (root->root_key.objectid, trans->transid, level, 1)
  420. *
  421. * When a tree block is cow'd, new back references are added for all the
  422. * blocks it points to. If the tree block isn't in reference counted root,
  423. * the old back references are removed. These new back references are of
  424. * the form (trans->transid will have increased since creation):
  425. *
  426. * (root->root_key.objectid, trans->transid, level, 1)
  427. *
  428. * When a backref is in deleting, the following fields are checked:
  429. *
  430. * if backref was for a tree root:
  431. * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
  432. * else
  433. * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
  434. *
  435. * Back Reference Key composing:
  436. *
  437. * The key objectid corresponds to the first byte in the extent, the key
  438. * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
  439. * byte of parent extent. If a extent is tree root, the key offset is set
  440. * to the key objectid.
  441. */
  442. static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
  443. struct btrfs_root *root,
  444. struct btrfs_path *path,
  445. u64 bytenr, u64 parent,
  446. u64 ref_root, u64 ref_generation,
  447. u64 owner_objectid, int del)
  448. {
  449. struct btrfs_key key;
  450. struct btrfs_extent_ref *ref;
  451. struct extent_buffer *leaf;
  452. u64 ref_objectid;
  453. int ret;
  454. key.objectid = bytenr;
  455. key.type = BTRFS_EXTENT_REF_KEY;
  456. key.offset = parent;
  457. ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
  458. if (ret < 0)
  459. goto out;
  460. if (ret > 0) {
  461. ret = -ENOENT;
  462. goto out;
  463. }
  464. leaf = path->nodes[0];
  465. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  466. ref_objectid = btrfs_ref_objectid(leaf, ref);
  467. if (btrfs_ref_root(leaf, ref) != ref_root ||
  468. btrfs_ref_generation(leaf, ref) != ref_generation ||
  469. (ref_objectid != owner_objectid &&
  470. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  471. ret = -EIO;
  472. WARN_ON(1);
  473. goto out;
  474. }
  475. ret = 0;
  476. out:
  477. return ret;
  478. }
  479. /*
  480. * updates all the backrefs that are pending on update_list for the
  481. * extent_root
  482. */
  483. static noinline int update_backrefs(struct btrfs_trans_handle *trans,
  484. struct btrfs_root *extent_root,
  485. struct btrfs_path *path,
  486. struct list_head *update_list)
  487. {
  488. struct btrfs_key key;
  489. struct btrfs_extent_ref *ref;
  490. struct btrfs_fs_info *info = extent_root->fs_info;
  491. struct pending_extent_op *op;
  492. struct extent_buffer *leaf;
  493. int ret = 0;
  494. struct list_head *cur = update_list->next;
  495. u64 ref_objectid;
  496. u64 ref_root = extent_root->root_key.objectid;
  497. op = list_entry(cur, struct pending_extent_op, list);
  498. search:
  499. key.objectid = op->bytenr;
  500. key.type = BTRFS_EXTENT_REF_KEY;
  501. key.offset = op->orig_parent;
  502. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
  503. BUG_ON(ret);
  504. leaf = path->nodes[0];
  505. loop:
  506. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  507. ref_objectid = btrfs_ref_objectid(leaf, ref);
  508. if (btrfs_ref_root(leaf, ref) != ref_root ||
  509. btrfs_ref_generation(leaf, ref) != op->orig_generation ||
  510. (ref_objectid != op->level &&
  511. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  512. printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
  513. "root %llu, owner %u\n",
  514. (unsigned long long)op->bytenr,
  515. (unsigned long long)op->orig_parent,
  516. (unsigned long long)ref_root, op->level);
  517. btrfs_print_leaf(extent_root, leaf);
  518. BUG();
  519. }
  520. key.objectid = op->bytenr;
  521. key.offset = op->parent;
  522. key.type = BTRFS_EXTENT_REF_KEY;
  523. ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
  524. BUG_ON(ret);
  525. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  526. btrfs_set_ref_generation(leaf, ref, op->generation);
  527. cur = cur->next;
  528. list_del_init(&op->list);
  529. unlock_extent(&info->extent_ins, op->bytenr,
  530. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  531. kfree(op);
  532. if (cur == update_list) {
  533. btrfs_mark_buffer_dirty(path->nodes[0]);
  534. btrfs_release_path(extent_root, path);
  535. goto out;
  536. }
  537. op = list_entry(cur, struct pending_extent_op, list);
  538. path->slots[0]++;
  539. while (path->slots[0] < btrfs_header_nritems(leaf)) {
  540. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  541. if (key.objectid == op->bytenr &&
  542. key.type == BTRFS_EXTENT_REF_KEY)
  543. goto loop;
  544. path->slots[0]++;
  545. }
  546. btrfs_mark_buffer_dirty(path->nodes[0]);
  547. btrfs_release_path(extent_root, path);
  548. goto search;
  549. out:
  550. return 0;
  551. }
  552. static noinline int insert_extents(struct btrfs_trans_handle *trans,
  553. struct btrfs_root *extent_root,
  554. struct btrfs_path *path,
  555. struct list_head *insert_list, int nr)
  556. {
  557. struct btrfs_key *keys;
  558. u32 *data_size;
  559. struct pending_extent_op *op;
  560. struct extent_buffer *leaf;
  561. struct list_head *cur = insert_list->next;
  562. struct btrfs_fs_info *info = extent_root->fs_info;
  563. u64 ref_root = extent_root->root_key.objectid;
  564. int i = 0, last = 0, ret;
  565. int total = nr * 2;
  566. if (!nr)
  567. return 0;
  568. keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
  569. if (!keys)
  570. return -ENOMEM;
  571. data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
  572. if (!data_size) {
  573. kfree(keys);
  574. return -ENOMEM;
  575. }
  576. list_for_each_entry(op, insert_list, list) {
  577. keys[i].objectid = op->bytenr;
  578. keys[i].offset = op->num_bytes;
  579. keys[i].type = BTRFS_EXTENT_ITEM_KEY;
  580. data_size[i] = sizeof(struct btrfs_extent_item);
  581. i++;
  582. keys[i].objectid = op->bytenr;
  583. keys[i].offset = op->parent;
  584. keys[i].type = BTRFS_EXTENT_REF_KEY;
  585. data_size[i] = sizeof(struct btrfs_extent_ref);
  586. i++;
  587. }
  588. op = list_entry(cur, struct pending_extent_op, list);
  589. i = 0;
  590. while (i < total) {
  591. int c;
  592. ret = btrfs_insert_some_items(trans, extent_root, path,
  593. keys+i, data_size+i, total-i);
  594. BUG_ON(ret < 0);
  595. if (last && ret > 1)
  596. BUG();
  597. leaf = path->nodes[0];
  598. for (c = 0; c < ret; c++) {
  599. int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
  600. /*
  601. * if the first item we inserted was a backref, then
  602. * the EXTENT_ITEM will be the odd c's, else it will
  603. * be the even c's
  604. */
  605. if ((ref_first && (c % 2)) ||
  606. (!ref_first && !(c % 2))) {
  607. struct btrfs_extent_item *itm;
  608. itm = btrfs_item_ptr(leaf, path->slots[0] + c,
  609. struct btrfs_extent_item);
  610. btrfs_set_extent_refs(path->nodes[0], itm, 1);
  611. op->del++;
  612. } else {
  613. struct btrfs_extent_ref *ref;
  614. ref = btrfs_item_ptr(leaf, path->slots[0] + c,
  615. struct btrfs_extent_ref);
  616. btrfs_set_ref_root(leaf, ref, ref_root);
  617. btrfs_set_ref_generation(leaf, ref,
  618. op->generation);
  619. btrfs_set_ref_objectid(leaf, ref, op->level);
  620. btrfs_set_ref_num_refs(leaf, ref, 1);
  621. op->del++;
  622. }
  623. /*
  624. * using del to see when its ok to free up the
  625. * pending_extent_op. In the case where we insert the
  626. * last item on the list in order to help do batching
  627. * we need to not free the extent op until we actually
  628. * insert the extent_item
  629. */
  630. if (op->del == 2) {
  631. unlock_extent(&info->extent_ins, op->bytenr,
  632. op->bytenr + op->num_bytes - 1,
  633. GFP_NOFS);
  634. cur = cur->next;
  635. list_del_init(&op->list);
  636. kfree(op);
  637. if (cur != insert_list)
  638. op = list_entry(cur,
  639. struct pending_extent_op,
  640. list);
  641. }
  642. }
  643. btrfs_mark_buffer_dirty(leaf);
  644. btrfs_release_path(extent_root, path);
  645. /*
  646. * Ok backref's and items usually go right next to eachother,
  647. * but if we could only insert 1 item that means that we
  648. * inserted on the end of a leaf, and we have no idea what may
  649. * be on the next leaf so we just play it safe. In order to
  650. * try and help this case we insert the last thing on our
  651. * insert list so hopefully it will end up being the last
  652. * thing on the leaf and everything else will be before it,
  653. * which will let us insert a whole bunch of items at the same
  654. * time.
  655. */
  656. if (ret == 1 && !last && (i + ret < total)) {
  657. /*
  658. * last: where we will pick up the next time around
  659. * i: our current key to insert, will be total - 1
  660. * cur: the current op we are screwing with
  661. * op: duh
  662. */
  663. last = i + ret;
  664. i = total - 1;
  665. cur = insert_list->prev;
  666. op = list_entry(cur, struct pending_extent_op, list);
  667. } else if (last) {
  668. /*
  669. * ok we successfully inserted the last item on the
  670. * list, lets reset everything
  671. *
  672. * i: our current key to insert, so where we left off
  673. * last time
  674. * last: done with this
  675. * cur: the op we are messing with
  676. * op: duh
  677. * total: since we inserted the last key, we need to
  678. * decrement total so we dont overflow
  679. */
  680. i = last;
  681. last = 0;
  682. total--;
  683. if (i < total) {
  684. cur = insert_list->next;
  685. op = list_entry(cur, struct pending_extent_op,
  686. list);
  687. }
  688. } else {
  689. i += ret;
  690. }
  691. cond_resched();
  692. }
  693. ret = 0;
  694. kfree(keys);
  695. kfree(data_size);
  696. return ret;
  697. }
  698. static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
  699. struct btrfs_root *root,
  700. struct btrfs_path *path,
  701. u64 bytenr, u64 parent,
  702. u64 ref_root, u64 ref_generation,
  703. u64 owner_objectid)
  704. {
  705. struct btrfs_key key;
  706. struct extent_buffer *leaf;
  707. struct btrfs_extent_ref *ref;
  708. u32 num_refs;
  709. int ret;
  710. key.objectid = bytenr;
  711. key.type = BTRFS_EXTENT_REF_KEY;
  712. key.offset = parent;
  713. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
  714. if (ret == 0) {
  715. leaf = path->nodes[0];
  716. ref = btrfs_item_ptr(leaf, path->slots[0],
  717. struct btrfs_extent_ref);
  718. btrfs_set_ref_root(leaf, ref, ref_root);
  719. btrfs_set_ref_generation(leaf, ref, ref_generation);
  720. btrfs_set_ref_objectid(leaf, ref, owner_objectid);
  721. btrfs_set_ref_num_refs(leaf, ref, 1);
  722. } else if (ret == -EEXIST) {
  723. u64 existing_owner;
  724. BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
  725. leaf = path->nodes[0];
  726. ref = btrfs_item_ptr(leaf, path->slots[0],
  727. struct btrfs_extent_ref);
  728. if (btrfs_ref_root(leaf, ref) != ref_root ||
  729. btrfs_ref_generation(leaf, ref) != ref_generation) {
  730. ret = -EIO;
  731. WARN_ON(1);
  732. goto out;
  733. }
  734. num_refs = btrfs_ref_num_refs(leaf, ref);
  735. BUG_ON(num_refs == 0);
  736. btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
  737. existing_owner = btrfs_ref_objectid(leaf, ref);
  738. if (existing_owner != owner_objectid &&
  739. existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
  740. btrfs_set_ref_objectid(leaf, ref,
  741. BTRFS_MULTIPLE_OBJECTIDS);
  742. }
  743. ret = 0;
  744. } else {
  745. goto out;
  746. }
  747. btrfs_mark_buffer_dirty(path->nodes[0]);
  748. out:
  749. btrfs_release_path(root, path);
  750. return ret;
  751. }
  752. static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
  753. struct btrfs_root *root,
  754. struct btrfs_path *path)
  755. {
  756. struct extent_buffer *leaf;
  757. struct btrfs_extent_ref *ref;
  758. u32 num_refs;
  759. int ret = 0;
  760. leaf = path->nodes[0];
  761. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  762. num_refs = btrfs_ref_num_refs(leaf, ref);
  763. BUG_ON(num_refs == 0);
  764. num_refs -= 1;
  765. if (num_refs == 0) {
  766. ret = btrfs_del_item(trans, root, path);
  767. } else {
  768. btrfs_set_ref_num_refs(leaf, ref, num_refs);
  769. btrfs_mark_buffer_dirty(leaf);
  770. }
  771. btrfs_release_path(root, path);
  772. return ret;
  773. }
  774. #ifdef BIO_RW_DISCARD
  775. static void btrfs_issue_discard(struct block_device *bdev,
  776. u64 start, u64 len)
  777. {
  778. blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
  779. }
  780. #endif
  781. static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
  782. u64 num_bytes)
  783. {
  784. #ifdef BIO_RW_DISCARD
  785. int ret;
  786. u64 map_length = num_bytes;
  787. struct btrfs_multi_bio *multi = NULL;
  788. /* Tell the block device(s) that the sectors can be discarded */
  789. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  790. bytenr, &map_length, &multi, 0);
  791. if (!ret) {
  792. struct btrfs_bio_stripe *stripe = multi->stripes;
  793. int i;
  794. if (map_length > num_bytes)
  795. map_length = num_bytes;
  796. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  797. btrfs_issue_discard(stripe->dev->bdev,
  798. stripe->physical,
  799. map_length);
  800. }
  801. kfree(multi);
  802. }
  803. return ret;
  804. #else
  805. return 0;
  806. #endif
  807. }
  808. static noinline int free_extents(struct btrfs_trans_handle *trans,
  809. struct btrfs_root *extent_root,
  810. struct list_head *del_list)
  811. {
  812. struct btrfs_fs_info *info = extent_root->fs_info;
  813. struct btrfs_path *path;
  814. struct btrfs_key key, found_key;
  815. struct extent_buffer *leaf;
  816. struct list_head *cur;
  817. struct pending_extent_op *op;
  818. struct btrfs_extent_item *ei;
  819. int ret, num_to_del, extent_slot = 0, found_extent = 0;
  820. u32 refs;
  821. u64 bytes_freed = 0;
  822. path = btrfs_alloc_path();
  823. if (!path)
  824. return -ENOMEM;
  825. path->reada = 1;
  826. search:
  827. /* search for the backref for the current ref we want to delete */
  828. cur = del_list->next;
  829. op = list_entry(cur, struct pending_extent_op, list);
  830. ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
  831. op->orig_parent,
  832. extent_root->root_key.objectid,
  833. op->orig_generation, op->level, 1);
  834. if (ret) {
  835. printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
  836. "root %llu gen %llu owner %u\n",
  837. (unsigned long long)op->bytenr,
  838. (unsigned long long)extent_root->root_key.objectid,
  839. (unsigned long long)op->orig_generation, op->level);
  840. btrfs_print_leaf(extent_root, path->nodes[0]);
  841. WARN_ON(1);
  842. goto out;
  843. }
  844. extent_slot = path->slots[0];
  845. num_to_del = 1;
  846. found_extent = 0;
  847. /*
  848. * if we aren't the first item on the leaf we can move back one and see
  849. * if our ref is right next to our extent item
  850. */
  851. if (likely(extent_slot)) {
  852. extent_slot--;
  853. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  854. extent_slot);
  855. if (found_key.objectid == op->bytenr &&
  856. found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  857. found_key.offset == op->num_bytes) {
  858. num_to_del++;
  859. found_extent = 1;
  860. }
  861. }
  862. /*
  863. * if we didn't find the extent we need to delete the backref and then
  864. * search for the extent item key so we can update its ref count
  865. */
  866. if (!found_extent) {
  867. key.objectid = op->bytenr;
  868. key.type = BTRFS_EXTENT_ITEM_KEY;
  869. key.offset = op->num_bytes;
  870. ret = remove_extent_backref(trans, extent_root, path);
  871. BUG_ON(ret);
  872. btrfs_release_path(extent_root, path);
  873. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  874. BUG_ON(ret);
  875. extent_slot = path->slots[0];
  876. }
  877. /* this is where we update the ref count for the extent */
  878. leaf = path->nodes[0];
  879. ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
  880. refs = btrfs_extent_refs(leaf, ei);
  881. BUG_ON(refs == 0);
  882. refs--;
  883. btrfs_set_extent_refs(leaf, ei, refs);
  884. btrfs_mark_buffer_dirty(leaf);
  885. /*
  886. * This extent needs deleting. The reason cur_slot is extent_slot +
  887. * num_to_del is because extent_slot points to the slot where the extent
  888. * is, and if the backref was not right next to the extent we will be
  889. * deleting at least 1 item, and will want to start searching at the
  890. * slot directly next to extent_slot. However if we did find the
  891. * backref next to the extent item them we will be deleting at least 2
  892. * items and will want to start searching directly after the ref slot
  893. */
  894. if (!refs) {
  895. struct list_head *pos, *n, *end;
  896. int cur_slot = extent_slot+num_to_del;
  897. u64 super_used;
  898. u64 root_used;
  899. path->slots[0] = extent_slot;
  900. bytes_freed = op->num_bytes;
  901. mutex_lock(&info->pinned_mutex);
  902. ret = pin_down_bytes(trans, extent_root, op->bytenr,
  903. op->num_bytes, op->level >=
  904. BTRFS_FIRST_FREE_OBJECTID);
  905. mutex_unlock(&info->pinned_mutex);
  906. BUG_ON(ret < 0);
  907. op->del = ret;
  908. /*
  909. * we need to see if we can delete multiple things at once, so
  910. * start looping through the list of extents we are wanting to
  911. * delete and see if their extent/backref's are right next to
  912. * eachother and the extents only have 1 ref
  913. */
  914. for (pos = cur->next; pos != del_list; pos = pos->next) {
  915. struct pending_extent_op *tmp;
  916. tmp = list_entry(pos, struct pending_extent_op, list);
  917. /* we only want to delete extent+ref at this stage */
  918. if (cur_slot >= btrfs_header_nritems(leaf) - 1)
  919. break;
  920. btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
  921. if (found_key.objectid != tmp->bytenr ||
  922. found_key.type != BTRFS_EXTENT_ITEM_KEY ||
  923. found_key.offset != tmp->num_bytes)
  924. break;
  925. /* check to make sure this extent only has one ref */
  926. ei = btrfs_item_ptr(leaf, cur_slot,
  927. struct btrfs_extent_item);
  928. if (btrfs_extent_refs(leaf, ei) != 1)
  929. break;
  930. btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
  931. if (found_key.objectid != tmp->bytenr ||
  932. found_key.type != BTRFS_EXTENT_REF_KEY ||
  933. found_key.offset != tmp->orig_parent)
  934. break;
  935. /*
  936. * the ref is right next to the extent, we can set the
  937. * ref count to 0 since we will delete them both now
  938. */
  939. btrfs_set_extent_refs(leaf, ei, 0);
  940. /* pin down the bytes for this extent */
  941. mutex_lock(&info->pinned_mutex);
  942. ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
  943. tmp->num_bytes, tmp->level >=
  944. BTRFS_FIRST_FREE_OBJECTID);
  945. mutex_unlock(&info->pinned_mutex);
  946. BUG_ON(ret < 0);
  947. /*
  948. * use the del field to tell if we need to go ahead and
  949. * free up the extent when we delete the item or not.
  950. */
  951. tmp->del = ret;
  952. bytes_freed += tmp->num_bytes;
  953. num_to_del += 2;
  954. cur_slot += 2;
  955. }
  956. end = pos;
  957. /* update the free space counters */
  958. spin_lock(&info->delalloc_lock);
  959. super_used = btrfs_super_bytes_used(&info->super_copy);
  960. btrfs_set_super_bytes_used(&info->super_copy,
  961. super_used - bytes_freed);
  962. root_used = btrfs_root_used(&extent_root->root_item);
  963. btrfs_set_root_used(&extent_root->root_item,
  964. root_used - bytes_freed);
  965. spin_unlock(&info->delalloc_lock);
  966. /* delete the items */
  967. ret = btrfs_del_items(trans, extent_root, path,
  968. path->slots[0], num_to_del);
  969. BUG_ON(ret);
  970. /*
  971. * loop through the extents we deleted and do the cleanup work
  972. * on them
  973. */
  974. for (pos = cur, n = pos->next; pos != end;
  975. pos = n, n = pos->next) {
  976. struct pending_extent_op *tmp;
  977. tmp = list_entry(pos, struct pending_extent_op, list);
  978. /*
  979. * remember tmp->del tells us wether or not we pinned
  980. * down the extent
  981. */
  982. ret = update_block_group(trans, extent_root,
  983. tmp->bytenr, tmp->num_bytes, 0,
  984. tmp->del);
  985. BUG_ON(ret);
  986. list_del_init(&tmp->list);
  987. unlock_extent(&info->extent_ins, tmp->bytenr,
  988. tmp->bytenr + tmp->num_bytes - 1,
  989. GFP_NOFS);
  990. kfree(tmp);
  991. }
  992. } else if (refs && found_extent) {
  993. /*
  994. * the ref and extent were right next to eachother, but the
  995. * extent still has a ref, so just free the backref and keep
  996. * going
  997. */
  998. ret = remove_extent_backref(trans, extent_root, path);
  999. BUG_ON(ret);
  1000. list_del_init(&op->list);
  1001. unlock_extent(&info->extent_ins, op->bytenr,
  1002. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  1003. kfree(op);
  1004. } else {
  1005. /*
  1006. * the extent has multiple refs and the backref we were looking
  1007. * for was not right next to it, so just unlock and go next,
  1008. * we're good to go
  1009. */
  1010. list_del_init(&op->list);
  1011. unlock_extent(&info->extent_ins, op->bytenr,
  1012. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  1013. kfree(op);
  1014. }
  1015. btrfs_release_path(extent_root, path);
  1016. if (!list_empty(del_list))
  1017. goto search;
  1018. out:
  1019. btrfs_free_path(path);
  1020. return ret;
  1021. }
  1022. static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  1023. struct btrfs_root *root, u64 bytenr,
  1024. u64 orig_parent, u64 parent,
  1025. u64 orig_root, u64 ref_root,
  1026. u64 orig_generation, u64 ref_generation,
  1027. u64 owner_objectid)
  1028. {
  1029. int ret;
  1030. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1031. struct btrfs_path *path;
  1032. if (root == root->fs_info->extent_root) {
  1033. struct pending_extent_op *extent_op;
  1034. u64 num_bytes;
  1035. BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
  1036. num_bytes = btrfs_level_size(root, (int)owner_objectid);
  1037. mutex_lock(&root->fs_info->extent_ins_mutex);
  1038. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  1039. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  1040. u64 priv;
  1041. ret = get_state_private(&root->fs_info->extent_ins,
  1042. bytenr, &priv);
  1043. BUG_ON(ret);
  1044. extent_op = (struct pending_extent_op *)
  1045. (unsigned long)priv;
  1046. BUG_ON(extent_op->parent != orig_parent);
  1047. BUG_ON(extent_op->generation != orig_generation);
  1048. extent_op->parent = parent;
  1049. extent_op->generation = ref_generation;
  1050. } else {
  1051. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  1052. BUG_ON(!extent_op);
  1053. extent_op->type = PENDING_BACKREF_UPDATE;
  1054. extent_op->bytenr = bytenr;
  1055. extent_op->num_bytes = num_bytes;
  1056. extent_op->parent = parent;
  1057. extent_op->orig_parent = orig_parent;
  1058. extent_op->generation = ref_generation;
  1059. extent_op->orig_generation = orig_generation;
  1060. extent_op->level = (int)owner_objectid;
  1061. INIT_LIST_HEAD(&extent_op->list);
  1062. extent_op->del = 0;
  1063. set_extent_bits(&root->fs_info->extent_ins,
  1064. bytenr, bytenr + num_bytes - 1,
  1065. EXTENT_WRITEBACK, GFP_NOFS);
  1066. set_state_private(&root->fs_info->extent_ins,
  1067. bytenr, (unsigned long)extent_op);
  1068. }
  1069. mutex_unlock(&root->fs_info->extent_ins_mutex);
  1070. return 0;
  1071. }
  1072. path = btrfs_alloc_path();
  1073. if (!path)
  1074. return -ENOMEM;
  1075. ret = lookup_extent_backref(trans, extent_root, path,
  1076. bytenr, orig_parent, orig_root,
  1077. orig_generation, owner_objectid, 1);
  1078. if (ret)
  1079. goto out;
  1080. ret = remove_extent_backref(trans, extent_root, path);
  1081. if (ret)
  1082. goto out;
  1083. ret = insert_extent_backref(trans, extent_root, path, bytenr,
  1084. parent, ref_root, ref_generation,
  1085. owner_objectid);
  1086. BUG_ON(ret);
  1087. finish_current_insert(trans, extent_root, 0);
  1088. del_pending_extents(trans, extent_root, 0);
  1089. out:
  1090. btrfs_free_path(path);
  1091. return ret;
  1092. }
  1093. int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  1094. struct btrfs_root *root, u64 bytenr,
  1095. u64 orig_parent, u64 parent,
  1096. u64 ref_root, u64 ref_generation,
  1097. u64 owner_objectid)
  1098. {
  1099. int ret;
  1100. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  1101. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  1102. return 0;
  1103. ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
  1104. parent, ref_root, ref_root,
  1105. ref_generation, ref_generation,
  1106. owner_objectid);
  1107. return ret;
  1108. }
  1109. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1110. struct btrfs_root *root, u64 bytenr,
  1111. u64 orig_parent, u64 parent,
  1112. u64 orig_root, u64 ref_root,
  1113. u64 orig_generation, u64 ref_generation,
  1114. u64 owner_objectid)
  1115. {
  1116. struct btrfs_path *path;
  1117. int ret;
  1118. struct btrfs_key key;
  1119. struct extent_buffer *l;
  1120. struct btrfs_extent_item *item;
  1121. u32 refs;
  1122. path = btrfs_alloc_path();
  1123. if (!path)
  1124. return -ENOMEM;
  1125. path->reada = 1;
  1126. key.objectid = bytenr;
  1127. key.type = BTRFS_EXTENT_ITEM_KEY;
  1128. key.offset = (u64)-1;
  1129. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  1130. 0, 1);
  1131. if (ret < 0)
  1132. return ret;
  1133. BUG_ON(ret == 0 || path->slots[0] == 0);
  1134. path->slots[0]--;
  1135. l = path->nodes[0];
  1136. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1137. if (key.objectid != bytenr) {
  1138. btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
  1139. printk(KERN_ERR "btrfs wanted %llu found %llu\n",
  1140. (unsigned long long)bytenr,
  1141. (unsigned long long)key.objectid);
  1142. BUG();
  1143. }
  1144. BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
  1145. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  1146. refs = btrfs_extent_refs(l, item);
  1147. btrfs_set_extent_refs(l, item, refs + 1);
  1148. btrfs_mark_buffer_dirty(path->nodes[0]);
  1149. btrfs_release_path(root->fs_info->extent_root, path);
  1150. path->reada = 1;
  1151. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1152. path, bytenr, parent,
  1153. ref_root, ref_generation,
  1154. owner_objectid);
  1155. BUG_ON(ret);
  1156. finish_current_insert(trans, root->fs_info->extent_root, 0);
  1157. del_pending_extents(trans, root->fs_info->extent_root, 0);
  1158. btrfs_free_path(path);
  1159. return 0;
  1160. }
  1161. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1162. struct btrfs_root *root,
  1163. u64 bytenr, u64 num_bytes, u64 parent,
  1164. u64 ref_root, u64 ref_generation,
  1165. u64 owner_objectid)
  1166. {
  1167. int ret;
  1168. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  1169. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  1170. return 0;
  1171. ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
  1172. 0, ref_root, 0, ref_generation,
  1173. owner_objectid);
  1174. return ret;
  1175. }
  1176. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  1177. struct btrfs_root *root)
  1178. {
  1179. u64 start;
  1180. u64 end;
  1181. int ret;
  1182. while(1) {
  1183. finish_current_insert(trans, root->fs_info->extent_root, 1);
  1184. del_pending_extents(trans, root->fs_info->extent_root, 1);
  1185. /* is there more work to do? */
  1186. ret = find_first_extent_bit(&root->fs_info->pending_del,
  1187. 0, &start, &end, EXTENT_WRITEBACK);
  1188. if (!ret)
  1189. continue;
  1190. ret = find_first_extent_bit(&root->fs_info->extent_ins,
  1191. 0, &start, &end, EXTENT_WRITEBACK);
  1192. if (!ret)
  1193. continue;
  1194. break;
  1195. }
  1196. return 0;
  1197. }
  1198. int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
  1199. struct btrfs_root *root, u64 bytenr,
  1200. u64 num_bytes, u32 *refs)
  1201. {
  1202. struct btrfs_path *path;
  1203. int ret;
  1204. struct btrfs_key key;
  1205. struct extent_buffer *l;
  1206. struct btrfs_extent_item *item;
  1207. WARN_ON(num_bytes < root->sectorsize);
  1208. path = btrfs_alloc_path();
  1209. path->reada = 1;
  1210. key.objectid = bytenr;
  1211. key.offset = num_bytes;
  1212. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1213. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  1214. 0, 0);
  1215. if (ret < 0)
  1216. goto out;
  1217. if (ret != 0) {
  1218. btrfs_print_leaf(root, path->nodes[0]);
  1219. printk(KERN_INFO "btrfs failed to find block number %llu\n",
  1220. (unsigned long long)bytenr);
  1221. BUG();
  1222. }
  1223. l = path->nodes[0];
  1224. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  1225. *refs = btrfs_extent_refs(l, item);
  1226. out:
  1227. btrfs_free_path(path);
  1228. return 0;
  1229. }
  1230. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  1231. struct btrfs_root *root, u64 objectid, u64 bytenr)
  1232. {
  1233. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1234. struct btrfs_path *path;
  1235. struct extent_buffer *leaf;
  1236. struct btrfs_extent_ref *ref_item;
  1237. struct btrfs_key key;
  1238. struct btrfs_key found_key;
  1239. u64 ref_root;
  1240. u64 last_snapshot;
  1241. u32 nritems;
  1242. int ret;
  1243. key.objectid = bytenr;
  1244. key.offset = (u64)-1;
  1245. key.type = BTRFS_EXTENT_ITEM_KEY;
  1246. path = btrfs_alloc_path();
  1247. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  1248. if (ret < 0)
  1249. goto out;
  1250. BUG_ON(ret == 0);
  1251. ret = -ENOENT;
  1252. if (path->slots[0] == 0)
  1253. goto out;
  1254. path->slots[0]--;
  1255. leaf = path->nodes[0];
  1256. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1257. if (found_key.objectid != bytenr ||
  1258. found_key.type != BTRFS_EXTENT_ITEM_KEY)
  1259. goto out;
  1260. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1261. while (1) {
  1262. leaf = path->nodes[0];
  1263. nritems = btrfs_header_nritems(leaf);
  1264. if (path->slots[0] >= nritems) {
  1265. ret = btrfs_next_leaf(extent_root, path);
  1266. if (ret < 0)
  1267. goto out;
  1268. if (ret == 0)
  1269. continue;
  1270. break;
  1271. }
  1272. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1273. if (found_key.objectid != bytenr)
  1274. break;
  1275. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  1276. path->slots[0]++;
  1277. continue;
  1278. }
  1279. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  1280. struct btrfs_extent_ref);
  1281. ref_root = btrfs_ref_root(leaf, ref_item);
  1282. if ((ref_root != root->root_key.objectid &&
  1283. ref_root != BTRFS_TREE_LOG_OBJECTID) ||
  1284. objectid != btrfs_ref_objectid(leaf, ref_item)) {
  1285. ret = 1;
  1286. goto out;
  1287. }
  1288. if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
  1289. ret = 1;
  1290. goto out;
  1291. }
  1292. path->slots[0]++;
  1293. }
  1294. ret = 0;
  1295. out:
  1296. btrfs_free_path(path);
  1297. return ret;
  1298. }
  1299. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1300. struct extent_buffer *buf, u32 nr_extents)
  1301. {
  1302. struct btrfs_key key;
  1303. struct btrfs_file_extent_item *fi;
  1304. u64 root_gen;
  1305. u32 nritems;
  1306. int i;
  1307. int level;
  1308. int ret = 0;
  1309. int shared = 0;
  1310. if (!root->ref_cows)
  1311. return 0;
  1312. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  1313. shared = 0;
  1314. root_gen = root->root_key.offset;
  1315. } else {
  1316. shared = 1;
  1317. root_gen = trans->transid - 1;
  1318. }
  1319. level = btrfs_header_level(buf);
  1320. nritems = btrfs_header_nritems(buf);
  1321. if (level == 0) {
  1322. struct btrfs_leaf_ref *ref;
  1323. struct btrfs_extent_info *info;
  1324. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  1325. if (!ref) {
  1326. ret = -ENOMEM;
  1327. goto out;
  1328. }
  1329. ref->root_gen = root_gen;
  1330. ref->bytenr = buf->start;
  1331. ref->owner = btrfs_header_owner(buf);
  1332. ref->generation = btrfs_header_generation(buf);
  1333. ref->nritems = nr_extents;
  1334. info = ref->extents;
  1335. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  1336. u64 disk_bytenr;
  1337. btrfs_item_key_to_cpu(buf, &key, i);
  1338. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1339. continue;
  1340. fi = btrfs_item_ptr(buf, i,
  1341. struct btrfs_file_extent_item);
  1342. if (btrfs_file_extent_type(buf, fi) ==
  1343. BTRFS_FILE_EXTENT_INLINE)
  1344. continue;
  1345. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1346. if (disk_bytenr == 0)
  1347. continue;
  1348. info->bytenr = disk_bytenr;
  1349. info->num_bytes =
  1350. btrfs_file_extent_disk_num_bytes(buf, fi);
  1351. info->objectid = key.objectid;
  1352. info->offset = key.offset;
  1353. info++;
  1354. }
  1355. ret = btrfs_add_leaf_ref(root, ref, shared);
  1356. if (ret == -EEXIST && shared) {
  1357. struct btrfs_leaf_ref *old;
  1358. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  1359. BUG_ON(!old);
  1360. btrfs_remove_leaf_ref(root, old);
  1361. btrfs_free_leaf_ref(root, old);
  1362. ret = btrfs_add_leaf_ref(root, ref, shared);
  1363. }
  1364. WARN_ON(ret);
  1365. btrfs_free_leaf_ref(root, ref);
  1366. }
  1367. out:
  1368. return ret;
  1369. }
  1370. /* when a block goes through cow, we update the reference counts of
  1371. * everything that block points to. The internal pointers of the block
  1372. * can be in just about any order, and it is likely to have clusters of
  1373. * things that are close together and clusters of things that are not.
  1374. *
  1375. * To help reduce the seeks that come with updating all of these reference
  1376. * counts, sort them by byte number before actual updates are done.
  1377. *
  1378. * struct refsort is used to match byte number to slot in the btree block.
  1379. * we sort based on the byte number and then use the slot to actually
  1380. * find the item.
  1381. *
  1382. * struct refsort is smaller than strcut btrfs_item and smaller than
  1383. * struct btrfs_key_ptr. Since we're currently limited to the page size
  1384. * for a btree block, there's no way for a kmalloc of refsorts for a
  1385. * single node to be bigger than a page.
  1386. */
  1387. struct refsort {
  1388. u64 bytenr;
  1389. u32 slot;
  1390. };
  1391. /*
  1392. * for passing into sort()
  1393. */
  1394. static int refsort_cmp(const void *a_void, const void *b_void)
  1395. {
  1396. const struct refsort *a = a_void;
  1397. const struct refsort *b = b_void;
  1398. if (a->bytenr < b->bytenr)
  1399. return -1;
  1400. if (a->bytenr > b->bytenr)
  1401. return 1;
  1402. return 0;
  1403. }
  1404. noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
  1405. struct btrfs_root *root,
  1406. struct extent_buffer *orig_buf,
  1407. struct extent_buffer *buf, u32 *nr_extents)
  1408. {
  1409. u64 bytenr;
  1410. u64 ref_root;
  1411. u64 orig_root;
  1412. u64 ref_generation;
  1413. u64 orig_generation;
  1414. struct refsort *sorted;
  1415. u32 nritems;
  1416. u32 nr_file_extents = 0;
  1417. struct btrfs_key key;
  1418. struct btrfs_file_extent_item *fi;
  1419. int i;
  1420. int level;
  1421. int ret = 0;
  1422. int faili = 0;
  1423. int refi = 0;
  1424. int slot;
  1425. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  1426. u64, u64, u64, u64, u64, u64, u64, u64);
  1427. ref_root = btrfs_header_owner(buf);
  1428. ref_generation = btrfs_header_generation(buf);
  1429. orig_root = btrfs_header_owner(orig_buf);
  1430. orig_generation = btrfs_header_generation(orig_buf);
  1431. nritems = btrfs_header_nritems(buf);
  1432. level = btrfs_header_level(buf);
  1433. sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
  1434. BUG_ON(!sorted);
  1435. if (root->ref_cows) {
  1436. process_func = __btrfs_inc_extent_ref;
  1437. } else {
  1438. if (level == 0 &&
  1439. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1440. goto out;
  1441. if (level != 0 &&
  1442. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1443. goto out;
  1444. process_func = __btrfs_update_extent_ref;
  1445. }
  1446. /*
  1447. * we make two passes through the items. In the first pass we
  1448. * only record the byte number and slot. Then we sort based on
  1449. * byte number and do the actual work based on the sorted results
  1450. */
  1451. for (i = 0; i < nritems; i++) {
  1452. cond_resched();
  1453. if (level == 0) {
  1454. btrfs_item_key_to_cpu(buf, &key, i);
  1455. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1456. continue;
  1457. fi = btrfs_item_ptr(buf, i,
  1458. struct btrfs_file_extent_item);
  1459. if (btrfs_file_extent_type(buf, fi) ==
  1460. BTRFS_FILE_EXTENT_INLINE)
  1461. continue;
  1462. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1463. if (bytenr == 0)
  1464. continue;
  1465. nr_file_extents++;
  1466. sorted[refi].bytenr = bytenr;
  1467. sorted[refi].slot = i;
  1468. refi++;
  1469. } else {
  1470. bytenr = btrfs_node_blockptr(buf, i);
  1471. sorted[refi].bytenr = bytenr;
  1472. sorted[refi].slot = i;
  1473. refi++;
  1474. }
  1475. }
  1476. /*
  1477. * if refi == 0, we didn't actually put anything into the sorted
  1478. * array and we're done
  1479. */
  1480. if (refi == 0)
  1481. goto out;
  1482. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  1483. for (i = 0; i < refi; i++) {
  1484. cond_resched();
  1485. slot = sorted[i].slot;
  1486. bytenr = sorted[i].bytenr;
  1487. if (level == 0) {
  1488. btrfs_item_key_to_cpu(buf, &key, slot);
  1489. ret = process_func(trans, root, bytenr,
  1490. orig_buf->start, buf->start,
  1491. orig_root, ref_root,
  1492. orig_generation, ref_generation,
  1493. key.objectid);
  1494. if (ret) {
  1495. faili = slot;
  1496. WARN_ON(1);
  1497. goto fail;
  1498. }
  1499. } else {
  1500. ret = process_func(trans, root, bytenr,
  1501. orig_buf->start, buf->start,
  1502. orig_root, ref_root,
  1503. orig_generation, ref_generation,
  1504. level - 1);
  1505. if (ret) {
  1506. faili = slot;
  1507. WARN_ON(1);
  1508. goto fail;
  1509. }
  1510. }
  1511. }
  1512. out:
  1513. kfree(sorted);
  1514. if (nr_extents) {
  1515. if (level == 0)
  1516. *nr_extents = nr_file_extents;
  1517. else
  1518. *nr_extents = nritems;
  1519. }
  1520. return 0;
  1521. fail:
  1522. kfree(sorted);
  1523. WARN_ON(1);
  1524. return ret;
  1525. }
  1526. int btrfs_update_ref(struct btrfs_trans_handle *trans,
  1527. struct btrfs_root *root, struct extent_buffer *orig_buf,
  1528. struct extent_buffer *buf, int start_slot, int nr)
  1529. {
  1530. u64 bytenr;
  1531. u64 ref_root;
  1532. u64 orig_root;
  1533. u64 ref_generation;
  1534. u64 orig_generation;
  1535. struct btrfs_key key;
  1536. struct btrfs_file_extent_item *fi;
  1537. int i;
  1538. int ret;
  1539. int slot;
  1540. int level;
  1541. BUG_ON(start_slot < 0);
  1542. BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
  1543. ref_root = btrfs_header_owner(buf);
  1544. ref_generation = btrfs_header_generation(buf);
  1545. orig_root = btrfs_header_owner(orig_buf);
  1546. orig_generation = btrfs_header_generation(orig_buf);
  1547. level = btrfs_header_level(buf);
  1548. if (!root->ref_cows) {
  1549. if (level == 0 &&
  1550. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1551. return 0;
  1552. if (level != 0 &&
  1553. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1554. return 0;
  1555. }
  1556. for (i = 0, slot = start_slot; i < nr; i++, slot++) {
  1557. cond_resched();
  1558. if (level == 0) {
  1559. btrfs_item_key_to_cpu(buf, &key, slot);
  1560. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1561. continue;
  1562. fi = btrfs_item_ptr(buf, slot,
  1563. struct btrfs_file_extent_item);
  1564. if (btrfs_file_extent_type(buf, fi) ==
  1565. BTRFS_FILE_EXTENT_INLINE)
  1566. continue;
  1567. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1568. if (bytenr == 0)
  1569. continue;
  1570. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1571. orig_buf->start, buf->start,
  1572. orig_root, ref_root,
  1573. orig_generation, ref_generation,
  1574. key.objectid);
  1575. if (ret)
  1576. goto fail;
  1577. } else {
  1578. bytenr = btrfs_node_blockptr(buf, slot);
  1579. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1580. orig_buf->start, buf->start,
  1581. orig_root, ref_root,
  1582. orig_generation, ref_generation,
  1583. level - 1);
  1584. if (ret)
  1585. goto fail;
  1586. }
  1587. }
  1588. return 0;
  1589. fail:
  1590. WARN_ON(1);
  1591. return -1;
  1592. }
  1593. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1594. struct btrfs_root *root,
  1595. struct btrfs_path *path,
  1596. struct btrfs_block_group_cache *cache)
  1597. {
  1598. int ret;
  1599. int pending_ret;
  1600. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1601. unsigned long bi;
  1602. struct extent_buffer *leaf;
  1603. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1604. if (ret < 0)
  1605. goto fail;
  1606. BUG_ON(ret);
  1607. leaf = path->nodes[0];
  1608. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1609. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1610. btrfs_mark_buffer_dirty(leaf);
  1611. btrfs_release_path(extent_root, path);
  1612. fail:
  1613. finish_current_insert(trans, extent_root, 0);
  1614. pending_ret = del_pending_extents(trans, extent_root, 0);
  1615. if (ret)
  1616. return ret;
  1617. if (pending_ret)
  1618. return pending_ret;
  1619. return 0;
  1620. }
  1621. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1622. struct btrfs_root *root)
  1623. {
  1624. struct btrfs_block_group_cache *cache, *entry;
  1625. struct rb_node *n;
  1626. int err = 0;
  1627. int werr = 0;
  1628. struct btrfs_path *path;
  1629. u64 last = 0;
  1630. path = btrfs_alloc_path();
  1631. if (!path)
  1632. return -ENOMEM;
  1633. while (1) {
  1634. cache = NULL;
  1635. spin_lock(&root->fs_info->block_group_cache_lock);
  1636. for (n = rb_first(&root->fs_info->block_group_cache_tree);
  1637. n; n = rb_next(n)) {
  1638. entry = rb_entry(n, struct btrfs_block_group_cache,
  1639. cache_node);
  1640. if (entry->dirty) {
  1641. cache = entry;
  1642. break;
  1643. }
  1644. }
  1645. spin_unlock(&root->fs_info->block_group_cache_lock);
  1646. if (!cache)
  1647. break;
  1648. cache->dirty = 0;
  1649. last += cache->key.offset;
  1650. err = write_one_cache_group(trans, root,
  1651. path, cache);
  1652. /*
  1653. * if we fail to write the cache group, we want
  1654. * to keep it marked dirty in hopes that a later
  1655. * write will work
  1656. */
  1657. if (err) {
  1658. werr = err;
  1659. continue;
  1660. }
  1661. }
  1662. btrfs_free_path(path);
  1663. return werr;
  1664. }
  1665. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  1666. {
  1667. struct btrfs_block_group_cache *block_group;
  1668. int readonly = 0;
  1669. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  1670. if (!block_group || block_group->ro)
  1671. readonly = 1;
  1672. if (block_group)
  1673. put_block_group(block_group);
  1674. return readonly;
  1675. }
  1676. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1677. u64 total_bytes, u64 bytes_used,
  1678. struct btrfs_space_info **space_info)
  1679. {
  1680. struct btrfs_space_info *found;
  1681. found = __find_space_info(info, flags);
  1682. if (found) {
  1683. spin_lock(&found->lock);
  1684. found->total_bytes += total_bytes;
  1685. found->bytes_used += bytes_used;
  1686. found->full = 0;
  1687. spin_unlock(&found->lock);
  1688. *space_info = found;
  1689. return 0;
  1690. }
  1691. found = kzalloc(sizeof(*found), GFP_NOFS);
  1692. if (!found)
  1693. return -ENOMEM;
  1694. list_add(&found->list, &info->space_info);
  1695. INIT_LIST_HEAD(&found->block_groups);
  1696. init_rwsem(&found->groups_sem);
  1697. spin_lock_init(&found->lock);
  1698. found->flags = flags;
  1699. found->total_bytes = total_bytes;
  1700. found->bytes_used = bytes_used;
  1701. found->bytes_pinned = 0;
  1702. found->bytes_reserved = 0;
  1703. found->bytes_readonly = 0;
  1704. found->full = 0;
  1705. found->force_alloc = 0;
  1706. *space_info = found;
  1707. return 0;
  1708. }
  1709. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1710. {
  1711. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1712. BTRFS_BLOCK_GROUP_RAID1 |
  1713. BTRFS_BLOCK_GROUP_RAID10 |
  1714. BTRFS_BLOCK_GROUP_DUP);
  1715. if (extra_flags) {
  1716. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1717. fs_info->avail_data_alloc_bits |= extra_flags;
  1718. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1719. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1720. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1721. fs_info->avail_system_alloc_bits |= extra_flags;
  1722. }
  1723. }
  1724. static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
  1725. {
  1726. spin_lock(&cache->space_info->lock);
  1727. spin_lock(&cache->lock);
  1728. if (!cache->ro) {
  1729. cache->space_info->bytes_readonly += cache->key.offset -
  1730. btrfs_block_group_used(&cache->item);
  1731. cache->ro = 1;
  1732. }
  1733. spin_unlock(&cache->lock);
  1734. spin_unlock(&cache->space_info->lock);
  1735. }
  1736. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1737. {
  1738. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  1739. if (num_devices == 1)
  1740. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1741. if (num_devices < 4)
  1742. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1743. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1744. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1745. BTRFS_BLOCK_GROUP_RAID10))) {
  1746. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1747. }
  1748. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1749. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1750. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1751. }
  1752. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1753. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1754. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1755. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1756. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1757. return flags;
  1758. }
  1759. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1760. struct btrfs_root *extent_root, u64 alloc_bytes,
  1761. u64 flags, int force)
  1762. {
  1763. struct btrfs_space_info *space_info;
  1764. u64 thresh;
  1765. int ret = 0;
  1766. mutex_lock(&extent_root->fs_info->chunk_mutex);
  1767. flags = btrfs_reduce_alloc_profile(extent_root, flags);
  1768. space_info = __find_space_info(extent_root->fs_info, flags);
  1769. if (!space_info) {
  1770. ret = update_space_info(extent_root->fs_info, flags,
  1771. 0, 0, &space_info);
  1772. BUG_ON(ret);
  1773. }
  1774. BUG_ON(!space_info);
  1775. spin_lock(&space_info->lock);
  1776. if (space_info->force_alloc) {
  1777. force = 1;
  1778. space_info->force_alloc = 0;
  1779. }
  1780. if (space_info->full) {
  1781. spin_unlock(&space_info->lock);
  1782. goto out;
  1783. }
  1784. thresh = space_info->total_bytes - space_info->bytes_readonly;
  1785. thresh = div_factor(thresh, 6);
  1786. if (!force &&
  1787. (space_info->bytes_used + space_info->bytes_pinned +
  1788. space_info->bytes_reserved + alloc_bytes) < thresh) {
  1789. spin_unlock(&space_info->lock);
  1790. goto out;
  1791. }
  1792. spin_unlock(&space_info->lock);
  1793. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  1794. if (ret)
  1795. space_info->full = 1;
  1796. out:
  1797. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1798. return ret;
  1799. }
  1800. static int update_block_group(struct btrfs_trans_handle *trans,
  1801. struct btrfs_root *root,
  1802. u64 bytenr, u64 num_bytes, int alloc,
  1803. int mark_free)
  1804. {
  1805. struct btrfs_block_group_cache *cache;
  1806. struct btrfs_fs_info *info = root->fs_info;
  1807. u64 total = num_bytes;
  1808. u64 old_val;
  1809. u64 byte_in_group;
  1810. while (total) {
  1811. cache = btrfs_lookup_block_group(info, bytenr);
  1812. if (!cache)
  1813. return -1;
  1814. byte_in_group = bytenr - cache->key.objectid;
  1815. WARN_ON(byte_in_group > cache->key.offset);
  1816. spin_lock(&cache->space_info->lock);
  1817. spin_lock(&cache->lock);
  1818. cache->dirty = 1;
  1819. old_val = btrfs_block_group_used(&cache->item);
  1820. num_bytes = min(total, cache->key.offset - byte_in_group);
  1821. if (alloc) {
  1822. old_val += num_bytes;
  1823. cache->space_info->bytes_used += num_bytes;
  1824. if (cache->ro)
  1825. cache->space_info->bytes_readonly -= num_bytes;
  1826. btrfs_set_block_group_used(&cache->item, old_val);
  1827. spin_unlock(&cache->lock);
  1828. spin_unlock(&cache->space_info->lock);
  1829. } else {
  1830. old_val -= num_bytes;
  1831. cache->space_info->bytes_used -= num_bytes;
  1832. if (cache->ro)
  1833. cache->space_info->bytes_readonly += num_bytes;
  1834. btrfs_set_block_group_used(&cache->item, old_val);
  1835. spin_unlock(&cache->lock);
  1836. spin_unlock(&cache->space_info->lock);
  1837. if (mark_free) {
  1838. int ret;
  1839. ret = btrfs_discard_extent(root, bytenr,
  1840. num_bytes);
  1841. WARN_ON(ret);
  1842. ret = btrfs_add_free_space(cache, bytenr,
  1843. num_bytes);
  1844. WARN_ON(ret);
  1845. }
  1846. }
  1847. put_block_group(cache);
  1848. total -= num_bytes;
  1849. bytenr += num_bytes;
  1850. }
  1851. return 0;
  1852. }
  1853. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1854. {
  1855. struct btrfs_block_group_cache *cache;
  1856. u64 bytenr;
  1857. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  1858. if (!cache)
  1859. return 0;
  1860. bytenr = cache->key.objectid;
  1861. put_block_group(cache);
  1862. return bytenr;
  1863. }
  1864. int btrfs_update_pinned_extents(struct btrfs_root *root,
  1865. u64 bytenr, u64 num, int pin)
  1866. {
  1867. u64 len;
  1868. struct btrfs_block_group_cache *cache;
  1869. struct btrfs_fs_info *fs_info = root->fs_info;
  1870. WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
  1871. if (pin) {
  1872. set_extent_dirty(&fs_info->pinned_extents,
  1873. bytenr, bytenr + num - 1, GFP_NOFS);
  1874. } else {
  1875. clear_extent_dirty(&fs_info->pinned_extents,
  1876. bytenr, bytenr + num - 1, GFP_NOFS);
  1877. }
  1878. while (num > 0) {
  1879. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1880. BUG_ON(!cache);
  1881. len = min(num, cache->key.offset -
  1882. (bytenr - cache->key.objectid));
  1883. if (pin) {
  1884. spin_lock(&cache->space_info->lock);
  1885. spin_lock(&cache->lock);
  1886. cache->pinned += len;
  1887. cache->space_info->bytes_pinned += len;
  1888. spin_unlock(&cache->lock);
  1889. spin_unlock(&cache->space_info->lock);
  1890. fs_info->total_pinned += len;
  1891. } else {
  1892. spin_lock(&cache->space_info->lock);
  1893. spin_lock(&cache->lock);
  1894. cache->pinned -= len;
  1895. cache->space_info->bytes_pinned -= len;
  1896. spin_unlock(&cache->lock);
  1897. spin_unlock(&cache->space_info->lock);
  1898. fs_info->total_pinned -= len;
  1899. if (cache->cached)
  1900. btrfs_add_free_space(cache, bytenr, len);
  1901. }
  1902. put_block_group(cache);
  1903. bytenr += len;
  1904. num -= len;
  1905. }
  1906. return 0;
  1907. }
  1908. static int update_reserved_extents(struct btrfs_root *root,
  1909. u64 bytenr, u64 num, int reserve)
  1910. {
  1911. u64 len;
  1912. struct btrfs_block_group_cache *cache;
  1913. struct btrfs_fs_info *fs_info = root->fs_info;
  1914. while (num > 0) {
  1915. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1916. BUG_ON(!cache);
  1917. len = min(num, cache->key.offset -
  1918. (bytenr - cache->key.objectid));
  1919. spin_lock(&cache->space_info->lock);
  1920. spin_lock(&cache->lock);
  1921. if (reserve) {
  1922. cache->reserved += len;
  1923. cache->space_info->bytes_reserved += len;
  1924. } else {
  1925. cache->reserved -= len;
  1926. cache->space_info->bytes_reserved -= len;
  1927. }
  1928. spin_unlock(&cache->lock);
  1929. spin_unlock(&cache->space_info->lock);
  1930. put_block_group(cache);
  1931. bytenr += len;
  1932. num -= len;
  1933. }
  1934. return 0;
  1935. }
  1936. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1937. {
  1938. u64 last = 0;
  1939. u64 start;
  1940. u64 end;
  1941. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1942. int ret;
  1943. mutex_lock(&root->fs_info->pinned_mutex);
  1944. while (1) {
  1945. ret = find_first_extent_bit(pinned_extents, last,
  1946. &start, &end, EXTENT_DIRTY);
  1947. if (ret)
  1948. break;
  1949. set_extent_dirty(copy, start, end, GFP_NOFS);
  1950. last = end + 1;
  1951. }
  1952. mutex_unlock(&root->fs_info->pinned_mutex);
  1953. return 0;
  1954. }
  1955. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1956. struct btrfs_root *root,
  1957. struct extent_io_tree *unpin)
  1958. {
  1959. u64 start;
  1960. u64 end;
  1961. int ret;
  1962. mutex_lock(&root->fs_info->pinned_mutex);
  1963. while (1) {
  1964. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1965. EXTENT_DIRTY);
  1966. if (ret)
  1967. break;
  1968. ret = btrfs_discard_extent(root, start, end + 1 - start);
  1969. btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
  1970. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1971. if (need_resched()) {
  1972. mutex_unlock(&root->fs_info->pinned_mutex);
  1973. cond_resched();
  1974. mutex_lock(&root->fs_info->pinned_mutex);
  1975. }
  1976. }
  1977. mutex_unlock(&root->fs_info->pinned_mutex);
  1978. return ret;
  1979. }
  1980. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1981. struct btrfs_root *extent_root, int all)
  1982. {
  1983. u64 start;
  1984. u64 end;
  1985. u64 priv;
  1986. u64 search = 0;
  1987. struct btrfs_fs_info *info = extent_root->fs_info;
  1988. struct btrfs_path *path;
  1989. struct pending_extent_op *extent_op, *tmp;
  1990. struct list_head insert_list, update_list;
  1991. int ret;
  1992. int num_inserts = 0, max_inserts, restart = 0;
  1993. path = btrfs_alloc_path();
  1994. INIT_LIST_HEAD(&insert_list);
  1995. INIT_LIST_HEAD(&update_list);
  1996. max_inserts = extent_root->leafsize /
  1997. (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
  1998. sizeof(struct btrfs_extent_ref) +
  1999. sizeof(struct btrfs_extent_item));
  2000. again:
  2001. mutex_lock(&info->extent_ins_mutex);
  2002. while (1) {
  2003. ret = find_first_extent_bit(&info->extent_ins, search, &start,
  2004. &end, EXTENT_WRITEBACK);
  2005. if (ret) {
  2006. if (restart && !num_inserts &&
  2007. list_empty(&update_list)) {
  2008. restart = 0;
  2009. search = 0;
  2010. continue;
  2011. }
  2012. break;
  2013. }
  2014. ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
  2015. if (!ret) {
  2016. if (all)
  2017. restart = 1;
  2018. search = end + 1;
  2019. if (need_resched()) {
  2020. mutex_unlock(&info->extent_ins_mutex);
  2021. cond_resched();
  2022. mutex_lock(&info->extent_ins_mutex);
  2023. }
  2024. continue;
  2025. }
  2026. ret = get_state_private(&info->extent_ins, start, &priv);
  2027. BUG_ON(ret);
  2028. extent_op = (struct pending_extent_op *)(unsigned long) priv;
  2029. if (extent_op->type == PENDING_EXTENT_INSERT) {
  2030. num_inserts++;
  2031. list_add_tail(&extent_op->list, &insert_list);
  2032. search = end + 1;
  2033. if (num_inserts == max_inserts) {
  2034. restart = 1;
  2035. break;
  2036. }
  2037. } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
  2038. list_add_tail(&extent_op->list, &update_list);
  2039. search = end + 1;
  2040. } else {
  2041. BUG();
  2042. }
  2043. }
  2044. /*
  2045. * process the update list, clear the writeback bit for it, and if
  2046. * somebody marked this thing for deletion then just unlock it and be
  2047. * done, the free_extents will handle it
  2048. */
  2049. list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
  2050. clear_extent_bits(&info->extent_ins, extent_op->bytenr,
  2051. extent_op->bytenr + extent_op->num_bytes - 1,
  2052. EXTENT_WRITEBACK, GFP_NOFS);
  2053. if (extent_op->del) {
  2054. list_del_init(&extent_op->list);
  2055. unlock_extent(&info->extent_ins, extent_op->bytenr,
  2056. extent_op->bytenr + extent_op->num_bytes
  2057. - 1, GFP_NOFS);
  2058. kfree(extent_op);
  2059. }
  2060. }
  2061. mutex_unlock(&info->extent_ins_mutex);
  2062. /*
  2063. * still have things left on the update list, go ahead an update
  2064. * everything
  2065. */
  2066. if (!list_empty(&update_list)) {
  2067. ret = update_backrefs(trans, extent_root, path, &update_list);
  2068. BUG_ON(ret);
  2069. /* we may have COW'ed new blocks, so lets start over */
  2070. if (all)
  2071. restart = 1;
  2072. }
  2073. /*
  2074. * if no inserts need to be done, but we skipped some extents and we
  2075. * need to make sure everything is cleaned then reset everything and
  2076. * go back to the beginning
  2077. */
  2078. if (!num_inserts && restart) {
  2079. search = 0;
  2080. restart = 0;
  2081. INIT_LIST_HEAD(&update_list);
  2082. INIT_LIST_HEAD(&insert_list);
  2083. goto again;
  2084. } else if (!num_inserts) {
  2085. goto out;
  2086. }
  2087. /*
  2088. * process the insert extents list. Again if we are deleting this
  2089. * extent, then just unlock it, pin down the bytes if need be, and be
  2090. * done with it. Saves us from having to actually insert the extent
  2091. * into the tree and then subsequently come along and delete it
  2092. */
  2093. mutex_lock(&info->extent_ins_mutex);
  2094. list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
  2095. clear_extent_bits(&info->extent_ins, extent_op->bytenr,
  2096. extent_op->bytenr + extent_op->num_bytes - 1,
  2097. EXTENT_WRITEBACK, GFP_NOFS);
  2098. if (extent_op->del) {
  2099. u64 used;
  2100. list_del_init(&extent_op->list);
  2101. unlock_extent(&info->extent_ins, extent_op->bytenr,
  2102. extent_op->bytenr + extent_op->num_bytes
  2103. - 1, GFP_NOFS);
  2104. mutex_lock(&extent_root->fs_info->pinned_mutex);
  2105. ret = pin_down_bytes(trans, extent_root,
  2106. extent_op->bytenr,
  2107. extent_op->num_bytes, 0);
  2108. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  2109. spin_lock(&info->delalloc_lock);
  2110. used = btrfs_super_bytes_used(&info->super_copy);
  2111. btrfs_set_super_bytes_used(&info->super_copy,
  2112. used - extent_op->num_bytes);
  2113. used = btrfs_root_used(&extent_root->root_item);
  2114. btrfs_set_root_used(&extent_root->root_item,
  2115. used - extent_op->num_bytes);
  2116. spin_unlock(&info->delalloc_lock);
  2117. ret = update_block_group(trans, extent_root,
  2118. extent_op->bytenr,
  2119. extent_op->num_bytes,
  2120. 0, ret > 0);
  2121. BUG_ON(ret);
  2122. kfree(extent_op);
  2123. num_inserts--;
  2124. }
  2125. }
  2126. mutex_unlock(&info->extent_ins_mutex);
  2127. ret = insert_extents(trans, extent_root, path, &insert_list,
  2128. num_inserts);
  2129. BUG_ON(ret);
  2130. /*
  2131. * if restart is set for whatever reason we need to go back and start
  2132. * searching through the pending list again.
  2133. *
  2134. * We just inserted some extents, which could have resulted in new
  2135. * blocks being allocated, which would result in new blocks needing
  2136. * updates, so if all is set we _must_ restart to get the updated
  2137. * blocks.
  2138. */
  2139. if (restart || all) {
  2140. INIT_LIST_HEAD(&insert_list);
  2141. INIT_LIST_HEAD(&update_list);
  2142. search = 0;
  2143. restart = 0;
  2144. num_inserts = 0;
  2145. goto again;
  2146. }
  2147. out:
  2148. btrfs_free_path(path);
  2149. return 0;
  2150. }
  2151. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  2152. struct btrfs_root *root,
  2153. u64 bytenr, u64 num_bytes, int is_data)
  2154. {
  2155. int err = 0;
  2156. struct extent_buffer *buf;
  2157. if (is_data)
  2158. goto pinit;
  2159. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  2160. if (!buf)
  2161. goto pinit;
  2162. /* we can reuse a block if it hasn't been written
  2163. * and it is from this transaction. We can't
  2164. * reuse anything from the tree log root because
  2165. * it has tiny sub-transactions.
  2166. */
  2167. if (btrfs_buffer_uptodate(buf, 0) &&
  2168. btrfs_try_tree_lock(buf)) {
  2169. u64 header_owner = btrfs_header_owner(buf);
  2170. u64 header_transid = btrfs_header_generation(buf);
  2171. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  2172. header_owner != BTRFS_TREE_RELOC_OBJECTID &&
  2173. header_transid == trans->transid &&
  2174. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  2175. clean_tree_block(NULL, root, buf);
  2176. btrfs_tree_unlock(buf);
  2177. free_extent_buffer(buf);
  2178. return 1;
  2179. }
  2180. btrfs_tree_unlock(buf);
  2181. }
  2182. free_extent_buffer(buf);
  2183. pinit:
  2184. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  2185. BUG_ON(err < 0);
  2186. return 0;
  2187. }
  2188. /*
  2189. * remove an extent from the root, returns 0 on success
  2190. */
  2191. static int __free_extent(struct btrfs_trans_handle *trans,
  2192. struct btrfs_root *root,
  2193. u64 bytenr, u64 num_bytes, u64 parent,
  2194. u64 root_objectid, u64 ref_generation,
  2195. u64 owner_objectid, int pin, int mark_free)
  2196. {
  2197. struct btrfs_path *path;
  2198. struct btrfs_key key;
  2199. struct btrfs_fs_info *info = root->fs_info;
  2200. struct btrfs_root *extent_root = info->extent_root;
  2201. struct extent_buffer *leaf;
  2202. int ret;
  2203. int extent_slot = 0;
  2204. int found_extent = 0;
  2205. int num_to_del = 1;
  2206. struct btrfs_extent_item *ei;
  2207. u32 refs;
  2208. key.objectid = bytenr;
  2209. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  2210. key.offset = num_bytes;
  2211. path = btrfs_alloc_path();
  2212. if (!path)
  2213. return -ENOMEM;
  2214. path->reada = 1;
  2215. ret = lookup_extent_backref(trans, extent_root, path,
  2216. bytenr, parent, root_objectid,
  2217. ref_generation, owner_objectid, 1);
  2218. if (ret == 0) {
  2219. struct btrfs_key found_key;
  2220. extent_slot = path->slots[0];
  2221. while (extent_slot > 0) {
  2222. extent_slot--;
  2223. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2224. extent_slot);
  2225. if (found_key.objectid != bytenr)
  2226. break;
  2227. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  2228. found_key.offset == num_bytes) {
  2229. found_extent = 1;
  2230. break;
  2231. }
  2232. if (path->slots[0] - extent_slot > 5)
  2233. break;
  2234. }
  2235. if (!found_extent) {
  2236. ret = remove_extent_backref(trans, extent_root, path);
  2237. BUG_ON(ret);
  2238. btrfs_release_path(extent_root, path);
  2239. ret = btrfs_search_slot(trans, extent_root,
  2240. &key, path, -1, 1);
  2241. if (ret) {
  2242. printk(KERN_ERR "umm, got %d back from search"
  2243. ", was looking for %llu\n", ret,
  2244. (unsigned long long)bytenr);
  2245. btrfs_print_leaf(extent_root, path->nodes[0]);
  2246. }
  2247. BUG_ON(ret);
  2248. extent_slot = path->slots[0];
  2249. }
  2250. } else {
  2251. btrfs_print_leaf(extent_root, path->nodes[0]);
  2252. WARN_ON(1);
  2253. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  2254. "root %llu gen %llu owner %llu\n",
  2255. (unsigned long long)bytenr,
  2256. (unsigned long long)root_objectid,
  2257. (unsigned long long)ref_generation,
  2258. (unsigned long long)owner_objectid);
  2259. }
  2260. leaf = path->nodes[0];
  2261. ei = btrfs_item_ptr(leaf, extent_slot,
  2262. struct btrfs_extent_item);
  2263. refs = btrfs_extent_refs(leaf, ei);
  2264. BUG_ON(refs == 0);
  2265. refs -= 1;
  2266. btrfs_set_extent_refs(leaf, ei, refs);
  2267. btrfs_mark_buffer_dirty(leaf);
  2268. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  2269. struct btrfs_extent_ref *ref;
  2270. ref = btrfs_item_ptr(leaf, path->slots[0],
  2271. struct btrfs_extent_ref);
  2272. BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
  2273. /* if the back ref and the extent are next to each other
  2274. * they get deleted below in one shot
  2275. */
  2276. path->slots[0] = extent_slot;
  2277. num_to_del = 2;
  2278. } else if (found_extent) {
  2279. /* otherwise delete the extent back ref */
  2280. ret = remove_extent_backref(trans, extent_root, path);
  2281. BUG_ON(ret);
  2282. /* if refs are 0, we need to setup the path for deletion */
  2283. if (refs == 0) {
  2284. btrfs_release_path(extent_root, path);
  2285. ret = btrfs_search_slot(trans, extent_root, &key, path,
  2286. -1, 1);
  2287. BUG_ON(ret);
  2288. }
  2289. }
  2290. if (refs == 0) {
  2291. u64 super_used;
  2292. u64 root_used;
  2293. if (pin) {
  2294. mutex_lock(&root->fs_info->pinned_mutex);
  2295. ret = pin_down_bytes(trans, root, bytenr, num_bytes,
  2296. owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
  2297. mutex_unlock(&root->fs_info->pinned_mutex);
  2298. if (ret > 0)
  2299. mark_free = 1;
  2300. BUG_ON(ret < 0);
  2301. }
  2302. /* block accounting for super block */
  2303. spin_lock(&info->delalloc_lock);
  2304. super_used = btrfs_super_bytes_used(&info->super_copy);
  2305. btrfs_set_super_bytes_used(&info->super_copy,
  2306. super_used - num_bytes);
  2307. /* block accounting for root item */
  2308. root_used = btrfs_root_used(&root->root_item);
  2309. btrfs_set_root_used(&root->root_item,
  2310. root_used - num_bytes);
  2311. spin_unlock(&info->delalloc_lock);
  2312. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  2313. num_to_del);
  2314. BUG_ON(ret);
  2315. btrfs_release_path(extent_root, path);
  2316. if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  2317. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  2318. BUG_ON(ret);
  2319. }
  2320. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  2321. mark_free);
  2322. BUG_ON(ret);
  2323. }
  2324. btrfs_free_path(path);
  2325. finish_current_insert(trans, extent_root, 0);
  2326. return ret;
  2327. }
  2328. /*
  2329. * find all the blocks marked as pending in the radix tree and remove
  2330. * them from the extent map
  2331. */
  2332. static int del_pending_extents(struct btrfs_trans_handle *trans,
  2333. struct btrfs_root *extent_root, int all)
  2334. {
  2335. int ret;
  2336. int err = 0;
  2337. u64 start;
  2338. u64 end;
  2339. u64 priv;
  2340. u64 search = 0;
  2341. int nr = 0, skipped = 0;
  2342. struct extent_io_tree *pending_del;
  2343. struct extent_io_tree *extent_ins;
  2344. struct pending_extent_op *extent_op;
  2345. struct btrfs_fs_info *info = extent_root->fs_info;
  2346. struct list_head delete_list;
  2347. INIT_LIST_HEAD(&delete_list);
  2348. extent_ins = &extent_root->fs_info->extent_ins;
  2349. pending_del = &extent_root->fs_info->pending_del;
  2350. again:
  2351. mutex_lock(&info->extent_ins_mutex);
  2352. while (1) {
  2353. ret = find_first_extent_bit(pending_del, search, &start, &end,
  2354. EXTENT_WRITEBACK);
  2355. if (ret) {
  2356. if (all && skipped && !nr) {
  2357. search = 0;
  2358. skipped = 0;
  2359. continue;
  2360. }
  2361. mutex_unlock(&info->extent_ins_mutex);
  2362. break;
  2363. }
  2364. ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
  2365. if (!ret) {
  2366. search = end+1;
  2367. skipped = 1;
  2368. if (need_resched()) {
  2369. mutex_unlock(&info->extent_ins_mutex);
  2370. cond_resched();
  2371. mutex_lock(&info->extent_ins_mutex);
  2372. }
  2373. continue;
  2374. }
  2375. BUG_ON(ret < 0);
  2376. ret = get_state_private(pending_del, start, &priv);
  2377. BUG_ON(ret);
  2378. extent_op = (struct pending_extent_op *)(unsigned long)priv;
  2379. clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
  2380. GFP_NOFS);
  2381. if (!test_range_bit(extent_ins, start, end,
  2382. EXTENT_WRITEBACK, 0)) {
  2383. list_add_tail(&extent_op->list, &delete_list);
  2384. nr++;
  2385. } else {
  2386. kfree(extent_op);
  2387. ret = get_state_private(&info->extent_ins, start,
  2388. &priv);
  2389. BUG_ON(ret);
  2390. extent_op = (struct pending_extent_op *)
  2391. (unsigned long)priv;
  2392. clear_extent_bits(&info->extent_ins, start, end,
  2393. EXTENT_WRITEBACK, GFP_NOFS);
  2394. if (extent_op->type == PENDING_BACKREF_UPDATE) {
  2395. list_add_tail(&extent_op->list, &delete_list);
  2396. search = end + 1;
  2397. nr++;
  2398. continue;
  2399. }
  2400. mutex_lock(&extent_root->fs_info->pinned_mutex);
  2401. ret = pin_down_bytes(trans, extent_root, start,
  2402. end + 1 - start, 0);
  2403. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  2404. ret = update_block_group(trans, extent_root, start,
  2405. end + 1 - start, 0, ret > 0);
  2406. unlock_extent(extent_ins, start, end, GFP_NOFS);
  2407. BUG_ON(ret);
  2408. kfree(extent_op);
  2409. }
  2410. if (ret)
  2411. err = ret;
  2412. search = end + 1;
  2413. if (need_resched()) {
  2414. mutex_unlock(&info->extent_ins_mutex);
  2415. cond_resched();
  2416. mutex_lock(&info->extent_ins_mutex);
  2417. }
  2418. }
  2419. if (nr) {
  2420. ret = free_extents(trans, extent_root, &delete_list);
  2421. BUG_ON(ret);
  2422. }
  2423. if (all && skipped) {
  2424. INIT_LIST_HEAD(&delete_list);
  2425. search = 0;
  2426. nr = 0;
  2427. goto again;
  2428. }
  2429. if (!err)
  2430. finish_current_insert(trans, extent_root, 0);
  2431. return err;
  2432. }
  2433. /*
  2434. * remove an extent from the root, returns 0 on success
  2435. */
  2436. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  2437. struct btrfs_root *root,
  2438. u64 bytenr, u64 num_bytes, u64 parent,
  2439. u64 root_objectid, u64 ref_generation,
  2440. u64 owner_objectid, int pin)
  2441. {
  2442. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2443. int pending_ret;
  2444. int ret;
  2445. WARN_ON(num_bytes < root->sectorsize);
  2446. if (root == extent_root) {
  2447. struct pending_extent_op *extent_op = NULL;
  2448. mutex_lock(&root->fs_info->extent_ins_mutex);
  2449. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  2450. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  2451. u64 priv;
  2452. ret = get_state_private(&root->fs_info->extent_ins,
  2453. bytenr, &priv);
  2454. BUG_ON(ret);
  2455. extent_op = (struct pending_extent_op *)
  2456. (unsigned long)priv;
  2457. extent_op->del = 1;
  2458. if (extent_op->type == PENDING_EXTENT_INSERT) {
  2459. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2460. return 0;
  2461. }
  2462. }
  2463. if (extent_op) {
  2464. ref_generation = extent_op->orig_generation;
  2465. parent = extent_op->orig_parent;
  2466. }
  2467. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2468. BUG_ON(!extent_op);
  2469. extent_op->type = PENDING_EXTENT_DELETE;
  2470. extent_op->bytenr = bytenr;
  2471. extent_op->num_bytes = num_bytes;
  2472. extent_op->parent = parent;
  2473. extent_op->orig_parent = parent;
  2474. extent_op->generation = ref_generation;
  2475. extent_op->orig_generation = ref_generation;
  2476. extent_op->level = (int)owner_objectid;
  2477. INIT_LIST_HEAD(&extent_op->list);
  2478. extent_op->del = 0;
  2479. set_extent_bits(&root->fs_info->pending_del,
  2480. bytenr, bytenr + num_bytes - 1,
  2481. EXTENT_WRITEBACK, GFP_NOFS);
  2482. set_state_private(&root->fs_info->pending_del,
  2483. bytenr, (unsigned long)extent_op);
  2484. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2485. return 0;
  2486. }
  2487. /* if metadata always pin */
  2488. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  2489. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  2490. mutex_lock(&root->fs_info->pinned_mutex);
  2491. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  2492. mutex_unlock(&root->fs_info->pinned_mutex);
  2493. update_reserved_extents(root, bytenr, num_bytes, 0);
  2494. return 0;
  2495. }
  2496. pin = 1;
  2497. }
  2498. /* if data pin when any transaction has committed this */
  2499. if (ref_generation != trans->transid)
  2500. pin = 1;
  2501. ret = __free_extent(trans, root, bytenr, num_bytes, parent,
  2502. root_objectid, ref_generation,
  2503. owner_objectid, pin, pin == 0);
  2504. finish_current_insert(trans, root->fs_info->extent_root, 0);
  2505. pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
  2506. return ret ? ret : pending_ret;
  2507. }
  2508. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  2509. struct btrfs_root *root,
  2510. u64 bytenr, u64 num_bytes, u64 parent,
  2511. u64 root_objectid, u64 ref_generation,
  2512. u64 owner_objectid, int pin)
  2513. {
  2514. int ret;
  2515. ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
  2516. root_objectid, ref_generation,
  2517. owner_objectid, pin);
  2518. return ret;
  2519. }
  2520. static u64 stripe_align(struct btrfs_root *root, u64 val)
  2521. {
  2522. u64 mask = ((u64)root->stripesize - 1);
  2523. u64 ret = (val + mask) & ~mask;
  2524. return ret;
  2525. }
  2526. /*
  2527. * walks the btree of allocated extents and find a hole of a given size.
  2528. * The key ins is changed to record the hole:
  2529. * ins->objectid == block start
  2530. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  2531. * ins->offset == number of blocks
  2532. * Any available blocks before search_start are skipped.
  2533. */
  2534. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  2535. struct btrfs_root *orig_root,
  2536. u64 num_bytes, u64 empty_size,
  2537. u64 search_start, u64 search_end,
  2538. u64 hint_byte, struct btrfs_key *ins,
  2539. u64 exclude_start, u64 exclude_nr,
  2540. int data)
  2541. {
  2542. int ret = 0;
  2543. struct btrfs_root *root = orig_root->fs_info->extent_root;
  2544. u64 total_needed = num_bytes;
  2545. u64 *last_ptr = NULL;
  2546. u64 last_wanted = 0;
  2547. struct btrfs_block_group_cache *block_group = NULL;
  2548. int chunk_alloc_done = 0;
  2549. int empty_cluster = 2 * 1024 * 1024;
  2550. int allowed_chunk_alloc = 0;
  2551. struct list_head *head = NULL, *cur = NULL;
  2552. int loop = 0;
  2553. int extra_loop = 0;
  2554. struct btrfs_space_info *space_info;
  2555. WARN_ON(num_bytes < root->sectorsize);
  2556. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  2557. ins->objectid = 0;
  2558. ins->offset = 0;
  2559. if (orig_root->ref_cows || empty_size)
  2560. allowed_chunk_alloc = 1;
  2561. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  2562. last_ptr = &root->fs_info->last_alloc;
  2563. if (!btrfs_test_opt(root, SSD))
  2564. empty_cluster = 64 * 1024;
  2565. }
  2566. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
  2567. last_ptr = &root->fs_info->last_data_alloc;
  2568. if (last_ptr) {
  2569. if (*last_ptr) {
  2570. hint_byte = *last_ptr;
  2571. last_wanted = *last_ptr;
  2572. } else
  2573. empty_size += empty_cluster;
  2574. } else {
  2575. empty_cluster = 0;
  2576. }
  2577. search_start = max(search_start, first_logical_byte(root, 0));
  2578. search_start = max(search_start, hint_byte);
  2579. if (last_wanted && search_start != last_wanted) {
  2580. last_wanted = 0;
  2581. empty_size += empty_cluster;
  2582. }
  2583. total_needed += empty_size;
  2584. block_group = btrfs_lookup_block_group(root->fs_info, search_start);
  2585. if (!block_group)
  2586. block_group = btrfs_lookup_first_block_group(root->fs_info,
  2587. search_start);
  2588. space_info = __find_space_info(root->fs_info, data);
  2589. down_read(&space_info->groups_sem);
  2590. while (1) {
  2591. struct btrfs_free_space *free_space;
  2592. /*
  2593. * the only way this happens if our hint points to a block
  2594. * group thats not of the proper type, while looping this
  2595. * should never happen
  2596. */
  2597. if (empty_size)
  2598. extra_loop = 1;
  2599. if (!block_group)
  2600. goto new_group_no_lock;
  2601. if (unlikely(!block_group->cached)) {
  2602. mutex_lock(&block_group->cache_mutex);
  2603. ret = cache_block_group(root, block_group);
  2604. mutex_unlock(&block_group->cache_mutex);
  2605. if (ret)
  2606. break;
  2607. }
  2608. mutex_lock(&block_group->alloc_mutex);
  2609. if (unlikely(!block_group_bits(block_group, data)))
  2610. goto new_group;
  2611. if (unlikely(block_group->ro))
  2612. goto new_group;
  2613. free_space = btrfs_find_free_space(block_group, search_start,
  2614. total_needed);
  2615. if (free_space) {
  2616. u64 start = block_group->key.objectid;
  2617. u64 end = block_group->key.objectid +
  2618. block_group->key.offset;
  2619. search_start = stripe_align(root, free_space->offset);
  2620. /* move on to the next group */
  2621. if (search_start + num_bytes >= search_end)
  2622. goto new_group;
  2623. /* move on to the next group */
  2624. if (search_start + num_bytes > end)
  2625. goto new_group;
  2626. if (last_wanted && search_start != last_wanted) {
  2627. total_needed += empty_cluster;
  2628. empty_size += empty_cluster;
  2629. last_wanted = 0;
  2630. /*
  2631. * if search_start is still in this block group
  2632. * then we just re-search this block group
  2633. */
  2634. if (search_start >= start &&
  2635. search_start < end) {
  2636. mutex_unlock(&block_group->alloc_mutex);
  2637. continue;
  2638. }
  2639. /* else we go to the next block group */
  2640. goto new_group;
  2641. }
  2642. if (exclude_nr > 0 &&
  2643. (search_start + num_bytes > exclude_start &&
  2644. search_start < exclude_start + exclude_nr)) {
  2645. search_start = exclude_start + exclude_nr;
  2646. /*
  2647. * if search_start is still in this block group
  2648. * then we just re-search this block group
  2649. */
  2650. if (search_start >= start &&
  2651. search_start < end) {
  2652. mutex_unlock(&block_group->alloc_mutex);
  2653. last_wanted = 0;
  2654. continue;
  2655. }
  2656. /* else we go to the next block group */
  2657. goto new_group;
  2658. }
  2659. ins->objectid = search_start;
  2660. ins->offset = num_bytes;
  2661. btrfs_remove_free_space_lock(block_group, search_start,
  2662. num_bytes);
  2663. /* we are all good, lets return */
  2664. mutex_unlock(&block_group->alloc_mutex);
  2665. break;
  2666. }
  2667. new_group:
  2668. mutex_unlock(&block_group->alloc_mutex);
  2669. put_block_group(block_group);
  2670. block_group = NULL;
  2671. new_group_no_lock:
  2672. /* don't try to compare new allocations against the
  2673. * last allocation any more
  2674. */
  2675. last_wanted = 0;
  2676. /*
  2677. * Here's how this works.
  2678. * loop == 0: we were searching a block group via a hint
  2679. * and didn't find anything, so we start at
  2680. * the head of the block groups and keep searching
  2681. * loop == 1: we're searching through all of the block groups
  2682. * if we hit the head again we have searched
  2683. * all of the block groups for this space and we
  2684. * need to try and allocate, if we cant error out.
  2685. * loop == 2: we allocated more space and are looping through
  2686. * all of the block groups again.
  2687. */
  2688. if (loop == 0) {
  2689. head = &space_info->block_groups;
  2690. cur = head->next;
  2691. loop++;
  2692. } else if (loop == 1 && cur == head) {
  2693. int keep_going;
  2694. /* at this point we give up on the empty_size
  2695. * allocations and just try to allocate the min
  2696. * space.
  2697. *
  2698. * The extra_loop field was set if an empty_size
  2699. * allocation was attempted above, and if this
  2700. * is try we need to try the loop again without
  2701. * the additional empty_size.
  2702. */
  2703. total_needed -= empty_size;
  2704. empty_size = 0;
  2705. keep_going = extra_loop;
  2706. loop++;
  2707. if (allowed_chunk_alloc && !chunk_alloc_done) {
  2708. up_read(&space_info->groups_sem);
  2709. ret = do_chunk_alloc(trans, root, num_bytes +
  2710. 2 * 1024 * 1024, data, 1);
  2711. down_read(&space_info->groups_sem);
  2712. if (ret < 0)
  2713. goto loop_check;
  2714. head = &space_info->block_groups;
  2715. /*
  2716. * we've allocated a new chunk, keep
  2717. * trying
  2718. */
  2719. keep_going = 1;
  2720. chunk_alloc_done = 1;
  2721. } else if (!allowed_chunk_alloc) {
  2722. space_info->force_alloc = 1;
  2723. }
  2724. loop_check:
  2725. if (keep_going) {
  2726. cur = head->next;
  2727. extra_loop = 0;
  2728. } else {
  2729. break;
  2730. }
  2731. } else if (cur == head) {
  2732. break;
  2733. }
  2734. block_group = list_entry(cur, struct btrfs_block_group_cache,
  2735. list);
  2736. atomic_inc(&block_group->count);
  2737. search_start = block_group->key.objectid;
  2738. cur = cur->next;
  2739. }
  2740. /* we found what we needed */
  2741. if (ins->objectid) {
  2742. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  2743. trans->block_group = block_group->key.objectid;
  2744. if (last_ptr)
  2745. *last_ptr = ins->objectid + ins->offset;
  2746. ret = 0;
  2747. } else if (!ret) {
  2748. printk(KERN_ERR "btrfs searching for %llu bytes, "
  2749. "num_bytes %llu, loop %d, allowed_alloc %d\n",
  2750. (unsigned long long)total_needed,
  2751. (unsigned long long)num_bytes,
  2752. loop, allowed_chunk_alloc);
  2753. ret = -ENOSPC;
  2754. }
  2755. if (block_group)
  2756. put_block_group(block_group);
  2757. up_read(&space_info->groups_sem);
  2758. return ret;
  2759. }
  2760. static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
  2761. {
  2762. struct btrfs_block_group_cache *cache;
  2763. printk(KERN_INFO "space_info has %llu free, is %sfull\n",
  2764. (unsigned long long)(info->total_bytes - info->bytes_used -
  2765. info->bytes_pinned - info->bytes_reserved),
  2766. (info->full) ? "" : "not ");
  2767. down_read(&info->groups_sem);
  2768. list_for_each_entry(cache, &info->block_groups, list) {
  2769. spin_lock(&cache->lock);
  2770. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  2771. "%llu pinned %llu reserved\n",
  2772. (unsigned long long)cache->key.objectid,
  2773. (unsigned long long)cache->key.offset,
  2774. (unsigned long long)btrfs_block_group_used(&cache->item),
  2775. (unsigned long long)cache->pinned,
  2776. (unsigned long long)cache->reserved);
  2777. btrfs_dump_free_space(cache, bytes);
  2778. spin_unlock(&cache->lock);
  2779. }
  2780. up_read(&info->groups_sem);
  2781. }
  2782. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2783. struct btrfs_root *root,
  2784. u64 num_bytes, u64 min_alloc_size,
  2785. u64 empty_size, u64 hint_byte,
  2786. u64 search_end, struct btrfs_key *ins,
  2787. u64 data)
  2788. {
  2789. int ret;
  2790. u64 search_start = 0;
  2791. u64 alloc_profile;
  2792. struct btrfs_fs_info *info = root->fs_info;
  2793. if (data) {
  2794. alloc_profile = info->avail_data_alloc_bits &
  2795. info->data_alloc_profile;
  2796. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  2797. } else if (root == root->fs_info->chunk_root) {
  2798. alloc_profile = info->avail_system_alloc_bits &
  2799. info->system_alloc_profile;
  2800. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  2801. } else {
  2802. alloc_profile = info->avail_metadata_alloc_bits &
  2803. info->metadata_alloc_profile;
  2804. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  2805. }
  2806. again:
  2807. data = btrfs_reduce_alloc_profile(root, data);
  2808. /*
  2809. * the only place that sets empty_size is btrfs_realloc_node, which
  2810. * is not called recursively on allocations
  2811. */
  2812. if (empty_size || root->ref_cows) {
  2813. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  2814. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2815. 2 * 1024 * 1024,
  2816. BTRFS_BLOCK_GROUP_METADATA |
  2817. (info->metadata_alloc_profile &
  2818. info->avail_metadata_alloc_bits), 0);
  2819. }
  2820. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2821. num_bytes + 2 * 1024 * 1024, data, 0);
  2822. }
  2823. WARN_ON(num_bytes < root->sectorsize);
  2824. ret = find_free_extent(trans, root, num_bytes, empty_size,
  2825. search_start, search_end, hint_byte, ins,
  2826. trans->alloc_exclude_start,
  2827. trans->alloc_exclude_nr, data);
  2828. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  2829. num_bytes = num_bytes >> 1;
  2830. num_bytes = num_bytes & ~(root->sectorsize - 1);
  2831. num_bytes = max(num_bytes, min_alloc_size);
  2832. do_chunk_alloc(trans, root->fs_info->extent_root,
  2833. num_bytes, data, 1);
  2834. goto again;
  2835. }
  2836. if (ret) {
  2837. struct btrfs_space_info *sinfo;
  2838. sinfo = __find_space_info(root->fs_info, data);
  2839. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  2840. "wanted %llu\n", (unsigned long long)data,
  2841. (unsigned long long)num_bytes);
  2842. dump_space_info(sinfo, num_bytes);
  2843. BUG();
  2844. }
  2845. return ret;
  2846. }
  2847. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  2848. {
  2849. struct btrfs_block_group_cache *cache;
  2850. int ret = 0;
  2851. cache = btrfs_lookup_block_group(root->fs_info, start);
  2852. if (!cache) {
  2853. printk(KERN_ERR "Unable to find block group for %llu\n",
  2854. (unsigned long long)start);
  2855. return -ENOSPC;
  2856. }
  2857. ret = btrfs_discard_extent(root, start, len);
  2858. btrfs_add_free_space(cache, start, len);
  2859. put_block_group(cache);
  2860. update_reserved_extents(root, start, len, 0);
  2861. return ret;
  2862. }
  2863. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2864. struct btrfs_root *root,
  2865. u64 num_bytes, u64 min_alloc_size,
  2866. u64 empty_size, u64 hint_byte,
  2867. u64 search_end, struct btrfs_key *ins,
  2868. u64 data)
  2869. {
  2870. int ret;
  2871. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  2872. empty_size, hint_byte, search_end, ins,
  2873. data);
  2874. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2875. return ret;
  2876. }
  2877. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2878. struct btrfs_root *root, u64 parent,
  2879. u64 root_objectid, u64 ref_generation,
  2880. u64 owner, struct btrfs_key *ins)
  2881. {
  2882. int ret;
  2883. int pending_ret;
  2884. u64 super_used;
  2885. u64 root_used;
  2886. u64 num_bytes = ins->offset;
  2887. u32 sizes[2];
  2888. struct btrfs_fs_info *info = root->fs_info;
  2889. struct btrfs_root *extent_root = info->extent_root;
  2890. struct btrfs_extent_item *extent_item;
  2891. struct btrfs_extent_ref *ref;
  2892. struct btrfs_path *path;
  2893. struct btrfs_key keys[2];
  2894. if (parent == 0)
  2895. parent = ins->objectid;
  2896. /* block accounting for super block */
  2897. spin_lock(&info->delalloc_lock);
  2898. super_used = btrfs_super_bytes_used(&info->super_copy);
  2899. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  2900. /* block accounting for root item */
  2901. root_used = btrfs_root_used(&root->root_item);
  2902. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  2903. spin_unlock(&info->delalloc_lock);
  2904. if (root == extent_root) {
  2905. struct pending_extent_op *extent_op;
  2906. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2907. BUG_ON(!extent_op);
  2908. extent_op->type = PENDING_EXTENT_INSERT;
  2909. extent_op->bytenr = ins->objectid;
  2910. extent_op->num_bytes = ins->offset;
  2911. extent_op->parent = parent;
  2912. extent_op->orig_parent = 0;
  2913. extent_op->generation = ref_generation;
  2914. extent_op->orig_generation = 0;
  2915. extent_op->level = (int)owner;
  2916. INIT_LIST_HEAD(&extent_op->list);
  2917. extent_op->del = 0;
  2918. mutex_lock(&root->fs_info->extent_ins_mutex);
  2919. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  2920. ins->objectid + ins->offset - 1,
  2921. EXTENT_WRITEBACK, GFP_NOFS);
  2922. set_state_private(&root->fs_info->extent_ins,
  2923. ins->objectid, (unsigned long)extent_op);
  2924. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2925. goto update_block;
  2926. }
  2927. memcpy(&keys[0], ins, sizeof(*ins));
  2928. keys[1].objectid = ins->objectid;
  2929. keys[1].type = BTRFS_EXTENT_REF_KEY;
  2930. keys[1].offset = parent;
  2931. sizes[0] = sizeof(*extent_item);
  2932. sizes[1] = sizeof(*ref);
  2933. path = btrfs_alloc_path();
  2934. BUG_ON(!path);
  2935. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  2936. sizes, 2);
  2937. BUG_ON(ret);
  2938. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2939. struct btrfs_extent_item);
  2940. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  2941. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2942. struct btrfs_extent_ref);
  2943. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2944. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2945. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2946. btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
  2947. btrfs_mark_buffer_dirty(path->nodes[0]);
  2948. trans->alloc_exclude_start = 0;
  2949. trans->alloc_exclude_nr = 0;
  2950. btrfs_free_path(path);
  2951. finish_current_insert(trans, extent_root, 0);
  2952. pending_ret = del_pending_extents(trans, extent_root, 0);
  2953. if (ret)
  2954. goto out;
  2955. if (pending_ret) {
  2956. ret = pending_ret;
  2957. goto out;
  2958. }
  2959. update_block:
  2960. ret = update_block_group(trans, root, ins->objectid,
  2961. ins->offset, 1, 0);
  2962. if (ret) {
  2963. printk(KERN_ERR "btrfs update block group failed for %llu "
  2964. "%llu\n", (unsigned long long)ins->objectid,
  2965. (unsigned long long)ins->offset);
  2966. BUG();
  2967. }
  2968. out:
  2969. return ret;
  2970. }
  2971. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2972. struct btrfs_root *root, u64 parent,
  2973. u64 root_objectid, u64 ref_generation,
  2974. u64 owner, struct btrfs_key *ins)
  2975. {
  2976. int ret;
  2977. if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
  2978. return 0;
  2979. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2980. ref_generation, owner, ins);
  2981. update_reserved_extents(root, ins->objectid, ins->offset, 0);
  2982. return ret;
  2983. }
  2984. /*
  2985. * this is used by the tree logging recovery code. It records that
  2986. * an extent has been allocated and makes sure to clear the free
  2987. * space cache bits as well
  2988. */
  2989. int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
  2990. struct btrfs_root *root, u64 parent,
  2991. u64 root_objectid, u64 ref_generation,
  2992. u64 owner, struct btrfs_key *ins)
  2993. {
  2994. int ret;
  2995. struct btrfs_block_group_cache *block_group;
  2996. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  2997. mutex_lock(&block_group->cache_mutex);
  2998. cache_block_group(root, block_group);
  2999. mutex_unlock(&block_group->cache_mutex);
  3000. ret = btrfs_remove_free_space(block_group, ins->objectid,
  3001. ins->offset);
  3002. BUG_ON(ret);
  3003. put_block_group(block_group);
  3004. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  3005. ref_generation, owner, ins);
  3006. return ret;
  3007. }
  3008. /*
  3009. * finds a free extent and does all the dirty work required for allocation
  3010. * returns the key for the extent through ins, and a tree buffer for
  3011. * the first block of the extent through buf.
  3012. *
  3013. * returns 0 if everything worked, non-zero otherwise.
  3014. */
  3015. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  3016. struct btrfs_root *root,
  3017. u64 num_bytes, u64 parent, u64 min_alloc_size,
  3018. u64 root_objectid, u64 ref_generation,
  3019. u64 owner_objectid, u64 empty_size, u64 hint_byte,
  3020. u64 search_end, struct btrfs_key *ins, u64 data)
  3021. {
  3022. int ret;
  3023. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  3024. min_alloc_size, empty_size, hint_byte,
  3025. search_end, ins, data);
  3026. BUG_ON(ret);
  3027. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  3028. ret = __btrfs_alloc_reserved_extent(trans, root, parent,
  3029. root_objectid, ref_generation,
  3030. owner_objectid, ins);
  3031. BUG_ON(ret);
  3032. } else {
  3033. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  3034. }
  3035. return ret;
  3036. }
  3037. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  3038. struct btrfs_root *root,
  3039. u64 bytenr, u32 blocksize,
  3040. int level)
  3041. {
  3042. struct extent_buffer *buf;
  3043. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  3044. if (!buf)
  3045. return ERR_PTR(-ENOMEM);
  3046. btrfs_set_header_generation(buf, trans->transid);
  3047. btrfs_set_buffer_lockdep_class(buf, level);
  3048. btrfs_tree_lock(buf);
  3049. clean_tree_block(trans, root, buf);
  3050. btrfs_set_lock_blocking(buf);
  3051. btrfs_set_buffer_uptodate(buf);
  3052. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  3053. set_extent_dirty(&root->dirty_log_pages, buf->start,
  3054. buf->start + buf->len - 1, GFP_NOFS);
  3055. } else {
  3056. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  3057. buf->start + buf->len - 1, GFP_NOFS);
  3058. }
  3059. trans->blocks_used++;
  3060. /* this returns a buffer locked for blocking */
  3061. return buf;
  3062. }
  3063. /*
  3064. * helper function to allocate a block for a given tree
  3065. * returns the tree buffer or NULL.
  3066. */
  3067. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  3068. struct btrfs_root *root,
  3069. u32 blocksize, u64 parent,
  3070. u64 root_objectid,
  3071. u64 ref_generation,
  3072. int level,
  3073. u64 hint,
  3074. u64 empty_size)
  3075. {
  3076. struct btrfs_key ins;
  3077. int ret;
  3078. struct extent_buffer *buf;
  3079. ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
  3080. root_objectid, ref_generation, level,
  3081. empty_size, hint, (u64)-1, &ins, 0);
  3082. if (ret) {
  3083. BUG_ON(ret > 0);
  3084. return ERR_PTR(ret);
  3085. }
  3086. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  3087. blocksize, level);
  3088. return buf;
  3089. }
  3090. int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3091. struct btrfs_root *root, struct extent_buffer *leaf)
  3092. {
  3093. u64 leaf_owner;
  3094. u64 leaf_generation;
  3095. struct refsort *sorted;
  3096. struct btrfs_key key;
  3097. struct btrfs_file_extent_item *fi;
  3098. int i;
  3099. int nritems;
  3100. int ret;
  3101. int refi = 0;
  3102. int slot;
  3103. BUG_ON(!btrfs_is_leaf(leaf));
  3104. nritems = btrfs_header_nritems(leaf);
  3105. leaf_owner = btrfs_header_owner(leaf);
  3106. leaf_generation = btrfs_header_generation(leaf);
  3107. sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
  3108. /* we do this loop twice. The first time we build a list
  3109. * of the extents we have a reference on, then we sort the list
  3110. * by bytenr. The second time around we actually do the
  3111. * extent freeing.
  3112. */
  3113. for (i = 0; i < nritems; i++) {
  3114. u64 disk_bytenr;
  3115. cond_resched();
  3116. btrfs_item_key_to_cpu(leaf, &key, i);
  3117. /* only extents have references, skip everything else */
  3118. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3119. continue;
  3120. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3121. /* inline extents live in the btree, they don't have refs */
  3122. if (btrfs_file_extent_type(leaf, fi) ==
  3123. BTRFS_FILE_EXTENT_INLINE)
  3124. continue;
  3125. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  3126. /* holes don't have refs */
  3127. if (disk_bytenr == 0)
  3128. continue;
  3129. sorted[refi].bytenr = disk_bytenr;
  3130. sorted[refi].slot = i;
  3131. refi++;
  3132. }
  3133. if (refi == 0)
  3134. goto out;
  3135. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  3136. for (i = 0; i < refi; i++) {
  3137. u64 disk_bytenr;
  3138. disk_bytenr = sorted[i].bytenr;
  3139. slot = sorted[i].slot;
  3140. cond_resched();
  3141. btrfs_item_key_to_cpu(leaf, &key, slot);
  3142. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3143. continue;
  3144. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  3145. ret = __btrfs_free_extent(trans, root, disk_bytenr,
  3146. btrfs_file_extent_disk_num_bytes(leaf, fi),
  3147. leaf->start, leaf_owner, leaf_generation,
  3148. key.objectid, 0);
  3149. BUG_ON(ret);
  3150. atomic_inc(&root->fs_info->throttle_gen);
  3151. wake_up(&root->fs_info->transaction_throttle);
  3152. cond_resched();
  3153. }
  3154. out:
  3155. kfree(sorted);
  3156. return 0;
  3157. }
  3158. static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3159. struct btrfs_root *root,
  3160. struct btrfs_leaf_ref *ref)
  3161. {
  3162. int i;
  3163. int ret;
  3164. struct btrfs_extent_info *info;
  3165. struct refsort *sorted;
  3166. if (ref->nritems == 0)
  3167. return 0;
  3168. sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
  3169. for (i = 0; i < ref->nritems; i++) {
  3170. sorted[i].bytenr = ref->extents[i].bytenr;
  3171. sorted[i].slot = i;
  3172. }
  3173. sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
  3174. /*
  3175. * the items in the ref were sorted when the ref was inserted
  3176. * into the ref cache, so this is already in order
  3177. */
  3178. for (i = 0; i < ref->nritems; i++) {
  3179. info = ref->extents + sorted[i].slot;
  3180. ret = __btrfs_free_extent(trans, root, info->bytenr,
  3181. info->num_bytes, ref->bytenr,
  3182. ref->owner, ref->generation,
  3183. info->objectid, 0);
  3184. atomic_inc(&root->fs_info->throttle_gen);
  3185. wake_up(&root->fs_info->transaction_throttle);
  3186. cond_resched();
  3187. BUG_ON(ret);
  3188. info++;
  3189. }
  3190. kfree(sorted);
  3191. return 0;
  3192. }
  3193. static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
  3194. u64 len, u32 *refs)
  3195. {
  3196. int ret;
  3197. ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
  3198. BUG_ON(ret);
  3199. #if 0 /* some debugging code in case we see problems here */
  3200. /* if the refs count is one, it won't get increased again. But
  3201. * if the ref count is > 1, someone may be decreasing it at
  3202. * the same time we are.
  3203. */
  3204. if (*refs != 1) {
  3205. struct extent_buffer *eb = NULL;
  3206. eb = btrfs_find_create_tree_block(root, start, len);
  3207. if (eb)
  3208. btrfs_tree_lock(eb);
  3209. mutex_lock(&root->fs_info->alloc_mutex);
  3210. ret = lookup_extent_ref(NULL, root, start, len, refs);
  3211. BUG_ON(ret);
  3212. mutex_unlock(&root->fs_info->alloc_mutex);
  3213. if (eb) {
  3214. btrfs_tree_unlock(eb);
  3215. free_extent_buffer(eb);
  3216. }
  3217. if (*refs == 1) {
  3218. printk(KERN_ERR "btrfs block %llu went down to one "
  3219. "during drop_snap\n", (unsigned long long)start);
  3220. }
  3221. }
  3222. #endif
  3223. cond_resched();
  3224. return ret;
  3225. }
  3226. /*
  3227. * this is used while deleting old snapshots, and it drops the refs
  3228. * on a whole subtree starting from a level 1 node.
  3229. *
  3230. * The idea is to sort all the leaf pointers, and then drop the
  3231. * ref on all the leaves in order. Most of the time the leaves
  3232. * will have ref cache entries, so no leaf IOs will be required to
  3233. * find the extents they have references on.
  3234. *
  3235. * For each leaf, any references it has are also dropped in order
  3236. *
  3237. * This ends up dropping the references in something close to optimal
  3238. * order for reading and modifying the extent allocation tree.
  3239. */
  3240. static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
  3241. struct btrfs_root *root,
  3242. struct btrfs_path *path)
  3243. {
  3244. u64 bytenr;
  3245. u64 root_owner;
  3246. u64 root_gen;
  3247. struct extent_buffer *eb = path->nodes[1];
  3248. struct extent_buffer *leaf;
  3249. struct btrfs_leaf_ref *ref;
  3250. struct refsort *sorted = NULL;
  3251. int nritems = btrfs_header_nritems(eb);
  3252. int ret;
  3253. int i;
  3254. int refi = 0;
  3255. int slot = path->slots[1];
  3256. u32 blocksize = btrfs_level_size(root, 0);
  3257. u32 refs;
  3258. if (nritems == 0)
  3259. goto out;
  3260. root_owner = btrfs_header_owner(eb);
  3261. root_gen = btrfs_header_generation(eb);
  3262. sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
  3263. /*
  3264. * step one, sort all the leaf pointers so we don't scribble
  3265. * randomly into the extent allocation tree
  3266. */
  3267. for (i = slot; i < nritems; i++) {
  3268. sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
  3269. sorted[refi].slot = i;
  3270. refi++;
  3271. }
  3272. /*
  3273. * nritems won't be zero, but if we're picking up drop_snapshot
  3274. * after a crash, slot might be > 0, so double check things
  3275. * just in case.
  3276. */
  3277. if (refi == 0)
  3278. goto out;
  3279. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  3280. /*
  3281. * the first loop frees everything the leaves point to
  3282. */
  3283. for (i = 0; i < refi; i++) {
  3284. u64 ptr_gen;
  3285. bytenr = sorted[i].bytenr;
  3286. /*
  3287. * check the reference count on this leaf. If it is > 1
  3288. * we just decrement it below and don't update any
  3289. * of the refs the leaf points to.
  3290. */
  3291. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  3292. BUG_ON(ret);
  3293. if (refs != 1)
  3294. continue;
  3295. ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
  3296. /*
  3297. * the leaf only had one reference, which means the
  3298. * only thing pointing to this leaf is the snapshot
  3299. * we're deleting. It isn't possible for the reference
  3300. * count to increase again later
  3301. *
  3302. * The reference cache is checked for the leaf,
  3303. * and if found we'll be able to drop any refs held by
  3304. * the leaf without needing to read it in.
  3305. */
  3306. ref = btrfs_lookup_leaf_ref(root, bytenr);
  3307. if (ref && ref->generation != ptr_gen) {
  3308. btrfs_free_leaf_ref(root, ref);
  3309. ref = NULL;
  3310. }
  3311. if (ref) {
  3312. ret = cache_drop_leaf_ref(trans, root, ref);
  3313. BUG_ON(ret);
  3314. btrfs_remove_leaf_ref(root, ref);
  3315. btrfs_free_leaf_ref(root, ref);
  3316. } else {
  3317. /*
  3318. * the leaf wasn't in the reference cache, so
  3319. * we have to read it.
  3320. */
  3321. leaf = read_tree_block(root, bytenr, blocksize,
  3322. ptr_gen);
  3323. ret = btrfs_drop_leaf_ref(trans, root, leaf);
  3324. BUG_ON(ret);
  3325. free_extent_buffer(leaf);
  3326. }
  3327. atomic_inc(&root->fs_info->throttle_gen);
  3328. wake_up(&root->fs_info->transaction_throttle);
  3329. cond_resched();
  3330. }
  3331. /*
  3332. * run through the loop again to free the refs on the leaves.
  3333. * This is faster than doing it in the loop above because
  3334. * the leaves are likely to be clustered together. We end up
  3335. * working in nice chunks on the extent allocation tree.
  3336. */
  3337. for (i = 0; i < refi; i++) {
  3338. bytenr = sorted[i].bytenr;
  3339. ret = __btrfs_free_extent(trans, root, bytenr,
  3340. blocksize, eb->start,
  3341. root_owner, root_gen, 0, 1);
  3342. BUG_ON(ret);
  3343. atomic_inc(&root->fs_info->throttle_gen);
  3344. wake_up(&root->fs_info->transaction_throttle);
  3345. cond_resched();
  3346. }
  3347. out:
  3348. kfree(sorted);
  3349. /*
  3350. * update the path to show we've processed the entire level 1
  3351. * node. This will get saved into the root's drop_snapshot_progress
  3352. * field so these drops are not repeated again if this transaction
  3353. * commits.
  3354. */
  3355. path->slots[1] = nritems;
  3356. return 0;
  3357. }
  3358. /*
  3359. * helper function for drop_snapshot, this walks down the tree dropping ref
  3360. * counts as it goes.
  3361. */
  3362. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  3363. struct btrfs_root *root,
  3364. struct btrfs_path *path, int *level)
  3365. {
  3366. u64 root_owner;
  3367. u64 root_gen;
  3368. u64 bytenr;
  3369. u64 ptr_gen;
  3370. struct extent_buffer *next;
  3371. struct extent_buffer *cur;
  3372. struct extent_buffer *parent;
  3373. u32 blocksize;
  3374. int ret;
  3375. u32 refs;
  3376. WARN_ON(*level < 0);
  3377. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3378. ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
  3379. path->nodes[*level]->len, &refs);
  3380. BUG_ON(ret);
  3381. if (refs > 1)
  3382. goto out;
  3383. /*
  3384. * walk down to the last node level and free all the leaves
  3385. */
  3386. while (*level >= 0) {
  3387. WARN_ON(*level < 0);
  3388. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3389. cur = path->nodes[*level];
  3390. if (btrfs_header_level(cur) != *level)
  3391. WARN_ON(1);
  3392. if (path->slots[*level] >=
  3393. btrfs_header_nritems(cur))
  3394. break;
  3395. /* the new code goes down to level 1 and does all the
  3396. * leaves pointed to that node in bulk. So, this check
  3397. * for level 0 will always be false.
  3398. *
  3399. * But, the disk format allows the drop_snapshot_progress
  3400. * field in the root to leave things in a state where
  3401. * a leaf will need cleaning up here. If someone crashes
  3402. * with the old code and then boots with the new code,
  3403. * we might find a leaf here.
  3404. */
  3405. if (*level == 0) {
  3406. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3407. BUG_ON(ret);
  3408. break;
  3409. }
  3410. /*
  3411. * once we get to level one, process the whole node
  3412. * at once, including everything below it.
  3413. */
  3414. if (*level == 1) {
  3415. ret = drop_level_one_refs(trans, root, path);
  3416. BUG_ON(ret);
  3417. break;
  3418. }
  3419. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3420. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3421. blocksize = btrfs_level_size(root, *level - 1);
  3422. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  3423. BUG_ON(ret);
  3424. /*
  3425. * if there is more than one reference, we don't need
  3426. * to read that node to drop any references it has. We
  3427. * just drop the ref we hold on that node and move on to the
  3428. * next slot in this level.
  3429. */
  3430. if (refs != 1) {
  3431. parent = path->nodes[*level];
  3432. root_owner = btrfs_header_owner(parent);
  3433. root_gen = btrfs_header_generation(parent);
  3434. path->slots[*level]++;
  3435. ret = __btrfs_free_extent(trans, root, bytenr,
  3436. blocksize, parent->start,
  3437. root_owner, root_gen,
  3438. *level - 1, 1);
  3439. BUG_ON(ret);
  3440. atomic_inc(&root->fs_info->throttle_gen);
  3441. wake_up(&root->fs_info->transaction_throttle);
  3442. cond_resched();
  3443. continue;
  3444. }
  3445. /*
  3446. * we need to keep freeing things in the next level down.
  3447. * read the block and loop around to process it
  3448. */
  3449. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3450. WARN_ON(*level <= 0);
  3451. if (path->nodes[*level-1])
  3452. free_extent_buffer(path->nodes[*level-1]);
  3453. path->nodes[*level-1] = next;
  3454. *level = btrfs_header_level(next);
  3455. path->slots[*level] = 0;
  3456. cond_resched();
  3457. }
  3458. out:
  3459. WARN_ON(*level < 0);
  3460. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3461. if (path->nodes[*level] == root->node) {
  3462. parent = path->nodes[*level];
  3463. bytenr = path->nodes[*level]->start;
  3464. } else {
  3465. parent = path->nodes[*level + 1];
  3466. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  3467. }
  3468. blocksize = btrfs_level_size(root, *level);
  3469. root_owner = btrfs_header_owner(parent);
  3470. root_gen = btrfs_header_generation(parent);
  3471. /*
  3472. * cleanup and free the reference on the last node
  3473. * we processed
  3474. */
  3475. ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
  3476. parent->start, root_owner, root_gen,
  3477. *level, 1);
  3478. free_extent_buffer(path->nodes[*level]);
  3479. path->nodes[*level] = NULL;
  3480. *level += 1;
  3481. BUG_ON(ret);
  3482. cond_resched();
  3483. return 0;
  3484. }
  3485. /*
  3486. * helper function for drop_subtree, this function is similar to
  3487. * walk_down_tree. The main difference is that it checks reference
  3488. * counts while tree blocks are locked.
  3489. */
  3490. static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
  3491. struct btrfs_root *root,
  3492. struct btrfs_path *path, int *level)
  3493. {
  3494. struct extent_buffer *next;
  3495. struct extent_buffer *cur;
  3496. struct extent_buffer *parent;
  3497. u64 bytenr;
  3498. u64 ptr_gen;
  3499. u32 blocksize;
  3500. u32 refs;
  3501. int ret;
  3502. cur = path->nodes[*level];
  3503. ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
  3504. &refs);
  3505. BUG_ON(ret);
  3506. if (refs > 1)
  3507. goto out;
  3508. while (*level >= 0) {
  3509. cur = path->nodes[*level];
  3510. if (*level == 0) {
  3511. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3512. BUG_ON(ret);
  3513. clean_tree_block(trans, root, cur);
  3514. break;
  3515. }
  3516. if (path->slots[*level] >= btrfs_header_nritems(cur)) {
  3517. clean_tree_block(trans, root, cur);
  3518. break;
  3519. }
  3520. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3521. blocksize = btrfs_level_size(root, *level - 1);
  3522. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3523. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3524. btrfs_tree_lock(next);
  3525. btrfs_set_lock_blocking(next);
  3526. ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
  3527. &refs);
  3528. BUG_ON(ret);
  3529. if (refs > 1) {
  3530. parent = path->nodes[*level];
  3531. ret = btrfs_free_extent(trans, root, bytenr,
  3532. blocksize, parent->start,
  3533. btrfs_header_owner(parent),
  3534. btrfs_header_generation(parent),
  3535. *level - 1, 1);
  3536. BUG_ON(ret);
  3537. path->slots[*level]++;
  3538. btrfs_tree_unlock(next);
  3539. free_extent_buffer(next);
  3540. continue;
  3541. }
  3542. *level = btrfs_header_level(next);
  3543. path->nodes[*level] = next;
  3544. path->slots[*level] = 0;
  3545. path->locks[*level] = 1;
  3546. cond_resched();
  3547. }
  3548. out:
  3549. parent = path->nodes[*level + 1];
  3550. bytenr = path->nodes[*level]->start;
  3551. blocksize = path->nodes[*level]->len;
  3552. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  3553. parent->start, btrfs_header_owner(parent),
  3554. btrfs_header_generation(parent), *level, 1);
  3555. BUG_ON(ret);
  3556. if (path->locks[*level]) {
  3557. btrfs_tree_unlock(path->nodes[*level]);
  3558. path->locks[*level] = 0;
  3559. }
  3560. free_extent_buffer(path->nodes[*level]);
  3561. path->nodes[*level] = NULL;
  3562. *level += 1;
  3563. cond_resched();
  3564. return 0;
  3565. }
  3566. /*
  3567. * helper for dropping snapshots. This walks back up the tree in the path
  3568. * to find the first node higher up where we haven't yet gone through
  3569. * all the slots
  3570. */
  3571. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  3572. struct btrfs_root *root,
  3573. struct btrfs_path *path,
  3574. int *level, int max_level)
  3575. {
  3576. u64 root_owner;
  3577. u64 root_gen;
  3578. struct btrfs_root_item *root_item = &root->root_item;
  3579. int i;
  3580. int slot;
  3581. int ret;
  3582. for (i = *level; i < max_level && path->nodes[i]; i++) {
  3583. slot = path->slots[i];
  3584. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  3585. struct extent_buffer *node;
  3586. struct btrfs_disk_key disk_key;
  3587. /*
  3588. * there is more work to do in this level.
  3589. * Update the drop_progress marker to reflect
  3590. * the work we've done so far, and then bump
  3591. * the slot number
  3592. */
  3593. node = path->nodes[i];
  3594. path->slots[i]++;
  3595. *level = i;
  3596. WARN_ON(*level == 0);
  3597. btrfs_node_key(node, &disk_key, path->slots[i]);
  3598. memcpy(&root_item->drop_progress,
  3599. &disk_key, sizeof(disk_key));
  3600. root_item->drop_level = i;
  3601. return 0;
  3602. } else {
  3603. struct extent_buffer *parent;
  3604. /*
  3605. * this whole node is done, free our reference
  3606. * on it and go up one level
  3607. */
  3608. if (path->nodes[*level] == root->node)
  3609. parent = path->nodes[*level];
  3610. else
  3611. parent = path->nodes[*level + 1];
  3612. root_owner = btrfs_header_owner(parent);
  3613. root_gen = btrfs_header_generation(parent);
  3614. clean_tree_block(trans, root, path->nodes[*level]);
  3615. ret = btrfs_free_extent(trans, root,
  3616. path->nodes[*level]->start,
  3617. path->nodes[*level]->len,
  3618. parent->start, root_owner,
  3619. root_gen, *level, 1);
  3620. BUG_ON(ret);
  3621. if (path->locks[*level]) {
  3622. btrfs_tree_unlock(path->nodes[*level]);
  3623. path->locks[*level] = 0;
  3624. }
  3625. free_extent_buffer(path->nodes[*level]);
  3626. path->nodes[*level] = NULL;
  3627. *level = i + 1;
  3628. }
  3629. }
  3630. return 1;
  3631. }
  3632. /*
  3633. * drop the reference count on the tree rooted at 'snap'. This traverses
  3634. * the tree freeing any blocks that have a ref count of zero after being
  3635. * decremented.
  3636. */
  3637. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  3638. *root)
  3639. {
  3640. int ret = 0;
  3641. int wret;
  3642. int level;
  3643. struct btrfs_path *path;
  3644. int i;
  3645. int orig_level;
  3646. struct btrfs_root_item *root_item = &root->root_item;
  3647. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  3648. path = btrfs_alloc_path();
  3649. BUG_ON(!path);
  3650. level = btrfs_header_level(root->node);
  3651. orig_level = level;
  3652. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  3653. path->nodes[level] = root->node;
  3654. extent_buffer_get(root->node);
  3655. path->slots[level] = 0;
  3656. } else {
  3657. struct btrfs_key key;
  3658. struct btrfs_disk_key found_key;
  3659. struct extent_buffer *node;
  3660. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  3661. level = root_item->drop_level;
  3662. path->lowest_level = level;
  3663. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3664. if (wret < 0) {
  3665. ret = wret;
  3666. goto out;
  3667. }
  3668. node = path->nodes[level];
  3669. btrfs_node_key(node, &found_key, path->slots[level]);
  3670. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  3671. sizeof(found_key)));
  3672. /*
  3673. * unlock our path, this is safe because only this
  3674. * function is allowed to delete this snapshot
  3675. */
  3676. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  3677. if (path->nodes[i] && path->locks[i]) {
  3678. path->locks[i] = 0;
  3679. btrfs_tree_unlock(path->nodes[i]);
  3680. }
  3681. }
  3682. }
  3683. while (1) {
  3684. wret = walk_down_tree(trans, root, path, &level);
  3685. if (wret > 0)
  3686. break;
  3687. if (wret < 0)
  3688. ret = wret;
  3689. wret = walk_up_tree(trans, root, path, &level,
  3690. BTRFS_MAX_LEVEL);
  3691. if (wret > 0)
  3692. break;
  3693. if (wret < 0)
  3694. ret = wret;
  3695. if (trans->transaction->in_commit) {
  3696. ret = -EAGAIN;
  3697. break;
  3698. }
  3699. atomic_inc(&root->fs_info->throttle_gen);
  3700. wake_up(&root->fs_info->transaction_throttle);
  3701. }
  3702. for (i = 0; i <= orig_level; i++) {
  3703. if (path->nodes[i]) {
  3704. free_extent_buffer(path->nodes[i]);
  3705. path->nodes[i] = NULL;
  3706. }
  3707. }
  3708. out:
  3709. btrfs_free_path(path);
  3710. return ret;
  3711. }
  3712. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  3713. struct btrfs_root *root,
  3714. struct extent_buffer *node,
  3715. struct extent_buffer *parent)
  3716. {
  3717. struct btrfs_path *path;
  3718. int level;
  3719. int parent_level;
  3720. int ret = 0;
  3721. int wret;
  3722. path = btrfs_alloc_path();
  3723. BUG_ON(!path);
  3724. BUG_ON(!btrfs_tree_locked(parent));
  3725. parent_level = btrfs_header_level(parent);
  3726. extent_buffer_get(parent);
  3727. path->nodes[parent_level] = parent;
  3728. path->slots[parent_level] = btrfs_header_nritems(parent);
  3729. BUG_ON(!btrfs_tree_locked(node));
  3730. level = btrfs_header_level(node);
  3731. extent_buffer_get(node);
  3732. path->nodes[level] = node;
  3733. path->slots[level] = 0;
  3734. while (1) {
  3735. wret = walk_down_subtree(trans, root, path, &level);
  3736. if (wret < 0)
  3737. ret = wret;
  3738. if (wret != 0)
  3739. break;
  3740. wret = walk_up_tree(trans, root, path, &level, parent_level);
  3741. if (wret < 0)
  3742. ret = wret;
  3743. if (wret != 0)
  3744. break;
  3745. }
  3746. btrfs_free_path(path);
  3747. return ret;
  3748. }
  3749. static unsigned long calc_ra(unsigned long start, unsigned long last,
  3750. unsigned long nr)
  3751. {
  3752. return min(last, start + nr - 1);
  3753. }
  3754. static noinline int relocate_inode_pages(struct inode *inode, u64 start,
  3755. u64 len)
  3756. {
  3757. u64 page_start;
  3758. u64 page_end;
  3759. unsigned long first_index;
  3760. unsigned long last_index;
  3761. unsigned long i;
  3762. struct page *page;
  3763. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3764. struct file_ra_state *ra;
  3765. struct btrfs_ordered_extent *ordered;
  3766. unsigned int total_read = 0;
  3767. unsigned int total_dirty = 0;
  3768. int ret = 0;
  3769. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  3770. mutex_lock(&inode->i_mutex);
  3771. first_index = start >> PAGE_CACHE_SHIFT;
  3772. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  3773. /* make sure the dirty trick played by the caller work */
  3774. ret = invalidate_inode_pages2_range(inode->i_mapping,
  3775. first_index, last_index);
  3776. if (ret)
  3777. goto out_unlock;
  3778. file_ra_state_init(ra, inode->i_mapping);
  3779. for (i = first_index ; i <= last_index; i++) {
  3780. if (total_read % ra->ra_pages == 0) {
  3781. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  3782. calc_ra(i, last_index, ra->ra_pages));
  3783. }
  3784. total_read++;
  3785. again:
  3786. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  3787. BUG_ON(1);
  3788. page = grab_cache_page(inode->i_mapping, i);
  3789. if (!page) {
  3790. ret = -ENOMEM;
  3791. goto out_unlock;
  3792. }
  3793. if (!PageUptodate(page)) {
  3794. btrfs_readpage(NULL, page);
  3795. lock_page(page);
  3796. if (!PageUptodate(page)) {
  3797. unlock_page(page);
  3798. page_cache_release(page);
  3799. ret = -EIO;
  3800. goto out_unlock;
  3801. }
  3802. }
  3803. wait_on_page_writeback(page);
  3804. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  3805. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3806. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3807. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3808. if (ordered) {
  3809. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3810. unlock_page(page);
  3811. page_cache_release(page);
  3812. btrfs_start_ordered_extent(inode, ordered, 1);
  3813. btrfs_put_ordered_extent(ordered);
  3814. goto again;
  3815. }
  3816. set_page_extent_mapped(page);
  3817. if (i == first_index)
  3818. set_extent_bits(io_tree, page_start, page_end,
  3819. EXTENT_BOUNDARY, GFP_NOFS);
  3820. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3821. set_page_dirty(page);
  3822. total_dirty++;
  3823. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3824. unlock_page(page);
  3825. page_cache_release(page);
  3826. }
  3827. out_unlock:
  3828. kfree(ra);
  3829. mutex_unlock(&inode->i_mutex);
  3830. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  3831. return ret;
  3832. }
  3833. static noinline int relocate_data_extent(struct inode *reloc_inode,
  3834. struct btrfs_key *extent_key,
  3835. u64 offset)
  3836. {
  3837. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3838. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  3839. struct extent_map *em;
  3840. u64 start = extent_key->objectid - offset;
  3841. u64 end = start + extent_key->offset - 1;
  3842. em = alloc_extent_map(GFP_NOFS);
  3843. BUG_ON(!em || IS_ERR(em));
  3844. em->start = start;
  3845. em->len = extent_key->offset;
  3846. em->block_len = extent_key->offset;
  3847. em->block_start = extent_key->objectid;
  3848. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3849. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  3850. /* setup extent map to cheat btrfs_readpage */
  3851. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3852. while (1) {
  3853. int ret;
  3854. spin_lock(&em_tree->lock);
  3855. ret = add_extent_mapping(em_tree, em);
  3856. spin_unlock(&em_tree->lock);
  3857. if (ret != -EEXIST) {
  3858. free_extent_map(em);
  3859. break;
  3860. }
  3861. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  3862. }
  3863. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3864. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  3865. }
  3866. struct btrfs_ref_path {
  3867. u64 extent_start;
  3868. u64 nodes[BTRFS_MAX_LEVEL];
  3869. u64 root_objectid;
  3870. u64 root_generation;
  3871. u64 owner_objectid;
  3872. u32 num_refs;
  3873. int lowest_level;
  3874. int current_level;
  3875. int shared_level;
  3876. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  3877. u64 new_nodes[BTRFS_MAX_LEVEL];
  3878. };
  3879. struct disk_extent {
  3880. u64 ram_bytes;
  3881. u64 disk_bytenr;
  3882. u64 disk_num_bytes;
  3883. u64 offset;
  3884. u64 num_bytes;
  3885. u8 compression;
  3886. u8 encryption;
  3887. u16 other_encoding;
  3888. };
  3889. static int is_cowonly_root(u64 root_objectid)
  3890. {
  3891. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  3892. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  3893. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  3894. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  3895. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  3896. root_objectid == BTRFS_CSUM_TREE_OBJECTID)
  3897. return 1;
  3898. return 0;
  3899. }
  3900. static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
  3901. struct btrfs_root *extent_root,
  3902. struct btrfs_ref_path *ref_path,
  3903. int first_time)
  3904. {
  3905. struct extent_buffer *leaf;
  3906. struct btrfs_path *path;
  3907. struct btrfs_extent_ref *ref;
  3908. struct btrfs_key key;
  3909. struct btrfs_key found_key;
  3910. u64 bytenr;
  3911. u32 nritems;
  3912. int level;
  3913. int ret = 1;
  3914. path = btrfs_alloc_path();
  3915. if (!path)
  3916. return -ENOMEM;
  3917. if (first_time) {
  3918. ref_path->lowest_level = -1;
  3919. ref_path->current_level = -1;
  3920. ref_path->shared_level = -1;
  3921. goto walk_up;
  3922. }
  3923. walk_down:
  3924. level = ref_path->current_level - 1;
  3925. while (level >= -1) {
  3926. u64 parent;
  3927. if (level < ref_path->lowest_level)
  3928. break;
  3929. if (level >= 0)
  3930. bytenr = ref_path->nodes[level];
  3931. else
  3932. bytenr = ref_path->extent_start;
  3933. BUG_ON(bytenr == 0);
  3934. parent = ref_path->nodes[level + 1];
  3935. ref_path->nodes[level + 1] = 0;
  3936. ref_path->current_level = level;
  3937. BUG_ON(parent == 0);
  3938. key.objectid = bytenr;
  3939. key.offset = parent + 1;
  3940. key.type = BTRFS_EXTENT_REF_KEY;
  3941. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3942. if (ret < 0)
  3943. goto out;
  3944. BUG_ON(ret == 0);
  3945. leaf = path->nodes[0];
  3946. nritems = btrfs_header_nritems(leaf);
  3947. if (path->slots[0] >= nritems) {
  3948. ret = btrfs_next_leaf(extent_root, path);
  3949. if (ret < 0)
  3950. goto out;
  3951. if (ret > 0)
  3952. goto next;
  3953. leaf = path->nodes[0];
  3954. }
  3955. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3956. if (found_key.objectid == bytenr &&
  3957. found_key.type == BTRFS_EXTENT_REF_KEY) {
  3958. if (level < ref_path->shared_level)
  3959. ref_path->shared_level = level;
  3960. goto found;
  3961. }
  3962. next:
  3963. level--;
  3964. btrfs_release_path(extent_root, path);
  3965. cond_resched();
  3966. }
  3967. /* reached lowest level */
  3968. ret = 1;
  3969. goto out;
  3970. walk_up:
  3971. level = ref_path->current_level;
  3972. while (level < BTRFS_MAX_LEVEL - 1) {
  3973. u64 ref_objectid;
  3974. if (level >= 0)
  3975. bytenr = ref_path->nodes[level];
  3976. else
  3977. bytenr = ref_path->extent_start;
  3978. BUG_ON(bytenr == 0);
  3979. key.objectid = bytenr;
  3980. key.offset = 0;
  3981. key.type = BTRFS_EXTENT_REF_KEY;
  3982. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3983. if (ret < 0)
  3984. goto out;
  3985. leaf = path->nodes[0];
  3986. nritems = btrfs_header_nritems(leaf);
  3987. if (path->slots[0] >= nritems) {
  3988. ret = btrfs_next_leaf(extent_root, path);
  3989. if (ret < 0)
  3990. goto out;
  3991. if (ret > 0) {
  3992. /* the extent was freed by someone */
  3993. if (ref_path->lowest_level == level)
  3994. goto out;
  3995. btrfs_release_path(extent_root, path);
  3996. goto walk_down;
  3997. }
  3998. leaf = path->nodes[0];
  3999. }
  4000. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4001. if (found_key.objectid != bytenr ||
  4002. found_key.type != BTRFS_EXTENT_REF_KEY) {
  4003. /* the extent was freed by someone */
  4004. if (ref_path->lowest_level == level) {
  4005. ret = 1;
  4006. goto out;
  4007. }
  4008. btrfs_release_path(extent_root, path);
  4009. goto walk_down;
  4010. }
  4011. found:
  4012. ref = btrfs_item_ptr(leaf, path->slots[0],
  4013. struct btrfs_extent_ref);
  4014. ref_objectid = btrfs_ref_objectid(leaf, ref);
  4015. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4016. if (first_time) {
  4017. level = (int)ref_objectid;
  4018. BUG_ON(level >= BTRFS_MAX_LEVEL);
  4019. ref_path->lowest_level = level;
  4020. ref_path->current_level = level;
  4021. ref_path->nodes[level] = bytenr;
  4022. } else {
  4023. WARN_ON(ref_objectid != level);
  4024. }
  4025. } else {
  4026. WARN_ON(level != -1);
  4027. }
  4028. first_time = 0;
  4029. if (ref_path->lowest_level == level) {
  4030. ref_path->owner_objectid = ref_objectid;
  4031. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  4032. }
  4033. /*
  4034. * the block is tree root or the block isn't in reference
  4035. * counted tree.
  4036. */
  4037. if (found_key.objectid == found_key.offset ||
  4038. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  4039. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  4040. ref_path->root_generation =
  4041. btrfs_ref_generation(leaf, ref);
  4042. if (level < 0) {
  4043. /* special reference from the tree log */
  4044. ref_path->nodes[0] = found_key.offset;
  4045. ref_path->current_level = 0;
  4046. }
  4047. ret = 0;
  4048. goto out;
  4049. }
  4050. level++;
  4051. BUG_ON(ref_path->nodes[level] != 0);
  4052. ref_path->nodes[level] = found_key.offset;
  4053. ref_path->current_level = level;
  4054. /*
  4055. * the reference was created in the running transaction,
  4056. * no need to continue walking up.
  4057. */
  4058. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  4059. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  4060. ref_path->root_generation =
  4061. btrfs_ref_generation(leaf, ref);
  4062. ret = 0;
  4063. goto out;
  4064. }
  4065. btrfs_release_path(extent_root, path);
  4066. cond_resched();
  4067. }
  4068. /* reached max tree level, but no tree root found. */
  4069. BUG();
  4070. out:
  4071. btrfs_free_path(path);
  4072. return ret;
  4073. }
  4074. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  4075. struct btrfs_root *extent_root,
  4076. struct btrfs_ref_path *ref_path,
  4077. u64 extent_start)
  4078. {
  4079. memset(ref_path, 0, sizeof(*ref_path));
  4080. ref_path->extent_start = extent_start;
  4081. return __next_ref_path(trans, extent_root, ref_path, 1);
  4082. }
  4083. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  4084. struct btrfs_root *extent_root,
  4085. struct btrfs_ref_path *ref_path)
  4086. {
  4087. return __next_ref_path(trans, extent_root, ref_path, 0);
  4088. }
  4089. static noinline int get_new_locations(struct inode *reloc_inode,
  4090. struct btrfs_key *extent_key,
  4091. u64 offset, int no_fragment,
  4092. struct disk_extent **extents,
  4093. int *nr_extents)
  4094. {
  4095. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  4096. struct btrfs_path *path;
  4097. struct btrfs_file_extent_item *fi;
  4098. struct extent_buffer *leaf;
  4099. struct disk_extent *exts = *extents;
  4100. struct btrfs_key found_key;
  4101. u64 cur_pos;
  4102. u64 last_byte;
  4103. u32 nritems;
  4104. int nr = 0;
  4105. int max = *nr_extents;
  4106. int ret;
  4107. WARN_ON(!no_fragment && *extents);
  4108. if (!exts) {
  4109. max = 1;
  4110. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  4111. if (!exts)
  4112. return -ENOMEM;
  4113. }
  4114. path = btrfs_alloc_path();
  4115. BUG_ON(!path);
  4116. cur_pos = extent_key->objectid - offset;
  4117. last_byte = extent_key->objectid + extent_key->offset;
  4118. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  4119. cur_pos, 0);
  4120. if (ret < 0)
  4121. goto out;
  4122. if (ret > 0) {
  4123. ret = -ENOENT;
  4124. goto out;
  4125. }
  4126. while (1) {
  4127. leaf = path->nodes[0];
  4128. nritems = btrfs_header_nritems(leaf);
  4129. if (path->slots[0] >= nritems) {
  4130. ret = btrfs_next_leaf(root, path);
  4131. if (ret < 0)
  4132. goto out;
  4133. if (ret > 0)
  4134. break;
  4135. leaf = path->nodes[0];
  4136. }
  4137. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4138. if (found_key.offset != cur_pos ||
  4139. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  4140. found_key.objectid != reloc_inode->i_ino)
  4141. break;
  4142. fi = btrfs_item_ptr(leaf, path->slots[0],
  4143. struct btrfs_file_extent_item);
  4144. if (btrfs_file_extent_type(leaf, fi) !=
  4145. BTRFS_FILE_EXTENT_REG ||
  4146. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  4147. break;
  4148. if (nr == max) {
  4149. struct disk_extent *old = exts;
  4150. max *= 2;
  4151. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  4152. memcpy(exts, old, sizeof(*exts) * nr);
  4153. if (old != *extents)
  4154. kfree(old);
  4155. }
  4156. exts[nr].disk_bytenr =
  4157. btrfs_file_extent_disk_bytenr(leaf, fi);
  4158. exts[nr].disk_num_bytes =
  4159. btrfs_file_extent_disk_num_bytes(leaf, fi);
  4160. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  4161. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4162. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  4163. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  4164. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  4165. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  4166. fi);
  4167. BUG_ON(exts[nr].offset > 0);
  4168. BUG_ON(exts[nr].compression || exts[nr].encryption);
  4169. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  4170. cur_pos += exts[nr].num_bytes;
  4171. nr++;
  4172. if (cur_pos + offset >= last_byte)
  4173. break;
  4174. if (no_fragment) {
  4175. ret = 1;
  4176. goto out;
  4177. }
  4178. path->slots[0]++;
  4179. }
  4180. BUG_ON(cur_pos + offset > last_byte);
  4181. if (cur_pos + offset < last_byte) {
  4182. ret = -ENOENT;
  4183. goto out;
  4184. }
  4185. ret = 0;
  4186. out:
  4187. btrfs_free_path(path);
  4188. if (ret) {
  4189. if (exts != *extents)
  4190. kfree(exts);
  4191. } else {
  4192. *extents = exts;
  4193. *nr_extents = nr;
  4194. }
  4195. return ret;
  4196. }
  4197. static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
  4198. struct btrfs_root *root,
  4199. struct btrfs_path *path,
  4200. struct btrfs_key *extent_key,
  4201. struct btrfs_key *leaf_key,
  4202. struct btrfs_ref_path *ref_path,
  4203. struct disk_extent *new_extents,
  4204. int nr_extents)
  4205. {
  4206. struct extent_buffer *leaf;
  4207. struct btrfs_file_extent_item *fi;
  4208. struct inode *inode = NULL;
  4209. struct btrfs_key key;
  4210. u64 lock_start = 0;
  4211. u64 lock_end = 0;
  4212. u64 num_bytes;
  4213. u64 ext_offset;
  4214. u64 search_end = (u64)-1;
  4215. u32 nritems;
  4216. int nr_scaned = 0;
  4217. int extent_locked = 0;
  4218. int extent_type;
  4219. int ret;
  4220. memcpy(&key, leaf_key, sizeof(key));
  4221. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  4222. if (key.objectid < ref_path->owner_objectid ||
  4223. (key.objectid == ref_path->owner_objectid &&
  4224. key.type < BTRFS_EXTENT_DATA_KEY)) {
  4225. key.objectid = ref_path->owner_objectid;
  4226. key.type = BTRFS_EXTENT_DATA_KEY;
  4227. key.offset = 0;
  4228. }
  4229. }
  4230. while (1) {
  4231. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  4232. if (ret < 0)
  4233. goto out;
  4234. leaf = path->nodes[0];
  4235. nritems = btrfs_header_nritems(leaf);
  4236. next:
  4237. if (extent_locked && ret > 0) {
  4238. /*
  4239. * the file extent item was modified by someone
  4240. * before the extent got locked.
  4241. */
  4242. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4243. lock_end, GFP_NOFS);
  4244. extent_locked = 0;
  4245. }
  4246. if (path->slots[0] >= nritems) {
  4247. if (++nr_scaned > 2)
  4248. break;
  4249. BUG_ON(extent_locked);
  4250. ret = btrfs_next_leaf(root, path);
  4251. if (ret < 0)
  4252. goto out;
  4253. if (ret > 0)
  4254. break;
  4255. leaf = path->nodes[0];
  4256. nritems = btrfs_header_nritems(leaf);
  4257. }
  4258. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4259. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  4260. if ((key.objectid > ref_path->owner_objectid) ||
  4261. (key.objectid == ref_path->owner_objectid &&
  4262. key.type > BTRFS_EXTENT_DATA_KEY) ||
  4263. key.offset >= search_end)
  4264. break;
  4265. }
  4266. if (inode && key.objectid != inode->i_ino) {
  4267. BUG_ON(extent_locked);
  4268. btrfs_release_path(root, path);
  4269. mutex_unlock(&inode->i_mutex);
  4270. iput(inode);
  4271. inode = NULL;
  4272. continue;
  4273. }
  4274. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  4275. path->slots[0]++;
  4276. ret = 1;
  4277. goto next;
  4278. }
  4279. fi = btrfs_item_ptr(leaf, path->slots[0],
  4280. struct btrfs_file_extent_item);
  4281. extent_type = btrfs_file_extent_type(leaf, fi);
  4282. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  4283. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  4284. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  4285. extent_key->objectid)) {
  4286. path->slots[0]++;
  4287. ret = 1;
  4288. goto next;
  4289. }
  4290. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4291. ext_offset = btrfs_file_extent_offset(leaf, fi);
  4292. if (search_end == (u64)-1) {
  4293. search_end = key.offset - ext_offset +
  4294. btrfs_file_extent_ram_bytes(leaf, fi);
  4295. }
  4296. if (!extent_locked) {
  4297. lock_start = key.offset;
  4298. lock_end = lock_start + num_bytes - 1;
  4299. } else {
  4300. if (lock_start > key.offset ||
  4301. lock_end + 1 < key.offset + num_bytes) {
  4302. unlock_extent(&BTRFS_I(inode)->io_tree,
  4303. lock_start, lock_end, GFP_NOFS);
  4304. extent_locked = 0;
  4305. }
  4306. }
  4307. if (!inode) {
  4308. btrfs_release_path(root, path);
  4309. inode = btrfs_iget_locked(root->fs_info->sb,
  4310. key.objectid, root);
  4311. if (inode->i_state & I_NEW) {
  4312. BTRFS_I(inode)->root = root;
  4313. BTRFS_I(inode)->location.objectid =
  4314. key.objectid;
  4315. BTRFS_I(inode)->location.type =
  4316. BTRFS_INODE_ITEM_KEY;
  4317. BTRFS_I(inode)->location.offset = 0;
  4318. btrfs_read_locked_inode(inode);
  4319. unlock_new_inode(inode);
  4320. }
  4321. /*
  4322. * some code call btrfs_commit_transaction while
  4323. * holding the i_mutex, so we can't use mutex_lock
  4324. * here.
  4325. */
  4326. if (is_bad_inode(inode) ||
  4327. !mutex_trylock(&inode->i_mutex)) {
  4328. iput(inode);
  4329. inode = NULL;
  4330. key.offset = (u64)-1;
  4331. goto skip;
  4332. }
  4333. }
  4334. if (!extent_locked) {
  4335. struct btrfs_ordered_extent *ordered;
  4336. btrfs_release_path(root, path);
  4337. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4338. lock_end, GFP_NOFS);
  4339. ordered = btrfs_lookup_first_ordered_extent(inode,
  4340. lock_end);
  4341. if (ordered &&
  4342. ordered->file_offset <= lock_end &&
  4343. ordered->file_offset + ordered->len > lock_start) {
  4344. unlock_extent(&BTRFS_I(inode)->io_tree,
  4345. lock_start, lock_end, GFP_NOFS);
  4346. btrfs_start_ordered_extent(inode, ordered, 1);
  4347. btrfs_put_ordered_extent(ordered);
  4348. key.offset += num_bytes;
  4349. goto skip;
  4350. }
  4351. if (ordered)
  4352. btrfs_put_ordered_extent(ordered);
  4353. extent_locked = 1;
  4354. continue;
  4355. }
  4356. if (nr_extents == 1) {
  4357. /* update extent pointer in place */
  4358. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4359. new_extents[0].disk_bytenr);
  4360. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4361. new_extents[0].disk_num_bytes);
  4362. btrfs_mark_buffer_dirty(leaf);
  4363. btrfs_drop_extent_cache(inode, key.offset,
  4364. key.offset + num_bytes - 1, 0);
  4365. ret = btrfs_inc_extent_ref(trans, root,
  4366. new_extents[0].disk_bytenr,
  4367. new_extents[0].disk_num_bytes,
  4368. leaf->start,
  4369. root->root_key.objectid,
  4370. trans->transid,
  4371. key.objectid);
  4372. BUG_ON(ret);
  4373. ret = btrfs_free_extent(trans, root,
  4374. extent_key->objectid,
  4375. extent_key->offset,
  4376. leaf->start,
  4377. btrfs_header_owner(leaf),
  4378. btrfs_header_generation(leaf),
  4379. key.objectid, 0);
  4380. BUG_ON(ret);
  4381. btrfs_release_path(root, path);
  4382. key.offset += num_bytes;
  4383. } else {
  4384. BUG_ON(1);
  4385. #if 0
  4386. u64 alloc_hint;
  4387. u64 extent_len;
  4388. int i;
  4389. /*
  4390. * drop old extent pointer at first, then insert the
  4391. * new pointers one bye one
  4392. */
  4393. btrfs_release_path(root, path);
  4394. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  4395. key.offset + num_bytes,
  4396. key.offset, &alloc_hint);
  4397. BUG_ON(ret);
  4398. for (i = 0; i < nr_extents; i++) {
  4399. if (ext_offset >= new_extents[i].num_bytes) {
  4400. ext_offset -= new_extents[i].num_bytes;
  4401. continue;
  4402. }
  4403. extent_len = min(new_extents[i].num_bytes -
  4404. ext_offset, num_bytes);
  4405. ret = btrfs_insert_empty_item(trans, root,
  4406. path, &key,
  4407. sizeof(*fi));
  4408. BUG_ON(ret);
  4409. leaf = path->nodes[0];
  4410. fi = btrfs_item_ptr(leaf, path->slots[0],
  4411. struct btrfs_file_extent_item);
  4412. btrfs_set_file_extent_generation(leaf, fi,
  4413. trans->transid);
  4414. btrfs_set_file_extent_type(leaf, fi,
  4415. BTRFS_FILE_EXTENT_REG);
  4416. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4417. new_extents[i].disk_bytenr);
  4418. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4419. new_extents[i].disk_num_bytes);
  4420. btrfs_set_file_extent_ram_bytes(leaf, fi,
  4421. new_extents[i].ram_bytes);
  4422. btrfs_set_file_extent_compression(leaf, fi,
  4423. new_extents[i].compression);
  4424. btrfs_set_file_extent_encryption(leaf, fi,
  4425. new_extents[i].encryption);
  4426. btrfs_set_file_extent_other_encoding(leaf, fi,
  4427. new_extents[i].other_encoding);
  4428. btrfs_set_file_extent_num_bytes(leaf, fi,
  4429. extent_len);
  4430. ext_offset += new_extents[i].offset;
  4431. btrfs_set_file_extent_offset(leaf, fi,
  4432. ext_offset);
  4433. btrfs_mark_buffer_dirty(leaf);
  4434. btrfs_drop_extent_cache(inode, key.offset,
  4435. key.offset + extent_len - 1, 0);
  4436. ret = btrfs_inc_extent_ref(trans, root,
  4437. new_extents[i].disk_bytenr,
  4438. new_extents[i].disk_num_bytes,
  4439. leaf->start,
  4440. root->root_key.objectid,
  4441. trans->transid, key.objectid);
  4442. BUG_ON(ret);
  4443. btrfs_release_path(root, path);
  4444. inode_add_bytes(inode, extent_len);
  4445. ext_offset = 0;
  4446. num_bytes -= extent_len;
  4447. key.offset += extent_len;
  4448. if (num_bytes == 0)
  4449. break;
  4450. }
  4451. BUG_ON(i >= nr_extents);
  4452. #endif
  4453. }
  4454. if (extent_locked) {
  4455. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4456. lock_end, GFP_NOFS);
  4457. extent_locked = 0;
  4458. }
  4459. skip:
  4460. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  4461. key.offset >= search_end)
  4462. break;
  4463. cond_resched();
  4464. }
  4465. ret = 0;
  4466. out:
  4467. btrfs_release_path(root, path);
  4468. if (inode) {
  4469. mutex_unlock(&inode->i_mutex);
  4470. if (extent_locked) {
  4471. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4472. lock_end, GFP_NOFS);
  4473. }
  4474. iput(inode);
  4475. }
  4476. return ret;
  4477. }
  4478. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  4479. struct btrfs_root *root,
  4480. struct extent_buffer *buf, u64 orig_start)
  4481. {
  4482. int level;
  4483. int ret;
  4484. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  4485. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  4486. level = btrfs_header_level(buf);
  4487. if (level == 0) {
  4488. struct btrfs_leaf_ref *ref;
  4489. struct btrfs_leaf_ref *orig_ref;
  4490. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  4491. if (!orig_ref)
  4492. return -ENOENT;
  4493. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  4494. if (!ref) {
  4495. btrfs_free_leaf_ref(root, orig_ref);
  4496. return -ENOMEM;
  4497. }
  4498. ref->nritems = orig_ref->nritems;
  4499. memcpy(ref->extents, orig_ref->extents,
  4500. sizeof(ref->extents[0]) * ref->nritems);
  4501. btrfs_free_leaf_ref(root, orig_ref);
  4502. ref->root_gen = trans->transid;
  4503. ref->bytenr = buf->start;
  4504. ref->owner = btrfs_header_owner(buf);
  4505. ref->generation = btrfs_header_generation(buf);
  4506. ret = btrfs_add_leaf_ref(root, ref, 0);
  4507. WARN_ON(ret);
  4508. btrfs_free_leaf_ref(root, ref);
  4509. }
  4510. return 0;
  4511. }
  4512. static noinline int invalidate_extent_cache(struct btrfs_root *root,
  4513. struct extent_buffer *leaf,
  4514. struct btrfs_block_group_cache *group,
  4515. struct btrfs_root *target_root)
  4516. {
  4517. struct btrfs_key key;
  4518. struct inode *inode = NULL;
  4519. struct btrfs_file_extent_item *fi;
  4520. u64 num_bytes;
  4521. u64 skip_objectid = 0;
  4522. u32 nritems;
  4523. u32 i;
  4524. nritems = btrfs_header_nritems(leaf);
  4525. for (i = 0; i < nritems; i++) {
  4526. btrfs_item_key_to_cpu(leaf, &key, i);
  4527. if (key.objectid == skip_objectid ||
  4528. key.type != BTRFS_EXTENT_DATA_KEY)
  4529. continue;
  4530. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4531. if (btrfs_file_extent_type(leaf, fi) ==
  4532. BTRFS_FILE_EXTENT_INLINE)
  4533. continue;
  4534. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  4535. continue;
  4536. if (!inode || inode->i_ino != key.objectid) {
  4537. iput(inode);
  4538. inode = btrfs_ilookup(target_root->fs_info->sb,
  4539. key.objectid, target_root, 1);
  4540. }
  4541. if (!inode) {
  4542. skip_objectid = key.objectid;
  4543. continue;
  4544. }
  4545. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4546. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4547. key.offset + num_bytes - 1, GFP_NOFS);
  4548. btrfs_drop_extent_cache(inode, key.offset,
  4549. key.offset + num_bytes - 1, 1);
  4550. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4551. key.offset + num_bytes - 1, GFP_NOFS);
  4552. cond_resched();
  4553. }
  4554. iput(inode);
  4555. return 0;
  4556. }
  4557. static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  4558. struct btrfs_root *root,
  4559. struct extent_buffer *leaf,
  4560. struct btrfs_block_group_cache *group,
  4561. struct inode *reloc_inode)
  4562. {
  4563. struct btrfs_key key;
  4564. struct btrfs_key extent_key;
  4565. struct btrfs_file_extent_item *fi;
  4566. struct btrfs_leaf_ref *ref;
  4567. struct disk_extent *new_extent;
  4568. u64 bytenr;
  4569. u64 num_bytes;
  4570. u32 nritems;
  4571. u32 i;
  4572. int ext_index;
  4573. int nr_extent;
  4574. int ret;
  4575. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  4576. BUG_ON(!new_extent);
  4577. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  4578. BUG_ON(!ref);
  4579. ext_index = -1;
  4580. nritems = btrfs_header_nritems(leaf);
  4581. for (i = 0; i < nritems; i++) {
  4582. btrfs_item_key_to_cpu(leaf, &key, i);
  4583. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  4584. continue;
  4585. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4586. if (btrfs_file_extent_type(leaf, fi) ==
  4587. BTRFS_FILE_EXTENT_INLINE)
  4588. continue;
  4589. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4590. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  4591. if (bytenr == 0)
  4592. continue;
  4593. ext_index++;
  4594. if (bytenr >= group->key.objectid + group->key.offset ||
  4595. bytenr + num_bytes <= group->key.objectid)
  4596. continue;
  4597. extent_key.objectid = bytenr;
  4598. extent_key.offset = num_bytes;
  4599. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  4600. nr_extent = 1;
  4601. ret = get_new_locations(reloc_inode, &extent_key,
  4602. group->key.objectid, 1,
  4603. &new_extent, &nr_extent);
  4604. if (ret > 0)
  4605. continue;
  4606. BUG_ON(ret < 0);
  4607. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  4608. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  4609. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  4610. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  4611. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4612. new_extent->disk_bytenr);
  4613. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4614. new_extent->disk_num_bytes);
  4615. btrfs_mark_buffer_dirty(leaf);
  4616. ret = btrfs_inc_extent_ref(trans, root,
  4617. new_extent->disk_bytenr,
  4618. new_extent->disk_num_bytes,
  4619. leaf->start,
  4620. root->root_key.objectid,
  4621. trans->transid, key.objectid);
  4622. BUG_ON(ret);
  4623. ret = btrfs_free_extent(trans, root,
  4624. bytenr, num_bytes, leaf->start,
  4625. btrfs_header_owner(leaf),
  4626. btrfs_header_generation(leaf),
  4627. key.objectid, 0);
  4628. BUG_ON(ret);
  4629. cond_resched();
  4630. }
  4631. kfree(new_extent);
  4632. BUG_ON(ext_index + 1 != ref->nritems);
  4633. btrfs_free_leaf_ref(root, ref);
  4634. return 0;
  4635. }
  4636. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  4637. struct btrfs_root *root)
  4638. {
  4639. struct btrfs_root *reloc_root;
  4640. int ret;
  4641. if (root->reloc_root) {
  4642. reloc_root = root->reloc_root;
  4643. root->reloc_root = NULL;
  4644. list_add(&reloc_root->dead_list,
  4645. &root->fs_info->dead_reloc_roots);
  4646. btrfs_set_root_bytenr(&reloc_root->root_item,
  4647. reloc_root->node->start);
  4648. btrfs_set_root_level(&root->root_item,
  4649. btrfs_header_level(reloc_root->node));
  4650. memset(&reloc_root->root_item.drop_progress, 0,
  4651. sizeof(struct btrfs_disk_key));
  4652. reloc_root->root_item.drop_level = 0;
  4653. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  4654. &reloc_root->root_key,
  4655. &reloc_root->root_item);
  4656. BUG_ON(ret);
  4657. }
  4658. return 0;
  4659. }
  4660. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  4661. {
  4662. struct btrfs_trans_handle *trans;
  4663. struct btrfs_root *reloc_root;
  4664. struct btrfs_root *prev_root = NULL;
  4665. struct list_head dead_roots;
  4666. int ret;
  4667. unsigned long nr;
  4668. INIT_LIST_HEAD(&dead_roots);
  4669. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  4670. while (!list_empty(&dead_roots)) {
  4671. reloc_root = list_entry(dead_roots.prev,
  4672. struct btrfs_root, dead_list);
  4673. list_del_init(&reloc_root->dead_list);
  4674. BUG_ON(reloc_root->commit_root != NULL);
  4675. while (1) {
  4676. trans = btrfs_join_transaction(root, 1);
  4677. BUG_ON(!trans);
  4678. mutex_lock(&root->fs_info->drop_mutex);
  4679. ret = btrfs_drop_snapshot(trans, reloc_root);
  4680. if (ret != -EAGAIN)
  4681. break;
  4682. mutex_unlock(&root->fs_info->drop_mutex);
  4683. nr = trans->blocks_used;
  4684. ret = btrfs_end_transaction(trans, root);
  4685. BUG_ON(ret);
  4686. btrfs_btree_balance_dirty(root, nr);
  4687. }
  4688. free_extent_buffer(reloc_root->node);
  4689. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  4690. &reloc_root->root_key);
  4691. BUG_ON(ret);
  4692. mutex_unlock(&root->fs_info->drop_mutex);
  4693. nr = trans->blocks_used;
  4694. ret = btrfs_end_transaction(trans, root);
  4695. BUG_ON(ret);
  4696. btrfs_btree_balance_dirty(root, nr);
  4697. kfree(prev_root);
  4698. prev_root = reloc_root;
  4699. }
  4700. if (prev_root) {
  4701. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  4702. kfree(prev_root);
  4703. }
  4704. return 0;
  4705. }
  4706. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  4707. {
  4708. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  4709. return 0;
  4710. }
  4711. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  4712. {
  4713. struct btrfs_root *reloc_root;
  4714. struct btrfs_trans_handle *trans;
  4715. struct btrfs_key location;
  4716. int found;
  4717. int ret;
  4718. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4719. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  4720. BUG_ON(ret);
  4721. found = !list_empty(&root->fs_info->dead_reloc_roots);
  4722. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4723. if (found) {
  4724. trans = btrfs_start_transaction(root, 1);
  4725. BUG_ON(!trans);
  4726. ret = btrfs_commit_transaction(trans, root);
  4727. BUG_ON(ret);
  4728. }
  4729. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4730. location.offset = (u64)-1;
  4731. location.type = BTRFS_ROOT_ITEM_KEY;
  4732. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  4733. BUG_ON(!reloc_root);
  4734. btrfs_orphan_cleanup(reloc_root);
  4735. return 0;
  4736. }
  4737. static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
  4738. struct btrfs_root *root)
  4739. {
  4740. struct btrfs_root *reloc_root;
  4741. struct extent_buffer *eb;
  4742. struct btrfs_root_item *root_item;
  4743. struct btrfs_key root_key;
  4744. int ret;
  4745. BUG_ON(!root->ref_cows);
  4746. if (root->reloc_root)
  4747. return 0;
  4748. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  4749. BUG_ON(!root_item);
  4750. ret = btrfs_copy_root(trans, root, root->commit_root,
  4751. &eb, BTRFS_TREE_RELOC_OBJECTID);
  4752. BUG_ON(ret);
  4753. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  4754. root_key.offset = root->root_key.objectid;
  4755. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4756. memcpy(root_item, &root->root_item, sizeof(root_item));
  4757. btrfs_set_root_refs(root_item, 0);
  4758. btrfs_set_root_bytenr(root_item, eb->start);
  4759. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  4760. btrfs_set_root_generation(root_item, trans->transid);
  4761. btrfs_tree_unlock(eb);
  4762. free_extent_buffer(eb);
  4763. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  4764. &root_key, root_item);
  4765. BUG_ON(ret);
  4766. kfree(root_item);
  4767. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  4768. &root_key);
  4769. BUG_ON(!reloc_root);
  4770. reloc_root->last_trans = trans->transid;
  4771. reloc_root->commit_root = NULL;
  4772. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  4773. root->reloc_root = reloc_root;
  4774. return 0;
  4775. }
  4776. /*
  4777. * Core function of space balance.
  4778. *
  4779. * The idea is using reloc trees to relocate tree blocks in reference
  4780. * counted roots. There is one reloc tree for each subvol, and all
  4781. * reloc trees share same root key objectid. Reloc trees are snapshots
  4782. * of the latest committed roots of subvols (root->commit_root).
  4783. *
  4784. * To relocate a tree block referenced by a subvol, there are two steps.
  4785. * COW the block through subvol's reloc tree, then update block pointer
  4786. * in the subvol to point to the new block. Since all reloc trees share
  4787. * same root key objectid, doing special handing for tree blocks owned
  4788. * by them is easy. Once a tree block has been COWed in one reloc tree,
  4789. * we can use the resulting new block directly when the same block is
  4790. * required to COW again through other reloc trees. By this way, relocated
  4791. * tree blocks are shared between reloc trees, so they are also shared
  4792. * between subvols.
  4793. */
  4794. static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
  4795. struct btrfs_root *root,
  4796. struct btrfs_path *path,
  4797. struct btrfs_key *first_key,
  4798. struct btrfs_ref_path *ref_path,
  4799. struct btrfs_block_group_cache *group,
  4800. struct inode *reloc_inode)
  4801. {
  4802. struct btrfs_root *reloc_root;
  4803. struct extent_buffer *eb = NULL;
  4804. struct btrfs_key *keys;
  4805. u64 *nodes;
  4806. int level;
  4807. int shared_level;
  4808. int lowest_level = 0;
  4809. int ret;
  4810. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  4811. lowest_level = ref_path->owner_objectid;
  4812. if (!root->ref_cows) {
  4813. path->lowest_level = lowest_level;
  4814. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  4815. BUG_ON(ret < 0);
  4816. path->lowest_level = 0;
  4817. btrfs_release_path(root, path);
  4818. return 0;
  4819. }
  4820. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4821. ret = init_reloc_tree(trans, root);
  4822. BUG_ON(ret);
  4823. reloc_root = root->reloc_root;
  4824. shared_level = ref_path->shared_level;
  4825. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  4826. keys = ref_path->node_keys;
  4827. nodes = ref_path->new_nodes;
  4828. memset(&keys[shared_level + 1], 0,
  4829. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4830. memset(&nodes[shared_level + 1], 0,
  4831. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4832. if (nodes[lowest_level] == 0) {
  4833. path->lowest_level = lowest_level;
  4834. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4835. 0, 1);
  4836. BUG_ON(ret);
  4837. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  4838. eb = path->nodes[level];
  4839. if (!eb || eb == reloc_root->node)
  4840. break;
  4841. nodes[level] = eb->start;
  4842. if (level == 0)
  4843. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  4844. else
  4845. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  4846. }
  4847. if (nodes[0] &&
  4848. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4849. eb = path->nodes[0];
  4850. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  4851. group, reloc_inode);
  4852. BUG_ON(ret);
  4853. }
  4854. btrfs_release_path(reloc_root, path);
  4855. } else {
  4856. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  4857. lowest_level);
  4858. BUG_ON(ret);
  4859. }
  4860. /*
  4861. * replace tree blocks in the fs tree with tree blocks in
  4862. * the reloc tree.
  4863. */
  4864. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  4865. BUG_ON(ret < 0);
  4866. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4867. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4868. 0, 0);
  4869. BUG_ON(ret);
  4870. extent_buffer_get(path->nodes[0]);
  4871. eb = path->nodes[0];
  4872. btrfs_release_path(reloc_root, path);
  4873. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  4874. BUG_ON(ret);
  4875. free_extent_buffer(eb);
  4876. }
  4877. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4878. path->lowest_level = 0;
  4879. return 0;
  4880. }
  4881. static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
  4882. struct btrfs_root *root,
  4883. struct btrfs_path *path,
  4884. struct btrfs_key *first_key,
  4885. struct btrfs_ref_path *ref_path)
  4886. {
  4887. int ret;
  4888. ret = relocate_one_path(trans, root, path, first_key,
  4889. ref_path, NULL, NULL);
  4890. BUG_ON(ret);
  4891. if (root == root->fs_info->extent_root)
  4892. btrfs_extent_post_op(trans, root);
  4893. return 0;
  4894. }
  4895. static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
  4896. struct btrfs_root *extent_root,
  4897. struct btrfs_path *path,
  4898. struct btrfs_key *extent_key)
  4899. {
  4900. int ret;
  4901. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  4902. if (ret)
  4903. goto out;
  4904. ret = btrfs_del_item(trans, extent_root, path);
  4905. out:
  4906. btrfs_release_path(extent_root, path);
  4907. return ret;
  4908. }
  4909. static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
  4910. struct btrfs_ref_path *ref_path)
  4911. {
  4912. struct btrfs_key root_key;
  4913. root_key.objectid = ref_path->root_objectid;
  4914. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4915. if (is_cowonly_root(ref_path->root_objectid))
  4916. root_key.offset = 0;
  4917. else
  4918. root_key.offset = (u64)-1;
  4919. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  4920. }
  4921. static noinline int relocate_one_extent(struct btrfs_root *extent_root,
  4922. struct btrfs_path *path,
  4923. struct btrfs_key *extent_key,
  4924. struct btrfs_block_group_cache *group,
  4925. struct inode *reloc_inode, int pass)
  4926. {
  4927. struct btrfs_trans_handle *trans;
  4928. struct btrfs_root *found_root;
  4929. struct btrfs_ref_path *ref_path = NULL;
  4930. struct disk_extent *new_extents = NULL;
  4931. int nr_extents = 0;
  4932. int loops;
  4933. int ret;
  4934. int level;
  4935. struct btrfs_key first_key;
  4936. u64 prev_block = 0;
  4937. trans = btrfs_start_transaction(extent_root, 1);
  4938. BUG_ON(!trans);
  4939. if (extent_key->objectid == 0) {
  4940. ret = del_extent_zero(trans, extent_root, path, extent_key);
  4941. goto out;
  4942. }
  4943. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  4944. if (!ref_path) {
  4945. ret = -ENOMEM;
  4946. goto out;
  4947. }
  4948. for (loops = 0; ; loops++) {
  4949. if (loops == 0) {
  4950. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  4951. extent_key->objectid);
  4952. } else {
  4953. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  4954. }
  4955. if (ret < 0)
  4956. goto out;
  4957. if (ret > 0)
  4958. break;
  4959. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  4960. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  4961. continue;
  4962. found_root = read_ref_root(extent_root->fs_info, ref_path);
  4963. BUG_ON(!found_root);
  4964. /*
  4965. * for reference counted tree, only process reference paths
  4966. * rooted at the latest committed root.
  4967. */
  4968. if (found_root->ref_cows &&
  4969. ref_path->root_generation != found_root->root_key.offset)
  4970. continue;
  4971. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4972. if (pass == 0) {
  4973. /*
  4974. * copy data extents to new locations
  4975. */
  4976. u64 group_start = group->key.objectid;
  4977. ret = relocate_data_extent(reloc_inode,
  4978. extent_key,
  4979. group_start);
  4980. if (ret < 0)
  4981. goto out;
  4982. break;
  4983. }
  4984. level = 0;
  4985. } else {
  4986. level = ref_path->owner_objectid;
  4987. }
  4988. if (prev_block != ref_path->nodes[level]) {
  4989. struct extent_buffer *eb;
  4990. u64 block_start = ref_path->nodes[level];
  4991. u64 block_size = btrfs_level_size(found_root, level);
  4992. eb = read_tree_block(found_root, block_start,
  4993. block_size, 0);
  4994. btrfs_tree_lock(eb);
  4995. BUG_ON(level != btrfs_header_level(eb));
  4996. if (level == 0)
  4997. btrfs_item_key_to_cpu(eb, &first_key, 0);
  4998. else
  4999. btrfs_node_key_to_cpu(eb, &first_key, 0);
  5000. btrfs_tree_unlock(eb);
  5001. free_extent_buffer(eb);
  5002. prev_block = block_start;
  5003. }
  5004. mutex_lock(&extent_root->fs_info->trans_mutex);
  5005. btrfs_record_root_in_trans(found_root);
  5006. mutex_unlock(&extent_root->fs_info->trans_mutex);
  5007. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  5008. /*
  5009. * try to update data extent references while
  5010. * keeping metadata shared between snapshots.
  5011. */
  5012. if (pass == 1) {
  5013. ret = relocate_one_path(trans, found_root,
  5014. path, &first_key, ref_path,
  5015. group, reloc_inode);
  5016. if (ret < 0)
  5017. goto out;
  5018. continue;
  5019. }
  5020. /*
  5021. * use fallback method to process the remaining
  5022. * references.
  5023. */
  5024. if (!new_extents) {
  5025. u64 group_start = group->key.objectid;
  5026. new_extents = kmalloc(sizeof(*new_extents),
  5027. GFP_NOFS);
  5028. nr_extents = 1;
  5029. ret = get_new_locations(reloc_inode,
  5030. extent_key,
  5031. group_start, 1,
  5032. &new_extents,
  5033. &nr_extents);
  5034. if (ret)
  5035. goto out;
  5036. }
  5037. ret = replace_one_extent(trans, found_root,
  5038. path, extent_key,
  5039. &first_key, ref_path,
  5040. new_extents, nr_extents);
  5041. } else {
  5042. ret = relocate_tree_block(trans, found_root, path,
  5043. &first_key, ref_path);
  5044. }
  5045. if (ret < 0)
  5046. goto out;
  5047. }
  5048. ret = 0;
  5049. out:
  5050. btrfs_end_transaction(trans, extent_root);
  5051. kfree(new_extents);
  5052. kfree(ref_path);
  5053. return ret;
  5054. }
  5055. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  5056. {
  5057. u64 num_devices;
  5058. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  5059. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  5060. num_devices = root->fs_info->fs_devices->rw_devices;
  5061. if (num_devices == 1) {
  5062. stripped |= BTRFS_BLOCK_GROUP_DUP;
  5063. stripped = flags & ~stripped;
  5064. /* turn raid0 into single device chunks */
  5065. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5066. return stripped;
  5067. /* turn mirroring into duplication */
  5068. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  5069. BTRFS_BLOCK_GROUP_RAID10))
  5070. return stripped | BTRFS_BLOCK_GROUP_DUP;
  5071. return flags;
  5072. } else {
  5073. /* they already had raid on here, just return */
  5074. if (flags & stripped)
  5075. return flags;
  5076. stripped |= BTRFS_BLOCK_GROUP_DUP;
  5077. stripped = flags & ~stripped;
  5078. /* switch duplicated blocks with raid1 */
  5079. if (flags & BTRFS_BLOCK_GROUP_DUP)
  5080. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  5081. /* turn single device chunks into raid0 */
  5082. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  5083. }
  5084. return flags;
  5085. }
  5086. static int __alloc_chunk_for_shrink(struct btrfs_root *root,
  5087. struct btrfs_block_group_cache *shrink_block_group,
  5088. int force)
  5089. {
  5090. struct btrfs_trans_handle *trans;
  5091. u64 new_alloc_flags;
  5092. u64 calc;
  5093. spin_lock(&shrink_block_group->lock);
  5094. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  5095. spin_unlock(&shrink_block_group->lock);
  5096. trans = btrfs_start_transaction(root, 1);
  5097. spin_lock(&shrink_block_group->lock);
  5098. new_alloc_flags = update_block_group_flags(root,
  5099. shrink_block_group->flags);
  5100. if (new_alloc_flags != shrink_block_group->flags) {
  5101. calc =
  5102. btrfs_block_group_used(&shrink_block_group->item);
  5103. } else {
  5104. calc = shrink_block_group->key.offset;
  5105. }
  5106. spin_unlock(&shrink_block_group->lock);
  5107. do_chunk_alloc(trans, root->fs_info->extent_root,
  5108. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  5109. btrfs_end_transaction(trans, root);
  5110. } else
  5111. spin_unlock(&shrink_block_group->lock);
  5112. return 0;
  5113. }
  5114. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  5115. struct btrfs_root *root,
  5116. u64 objectid, u64 size)
  5117. {
  5118. struct btrfs_path *path;
  5119. struct btrfs_inode_item *item;
  5120. struct extent_buffer *leaf;
  5121. int ret;
  5122. path = btrfs_alloc_path();
  5123. if (!path)
  5124. return -ENOMEM;
  5125. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  5126. if (ret)
  5127. goto out;
  5128. leaf = path->nodes[0];
  5129. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  5130. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  5131. btrfs_set_inode_generation(leaf, item, 1);
  5132. btrfs_set_inode_size(leaf, item, size);
  5133. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  5134. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
  5135. btrfs_mark_buffer_dirty(leaf);
  5136. btrfs_release_path(root, path);
  5137. out:
  5138. btrfs_free_path(path);
  5139. return ret;
  5140. }
  5141. static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
  5142. struct btrfs_block_group_cache *group)
  5143. {
  5144. struct inode *inode = NULL;
  5145. struct btrfs_trans_handle *trans;
  5146. struct btrfs_root *root;
  5147. struct btrfs_key root_key;
  5148. u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
  5149. int err = 0;
  5150. root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  5151. root_key.type = BTRFS_ROOT_ITEM_KEY;
  5152. root_key.offset = (u64)-1;
  5153. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  5154. if (IS_ERR(root))
  5155. return ERR_CAST(root);
  5156. trans = btrfs_start_transaction(root, 1);
  5157. BUG_ON(!trans);
  5158. err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
  5159. if (err)
  5160. goto out;
  5161. err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
  5162. BUG_ON(err);
  5163. err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
  5164. group->key.offset, 0, group->key.offset,
  5165. 0, 0, 0);
  5166. BUG_ON(err);
  5167. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  5168. if (inode->i_state & I_NEW) {
  5169. BTRFS_I(inode)->root = root;
  5170. BTRFS_I(inode)->location.objectid = objectid;
  5171. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  5172. BTRFS_I(inode)->location.offset = 0;
  5173. btrfs_read_locked_inode(inode);
  5174. unlock_new_inode(inode);
  5175. BUG_ON(is_bad_inode(inode));
  5176. } else {
  5177. BUG_ON(1);
  5178. }
  5179. BTRFS_I(inode)->index_cnt = group->key.objectid;
  5180. err = btrfs_orphan_add(trans, inode);
  5181. out:
  5182. btrfs_end_transaction(trans, root);
  5183. if (err) {
  5184. if (inode)
  5185. iput(inode);
  5186. inode = ERR_PTR(err);
  5187. }
  5188. return inode;
  5189. }
  5190. int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
  5191. {
  5192. struct btrfs_ordered_sum *sums;
  5193. struct btrfs_sector_sum *sector_sum;
  5194. struct btrfs_ordered_extent *ordered;
  5195. struct btrfs_root *root = BTRFS_I(inode)->root;
  5196. struct list_head list;
  5197. size_t offset;
  5198. int ret;
  5199. u64 disk_bytenr;
  5200. INIT_LIST_HEAD(&list);
  5201. ordered = btrfs_lookup_ordered_extent(inode, file_pos);
  5202. BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
  5203. disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
  5204. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
  5205. disk_bytenr + len - 1, &list);
  5206. while (!list_empty(&list)) {
  5207. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  5208. list_del_init(&sums->list);
  5209. sector_sum = sums->sums;
  5210. sums->bytenr = ordered->start;
  5211. offset = 0;
  5212. while (offset < sums->len) {
  5213. sector_sum->bytenr += ordered->start - disk_bytenr;
  5214. sector_sum++;
  5215. offset += root->sectorsize;
  5216. }
  5217. btrfs_add_ordered_sum(inode, ordered, sums);
  5218. }
  5219. btrfs_put_ordered_extent(ordered);
  5220. return 0;
  5221. }
  5222. int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
  5223. {
  5224. struct btrfs_trans_handle *trans;
  5225. struct btrfs_path *path;
  5226. struct btrfs_fs_info *info = root->fs_info;
  5227. struct extent_buffer *leaf;
  5228. struct inode *reloc_inode;
  5229. struct btrfs_block_group_cache *block_group;
  5230. struct btrfs_key key;
  5231. u64 skipped;
  5232. u64 cur_byte;
  5233. u64 total_found;
  5234. u32 nritems;
  5235. int ret;
  5236. int progress;
  5237. int pass = 0;
  5238. root = root->fs_info->extent_root;
  5239. block_group = btrfs_lookup_block_group(info, group_start);
  5240. BUG_ON(!block_group);
  5241. printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
  5242. (unsigned long long)block_group->key.objectid,
  5243. (unsigned long long)block_group->flags);
  5244. path = btrfs_alloc_path();
  5245. BUG_ON(!path);
  5246. reloc_inode = create_reloc_inode(info, block_group);
  5247. BUG_ON(IS_ERR(reloc_inode));
  5248. __alloc_chunk_for_shrink(root, block_group, 1);
  5249. set_block_group_readonly(block_group);
  5250. btrfs_start_delalloc_inodes(info->tree_root);
  5251. btrfs_wait_ordered_extents(info->tree_root, 0);
  5252. again:
  5253. skipped = 0;
  5254. total_found = 0;
  5255. progress = 0;
  5256. key.objectid = block_group->key.objectid;
  5257. key.offset = 0;
  5258. key.type = 0;
  5259. cur_byte = key.objectid;
  5260. trans = btrfs_start_transaction(info->tree_root, 1);
  5261. btrfs_commit_transaction(trans, info->tree_root);
  5262. mutex_lock(&root->fs_info->cleaner_mutex);
  5263. btrfs_clean_old_snapshots(info->tree_root);
  5264. btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
  5265. mutex_unlock(&root->fs_info->cleaner_mutex);
  5266. while (1) {
  5267. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5268. if (ret < 0)
  5269. goto out;
  5270. next:
  5271. leaf = path->nodes[0];
  5272. nritems = btrfs_header_nritems(leaf);
  5273. if (path->slots[0] >= nritems) {
  5274. ret = btrfs_next_leaf(root, path);
  5275. if (ret < 0)
  5276. goto out;
  5277. if (ret == 1) {
  5278. ret = 0;
  5279. break;
  5280. }
  5281. leaf = path->nodes[0];
  5282. nritems = btrfs_header_nritems(leaf);
  5283. }
  5284. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  5285. if (key.objectid >= block_group->key.objectid +
  5286. block_group->key.offset)
  5287. break;
  5288. if (progress && need_resched()) {
  5289. btrfs_release_path(root, path);
  5290. cond_resched();
  5291. progress = 0;
  5292. continue;
  5293. }
  5294. progress = 1;
  5295. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
  5296. key.objectid + key.offset <= cur_byte) {
  5297. path->slots[0]++;
  5298. goto next;
  5299. }
  5300. total_found++;
  5301. cur_byte = key.objectid + key.offset;
  5302. btrfs_release_path(root, path);
  5303. __alloc_chunk_for_shrink(root, block_group, 0);
  5304. ret = relocate_one_extent(root, path, &key, block_group,
  5305. reloc_inode, pass);
  5306. BUG_ON(ret < 0);
  5307. if (ret > 0)
  5308. skipped++;
  5309. key.objectid = cur_byte;
  5310. key.type = 0;
  5311. key.offset = 0;
  5312. }
  5313. btrfs_release_path(root, path);
  5314. if (pass == 0) {
  5315. btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
  5316. invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
  5317. }
  5318. if (total_found > 0) {
  5319. printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
  5320. (unsigned long long)total_found, pass);
  5321. pass++;
  5322. if (total_found == skipped && pass > 2) {
  5323. iput(reloc_inode);
  5324. reloc_inode = create_reloc_inode(info, block_group);
  5325. pass = 0;
  5326. }
  5327. goto again;
  5328. }
  5329. /* delete reloc_inode */
  5330. iput(reloc_inode);
  5331. /* unpin extents in this range */
  5332. trans = btrfs_start_transaction(info->tree_root, 1);
  5333. btrfs_commit_transaction(trans, info->tree_root);
  5334. spin_lock(&block_group->lock);
  5335. WARN_ON(block_group->pinned > 0);
  5336. WARN_ON(block_group->reserved > 0);
  5337. WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
  5338. spin_unlock(&block_group->lock);
  5339. put_block_group(block_group);
  5340. ret = 0;
  5341. out:
  5342. btrfs_free_path(path);
  5343. return ret;
  5344. }
  5345. static int find_first_block_group(struct btrfs_root *root,
  5346. struct btrfs_path *path, struct btrfs_key *key)
  5347. {
  5348. int ret = 0;
  5349. struct btrfs_key found_key;
  5350. struct extent_buffer *leaf;
  5351. int slot;
  5352. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  5353. if (ret < 0)
  5354. goto out;
  5355. while (1) {
  5356. slot = path->slots[0];
  5357. leaf = path->nodes[0];
  5358. if (slot >= btrfs_header_nritems(leaf)) {
  5359. ret = btrfs_next_leaf(root, path);
  5360. if (ret == 0)
  5361. continue;
  5362. if (ret < 0)
  5363. goto out;
  5364. break;
  5365. }
  5366. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5367. if (found_key.objectid >= key->objectid &&
  5368. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  5369. ret = 0;
  5370. goto out;
  5371. }
  5372. path->slots[0]++;
  5373. }
  5374. ret = -ENOENT;
  5375. out:
  5376. return ret;
  5377. }
  5378. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  5379. {
  5380. struct btrfs_block_group_cache *block_group;
  5381. struct rb_node *n;
  5382. spin_lock(&info->block_group_cache_lock);
  5383. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  5384. block_group = rb_entry(n, struct btrfs_block_group_cache,
  5385. cache_node);
  5386. rb_erase(&block_group->cache_node,
  5387. &info->block_group_cache_tree);
  5388. spin_unlock(&info->block_group_cache_lock);
  5389. btrfs_remove_free_space_cache(block_group);
  5390. down_write(&block_group->space_info->groups_sem);
  5391. list_del(&block_group->list);
  5392. up_write(&block_group->space_info->groups_sem);
  5393. WARN_ON(atomic_read(&block_group->count) != 1);
  5394. kfree(block_group);
  5395. spin_lock(&info->block_group_cache_lock);
  5396. }
  5397. spin_unlock(&info->block_group_cache_lock);
  5398. return 0;
  5399. }
  5400. int btrfs_read_block_groups(struct btrfs_root *root)
  5401. {
  5402. struct btrfs_path *path;
  5403. int ret;
  5404. struct btrfs_block_group_cache *cache;
  5405. struct btrfs_fs_info *info = root->fs_info;
  5406. struct btrfs_space_info *space_info;
  5407. struct btrfs_key key;
  5408. struct btrfs_key found_key;
  5409. struct extent_buffer *leaf;
  5410. root = info->extent_root;
  5411. key.objectid = 0;
  5412. key.offset = 0;
  5413. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  5414. path = btrfs_alloc_path();
  5415. if (!path)
  5416. return -ENOMEM;
  5417. while (1) {
  5418. ret = find_first_block_group(root, path, &key);
  5419. if (ret > 0) {
  5420. ret = 0;
  5421. goto error;
  5422. }
  5423. if (ret != 0)
  5424. goto error;
  5425. leaf = path->nodes[0];
  5426. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5427. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5428. if (!cache) {
  5429. ret = -ENOMEM;
  5430. break;
  5431. }
  5432. atomic_set(&cache->count, 1);
  5433. spin_lock_init(&cache->lock);
  5434. mutex_init(&cache->alloc_mutex);
  5435. mutex_init(&cache->cache_mutex);
  5436. INIT_LIST_HEAD(&cache->list);
  5437. read_extent_buffer(leaf, &cache->item,
  5438. btrfs_item_ptr_offset(leaf, path->slots[0]),
  5439. sizeof(cache->item));
  5440. memcpy(&cache->key, &found_key, sizeof(found_key));
  5441. key.objectid = found_key.objectid + found_key.offset;
  5442. btrfs_release_path(root, path);
  5443. cache->flags = btrfs_block_group_flags(&cache->item);
  5444. ret = update_space_info(info, cache->flags, found_key.offset,
  5445. btrfs_block_group_used(&cache->item),
  5446. &space_info);
  5447. BUG_ON(ret);
  5448. cache->space_info = space_info;
  5449. down_write(&space_info->groups_sem);
  5450. list_add_tail(&cache->list, &space_info->block_groups);
  5451. up_write(&space_info->groups_sem);
  5452. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5453. BUG_ON(ret);
  5454. set_avail_alloc_bits(root->fs_info, cache->flags);
  5455. if (btrfs_chunk_readonly(root, cache->key.objectid))
  5456. set_block_group_readonly(cache);
  5457. }
  5458. ret = 0;
  5459. error:
  5460. btrfs_free_path(path);
  5461. return ret;
  5462. }
  5463. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  5464. struct btrfs_root *root, u64 bytes_used,
  5465. u64 type, u64 chunk_objectid, u64 chunk_offset,
  5466. u64 size)
  5467. {
  5468. int ret;
  5469. struct btrfs_root *extent_root;
  5470. struct btrfs_block_group_cache *cache;
  5471. extent_root = root->fs_info->extent_root;
  5472. root->fs_info->last_trans_new_blockgroup = trans->transid;
  5473. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5474. if (!cache)
  5475. return -ENOMEM;
  5476. cache->key.objectid = chunk_offset;
  5477. cache->key.offset = size;
  5478. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  5479. atomic_set(&cache->count, 1);
  5480. spin_lock_init(&cache->lock);
  5481. mutex_init(&cache->alloc_mutex);
  5482. mutex_init(&cache->cache_mutex);
  5483. INIT_LIST_HEAD(&cache->list);
  5484. btrfs_set_block_group_used(&cache->item, bytes_used);
  5485. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  5486. cache->flags = type;
  5487. btrfs_set_block_group_flags(&cache->item, type);
  5488. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  5489. &cache->space_info);
  5490. BUG_ON(ret);
  5491. down_write(&cache->space_info->groups_sem);
  5492. list_add_tail(&cache->list, &cache->space_info->block_groups);
  5493. up_write(&cache->space_info->groups_sem);
  5494. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5495. BUG_ON(ret);
  5496. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  5497. sizeof(cache->item));
  5498. BUG_ON(ret);
  5499. finish_current_insert(trans, extent_root, 0);
  5500. ret = del_pending_extents(trans, extent_root, 0);
  5501. BUG_ON(ret);
  5502. set_avail_alloc_bits(extent_root->fs_info, type);
  5503. return 0;
  5504. }
  5505. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  5506. struct btrfs_root *root, u64 group_start)
  5507. {
  5508. struct btrfs_path *path;
  5509. struct btrfs_block_group_cache *block_group;
  5510. struct btrfs_key key;
  5511. int ret;
  5512. root = root->fs_info->extent_root;
  5513. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  5514. BUG_ON(!block_group);
  5515. BUG_ON(!block_group->ro);
  5516. memcpy(&key, &block_group->key, sizeof(key));
  5517. path = btrfs_alloc_path();
  5518. BUG_ON(!path);
  5519. spin_lock(&root->fs_info->block_group_cache_lock);
  5520. rb_erase(&block_group->cache_node,
  5521. &root->fs_info->block_group_cache_tree);
  5522. spin_unlock(&root->fs_info->block_group_cache_lock);
  5523. btrfs_remove_free_space_cache(block_group);
  5524. down_write(&block_group->space_info->groups_sem);
  5525. list_del(&block_group->list);
  5526. up_write(&block_group->space_info->groups_sem);
  5527. spin_lock(&block_group->space_info->lock);
  5528. block_group->space_info->total_bytes -= block_group->key.offset;
  5529. block_group->space_info->bytes_readonly -= block_group->key.offset;
  5530. spin_unlock(&block_group->space_info->lock);
  5531. block_group->space_info->full = 0;
  5532. put_block_group(block_group);
  5533. put_block_group(block_group);
  5534. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  5535. if (ret > 0)
  5536. ret = -EIO;
  5537. if (ret < 0)
  5538. goto out;
  5539. ret = btrfs_del_item(trans, root, path);
  5540. out:
  5541. btrfs_free_path(path);
  5542. return ret;
  5543. }