extent-tree.c 154 KB

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