extent-tree.c 262 KB

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