extent-tree.c 153 KB

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