extent-tree.c 229 KB

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