extent-tree.c 154 KB

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