extent-tree.c 153 KB

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