extent-tree.c 194 KB

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