extent-tree.c 231 KB

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