extent-tree.c 238 KB

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