extent-tree.c 154 KB

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