extent-tree.c 261 KB

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