extent-tree.c 209 KB

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