extent-tree.c 257 KB

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