extent-tree.c 229 KB

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