extent-tree.c 228 KB

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