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 = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK;
  2766. /* chunk -> extended profile */
  2767. if (extra_flags == 0)
  2768. extra_flags = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2769. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2770. fs_info->avail_data_alloc_bits |= extra_flags;
  2771. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2772. fs_info->avail_metadata_alloc_bits |= extra_flags;
  2773. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2774. fs_info->avail_system_alloc_bits |= extra_flags;
  2775. }
  2776. /*
  2777. * @flags: available profiles in extended format (see ctree.h)
  2778. *
  2779. * Returns reduced profile in chunk format. If profile changing is in
  2780. * progress (either running or paused) picks the target profile (if it's
  2781. * already available), otherwise falls back to plain reducing.
  2782. */
  2783. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  2784. {
  2785. /*
  2786. * we add in the count of missing devices because we want
  2787. * to make sure that any RAID levels on a degraded FS
  2788. * continue to be honored.
  2789. */
  2790. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  2791. root->fs_info->fs_devices->missing_devices;
  2792. /* pick restriper's target profile if it's available */
  2793. spin_lock(&root->fs_info->balance_lock);
  2794. if (root->fs_info->balance_ctl) {
  2795. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  2796. u64 tgt = 0;
  2797. if ((flags & BTRFS_BLOCK_GROUP_DATA) &&
  2798. (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2799. (flags & bctl->data.target)) {
  2800. tgt = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  2801. } else if ((flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
  2802. (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2803. (flags & bctl->sys.target)) {
  2804. tgt = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  2805. } else if ((flags & BTRFS_BLOCK_GROUP_METADATA) &&
  2806. (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2807. (flags & bctl->meta.target)) {
  2808. tgt = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  2809. }
  2810. if (tgt) {
  2811. spin_unlock(&root->fs_info->balance_lock);
  2812. flags = tgt;
  2813. goto out;
  2814. }
  2815. }
  2816. spin_unlock(&root->fs_info->balance_lock);
  2817. if (num_devices == 1)
  2818. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  2819. if (num_devices < 4)
  2820. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  2821. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  2822. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2823. BTRFS_BLOCK_GROUP_RAID10))) {
  2824. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  2825. }
  2826. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  2827. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  2828. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  2829. }
  2830. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  2831. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  2832. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  2833. (flags & BTRFS_BLOCK_GROUP_DUP))) {
  2834. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  2835. }
  2836. out:
  2837. /* extended -> chunk profile */
  2838. flags &= ~BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2839. return flags;
  2840. }
  2841. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  2842. {
  2843. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2844. flags |= root->fs_info->avail_data_alloc_bits;
  2845. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2846. flags |= root->fs_info->avail_system_alloc_bits;
  2847. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2848. flags |= root->fs_info->avail_metadata_alloc_bits;
  2849. return btrfs_reduce_alloc_profile(root, flags);
  2850. }
  2851. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  2852. {
  2853. u64 flags;
  2854. if (data)
  2855. flags = BTRFS_BLOCK_GROUP_DATA;
  2856. else if (root == root->fs_info->chunk_root)
  2857. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  2858. else
  2859. flags = BTRFS_BLOCK_GROUP_METADATA;
  2860. return get_alloc_profile(root, flags);
  2861. }
  2862. void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
  2863. {
  2864. BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
  2865. BTRFS_BLOCK_GROUP_DATA);
  2866. }
  2867. /*
  2868. * This will check the space that the inode allocates from to make sure we have
  2869. * enough space for bytes.
  2870. */
  2871. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  2872. {
  2873. struct btrfs_space_info *data_sinfo;
  2874. struct btrfs_root *root = BTRFS_I(inode)->root;
  2875. u64 used;
  2876. int ret = 0, committed = 0, alloc_chunk = 1;
  2877. /* make sure bytes are sectorsize aligned */
  2878. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2879. if (root == root->fs_info->tree_root ||
  2880. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  2881. alloc_chunk = 0;
  2882. committed = 1;
  2883. }
  2884. data_sinfo = BTRFS_I(inode)->space_info;
  2885. if (!data_sinfo)
  2886. goto alloc;
  2887. again:
  2888. /* make sure we have enough space to handle the data first */
  2889. spin_lock(&data_sinfo->lock);
  2890. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  2891. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  2892. data_sinfo->bytes_may_use;
  2893. if (used + bytes > data_sinfo->total_bytes) {
  2894. struct btrfs_trans_handle *trans;
  2895. /*
  2896. * if we don't have enough free bytes in this space then we need
  2897. * to alloc a new chunk.
  2898. */
  2899. if (!data_sinfo->full && alloc_chunk) {
  2900. u64 alloc_target;
  2901. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  2902. spin_unlock(&data_sinfo->lock);
  2903. alloc:
  2904. alloc_target = btrfs_get_alloc_profile(root, 1);
  2905. trans = btrfs_join_transaction(root);
  2906. if (IS_ERR(trans))
  2907. return PTR_ERR(trans);
  2908. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2909. bytes + 2 * 1024 * 1024,
  2910. alloc_target,
  2911. CHUNK_ALLOC_NO_FORCE);
  2912. btrfs_end_transaction(trans, root);
  2913. if (ret < 0) {
  2914. if (ret != -ENOSPC)
  2915. return ret;
  2916. else
  2917. goto commit_trans;
  2918. }
  2919. if (!data_sinfo) {
  2920. btrfs_set_inode_space_info(root, inode);
  2921. data_sinfo = BTRFS_I(inode)->space_info;
  2922. }
  2923. goto again;
  2924. }
  2925. /*
  2926. * If we have less pinned bytes than we want to allocate then
  2927. * don't bother committing the transaction, it won't help us.
  2928. */
  2929. if (data_sinfo->bytes_pinned < bytes)
  2930. committed = 1;
  2931. spin_unlock(&data_sinfo->lock);
  2932. /* commit the current transaction and try again */
  2933. commit_trans:
  2934. if (!committed &&
  2935. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  2936. committed = 1;
  2937. trans = btrfs_join_transaction(root);
  2938. if (IS_ERR(trans))
  2939. return PTR_ERR(trans);
  2940. ret = btrfs_commit_transaction(trans, root);
  2941. if (ret)
  2942. return ret;
  2943. goto again;
  2944. }
  2945. return -ENOSPC;
  2946. }
  2947. data_sinfo->bytes_may_use += bytes;
  2948. trace_btrfs_space_reservation(root->fs_info, "space_info",
  2949. (u64)(unsigned long)data_sinfo,
  2950. bytes, 1);
  2951. spin_unlock(&data_sinfo->lock);
  2952. return 0;
  2953. }
  2954. /*
  2955. * Called if we need to clear a data reservation for this inode.
  2956. */
  2957. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  2958. {
  2959. struct btrfs_root *root = BTRFS_I(inode)->root;
  2960. struct btrfs_space_info *data_sinfo;
  2961. /* make sure bytes are sectorsize aligned */
  2962. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2963. data_sinfo = BTRFS_I(inode)->space_info;
  2964. spin_lock(&data_sinfo->lock);
  2965. data_sinfo->bytes_may_use -= bytes;
  2966. trace_btrfs_space_reservation(root->fs_info, "space_info",
  2967. (u64)(unsigned long)data_sinfo,
  2968. bytes, 0);
  2969. spin_unlock(&data_sinfo->lock);
  2970. }
  2971. static void force_metadata_allocation(struct btrfs_fs_info *info)
  2972. {
  2973. struct list_head *head = &info->space_info;
  2974. struct btrfs_space_info *found;
  2975. rcu_read_lock();
  2976. list_for_each_entry_rcu(found, head, list) {
  2977. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  2978. found->force_alloc = CHUNK_ALLOC_FORCE;
  2979. }
  2980. rcu_read_unlock();
  2981. }
  2982. static int should_alloc_chunk(struct btrfs_root *root,
  2983. struct btrfs_space_info *sinfo, u64 alloc_bytes,
  2984. int force)
  2985. {
  2986. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  2987. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  2988. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  2989. u64 thresh;
  2990. if (force == CHUNK_ALLOC_FORCE)
  2991. return 1;
  2992. /*
  2993. * We need to take into account the global rsv because for all intents
  2994. * and purposes it's used space. Don't worry about locking the
  2995. * global_rsv, it doesn't change except when the transaction commits.
  2996. */
  2997. num_allocated += global_rsv->size;
  2998. /*
  2999. * in limited mode, we want to have some free space up to
  3000. * about 1% of the FS size.
  3001. */
  3002. if (force == CHUNK_ALLOC_LIMITED) {
  3003. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3004. thresh = max_t(u64, 64 * 1024 * 1024,
  3005. div_factor_fine(thresh, 1));
  3006. if (num_bytes - num_allocated < thresh)
  3007. return 1;
  3008. }
  3009. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3010. /* 256MB or 2% of the FS */
  3011. thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 2));
  3012. /* system chunks need a much small threshold */
  3013. if (sinfo->flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3014. thresh = 32 * 1024 * 1024;
  3015. if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 8))
  3016. return 0;
  3017. return 1;
  3018. }
  3019. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3020. struct btrfs_root *extent_root, u64 alloc_bytes,
  3021. u64 flags, int force)
  3022. {
  3023. struct btrfs_space_info *space_info;
  3024. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3025. int wait_for_alloc = 0;
  3026. int ret = 0;
  3027. BUG_ON(!profile_is_valid(flags, 0));
  3028. space_info = __find_space_info(extent_root->fs_info, flags);
  3029. if (!space_info) {
  3030. ret = update_space_info(extent_root->fs_info, flags,
  3031. 0, 0, &space_info);
  3032. BUG_ON(ret);
  3033. }
  3034. BUG_ON(!space_info);
  3035. again:
  3036. spin_lock(&space_info->lock);
  3037. if (force < space_info->force_alloc)
  3038. force = space_info->force_alloc;
  3039. if (space_info->full) {
  3040. spin_unlock(&space_info->lock);
  3041. return 0;
  3042. }
  3043. if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
  3044. spin_unlock(&space_info->lock);
  3045. return 0;
  3046. } else if (space_info->chunk_alloc) {
  3047. wait_for_alloc = 1;
  3048. } else {
  3049. space_info->chunk_alloc = 1;
  3050. }
  3051. spin_unlock(&space_info->lock);
  3052. mutex_lock(&fs_info->chunk_mutex);
  3053. /*
  3054. * The chunk_mutex is held throughout the entirety of a chunk
  3055. * allocation, so once we've acquired the chunk_mutex we know that the
  3056. * other guy is done and we need to recheck and see if we should
  3057. * allocate.
  3058. */
  3059. if (wait_for_alloc) {
  3060. mutex_unlock(&fs_info->chunk_mutex);
  3061. wait_for_alloc = 0;
  3062. goto again;
  3063. }
  3064. /*
  3065. * If we have mixed data/metadata chunks we want to make sure we keep
  3066. * allocating mixed chunks instead of individual chunks.
  3067. */
  3068. if (btrfs_mixed_space_info(space_info))
  3069. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3070. /*
  3071. * if we're doing a data chunk, go ahead and make sure that
  3072. * we keep a reasonable number of metadata chunks allocated in the
  3073. * FS as well.
  3074. */
  3075. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3076. fs_info->data_chunk_allocations++;
  3077. if (!(fs_info->data_chunk_allocations %
  3078. fs_info->metadata_ratio))
  3079. force_metadata_allocation(fs_info);
  3080. }
  3081. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3082. if (ret < 0 && ret != -ENOSPC)
  3083. goto out;
  3084. spin_lock(&space_info->lock);
  3085. if (ret)
  3086. space_info->full = 1;
  3087. else
  3088. ret = 1;
  3089. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3090. space_info->chunk_alloc = 0;
  3091. spin_unlock(&space_info->lock);
  3092. out:
  3093. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  3094. return ret;
  3095. }
  3096. /*
  3097. * shrink metadata reservation for delalloc
  3098. */
  3099. static int shrink_delalloc(struct btrfs_root *root, u64 to_reclaim,
  3100. bool wait_ordered)
  3101. {
  3102. struct btrfs_block_rsv *block_rsv;
  3103. struct btrfs_space_info *space_info;
  3104. struct btrfs_trans_handle *trans;
  3105. u64 reserved;
  3106. u64 max_reclaim;
  3107. u64 reclaimed = 0;
  3108. long time_left;
  3109. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3110. int loops = 0;
  3111. unsigned long progress;
  3112. trans = (struct btrfs_trans_handle *)current->journal_info;
  3113. block_rsv = &root->fs_info->delalloc_block_rsv;
  3114. space_info = block_rsv->space_info;
  3115. smp_mb();
  3116. reserved = space_info->bytes_may_use;
  3117. progress = space_info->reservation_progress;
  3118. if (reserved == 0)
  3119. return 0;
  3120. smp_mb();
  3121. if (root->fs_info->delalloc_bytes == 0) {
  3122. if (trans)
  3123. return 0;
  3124. btrfs_wait_ordered_extents(root, 0, 0);
  3125. return 0;
  3126. }
  3127. max_reclaim = min(reserved, to_reclaim);
  3128. nr_pages = max_t(unsigned long, nr_pages,
  3129. max_reclaim >> PAGE_CACHE_SHIFT);
  3130. while (loops < 1024) {
  3131. /* have the flusher threads jump in and do some IO */
  3132. smp_mb();
  3133. nr_pages = min_t(unsigned long, nr_pages,
  3134. root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
  3135. writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages,
  3136. WB_REASON_FS_FREE_SPACE);
  3137. spin_lock(&space_info->lock);
  3138. if (reserved > space_info->bytes_may_use)
  3139. reclaimed += reserved - space_info->bytes_may_use;
  3140. reserved = space_info->bytes_may_use;
  3141. spin_unlock(&space_info->lock);
  3142. loops++;
  3143. if (reserved == 0 || reclaimed >= max_reclaim)
  3144. break;
  3145. if (trans && trans->transaction->blocked)
  3146. return -EAGAIN;
  3147. if (wait_ordered && !trans) {
  3148. btrfs_wait_ordered_extents(root, 0, 0);
  3149. } else {
  3150. time_left = schedule_timeout_interruptible(1);
  3151. /* We were interrupted, exit */
  3152. if (time_left)
  3153. break;
  3154. }
  3155. /* we've kicked the IO a few times, if anything has been freed,
  3156. * exit. There is no sense in looping here for a long time
  3157. * when we really need to commit the transaction, or there are
  3158. * just too many writers without enough free space
  3159. */
  3160. if (loops > 3) {
  3161. smp_mb();
  3162. if (progress != space_info->reservation_progress)
  3163. break;
  3164. }
  3165. }
  3166. return reclaimed >= to_reclaim;
  3167. }
  3168. /**
  3169. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3170. * @root - the root we're allocating for
  3171. * @bytes - the number of bytes we want to reserve
  3172. * @force - force the commit
  3173. *
  3174. * This will check to make sure that committing the transaction will actually
  3175. * get us somewhere and then commit the transaction if it does. Otherwise it
  3176. * will return -ENOSPC.
  3177. */
  3178. static int may_commit_transaction(struct btrfs_root *root,
  3179. struct btrfs_space_info *space_info,
  3180. u64 bytes, int force)
  3181. {
  3182. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3183. struct btrfs_trans_handle *trans;
  3184. trans = (struct btrfs_trans_handle *)current->journal_info;
  3185. if (trans)
  3186. return -EAGAIN;
  3187. if (force)
  3188. goto commit;
  3189. /* See if there is enough pinned space to make this reservation */
  3190. spin_lock(&space_info->lock);
  3191. if (space_info->bytes_pinned >= bytes) {
  3192. spin_unlock(&space_info->lock);
  3193. goto commit;
  3194. }
  3195. spin_unlock(&space_info->lock);
  3196. /*
  3197. * See if there is some space in the delayed insertion reservation for
  3198. * this reservation.
  3199. */
  3200. if (space_info != delayed_rsv->space_info)
  3201. return -ENOSPC;
  3202. spin_lock(&space_info->lock);
  3203. spin_lock(&delayed_rsv->lock);
  3204. if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
  3205. spin_unlock(&delayed_rsv->lock);
  3206. spin_unlock(&space_info->lock);
  3207. return -ENOSPC;
  3208. }
  3209. spin_unlock(&delayed_rsv->lock);
  3210. spin_unlock(&space_info->lock);
  3211. commit:
  3212. trans = btrfs_join_transaction(root);
  3213. if (IS_ERR(trans))
  3214. return -ENOSPC;
  3215. return btrfs_commit_transaction(trans, root);
  3216. }
  3217. /**
  3218. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3219. * @root - the root we're allocating for
  3220. * @block_rsv - the block_rsv we're allocating for
  3221. * @orig_bytes - the number of bytes we want
  3222. * @flush - wether or not we can flush to make our reservation
  3223. *
  3224. * This will reserve orgi_bytes number of bytes from the space info associated
  3225. * with the block_rsv. If there is not enough space it will make an attempt to
  3226. * flush out space to make room. It will do this by flushing delalloc if
  3227. * possible or committing the transaction. If flush is 0 then no attempts to
  3228. * regain reservations will be made and this will fail if there is not enough
  3229. * space already.
  3230. */
  3231. static int reserve_metadata_bytes(struct btrfs_root *root,
  3232. struct btrfs_block_rsv *block_rsv,
  3233. u64 orig_bytes, int flush)
  3234. {
  3235. struct btrfs_space_info *space_info = block_rsv->space_info;
  3236. u64 used;
  3237. u64 num_bytes = orig_bytes;
  3238. int retries = 0;
  3239. int ret = 0;
  3240. bool committed = false;
  3241. bool flushing = false;
  3242. bool wait_ordered = false;
  3243. again:
  3244. ret = 0;
  3245. spin_lock(&space_info->lock);
  3246. /*
  3247. * We only want to wait if somebody other than us is flushing and we are
  3248. * actually alloed to flush.
  3249. */
  3250. while (flush && !flushing && space_info->flush) {
  3251. spin_unlock(&space_info->lock);
  3252. /*
  3253. * If we have a trans handle we can't wait because the flusher
  3254. * may have to commit the transaction, which would mean we would
  3255. * deadlock since we are waiting for the flusher to finish, but
  3256. * hold the current transaction open.
  3257. */
  3258. if (current->journal_info)
  3259. return -EAGAIN;
  3260. ret = wait_event_interruptible(space_info->wait,
  3261. !space_info->flush);
  3262. /* Must have been interrupted, return */
  3263. if (ret)
  3264. return -EINTR;
  3265. spin_lock(&space_info->lock);
  3266. }
  3267. ret = -ENOSPC;
  3268. used = space_info->bytes_used + space_info->bytes_reserved +
  3269. space_info->bytes_pinned + space_info->bytes_readonly +
  3270. space_info->bytes_may_use;
  3271. /*
  3272. * The idea here is that we've not already over-reserved the block group
  3273. * then we can go ahead and save our reservation first and then start
  3274. * flushing if we need to. Otherwise if we've already overcommitted
  3275. * lets start flushing stuff first and then come back and try to make
  3276. * our reservation.
  3277. */
  3278. if (used <= space_info->total_bytes) {
  3279. if (used + orig_bytes <= space_info->total_bytes) {
  3280. space_info->bytes_may_use += orig_bytes;
  3281. trace_btrfs_space_reservation(root->fs_info,
  3282. "space_info",
  3283. (u64)(unsigned long)space_info,
  3284. orig_bytes, 1);
  3285. ret = 0;
  3286. } else {
  3287. /*
  3288. * Ok set num_bytes to orig_bytes since we aren't
  3289. * overocmmitted, this way we only try and reclaim what
  3290. * we need.
  3291. */
  3292. num_bytes = orig_bytes;
  3293. }
  3294. } else {
  3295. /*
  3296. * Ok we're over committed, set num_bytes to the overcommitted
  3297. * amount plus the amount of bytes that we need for this
  3298. * reservation.
  3299. */
  3300. wait_ordered = true;
  3301. num_bytes = used - space_info->total_bytes +
  3302. (orig_bytes * (retries + 1));
  3303. }
  3304. if (ret) {
  3305. u64 profile = btrfs_get_alloc_profile(root, 0);
  3306. u64 avail;
  3307. /*
  3308. * If we have a lot of space that's pinned, don't bother doing
  3309. * the overcommit dance yet and just commit the transaction.
  3310. */
  3311. avail = (space_info->total_bytes - space_info->bytes_used) * 8;
  3312. do_div(avail, 10);
  3313. if (space_info->bytes_pinned >= avail && flush && !committed) {
  3314. space_info->flush = 1;
  3315. flushing = true;
  3316. spin_unlock(&space_info->lock);
  3317. ret = may_commit_transaction(root, space_info,
  3318. orig_bytes, 1);
  3319. if (ret)
  3320. goto out;
  3321. committed = true;
  3322. goto again;
  3323. }
  3324. spin_lock(&root->fs_info->free_chunk_lock);
  3325. avail = root->fs_info->free_chunk_space;
  3326. /*
  3327. * If we have dup, raid1 or raid10 then only half of the free
  3328. * space is actually useable.
  3329. */
  3330. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3331. BTRFS_BLOCK_GROUP_RAID1 |
  3332. BTRFS_BLOCK_GROUP_RAID10))
  3333. avail >>= 1;
  3334. /*
  3335. * If we aren't flushing don't let us overcommit too much, say
  3336. * 1/8th of the space. If we can flush, let it overcommit up to
  3337. * 1/2 of the space.
  3338. */
  3339. if (flush)
  3340. avail >>= 3;
  3341. else
  3342. avail >>= 1;
  3343. spin_unlock(&root->fs_info->free_chunk_lock);
  3344. if (used + num_bytes < space_info->total_bytes + avail) {
  3345. space_info->bytes_may_use += orig_bytes;
  3346. trace_btrfs_space_reservation(root->fs_info,
  3347. "space_info",
  3348. (u64)(unsigned long)space_info,
  3349. orig_bytes, 1);
  3350. ret = 0;
  3351. } else {
  3352. wait_ordered = true;
  3353. }
  3354. }
  3355. /*
  3356. * Couldn't make our reservation, save our place so while we're trying
  3357. * to reclaim space we can actually use it instead of somebody else
  3358. * stealing it from us.
  3359. */
  3360. if (ret && flush) {
  3361. flushing = true;
  3362. space_info->flush = 1;
  3363. }
  3364. spin_unlock(&space_info->lock);
  3365. if (!ret || !flush)
  3366. goto out;
  3367. /*
  3368. * We do synchronous shrinking since we don't actually unreserve
  3369. * metadata until after the IO is completed.
  3370. */
  3371. ret = shrink_delalloc(root, num_bytes, wait_ordered);
  3372. if (ret < 0)
  3373. goto out;
  3374. ret = 0;
  3375. /*
  3376. * So if we were overcommitted it's possible that somebody else flushed
  3377. * out enough space and we simply didn't have enough space to reclaim,
  3378. * so go back around and try again.
  3379. */
  3380. if (retries < 2) {
  3381. wait_ordered = true;
  3382. retries++;
  3383. goto again;
  3384. }
  3385. ret = -ENOSPC;
  3386. if (committed)
  3387. goto out;
  3388. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3389. if (!ret) {
  3390. committed = true;
  3391. goto again;
  3392. }
  3393. out:
  3394. if (flushing) {
  3395. spin_lock(&space_info->lock);
  3396. space_info->flush = 0;
  3397. wake_up_all(&space_info->wait);
  3398. spin_unlock(&space_info->lock);
  3399. }
  3400. return ret;
  3401. }
  3402. static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
  3403. struct btrfs_root *root)
  3404. {
  3405. struct btrfs_block_rsv *block_rsv = NULL;
  3406. if (root->ref_cows || root == root->fs_info->csum_root)
  3407. block_rsv = trans->block_rsv;
  3408. if (!block_rsv)
  3409. block_rsv = root->block_rsv;
  3410. if (!block_rsv)
  3411. block_rsv = &root->fs_info->empty_block_rsv;
  3412. return block_rsv;
  3413. }
  3414. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3415. u64 num_bytes)
  3416. {
  3417. int ret = -ENOSPC;
  3418. spin_lock(&block_rsv->lock);
  3419. if (block_rsv->reserved >= num_bytes) {
  3420. block_rsv->reserved -= num_bytes;
  3421. if (block_rsv->reserved < block_rsv->size)
  3422. block_rsv->full = 0;
  3423. ret = 0;
  3424. }
  3425. spin_unlock(&block_rsv->lock);
  3426. return ret;
  3427. }
  3428. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3429. u64 num_bytes, int update_size)
  3430. {
  3431. spin_lock(&block_rsv->lock);
  3432. block_rsv->reserved += num_bytes;
  3433. if (update_size)
  3434. block_rsv->size += num_bytes;
  3435. else if (block_rsv->reserved >= block_rsv->size)
  3436. block_rsv->full = 1;
  3437. spin_unlock(&block_rsv->lock);
  3438. }
  3439. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3440. struct btrfs_block_rsv *block_rsv,
  3441. struct btrfs_block_rsv *dest, u64 num_bytes)
  3442. {
  3443. struct btrfs_space_info *space_info = block_rsv->space_info;
  3444. spin_lock(&block_rsv->lock);
  3445. if (num_bytes == (u64)-1)
  3446. num_bytes = block_rsv->size;
  3447. block_rsv->size -= num_bytes;
  3448. if (block_rsv->reserved >= block_rsv->size) {
  3449. num_bytes = block_rsv->reserved - block_rsv->size;
  3450. block_rsv->reserved = block_rsv->size;
  3451. block_rsv->full = 1;
  3452. } else {
  3453. num_bytes = 0;
  3454. }
  3455. spin_unlock(&block_rsv->lock);
  3456. if (num_bytes > 0) {
  3457. if (dest) {
  3458. spin_lock(&dest->lock);
  3459. if (!dest->full) {
  3460. u64 bytes_to_add;
  3461. bytes_to_add = dest->size - dest->reserved;
  3462. bytes_to_add = min(num_bytes, bytes_to_add);
  3463. dest->reserved += bytes_to_add;
  3464. if (dest->reserved >= dest->size)
  3465. dest->full = 1;
  3466. num_bytes -= bytes_to_add;
  3467. }
  3468. spin_unlock(&dest->lock);
  3469. }
  3470. if (num_bytes) {
  3471. spin_lock(&space_info->lock);
  3472. space_info->bytes_may_use -= num_bytes;
  3473. trace_btrfs_space_reservation(fs_info, "space_info",
  3474. (u64)(unsigned long)space_info,
  3475. num_bytes, 0);
  3476. space_info->reservation_progress++;
  3477. spin_unlock(&space_info->lock);
  3478. }
  3479. }
  3480. }
  3481. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3482. struct btrfs_block_rsv *dst, u64 num_bytes)
  3483. {
  3484. int ret;
  3485. ret = block_rsv_use_bytes(src, num_bytes);
  3486. if (ret)
  3487. return ret;
  3488. block_rsv_add_bytes(dst, num_bytes, 1);
  3489. return 0;
  3490. }
  3491. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
  3492. {
  3493. memset(rsv, 0, sizeof(*rsv));
  3494. spin_lock_init(&rsv->lock);
  3495. }
  3496. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
  3497. {
  3498. struct btrfs_block_rsv *block_rsv;
  3499. struct btrfs_fs_info *fs_info = root->fs_info;
  3500. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3501. if (!block_rsv)
  3502. return NULL;
  3503. btrfs_init_block_rsv(block_rsv);
  3504. block_rsv->space_info = __find_space_info(fs_info,
  3505. BTRFS_BLOCK_GROUP_METADATA);
  3506. return block_rsv;
  3507. }
  3508. void btrfs_free_block_rsv(struct btrfs_root *root,
  3509. struct btrfs_block_rsv *rsv)
  3510. {
  3511. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3512. kfree(rsv);
  3513. }
  3514. static inline int __block_rsv_add(struct btrfs_root *root,
  3515. struct btrfs_block_rsv *block_rsv,
  3516. u64 num_bytes, int flush)
  3517. {
  3518. int ret;
  3519. if (num_bytes == 0)
  3520. return 0;
  3521. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3522. if (!ret) {
  3523. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3524. return 0;
  3525. }
  3526. return ret;
  3527. }
  3528. int btrfs_block_rsv_add(struct btrfs_root *root,
  3529. struct btrfs_block_rsv *block_rsv,
  3530. u64 num_bytes)
  3531. {
  3532. return __block_rsv_add(root, block_rsv, num_bytes, 1);
  3533. }
  3534. int btrfs_block_rsv_add_noflush(struct btrfs_root *root,
  3535. struct btrfs_block_rsv *block_rsv,
  3536. u64 num_bytes)
  3537. {
  3538. return __block_rsv_add(root, block_rsv, num_bytes, 0);
  3539. }
  3540. int btrfs_block_rsv_check(struct btrfs_root *root,
  3541. struct btrfs_block_rsv *block_rsv, int min_factor)
  3542. {
  3543. u64 num_bytes = 0;
  3544. int ret = -ENOSPC;
  3545. if (!block_rsv)
  3546. return 0;
  3547. spin_lock(&block_rsv->lock);
  3548. num_bytes = div_factor(block_rsv->size, min_factor);
  3549. if (block_rsv->reserved >= num_bytes)
  3550. ret = 0;
  3551. spin_unlock(&block_rsv->lock);
  3552. return ret;
  3553. }
  3554. static inline int __btrfs_block_rsv_refill(struct btrfs_root *root,
  3555. struct btrfs_block_rsv *block_rsv,
  3556. u64 min_reserved, int flush)
  3557. {
  3558. u64 num_bytes = 0;
  3559. int ret = -ENOSPC;
  3560. if (!block_rsv)
  3561. return 0;
  3562. spin_lock(&block_rsv->lock);
  3563. num_bytes = min_reserved;
  3564. if (block_rsv->reserved >= num_bytes)
  3565. ret = 0;
  3566. else
  3567. num_bytes -= block_rsv->reserved;
  3568. spin_unlock(&block_rsv->lock);
  3569. if (!ret)
  3570. return 0;
  3571. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3572. if (!ret) {
  3573. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  3574. return 0;
  3575. }
  3576. return ret;
  3577. }
  3578. int btrfs_block_rsv_refill(struct btrfs_root *root,
  3579. struct btrfs_block_rsv *block_rsv,
  3580. u64 min_reserved)
  3581. {
  3582. return __btrfs_block_rsv_refill(root, block_rsv, min_reserved, 1);
  3583. }
  3584. int btrfs_block_rsv_refill_noflush(struct btrfs_root *root,
  3585. struct btrfs_block_rsv *block_rsv,
  3586. u64 min_reserved)
  3587. {
  3588. return __btrfs_block_rsv_refill(root, block_rsv, min_reserved, 0);
  3589. }
  3590. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  3591. struct btrfs_block_rsv *dst_rsv,
  3592. u64 num_bytes)
  3593. {
  3594. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3595. }
  3596. void btrfs_block_rsv_release(struct btrfs_root *root,
  3597. struct btrfs_block_rsv *block_rsv,
  3598. u64 num_bytes)
  3599. {
  3600. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3601. if (global_rsv->full || global_rsv == block_rsv ||
  3602. block_rsv->space_info != global_rsv->space_info)
  3603. global_rsv = NULL;
  3604. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  3605. num_bytes);
  3606. }
  3607. /*
  3608. * helper to calculate size of global block reservation.
  3609. * the desired value is sum of space used by extent tree,
  3610. * checksum tree and root tree
  3611. */
  3612. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  3613. {
  3614. struct btrfs_space_info *sinfo;
  3615. u64 num_bytes;
  3616. u64 meta_used;
  3617. u64 data_used;
  3618. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  3619. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  3620. spin_lock(&sinfo->lock);
  3621. data_used = sinfo->bytes_used;
  3622. spin_unlock(&sinfo->lock);
  3623. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3624. spin_lock(&sinfo->lock);
  3625. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  3626. data_used = 0;
  3627. meta_used = sinfo->bytes_used;
  3628. spin_unlock(&sinfo->lock);
  3629. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  3630. csum_size * 2;
  3631. num_bytes += div64_u64(data_used + meta_used, 50);
  3632. if (num_bytes * 3 > meta_used)
  3633. num_bytes = div64_u64(meta_used, 3) * 2;
  3634. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  3635. }
  3636. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  3637. {
  3638. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  3639. struct btrfs_space_info *sinfo = block_rsv->space_info;
  3640. u64 num_bytes;
  3641. num_bytes = calc_global_metadata_size(fs_info);
  3642. spin_lock(&block_rsv->lock);
  3643. spin_lock(&sinfo->lock);
  3644. block_rsv->size = num_bytes;
  3645. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  3646. sinfo->bytes_reserved + sinfo->bytes_readonly +
  3647. sinfo->bytes_may_use;
  3648. if (sinfo->total_bytes > num_bytes) {
  3649. num_bytes = sinfo->total_bytes - num_bytes;
  3650. block_rsv->reserved += num_bytes;
  3651. sinfo->bytes_may_use += num_bytes;
  3652. trace_btrfs_space_reservation(fs_info, "space_info",
  3653. (u64)(unsigned long)sinfo, num_bytes, 1);
  3654. }
  3655. if (block_rsv->reserved >= block_rsv->size) {
  3656. num_bytes = block_rsv->reserved - block_rsv->size;
  3657. sinfo->bytes_may_use -= num_bytes;
  3658. trace_btrfs_space_reservation(fs_info, "space_info",
  3659. (u64)(unsigned long)sinfo, num_bytes, 0);
  3660. sinfo->reservation_progress++;
  3661. block_rsv->reserved = block_rsv->size;
  3662. block_rsv->full = 1;
  3663. }
  3664. spin_unlock(&sinfo->lock);
  3665. spin_unlock(&block_rsv->lock);
  3666. }
  3667. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  3668. {
  3669. struct btrfs_space_info *space_info;
  3670. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3671. fs_info->chunk_block_rsv.space_info = space_info;
  3672. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3673. fs_info->global_block_rsv.space_info = space_info;
  3674. fs_info->delalloc_block_rsv.space_info = space_info;
  3675. fs_info->trans_block_rsv.space_info = space_info;
  3676. fs_info->empty_block_rsv.space_info = space_info;
  3677. fs_info->delayed_block_rsv.space_info = space_info;
  3678. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  3679. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  3680. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  3681. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  3682. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  3683. update_global_block_rsv(fs_info);
  3684. }
  3685. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  3686. {
  3687. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  3688. (u64)-1);
  3689. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  3690. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  3691. WARN_ON(fs_info->trans_block_rsv.size > 0);
  3692. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  3693. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  3694. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  3695. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  3696. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  3697. }
  3698. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  3699. struct btrfs_root *root)
  3700. {
  3701. if (!trans->bytes_reserved)
  3702. return;
  3703. trace_btrfs_space_reservation(root->fs_info, "transaction",
  3704. (u64)(unsigned long)trans,
  3705. trans->bytes_reserved, 0);
  3706. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  3707. trans->bytes_reserved = 0;
  3708. }
  3709. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  3710. struct inode *inode)
  3711. {
  3712. struct btrfs_root *root = BTRFS_I(inode)->root;
  3713. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3714. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  3715. /*
  3716. * We need to hold space in order to delete our orphan item once we've
  3717. * added it, so this takes the reservation so we can release it later
  3718. * when we are truly done with the orphan item.
  3719. */
  3720. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  3721. trace_btrfs_space_reservation(root->fs_info, "orphan",
  3722. btrfs_ino(inode), num_bytes, 1);
  3723. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3724. }
  3725. void btrfs_orphan_release_metadata(struct inode *inode)
  3726. {
  3727. struct btrfs_root *root = BTRFS_I(inode)->root;
  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, 0);
  3731. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  3732. }
  3733. int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
  3734. struct btrfs_pending_snapshot *pending)
  3735. {
  3736. struct btrfs_root *root = pending->root;
  3737. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3738. struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
  3739. /*
  3740. * two for root back/forward refs, two for directory entries
  3741. * and one for root of the snapshot.
  3742. */
  3743. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
  3744. dst_rsv->space_info = src_rsv->space_info;
  3745. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3746. }
  3747. /**
  3748. * drop_outstanding_extent - drop an outstanding extent
  3749. * @inode: the inode we're dropping the extent for
  3750. *
  3751. * This is called when we are freeing up an outstanding extent, either called
  3752. * after an error or after an extent is written. This will return the number of
  3753. * reserved extents that need to be freed. This must be called with
  3754. * BTRFS_I(inode)->lock held.
  3755. */
  3756. static unsigned drop_outstanding_extent(struct inode *inode)
  3757. {
  3758. unsigned drop_inode_space = 0;
  3759. unsigned dropped_extents = 0;
  3760. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  3761. BTRFS_I(inode)->outstanding_extents--;
  3762. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  3763. BTRFS_I(inode)->delalloc_meta_reserved) {
  3764. drop_inode_space = 1;
  3765. BTRFS_I(inode)->delalloc_meta_reserved = 0;
  3766. }
  3767. /*
  3768. * If we have more or the same amount of outsanding extents than we have
  3769. * reserved then we need to leave the reserved extents count alone.
  3770. */
  3771. if (BTRFS_I(inode)->outstanding_extents >=
  3772. BTRFS_I(inode)->reserved_extents)
  3773. return drop_inode_space;
  3774. dropped_extents = BTRFS_I(inode)->reserved_extents -
  3775. BTRFS_I(inode)->outstanding_extents;
  3776. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  3777. return dropped_extents + drop_inode_space;
  3778. }
  3779. /**
  3780. * calc_csum_metadata_size - return the amount of metada space that must be
  3781. * reserved/free'd for the given bytes.
  3782. * @inode: the inode we're manipulating
  3783. * @num_bytes: the number of bytes in question
  3784. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  3785. *
  3786. * This adjusts the number of csum_bytes in the inode and then returns the
  3787. * correct amount of metadata that must either be reserved or freed. We
  3788. * calculate how many checksums we can fit into one leaf and then divide the
  3789. * number of bytes that will need to be checksumed by this value to figure out
  3790. * how many checksums will be required. If we are adding bytes then the number
  3791. * may go up and we will return the number of additional bytes that must be
  3792. * reserved. If it is going down we will return the number of bytes that must
  3793. * be freed.
  3794. *
  3795. * This must be called with BTRFS_I(inode)->lock held.
  3796. */
  3797. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  3798. int reserve)
  3799. {
  3800. struct btrfs_root *root = BTRFS_I(inode)->root;
  3801. u64 csum_size;
  3802. int num_csums_per_leaf;
  3803. int num_csums;
  3804. int old_csums;
  3805. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  3806. BTRFS_I(inode)->csum_bytes == 0)
  3807. return 0;
  3808. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  3809. if (reserve)
  3810. BTRFS_I(inode)->csum_bytes += num_bytes;
  3811. else
  3812. BTRFS_I(inode)->csum_bytes -= num_bytes;
  3813. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  3814. num_csums_per_leaf = (int)div64_u64(csum_size,
  3815. sizeof(struct btrfs_csum_item) +
  3816. sizeof(struct btrfs_disk_key));
  3817. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  3818. num_csums = num_csums + num_csums_per_leaf - 1;
  3819. num_csums = num_csums / num_csums_per_leaf;
  3820. old_csums = old_csums + num_csums_per_leaf - 1;
  3821. old_csums = old_csums / num_csums_per_leaf;
  3822. /* No change, no need to reserve more */
  3823. if (old_csums == num_csums)
  3824. return 0;
  3825. if (reserve)
  3826. return btrfs_calc_trans_metadata_size(root,
  3827. num_csums - old_csums);
  3828. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  3829. }
  3830. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  3831. {
  3832. struct btrfs_root *root = BTRFS_I(inode)->root;
  3833. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  3834. u64 to_reserve = 0;
  3835. u64 csum_bytes;
  3836. unsigned nr_extents = 0;
  3837. int extra_reserve = 0;
  3838. int flush = 1;
  3839. int ret;
  3840. /* Need to be holding the i_mutex here if we aren't free space cache */
  3841. if (btrfs_is_free_space_inode(root, inode))
  3842. flush = 0;
  3843. if (flush && btrfs_transaction_in_commit(root->fs_info))
  3844. schedule_timeout(1);
  3845. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  3846. num_bytes = ALIGN(num_bytes, root->sectorsize);
  3847. spin_lock(&BTRFS_I(inode)->lock);
  3848. BTRFS_I(inode)->outstanding_extents++;
  3849. if (BTRFS_I(inode)->outstanding_extents >
  3850. BTRFS_I(inode)->reserved_extents)
  3851. nr_extents = BTRFS_I(inode)->outstanding_extents -
  3852. BTRFS_I(inode)->reserved_extents;
  3853. /*
  3854. * Add an item to reserve for updating the inode when we complete the
  3855. * delalloc io.
  3856. */
  3857. if (!BTRFS_I(inode)->delalloc_meta_reserved) {
  3858. nr_extents++;
  3859. extra_reserve = 1;
  3860. }
  3861. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  3862. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  3863. csum_bytes = BTRFS_I(inode)->csum_bytes;
  3864. spin_unlock(&BTRFS_I(inode)->lock);
  3865. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  3866. if (ret) {
  3867. u64 to_free = 0;
  3868. unsigned dropped;
  3869. spin_lock(&BTRFS_I(inode)->lock);
  3870. dropped = drop_outstanding_extent(inode);
  3871. /*
  3872. * If the inodes csum_bytes is the same as the original
  3873. * csum_bytes then we know we haven't raced with any free()ers
  3874. * so we can just reduce our inodes csum bytes and carry on.
  3875. * Otherwise we have to do the normal free thing to account for
  3876. * the case that the free side didn't free up its reserve
  3877. * because of this outstanding reservation.
  3878. */
  3879. if (BTRFS_I(inode)->csum_bytes == csum_bytes)
  3880. calc_csum_metadata_size(inode, num_bytes, 0);
  3881. else
  3882. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  3883. spin_unlock(&BTRFS_I(inode)->lock);
  3884. if (dropped)
  3885. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  3886. if (to_free) {
  3887. btrfs_block_rsv_release(root, block_rsv, to_free);
  3888. trace_btrfs_space_reservation(root->fs_info,
  3889. "delalloc",
  3890. btrfs_ino(inode),
  3891. to_free, 0);
  3892. }
  3893. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  3894. return ret;
  3895. }
  3896. spin_lock(&BTRFS_I(inode)->lock);
  3897. if (extra_reserve) {
  3898. BTRFS_I(inode)->delalloc_meta_reserved = 1;
  3899. nr_extents--;
  3900. }
  3901. BTRFS_I(inode)->reserved_extents += nr_extents;
  3902. spin_unlock(&BTRFS_I(inode)->lock);
  3903. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  3904. if (to_reserve)
  3905. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  3906. btrfs_ino(inode), to_reserve, 1);
  3907. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  3908. return 0;
  3909. }
  3910. /**
  3911. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  3912. * @inode: the inode to release the reservation for
  3913. * @num_bytes: the number of bytes we're releasing
  3914. *
  3915. * This will release the metadata reservation for an inode. This can be called
  3916. * once we complete IO for a given set of bytes to release their metadata
  3917. * reservations.
  3918. */
  3919. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  3920. {
  3921. struct btrfs_root *root = BTRFS_I(inode)->root;
  3922. u64 to_free = 0;
  3923. unsigned dropped;
  3924. num_bytes = ALIGN(num_bytes, root->sectorsize);
  3925. spin_lock(&BTRFS_I(inode)->lock);
  3926. dropped = drop_outstanding_extent(inode);
  3927. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  3928. spin_unlock(&BTRFS_I(inode)->lock);
  3929. if (dropped > 0)
  3930. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  3931. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  3932. btrfs_ino(inode), to_free, 0);
  3933. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  3934. to_free);
  3935. }
  3936. /**
  3937. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  3938. * @inode: inode we're writing to
  3939. * @num_bytes: the number of bytes we want to allocate
  3940. *
  3941. * This will do the following things
  3942. *
  3943. * o reserve space in the data space info for num_bytes
  3944. * o reserve space in the metadata space info based on number of outstanding
  3945. * extents and how much csums will be needed
  3946. * o add to the inodes ->delalloc_bytes
  3947. * o add it to the fs_info's delalloc inodes list.
  3948. *
  3949. * This will return 0 for success and -ENOSPC if there is no space left.
  3950. */
  3951. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  3952. {
  3953. int ret;
  3954. ret = btrfs_check_data_free_space(inode, num_bytes);
  3955. if (ret)
  3956. return ret;
  3957. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  3958. if (ret) {
  3959. btrfs_free_reserved_data_space(inode, num_bytes);
  3960. return ret;
  3961. }
  3962. return 0;
  3963. }
  3964. /**
  3965. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  3966. * @inode: inode we're releasing space for
  3967. * @num_bytes: the number of bytes we want to free up
  3968. *
  3969. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  3970. * called in the case that we don't need the metadata AND data reservations
  3971. * anymore. So if there is an error or we insert an inline extent.
  3972. *
  3973. * This function will release the metadata space that was not used and will
  3974. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  3975. * list if there are no delalloc bytes left.
  3976. */
  3977. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  3978. {
  3979. btrfs_delalloc_release_metadata(inode, num_bytes);
  3980. btrfs_free_reserved_data_space(inode, num_bytes);
  3981. }
  3982. static int update_block_group(struct btrfs_trans_handle *trans,
  3983. struct btrfs_root *root,
  3984. u64 bytenr, u64 num_bytes, int alloc)
  3985. {
  3986. struct btrfs_block_group_cache *cache = NULL;
  3987. struct btrfs_fs_info *info = root->fs_info;
  3988. u64 total = num_bytes;
  3989. u64 old_val;
  3990. u64 byte_in_group;
  3991. int factor;
  3992. /* block accounting for super block */
  3993. spin_lock(&info->delalloc_lock);
  3994. old_val = btrfs_super_bytes_used(info->super_copy);
  3995. if (alloc)
  3996. old_val += num_bytes;
  3997. else
  3998. old_val -= num_bytes;
  3999. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4000. spin_unlock(&info->delalloc_lock);
  4001. while (total) {
  4002. cache = btrfs_lookup_block_group(info, bytenr);
  4003. if (!cache)
  4004. return -1;
  4005. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4006. BTRFS_BLOCK_GROUP_RAID1 |
  4007. BTRFS_BLOCK_GROUP_RAID10))
  4008. factor = 2;
  4009. else
  4010. factor = 1;
  4011. /*
  4012. * If this block group has free space cache written out, we
  4013. * need to make sure to load it if we are removing space. This
  4014. * is because we need the unpinning stage to actually add the
  4015. * space back to the block group, otherwise we will leak space.
  4016. */
  4017. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4018. cache_block_group(cache, trans, NULL, 1);
  4019. byte_in_group = bytenr - cache->key.objectid;
  4020. WARN_ON(byte_in_group > cache->key.offset);
  4021. spin_lock(&cache->space_info->lock);
  4022. spin_lock(&cache->lock);
  4023. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4024. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4025. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4026. cache->dirty = 1;
  4027. old_val = btrfs_block_group_used(&cache->item);
  4028. num_bytes = min(total, cache->key.offset - byte_in_group);
  4029. if (alloc) {
  4030. old_val += num_bytes;
  4031. btrfs_set_block_group_used(&cache->item, old_val);
  4032. cache->reserved -= num_bytes;
  4033. cache->space_info->bytes_reserved -= num_bytes;
  4034. cache->space_info->bytes_used += num_bytes;
  4035. cache->space_info->disk_used += num_bytes * factor;
  4036. spin_unlock(&cache->lock);
  4037. spin_unlock(&cache->space_info->lock);
  4038. } else {
  4039. old_val -= num_bytes;
  4040. btrfs_set_block_group_used(&cache->item, old_val);
  4041. cache->pinned += num_bytes;
  4042. cache->space_info->bytes_pinned += num_bytes;
  4043. cache->space_info->bytes_used -= num_bytes;
  4044. cache->space_info->disk_used -= num_bytes * factor;
  4045. spin_unlock(&cache->lock);
  4046. spin_unlock(&cache->space_info->lock);
  4047. set_extent_dirty(info->pinned_extents,
  4048. bytenr, bytenr + num_bytes - 1,
  4049. GFP_NOFS | __GFP_NOFAIL);
  4050. }
  4051. btrfs_put_block_group(cache);
  4052. total -= num_bytes;
  4053. bytenr += num_bytes;
  4054. }
  4055. return 0;
  4056. }
  4057. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4058. {
  4059. struct btrfs_block_group_cache *cache;
  4060. u64 bytenr;
  4061. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4062. if (!cache)
  4063. return 0;
  4064. bytenr = cache->key.objectid;
  4065. btrfs_put_block_group(cache);
  4066. return bytenr;
  4067. }
  4068. static int pin_down_extent(struct btrfs_root *root,
  4069. struct btrfs_block_group_cache *cache,
  4070. u64 bytenr, u64 num_bytes, int reserved)
  4071. {
  4072. spin_lock(&cache->space_info->lock);
  4073. spin_lock(&cache->lock);
  4074. cache->pinned += num_bytes;
  4075. cache->space_info->bytes_pinned += num_bytes;
  4076. if (reserved) {
  4077. cache->reserved -= num_bytes;
  4078. cache->space_info->bytes_reserved -= num_bytes;
  4079. }
  4080. spin_unlock(&cache->lock);
  4081. spin_unlock(&cache->space_info->lock);
  4082. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4083. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4084. return 0;
  4085. }
  4086. /*
  4087. * this function must be called within transaction
  4088. */
  4089. int btrfs_pin_extent(struct btrfs_root *root,
  4090. u64 bytenr, u64 num_bytes, int reserved)
  4091. {
  4092. struct btrfs_block_group_cache *cache;
  4093. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4094. BUG_ON(!cache);
  4095. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4096. btrfs_put_block_group(cache);
  4097. return 0;
  4098. }
  4099. /*
  4100. * this function must be called within transaction
  4101. */
  4102. int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
  4103. struct btrfs_root *root,
  4104. u64 bytenr, u64 num_bytes)
  4105. {
  4106. struct btrfs_block_group_cache *cache;
  4107. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4108. BUG_ON(!cache);
  4109. /*
  4110. * pull in the free space cache (if any) so that our pin
  4111. * removes the free space from the cache. We have load_only set
  4112. * to one because the slow code to read in the free extents does check
  4113. * the pinned extents.
  4114. */
  4115. cache_block_group(cache, trans, root, 1);
  4116. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4117. /* remove us from the free space cache (if we're there at all) */
  4118. btrfs_remove_free_space(cache, bytenr, num_bytes);
  4119. btrfs_put_block_group(cache);
  4120. return 0;
  4121. }
  4122. /**
  4123. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4124. * @cache: The cache we are manipulating
  4125. * @num_bytes: The number of bytes in question
  4126. * @reserve: One of the reservation enums
  4127. *
  4128. * This is called by the allocator when it reserves space, or by somebody who is
  4129. * freeing space that was never actually used on disk. For example if you
  4130. * reserve some space for a new leaf in transaction A and before transaction A
  4131. * commits you free that leaf, you call this with reserve set to 0 in order to
  4132. * clear the reservation.
  4133. *
  4134. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4135. * ENOSPC accounting. For data we handle the reservation through clearing the
  4136. * delalloc bits in the io_tree. We have to do this since we could end up
  4137. * allocating less disk space for the amount of data we have reserved in the
  4138. * case of compression.
  4139. *
  4140. * If this is a reservation and the block group has become read only we cannot
  4141. * make the reservation and return -EAGAIN, otherwise this function always
  4142. * succeeds.
  4143. */
  4144. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4145. u64 num_bytes, int reserve)
  4146. {
  4147. struct btrfs_space_info *space_info = cache->space_info;
  4148. int ret = 0;
  4149. spin_lock(&space_info->lock);
  4150. spin_lock(&cache->lock);
  4151. if (reserve != RESERVE_FREE) {
  4152. if (cache->ro) {
  4153. ret = -EAGAIN;
  4154. } else {
  4155. cache->reserved += num_bytes;
  4156. space_info->bytes_reserved += num_bytes;
  4157. if (reserve == RESERVE_ALLOC) {
  4158. trace_btrfs_space_reservation(cache->fs_info,
  4159. "space_info",
  4160. (u64)(unsigned long)space_info,
  4161. num_bytes, 0);
  4162. space_info->bytes_may_use -= num_bytes;
  4163. }
  4164. }
  4165. } else {
  4166. if (cache->ro)
  4167. space_info->bytes_readonly += num_bytes;
  4168. cache->reserved -= num_bytes;
  4169. space_info->bytes_reserved -= num_bytes;
  4170. space_info->reservation_progress++;
  4171. }
  4172. spin_unlock(&cache->lock);
  4173. spin_unlock(&space_info->lock);
  4174. return ret;
  4175. }
  4176. int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4177. struct btrfs_root *root)
  4178. {
  4179. struct btrfs_fs_info *fs_info = root->fs_info;
  4180. struct btrfs_caching_control *next;
  4181. struct btrfs_caching_control *caching_ctl;
  4182. struct btrfs_block_group_cache *cache;
  4183. down_write(&fs_info->extent_commit_sem);
  4184. list_for_each_entry_safe(caching_ctl, next,
  4185. &fs_info->caching_block_groups, list) {
  4186. cache = caching_ctl->block_group;
  4187. if (block_group_cache_done(cache)) {
  4188. cache->last_byte_to_unpin = (u64)-1;
  4189. list_del_init(&caching_ctl->list);
  4190. put_caching_control(caching_ctl);
  4191. } else {
  4192. cache->last_byte_to_unpin = caching_ctl->progress;
  4193. }
  4194. }
  4195. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4196. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4197. else
  4198. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4199. up_write(&fs_info->extent_commit_sem);
  4200. update_global_block_rsv(fs_info);
  4201. return 0;
  4202. }
  4203. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4204. {
  4205. struct btrfs_fs_info *fs_info = root->fs_info;
  4206. struct btrfs_block_group_cache *cache = NULL;
  4207. u64 len;
  4208. while (start <= end) {
  4209. if (!cache ||
  4210. start >= cache->key.objectid + cache->key.offset) {
  4211. if (cache)
  4212. btrfs_put_block_group(cache);
  4213. cache = btrfs_lookup_block_group(fs_info, start);
  4214. BUG_ON(!cache);
  4215. }
  4216. len = cache->key.objectid + cache->key.offset - start;
  4217. len = min(len, end + 1 - start);
  4218. if (start < cache->last_byte_to_unpin) {
  4219. len = min(len, cache->last_byte_to_unpin - start);
  4220. btrfs_add_free_space(cache, start, len);
  4221. }
  4222. start += len;
  4223. spin_lock(&cache->space_info->lock);
  4224. spin_lock(&cache->lock);
  4225. cache->pinned -= len;
  4226. cache->space_info->bytes_pinned -= len;
  4227. if (cache->ro)
  4228. cache->space_info->bytes_readonly += len;
  4229. spin_unlock(&cache->lock);
  4230. spin_unlock(&cache->space_info->lock);
  4231. }
  4232. if (cache)
  4233. btrfs_put_block_group(cache);
  4234. return 0;
  4235. }
  4236. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4237. struct btrfs_root *root)
  4238. {
  4239. struct btrfs_fs_info *fs_info = root->fs_info;
  4240. struct extent_io_tree *unpin;
  4241. u64 start;
  4242. u64 end;
  4243. int ret;
  4244. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4245. unpin = &fs_info->freed_extents[1];
  4246. else
  4247. unpin = &fs_info->freed_extents[0];
  4248. while (1) {
  4249. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4250. EXTENT_DIRTY);
  4251. if (ret)
  4252. break;
  4253. if (btrfs_test_opt(root, DISCARD))
  4254. ret = btrfs_discard_extent(root, start,
  4255. end + 1 - start, NULL);
  4256. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4257. unpin_extent_range(root, start, end);
  4258. cond_resched();
  4259. }
  4260. return 0;
  4261. }
  4262. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4263. struct btrfs_root *root,
  4264. u64 bytenr, u64 num_bytes, u64 parent,
  4265. u64 root_objectid, u64 owner_objectid,
  4266. u64 owner_offset, int refs_to_drop,
  4267. struct btrfs_delayed_extent_op *extent_op)
  4268. {
  4269. struct btrfs_key key;
  4270. struct btrfs_path *path;
  4271. struct btrfs_fs_info *info = root->fs_info;
  4272. struct btrfs_root *extent_root = info->extent_root;
  4273. struct extent_buffer *leaf;
  4274. struct btrfs_extent_item *ei;
  4275. struct btrfs_extent_inline_ref *iref;
  4276. int ret;
  4277. int is_data;
  4278. int extent_slot = 0;
  4279. int found_extent = 0;
  4280. int num_to_del = 1;
  4281. u32 item_size;
  4282. u64 refs;
  4283. path = btrfs_alloc_path();
  4284. if (!path)
  4285. return -ENOMEM;
  4286. path->reada = 1;
  4287. path->leave_spinning = 1;
  4288. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4289. BUG_ON(!is_data && refs_to_drop != 1);
  4290. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4291. bytenr, num_bytes, parent,
  4292. root_objectid, owner_objectid,
  4293. owner_offset);
  4294. if (ret == 0) {
  4295. extent_slot = path->slots[0];
  4296. while (extent_slot >= 0) {
  4297. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4298. extent_slot);
  4299. if (key.objectid != bytenr)
  4300. break;
  4301. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4302. key.offset == num_bytes) {
  4303. found_extent = 1;
  4304. break;
  4305. }
  4306. if (path->slots[0] - extent_slot > 5)
  4307. break;
  4308. extent_slot--;
  4309. }
  4310. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4311. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4312. if (found_extent && item_size < sizeof(*ei))
  4313. found_extent = 0;
  4314. #endif
  4315. if (!found_extent) {
  4316. BUG_ON(iref);
  4317. ret = remove_extent_backref(trans, extent_root, path,
  4318. NULL, refs_to_drop,
  4319. is_data);
  4320. BUG_ON(ret);
  4321. btrfs_release_path(path);
  4322. path->leave_spinning = 1;
  4323. key.objectid = bytenr;
  4324. key.type = BTRFS_EXTENT_ITEM_KEY;
  4325. key.offset = num_bytes;
  4326. ret = btrfs_search_slot(trans, extent_root,
  4327. &key, path, -1, 1);
  4328. if (ret) {
  4329. printk(KERN_ERR "umm, got %d back from search"
  4330. ", was looking for %llu\n", ret,
  4331. (unsigned long long)bytenr);
  4332. if (ret > 0)
  4333. btrfs_print_leaf(extent_root,
  4334. path->nodes[0]);
  4335. }
  4336. BUG_ON(ret);
  4337. extent_slot = path->slots[0];
  4338. }
  4339. } else {
  4340. btrfs_print_leaf(extent_root, path->nodes[0]);
  4341. WARN_ON(1);
  4342. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  4343. "parent %llu root %llu owner %llu offset %llu\n",
  4344. (unsigned long long)bytenr,
  4345. (unsigned long long)parent,
  4346. (unsigned long long)root_objectid,
  4347. (unsigned long long)owner_objectid,
  4348. (unsigned long long)owner_offset);
  4349. }
  4350. leaf = path->nodes[0];
  4351. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4352. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4353. if (item_size < sizeof(*ei)) {
  4354. BUG_ON(found_extent || extent_slot != path->slots[0]);
  4355. ret = convert_extent_item_v0(trans, extent_root, path,
  4356. owner_objectid, 0);
  4357. BUG_ON(ret < 0);
  4358. btrfs_release_path(path);
  4359. path->leave_spinning = 1;
  4360. key.objectid = bytenr;
  4361. key.type = BTRFS_EXTENT_ITEM_KEY;
  4362. key.offset = num_bytes;
  4363. ret = btrfs_search_slot(trans, extent_root, &key, path,
  4364. -1, 1);
  4365. if (ret) {
  4366. printk(KERN_ERR "umm, got %d back from search"
  4367. ", was looking for %llu\n", ret,
  4368. (unsigned long long)bytenr);
  4369. btrfs_print_leaf(extent_root, path->nodes[0]);
  4370. }
  4371. BUG_ON(ret);
  4372. extent_slot = path->slots[0];
  4373. leaf = path->nodes[0];
  4374. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4375. }
  4376. #endif
  4377. BUG_ON(item_size < sizeof(*ei));
  4378. ei = btrfs_item_ptr(leaf, extent_slot,
  4379. struct btrfs_extent_item);
  4380. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4381. struct btrfs_tree_block_info *bi;
  4382. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  4383. bi = (struct btrfs_tree_block_info *)(ei + 1);
  4384. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  4385. }
  4386. refs = btrfs_extent_refs(leaf, ei);
  4387. BUG_ON(refs < refs_to_drop);
  4388. refs -= refs_to_drop;
  4389. if (refs > 0) {
  4390. if (extent_op)
  4391. __run_delayed_extent_op(extent_op, leaf, ei);
  4392. /*
  4393. * In the case of inline back ref, reference count will
  4394. * be updated by remove_extent_backref
  4395. */
  4396. if (iref) {
  4397. BUG_ON(!found_extent);
  4398. } else {
  4399. btrfs_set_extent_refs(leaf, ei, refs);
  4400. btrfs_mark_buffer_dirty(leaf);
  4401. }
  4402. if (found_extent) {
  4403. ret = remove_extent_backref(trans, extent_root, path,
  4404. iref, refs_to_drop,
  4405. is_data);
  4406. BUG_ON(ret);
  4407. }
  4408. } else {
  4409. if (found_extent) {
  4410. BUG_ON(is_data && refs_to_drop !=
  4411. extent_data_ref_count(root, path, iref));
  4412. if (iref) {
  4413. BUG_ON(path->slots[0] != extent_slot);
  4414. } else {
  4415. BUG_ON(path->slots[0] != extent_slot + 1);
  4416. path->slots[0] = extent_slot;
  4417. num_to_del = 2;
  4418. }
  4419. }
  4420. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  4421. num_to_del);
  4422. BUG_ON(ret);
  4423. btrfs_release_path(path);
  4424. if (is_data) {
  4425. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  4426. BUG_ON(ret);
  4427. } else {
  4428. invalidate_mapping_pages(info->btree_inode->i_mapping,
  4429. bytenr >> PAGE_CACHE_SHIFT,
  4430. (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
  4431. }
  4432. ret = update_block_group(trans, root, bytenr, num_bytes, 0);
  4433. BUG_ON(ret);
  4434. }
  4435. btrfs_free_path(path);
  4436. return ret;
  4437. }
  4438. /*
  4439. * when we free an block, it is possible (and likely) that we free the last
  4440. * delayed ref for that extent as well. This searches the delayed ref tree for
  4441. * a given extent, and if there are no other delayed refs to be processed, it
  4442. * removes it from the tree.
  4443. */
  4444. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  4445. struct btrfs_root *root, u64 bytenr)
  4446. {
  4447. struct btrfs_delayed_ref_head *head;
  4448. struct btrfs_delayed_ref_root *delayed_refs;
  4449. struct btrfs_delayed_ref_node *ref;
  4450. struct rb_node *node;
  4451. int ret = 0;
  4452. delayed_refs = &trans->transaction->delayed_refs;
  4453. spin_lock(&delayed_refs->lock);
  4454. head = btrfs_find_delayed_ref_head(trans, bytenr);
  4455. if (!head)
  4456. goto out;
  4457. node = rb_prev(&head->node.rb_node);
  4458. if (!node)
  4459. goto out;
  4460. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  4461. /* there are still entries for this ref, we can't drop it */
  4462. if (ref->bytenr == bytenr)
  4463. goto out;
  4464. if (head->extent_op) {
  4465. if (!head->must_insert_reserved)
  4466. goto out;
  4467. kfree(head->extent_op);
  4468. head->extent_op = NULL;
  4469. }
  4470. /*
  4471. * waiting for the lock here would deadlock. If someone else has it
  4472. * locked they are already in the process of dropping it anyway
  4473. */
  4474. if (!mutex_trylock(&head->mutex))
  4475. goto out;
  4476. /*
  4477. * at this point we have a head with no other entries. Go
  4478. * ahead and process it.
  4479. */
  4480. head->node.in_tree = 0;
  4481. rb_erase(&head->node.rb_node, &delayed_refs->root);
  4482. delayed_refs->num_entries--;
  4483. if (waitqueue_active(&delayed_refs->seq_wait))
  4484. wake_up(&delayed_refs->seq_wait);
  4485. /*
  4486. * we don't take a ref on the node because we're removing it from the
  4487. * tree, so we just steal the ref the tree was holding.
  4488. */
  4489. delayed_refs->num_heads--;
  4490. if (list_empty(&head->cluster))
  4491. delayed_refs->num_heads_ready--;
  4492. list_del_init(&head->cluster);
  4493. spin_unlock(&delayed_refs->lock);
  4494. BUG_ON(head->extent_op);
  4495. if (head->must_insert_reserved)
  4496. ret = 1;
  4497. mutex_unlock(&head->mutex);
  4498. btrfs_put_delayed_ref(&head->node);
  4499. return ret;
  4500. out:
  4501. spin_unlock(&delayed_refs->lock);
  4502. return 0;
  4503. }
  4504. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  4505. struct btrfs_root *root,
  4506. struct extent_buffer *buf,
  4507. u64 parent, int last_ref, int for_cow)
  4508. {
  4509. struct btrfs_block_group_cache *cache = NULL;
  4510. int ret;
  4511. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4512. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  4513. buf->start, buf->len,
  4514. parent, root->root_key.objectid,
  4515. btrfs_header_level(buf),
  4516. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  4517. BUG_ON(ret);
  4518. }
  4519. if (!last_ref)
  4520. return;
  4521. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  4522. if (btrfs_header_generation(buf) == trans->transid) {
  4523. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4524. ret = check_ref_cleanup(trans, root, buf->start);
  4525. if (!ret)
  4526. goto out;
  4527. }
  4528. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  4529. pin_down_extent(root, cache, buf->start, buf->len, 1);
  4530. goto out;
  4531. }
  4532. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  4533. btrfs_add_free_space(cache, buf->start, buf->len);
  4534. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  4535. }
  4536. out:
  4537. /*
  4538. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  4539. * anymore.
  4540. */
  4541. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  4542. btrfs_put_block_group(cache);
  4543. }
  4544. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4545. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  4546. u64 owner, u64 offset, int for_cow)
  4547. {
  4548. int ret;
  4549. struct btrfs_fs_info *fs_info = root->fs_info;
  4550. /*
  4551. * tree log blocks never actually go into the extent allocation
  4552. * tree, just update pinning info and exit early.
  4553. */
  4554. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  4555. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  4556. /* unlocks the pinned mutex */
  4557. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  4558. ret = 0;
  4559. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4560. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  4561. num_bytes,
  4562. parent, root_objectid, (int)owner,
  4563. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  4564. BUG_ON(ret);
  4565. } else {
  4566. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  4567. num_bytes,
  4568. parent, root_objectid, owner,
  4569. offset, BTRFS_DROP_DELAYED_REF,
  4570. NULL, for_cow);
  4571. BUG_ON(ret);
  4572. }
  4573. return ret;
  4574. }
  4575. static u64 stripe_align(struct btrfs_root *root, u64 val)
  4576. {
  4577. u64 mask = ((u64)root->stripesize - 1);
  4578. u64 ret = (val + mask) & ~mask;
  4579. return ret;
  4580. }
  4581. /*
  4582. * when we wait for progress in the block group caching, its because
  4583. * our allocation attempt failed at least once. So, we must sleep
  4584. * and let some progress happen before we try again.
  4585. *
  4586. * This function will sleep at least once waiting for new free space to
  4587. * show up, and then it will check the block group free space numbers
  4588. * for our min num_bytes. Another option is to have it go ahead
  4589. * and look in the rbtree for a free extent of a given size, but this
  4590. * is a good start.
  4591. */
  4592. static noinline int
  4593. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  4594. u64 num_bytes)
  4595. {
  4596. struct btrfs_caching_control *caching_ctl;
  4597. DEFINE_WAIT(wait);
  4598. caching_ctl = get_caching_control(cache);
  4599. if (!caching_ctl)
  4600. return 0;
  4601. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  4602. (cache->free_space_ctl->free_space >= num_bytes));
  4603. put_caching_control(caching_ctl);
  4604. return 0;
  4605. }
  4606. static noinline int
  4607. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  4608. {
  4609. struct btrfs_caching_control *caching_ctl;
  4610. DEFINE_WAIT(wait);
  4611. caching_ctl = get_caching_control(cache);
  4612. if (!caching_ctl)
  4613. return 0;
  4614. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  4615. put_caching_control(caching_ctl);
  4616. return 0;
  4617. }
  4618. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  4619. {
  4620. int index;
  4621. if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
  4622. index = 0;
  4623. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
  4624. index = 1;
  4625. else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
  4626. index = 2;
  4627. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
  4628. index = 3;
  4629. else
  4630. index = 4;
  4631. return index;
  4632. }
  4633. enum btrfs_loop_type {
  4634. LOOP_CACHING_NOWAIT = 0,
  4635. LOOP_CACHING_WAIT = 1,
  4636. LOOP_ALLOC_CHUNK = 2,
  4637. LOOP_NO_EMPTY_SIZE = 3,
  4638. };
  4639. /*
  4640. * walks the btree of allocated extents and find a hole of a given size.
  4641. * The key ins is changed to record the hole:
  4642. * ins->objectid == block start
  4643. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  4644. * ins->offset == number of blocks
  4645. * Any available blocks before search_start are skipped.
  4646. */
  4647. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  4648. struct btrfs_root *orig_root,
  4649. u64 num_bytes, u64 empty_size,
  4650. u64 search_start, u64 search_end,
  4651. u64 hint_byte, struct btrfs_key *ins,
  4652. u64 data)
  4653. {
  4654. int ret = 0;
  4655. struct btrfs_root *root = orig_root->fs_info->extent_root;
  4656. struct btrfs_free_cluster *last_ptr = NULL;
  4657. struct btrfs_block_group_cache *block_group = NULL;
  4658. struct btrfs_block_group_cache *used_block_group;
  4659. int empty_cluster = 2 * 1024 * 1024;
  4660. int allowed_chunk_alloc = 0;
  4661. int done_chunk_alloc = 0;
  4662. struct btrfs_space_info *space_info;
  4663. int loop = 0;
  4664. int index = 0;
  4665. int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
  4666. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  4667. bool found_uncached_bg = false;
  4668. bool failed_cluster_refill = false;
  4669. bool failed_alloc = false;
  4670. bool use_cluster = true;
  4671. bool have_caching_bg = false;
  4672. WARN_ON(num_bytes < root->sectorsize);
  4673. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  4674. ins->objectid = 0;
  4675. ins->offset = 0;
  4676. trace_find_free_extent(orig_root, num_bytes, empty_size, data);
  4677. space_info = __find_space_info(root->fs_info, data);
  4678. if (!space_info) {
  4679. printk(KERN_ERR "No space info for %llu\n", data);
  4680. return -ENOSPC;
  4681. }
  4682. /*
  4683. * If the space info is for both data and metadata it means we have a
  4684. * small filesystem and we can't use the clustering stuff.
  4685. */
  4686. if (btrfs_mixed_space_info(space_info))
  4687. use_cluster = false;
  4688. if (orig_root->ref_cows || empty_size)
  4689. allowed_chunk_alloc = 1;
  4690. if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  4691. last_ptr = &root->fs_info->meta_alloc_cluster;
  4692. if (!btrfs_test_opt(root, SSD))
  4693. empty_cluster = 64 * 1024;
  4694. }
  4695. if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  4696. btrfs_test_opt(root, SSD)) {
  4697. last_ptr = &root->fs_info->data_alloc_cluster;
  4698. }
  4699. if (last_ptr) {
  4700. spin_lock(&last_ptr->lock);
  4701. if (last_ptr->block_group)
  4702. hint_byte = last_ptr->window_start;
  4703. spin_unlock(&last_ptr->lock);
  4704. }
  4705. search_start = max(search_start, first_logical_byte(root, 0));
  4706. search_start = max(search_start, hint_byte);
  4707. if (!last_ptr)
  4708. empty_cluster = 0;
  4709. if (search_start == hint_byte) {
  4710. block_group = btrfs_lookup_block_group(root->fs_info,
  4711. search_start);
  4712. used_block_group = block_group;
  4713. /*
  4714. * we don't want to use the block group if it doesn't match our
  4715. * allocation bits, or if its not cached.
  4716. *
  4717. * However if we are re-searching with an ideal block group
  4718. * picked out then we don't care that the block group is cached.
  4719. */
  4720. if (block_group && block_group_bits(block_group, data) &&
  4721. block_group->cached != BTRFS_CACHE_NO) {
  4722. down_read(&space_info->groups_sem);
  4723. if (list_empty(&block_group->list) ||
  4724. block_group->ro) {
  4725. /*
  4726. * someone is removing this block group,
  4727. * we can't jump into the have_block_group
  4728. * target because our list pointers are not
  4729. * valid
  4730. */
  4731. btrfs_put_block_group(block_group);
  4732. up_read(&space_info->groups_sem);
  4733. } else {
  4734. index = get_block_group_index(block_group);
  4735. goto have_block_group;
  4736. }
  4737. } else if (block_group) {
  4738. btrfs_put_block_group(block_group);
  4739. }
  4740. }
  4741. search:
  4742. have_caching_bg = false;
  4743. down_read(&space_info->groups_sem);
  4744. list_for_each_entry(block_group, &space_info->block_groups[index],
  4745. list) {
  4746. u64 offset;
  4747. int cached;
  4748. used_block_group = block_group;
  4749. btrfs_get_block_group(block_group);
  4750. search_start = block_group->key.objectid;
  4751. /*
  4752. * this can happen if we end up cycling through all the
  4753. * raid types, but we want to make sure we only allocate
  4754. * for the proper type.
  4755. */
  4756. if (!block_group_bits(block_group, data)) {
  4757. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  4758. BTRFS_BLOCK_GROUP_RAID1 |
  4759. BTRFS_BLOCK_GROUP_RAID10;
  4760. /*
  4761. * if they asked for extra copies and this block group
  4762. * doesn't provide them, bail. This does allow us to
  4763. * fill raid0 from raid1.
  4764. */
  4765. if ((data & extra) && !(block_group->flags & extra))
  4766. goto loop;
  4767. }
  4768. have_block_group:
  4769. cached = block_group_cache_done(block_group);
  4770. if (unlikely(!cached)) {
  4771. found_uncached_bg = true;
  4772. ret = cache_block_group(block_group, trans,
  4773. orig_root, 0);
  4774. BUG_ON(ret);
  4775. }
  4776. if (unlikely(block_group->ro))
  4777. goto loop;
  4778. /*
  4779. * Ok we want to try and use the cluster allocator, so
  4780. * lets look there
  4781. */
  4782. if (last_ptr) {
  4783. /*
  4784. * the refill lock keeps out other
  4785. * people trying to start a new cluster
  4786. */
  4787. spin_lock(&last_ptr->refill_lock);
  4788. used_block_group = last_ptr->block_group;
  4789. if (used_block_group != block_group &&
  4790. (!used_block_group ||
  4791. used_block_group->ro ||
  4792. !block_group_bits(used_block_group, data))) {
  4793. used_block_group = block_group;
  4794. goto refill_cluster;
  4795. }
  4796. if (used_block_group != block_group)
  4797. btrfs_get_block_group(used_block_group);
  4798. offset = btrfs_alloc_from_cluster(used_block_group,
  4799. last_ptr, num_bytes, used_block_group->key.objectid);
  4800. if (offset) {
  4801. /* we have a block, we're done */
  4802. spin_unlock(&last_ptr->refill_lock);
  4803. trace_btrfs_reserve_extent_cluster(root,
  4804. block_group, search_start, num_bytes);
  4805. goto checks;
  4806. }
  4807. WARN_ON(last_ptr->block_group != used_block_group);
  4808. if (used_block_group != block_group) {
  4809. btrfs_put_block_group(used_block_group);
  4810. used_block_group = block_group;
  4811. }
  4812. refill_cluster:
  4813. BUG_ON(used_block_group != block_group);
  4814. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  4815. * set up a new clusters, so lets just skip it
  4816. * and let the allocator find whatever block
  4817. * it can find. If we reach this point, we
  4818. * will have tried the cluster allocator
  4819. * plenty of times and not have found
  4820. * anything, so we are likely way too
  4821. * fragmented for the clustering stuff to find
  4822. * anything.
  4823. *
  4824. * However, if the cluster is taken from the
  4825. * current block group, release the cluster
  4826. * first, so that we stand a better chance of
  4827. * succeeding in the unclustered
  4828. * allocation. */
  4829. if (loop >= LOOP_NO_EMPTY_SIZE &&
  4830. last_ptr->block_group != block_group) {
  4831. spin_unlock(&last_ptr->refill_lock);
  4832. goto unclustered_alloc;
  4833. }
  4834. /*
  4835. * this cluster didn't work out, free it and
  4836. * start over
  4837. */
  4838. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4839. if (loop >= LOOP_NO_EMPTY_SIZE) {
  4840. spin_unlock(&last_ptr->refill_lock);
  4841. goto unclustered_alloc;
  4842. }
  4843. /* allocate a cluster in this block group */
  4844. ret = btrfs_find_space_cluster(trans, root,
  4845. block_group, last_ptr,
  4846. search_start, num_bytes,
  4847. empty_cluster + empty_size);
  4848. if (ret == 0) {
  4849. /*
  4850. * now pull our allocation out of this
  4851. * cluster
  4852. */
  4853. offset = btrfs_alloc_from_cluster(block_group,
  4854. last_ptr, num_bytes,
  4855. search_start);
  4856. if (offset) {
  4857. /* we found one, proceed */
  4858. spin_unlock(&last_ptr->refill_lock);
  4859. trace_btrfs_reserve_extent_cluster(root,
  4860. block_group, search_start,
  4861. num_bytes);
  4862. goto checks;
  4863. }
  4864. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  4865. && !failed_cluster_refill) {
  4866. spin_unlock(&last_ptr->refill_lock);
  4867. failed_cluster_refill = true;
  4868. wait_block_group_cache_progress(block_group,
  4869. num_bytes + empty_cluster + empty_size);
  4870. goto have_block_group;
  4871. }
  4872. /*
  4873. * at this point we either didn't find a cluster
  4874. * or we weren't able to allocate a block from our
  4875. * cluster. Free the cluster we've been trying
  4876. * to use, and go to the next block group
  4877. */
  4878. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4879. spin_unlock(&last_ptr->refill_lock);
  4880. goto loop;
  4881. }
  4882. unclustered_alloc:
  4883. spin_lock(&block_group->free_space_ctl->tree_lock);
  4884. if (cached &&
  4885. block_group->free_space_ctl->free_space <
  4886. num_bytes + empty_cluster + empty_size) {
  4887. spin_unlock(&block_group->free_space_ctl->tree_lock);
  4888. goto loop;
  4889. }
  4890. spin_unlock(&block_group->free_space_ctl->tree_lock);
  4891. offset = btrfs_find_space_for_alloc(block_group, search_start,
  4892. num_bytes, empty_size);
  4893. /*
  4894. * If we didn't find a chunk, and we haven't failed on this
  4895. * block group before, and this block group is in the middle of
  4896. * caching and we are ok with waiting, then go ahead and wait
  4897. * for progress to be made, and set failed_alloc to true.
  4898. *
  4899. * If failed_alloc is true then we've already waited on this
  4900. * block group once and should move on to the next block group.
  4901. */
  4902. if (!offset && !failed_alloc && !cached &&
  4903. loop > LOOP_CACHING_NOWAIT) {
  4904. wait_block_group_cache_progress(block_group,
  4905. num_bytes + empty_size);
  4906. failed_alloc = true;
  4907. goto have_block_group;
  4908. } else if (!offset) {
  4909. if (!cached)
  4910. have_caching_bg = true;
  4911. goto loop;
  4912. }
  4913. checks:
  4914. search_start = stripe_align(root, offset);
  4915. /* move on to the next group */
  4916. if (search_start + num_bytes >= search_end) {
  4917. btrfs_add_free_space(used_block_group, offset, num_bytes);
  4918. goto loop;
  4919. }
  4920. /* move on to the next group */
  4921. if (search_start + num_bytes >
  4922. used_block_group->key.objectid + used_block_group->key.offset) {
  4923. btrfs_add_free_space(used_block_group, offset, num_bytes);
  4924. goto loop;
  4925. }
  4926. if (offset < search_start)
  4927. btrfs_add_free_space(used_block_group, offset,
  4928. search_start - offset);
  4929. BUG_ON(offset > search_start);
  4930. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  4931. alloc_type);
  4932. if (ret == -EAGAIN) {
  4933. btrfs_add_free_space(used_block_group, offset, num_bytes);
  4934. goto loop;
  4935. }
  4936. /* we are all good, lets return */
  4937. ins->objectid = search_start;
  4938. ins->offset = num_bytes;
  4939. trace_btrfs_reserve_extent(orig_root, block_group,
  4940. search_start, num_bytes);
  4941. if (offset < search_start)
  4942. btrfs_add_free_space(used_block_group, offset,
  4943. search_start - offset);
  4944. BUG_ON(offset > search_start);
  4945. if (used_block_group != block_group)
  4946. btrfs_put_block_group(used_block_group);
  4947. btrfs_put_block_group(block_group);
  4948. break;
  4949. loop:
  4950. failed_cluster_refill = false;
  4951. failed_alloc = false;
  4952. BUG_ON(index != get_block_group_index(block_group));
  4953. if (used_block_group != block_group)
  4954. btrfs_put_block_group(used_block_group);
  4955. btrfs_put_block_group(block_group);
  4956. }
  4957. up_read(&space_info->groups_sem);
  4958. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  4959. goto search;
  4960. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  4961. goto search;
  4962. /*
  4963. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  4964. * caching kthreads as we move along
  4965. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  4966. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  4967. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  4968. * again
  4969. */
  4970. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  4971. index = 0;
  4972. loop++;
  4973. if (loop == LOOP_ALLOC_CHUNK) {
  4974. if (allowed_chunk_alloc) {
  4975. ret = do_chunk_alloc(trans, root, num_bytes +
  4976. 2 * 1024 * 1024, data,
  4977. CHUNK_ALLOC_LIMITED);
  4978. allowed_chunk_alloc = 0;
  4979. if (ret == 1)
  4980. done_chunk_alloc = 1;
  4981. } else if (!done_chunk_alloc &&
  4982. space_info->force_alloc ==
  4983. CHUNK_ALLOC_NO_FORCE) {
  4984. space_info->force_alloc = CHUNK_ALLOC_LIMITED;
  4985. }
  4986. /*
  4987. * We didn't allocate a chunk, go ahead and drop the
  4988. * empty size and loop again.
  4989. */
  4990. if (!done_chunk_alloc)
  4991. loop = LOOP_NO_EMPTY_SIZE;
  4992. }
  4993. if (loop == LOOP_NO_EMPTY_SIZE) {
  4994. empty_size = 0;
  4995. empty_cluster = 0;
  4996. }
  4997. goto search;
  4998. } else if (!ins->objectid) {
  4999. ret = -ENOSPC;
  5000. } else if (ins->objectid) {
  5001. ret = 0;
  5002. }
  5003. return ret;
  5004. }
  5005. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5006. int dump_block_groups)
  5007. {
  5008. struct btrfs_block_group_cache *cache;
  5009. int index = 0;
  5010. spin_lock(&info->lock);
  5011. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5012. (unsigned long long)info->flags,
  5013. (unsigned long long)(info->total_bytes - info->bytes_used -
  5014. info->bytes_pinned - info->bytes_reserved -
  5015. info->bytes_readonly),
  5016. (info->full) ? "" : "not ");
  5017. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5018. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5019. (unsigned long long)info->total_bytes,
  5020. (unsigned long long)info->bytes_used,
  5021. (unsigned long long)info->bytes_pinned,
  5022. (unsigned long long)info->bytes_reserved,
  5023. (unsigned long long)info->bytes_may_use,
  5024. (unsigned long long)info->bytes_readonly);
  5025. spin_unlock(&info->lock);
  5026. if (!dump_block_groups)
  5027. return;
  5028. down_read(&info->groups_sem);
  5029. again:
  5030. list_for_each_entry(cache, &info->block_groups[index], list) {
  5031. spin_lock(&cache->lock);
  5032. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  5033. "%llu pinned %llu reserved\n",
  5034. (unsigned long long)cache->key.objectid,
  5035. (unsigned long long)cache->key.offset,
  5036. (unsigned long long)btrfs_block_group_used(&cache->item),
  5037. (unsigned long long)cache->pinned,
  5038. (unsigned long long)cache->reserved);
  5039. btrfs_dump_free_space(cache, bytes);
  5040. spin_unlock(&cache->lock);
  5041. }
  5042. if (++index < BTRFS_NR_RAID_TYPES)
  5043. goto again;
  5044. up_read(&info->groups_sem);
  5045. }
  5046. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  5047. struct btrfs_root *root,
  5048. u64 num_bytes, u64 min_alloc_size,
  5049. u64 empty_size, u64 hint_byte,
  5050. u64 search_end, struct btrfs_key *ins,
  5051. u64 data)
  5052. {
  5053. bool final_tried = false;
  5054. int ret;
  5055. u64 search_start = 0;
  5056. data = btrfs_get_alloc_profile(root, data);
  5057. again:
  5058. /*
  5059. * the only place that sets empty_size is btrfs_realloc_node, which
  5060. * is not called recursively on allocations
  5061. */
  5062. if (empty_size || root->ref_cows)
  5063. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  5064. num_bytes + 2 * 1024 * 1024, data,
  5065. CHUNK_ALLOC_NO_FORCE);
  5066. WARN_ON(num_bytes < root->sectorsize);
  5067. ret = find_free_extent(trans, root, num_bytes, empty_size,
  5068. search_start, search_end, hint_byte,
  5069. 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. btrfs_set_lock_blocking(buf);
  5317. btrfs_set_buffer_uptodate(buf);
  5318. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  5319. /*
  5320. * we allow two log transactions at a time, use different
  5321. * EXENT bit to differentiate dirty pages.
  5322. */
  5323. if (root->log_transid % 2 == 0)
  5324. set_extent_dirty(&root->dirty_log_pages, buf->start,
  5325. buf->start + buf->len - 1, GFP_NOFS);
  5326. else
  5327. set_extent_new(&root->dirty_log_pages, buf->start,
  5328. buf->start + buf->len - 1, GFP_NOFS);
  5329. } else {
  5330. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  5331. buf->start + buf->len - 1, GFP_NOFS);
  5332. }
  5333. trans->blocks_used++;
  5334. /* this returns a buffer locked for blocking */
  5335. return buf;
  5336. }
  5337. static struct btrfs_block_rsv *
  5338. use_block_rsv(struct btrfs_trans_handle *trans,
  5339. struct btrfs_root *root, u32 blocksize)
  5340. {
  5341. struct btrfs_block_rsv *block_rsv;
  5342. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  5343. int ret;
  5344. block_rsv = get_block_rsv(trans, root);
  5345. if (block_rsv->size == 0) {
  5346. ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0);
  5347. /*
  5348. * If we couldn't reserve metadata bytes try and use some from
  5349. * the global reserve.
  5350. */
  5351. if (ret && block_rsv != global_rsv) {
  5352. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5353. if (!ret)
  5354. return global_rsv;
  5355. return ERR_PTR(ret);
  5356. } else if (ret) {
  5357. return ERR_PTR(ret);
  5358. }
  5359. return block_rsv;
  5360. }
  5361. ret = block_rsv_use_bytes(block_rsv, blocksize);
  5362. if (!ret)
  5363. return block_rsv;
  5364. if (ret) {
  5365. static DEFINE_RATELIMIT_STATE(_rs,
  5366. DEFAULT_RATELIMIT_INTERVAL,
  5367. /*DEFAULT_RATELIMIT_BURST*/ 2);
  5368. if (__ratelimit(&_rs)) {
  5369. printk(KERN_DEBUG "btrfs: block rsv returned %d\n", ret);
  5370. WARN_ON(1);
  5371. }
  5372. ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0);
  5373. if (!ret) {
  5374. return block_rsv;
  5375. } else if (ret && block_rsv != global_rsv) {
  5376. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5377. if (!ret)
  5378. return global_rsv;
  5379. }
  5380. }
  5381. return ERR_PTR(-ENOSPC);
  5382. }
  5383. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  5384. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  5385. {
  5386. block_rsv_add_bytes(block_rsv, blocksize, 0);
  5387. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  5388. }
  5389. /*
  5390. * finds a free extent and does all the dirty work required for allocation
  5391. * returns the key for the extent through ins, and a tree buffer for
  5392. * the first block of the extent through buf.
  5393. *
  5394. * returns the tree buffer or NULL.
  5395. */
  5396. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  5397. struct btrfs_root *root, u32 blocksize,
  5398. u64 parent, u64 root_objectid,
  5399. struct btrfs_disk_key *key, int level,
  5400. u64 hint, u64 empty_size, int for_cow)
  5401. {
  5402. struct btrfs_key ins;
  5403. struct btrfs_block_rsv *block_rsv;
  5404. struct extent_buffer *buf;
  5405. u64 flags = 0;
  5406. int ret;
  5407. block_rsv = use_block_rsv(trans, root, blocksize);
  5408. if (IS_ERR(block_rsv))
  5409. return ERR_CAST(block_rsv);
  5410. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  5411. empty_size, hint, (u64)-1, &ins, 0);
  5412. if (ret) {
  5413. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  5414. return ERR_PTR(ret);
  5415. }
  5416. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  5417. blocksize, level);
  5418. BUG_ON(IS_ERR(buf));
  5419. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  5420. if (parent == 0)
  5421. parent = ins.objectid;
  5422. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5423. } else
  5424. BUG_ON(parent > 0);
  5425. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  5426. struct btrfs_delayed_extent_op *extent_op;
  5427. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  5428. BUG_ON(!extent_op);
  5429. if (key)
  5430. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  5431. else
  5432. memset(&extent_op->key, 0, sizeof(extent_op->key));
  5433. extent_op->flags_to_set = flags;
  5434. extent_op->update_key = 1;
  5435. extent_op->update_flags = 1;
  5436. extent_op->is_data = 0;
  5437. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5438. ins.objectid,
  5439. ins.offset, parent, root_objectid,
  5440. level, BTRFS_ADD_DELAYED_EXTENT,
  5441. extent_op, for_cow);
  5442. BUG_ON(ret);
  5443. }
  5444. return buf;
  5445. }
  5446. struct walk_control {
  5447. u64 refs[BTRFS_MAX_LEVEL];
  5448. u64 flags[BTRFS_MAX_LEVEL];
  5449. struct btrfs_key update_progress;
  5450. int stage;
  5451. int level;
  5452. int shared_level;
  5453. int update_ref;
  5454. int keep_locks;
  5455. int reada_slot;
  5456. int reada_count;
  5457. int for_reloc;
  5458. };
  5459. #define DROP_REFERENCE 1
  5460. #define UPDATE_BACKREF 2
  5461. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  5462. struct btrfs_root *root,
  5463. struct walk_control *wc,
  5464. struct btrfs_path *path)
  5465. {
  5466. u64 bytenr;
  5467. u64 generation;
  5468. u64 refs;
  5469. u64 flags;
  5470. u32 nritems;
  5471. u32 blocksize;
  5472. struct btrfs_key key;
  5473. struct extent_buffer *eb;
  5474. int ret;
  5475. int slot;
  5476. int nread = 0;
  5477. if (path->slots[wc->level] < wc->reada_slot) {
  5478. wc->reada_count = wc->reada_count * 2 / 3;
  5479. wc->reada_count = max(wc->reada_count, 2);
  5480. } else {
  5481. wc->reada_count = wc->reada_count * 3 / 2;
  5482. wc->reada_count = min_t(int, wc->reada_count,
  5483. BTRFS_NODEPTRS_PER_BLOCK(root));
  5484. }
  5485. eb = path->nodes[wc->level];
  5486. nritems = btrfs_header_nritems(eb);
  5487. blocksize = btrfs_level_size(root, wc->level - 1);
  5488. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  5489. if (nread >= wc->reada_count)
  5490. break;
  5491. cond_resched();
  5492. bytenr = btrfs_node_blockptr(eb, slot);
  5493. generation = btrfs_node_ptr_generation(eb, slot);
  5494. if (slot == path->slots[wc->level])
  5495. goto reada;
  5496. if (wc->stage == UPDATE_BACKREF &&
  5497. generation <= root->root_key.offset)
  5498. continue;
  5499. /* We don't lock the tree block, it's OK to be racy here */
  5500. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5501. &refs, &flags);
  5502. BUG_ON(ret);
  5503. BUG_ON(refs == 0);
  5504. if (wc->stage == DROP_REFERENCE) {
  5505. if (refs == 1)
  5506. goto reada;
  5507. if (wc->level == 1 &&
  5508. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5509. continue;
  5510. if (!wc->update_ref ||
  5511. generation <= root->root_key.offset)
  5512. continue;
  5513. btrfs_node_key_to_cpu(eb, &key, slot);
  5514. ret = btrfs_comp_cpu_keys(&key,
  5515. &wc->update_progress);
  5516. if (ret < 0)
  5517. continue;
  5518. } else {
  5519. if (wc->level == 1 &&
  5520. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5521. continue;
  5522. }
  5523. reada:
  5524. ret = readahead_tree_block(root, bytenr, blocksize,
  5525. generation);
  5526. if (ret)
  5527. break;
  5528. nread++;
  5529. }
  5530. wc->reada_slot = slot;
  5531. }
  5532. /*
  5533. * hepler to process tree block while walking down the tree.
  5534. *
  5535. * when wc->stage == UPDATE_BACKREF, this function updates
  5536. * back refs for pointers in the block.
  5537. *
  5538. * NOTE: return value 1 means we should stop walking down.
  5539. */
  5540. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  5541. struct btrfs_root *root,
  5542. struct btrfs_path *path,
  5543. struct walk_control *wc, int lookup_info)
  5544. {
  5545. int level = wc->level;
  5546. struct extent_buffer *eb = path->nodes[level];
  5547. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5548. int ret;
  5549. if (wc->stage == UPDATE_BACKREF &&
  5550. btrfs_header_owner(eb) != root->root_key.objectid)
  5551. return 1;
  5552. /*
  5553. * when reference count of tree block is 1, it won't increase
  5554. * again. once full backref flag is set, we never clear it.
  5555. */
  5556. if (lookup_info &&
  5557. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  5558. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  5559. BUG_ON(!path->locks[level]);
  5560. ret = btrfs_lookup_extent_info(trans, root,
  5561. eb->start, eb->len,
  5562. &wc->refs[level],
  5563. &wc->flags[level]);
  5564. BUG_ON(ret);
  5565. BUG_ON(wc->refs[level] == 0);
  5566. }
  5567. if (wc->stage == DROP_REFERENCE) {
  5568. if (wc->refs[level] > 1)
  5569. return 1;
  5570. if (path->locks[level] && !wc->keep_locks) {
  5571. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5572. path->locks[level] = 0;
  5573. }
  5574. return 0;
  5575. }
  5576. /* wc->stage == UPDATE_BACKREF */
  5577. if (!(wc->flags[level] & flag)) {
  5578. BUG_ON(!path->locks[level]);
  5579. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  5580. BUG_ON(ret);
  5581. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  5582. BUG_ON(ret);
  5583. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  5584. eb->len, flag, 0);
  5585. BUG_ON(ret);
  5586. wc->flags[level] |= flag;
  5587. }
  5588. /*
  5589. * the block is shared by multiple trees, so it's not good to
  5590. * keep the tree lock
  5591. */
  5592. if (path->locks[level] && level > 0) {
  5593. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5594. path->locks[level] = 0;
  5595. }
  5596. return 0;
  5597. }
  5598. /*
  5599. * hepler to process tree block pointer.
  5600. *
  5601. * when wc->stage == DROP_REFERENCE, this function checks
  5602. * reference count of the block pointed to. if the block
  5603. * is shared and we need update back refs for the subtree
  5604. * rooted at the block, this function changes wc->stage to
  5605. * UPDATE_BACKREF. if the block is shared and there is no
  5606. * need to update back, this function drops the reference
  5607. * to the block.
  5608. *
  5609. * NOTE: return value 1 means we should stop walking down.
  5610. */
  5611. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  5612. struct btrfs_root *root,
  5613. struct btrfs_path *path,
  5614. struct walk_control *wc, int *lookup_info)
  5615. {
  5616. u64 bytenr;
  5617. u64 generation;
  5618. u64 parent;
  5619. u32 blocksize;
  5620. struct btrfs_key key;
  5621. struct extent_buffer *next;
  5622. int level = wc->level;
  5623. int reada = 0;
  5624. int ret = 0;
  5625. generation = btrfs_node_ptr_generation(path->nodes[level],
  5626. path->slots[level]);
  5627. /*
  5628. * if the lower level block was created before the snapshot
  5629. * was created, we know there is no need to update back refs
  5630. * for the subtree
  5631. */
  5632. if (wc->stage == UPDATE_BACKREF &&
  5633. generation <= root->root_key.offset) {
  5634. *lookup_info = 1;
  5635. return 1;
  5636. }
  5637. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  5638. blocksize = btrfs_level_size(root, level - 1);
  5639. next = btrfs_find_tree_block(root, bytenr, blocksize);
  5640. if (!next) {
  5641. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5642. if (!next)
  5643. return -ENOMEM;
  5644. reada = 1;
  5645. }
  5646. btrfs_tree_lock(next);
  5647. btrfs_set_lock_blocking(next);
  5648. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5649. &wc->refs[level - 1],
  5650. &wc->flags[level - 1]);
  5651. BUG_ON(ret);
  5652. BUG_ON(wc->refs[level - 1] == 0);
  5653. *lookup_info = 0;
  5654. if (wc->stage == DROP_REFERENCE) {
  5655. if (wc->refs[level - 1] > 1) {
  5656. if (level == 1 &&
  5657. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5658. goto skip;
  5659. if (!wc->update_ref ||
  5660. generation <= root->root_key.offset)
  5661. goto skip;
  5662. btrfs_node_key_to_cpu(path->nodes[level], &key,
  5663. path->slots[level]);
  5664. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  5665. if (ret < 0)
  5666. goto skip;
  5667. wc->stage = UPDATE_BACKREF;
  5668. wc->shared_level = level - 1;
  5669. }
  5670. } else {
  5671. if (level == 1 &&
  5672. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5673. goto skip;
  5674. }
  5675. if (!btrfs_buffer_uptodate(next, generation)) {
  5676. btrfs_tree_unlock(next);
  5677. free_extent_buffer(next);
  5678. next = NULL;
  5679. *lookup_info = 1;
  5680. }
  5681. if (!next) {
  5682. if (reada && level == 1)
  5683. reada_walk_down(trans, root, wc, path);
  5684. next = read_tree_block(root, bytenr, blocksize, generation);
  5685. if (!next)
  5686. return -EIO;
  5687. btrfs_tree_lock(next);
  5688. btrfs_set_lock_blocking(next);
  5689. }
  5690. level--;
  5691. BUG_ON(level != btrfs_header_level(next));
  5692. path->nodes[level] = next;
  5693. path->slots[level] = 0;
  5694. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5695. wc->level = level;
  5696. if (wc->level == 1)
  5697. wc->reada_slot = 0;
  5698. return 0;
  5699. skip:
  5700. wc->refs[level - 1] = 0;
  5701. wc->flags[level - 1] = 0;
  5702. if (wc->stage == DROP_REFERENCE) {
  5703. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  5704. parent = path->nodes[level]->start;
  5705. } else {
  5706. BUG_ON(root->root_key.objectid !=
  5707. btrfs_header_owner(path->nodes[level]));
  5708. parent = 0;
  5709. }
  5710. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  5711. root->root_key.objectid, level - 1, 0, 0);
  5712. BUG_ON(ret);
  5713. }
  5714. btrfs_tree_unlock(next);
  5715. free_extent_buffer(next);
  5716. *lookup_info = 1;
  5717. return 1;
  5718. }
  5719. /*
  5720. * hepler to process tree block while walking up the tree.
  5721. *
  5722. * when wc->stage == DROP_REFERENCE, this function drops
  5723. * reference count on the block.
  5724. *
  5725. * when wc->stage == UPDATE_BACKREF, this function changes
  5726. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  5727. * to UPDATE_BACKREF previously while processing the block.
  5728. *
  5729. * NOTE: return value 1 means we should stop walking up.
  5730. */
  5731. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  5732. struct btrfs_root *root,
  5733. struct btrfs_path *path,
  5734. struct walk_control *wc)
  5735. {
  5736. int ret;
  5737. int level = wc->level;
  5738. struct extent_buffer *eb = path->nodes[level];
  5739. u64 parent = 0;
  5740. if (wc->stage == UPDATE_BACKREF) {
  5741. BUG_ON(wc->shared_level < level);
  5742. if (level < wc->shared_level)
  5743. goto out;
  5744. ret = find_next_key(path, level + 1, &wc->update_progress);
  5745. if (ret > 0)
  5746. wc->update_ref = 0;
  5747. wc->stage = DROP_REFERENCE;
  5748. wc->shared_level = -1;
  5749. path->slots[level] = 0;
  5750. /*
  5751. * check reference count again if the block isn't locked.
  5752. * we should start walking down the tree again if reference
  5753. * count is one.
  5754. */
  5755. if (!path->locks[level]) {
  5756. BUG_ON(level == 0);
  5757. btrfs_tree_lock(eb);
  5758. btrfs_set_lock_blocking(eb);
  5759. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5760. ret = btrfs_lookup_extent_info(trans, root,
  5761. eb->start, eb->len,
  5762. &wc->refs[level],
  5763. &wc->flags[level]);
  5764. BUG_ON(ret);
  5765. BUG_ON(wc->refs[level] == 0);
  5766. if (wc->refs[level] == 1) {
  5767. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5768. return 1;
  5769. }
  5770. }
  5771. }
  5772. /* wc->stage == DROP_REFERENCE */
  5773. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  5774. if (wc->refs[level] == 1) {
  5775. if (level == 0) {
  5776. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5777. ret = btrfs_dec_ref(trans, root, eb, 1,
  5778. wc->for_reloc);
  5779. else
  5780. ret = btrfs_dec_ref(trans, root, eb, 0,
  5781. wc->for_reloc);
  5782. BUG_ON(ret);
  5783. }
  5784. /* make block locked assertion in clean_tree_block happy */
  5785. if (!path->locks[level] &&
  5786. btrfs_header_generation(eb) == trans->transid) {
  5787. btrfs_tree_lock(eb);
  5788. btrfs_set_lock_blocking(eb);
  5789. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5790. }
  5791. clean_tree_block(trans, root, eb);
  5792. }
  5793. if (eb == root->node) {
  5794. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5795. parent = eb->start;
  5796. else
  5797. BUG_ON(root->root_key.objectid !=
  5798. btrfs_header_owner(eb));
  5799. } else {
  5800. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5801. parent = path->nodes[level + 1]->start;
  5802. else
  5803. BUG_ON(root->root_key.objectid !=
  5804. btrfs_header_owner(path->nodes[level + 1]));
  5805. }
  5806. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1, 0);
  5807. out:
  5808. wc->refs[level] = 0;
  5809. wc->flags[level] = 0;
  5810. return 0;
  5811. }
  5812. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  5813. struct btrfs_root *root,
  5814. struct btrfs_path *path,
  5815. struct walk_control *wc)
  5816. {
  5817. int level = wc->level;
  5818. int lookup_info = 1;
  5819. int ret;
  5820. while (level >= 0) {
  5821. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  5822. if (ret > 0)
  5823. break;
  5824. if (level == 0)
  5825. break;
  5826. if (path->slots[level] >=
  5827. btrfs_header_nritems(path->nodes[level]))
  5828. break;
  5829. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  5830. if (ret > 0) {
  5831. path->slots[level]++;
  5832. continue;
  5833. } else if (ret < 0)
  5834. return ret;
  5835. level = wc->level;
  5836. }
  5837. return 0;
  5838. }
  5839. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  5840. struct btrfs_root *root,
  5841. struct btrfs_path *path,
  5842. struct walk_control *wc, int max_level)
  5843. {
  5844. int level = wc->level;
  5845. int ret;
  5846. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  5847. while (level < max_level && path->nodes[level]) {
  5848. wc->level = level;
  5849. if (path->slots[level] + 1 <
  5850. btrfs_header_nritems(path->nodes[level])) {
  5851. path->slots[level]++;
  5852. return 0;
  5853. } else {
  5854. ret = walk_up_proc(trans, root, path, wc);
  5855. if (ret > 0)
  5856. return 0;
  5857. if (path->locks[level]) {
  5858. btrfs_tree_unlock_rw(path->nodes[level],
  5859. path->locks[level]);
  5860. path->locks[level] = 0;
  5861. }
  5862. free_extent_buffer(path->nodes[level]);
  5863. path->nodes[level] = NULL;
  5864. level++;
  5865. }
  5866. }
  5867. return 1;
  5868. }
  5869. /*
  5870. * drop a subvolume tree.
  5871. *
  5872. * this function traverses the tree freeing any blocks that only
  5873. * referenced by the tree.
  5874. *
  5875. * when a shared tree block is found. this function decreases its
  5876. * reference count by one. if update_ref is true, this function
  5877. * also make sure backrefs for the shared block and all lower level
  5878. * blocks are properly updated.
  5879. */
  5880. void btrfs_drop_snapshot(struct btrfs_root *root,
  5881. struct btrfs_block_rsv *block_rsv, int update_ref,
  5882. int for_reloc)
  5883. {
  5884. struct btrfs_path *path;
  5885. struct btrfs_trans_handle *trans;
  5886. struct btrfs_root *tree_root = root->fs_info->tree_root;
  5887. struct btrfs_root_item *root_item = &root->root_item;
  5888. struct walk_control *wc;
  5889. struct btrfs_key key;
  5890. int err = 0;
  5891. int ret;
  5892. int level;
  5893. path = btrfs_alloc_path();
  5894. if (!path) {
  5895. err = -ENOMEM;
  5896. goto out;
  5897. }
  5898. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5899. if (!wc) {
  5900. btrfs_free_path(path);
  5901. err = -ENOMEM;
  5902. goto out;
  5903. }
  5904. trans = btrfs_start_transaction(tree_root, 0);
  5905. BUG_ON(IS_ERR(trans));
  5906. if (block_rsv)
  5907. trans->block_rsv = block_rsv;
  5908. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  5909. level = btrfs_header_level(root->node);
  5910. path->nodes[level] = btrfs_lock_root_node(root);
  5911. btrfs_set_lock_blocking(path->nodes[level]);
  5912. path->slots[level] = 0;
  5913. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5914. memset(&wc->update_progress, 0,
  5915. sizeof(wc->update_progress));
  5916. } else {
  5917. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  5918. memcpy(&wc->update_progress, &key,
  5919. sizeof(wc->update_progress));
  5920. level = root_item->drop_level;
  5921. BUG_ON(level == 0);
  5922. path->lowest_level = level;
  5923. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5924. path->lowest_level = 0;
  5925. if (ret < 0) {
  5926. err = ret;
  5927. goto out_free;
  5928. }
  5929. WARN_ON(ret > 0);
  5930. /*
  5931. * unlock our path, this is safe because only this
  5932. * function is allowed to delete this snapshot
  5933. */
  5934. btrfs_unlock_up_safe(path, 0);
  5935. level = btrfs_header_level(root->node);
  5936. while (1) {
  5937. btrfs_tree_lock(path->nodes[level]);
  5938. btrfs_set_lock_blocking(path->nodes[level]);
  5939. ret = btrfs_lookup_extent_info(trans, root,
  5940. path->nodes[level]->start,
  5941. path->nodes[level]->len,
  5942. &wc->refs[level],
  5943. &wc->flags[level]);
  5944. BUG_ON(ret);
  5945. BUG_ON(wc->refs[level] == 0);
  5946. if (level == root_item->drop_level)
  5947. break;
  5948. btrfs_tree_unlock(path->nodes[level]);
  5949. WARN_ON(wc->refs[level] != 1);
  5950. level--;
  5951. }
  5952. }
  5953. wc->level = level;
  5954. wc->shared_level = -1;
  5955. wc->stage = DROP_REFERENCE;
  5956. wc->update_ref = update_ref;
  5957. wc->keep_locks = 0;
  5958. wc->for_reloc = for_reloc;
  5959. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  5960. while (1) {
  5961. ret = walk_down_tree(trans, root, path, wc);
  5962. if (ret < 0) {
  5963. err = ret;
  5964. break;
  5965. }
  5966. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  5967. if (ret < 0) {
  5968. err = ret;
  5969. break;
  5970. }
  5971. if (ret > 0) {
  5972. BUG_ON(wc->stage != DROP_REFERENCE);
  5973. break;
  5974. }
  5975. if (wc->stage == DROP_REFERENCE) {
  5976. level = wc->level;
  5977. btrfs_node_key(path->nodes[level],
  5978. &root_item->drop_progress,
  5979. path->slots[level]);
  5980. root_item->drop_level = level;
  5981. }
  5982. BUG_ON(wc->level == 0);
  5983. if (btrfs_should_end_transaction(trans, tree_root)) {
  5984. ret = btrfs_update_root(trans, tree_root,
  5985. &root->root_key,
  5986. root_item);
  5987. BUG_ON(ret);
  5988. btrfs_end_transaction_throttle(trans, tree_root);
  5989. trans = btrfs_start_transaction(tree_root, 0);
  5990. BUG_ON(IS_ERR(trans));
  5991. if (block_rsv)
  5992. trans->block_rsv = block_rsv;
  5993. }
  5994. }
  5995. btrfs_release_path(path);
  5996. BUG_ON(err);
  5997. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  5998. BUG_ON(ret);
  5999. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6000. ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
  6001. NULL, NULL);
  6002. BUG_ON(ret < 0);
  6003. if (ret > 0) {
  6004. /* if we fail to delete the orphan item this time
  6005. * around, it'll get picked up the next time.
  6006. *
  6007. * The most common failure here is just -ENOENT.
  6008. */
  6009. btrfs_del_orphan_item(trans, tree_root,
  6010. root->root_key.objectid);
  6011. }
  6012. }
  6013. if (root->in_radix) {
  6014. btrfs_free_fs_root(tree_root->fs_info, root);
  6015. } else {
  6016. free_extent_buffer(root->node);
  6017. free_extent_buffer(root->commit_root);
  6018. kfree(root);
  6019. }
  6020. out_free:
  6021. btrfs_end_transaction_throttle(trans, tree_root);
  6022. kfree(wc);
  6023. btrfs_free_path(path);
  6024. out:
  6025. if (err)
  6026. btrfs_std_error(root->fs_info, err);
  6027. return;
  6028. }
  6029. /*
  6030. * drop subtree rooted at tree block 'node'.
  6031. *
  6032. * NOTE: this function will unlock and release tree block 'node'
  6033. * only used by relocation code
  6034. */
  6035. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6036. struct btrfs_root *root,
  6037. struct extent_buffer *node,
  6038. struct extent_buffer *parent)
  6039. {
  6040. struct btrfs_path *path;
  6041. struct walk_control *wc;
  6042. int level;
  6043. int parent_level;
  6044. int ret = 0;
  6045. int wret;
  6046. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6047. path = btrfs_alloc_path();
  6048. if (!path)
  6049. return -ENOMEM;
  6050. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6051. if (!wc) {
  6052. btrfs_free_path(path);
  6053. return -ENOMEM;
  6054. }
  6055. btrfs_assert_tree_locked(parent);
  6056. parent_level = btrfs_header_level(parent);
  6057. extent_buffer_get(parent);
  6058. path->nodes[parent_level] = parent;
  6059. path->slots[parent_level] = btrfs_header_nritems(parent);
  6060. btrfs_assert_tree_locked(node);
  6061. level = btrfs_header_level(node);
  6062. path->nodes[level] = node;
  6063. path->slots[level] = 0;
  6064. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6065. wc->refs[parent_level] = 1;
  6066. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6067. wc->level = level;
  6068. wc->shared_level = -1;
  6069. wc->stage = DROP_REFERENCE;
  6070. wc->update_ref = 0;
  6071. wc->keep_locks = 1;
  6072. wc->for_reloc = 1;
  6073. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6074. while (1) {
  6075. wret = walk_down_tree(trans, root, path, wc);
  6076. if (wret < 0) {
  6077. ret = wret;
  6078. break;
  6079. }
  6080. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6081. if (wret < 0)
  6082. ret = wret;
  6083. if (wret != 0)
  6084. break;
  6085. }
  6086. kfree(wc);
  6087. btrfs_free_path(path);
  6088. return ret;
  6089. }
  6090. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6091. {
  6092. u64 num_devices;
  6093. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6094. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6095. if (root->fs_info->balance_ctl) {
  6096. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  6097. u64 tgt = 0;
  6098. /* pick restriper's target profile and return */
  6099. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  6100. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  6101. tgt = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  6102. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  6103. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  6104. tgt = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  6105. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  6106. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  6107. tgt = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  6108. }
  6109. if (tgt) {
  6110. /* extended -> chunk profile */
  6111. tgt &= ~BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  6112. return tgt;
  6113. }
  6114. }
  6115. /*
  6116. * we add in the count of missing devices because we want
  6117. * to make sure that any RAID levels on a degraded FS
  6118. * continue to be honored.
  6119. */
  6120. num_devices = root->fs_info->fs_devices->rw_devices +
  6121. root->fs_info->fs_devices->missing_devices;
  6122. if (num_devices == 1) {
  6123. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6124. stripped = flags & ~stripped;
  6125. /* turn raid0 into single device chunks */
  6126. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6127. return stripped;
  6128. /* turn mirroring into duplication */
  6129. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6130. BTRFS_BLOCK_GROUP_RAID10))
  6131. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6132. return flags;
  6133. } else {
  6134. /* they already had raid on here, just return */
  6135. if (flags & stripped)
  6136. return flags;
  6137. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6138. stripped = flags & ~stripped;
  6139. /* switch duplicated blocks with raid1 */
  6140. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6141. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6142. /* turn single device chunks into raid0 */
  6143. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  6144. }
  6145. return flags;
  6146. }
  6147. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6148. {
  6149. struct btrfs_space_info *sinfo = cache->space_info;
  6150. u64 num_bytes;
  6151. u64 min_allocable_bytes;
  6152. int ret = -ENOSPC;
  6153. /*
  6154. * We need some metadata space and system metadata space for
  6155. * allocating chunks in some corner cases until we force to set
  6156. * it to be readonly.
  6157. */
  6158. if ((sinfo->flags &
  6159. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6160. !force)
  6161. min_allocable_bytes = 1 * 1024 * 1024;
  6162. else
  6163. min_allocable_bytes = 0;
  6164. spin_lock(&sinfo->lock);
  6165. spin_lock(&cache->lock);
  6166. if (cache->ro) {
  6167. ret = 0;
  6168. goto out;
  6169. }
  6170. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6171. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6172. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6173. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6174. min_allocable_bytes <= sinfo->total_bytes) {
  6175. sinfo->bytes_readonly += num_bytes;
  6176. cache->ro = 1;
  6177. ret = 0;
  6178. }
  6179. out:
  6180. spin_unlock(&cache->lock);
  6181. spin_unlock(&sinfo->lock);
  6182. return ret;
  6183. }
  6184. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6185. struct btrfs_block_group_cache *cache)
  6186. {
  6187. struct btrfs_trans_handle *trans;
  6188. u64 alloc_flags;
  6189. int ret;
  6190. BUG_ON(cache->ro);
  6191. trans = btrfs_join_transaction(root);
  6192. BUG_ON(IS_ERR(trans));
  6193. alloc_flags = update_block_group_flags(root, cache->flags);
  6194. if (alloc_flags != cache->flags)
  6195. do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
  6196. CHUNK_ALLOC_FORCE);
  6197. ret = set_block_group_ro(cache, 0);
  6198. if (!ret)
  6199. goto out;
  6200. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6201. ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
  6202. CHUNK_ALLOC_FORCE);
  6203. if (ret < 0)
  6204. goto out;
  6205. ret = set_block_group_ro(cache, 0);
  6206. out:
  6207. btrfs_end_transaction(trans, root);
  6208. return ret;
  6209. }
  6210. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6211. struct btrfs_root *root, u64 type)
  6212. {
  6213. u64 alloc_flags = get_alloc_profile(root, type);
  6214. return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
  6215. CHUNK_ALLOC_FORCE);
  6216. }
  6217. /*
  6218. * helper to account the unused space of all the readonly block group in the
  6219. * list. takes mirrors into account.
  6220. */
  6221. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6222. {
  6223. struct btrfs_block_group_cache *block_group;
  6224. u64 free_bytes = 0;
  6225. int factor;
  6226. list_for_each_entry(block_group, groups_list, list) {
  6227. spin_lock(&block_group->lock);
  6228. if (!block_group->ro) {
  6229. spin_unlock(&block_group->lock);
  6230. continue;
  6231. }
  6232. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6233. BTRFS_BLOCK_GROUP_RAID10 |
  6234. BTRFS_BLOCK_GROUP_DUP))
  6235. factor = 2;
  6236. else
  6237. factor = 1;
  6238. free_bytes += (block_group->key.offset -
  6239. btrfs_block_group_used(&block_group->item)) *
  6240. factor;
  6241. spin_unlock(&block_group->lock);
  6242. }
  6243. return free_bytes;
  6244. }
  6245. /*
  6246. * helper to account the unused space of all the readonly block group in the
  6247. * space_info. takes mirrors into account.
  6248. */
  6249. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  6250. {
  6251. int i;
  6252. u64 free_bytes = 0;
  6253. spin_lock(&sinfo->lock);
  6254. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  6255. if (!list_empty(&sinfo->block_groups[i]))
  6256. free_bytes += __btrfs_get_ro_block_group_free_space(
  6257. &sinfo->block_groups[i]);
  6258. spin_unlock(&sinfo->lock);
  6259. return free_bytes;
  6260. }
  6261. int btrfs_set_block_group_rw(struct btrfs_root *root,
  6262. struct btrfs_block_group_cache *cache)
  6263. {
  6264. struct btrfs_space_info *sinfo = cache->space_info;
  6265. u64 num_bytes;
  6266. BUG_ON(!cache->ro);
  6267. spin_lock(&sinfo->lock);
  6268. spin_lock(&cache->lock);
  6269. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6270. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6271. sinfo->bytes_readonly -= num_bytes;
  6272. cache->ro = 0;
  6273. spin_unlock(&cache->lock);
  6274. spin_unlock(&sinfo->lock);
  6275. return 0;
  6276. }
  6277. /*
  6278. * checks to see if its even possible to relocate this block group.
  6279. *
  6280. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  6281. * ok to go ahead and try.
  6282. */
  6283. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  6284. {
  6285. struct btrfs_block_group_cache *block_group;
  6286. struct btrfs_space_info *space_info;
  6287. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  6288. struct btrfs_device *device;
  6289. u64 min_free;
  6290. u64 dev_min = 1;
  6291. u64 dev_nr = 0;
  6292. int index;
  6293. int full = 0;
  6294. int ret = 0;
  6295. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  6296. /* odd, couldn't find the block group, leave it alone */
  6297. if (!block_group)
  6298. return -1;
  6299. min_free = btrfs_block_group_used(&block_group->item);
  6300. /* no bytes used, we're good */
  6301. if (!min_free)
  6302. goto out;
  6303. space_info = block_group->space_info;
  6304. spin_lock(&space_info->lock);
  6305. full = space_info->full;
  6306. /*
  6307. * if this is the last block group we have in this space, we can't
  6308. * relocate it unless we're able to allocate a new chunk below.
  6309. *
  6310. * Otherwise, we need to make sure we have room in the space to handle
  6311. * all of the extents from this block group. If we can, we're good
  6312. */
  6313. if ((space_info->total_bytes != block_group->key.offset) &&
  6314. (space_info->bytes_used + space_info->bytes_reserved +
  6315. space_info->bytes_pinned + space_info->bytes_readonly +
  6316. min_free < space_info->total_bytes)) {
  6317. spin_unlock(&space_info->lock);
  6318. goto out;
  6319. }
  6320. spin_unlock(&space_info->lock);
  6321. /*
  6322. * ok we don't have enough space, but maybe we have free space on our
  6323. * devices to allocate new chunks for relocation, so loop through our
  6324. * alloc devices and guess if we have enough space. However, if we
  6325. * were marked as full, then we know there aren't enough chunks, and we
  6326. * can just return.
  6327. */
  6328. ret = -1;
  6329. if (full)
  6330. goto out;
  6331. /*
  6332. * index:
  6333. * 0: raid10
  6334. * 1: raid1
  6335. * 2: dup
  6336. * 3: raid0
  6337. * 4: single
  6338. */
  6339. index = get_block_group_index(block_group);
  6340. if (index == 0) {
  6341. dev_min = 4;
  6342. /* Divide by 2 */
  6343. min_free >>= 1;
  6344. } else if (index == 1) {
  6345. dev_min = 2;
  6346. } else if (index == 2) {
  6347. /* Multiply by 2 */
  6348. min_free <<= 1;
  6349. } else if (index == 3) {
  6350. dev_min = fs_devices->rw_devices;
  6351. do_div(min_free, dev_min);
  6352. }
  6353. mutex_lock(&root->fs_info->chunk_mutex);
  6354. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  6355. u64 dev_offset;
  6356. /*
  6357. * check to make sure we can actually find a chunk with enough
  6358. * space to fit our block group in.
  6359. */
  6360. if (device->total_bytes > device->bytes_used + min_free) {
  6361. ret = find_free_dev_extent(device, min_free,
  6362. &dev_offset, NULL);
  6363. if (!ret)
  6364. dev_nr++;
  6365. if (dev_nr >= dev_min)
  6366. break;
  6367. ret = -1;
  6368. }
  6369. }
  6370. mutex_unlock(&root->fs_info->chunk_mutex);
  6371. out:
  6372. btrfs_put_block_group(block_group);
  6373. return ret;
  6374. }
  6375. static int find_first_block_group(struct btrfs_root *root,
  6376. struct btrfs_path *path, struct btrfs_key *key)
  6377. {
  6378. int ret = 0;
  6379. struct btrfs_key found_key;
  6380. struct extent_buffer *leaf;
  6381. int slot;
  6382. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  6383. if (ret < 0)
  6384. goto out;
  6385. while (1) {
  6386. slot = path->slots[0];
  6387. leaf = path->nodes[0];
  6388. if (slot >= btrfs_header_nritems(leaf)) {
  6389. ret = btrfs_next_leaf(root, path);
  6390. if (ret == 0)
  6391. continue;
  6392. if (ret < 0)
  6393. goto out;
  6394. break;
  6395. }
  6396. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6397. if (found_key.objectid >= key->objectid &&
  6398. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  6399. ret = 0;
  6400. goto out;
  6401. }
  6402. path->slots[0]++;
  6403. }
  6404. out:
  6405. return ret;
  6406. }
  6407. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  6408. {
  6409. struct btrfs_block_group_cache *block_group;
  6410. u64 last = 0;
  6411. while (1) {
  6412. struct inode *inode;
  6413. block_group = btrfs_lookup_first_block_group(info, last);
  6414. while (block_group) {
  6415. spin_lock(&block_group->lock);
  6416. if (block_group->iref)
  6417. break;
  6418. spin_unlock(&block_group->lock);
  6419. block_group = next_block_group(info->tree_root,
  6420. block_group);
  6421. }
  6422. if (!block_group) {
  6423. if (last == 0)
  6424. break;
  6425. last = 0;
  6426. continue;
  6427. }
  6428. inode = block_group->inode;
  6429. block_group->iref = 0;
  6430. block_group->inode = NULL;
  6431. spin_unlock(&block_group->lock);
  6432. iput(inode);
  6433. last = block_group->key.objectid + block_group->key.offset;
  6434. btrfs_put_block_group(block_group);
  6435. }
  6436. }
  6437. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  6438. {
  6439. struct btrfs_block_group_cache *block_group;
  6440. struct btrfs_space_info *space_info;
  6441. struct btrfs_caching_control *caching_ctl;
  6442. struct rb_node *n;
  6443. down_write(&info->extent_commit_sem);
  6444. while (!list_empty(&info->caching_block_groups)) {
  6445. caching_ctl = list_entry(info->caching_block_groups.next,
  6446. struct btrfs_caching_control, list);
  6447. list_del(&caching_ctl->list);
  6448. put_caching_control(caching_ctl);
  6449. }
  6450. up_write(&info->extent_commit_sem);
  6451. spin_lock(&info->block_group_cache_lock);
  6452. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  6453. block_group = rb_entry(n, struct btrfs_block_group_cache,
  6454. cache_node);
  6455. rb_erase(&block_group->cache_node,
  6456. &info->block_group_cache_tree);
  6457. spin_unlock(&info->block_group_cache_lock);
  6458. down_write(&block_group->space_info->groups_sem);
  6459. list_del(&block_group->list);
  6460. up_write(&block_group->space_info->groups_sem);
  6461. if (block_group->cached == BTRFS_CACHE_STARTED)
  6462. wait_block_group_cache_done(block_group);
  6463. /*
  6464. * We haven't cached this block group, which means we could
  6465. * possibly have excluded extents on this block group.
  6466. */
  6467. if (block_group->cached == BTRFS_CACHE_NO)
  6468. free_excluded_extents(info->extent_root, block_group);
  6469. btrfs_remove_free_space_cache(block_group);
  6470. btrfs_put_block_group(block_group);
  6471. spin_lock(&info->block_group_cache_lock);
  6472. }
  6473. spin_unlock(&info->block_group_cache_lock);
  6474. /* now that all the block groups are freed, go through and
  6475. * free all the space_info structs. This is only called during
  6476. * the final stages of unmount, and so we know nobody is
  6477. * using them. We call synchronize_rcu() once before we start,
  6478. * just to be on the safe side.
  6479. */
  6480. synchronize_rcu();
  6481. release_global_block_rsv(info);
  6482. while(!list_empty(&info->space_info)) {
  6483. space_info = list_entry(info->space_info.next,
  6484. struct btrfs_space_info,
  6485. list);
  6486. if (space_info->bytes_pinned > 0 ||
  6487. space_info->bytes_reserved > 0 ||
  6488. space_info->bytes_may_use > 0) {
  6489. WARN_ON(1);
  6490. dump_space_info(space_info, 0, 0);
  6491. }
  6492. list_del(&space_info->list);
  6493. kfree(space_info);
  6494. }
  6495. return 0;
  6496. }
  6497. static void __link_block_group(struct btrfs_space_info *space_info,
  6498. struct btrfs_block_group_cache *cache)
  6499. {
  6500. int index = get_block_group_index(cache);
  6501. down_write(&space_info->groups_sem);
  6502. list_add_tail(&cache->list, &space_info->block_groups[index]);
  6503. up_write(&space_info->groups_sem);
  6504. }
  6505. int btrfs_read_block_groups(struct btrfs_root *root)
  6506. {
  6507. struct btrfs_path *path;
  6508. int ret;
  6509. struct btrfs_block_group_cache *cache;
  6510. struct btrfs_fs_info *info = root->fs_info;
  6511. struct btrfs_space_info *space_info;
  6512. struct btrfs_key key;
  6513. struct btrfs_key found_key;
  6514. struct extent_buffer *leaf;
  6515. int need_clear = 0;
  6516. u64 cache_gen;
  6517. root = info->extent_root;
  6518. key.objectid = 0;
  6519. key.offset = 0;
  6520. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  6521. path = btrfs_alloc_path();
  6522. if (!path)
  6523. return -ENOMEM;
  6524. path->reada = 1;
  6525. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  6526. if (btrfs_test_opt(root, SPACE_CACHE) &&
  6527. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  6528. need_clear = 1;
  6529. if (btrfs_test_opt(root, CLEAR_CACHE))
  6530. need_clear = 1;
  6531. while (1) {
  6532. ret = find_first_block_group(root, path, &key);
  6533. if (ret > 0)
  6534. break;
  6535. if (ret != 0)
  6536. goto error;
  6537. leaf = path->nodes[0];
  6538. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6539. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6540. if (!cache) {
  6541. ret = -ENOMEM;
  6542. goto error;
  6543. }
  6544. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  6545. GFP_NOFS);
  6546. if (!cache->free_space_ctl) {
  6547. kfree(cache);
  6548. ret = -ENOMEM;
  6549. goto error;
  6550. }
  6551. atomic_set(&cache->count, 1);
  6552. spin_lock_init(&cache->lock);
  6553. cache->fs_info = info;
  6554. INIT_LIST_HEAD(&cache->list);
  6555. INIT_LIST_HEAD(&cache->cluster_list);
  6556. if (need_clear)
  6557. cache->disk_cache_state = BTRFS_DC_CLEAR;
  6558. read_extent_buffer(leaf, &cache->item,
  6559. btrfs_item_ptr_offset(leaf, path->slots[0]),
  6560. sizeof(cache->item));
  6561. memcpy(&cache->key, &found_key, sizeof(found_key));
  6562. key.objectid = found_key.objectid + found_key.offset;
  6563. btrfs_release_path(path);
  6564. cache->flags = btrfs_block_group_flags(&cache->item);
  6565. cache->sectorsize = root->sectorsize;
  6566. btrfs_init_free_space_ctl(cache);
  6567. /*
  6568. * We need to exclude the super stripes now so that the space
  6569. * info has super bytes accounted for, otherwise we'll think
  6570. * we have more space than we actually do.
  6571. */
  6572. exclude_super_stripes(root, cache);
  6573. /*
  6574. * check for two cases, either we are full, and therefore
  6575. * don't need to bother with the caching work since we won't
  6576. * find any space, or we are empty, and we can just add all
  6577. * the space in and be done with it. This saves us _alot_ of
  6578. * time, particularly in the full case.
  6579. */
  6580. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  6581. cache->last_byte_to_unpin = (u64)-1;
  6582. cache->cached = BTRFS_CACHE_FINISHED;
  6583. free_excluded_extents(root, cache);
  6584. } else if (btrfs_block_group_used(&cache->item) == 0) {
  6585. cache->last_byte_to_unpin = (u64)-1;
  6586. cache->cached = BTRFS_CACHE_FINISHED;
  6587. add_new_free_space(cache, root->fs_info,
  6588. found_key.objectid,
  6589. found_key.objectid +
  6590. found_key.offset);
  6591. free_excluded_extents(root, cache);
  6592. }
  6593. ret = update_space_info(info, cache->flags, found_key.offset,
  6594. btrfs_block_group_used(&cache->item),
  6595. &space_info);
  6596. BUG_ON(ret);
  6597. cache->space_info = space_info;
  6598. spin_lock(&cache->space_info->lock);
  6599. cache->space_info->bytes_readonly += cache->bytes_super;
  6600. spin_unlock(&cache->space_info->lock);
  6601. __link_block_group(space_info, cache);
  6602. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6603. BUG_ON(ret);
  6604. set_avail_alloc_bits(root->fs_info, cache->flags);
  6605. if (btrfs_chunk_readonly(root, cache->key.objectid))
  6606. set_block_group_ro(cache, 1);
  6607. }
  6608. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  6609. if (!(get_alloc_profile(root, space_info->flags) &
  6610. (BTRFS_BLOCK_GROUP_RAID10 |
  6611. BTRFS_BLOCK_GROUP_RAID1 |
  6612. BTRFS_BLOCK_GROUP_DUP)))
  6613. continue;
  6614. /*
  6615. * avoid allocating from un-mirrored block group if there are
  6616. * mirrored block groups.
  6617. */
  6618. list_for_each_entry(cache, &space_info->block_groups[3], list)
  6619. set_block_group_ro(cache, 1);
  6620. list_for_each_entry(cache, &space_info->block_groups[4], list)
  6621. set_block_group_ro(cache, 1);
  6622. }
  6623. init_global_block_rsv(info);
  6624. ret = 0;
  6625. error:
  6626. btrfs_free_path(path);
  6627. return ret;
  6628. }
  6629. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  6630. struct btrfs_root *root, u64 bytes_used,
  6631. u64 type, u64 chunk_objectid, u64 chunk_offset,
  6632. u64 size)
  6633. {
  6634. int ret;
  6635. struct btrfs_root *extent_root;
  6636. struct btrfs_block_group_cache *cache;
  6637. extent_root = root->fs_info->extent_root;
  6638. root->fs_info->last_trans_log_full_commit = trans->transid;
  6639. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6640. if (!cache)
  6641. return -ENOMEM;
  6642. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  6643. GFP_NOFS);
  6644. if (!cache->free_space_ctl) {
  6645. kfree(cache);
  6646. return -ENOMEM;
  6647. }
  6648. cache->key.objectid = chunk_offset;
  6649. cache->key.offset = size;
  6650. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  6651. cache->sectorsize = root->sectorsize;
  6652. cache->fs_info = root->fs_info;
  6653. atomic_set(&cache->count, 1);
  6654. spin_lock_init(&cache->lock);
  6655. INIT_LIST_HEAD(&cache->list);
  6656. INIT_LIST_HEAD(&cache->cluster_list);
  6657. btrfs_init_free_space_ctl(cache);
  6658. btrfs_set_block_group_used(&cache->item, bytes_used);
  6659. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  6660. cache->flags = type;
  6661. btrfs_set_block_group_flags(&cache->item, type);
  6662. cache->last_byte_to_unpin = (u64)-1;
  6663. cache->cached = BTRFS_CACHE_FINISHED;
  6664. exclude_super_stripes(root, cache);
  6665. add_new_free_space(cache, root->fs_info, chunk_offset,
  6666. chunk_offset + size);
  6667. free_excluded_extents(root, cache);
  6668. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  6669. &cache->space_info);
  6670. BUG_ON(ret);
  6671. update_global_block_rsv(root->fs_info);
  6672. spin_lock(&cache->space_info->lock);
  6673. cache->space_info->bytes_readonly += cache->bytes_super;
  6674. spin_unlock(&cache->space_info->lock);
  6675. __link_block_group(cache->space_info, cache);
  6676. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6677. BUG_ON(ret);
  6678. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  6679. sizeof(cache->item));
  6680. BUG_ON(ret);
  6681. set_avail_alloc_bits(extent_root->fs_info, type);
  6682. return 0;
  6683. }
  6684. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  6685. {
  6686. u64 extra_flags = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK;
  6687. /* chunk -> extended profile */
  6688. if (extra_flags == 0)
  6689. extra_flags = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  6690. if (flags & BTRFS_BLOCK_GROUP_DATA)
  6691. fs_info->avail_data_alloc_bits &= ~extra_flags;
  6692. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  6693. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  6694. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  6695. fs_info->avail_system_alloc_bits &= ~extra_flags;
  6696. }
  6697. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  6698. struct btrfs_root *root, u64 group_start)
  6699. {
  6700. struct btrfs_path *path;
  6701. struct btrfs_block_group_cache *block_group;
  6702. struct btrfs_free_cluster *cluster;
  6703. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6704. struct btrfs_key key;
  6705. struct inode *inode;
  6706. int ret;
  6707. int index;
  6708. int factor;
  6709. root = root->fs_info->extent_root;
  6710. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  6711. BUG_ON(!block_group);
  6712. BUG_ON(!block_group->ro);
  6713. /*
  6714. * Free the reserved super bytes from this block group before
  6715. * remove it.
  6716. */
  6717. free_excluded_extents(root, block_group);
  6718. memcpy(&key, &block_group->key, sizeof(key));
  6719. index = get_block_group_index(block_group);
  6720. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  6721. BTRFS_BLOCK_GROUP_RAID1 |
  6722. BTRFS_BLOCK_GROUP_RAID10))
  6723. factor = 2;
  6724. else
  6725. factor = 1;
  6726. /* make sure this block group isn't part of an allocation cluster */
  6727. cluster = &root->fs_info->data_alloc_cluster;
  6728. spin_lock(&cluster->refill_lock);
  6729. btrfs_return_cluster_to_free_space(block_group, cluster);
  6730. spin_unlock(&cluster->refill_lock);
  6731. /*
  6732. * make sure this block group isn't part of a metadata
  6733. * allocation cluster
  6734. */
  6735. cluster = &root->fs_info->meta_alloc_cluster;
  6736. spin_lock(&cluster->refill_lock);
  6737. btrfs_return_cluster_to_free_space(block_group, cluster);
  6738. spin_unlock(&cluster->refill_lock);
  6739. path = btrfs_alloc_path();
  6740. if (!path) {
  6741. ret = -ENOMEM;
  6742. goto out;
  6743. }
  6744. inode = lookup_free_space_inode(tree_root, block_group, path);
  6745. if (!IS_ERR(inode)) {
  6746. ret = btrfs_orphan_add(trans, inode);
  6747. BUG_ON(ret);
  6748. clear_nlink(inode);
  6749. /* One for the block groups ref */
  6750. spin_lock(&block_group->lock);
  6751. if (block_group->iref) {
  6752. block_group->iref = 0;
  6753. block_group->inode = NULL;
  6754. spin_unlock(&block_group->lock);
  6755. iput(inode);
  6756. } else {
  6757. spin_unlock(&block_group->lock);
  6758. }
  6759. /* One for our lookup ref */
  6760. btrfs_add_delayed_iput(inode);
  6761. }
  6762. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  6763. key.offset = block_group->key.objectid;
  6764. key.type = 0;
  6765. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  6766. if (ret < 0)
  6767. goto out;
  6768. if (ret > 0)
  6769. btrfs_release_path(path);
  6770. if (ret == 0) {
  6771. ret = btrfs_del_item(trans, tree_root, path);
  6772. if (ret)
  6773. goto out;
  6774. btrfs_release_path(path);
  6775. }
  6776. spin_lock(&root->fs_info->block_group_cache_lock);
  6777. rb_erase(&block_group->cache_node,
  6778. &root->fs_info->block_group_cache_tree);
  6779. spin_unlock(&root->fs_info->block_group_cache_lock);
  6780. down_write(&block_group->space_info->groups_sem);
  6781. /*
  6782. * we must use list_del_init so people can check to see if they
  6783. * are still on the list after taking the semaphore
  6784. */
  6785. list_del_init(&block_group->list);
  6786. if (list_empty(&block_group->space_info->block_groups[index]))
  6787. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  6788. up_write(&block_group->space_info->groups_sem);
  6789. if (block_group->cached == BTRFS_CACHE_STARTED)
  6790. wait_block_group_cache_done(block_group);
  6791. btrfs_remove_free_space_cache(block_group);
  6792. spin_lock(&block_group->space_info->lock);
  6793. block_group->space_info->total_bytes -= block_group->key.offset;
  6794. block_group->space_info->bytes_readonly -= block_group->key.offset;
  6795. block_group->space_info->disk_total -= block_group->key.offset * factor;
  6796. spin_unlock(&block_group->space_info->lock);
  6797. memcpy(&key, &block_group->key, sizeof(key));
  6798. btrfs_clear_space_info_full(root->fs_info);
  6799. btrfs_put_block_group(block_group);
  6800. btrfs_put_block_group(block_group);
  6801. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  6802. if (ret > 0)
  6803. ret = -EIO;
  6804. if (ret < 0)
  6805. goto out;
  6806. ret = btrfs_del_item(trans, root, path);
  6807. out:
  6808. btrfs_free_path(path);
  6809. return ret;
  6810. }
  6811. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  6812. {
  6813. struct btrfs_space_info *space_info;
  6814. struct btrfs_super_block *disk_super;
  6815. u64 features;
  6816. u64 flags;
  6817. int mixed = 0;
  6818. int ret;
  6819. disk_super = fs_info->super_copy;
  6820. if (!btrfs_super_root(disk_super))
  6821. return 1;
  6822. features = btrfs_super_incompat_flags(disk_super);
  6823. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  6824. mixed = 1;
  6825. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  6826. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6827. if (ret)
  6828. goto out;
  6829. if (mixed) {
  6830. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  6831. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6832. } else {
  6833. flags = BTRFS_BLOCK_GROUP_METADATA;
  6834. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6835. if (ret)
  6836. goto out;
  6837. flags = BTRFS_BLOCK_GROUP_DATA;
  6838. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6839. }
  6840. out:
  6841. return ret;
  6842. }
  6843. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  6844. {
  6845. return unpin_extent_range(root, start, end);
  6846. }
  6847. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  6848. u64 num_bytes, u64 *actual_bytes)
  6849. {
  6850. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  6851. }
  6852. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  6853. {
  6854. struct btrfs_fs_info *fs_info = root->fs_info;
  6855. struct btrfs_block_group_cache *cache = NULL;
  6856. u64 group_trimmed;
  6857. u64 start;
  6858. u64 end;
  6859. u64 trimmed = 0;
  6860. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  6861. int ret = 0;
  6862. /*
  6863. * try to trim all FS space, our block group may start from non-zero.
  6864. */
  6865. if (range->len == total_bytes)
  6866. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  6867. else
  6868. cache = btrfs_lookup_block_group(fs_info, range->start);
  6869. while (cache) {
  6870. if (cache->key.objectid >= (range->start + range->len)) {
  6871. btrfs_put_block_group(cache);
  6872. break;
  6873. }
  6874. start = max(range->start, cache->key.objectid);
  6875. end = min(range->start + range->len,
  6876. cache->key.objectid + cache->key.offset);
  6877. if (end - start >= range->minlen) {
  6878. if (!block_group_cache_done(cache)) {
  6879. ret = cache_block_group(cache, NULL, root, 0);
  6880. if (!ret)
  6881. wait_block_group_cache_done(cache);
  6882. }
  6883. ret = btrfs_trim_block_group(cache,
  6884. &group_trimmed,
  6885. start,
  6886. end,
  6887. range->minlen);
  6888. trimmed += group_trimmed;
  6889. if (ret) {
  6890. btrfs_put_block_group(cache);
  6891. break;
  6892. }
  6893. }
  6894. cache = next_block_group(fs_info->tree_root, cache);
  6895. }
  6896. range->len = trimmed;
  6897. return ret;
  6898. }