extent-tree.c 224 KB

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