extent-tree.c 229 KB

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