extent-tree.c 155 KB

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