extent-tree.c 169 KB

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