extent-tree.c 237 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910
  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 = crc32c(high_crc, &lenum, sizeof(lenum));
  958. lenum = cpu_to_le64(owner);
  959. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  960. lenum = cpu_to_le64(offset);
  961. low_crc = 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. int ret;
  2088. unsigned long count = 0;
  2089. int must_insert_reserved = 0;
  2090. delayed_refs = &trans->transaction->delayed_refs;
  2091. while (1) {
  2092. if (!locked_ref) {
  2093. if (count >= nr)
  2094. break;
  2095. spin_lock(&delayed_refs->lock);
  2096. locked_ref = btrfs_select_ref_head(trans);
  2097. if (!locked_ref) {
  2098. spin_unlock(&delayed_refs->lock);
  2099. break;
  2100. }
  2101. /* grab the lock that says we are going to process
  2102. * all the refs for this head */
  2103. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  2104. spin_unlock(&delayed_refs->lock);
  2105. /*
  2106. * we may have dropped the spin lock to get the head
  2107. * mutex lock, and that might have given someone else
  2108. * time to free the head. If that's true, it has been
  2109. * removed from our list and we can move on.
  2110. */
  2111. if (ret == -EAGAIN) {
  2112. locked_ref = NULL;
  2113. count++;
  2114. continue;
  2115. }
  2116. }
  2117. /*
  2118. * We need to try and merge add/drops of the same ref since we
  2119. * can run into issues with relocate dropping the implicit ref
  2120. * and then it being added back again before the drop can
  2121. * finish. If we merged anything we need to re-loop so we can
  2122. * get a good ref.
  2123. */
  2124. spin_lock(&locked_ref->lock);
  2125. btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
  2126. locked_ref);
  2127. /*
  2128. * locked_ref is the head node, so we have to go one
  2129. * node back for any delayed ref updates
  2130. */
  2131. ref = select_delayed_ref(locked_ref);
  2132. if (ref && ref->seq &&
  2133. btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
  2134. spin_unlock(&locked_ref->lock);
  2135. btrfs_delayed_ref_unlock(locked_ref);
  2136. spin_lock(&delayed_refs->lock);
  2137. locked_ref->processing = 0;
  2138. delayed_refs->num_heads_ready++;
  2139. spin_unlock(&delayed_refs->lock);
  2140. locked_ref = NULL;
  2141. cond_resched();
  2142. continue;
  2143. }
  2144. /*
  2145. * record the must insert reserved flag before we
  2146. * drop the spin lock.
  2147. */
  2148. must_insert_reserved = locked_ref->must_insert_reserved;
  2149. locked_ref->must_insert_reserved = 0;
  2150. extent_op = locked_ref->extent_op;
  2151. locked_ref->extent_op = NULL;
  2152. if (!ref) {
  2153. /* All delayed refs have been processed, Go ahead
  2154. * and send the head node to run_one_delayed_ref,
  2155. * so that any accounting fixes can happen
  2156. */
  2157. ref = &locked_ref->node;
  2158. if (extent_op && must_insert_reserved) {
  2159. btrfs_free_delayed_extent_op(extent_op);
  2160. extent_op = NULL;
  2161. }
  2162. if (extent_op) {
  2163. spin_unlock(&locked_ref->lock);
  2164. ret = run_delayed_extent_op(trans, root,
  2165. ref, extent_op);
  2166. btrfs_free_delayed_extent_op(extent_op);
  2167. if (ret) {
  2168. /*
  2169. * Need to reset must_insert_reserved if
  2170. * there was an error so the abort stuff
  2171. * can cleanup the reserved space
  2172. * properly.
  2173. */
  2174. if (must_insert_reserved)
  2175. locked_ref->must_insert_reserved = 1;
  2176. locked_ref->processing = 0;
  2177. btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
  2178. btrfs_delayed_ref_unlock(locked_ref);
  2179. return ret;
  2180. }
  2181. continue;
  2182. }
  2183. /*
  2184. * Need to drop our head ref lock and re-aqcuire the
  2185. * delayed ref lock and then re-check to make sure
  2186. * nobody got added.
  2187. */
  2188. spin_unlock(&locked_ref->lock);
  2189. spin_lock(&delayed_refs->lock);
  2190. spin_lock(&locked_ref->lock);
  2191. if (rb_first(&locked_ref->ref_root)) {
  2192. spin_unlock(&locked_ref->lock);
  2193. spin_unlock(&delayed_refs->lock);
  2194. continue;
  2195. }
  2196. ref->in_tree = 0;
  2197. delayed_refs->num_heads--;
  2198. rb_erase(&locked_ref->href_node,
  2199. &delayed_refs->href_root);
  2200. spin_unlock(&delayed_refs->lock);
  2201. } else {
  2202. ref->in_tree = 0;
  2203. rb_erase(&ref->rb_node, &locked_ref->ref_root);
  2204. }
  2205. atomic_dec(&delayed_refs->num_entries);
  2206. if (!btrfs_delayed_ref_is_head(ref)) {
  2207. /*
  2208. * when we play the delayed ref, also correct the
  2209. * ref_mod on head
  2210. */
  2211. switch (ref->action) {
  2212. case BTRFS_ADD_DELAYED_REF:
  2213. case BTRFS_ADD_DELAYED_EXTENT:
  2214. locked_ref->node.ref_mod -= ref->ref_mod;
  2215. break;
  2216. case BTRFS_DROP_DELAYED_REF:
  2217. locked_ref->node.ref_mod += ref->ref_mod;
  2218. break;
  2219. default:
  2220. WARN_ON(1);
  2221. }
  2222. }
  2223. spin_unlock(&locked_ref->lock);
  2224. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2225. must_insert_reserved);
  2226. btrfs_free_delayed_extent_op(extent_op);
  2227. if (ret) {
  2228. locked_ref->processing = 0;
  2229. btrfs_delayed_ref_unlock(locked_ref);
  2230. btrfs_put_delayed_ref(ref);
  2231. btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
  2232. return ret;
  2233. }
  2234. /*
  2235. * If this node is a head, that means all the refs in this head
  2236. * have been dealt with, and we will pick the next head to deal
  2237. * with, so we must unlock the head and drop it from the cluster
  2238. * list before we release it.
  2239. */
  2240. if (btrfs_delayed_ref_is_head(ref)) {
  2241. btrfs_delayed_ref_unlock(locked_ref);
  2242. locked_ref = NULL;
  2243. }
  2244. btrfs_put_delayed_ref(ref);
  2245. count++;
  2246. cond_resched();
  2247. }
  2248. return 0;
  2249. }
  2250. #ifdef SCRAMBLE_DELAYED_REFS
  2251. /*
  2252. * Normally delayed refs get processed in ascending bytenr order. This
  2253. * correlates in most cases to the order added. To expose dependencies on this
  2254. * order, we start to process the tree in the middle instead of the beginning
  2255. */
  2256. static u64 find_middle(struct rb_root *root)
  2257. {
  2258. struct rb_node *n = root->rb_node;
  2259. struct btrfs_delayed_ref_node *entry;
  2260. int alt = 1;
  2261. u64 middle;
  2262. u64 first = 0, last = 0;
  2263. n = rb_first(root);
  2264. if (n) {
  2265. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2266. first = entry->bytenr;
  2267. }
  2268. n = rb_last(root);
  2269. if (n) {
  2270. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2271. last = entry->bytenr;
  2272. }
  2273. n = root->rb_node;
  2274. while (n) {
  2275. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2276. WARN_ON(!entry->in_tree);
  2277. middle = entry->bytenr;
  2278. if (alt)
  2279. n = n->rb_left;
  2280. else
  2281. n = n->rb_right;
  2282. alt = 1 - alt;
  2283. }
  2284. return middle;
  2285. }
  2286. #endif
  2287. int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
  2288. struct btrfs_fs_info *fs_info)
  2289. {
  2290. struct qgroup_update *qgroup_update;
  2291. int ret = 0;
  2292. if (list_empty(&trans->qgroup_ref_list) !=
  2293. !trans->delayed_ref_elem.seq) {
  2294. /* list without seq or seq without list */
  2295. btrfs_err(fs_info,
  2296. "qgroup accounting update error, list is%s empty, seq is %#x.%x",
  2297. list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2298. (u32)(trans->delayed_ref_elem.seq >> 32),
  2299. (u32)trans->delayed_ref_elem.seq);
  2300. BUG();
  2301. }
  2302. if (!trans->delayed_ref_elem.seq)
  2303. return 0;
  2304. while (!list_empty(&trans->qgroup_ref_list)) {
  2305. qgroup_update = list_first_entry(&trans->qgroup_ref_list,
  2306. struct qgroup_update, list);
  2307. list_del(&qgroup_update->list);
  2308. if (!ret)
  2309. ret = btrfs_qgroup_account_ref(
  2310. trans, fs_info, qgroup_update->node,
  2311. qgroup_update->extent_op);
  2312. kfree(qgroup_update);
  2313. }
  2314. btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  2315. return ret;
  2316. }
  2317. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2318. {
  2319. u64 num_bytes;
  2320. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2321. sizeof(struct btrfs_extent_inline_ref));
  2322. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2323. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2324. /*
  2325. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2326. * closer to what we're really going to want to ouse.
  2327. */
  2328. return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2329. }
  2330. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2331. struct btrfs_root *root)
  2332. {
  2333. struct btrfs_block_rsv *global_rsv;
  2334. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2335. u64 num_bytes;
  2336. int ret = 0;
  2337. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2338. num_heads = heads_to_leaves(root, num_heads);
  2339. if (num_heads > 1)
  2340. num_bytes += (num_heads - 1) * root->leafsize;
  2341. num_bytes <<= 1;
  2342. global_rsv = &root->fs_info->global_block_rsv;
  2343. /*
  2344. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2345. * wiggle room since running delayed refs can create more delayed refs.
  2346. */
  2347. if (global_rsv->space_info->full)
  2348. num_bytes <<= 1;
  2349. spin_lock(&global_rsv->lock);
  2350. if (global_rsv->reserved <= num_bytes)
  2351. ret = 1;
  2352. spin_unlock(&global_rsv->lock);
  2353. return ret;
  2354. }
  2355. /*
  2356. * this starts processing the delayed reference count updates and
  2357. * extent insertions we have queued up so far. count can be
  2358. * 0, which means to process everything in the tree at the start
  2359. * of the run (but not newly added entries), or it can be some target
  2360. * number you'd like to process.
  2361. *
  2362. * Returns 0 on success or if called with an aborted transaction
  2363. * Returns <0 on error and aborts the transaction
  2364. */
  2365. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2366. struct btrfs_root *root, unsigned long count)
  2367. {
  2368. struct rb_node *node;
  2369. struct btrfs_delayed_ref_root *delayed_refs;
  2370. struct btrfs_delayed_ref_head *head;
  2371. int ret;
  2372. int run_all = count == (unsigned long)-1;
  2373. int run_most = 0;
  2374. /* We'll clean this up in btrfs_cleanup_transaction */
  2375. if (trans->aborted)
  2376. return 0;
  2377. if (root == root->fs_info->extent_root)
  2378. root = root->fs_info->tree_root;
  2379. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  2380. delayed_refs = &trans->transaction->delayed_refs;
  2381. if (count == 0) {
  2382. count = atomic_read(&delayed_refs->num_entries) * 2;
  2383. run_most = 1;
  2384. }
  2385. again:
  2386. #ifdef SCRAMBLE_DELAYED_REFS
  2387. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2388. #endif
  2389. ret = __btrfs_run_delayed_refs(trans, root, count);
  2390. if (ret < 0) {
  2391. btrfs_abort_transaction(trans, root, ret);
  2392. return ret;
  2393. }
  2394. if (run_all) {
  2395. if (!list_empty(&trans->new_bgs))
  2396. btrfs_create_pending_block_groups(trans, root);
  2397. spin_lock(&delayed_refs->lock);
  2398. node = rb_first(&delayed_refs->href_root);
  2399. if (!node) {
  2400. spin_unlock(&delayed_refs->lock);
  2401. goto out;
  2402. }
  2403. count = (unsigned long)-1;
  2404. while (node) {
  2405. head = rb_entry(node, struct btrfs_delayed_ref_head,
  2406. href_node);
  2407. if (btrfs_delayed_ref_is_head(&head->node)) {
  2408. struct btrfs_delayed_ref_node *ref;
  2409. ref = &head->node;
  2410. atomic_inc(&ref->refs);
  2411. spin_unlock(&delayed_refs->lock);
  2412. /*
  2413. * Mutex was contended, block until it's
  2414. * released and try again
  2415. */
  2416. mutex_lock(&head->mutex);
  2417. mutex_unlock(&head->mutex);
  2418. btrfs_put_delayed_ref(ref);
  2419. cond_resched();
  2420. goto again;
  2421. } else {
  2422. WARN_ON(1);
  2423. }
  2424. node = rb_next(node);
  2425. }
  2426. spin_unlock(&delayed_refs->lock);
  2427. cond_resched();
  2428. goto again;
  2429. }
  2430. out:
  2431. assert_qgroups_uptodate(trans);
  2432. return 0;
  2433. }
  2434. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2435. struct btrfs_root *root,
  2436. u64 bytenr, u64 num_bytes, u64 flags,
  2437. int level, int is_data)
  2438. {
  2439. struct btrfs_delayed_extent_op *extent_op;
  2440. int ret;
  2441. extent_op = btrfs_alloc_delayed_extent_op();
  2442. if (!extent_op)
  2443. return -ENOMEM;
  2444. extent_op->flags_to_set = flags;
  2445. extent_op->update_flags = 1;
  2446. extent_op->update_key = 0;
  2447. extent_op->is_data = is_data ? 1 : 0;
  2448. extent_op->level = level;
  2449. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2450. num_bytes, extent_op);
  2451. if (ret)
  2452. btrfs_free_delayed_extent_op(extent_op);
  2453. return ret;
  2454. }
  2455. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2456. struct btrfs_root *root,
  2457. struct btrfs_path *path,
  2458. u64 objectid, u64 offset, u64 bytenr)
  2459. {
  2460. struct btrfs_delayed_ref_head *head;
  2461. struct btrfs_delayed_ref_node *ref;
  2462. struct btrfs_delayed_data_ref *data_ref;
  2463. struct btrfs_delayed_ref_root *delayed_refs;
  2464. struct rb_node *node;
  2465. int ret = 0;
  2466. delayed_refs = &trans->transaction->delayed_refs;
  2467. spin_lock(&delayed_refs->lock);
  2468. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2469. if (!head) {
  2470. spin_unlock(&delayed_refs->lock);
  2471. return 0;
  2472. }
  2473. if (!mutex_trylock(&head->mutex)) {
  2474. atomic_inc(&head->node.refs);
  2475. spin_unlock(&delayed_refs->lock);
  2476. btrfs_release_path(path);
  2477. /*
  2478. * Mutex was contended, block until it's released and let
  2479. * caller try again
  2480. */
  2481. mutex_lock(&head->mutex);
  2482. mutex_unlock(&head->mutex);
  2483. btrfs_put_delayed_ref(&head->node);
  2484. return -EAGAIN;
  2485. }
  2486. spin_unlock(&delayed_refs->lock);
  2487. spin_lock(&head->lock);
  2488. node = rb_first(&head->ref_root);
  2489. while (node) {
  2490. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2491. node = rb_next(node);
  2492. /* If it's a shared ref we know a cross reference exists */
  2493. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
  2494. ret = 1;
  2495. break;
  2496. }
  2497. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2498. /*
  2499. * If our ref doesn't match the one we're currently looking at
  2500. * then we have a cross reference.
  2501. */
  2502. if (data_ref->root != root->root_key.objectid ||
  2503. data_ref->objectid != objectid ||
  2504. data_ref->offset != offset) {
  2505. ret = 1;
  2506. break;
  2507. }
  2508. }
  2509. spin_unlock(&head->lock);
  2510. mutex_unlock(&head->mutex);
  2511. return ret;
  2512. }
  2513. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2514. struct btrfs_root *root,
  2515. struct btrfs_path *path,
  2516. u64 objectid, u64 offset, u64 bytenr)
  2517. {
  2518. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2519. struct extent_buffer *leaf;
  2520. struct btrfs_extent_data_ref *ref;
  2521. struct btrfs_extent_inline_ref *iref;
  2522. struct btrfs_extent_item *ei;
  2523. struct btrfs_key key;
  2524. u32 item_size;
  2525. int ret;
  2526. key.objectid = bytenr;
  2527. key.offset = (u64)-1;
  2528. key.type = BTRFS_EXTENT_ITEM_KEY;
  2529. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2530. if (ret < 0)
  2531. goto out;
  2532. BUG_ON(ret == 0); /* Corruption */
  2533. ret = -ENOENT;
  2534. if (path->slots[0] == 0)
  2535. goto out;
  2536. path->slots[0]--;
  2537. leaf = path->nodes[0];
  2538. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2539. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2540. goto out;
  2541. ret = 1;
  2542. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2543. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2544. if (item_size < sizeof(*ei)) {
  2545. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2546. goto out;
  2547. }
  2548. #endif
  2549. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2550. if (item_size != sizeof(*ei) +
  2551. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2552. goto out;
  2553. if (btrfs_extent_generation(leaf, ei) <=
  2554. btrfs_root_last_snapshot(&root->root_item))
  2555. goto out;
  2556. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2557. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2558. BTRFS_EXTENT_DATA_REF_KEY)
  2559. goto out;
  2560. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2561. if (btrfs_extent_refs(leaf, ei) !=
  2562. btrfs_extent_data_ref_count(leaf, ref) ||
  2563. btrfs_extent_data_ref_root(leaf, ref) !=
  2564. root->root_key.objectid ||
  2565. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2566. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2567. goto out;
  2568. ret = 0;
  2569. out:
  2570. return ret;
  2571. }
  2572. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2573. struct btrfs_root *root,
  2574. u64 objectid, u64 offset, u64 bytenr)
  2575. {
  2576. struct btrfs_path *path;
  2577. int ret;
  2578. int ret2;
  2579. path = btrfs_alloc_path();
  2580. if (!path)
  2581. return -ENOENT;
  2582. do {
  2583. ret = check_committed_ref(trans, root, path, objectid,
  2584. offset, bytenr);
  2585. if (ret && ret != -ENOENT)
  2586. goto out;
  2587. ret2 = check_delayed_ref(trans, root, path, objectid,
  2588. offset, bytenr);
  2589. } while (ret2 == -EAGAIN);
  2590. if (ret2 && ret2 != -ENOENT) {
  2591. ret = ret2;
  2592. goto out;
  2593. }
  2594. if (ret != -ENOENT || ret2 != -ENOENT)
  2595. ret = 0;
  2596. out:
  2597. btrfs_free_path(path);
  2598. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2599. WARN_ON(ret > 0);
  2600. return ret;
  2601. }
  2602. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2603. struct btrfs_root *root,
  2604. struct extent_buffer *buf,
  2605. int full_backref, int inc, int for_cow)
  2606. {
  2607. u64 bytenr;
  2608. u64 num_bytes;
  2609. u64 parent;
  2610. u64 ref_root;
  2611. u32 nritems;
  2612. struct btrfs_key key;
  2613. struct btrfs_file_extent_item *fi;
  2614. int i;
  2615. int level;
  2616. int ret = 0;
  2617. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2618. u64, u64, u64, u64, u64, u64, int);
  2619. ref_root = btrfs_header_owner(buf);
  2620. nritems = btrfs_header_nritems(buf);
  2621. level = btrfs_header_level(buf);
  2622. if (!root->ref_cows && level == 0)
  2623. return 0;
  2624. if (inc)
  2625. process_func = btrfs_inc_extent_ref;
  2626. else
  2627. process_func = btrfs_free_extent;
  2628. if (full_backref)
  2629. parent = buf->start;
  2630. else
  2631. parent = 0;
  2632. for (i = 0; i < nritems; i++) {
  2633. if (level == 0) {
  2634. btrfs_item_key_to_cpu(buf, &key, i);
  2635. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2636. continue;
  2637. fi = btrfs_item_ptr(buf, i,
  2638. struct btrfs_file_extent_item);
  2639. if (btrfs_file_extent_type(buf, fi) ==
  2640. BTRFS_FILE_EXTENT_INLINE)
  2641. continue;
  2642. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2643. if (bytenr == 0)
  2644. continue;
  2645. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2646. key.offset -= btrfs_file_extent_offset(buf, fi);
  2647. ret = process_func(trans, root, bytenr, num_bytes,
  2648. parent, ref_root, key.objectid,
  2649. key.offset, for_cow);
  2650. if (ret)
  2651. goto fail;
  2652. } else {
  2653. bytenr = btrfs_node_blockptr(buf, i);
  2654. num_bytes = btrfs_level_size(root, level - 1);
  2655. ret = process_func(trans, root, bytenr, num_bytes,
  2656. parent, ref_root, level - 1, 0,
  2657. for_cow);
  2658. if (ret)
  2659. goto fail;
  2660. }
  2661. }
  2662. return 0;
  2663. fail:
  2664. return ret;
  2665. }
  2666. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2667. struct extent_buffer *buf, int full_backref, int for_cow)
  2668. {
  2669. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2670. }
  2671. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2672. struct extent_buffer *buf, int full_backref, int for_cow)
  2673. {
  2674. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2675. }
  2676. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2677. struct btrfs_root *root,
  2678. struct btrfs_path *path,
  2679. struct btrfs_block_group_cache *cache)
  2680. {
  2681. int ret;
  2682. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2683. unsigned long bi;
  2684. struct extent_buffer *leaf;
  2685. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2686. if (ret < 0)
  2687. goto fail;
  2688. BUG_ON(ret); /* Corruption */
  2689. leaf = path->nodes[0];
  2690. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2691. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2692. btrfs_mark_buffer_dirty(leaf);
  2693. btrfs_release_path(path);
  2694. fail:
  2695. if (ret) {
  2696. btrfs_abort_transaction(trans, root, ret);
  2697. return ret;
  2698. }
  2699. return 0;
  2700. }
  2701. static struct btrfs_block_group_cache *
  2702. next_block_group(struct btrfs_root *root,
  2703. struct btrfs_block_group_cache *cache)
  2704. {
  2705. struct rb_node *node;
  2706. spin_lock(&root->fs_info->block_group_cache_lock);
  2707. node = rb_next(&cache->cache_node);
  2708. btrfs_put_block_group(cache);
  2709. if (node) {
  2710. cache = rb_entry(node, struct btrfs_block_group_cache,
  2711. cache_node);
  2712. btrfs_get_block_group(cache);
  2713. } else
  2714. cache = NULL;
  2715. spin_unlock(&root->fs_info->block_group_cache_lock);
  2716. return cache;
  2717. }
  2718. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2719. struct btrfs_trans_handle *trans,
  2720. struct btrfs_path *path)
  2721. {
  2722. struct btrfs_root *root = block_group->fs_info->tree_root;
  2723. struct inode *inode = NULL;
  2724. u64 alloc_hint = 0;
  2725. int dcs = BTRFS_DC_ERROR;
  2726. int num_pages = 0;
  2727. int retries = 0;
  2728. int ret = 0;
  2729. /*
  2730. * If this block group is smaller than 100 megs don't bother caching the
  2731. * block group.
  2732. */
  2733. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2734. spin_lock(&block_group->lock);
  2735. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2736. spin_unlock(&block_group->lock);
  2737. return 0;
  2738. }
  2739. again:
  2740. inode = lookup_free_space_inode(root, block_group, path);
  2741. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2742. ret = PTR_ERR(inode);
  2743. btrfs_release_path(path);
  2744. goto out;
  2745. }
  2746. if (IS_ERR(inode)) {
  2747. BUG_ON(retries);
  2748. retries++;
  2749. if (block_group->ro)
  2750. goto out_free;
  2751. ret = create_free_space_inode(root, trans, block_group, path);
  2752. if (ret)
  2753. goto out_free;
  2754. goto again;
  2755. }
  2756. /* We've already setup this transaction, go ahead and exit */
  2757. if (block_group->cache_generation == trans->transid &&
  2758. i_size_read(inode)) {
  2759. dcs = BTRFS_DC_SETUP;
  2760. goto out_put;
  2761. }
  2762. /*
  2763. * We want to set the generation to 0, that way if anything goes wrong
  2764. * from here on out we know not to trust this cache when we load up next
  2765. * time.
  2766. */
  2767. BTRFS_I(inode)->generation = 0;
  2768. ret = btrfs_update_inode(trans, root, inode);
  2769. WARN_ON(ret);
  2770. if (i_size_read(inode) > 0) {
  2771. ret = btrfs_check_trunc_cache_free_space(root,
  2772. &root->fs_info->global_block_rsv);
  2773. if (ret)
  2774. goto out_put;
  2775. ret = btrfs_truncate_free_space_cache(root, trans, inode);
  2776. if (ret)
  2777. goto out_put;
  2778. }
  2779. spin_lock(&block_group->lock);
  2780. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2781. !btrfs_test_opt(root, SPACE_CACHE)) {
  2782. /*
  2783. * don't bother trying to write stuff out _if_
  2784. * a) we're not cached,
  2785. * b) we're with nospace_cache mount option.
  2786. */
  2787. dcs = BTRFS_DC_WRITTEN;
  2788. spin_unlock(&block_group->lock);
  2789. goto out_put;
  2790. }
  2791. spin_unlock(&block_group->lock);
  2792. /*
  2793. * Try to preallocate enough space based on how big the block group is.
  2794. * Keep in mind this has to include any pinned space which could end up
  2795. * taking up quite a bit since it's not folded into the other space
  2796. * cache.
  2797. */
  2798. num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
  2799. if (!num_pages)
  2800. num_pages = 1;
  2801. num_pages *= 16;
  2802. num_pages *= PAGE_CACHE_SIZE;
  2803. ret = btrfs_check_data_free_space(inode, num_pages);
  2804. if (ret)
  2805. goto out_put;
  2806. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2807. num_pages, num_pages,
  2808. &alloc_hint);
  2809. if (!ret)
  2810. dcs = BTRFS_DC_SETUP;
  2811. btrfs_free_reserved_data_space(inode, num_pages);
  2812. out_put:
  2813. iput(inode);
  2814. out_free:
  2815. btrfs_release_path(path);
  2816. out:
  2817. spin_lock(&block_group->lock);
  2818. if (!ret && dcs == BTRFS_DC_SETUP)
  2819. block_group->cache_generation = trans->transid;
  2820. block_group->disk_cache_state = dcs;
  2821. spin_unlock(&block_group->lock);
  2822. return ret;
  2823. }
  2824. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2825. struct btrfs_root *root)
  2826. {
  2827. struct btrfs_block_group_cache *cache;
  2828. int err = 0;
  2829. struct btrfs_path *path;
  2830. u64 last = 0;
  2831. path = btrfs_alloc_path();
  2832. if (!path)
  2833. return -ENOMEM;
  2834. again:
  2835. while (1) {
  2836. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2837. while (cache) {
  2838. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2839. break;
  2840. cache = next_block_group(root, cache);
  2841. }
  2842. if (!cache) {
  2843. if (last == 0)
  2844. break;
  2845. last = 0;
  2846. continue;
  2847. }
  2848. err = cache_save_setup(cache, trans, path);
  2849. last = cache->key.objectid + cache->key.offset;
  2850. btrfs_put_block_group(cache);
  2851. }
  2852. while (1) {
  2853. if (last == 0) {
  2854. err = btrfs_run_delayed_refs(trans, root,
  2855. (unsigned long)-1);
  2856. if (err) /* File system offline */
  2857. goto out;
  2858. }
  2859. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2860. while (cache) {
  2861. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2862. btrfs_put_block_group(cache);
  2863. goto again;
  2864. }
  2865. if (cache->dirty)
  2866. break;
  2867. cache = next_block_group(root, cache);
  2868. }
  2869. if (!cache) {
  2870. if (last == 0)
  2871. break;
  2872. last = 0;
  2873. continue;
  2874. }
  2875. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2876. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2877. cache->dirty = 0;
  2878. last = cache->key.objectid + cache->key.offset;
  2879. err = write_one_cache_group(trans, root, path, cache);
  2880. btrfs_put_block_group(cache);
  2881. if (err) /* File system offline */
  2882. goto out;
  2883. }
  2884. while (1) {
  2885. /*
  2886. * I don't think this is needed since we're just marking our
  2887. * preallocated extent as written, but just in case it can't
  2888. * hurt.
  2889. */
  2890. if (last == 0) {
  2891. err = btrfs_run_delayed_refs(trans, root,
  2892. (unsigned long)-1);
  2893. if (err) /* File system offline */
  2894. goto out;
  2895. }
  2896. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2897. while (cache) {
  2898. /*
  2899. * Really this shouldn't happen, but it could if we
  2900. * couldn't write the entire preallocated extent and
  2901. * splitting the extent resulted in a new block.
  2902. */
  2903. if (cache->dirty) {
  2904. btrfs_put_block_group(cache);
  2905. goto again;
  2906. }
  2907. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2908. break;
  2909. cache = next_block_group(root, cache);
  2910. }
  2911. if (!cache) {
  2912. if (last == 0)
  2913. break;
  2914. last = 0;
  2915. continue;
  2916. }
  2917. err = btrfs_write_out_cache(root, trans, cache, path);
  2918. /*
  2919. * If we didn't have an error then the cache state is still
  2920. * NEED_WRITE, so we can set it to WRITTEN.
  2921. */
  2922. if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2923. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2924. last = cache->key.objectid + cache->key.offset;
  2925. btrfs_put_block_group(cache);
  2926. }
  2927. out:
  2928. btrfs_free_path(path);
  2929. return err;
  2930. }
  2931. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2932. {
  2933. struct btrfs_block_group_cache *block_group;
  2934. int readonly = 0;
  2935. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2936. if (!block_group || block_group->ro)
  2937. readonly = 1;
  2938. if (block_group)
  2939. btrfs_put_block_group(block_group);
  2940. return readonly;
  2941. }
  2942. static const char *alloc_name(u64 flags)
  2943. {
  2944. switch (flags) {
  2945. case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
  2946. return "mixed";
  2947. case BTRFS_BLOCK_GROUP_METADATA:
  2948. return "metadata";
  2949. case BTRFS_BLOCK_GROUP_DATA:
  2950. return "data";
  2951. case BTRFS_BLOCK_GROUP_SYSTEM:
  2952. return "system";
  2953. default:
  2954. WARN_ON(1);
  2955. return "invalid-combination";
  2956. };
  2957. }
  2958. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2959. u64 total_bytes, u64 bytes_used,
  2960. struct btrfs_space_info **space_info)
  2961. {
  2962. struct btrfs_space_info *found;
  2963. int i;
  2964. int factor;
  2965. int ret;
  2966. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2967. BTRFS_BLOCK_GROUP_RAID10))
  2968. factor = 2;
  2969. else
  2970. factor = 1;
  2971. found = __find_space_info(info, flags);
  2972. if (found) {
  2973. spin_lock(&found->lock);
  2974. found->total_bytes += total_bytes;
  2975. found->disk_total += total_bytes * factor;
  2976. found->bytes_used += bytes_used;
  2977. found->disk_used += bytes_used * factor;
  2978. found->full = 0;
  2979. spin_unlock(&found->lock);
  2980. *space_info = found;
  2981. return 0;
  2982. }
  2983. found = kzalloc(sizeof(*found), GFP_NOFS);
  2984. if (!found)
  2985. return -ENOMEM;
  2986. ret = percpu_counter_init(&found->total_bytes_pinned, 0);
  2987. if (ret) {
  2988. kfree(found);
  2989. return ret;
  2990. }
  2991. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  2992. INIT_LIST_HEAD(&found->block_groups[i]);
  2993. kobject_init(&found->block_group_kobjs[i], &btrfs_raid_ktype);
  2994. }
  2995. init_rwsem(&found->groups_sem);
  2996. spin_lock_init(&found->lock);
  2997. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  2998. found->total_bytes = total_bytes;
  2999. found->disk_total = total_bytes * factor;
  3000. found->bytes_used = bytes_used;
  3001. found->disk_used = bytes_used * factor;
  3002. found->bytes_pinned = 0;
  3003. found->bytes_reserved = 0;
  3004. found->bytes_readonly = 0;
  3005. found->bytes_may_use = 0;
  3006. found->full = 0;
  3007. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3008. found->chunk_alloc = 0;
  3009. found->flush = 0;
  3010. init_waitqueue_head(&found->wait);
  3011. ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
  3012. info->space_info_kobj, "%s",
  3013. alloc_name(found->flags));
  3014. if (ret) {
  3015. kfree(found);
  3016. return ret;
  3017. }
  3018. *space_info = found;
  3019. list_add_rcu(&found->list, &info->space_info);
  3020. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3021. info->data_sinfo = found;
  3022. return ret;
  3023. }
  3024. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3025. {
  3026. u64 extra_flags = chunk_to_extended(flags) &
  3027. BTRFS_EXTENDED_PROFILE_MASK;
  3028. write_seqlock(&fs_info->profiles_lock);
  3029. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3030. fs_info->avail_data_alloc_bits |= extra_flags;
  3031. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3032. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3033. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3034. fs_info->avail_system_alloc_bits |= extra_flags;
  3035. write_sequnlock(&fs_info->profiles_lock);
  3036. }
  3037. /*
  3038. * returns target flags in extended format or 0 if restripe for this
  3039. * chunk_type is not in progress
  3040. *
  3041. * should be called with either volume_mutex or balance_lock held
  3042. */
  3043. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3044. {
  3045. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3046. u64 target = 0;
  3047. if (!bctl)
  3048. return 0;
  3049. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3050. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3051. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3052. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3053. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3054. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3055. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3056. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3057. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3058. }
  3059. return target;
  3060. }
  3061. /*
  3062. * @flags: available profiles in extended format (see ctree.h)
  3063. *
  3064. * Returns reduced profile in chunk format. If profile changing is in
  3065. * progress (either running or paused) picks the target profile (if it's
  3066. * already available), otherwise falls back to plain reducing.
  3067. */
  3068. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3069. {
  3070. /*
  3071. * we add in the count of missing devices because we want
  3072. * to make sure that any RAID levels on a degraded FS
  3073. * continue to be honored.
  3074. */
  3075. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  3076. root->fs_info->fs_devices->missing_devices;
  3077. u64 target;
  3078. u64 tmp;
  3079. /*
  3080. * see if restripe for this chunk_type is in progress, if so
  3081. * try to reduce to the target profile
  3082. */
  3083. spin_lock(&root->fs_info->balance_lock);
  3084. target = get_restripe_target(root->fs_info, flags);
  3085. if (target) {
  3086. /* pick target profile only if it's already available */
  3087. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3088. spin_unlock(&root->fs_info->balance_lock);
  3089. return extended_to_chunk(target);
  3090. }
  3091. }
  3092. spin_unlock(&root->fs_info->balance_lock);
  3093. /* First, mask out the RAID levels which aren't possible */
  3094. if (num_devices == 1)
  3095. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3096. BTRFS_BLOCK_GROUP_RAID5);
  3097. if (num_devices < 3)
  3098. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3099. if (num_devices < 4)
  3100. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3101. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3102. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3103. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3104. flags &= ~tmp;
  3105. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3106. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3107. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3108. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3109. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3110. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3111. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3112. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3113. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3114. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3115. return extended_to_chunk(flags | tmp);
  3116. }
  3117. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  3118. {
  3119. unsigned seq;
  3120. do {
  3121. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3122. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3123. flags |= root->fs_info->avail_data_alloc_bits;
  3124. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3125. flags |= root->fs_info->avail_system_alloc_bits;
  3126. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3127. flags |= root->fs_info->avail_metadata_alloc_bits;
  3128. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3129. return btrfs_reduce_alloc_profile(root, flags);
  3130. }
  3131. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3132. {
  3133. u64 flags;
  3134. u64 ret;
  3135. if (data)
  3136. flags = BTRFS_BLOCK_GROUP_DATA;
  3137. else if (root == root->fs_info->chunk_root)
  3138. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3139. else
  3140. flags = BTRFS_BLOCK_GROUP_METADATA;
  3141. ret = get_alloc_profile(root, flags);
  3142. return ret;
  3143. }
  3144. /*
  3145. * This will check the space that the inode allocates from to make sure we have
  3146. * enough space for bytes.
  3147. */
  3148. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  3149. {
  3150. struct btrfs_space_info *data_sinfo;
  3151. struct btrfs_root *root = BTRFS_I(inode)->root;
  3152. struct btrfs_fs_info *fs_info = root->fs_info;
  3153. u64 used;
  3154. int ret = 0, committed = 0, alloc_chunk = 1;
  3155. /* make sure bytes are sectorsize aligned */
  3156. bytes = ALIGN(bytes, root->sectorsize);
  3157. if (btrfs_is_free_space_inode(inode)) {
  3158. committed = 1;
  3159. ASSERT(current->journal_info);
  3160. }
  3161. data_sinfo = fs_info->data_sinfo;
  3162. if (!data_sinfo)
  3163. goto alloc;
  3164. again:
  3165. /* make sure we have enough space to handle the data first */
  3166. spin_lock(&data_sinfo->lock);
  3167. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3168. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3169. data_sinfo->bytes_may_use;
  3170. if (used + bytes > data_sinfo->total_bytes) {
  3171. struct btrfs_trans_handle *trans;
  3172. /*
  3173. * if we don't have enough free bytes in this space then we need
  3174. * to alloc a new chunk.
  3175. */
  3176. if (!data_sinfo->full && alloc_chunk) {
  3177. u64 alloc_target;
  3178. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3179. spin_unlock(&data_sinfo->lock);
  3180. alloc:
  3181. alloc_target = btrfs_get_alloc_profile(root, 1);
  3182. /*
  3183. * It is ugly that we don't call nolock join
  3184. * transaction for the free space inode case here.
  3185. * But it is safe because we only do the data space
  3186. * reservation for the free space cache in the
  3187. * transaction context, the common join transaction
  3188. * just increase the counter of the current transaction
  3189. * handler, doesn't try to acquire the trans_lock of
  3190. * the fs.
  3191. */
  3192. trans = btrfs_join_transaction(root);
  3193. if (IS_ERR(trans))
  3194. return PTR_ERR(trans);
  3195. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3196. alloc_target,
  3197. CHUNK_ALLOC_NO_FORCE);
  3198. btrfs_end_transaction(trans, root);
  3199. if (ret < 0) {
  3200. if (ret != -ENOSPC)
  3201. return ret;
  3202. else
  3203. goto commit_trans;
  3204. }
  3205. if (!data_sinfo)
  3206. data_sinfo = fs_info->data_sinfo;
  3207. goto again;
  3208. }
  3209. /*
  3210. * If we don't have enough pinned space to deal with this
  3211. * allocation don't bother committing the transaction.
  3212. */
  3213. if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
  3214. bytes) < 0)
  3215. committed = 1;
  3216. spin_unlock(&data_sinfo->lock);
  3217. /* commit the current transaction and try again */
  3218. commit_trans:
  3219. if (!committed &&
  3220. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3221. committed = 1;
  3222. trans = btrfs_join_transaction(root);
  3223. if (IS_ERR(trans))
  3224. return PTR_ERR(trans);
  3225. ret = btrfs_commit_transaction(trans, root);
  3226. if (ret)
  3227. return ret;
  3228. goto again;
  3229. }
  3230. trace_btrfs_space_reservation(root->fs_info,
  3231. "space_info:enospc",
  3232. data_sinfo->flags, bytes, 1);
  3233. return -ENOSPC;
  3234. }
  3235. data_sinfo->bytes_may_use += bytes;
  3236. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3237. data_sinfo->flags, bytes, 1);
  3238. spin_unlock(&data_sinfo->lock);
  3239. return 0;
  3240. }
  3241. /*
  3242. * Called if we need to clear a data reservation for this inode.
  3243. */
  3244. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3245. {
  3246. struct btrfs_root *root = BTRFS_I(inode)->root;
  3247. struct btrfs_space_info *data_sinfo;
  3248. /* make sure bytes are sectorsize aligned */
  3249. bytes = ALIGN(bytes, root->sectorsize);
  3250. data_sinfo = root->fs_info->data_sinfo;
  3251. spin_lock(&data_sinfo->lock);
  3252. WARN_ON(data_sinfo->bytes_may_use < bytes);
  3253. data_sinfo->bytes_may_use -= bytes;
  3254. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3255. data_sinfo->flags, bytes, 0);
  3256. spin_unlock(&data_sinfo->lock);
  3257. }
  3258. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3259. {
  3260. struct list_head *head = &info->space_info;
  3261. struct btrfs_space_info *found;
  3262. rcu_read_lock();
  3263. list_for_each_entry_rcu(found, head, list) {
  3264. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3265. found->force_alloc = CHUNK_ALLOC_FORCE;
  3266. }
  3267. rcu_read_unlock();
  3268. }
  3269. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3270. {
  3271. return (global->size << 1);
  3272. }
  3273. static int should_alloc_chunk(struct btrfs_root *root,
  3274. struct btrfs_space_info *sinfo, int force)
  3275. {
  3276. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3277. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3278. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3279. u64 thresh;
  3280. if (force == CHUNK_ALLOC_FORCE)
  3281. return 1;
  3282. /*
  3283. * We need to take into account the global rsv because for all intents
  3284. * and purposes it's used space. Don't worry about locking the
  3285. * global_rsv, it doesn't change except when the transaction commits.
  3286. */
  3287. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3288. num_allocated += calc_global_rsv_need_space(global_rsv);
  3289. /*
  3290. * in limited mode, we want to have some free space up to
  3291. * about 1% of the FS size.
  3292. */
  3293. if (force == CHUNK_ALLOC_LIMITED) {
  3294. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3295. thresh = max_t(u64, 64 * 1024 * 1024,
  3296. div_factor_fine(thresh, 1));
  3297. if (num_bytes - num_allocated < thresh)
  3298. return 1;
  3299. }
  3300. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3301. return 0;
  3302. return 1;
  3303. }
  3304. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3305. {
  3306. u64 num_dev;
  3307. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3308. BTRFS_BLOCK_GROUP_RAID0 |
  3309. BTRFS_BLOCK_GROUP_RAID5 |
  3310. BTRFS_BLOCK_GROUP_RAID6))
  3311. num_dev = root->fs_info->fs_devices->rw_devices;
  3312. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3313. num_dev = 2;
  3314. else
  3315. num_dev = 1; /* DUP or single */
  3316. /* metadata for updaing devices and chunk tree */
  3317. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3318. }
  3319. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3320. struct btrfs_root *root, u64 type)
  3321. {
  3322. struct btrfs_space_info *info;
  3323. u64 left;
  3324. u64 thresh;
  3325. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3326. spin_lock(&info->lock);
  3327. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3328. info->bytes_reserved - info->bytes_readonly;
  3329. spin_unlock(&info->lock);
  3330. thresh = get_system_chunk_thresh(root, type);
  3331. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3332. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3333. left, thresh, type);
  3334. dump_space_info(info, 0, 0);
  3335. }
  3336. if (left < thresh) {
  3337. u64 flags;
  3338. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3339. btrfs_alloc_chunk(trans, root, flags);
  3340. }
  3341. }
  3342. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3343. struct btrfs_root *extent_root, u64 flags, int force)
  3344. {
  3345. struct btrfs_space_info *space_info;
  3346. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3347. int wait_for_alloc = 0;
  3348. int ret = 0;
  3349. /* Don't re-enter if we're already allocating a chunk */
  3350. if (trans->allocating_chunk)
  3351. return -ENOSPC;
  3352. space_info = __find_space_info(extent_root->fs_info, flags);
  3353. if (!space_info) {
  3354. ret = update_space_info(extent_root->fs_info, flags,
  3355. 0, 0, &space_info);
  3356. BUG_ON(ret); /* -ENOMEM */
  3357. }
  3358. BUG_ON(!space_info); /* Logic error */
  3359. again:
  3360. spin_lock(&space_info->lock);
  3361. if (force < space_info->force_alloc)
  3362. force = space_info->force_alloc;
  3363. if (space_info->full) {
  3364. if (should_alloc_chunk(extent_root, space_info, force))
  3365. ret = -ENOSPC;
  3366. else
  3367. ret = 0;
  3368. spin_unlock(&space_info->lock);
  3369. return ret;
  3370. }
  3371. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3372. spin_unlock(&space_info->lock);
  3373. return 0;
  3374. } else if (space_info->chunk_alloc) {
  3375. wait_for_alloc = 1;
  3376. } else {
  3377. space_info->chunk_alloc = 1;
  3378. }
  3379. spin_unlock(&space_info->lock);
  3380. mutex_lock(&fs_info->chunk_mutex);
  3381. /*
  3382. * The chunk_mutex is held throughout the entirety of a chunk
  3383. * allocation, so once we've acquired the chunk_mutex we know that the
  3384. * other guy is done and we need to recheck and see if we should
  3385. * allocate.
  3386. */
  3387. if (wait_for_alloc) {
  3388. mutex_unlock(&fs_info->chunk_mutex);
  3389. wait_for_alloc = 0;
  3390. goto again;
  3391. }
  3392. trans->allocating_chunk = true;
  3393. /*
  3394. * If we have mixed data/metadata chunks we want to make sure we keep
  3395. * allocating mixed chunks instead of individual chunks.
  3396. */
  3397. if (btrfs_mixed_space_info(space_info))
  3398. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3399. /*
  3400. * if we're doing a data chunk, go ahead and make sure that
  3401. * we keep a reasonable number of metadata chunks allocated in the
  3402. * FS as well.
  3403. */
  3404. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3405. fs_info->data_chunk_allocations++;
  3406. if (!(fs_info->data_chunk_allocations %
  3407. fs_info->metadata_ratio))
  3408. force_metadata_allocation(fs_info);
  3409. }
  3410. /*
  3411. * Check if we have enough space in SYSTEM chunk because we may need
  3412. * to update devices.
  3413. */
  3414. check_system_chunk(trans, extent_root, flags);
  3415. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3416. trans->allocating_chunk = false;
  3417. spin_lock(&space_info->lock);
  3418. if (ret < 0 && ret != -ENOSPC)
  3419. goto out;
  3420. if (ret)
  3421. space_info->full = 1;
  3422. else
  3423. ret = 1;
  3424. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3425. out:
  3426. space_info->chunk_alloc = 0;
  3427. spin_unlock(&space_info->lock);
  3428. mutex_unlock(&fs_info->chunk_mutex);
  3429. return ret;
  3430. }
  3431. static int can_overcommit(struct btrfs_root *root,
  3432. struct btrfs_space_info *space_info, u64 bytes,
  3433. enum btrfs_reserve_flush_enum flush)
  3434. {
  3435. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3436. u64 profile = btrfs_get_alloc_profile(root, 0);
  3437. u64 space_size;
  3438. u64 avail;
  3439. u64 used;
  3440. used = space_info->bytes_used + space_info->bytes_reserved +
  3441. space_info->bytes_pinned + space_info->bytes_readonly;
  3442. /*
  3443. * We only want to allow over committing if we have lots of actual space
  3444. * free, but if we don't have enough space to handle the global reserve
  3445. * space then we could end up having a real enospc problem when trying
  3446. * to allocate a chunk or some other such important allocation.
  3447. */
  3448. spin_lock(&global_rsv->lock);
  3449. space_size = calc_global_rsv_need_space(global_rsv);
  3450. spin_unlock(&global_rsv->lock);
  3451. if (used + space_size >= space_info->total_bytes)
  3452. return 0;
  3453. used += space_info->bytes_may_use;
  3454. spin_lock(&root->fs_info->free_chunk_lock);
  3455. avail = root->fs_info->free_chunk_space;
  3456. spin_unlock(&root->fs_info->free_chunk_lock);
  3457. /*
  3458. * If we have dup, raid1 or raid10 then only half of the free
  3459. * space is actually useable. For raid56, the space info used
  3460. * doesn't include the parity drive, so we don't have to
  3461. * change the math
  3462. */
  3463. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3464. BTRFS_BLOCK_GROUP_RAID1 |
  3465. BTRFS_BLOCK_GROUP_RAID10))
  3466. avail >>= 1;
  3467. /*
  3468. * If we aren't flushing all things, let us overcommit up to
  3469. * 1/2th of the space. If we can flush, don't let us overcommit
  3470. * too much, let it overcommit up to 1/8 of the space.
  3471. */
  3472. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3473. avail >>= 3;
  3474. else
  3475. avail >>= 1;
  3476. if (used + bytes < space_info->total_bytes + avail)
  3477. return 1;
  3478. return 0;
  3479. }
  3480. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3481. unsigned long nr_pages)
  3482. {
  3483. struct super_block *sb = root->fs_info->sb;
  3484. if (down_read_trylock(&sb->s_umount)) {
  3485. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  3486. up_read(&sb->s_umount);
  3487. } else {
  3488. /*
  3489. * We needn't worry the filesystem going from r/w to r/o though
  3490. * we don't acquire ->s_umount mutex, because the filesystem
  3491. * should guarantee the delalloc inodes list be empty after
  3492. * the filesystem is readonly(all dirty pages are written to
  3493. * the disk).
  3494. */
  3495. btrfs_start_delalloc_roots(root->fs_info, 0);
  3496. if (!current->journal_info)
  3497. btrfs_wait_ordered_roots(root->fs_info, -1);
  3498. }
  3499. }
  3500. static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
  3501. {
  3502. u64 bytes;
  3503. int nr;
  3504. bytes = btrfs_calc_trans_metadata_size(root, 1);
  3505. nr = (int)div64_u64(to_reclaim, bytes);
  3506. if (!nr)
  3507. nr = 1;
  3508. return nr;
  3509. }
  3510. #define EXTENT_SIZE_PER_ITEM (256 * 1024)
  3511. /*
  3512. * shrink metadata reservation for delalloc
  3513. */
  3514. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3515. bool wait_ordered)
  3516. {
  3517. struct btrfs_block_rsv *block_rsv;
  3518. struct btrfs_space_info *space_info;
  3519. struct btrfs_trans_handle *trans;
  3520. u64 delalloc_bytes;
  3521. u64 max_reclaim;
  3522. long time_left;
  3523. unsigned long nr_pages;
  3524. int loops;
  3525. int items;
  3526. enum btrfs_reserve_flush_enum flush;
  3527. /* Calc the number of the pages we need flush for space reservation */
  3528. items = calc_reclaim_items_nr(root, to_reclaim);
  3529. to_reclaim = items * EXTENT_SIZE_PER_ITEM;
  3530. trans = (struct btrfs_trans_handle *)current->journal_info;
  3531. block_rsv = &root->fs_info->delalloc_block_rsv;
  3532. space_info = block_rsv->space_info;
  3533. delalloc_bytes = percpu_counter_sum_positive(
  3534. &root->fs_info->delalloc_bytes);
  3535. if (delalloc_bytes == 0) {
  3536. if (trans)
  3537. return;
  3538. if (wait_ordered)
  3539. btrfs_wait_ordered_roots(root->fs_info, items);
  3540. return;
  3541. }
  3542. loops = 0;
  3543. while (delalloc_bytes && loops < 3) {
  3544. max_reclaim = min(delalloc_bytes, to_reclaim);
  3545. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3546. btrfs_writeback_inodes_sb_nr(root, nr_pages);
  3547. /*
  3548. * We need to wait for the async pages to actually start before
  3549. * we do anything.
  3550. */
  3551. max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
  3552. if (!max_reclaim)
  3553. goto skip_async;
  3554. if (max_reclaim <= nr_pages)
  3555. max_reclaim = 0;
  3556. else
  3557. max_reclaim -= nr_pages;
  3558. wait_event(root->fs_info->async_submit_wait,
  3559. atomic_read(&root->fs_info->async_delalloc_pages) <=
  3560. (int)max_reclaim);
  3561. skip_async:
  3562. if (!trans)
  3563. flush = BTRFS_RESERVE_FLUSH_ALL;
  3564. else
  3565. flush = BTRFS_RESERVE_NO_FLUSH;
  3566. spin_lock(&space_info->lock);
  3567. if (can_overcommit(root, space_info, orig, flush)) {
  3568. spin_unlock(&space_info->lock);
  3569. break;
  3570. }
  3571. spin_unlock(&space_info->lock);
  3572. loops++;
  3573. if (wait_ordered && !trans) {
  3574. btrfs_wait_ordered_roots(root->fs_info, items);
  3575. } else {
  3576. time_left = schedule_timeout_killable(1);
  3577. if (time_left)
  3578. break;
  3579. }
  3580. delalloc_bytes = percpu_counter_sum_positive(
  3581. &root->fs_info->delalloc_bytes);
  3582. }
  3583. }
  3584. /**
  3585. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3586. * @root - the root we're allocating for
  3587. * @bytes - the number of bytes we want to reserve
  3588. * @force - force the commit
  3589. *
  3590. * This will check to make sure that committing the transaction will actually
  3591. * get us somewhere and then commit the transaction if it does. Otherwise it
  3592. * will return -ENOSPC.
  3593. */
  3594. static int may_commit_transaction(struct btrfs_root *root,
  3595. struct btrfs_space_info *space_info,
  3596. u64 bytes, int force)
  3597. {
  3598. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3599. struct btrfs_trans_handle *trans;
  3600. trans = (struct btrfs_trans_handle *)current->journal_info;
  3601. if (trans)
  3602. return -EAGAIN;
  3603. if (force)
  3604. goto commit;
  3605. /* See if there is enough pinned space to make this reservation */
  3606. spin_lock(&space_info->lock);
  3607. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3608. bytes) >= 0) {
  3609. spin_unlock(&space_info->lock);
  3610. goto commit;
  3611. }
  3612. spin_unlock(&space_info->lock);
  3613. /*
  3614. * See if there is some space in the delayed insertion reservation for
  3615. * this reservation.
  3616. */
  3617. if (space_info != delayed_rsv->space_info)
  3618. return -ENOSPC;
  3619. spin_lock(&space_info->lock);
  3620. spin_lock(&delayed_rsv->lock);
  3621. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3622. bytes - delayed_rsv->size) >= 0) {
  3623. spin_unlock(&delayed_rsv->lock);
  3624. spin_unlock(&space_info->lock);
  3625. return -ENOSPC;
  3626. }
  3627. spin_unlock(&delayed_rsv->lock);
  3628. spin_unlock(&space_info->lock);
  3629. commit:
  3630. trans = btrfs_join_transaction(root);
  3631. if (IS_ERR(trans))
  3632. return -ENOSPC;
  3633. return btrfs_commit_transaction(trans, root);
  3634. }
  3635. enum flush_state {
  3636. FLUSH_DELAYED_ITEMS_NR = 1,
  3637. FLUSH_DELAYED_ITEMS = 2,
  3638. FLUSH_DELALLOC = 3,
  3639. FLUSH_DELALLOC_WAIT = 4,
  3640. ALLOC_CHUNK = 5,
  3641. COMMIT_TRANS = 6,
  3642. };
  3643. static int flush_space(struct btrfs_root *root,
  3644. struct btrfs_space_info *space_info, u64 num_bytes,
  3645. u64 orig_bytes, int state)
  3646. {
  3647. struct btrfs_trans_handle *trans;
  3648. int nr;
  3649. int ret = 0;
  3650. switch (state) {
  3651. case FLUSH_DELAYED_ITEMS_NR:
  3652. case FLUSH_DELAYED_ITEMS:
  3653. if (state == FLUSH_DELAYED_ITEMS_NR)
  3654. nr = calc_reclaim_items_nr(root, num_bytes) * 2;
  3655. else
  3656. nr = -1;
  3657. trans = btrfs_join_transaction(root);
  3658. if (IS_ERR(trans)) {
  3659. ret = PTR_ERR(trans);
  3660. break;
  3661. }
  3662. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3663. btrfs_end_transaction(trans, root);
  3664. break;
  3665. case FLUSH_DELALLOC:
  3666. case FLUSH_DELALLOC_WAIT:
  3667. shrink_delalloc(root, num_bytes, orig_bytes,
  3668. state == FLUSH_DELALLOC_WAIT);
  3669. break;
  3670. case ALLOC_CHUNK:
  3671. trans = btrfs_join_transaction(root);
  3672. if (IS_ERR(trans)) {
  3673. ret = PTR_ERR(trans);
  3674. break;
  3675. }
  3676. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3677. btrfs_get_alloc_profile(root, 0),
  3678. CHUNK_ALLOC_NO_FORCE);
  3679. btrfs_end_transaction(trans, root);
  3680. if (ret == -ENOSPC)
  3681. ret = 0;
  3682. break;
  3683. case COMMIT_TRANS:
  3684. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3685. break;
  3686. default:
  3687. ret = -ENOSPC;
  3688. break;
  3689. }
  3690. return ret;
  3691. }
  3692. /**
  3693. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3694. * @root - the root we're allocating for
  3695. * @block_rsv - the block_rsv we're allocating for
  3696. * @orig_bytes - the number of bytes we want
  3697. * @flush - whether or not we can flush to make our reservation
  3698. *
  3699. * This will reserve orgi_bytes number of bytes from the space info associated
  3700. * with the block_rsv. If there is not enough space it will make an attempt to
  3701. * flush out space to make room. It will do this by flushing delalloc if
  3702. * possible or committing the transaction. If flush is 0 then no attempts to
  3703. * regain reservations will be made and this will fail if there is not enough
  3704. * space already.
  3705. */
  3706. static int reserve_metadata_bytes(struct btrfs_root *root,
  3707. struct btrfs_block_rsv *block_rsv,
  3708. u64 orig_bytes,
  3709. enum btrfs_reserve_flush_enum flush)
  3710. {
  3711. struct btrfs_space_info *space_info = block_rsv->space_info;
  3712. u64 used;
  3713. u64 num_bytes = orig_bytes;
  3714. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  3715. int ret = 0;
  3716. bool flushing = false;
  3717. again:
  3718. ret = 0;
  3719. spin_lock(&space_info->lock);
  3720. /*
  3721. * We only want to wait if somebody other than us is flushing and we
  3722. * are actually allowed to flush all things.
  3723. */
  3724. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  3725. space_info->flush) {
  3726. spin_unlock(&space_info->lock);
  3727. /*
  3728. * If we have a trans handle we can't wait because the flusher
  3729. * may have to commit the transaction, which would mean we would
  3730. * deadlock since we are waiting for the flusher to finish, but
  3731. * hold the current transaction open.
  3732. */
  3733. if (current->journal_info)
  3734. return -EAGAIN;
  3735. ret = wait_event_killable(space_info->wait, !space_info->flush);
  3736. /* Must have been killed, return */
  3737. if (ret)
  3738. return -EINTR;
  3739. spin_lock(&space_info->lock);
  3740. }
  3741. ret = -ENOSPC;
  3742. used = space_info->bytes_used + space_info->bytes_reserved +
  3743. space_info->bytes_pinned + space_info->bytes_readonly +
  3744. space_info->bytes_may_use;
  3745. /*
  3746. * The idea here is that we've not already over-reserved the block group
  3747. * then we can go ahead and save our reservation first and then start
  3748. * flushing if we need to. Otherwise if we've already overcommitted
  3749. * lets start flushing stuff first and then come back and try to make
  3750. * our reservation.
  3751. */
  3752. if (used <= space_info->total_bytes) {
  3753. if (used + orig_bytes <= space_info->total_bytes) {
  3754. space_info->bytes_may_use += orig_bytes;
  3755. trace_btrfs_space_reservation(root->fs_info,
  3756. "space_info", space_info->flags, orig_bytes, 1);
  3757. ret = 0;
  3758. } else {
  3759. /*
  3760. * Ok set num_bytes to orig_bytes since we aren't
  3761. * overocmmitted, this way we only try and reclaim what
  3762. * we need.
  3763. */
  3764. num_bytes = orig_bytes;
  3765. }
  3766. } else {
  3767. /*
  3768. * Ok we're over committed, set num_bytes to the overcommitted
  3769. * amount plus the amount of bytes that we need for this
  3770. * reservation.
  3771. */
  3772. num_bytes = used - space_info->total_bytes +
  3773. (orig_bytes * 2);
  3774. }
  3775. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  3776. space_info->bytes_may_use += orig_bytes;
  3777. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3778. space_info->flags, orig_bytes,
  3779. 1);
  3780. ret = 0;
  3781. }
  3782. /*
  3783. * Couldn't make our reservation, save our place so while we're trying
  3784. * to reclaim space we can actually use it instead of somebody else
  3785. * stealing it from us.
  3786. *
  3787. * We make the other tasks wait for the flush only when we can flush
  3788. * all things.
  3789. */
  3790. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  3791. flushing = true;
  3792. space_info->flush = 1;
  3793. }
  3794. spin_unlock(&space_info->lock);
  3795. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  3796. goto out;
  3797. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  3798. flush_state);
  3799. flush_state++;
  3800. /*
  3801. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  3802. * would happen. So skip delalloc flush.
  3803. */
  3804. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3805. (flush_state == FLUSH_DELALLOC ||
  3806. flush_state == FLUSH_DELALLOC_WAIT))
  3807. flush_state = ALLOC_CHUNK;
  3808. if (!ret)
  3809. goto again;
  3810. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3811. flush_state < COMMIT_TRANS)
  3812. goto again;
  3813. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  3814. flush_state <= COMMIT_TRANS)
  3815. goto again;
  3816. out:
  3817. if (ret == -ENOSPC &&
  3818. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  3819. struct btrfs_block_rsv *global_rsv =
  3820. &root->fs_info->global_block_rsv;
  3821. if (block_rsv != global_rsv &&
  3822. !block_rsv_use_bytes(global_rsv, orig_bytes))
  3823. ret = 0;
  3824. }
  3825. if (ret == -ENOSPC)
  3826. trace_btrfs_space_reservation(root->fs_info,
  3827. "space_info:enospc",
  3828. space_info->flags, orig_bytes, 1);
  3829. if (flushing) {
  3830. spin_lock(&space_info->lock);
  3831. space_info->flush = 0;
  3832. wake_up_all(&space_info->wait);
  3833. spin_unlock(&space_info->lock);
  3834. }
  3835. return ret;
  3836. }
  3837. static struct btrfs_block_rsv *get_block_rsv(
  3838. const struct btrfs_trans_handle *trans,
  3839. const struct btrfs_root *root)
  3840. {
  3841. struct btrfs_block_rsv *block_rsv = NULL;
  3842. if (root->ref_cows)
  3843. block_rsv = trans->block_rsv;
  3844. if (root == root->fs_info->csum_root && trans->adding_csums)
  3845. block_rsv = trans->block_rsv;
  3846. if (root == root->fs_info->uuid_root)
  3847. block_rsv = trans->block_rsv;
  3848. if (!block_rsv)
  3849. block_rsv = root->block_rsv;
  3850. if (!block_rsv)
  3851. block_rsv = &root->fs_info->empty_block_rsv;
  3852. return block_rsv;
  3853. }
  3854. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3855. u64 num_bytes)
  3856. {
  3857. int ret = -ENOSPC;
  3858. spin_lock(&block_rsv->lock);
  3859. if (block_rsv->reserved >= num_bytes) {
  3860. block_rsv->reserved -= num_bytes;
  3861. if (block_rsv->reserved < block_rsv->size)
  3862. block_rsv->full = 0;
  3863. ret = 0;
  3864. }
  3865. spin_unlock(&block_rsv->lock);
  3866. return ret;
  3867. }
  3868. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3869. u64 num_bytes, int update_size)
  3870. {
  3871. spin_lock(&block_rsv->lock);
  3872. block_rsv->reserved += num_bytes;
  3873. if (update_size)
  3874. block_rsv->size += num_bytes;
  3875. else if (block_rsv->reserved >= block_rsv->size)
  3876. block_rsv->full = 1;
  3877. spin_unlock(&block_rsv->lock);
  3878. }
  3879. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  3880. struct btrfs_block_rsv *dest, u64 num_bytes,
  3881. int min_factor)
  3882. {
  3883. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  3884. u64 min_bytes;
  3885. if (global_rsv->space_info != dest->space_info)
  3886. return -ENOSPC;
  3887. spin_lock(&global_rsv->lock);
  3888. min_bytes = div_factor(global_rsv->size, min_factor);
  3889. if (global_rsv->reserved < min_bytes + num_bytes) {
  3890. spin_unlock(&global_rsv->lock);
  3891. return -ENOSPC;
  3892. }
  3893. global_rsv->reserved -= num_bytes;
  3894. if (global_rsv->reserved < global_rsv->size)
  3895. global_rsv->full = 0;
  3896. spin_unlock(&global_rsv->lock);
  3897. block_rsv_add_bytes(dest, num_bytes, 1);
  3898. return 0;
  3899. }
  3900. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3901. struct btrfs_block_rsv *block_rsv,
  3902. struct btrfs_block_rsv *dest, u64 num_bytes)
  3903. {
  3904. struct btrfs_space_info *space_info = block_rsv->space_info;
  3905. spin_lock(&block_rsv->lock);
  3906. if (num_bytes == (u64)-1)
  3907. num_bytes = block_rsv->size;
  3908. block_rsv->size -= num_bytes;
  3909. if (block_rsv->reserved >= block_rsv->size) {
  3910. num_bytes = block_rsv->reserved - block_rsv->size;
  3911. block_rsv->reserved = block_rsv->size;
  3912. block_rsv->full = 1;
  3913. } else {
  3914. num_bytes = 0;
  3915. }
  3916. spin_unlock(&block_rsv->lock);
  3917. if (num_bytes > 0) {
  3918. if (dest) {
  3919. spin_lock(&dest->lock);
  3920. if (!dest->full) {
  3921. u64 bytes_to_add;
  3922. bytes_to_add = dest->size - dest->reserved;
  3923. bytes_to_add = min(num_bytes, bytes_to_add);
  3924. dest->reserved += bytes_to_add;
  3925. if (dest->reserved >= dest->size)
  3926. dest->full = 1;
  3927. num_bytes -= bytes_to_add;
  3928. }
  3929. spin_unlock(&dest->lock);
  3930. }
  3931. if (num_bytes) {
  3932. spin_lock(&space_info->lock);
  3933. space_info->bytes_may_use -= num_bytes;
  3934. trace_btrfs_space_reservation(fs_info, "space_info",
  3935. space_info->flags, num_bytes, 0);
  3936. spin_unlock(&space_info->lock);
  3937. }
  3938. }
  3939. }
  3940. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3941. struct btrfs_block_rsv *dst, u64 num_bytes)
  3942. {
  3943. int ret;
  3944. ret = block_rsv_use_bytes(src, num_bytes);
  3945. if (ret)
  3946. return ret;
  3947. block_rsv_add_bytes(dst, num_bytes, 1);
  3948. return 0;
  3949. }
  3950. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  3951. {
  3952. memset(rsv, 0, sizeof(*rsv));
  3953. spin_lock_init(&rsv->lock);
  3954. rsv->type = type;
  3955. }
  3956. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  3957. unsigned short type)
  3958. {
  3959. struct btrfs_block_rsv *block_rsv;
  3960. struct btrfs_fs_info *fs_info = root->fs_info;
  3961. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3962. if (!block_rsv)
  3963. return NULL;
  3964. btrfs_init_block_rsv(block_rsv, type);
  3965. block_rsv->space_info = __find_space_info(fs_info,
  3966. BTRFS_BLOCK_GROUP_METADATA);
  3967. return block_rsv;
  3968. }
  3969. void btrfs_free_block_rsv(struct btrfs_root *root,
  3970. struct btrfs_block_rsv *rsv)
  3971. {
  3972. if (!rsv)
  3973. return;
  3974. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3975. kfree(rsv);
  3976. }
  3977. int btrfs_block_rsv_add(struct btrfs_root *root,
  3978. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  3979. enum btrfs_reserve_flush_enum flush)
  3980. {
  3981. int ret;
  3982. if (num_bytes == 0)
  3983. return 0;
  3984. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3985. if (!ret) {
  3986. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3987. return 0;
  3988. }
  3989. return ret;
  3990. }
  3991. int btrfs_block_rsv_check(struct btrfs_root *root,
  3992. struct btrfs_block_rsv *block_rsv, int min_factor)
  3993. {
  3994. u64 num_bytes = 0;
  3995. int ret = -ENOSPC;
  3996. if (!block_rsv)
  3997. return 0;
  3998. spin_lock(&block_rsv->lock);
  3999. num_bytes = div_factor(block_rsv->size, min_factor);
  4000. if (block_rsv->reserved >= num_bytes)
  4001. ret = 0;
  4002. spin_unlock(&block_rsv->lock);
  4003. return ret;
  4004. }
  4005. int btrfs_block_rsv_refill(struct btrfs_root *root,
  4006. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  4007. enum btrfs_reserve_flush_enum flush)
  4008. {
  4009. u64 num_bytes = 0;
  4010. int ret = -ENOSPC;
  4011. if (!block_rsv)
  4012. return 0;
  4013. spin_lock(&block_rsv->lock);
  4014. num_bytes = min_reserved;
  4015. if (block_rsv->reserved >= num_bytes)
  4016. ret = 0;
  4017. else
  4018. num_bytes -= block_rsv->reserved;
  4019. spin_unlock(&block_rsv->lock);
  4020. if (!ret)
  4021. return 0;
  4022. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4023. if (!ret) {
  4024. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  4025. return 0;
  4026. }
  4027. return ret;
  4028. }
  4029. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  4030. struct btrfs_block_rsv *dst_rsv,
  4031. u64 num_bytes)
  4032. {
  4033. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4034. }
  4035. void btrfs_block_rsv_release(struct btrfs_root *root,
  4036. struct btrfs_block_rsv *block_rsv,
  4037. u64 num_bytes)
  4038. {
  4039. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4040. if (global_rsv == block_rsv ||
  4041. block_rsv->space_info != global_rsv->space_info)
  4042. global_rsv = NULL;
  4043. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4044. num_bytes);
  4045. }
  4046. /*
  4047. * helper to calculate size of global block reservation.
  4048. * the desired value is sum of space used by extent tree,
  4049. * checksum tree and root tree
  4050. */
  4051. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4052. {
  4053. struct btrfs_space_info *sinfo;
  4054. u64 num_bytes;
  4055. u64 meta_used;
  4056. u64 data_used;
  4057. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4058. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4059. spin_lock(&sinfo->lock);
  4060. data_used = sinfo->bytes_used;
  4061. spin_unlock(&sinfo->lock);
  4062. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4063. spin_lock(&sinfo->lock);
  4064. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4065. data_used = 0;
  4066. meta_used = sinfo->bytes_used;
  4067. spin_unlock(&sinfo->lock);
  4068. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4069. csum_size * 2;
  4070. num_bytes += div64_u64(data_used + meta_used, 50);
  4071. if (num_bytes * 3 > meta_used)
  4072. num_bytes = div64_u64(meta_used, 3);
  4073. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  4074. }
  4075. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4076. {
  4077. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4078. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4079. u64 num_bytes;
  4080. num_bytes = calc_global_metadata_size(fs_info);
  4081. spin_lock(&sinfo->lock);
  4082. spin_lock(&block_rsv->lock);
  4083. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4084. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4085. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4086. sinfo->bytes_may_use;
  4087. if (sinfo->total_bytes > num_bytes) {
  4088. num_bytes = sinfo->total_bytes - num_bytes;
  4089. block_rsv->reserved += num_bytes;
  4090. sinfo->bytes_may_use += num_bytes;
  4091. trace_btrfs_space_reservation(fs_info, "space_info",
  4092. sinfo->flags, num_bytes, 1);
  4093. }
  4094. if (block_rsv->reserved >= block_rsv->size) {
  4095. num_bytes = block_rsv->reserved - block_rsv->size;
  4096. sinfo->bytes_may_use -= num_bytes;
  4097. trace_btrfs_space_reservation(fs_info, "space_info",
  4098. sinfo->flags, num_bytes, 0);
  4099. block_rsv->reserved = block_rsv->size;
  4100. block_rsv->full = 1;
  4101. }
  4102. spin_unlock(&block_rsv->lock);
  4103. spin_unlock(&sinfo->lock);
  4104. }
  4105. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4106. {
  4107. struct btrfs_space_info *space_info;
  4108. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4109. fs_info->chunk_block_rsv.space_info = space_info;
  4110. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4111. fs_info->global_block_rsv.space_info = space_info;
  4112. fs_info->delalloc_block_rsv.space_info = space_info;
  4113. fs_info->trans_block_rsv.space_info = space_info;
  4114. fs_info->empty_block_rsv.space_info = space_info;
  4115. fs_info->delayed_block_rsv.space_info = space_info;
  4116. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4117. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4118. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4119. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4120. if (fs_info->quota_root)
  4121. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4122. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4123. update_global_block_rsv(fs_info);
  4124. }
  4125. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4126. {
  4127. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4128. (u64)-1);
  4129. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4130. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4131. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4132. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4133. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4134. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4135. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4136. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4137. }
  4138. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4139. struct btrfs_root *root)
  4140. {
  4141. if (!trans->block_rsv)
  4142. return;
  4143. if (!trans->bytes_reserved)
  4144. return;
  4145. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4146. trans->transid, trans->bytes_reserved, 0);
  4147. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4148. trans->bytes_reserved = 0;
  4149. }
  4150. /* Can only return 0 or -ENOSPC */
  4151. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4152. struct inode *inode)
  4153. {
  4154. struct btrfs_root *root = BTRFS_I(inode)->root;
  4155. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4156. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4157. /*
  4158. * We need to hold space in order to delete our orphan item once we've
  4159. * added it, so this takes the reservation so we can release it later
  4160. * when we are truly done with the orphan item.
  4161. */
  4162. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4163. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4164. btrfs_ino(inode), num_bytes, 1);
  4165. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4166. }
  4167. void btrfs_orphan_release_metadata(struct inode *inode)
  4168. {
  4169. struct btrfs_root *root = BTRFS_I(inode)->root;
  4170. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4171. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4172. btrfs_ino(inode), num_bytes, 0);
  4173. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4174. }
  4175. /*
  4176. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4177. * root: the root of the parent directory
  4178. * rsv: block reservation
  4179. * items: the number of items that we need do reservation
  4180. * qgroup_reserved: used to return the reserved size in qgroup
  4181. *
  4182. * This function is used to reserve the space for snapshot/subvolume
  4183. * creation and deletion. Those operations are different with the
  4184. * common file/directory operations, they change two fs/file trees
  4185. * and root tree, the number of items that the qgroup reserves is
  4186. * different with the free space reservation. So we can not use
  4187. * the space reseravtion mechanism in start_transaction().
  4188. */
  4189. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4190. struct btrfs_block_rsv *rsv,
  4191. int items,
  4192. u64 *qgroup_reserved,
  4193. bool use_global_rsv)
  4194. {
  4195. u64 num_bytes;
  4196. int ret;
  4197. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4198. if (root->fs_info->quota_enabled) {
  4199. /* One for parent inode, two for dir entries */
  4200. num_bytes = 3 * root->leafsize;
  4201. ret = btrfs_qgroup_reserve(root, num_bytes);
  4202. if (ret)
  4203. return ret;
  4204. } else {
  4205. num_bytes = 0;
  4206. }
  4207. *qgroup_reserved = num_bytes;
  4208. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4209. rsv->space_info = __find_space_info(root->fs_info,
  4210. BTRFS_BLOCK_GROUP_METADATA);
  4211. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4212. BTRFS_RESERVE_FLUSH_ALL);
  4213. if (ret == -ENOSPC && use_global_rsv)
  4214. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
  4215. if (ret) {
  4216. if (*qgroup_reserved)
  4217. btrfs_qgroup_free(root, *qgroup_reserved);
  4218. }
  4219. return ret;
  4220. }
  4221. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4222. struct btrfs_block_rsv *rsv,
  4223. u64 qgroup_reserved)
  4224. {
  4225. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4226. if (qgroup_reserved)
  4227. btrfs_qgroup_free(root, qgroup_reserved);
  4228. }
  4229. /**
  4230. * drop_outstanding_extent - drop an outstanding extent
  4231. * @inode: the inode we're dropping the extent for
  4232. *
  4233. * This is called when we are freeing up an outstanding extent, either called
  4234. * after an error or after an extent is written. This will return the number of
  4235. * reserved extents that need to be freed. This must be called with
  4236. * BTRFS_I(inode)->lock held.
  4237. */
  4238. static unsigned drop_outstanding_extent(struct inode *inode)
  4239. {
  4240. unsigned drop_inode_space = 0;
  4241. unsigned dropped_extents = 0;
  4242. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  4243. BTRFS_I(inode)->outstanding_extents--;
  4244. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4245. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4246. &BTRFS_I(inode)->runtime_flags))
  4247. drop_inode_space = 1;
  4248. /*
  4249. * If we have more or the same amount of outsanding extents than we have
  4250. * reserved then we need to leave the reserved extents count alone.
  4251. */
  4252. if (BTRFS_I(inode)->outstanding_extents >=
  4253. BTRFS_I(inode)->reserved_extents)
  4254. return drop_inode_space;
  4255. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4256. BTRFS_I(inode)->outstanding_extents;
  4257. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4258. return dropped_extents + drop_inode_space;
  4259. }
  4260. /**
  4261. * calc_csum_metadata_size - return the amount of metada space that must be
  4262. * reserved/free'd for the given bytes.
  4263. * @inode: the inode we're manipulating
  4264. * @num_bytes: the number of bytes in question
  4265. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4266. *
  4267. * This adjusts the number of csum_bytes in the inode and then returns the
  4268. * correct amount of metadata that must either be reserved or freed. We
  4269. * calculate how many checksums we can fit into one leaf and then divide the
  4270. * number of bytes that will need to be checksumed by this value to figure out
  4271. * how many checksums will be required. If we are adding bytes then the number
  4272. * may go up and we will return the number of additional bytes that must be
  4273. * reserved. If it is going down we will return the number of bytes that must
  4274. * be freed.
  4275. *
  4276. * This must be called with BTRFS_I(inode)->lock held.
  4277. */
  4278. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4279. int reserve)
  4280. {
  4281. struct btrfs_root *root = BTRFS_I(inode)->root;
  4282. u64 csum_size;
  4283. int num_csums_per_leaf;
  4284. int num_csums;
  4285. int old_csums;
  4286. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4287. BTRFS_I(inode)->csum_bytes == 0)
  4288. return 0;
  4289. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4290. if (reserve)
  4291. BTRFS_I(inode)->csum_bytes += num_bytes;
  4292. else
  4293. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4294. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  4295. num_csums_per_leaf = (int)div64_u64(csum_size,
  4296. sizeof(struct btrfs_csum_item) +
  4297. sizeof(struct btrfs_disk_key));
  4298. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4299. num_csums = num_csums + num_csums_per_leaf - 1;
  4300. num_csums = num_csums / num_csums_per_leaf;
  4301. old_csums = old_csums + num_csums_per_leaf - 1;
  4302. old_csums = old_csums / num_csums_per_leaf;
  4303. /* No change, no need to reserve more */
  4304. if (old_csums == num_csums)
  4305. return 0;
  4306. if (reserve)
  4307. return btrfs_calc_trans_metadata_size(root,
  4308. num_csums - old_csums);
  4309. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4310. }
  4311. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4312. {
  4313. struct btrfs_root *root = BTRFS_I(inode)->root;
  4314. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4315. u64 to_reserve = 0;
  4316. u64 csum_bytes;
  4317. unsigned nr_extents = 0;
  4318. int extra_reserve = 0;
  4319. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4320. int ret = 0;
  4321. bool delalloc_lock = true;
  4322. u64 to_free = 0;
  4323. unsigned dropped;
  4324. /* If we are a free space inode we need to not flush since we will be in
  4325. * the middle of a transaction commit. We also don't need the delalloc
  4326. * mutex since we won't race with anybody. We need this mostly to make
  4327. * lockdep shut its filthy mouth.
  4328. */
  4329. if (btrfs_is_free_space_inode(inode)) {
  4330. flush = BTRFS_RESERVE_NO_FLUSH;
  4331. delalloc_lock = false;
  4332. }
  4333. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4334. btrfs_transaction_in_commit(root->fs_info))
  4335. schedule_timeout(1);
  4336. if (delalloc_lock)
  4337. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4338. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4339. spin_lock(&BTRFS_I(inode)->lock);
  4340. BTRFS_I(inode)->outstanding_extents++;
  4341. if (BTRFS_I(inode)->outstanding_extents >
  4342. BTRFS_I(inode)->reserved_extents)
  4343. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4344. BTRFS_I(inode)->reserved_extents;
  4345. /*
  4346. * Add an item to reserve for updating the inode when we complete the
  4347. * delalloc io.
  4348. */
  4349. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4350. &BTRFS_I(inode)->runtime_flags)) {
  4351. nr_extents++;
  4352. extra_reserve = 1;
  4353. }
  4354. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4355. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4356. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4357. spin_unlock(&BTRFS_I(inode)->lock);
  4358. if (root->fs_info->quota_enabled) {
  4359. ret = btrfs_qgroup_reserve(root, num_bytes +
  4360. nr_extents * root->leafsize);
  4361. if (ret)
  4362. goto out_fail;
  4363. }
  4364. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4365. if (unlikely(ret)) {
  4366. if (root->fs_info->quota_enabled)
  4367. btrfs_qgroup_free(root, num_bytes +
  4368. nr_extents * root->leafsize);
  4369. goto out_fail;
  4370. }
  4371. spin_lock(&BTRFS_I(inode)->lock);
  4372. if (extra_reserve) {
  4373. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4374. &BTRFS_I(inode)->runtime_flags);
  4375. nr_extents--;
  4376. }
  4377. BTRFS_I(inode)->reserved_extents += nr_extents;
  4378. spin_unlock(&BTRFS_I(inode)->lock);
  4379. if (delalloc_lock)
  4380. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4381. if (to_reserve)
  4382. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4383. btrfs_ino(inode), to_reserve, 1);
  4384. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4385. return 0;
  4386. out_fail:
  4387. spin_lock(&BTRFS_I(inode)->lock);
  4388. dropped = drop_outstanding_extent(inode);
  4389. /*
  4390. * If the inodes csum_bytes is the same as the original
  4391. * csum_bytes then we know we haven't raced with any free()ers
  4392. * so we can just reduce our inodes csum bytes and carry on.
  4393. */
  4394. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4395. calc_csum_metadata_size(inode, num_bytes, 0);
  4396. } else {
  4397. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4398. u64 bytes;
  4399. /*
  4400. * This is tricky, but first we need to figure out how much we
  4401. * free'd from any free-ers that occured during this
  4402. * reservation, so we reset ->csum_bytes to the csum_bytes
  4403. * before we dropped our lock, and then call the free for the
  4404. * number of bytes that were freed while we were trying our
  4405. * reservation.
  4406. */
  4407. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4408. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4409. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4410. /*
  4411. * Now we need to see how much we would have freed had we not
  4412. * been making this reservation and our ->csum_bytes were not
  4413. * artificially inflated.
  4414. */
  4415. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4416. bytes = csum_bytes - orig_csum_bytes;
  4417. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4418. /*
  4419. * Now reset ->csum_bytes to what it should be. If bytes is
  4420. * more than to_free then we would have free'd more space had we
  4421. * not had an artificially high ->csum_bytes, so we need to free
  4422. * the remainder. If bytes is the same or less then we don't
  4423. * need to do anything, the other free-ers did the correct
  4424. * thing.
  4425. */
  4426. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4427. if (bytes > to_free)
  4428. to_free = bytes - to_free;
  4429. else
  4430. to_free = 0;
  4431. }
  4432. spin_unlock(&BTRFS_I(inode)->lock);
  4433. if (dropped)
  4434. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4435. if (to_free) {
  4436. btrfs_block_rsv_release(root, block_rsv, to_free);
  4437. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4438. btrfs_ino(inode), to_free, 0);
  4439. }
  4440. if (delalloc_lock)
  4441. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4442. return ret;
  4443. }
  4444. /**
  4445. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4446. * @inode: the inode to release the reservation for
  4447. * @num_bytes: the number of bytes we're releasing
  4448. *
  4449. * This will release the metadata reservation for an inode. This can be called
  4450. * once we complete IO for a given set of bytes to release their metadata
  4451. * reservations.
  4452. */
  4453. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4454. {
  4455. struct btrfs_root *root = BTRFS_I(inode)->root;
  4456. u64 to_free = 0;
  4457. unsigned dropped;
  4458. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4459. spin_lock(&BTRFS_I(inode)->lock);
  4460. dropped = drop_outstanding_extent(inode);
  4461. if (num_bytes)
  4462. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4463. spin_unlock(&BTRFS_I(inode)->lock);
  4464. if (dropped > 0)
  4465. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4466. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4467. btrfs_ino(inode), to_free, 0);
  4468. if (root->fs_info->quota_enabled) {
  4469. btrfs_qgroup_free(root, num_bytes +
  4470. dropped * root->leafsize);
  4471. }
  4472. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4473. to_free);
  4474. }
  4475. /**
  4476. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4477. * @inode: inode we're writing to
  4478. * @num_bytes: the number of bytes we want to allocate
  4479. *
  4480. * This will do the following things
  4481. *
  4482. * o reserve space in the data space info for num_bytes
  4483. * o reserve space in the metadata space info based on number of outstanding
  4484. * extents and how much csums will be needed
  4485. * o add to the inodes ->delalloc_bytes
  4486. * o add it to the fs_info's delalloc inodes list.
  4487. *
  4488. * This will return 0 for success and -ENOSPC if there is no space left.
  4489. */
  4490. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4491. {
  4492. int ret;
  4493. ret = btrfs_check_data_free_space(inode, num_bytes);
  4494. if (ret)
  4495. return ret;
  4496. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4497. if (ret) {
  4498. btrfs_free_reserved_data_space(inode, num_bytes);
  4499. return ret;
  4500. }
  4501. return 0;
  4502. }
  4503. /**
  4504. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4505. * @inode: inode we're releasing space for
  4506. * @num_bytes: the number of bytes we want to free up
  4507. *
  4508. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4509. * called in the case that we don't need the metadata AND data reservations
  4510. * anymore. So if there is an error or we insert an inline extent.
  4511. *
  4512. * This function will release the metadata space that was not used and will
  4513. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4514. * list if there are no delalloc bytes left.
  4515. */
  4516. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4517. {
  4518. btrfs_delalloc_release_metadata(inode, num_bytes);
  4519. btrfs_free_reserved_data_space(inode, num_bytes);
  4520. }
  4521. static int update_block_group(struct btrfs_root *root,
  4522. u64 bytenr, u64 num_bytes, int alloc)
  4523. {
  4524. struct btrfs_block_group_cache *cache = NULL;
  4525. struct btrfs_fs_info *info = root->fs_info;
  4526. u64 total = num_bytes;
  4527. u64 old_val;
  4528. u64 byte_in_group;
  4529. int factor;
  4530. /* block accounting for super block */
  4531. spin_lock(&info->delalloc_root_lock);
  4532. old_val = btrfs_super_bytes_used(info->super_copy);
  4533. if (alloc)
  4534. old_val += num_bytes;
  4535. else
  4536. old_val -= num_bytes;
  4537. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4538. spin_unlock(&info->delalloc_root_lock);
  4539. while (total) {
  4540. cache = btrfs_lookup_block_group(info, bytenr);
  4541. if (!cache)
  4542. return -ENOENT;
  4543. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4544. BTRFS_BLOCK_GROUP_RAID1 |
  4545. BTRFS_BLOCK_GROUP_RAID10))
  4546. factor = 2;
  4547. else
  4548. factor = 1;
  4549. /*
  4550. * If this block group has free space cache written out, we
  4551. * need to make sure to load it if we are removing space. This
  4552. * is because we need the unpinning stage to actually add the
  4553. * space back to the block group, otherwise we will leak space.
  4554. */
  4555. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4556. cache_block_group(cache, 1);
  4557. byte_in_group = bytenr - cache->key.objectid;
  4558. WARN_ON(byte_in_group > cache->key.offset);
  4559. spin_lock(&cache->space_info->lock);
  4560. spin_lock(&cache->lock);
  4561. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4562. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4563. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4564. cache->dirty = 1;
  4565. old_val = btrfs_block_group_used(&cache->item);
  4566. num_bytes = min(total, cache->key.offset - byte_in_group);
  4567. if (alloc) {
  4568. old_val += num_bytes;
  4569. btrfs_set_block_group_used(&cache->item, old_val);
  4570. cache->reserved -= num_bytes;
  4571. cache->space_info->bytes_reserved -= num_bytes;
  4572. cache->space_info->bytes_used += num_bytes;
  4573. cache->space_info->disk_used += num_bytes * factor;
  4574. spin_unlock(&cache->lock);
  4575. spin_unlock(&cache->space_info->lock);
  4576. } else {
  4577. old_val -= num_bytes;
  4578. btrfs_set_block_group_used(&cache->item, old_val);
  4579. cache->pinned += num_bytes;
  4580. cache->space_info->bytes_pinned += num_bytes;
  4581. cache->space_info->bytes_used -= num_bytes;
  4582. cache->space_info->disk_used -= num_bytes * factor;
  4583. spin_unlock(&cache->lock);
  4584. spin_unlock(&cache->space_info->lock);
  4585. set_extent_dirty(info->pinned_extents,
  4586. bytenr, bytenr + num_bytes - 1,
  4587. GFP_NOFS | __GFP_NOFAIL);
  4588. }
  4589. btrfs_put_block_group(cache);
  4590. total -= num_bytes;
  4591. bytenr += num_bytes;
  4592. }
  4593. return 0;
  4594. }
  4595. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4596. {
  4597. struct btrfs_block_group_cache *cache;
  4598. u64 bytenr;
  4599. spin_lock(&root->fs_info->block_group_cache_lock);
  4600. bytenr = root->fs_info->first_logical_byte;
  4601. spin_unlock(&root->fs_info->block_group_cache_lock);
  4602. if (bytenr < (u64)-1)
  4603. return bytenr;
  4604. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4605. if (!cache)
  4606. return 0;
  4607. bytenr = cache->key.objectid;
  4608. btrfs_put_block_group(cache);
  4609. return bytenr;
  4610. }
  4611. static int pin_down_extent(struct btrfs_root *root,
  4612. struct btrfs_block_group_cache *cache,
  4613. u64 bytenr, u64 num_bytes, int reserved)
  4614. {
  4615. spin_lock(&cache->space_info->lock);
  4616. spin_lock(&cache->lock);
  4617. cache->pinned += num_bytes;
  4618. cache->space_info->bytes_pinned += num_bytes;
  4619. if (reserved) {
  4620. cache->reserved -= num_bytes;
  4621. cache->space_info->bytes_reserved -= num_bytes;
  4622. }
  4623. spin_unlock(&cache->lock);
  4624. spin_unlock(&cache->space_info->lock);
  4625. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4626. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4627. if (reserved)
  4628. trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
  4629. return 0;
  4630. }
  4631. /*
  4632. * this function must be called within transaction
  4633. */
  4634. int btrfs_pin_extent(struct btrfs_root *root,
  4635. u64 bytenr, u64 num_bytes, int reserved)
  4636. {
  4637. struct btrfs_block_group_cache *cache;
  4638. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4639. BUG_ON(!cache); /* Logic error */
  4640. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4641. btrfs_put_block_group(cache);
  4642. return 0;
  4643. }
  4644. /*
  4645. * this function must be called within transaction
  4646. */
  4647. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  4648. u64 bytenr, u64 num_bytes)
  4649. {
  4650. struct btrfs_block_group_cache *cache;
  4651. int ret;
  4652. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4653. if (!cache)
  4654. return -EINVAL;
  4655. /*
  4656. * pull in the free space cache (if any) so that our pin
  4657. * removes the free space from the cache. We have load_only set
  4658. * to one because the slow code to read in the free extents does check
  4659. * the pinned extents.
  4660. */
  4661. cache_block_group(cache, 1);
  4662. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4663. /* remove us from the free space cache (if we're there at all) */
  4664. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  4665. btrfs_put_block_group(cache);
  4666. return ret;
  4667. }
  4668. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  4669. {
  4670. int ret;
  4671. struct btrfs_block_group_cache *block_group;
  4672. struct btrfs_caching_control *caching_ctl;
  4673. block_group = btrfs_lookup_block_group(root->fs_info, start);
  4674. if (!block_group)
  4675. return -EINVAL;
  4676. cache_block_group(block_group, 0);
  4677. caching_ctl = get_caching_control(block_group);
  4678. if (!caching_ctl) {
  4679. /* Logic error */
  4680. BUG_ON(!block_group_cache_done(block_group));
  4681. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4682. } else {
  4683. mutex_lock(&caching_ctl->mutex);
  4684. if (start >= caching_ctl->progress) {
  4685. ret = add_excluded_extent(root, start, num_bytes);
  4686. } else if (start + num_bytes <= caching_ctl->progress) {
  4687. ret = btrfs_remove_free_space(block_group,
  4688. start, num_bytes);
  4689. } else {
  4690. num_bytes = caching_ctl->progress - start;
  4691. ret = btrfs_remove_free_space(block_group,
  4692. start, num_bytes);
  4693. if (ret)
  4694. goto out_lock;
  4695. num_bytes = (start + num_bytes) -
  4696. caching_ctl->progress;
  4697. start = caching_ctl->progress;
  4698. ret = add_excluded_extent(root, start, num_bytes);
  4699. }
  4700. out_lock:
  4701. mutex_unlock(&caching_ctl->mutex);
  4702. put_caching_control(caching_ctl);
  4703. }
  4704. btrfs_put_block_group(block_group);
  4705. return ret;
  4706. }
  4707. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  4708. struct extent_buffer *eb)
  4709. {
  4710. struct btrfs_file_extent_item *item;
  4711. struct btrfs_key key;
  4712. int found_type;
  4713. int i;
  4714. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  4715. return 0;
  4716. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  4717. btrfs_item_key_to_cpu(eb, &key, i);
  4718. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4719. continue;
  4720. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  4721. found_type = btrfs_file_extent_type(eb, item);
  4722. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  4723. continue;
  4724. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  4725. continue;
  4726. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  4727. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  4728. __exclude_logged_extent(log, key.objectid, key.offset);
  4729. }
  4730. return 0;
  4731. }
  4732. /**
  4733. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4734. * @cache: The cache we are manipulating
  4735. * @num_bytes: The number of bytes in question
  4736. * @reserve: One of the reservation enums
  4737. *
  4738. * This is called by the allocator when it reserves space, or by somebody who is
  4739. * freeing space that was never actually used on disk. For example if you
  4740. * reserve some space for a new leaf in transaction A and before transaction A
  4741. * commits you free that leaf, you call this with reserve set to 0 in order to
  4742. * clear the reservation.
  4743. *
  4744. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4745. * ENOSPC accounting. For data we handle the reservation through clearing the
  4746. * delalloc bits in the io_tree. We have to do this since we could end up
  4747. * allocating less disk space for the amount of data we have reserved in the
  4748. * case of compression.
  4749. *
  4750. * If this is a reservation and the block group has become read only we cannot
  4751. * make the reservation and return -EAGAIN, otherwise this function always
  4752. * succeeds.
  4753. */
  4754. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4755. u64 num_bytes, int reserve)
  4756. {
  4757. struct btrfs_space_info *space_info = cache->space_info;
  4758. int ret = 0;
  4759. spin_lock(&space_info->lock);
  4760. spin_lock(&cache->lock);
  4761. if (reserve != RESERVE_FREE) {
  4762. if (cache->ro) {
  4763. ret = -EAGAIN;
  4764. } else {
  4765. cache->reserved += num_bytes;
  4766. space_info->bytes_reserved += num_bytes;
  4767. if (reserve == RESERVE_ALLOC) {
  4768. trace_btrfs_space_reservation(cache->fs_info,
  4769. "space_info", space_info->flags,
  4770. num_bytes, 0);
  4771. space_info->bytes_may_use -= num_bytes;
  4772. }
  4773. }
  4774. } else {
  4775. if (cache->ro)
  4776. space_info->bytes_readonly += num_bytes;
  4777. cache->reserved -= num_bytes;
  4778. space_info->bytes_reserved -= num_bytes;
  4779. }
  4780. spin_unlock(&cache->lock);
  4781. spin_unlock(&space_info->lock);
  4782. return ret;
  4783. }
  4784. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4785. struct btrfs_root *root)
  4786. {
  4787. struct btrfs_fs_info *fs_info = root->fs_info;
  4788. struct btrfs_caching_control *next;
  4789. struct btrfs_caching_control *caching_ctl;
  4790. struct btrfs_block_group_cache *cache;
  4791. struct btrfs_space_info *space_info;
  4792. down_write(&fs_info->extent_commit_sem);
  4793. list_for_each_entry_safe(caching_ctl, next,
  4794. &fs_info->caching_block_groups, list) {
  4795. cache = caching_ctl->block_group;
  4796. if (block_group_cache_done(cache)) {
  4797. cache->last_byte_to_unpin = (u64)-1;
  4798. list_del_init(&caching_ctl->list);
  4799. put_caching_control(caching_ctl);
  4800. } else {
  4801. cache->last_byte_to_unpin = caching_ctl->progress;
  4802. }
  4803. }
  4804. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4805. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4806. else
  4807. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4808. up_write(&fs_info->extent_commit_sem);
  4809. list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
  4810. percpu_counter_set(&space_info->total_bytes_pinned, 0);
  4811. update_global_block_rsv(fs_info);
  4812. }
  4813. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4814. {
  4815. struct btrfs_fs_info *fs_info = root->fs_info;
  4816. struct btrfs_block_group_cache *cache = NULL;
  4817. struct btrfs_space_info *space_info;
  4818. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4819. u64 len;
  4820. bool readonly;
  4821. while (start <= end) {
  4822. readonly = false;
  4823. if (!cache ||
  4824. start >= cache->key.objectid + cache->key.offset) {
  4825. if (cache)
  4826. btrfs_put_block_group(cache);
  4827. cache = btrfs_lookup_block_group(fs_info, start);
  4828. BUG_ON(!cache); /* Logic error */
  4829. }
  4830. len = cache->key.objectid + cache->key.offset - start;
  4831. len = min(len, end + 1 - start);
  4832. if (start < cache->last_byte_to_unpin) {
  4833. len = min(len, cache->last_byte_to_unpin - start);
  4834. btrfs_add_free_space(cache, start, len);
  4835. }
  4836. start += len;
  4837. space_info = cache->space_info;
  4838. spin_lock(&space_info->lock);
  4839. spin_lock(&cache->lock);
  4840. cache->pinned -= len;
  4841. space_info->bytes_pinned -= len;
  4842. if (cache->ro) {
  4843. space_info->bytes_readonly += len;
  4844. readonly = true;
  4845. }
  4846. spin_unlock(&cache->lock);
  4847. if (!readonly && global_rsv->space_info == space_info) {
  4848. spin_lock(&global_rsv->lock);
  4849. if (!global_rsv->full) {
  4850. len = min(len, global_rsv->size -
  4851. global_rsv->reserved);
  4852. global_rsv->reserved += len;
  4853. space_info->bytes_may_use += len;
  4854. if (global_rsv->reserved >= global_rsv->size)
  4855. global_rsv->full = 1;
  4856. }
  4857. spin_unlock(&global_rsv->lock);
  4858. }
  4859. spin_unlock(&space_info->lock);
  4860. }
  4861. if (cache)
  4862. btrfs_put_block_group(cache);
  4863. return 0;
  4864. }
  4865. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4866. struct btrfs_root *root)
  4867. {
  4868. struct btrfs_fs_info *fs_info = root->fs_info;
  4869. struct extent_io_tree *unpin;
  4870. u64 start;
  4871. u64 end;
  4872. int ret;
  4873. if (trans->aborted)
  4874. return 0;
  4875. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4876. unpin = &fs_info->freed_extents[1];
  4877. else
  4878. unpin = &fs_info->freed_extents[0];
  4879. while (1) {
  4880. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4881. EXTENT_DIRTY, NULL);
  4882. if (ret)
  4883. break;
  4884. if (btrfs_test_opt(root, DISCARD))
  4885. ret = btrfs_discard_extent(root, start,
  4886. end + 1 - start, NULL);
  4887. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4888. unpin_extent_range(root, start, end);
  4889. cond_resched();
  4890. }
  4891. return 0;
  4892. }
  4893. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
  4894. u64 owner, u64 root_objectid)
  4895. {
  4896. struct btrfs_space_info *space_info;
  4897. u64 flags;
  4898. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4899. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  4900. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  4901. else
  4902. flags = BTRFS_BLOCK_GROUP_METADATA;
  4903. } else {
  4904. flags = BTRFS_BLOCK_GROUP_DATA;
  4905. }
  4906. space_info = __find_space_info(fs_info, flags);
  4907. BUG_ON(!space_info); /* Logic bug */
  4908. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  4909. }
  4910. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4911. struct btrfs_root *root,
  4912. u64 bytenr, u64 num_bytes, u64 parent,
  4913. u64 root_objectid, u64 owner_objectid,
  4914. u64 owner_offset, int refs_to_drop,
  4915. struct btrfs_delayed_extent_op *extent_op)
  4916. {
  4917. struct btrfs_key key;
  4918. struct btrfs_path *path;
  4919. struct btrfs_fs_info *info = root->fs_info;
  4920. struct btrfs_root *extent_root = info->extent_root;
  4921. struct extent_buffer *leaf;
  4922. struct btrfs_extent_item *ei;
  4923. struct btrfs_extent_inline_ref *iref;
  4924. int ret;
  4925. int is_data;
  4926. int extent_slot = 0;
  4927. int found_extent = 0;
  4928. int num_to_del = 1;
  4929. u32 item_size;
  4930. u64 refs;
  4931. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  4932. SKINNY_METADATA);
  4933. path = btrfs_alloc_path();
  4934. if (!path)
  4935. return -ENOMEM;
  4936. path->reada = 1;
  4937. path->leave_spinning = 1;
  4938. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4939. BUG_ON(!is_data && refs_to_drop != 1);
  4940. if (is_data)
  4941. skinny_metadata = 0;
  4942. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4943. bytenr, num_bytes, parent,
  4944. root_objectid, owner_objectid,
  4945. owner_offset);
  4946. if (ret == 0) {
  4947. extent_slot = path->slots[0];
  4948. while (extent_slot >= 0) {
  4949. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4950. extent_slot);
  4951. if (key.objectid != bytenr)
  4952. break;
  4953. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4954. key.offset == num_bytes) {
  4955. found_extent = 1;
  4956. break;
  4957. }
  4958. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  4959. key.offset == owner_objectid) {
  4960. found_extent = 1;
  4961. break;
  4962. }
  4963. if (path->slots[0] - extent_slot > 5)
  4964. break;
  4965. extent_slot--;
  4966. }
  4967. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4968. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4969. if (found_extent && item_size < sizeof(*ei))
  4970. found_extent = 0;
  4971. #endif
  4972. if (!found_extent) {
  4973. BUG_ON(iref);
  4974. ret = remove_extent_backref(trans, extent_root, path,
  4975. NULL, refs_to_drop,
  4976. is_data);
  4977. if (ret) {
  4978. btrfs_abort_transaction(trans, extent_root, ret);
  4979. goto out;
  4980. }
  4981. btrfs_release_path(path);
  4982. path->leave_spinning = 1;
  4983. key.objectid = bytenr;
  4984. key.type = BTRFS_EXTENT_ITEM_KEY;
  4985. key.offset = num_bytes;
  4986. if (!is_data && skinny_metadata) {
  4987. key.type = BTRFS_METADATA_ITEM_KEY;
  4988. key.offset = owner_objectid;
  4989. }
  4990. ret = btrfs_search_slot(trans, extent_root,
  4991. &key, path, -1, 1);
  4992. if (ret > 0 && skinny_metadata && path->slots[0]) {
  4993. /*
  4994. * Couldn't find our skinny metadata item,
  4995. * see if we have ye olde extent item.
  4996. */
  4997. path->slots[0]--;
  4998. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4999. path->slots[0]);
  5000. if (key.objectid == bytenr &&
  5001. key.type == BTRFS_EXTENT_ITEM_KEY &&
  5002. key.offset == num_bytes)
  5003. ret = 0;
  5004. }
  5005. if (ret > 0 && skinny_metadata) {
  5006. skinny_metadata = false;
  5007. key.type = BTRFS_EXTENT_ITEM_KEY;
  5008. key.offset = num_bytes;
  5009. btrfs_release_path(path);
  5010. ret = btrfs_search_slot(trans, extent_root,
  5011. &key, path, -1, 1);
  5012. }
  5013. if (ret) {
  5014. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5015. ret, bytenr);
  5016. if (ret > 0)
  5017. btrfs_print_leaf(extent_root,
  5018. path->nodes[0]);
  5019. }
  5020. if (ret < 0) {
  5021. btrfs_abort_transaction(trans, extent_root, ret);
  5022. goto out;
  5023. }
  5024. extent_slot = path->slots[0];
  5025. }
  5026. } else if (WARN_ON(ret == -ENOENT)) {
  5027. btrfs_print_leaf(extent_root, path->nodes[0]);
  5028. btrfs_err(info,
  5029. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  5030. bytenr, parent, root_objectid, owner_objectid,
  5031. owner_offset);
  5032. } else {
  5033. btrfs_abort_transaction(trans, extent_root, ret);
  5034. goto out;
  5035. }
  5036. leaf = path->nodes[0];
  5037. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5038. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5039. if (item_size < sizeof(*ei)) {
  5040. BUG_ON(found_extent || extent_slot != path->slots[0]);
  5041. ret = convert_extent_item_v0(trans, extent_root, path,
  5042. owner_objectid, 0);
  5043. if (ret < 0) {
  5044. btrfs_abort_transaction(trans, extent_root, ret);
  5045. goto out;
  5046. }
  5047. btrfs_release_path(path);
  5048. path->leave_spinning = 1;
  5049. key.objectid = bytenr;
  5050. key.type = BTRFS_EXTENT_ITEM_KEY;
  5051. key.offset = num_bytes;
  5052. ret = btrfs_search_slot(trans, extent_root, &key, path,
  5053. -1, 1);
  5054. if (ret) {
  5055. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5056. ret, bytenr);
  5057. btrfs_print_leaf(extent_root, path->nodes[0]);
  5058. }
  5059. if (ret < 0) {
  5060. btrfs_abort_transaction(trans, extent_root, ret);
  5061. goto out;
  5062. }
  5063. extent_slot = path->slots[0];
  5064. leaf = path->nodes[0];
  5065. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5066. }
  5067. #endif
  5068. BUG_ON(item_size < sizeof(*ei));
  5069. ei = btrfs_item_ptr(leaf, extent_slot,
  5070. struct btrfs_extent_item);
  5071. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5072. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5073. struct btrfs_tree_block_info *bi;
  5074. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5075. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5076. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5077. }
  5078. refs = btrfs_extent_refs(leaf, ei);
  5079. if (refs < refs_to_drop) {
  5080. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5081. "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
  5082. ret = -EINVAL;
  5083. btrfs_abort_transaction(trans, extent_root, ret);
  5084. goto out;
  5085. }
  5086. refs -= refs_to_drop;
  5087. if (refs > 0) {
  5088. if (extent_op)
  5089. __run_delayed_extent_op(extent_op, leaf, ei);
  5090. /*
  5091. * In the case of inline back ref, reference count will
  5092. * be updated by remove_extent_backref
  5093. */
  5094. if (iref) {
  5095. BUG_ON(!found_extent);
  5096. } else {
  5097. btrfs_set_extent_refs(leaf, ei, refs);
  5098. btrfs_mark_buffer_dirty(leaf);
  5099. }
  5100. if (found_extent) {
  5101. ret = remove_extent_backref(trans, extent_root, path,
  5102. iref, refs_to_drop,
  5103. is_data);
  5104. if (ret) {
  5105. btrfs_abort_transaction(trans, extent_root, ret);
  5106. goto out;
  5107. }
  5108. }
  5109. add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
  5110. root_objectid);
  5111. } else {
  5112. if (found_extent) {
  5113. BUG_ON(is_data && refs_to_drop !=
  5114. extent_data_ref_count(root, path, iref));
  5115. if (iref) {
  5116. BUG_ON(path->slots[0] != extent_slot);
  5117. } else {
  5118. BUG_ON(path->slots[0] != extent_slot + 1);
  5119. path->slots[0] = extent_slot;
  5120. num_to_del = 2;
  5121. }
  5122. }
  5123. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5124. num_to_del);
  5125. if (ret) {
  5126. btrfs_abort_transaction(trans, extent_root, ret);
  5127. goto out;
  5128. }
  5129. btrfs_release_path(path);
  5130. if (is_data) {
  5131. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5132. if (ret) {
  5133. btrfs_abort_transaction(trans, extent_root, ret);
  5134. goto out;
  5135. }
  5136. }
  5137. ret = update_block_group(root, bytenr, num_bytes, 0);
  5138. if (ret) {
  5139. btrfs_abort_transaction(trans, extent_root, ret);
  5140. goto out;
  5141. }
  5142. }
  5143. out:
  5144. btrfs_free_path(path);
  5145. return ret;
  5146. }
  5147. /*
  5148. * when we free an block, it is possible (and likely) that we free the last
  5149. * delayed ref for that extent as well. This searches the delayed ref tree for
  5150. * a given extent, and if there are no other delayed refs to be processed, it
  5151. * removes it from the tree.
  5152. */
  5153. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5154. struct btrfs_root *root, u64 bytenr)
  5155. {
  5156. struct btrfs_delayed_ref_head *head;
  5157. struct btrfs_delayed_ref_root *delayed_refs;
  5158. int ret = 0;
  5159. delayed_refs = &trans->transaction->delayed_refs;
  5160. spin_lock(&delayed_refs->lock);
  5161. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5162. if (!head)
  5163. goto out;
  5164. spin_lock(&head->lock);
  5165. if (rb_first(&head->ref_root))
  5166. goto out;
  5167. if (head->extent_op) {
  5168. if (!head->must_insert_reserved)
  5169. goto out;
  5170. btrfs_free_delayed_extent_op(head->extent_op);
  5171. head->extent_op = NULL;
  5172. }
  5173. /*
  5174. * waiting for the lock here would deadlock. If someone else has it
  5175. * locked they are already in the process of dropping it anyway
  5176. */
  5177. if (!mutex_trylock(&head->mutex))
  5178. goto out;
  5179. /*
  5180. * at this point we have a head with no other entries. Go
  5181. * ahead and process it.
  5182. */
  5183. head->node.in_tree = 0;
  5184. rb_erase(&head->href_node, &delayed_refs->href_root);
  5185. atomic_dec(&delayed_refs->num_entries);
  5186. /*
  5187. * we don't take a ref on the node because we're removing it from the
  5188. * tree, so we just steal the ref the tree was holding.
  5189. */
  5190. delayed_refs->num_heads--;
  5191. if (head->processing == 0)
  5192. delayed_refs->num_heads_ready--;
  5193. head->processing = 0;
  5194. spin_unlock(&head->lock);
  5195. spin_unlock(&delayed_refs->lock);
  5196. BUG_ON(head->extent_op);
  5197. if (head->must_insert_reserved)
  5198. ret = 1;
  5199. mutex_unlock(&head->mutex);
  5200. btrfs_put_delayed_ref(&head->node);
  5201. return ret;
  5202. out:
  5203. spin_unlock(&head->lock);
  5204. spin_unlock(&delayed_refs->lock);
  5205. return 0;
  5206. }
  5207. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5208. struct btrfs_root *root,
  5209. struct extent_buffer *buf,
  5210. u64 parent, int last_ref)
  5211. {
  5212. struct btrfs_block_group_cache *cache = NULL;
  5213. int pin = 1;
  5214. int ret;
  5215. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5216. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5217. buf->start, buf->len,
  5218. parent, root->root_key.objectid,
  5219. btrfs_header_level(buf),
  5220. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5221. BUG_ON(ret); /* -ENOMEM */
  5222. }
  5223. if (!last_ref)
  5224. return;
  5225. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5226. if (btrfs_header_generation(buf) == trans->transid) {
  5227. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5228. ret = check_ref_cleanup(trans, root, buf->start);
  5229. if (!ret)
  5230. goto out;
  5231. }
  5232. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5233. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5234. goto out;
  5235. }
  5236. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5237. btrfs_add_free_space(cache, buf->start, buf->len);
  5238. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  5239. trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
  5240. pin = 0;
  5241. }
  5242. out:
  5243. if (pin)
  5244. add_pinned_bytes(root->fs_info, buf->len,
  5245. btrfs_header_level(buf),
  5246. root->root_key.objectid);
  5247. /*
  5248. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5249. * anymore.
  5250. */
  5251. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5252. btrfs_put_block_group(cache);
  5253. }
  5254. /* Can return -ENOMEM */
  5255. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5256. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5257. u64 owner, u64 offset, int for_cow)
  5258. {
  5259. int ret;
  5260. struct btrfs_fs_info *fs_info = root->fs_info;
  5261. add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
  5262. /*
  5263. * tree log blocks never actually go into the extent allocation
  5264. * tree, just update pinning info and exit early.
  5265. */
  5266. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5267. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5268. /* unlocks the pinned mutex */
  5269. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5270. ret = 0;
  5271. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5272. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5273. num_bytes,
  5274. parent, root_objectid, (int)owner,
  5275. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  5276. } else {
  5277. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5278. num_bytes,
  5279. parent, root_objectid, owner,
  5280. offset, BTRFS_DROP_DELAYED_REF,
  5281. NULL, for_cow);
  5282. }
  5283. return ret;
  5284. }
  5285. static u64 stripe_align(struct btrfs_root *root,
  5286. struct btrfs_block_group_cache *cache,
  5287. u64 val, u64 num_bytes)
  5288. {
  5289. u64 ret = ALIGN(val, root->stripesize);
  5290. return ret;
  5291. }
  5292. /*
  5293. * when we wait for progress in the block group caching, its because
  5294. * our allocation attempt failed at least once. So, we must sleep
  5295. * and let some progress happen before we try again.
  5296. *
  5297. * This function will sleep at least once waiting for new free space to
  5298. * show up, and then it will check the block group free space numbers
  5299. * for our min num_bytes. Another option is to have it go ahead
  5300. * and look in the rbtree for a free extent of a given size, but this
  5301. * is a good start.
  5302. *
  5303. * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
  5304. * any of the information in this block group.
  5305. */
  5306. static noinline void
  5307. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5308. u64 num_bytes)
  5309. {
  5310. struct btrfs_caching_control *caching_ctl;
  5311. caching_ctl = get_caching_control(cache);
  5312. if (!caching_ctl)
  5313. return;
  5314. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5315. (cache->free_space_ctl->free_space >= num_bytes));
  5316. put_caching_control(caching_ctl);
  5317. }
  5318. static noinline int
  5319. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5320. {
  5321. struct btrfs_caching_control *caching_ctl;
  5322. int ret = 0;
  5323. caching_ctl = get_caching_control(cache);
  5324. if (!caching_ctl)
  5325. return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
  5326. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5327. if (cache->cached == BTRFS_CACHE_ERROR)
  5328. ret = -EIO;
  5329. put_caching_control(caching_ctl);
  5330. return ret;
  5331. }
  5332. int __get_raid_index(u64 flags)
  5333. {
  5334. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5335. return BTRFS_RAID_RAID10;
  5336. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5337. return BTRFS_RAID_RAID1;
  5338. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5339. return BTRFS_RAID_DUP;
  5340. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5341. return BTRFS_RAID_RAID0;
  5342. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5343. return BTRFS_RAID_RAID5;
  5344. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5345. return BTRFS_RAID_RAID6;
  5346. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5347. }
  5348. int get_block_group_index(struct btrfs_block_group_cache *cache)
  5349. {
  5350. return __get_raid_index(cache->flags);
  5351. }
  5352. static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
  5353. [BTRFS_RAID_RAID10] = "raid10",
  5354. [BTRFS_RAID_RAID1] = "raid1",
  5355. [BTRFS_RAID_DUP] = "dup",
  5356. [BTRFS_RAID_RAID0] = "raid0",
  5357. [BTRFS_RAID_SINGLE] = "single",
  5358. [BTRFS_RAID_RAID5] = "raid5",
  5359. [BTRFS_RAID_RAID6] = "raid6",
  5360. };
  5361. static const char *get_raid_name(enum btrfs_raid_types type)
  5362. {
  5363. if (type >= BTRFS_NR_RAID_TYPES)
  5364. return NULL;
  5365. return btrfs_raid_type_names[type];
  5366. }
  5367. enum btrfs_loop_type {
  5368. LOOP_CACHING_NOWAIT = 0,
  5369. LOOP_CACHING_WAIT = 1,
  5370. LOOP_ALLOC_CHUNK = 2,
  5371. LOOP_NO_EMPTY_SIZE = 3,
  5372. };
  5373. /*
  5374. * walks the btree of allocated extents and find a hole of a given size.
  5375. * The key ins is changed to record the hole:
  5376. * ins->objectid == start position
  5377. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5378. * ins->offset == the size of the hole.
  5379. * Any available blocks before search_start are skipped.
  5380. *
  5381. * If there is no suitable free space, we will record the max size of
  5382. * the free space extent currently.
  5383. */
  5384. static noinline int find_free_extent(struct btrfs_root *orig_root,
  5385. u64 num_bytes, u64 empty_size,
  5386. u64 hint_byte, struct btrfs_key *ins,
  5387. u64 flags)
  5388. {
  5389. int ret = 0;
  5390. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5391. struct btrfs_free_cluster *last_ptr = NULL;
  5392. struct btrfs_block_group_cache *block_group = NULL;
  5393. struct btrfs_block_group_cache *used_block_group;
  5394. u64 search_start = 0;
  5395. u64 max_extent_size = 0;
  5396. int empty_cluster = 2 * 1024 * 1024;
  5397. struct btrfs_space_info *space_info;
  5398. int loop = 0;
  5399. int index = __get_raid_index(flags);
  5400. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5401. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5402. bool failed_cluster_refill = false;
  5403. bool failed_alloc = false;
  5404. bool use_cluster = true;
  5405. bool have_caching_bg = false;
  5406. WARN_ON(num_bytes < root->sectorsize);
  5407. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  5408. ins->objectid = 0;
  5409. ins->offset = 0;
  5410. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5411. space_info = __find_space_info(root->fs_info, flags);
  5412. if (!space_info) {
  5413. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5414. return -ENOSPC;
  5415. }
  5416. /*
  5417. * If the space info is for both data and metadata it means we have a
  5418. * small filesystem and we can't use the clustering stuff.
  5419. */
  5420. if (btrfs_mixed_space_info(space_info))
  5421. use_cluster = false;
  5422. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5423. last_ptr = &root->fs_info->meta_alloc_cluster;
  5424. if (!btrfs_test_opt(root, SSD))
  5425. empty_cluster = 64 * 1024;
  5426. }
  5427. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5428. btrfs_test_opt(root, SSD)) {
  5429. last_ptr = &root->fs_info->data_alloc_cluster;
  5430. }
  5431. if (last_ptr) {
  5432. spin_lock(&last_ptr->lock);
  5433. if (last_ptr->block_group)
  5434. hint_byte = last_ptr->window_start;
  5435. spin_unlock(&last_ptr->lock);
  5436. }
  5437. search_start = max(search_start, first_logical_byte(root, 0));
  5438. search_start = max(search_start, hint_byte);
  5439. if (!last_ptr)
  5440. empty_cluster = 0;
  5441. if (search_start == hint_byte) {
  5442. block_group = btrfs_lookup_block_group(root->fs_info,
  5443. search_start);
  5444. used_block_group = block_group;
  5445. /*
  5446. * we don't want to use the block group if it doesn't match our
  5447. * allocation bits, or if its not cached.
  5448. *
  5449. * However if we are re-searching with an ideal block group
  5450. * picked out then we don't care that the block group is cached.
  5451. */
  5452. if (block_group && block_group_bits(block_group, flags) &&
  5453. block_group->cached != BTRFS_CACHE_NO) {
  5454. down_read(&space_info->groups_sem);
  5455. if (list_empty(&block_group->list) ||
  5456. block_group->ro) {
  5457. /*
  5458. * someone is removing this block group,
  5459. * we can't jump into the have_block_group
  5460. * target because our list pointers are not
  5461. * valid
  5462. */
  5463. btrfs_put_block_group(block_group);
  5464. up_read(&space_info->groups_sem);
  5465. } else {
  5466. index = get_block_group_index(block_group);
  5467. goto have_block_group;
  5468. }
  5469. } else if (block_group) {
  5470. btrfs_put_block_group(block_group);
  5471. }
  5472. }
  5473. search:
  5474. have_caching_bg = false;
  5475. down_read(&space_info->groups_sem);
  5476. list_for_each_entry(block_group, &space_info->block_groups[index],
  5477. list) {
  5478. u64 offset;
  5479. int cached;
  5480. used_block_group = block_group;
  5481. btrfs_get_block_group(block_group);
  5482. search_start = block_group->key.objectid;
  5483. /*
  5484. * this can happen if we end up cycling through all the
  5485. * raid types, but we want to make sure we only allocate
  5486. * for the proper type.
  5487. */
  5488. if (!block_group_bits(block_group, flags)) {
  5489. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  5490. BTRFS_BLOCK_GROUP_RAID1 |
  5491. BTRFS_BLOCK_GROUP_RAID5 |
  5492. BTRFS_BLOCK_GROUP_RAID6 |
  5493. BTRFS_BLOCK_GROUP_RAID10;
  5494. /*
  5495. * if they asked for extra copies and this block group
  5496. * doesn't provide them, bail. This does allow us to
  5497. * fill raid0 from raid1.
  5498. */
  5499. if ((flags & extra) && !(block_group->flags & extra))
  5500. goto loop;
  5501. }
  5502. have_block_group:
  5503. cached = block_group_cache_done(block_group);
  5504. if (unlikely(!cached)) {
  5505. ret = cache_block_group(block_group, 0);
  5506. BUG_ON(ret < 0);
  5507. ret = 0;
  5508. }
  5509. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
  5510. goto loop;
  5511. if (unlikely(block_group->ro))
  5512. goto loop;
  5513. /*
  5514. * Ok we want to try and use the cluster allocator, so
  5515. * lets look there
  5516. */
  5517. if (last_ptr) {
  5518. unsigned long aligned_cluster;
  5519. /*
  5520. * the refill lock keeps out other
  5521. * people trying to start a new cluster
  5522. */
  5523. spin_lock(&last_ptr->refill_lock);
  5524. used_block_group = last_ptr->block_group;
  5525. if (used_block_group != block_group &&
  5526. (!used_block_group ||
  5527. used_block_group->ro ||
  5528. !block_group_bits(used_block_group, flags))) {
  5529. used_block_group = block_group;
  5530. goto refill_cluster;
  5531. }
  5532. if (used_block_group != block_group)
  5533. btrfs_get_block_group(used_block_group);
  5534. offset = btrfs_alloc_from_cluster(used_block_group,
  5535. last_ptr,
  5536. num_bytes,
  5537. used_block_group->key.objectid,
  5538. &max_extent_size);
  5539. if (offset) {
  5540. /* we have a block, we're done */
  5541. spin_unlock(&last_ptr->refill_lock);
  5542. trace_btrfs_reserve_extent_cluster(root,
  5543. block_group, search_start, num_bytes);
  5544. goto checks;
  5545. }
  5546. WARN_ON(last_ptr->block_group != used_block_group);
  5547. if (used_block_group != block_group) {
  5548. btrfs_put_block_group(used_block_group);
  5549. used_block_group = block_group;
  5550. }
  5551. refill_cluster:
  5552. BUG_ON(used_block_group != block_group);
  5553. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5554. * set up a new clusters, so lets just skip it
  5555. * and let the allocator find whatever block
  5556. * it can find. If we reach this point, we
  5557. * will have tried the cluster allocator
  5558. * plenty of times and not have found
  5559. * anything, so we are likely way too
  5560. * fragmented for the clustering stuff to find
  5561. * anything.
  5562. *
  5563. * However, if the cluster is taken from the
  5564. * current block group, release the cluster
  5565. * first, so that we stand a better chance of
  5566. * succeeding in the unclustered
  5567. * allocation. */
  5568. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5569. last_ptr->block_group != block_group) {
  5570. spin_unlock(&last_ptr->refill_lock);
  5571. goto unclustered_alloc;
  5572. }
  5573. /*
  5574. * this cluster didn't work out, free it and
  5575. * start over
  5576. */
  5577. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5578. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5579. spin_unlock(&last_ptr->refill_lock);
  5580. goto unclustered_alloc;
  5581. }
  5582. aligned_cluster = max_t(unsigned long,
  5583. empty_cluster + empty_size,
  5584. block_group->full_stripe_len);
  5585. /* allocate a cluster in this block group */
  5586. ret = btrfs_find_space_cluster(root, block_group,
  5587. last_ptr, search_start,
  5588. num_bytes,
  5589. aligned_cluster);
  5590. if (ret == 0) {
  5591. /*
  5592. * now pull our allocation out of this
  5593. * cluster
  5594. */
  5595. offset = btrfs_alloc_from_cluster(block_group,
  5596. last_ptr,
  5597. num_bytes,
  5598. search_start,
  5599. &max_extent_size);
  5600. if (offset) {
  5601. /* we found one, proceed */
  5602. spin_unlock(&last_ptr->refill_lock);
  5603. trace_btrfs_reserve_extent_cluster(root,
  5604. block_group, search_start,
  5605. num_bytes);
  5606. goto checks;
  5607. }
  5608. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5609. && !failed_cluster_refill) {
  5610. spin_unlock(&last_ptr->refill_lock);
  5611. failed_cluster_refill = true;
  5612. wait_block_group_cache_progress(block_group,
  5613. num_bytes + empty_cluster + empty_size);
  5614. goto have_block_group;
  5615. }
  5616. /*
  5617. * at this point we either didn't find a cluster
  5618. * or we weren't able to allocate a block from our
  5619. * cluster. Free the cluster we've been trying
  5620. * to use, and go to the next block group
  5621. */
  5622. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5623. spin_unlock(&last_ptr->refill_lock);
  5624. goto loop;
  5625. }
  5626. unclustered_alloc:
  5627. spin_lock(&block_group->free_space_ctl->tree_lock);
  5628. if (cached &&
  5629. block_group->free_space_ctl->free_space <
  5630. num_bytes + empty_cluster + empty_size) {
  5631. if (block_group->free_space_ctl->free_space >
  5632. max_extent_size)
  5633. max_extent_size =
  5634. block_group->free_space_ctl->free_space;
  5635. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5636. goto loop;
  5637. }
  5638. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5639. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5640. num_bytes, empty_size,
  5641. &max_extent_size);
  5642. /*
  5643. * If we didn't find a chunk, and we haven't failed on this
  5644. * block group before, and this block group is in the middle of
  5645. * caching and we are ok with waiting, then go ahead and wait
  5646. * for progress to be made, and set failed_alloc to true.
  5647. *
  5648. * If failed_alloc is true then we've already waited on this
  5649. * block group once and should move on to the next block group.
  5650. */
  5651. if (!offset && !failed_alloc && !cached &&
  5652. loop > LOOP_CACHING_NOWAIT) {
  5653. wait_block_group_cache_progress(block_group,
  5654. num_bytes + empty_size);
  5655. failed_alloc = true;
  5656. goto have_block_group;
  5657. } else if (!offset) {
  5658. if (!cached)
  5659. have_caching_bg = true;
  5660. goto loop;
  5661. }
  5662. checks:
  5663. search_start = stripe_align(root, used_block_group,
  5664. offset, num_bytes);
  5665. /* move on to the next group */
  5666. if (search_start + num_bytes >
  5667. used_block_group->key.objectid + used_block_group->key.offset) {
  5668. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5669. goto loop;
  5670. }
  5671. if (offset < search_start)
  5672. btrfs_add_free_space(used_block_group, offset,
  5673. search_start - offset);
  5674. BUG_ON(offset > search_start);
  5675. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5676. alloc_type);
  5677. if (ret == -EAGAIN) {
  5678. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5679. goto loop;
  5680. }
  5681. /* we are all good, lets return */
  5682. ins->objectid = search_start;
  5683. ins->offset = num_bytes;
  5684. trace_btrfs_reserve_extent(orig_root, block_group,
  5685. search_start, num_bytes);
  5686. if (used_block_group != block_group)
  5687. btrfs_put_block_group(used_block_group);
  5688. btrfs_put_block_group(block_group);
  5689. break;
  5690. loop:
  5691. failed_cluster_refill = false;
  5692. failed_alloc = false;
  5693. BUG_ON(index != get_block_group_index(block_group));
  5694. if (used_block_group != block_group)
  5695. btrfs_put_block_group(used_block_group);
  5696. btrfs_put_block_group(block_group);
  5697. }
  5698. up_read(&space_info->groups_sem);
  5699. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5700. goto search;
  5701. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5702. goto search;
  5703. /*
  5704. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5705. * caching kthreads as we move along
  5706. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5707. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5708. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5709. * again
  5710. */
  5711. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5712. index = 0;
  5713. loop++;
  5714. if (loop == LOOP_ALLOC_CHUNK) {
  5715. struct btrfs_trans_handle *trans;
  5716. trans = btrfs_join_transaction(root);
  5717. if (IS_ERR(trans)) {
  5718. ret = PTR_ERR(trans);
  5719. goto out;
  5720. }
  5721. ret = do_chunk_alloc(trans, root, flags,
  5722. CHUNK_ALLOC_FORCE);
  5723. /*
  5724. * Do not bail out on ENOSPC since we
  5725. * can do more things.
  5726. */
  5727. if (ret < 0 && ret != -ENOSPC)
  5728. btrfs_abort_transaction(trans,
  5729. root, ret);
  5730. else
  5731. ret = 0;
  5732. btrfs_end_transaction(trans, root);
  5733. if (ret)
  5734. goto out;
  5735. }
  5736. if (loop == LOOP_NO_EMPTY_SIZE) {
  5737. empty_size = 0;
  5738. empty_cluster = 0;
  5739. }
  5740. goto search;
  5741. } else if (!ins->objectid) {
  5742. ret = -ENOSPC;
  5743. } else if (ins->objectid) {
  5744. ret = 0;
  5745. }
  5746. out:
  5747. if (ret == -ENOSPC)
  5748. ins->offset = max_extent_size;
  5749. return ret;
  5750. }
  5751. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5752. int dump_block_groups)
  5753. {
  5754. struct btrfs_block_group_cache *cache;
  5755. int index = 0;
  5756. spin_lock(&info->lock);
  5757. printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
  5758. info->flags,
  5759. info->total_bytes - info->bytes_used - info->bytes_pinned -
  5760. info->bytes_reserved - info->bytes_readonly,
  5761. (info->full) ? "" : "not ");
  5762. printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
  5763. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5764. info->total_bytes, info->bytes_used, info->bytes_pinned,
  5765. info->bytes_reserved, info->bytes_may_use,
  5766. info->bytes_readonly);
  5767. spin_unlock(&info->lock);
  5768. if (!dump_block_groups)
  5769. return;
  5770. down_read(&info->groups_sem);
  5771. again:
  5772. list_for_each_entry(cache, &info->block_groups[index], list) {
  5773. spin_lock(&cache->lock);
  5774. printk(KERN_INFO "BTRFS: "
  5775. "block group %llu has %llu bytes, "
  5776. "%llu used %llu pinned %llu reserved %s\n",
  5777. cache->key.objectid, cache->key.offset,
  5778. btrfs_block_group_used(&cache->item), cache->pinned,
  5779. cache->reserved, cache->ro ? "[readonly]" : "");
  5780. btrfs_dump_free_space(cache, bytes);
  5781. spin_unlock(&cache->lock);
  5782. }
  5783. if (++index < BTRFS_NR_RAID_TYPES)
  5784. goto again;
  5785. up_read(&info->groups_sem);
  5786. }
  5787. int btrfs_reserve_extent(struct btrfs_root *root,
  5788. u64 num_bytes, u64 min_alloc_size,
  5789. u64 empty_size, u64 hint_byte,
  5790. struct btrfs_key *ins, int is_data)
  5791. {
  5792. bool final_tried = false;
  5793. u64 flags;
  5794. int ret;
  5795. flags = btrfs_get_alloc_profile(root, is_data);
  5796. again:
  5797. WARN_ON(num_bytes < root->sectorsize);
  5798. ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
  5799. flags);
  5800. if (ret == -ENOSPC) {
  5801. if (!final_tried && ins->offset) {
  5802. num_bytes = min(num_bytes >> 1, ins->offset);
  5803. num_bytes = round_down(num_bytes, root->sectorsize);
  5804. num_bytes = max(num_bytes, min_alloc_size);
  5805. if (num_bytes == min_alloc_size)
  5806. final_tried = true;
  5807. goto again;
  5808. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5809. struct btrfs_space_info *sinfo;
  5810. sinfo = __find_space_info(root->fs_info, flags);
  5811. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  5812. flags, num_bytes);
  5813. if (sinfo)
  5814. dump_space_info(sinfo, num_bytes, 1);
  5815. }
  5816. }
  5817. return ret;
  5818. }
  5819. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5820. u64 start, u64 len, int pin)
  5821. {
  5822. struct btrfs_block_group_cache *cache;
  5823. int ret = 0;
  5824. cache = btrfs_lookup_block_group(root->fs_info, start);
  5825. if (!cache) {
  5826. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  5827. start);
  5828. return -ENOSPC;
  5829. }
  5830. if (btrfs_test_opt(root, DISCARD))
  5831. ret = btrfs_discard_extent(root, start, len, NULL);
  5832. if (pin)
  5833. pin_down_extent(root, cache, start, len, 1);
  5834. else {
  5835. btrfs_add_free_space(cache, start, len);
  5836. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5837. }
  5838. btrfs_put_block_group(cache);
  5839. trace_btrfs_reserved_extent_free(root, start, len);
  5840. return ret;
  5841. }
  5842. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5843. u64 start, u64 len)
  5844. {
  5845. return __btrfs_free_reserved_extent(root, start, len, 0);
  5846. }
  5847. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5848. u64 start, u64 len)
  5849. {
  5850. return __btrfs_free_reserved_extent(root, start, len, 1);
  5851. }
  5852. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5853. struct btrfs_root *root,
  5854. u64 parent, u64 root_objectid,
  5855. u64 flags, u64 owner, u64 offset,
  5856. struct btrfs_key *ins, int ref_mod)
  5857. {
  5858. int ret;
  5859. struct btrfs_fs_info *fs_info = root->fs_info;
  5860. struct btrfs_extent_item *extent_item;
  5861. struct btrfs_extent_inline_ref *iref;
  5862. struct btrfs_path *path;
  5863. struct extent_buffer *leaf;
  5864. int type;
  5865. u32 size;
  5866. if (parent > 0)
  5867. type = BTRFS_SHARED_DATA_REF_KEY;
  5868. else
  5869. type = BTRFS_EXTENT_DATA_REF_KEY;
  5870. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5871. path = btrfs_alloc_path();
  5872. if (!path)
  5873. return -ENOMEM;
  5874. path->leave_spinning = 1;
  5875. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5876. ins, size);
  5877. if (ret) {
  5878. btrfs_free_path(path);
  5879. return ret;
  5880. }
  5881. leaf = path->nodes[0];
  5882. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5883. struct btrfs_extent_item);
  5884. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5885. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5886. btrfs_set_extent_flags(leaf, extent_item,
  5887. flags | BTRFS_EXTENT_FLAG_DATA);
  5888. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5889. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5890. if (parent > 0) {
  5891. struct btrfs_shared_data_ref *ref;
  5892. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5893. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5894. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5895. } else {
  5896. struct btrfs_extent_data_ref *ref;
  5897. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5898. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5899. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5900. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5901. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5902. }
  5903. btrfs_mark_buffer_dirty(path->nodes[0]);
  5904. btrfs_free_path(path);
  5905. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5906. if (ret) { /* -ENOENT, logic error */
  5907. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5908. ins->objectid, ins->offset);
  5909. BUG();
  5910. }
  5911. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5912. return ret;
  5913. }
  5914. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5915. struct btrfs_root *root,
  5916. u64 parent, u64 root_objectid,
  5917. u64 flags, struct btrfs_disk_key *key,
  5918. int level, struct btrfs_key *ins)
  5919. {
  5920. int ret;
  5921. struct btrfs_fs_info *fs_info = root->fs_info;
  5922. struct btrfs_extent_item *extent_item;
  5923. struct btrfs_tree_block_info *block_info;
  5924. struct btrfs_extent_inline_ref *iref;
  5925. struct btrfs_path *path;
  5926. struct extent_buffer *leaf;
  5927. u32 size = sizeof(*extent_item) + sizeof(*iref);
  5928. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5929. SKINNY_METADATA);
  5930. if (!skinny_metadata)
  5931. size += sizeof(*block_info);
  5932. path = btrfs_alloc_path();
  5933. if (!path) {
  5934. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  5935. root->leafsize);
  5936. return -ENOMEM;
  5937. }
  5938. path->leave_spinning = 1;
  5939. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5940. ins, size);
  5941. if (ret) {
  5942. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  5943. root->leafsize);
  5944. btrfs_free_path(path);
  5945. return ret;
  5946. }
  5947. leaf = path->nodes[0];
  5948. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5949. struct btrfs_extent_item);
  5950. btrfs_set_extent_refs(leaf, extent_item, 1);
  5951. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5952. btrfs_set_extent_flags(leaf, extent_item,
  5953. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5954. if (skinny_metadata) {
  5955. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5956. } else {
  5957. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5958. btrfs_set_tree_block_key(leaf, block_info, key);
  5959. btrfs_set_tree_block_level(leaf, block_info, level);
  5960. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5961. }
  5962. if (parent > 0) {
  5963. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5964. btrfs_set_extent_inline_ref_type(leaf, iref,
  5965. BTRFS_SHARED_BLOCK_REF_KEY);
  5966. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5967. } else {
  5968. btrfs_set_extent_inline_ref_type(leaf, iref,
  5969. BTRFS_TREE_BLOCK_REF_KEY);
  5970. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5971. }
  5972. btrfs_mark_buffer_dirty(leaf);
  5973. btrfs_free_path(path);
  5974. ret = update_block_group(root, ins->objectid, root->leafsize, 1);
  5975. if (ret) { /* -ENOENT, logic error */
  5976. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5977. ins->objectid, ins->offset);
  5978. BUG();
  5979. }
  5980. trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize);
  5981. return ret;
  5982. }
  5983. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5984. struct btrfs_root *root,
  5985. u64 root_objectid, u64 owner,
  5986. u64 offset, struct btrfs_key *ins)
  5987. {
  5988. int ret;
  5989. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5990. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5991. ins->offset, 0,
  5992. root_objectid, owner, offset,
  5993. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5994. return ret;
  5995. }
  5996. /*
  5997. * this is used by the tree logging recovery code. It records that
  5998. * an extent has been allocated and makes sure to clear the free
  5999. * space cache bits as well
  6000. */
  6001. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  6002. struct btrfs_root *root,
  6003. u64 root_objectid, u64 owner, u64 offset,
  6004. struct btrfs_key *ins)
  6005. {
  6006. int ret;
  6007. struct btrfs_block_group_cache *block_group;
  6008. /*
  6009. * Mixed block groups will exclude before processing the log so we only
  6010. * need to do the exlude dance if this fs isn't mixed.
  6011. */
  6012. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  6013. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  6014. if (ret)
  6015. return ret;
  6016. }
  6017. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  6018. if (!block_group)
  6019. return -EINVAL;
  6020. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  6021. RESERVE_ALLOC_NO_ACCOUNT);
  6022. BUG_ON(ret); /* logic error */
  6023. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  6024. 0, owner, offset, ins, 1);
  6025. btrfs_put_block_group(block_group);
  6026. return ret;
  6027. }
  6028. static struct extent_buffer *
  6029. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  6030. u64 bytenr, u32 blocksize, int level)
  6031. {
  6032. struct extent_buffer *buf;
  6033. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6034. if (!buf)
  6035. return ERR_PTR(-ENOMEM);
  6036. btrfs_set_header_generation(buf, trans->transid);
  6037. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  6038. btrfs_tree_lock(buf);
  6039. clean_tree_block(trans, root, buf);
  6040. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  6041. btrfs_set_lock_blocking(buf);
  6042. btrfs_set_buffer_uptodate(buf);
  6043. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  6044. /*
  6045. * we allow two log transactions at a time, use different
  6046. * EXENT bit to differentiate dirty pages.
  6047. */
  6048. if (root->log_transid % 2 == 0)
  6049. set_extent_dirty(&root->dirty_log_pages, buf->start,
  6050. buf->start + buf->len - 1, GFP_NOFS);
  6051. else
  6052. set_extent_new(&root->dirty_log_pages, buf->start,
  6053. buf->start + buf->len - 1, GFP_NOFS);
  6054. } else {
  6055. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  6056. buf->start + buf->len - 1, GFP_NOFS);
  6057. }
  6058. trans->blocks_used++;
  6059. /* this returns a buffer locked for blocking */
  6060. return buf;
  6061. }
  6062. static struct btrfs_block_rsv *
  6063. use_block_rsv(struct btrfs_trans_handle *trans,
  6064. struct btrfs_root *root, u32 blocksize)
  6065. {
  6066. struct btrfs_block_rsv *block_rsv;
  6067. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  6068. int ret;
  6069. bool global_updated = false;
  6070. block_rsv = get_block_rsv(trans, root);
  6071. if (unlikely(block_rsv->size == 0))
  6072. goto try_reserve;
  6073. again:
  6074. ret = block_rsv_use_bytes(block_rsv, blocksize);
  6075. if (!ret)
  6076. return block_rsv;
  6077. if (block_rsv->failfast)
  6078. return ERR_PTR(ret);
  6079. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  6080. global_updated = true;
  6081. update_global_block_rsv(root->fs_info);
  6082. goto again;
  6083. }
  6084. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6085. static DEFINE_RATELIMIT_STATE(_rs,
  6086. DEFAULT_RATELIMIT_INTERVAL * 10,
  6087. /*DEFAULT_RATELIMIT_BURST*/ 1);
  6088. if (__ratelimit(&_rs))
  6089. WARN(1, KERN_DEBUG
  6090. "BTRFS: block rsv returned %d\n", ret);
  6091. }
  6092. try_reserve:
  6093. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  6094. BTRFS_RESERVE_NO_FLUSH);
  6095. if (!ret)
  6096. return block_rsv;
  6097. /*
  6098. * If we couldn't reserve metadata bytes try and use some from
  6099. * the global reserve if its space type is the same as the global
  6100. * reservation.
  6101. */
  6102. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  6103. block_rsv->space_info == global_rsv->space_info) {
  6104. ret = block_rsv_use_bytes(global_rsv, blocksize);
  6105. if (!ret)
  6106. return global_rsv;
  6107. }
  6108. return ERR_PTR(ret);
  6109. }
  6110. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6111. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6112. {
  6113. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6114. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6115. }
  6116. /*
  6117. * finds a free extent and does all the dirty work required for allocation
  6118. * returns the key for the extent through ins, and a tree buffer for
  6119. * the first block of the extent through buf.
  6120. *
  6121. * returns the tree buffer or NULL.
  6122. */
  6123. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  6124. struct btrfs_root *root, u32 blocksize,
  6125. u64 parent, u64 root_objectid,
  6126. struct btrfs_disk_key *key, int level,
  6127. u64 hint, u64 empty_size)
  6128. {
  6129. struct btrfs_key ins;
  6130. struct btrfs_block_rsv *block_rsv;
  6131. struct extent_buffer *buf;
  6132. u64 flags = 0;
  6133. int ret;
  6134. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6135. SKINNY_METADATA);
  6136. block_rsv = use_block_rsv(trans, root, blocksize);
  6137. if (IS_ERR(block_rsv))
  6138. return ERR_CAST(block_rsv);
  6139. ret = btrfs_reserve_extent(root, blocksize, blocksize,
  6140. empty_size, hint, &ins, 0);
  6141. if (ret) {
  6142. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6143. return ERR_PTR(ret);
  6144. }
  6145. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  6146. blocksize, level);
  6147. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  6148. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6149. if (parent == 0)
  6150. parent = ins.objectid;
  6151. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6152. } else
  6153. BUG_ON(parent > 0);
  6154. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6155. struct btrfs_delayed_extent_op *extent_op;
  6156. extent_op = btrfs_alloc_delayed_extent_op();
  6157. BUG_ON(!extent_op); /* -ENOMEM */
  6158. if (key)
  6159. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6160. else
  6161. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6162. extent_op->flags_to_set = flags;
  6163. if (skinny_metadata)
  6164. extent_op->update_key = 0;
  6165. else
  6166. extent_op->update_key = 1;
  6167. extent_op->update_flags = 1;
  6168. extent_op->is_data = 0;
  6169. extent_op->level = level;
  6170. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6171. ins.objectid,
  6172. ins.offset, parent, root_objectid,
  6173. level, BTRFS_ADD_DELAYED_EXTENT,
  6174. extent_op, 0);
  6175. BUG_ON(ret); /* -ENOMEM */
  6176. }
  6177. return buf;
  6178. }
  6179. struct walk_control {
  6180. u64 refs[BTRFS_MAX_LEVEL];
  6181. u64 flags[BTRFS_MAX_LEVEL];
  6182. struct btrfs_key update_progress;
  6183. int stage;
  6184. int level;
  6185. int shared_level;
  6186. int update_ref;
  6187. int keep_locks;
  6188. int reada_slot;
  6189. int reada_count;
  6190. int for_reloc;
  6191. };
  6192. #define DROP_REFERENCE 1
  6193. #define UPDATE_BACKREF 2
  6194. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6195. struct btrfs_root *root,
  6196. struct walk_control *wc,
  6197. struct btrfs_path *path)
  6198. {
  6199. u64 bytenr;
  6200. u64 generation;
  6201. u64 refs;
  6202. u64 flags;
  6203. u32 nritems;
  6204. u32 blocksize;
  6205. struct btrfs_key key;
  6206. struct extent_buffer *eb;
  6207. int ret;
  6208. int slot;
  6209. int nread = 0;
  6210. if (path->slots[wc->level] < wc->reada_slot) {
  6211. wc->reada_count = wc->reada_count * 2 / 3;
  6212. wc->reada_count = max(wc->reada_count, 2);
  6213. } else {
  6214. wc->reada_count = wc->reada_count * 3 / 2;
  6215. wc->reada_count = min_t(int, wc->reada_count,
  6216. BTRFS_NODEPTRS_PER_BLOCK(root));
  6217. }
  6218. eb = path->nodes[wc->level];
  6219. nritems = btrfs_header_nritems(eb);
  6220. blocksize = btrfs_level_size(root, wc->level - 1);
  6221. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6222. if (nread >= wc->reada_count)
  6223. break;
  6224. cond_resched();
  6225. bytenr = btrfs_node_blockptr(eb, slot);
  6226. generation = btrfs_node_ptr_generation(eb, slot);
  6227. if (slot == path->slots[wc->level])
  6228. goto reada;
  6229. if (wc->stage == UPDATE_BACKREF &&
  6230. generation <= root->root_key.offset)
  6231. continue;
  6232. /* We don't lock the tree block, it's OK to be racy here */
  6233. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6234. wc->level - 1, 1, &refs,
  6235. &flags);
  6236. /* We don't care about errors in readahead. */
  6237. if (ret < 0)
  6238. continue;
  6239. BUG_ON(refs == 0);
  6240. if (wc->stage == DROP_REFERENCE) {
  6241. if (refs == 1)
  6242. goto reada;
  6243. if (wc->level == 1 &&
  6244. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6245. continue;
  6246. if (!wc->update_ref ||
  6247. generation <= root->root_key.offset)
  6248. continue;
  6249. btrfs_node_key_to_cpu(eb, &key, slot);
  6250. ret = btrfs_comp_cpu_keys(&key,
  6251. &wc->update_progress);
  6252. if (ret < 0)
  6253. continue;
  6254. } else {
  6255. if (wc->level == 1 &&
  6256. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6257. continue;
  6258. }
  6259. reada:
  6260. ret = readahead_tree_block(root, bytenr, blocksize,
  6261. generation);
  6262. if (ret)
  6263. break;
  6264. nread++;
  6265. }
  6266. wc->reada_slot = slot;
  6267. }
  6268. /*
  6269. * helper to process tree block while walking down the tree.
  6270. *
  6271. * when wc->stage == UPDATE_BACKREF, this function updates
  6272. * back refs for pointers in the block.
  6273. *
  6274. * NOTE: return value 1 means we should stop walking down.
  6275. */
  6276. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  6277. struct btrfs_root *root,
  6278. struct btrfs_path *path,
  6279. struct walk_control *wc, int lookup_info)
  6280. {
  6281. int level = wc->level;
  6282. struct extent_buffer *eb = path->nodes[level];
  6283. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6284. int ret;
  6285. if (wc->stage == UPDATE_BACKREF &&
  6286. btrfs_header_owner(eb) != root->root_key.objectid)
  6287. return 1;
  6288. /*
  6289. * when reference count of tree block is 1, it won't increase
  6290. * again. once full backref flag is set, we never clear it.
  6291. */
  6292. if (lookup_info &&
  6293. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  6294. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  6295. BUG_ON(!path->locks[level]);
  6296. ret = btrfs_lookup_extent_info(trans, root,
  6297. eb->start, level, 1,
  6298. &wc->refs[level],
  6299. &wc->flags[level]);
  6300. BUG_ON(ret == -ENOMEM);
  6301. if (ret)
  6302. return ret;
  6303. BUG_ON(wc->refs[level] == 0);
  6304. }
  6305. if (wc->stage == DROP_REFERENCE) {
  6306. if (wc->refs[level] > 1)
  6307. return 1;
  6308. if (path->locks[level] && !wc->keep_locks) {
  6309. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6310. path->locks[level] = 0;
  6311. }
  6312. return 0;
  6313. }
  6314. /* wc->stage == UPDATE_BACKREF */
  6315. if (!(wc->flags[level] & flag)) {
  6316. BUG_ON(!path->locks[level]);
  6317. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  6318. BUG_ON(ret); /* -ENOMEM */
  6319. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  6320. BUG_ON(ret); /* -ENOMEM */
  6321. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  6322. eb->len, flag,
  6323. btrfs_header_level(eb), 0);
  6324. BUG_ON(ret); /* -ENOMEM */
  6325. wc->flags[level] |= flag;
  6326. }
  6327. /*
  6328. * the block is shared by multiple trees, so it's not good to
  6329. * keep the tree lock
  6330. */
  6331. if (path->locks[level] && level > 0) {
  6332. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6333. path->locks[level] = 0;
  6334. }
  6335. return 0;
  6336. }
  6337. /*
  6338. * helper to process tree block pointer.
  6339. *
  6340. * when wc->stage == DROP_REFERENCE, this function checks
  6341. * reference count of the block pointed to. if the block
  6342. * is shared and we need update back refs for the subtree
  6343. * rooted at the block, this function changes wc->stage to
  6344. * UPDATE_BACKREF. if the block is shared and there is no
  6345. * need to update back, this function drops the reference
  6346. * to the block.
  6347. *
  6348. * NOTE: return value 1 means we should stop walking down.
  6349. */
  6350. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  6351. struct btrfs_root *root,
  6352. struct btrfs_path *path,
  6353. struct walk_control *wc, int *lookup_info)
  6354. {
  6355. u64 bytenr;
  6356. u64 generation;
  6357. u64 parent;
  6358. u32 blocksize;
  6359. struct btrfs_key key;
  6360. struct extent_buffer *next;
  6361. int level = wc->level;
  6362. int reada = 0;
  6363. int ret = 0;
  6364. generation = btrfs_node_ptr_generation(path->nodes[level],
  6365. path->slots[level]);
  6366. /*
  6367. * if the lower level block was created before the snapshot
  6368. * was created, we know there is no need to update back refs
  6369. * for the subtree
  6370. */
  6371. if (wc->stage == UPDATE_BACKREF &&
  6372. generation <= root->root_key.offset) {
  6373. *lookup_info = 1;
  6374. return 1;
  6375. }
  6376. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  6377. blocksize = btrfs_level_size(root, level - 1);
  6378. next = btrfs_find_tree_block(root, bytenr, blocksize);
  6379. if (!next) {
  6380. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6381. if (!next)
  6382. return -ENOMEM;
  6383. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  6384. level - 1);
  6385. reada = 1;
  6386. }
  6387. btrfs_tree_lock(next);
  6388. btrfs_set_lock_blocking(next);
  6389. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  6390. &wc->refs[level - 1],
  6391. &wc->flags[level - 1]);
  6392. if (ret < 0) {
  6393. btrfs_tree_unlock(next);
  6394. return ret;
  6395. }
  6396. if (unlikely(wc->refs[level - 1] == 0)) {
  6397. btrfs_err(root->fs_info, "Missing references.");
  6398. BUG();
  6399. }
  6400. *lookup_info = 0;
  6401. if (wc->stage == DROP_REFERENCE) {
  6402. if (wc->refs[level - 1] > 1) {
  6403. if (level == 1 &&
  6404. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6405. goto skip;
  6406. if (!wc->update_ref ||
  6407. generation <= root->root_key.offset)
  6408. goto skip;
  6409. btrfs_node_key_to_cpu(path->nodes[level], &key,
  6410. path->slots[level]);
  6411. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  6412. if (ret < 0)
  6413. goto skip;
  6414. wc->stage = UPDATE_BACKREF;
  6415. wc->shared_level = level - 1;
  6416. }
  6417. } else {
  6418. if (level == 1 &&
  6419. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6420. goto skip;
  6421. }
  6422. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  6423. btrfs_tree_unlock(next);
  6424. free_extent_buffer(next);
  6425. next = NULL;
  6426. *lookup_info = 1;
  6427. }
  6428. if (!next) {
  6429. if (reada && level == 1)
  6430. reada_walk_down(trans, root, wc, path);
  6431. next = read_tree_block(root, bytenr, blocksize, generation);
  6432. if (!next || !extent_buffer_uptodate(next)) {
  6433. free_extent_buffer(next);
  6434. return -EIO;
  6435. }
  6436. btrfs_tree_lock(next);
  6437. btrfs_set_lock_blocking(next);
  6438. }
  6439. level--;
  6440. BUG_ON(level != btrfs_header_level(next));
  6441. path->nodes[level] = next;
  6442. path->slots[level] = 0;
  6443. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6444. wc->level = level;
  6445. if (wc->level == 1)
  6446. wc->reada_slot = 0;
  6447. return 0;
  6448. skip:
  6449. wc->refs[level - 1] = 0;
  6450. wc->flags[level - 1] = 0;
  6451. if (wc->stage == DROP_REFERENCE) {
  6452. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  6453. parent = path->nodes[level]->start;
  6454. } else {
  6455. BUG_ON(root->root_key.objectid !=
  6456. btrfs_header_owner(path->nodes[level]));
  6457. parent = 0;
  6458. }
  6459. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  6460. root->root_key.objectid, level - 1, 0, 0);
  6461. BUG_ON(ret); /* -ENOMEM */
  6462. }
  6463. btrfs_tree_unlock(next);
  6464. free_extent_buffer(next);
  6465. *lookup_info = 1;
  6466. return 1;
  6467. }
  6468. /*
  6469. * helper to process tree block while walking up the tree.
  6470. *
  6471. * when wc->stage == DROP_REFERENCE, this function drops
  6472. * reference count on the block.
  6473. *
  6474. * when wc->stage == UPDATE_BACKREF, this function changes
  6475. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  6476. * to UPDATE_BACKREF previously while processing the block.
  6477. *
  6478. * NOTE: return value 1 means we should stop walking up.
  6479. */
  6480. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  6481. struct btrfs_root *root,
  6482. struct btrfs_path *path,
  6483. struct walk_control *wc)
  6484. {
  6485. int ret;
  6486. int level = wc->level;
  6487. struct extent_buffer *eb = path->nodes[level];
  6488. u64 parent = 0;
  6489. if (wc->stage == UPDATE_BACKREF) {
  6490. BUG_ON(wc->shared_level < level);
  6491. if (level < wc->shared_level)
  6492. goto out;
  6493. ret = find_next_key(path, level + 1, &wc->update_progress);
  6494. if (ret > 0)
  6495. wc->update_ref = 0;
  6496. wc->stage = DROP_REFERENCE;
  6497. wc->shared_level = -1;
  6498. path->slots[level] = 0;
  6499. /*
  6500. * check reference count again if the block isn't locked.
  6501. * we should start walking down the tree again if reference
  6502. * count is one.
  6503. */
  6504. if (!path->locks[level]) {
  6505. BUG_ON(level == 0);
  6506. btrfs_tree_lock(eb);
  6507. btrfs_set_lock_blocking(eb);
  6508. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6509. ret = btrfs_lookup_extent_info(trans, root,
  6510. eb->start, level, 1,
  6511. &wc->refs[level],
  6512. &wc->flags[level]);
  6513. if (ret < 0) {
  6514. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6515. path->locks[level] = 0;
  6516. return ret;
  6517. }
  6518. BUG_ON(wc->refs[level] == 0);
  6519. if (wc->refs[level] == 1) {
  6520. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6521. path->locks[level] = 0;
  6522. return 1;
  6523. }
  6524. }
  6525. }
  6526. /* wc->stage == DROP_REFERENCE */
  6527. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6528. if (wc->refs[level] == 1) {
  6529. if (level == 0) {
  6530. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6531. ret = btrfs_dec_ref(trans, root, eb, 1,
  6532. wc->for_reloc);
  6533. else
  6534. ret = btrfs_dec_ref(trans, root, eb, 0,
  6535. wc->for_reloc);
  6536. BUG_ON(ret); /* -ENOMEM */
  6537. }
  6538. /* make block locked assertion in clean_tree_block happy */
  6539. if (!path->locks[level] &&
  6540. btrfs_header_generation(eb) == trans->transid) {
  6541. btrfs_tree_lock(eb);
  6542. btrfs_set_lock_blocking(eb);
  6543. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6544. }
  6545. clean_tree_block(trans, root, eb);
  6546. }
  6547. if (eb == root->node) {
  6548. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6549. parent = eb->start;
  6550. else
  6551. BUG_ON(root->root_key.objectid !=
  6552. btrfs_header_owner(eb));
  6553. } else {
  6554. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6555. parent = path->nodes[level + 1]->start;
  6556. else
  6557. BUG_ON(root->root_key.objectid !=
  6558. btrfs_header_owner(path->nodes[level + 1]));
  6559. }
  6560. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6561. out:
  6562. wc->refs[level] = 0;
  6563. wc->flags[level] = 0;
  6564. return 0;
  6565. }
  6566. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6567. struct btrfs_root *root,
  6568. struct btrfs_path *path,
  6569. struct walk_control *wc)
  6570. {
  6571. int level = wc->level;
  6572. int lookup_info = 1;
  6573. int ret;
  6574. while (level >= 0) {
  6575. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6576. if (ret > 0)
  6577. break;
  6578. if (level == 0)
  6579. break;
  6580. if (path->slots[level] >=
  6581. btrfs_header_nritems(path->nodes[level]))
  6582. break;
  6583. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6584. if (ret > 0) {
  6585. path->slots[level]++;
  6586. continue;
  6587. } else if (ret < 0)
  6588. return ret;
  6589. level = wc->level;
  6590. }
  6591. return 0;
  6592. }
  6593. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6594. struct btrfs_root *root,
  6595. struct btrfs_path *path,
  6596. struct walk_control *wc, int max_level)
  6597. {
  6598. int level = wc->level;
  6599. int ret;
  6600. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6601. while (level < max_level && path->nodes[level]) {
  6602. wc->level = level;
  6603. if (path->slots[level] + 1 <
  6604. btrfs_header_nritems(path->nodes[level])) {
  6605. path->slots[level]++;
  6606. return 0;
  6607. } else {
  6608. ret = walk_up_proc(trans, root, path, wc);
  6609. if (ret > 0)
  6610. return 0;
  6611. if (path->locks[level]) {
  6612. btrfs_tree_unlock_rw(path->nodes[level],
  6613. path->locks[level]);
  6614. path->locks[level] = 0;
  6615. }
  6616. free_extent_buffer(path->nodes[level]);
  6617. path->nodes[level] = NULL;
  6618. level++;
  6619. }
  6620. }
  6621. return 1;
  6622. }
  6623. /*
  6624. * drop a subvolume tree.
  6625. *
  6626. * this function traverses the tree freeing any blocks that only
  6627. * referenced by the tree.
  6628. *
  6629. * when a shared tree block is found. this function decreases its
  6630. * reference count by one. if update_ref is true, this function
  6631. * also make sure backrefs for the shared block and all lower level
  6632. * blocks are properly updated.
  6633. *
  6634. * If called with for_reloc == 0, may exit early with -EAGAIN
  6635. */
  6636. int btrfs_drop_snapshot(struct btrfs_root *root,
  6637. struct btrfs_block_rsv *block_rsv, int update_ref,
  6638. int for_reloc)
  6639. {
  6640. struct btrfs_path *path;
  6641. struct btrfs_trans_handle *trans;
  6642. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6643. struct btrfs_root_item *root_item = &root->root_item;
  6644. struct walk_control *wc;
  6645. struct btrfs_key key;
  6646. int err = 0;
  6647. int ret;
  6648. int level;
  6649. bool root_dropped = false;
  6650. path = btrfs_alloc_path();
  6651. if (!path) {
  6652. err = -ENOMEM;
  6653. goto out;
  6654. }
  6655. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6656. if (!wc) {
  6657. btrfs_free_path(path);
  6658. err = -ENOMEM;
  6659. goto out;
  6660. }
  6661. trans = btrfs_start_transaction(tree_root, 0);
  6662. if (IS_ERR(trans)) {
  6663. err = PTR_ERR(trans);
  6664. goto out_free;
  6665. }
  6666. if (block_rsv)
  6667. trans->block_rsv = block_rsv;
  6668. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6669. level = btrfs_header_level(root->node);
  6670. path->nodes[level] = btrfs_lock_root_node(root);
  6671. btrfs_set_lock_blocking(path->nodes[level]);
  6672. path->slots[level] = 0;
  6673. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6674. memset(&wc->update_progress, 0,
  6675. sizeof(wc->update_progress));
  6676. } else {
  6677. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6678. memcpy(&wc->update_progress, &key,
  6679. sizeof(wc->update_progress));
  6680. level = root_item->drop_level;
  6681. BUG_ON(level == 0);
  6682. path->lowest_level = level;
  6683. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6684. path->lowest_level = 0;
  6685. if (ret < 0) {
  6686. err = ret;
  6687. goto out_end_trans;
  6688. }
  6689. WARN_ON(ret > 0);
  6690. /*
  6691. * unlock our path, this is safe because only this
  6692. * function is allowed to delete this snapshot
  6693. */
  6694. btrfs_unlock_up_safe(path, 0);
  6695. level = btrfs_header_level(root->node);
  6696. while (1) {
  6697. btrfs_tree_lock(path->nodes[level]);
  6698. btrfs_set_lock_blocking(path->nodes[level]);
  6699. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6700. ret = btrfs_lookup_extent_info(trans, root,
  6701. path->nodes[level]->start,
  6702. level, 1, &wc->refs[level],
  6703. &wc->flags[level]);
  6704. if (ret < 0) {
  6705. err = ret;
  6706. goto out_end_trans;
  6707. }
  6708. BUG_ON(wc->refs[level] == 0);
  6709. if (level == root_item->drop_level)
  6710. break;
  6711. btrfs_tree_unlock(path->nodes[level]);
  6712. path->locks[level] = 0;
  6713. WARN_ON(wc->refs[level] != 1);
  6714. level--;
  6715. }
  6716. }
  6717. wc->level = level;
  6718. wc->shared_level = -1;
  6719. wc->stage = DROP_REFERENCE;
  6720. wc->update_ref = update_ref;
  6721. wc->keep_locks = 0;
  6722. wc->for_reloc = for_reloc;
  6723. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6724. while (1) {
  6725. ret = walk_down_tree(trans, root, path, wc);
  6726. if (ret < 0) {
  6727. err = ret;
  6728. break;
  6729. }
  6730. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6731. if (ret < 0) {
  6732. err = ret;
  6733. break;
  6734. }
  6735. if (ret > 0) {
  6736. BUG_ON(wc->stage != DROP_REFERENCE);
  6737. break;
  6738. }
  6739. if (wc->stage == DROP_REFERENCE) {
  6740. level = wc->level;
  6741. btrfs_node_key(path->nodes[level],
  6742. &root_item->drop_progress,
  6743. path->slots[level]);
  6744. root_item->drop_level = level;
  6745. }
  6746. BUG_ON(wc->level == 0);
  6747. if (btrfs_should_end_transaction(trans, tree_root) ||
  6748. (!for_reloc && btrfs_need_cleaner_sleep(root))) {
  6749. ret = btrfs_update_root(trans, tree_root,
  6750. &root->root_key,
  6751. root_item);
  6752. if (ret) {
  6753. btrfs_abort_transaction(trans, tree_root, ret);
  6754. err = ret;
  6755. goto out_end_trans;
  6756. }
  6757. btrfs_end_transaction_throttle(trans, tree_root);
  6758. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  6759. pr_debug("BTRFS: drop snapshot early exit\n");
  6760. err = -EAGAIN;
  6761. goto out_free;
  6762. }
  6763. trans = btrfs_start_transaction(tree_root, 0);
  6764. if (IS_ERR(trans)) {
  6765. err = PTR_ERR(trans);
  6766. goto out_free;
  6767. }
  6768. if (block_rsv)
  6769. trans->block_rsv = block_rsv;
  6770. }
  6771. }
  6772. btrfs_release_path(path);
  6773. if (err)
  6774. goto out_end_trans;
  6775. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6776. if (ret) {
  6777. btrfs_abort_transaction(trans, tree_root, ret);
  6778. goto out_end_trans;
  6779. }
  6780. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6781. ret = btrfs_find_root(tree_root, &root->root_key, path,
  6782. NULL, NULL);
  6783. if (ret < 0) {
  6784. btrfs_abort_transaction(trans, tree_root, ret);
  6785. err = ret;
  6786. goto out_end_trans;
  6787. } else if (ret > 0) {
  6788. /* if we fail to delete the orphan item this time
  6789. * around, it'll get picked up the next time.
  6790. *
  6791. * The most common failure here is just -ENOENT.
  6792. */
  6793. btrfs_del_orphan_item(trans, tree_root,
  6794. root->root_key.objectid);
  6795. }
  6796. }
  6797. if (root->in_radix) {
  6798. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  6799. } else {
  6800. free_extent_buffer(root->node);
  6801. free_extent_buffer(root->commit_root);
  6802. btrfs_put_fs_root(root);
  6803. }
  6804. root_dropped = true;
  6805. out_end_trans:
  6806. btrfs_end_transaction_throttle(trans, tree_root);
  6807. out_free:
  6808. kfree(wc);
  6809. btrfs_free_path(path);
  6810. out:
  6811. /*
  6812. * So if we need to stop dropping the snapshot for whatever reason we
  6813. * need to make sure to add it back to the dead root list so that we
  6814. * keep trying to do the work later. This also cleans up roots if we
  6815. * don't have it in the radix (like when we recover after a power fail
  6816. * or unmount) so we don't leak memory.
  6817. */
  6818. if (!for_reloc && root_dropped == false)
  6819. btrfs_add_dead_root(root);
  6820. if (err && err != -EAGAIN)
  6821. btrfs_std_error(root->fs_info, err);
  6822. return err;
  6823. }
  6824. /*
  6825. * drop subtree rooted at tree block 'node'.
  6826. *
  6827. * NOTE: this function will unlock and release tree block 'node'
  6828. * only used by relocation code
  6829. */
  6830. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6831. struct btrfs_root *root,
  6832. struct extent_buffer *node,
  6833. struct extent_buffer *parent)
  6834. {
  6835. struct btrfs_path *path;
  6836. struct walk_control *wc;
  6837. int level;
  6838. int parent_level;
  6839. int ret = 0;
  6840. int wret;
  6841. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6842. path = btrfs_alloc_path();
  6843. if (!path)
  6844. return -ENOMEM;
  6845. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6846. if (!wc) {
  6847. btrfs_free_path(path);
  6848. return -ENOMEM;
  6849. }
  6850. btrfs_assert_tree_locked(parent);
  6851. parent_level = btrfs_header_level(parent);
  6852. extent_buffer_get(parent);
  6853. path->nodes[parent_level] = parent;
  6854. path->slots[parent_level] = btrfs_header_nritems(parent);
  6855. btrfs_assert_tree_locked(node);
  6856. level = btrfs_header_level(node);
  6857. path->nodes[level] = node;
  6858. path->slots[level] = 0;
  6859. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6860. wc->refs[parent_level] = 1;
  6861. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6862. wc->level = level;
  6863. wc->shared_level = -1;
  6864. wc->stage = DROP_REFERENCE;
  6865. wc->update_ref = 0;
  6866. wc->keep_locks = 1;
  6867. wc->for_reloc = 1;
  6868. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6869. while (1) {
  6870. wret = walk_down_tree(trans, root, path, wc);
  6871. if (wret < 0) {
  6872. ret = wret;
  6873. break;
  6874. }
  6875. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6876. if (wret < 0)
  6877. ret = wret;
  6878. if (wret != 0)
  6879. break;
  6880. }
  6881. kfree(wc);
  6882. btrfs_free_path(path);
  6883. return ret;
  6884. }
  6885. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6886. {
  6887. u64 num_devices;
  6888. u64 stripped;
  6889. /*
  6890. * if restripe for this chunk_type is on pick target profile and
  6891. * return, otherwise do the usual balance
  6892. */
  6893. stripped = get_restripe_target(root->fs_info, flags);
  6894. if (stripped)
  6895. return extended_to_chunk(stripped);
  6896. /*
  6897. * we add in the count of missing devices because we want
  6898. * to make sure that any RAID levels on a degraded FS
  6899. * continue to be honored.
  6900. */
  6901. num_devices = root->fs_info->fs_devices->rw_devices +
  6902. root->fs_info->fs_devices->missing_devices;
  6903. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6904. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  6905. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6906. if (num_devices == 1) {
  6907. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6908. stripped = flags & ~stripped;
  6909. /* turn raid0 into single device chunks */
  6910. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6911. return stripped;
  6912. /* turn mirroring into duplication */
  6913. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6914. BTRFS_BLOCK_GROUP_RAID10))
  6915. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6916. } else {
  6917. /* they already had raid on here, just return */
  6918. if (flags & stripped)
  6919. return flags;
  6920. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6921. stripped = flags & ~stripped;
  6922. /* switch duplicated blocks with raid1 */
  6923. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6924. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6925. /* this is drive concat, leave it alone */
  6926. }
  6927. return flags;
  6928. }
  6929. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6930. {
  6931. struct btrfs_space_info *sinfo = cache->space_info;
  6932. u64 num_bytes;
  6933. u64 min_allocable_bytes;
  6934. int ret = -ENOSPC;
  6935. /*
  6936. * We need some metadata space and system metadata space for
  6937. * allocating chunks in some corner cases until we force to set
  6938. * it to be readonly.
  6939. */
  6940. if ((sinfo->flags &
  6941. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6942. !force)
  6943. min_allocable_bytes = 1 * 1024 * 1024;
  6944. else
  6945. min_allocable_bytes = 0;
  6946. spin_lock(&sinfo->lock);
  6947. spin_lock(&cache->lock);
  6948. if (cache->ro) {
  6949. ret = 0;
  6950. goto out;
  6951. }
  6952. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6953. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6954. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6955. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6956. min_allocable_bytes <= sinfo->total_bytes) {
  6957. sinfo->bytes_readonly += num_bytes;
  6958. cache->ro = 1;
  6959. ret = 0;
  6960. }
  6961. out:
  6962. spin_unlock(&cache->lock);
  6963. spin_unlock(&sinfo->lock);
  6964. return ret;
  6965. }
  6966. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6967. struct btrfs_block_group_cache *cache)
  6968. {
  6969. struct btrfs_trans_handle *trans;
  6970. u64 alloc_flags;
  6971. int ret;
  6972. BUG_ON(cache->ro);
  6973. trans = btrfs_join_transaction(root);
  6974. if (IS_ERR(trans))
  6975. return PTR_ERR(trans);
  6976. alloc_flags = update_block_group_flags(root, cache->flags);
  6977. if (alloc_flags != cache->flags) {
  6978. ret = do_chunk_alloc(trans, root, alloc_flags,
  6979. CHUNK_ALLOC_FORCE);
  6980. if (ret < 0)
  6981. goto out;
  6982. }
  6983. ret = set_block_group_ro(cache, 0);
  6984. if (!ret)
  6985. goto out;
  6986. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6987. ret = do_chunk_alloc(trans, root, alloc_flags,
  6988. CHUNK_ALLOC_FORCE);
  6989. if (ret < 0)
  6990. goto out;
  6991. ret = set_block_group_ro(cache, 0);
  6992. out:
  6993. btrfs_end_transaction(trans, root);
  6994. return ret;
  6995. }
  6996. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6997. struct btrfs_root *root, u64 type)
  6998. {
  6999. u64 alloc_flags = get_alloc_profile(root, type);
  7000. return do_chunk_alloc(trans, root, alloc_flags,
  7001. CHUNK_ALLOC_FORCE);
  7002. }
  7003. /*
  7004. * helper to account the unused space of all the readonly block group in the
  7005. * list. takes mirrors into account.
  7006. */
  7007. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  7008. {
  7009. struct btrfs_block_group_cache *block_group;
  7010. u64 free_bytes = 0;
  7011. int factor;
  7012. list_for_each_entry(block_group, groups_list, list) {
  7013. spin_lock(&block_group->lock);
  7014. if (!block_group->ro) {
  7015. spin_unlock(&block_group->lock);
  7016. continue;
  7017. }
  7018. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7019. BTRFS_BLOCK_GROUP_RAID10 |
  7020. BTRFS_BLOCK_GROUP_DUP))
  7021. factor = 2;
  7022. else
  7023. factor = 1;
  7024. free_bytes += (block_group->key.offset -
  7025. btrfs_block_group_used(&block_group->item)) *
  7026. factor;
  7027. spin_unlock(&block_group->lock);
  7028. }
  7029. return free_bytes;
  7030. }
  7031. /*
  7032. * helper to account the unused space of all the readonly block group in the
  7033. * space_info. takes mirrors into account.
  7034. */
  7035. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  7036. {
  7037. int i;
  7038. u64 free_bytes = 0;
  7039. spin_lock(&sinfo->lock);
  7040. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  7041. if (!list_empty(&sinfo->block_groups[i]))
  7042. free_bytes += __btrfs_get_ro_block_group_free_space(
  7043. &sinfo->block_groups[i]);
  7044. spin_unlock(&sinfo->lock);
  7045. return free_bytes;
  7046. }
  7047. void btrfs_set_block_group_rw(struct btrfs_root *root,
  7048. struct btrfs_block_group_cache *cache)
  7049. {
  7050. struct btrfs_space_info *sinfo = cache->space_info;
  7051. u64 num_bytes;
  7052. BUG_ON(!cache->ro);
  7053. spin_lock(&sinfo->lock);
  7054. spin_lock(&cache->lock);
  7055. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7056. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7057. sinfo->bytes_readonly -= num_bytes;
  7058. cache->ro = 0;
  7059. spin_unlock(&cache->lock);
  7060. spin_unlock(&sinfo->lock);
  7061. }
  7062. /*
  7063. * checks to see if its even possible to relocate this block group.
  7064. *
  7065. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7066. * ok to go ahead and try.
  7067. */
  7068. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7069. {
  7070. struct btrfs_block_group_cache *block_group;
  7071. struct btrfs_space_info *space_info;
  7072. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7073. struct btrfs_device *device;
  7074. struct btrfs_trans_handle *trans;
  7075. u64 min_free;
  7076. u64 dev_min = 1;
  7077. u64 dev_nr = 0;
  7078. u64 target;
  7079. int index;
  7080. int full = 0;
  7081. int ret = 0;
  7082. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7083. /* odd, couldn't find the block group, leave it alone */
  7084. if (!block_group)
  7085. return -1;
  7086. min_free = btrfs_block_group_used(&block_group->item);
  7087. /* no bytes used, we're good */
  7088. if (!min_free)
  7089. goto out;
  7090. space_info = block_group->space_info;
  7091. spin_lock(&space_info->lock);
  7092. full = space_info->full;
  7093. /*
  7094. * if this is the last block group we have in this space, we can't
  7095. * relocate it unless we're able to allocate a new chunk below.
  7096. *
  7097. * Otherwise, we need to make sure we have room in the space to handle
  7098. * all of the extents from this block group. If we can, we're good
  7099. */
  7100. if ((space_info->total_bytes != block_group->key.offset) &&
  7101. (space_info->bytes_used + space_info->bytes_reserved +
  7102. space_info->bytes_pinned + space_info->bytes_readonly +
  7103. min_free < space_info->total_bytes)) {
  7104. spin_unlock(&space_info->lock);
  7105. goto out;
  7106. }
  7107. spin_unlock(&space_info->lock);
  7108. /*
  7109. * ok we don't have enough space, but maybe we have free space on our
  7110. * devices to allocate new chunks for relocation, so loop through our
  7111. * alloc devices and guess if we have enough space. if this block
  7112. * group is going to be restriped, run checks against the target
  7113. * profile instead of the current one.
  7114. */
  7115. ret = -1;
  7116. /*
  7117. * index:
  7118. * 0: raid10
  7119. * 1: raid1
  7120. * 2: dup
  7121. * 3: raid0
  7122. * 4: single
  7123. */
  7124. target = get_restripe_target(root->fs_info, block_group->flags);
  7125. if (target) {
  7126. index = __get_raid_index(extended_to_chunk(target));
  7127. } else {
  7128. /*
  7129. * this is just a balance, so if we were marked as full
  7130. * we know there is no space for a new chunk
  7131. */
  7132. if (full)
  7133. goto out;
  7134. index = get_block_group_index(block_group);
  7135. }
  7136. if (index == BTRFS_RAID_RAID10) {
  7137. dev_min = 4;
  7138. /* Divide by 2 */
  7139. min_free >>= 1;
  7140. } else if (index == BTRFS_RAID_RAID1) {
  7141. dev_min = 2;
  7142. } else if (index == BTRFS_RAID_DUP) {
  7143. /* Multiply by 2 */
  7144. min_free <<= 1;
  7145. } else if (index == BTRFS_RAID_RAID0) {
  7146. dev_min = fs_devices->rw_devices;
  7147. do_div(min_free, dev_min);
  7148. }
  7149. /* We need to do this so that we can look at pending chunks */
  7150. trans = btrfs_join_transaction(root);
  7151. if (IS_ERR(trans)) {
  7152. ret = PTR_ERR(trans);
  7153. goto out;
  7154. }
  7155. mutex_lock(&root->fs_info->chunk_mutex);
  7156. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7157. u64 dev_offset;
  7158. /*
  7159. * check to make sure we can actually find a chunk with enough
  7160. * space to fit our block group in.
  7161. */
  7162. if (device->total_bytes > device->bytes_used + min_free &&
  7163. !device->is_tgtdev_for_dev_replace) {
  7164. ret = find_free_dev_extent(trans, device, min_free,
  7165. &dev_offset, NULL);
  7166. if (!ret)
  7167. dev_nr++;
  7168. if (dev_nr >= dev_min)
  7169. break;
  7170. ret = -1;
  7171. }
  7172. }
  7173. mutex_unlock(&root->fs_info->chunk_mutex);
  7174. btrfs_end_transaction(trans, root);
  7175. out:
  7176. btrfs_put_block_group(block_group);
  7177. return ret;
  7178. }
  7179. static int find_first_block_group(struct btrfs_root *root,
  7180. struct btrfs_path *path, struct btrfs_key *key)
  7181. {
  7182. int ret = 0;
  7183. struct btrfs_key found_key;
  7184. struct extent_buffer *leaf;
  7185. int slot;
  7186. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7187. if (ret < 0)
  7188. goto out;
  7189. while (1) {
  7190. slot = path->slots[0];
  7191. leaf = path->nodes[0];
  7192. if (slot >= btrfs_header_nritems(leaf)) {
  7193. ret = btrfs_next_leaf(root, path);
  7194. if (ret == 0)
  7195. continue;
  7196. if (ret < 0)
  7197. goto out;
  7198. break;
  7199. }
  7200. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7201. if (found_key.objectid >= key->objectid &&
  7202. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7203. ret = 0;
  7204. goto out;
  7205. }
  7206. path->slots[0]++;
  7207. }
  7208. out:
  7209. return ret;
  7210. }
  7211. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7212. {
  7213. struct btrfs_block_group_cache *block_group;
  7214. u64 last = 0;
  7215. while (1) {
  7216. struct inode *inode;
  7217. block_group = btrfs_lookup_first_block_group(info, last);
  7218. while (block_group) {
  7219. spin_lock(&block_group->lock);
  7220. if (block_group->iref)
  7221. break;
  7222. spin_unlock(&block_group->lock);
  7223. block_group = next_block_group(info->tree_root,
  7224. block_group);
  7225. }
  7226. if (!block_group) {
  7227. if (last == 0)
  7228. break;
  7229. last = 0;
  7230. continue;
  7231. }
  7232. inode = block_group->inode;
  7233. block_group->iref = 0;
  7234. block_group->inode = NULL;
  7235. spin_unlock(&block_group->lock);
  7236. iput(inode);
  7237. last = block_group->key.objectid + block_group->key.offset;
  7238. btrfs_put_block_group(block_group);
  7239. }
  7240. }
  7241. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7242. {
  7243. struct btrfs_block_group_cache *block_group;
  7244. struct btrfs_space_info *space_info;
  7245. struct btrfs_caching_control *caching_ctl;
  7246. struct rb_node *n;
  7247. down_write(&info->extent_commit_sem);
  7248. while (!list_empty(&info->caching_block_groups)) {
  7249. caching_ctl = list_entry(info->caching_block_groups.next,
  7250. struct btrfs_caching_control, list);
  7251. list_del(&caching_ctl->list);
  7252. put_caching_control(caching_ctl);
  7253. }
  7254. up_write(&info->extent_commit_sem);
  7255. spin_lock(&info->block_group_cache_lock);
  7256. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7257. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7258. cache_node);
  7259. rb_erase(&block_group->cache_node,
  7260. &info->block_group_cache_tree);
  7261. spin_unlock(&info->block_group_cache_lock);
  7262. down_write(&block_group->space_info->groups_sem);
  7263. list_del(&block_group->list);
  7264. up_write(&block_group->space_info->groups_sem);
  7265. if (block_group->cached == BTRFS_CACHE_STARTED)
  7266. wait_block_group_cache_done(block_group);
  7267. /*
  7268. * We haven't cached this block group, which means we could
  7269. * possibly have excluded extents on this block group.
  7270. */
  7271. if (block_group->cached == BTRFS_CACHE_NO ||
  7272. block_group->cached == BTRFS_CACHE_ERROR)
  7273. free_excluded_extents(info->extent_root, block_group);
  7274. btrfs_remove_free_space_cache(block_group);
  7275. btrfs_put_block_group(block_group);
  7276. spin_lock(&info->block_group_cache_lock);
  7277. }
  7278. spin_unlock(&info->block_group_cache_lock);
  7279. /* now that all the block groups are freed, go through and
  7280. * free all the space_info structs. This is only called during
  7281. * the final stages of unmount, and so we know nobody is
  7282. * using them. We call synchronize_rcu() once before we start,
  7283. * just to be on the safe side.
  7284. */
  7285. synchronize_rcu();
  7286. release_global_block_rsv(info);
  7287. while (!list_empty(&info->space_info)) {
  7288. int i;
  7289. space_info = list_entry(info->space_info.next,
  7290. struct btrfs_space_info,
  7291. list);
  7292. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  7293. if (WARN_ON(space_info->bytes_pinned > 0 ||
  7294. space_info->bytes_reserved > 0 ||
  7295. space_info->bytes_may_use > 0)) {
  7296. dump_space_info(space_info, 0, 0);
  7297. }
  7298. }
  7299. list_del(&space_info->list);
  7300. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  7301. struct kobject *kobj;
  7302. kobj = &space_info->block_group_kobjs[i];
  7303. if (kobj->parent) {
  7304. kobject_del(kobj);
  7305. kobject_put(kobj);
  7306. }
  7307. }
  7308. kobject_del(&space_info->kobj);
  7309. kobject_put(&space_info->kobj);
  7310. }
  7311. return 0;
  7312. }
  7313. static void __link_block_group(struct btrfs_space_info *space_info,
  7314. struct btrfs_block_group_cache *cache)
  7315. {
  7316. int index = get_block_group_index(cache);
  7317. down_write(&space_info->groups_sem);
  7318. if (list_empty(&space_info->block_groups[index])) {
  7319. struct kobject *kobj = &space_info->block_group_kobjs[index];
  7320. int ret;
  7321. kobject_get(&space_info->kobj); /* put in release */
  7322. ret = kobject_add(kobj, &space_info->kobj, "%s",
  7323. get_raid_name(index));
  7324. if (ret) {
  7325. pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
  7326. kobject_put(&space_info->kobj);
  7327. }
  7328. }
  7329. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7330. up_write(&space_info->groups_sem);
  7331. }
  7332. int btrfs_read_block_groups(struct btrfs_root *root)
  7333. {
  7334. struct btrfs_path *path;
  7335. int ret;
  7336. struct btrfs_block_group_cache *cache;
  7337. struct btrfs_fs_info *info = root->fs_info;
  7338. struct btrfs_space_info *space_info;
  7339. struct btrfs_key key;
  7340. struct btrfs_key found_key;
  7341. struct extent_buffer *leaf;
  7342. int need_clear = 0;
  7343. u64 cache_gen;
  7344. root = info->extent_root;
  7345. key.objectid = 0;
  7346. key.offset = 0;
  7347. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7348. path = btrfs_alloc_path();
  7349. if (!path)
  7350. return -ENOMEM;
  7351. path->reada = 1;
  7352. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  7353. if (btrfs_test_opt(root, SPACE_CACHE) &&
  7354. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  7355. need_clear = 1;
  7356. if (btrfs_test_opt(root, CLEAR_CACHE))
  7357. need_clear = 1;
  7358. while (1) {
  7359. ret = find_first_block_group(root, path, &key);
  7360. if (ret > 0)
  7361. break;
  7362. if (ret != 0)
  7363. goto error;
  7364. leaf = path->nodes[0];
  7365. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7366. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7367. if (!cache) {
  7368. ret = -ENOMEM;
  7369. goto error;
  7370. }
  7371. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7372. GFP_NOFS);
  7373. if (!cache->free_space_ctl) {
  7374. kfree(cache);
  7375. ret = -ENOMEM;
  7376. goto error;
  7377. }
  7378. atomic_set(&cache->count, 1);
  7379. spin_lock_init(&cache->lock);
  7380. cache->fs_info = info;
  7381. INIT_LIST_HEAD(&cache->list);
  7382. INIT_LIST_HEAD(&cache->cluster_list);
  7383. if (need_clear) {
  7384. /*
  7385. * When we mount with old space cache, we need to
  7386. * set BTRFS_DC_CLEAR and set dirty flag.
  7387. *
  7388. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  7389. * truncate the old free space cache inode and
  7390. * setup a new one.
  7391. * b) Setting 'dirty flag' makes sure that we flush
  7392. * the new space cache info onto disk.
  7393. */
  7394. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7395. if (btrfs_test_opt(root, SPACE_CACHE))
  7396. cache->dirty = 1;
  7397. }
  7398. read_extent_buffer(leaf, &cache->item,
  7399. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7400. sizeof(cache->item));
  7401. memcpy(&cache->key, &found_key, sizeof(found_key));
  7402. key.objectid = found_key.objectid + found_key.offset;
  7403. btrfs_release_path(path);
  7404. cache->flags = btrfs_block_group_flags(&cache->item);
  7405. cache->sectorsize = root->sectorsize;
  7406. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7407. &root->fs_info->mapping_tree,
  7408. found_key.objectid);
  7409. btrfs_init_free_space_ctl(cache);
  7410. /*
  7411. * We need to exclude the super stripes now so that the space
  7412. * info has super bytes accounted for, otherwise we'll think
  7413. * we have more space than we actually do.
  7414. */
  7415. ret = exclude_super_stripes(root, cache);
  7416. if (ret) {
  7417. /*
  7418. * We may have excluded something, so call this just in
  7419. * case.
  7420. */
  7421. free_excluded_extents(root, cache);
  7422. kfree(cache->free_space_ctl);
  7423. kfree(cache);
  7424. goto error;
  7425. }
  7426. /*
  7427. * check for two cases, either we are full, and therefore
  7428. * don't need to bother with the caching work since we won't
  7429. * find any space, or we are empty, and we can just add all
  7430. * the space in and be done with it. This saves us _alot_ of
  7431. * time, particularly in the full case.
  7432. */
  7433. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7434. cache->last_byte_to_unpin = (u64)-1;
  7435. cache->cached = BTRFS_CACHE_FINISHED;
  7436. free_excluded_extents(root, cache);
  7437. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7438. cache->last_byte_to_unpin = (u64)-1;
  7439. cache->cached = BTRFS_CACHE_FINISHED;
  7440. add_new_free_space(cache, root->fs_info,
  7441. found_key.objectid,
  7442. found_key.objectid +
  7443. found_key.offset);
  7444. free_excluded_extents(root, cache);
  7445. }
  7446. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7447. if (ret) {
  7448. btrfs_remove_free_space_cache(cache);
  7449. btrfs_put_block_group(cache);
  7450. goto error;
  7451. }
  7452. ret = update_space_info(info, cache->flags, found_key.offset,
  7453. btrfs_block_group_used(&cache->item),
  7454. &space_info);
  7455. if (ret) {
  7456. btrfs_remove_free_space_cache(cache);
  7457. spin_lock(&info->block_group_cache_lock);
  7458. rb_erase(&cache->cache_node,
  7459. &info->block_group_cache_tree);
  7460. spin_unlock(&info->block_group_cache_lock);
  7461. btrfs_put_block_group(cache);
  7462. goto error;
  7463. }
  7464. cache->space_info = space_info;
  7465. spin_lock(&cache->space_info->lock);
  7466. cache->space_info->bytes_readonly += cache->bytes_super;
  7467. spin_unlock(&cache->space_info->lock);
  7468. __link_block_group(space_info, cache);
  7469. set_avail_alloc_bits(root->fs_info, cache->flags);
  7470. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7471. set_block_group_ro(cache, 1);
  7472. }
  7473. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7474. if (!(get_alloc_profile(root, space_info->flags) &
  7475. (BTRFS_BLOCK_GROUP_RAID10 |
  7476. BTRFS_BLOCK_GROUP_RAID1 |
  7477. BTRFS_BLOCK_GROUP_RAID5 |
  7478. BTRFS_BLOCK_GROUP_RAID6 |
  7479. BTRFS_BLOCK_GROUP_DUP)))
  7480. continue;
  7481. /*
  7482. * avoid allocating from un-mirrored block group if there are
  7483. * mirrored block groups.
  7484. */
  7485. list_for_each_entry(cache,
  7486. &space_info->block_groups[BTRFS_RAID_RAID0],
  7487. list)
  7488. set_block_group_ro(cache, 1);
  7489. list_for_each_entry(cache,
  7490. &space_info->block_groups[BTRFS_RAID_SINGLE],
  7491. list)
  7492. set_block_group_ro(cache, 1);
  7493. }
  7494. init_global_block_rsv(info);
  7495. ret = 0;
  7496. error:
  7497. btrfs_free_path(path);
  7498. return ret;
  7499. }
  7500. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  7501. struct btrfs_root *root)
  7502. {
  7503. struct btrfs_block_group_cache *block_group, *tmp;
  7504. struct btrfs_root *extent_root = root->fs_info->extent_root;
  7505. struct btrfs_block_group_item item;
  7506. struct btrfs_key key;
  7507. int ret = 0;
  7508. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  7509. new_bg_list) {
  7510. list_del_init(&block_group->new_bg_list);
  7511. if (ret)
  7512. continue;
  7513. spin_lock(&block_group->lock);
  7514. memcpy(&item, &block_group->item, sizeof(item));
  7515. memcpy(&key, &block_group->key, sizeof(key));
  7516. spin_unlock(&block_group->lock);
  7517. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  7518. sizeof(item));
  7519. if (ret)
  7520. btrfs_abort_transaction(trans, extent_root, ret);
  7521. ret = btrfs_finish_chunk_alloc(trans, extent_root,
  7522. key.objectid, key.offset);
  7523. if (ret)
  7524. btrfs_abort_transaction(trans, extent_root, ret);
  7525. }
  7526. }
  7527. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7528. struct btrfs_root *root, u64 bytes_used,
  7529. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7530. u64 size)
  7531. {
  7532. int ret;
  7533. struct btrfs_root *extent_root;
  7534. struct btrfs_block_group_cache *cache;
  7535. extent_root = root->fs_info->extent_root;
  7536. root->fs_info->last_trans_log_full_commit = trans->transid;
  7537. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7538. if (!cache)
  7539. return -ENOMEM;
  7540. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7541. GFP_NOFS);
  7542. if (!cache->free_space_ctl) {
  7543. kfree(cache);
  7544. return -ENOMEM;
  7545. }
  7546. cache->key.objectid = chunk_offset;
  7547. cache->key.offset = size;
  7548. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7549. cache->sectorsize = root->sectorsize;
  7550. cache->fs_info = root->fs_info;
  7551. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7552. &root->fs_info->mapping_tree,
  7553. chunk_offset);
  7554. atomic_set(&cache->count, 1);
  7555. spin_lock_init(&cache->lock);
  7556. INIT_LIST_HEAD(&cache->list);
  7557. INIT_LIST_HEAD(&cache->cluster_list);
  7558. INIT_LIST_HEAD(&cache->new_bg_list);
  7559. btrfs_init_free_space_ctl(cache);
  7560. btrfs_set_block_group_used(&cache->item, bytes_used);
  7561. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7562. cache->flags = type;
  7563. btrfs_set_block_group_flags(&cache->item, type);
  7564. cache->last_byte_to_unpin = (u64)-1;
  7565. cache->cached = BTRFS_CACHE_FINISHED;
  7566. ret = exclude_super_stripes(root, cache);
  7567. if (ret) {
  7568. /*
  7569. * We may have excluded something, so call this just in
  7570. * case.
  7571. */
  7572. free_excluded_extents(root, cache);
  7573. kfree(cache->free_space_ctl);
  7574. kfree(cache);
  7575. return ret;
  7576. }
  7577. add_new_free_space(cache, root->fs_info, chunk_offset,
  7578. chunk_offset + size);
  7579. free_excluded_extents(root, cache);
  7580. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7581. if (ret) {
  7582. btrfs_remove_free_space_cache(cache);
  7583. btrfs_put_block_group(cache);
  7584. return ret;
  7585. }
  7586. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7587. &cache->space_info);
  7588. if (ret) {
  7589. btrfs_remove_free_space_cache(cache);
  7590. spin_lock(&root->fs_info->block_group_cache_lock);
  7591. rb_erase(&cache->cache_node,
  7592. &root->fs_info->block_group_cache_tree);
  7593. spin_unlock(&root->fs_info->block_group_cache_lock);
  7594. btrfs_put_block_group(cache);
  7595. return ret;
  7596. }
  7597. update_global_block_rsv(root->fs_info);
  7598. spin_lock(&cache->space_info->lock);
  7599. cache->space_info->bytes_readonly += cache->bytes_super;
  7600. spin_unlock(&cache->space_info->lock);
  7601. __link_block_group(cache->space_info, cache);
  7602. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  7603. set_avail_alloc_bits(extent_root->fs_info, type);
  7604. return 0;
  7605. }
  7606. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  7607. {
  7608. u64 extra_flags = chunk_to_extended(flags) &
  7609. BTRFS_EXTENDED_PROFILE_MASK;
  7610. write_seqlock(&fs_info->profiles_lock);
  7611. if (flags & BTRFS_BLOCK_GROUP_DATA)
  7612. fs_info->avail_data_alloc_bits &= ~extra_flags;
  7613. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  7614. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  7615. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  7616. fs_info->avail_system_alloc_bits &= ~extra_flags;
  7617. write_sequnlock(&fs_info->profiles_lock);
  7618. }
  7619. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7620. struct btrfs_root *root, u64 group_start)
  7621. {
  7622. struct btrfs_path *path;
  7623. struct btrfs_block_group_cache *block_group;
  7624. struct btrfs_free_cluster *cluster;
  7625. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7626. struct btrfs_key key;
  7627. struct inode *inode;
  7628. int ret;
  7629. int index;
  7630. int factor;
  7631. root = root->fs_info->extent_root;
  7632. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7633. BUG_ON(!block_group);
  7634. BUG_ON(!block_group->ro);
  7635. /*
  7636. * Free the reserved super bytes from this block group before
  7637. * remove it.
  7638. */
  7639. free_excluded_extents(root, block_group);
  7640. memcpy(&key, &block_group->key, sizeof(key));
  7641. index = get_block_group_index(block_group);
  7642. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7643. BTRFS_BLOCK_GROUP_RAID1 |
  7644. BTRFS_BLOCK_GROUP_RAID10))
  7645. factor = 2;
  7646. else
  7647. factor = 1;
  7648. /* make sure this block group isn't part of an allocation cluster */
  7649. cluster = &root->fs_info->data_alloc_cluster;
  7650. spin_lock(&cluster->refill_lock);
  7651. btrfs_return_cluster_to_free_space(block_group, cluster);
  7652. spin_unlock(&cluster->refill_lock);
  7653. /*
  7654. * make sure this block group isn't part of a metadata
  7655. * allocation cluster
  7656. */
  7657. cluster = &root->fs_info->meta_alloc_cluster;
  7658. spin_lock(&cluster->refill_lock);
  7659. btrfs_return_cluster_to_free_space(block_group, cluster);
  7660. spin_unlock(&cluster->refill_lock);
  7661. path = btrfs_alloc_path();
  7662. if (!path) {
  7663. ret = -ENOMEM;
  7664. goto out;
  7665. }
  7666. inode = lookup_free_space_inode(tree_root, block_group, path);
  7667. if (!IS_ERR(inode)) {
  7668. ret = btrfs_orphan_add(trans, inode);
  7669. if (ret) {
  7670. btrfs_add_delayed_iput(inode);
  7671. goto out;
  7672. }
  7673. clear_nlink(inode);
  7674. /* One for the block groups ref */
  7675. spin_lock(&block_group->lock);
  7676. if (block_group->iref) {
  7677. block_group->iref = 0;
  7678. block_group->inode = NULL;
  7679. spin_unlock(&block_group->lock);
  7680. iput(inode);
  7681. } else {
  7682. spin_unlock(&block_group->lock);
  7683. }
  7684. /* One for our lookup ref */
  7685. btrfs_add_delayed_iput(inode);
  7686. }
  7687. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7688. key.offset = block_group->key.objectid;
  7689. key.type = 0;
  7690. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7691. if (ret < 0)
  7692. goto out;
  7693. if (ret > 0)
  7694. btrfs_release_path(path);
  7695. if (ret == 0) {
  7696. ret = btrfs_del_item(trans, tree_root, path);
  7697. if (ret)
  7698. goto out;
  7699. btrfs_release_path(path);
  7700. }
  7701. spin_lock(&root->fs_info->block_group_cache_lock);
  7702. rb_erase(&block_group->cache_node,
  7703. &root->fs_info->block_group_cache_tree);
  7704. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  7705. root->fs_info->first_logical_byte = (u64)-1;
  7706. spin_unlock(&root->fs_info->block_group_cache_lock);
  7707. down_write(&block_group->space_info->groups_sem);
  7708. /*
  7709. * we must use list_del_init so people can check to see if they
  7710. * are still on the list after taking the semaphore
  7711. */
  7712. list_del_init(&block_group->list);
  7713. if (list_empty(&block_group->space_info->block_groups[index])) {
  7714. kobject_del(&block_group->space_info->block_group_kobjs[index]);
  7715. kobject_put(&block_group->space_info->block_group_kobjs[index]);
  7716. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7717. }
  7718. up_write(&block_group->space_info->groups_sem);
  7719. if (block_group->cached == BTRFS_CACHE_STARTED)
  7720. wait_block_group_cache_done(block_group);
  7721. btrfs_remove_free_space_cache(block_group);
  7722. spin_lock(&block_group->space_info->lock);
  7723. block_group->space_info->total_bytes -= block_group->key.offset;
  7724. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7725. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7726. spin_unlock(&block_group->space_info->lock);
  7727. memcpy(&key, &block_group->key, sizeof(key));
  7728. btrfs_clear_space_info_full(root->fs_info);
  7729. btrfs_put_block_group(block_group);
  7730. btrfs_put_block_group(block_group);
  7731. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7732. if (ret > 0)
  7733. ret = -EIO;
  7734. if (ret < 0)
  7735. goto out;
  7736. ret = btrfs_del_item(trans, root, path);
  7737. out:
  7738. btrfs_free_path(path);
  7739. return ret;
  7740. }
  7741. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7742. {
  7743. struct btrfs_space_info *space_info;
  7744. struct btrfs_super_block *disk_super;
  7745. u64 features;
  7746. u64 flags;
  7747. int mixed = 0;
  7748. int ret;
  7749. disk_super = fs_info->super_copy;
  7750. if (!btrfs_super_root(disk_super))
  7751. return 1;
  7752. features = btrfs_super_incompat_flags(disk_super);
  7753. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7754. mixed = 1;
  7755. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7756. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7757. if (ret)
  7758. goto out;
  7759. if (mixed) {
  7760. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7761. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7762. } else {
  7763. flags = BTRFS_BLOCK_GROUP_METADATA;
  7764. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7765. if (ret)
  7766. goto out;
  7767. flags = BTRFS_BLOCK_GROUP_DATA;
  7768. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7769. }
  7770. out:
  7771. return ret;
  7772. }
  7773. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7774. {
  7775. return unpin_extent_range(root, start, end);
  7776. }
  7777. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7778. u64 num_bytes, u64 *actual_bytes)
  7779. {
  7780. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7781. }
  7782. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7783. {
  7784. struct btrfs_fs_info *fs_info = root->fs_info;
  7785. struct btrfs_block_group_cache *cache = NULL;
  7786. u64 group_trimmed;
  7787. u64 start;
  7788. u64 end;
  7789. u64 trimmed = 0;
  7790. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7791. int ret = 0;
  7792. /*
  7793. * try to trim all FS space, our block group may start from non-zero.
  7794. */
  7795. if (range->len == total_bytes)
  7796. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7797. else
  7798. cache = btrfs_lookup_block_group(fs_info, range->start);
  7799. while (cache) {
  7800. if (cache->key.objectid >= (range->start + range->len)) {
  7801. btrfs_put_block_group(cache);
  7802. break;
  7803. }
  7804. start = max(range->start, cache->key.objectid);
  7805. end = min(range->start + range->len,
  7806. cache->key.objectid + cache->key.offset);
  7807. if (end - start >= range->minlen) {
  7808. if (!block_group_cache_done(cache)) {
  7809. ret = cache_block_group(cache, 0);
  7810. if (ret) {
  7811. btrfs_put_block_group(cache);
  7812. break;
  7813. }
  7814. ret = wait_block_group_cache_done(cache);
  7815. if (ret) {
  7816. btrfs_put_block_group(cache);
  7817. break;
  7818. }
  7819. }
  7820. ret = btrfs_trim_block_group(cache,
  7821. &group_trimmed,
  7822. start,
  7823. end,
  7824. range->minlen);
  7825. trimmed += group_trimmed;
  7826. if (ret) {
  7827. btrfs_put_block_group(cache);
  7828. break;
  7829. }
  7830. }
  7831. cache = next_block_group(fs_info->tree_root, cache);
  7832. }
  7833. range->len = trimmed;
  7834. return ret;
  7835. }