extent-tree.c 170 KB

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