extent-tree.c 157 KB

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