extent-tree.c 271 KB

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