extent-tree.c 211 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935
  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. 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. void 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. leaf = path->nodes[0];
  1459. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1460. item_offset = (unsigned long)iref - (unsigned long)ei;
  1461. type = extent_ref_type(parent, owner);
  1462. size = btrfs_extent_inline_ref_size(type);
  1463. btrfs_extend_item(trans, root, path, size);
  1464. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1465. refs = btrfs_extent_refs(leaf, ei);
  1466. refs += refs_to_add;
  1467. btrfs_set_extent_refs(leaf, ei, refs);
  1468. if (extent_op)
  1469. __run_delayed_extent_op(extent_op, leaf, ei);
  1470. ptr = (unsigned long)ei + item_offset;
  1471. end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
  1472. if (ptr < end - size)
  1473. memmove_extent_buffer(leaf, ptr + size, ptr,
  1474. end - size - ptr);
  1475. iref = (struct btrfs_extent_inline_ref *)ptr;
  1476. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  1477. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1478. struct btrfs_extent_data_ref *dref;
  1479. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1480. btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
  1481. btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
  1482. btrfs_set_extent_data_ref_offset(leaf, dref, offset);
  1483. btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
  1484. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1485. struct btrfs_shared_data_ref *sref;
  1486. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1487. btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
  1488. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1489. } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1490. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1491. } else {
  1492. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  1493. }
  1494. btrfs_mark_buffer_dirty(leaf);
  1495. }
  1496. static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  1497. struct btrfs_root *root,
  1498. struct btrfs_path *path,
  1499. struct btrfs_extent_inline_ref **ref_ret,
  1500. u64 bytenr, u64 num_bytes, u64 parent,
  1501. u64 root_objectid, u64 owner, u64 offset)
  1502. {
  1503. int ret;
  1504. ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
  1505. bytenr, num_bytes, parent,
  1506. root_objectid, owner, offset, 0);
  1507. if (ret != -ENOENT)
  1508. return ret;
  1509. btrfs_release_path(path);
  1510. *ref_ret = NULL;
  1511. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1512. ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
  1513. root_objectid);
  1514. } else {
  1515. ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
  1516. root_objectid, owner, offset);
  1517. }
  1518. return ret;
  1519. }
  1520. /*
  1521. * helper to update/remove inline back ref
  1522. */
  1523. static noinline_for_stack
  1524. void update_inline_extent_backref(struct btrfs_trans_handle *trans,
  1525. struct btrfs_root *root,
  1526. struct btrfs_path *path,
  1527. struct btrfs_extent_inline_ref *iref,
  1528. int refs_to_mod,
  1529. struct btrfs_delayed_extent_op *extent_op)
  1530. {
  1531. struct extent_buffer *leaf;
  1532. struct btrfs_extent_item *ei;
  1533. struct btrfs_extent_data_ref *dref = NULL;
  1534. struct btrfs_shared_data_ref *sref = NULL;
  1535. unsigned long ptr;
  1536. unsigned long end;
  1537. u32 item_size;
  1538. int size;
  1539. int type;
  1540. u64 refs;
  1541. leaf = path->nodes[0];
  1542. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1543. refs = btrfs_extent_refs(leaf, ei);
  1544. WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
  1545. refs += refs_to_mod;
  1546. btrfs_set_extent_refs(leaf, ei, refs);
  1547. if (extent_op)
  1548. __run_delayed_extent_op(extent_op, leaf, ei);
  1549. type = btrfs_extent_inline_ref_type(leaf, iref);
  1550. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1551. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1552. refs = btrfs_extent_data_ref_count(leaf, dref);
  1553. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1554. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1555. refs = btrfs_shared_data_ref_count(leaf, sref);
  1556. } else {
  1557. refs = 1;
  1558. BUG_ON(refs_to_mod != -1);
  1559. }
  1560. BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
  1561. refs += refs_to_mod;
  1562. if (refs > 0) {
  1563. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  1564. btrfs_set_extent_data_ref_count(leaf, dref, refs);
  1565. else
  1566. btrfs_set_shared_data_ref_count(leaf, sref, refs);
  1567. } else {
  1568. size = btrfs_extent_inline_ref_size(type);
  1569. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1570. ptr = (unsigned long)iref;
  1571. end = (unsigned long)ei + item_size;
  1572. if (ptr + size < end)
  1573. memmove_extent_buffer(leaf, ptr, ptr + size,
  1574. end - ptr - size);
  1575. item_size -= size;
  1576. btrfs_truncate_item(trans, root, path, item_size, 1);
  1577. }
  1578. btrfs_mark_buffer_dirty(leaf);
  1579. }
  1580. static noinline_for_stack
  1581. int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
  1582. struct btrfs_root *root,
  1583. struct btrfs_path *path,
  1584. u64 bytenr, u64 num_bytes, u64 parent,
  1585. u64 root_objectid, u64 owner,
  1586. u64 offset, int refs_to_add,
  1587. struct btrfs_delayed_extent_op *extent_op)
  1588. {
  1589. struct btrfs_extent_inline_ref *iref;
  1590. int ret;
  1591. ret = lookup_inline_extent_backref(trans, root, path, &iref,
  1592. bytenr, num_bytes, parent,
  1593. root_objectid, owner, offset, 1);
  1594. if (ret == 0) {
  1595. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
  1596. update_inline_extent_backref(trans, root, path, iref,
  1597. refs_to_add, extent_op);
  1598. } else if (ret == -ENOENT) {
  1599. setup_inline_extent_backref(trans, root, path, iref, parent,
  1600. root_objectid, owner, offset,
  1601. refs_to_add, extent_op);
  1602. ret = 0;
  1603. }
  1604. return ret;
  1605. }
  1606. static int insert_extent_backref(struct btrfs_trans_handle *trans,
  1607. struct btrfs_root *root,
  1608. struct btrfs_path *path,
  1609. u64 bytenr, u64 parent, u64 root_objectid,
  1610. u64 owner, u64 offset, int refs_to_add)
  1611. {
  1612. int ret;
  1613. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1614. BUG_ON(refs_to_add != 1);
  1615. ret = insert_tree_block_ref(trans, root, path, bytenr,
  1616. parent, root_objectid);
  1617. } else {
  1618. ret = insert_extent_data_ref(trans, root, path, bytenr,
  1619. parent, root_objectid,
  1620. owner, offset, refs_to_add);
  1621. }
  1622. return ret;
  1623. }
  1624. static int remove_extent_backref(struct btrfs_trans_handle *trans,
  1625. struct btrfs_root *root,
  1626. struct btrfs_path *path,
  1627. struct btrfs_extent_inline_ref *iref,
  1628. int refs_to_drop, int is_data)
  1629. {
  1630. int ret = 0;
  1631. BUG_ON(!is_data && refs_to_drop != 1);
  1632. if (iref) {
  1633. update_inline_extent_backref(trans, root, path, iref,
  1634. -refs_to_drop, NULL);
  1635. } else if (is_data) {
  1636. ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
  1637. } else {
  1638. ret = btrfs_del_item(trans, root, path);
  1639. }
  1640. return ret;
  1641. }
  1642. static int btrfs_issue_discard(struct block_device *bdev,
  1643. u64 start, u64 len)
  1644. {
  1645. return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
  1646. }
  1647. static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
  1648. u64 num_bytes, u64 *actual_bytes)
  1649. {
  1650. int ret;
  1651. u64 discarded_bytes = 0;
  1652. struct btrfs_bio *bbio = NULL;
  1653. /* Tell the block device(s) that the sectors can be discarded */
  1654. ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
  1655. bytenr, &num_bytes, &bbio, 0);
  1656. if (!ret) {
  1657. struct btrfs_bio_stripe *stripe = bbio->stripes;
  1658. int i;
  1659. for (i = 0; i < bbio->num_stripes; i++, stripe++) {
  1660. if (!stripe->dev->can_discard)
  1661. continue;
  1662. ret = btrfs_issue_discard(stripe->dev->bdev,
  1663. stripe->physical,
  1664. stripe->length);
  1665. if (!ret)
  1666. discarded_bytes += stripe->length;
  1667. else if (ret != -EOPNOTSUPP)
  1668. break;
  1669. /*
  1670. * Just in case we get back EOPNOTSUPP for some reason,
  1671. * just ignore the return value so we don't screw up
  1672. * people calling discard_extent.
  1673. */
  1674. ret = 0;
  1675. }
  1676. kfree(bbio);
  1677. }
  1678. if (actual_bytes)
  1679. *actual_bytes = discarded_bytes;
  1680. return ret;
  1681. }
  1682. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1683. struct btrfs_root *root,
  1684. u64 bytenr, u64 num_bytes, u64 parent,
  1685. u64 root_objectid, u64 owner, u64 offset, int for_cow)
  1686. {
  1687. int ret;
  1688. struct btrfs_fs_info *fs_info = root->fs_info;
  1689. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
  1690. root_objectid == BTRFS_TREE_LOG_OBJECTID);
  1691. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1692. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  1693. num_bytes,
  1694. parent, root_objectid, (int)owner,
  1695. BTRFS_ADD_DELAYED_REF, NULL, for_cow);
  1696. } else {
  1697. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  1698. num_bytes,
  1699. parent, root_objectid, owner, offset,
  1700. BTRFS_ADD_DELAYED_REF, NULL, for_cow);
  1701. }
  1702. return ret;
  1703. }
  1704. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1705. struct btrfs_root *root,
  1706. u64 bytenr, u64 num_bytes,
  1707. u64 parent, u64 root_objectid,
  1708. u64 owner, u64 offset, int refs_to_add,
  1709. struct btrfs_delayed_extent_op *extent_op)
  1710. {
  1711. struct btrfs_path *path;
  1712. struct extent_buffer *leaf;
  1713. struct btrfs_extent_item *item;
  1714. u64 refs;
  1715. int ret;
  1716. int err = 0;
  1717. path = btrfs_alloc_path();
  1718. if (!path)
  1719. return -ENOMEM;
  1720. path->reada = 1;
  1721. path->leave_spinning = 1;
  1722. /* this will setup the path even if it fails to insert the back ref */
  1723. ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
  1724. path, bytenr, num_bytes, parent,
  1725. root_objectid, owner, offset,
  1726. refs_to_add, extent_op);
  1727. if (ret == 0)
  1728. goto out;
  1729. if (ret != -EAGAIN) {
  1730. err = ret;
  1731. goto out;
  1732. }
  1733. leaf = path->nodes[0];
  1734. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1735. refs = btrfs_extent_refs(leaf, item);
  1736. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  1737. if (extent_op)
  1738. __run_delayed_extent_op(extent_op, leaf, item);
  1739. btrfs_mark_buffer_dirty(leaf);
  1740. btrfs_release_path(path);
  1741. path->reada = 1;
  1742. path->leave_spinning = 1;
  1743. /* now insert the actual backref */
  1744. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1745. path, bytenr, parent, root_objectid,
  1746. owner, offset, refs_to_add);
  1747. BUG_ON(ret);
  1748. out:
  1749. btrfs_free_path(path);
  1750. return err;
  1751. }
  1752. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  1753. struct btrfs_root *root,
  1754. struct btrfs_delayed_ref_node *node,
  1755. struct btrfs_delayed_extent_op *extent_op,
  1756. int insert_reserved)
  1757. {
  1758. int ret = 0;
  1759. struct btrfs_delayed_data_ref *ref;
  1760. struct btrfs_key ins;
  1761. u64 parent = 0;
  1762. u64 ref_root = 0;
  1763. u64 flags = 0;
  1764. ins.objectid = node->bytenr;
  1765. ins.offset = node->num_bytes;
  1766. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1767. ref = btrfs_delayed_node_to_data_ref(node);
  1768. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1769. parent = ref->parent;
  1770. else
  1771. ref_root = ref->root;
  1772. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1773. if (extent_op) {
  1774. BUG_ON(extent_op->update_key);
  1775. flags |= extent_op->flags_to_set;
  1776. }
  1777. ret = alloc_reserved_file_extent(trans, root,
  1778. parent, ref_root, flags,
  1779. ref->objectid, ref->offset,
  1780. &ins, node->ref_mod);
  1781. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1782. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1783. node->num_bytes, parent,
  1784. ref_root, ref->objectid,
  1785. ref->offset, node->ref_mod,
  1786. extent_op);
  1787. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1788. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1789. node->num_bytes, parent,
  1790. ref_root, ref->objectid,
  1791. ref->offset, node->ref_mod,
  1792. extent_op);
  1793. } else {
  1794. BUG();
  1795. }
  1796. return ret;
  1797. }
  1798. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1799. struct extent_buffer *leaf,
  1800. struct btrfs_extent_item *ei)
  1801. {
  1802. u64 flags = btrfs_extent_flags(leaf, ei);
  1803. if (extent_op->update_flags) {
  1804. flags |= extent_op->flags_to_set;
  1805. btrfs_set_extent_flags(leaf, ei, flags);
  1806. }
  1807. if (extent_op->update_key) {
  1808. struct btrfs_tree_block_info *bi;
  1809. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1810. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1811. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1812. }
  1813. }
  1814. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1815. struct btrfs_root *root,
  1816. struct btrfs_delayed_ref_node *node,
  1817. struct btrfs_delayed_extent_op *extent_op)
  1818. {
  1819. struct btrfs_key key;
  1820. struct btrfs_path *path;
  1821. struct btrfs_extent_item *ei;
  1822. struct extent_buffer *leaf;
  1823. u32 item_size;
  1824. int ret;
  1825. int err = 0;
  1826. path = btrfs_alloc_path();
  1827. if (!path)
  1828. return -ENOMEM;
  1829. key.objectid = node->bytenr;
  1830. key.type = BTRFS_EXTENT_ITEM_KEY;
  1831. key.offset = node->num_bytes;
  1832. path->reada = 1;
  1833. path->leave_spinning = 1;
  1834. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  1835. path, 0, 1);
  1836. if (ret < 0) {
  1837. err = ret;
  1838. goto out;
  1839. }
  1840. if (ret > 0) {
  1841. err = -EIO;
  1842. goto out;
  1843. }
  1844. leaf = path->nodes[0];
  1845. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1846. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1847. if (item_size < sizeof(*ei)) {
  1848. ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
  1849. path, (u64)-1, 0);
  1850. if (ret < 0) {
  1851. err = ret;
  1852. goto out;
  1853. }
  1854. leaf = path->nodes[0];
  1855. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1856. }
  1857. #endif
  1858. BUG_ON(item_size < sizeof(*ei));
  1859. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1860. __run_delayed_extent_op(extent_op, leaf, ei);
  1861. btrfs_mark_buffer_dirty(leaf);
  1862. out:
  1863. btrfs_free_path(path);
  1864. return err;
  1865. }
  1866. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1867. struct btrfs_root *root,
  1868. struct btrfs_delayed_ref_node *node,
  1869. struct btrfs_delayed_extent_op *extent_op,
  1870. int insert_reserved)
  1871. {
  1872. int ret = 0;
  1873. struct btrfs_delayed_tree_ref *ref;
  1874. struct btrfs_key ins;
  1875. u64 parent = 0;
  1876. u64 ref_root = 0;
  1877. ins.objectid = node->bytenr;
  1878. ins.offset = node->num_bytes;
  1879. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1880. ref = btrfs_delayed_node_to_tree_ref(node);
  1881. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1882. parent = ref->parent;
  1883. else
  1884. ref_root = ref->root;
  1885. BUG_ON(node->ref_mod != 1);
  1886. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1887. BUG_ON(!extent_op || !extent_op->update_flags ||
  1888. !extent_op->update_key);
  1889. ret = alloc_reserved_tree_block(trans, root,
  1890. parent, ref_root,
  1891. extent_op->flags_to_set,
  1892. &extent_op->key,
  1893. ref->level, &ins);
  1894. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1895. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1896. node->num_bytes, parent, ref_root,
  1897. ref->level, 0, 1, extent_op);
  1898. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1899. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1900. node->num_bytes, parent, ref_root,
  1901. ref->level, 0, 1, extent_op);
  1902. } else {
  1903. BUG();
  1904. }
  1905. return ret;
  1906. }
  1907. /* helper function to actually process a single delayed ref entry */
  1908. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  1909. struct btrfs_root *root,
  1910. struct btrfs_delayed_ref_node *node,
  1911. struct btrfs_delayed_extent_op *extent_op,
  1912. int insert_reserved)
  1913. {
  1914. int ret;
  1915. if (btrfs_delayed_ref_is_head(node)) {
  1916. struct btrfs_delayed_ref_head *head;
  1917. /*
  1918. * we've hit the end of the chain and we were supposed
  1919. * to insert this extent into the tree. But, it got
  1920. * deleted before we ever needed to insert it, so all
  1921. * we have to do is clean up the accounting
  1922. */
  1923. BUG_ON(extent_op);
  1924. head = btrfs_delayed_node_to_head(node);
  1925. if (insert_reserved) {
  1926. btrfs_pin_extent(root, node->bytenr,
  1927. node->num_bytes, 1);
  1928. if (head->is_data) {
  1929. ret = btrfs_del_csums(trans, root,
  1930. node->bytenr,
  1931. node->num_bytes);
  1932. BUG_ON(ret);
  1933. }
  1934. }
  1935. mutex_unlock(&head->mutex);
  1936. return 0;
  1937. }
  1938. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  1939. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1940. ret = run_delayed_tree_ref(trans, root, node, extent_op,
  1941. insert_reserved);
  1942. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  1943. node->type == BTRFS_SHARED_DATA_REF_KEY)
  1944. ret = run_delayed_data_ref(trans, root, node, extent_op,
  1945. insert_reserved);
  1946. else
  1947. BUG();
  1948. return ret;
  1949. }
  1950. static noinline struct btrfs_delayed_ref_node *
  1951. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  1952. {
  1953. struct rb_node *node;
  1954. struct btrfs_delayed_ref_node *ref;
  1955. int action = BTRFS_ADD_DELAYED_REF;
  1956. again:
  1957. /*
  1958. * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
  1959. * this prevents ref count from going down to zero when
  1960. * there still are pending delayed ref.
  1961. */
  1962. node = rb_prev(&head->node.rb_node);
  1963. while (1) {
  1964. if (!node)
  1965. break;
  1966. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  1967. rb_node);
  1968. if (ref->bytenr != head->node.bytenr)
  1969. break;
  1970. if (ref->action == action)
  1971. return ref;
  1972. node = rb_prev(node);
  1973. }
  1974. if (action == BTRFS_ADD_DELAYED_REF) {
  1975. action = BTRFS_DROP_DELAYED_REF;
  1976. goto again;
  1977. }
  1978. return NULL;
  1979. }
  1980. static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
  1981. struct btrfs_root *root,
  1982. struct list_head *cluster)
  1983. {
  1984. struct btrfs_delayed_ref_root *delayed_refs;
  1985. struct btrfs_delayed_ref_node *ref;
  1986. struct btrfs_delayed_ref_head *locked_ref = NULL;
  1987. struct btrfs_delayed_extent_op *extent_op;
  1988. int ret;
  1989. int count = 0;
  1990. int must_insert_reserved = 0;
  1991. delayed_refs = &trans->transaction->delayed_refs;
  1992. while (1) {
  1993. if (!locked_ref) {
  1994. /* pick a new head ref from the cluster list */
  1995. if (list_empty(cluster))
  1996. break;
  1997. locked_ref = list_entry(cluster->next,
  1998. struct btrfs_delayed_ref_head, cluster);
  1999. /* grab the lock that says we are going to process
  2000. * all the refs for this head */
  2001. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  2002. /*
  2003. * we may have dropped the spin lock to get the head
  2004. * mutex lock, and that might have given someone else
  2005. * time to free the head. If that's true, it has been
  2006. * removed from our list and we can move on.
  2007. */
  2008. if (ret == -EAGAIN) {
  2009. locked_ref = NULL;
  2010. count++;
  2011. continue;
  2012. }
  2013. }
  2014. /*
  2015. * locked_ref is the head node, so we have to go one
  2016. * node back for any delayed ref updates
  2017. */
  2018. ref = select_delayed_ref(locked_ref);
  2019. if (ref && ref->seq &&
  2020. btrfs_check_delayed_seq(delayed_refs, ref->seq)) {
  2021. /*
  2022. * there are still refs with lower seq numbers in the
  2023. * process of being added. Don't run this ref yet.
  2024. */
  2025. list_del_init(&locked_ref->cluster);
  2026. mutex_unlock(&locked_ref->mutex);
  2027. locked_ref = NULL;
  2028. delayed_refs->num_heads_ready++;
  2029. spin_unlock(&delayed_refs->lock);
  2030. cond_resched();
  2031. spin_lock(&delayed_refs->lock);
  2032. continue;
  2033. }
  2034. /*
  2035. * record the must insert reserved flag before we
  2036. * drop the spin lock.
  2037. */
  2038. must_insert_reserved = locked_ref->must_insert_reserved;
  2039. locked_ref->must_insert_reserved = 0;
  2040. extent_op = locked_ref->extent_op;
  2041. locked_ref->extent_op = NULL;
  2042. if (!ref) {
  2043. /* All delayed refs have been processed, Go ahead
  2044. * and send the head node to run_one_delayed_ref,
  2045. * so that any accounting fixes can happen
  2046. */
  2047. ref = &locked_ref->node;
  2048. if (extent_op && must_insert_reserved) {
  2049. kfree(extent_op);
  2050. extent_op = NULL;
  2051. }
  2052. if (extent_op) {
  2053. spin_unlock(&delayed_refs->lock);
  2054. ret = run_delayed_extent_op(trans, root,
  2055. ref, extent_op);
  2056. BUG_ON(ret);
  2057. kfree(extent_op);
  2058. goto next;
  2059. }
  2060. list_del_init(&locked_ref->cluster);
  2061. locked_ref = NULL;
  2062. }
  2063. ref->in_tree = 0;
  2064. rb_erase(&ref->rb_node, &delayed_refs->root);
  2065. delayed_refs->num_entries--;
  2066. /*
  2067. * we modified num_entries, but as we're currently running
  2068. * delayed refs, skip
  2069. * wake_up(&delayed_refs->seq_wait);
  2070. * here.
  2071. */
  2072. spin_unlock(&delayed_refs->lock);
  2073. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2074. must_insert_reserved);
  2075. BUG_ON(ret);
  2076. btrfs_put_delayed_ref(ref);
  2077. kfree(extent_op);
  2078. count++;
  2079. next:
  2080. do_chunk_alloc(trans, root->fs_info->extent_root,
  2081. 2 * 1024 * 1024,
  2082. btrfs_get_alloc_profile(root, 0),
  2083. CHUNK_ALLOC_NO_FORCE);
  2084. cond_resched();
  2085. spin_lock(&delayed_refs->lock);
  2086. }
  2087. return count;
  2088. }
  2089. static void wait_for_more_refs(struct btrfs_delayed_ref_root *delayed_refs,
  2090. unsigned long num_refs)
  2091. {
  2092. struct list_head *first_seq = delayed_refs->seq_head.next;
  2093. spin_unlock(&delayed_refs->lock);
  2094. pr_debug("waiting for more refs (num %ld, first %p)\n",
  2095. num_refs, first_seq);
  2096. wait_event(delayed_refs->seq_wait,
  2097. num_refs != delayed_refs->num_entries ||
  2098. delayed_refs->seq_head.next != first_seq);
  2099. pr_debug("done waiting for more refs (num %ld, first %p)\n",
  2100. delayed_refs->num_entries, delayed_refs->seq_head.next);
  2101. spin_lock(&delayed_refs->lock);
  2102. }
  2103. /*
  2104. * this starts processing the delayed reference count updates and
  2105. * extent insertions we have queued up so far. count can be
  2106. * 0, which means to process everything in the tree at the start
  2107. * of the run (but not newly added entries), or it can be some target
  2108. * number you'd like to process.
  2109. */
  2110. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2111. struct btrfs_root *root, unsigned long count)
  2112. {
  2113. struct rb_node *node;
  2114. struct btrfs_delayed_ref_root *delayed_refs;
  2115. struct btrfs_delayed_ref_node *ref;
  2116. struct list_head cluster;
  2117. int ret;
  2118. u64 delayed_start;
  2119. int run_all = count == (unsigned long)-1;
  2120. int run_most = 0;
  2121. unsigned long num_refs = 0;
  2122. int consider_waiting;
  2123. if (root == root->fs_info->extent_root)
  2124. root = root->fs_info->tree_root;
  2125. do_chunk_alloc(trans, root->fs_info->extent_root,
  2126. 2 * 1024 * 1024, btrfs_get_alloc_profile(root, 0),
  2127. CHUNK_ALLOC_NO_FORCE);
  2128. delayed_refs = &trans->transaction->delayed_refs;
  2129. INIT_LIST_HEAD(&cluster);
  2130. again:
  2131. consider_waiting = 0;
  2132. spin_lock(&delayed_refs->lock);
  2133. if (count == 0) {
  2134. count = delayed_refs->num_entries * 2;
  2135. run_most = 1;
  2136. }
  2137. while (1) {
  2138. if (!(run_all || run_most) &&
  2139. delayed_refs->num_heads_ready < 64)
  2140. break;
  2141. /*
  2142. * go find something we can process in the rbtree. We start at
  2143. * the beginning of the tree, and then build a cluster
  2144. * of refs to process starting at the first one we are able to
  2145. * lock
  2146. */
  2147. delayed_start = delayed_refs->run_delayed_start;
  2148. ret = btrfs_find_ref_cluster(trans, &cluster,
  2149. delayed_refs->run_delayed_start);
  2150. if (ret)
  2151. break;
  2152. if (delayed_start >= delayed_refs->run_delayed_start) {
  2153. if (consider_waiting == 0) {
  2154. /*
  2155. * btrfs_find_ref_cluster looped. let's do one
  2156. * more cycle. if we don't run any delayed ref
  2157. * during that cycle (because we can't because
  2158. * all of them are blocked) and if the number of
  2159. * refs doesn't change, we avoid busy waiting.
  2160. */
  2161. consider_waiting = 1;
  2162. num_refs = delayed_refs->num_entries;
  2163. } else {
  2164. wait_for_more_refs(delayed_refs, num_refs);
  2165. /*
  2166. * after waiting, things have changed. we
  2167. * dropped the lock and someone else might have
  2168. * run some refs, built new clusters and so on.
  2169. * therefore, we restart staleness detection.
  2170. */
  2171. consider_waiting = 0;
  2172. }
  2173. }
  2174. ret = run_clustered_refs(trans, root, &cluster);
  2175. BUG_ON(ret < 0);
  2176. count -= min_t(unsigned long, ret, count);
  2177. if (count == 0)
  2178. break;
  2179. if (ret || delayed_refs->run_delayed_start == 0) {
  2180. /* refs were run, let's reset staleness detection */
  2181. consider_waiting = 0;
  2182. }
  2183. }
  2184. if (run_all) {
  2185. node = rb_first(&delayed_refs->root);
  2186. if (!node)
  2187. goto out;
  2188. count = (unsigned long)-1;
  2189. while (node) {
  2190. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2191. rb_node);
  2192. if (btrfs_delayed_ref_is_head(ref)) {
  2193. struct btrfs_delayed_ref_head *head;
  2194. head = btrfs_delayed_node_to_head(ref);
  2195. atomic_inc(&ref->refs);
  2196. spin_unlock(&delayed_refs->lock);
  2197. /*
  2198. * Mutex was contended, block until it's
  2199. * released and try again
  2200. */
  2201. mutex_lock(&head->mutex);
  2202. mutex_unlock(&head->mutex);
  2203. btrfs_put_delayed_ref(ref);
  2204. cond_resched();
  2205. goto again;
  2206. }
  2207. node = rb_next(node);
  2208. }
  2209. spin_unlock(&delayed_refs->lock);
  2210. schedule_timeout(1);
  2211. goto again;
  2212. }
  2213. out:
  2214. spin_unlock(&delayed_refs->lock);
  2215. return 0;
  2216. }
  2217. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2218. struct btrfs_root *root,
  2219. u64 bytenr, u64 num_bytes, u64 flags,
  2220. int is_data)
  2221. {
  2222. struct btrfs_delayed_extent_op *extent_op;
  2223. int ret;
  2224. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2225. if (!extent_op)
  2226. return -ENOMEM;
  2227. extent_op->flags_to_set = flags;
  2228. extent_op->update_flags = 1;
  2229. extent_op->update_key = 0;
  2230. extent_op->is_data = is_data ? 1 : 0;
  2231. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2232. num_bytes, extent_op);
  2233. if (ret)
  2234. kfree(extent_op);
  2235. return ret;
  2236. }
  2237. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2238. struct btrfs_root *root,
  2239. struct btrfs_path *path,
  2240. u64 objectid, u64 offset, u64 bytenr)
  2241. {
  2242. struct btrfs_delayed_ref_head *head;
  2243. struct btrfs_delayed_ref_node *ref;
  2244. struct btrfs_delayed_data_ref *data_ref;
  2245. struct btrfs_delayed_ref_root *delayed_refs;
  2246. struct rb_node *node;
  2247. int ret = 0;
  2248. ret = -ENOENT;
  2249. delayed_refs = &trans->transaction->delayed_refs;
  2250. spin_lock(&delayed_refs->lock);
  2251. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2252. if (!head)
  2253. goto out;
  2254. if (!mutex_trylock(&head->mutex)) {
  2255. atomic_inc(&head->node.refs);
  2256. spin_unlock(&delayed_refs->lock);
  2257. btrfs_release_path(path);
  2258. /*
  2259. * Mutex was contended, block until it's released and let
  2260. * caller try again
  2261. */
  2262. mutex_lock(&head->mutex);
  2263. mutex_unlock(&head->mutex);
  2264. btrfs_put_delayed_ref(&head->node);
  2265. return -EAGAIN;
  2266. }
  2267. node = rb_prev(&head->node.rb_node);
  2268. if (!node)
  2269. goto out_unlock;
  2270. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2271. if (ref->bytenr != bytenr)
  2272. goto out_unlock;
  2273. ret = 1;
  2274. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2275. goto out_unlock;
  2276. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2277. node = rb_prev(node);
  2278. if (node) {
  2279. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2280. if (ref->bytenr == bytenr)
  2281. goto out_unlock;
  2282. }
  2283. if (data_ref->root != root->root_key.objectid ||
  2284. data_ref->objectid != objectid || data_ref->offset != offset)
  2285. goto out_unlock;
  2286. ret = 0;
  2287. out_unlock:
  2288. mutex_unlock(&head->mutex);
  2289. out:
  2290. spin_unlock(&delayed_refs->lock);
  2291. return ret;
  2292. }
  2293. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2294. struct btrfs_root *root,
  2295. struct btrfs_path *path,
  2296. u64 objectid, u64 offset, u64 bytenr)
  2297. {
  2298. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2299. struct extent_buffer *leaf;
  2300. struct btrfs_extent_data_ref *ref;
  2301. struct btrfs_extent_inline_ref *iref;
  2302. struct btrfs_extent_item *ei;
  2303. struct btrfs_key key;
  2304. u32 item_size;
  2305. int ret;
  2306. key.objectid = bytenr;
  2307. key.offset = (u64)-1;
  2308. key.type = BTRFS_EXTENT_ITEM_KEY;
  2309. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2310. if (ret < 0)
  2311. goto out;
  2312. BUG_ON(ret == 0);
  2313. ret = -ENOENT;
  2314. if (path->slots[0] == 0)
  2315. goto out;
  2316. path->slots[0]--;
  2317. leaf = path->nodes[0];
  2318. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2319. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2320. goto out;
  2321. ret = 1;
  2322. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2323. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2324. if (item_size < sizeof(*ei)) {
  2325. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2326. goto out;
  2327. }
  2328. #endif
  2329. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2330. if (item_size != sizeof(*ei) +
  2331. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2332. goto out;
  2333. if (btrfs_extent_generation(leaf, ei) <=
  2334. btrfs_root_last_snapshot(&root->root_item))
  2335. goto out;
  2336. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2337. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2338. BTRFS_EXTENT_DATA_REF_KEY)
  2339. goto out;
  2340. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2341. if (btrfs_extent_refs(leaf, ei) !=
  2342. btrfs_extent_data_ref_count(leaf, ref) ||
  2343. btrfs_extent_data_ref_root(leaf, ref) !=
  2344. root->root_key.objectid ||
  2345. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2346. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2347. goto out;
  2348. ret = 0;
  2349. out:
  2350. return ret;
  2351. }
  2352. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2353. struct btrfs_root *root,
  2354. u64 objectid, u64 offset, u64 bytenr)
  2355. {
  2356. struct btrfs_path *path;
  2357. int ret;
  2358. int ret2;
  2359. path = btrfs_alloc_path();
  2360. if (!path)
  2361. return -ENOENT;
  2362. do {
  2363. ret = check_committed_ref(trans, root, path, objectid,
  2364. offset, bytenr);
  2365. if (ret && ret != -ENOENT)
  2366. goto out;
  2367. ret2 = check_delayed_ref(trans, root, path, objectid,
  2368. offset, bytenr);
  2369. } while (ret2 == -EAGAIN);
  2370. if (ret2 && ret2 != -ENOENT) {
  2371. ret = ret2;
  2372. goto out;
  2373. }
  2374. if (ret != -ENOENT || ret2 != -ENOENT)
  2375. ret = 0;
  2376. out:
  2377. btrfs_free_path(path);
  2378. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2379. WARN_ON(ret > 0);
  2380. return ret;
  2381. }
  2382. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2383. struct btrfs_root *root,
  2384. struct extent_buffer *buf,
  2385. int full_backref, int inc, int for_cow)
  2386. {
  2387. u64 bytenr;
  2388. u64 num_bytes;
  2389. u64 parent;
  2390. u64 ref_root;
  2391. u32 nritems;
  2392. struct btrfs_key key;
  2393. struct btrfs_file_extent_item *fi;
  2394. int i;
  2395. int level;
  2396. int ret = 0;
  2397. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2398. u64, u64, u64, u64, u64, u64, int);
  2399. ref_root = btrfs_header_owner(buf);
  2400. nritems = btrfs_header_nritems(buf);
  2401. level = btrfs_header_level(buf);
  2402. if (!root->ref_cows && level == 0)
  2403. return 0;
  2404. if (inc)
  2405. process_func = btrfs_inc_extent_ref;
  2406. else
  2407. process_func = btrfs_free_extent;
  2408. if (full_backref)
  2409. parent = buf->start;
  2410. else
  2411. parent = 0;
  2412. for (i = 0; i < nritems; i++) {
  2413. if (level == 0) {
  2414. btrfs_item_key_to_cpu(buf, &key, i);
  2415. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2416. continue;
  2417. fi = btrfs_item_ptr(buf, i,
  2418. struct btrfs_file_extent_item);
  2419. if (btrfs_file_extent_type(buf, fi) ==
  2420. BTRFS_FILE_EXTENT_INLINE)
  2421. continue;
  2422. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2423. if (bytenr == 0)
  2424. continue;
  2425. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2426. key.offset -= btrfs_file_extent_offset(buf, fi);
  2427. ret = process_func(trans, root, bytenr, num_bytes,
  2428. parent, ref_root, key.objectid,
  2429. key.offset, for_cow);
  2430. if (ret)
  2431. goto fail;
  2432. } else {
  2433. bytenr = btrfs_node_blockptr(buf, i);
  2434. num_bytes = btrfs_level_size(root, level - 1);
  2435. ret = process_func(trans, root, bytenr, num_bytes,
  2436. parent, ref_root, level - 1, 0,
  2437. for_cow);
  2438. if (ret)
  2439. goto fail;
  2440. }
  2441. }
  2442. return 0;
  2443. fail:
  2444. BUG();
  2445. return ret;
  2446. }
  2447. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2448. struct extent_buffer *buf, int full_backref, int for_cow)
  2449. {
  2450. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2451. }
  2452. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2453. struct extent_buffer *buf, int full_backref, int for_cow)
  2454. {
  2455. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2456. }
  2457. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2458. struct btrfs_root *root,
  2459. struct btrfs_path *path,
  2460. struct btrfs_block_group_cache *cache)
  2461. {
  2462. int ret;
  2463. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2464. unsigned long bi;
  2465. struct extent_buffer *leaf;
  2466. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2467. if (ret < 0)
  2468. goto fail;
  2469. BUG_ON(ret);
  2470. leaf = path->nodes[0];
  2471. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2472. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2473. btrfs_mark_buffer_dirty(leaf);
  2474. btrfs_release_path(path);
  2475. fail:
  2476. if (ret)
  2477. return ret;
  2478. return 0;
  2479. }
  2480. static struct btrfs_block_group_cache *
  2481. next_block_group(struct btrfs_root *root,
  2482. struct btrfs_block_group_cache *cache)
  2483. {
  2484. struct rb_node *node;
  2485. spin_lock(&root->fs_info->block_group_cache_lock);
  2486. node = rb_next(&cache->cache_node);
  2487. btrfs_put_block_group(cache);
  2488. if (node) {
  2489. cache = rb_entry(node, struct btrfs_block_group_cache,
  2490. cache_node);
  2491. btrfs_get_block_group(cache);
  2492. } else
  2493. cache = NULL;
  2494. spin_unlock(&root->fs_info->block_group_cache_lock);
  2495. return cache;
  2496. }
  2497. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2498. struct btrfs_trans_handle *trans,
  2499. struct btrfs_path *path)
  2500. {
  2501. struct btrfs_root *root = block_group->fs_info->tree_root;
  2502. struct inode *inode = NULL;
  2503. u64 alloc_hint = 0;
  2504. int dcs = BTRFS_DC_ERROR;
  2505. int num_pages = 0;
  2506. int retries = 0;
  2507. int ret = 0;
  2508. /*
  2509. * If this block group is smaller than 100 megs don't bother caching the
  2510. * block group.
  2511. */
  2512. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2513. spin_lock(&block_group->lock);
  2514. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2515. spin_unlock(&block_group->lock);
  2516. return 0;
  2517. }
  2518. again:
  2519. inode = lookup_free_space_inode(root, block_group, path);
  2520. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2521. ret = PTR_ERR(inode);
  2522. btrfs_release_path(path);
  2523. goto out;
  2524. }
  2525. if (IS_ERR(inode)) {
  2526. BUG_ON(retries);
  2527. retries++;
  2528. if (block_group->ro)
  2529. goto out_free;
  2530. ret = create_free_space_inode(root, trans, block_group, path);
  2531. if (ret)
  2532. goto out_free;
  2533. goto again;
  2534. }
  2535. /* We've already setup this transaction, go ahead and exit */
  2536. if (block_group->cache_generation == trans->transid &&
  2537. i_size_read(inode)) {
  2538. dcs = BTRFS_DC_SETUP;
  2539. goto out_put;
  2540. }
  2541. /*
  2542. * We want to set the generation to 0, that way if anything goes wrong
  2543. * from here on out we know not to trust this cache when we load up next
  2544. * time.
  2545. */
  2546. BTRFS_I(inode)->generation = 0;
  2547. ret = btrfs_update_inode(trans, root, inode);
  2548. WARN_ON(ret);
  2549. if (i_size_read(inode) > 0) {
  2550. ret = btrfs_truncate_free_space_cache(root, trans, path,
  2551. inode);
  2552. if (ret)
  2553. goto out_put;
  2554. }
  2555. spin_lock(&block_group->lock);
  2556. if (block_group->cached != BTRFS_CACHE_FINISHED) {
  2557. /* We're not cached, don't bother trying to write stuff out */
  2558. dcs = BTRFS_DC_WRITTEN;
  2559. spin_unlock(&block_group->lock);
  2560. goto out_put;
  2561. }
  2562. spin_unlock(&block_group->lock);
  2563. num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
  2564. if (!num_pages)
  2565. num_pages = 1;
  2566. /*
  2567. * Just to make absolutely sure we have enough space, we're going to
  2568. * preallocate 12 pages worth of space for each block group. In
  2569. * practice we ought to use at most 8, but we need extra space so we can
  2570. * add our header and have a terminator between the extents and the
  2571. * bitmaps.
  2572. */
  2573. num_pages *= 16;
  2574. num_pages *= PAGE_CACHE_SIZE;
  2575. ret = btrfs_check_data_free_space(inode, num_pages);
  2576. if (ret)
  2577. goto out_put;
  2578. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2579. num_pages, num_pages,
  2580. &alloc_hint);
  2581. if (!ret)
  2582. dcs = BTRFS_DC_SETUP;
  2583. btrfs_free_reserved_data_space(inode, num_pages);
  2584. out_put:
  2585. iput(inode);
  2586. out_free:
  2587. btrfs_release_path(path);
  2588. out:
  2589. spin_lock(&block_group->lock);
  2590. if (!ret && dcs == BTRFS_DC_SETUP)
  2591. block_group->cache_generation = trans->transid;
  2592. block_group->disk_cache_state = dcs;
  2593. spin_unlock(&block_group->lock);
  2594. return ret;
  2595. }
  2596. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2597. struct btrfs_root *root)
  2598. {
  2599. struct btrfs_block_group_cache *cache;
  2600. int err = 0;
  2601. struct btrfs_path *path;
  2602. u64 last = 0;
  2603. path = btrfs_alloc_path();
  2604. if (!path)
  2605. return -ENOMEM;
  2606. again:
  2607. while (1) {
  2608. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2609. while (cache) {
  2610. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2611. break;
  2612. cache = next_block_group(root, cache);
  2613. }
  2614. if (!cache) {
  2615. if (last == 0)
  2616. break;
  2617. last = 0;
  2618. continue;
  2619. }
  2620. err = cache_save_setup(cache, trans, path);
  2621. last = cache->key.objectid + cache->key.offset;
  2622. btrfs_put_block_group(cache);
  2623. }
  2624. while (1) {
  2625. if (last == 0) {
  2626. err = btrfs_run_delayed_refs(trans, root,
  2627. (unsigned long)-1);
  2628. BUG_ON(err);
  2629. }
  2630. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2631. while (cache) {
  2632. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2633. btrfs_put_block_group(cache);
  2634. goto again;
  2635. }
  2636. if (cache->dirty)
  2637. break;
  2638. cache = next_block_group(root, cache);
  2639. }
  2640. if (!cache) {
  2641. if (last == 0)
  2642. break;
  2643. last = 0;
  2644. continue;
  2645. }
  2646. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2647. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2648. cache->dirty = 0;
  2649. last = cache->key.objectid + cache->key.offset;
  2650. err = write_one_cache_group(trans, root, path, cache);
  2651. BUG_ON(err);
  2652. btrfs_put_block_group(cache);
  2653. }
  2654. while (1) {
  2655. /*
  2656. * I don't think this is needed since we're just marking our
  2657. * preallocated extent as written, but just in case it can't
  2658. * hurt.
  2659. */
  2660. if (last == 0) {
  2661. err = btrfs_run_delayed_refs(trans, root,
  2662. (unsigned long)-1);
  2663. BUG_ON(err);
  2664. }
  2665. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2666. while (cache) {
  2667. /*
  2668. * Really this shouldn't happen, but it could if we
  2669. * couldn't write the entire preallocated extent and
  2670. * splitting the extent resulted in a new block.
  2671. */
  2672. if (cache->dirty) {
  2673. btrfs_put_block_group(cache);
  2674. goto again;
  2675. }
  2676. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2677. break;
  2678. cache = next_block_group(root, cache);
  2679. }
  2680. if (!cache) {
  2681. if (last == 0)
  2682. break;
  2683. last = 0;
  2684. continue;
  2685. }
  2686. btrfs_write_out_cache(root, trans, cache, path);
  2687. /*
  2688. * If we didn't have an error then the cache state is still
  2689. * NEED_WRITE, so we can set it to WRITTEN.
  2690. */
  2691. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2692. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2693. last = cache->key.objectid + cache->key.offset;
  2694. btrfs_put_block_group(cache);
  2695. }
  2696. btrfs_free_path(path);
  2697. return 0;
  2698. }
  2699. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2700. {
  2701. struct btrfs_block_group_cache *block_group;
  2702. int readonly = 0;
  2703. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2704. if (!block_group || block_group->ro)
  2705. readonly = 1;
  2706. if (block_group)
  2707. btrfs_put_block_group(block_group);
  2708. return readonly;
  2709. }
  2710. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2711. u64 total_bytes, u64 bytes_used,
  2712. struct btrfs_space_info **space_info)
  2713. {
  2714. struct btrfs_space_info *found;
  2715. int i;
  2716. int factor;
  2717. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2718. BTRFS_BLOCK_GROUP_RAID10))
  2719. factor = 2;
  2720. else
  2721. factor = 1;
  2722. found = __find_space_info(info, flags);
  2723. if (found) {
  2724. spin_lock(&found->lock);
  2725. found->total_bytes += total_bytes;
  2726. found->disk_total += total_bytes * factor;
  2727. found->bytes_used += bytes_used;
  2728. found->disk_used += bytes_used * factor;
  2729. found->full = 0;
  2730. spin_unlock(&found->lock);
  2731. *space_info = found;
  2732. return 0;
  2733. }
  2734. found = kzalloc(sizeof(*found), GFP_NOFS);
  2735. if (!found)
  2736. return -ENOMEM;
  2737. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  2738. INIT_LIST_HEAD(&found->block_groups[i]);
  2739. init_rwsem(&found->groups_sem);
  2740. spin_lock_init(&found->lock);
  2741. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  2742. found->total_bytes = total_bytes;
  2743. found->disk_total = total_bytes * factor;
  2744. found->bytes_used = bytes_used;
  2745. found->disk_used = bytes_used * factor;
  2746. found->bytes_pinned = 0;
  2747. found->bytes_reserved = 0;
  2748. found->bytes_readonly = 0;
  2749. found->bytes_may_use = 0;
  2750. found->full = 0;
  2751. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  2752. found->chunk_alloc = 0;
  2753. found->flush = 0;
  2754. init_waitqueue_head(&found->wait);
  2755. *space_info = found;
  2756. list_add_rcu(&found->list, &info->space_info);
  2757. return 0;
  2758. }
  2759. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  2760. {
  2761. u64 extra_flags = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK;
  2762. /* chunk -> extended profile */
  2763. if (extra_flags == 0)
  2764. extra_flags = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2765. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2766. fs_info->avail_data_alloc_bits |= extra_flags;
  2767. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2768. fs_info->avail_metadata_alloc_bits |= extra_flags;
  2769. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2770. fs_info->avail_system_alloc_bits |= extra_flags;
  2771. }
  2772. /*
  2773. * @flags: available profiles in extended format (see ctree.h)
  2774. *
  2775. * Returns reduced profile in chunk format. If profile changing is in
  2776. * progress (either running or paused) picks the target profile (if it's
  2777. * already available), otherwise falls back to plain reducing.
  2778. */
  2779. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  2780. {
  2781. /*
  2782. * we add in the count of missing devices because we want
  2783. * to make sure that any RAID levels on a degraded FS
  2784. * continue to be honored.
  2785. */
  2786. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  2787. root->fs_info->fs_devices->missing_devices;
  2788. /* pick restriper's target profile if it's available */
  2789. spin_lock(&root->fs_info->balance_lock);
  2790. if (root->fs_info->balance_ctl) {
  2791. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  2792. u64 tgt = 0;
  2793. if ((flags & BTRFS_BLOCK_GROUP_DATA) &&
  2794. (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2795. (flags & bctl->data.target)) {
  2796. tgt = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  2797. } else if ((flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
  2798. (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2799. (flags & bctl->sys.target)) {
  2800. tgt = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  2801. } else if ((flags & BTRFS_BLOCK_GROUP_METADATA) &&
  2802. (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2803. (flags & bctl->meta.target)) {
  2804. tgt = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  2805. }
  2806. if (tgt) {
  2807. spin_unlock(&root->fs_info->balance_lock);
  2808. flags = tgt;
  2809. goto out;
  2810. }
  2811. }
  2812. spin_unlock(&root->fs_info->balance_lock);
  2813. if (num_devices == 1)
  2814. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  2815. if (num_devices < 4)
  2816. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  2817. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  2818. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2819. BTRFS_BLOCK_GROUP_RAID10))) {
  2820. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  2821. }
  2822. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  2823. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  2824. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  2825. }
  2826. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  2827. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  2828. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  2829. (flags & BTRFS_BLOCK_GROUP_DUP))) {
  2830. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  2831. }
  2832. out:
  2833. /* extended -> chunk profile */
  2834. flags &= ~BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2835. return flags;
  2836. }
  2837. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  2838. {
  2839. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2840. flags |= root->fs_info->avail_data_alloc_bits;
  2841. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2842. flags |= root->fs_info->avail_system_alloc_bits;
  2843. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2844. flags |= root->fs_info->avail_metadata_alloc_bits;
  2845. return btrfs_reduce_alloc_profile(root, flags);
  2846. }
  2847. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  2848. {
  2849. u64 flags;
  2850. if (data)
  2851. flags = BTRFS_BLOCK_GROUP_DATA;
  2852. else if (root == root->fs_info->chunk_root)
  2853. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  2854. else
  2855. flags = BTRFS_BLOCK_GROUP_METADATA;
  2856. return get_alloc_profile(root, flags);
  2857. }
  2858. void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
  2859. {
  2860. BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
  2861. BTRFS_BLOCK_GROUP_DATA);
  2862. }
  2863. /*
  2864. * This will check the space that the inode allocates from to make sure we have
  2865. * enough space for bytes.
  2866. */
  2867. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  2868. {
  2869. struct btrfs_space_info *data_sinfo;
  2870. struct btrfs_root *root = BTRFS_I(inode)->root;
  2871. u64 used;
  2872. int ret = 0, committed = 0, alloc_chunk = 1;
  2873. /* make sure bytes are sectorsize aligned */
  2874. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2875. if (root == root->fs_info->tree_root ||
  2876. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  2877. alloc_chunk = 0;
  2878. committed = 1;
  2879. }
  2880. data_sinfo = BTRFS_I(inode)->space_info;
  2881. if (!data_sinfo)
  2882. goto alloc;
  2883. again:
  2884. /* make sure we have enough space to handle the data first */
  2885. spin_lock(&data_sinfo->lock);
  2886. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  2887. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  2888. data_sinfo->bytes_may_use;
  2889. if (used + bytes > data_sinfo->total_bytes) {
  2890. struct btrfs_trans_handle *trans;
  2891. /*
  2892. * if we don't have enough free bytes in this space then we need
  2893. * to alloc a new chunk.
  2894. */
  2895. if (!data_sinfo->full && alloc_chunk) {
  2896. u64 alloc_target;
  2897. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  2898. spin_unlock(&data_sinfo->lock);
  2899. alloc:
  2900. alloc_target = btrfs_get_alloc_profile(root, 1);
  2901. trans = btrfs_join_transaction(root);
  2902. if (IS_ERR(trans))
  2903. return PTR_ERR(trans);
  2904. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2905. bytes + 2 * 1024 * 1024,
  2906. alloc_target,
  2907. CHUNK_ALLOC_NO_FORCE);
  2908. btrfs_end_transaction(trans, root);
  2909. if (ret < 0) {
  2910. if (ret != -ENOSPC)
  2911. return ret;
  2912. else
  2913. goto commit_trans;
  2914. }
  2915. if (!data_sinfo) {
  2916. btrfs_set_inode_space_info(root, inode);
  2917. data_sinfo = BTRFS_I(inode)->space_info;
  2918. }
  2919. goto again;
  2920. }
  2921. /*
  2922. * If we have less pinned bytes than we want to allocate then
  2923. * don't bother committing the transaction, it won't help us.
  2924. */
  2925. if (data_sinfo->bytes_pinned < bytes)
  2926. committed = 1;
  2927. spin_unlock(&data_sinfo->lock);
  2928. /* commit the current transaction and try again */
  2929. commit_trans:
  2930. if (!committed &&
  2931. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  2932. committed = 1;
  2933. trans = btrfs_join_transaction(root);
  2934. if (IS_ERR(trans))
  2935. return PTR_ERR(trans);
  2936. ret = btrfs_commit_transaction(trans, root);
  2937. if (ret)
  2938. return ret;
  2939. goto again;
  2940. }
  2941. return -ENOSPC;
  2942. }
  2943. data_sinfo->bytes_may_use += bytes;
  2944. trace_btrfs_space_reservation(root->fs_info, "space_info",
  2945. (u64)(unsigned long)data_sinfo,
  2946. bytes, 1);
  2947. spin_unlock(&data_sinfo->lock);
  2948. return 0;
  2949. }
  2950. /*
  2951. * Called if we need to clear a data reservation for this inode.
  2952. */
  2953. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  2954. {
  2955. struct btrfs_root *root = BTRFS_I(inode)->root;
  2956. struct btrfs_space_info *data_sinfo;
  2957. /* make sure bytes are sectorsize aligned */
  2958. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2959. data_sinfo = BTRFS_I(inode)->space_info;
  2960. spin_lock(&data_sinfo->lock);
  2961. data_sinfo->bytes_may_use -= bytes;
  2962. trace_btrfs_space_reservation(root->fs_info, "space_info",
  2963. (u64)(unsigned long)data_sinfo,
  2964. bytes, 0);
  2965. spin_unlock(&data_sinfo->lock);
  2966. }
  2967. static void force_metadata_allocation(struct btrfs_fs_info *info)
  2968. {
  2969. struct list_head *head = &info->space_info;
  2970. struct btrfs_space_info *found;
  2971. rcu_read_lock();
  2972. list_for_each_entry_rcu(found, head, list) {
  2973. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  2974. found->force_alloc = CHUNK_ALLOC_FORCE;
  2975. }
  2976. rcu_read_unlock();
  2977. }
  2978. static int should_alloc_chunk(struct btrfs_root *root,
  2979. struct btrfs_space_info *sinfo, u64 alloc_bytes,
  2980. int force)
  2981. {
  2982. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  2983. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  2984. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  2985. u64 thresh;
  2986. if (force == CHUNK_ALLOC_FORCE)
  2987. return 1;
  2988. /*
  2989. * We need to take into account the global rsv because for all intents
  2990. * and purposes it's used space. Don't worry about locking the
  2991. * global_rsv, it doesn't change except when the transaction commits.
  2992. */
  2993. num_allocated += global_rsv->size;
  2994. /*
  2995. * in limited mode, we want to have some free space up to
  2996. * about 1% of the FS size.
  2997. */
  2998. if (force == CHUNK_ALLOC_LIMITED) {
  2999. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3000. thresh = max_t(u64, 64 * 1024 * 1024,
  3001. div_factor_fine(thresh, 1));
  3002. if (num_bytes - num_allocated < thresh)
  3003. return 1;
  3004. }
  3005. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3006. /* 256MB or 2% of the FS */
  3007. thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 2));
  3008. /* system chunks need a much small threshold */
  3009. if (sinfo->flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3010. thresh = 32 * 1024 * 1024;
  3011. if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 8))
  3012. return 0;
  3013. return 1;
  3014. }
  3015. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3016. struct btrfs_root *extent_root, u64 alloc_bytes,
  3017. u64 flags, int force)
  3018. {
  3019. struct btrfs_space_info *space_info;
  3020. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3021. int wait_for_alloc = 0;
  3022. int ret = 0;
  3023. BUG_ON(!profile_is_valid(flags, 0));
  3024. space_info = __find_space_info(extent_root->fs_info, flags);
  3025. if (!space_info) {
  3026. ret = update_space_info(extent_root->fs_info, flags,
  3027. 0, 0, &space_info);
  3028. BUG_ON(ret);
  3029. }
  3030. BUG_ON(!space_info);
  3031. again:
  3032. spin_lock(&space_info->lock);
  3033. if (force < space_info->force_alloc)
  3034. force = space_info->force_alloc;
  3035. if (space_info->full) {
  3036. spin_unlock(&space_info->lock);
  3037. return 0;
  3038. }
  3039. if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
  3040. spin_unlock(&space_info->lock);
  3041. return 0;
  3042. } else if (space_info->chunk_alloc) {
  3043. wait_for_alloc = 1;
  3044. } else {
  3045. space_info->chunk_alloc = 1;
  3046. }
  3047. spin_unlock(&space_info->lock);
  3048. mutex_lock(&fs_info->chunk_mutex);
  3049. /*
  3050. * The chunk_mutex is held throughout the entirety of a chunk
  3051. * allocation, so once we've acquired the chunk_mutex we know that the
  3052. * other guy is done and we need to recheck and see if we should
  3053. * allocate.
  3054. */
  3055. if (wait_for_alloc) {
  3056. mutex_unlock(&fs_info->chunk_mutex);
  3057. wait_for_alloc = 0;
  3058. goto again;
  3059. }
  3060. /*
  3061. * If we have mixed data/metadata chunks we want to make sure we keep
  3062. * allocating mixed chunks instead of individual chunks.
  3063. */
  3064. if (btrfs_mixed_space_info(space_info))
  3065. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3066. /*
  3067. * if we're doing a data chunk, go ahead and make sure that
  3068. * we keep a reasonable number of metadata chunks allocated in the
  3069. * FS as well.
  3070. */
  3071. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3072. fs_info->data_chunk_allocations++;
  3073. if (!(fs_info->data_chunk_allocations %
  3074. fs_info->metadata_ratio))
  3075. force_metadata_allocation(fs_info);
  3076. }
  3077. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3078. if (ret < 0 && ret != -ENOSPC)
  3079. goto out;
  3080. spin_lock(&space_info->lock);
  3081. if (ret)
  3082. space_info->full = 1;
  3083. else
  3084. ret = 1;
  3085. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3086. space_info->chunk_alloc = 0;
  3087. spin_unlock(&space_info->lock);
  3088. out:
  3089. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  3090. return ret;
  3091. }
  3092. /*
  3093. * shrink metadata reservation for delalloc
  3094. */
  3095. static int shrink_delalloc(struct btrfs_root *root, u64 to_reclaim,
  3096. bool wait_ordered)
  3097. {
  3098. struct btrfs_block_rsv *block_rsv;
  3099. struct btrfs_space_info *space_info;
  3100. struct btrfs_trans_handle *trans;
  3101. u64 reserved;
  3102. u64 max_reclaim;
  3103. u64 reclaimed = 0;
  3104. long time_left;
  3105. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3106. int loops = 0;
  3107. unsigned long progress;
  3108. trans = (struct btrfs_trans_handle *)current->journal_info;
  3109. block_rsv = &root->fs_info->delalloc_block_rsv;
  3110. space_info = block_rsv->space_info;
  3111. smp_mb();
  3112. reserved = space_info->bytes_may_use;
  3113. progress = space_info->reservation_progress;
  3114. if (reserved == 0)
  3115. return 0;
  3116. smp_mb();
  3117. if (root->fs_info->delalloc_bytes == 0) {
  3118. if (trans)
  3119. return 0;
  3120. btrfs_wait_ordered_extents(root, 0, 0);
  3121. return 0;
  3122. }
  3123. max_reclaim = min(reserved, to_reclaim);
  3124. nr_pages = max_t(unsigned long, nr_pages,
  3125. max_reclaim >> PAGE_CACHE_SHIFT);
  3126. while (loops < 1024) {
  3127. /* have the flusher threads jump in and do some IO */
  3128. smp_mb();
  3129. nr_pages = min_t(unsigned long, nr_pages,
  3130. root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
  3131. writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages,
  3132. WB_REASON_FS_FREE_SPACE);
  3133. spin_lock(&space_info->lock);
  3134. if (reserved > space_info->bytes_may_use)
  3135. reclaimed += reserved - space_info->bytes_may_use;
  3136. reserved = space_info->bytes_may_use;
  3137. spin_unlock(&space_info->lock);
  3138. loops++;
  3139. if (reserved == 0 || reclaimed >= max_reclaim)
  3140. break;
  3141. if (trans && trans->transaction->blocked)
  3142. return -EAGAIN;
  3143. if (wait_ordered && !trans) {
  3144. btrfs_wait_ordered_extents(root, 0, 0);
  3145. } else {
  3146. time_left = schedule_timeout_interruptible(1);
  3147. /* We were interrupted, exit */
  3148. if (time_left)
  3149. break;
  3150. }
  3151. /* we've kicked the IO a few times, if anything has been freed,
  3152. * exit. There is no sense in looping here for a long time
  3153. * when we really need to commit the transaction, or there are
  3154. * just too many writers without enough free space
  3155. */
  3156. if (loops > 3) {
  3157. smp_mb();
  3158. if (progress != space_info->reservation_progress)
  3159. break;
  3160. }
  3161. }
  3162. return reclaimed >= to_reclaim;
  3163. }
  3164. /**
  3165. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3166. * @root - the root we're allocating for
  3167. * @bytes - the number of bytes we want to reserve
  3168. * @force - force the commit
  3169. *
  3170. * This will check to make sure that committing the transaction will actually
  3171. * get us somewhere and then commit the transaction if it does. Otherwise it
  3172. * will return -ENOSPC.
  3173. */
  3174. static int may_commit_transaction(struct btrfs_root *root,
  3175. struct btrfs_space_info *space_info,
  3176. u64 bytes, int force)
  3177. {
  3178. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3179. struct btrfs_trans_handle *trans;
  3180. trans = (struct btrfs_trans_handle *)current->journal_info;
  3181. if (trans)
  3182. return -EAGAIN;
  3183. if (force)
  3184. goto commit;
  3185. /* See if there is enough pinned space to make this reservation */
  3186. spin_lock(&space_info->lock);
  3187. if (space_info->bytes_pinned >= bytes) {
  3188. spin_unlock(&space_info->lock);
  3189. goto commit;
  3190. }
  3191. spin_unlock(&space_info->lock);
  3192. /*
  3193. * See if there is some space in the delayed insertion reservation for
  3194. * this reservation.
  3195. */
  3196. if (space_info != delayed_rsv->space_info)
  3197. return -ENOSPC;
  3198. spin_lock(&space_info->lock);
  3199. spin_lock(&delayed_rsv->lock);
  3200. if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
  3201. spin_unlock(&delayed_rsv->lock);
  3202. spin_unlock(&space_info->lock);
  3203. return -ENOSPC;
  3204. }
  3205. spin_unlock(&delayed_rsv->lock);
  3206. spin_unlock(&space_info->lock);
  3207. commit:
  3208. trans = btrfs_join_transaction(root);
  3209. if (IS_ERR(trans))
  3210. return -ENOSPC;
  3211. return btrfs_commit_transaction(trans, root);
  3212. }
  3213. /**
  3214. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3215. * @root - the root we're allocating for
  3216. * @block_rsv - the block_rsv we're allocating for
  3217. * @orig_bytes - the number of bytes we want
  3218. * @flush - wether or not we can flush to make our reservation
  3219. *
  3220. * This will reserve orgi_bytes number of bytes from the space info associated
  3221. * with the block_rsv. If there is not enough space it will make an attempt to
  3222. * flush out space to make room. It will do this by flushing delalloc if
  3223. * possible or committing the transaction. If flush is 0 then no attempts to
  3224. * regain reservations will be made and this will fail if there is not enough
  3225. * space already.
  3226. */
  3227. static int reserve_metadata_bytes(struct btrfs_root *root,
  3228. struct btrfs_block_rsv *block_rsv,
  3229. u64 orig_bytes, int flush)
  3230. {
  3231. struct btrfs_space_info *space_info = block_rsv->space_info;
  3232. u64 used;
  3233. u64 num_bytes = orig_bytes;
  3234. int retries = 0;
  3235. int ret = 0;
  3236. bool committed = false;
  3237. bool flushing = false;
  3238. bool wait_ordered = false;
  3239. again:
  3240. ret = 0;
  3241. spin_lock(&space_info->lock);
  3242. /*
  3243. * We only want to wait if somebody other than us is flushing and we are
  3244. * actually alloed to flush.
  3245. */
  3246. while (flush && !flushing && space_info->flush) {
  3247. spin_unlock(&space_info->lock);
  3248. /*
  3249. * If we have a trans handle we can't wait because the flusher
  3250. * may have to commit the transaction, which would mean we would
  3251. * deadlock since we are waiting for the flusher to finish, but
  3252. * hold the current transaction open.
  3253. */
  3254. if (current->journal_info)
  3255. return -EAGAIN;
  3256. ret = wait_event_interruptible(space_info->wait,
  3257. !space_info->flush);
  3258. /* Must have been interrupted, return */
  3259. if (ret)
  3260. return -EINTR;
  3261. spin_lock(&space_info->lock);
  3262. }
  3263. ret = -ENOSPC;
  3264. used = space_info->bytes_used + space_info->bytes_reserved +
  3265. space_info->bytes_pinned + space_info->bytes_readonly +
  3266. space_info->bytes_may_use;
  3267. /*
  3268. * The idea here is that we've not already over-reserved the block group
  3269. * then we can go ahead and save our reservation first and then start
  3270. * flushing if we need to. Otherwise if we've already overcommitted
  3271. * lets start flushing stuff first and then come back and try to make
  3272. * our reservation.
  3273. */
  3274. if (used <= space_info->total_bytes) {
  3275. if (used + orig_bytes <= space_info->total_bytes) {
  3276. space_info->bytes_may_use += orig_bytes;
  3277. trace_btrfs_space_reservation(root->fs_info,
  3278. "space_info",
  3279. (u64)(unsigned long)space_info,
  3280. orig_bytes, 1);
  3281. ret = 0;
  3282. } else {
  3283. /*
  3284. * Ok set num_bytes to orig_bytes since we aren't
  3285. * overocmmitted, this way we only try and reclaim what
  3286. * we need.
  3287. */
  3288. num_bytes = orig_bytes;
  3289. }
  3290. } else {
  3291. /*
  3292. * Ok we're over committed, set num_bytes to the overcommitted
  3293. * amount plus the amount of bytes that we need for this
  3294. * reservation.
  3295. */
  3296. wait_ordered = true;
  3297. num_bytes = used - space_info->total_bytes +
  3298. (orig_bytes * (retries + 1));
  3299. }
  3300. if (ret) {
  3301. u64 profile = btrfs_get_alloc_profile(root, 0);
  3302. u64 avail;
  3303. /*
  3304. * If we have a lot of space that's pinned, don't bother doing
  3305. * the overcommit dance yet and just commit the transaction.
  3306. */
  3307. avail = (space_info->total_bytes - space_info->bytes_used) * 8;
  3308. do_div(avail, 10);
  3309. if (space_info->bytes_pinned >= avail && flush && !committed) {
  3310. space_info->flush = 1;
  3311. flushing = true;
  3312. spin_unlock(&space_info->lock);
  3313. ret = may_commit_transaction(root, space_info,
  3314. orig_bytes, 1);
  3315. if (ret)
  3316. goto out;
  3317. committed = true;
  3318. goto again;
  3319. }
  3320. spin_lock(&root->fs_info->free_chunk_lock);
  3321. avail = root->fs_info->free_chunk_space;
  3322. /*
  3323. * If we have dup, raid1 or raid10 then only half of the free
  3324. * space is actually useable.
  3325. */
  3326. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3327. BTRFS_BLOCK_GROUP_RAID1 |
  3328. BTRFS_BLOCK_GROUP_RAID10))
  3329. avail >>= 1;
  3330. /*
  3331. * If we aren't flushing don't let us overcommit too much, say
  3332. * 1/8th of the space. If we can flush, let it overcommit up to
  3333. * 1/2 of the space.
  3334. */
  3335. if (flush)
  3336. avail >>= 3;
  3337. else
  3338. avail >>= 1;
  3339. spin_unlock(&root->fs_info->free_chunk_lock);
  3340. if (used + num_bytes < space_info->total_bytes + avail) {
  3341. space_info->bytes_may_use += orig_bytes;
  3342. trace_btrfs_space_reservation(root->fs_info,
  3343. "space_info",
  3344. (u64)(unsigned long)space_info,
  3345. orig_bytes, 1);
  3346. ret = 0;
  3347. } else {
  3348. wait_ordered = true;
  3349. }
  3350. }
  3351. /*
  3352. * Couldn't make our reservation, save our place so while we're trying
  3353. * to reclaim space we can actually use it instead of somebody else
  3354. * stealing it from us.
  3355. */
  3356. if (ret && flush) {
  3357. flushing = true;
  3358. space_info->flush = 1;
  3359. }
  3360. spin_unlock(&space_info->lock);
  3361. if (!ret || !flush)
  3362. goto out;
  3363. /*
  3364. * We do synchronous shrinking since we don't actually unreserve
  3365. * metadata until after the IO is completed.
  3366. */
  3367. ret = shrink_delalloc(root, num_bytes, wait_ordered);
  3368. if (ret < 0)
  3369. goto out;
  3370. ret = 0;
  3371. /*
  3372. * So if we were overcommitted it's possible that somebody else flushed
  3373. * out enough space and we simply didn't have enough space to reclaim,
  3374. * so go back around and try again.
  3375. */
  3376. if (retries < 2) {
  3377. wait_ordered = true;
  3378. retries++;
  3379. goto again;
  3380. }
  3381. ret = -ENOSPC;
  3382. if (committed)
  3383. goto out;
  3384. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3385. if (!ret) {
  3386. committed = true;
  3387. goto again;
  3388. }
  3389. out:
  3390. if (flushing) {
  3391. spin_lock(&space_info->lock);
  3392. space_info->flush = 0;
  3393. wake_up_all(&space_info->wait);
  3394. spin_unlock(&space_info->lock);
  3395. }
  3396. return ret;
  3397. }
  3398. static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
  3399. struct btrfs_root *root)
  3400. {
  3401. struct btrfs_block_rsv *block_rsv = NULL;
  3402. if (root->ref_cows || root == root->fs_info->csum_root)
  3403. block_rsv = trans->block_rsv;
  3404. if (!block_rsv)
  3405. block_rsv = root->block_rsv;
  3406. if (!block_rsv)
  3407. block_rsv = &root->fs_info->empty_block_rsv;
  3408. return block_rsv;
  3409. }
  3410. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3411. u64 num_bytes)
  3412. {
  3413. int ret = -ENOSPC;
  3414. spin_lock(&block_rsv->lock);
  3415. if (block_rsv->reserved >= num_bytes) {
  3416. block_rsv->reserved -= num_bytes;
  3417. if (block_rsv->reserved < block_rsv->size)
  3418. block_rsv->full = 0;
  3419. ret = 0;
  3420. }
  3421. spin_unlock(&block_rsv->lock);
  3422. return ret;
  3423. }
  3424. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3425. u64 num_bytes, int update_size)
  3426. {
  3427. spin_lock(&block_rsv->lock);
  3428. block_rsv->reserved += num_bytes;
  3429. if (update_size)
  3430. block_rsv->size += num_bytes;
  3431. else if (block_rsv->reserved >= block_rsv->size)
  3432. block_rsv->full = 1;
  3433. spin_unlock(&block_rsv->lock);
  3434. }
  3435. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3436. struct btrfs_block_rsv *block_rsv,
  3437. struct btrfs_block_rsv *dest, u64 num_bytes)
  3438. {
  3439. struct btrfs_space_info *space_info = block_rsv->space_info;
  3440. spin_lock(&block_rsv->lock);
  3441. if (num_bytes == (u64)-1)
  3442. num_bytes = block_rsv->size;
  3443. block_rsv->size -= num_bytes;
  3444. if (block_rsv->reserved >= block_rsv->size) {
  3445. num_bytes = block_rsv->reserved - block_rsv->size;
  3446. block_rsv->reserved = block_rsv->size;
  3447. block_rsv->full = 1;
  3448. } else {
  3449. num_bytes = 0;
  3450. }
  3451. spin_unlock(&block_rsv->lock);
  3452. if (num_bytes > 0) {
  3453. if (dest) {
  3454. spin_lock(&dest->lock);
  3455. if (!dest->full) {
  3456. u64 bytes_to_add;
  3457. bytes_to_add = dest->size - dest->reserved;
  3458. bytes_to_add = min(num_bytes, bytes_to_add);
  3459. dest->reserved += bytes_to_add;
  3460. if (dest->reserved >= dest->size)
  3461. dest->full = 1;
  3462. num_bytes -= bytes_to_add;
  3463. }
  3464. spin_unlock(&dest->lock);
  3465. }
  3466. if (num_bytes) {
  3467. spin_lock(&space_info->lock);
  3468. space_info->bytes_may_use -= num_bytes;
  3469. trace_btrfs_space_reservation(fs_info, "space_info",
  3470. (u64)(unsigned long)space_info,
  3471. num_bytes, 0);
  3472. space_info->reservation_progress++;
  3473. spin_unlock(&space_info->lock);
  3474. }
  3475. }
  3476. }
  3477. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3478. struct btrfs_block_rsv *dst, u64 num_bytes)
  3479. {
  3480. int ret;
  3481. ret = block_rsv_use_bytes(src, num_bytes);
  3482. if (ret)
  3483. return ret;
  3484. block_rsv_add_bytes(dst, num_bytes, 1);
  3485. return 0;
  3486. }
  3487. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
  3488. {
  3489. memset(rsv, 0, sizeof(*rsv));
  3490. spin_lock_init(&rsv->lock);
  3491. }
  3492. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
  3493. {
  3494. struct btrfs_block_rsv *block_rsv;
  3495. struct btrfs_fs_info *fs_info = root->fs_info;
  3496. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3497. if (!block_rsv)
  3498. return NULL;
  3499. btrfs_init_block_rsv(block_rsv);
  3500. block_rsv->space_info = __find_space_info(fs_info,
  3501. BTRFS_BLOCK_GROUP_METADATA);
  3502. return block_rsv;
  3503. }
  3504. void btrfs_free_block_rsv(struct btrfs_root *root,
  3505. struct btrfs_block_rsv *rsv)
  3506. {
  3507. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3508. kfree(rsv);
  3509. }
  3510. static inline int __block_rsv_add(struct btrfs_root *root,
  3511. struct btrfs_block_rsv *block_rsv,
  3512. u64 num_bytes, int flush)
  3513. {
  3514. int ret;
  3515. if (num_bytes == 0)
  3516. return 0;
  3517. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3518. if (!ret) {
  3519. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3520. return 0;
  3521. }
  3522. return ret;
  3523. }
  3524. int btrfs_block_rsv_add(struct btrfs_root *root,
  3525. struct btrfs_block_rsv *block_rsv,
  3526. u64 num_bytes)
  3527. {
  3528. return __block_rsv_add(root, block_rsv, num_bytes, 1);
  3529. }
  3530. int btrfs_block_rsv_add_noflush(struct btrfs_root *root,
  3531. struct btrfs_block_rsv *block_rsv,
  3532. u64 num_bytes)
  3533. {
  3534. return __block_rsv_add(root, block_rsv, num_bytes, 0);
  3535. }
  3536. int btrfs_block_rsv_check(struct btrfs_root *root,
  3537. struct btrfs_block_rsv *block_rsv, int min_factor)
  3538. {
  3539. u64 num_bytes = 0;
  3540. int ret = -ENOSPC;
  3541. if (!block_rsv)
  3542. return 0;
  3543. spin_lock(&block_rsv->lock);
  3544. num_bytes = div_factor(block_rsv->size, min_factor);
  3545. if (block_rsv->reserved >= num_bytes)
  3546. ret = 0;
  3547. spin_unlock(&block_rsv->lock);
  3548. return ret;
  3549. }
  3550. static inline int __btrfs_block_rsv_refill(struct btrfs_root *root,
  3551. struct btrfs_block_rsv *block_rsv,
  3552. u64 min_reserved, int flush)
  3553. {
  3554. u64 num_bytes = 0;
  3555. int ret = -ENOSPC;
  3556. if (!block_rsv)
  3557. return 0;
  3558. spin_lock(&block_rsv->lock);
  3559. num_bytes = min_reserved;
  3560. if (block_rsv->reserved >= num_bytes)
  3561. ret = 0;
  3562. else
  3563. num_bytes -= block_rsv->reserved;
  3564. spin_unlock(&block_rsv->lock);
  3565. if (!ret)
  3566. return 0;
  3567. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3568. if (!ret) {
  3569. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  3570. return 0;
  3571. }
  3572. return ret;
  3573. }
  3574. int btrfs_block_rsv_refill(struct btrfs_root *root,
  3575. struct btrfs_block_rsv *block_rsv,
  3576. u64 min_reserved)
  3577. {
  3578. return __btrfs_block_rsv_refill(root, block_rsv, min_reserved, 1);
  3579. }
  3580. int btrfs_block_rsv_refill_noflush(struct btrfs_root *root,
  3581. struct btrfs_block_rsv *block_rsv,
  3582. u64 min_reserved)
  3583. {
  3584. return __btrfs_block_rsv_refill(root, block_rsv, min_reserved, 0);
  3585. }
  3586. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  3587. struct btrfs_block_rsv *dst_rsv,
  3588. u64 num_bytes)
  3589. {
  3590. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3591. }
  3592. void btrfs_block_rsv_release(struct btrfs_root *root,
  3593. struct btrfs_block_rsv *block_rsv,
  3594. u64 num_bytes)
  3595. {
  3596. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3597. if (global_rsv->full || global_rsv == block_rsv ||
  3598. block_rsv->space_info != global_rsv->space_info)
  3599. global_rsv = NULL;
  3600. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  3601. num_bytes);
  3602. }
  3603. /*
  3604. * helper to calculate size of global block reservation.
  3605. * the desired value is sum of space used by extent tree,
  3606. * checksum tree and root tree
  3607. */
  3608. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  3609. {
  3610. struct btrfs_space_info *sinfo;
  3611. u64 num_bytes;
  3612. u64 meta_used;
  3613. u64 data_used;
  3614. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  3615. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  3616. spin_lock(&sinfo->lock);
  3617. data_used = sinfo->bytes_used;
  3618. spin_unlock(&sinfo->lock);
  3619. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3620. spin_lock(&sinfo->lock);
  3621. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  3622. data_used = 0;
  3623. meta_used = sinfo->bytes_used;
  3624. spin_unlock(&sinfo->lock);
  3625. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  3626. csum_size * 2;
  3627. num_bytes += div64_u64(data_used + meta_used, 50);
  3628. if (num_bytes * 3 > meta_used)
  3629. num_bytes = div64_u64(meta_used, 3) * 2;
  3630. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  3631. }
  3632. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  3633. {
  3634. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  3635. struct btrfs_space_info *sinfo = block_rsv->space_info;
  3636. u64 num_bytes;
  3637. num_bytes = calc_global_metadata_size(fs_info);
  3638. spin_lock(&block_rsv->lock);
  3639. spin_lock(&sinfo->lock);
  3640. block_rsv->size = num_bytes;
  3641. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  3642. sinfo->bytes_reserved + sinfo->bytes_readonly +
  3643. sinfo->bytes_may_use;
  3644. if (sinfo->total_bytes > num_bytes) {
  3645. num_bytes = sinfo->total_bytes - num_bytes;
  3646. block_rsv->reserved += num_bytes;
  3647. sinfo->bytes_may_use += num_bytes;
  3648. trace_btrfs_space_reservation(fs_info, "space_info",
  3649. (u64)(unsigned long)sinfo, num_bytes, 1);
  3650. }
  3651. if (block_rsv->reserved >= block_rsv->size) {
  3652. num_bytes = block_rsv->reserved - block_rsv->size;
  3653. sinfo->bytes_may_use -= num_bytes;
  3654. trace_btrfs_space_reservation(fs_info, "space_info",
  3655. (u64)(unsigned long)sinfo, num_bytes, 0);
  3656. sinfo->reservation_progress++;
  3657. block_rsv->reserved = block_rsv->size;
  3658. block_rsv->full = 1;
  3659. }
  3660. spin_unlock(&sinfo->lock);
  3661. spin_unlock(&block_rsv->lock);
  3662. }
  3663. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  3664. {
  3665. struct btrfs_space_info *space_info;
  3666. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3667. fs_info->chunk_block_rsv.space_info = space_info;
  3668. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3669. fs_info->global_block_rsv.space_info = space_info;
  3670. fs_info->delalloc_block_rsv.space_info = space_info;
  3671. fs_info->trans_block_rsv.space_info = space_info;
  3672. fs_info->empty_block_rsv.space_info = space_info;
  3673. fs_info->delayed_block_rsv.space_info = space_info;
  3674. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  3675. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  3676. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  3677. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  3678. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  3679. update_global_block_rsv(fs_info);
  3680. }
  3681. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  3682. {
  3683. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  3684. (u64)-1);
  3685. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  3686. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  3687. WARN_ON(fs_info->trans_block_rsv.size > 0);
  3688. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  3689. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  3690. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  3691. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  3692. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  3693. }
  3694. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  3695. struct btrfs_root *root)
  3696. {
  3697. if (!trans->bytes_reserved)
  3698. return;
  3699. trace_btrfs_space_reservation(root->fs_info, "transaction",
  3700. (u64)(unsigned long)trans,
  3701. trans->bytes_reserved, 0);
  3702. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  3703. trans->bytes_reserved = 0;
  3704. }
  3705. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  3706. struct inode *inode)
  3707. {
  3708. struct btrfs_root *root = BTRFS_I(inode)->root;
  3709. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3710. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  3711. /*
  3712. * We need to hold space in order to delete our orphan item once we've
  3713. * added it, so this takes the reservation so we can release it later
  3714. * when we are truly done with the orphan item.
  3715. */
  3716. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  3717. trace_btrfs_space_reservation(root->fs_info, "orphan",
  3718. btrfs_ino(inode), num_bytes, 1);
  3719. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3720. }
  3721. void btrfs_orphan_release_metadata(struct inode *inode)
  3722. {
  3723. struct btrfs_root *root = BTRFS_I(inode)->root;
  3724. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  3725. trace_btrfs_space_reservation(root->fs_info, "orphan",
  3726. btrfs_ino(inode), num_bytes, 0);
  3727. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  3728. }
  3729. int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
  3730. struct btrfs_pending_snapshot *pending)
  3731. {
  3732. struct btrfs_root *root = pending->root;
  3733. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3734. struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
  3735. /*
  3736. * two for root back/forward refs, two for directory entries
  3737. * and one for root of the snapshot.
  3738. */
  3739. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
  3740. dst_rsv->space_info = src_rsv->space_info;
  3741. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3742. }
  3743. /**
  3744. * drop_outstanding_extent - drop an outstanding extent
  3745. * @inode: the inode we're dropping the extent for
  3746. *
  3747. * This is called when we are freeing up an outstanding extent, either called
  3748. * after an error or after an extent is written. This will return the number of
  3749. * reserved extents that need to be freed. This must be called with
  3750. * BTRFS_I(inode)->lock held.
  3751. */
  3752. static unsigned drop_outstanding_extent(struct inode *inode)
  3753. {
  3754. unsigned drop_inode_space = 0;
  3755. unsigned dropped_extents = 0;
  3756. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  3757. BTRFS_I(inode)->outstanding_extents--;
  3758. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  3759. BTRFS_I(inode)->delalloc_meta_reserved) {
  3760. drop_inode_space = 1;
  3761. BTRFS_I(inode)->delalloc_meta_reserved = 0;
  3762. }
  3763. /*
  3764. * If we have more or the same amount of outsanding extents than we have
  3765. * reserved then we need to leave the reserved extents count alone.
  3766. */
  3767. if (BTRFS_I(inode)->outstanding_extents >=
  3768. BTRFS_I(inode)->reserved_extents)
  3769. return drop_inode_space;
  3770. dropped_extents = BTRFS_I(inode)->reserved_extents -
  3771. BTRFS_I(inode)->outstanding_extents;
  3772. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  3773. return dropped_extents + drop_inode_space;
  3774. }
  3775. /**
  3776. * calc_csum_metadata_size - return the amount of metada space that must be
  3777. * reserved/free'd for the given bytes.
  3778. * @inode: the inode we're manipulating
  3779. * @num_bytes: the number of bytes in question
  3780. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  3781. *
  3782. * This adjusts the number of csum_bytes in the inode and then returns the
  3783. * correct amount of metadata that must either be reserved or freed. We
  3784. * calculate how many checksums we can fit into one leaf and then divide the
  3785. * number of bytes that will need to be checksumed by this value to figure out
  3786. * how many checksums will be required. If we are adding bytes then the number
  3787. * may go up and we will return the number of additional bytes that must be
  3788. * reserved. If it is going down we will return the number of bytes that must
  3789. * be freed.
  3790. *
  3791. * This must be called with BTRFS_I(inode)->lock held.
  3792. */
  3793. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  3794. int reserve)
  3795. {
  3796. struct btrfs_root *root = BTRFS_I(inode)->root;
  3797. u64 csum_size;
  3798. int num_csums_per_leaf;
  3799. int num_csums;
  3800. int old_csums;
  3801. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  3802. BTRFS_I(inode)->csum_bytes == 0)
  3803. return 0;
  3804. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  3805. if (reserve)
  3806. BTRFS_I(inode)->csum_bytes += num_bytes;
  3807. else
  3808. BTRFS_I(inode)->csum_bytes -= num_bytes;
  3809. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  3810. num_csums_per_leaf = (int)div64_u64(csum_size,
  3811. sizeof(struct btrfs_csum_item) +
  3812. sizeof(struct btrfs_disk_key));
  3813. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  3814. num_csums = num_csums + num_csums_per_leaf - 1;
  3815. num_csums = num_csums / num_csums_per_leaf;
  3816. old_csums = old_csums + num_csums_per_leaf - 1;
  3817. old_csums = old_csums / num_csums_per_leaf;
  3818. /* No change, no need to reserve more */
  3819. if (old_csums == num_csums)
  3820. return 0;
  3821. if (reserve)
  3822. return btrfs_calc_trans_metadata_size(root,
  3823. num_csums - old_csums);
  3824. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  3825. }
  3826. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  3827. {
  3828. struct btrfs_root *root = BTRFS_I(inode)->root;
  3829. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  3830. u64 to_reserve = 0;
  3831. u64 csum_bytes;
  3832. unsigned nr_extents = 0;
  3833. int extra_reserve = 0;
  3834. int flush = 1;
  3835. int ret;
  3836. /* Need to be holding the i_mutex here if we aren't free space cache */
  3837. if (btrfs_is_free_space_inode(root, inode))
  3838. flush = 0;
  3839. if (flush && btrfs_transaction_in_commit(root->fs_info))
  3840. schedule_timeout(1);
  3841. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  3842. num_bytes = ALIGN(num_bytes, root->sectorsize);
  3843. spin_lock(&BTRFS_I(inode)->lock);
  3844. BTRFS_I(inode)->outstanding_extents++;
  3845. if (BTRFS_I(inode)->outstanding_extents >
  3846. BTRFS_I(inode)->reserved_extents)
  3847. nr_extents = BTRFS_I(inode)->outstanding_extents -
  3848. BTRFS_I(inode)->reserved_extents;
  3849. /*
  3850. * Add an item to reserve for updating the inode when we complete the
  3851. * delalloc io.
  3852. */
  3853. if (!BTRFS_I(inode)->delalloc_meta_reserved) {
  3854. nr_extents++;
  3855. extra_reserve = 1;
  3856. }
  3857. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  3858. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  3859. csum_bytes = BTRFS_I(inode)->csum_bytes;
  3860. spin_unlock(&BTRFS_I(inode)->lock);
  3861. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  3862. if (ret) {
  3863. u64 to_free = 0;
  3864. unsigned dropped;
  3865. spin_lock(&BTRFS_I(inode)->lock);
  3866. dropped = drop_outstanding_extent(inode);
  3867. /*
  3868. * If the inodes csum_bytes is the same as the original
  3869. * csum_bytes then we know we haven't raced with any free()ers
  3870. * so we can just reduce our inodes csum bytes and carry on.
  3871. * Otherwise we have to do the normal free thing to account for
  3872. * the case that the free side didn't free up its reserve
  3873. * because of this outstanding reservation.
  3874. */
  3875. if (BTRFS_I(inode)->csum_bytes == csum_bytes)
  3876. calc_csum_metadata_size(inode, num_bytes, 0);
  3877. else
  3878. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  3879. spin_unlock(&BTRFS_I(inode)->lock);
  3880. if (dropped)
  3881. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  3882. if (to_free) {
  3883. btrfs_block_rsv_release(root, block_rsv, to_free);
  3884. trace_btrfs_space_reservation(root->fs_info,
  3885. "delalloc",
  3886. btrfs_ino(inode),
  3887. to_free, 0);
  3888. }
  3889. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  3890. return ret;
  3891. }
  3892. spin_lock(&BTRFS_I(inode)->lock);
  3893. if (extra_reserve) {
  3894. BTRFS_I(inode)->delalloc_meta_reserved = 1;
  3895. nr_extents--;
  3896. }
  3897. BTRFS_I(inode)->reserved_extents += nr_extents;
  3898. spin_unlock(&BTRFS_I(inode)->lock);
  3899. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  3900. if (to_reserve)
  3901. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  3902. btrfs_ino(inode), to_reserve, 1);
  3903. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  3904. return 0;
  3905. }
  3906. /**
  3907. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  3908. * @inode: the inode to release the reservation for
  3909. * @num_bytes: the number of bytes we're releasing
  3910. *
  3911. * This will release the metadata reservation for an inode. This can be called
  3912. * once we complete IO for a given set of bytes to release their metadata
  3913. * reservations.
  3914. */
  3915. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  3916. {
  3917. struct btrfs_root *root = BTRFS_I(inode)->root;
  3918. u64 to_free = 0;
  3919. unsigned dropped;
  3920. num_bytes = ALIGN(num_bytes, root->sectorsize);
  3921. spin_lock(&BTRFS_I(inode)->lock);
  3922. dropped = drop_outstanding_extent(inode);
  3923. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  3924. spin_unlock(&BTRFS_I(inode)->lock);
  3925. if (dropped > 0)
  3926. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  3927. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  3928. btrfs_ino(inode), to_free, 0);
  3929. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  3930. to_free);
  3931. }
  3932. /**
  3933. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  3934. * @inode: inode we're writing to
  3935. * @num_bytes: the number of bytes we want to allocate
  3936. *
  3937. * This will do the following things
  3938. *
  3939. * o reserve space in the data space info for num_bytes
  3940. * o reserve space in the metadata space info based on number of outstanding
  3941. * extents and how much csums will be needed
  3942. * o add to the inodes ->delalloc_bytes
  3943. * o add it to the fs_info's delalloc inodes list.
  3944. *
  3945. * This will return 0 for success and -ENOSPC if there is no space left.
  3946. */
  3947. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  3948. {
  3949. int ret;
  3950. ret = btrfs_check_data_free_space(inode, num_bytes);
  3951. if (ret)
  3952. return ret;
  3953. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  3954. if (ret) {
  3955. btrfs_free_reserved_data_space(inode, num_bytes);
  3956. return ret;
  3957. }
  3958. return 0;
  3959. }
  3960. /**
  3961. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  3962. * @inode: inode we're releasing space for
  3963. * @num_bytes: the number of bytes we want to free up
  3964. *
  3965. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  3966. * called in the case that we don't need the metadata AND data reservations
  3967. * anymore. So if there is an error or we insert an inline extent.
  3968. *
  3969. * This function will release the metadata space that was not used and will
  3970. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  3971. * list if there are no delalloc bytes left.
  3972. */
  3973. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  3974. {
  3975. btrfs_delalloc_release_metadata(inode, num_bytes);
  3976. btrfs_free_reserved_data_space(inode, num_bytes);
  3977. }
  3978. static int update_block_group(struct btrfs_trans_handle *trans,
  3979. struct btrfs_root *root,
  3980. u64 bytenr, u64 num_bytes, int alloc)
  3981. {
  3982. struct btrfs_block_group_cache *cache = NULL;
  3983. struct btrfs_fs_info *info = root->fs_info;
  3984. u64 total = num_bytes;
  3985. u64 old_val;
  3986. u64 byte_in_group;
  3987. int factor;
  3988. /* block accounting for super block */
  3989. spin_lock(&info->delalloc_lock);
  3990. old_val = btrfs_super_bytes_used(info->super_copy);
  3991. if (alloc)
  3992. old_val += num_bytes;
  3993. else
  3994. old_val -= num_bytes;
  3995. btrfs_set_super_bytes_used(info->super_copy, old_val);
  3996. spin_unlock(&info->delalloc_lock);
  3997. while (total) {
  3998. cache = btrfs_lookup_block_group(info, bytenr);
  3999. if (!cache)
  4000. return -1;
  4001. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4002. BTRFS_BLOCK_GROUP_RAID1 |
  4003. BTRFS_BLOCK_GROUP_RAID10))
  4004. factor = 2;
  4005. else
  4006. factor = 1;
  4007. /*
  4008. * If this block group has free space cache written out, we
  4009. * need to make sure to load it if we are removing space. This
  4010. * is because we need the unpinning stage to actually add the
  4011. * space back to the block group, otherwise we will leak space.
  4012. */
  4013. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4014. cache_block_group(cache, trans, NULL, 1);
  4015. byte_in_group = bytenr - cache->key.objectid;
  4016. WARN_ON(byte_in_group > cache->key.offset);
  4017. spin_lock(&cache->space_info->lock);
  4018. spin_lock(&cache->lock);
  4019. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4020. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4021. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4022. cache->dirty = 1;
  4023. old_val = btrfs_block_group_used(&cache->item);
  4024. num_bytes = min(total, cache->key.offset - byte_in_group);
  4025. if (alloc) {
  4026. old_val += num_bytes;
  4027. btrfs_set_block_group_used(&cache->item, old_val);
  4028. cache->reserved -= num_bytes;
  4029. cache->space_info->bytes_reserved -= num_bytes;
  4030. cache->space_info->bytes_used += num_bytes;
  4031. cache->space_info->disk_used += num_bytes * factor;
  4032. spin_unlock(&cache->lock);
  4033. spin_unlock(&cache->space_info->lock);
  4034. } else {
  4035. old_val -= num_bytes;
  4036. btrfs_set_block_group_used(&cache->item, old_val);
  4037. cache->pinned += num_bytes;
  4038. cache->space_info->bytes_pinned += num_bytes;
  4039. cache->space_info->bytes_used -= num_bytes;
  4040. cache->space_info->disk_used -= num_bytes * factor;
  4041. spin_unlock(&cache->lock);
  4042. spin_unlock(&cache->space_info->lock);
  4043. set_extent_dirty(info->pinned_extents,
  4044. bytenr, bytenr + num_bytes - 1,
  4045. GFP_NOFS | __GFP_NOFAIL);
  4046. }
  4047. btrfs_put_block_group(cache);
  4048. total -= num_bytes;
  4049. bytenr += num_bytes;
  4050. }
  4051. return 0;
  4052. }
  4053. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4054. {
  4055. struct btrfs_block_group_cache *cache;
  4056. u64 bytenr;
  4057. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4058. if (!cache)
  4059. return 0;
  4060. bytenr = cache->key.objectid;
  4061. btrfs_put_block_group(cache);
  4062. return bytenr;
  4063. }
  4064. static int pin_down_extent(struct btrfs_root *root,
  4065. struct btrfs_block_group_cache *cache,
  4066. u64 bytenr, u64 num_bytes, int reserved)
  4067. {
  4068. spin_lock(&cache->space_info->lock);
  4069. spin_lock(&cache->lock);
  4070. cache->pinned += num_bytes;
  4071. cache->space_info->bytes_pinned += num_bytes;
  4072. if (reserved) {
  4073. cache->reserved -= num_bytes;
  4074. cache->space_info->bytes_reserved -= num_bytes;
  4075. }
  4076. spin_unlock(&cache->lock);
  4077. spin_unlock(&cache->space_info->lock);
  4078. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4079. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4080. return 0;
  4081. }
  4082. /*
  4083. * this function must be called within transaction
  4084. */
  4085. int btrfs_pin_extent(struct btrfs_root *root,
  4086. u64 bytenr, u64 num_bytes, int reserved)
  4087. {
  4088. struct btrfs_block_group_cache *cache;
  4089. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4090. BUG_ON(!cache);
  4091. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4092. btrfs_put_block_group(cache);
  4093. return 0;
  4094. }
  4095. /*
  4096. * this function must be called within transaction
  4097. */
  4098. int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
  4099. struct btrfs_root *root,
  4100. u64 bytenr, u64 num_bytes)
  4101. {
  4102. struct btrfs_block_group_cache *cache;
  4103. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4104. BUG_ON(!cache);
  4105. /*
  4106. * pull in the free space cache (if any) so that our pin
  4107. * removes the free space from the cache. We have load_only set
  4108. * to one because the slow code to read in the free extents does check
  4109. * the pinned extents.
  4110. */
  4111. cache_block_group(cache, trans, root, 1);
  4112. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4113. /* remove us from the free space cache (if we're there at all) */
  4114. btrfs_remove_free_space(cache, bytenr, num_bytes);
  4115. btrfs_put_block_group(cache);
  4116. return 0;
  4117. }
  4118. /**
  4119. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4120. * @cache: The cache we are manipulating
  4121. * @num_bytes: The number of bytes in question
  4122. * @reserve: One of the reservation enums
  4123. *
  4124. * This is called by the allocator when it reserves space, or by somebody who is
  4125. * freeing space that was never actually used on disk. For example if you
  4126. * reserve some space for a new leaf in transaction A and before transaction A
  4127. * commits you free that leaf, you call this with reserve set to 0 in order to
  4128. * clear the reservation.
  4129. *
  4130. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4131. * ENOSPC accounting. For data we handle the reservation through clearing the
  4132. * delalloc bits in the io_tree. We have to do this since we could end up
  4133. * allocating less disk space for the amount of data we have reserved in the
  4134. * case of compression.
  4135. *
  4136. * If this is a reservation and the block group has become read only we cannot
  4137. * make the reservation and return -EAGAIN, otherwise this function always
  4138. * succeeds.
  4139. */
  4140. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4141. u64 num_bytes, int reserve)
  4142. {
  4143. struct btrfs_space_info *space_info = cache->space_info;
  4144. int ret = 0;
  4145. spin_lock(&space_info->lock);
  4146. spin_lock(&cache->lock);
  4147. if (reserve != RESERVE_FREE) {
  4148. if (cache->ro) {
  4149. ret = -EAGAIN;
  4150. } else {
  4151. cache->reserved += num_bytes;
  4152. space_info->bytes_reserved += num_bytes;
  4153. if (reserve == RESERVE_ALLOC) {
  4154. trace_btrfs_space_reservation(cache->fs_info,
  4155. "space_info",
  4156. (u64)(unsigned long)space_info,
  4157. num_bytes, 0);
  4158. space_info->bytes_may_use -= num_bytes;
  4159. }
  4160. }
  4161. } else {
  4162. if (cache->ro)
  4163. space_info->bytes_readonly += num_bytes;
  4164. cache->reserved -= num_bytes;
  4165. space_info->bytes_reserved -= num_bytes;
  4166. space_info->reservation_progress++;
  4167. }
  4168. spin_unlock(&cache->lock);
  4169. spin_unlock(&space_info->lock);
  4170. return ret;
  4171. }
  4172. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4173. struct btrfs_root *root)
  4174. {
  4175. struct btrfs_fs_info *fs_info = root->fs_info;
  4176. struct btrfs_caching_control *next;
  4177. struct btrfs_caching_control *caching_ctl;
  4178. struct btrfs_block_group_cache *cache;
  4179. down_write(&fs_info->extent_commit_sem);
  4180. list_for_each_entry_safe(caching_ctl, next,
  4181. &fs_info->caching_block_groups, list) {
  4182. cache = caching_ctl->block_group;
  4183. if (block_group_cache_done(cache)) {
  4184. cache->last_byte_to_unpin = (u64)-1;
  4185. list_del_init(&caching_ctl->list);
  4186. put_caching_control(caching_ctl);
  4187. } else {
  4188. cache->last_byte_to_unpin = caching_ctl->progress;
  4189. }
  4190. }
  4191. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4192. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4193. else
  4194. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4195. up_write(&fs_info->extent_commit_sem);
  4196. update_global_block_rsv(fs_info);
  4197. }
  4198. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4199. {
  4200. struct btrfs_fs_info *fs_info = root->fs_info;
  4201. struct btrfs_block_group_cache *cache = NULL;
  4202. u64 len;
  4203. while (start <= end) {
  4204. if (!cache ||
  4205. start >= cache->key.objectid + cache->key.offset) {
  4206. if (cache)
  4207. btrfs_put_block_group(cache);
  4208. cache = btrfs_lookup_block_group(fs_info, start);
  4209. BUG_ON(!cache);
  4210. }
  4211. len = cache->key.objectid + cache->key.offset - start;
  4212. len = min(len, end + 1 - start);
  4213. if (start < cache->last_byte_to_unpin) {
  4214. len = min(len, cache->last_byte_to_unpin - start);
  4215. btrfs_add_free_space(cache, start, len);
  4216. }
  4217. start += len;
  4218. spin_lock(&cache->space_info->lock);
  4219. spin_lock(&cache->lock);
  4220. cache->pinned -= len;
  4221. cache->space_info->bytes_pinned -= len;
  4222. if (cache->ro)
  4223. cache->space_info->bytes_readonly += len;
  4224. spin_unlock(&cache->lock);
  4225. spin_unlock(&cache->space_info->lock);
  4226. }
  4227. if (cache)
  4228. btrfs_put_block_group(cache);
  4229. return 0;
  4230. }
  4231. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4232. struct btrfs_root *root)
  4233. {
  4234. struct btrfs_fs_info *fs_info = root->fs_info;
  4235. struct extent_io_tree *unpin;
  4236. u64 start;
  4237. u64 end;
  4238. int ret;
  4239. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4240. unpin = &fs_info->freed_extents[1];
  4241. else
  4242. unpin = &fs_info->freed_extents[0];
  4243. while (1) {
  4244. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4245. EXTENT_DIRTY);
  4246. if (ret)
  4247. break;
  4248. if (btrfs_test_opt(root, DISCARD))
  4249. ret = btrfs_discard_extent(root, start,
  4250. end + 1 - start, NULL);
  4251. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4252. unpin_extent_range(root, start, end);
  4253. cond_resched();
  4254. }
  4255. return 0;
  4256. }
  4257. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4258. struct btrfs_root *root,
  4259. u64 bytenr, u64 num_bytes, u64 parent,
  4260. u64 root_objectid, u64 owner_objectid,
  4261. u64 owner_offset, int refs_to_drop,
  4262. struct btrfs_delayed_extent_op *extent_op)
  4263. {
  4264. struct btrfs_key key;
  4265. struct btrfs_path *path;
  4266. struct btrfs_fs_info *info = root->fs_info;
  4267. struct btrfs_root *extent_root = info->extent_root;
  4268. struct extent_buffer *leaf;
  4269. struct btrfs_extent_item *ei;
  4270. struct btrfs_extent_inline_ref *iref;
  4271. int ret;
  4272. int is_data;
  4273. int extent_slot = 0;
  4274. int found_extent = 0;
  4275. int num_to_del = 1;
  4276. u32 item_size;
  4277. u64 refs;
  4278. path = btrfs_alloc_path();
  4279. if (!path)
  4280. return -ENOMEM;
  4281. path->reada = 1;
  4282. path->leave_spinning = 1;
  4283. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4284. BUG_ON(!is_data && refs_to_drop != 1);
  4285. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4286. bytenr, num_bytes, parent,
  4287. root_objectid, owner_objectid,
  4288. owner_offset);
  4289. if (ret == 0) {
  4290. extent_slot = path->slots[0];
  4291. while (extent_slot >= 0) {
  4292. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4293. extent_slot);
  4294. if (key.objectid != bytenr)
  4295. break;
  4296. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4297. key.offset == num_bytes) {
  4298. found_extent = 1;
  4299. break;
  4300. }
  4301. if (path->slots[0] - extent_slot > 5)
  4302. break;
  4303. extent_slot--;
  4304. }
  4305. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4306. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4307. if (found_extent && item_size < sizeof(*ei))
  4308. found_extent = 0;
  4309. #endif
  4310. if (!found_extent) {
  4311. BUG_ON(iref);
  4312. ret = remove_extent_backref(trans, extent_root, path,
  4313. NULL, refs_to_drop,
  4314. is_data);
  4315. BUG_ON(ret);
  4316. btrfs_release_path(path);
  4317. path->leave_spinning = 1;
  4318. key.objectid = bytenr;
  4319. key.type = BTRFS_EXTENT_ITEM_KEY;
  4320. key.offset = num_bytes;
  4321. ret = btrfs_search_slot(trans, extent_root,
  4322. &key, path, -1, 1);
  4323. if (ret) {
  4324. printk(KERN_ERR "umm, got %d back from search"
  4325. ", was looking for %llu\n", ret,
  4326. (unsigned long long)bytenr);
  4327. if (ret > 0)
  4328. btrfs_print_leaf(extent_root,
  4329. path->nodes[0]);
  4330. }
  4331. BUG_ON(ret);
  4332. extent_slot = path->slots[0];
  4333. }
  4334. } else {
  4335. btrfs_print_leaf(extent_root, path->nodes[0]);
  4336. WARN_ON(1);
  4337. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  4338. "parent %llu root %llu owner %llu offset %llu\n",
  4339. (unsigned long long)bytenr,
  4340. (unsigned long long)parent,
  4341. (unsigned long long)root_objectid,
  4342. (unsigned long long)owner_objectid,
  4343. (unsigned long long)owner_offset);
  4344. }
  4345. leaf = path->nodes[0];
  4346. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4347. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4348. if (item_size < sizeof(*ei)) {
  4349. BUG_ON(found_extent || extent_slot != path->slots[0]);
  4350. ret = convert_extent_item_v0(trans, extent_root, path,
  4351. owner_objectid, 0);
  4352. BUG_ON(ret < 0);
  4353. btrfs_release_path(path);
  4354. path->leave_spinning = 1;
  4355. key.objectid = bytenr;
  4356. key.type = BTRFS_EXTENT_ITEM_KEY;
  4357. key.offset = num_bytes;
  4358. ret = btrfs_search_slot(trans, extent_root, &key, path,
  4359. -1, 1);
  4360. if (ret) {
  4361. printk(KERN_ERR "umm, got %d back from search"
  4362. ", was looking for %llu\n", ret,
  4363. (unsigned long long)bytenr);
  4364. btrfs_print_leaf(extent_root, path->nodes[0]);
  4365. }
  4366. BUG_ON(ret);
  4367. extent_slot = path->slots[0];
  4368. leaf = path->nodes[0];
  4369. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4370. }
  4371. #endif
  4372. BUG_ON(item_size < sizeof(*ei));
  4373. ei = btrfs_item_ptr(leaf, extent_slot,
  4374. struct btrfs_extent_item);
  4375. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4376. struct btrfs_tree_block_info *bi;
  4377. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  4378. bi = (struct btrfs_tree_block_info *)(ei + 1);
  4379. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  4380. }
  4381. refs = btrfs_extent_refs(leaf, ei);
  4382. BUG_ON(refs < refs_to_drop);
  4383. refs -= refs_to_drop;
  4384. if (refs > 0) {
  4385. if (extent_op)
  4386. __run_delayed_extent_op(extent_op, leaf, ei);
  4387. /*
  4388. * In the case of inline back ref, reference count will
  4389. * be updated by remove_extent_backref
  4390. */
  4391. if (iref) {
  4392. BUG_ON(!found_extent);
  4393. } else {
  4394. btrfs_set_extent_refs(leaf, ei, refs);
  4395. btrfs_mark_buffer_dirty(leaf);
  4396. }
  4397. if (found_extent) {
  4398. ret = remove_extent_backref(trans, extent_root, path,
  4399. iref, refs_to_drop,
  4400. is_data);
  4401. BUG_ON(ret);
  4402. }
  4403. } else {
  4404. if (found_extent) {
  4405. BUG_ON(is_data && refs_to_drop !=
  4406. extent_data_ref_count(root, path, iref));
  4407. if (iref) {
  4408. BUG_ON(path->slots[0] != extent_slot);
  4409. } else {
  4410. BUG_ON(path->slots[0] != extent_slot + 1);
  4411. path->slots[0] = extent_slot;
  4412. num_to_del = 2;
  4413. }
  4414. }
  4415. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  4416. num_to_del);
  4417. BUG_ON(ret);
  4418. btrfs_release_path(path);
  4419. if (is_data) {
  4420. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  4421. BUG_ON(ret);
  4422. } else {
  4423. invalidate_mapping_pages(info->btree_inode->i_mapping,
  4424. bytenr >> PAGE_CACHE_SHIFT,
  4425. (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
  4426. }
  4427. ret = update_block_group(trans, root, bytenr, num_bytes, 0);
  4428. BUG_ON(ret);
  4429. }
  4430. btrfs_free_path(path);
  4431. return ret;
  4432. }
  4433. /*
  4434. * when we free an block, it is possible (and likely) that we free the last
  4435. * delayed ref for that extent as well. This searches the delayed ref tree for
  4436. * a given extent, and if there are no other delayed refs to be processed, it
  4437. * removes it from the tree.
  4438. */
  4439. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  4440. struct btrfs_root *root, u64 bytenr)
  4441. {
  4442. struct btrfs_delayed_ref_head *head;
  4443. struct btrfs_delayed_ref_root *delayed_refs;
  4444. struct btrfs_delayed_ref_node *ref;
  4445. struct rb_node *node;
  4446. int ret = 0;
  4447. delayed_refs = &trans->transaction->delayed_refs;
  4448. spin_lock(&delayed_refs->lock);
  4449. head = btrfs_find_delayed_ref_head(trans, bytenr);
  4450. if (!head)
  4451. goto out;
  4452. node = rb_prev(&head->node.rb_node);
  4453. if (!node)
  4454. goto out;
  4455. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  4456. /* there are still entries for this ref, we can't drop it */
  4457. if (ref->bytenr == bytenr)
  4458. goto out;
  4459. if (head->extent_op) {
  4460. if (!head->must_insert_reserved)
  4461. goto out;
  4462. kfree(head->extent_op);
  4463. head->extent_op = NULL;
  4464. }
  4465. /*
  4466. * waiting for the lock here would deadlock. If someone else has it
  4467. * locked they are already in the process of dropping it anyway
  4468. */
  4469. if (!mutex_trylock(&head->mutex))
  4470. goto out;
  4471. /*
  4472. * at this point we have a head with no other entries. Go
  4473. * ahead and process it.
  4474. */
  4475. head->node.in_tree = 0;
  4476. rb_erase(&head->node.rb_node, &delayed_refs->root);
  4477. delayed_refs->num_entries--;
  4478. if (waitqueue_active(&delayed_refs->seq_wait))
  4479. wake_up(&delayed_refs->seq_wait);
  4480. /*
  4481. * we don't take a ref on the node because we're removing it from the
  4482. * tree, so we just steal the ref the tree was holding.
  4483. */
  4484. delayed_refs->num_heads--;
  4485. if (list_empty(&head->cluster))
  4486. delayed_refs->num_heads_ready--;
  4487. list_del_init(&head->cluster);
  4488. spin_unlock(&delayed_refs->lock);
  4489. BUG_ON(head->extent_op);
  4490. if (head->must_insert_reserved)
  4491. ret = 1;
  4492. mutex_unlock(&head->mutex);
  4493. btrfs_put_delayed_ref(&head->node);
  4494. return ret;
  4495. out:
  4496. spin_unlock(&delayed_refs->lock);
  4497. return 0;
  4498. }
  4499. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  4500. struct btrfs_root *root,
  4501. struct extent_buffer *buf,
  4502. u64 parent, int last_ref, int for_cow)
  4503. {
  4504. struct btrfs_block_group_cache *cache = NULL;
  4505. int ret;
  4506. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4507. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  4508. buf->start, buf->len,
  4509. parent, root->root_key.objectid,
  4510. btrfs_header_level(buf),
  4511. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  4512. BUG_ON(ret);
  4513. }
  4514. if (!last_ref)
  4515. return;
  4516. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  4517. if (btrfs_header_generation(buf) == trans->transid) {
  4518. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4519. ret = check_ref_cleanup(trans, root, buf->start);
  4520. if (!ret)
  4521. goto out;
  4522. }
  4523. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  4524. pin_down_extent(root, cache, buf->start, buf->len, 1);
  4525. goto out;
  4526. }
  4527. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  4528. btrfs_add_free_space(cache, buf->start, buf->len);
  4529. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  4530. }
  4531. out:
  4532. /*
  4533. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  4534. * anymore.
  4535. */
  4536. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  4537. btrfs_put_block_group(cache);
  4538. }
  4539. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4540. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  4541. u64 owner, u64 offset, int for_cow)
  4542. {
  4543. int ret;
  4544. struct btrfs_fs_info *fs_info = root->fs_info;
  4545. /*
  4546. * tree log blocks never actually go into the extent allocation
  4547. * tree, just update pinning info and exit early.
  4548. */
  4549. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  4550. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  4551. /* unlocks the pinned mutex */
  4552. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  4553. ret = 0;
  4554. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4555. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  4556. num_bytes,
  4557. parent, root_objectid, (int)owner,
  4558. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  4559. BUG_ON(ret);
  4560. } else {
  4561. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  4562. num_bytes,
  4563. parent, root_objectid, owner,
  4564. offset, BTRFS_DROP_DELAYED_REF,
  4565. NULL, for_cow);
  4566. BUG_ON(ret);
  4567. }
  4568. return ret;
  4569. }
  4570. static u64 stripe_align(struct btrfs_root *root, u64 val)
  4571. {
  4572. u64 mask = ((u64)root->stripesize - 1);
  4573. u64 ret = (val + mask) & ~mask;
  4574. return ret;
  4575. }
  4576. /*
  4577. * when we wait for progress in the block group caching, its because
  4578. * our allocation attempt failed at least once. So, we must sleep
  4579. * and let some progress happen before we try again.
  4580. *
  4581. * This function will sleep at least once waiting for new free space to
  4582. * show up, and then it will check the block group free space numbers
  4583. * for our min num_bytes. Another option is to have it go ahead
  4584. * and look in the rbtree for a free extent of a given size, but this
  4585. * is a good start.
  4586. */
  4587. static noinline int
  4588. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  4589. u64 num_bytes)
  4590. {
  4591. struct btrfs_caching_control *caching_ctl;
  4592. DEFINE_WAIT(wait);
  4593. caching_ctl = get_caching_control(cache);
  4594. if (!caching_ctl)
  4595. return 0;
  4596. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  4597. (cache->free_space_ctl->free_space >= num_bytes));
  4598. put_caching_control(caching_ctl);
  4599. return 0;
  4600. }
  4601. static noinline int
  4602. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  4603. {
  4604. struct btrfs_caching_control *caching_ctl;
  4605. DEFINE_WAIT(wait);
  4606. caching_ctl = get_caching_control(cache);
  4607. if (!caching_ctl)
  4608. return 0;
  4609. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  4610. put_caching_control(caching_ctl);
  4611. return 0;
  4612. }
  4613. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  4614. {
  4615. int index;
  4616. if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
  4617. index = 0;
  4618. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
  4619. index = 1;
  4620. else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
  4621. index = 2;
  4622. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
  4623. index = 3;
  4624. else
  4625. index = 4;
  4626. return index;
  4627. }
  4628. enum btrfs_loop_type {
  4629. LOOP_FIND_IDEAL = 0,
  4630. LOOP_CACHING_NOWAIT = 1,
  4631. LOOP_CACHING_WAIT = 2,
  4632. LOOP_ALLOC_CHUNK = 3,
  4633. LOOP_NO_EMPTY_SIZE = 4,
  4634. };
  4635. /*
  4636. * walks the btree of allocated extents and find a hole of a given size.
  4637. * The key ins is changed to record the hole:
  4638. * ins->objectid == block start
  4639. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  4640. * ins->offset == number of blocks
  4641. * Any available blocks before search_start are skipped.
  4642. */
  4643. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  4644. struct btrfs_root *orig_root,
  4645. u64 num_bytes, u64 empty_size,
  4646. u64 search_start, u64 search_end,
  4647. u64 hint_byte, struct btrfs_key *ins,
  4648. u64 data)
  4649. {
  4650. int ret = 0;
  4651. struct btrfs_root *root = orig_root->fs_info->extent_root;
  4652. struct btrfs_free_cluster *last_ptr = NULL;
  4653. struct btrfs_block_group_cache *block_group = NULL;
  4654. struct btrfs_block_group_cache *used_block_group;
  4655. int empty_cluster = 2 * 1024 * 1024;
  4656. int allowed_chunk_alloc = 0;
  4657. int done_chunk_alloc = 0;
  4658. struct btrfs_space_info *space_info;
  4659. int loop = 0;
  4660. int index = 0;
  4661. int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
  4662. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  4663. bool found_uncached_bg = false;
  4664. bool failed_cluster_refill = false;
  4665. bool failed_alloc = false;
  4666. bool use_cluster = true;
  4667. bool have_caching_bg = false;
  4668. u64 ideal_cache_percent = 0;
  4669. u64 ideal_cache_offset = 0;
  4670. WARN_ON(num_bytes < root->sectorsize);
  4671. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  4672. ins->objectid = 0;
  4673. ins->offset = 0;
  4674. trace_find_free_extent(orig_root, num_bytes, empty_size, data);
  4675. space_info = __find_space_info(root->fs_info, data);
  4676. if (!space_info) {
  4677. printk(KERN_ERR "No space info for %llu\n", data);
  4678. return -ENOSPC;
  4679. }
  4680. /*
  4681. * If the space info is for both data and metadata it means we have a
  4682. * small filesystem and we can't use the clustering stuff.
  4683. */
  4684. if (btrfs_mixed_space_info(space_info))
  4685. use_cluster = false;
  4686. if (orig_root->ref_cows || empty_size)
  4687. allowed_chunk_alloc = 1;
  4688. if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  4689. last_ptr = &root->fs_info->meta_alloc_cluster;
  4690. if (!btrfs_test_opt(root, SSD))
  4691. empty_cluster = 64 * 1024;
  4692. }
  4693. if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  4694. btrfs_test_opt(root, SSD)) {
  4695. last_ptr = &root->fs_info->data_alloc_cluster;
  4696. }
  4697. if (last_ptr) {
  4698. spin_lock(&last_ptr->lock);
  4699. if (last_ptr->block_group)
  4700. hint_byte = last_ptr->window_start;
  4701. spin_unlock(&last_ptr->lock);
  4702. }
  4703. search_start = max(search_start, first_logical_byte(root, 0));
  4704. search_start = max(search_start, hint_byte);
  4705. if (!last_ptr)
  4706. empty_cluster = 0;
  4707. if (search_start == hint_byte) {
  4708. ideal_cache:
  4709. block_group = btrfs_lookup_block_group(root->fs_info,
  4710. search_start);
  4711. used_block_group = block_group;
  4712. /*
  4713. * we don't want to use the block group if it doesn't match our
  4714. * allocation bits, or if its not cached.
  4715. *
  4716. * However if we are re-searching with an ideal block group
  4717. * picked out then we don't care that the block group is cached.
  4718. */
  4719. if (block_group && block_group_bits(block_group, data) &&
  4720. (block_group->cached != BTRFS_CACHE_NO ||
  4721. search_start == ideal_cache_offset)) {
  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. u64 free_percent;
  4772. found_uncached_bg = true;
  4773. ret = cache_block_group(block_group, trans,
  4774. orig_root, 1);
  4775. if (block_group->cached == BTRFS_CACHE_FINISHED)
  4776. goto alloc;
  4777. free_percent = btrfs_block_group_used(&block_group->item);
  4778. free_percent *= 100;
  4779. free_percent = div64_u64(free_percent,
  4780. block_group->key.offset);
  4781. free_percent = 100 - free_percent;
  4782. if (free_percent > ideal_cache_percent &&
  4783. likely(!block_group->ro)) {
  4784. ideal_cache_offset = block_group->key.objectid;
  4785. ideal_cache_percent = free_percent;
  4786. }
  4787. /*
  4788. * The caching workers are limited to 2 threads, so we
  4789. * can queue as much work as we care to.
  4790. */
  4791. if (loop > LOOP_FIND_IDEAL) {
  4792. ret = cache_block_group(block_group, trans,
  4793. orig_root, 0);
  4794. BUG_ON(ret);
  4795. }
  4796. /*
  4797. * If loop is set for cached only, try the next block
  4798. * group.
  4799. */
  4800. if (loop == LOOP_FIND_IDEAL)
  4801. goto loop;
  4802. }
  4803. alloc:
  4804. if (unlikely(block_group->ro))
  4805. goto loop;
  4806. /*
  4807. * Ok we want to try and use the cluster allocator, so
  4808. * lets look there
  4809. */
  4810. if (last_ptr) {
  4811. /*
  4812. * the refill lock keeps out other
  4813. * people trying to start a new cluster
  4814. */
  4815. spin_lock(&last_ptr->refill_lock);
  4816. used_block_group = last_ptr->block_group;
  4817. if (used_block_group != block_group &&
  4818. (!used_block_group ||
  4819. used_block_group->ro ||
  4820. !block_group_bits(used_block_group, data))) {
  4821. used_block_group = block_group;
  4822. goto refill_cluster;
  4823. }
  4824. if (used_block_group != block_group)
  4825. btrfs_get_block_group(used_block_group);
  4826. offset = btrfs_alloc_from_cluster(used_block_group,
  4827. last_ptr, num_bytes, used_block_group->key.objectid);
  4828. if (offset) {
  4829. /* we have a block, we're done */
  4830. spin_unlock(&last_ptr->refill_lock);
  4831. trace_btrfs_reserve_extent_cluster(root,
  4832. block_group, search_start, num_bytes);
  4833. goto checks;
  4834. }
  4835. WARN_ON(last_ptr->block_group != used_block_group);
  4836. if (used_block_group != block_group) {
  4837. btrfs_put_block_group(used_block_group);
  4838. used_block_group = block_group;
  4839. }
  4840. refill_cluster:
  4841. BUG_ON(used_block_group != block_group);
  4842. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  4843. * set up a new clusters, so lets just skip it
  4844. * and let the allocator find whatever block
  4845. * it can find. If we reach this point, we
  4846. * will have tried the cluster allocator
  4847. * plenty of times and not have found
  4848. * anything, so we are likely way too
  4849. * fragmented for the clustering stuff to find
  4850. * anything.
  4851. *
  4852. * However, if the cluster is taken from the
  4853. * current block group, release the cluster
  4854. * first, so that we stand a better chance of
  4855. * succeeding in the unclustered
  4856. * allocation. */
  4857. if (loop >= LOOP_NO_EMPTY_SIZE &&
  4858. last_ptr->block_group != block_group) {
  4859. spin_unlock(&last_ptr->refill_lock);
  4860. goto unclustered_alloc;
  4861. }
  4862. /*
  4863. * this cluster didn't work out, free it and
  4864. * start over
  4865. */
  4866. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4867. if (loop >= LOOP_NO_EMPTY_SIZE) {
  4868. spin_unlock(&last_ptr->refill_lock);
  4869. goto unclustered_alloc;
  4870. }
  4871. /* allocate a cluster in this block group */
  4872. ret = btrfs_find_space_cluster(trans, root,
  4873. block_group, last_ptr,
  4874. search_start, num_bytes,
  4875. empty_cluster + empty_size);
  4876. if (ret == 0) {
  4877. /*
  4878. * now pull our allocation out of this
  4879. * cluster
  4880. */
  4881. offset = btrfs_alloc_from_cluster(block_group,
  4882. last_ptr, num_bytes,
  4883. search_start);
  4884. if (offset) {
  4885. /* we found one, proceed */
  4886. spin_unlock(&last_ptr->refill_lock);
  4887. trace_btrfs_reserve_extent_cluster(root,
  4888. block_group, search_start,
  4889. num_bytes);
  4890. goto checks;
  4891. }
  4892. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  4893. && !failed_cluster_refill) {
  4894. spin_unlock(&last_ptr->refill_lock);
  4895. failed_cluster_refill = true;
  4896. wait_block_group_cache_progress(block_group,
  4897. num_bytes + empty_cluster + empty_size);
  4898. goto have_block_group;
  4899. }
  4900. /*
  4901. * at this point we either didn't find a cluster
  4902. * or we weren't able to allocate a block from our
  4903. * cluster. Free the cluster we've been trying
  4904. * to use, and go to the next block group
  4905. */
  4906. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4907. spin_unlock(&last_ptr->refill_lock);
  4908. goto loop;
  4909. }
  4910. unclustered_alloc:
  4911. spin_lock(&block_group->free_space_ctl->tree_lock);
  4912. if (cached &&
  4913. block_group->free_space_ctl->free_space <
  4914. num_bytes + empty_cluster + empty_size) {
  4915. spin_unlock(&block_group->free_space_ctl->tree_lock);
  4916. goto loop;
  4917. }
  4918. spin_unlock(&block_group->free_space_ctl->tree_lock);
  4919. offset = btrfs_find_space_for_alloc(block_group, search_start,
  4920. num_bytes, empty_size);
  4921. /*
  4922. * If we didn't find a chunk, and we haven't failed on this
  4923. * block group before, and this block group is in the middle of
  4924. * caching and we are ok with waiting, then go ahead and wait
  4925. * for progress to be made, and set failed_alloc to true.
  4926. *
  4927. * If failed_alloc is true then we've already waited on this
  4928. * block group once and should move on to the next block group.
  4929. */
  4930. if (!offset && !failed_alloc && !cached &&
  4931. loop > LOOP_CACHING_NOWAIT) {
  4932. wait_block_group_cache_progress(block_group,
  4933. num_bytes + empty_size);
  4934. failed_alloc = true;
  4935. goto have_block_group;
  4936. } else if (!offset) {
  4937. if (!cached)
  4938. have_caching_bg = true;
  4939. goto loop;
  4940. }
  4941. checks:
  4942. search_start = stripe_align(root, offset);
  4943. /* move on to the next group */
  4944. if (search_start + num_bytes >= search_end) {
  4945. btrfs_add_free_space(used_block_group, offset, num_bytes);
  4946. goto loop;
  4947. }
  4948. /* move on to the next group */
  4949. if (search_start + num_bytes >
  4950. used_block_group->key.objectid + used_block_group->key.offset) {
  4951. btrfs_add_free_space(used_block_group, offset, num_bytes);
  4952. goto loop;
  4953. }
  4954. if (offset < search_start)
  4955. btrfs_add_free_space(used_block_group, offset,
  4956. search_start - offset);
  4957. BUG_ON(offset > search_start);
  4958. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  4959. alloc_type);
  4960. if (ret == -EAGAIN) {
  4961. btrfs_add_free_space(used_block_group, offset, num_bytes);
  4962. goto loop;
  4963. }
  4964. /* we are all good, lets return */
  4965. ins->objectid = search_start;
  4966. ins->offset = num_bytes;
  4967. trace_btrfs_reserve_extent(orig_root, block_group,
  4968. search_start, num_bytes);
  4969. if (offset < search_start)
  4970. btrfs_add_free_space(used_block_group, offset,
  4971. search_start - offset);
  4972. BUG_ON(offset > search_start);
  4973. if (used_block_group != block_group)
  4974. btrfs_put_block_group(used_block_group);
  4975. btrfs_put_block_group(block_group);
  4976. break;
  4977. loop:
  4978. failed_cluster_refill = false;
  4979. failed_alloc = false;
  4980. BUG_ON(index != get_block_group_index(block_group));
  4981. if (used_block_group != block_group)
  4982. btrfs_put_block_group(used_block_group);
  4983. btrfs_put_block_group(block_group);
  4984. }
  4985. up_read(&space_info->groups_sem);
  4986. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  4987. goto search;
  4988. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  4989. goto search;
  4990. /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
  4991. * for them to make caching progress. Also
  4992. * determine the best possible bg to cache
  4993. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  4994. * caching kthreads as we move along
  4995. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  4996. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  4997. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  4998. * again
  4999. */
  5000. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5001. index = 0;
  5002. if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
  5003. found_uncached_bg = false;
  5004. loop++;
  5005. if (!ideal_cache_percent)
  5006. goto search;
  5007. /*
  5008. * 1 of the following 2 things have happened so far
  5009. *
  5010. * 1) We found an ideal block group for caching that
  5011. * is mostly full and will cache quickly, so we might
  5012. * as well wait for it.
  5013. *
  5014. * 2) We searched for cached only and we didn't find
  5015. * anything, and we didn't start any caching kthreads
  5016. * either, so chances are we will loop through and
  5017. * start a couple caching kthreads, and then come back
  5018. * around and just wait for them. This will be slower
  5019. * because we will have 2 caching kthreads reading at
  5020. * the same time when we could have just started one
  5021. * and waited for it to get far enough to give us an
  5022. * allocation, so go ahead and go to the wait caching
  5023. * loop.
  5024. */
  5025. loop = LOOP_CACHING_WAIT;
  5026. search_start = ideal_cache_offset;
  5027. ideal_cache_percent = 0;
  5028. goto ideal_cache;
  5029. } else if (loop == LOOP_FIND_IDEAL) {
  5030. /*
  5031. * Didn't find a uncached bg, wait on anything we find
  5032. * next.
  5033. */
  5034. loop = LOOP_CACHING_WAIT;
  5035. goto search;
  5036. }
  5037. loop++;
  5038. if (loop == LOOP_ALLOC_CHUNK) {
  5039. if (allowed_chunk_alloc) {
  5040. ret = do_chunk_alloc(trans, root, num_bytes +
  5041. 2 * 1024 * 1024, data,
  5042. CHUNK_ALLOC_LIMITED);
  5043. allowed_chunk_alloc = 0;
  5044. if (ret == 1)
  5045. done_chunk_alloc = 1;
  5046. } else if (!done_chunk_alloc &&
  5047. space_info->force_alloc ==
  5048. CHUNK_ALLOC_NO_FORCE) {
  5049. space_info->force_alloc = CHUNK_ALLOC_LIMITED;
  5050. }
  5051. /*
  5052. * We didn't allocate a chunk, go ahead and drop the
  5053. * empty size and loop again.
  5054. */
  5055. if (!done_chunk_alloc)
  5056. loop = LOOP_NO_EMPTY_SIZE;
  5057. }
  5058. if (loop == LOOP_NO_EMPTY_SIZE) {
  5059. empty_size = 0;
  5060. empty_cluster = 0;
  5061. }
  5062. goto search;
  5063. } else if (!ins->objectid) {
  5064. ret = -ENOSPC;
  5065. } else if (ins->objectid) {
  5066. ret = 0;
  5067. }
  5068. return ret;
  5069. }
  5070. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5071. int dump_block_groups)
  5072. {
  5073. struct btrfs_block_group_cache *cache;
  5074. int index = 0;
  5075. spin_lock(&info->lock);
  5076. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5077. (unsigned long long)info->flags,
  5078. (unsigned long long)(info->total_bytes - info->bytes_used -
  5079. info->bytes_pinned - info->bytes_reserved -
  5080. info->bytes_readonly),
  5081. (info->full) ? "" : "not ");
  5082. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5083. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5084. (unsigned long long)info->total_bytes,
  5085. (unsigned long long)info->bytes_used,
  5086. (unsigned long long)info->bytes_pinned,
  5087. (unsigned long long)info->bytes_reserved,
  5088. (unsigned long long)info->bytes_may_use,
  5089. (unsigned long long)info->bytes_readonly);
  5090. spin_unlock(&info->lock);
  5091. if (!dump_block_groups)
  5092. return;
  5093. down_read(&info->groups_sem);
  5094. again:
  5095. list_for_each_entry(cache, &info->block_groups[index], list) {
  5096. spin_lock(&cache->lock);
  5097. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  5098. "%llu pinned %llu reserved\n",
  5099. (unsigned long long)cache->key.objectid,
  5100. (unsigned long long)cache->key.offset,
  5101. (unsigned long long)btrfs_block_group_used(&cache->item),
  5102. (unsigned long long)cache->pinned,
  5103. (unsigned long long)cache->reserved);
  5104. btrfs_dump_free_space(cache, bytes);
  5105. spin_unlock(&cache->lock);
  5106. }
  5107. if (++index < BTRFS_NR_RAID_TYPES)
  5108. goto again;
  5109. up_read(&info->groups_sem);
  5110. }
  5111. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  5112. struct btrfs_root *root,
  5113. u64 num_bytes, u64 min_alloc_size,
  5114. u64 empty_size, u64 hint_byte,
  5115. u64 search_end, struct btrfs_key *ins,
  5116. u64 data)
  5117. {
  5118. bool final_tried = false;
  5119. int ret;
  5120. u64 search_start = 0;
  5121. data = btrfs_get_alloc_profile(root, data);
  5122. again:
  5123. /*
  5124. * the only place that sets empty_size is btrfs_realloc_node, which
  5125. * is not called recursively on allocations
  5126. */
  5127. if (empty_size || root->ref_cows)
  5128. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  5129. num_bytes + 2 * 1024 * 1024, data,
  5130. CHUNK_ALLOC_NO_FORCE);
  5131. WARN_ON(num_bytes < root->sectorsize);
  5132. ret = find_free_extent(trans, root, num_bytes, empty_size,
  5133. search_start, search_end, hint_byte,
  5134. ins, data);
  5135. if (ret == -ENOSPC) {
  5136. if (!final_tried) {
  5137. num_bytes = num_bytes >> 1;
  5138. num_bytes = num_bytes & ~(root->sectorsize - 1);
  5139. num_bytes = max(num_bytes, min_alloc_size);
  5140. do_chunk_alloc(trans, root->fs_info->extent_root,
  5141. num_bytes, data, CHUNK_ALLOC_FORCE);
  5142. if (num_bytes == min_alloc_size)
  5143. final_tried = true;
  5144. goto again;
  5145. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5146. struct btrfs_space_info *sinfo;
  5147. sinfo = __find_space_info(root->fs_info, data);
  5148. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  5149. "wanted %llu\n", (unsigned long long)data,
  5150. (unsigned long long)num_bytes);
  5151. if (sinfo)
  5152. dump_space_info(sinfo, num_bytes, 1);
  5153. }
  5154. }
  5155. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5156. return ret;
  5157. }
  5158. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5159. u64 start, u64 len, int pin)
  5160. {
  5161. struct btrfs_block_group_cache *cache;
  5162. int ret = 0;
  5163. cache = btrfs_lookup_block_group(root->fs_info, start);
  5164. if (!cache) {
  5165. printk(KERN_ERR "Unable to find block group for %llu\n",
  5166. (unsigned long long)start);
  5167. return -ENOSPC;
  5168. }
  5169. if (btrfs_test_opt(root, DISCARD))
  5170. ret = btrfs_discard_extent(root, start, len, NULL);
  5171. if (pin)
  5172. pin_down_extent(root, cache, start, len, 1);
  5173. else {
  5174. btrfs_add_free_space(cache, start, len);
  5175. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5176. }
  5177. btrfs_put_block_group(cache);
  5178. trace_btrfs_reserved_extent_free(root, start, len);
  5179. return ret;
  5180. }
  5181. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5182. u64 start, u64 len)
  5183. {
  5184. return __btrfs_free_reserved_extent(root, start, len, 0);
  5185. }
  5186. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5187. u64 start, u64 len)
  5188. {
  5189. return __btrfs_free_reserved_extent(root, start, len, 1);
  5190. }
  5191. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5192. struct btrfs_root *root,
  5193. u64 parent, u64 root_objectid,
  5194. u64 flags, u64 owner, u64 offset,
  5195. struct btrfs_key *ins, int ref_mod)
  5196. {
  5197. int ret;
  5198. struct btrfs_fs_info *fs_info = root->fs_info;
  5199. struct btrfs_extent_item *extent_item;
  5200. struct btrfs_extent_inline_ref *iref;
  5201. struct btrfs_path *path;
  5202. struct extent_buffer *leaf;
  5203. int type;
  5204. u32 size;
  5205. if (parent > 0)
  5206. type = BTRFS_SHARED_DATA_REF_KEY;
  5207. else
  5208. type = BTRFS_EXTENT_DATA_REF_KEY;
  5209. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5210. path = btrfs_alloc_path();
  5211. if (!path)
  5212. return -ENOMEM;
  5213. path->leave_spinning = 1;
  5214. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5215. ins, size);
  5216. BUG_ON(ret);
  5217. leaf = path->nodes[0];
  5218. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5219. struct btrfs_extent_item);
  5220. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5221. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5222. btrfs_set_extent_flags(leaf, extent_item,
  5223. flags | BTRFS_EXTENT_FLAG_DATA);
  5224. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5225. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5226. if (parent > 0) {
  5227. struct btrfs_shared_data_ref *ref;
  5228. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5229. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5230. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5231. } else {
  5232. struct btrfs_extent_data_ref *ref;
  5233. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5234. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5235. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5236. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5237. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5238. }
  5239. btrfs_mark_buffer_dirty(path->nodes[0]);
  5240. btrfs_free_path(path);
  5241. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  5242. if (ret) {
  5243. printk(KERN_ERR "btrfs update block group failed for %llu "
  5244. "%llu\n", (unsigned long long)ins->objectid,
  5245. (unsigned long long)ins->offset);
  5246. BUG();
  5247. }
  5248. return ret;
  5249. }
  5250. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5251. struct btrfs_root *root,
  5252. u64 parent, u64 root_objectid,
  5253. u64 flags, struct btrfs_disk_key *key,
  5254. int level, struct btrfs_key *ins)
  5255. {
  5256. int ret;
  5257. struct btrfs_fs_info *fs_info = root->fs_info;
  5258. struct btrfs_extent_item *extent_item;
  5259. struct btrfs_tree_block_info *block_info;
  5260. struct btrfs_extent_inline_ref *iref;
  5261. struct btrfs_path *path;
  5262. struct extent_buffer *leaf;
  5263. u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
  5264. path = btrfs_alloc_path();
  5265. if (!path)
  5266. return -ENOMEM;
  5267. path->leave_spinning = 1;
  5268. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5269. ins, size);
  5270. BUG_ON(ret);
  5271. leaf = path->nodes[0];
  5272. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5273. struct btrfs_extent_item);
  5274. btrfs_set_extent_refs(leaf, extent_item, 1);
  5275. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5276. btrfs_set_extent_flags(leaf, extent_item,
  5277. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5278. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5279. btrfs_set_tree_block_key(leaf, block_info, key);
  5280. btrfs_set_tree_block_level(leaf, block_info, level);
  5281. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5282. if (parent > 0) {
  5283. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5284. btrfs_set_extent_inline_ref_type(leaf, iref,
  5285. BTRFS_SHARED_BLOCK_REF_KEY);
  5286. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5287. } else {
  5288. btrfs_set_extent_inline_ref_type(leaf, iref,
  5289. BTRFS_TREE_BLOCK_REF_KEY);
  5290. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5291. }
  5292. btrfs_mark_buffer_dirty(leaf);
  5293. btrfs_free_path(path);
  5294. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  5295. if (ret) {
  5296. printk(KERN_ERR "btrfs update block group failed for %llu "
  5297. "%llu\n", (unsigned long long)ins->objectid,
  5298. (unsigned long long)ins->offset);
  5299. BUG();
  5300. }
  5301. return ret;
  5302. }
  5303. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5304. struct btrfs_root *root,
  5305. u64 root_objectid, u64 owner,
  5306. u64 offset, struct btrfs_key *ins)
  5307. {
  5308. int ret;
  5309. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5310. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5311. ins->offset, 0,
  5312. root_objectid, owner, offset,
  5313. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5314. return ret;
  5315. }
  5316. /*
  5317. * this is used by the tree logging recovery code. It records that
  5318. * an extent has been allocated and makes sure to clear the free
  5319. * space cache bits as well
  5320. */
  5321. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  5322. struct btrfs_root *root,
  5323. u64 root_objectid, u64 owner, u64 offset,
  5324. struct btrfs_key *ins)
  5325. {
  5326. int ret;
  5327. struct btrfs_block_group_cache *block_group;
  5328. struct btrfs_caching_control *caching_ctl;
  5329. u64 start = ins->objectid;
  5330. u64 num_bytes = ins->offset;
  5331. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  5332. cache_block_group(block_group, trans, NULL, 0);
  5333. caching_ctl = get_caching_control(block_group);
  5334. if (!caching_ctl) {
  5335. BUG_ON(!block_group_cache_done(block_group));
  5336. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  5337. BUG_ON(ret);
  5338. } else {
  5339. mutex_lock(&caching_ctl->mutex);
  5340. if (start >= caching_ctl->progress) {
  5341. ret = add_excluded_extent(root, start, num_bytes);
  5342. BUG_ON(ret);
  5343. } else if (start + num_bytes <= caching_ctl->progress) {
  5344. ret = btrfs_remove_free_space(block_group,
  5345. start, num_bytes);
  5346. BUG_ON(ret);
  5347. } else {
  5348. num_bytes = caching_ctl->progress - start;
  5349. ret = btrfs_remove_free_space(block_group,
  5350. start, num_bytes);
  5351. BUG_ON(ret);
  5352. start = caching_ctl->progress;
  5353. num_bytes = ins->objectid + ins->offset -
  5354. caching_ctl->progress;
  5355. ret = add_excluded_extent(root, start, num_bytes);
  5356. BUG_ON(ret);
  5357. }
  5358. mutex_unlock(&caching_ctl->mutex);
  5359. put_caching_control(caching_ctl);
  5360. }
  5361. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  5362. RESERVE_ALLOC_NO_ACCOUNT);
  5363. BUG_ON(ret);
  5364. btrfs_put_block_group(block_group);
  5365. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  5366. 0, owner, offset, ins, 1);
  5367. return ret;
  5368. }
  5369. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  5370. struct btrfs_root *root,
  5371. u64 bytenr, u32 blocksize,
  5372. int level)
  5373. {
  5374. struct extent_buffer *buf;
  5375. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5376. if (!buf)
  5377. return ERR_PTR(-ENOMEM);
  5378. btrfs_set_header_generation(buf, trans->transid);
  5379. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  5380. btrfs_tree_lock(buf);
  5381. clean_tree_block(trans, root, buf);
  5382. btrfs_set_lock_blocking(buf);
  5383. btrfs_set_buffer_uptodate(buf);
  5384. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  5385. /*
  5386. * we allow two log transactions at a time, use different
  5387. * EXENT bit to differentiate dirty pages.
  5388. */
  5389. if (root->log_transid % 2 == 0)
  5390. set_extent_dirty(&root->dirty_log_pages, buf->start,
  5391. buf->start + buf->len - 1, GFP_NOFS);
  5392. else
  5393. set_extent_new(&root->dirty_log_pages, buf->start,
  5394. buf->start + buf->len - 1, GFP_NOFS);
  5395. } else {
  5396. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  5397. buf->start + buf->len - 1, GFP_NOFS);
  5398. }
  5399. trans->blocks_used++;
  5400. /* this returns a buffer locked for blocking */
  5401. return buf;
  5402. }
  5403. static struct btrfs_block_rsv *
  5404. use_block_rsv(struct btrfs_trans_handle *trans,
  5405. struct btrfs_root *root, u32 blocksize)
  5406. {
  5407. struct btrfs_block_rsv *block_rsv;
  5408. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  5409. int ret;
  5410. block_rsv = get_block_rsv(trans, root);
  5411. if (block_rsv->size == 0) {
  5412. ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0);
  5413. /*
  5414. * If we couldn't reserve metadata bytes try and use some from
  5415. * the global reserve.
  5416. */
  5417. if (ret && block_rsv != global_rsv) {
  5418. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5419. if (!ret)
  5420. return global_rsv;
  5421. return ERR_PTR(ret);
  5422. } else if (ret) {
  5423. return ERR_PTR(ret);
  5424. }
  5425. return block_rsv;
  5426. }
  5427. ret = block_rsv_use_bytes(block_rsv, blocksize);
  5428. if (!ret)
  5429. return block_rsv;
  5430. if (ret) {
  5431. static DEFINE_RATELIMIT_STATE(_rs,
  5432. DEFAULT_RATELIMIT_INTERVAL,
  5433. /*DEFAULT_RATELIMIT_BURST*/ 2);
  5434. if (__ratelimit(&_rs)) {
  5435. printk(KERN_DEBUG "btrfs: block rsv returned %d\n", ret);
  5436. WARN_ON(1);
  5437. }
  5438. ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0);
  5439. if (!ret) {
  5440. return block_rsv;
  5441. } else if (ret && block_rsv != global_rsv) {
  5442. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5443. if (!ret)
  5444. return global_rsv;
  5445. }
  5446. }
  5447. return ERR_PTR(-ENOSPC);
  5448. }
  5449. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  5450. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  5451. {
  5452. block_rsv_add_bytes(block_rsv, blocksize, 0);
  5453. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  5454. }
  5455. /*
  5456. * finds a free extent and does all the dirty work required for allocation
  5457. * returns the key for the extent through ins, and a tree buffer for
  5458. * the first block of the extent through buf.
  5459. *
  5460. * returns the tree buffer or NULL.
  5461. */
  5462. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  5463. struct btrfs_root *root, u32 blocksize,
  5464. u64 parent, u64 root_objectid,
  5465. struct btrfs_disk_key *key, int level,
  5466. u64 hint, u64 empty_size, int for_cow)
  5467. {
  5468. struct btrfs_key ins;
  5469. struct btrfs_block_rsv *block_rsv;
  5470. struct extent_buffer *buf;
  5471. u64 flags = 0;
  5472. int ret;
  5473. block_rsv = use_block_rsv(trans, root, blocksize);
  5474. if (IS_ERR(block_rsv))
  5475. return ERR_CAST(block_rsv);
  5476. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  5477. empty_size, hint, (u64)-1, &ins, 0);
  5478. if (ret) {
  5479. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  5480. return ERR_PTR(ret);
  5481. }
  5482. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  5483. blocksize, level);
  5484. BUG_ON(IS_ERR(buf));
  5485. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  5486. if (parent == 0)
  5487. parent = ins.objectid;
  5488. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5489. } else
  5490. BUG_ON(parent > 0);
  5491. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  5492. struct btrfs_delayed_extent_op *extent_op;
  5493. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  5494. BUG_ON(!extent_op);
  5495. if (key)
  5496. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  5497. else
  5498. memset(&extent_op->key, 0, sizeof(extent_op->key));
  5499. extent_op->flags_to_set = flags;
  5500. extent_op->update_key = 1;
  5501. extent_op->update_flags = 1;
  5502. extent_op->is_data = 0;
  5503. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5504. ins.objectid,
  5505. ins.offset, parent, root_objectid,
  5506. level, BTRFS_ADD_DELAYED_EXTENT,
  5507. extent_op, for_cow);
  5508. BUG_ON(ret);
  5509. }
  5510. return buf;
  5511. }
  5512. struct walk_control {
  5513. u64 refs[BTRFS_MAX_LEVEL];
  5514. u64 flags[BTRFS_MAX_LEVEL];
  5515. struct btrfs_key update_progress;
  5516. int stage;
  5517. int level;
  5518. int shared_level;
  5519. int update_ref;
  5520. int keep_locks;
  5521. int reada_slot;
  5522. int reada_count;
  5523. int for_reloc;
  5524. };
  5525. #define DROP_REFERENCE 1
  5526. #define UPDATE_BACKREF 2
  5527. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  5528. struct btrfs_root *root,
  5529. struct walk_control *wc,
  5530. struct btrfs_path *path)
  5531. {
  5532. u64 bytenr;
  5533. u64 generation;
  5534. u64 refs;
  5535. u64 flags;
  5536. u32 nritems;
  5537. u32 blocksize;
  5538. struct btrfs_key key;
  5539. struct extent_buffer *eb;
  5540. int ret;
  5541. int slot;
  5542. int nread = 0;
  5543. if (path->slots[wc->level] < wc->reada_slot) {
  5544. wc->reada_count = wc->reada_count * 2 / 3;
  5545. wc->reada_count = max(wc->reada_count, 2);
  5546. } else {
  5547. wc->reada_count = wc->reada_count * 3 / 2;
  5548. wc->reada_count = min_t(int, wc->reada_count,
  5549. BTRFS_NODEPTRS_PER_BLOCK(root));
  5550. }
  5551. eb = path->nodes[wc->level];
  5552. nritems = btrfs_header_nritems(eb);
  5553. blocksize = btrfs_level_size(root, wc->level - 1);
  5554. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  5555. if (nread >= wc->reada_count)
  5556. break;
  5557. cond_resched();
  5558. bytenr = btrfs_node_blockptr(eb, slot);
  5559. generation = btrfs_node_ptr_generation(eb, slot);
  5560. if (slot == path->slots[wc->level])
  5561. goto reada;
  5562. if (wc->stage == UPDATE_BACKREF &&
  5563. generation <= root->root_key.offset)
  5564. continue;
  5565. /* We don't lock the tree block, it's OK to be racy here */
  5566. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5567. &refs, &flags);
  5568. BUG_ON(ret);
  5569. BUG_ON(refs == 0);
  5570. if (wc->stage == DROP_REFERENCE) {
  5571. if (refs == 1)
  5572. goto reada;
  5573. if (wc->level == 1 &&
  5574. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5575. continue;
  5576. if (!wc->update_ref ||
  5577. generation <= root->root_key.offset)
  5578. continue;
  5579. btrfs_node_key_to_cpu(eb, &key, slot);
  5580. ret = btrfs_comp_cpu_keys(&key,
  5581. &wc->update_progress);
  5582. if (ret < 0)
  5583. continue;
  5584. } else {
  5585. if (wc->level == 1 &&
  5586. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5587. continue;
  5588. }
  5589. reada:
  5590. ret = readahead_tree_block(root, bytenr, blocksize,
  5591. generation);
  5592. if (ret)
  5593. break;
  5594. nread++;
  5595. }
  5596. wc->reada_slot = slot;
  5597. }
  5598. /*
  5599. * hepler to process tree block while walking down the tree.
  5600. *
  5601. * when wc->stage == UPDATE_BACKREF, this function updates
  5602. * back refs for pointers in the block.
  5603. *
  5604. * NOTE: return value 1 means we should stop walking down.
  5605. */
  5606. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  5607. struct btrfs_root *root,
  5608. struct btrfs_path *path,
  5609. struct walk_control *wc, int lookup_info)
  5610. {
  5611. int level = wc->level;
  5612. struct extent_buffer *eb = path->nodes[level];
  5613. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5614. int ret;
  5615. if (wc->stage == UPDATE_BACKREF &&
  5616. btrfs_header_owner(eb) != root->root_key.objectid)
  5617. return 1;
  5618. /*
  5619. * when reference count of tree block is 1, it won't increase
  5620. * again. once full backref flag is set, we never clear it.
  5621. */
  5622. if (lookup_info &&
  5623. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  5624. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  5625. BUG_ON(!path->locks[level]);
  5626. ret = btrfs_lookup_extent_info(trans, root,
  5627. eb->start, eb->len,
  5628. &wc->refs[level],
  5629. &wc->flags[level]);
  5630. BUG_ON(ret);
  5631. BUG_ON(wc->refs[level] == 0);
  5632. }
  5633. if (wc->stage == DROP_REFERENCE) {
  5634. if (wc->refs[level] > 1)
  5635. return 1;
  5636. if (path->locks[level] && !wc->keep_locks) {
  5637. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5638. path->locks[level] = 0;
  5639. }
  5640. return 0;
  5641. }
  5642. /* wc->stage == UPDATE_BACKREF */
  5643. if (!(wc->flags[level] & flag)) {
  5644. BUG_ON(!path->locks[level]);
  5645. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  5646. BUG_ON(ret);
  5647. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  5648. BUG_ON(ret);
  5649. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  5650. eb->len, flag, 0);
  5651. BUG_ON(ret);
  5652. wc->flags[level] |= flag;
  5653. }
  5654. /*
  5655. * the block is shared by multiple trees, so it's not good to
  5656. * keep the tree lock
  5657. */
  5658. if (path->locks[level] && level > 0) {
  5659. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5660. path->locks[level] = 0;
  5661. }
  5662. return 0;
  5663. }
  5664. /*
  5665. * hepler to process tree block pointer.
  5666. *
  5667. * when wc->stage == DROP_REFERENCE, this function checks
  5668. * reference count of the block pointed to. if the block
  5669. * is shared and we need update back refs for the subtree
  5670. * rooted at the block, this function changes wc->stage to
  5671. * UPDATE_BACKREF. if the block is shared and there is no
  5672. * need to update back, this function drops the reference
  5673. * to the block.
  5674. *
  5675. * NOTE: return value 1 means we should stop walking down.
  5676. */
  5677. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  5678. struct btrfs_root *root,
  5679. struct btrfs_path *path,
  5680. struct walk_control *wc, int *lookup_info)
  5681. {
  5682. u64 bytenr;
  5683. u64 generation;
  5684. u64 parent;
  5685. u32 blocksize;
  5686. struct btrfs_key key;
  5687. struct extent_buffer *next;
  5688. int level = wc->level;
  5689. int reada = 0;
  5690. int ret = 0;
  5691. generation = btrfs_node_ptr_generation(path->nodes[level],
  5692. path->slots[level]);
  5693. /*
  5694. * if the lower level block was created before the snapshot
  5695. * was created, we know there is no need to update back refs
  5696. * for the subtree
  5697. */
  5698. if (wc->stage == UPDATE_BACKREF &&
  5699. generation <= root->root_key.offset) {
  5700. *lookup_info = 1;
  5701. return 1;
  5702. }
  5703. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  5704. blocksize = btrfs_level_size(root, level - 1);
  5705. next = btrfs_find_tree_block(root, bytenr, blocksize);
  5706. if (!next) {
  5707. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5708. if (!next)
  5709. return -ENOMEM;
  5710. reada = 1;
  5711. }
  5712. btrfs_tree_lock(next);
  5713. btrfs_set_lock_blocking(next);
  5714. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5715. &wc->refs[level - 1],
  5716. &wc->flags[level - 1]);
  5717. BUG_ON(ret);
  5718. BUG_ON(wc->refs[level - 1] == 0);
  5719. *lookup_info = 0;
  5720. if (wc->stage == DROP_REFERENCE) {
  5721. if (wc->refs[level - 1] > 1) {
  5722. if (level == 1 &&
  5723. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5724. goto skip;
  5725. if (!wc->update_ref ||
  5726. generation <= root->root_key.offset)
  5727. goto skip;
  5728. btrfs_node_key_to_cpu(path->nodes[level], &key,
  5729. path->slots[level]);
  5730. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  5731. if (ret < 0)
  5732. goto skip;
  5733. wc->stage = UPDATE_BACKREF;
  5734. wc->shared_level = level - 1;
  5735. }
  5736. } else {
  5737. if (level == 1 &&
  5738. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5739. goto skip;
  5740. }
  5741. if (!btrfs_buffer_uptodate(next, generation)) {
  5742. btrfs_tree_unlock(next);
  5743. free_extent_buffer(next);
  5744. next = NULL;
  5745. *lookup_info = 1;
  5746. }
  5747. if (!next) {
  5748. if (reada && level == 1)
  5749. reada_walk_down(trans, root, wc, path);
  5750. next = read_tree_block(root, bytenr, blocksize, generation);
  5751. if (!next)
  5752. return -EIO;
  5753. btrfs_tree_lock(next);
  5754. btrfs_set_lock_blocking(next);
  5755. }
  5756. level--;
  5757. BUG_ON(level != btrfs_header_level(next));
  5758. path->nodes[level] = next;
  5759. path->slots[level] = 0;
  5760. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5761. wc->level = level;
  5762. if (wc->level == 1)
  5763. wc->reada_slot = 0;
  5764. return 0;
  5765. skip:
  5766. wc->refs[level - 1] = 0;
  5767. wc->flags[level - 1] = 0;
  5768. if (wc->stage == DROP_REFERENCE) {
  5769. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  5770. parent = path->nodes[level]->start;
  5771. } else {
  5772. BUG_ON(root->root_key.objectid !=
  5773. btrfs_header_owner(path->nodes[level]));
  5774. parent = 0;
  5775. }
  5776. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  5777. root->root_key.objectid, level - 1, 0, 0);
  5778. BUG_ON(ret);
  5779. }
  5780. btrfs_tree_unlock(next);
  5781. free_extent_buffer(next);
  5782. *lookup_info = 1;
  5783. return 1;
  5784. }
  5785. /*
  5786. * hepler to process tree block while walking up the tree.
  5787. *
  5788. * when wc->stage == DROP_REFERENCE, this function drops
  5789. * reference count on the block.
  5790. *
  5791. * when wc->stage == UPDATE_BACKREF, this function changes
  5792. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  5793. * to UPDATE_BACKREF previously while processing the block.
  5794. *
  5795. * NOTE: return value 1 means we should stop walking up.
  5796. */
  5797. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  5798. struct btrfs_root *root,
  5799. struct btrfs_path *path,
  5800. struct walk_control *wc)
  5801. {
  5802. int ret;
  5803. int level = wc->level;
  5804. struct extent_buffer *eb = path->nodes[level];
  5805. u64 parent = 0;
  5806. if (wc->stage == UPDATE_BACKREF) {
  5807. BUG_ON(wc->shared_level < level);
  5808. if (level < wc->shared_level)
  5809. goto out;
  5810. ret = find_next_key(path, level + 1, &wc->update_progress);
  5811. if (ret > 0)
  5812. wc->update_ref = 0;
  5813. wc->stage = DROP_REFERENCE;
  5814. wc->shared_level = -1;
  5815. path->slots[level] = 0;
  5816. /*
  5817. * check reference count again if the block isn't locked.
  5818. * we should start walking down the tree again if reference
  5819. * count is one.
  5820. */
  5821. if (!path->locks[level]) {
  5822. BUG_ON(level == 0);
  5823. btrfs_tree_lock(eb);
  5824. btrfs_set_lock_blocking(eb);
  5825. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5826. ret = btrfs_lookup_extent_info(trans, root,
  5827. eb->start, eb->len,
  5828. &wc->refs[level],
  5829. &wc->flags[level]);
  5830. BUG_ON(ret);
  5831. BUG_ON(wc->refs[level] == 0);
  5832. if (wc->refs[level] == 1) {
  5833. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5834. return 1;
  5835. }
  5836. }
  5837. }
  5838. /* wc->stage == DROP_REFERENCE */
  5839. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  5840. if (wc->refs[level] == 1) {
  5841. if (level == 0) {
  5842. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5843. ret = btrfs_dec_ref(trans, root, eb, 1,
  5844. wc->for_reloc);
  5845. else
  5846. ret = btrfs_dec_ref(trans, root, eb, 0,
  5847. wc->for_reloc);
  5848. BUG_ON(ret);
  5849. }
  5850. /* make block locked assertion in clean_tree_block happy */
  5851. if (!path->locks[level] &&
  5852. btrfs_header_generation(eb) == trans->transid) {
  5853. btrfs_tree_lock(eb);
  5854. btrfs_set_lock_blocking(eb);
  5855. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5856. }
  5857. clean_tree_block(trans, root, eb);
  5858. }
  5859. if (eb == root->node) {
  5860. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5861. parent = eb->start;
  5862. else
  5863. BUG_ON(root->root_key.objectid !=
  5864. btrfs_header_owner(eb));
  5865. } else {
  5866. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5867. parent = path->nodes[level + 1]->start;
  5868. else
  5869. BUG_ON(root->root_key.objectid !=
  5870. btrfs_header_owner(path->nodes[level + 1]));
  5871. }
  5872. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1, 0);
  5873. out:
  5874. wc->refs[level] = 0;
  5875. wc->flags[level] = 0;
  5876. return 0;
  5877. }
  5878. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  5879. struct btrfs_root *root,
  5880. struct btrfs_path *path,
  5881. struct walk_control *wc)
  5882. {
  5883. int level = wc->level;
  5884. int lookup_info = 1;
  5885. int ret;
  5886. while (level >= 0) {
  5887. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  5888. if (ret > 0)
  5889. break;
  5890. if (level == 0)
  5891. break;
  5892. if (path->slots[level] >=
  5893. btrfs_header_nritems(path->nodes[level]))
  5894. break;
  5895. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  5896. if (ret > 0) {
  5897. path->slots[level]++;
  5898. continue;
  5899. } else if (ret < 0)
  5900. return ret;
  5901. level = wc->level;
  5902. }
  5903. return 0;
  5904. }
  5905. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  5906. struct btrfs_root *root,
  5907. struct btrfs_path *path,
  5908. struct walk_control *wc, int max_level)
  5909. {
  5910. int level = wc->level;
  5911. int ret;
  5912. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  5913. while (level < max_level && path->nodes[level]) {
  5914. wc->level = level;
  5915. if (path->slots[level] + 1 <
  5916. btrfs_header_nritems(path->nodes[level])) {
  5917. path->slots[level]++;
  5918. return 0;
  5919. } else {
  5920. ret = walk_up_proc(trans, root, path, wc);
  5921. if (ret > 0)
  5922. return 0;
  5923. if (path->locks[level]) {
  5924. btrfs_tree_unlock_rw(path->nodes[level],
  5925. path->locks[level]);
  5926. path->locks[level] = 0;
  5927. }
  5928. free_extent_buffer(path->nodes[level]);
  5929. path->nodes[level] = NULL;
  5930. level++;
  5931. }
  5932. }
  5933. return 1;
  5934. }
  5935. /*
  5936. * drop a subvolume tree.
  5937. *
  5938. * this function traverses the tree freeing any blocks that only
  5939. * referenced by the tree.
  5940. *
  5941. * when a shared tree block is found. this function decreases its
  5942. * reference count by one. if update_ref is true, this function
  5943. * also make sure backrefs for the shared block and all lower level
  5944. * blocks are properly updated.
  5945. */
  5946. int btrfs_drop_snapshot(struct btrfs_root *root,
  5947. struct btrfs_block_rsv *block_rsv, int update_ref,
  5948. int for_reloc)
  5949. {
  5950. struct btrfs_path *path;
  5951. struct btrfs_trans_handle *trans;
  5952. struct btrfs_root *tree_root = root->fs_info->tree_root;
  5953. struct btrfs_root_item *root_item = &root->root_item;
  5954. struct walk_control *wc;
  5955. struct btrfs_key key;
  5956. int err = 0;
  5957. int ret;
  5958. int level;
  5959. path = btrfs_alloc_path();
  5960. if (!path) {
  5961. err = -ENOMEM;
  5962. goto out;
  5963. }
  5964. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5965. if (!wc) {
  5966. btrfs_free_path(path);
  5967. err = -ENOMEM;
  5968. goto out;
  5969. }
  5970. trans = btrfs_start_transaction(tree_root, 0);
  5971. BUG_ON(IS_ERR(trans));
  5972. if (block_rsv)
  5973. trans->block_rsv = block_rsv;
  5974. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  5975. level = btrfs_header_level(root->node);
  5976. path->nodes[level] = btrfs_lock_root_node(root);
  5977. btrfs_set_lock_blocking(path->nodes[level]);
  5978. path->slots[level] = 0;
  5979. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5980. memset(&wc->update_progress, 0,
  5981. sizeof(wc->update_progress));
  5982. } else {
  5983. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  5984. memcpy(&wc->update_progress, &key,
  5985. sizeof(wc->update_progress));
  5986. level = root_item->drop_level;
  5987. BUG_ON(level == 0);
  5988. path->lowest_level = level;
  5989. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5990. path->lowest_level = 0;
  5991. if (ret < 0) {
  5992. err = ret;
  5993. goto out_free;
  5994. }
  5995. WARN_ON(ret > 0);
  5996. /*
  5997. * unlock our path, this is safe because only this
  5998. * function is allowed to delete this snapshot
  5999. */
  6000. btrfs_unlock_up_safe(path, 0);
  6001. level = btrfs_header_level(root->node);
  6002. while (1) {
  6003. btrfs_tree_lock(path->nodes[level]);
  6004. btrfs_set_lock_blocking(path->nodes[level]);
  6005. ret = btrfs_lookup_extent_info(trans, root,
  6006. path->nodes[level]->start,
  6007. path->nodes[level]->len,
  6008. &wc->refs[level],
  6009. &wc->flags[level]);
  6010. BUG_ON(ret);
  6011. BUG_ON(wc->refs[level] == 0);
  6012. if (level == root_item->drop_level)
  6013. break;
  6014. btrfs_tree_unlock(path->nodes[level]);
  6015. WARN_ON(wc->refs[level] != 1);
  6016. level--;
  6017. }
  6018. }
  6019. wc->level = level;
  6020. wc->shared_level = -1;
  6021. wc->stage = DROP_REFERENCE;
  6022. wc->update_ref = update_ref;
  6023. wc->keep_locks = 0;
  6024. wc->for_reloc = for_reloc;
  6025. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6026. while (1) {
  6027. ret = walk_down_tree(trans, root, path, wc);
  6028. if (ret < 0) {
  6029. err = ret;
  6030. break;
  6031. }
  6032. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6033. if (ret < 0) {
  6034. err = ret;
  6035. break;
  6036. }
  6037. if (ret > 0) {
  6038. BUG_ON(wc->stage != DROP_REFERENCE);
  6039. break;
  6040. }
  6041. if (wc->stage == DROP_REFERENCE) {
  6042. level = wc->level;
  6043. btrfs_node_key(path->nodes[level],
  6044. &root_item->drop_progress,
  6045. path->slots[level]);
  6046. root_item->drop_level = level;
  6047. }
  6048. BUG_ON(wc->level == 0);
  6049. if (btrfs_should_end_transaction(trans, tree_root)) {
  6050. ret = btrfs_update_root(trans, tree_root,
  6051. &root->root_key,
  6052. root_item);
  6053. BUG_ON(ret);
  6054. btrfs_end_transaction_throttle(trans, tree_root);
  6055. trans = btrfs_start_transaction(tree_root, 0);
  6056. BUG_ON(IS_ERR(trans));
  6057. if (block_rsv)
  6058. trans->block_rsv = block_rsv;
  6059. }
  6060. }
  6061. btrfs_release_path(path);
  6062. BUG_ON(err);
  6063. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6064. BUG_ON(ret);
  6065. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6066. ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
  6067. NULL, NULL);
  6068. BUG_ON(ret < 0);
  6069. if (ret > 0) {
  6070. /* if we fail to delete the orphan item this time
  6071. * around, it'll get picked up the next time.
  6072. *
  6073. * The most common failure here is just -ENOENT.
  6074. */
  6075. btrfs_del_orphan_item(trans, tree_root,
  6076. root->root_key.objectid);
  6077. }
  6078. }
  6079. if (root->in_radix) {
  6080. btrfs_free_fs_root(tree_root->fs_info, root);
  6081. } else {
  6082. free_extent_buffer(root->node);
  6083. free_extent_buffer(root->commit_root);
  6084. kfree(root);
  6085. }
  6086. out_free:
  6087. btrfs_end_transaction_throttle(trans, tree_root);
  6088. kfree(wc);
  6089. btrfs_free_path(path);
  6090. out:
  6091. if (err)
  6092. btrfs_std_error(root->fs_info, err);
  6093. return err;
  6094. }
  6095. /*
  6096. * drop subtree rooted at tree block 'node'.
  6097. *
  6098. * NOTE: this function will unlock and release tree block 'node'
  6099. * only used by relocation code
  6100. */
  6101. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6102. struct btrfs_root *root,
  6103. struct extent_buffer *node,
  6104. struct extent_buffer *parent)
  6105. {
  6106. struct btrfs_path *path;
  6107. struct walk_control *wc;
  6108. int level;
  6109. int parent_level;
  6110. int ret = 0;
  6111. int wret;
  6112. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6113. path = btrfs_alloc_path();
  6114. if (!path)
  6115. return -ENOMEM;
  6116. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6117. if (!wc) {
  6118. btrfs_free_path(path);
  6119. return -ENOMEM;
  6120. }
  6121. btrfs_assert_tree_locked(parent);
  6122. parent_level = btrfs_header_level(parent);
  6123. extent_buffer_get(parent);
  6124. path->nodes[parent_level] = parent;
  6125. path->slots[parent_level] = btrfs_header_nritems(parent);
  6126. btrfs_assert_tree_locked(node);
  6127. level = btrfs_header_level(node);
  6128. path->nodes[level] = node;
  6129. path->slots[level] = 0;
  6130. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6131. wc->refs[parent_level] = 1;
  6132. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6133. wc->level = level;
  6134. wc->shared_level = -1;
  6135. wc->stage = DROP_REFERENCE;
  6136. wc->update_ref = 0;
  6137. wc->keep_locks = 1;
  6138. wc->for_reloc = 1;
  6139. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6140. while (1) {
  6141. wret = walk_down_tree(trans, root, path, wc);
  6142. if (wret < 0) {
  6143. ret = wret;
  6144. break;
  6145. }
  6146. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6147. if (wret < 0)
  6148. ret = wret;
  6149. if (wret != 0)
  6150. break;
  6151. }
  6152. kfree(wc);
  6153. btrfs_free_path(path);
  6154. return ret;
  6155. }
  6156. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6157. {
  6158. u64 num_devices;
  6159. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6160. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6161. if (root->fs_info->balance_ctl) {
  6162. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  6163. u64 tgt = 0;
  6164. /* pick restriper's target profile and return */
  6165. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  6166. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  6167. tgt = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  6168. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  6169. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  6170. tgt = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  6171. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  6172. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  6173. tgt = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  6174. }
  6175. if (tgt) {
  6176. /* extended -> chunk profile */
  6177. tgt &= ~BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  6178. return tgt;
  6179. }
  6180. }
  6181. /*
  6182. * we add in the count of missing devices because we want
  6183. * to make sure that any RAID levels on a degraded FS
  6184. * continue to be honored.
  6185. */
  6186. num_devices = root->fs_info->fs_devices->rw_devices +
  6187. root->fs_info->fs_devices->missing_devices;
  6188. if (num_devices == 1) {
  6189. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6190. stripped = flags & ~stripped;
  6191. /* turn raid0 into single device chunks */
  6192. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6193. return stripped;
  6194. /* turn mirroring into duplication */
  6195. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6196. BTRFS_BLOCK_GROUP_RAID10))
  6197. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6198. return flags;
  6199. } else {
  6200. /* they already had raid on here, just return */
  6201. if (flags & stripped)
  6202. return flags;
  6203. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6204. stripped = flags & ~stripped;
  6205. /* switch duplicated blocks with raid1 */
  6206. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6207. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6208. /* turn single device chunks into raid0 */
  6209. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  6210. }
  6211. return flags;
  6212. }
  6213. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6214. {
  6215. struct btrfs_space_info *sinfo = cache->space_info;
  6216. u64 num_bytes;
  6217. u64 min_allocable_bytes;
  6218. int ret = -ENOSPC;
  6219. /*
  6220. * We need some metadata space and system metadata space for
  6221. * allocating chunks in some corner cases until we force to set
  6222. * it to be readonly.
  6223. */
  6224. if ((sinfo->flags &
  6225. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6226. !force)
  6227. min_allocable_bytes = 1 * 1024 * 1024;
  6228. else
  6229. min_allocable_bytes = 0;
  6230. spin_lock(&sinfo->lock);
  6231. spin_lock(&cache->lock);
  6232. if (cache->ro) {
  6233. ret = 0;
  6234. goto out;
  6235. }
  6236. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6237. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6238. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6239. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6240. min_allocable_bytes <= sinfo->total_bytes) {
  6241. sinfo->bytes_readonly += num_bytes;
  6242. cache->ro = 1;
  6243. ret = 0;
  6244. }
  6245. out:
  6246. spin_unlock(&cache->lock);
  6247. spin_unlock(&sinfo->lock);
  6248. return ret;
  6249. }
  6250. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6251. struct btrfs_block_group_cache *cache)
  6252. {
  6253. struct btrfs_trans_handle *trans;
  6254. u64 alloc_flags;
  6255. int ret;
  6256. BUG_ON(cache->ro);
  6257. trans = btrfs_join_transaction(root);
  6258. BUG_ON(IS_ERR(trans));
  6259. alloc_flags = update_block_group_flags(root, cache->flags);
  6260. if (alloc_flags != cache->flags)
  6261. do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
  6262. CHUNK_ALLOC_FORCE);
  6263. ret = set_block_group_ro(cache, 0);
  6264. if (!ret)
  6265. goto out;
  6266. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6267. ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
  6268. CHUNK_ALLOC_FORCE);
  6269. if (ret < 0)
  6270. goto out;
  6271. ret = set_block_group_ro(cache, 0);
  6272. out:
  6273. btrfs_end_transaction(trans, root);
  6274. return ret;
  6275. }
  6276. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6277. struct btrfs_root *root, u64 type)
  6278. {
  6279. u64 alloc_flags = get_alloc_profile(root, type);
  6280. return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
  6281. CHUNK_ALLOC_FORCE);
  6282. }
  6283. /*
  6284. * helper to account the unused space of all the readonly block group in the
  6285. * list. takes mirrors into account.
  6286. */
  6287. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6288. {
  6289. struct btrfs_block_group_cache *block_group;
  6290. u64 free_bytes = 0;
  6291. int factor;
  6292. list_for_each_entry(block_group, groups_list, list) {
  6293. spin_lock(&block_group->lock);
  6294. if (!block_group->ro) {
  6295. spin_unlock(&block_group->lock);
  6296. continue;
  6297. }
  6298. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6299. BTRFS_BLOCK_GROUP_RAID10 |
  6300. BTRFS_BLOCK_GROUP_DUP))
  6301. factor = 2;
  6302. else
  6303. factor = 1;
  6304. free_bytes += (block_group->key.offset -
  6305. btrfs_block_group_used(&block_group->item)) *
  6306. factor;
  6307. spin_unlock(&block_group->lock);
  6308. }
  6309. return free_bytes;
  6310. }
  6311. /*
  6312. * helper to account the unused space of all the readonly block group in the
  6313. * space_info. takes mirrors into account.
  6314. */
  6315. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  6316. {
  6317. int i;
  6318. u64 free_bytes = 0;
  6319. spin_lock(&sinfo->lock);
  6320. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  6321. if (!list_empty(&sinfo->block_groups[i]))
  6322. free_bytes += __btrfs_get_ro_block_group_free_space(
  6323. &sinfo->block_groups[i]);
  6324. spin_unlock(&sinfo->lock);
  6325. return free_bytes;
  6326. }
  6327. void btrfs_set_block_group_rw(struct btrfs_root *root,
  6328. struct btrfs_block_group_cache *cache)
  6329. {
  6330. struct btrfs_space_info *sinfo = cache->space_info;
  6331. u64 num_bytes;
  6332. BUG_ON(!cache->ro);
  6333. spin_lock(&sinfo->lock);
  6334. spin_lock(&cache->lock);
  6335. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6336. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6337. sinfo->bytes_readonly -= num_bytes;
  6338. cache->ro = 0;
  6339. spin_unlock(&cache->lock);
  6340. spin_unlock(&sinfo->lock);
  6341. }
  6342. /*
  6343. * checks to see if its even possible to relocate this block group.
  6344. *
  6345. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  6346. * ok to go ahead and try.
  6347. */
  6348. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  6349. {
  6350. struct btrfs_block_group_cache *block_group;
  6351. struct btrfs_space_info *space_info;
  6352. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  6353. struct btrfs_device *device;
  6354. u64 min_free;
  6355. u64 dev_min = 1;
  6356. u64 dev_nr = 0;
  6357. int index;
  6358. int full = 0;
  6359. int ret = 0;
  6360. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  6361. /* odd, couldn't find the block group, leave it alone */
  6362. if (!block_group)
  6363. return -1;
  6364. min_free = btrfs_block_group_used(&block_group->item);
  6365. /* no bytes used, we're good */
  6366. if (!min_free)
  6367. goto out;
  6368. space_info = block_group->space_info;
  6369. spin_lock(&space_info->lock);
  6370. full = space_info->full;
  6371. /*
  6372. * if this is the last block group we have in this space, we can't
  6373. * relocate it unless we're able to allocate a new chunk below.
  6374. *
  6375. * Otherwise, we need to make sure we have room in the space to handle
  6376. * all of the extents from this block group. If we can, we're good
  6377. */
  6378. if ((space_info->total_bytes != block_group->key.offset) &&
  6379. (space_info->bytes_used + space_info->bytes_reserved +
  6380. space_info->bytes_pinned + space_info->bytes_readonly +
  6381. min_free < space_info->total_bytes)) {
  6382. spin_unlock(&space_info->lock);
  6383. goto out;
  6384. }
  6385. spin_unlock(&space_info->lock);
  6386. /*
  6387. * ok we don't have enough space, but maybe we have free space on our
  6388. * devices to allocate new chunks for relocation, so loop through our
  6389. * alloc devices and guess if we have enough space. However, if we
  6390. * were marked as full, then we know there aren't enough chunks, and we
  6391. * can just return.
  6392. */
  6393. ret = -1;
  6394. if (full)
  6395. goto out;
  6396. /*
  6397. * index:
  6398. * 0: raid10
  6399. * 1: raid1
  6400. * 2: dup
  6401. * 3: raid0
  6402. * 4: single
  6403. */
  6404. index = get_block_group_index(block_group);
  6405. if (index == 0) {
  6406. dev_min = 4;
  6407. /* Divide by 2 */
  6408. min_free >>= 1;
  6409. } else if (index == 1) {
  6410. dev_min = 2;
  6411. } else if (index == 2) {
  6412. /* Multiply by 2 */
  6413. min_free <<= 1;
  6414. } else if (index == 3) {
  6415. dev_min = fs_devices->rw_devices;
  6416. do_div(min_free, dev_min);
  6417. }
  6418. mutex_lock(&root->fs_info->chunk_mutex);
  6419. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  6420. u64 dev_offset;
  6421. /*
  6422. * check to make sure we can actually find a chunk with enough
  6423. * space to fit our block group in.
  6424. */
  6425. if (device->total_bytes > device->bytes_used + min_free) {
  6426. ret = find_free_dev_extent(device, min_free,
  6427. &dev_offset, NULL);
  6428. if (!ret)
  6429. dev_nr++;
  6430. if (dev_nr >= dev_min)
  6431. break;
  6432. ret = -1;
  6433. }
  6434. }
  6435. mutex_unlock(&root->fs_info->chunk_mutex);
  6436. out:
  6437. btrfs_put_block_group(block_group);
  6438. return ret;
  6439. }
  6440. static int find_first_block_group(struct btrfs_root *root,
  6441. struct btrfs_path *path, struct btrfs_key *key)
  6442. {
  6443. int ret = 0;
  6444. struct btrfs_key found_key;
  6445. struct extent_buffer *leaf;
  6446. int slot;
  6447. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  6448. if (ret < 0)
  6449. goto out;
  6450. while (1) {
  6451. slot = path->slots[0];
  6452. leaf = path->nodes[0];
  6453. if (slot >= btrfs_header_nritems(leaf)) {
  6454. ret = btrfs_next_leaf(root, path);
  6455. if (ret == 0)
  6456. continue;
  6457. if (ret < 0)
  6458. goto out;
  6459. break;
  6460. }
  6461. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6462. if (found_key.objectid >= key->objectid &&
  6463. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  6464. ret = 0;
  6465. goto out;
  6466. }
  6467. path->slots[0]++;
  6468. }
  6469. out:
  6470. return ret;
  6471. }
  6472. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  6473. {
  6474. struct btrfs_block_group_cache *block_group;
  6475. u64 last = 0;
  6476. while (1) {
  6477. struct inode *inode;
  6478. block_group = btrfs_lookup_first_block_group(info, last);
  6479. while (block_group) {
  6480. spin_lock(&block_group->lock);
  6481. if (block_group->iref)
  6482. break;
  6483. spin_unlock(&block_group->lock);
  6484. block_group = next_block_group(info->tree_root,
  6485. block_group);
  6486. }
  6487. if (!block_group) {
  6488. if (last == 0)
  6489. break;
  6490. last = 0;
  6491. continue;
  6492. }
  6493. inode = block_group->inode;
  6494. block_group->iref = 0;
  6495. block_group->inode = NULL;
  6496. spin_unlock(&block_group->lock);
  6497. iput(inode);
  6498. last = block_group->key.objectid + block_group->key.offset;
  6499. btrfs_put_block_group(block_group);
  6500. }
  6501. }
  6502. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  6503. {
  6504. struct btrfs_block_group_cache *block_group;
  6505. struct btrfs_space_info *space_info;
  6506. struct btrfs_caching_control *caching_ctl;
  6507. struct rb_node *n;
  6508. down_write(&info->extent_commit_sem);
  6509. while (!list_empty(&info->caching_block_groups)) {
  6510. caching_ctl = list_entry(info->caching_block_groups.next,
  6511. struct btrfs_caching_control, list);
  6512. list_del(&caching_ctl->list);
  6513. put_caching_control(caching_ctl);
  6514. }
  6515. up_write(&info->extent_commit_sem);
  6516. spin_lock(&info->block_group_cache_lock);
  6517. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  6518. block_group = rb_entry(n, struct btrfs_block_group_cache,
  6519. cache_node);
  6520. rb_erase(&block_group->cache_node,
  6521. &info->block_group_cache_tree);
  6522. spin_unlock(&info->block_group_cache_lock);
  6523. down_write(&block_group->space_info->groups_sem);
  6524. list_del(&block_group->list);
  6525. up_write(&block_group->space_info->groups_sem);
  6526. if (block_group->cached == BTRFS_CACHE_STARTED)
  6527. wait_block_group_cache_done(block_group);
  6528. /*
  6529. * We haven't cached this block group, which means we could
  6530. * possibly have excluded extents on this block group.
  6531. */
  6532. if (block_group->cached == BTRFS_CACHE_NO)
  6533. free_excluded_extents(info->extent_root, block_group);
  6534. btrfs_remove_free_space_cache(block_group);
  6535. btrfs_put_block_group(block_group);
  6536. spin_lock(&info->block_group_cache_lock);
  6537. }
  6538. spin_unlock(&info->block_group_cache_lock);
  6539. /* now that all the block groups are freed, go through and
  6540. * free all the space_info structs. This is only called during
  6541. * the final stages of unmount, and so we know nobody is
  6542. * using them. We call synchronize_rcu() once before we start,
  6543. * just to be on the safe side.
  6544. */
  6545. synchronize_rcu();
  6546. release_global_block_rsv(info);
  6547. while(!list_empty(&info->space_info)) {
  6548. space_info = list_entry(info->space_info.next,
  6549. struct btrfs_space_info,
  6550. list);
  6551. if (space_info->bytes_pinned > 0 ||
  6552. space_info->bytes_reserved > 0 ||
  6553. space_info->bytes_may_use > 0) {
  6554. WARN_ON(1);
  6555. dump_space_info(space_info, 0, 0);
  6556. }
  6557. list_del(&space_info->list);
  6558. kfree(space_info);
  6559. }
  6560. return 0;
  6561. }
  6562. static void __link_block_group(struct btrfs_space_info *space_info,
  6563. struct btrfs_block_group_cache *cache)
  6564. {
  6565. int index = get_block_group_index(cache);
  6566. down_write(&space_info->groups_sem);
  6567. list_add_tail(&cache->list, &space_info->block_groups[index]);
  6568. up_write(&space_info->groups_sem);
  6569. }
  6570. int btrfs_read_block_groups(struct btrfs_root *root)
  6571. {
  6572. struct btrfs_path *path;
  6573. int ret;
  6574. struct btrfs_block_group_cache *cache;
  6575. struct btrfs_fs_info *info = root->fs_info;
  6576. struct btrfs_space_info *space_info;
  6577. struct btrfs_key key;
  6578. struct btrfs_key found_key;
  6579. struct extent_buffer *leaf;
  6580. int need_clear = 0;
  6581. u64 cache_gen;
  6582. root = info->extent_root;
  6583. key.objectid = 0;
  6584. key.offset = 0;
  6585. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  6586. path = btrfs_alloc_path();
  6587. if (!path)
  6588. return -ENOMEM;
  6589. path->reada = 1;
  6590. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  6591. if (btrfs_test_opt(root, SPACE_CACHE) &&
  6592. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  6593. need_clear = 1;
  6594. if (btrfs_test_opt(root, CLEAR_CACHE))
  6595. need_clear = 1;
  6596. while (1) {
  6597. ret = find_first_block_group(root, path, &key);
  6598. if (ret > 0)
  6599. break;
  6600. if (ret != 0)
  6601. goto error;
  6602. leaf = path->nodes[0];
  6603. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6604. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6605. if (!cache) {
  6606. ret = -ENOMEM;
  6607. goto error;
  6608. }
  6609. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  6610. GFP_NOFS);
  6611. if (!cache->free_space_ctl) {
  6612. kfree(cache);
  6613. ret = -ENOMEM;
  6614. goto error;
  6615. }
  6616. atomic_set(&cache->count, 1);
  6617. spin_lock_init(&cache->lock);
  6618. cache->fs_info = info;
  6619. INIT_LIST_HEAD(&cache->list);
  6620. INIT_LIST_HEAD(&cache->cluster_list);
  6621. if (need_clear)
  6622. cache->disk_cache_state = BTRFS_DC_CLEAR;
  6623. read_extent_buffer(leaf, &cache->item,
  6624. btrfs_item_ptr_offset(leaf, path->slots[0]),
  6625. sizeof(cache->item));
  6626. memcpy(&cache->key, &found_key, sizeof(found_key));
  6627. key.objectid = found_key.objectid + found_key.offset;
  6628. btrfs_release_path(path);
  6629. cache->flags = btrfs_block_group_flags(&cache->item);
  6630. cache->sectorsize = root->sectorsize;
  6631. btrfs_init_free_space_ctl(cache);
  6632. /*
  6633. * We need to exclude the super stripes now so that the space
  6634. * info has super bytes accounted for, otherwise we'll think
  6635. * we have more space than we actually do.
  6636. */
  6637. exclude_super_stripes(root, cache);
  6638. /*
  6639. * check for two cases, either we are full, and therefore
  6640. * don't need to bother with the caching work since we won't
  6641. * find any space, or we are empty, and we can just add all
  6642. * the space in and be done with it. This saves us _alot_ of
  6643. * time, particularly in the full case.
  6644. */
  6645. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  6646. cache->last_byte_to_unpin = (u64)-1;
  6647. cache->cached = BTRFS_CACHE_FINISHED;
  6648. free_excluded_extents(root, cache);
  6649. } else if (btrfs_block_group_used(&cache->item) == 0) {
  6650. cache->last_byte_to_unpin = (u64)-1;
  6651. cache->cached = BTRFS_CACHE_FINISHED;
  6652. add_new_free_space(cache, root->fs_info,
  6653. found_key.objectid,
  6654. found_key.objectid +
  6655. found_key.offset);
  6656. free_excluded_extents(root, cache);
  6657. }
  6658. ret = update_space_info(info, cache->flags, found_key.offset,
  6659. btrfs_block_group_used(&cache->item),
  6660. &space_info);
  6661. BUG_ON(ret);
  6662. cache->space_info = space_info;
  6663. spin_lock(&cache->space_info->lock);
  6664. cache->space_info->bytes_readonly += cache->bytes_super;
  6665. spin_unlock(&cache->space_info->lock);
  6666. __link_block_group(space_info, cache);
  6667. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6668. BUG_ON(ret);
  6669. set_avail_alloc_bits(root->fs_info, cache->flags);
  6670. if (btrfs_chunk_readonly(root, cache->key.objectid))
  6671. set_block_group_ro(cache, 1);
  6672. }
  6673. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  6674. if (!(get_alloc_profile(root, space_info->flags) &
  6675. (BTRFS_BLOCK_GROUP_RAID10 |
  6676. BTRFS_BLOCK_GROUP_RAID1 |
  6677. BTRFS_BLOCK_GROUP_DUP)))
  6678. continue;
  6679. /*
  6680. * avoid allocating from un-mirrored block group if there are
  6681. * mirrored block groups.
  6682. */
  6683. list_for_each_entry(cache, &space_info->block_groups[3], list)
  6684. set_block_group_ro(cache, 1);
  6685. list_for_each_entry(cache, &space_info->block_groups[4], list)
  6686. set_block_group_ro(cache, 1);
  6687. }
  6688. init_global_block_rsv(info);
  6689. ret = 0;
  6690. error:
  6691. btrfs_free_path(path);
  6692. return ret;
  6693. }
  6694. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  6695. struct btrfs_root *root, u64 bytes_used,
  6696. u64 type, u64 chunk_objectid, u64 chunk_offset,
  6697. u64 size)
  6698. {
  6699. int ret;
  6700. struct btrfs_root *extent_root;
  6701. struct btrfs_block_group_cache *cache;
  6702. extent_root = root->fs_info->extent_root;
  6703. root->fs_info->last_trans_log_full_commit = trans->transid;
  6704. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6705. if (!cache)
  6706. return -ENOMEM;
  6707. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  6708. GFP_NOFS);
  6709. if (!cache->free_space_ctl) {
  6710. kfree(cache);
  6711. return -ENOMEM;
  6712. }
  6713. cache->key.objectid = chunk_offset;
  6714. cache->key.offset = size;
  6715. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  6716. cache->sectorsize = root->sectorsize;
  6717. cache->fs_info = root->fs_info;
  6718. atomic_set(&cache->count, 1);
  6719. spin_lock_init(&cache->lock);
  6720. INIT_LIST_HEAD(&cache->list);
  6721. INIT_LIST_HEAD(&cache->cluster_list);
  6722. btrfs_init_free_space_ctl(cache);
  6723. btrfs_set_block_group_used(&cache->item, bytes_used);
  6724. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  6725. cache->flags = type;
  6726. btrfs_set_block_group_flags(&cache->item, type);
  6727. cache->last_byte_to_unpin = (u64)-1;
  6728. cache->cached = BTRFS_CACHE_FINISHED;
  6729. exclude_super_stripes(root, cache);
  6730. add_new_free_space(cache, root->fs_info, chunk_offset,
  6731. chunk_offset + size);
  6732. free_excluded_extents(root, cache);
  6733. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  6734. &cache->space_info);
  6735. BUG_ON(ret);
  6736. update_global_block_rsv(root->fs_info);
  6737. spin_lock(&cache->space_info->lock);
  6738. cache->space_info->bytes_readonly += cache->bytes_super;
  6739. spin_unlock(&cache->space_info->lock);
  6740. __link_block_group(cache->space_info, cache);
  6741. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6742. BUG_ON(ret);
  6743. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  6744. sizeof(cache->item));
  6745. BUG_ON(ret);
  6746. set_avail_alloc_bits(extent_root->fs_info, type);
  6747. return 0;
  6748. }
  6749. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  6750. {
  6751. u64 extra_flags = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK;
  6752. /* chunk -> extended profile */
  6753. if (extra_flags == 0)
  6754. extra_flags = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  6755. if (flags & BTRFS_BLOCK_GROUP_DATA)
  6756. fs_info->avail_data_alloc_bits &= ~extra_flags;
  6757. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  6758. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  6759. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  6760. fs_info->avail_system_alloc_bits &= ~extra_flags;
  6761. }
  6762. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  6763. struct btrfs_root *root, u64 group_start)
  6764. {
  6765. struct btrfs_path *path;
  6766. struct btrfs_block_group_cache *block_group;
  6767. struct btrfs_free_cluster *cluster;
  6768. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6769. struct btrfs_key key;
  6770. struct inode *inode;
  6771. int ret;
  6772. int index;
  6773. int factor;
  6774. root = root->fs_info->extent_root;
  6775. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  6776. BUG_ON(!block_group);
  6777. BUG_ON(!block_group->ro);
  6778. /*
  6779. * Free the reserved super bytes from this block group before
  6780. * remove it.
  6781. */
  6782. free_excluded_extents(root, block_group);
  6783. memcpy(&key, &block_group->key, sizeof(key));
  6784. index = get_block_group_index(block_group);
  6785. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  6786. BTRFS_BLOCK_GROUP_RAID1 |
  6787. BTRFS_BLOCK_GROUP_RAID10))
  6788. factor = 2;
  6789. else
  6790. factor = 1;
  6791. /* make sure this block group isn't part of an allocation cluster */
  6792. cluster = &root->fs_info->data_alloc_cluster;
  6793. spin_lock(&cluster->refill_lock);
  6794. btrfs_return_cluster_to_free_space(block_group, cluster);
  6795. spin_unlock(&cluster->refill_lock);
  6796. /*
  6797. * make sure this block group isn't part of a metadata
  6798. * allocation cluster
  6799. */
  6800. cluster = &root->fs_info->meta_alloc_cluster;
  6801. spin_lock(&cluster->refill_lock);
  6802. btrfs_return_cluster_to_free_space(block_group, cluster);
  6803. spin_unlock(&cluster->refill_lock);
  6804. path = btrfs_alloc_path();
  6805. if (!path) {
  6806. ret = -ENOMEM;
  6807. goto out;
  6808. }
  6809. inode = lookup_free_space_inode(tree_root, block_group, path);
  6810. if (!IS_ERR(inode)) {
  6811. ret = btrfs_orphan_add(trans, inode);
  6812. BUG_ON(ret);
  6813. clear_nlink(inode);
  6814. /* One for the block groups ref */
  6815. spin_lock(&block_group->lock);
  6816. if (block_group->iref) {
  6817. block_group->iref = 0;
  6818. block_group->inode = NULL;
  6819. spin_unlock(&block_group->lock);
  6820. iput(inode);
  6821. } else {
  6822. spin_unlock(&block_group->lock);
  6823. }
  6824. /* One for our lookup ref */
  6825. btrfs_add_delayed_iput(inode);
  6826. }
  6827. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  6828. key.offset = block_group->key.objectid;
  6829. key.type = 0;
  6830. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  6831. if (ret < 0)
  6832. goto out;
  6833. if (ret > 0)
  6834. btrfs_release_path(path);
  6835. if (ret == 0) {
  6836. ret = btrfs_del_item(trans, tree_root, path);
  6837. if (ret)
  6838. goto out;
  6839. btrfs_release_path(path);
  6840. }
  6841. spin_lock(&root->fs_info->block_group_cache_lock);
  6842. rb_erase(&block_group->cache_node,
  6843. &root->fs_info->block_group_cache_tree);
  6844. spin_unlock(&root->fs_info->block_group_cache_lock);
  6845. down_write(&block_group->space_info->groups_sem);
  6846. /*
  6847. * we must use list_del_init so people can check to see if they
  6848. * are still on the list after taking the semaphore
  6849. */
  6850. list_del_init(&block_group->list);
  6851. if (list_empty(&block_group->space_info->block_groups[index]))
  6852. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  6853. up_write(&block_group->space_info->groups_sem);
  6854. if (block_group->cached == BTRFS_CACHE_STARTED)
  6855. wait_block_group_cache_done(block_group);
  6856. btrfs_remove_free_space_cache(block_group);
  6857. spin_lock(&block_group->space_info->lock);
  6858. block_group->space_info->total_bytes -= block_group->key.offset;
  6859. block_group->space_info->bytes_readonly -= block_group->key.offset;
  6860. block_group->space_info->disk_total -= block_group->key.offset * factor;
  6861. spin_unlock(&block_group->space_info->lock);
  6862. memcpy(&key, &block_group->key, sizeof(key));
  6863. btrfs_clear_space_info_full(root->fs_info);
  6864. btrfs_put_block_group(block_group);
  6865. btrfs_put_block_group(block_group);
  6866. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  6867. if (ret > 0)
  6868. ret = -EIO;
  6869. if (ret < 0)
  6870. goto out;
  6871. ret = btrfs_del_item(trans, root, path);
  6872. out:
  6873. btrfs_free_path(path);
  6874. return ret;
  6875. }
  6876. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  6877. {
  6878. struct btrfs_space_info *space_info;
  6879. struct btrfs_super_block *disk_super;
  6880. u64 features;
  6881. u64 flags;
  6882. int mixed = 0;
  6883. int ret;
  6884. disk_super = fs_info->super_copy;
  6885. if (!btrfs_super_root(disk_super))
  6886. return 1;
  6887. features = btrfs_super_incompat_flags(disk_super);
  6888. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  6889. mixed = 1;
  6890. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  6891. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6892. if (ret)
  6893. goto out;
  6894. if (mixed) {
  6895. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  6896. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6897. } else {
  6898. flags = BTRFS_BLOCK_GROUP_METADATA;
  6899. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6900. if (ret)
  6901. goto out;
  6902. flags = BTRFS_BLOCK_GROUP_DATA;
  6903. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  6904. }
  6905. out:
  6906. return ret;
  6907. }
  6908. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  6909. {
  6910. return unpin_extent_range(root, start, end);
  6911. }
  6912. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  6913. u64 num_bytes, u64 *actual_bytes)
  6914. {
  6915. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  6916. }
  6917. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  6918. {
  6919. struct btrfs_fs_info *fs_info = root->fs_info;
  6920. struct btrfs_block_group_cache *cache = NULL;
  6921. u64 group_trimmed;
  6922. u64 start;
  6923. u64 end;
  6924. u64 trimmed = 0;
  6925. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  6926. int ret = 0;
  6927. /*
  6928. * try to trim all FS space, our block group may start from non-zero.
  6929. */
  6930. if (range->len == total_bytes)
  6931. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  6932. else
  6933. cache = btrfs_lookup_block_group(fs_info, range->start);
  6934. while (cache) {
  6935. if (cache->key.objectid >= (range->start + range->len)) {
  6936. btrfs_put_block_group(cache);
  6937. break;
  6938. }
  6939. start = max(range->start, cache->key.objectid);
  6940. end = min(range->start + range->len,
  6941. cache->key.objectid + cache->key.offset);
  6942. if (end - start >= range->minlen) {
  6943. if (!block_group_cache_done(cache)) {
  6944. ret = cache_block_group(cache, NULL, root, 0);
  6945. if (!ret)
  6946. wait_block_group_cache_done(cache);
  6947. }
  6948. ret = btrfs_trim_block_group(cache,
  6949. &group_trimmed,
  6950. start,
  6951. end,
  6952. range->minlen);
  6953. trimmed += group_trimmed;
  6954. if (ret) {
  6955. btrfs_put_block_group(cache);
  6956. break;
  6957. }
  6958. }
  6959. cache = next_block_group(fs_info->tree_root, cache);
  6960. }
  6961. range->len = trimmed;
  6962. return ret;
  6963. }