extent-tree.c 232 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742
  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. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2264. {
  2265. u64 num_bytes;
  2266. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2267. sizeof(struct btrfs_extent_inline_ref));
  2268. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2269. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2270. /*
  2271. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2272. * closer to what we're really going to want to ouse.
  2273. */
  2274. return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2275. }
  2276. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2277. struct btrfs_root *root)
  2278. {
  2279. struct btrfs_block_rsv *global_rsv;
  2280. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2281. u64 num_bytes;
  2282. int ret = 0;
  2283. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2284. num_heads = heads_to_leaves(root, num_heads);
  2285. if (num_heads > 1)
  2286. num_bytes += (num_heads - 1) * root->leafsize;
  2287. num_bytes <<= 1;
  2288. global_rsv = &root->fs_info->global_block_rsv;
  2289. /*
  2290. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2291. * wiggle room since running delayed refs can create more delayed refs.
  2292. */
  2293. if (global_rsv->space_info->full)
  2294. num_bytes <<= 1;
  2295. spin_lock(&global_rsv->lock);
  2296. if (global_rsv->reserved <= num_bytes)
  2297. ret = 1;
  2298. spin_unlock(&global_rsv->lock);
  2299. return ret;
  2300. }
  2301. /*
  2302. * this starts processing the delayed reference count updates and
  2303. * extent insertions we have queued up so far. count can be
  2304. * 0, which means to process everything in the tree at the start
  2305. * of the run (but not newly added entries), or it can be some target
  2306. * number you'd like to process.
  2307. *
  2308. * Returns 0 on success or if called with an aborted transaction
  2309. * Returns <0 on error and aborts the transaction
  2310. */
  2311. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2312. struct btrfs_root *root, unsigned long count)
  2313. {
  2314. struct rb_node *node;
  2315. struct btrfs_delayed_ref_root *delayed_refs;
  2316. struct btrfs_delayed_ref_node *ref;
  2317. struct list_head cluster;
  2318. int ret;
  2319. u64 delayed_start;
  2320. int run_all = count == (unsigned long)-1;
  2321. int run_most = 0;
  2322. int loops;
  2323. /* We'll clean this up in btrfs_cleanup_transaction */
  2324. if (trans->aborted)
  2325. return 0;
  2326. if (root == root->fs_info->extent_root)
  2327. root = root->fs_info->tree_root;
  2328. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  2329. delayed_refs = &trans->transaction->delayed_refs;
  2330. INIT_LIST_HEAD(&cluster);
  2331. if (count == 0) {
  2332. count = delayed_refs->num_entries * 2;
  2333. run_most = 1;
  2334. }
  2335. if (!run_all && !run_most) {
  2336. int old;
  2337. int seq = atomic_read(&delayed_refs->ref_seq);
  2338. progress:
  2339. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2340. if (old) {
  2341. DEFINE_WAIT(__wait);
  2342. if (delayed_refs->flushing ||
  2343. !btrfs_should_throttle_delayed_refs(trans, root))
  2344. return 0;
  2345. prepare_to_wait(&delayed_refs->wait, &__wait,
  2346. TASK_UNINTERRUPTIBLE);
  2347. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2348. if (old) {
  2349. schedule();
  2350. finish_wait(&delayed_refs->wait, &__wait);
  2351. if (!refs_newer(delayed_refs, seq, 256))
  2352. goto progress;
  2353. else
  2354. return 0;
  2355. } else {
  2356. finish_wait(&delayed_refs->wait, &__wait);
  2357. goto again;
  2358. }
  2359. }
  2360. } else {
  2361. atomic_inc(&delayed_refs->procs_running_refs);
  2362. }
  2363. again:
  2364. loops = 0;
  2365. spin_lock(&delayed_refs->lock);
  2366. #ifdef SCRAMBLE_DELAYED_REFS
  2367. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2368. #endif
  2369. while (1) {
  2370. if (!(run_all || run_most) &&
  2371. !btrfs_should_throttle_delayed_refs(trans, root))
  2372. break;
  2373. /*
  2374. * go find something we can process in the rbtree. We start at
  2375. * the beginning of the tree, and then build a cluster
  2376. * of refs to process starting at the first one we are able to
  2377. * lock
  2378. */
  2379. delayed_start = delayed_refs->run_delayed_start;
  2380. ret = btrfs_find_ref_cluster(trans, &cluster,
  2381. delayed_refs->run_delayed_start);
  2382. if (ret)
  2383. break;
  2384. ret = run_clustered_refs(trans, root, &cluster);
  2385. if (ret < 0) {
  2386. btrfs_release_ref_cluster(&cluster);
  2387. spin_unlock(&delayed_refs->lock);
  2388. btrfs_abort_transaction(trans, root, ret);
  2389. atomic_dec(&delayed_refs->procs_running_refs);
  2390. wake_up(&delayed_refs->wait);
  2391. return ret;
  2392. }
  2393. atomic_add(ret, &delayed_refs->ref_seq);
  2394. count -= min_t(unsigned long, ret, count);
  2395. if (count == 0)
  2396. break;
  2397. if (delayed_start >= delayed_refs->run_delayed_start) {
  2398. if (loops == 0) {
  2399. /*
  2400. * btrfs_find_ref_cluster looped. let's do one
  2401. * more cycle. if we don't run any delayed ref
  2402. * during that cycle (because we can't because
  2403. * all of them are blocked), bail out.
  2404. */
  2405. loops = 1;
  2406. } else {
  2407. /*
  2408. * no runnable refs left, stop trying
  2409. */
  2410. BUG_ON(run_all);
  2411. break;
  2412. }
  2413. }
  2414. if (ret) {
  2415. /* refs were run, let's reset staleness detection */
  2416. loops = 0;
  2417. }
  2418. }
  2419. if (run_all) {
  2420. if (!list_empty(&trans->new_bgs)) {
  2421. spin_unlock(&delayed_refs->lock);
  2422. btrfs_create_pending_block_groups(trans, root);
  2423. spin_lock(&delayed_refs->lock);
  2424. }
  2425. node = rb_first(&delayed_refs->root);
  2426. if (!node)
  2427. goto out;
  2428. count = (unsigned long)-1;
  2429. while (node) {
  2430. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2431. rb_node);
  2432. if (btrfs_delayed_ref_is_head(ref)) {
  2433. struct btrfs_delayed_ref_head *head;
  2434. head = btrfs_delayed_node_to_head(ref);
  2435. atomic_inc(&ref->refs);
  2436. spin_unlock(&delayed_refs->lock);
  2437. /*
  2438. * Mutex was contended, block until it's
  2439. * released and try again
  2440. */
  2441. mutex_lock(&head->mutex);
  2442. mutex_unlock(&head->mutex);
  2443. btrfs_put_delayed_ref(ref);
  2444. cond_resched();
  2445. goto again;
  2446. }
  2447. node = rb_next(node);
  2448. }
  2449. spin_unlock(&delayed_refs->lock);
  2450. schedule_timeout(1);
  2451. goto again;
  2452. }
  2453. out:
  2454. atomic_dec(&delayed_refs->procs_running_refs);
  2455. smp_mb();
  2456. if (waitqueue_active(&delayed_refs->wait))
  2457. wake_up(&delayed_refs->wait);
  2458. spin_unlock(&delayed_refs->lock);
  2459. assert_qgroups_uptodate(trans);
  2460. return 0;
  2461. }
  2462. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2463. struct btrfs_root *root,
  2464. u64 bytenr, u64 num_bytes, u64 flags,
  2465. int level, int is_data)
  2466. {
  2467. struct btrfs_delayed_extent_op *extent_op;
  2468. int ret;
  2469. extent_op = btrfs_alloc_delayed_extent_op();
  2470. if (!extent_op)
  2471. return -ENOMEM;
  2472. extent_op->flags_to_set = flags;
  2473. extent_op->update_flags = 1;
  2474. extent_op->update_key = 0;
  2475. extent_op->is_data = is_data ? 1 : 0;
  2476. extent_op->level = level;
  2477. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2478. num_bytes, extent_op);
  2479. if (ret)
  2480. btrfs_free_delayed_extent_op(extent_op);
  2481. return ret;
  2482. }
  2483. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2484. struct btrfs_root *root,
  2485. struct btrfs_path *path,
  2486. u64 objectid, u64 offset, u64 bytenr)
  2487. {
  2488. struct btrfs_delayed_ref_head *head;
  2489. struct btrfs_delayed_ref_node *ref;
  2490. struct btrfs_delayed_data_ref *data_ref;
  2491. struct btrfs_delayed_ref_root *delayed_refs;
  2492. struct rb_node *node;
  2493. int ret = 0;
  2494. ret = -ENOENT;
  2495. delayed_refs = &trans->transaction->delayed_refs;
  2496. spin_lock(&delayed_refs->lock);
  2497. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2498. if (!head)
  2499. goto out;
  2500. if (!mutex_trylock(&head->mutex)) {
  2501. atomic_inc(&head->node.refs);
  2502. spin_unlock(&delayed_refs->lock);
  2503. btrfs_release_path(path);
  2504. /*
  2505. * Mutex was contended, block until it's released and let
  2506. * caller try again
  2507. */
  2508. mutex_lock(&head->mutex);
  2509. mutex_unlock(&head->mutex);
  2510. btrfs_put_delayed_ref(&head->node);
  2511. return -EAGAIN;
  2512. }
  2513. node = rb_prev(&head->node.rb_node);
  2514. if (!node)
  2515. goto out_unlock;
  2516. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2517. if (ref->bytenr != bytenr)
  2518. goto out_unlock;
  2519. ret = 1;
  2520. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2521. goto out_unlock;
  2522. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2523. node = rb_prev(node);
  2524. if (node) {
  2525. int seq = ref->seq;
  2526. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2527. if (ref->bytenr == bytenr && ref->seq == seq)
  2528. goto out_unlock;
  2529. }
  2530. if (data_ref->root != root->root_key.objectid ||
  2531. data_ref->objectid != objectid || data_ref->offset != offset)
  2532. goto out_unlock;
  2533. ret = 0;
  2534. out_unlock:
  2535. mutex_unlock(&head->mutex);
  2536. out:
  2537. spin_unlock(&delayed_refs->lock);
  2538. return ret;
  2539. }
  2540. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2541. struct btrfs_root *root,
  2542. struct btrfs_path *path,
  2543. u64 objectid, u64 offset, u64 bytenr)
  2544. {
  2545. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2546. struct extent_buffer *leaf;
  2547. struct btrfs_extent_data_ref *ref;
  2548. struct btrfs_extent_inline_ref *iref;
  2549. struct btrfs_extent_item *ei;
  2550. struct btrfs_key key;
  2551. u32 item_size;
  2552. int ret;
  2553. key.objectid = bytenr;
  2554. key.offset = (u64)-1;
  2555. key.type = BTRFS_EXTENT_ITEM_KEY;
  2556. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2557. if (ret < 0)
  2558. goto out;
  2559. BUG_ON(ret == 0); /* Corruption */
  2560. ret = -ENOENT;
  2561. if (path->slots[0] == 0)
  2562. goto out;
  2563. path->slots[0]--;
  2564. leaf = path->nodes[0];
  2565. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2566. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2567. goto out;
  2568. ret = 1;
  2569. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2570. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2571. if (item_size < sizeof(*ei)) {
  2572. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2573. goto out;
  2574. }
  2575. #endif
  2576. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2577. if (item_size != sizeof(*ei) +
  2578. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2579. goto out;
  2580. if (btrfs_extent_generation(leaf, ei) <=
  2581. btrfs_root_last_snapshot(&root->root_item))
  2582. goto out;
  2583. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2584. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2585. BTRFS_EXTENT_DATA_REF_KEY)
  2586. goto out;
  2587. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2588. if (btrfs_extent_refs(leaf, ei) !=
  2589. btrfs_extent_data_ref_count(leaf, ref) ||
  2590. btrfs_extent_data_ref_root(leaf, ref) !=
  2591. root->root_key.objectid ||
  2592. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2593. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2594. goto out;
  2595. ret = 0;
  2596. out:
  2597. return ret;
  2598. }
  2599. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2600. struct btrfs_root *root,
  2601. u64 objectid, u64 offset, u64 bytenr)
  2602. {
  2603. struct btrfs_path *path;
  2604. int ret;
  2605. int ret2;
  2606. path = btrfs_alloc_path();
  2607. if (!path)
  2608. return -ENOENT;
  2609. do {
  2610. ret = check_committed_ref(trans, root, path, objectid,
  2611. offset, bytenr);
  2612. if (ret && ret != -ENOENT)
  2613. goto out;
  2614. ret2 = check_delayed_ref(trans, root, path, objectid,
  2615. offset, bytenr);
  2616. } while (ret2 == -EAGAIN);
  2617. if (ret2 && ret2 != -ENOENT) {
  2618. ret = ret2;
  2619. goto out;
  2620. }
  2621. if (ret != -ENOENT || ret2 != -ENOENT)
  2622. ret = 0;
  2623. out:
  2624. btrfs_free_path(path);
  2625. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2626. WARN_ON(ret > 0);
  2627. return ret;
  2628. }
  2629. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2630. struct btrfs_root *root,
  2631. struct extent_buffer *buf,
  2632. int full_backref, int inc, int for_cow)
  2633. {
  2634. u64 bytenr;
  2635. u64 num_bytes;
  2636. u64 parent;
  2637. u64 ref_root;
  2638. u32 nritems;
  2639. struct btrfs_key key;
  2640. struct btrfs_file_extent_item *fi;
  2641. int i;
  2642. int level;
  2643. int ret = 0;
  2644. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2645. u64, u64, u64, u64, u64, u64, int);
  2646. ref_root = btrfs_header_owner(buf);
  2647. nritems = btrfs_header_nritems(buf);
  2648. level = btrfs_header_level(buf);
  2649. if (!root->ref_cows && level == 0)
  2650. return 0;
  2651. if (inc)
  2652. process_func = btrfs_inc_extent_ref;
  2653. else
  2654. process_func = btrfs_free_extent;
  2655. if (full_backref)
  2656. parent = buf->start;
  2657. else
  2658. parent = 0;
  2659. for (i = 0; i < nritems; i++) {
  2660. if (level == 0) {
  2661. btrfs_item_key_to_cpu(buf, &key, i);
  2662. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2663. continue;
  2664. fi = btrfs_item_ptr(buf, i,
  2665. struct btrfs_file_extent_item);
  2666. if (btrfs_file_extent_type(buf, fi) ==
  2667. BTRFS_FILE_EXTENT_INLINE)
  2668. continue;
  2669. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2670. if (bytenr == 0)
  2671. continue;
  2672. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2673. key.offset -= btrfs_file_extent_offset(buf, fi);
  2674. ret = process_func(trans, root, bytenr, num_bytes,
  2675. parent, ref_root, key.objectid,
  2676. key.offset, for_cow);
  2677. if (ret)
  2678. goto fail;
  2679. } else {
  2680. bytenr = btrfs_node_blockptr(buf, i);
  2681. num_bytes = btrfs_level_size(root, level - 1);
  2682. ret = process_func(trans, root, bytenr, num_bytes,
  2683. parent, ref_root, level - 1, 0,
  2684. for_cow);
  2685. if (ret)
  2686. goto fail;
  2687. }
  2688. }
  2689. return 0;
  2690. fail:
  2691. return ret;
  2692. }
  2693. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2694. struct extent_buffer *buf, int full_backref, int for_cow)
  2695. {
  2696. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2697. }
  2698. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2699. struct extent_buffer *buf, int full_backref, int for_cow)
  2700. {
  2701. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2702. }
  2703. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2704. struct btrfs_root *root,
  2705. struct btrfs_path *path,
  2706. struct btrfs_block_group_cache *cache)
  2707. {
  2708. int ret;
  2709. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2710. unsigned long bi;
  2711. struct extent_buffer *leaf;
  2712. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2713. if (ret < 0)
  2714. goto fail;
  2715. BUG_ON(ret); /* Corruption */
  2716. leaf = path->nodes[0];
  2717. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2718. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2719. btrfs_mark_buffer_dirty(leaf);
  2720. btrfs_release_path(path);
  2721. fail:
  2722. if (ret) {
  2723. btrfs_abort_transaction(trans, root, ret);
  2724. return ret;
  2725. }
  2726. return 0;
  2727. }
  2728. static struct btrfs_block_group_cache *
  2729. next_block_group(struct btrfs_root *root,
  2730. struct btrfs_block_group_cache *cache)
  2731. {
  2732. struct rb_node *node;
  2733. spin_lock(&root->fs_info->block_group_cache_lock);
  2734. node = rb_next(&cache->cache_node);
  2735. btrfs_put_block_group(cache);
  2736. if (node) {
  2737. cache = rb_entry(node, struct btrfs_block_group_cache,
  2738. cache_node);
  2739. btrfs_get_block_group(cache);
  2740. } else
  2741. cache = NULL;
  2742. spin_unlock(&root->fs_info->block_group_cache_lock);
  2743. return cache;
  2744. }
  2745. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2746. struct btrfs_trans_handle *trans,
  2747. struct btrfs_path *path)
  2748. {
  2749. struct btrfs_root *root = block_group->fs_info->tree_root;
  2750. struct inode *inode = NULL;
  2751. u64 alloc_hint = 0;
  2752. int dcs = BTRFS_DC_ERROR;
  2753. int num_pages = 0;
  2754. int retries = 0;
  2755. int ret = 0;
  2756. /*
  2757. * If this block group is smaller than 100 megs don't bother caching the
  2758. * block group.
  2759. */
  2760. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2761. spin_lock(&block_group->lock);
  2762. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2763. spin_unlock(&block_group->lock);
  2764. return 0;
  2765. }
  2766. again:
  2767. inode = lookup_free_space_inode(root, block_group, path);
  2768. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2769. ret = PTR_ERR(inode);
  2770. btrfs_release_path(path);
  2771. goto out;
  2772. }
  2773. if (IS_ERR(inode)) {
  2774. BUG_ON(retries);
  2775. retries++;
  2776. if (block_group->ro)
  2777. goto out_free;
  2778. ret = create_free_space_inode(root, trans, block_group, path);
  2779. if (ret)
  2780. goto out_free;
  2781. goto again;
  2782. }
  2783. /* We've already setup this transaction, go ahead and exit */
  2784. if (block_group->cache_generation == trans->transid &&
  2785. i_size_read(inode)) {
  2786. dcs = BTRFS_DC_SETUP;
  2787. goto out_put;
  2788. }
  2789. /*
  2790. * We want to set the generation to 0, that way if anything goes wrong
  2791. * from here on out we know not to trust this cache when we load up next
  2792. * time.
  2793. */
  2794. BTRFS_I(inode)->generation = 0;
  2795. ret = btrfs_update_inode(trans, root, inode);
  2796. WARN_ON(ret);
  2797. if (i_size_read(inode) > 0) {
  2798. ret = btrfs_check_trunc_cache_free_space(root,
  2799. &root->fs_info->global_block_rsv);
  2800. if (ret)
  2801. goto out_put;
  2802. ret = btrfs_truncate_free_space_cache(root, trans, path,
  2803. inode);
  2804. if (ret)
  2805. goto out_put;
  2806. }
  2807. spin_lock(&block_group->lock);
  2808. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2809. !btrfs_test_opt(root, SPACE_CACHE)) {
  2810. /*
  2811. * don't bother trying to write stuff out _if_
  2812. * a) we're not cached,
  2813. * b) we're with nospace_cache mount option.
  2814. */
  2815. dcs = BTRFS_DC_WRITTEN;
  2816. spin_unlock(&block_group->lock);
  2817. goto out_put;
  2818. }
  2819. spin_unlock(&block_group->lock);
  2820. /*
  2821. * Try to preallocate enough space based on how big the block group is.
  2822. * Keep in mind this has to include any pinned space which could end up
  2823. * taking up quite a bit since it's not folded into the other space
  2824. * cache.
  2825. */
  2826. num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
  2827. if (!num_pages)
  2828. num_pages = 1;
  2829. num_pages *= 16;
  2830. num_pages *= PAGE_CACHE_SIZE;
  2831. ret = btrfs_check_data_free_space(inode, num_pages);
  2832. if (ret)
  2833. goto out_put;
  2834. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2835. num_pages, num_pages,
  2836. &alloc_hint);
  2837. if (!ret)
  2838. dcs = BTRFS_DC_SETUP;
  2839. btrfs_free_reserved_data_space(inode, num_pages);
  2840. out_put:
  2841. iput(inode);
  2842. out_free:
  2843. btrfs_release_path(path);
  2844. out:
  2845. spin_lock(&block_group->lock);
  2846. if (!ret && dcs == BTRFS_DC_SETUP)
  2847. block_group->cache_generation = trans->transid;
  2848. block_group->disk_cache_state = dcs;
  2849. spin_unlock(&block_group->lock);
  2850. return ret;
  2851. }
  2852. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2853. struct btrfs_root *root)
  2854. {
  2855. struct btrfs_block_group_cache *cache;
  2856. int err = 0;
  2857. struct btrfs_path *path;
  2858. u64 last = 0;
  2859. path = btrfs_alloc_path();
  2860. if (!path)
  2861. return -ENOMEM;
  2862. again:
  2863. while (1) {
  2864. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2865. while (cache) {
  2866. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2867. break;
  2868. cache = next_block_group(root, cache);
  2869. }
  2870. if (!cache) {
  2871. if (last == 0)
  2872. break;
  2873. last = 0;
  2874. continue;
  2875. }
  2876. err = cache_save_setup(cache, trans, path);
  2877. last = cache->key.objectid + cache->key.offset;
  2878. btrfs_put_block_group(cache);
  2879. }
  2880. while (1) {
  2881. if (last == 0) {
  2882. err = btrfs_run_delayed_refs(trans, root,
  2883. (unsigned long)-1);
  2884. if (err) /* File system offline */
  2885. goto out;
  2886. }
  2887. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2888. while (cache) {
  2889. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2890. btrfs_put_block_group(cache);
  2891. goto again;
  2892. }
  2893. if (cache->dirty)
  2894. break;
  2895. cache = next_block_group(root, cache);
  2896. }
  2897. if (!cache) {
  2898. if (last == 0)
  2899. break;
  2900. last = 0;
  2901. continue;
  2902. }
  2903. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2904. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2905. cache->dirty = 0;
  2906. last = cache->key.objectid + cache->key.offset;
  2907. err = write_one_cache_group(trans, root, path, cache);
  2908. if (err) /* File system offline */
  2909. goto out;
  2910. btrfs_put_block_group(cache);
  2911. }
  2912. while (1) {
  2913. /*
  2914. * I don't think this is needed since we're just marking our
  2915. * preallocated extent as written, but just in case it can't
  2916. * hurt.
  2917. */
  2918. if (last == 0) {
  2919. err = btrfs_run_delayed_refs(trans, root,
  2920. (unsigned long)-1);
  2921. if (err) /* File system offline */
  2922. goto out;
  2923. }
  2924. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2925. while (cache) {
  2926. /*
  2927. * Really this shouldn't happen, but it could if we
  2928. * couldn't write the entire preallocated extent and
  2929. * splitting the extent resulted in a new block.
  2930. */
  2931. if (cache->dirty) {
  2932. btrfs_put_block_group(cache);
  2933. goto again;
  2934. }
  2935. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2936. break;
  2937. cache = next_block_group(root, cache);
  2938. }
  2939. if (!cache) {
  2940. if (last == 0)
  2941. break;
  2942. last = 0;
  2943. continue;
  2944. }
  2945. err = btrfs_write_out_cache(root, trans, cache, path);
  2946. /*
  2947. * If we didn't have an error then the cache state is still
  2948. * NEED_WRITE, so we can set it to WRITTEN.
  2949. */
  2950. if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2951. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2952. last = cache->key.objectid + cache->key.offset;
  2953. btrfs_put_block_group(cache);
  2954. }
  2955. out:
  2956. btrfs_free_path(path);
  2957. return err;
  2958. }
  2959. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2960. {
  2961. struct btrfs_block_group_cache *block_group;
  2962. int readonly = 0;
  2963. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2964. if (!block_group || block_group->ro)
  2965. readonly = 1;
  2966. if (block_group)
  2967. btrfs_put_block_group(block_group);
  2968. return readonly;
  2969. }
  2970. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2971. u64 total_bytes, u64 bytes_used,
  2972. struct btrfs_space_info **space_info)
  2973. {
  2974. struct btrfs_space_info *found;
  2975. int i;
  2976. int factor;
  2977. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2978. BTRFS_BLOCK_GROUP_RAID10))
  2979. factor = 2;
  2980. else
  2981. factor = 1;
  2982. found = __find_space_info(info, flags);
  2983. if (found) {
  2984. spin_lock(&found->lock);
  2985. found->total_bytes += total_bytes;
  2986. found->disk_total += total_bytes * factor;
  2987. found->bytes_used += bytes_used;
  2988. found->disk_used += bytes_used * factor;
  2989. found->full = 0;
  2990. spin_unlock(&found->lock);
  2991. *space_info = found;
  2992. return 0;
  2993. }
  2994. found = kzalloc(sizeof(*found), GFP_NOFS);
  2995. if (!found)
  2996. return -ENOMEM;
  2997. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  2998. INIT_LIST_HEAD(&found->block_groups[i]);
  2999. init_rwsem(&found->groups_sem);
  3000. spin_lock_init(&found->lock);
  3001. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3002. found->total_bytes = total_bytes;
  3003. found->disk_total = total_bytes * factor;
  3004. found->bytes_used = bytes_used;
  3005. found->disk_used = bytes_used * factor;
  3006. found->bytes_pinned = 0;
  3007. found->bytes_reserved = 0;
  3008. found->bytes_readonly = 0;
  3009. found->bytes_may_use = 0;
  3010. found->full = 0;
  3011. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3012. found->chunk_alloc = 0;
  3013. found->flush = 0;
  3014. init_waitqueue_head(&found->wait);
  3015. *space_info = found;
  3016. list_add_rcu(&found->list, &info->space_info);
  3017. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3018. info->data_sinfo = found;
  3019. return 0;
  3020. }
  3021. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3022. {
  3023. u64 extra_flags = chunk_to_extended(flags) &
  3024. BTRFS_EXTENDED_PROFILE_MASK;
  3025. write_seqlock(&fs_info->profiles_lock);
  3026. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3027. fs_info->avail_data_alloc_bits |= extra_flags;
  3028. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3029. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3030. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3031. fs_info->avail_system_alloc_bits |= extra_flags;
  3032. write_sequnlock(&fs_info->profiles_lock);
  3033. }
  3034. /*
  3035. * returns target flags in extended format or 0 if restripe for this
  3036. * chunk_type is not in progress
  3037. *
  3038. * should be called with either volume_mutex or balance_lock held
  3039. */
  3040. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3041. {
  3042. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3043. u64 target = 0;
  3044. if (!bctl)
  3045. return 0;
  3046. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3047. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3048. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3049. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3050. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3051. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3052. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3053. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3054. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3055. }
  3056. return target;
  3057. }
  3058. /*
  3059. * @flags: available profiles in extended format (see ctree.h)
  3060. *
  3061. * Returns reduced profile in chunk format. If profile changing is in
  3062. * progress (either running or paused) picks the target profile (if it's
  3063. * already available), otherwise falls back to plain reducing.
  3064. */
  3065. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3066. {
  3067. /*
  3068. * we add in the count of missing devices because we want
  3069. * to make sure that any RAID levels on a degraded FS
  3070. * continue to be honored.
  3071. */
  3072. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  3073. root->fs_info->fs_devices->missing_devices;
  3074. u64 target;
  3075. u64 tmp;
  3076. /*
  3077. * see if restripe for this chunk_type is in progress, if so
  3078. * try to reduce to the target profile
  3079. */
  3080. spin_lock(&root->fs_info->balance_lock);
  3081. target = get_restripe_target(root->fs_info, flags);
  3082. if (target) {
  3083. /* pick target profile only if it's already available */
  3084. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3085. spin_unlock(&root->fs_info->balance_lock);
  3086. return extended_to_chunk(target);
  3087. }
  3088. }
  3089. spin_unlock(&root->fs_info->balance_lock);
  3090. /* First, mask out the RAID levels which aren't possible */
  3091. if (num_devices == 1)
  3092. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3093. BTRFS_BLOCK_GROUP_RAID5);
  3094. if (num_devices < 3)
  3095. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3096. if (num_devices < 4)
  3097. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3098. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3099. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3100. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3101. flags &= ~tmp;
  3102. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3103. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3104. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3105. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3106. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3107. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3108. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3109. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3110. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3111. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3112. return extended_to_chunk(flags | tmp);
  3113. }
  3114. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  3115. {
  3116. unsigned seq;
  3117. do {
  3118. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3119. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3120. flags |= root->fs_info->avail_data_alloc_bits;
  3121. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3122. flags |= root->fs_info->avail_system_alloc_bits;
  3123. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3124. flags |= root->fs_info->avail_metadata_alloc_bits;
  3125. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3126. return btrfs_reduce_alloc_profile(root, flags);
  3127. }
  3128. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3129. {
  3130. u64 flags;
  3131. u64 ret;
  3132. if (data)
  3133. flags = BTRFS_BLOCK_GROUP_DATA;
  3134. else if (root == root->fs_info->chunk_root)
  3135. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3136. else
  3137. flags = BTRFS_BLOCK_GROUP_METADATA;
  3138. ret = get_alloc_profile(root, flags);
  3139. return ret;
  3140. }
  3141. /*
  3142. * This will check the space that the inode allocates from to make sure we have
  3143. * enough space for bytes.
  3144. */
  3145. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  3146. {
  3147. struct btrfs_space_info *data_sinfo;
  3148. struct btrfs_root *root = BTRFS_I(inode)->root;
  3149. struct btrfs_fs_info *fs_info = root->fs_info;
  3150. u64 used;
  3151. int ret = 0, committed = 0, alloc_chunk = 1;
  3152. /* make sure bytes are sectorsize aligned */
  3153. bytes = ALIGN(bytes, root->sectorsize);
  3154. if (root == root->fs_info->tree_root ||
  3155. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  3156. alloc_chunk = 0;
  3157. committed = 1;
  3158. }
  3159. data_sinfo = fs_info->data_sinfo;
  3160. if (!data_sinfo)
  3161. goto alloc;
  3162. again:
  3163. /* make sure we have enough space to handle the data first */
  3164. spin_lock(&data_sinfo->lock);
  3165. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3166. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3167. data_sinfo->bytes_may_use;
  3168. if (used + bytes > data_sinfo->total_bytes) {
  3169. struct btrfs_trans_handle *trans;
  3170. /*
  3171. * if we don't have enough free bytes in this space then we need
  3172. * to alloc a new chunk.
  3173. */
  3174. if (!data_sinfo->full && alloc_chunk) {
  3175. u64 alloc_target;
  3176. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3177. spin_unlock(&data_sinfo->lock);
  3178. alloc:
  3179. alloc_target = btrfs_get_alloc_profile(root, 1);
  3180. trans = btrfs_join_transaction(root);
  3181. if (IS_ERR(trans))
  3182. return PTR_ERR(trans);
  3183. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3184. alloc_target,
  3185. CHUNK_ALLOC_NO_FORCE);
  3186. btrfs_end_transaction(trans, root);
  3187. if (ret < 0) {
  3188. if (ret != -ENOSPC)
  3189. return ret;
  3190. else
  3191. goto commit_trans;
  3192. }
  3193. if (!data_sinfo)
  3194. data_sinfo = fs_info->data_sinfo;
  3195. goto again;
  3196. }
  3197. /*
  3198. * If we have less pinned bytes than we want to allocate then
  3199. * don't bother committing the transaction, it won't help us.
  3200. */
  3201. if (data_sinfo->bytes_pinned < bytes)
  3202. committed = 1;
  3203. spin_unlock(&data_sinfo->lock);
  3204. /* commit the current transaction and try again */
  3205. commit_trans:
  3206. if (!committed &&
  3207. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3208. committed = 1;
  3209. trans = btrfs_join_transaction(root);
  3210. if (IS_ERR(trans))
  3211. return PTR_ERR(trans);
  3212. ret = btrfs_commit_transaction(trans, root);
  3213. if (ret)
  3214. return ret;
  3215. goto again;
  3216. }
  3217. return -ENOSPC;
  3218. }
  3219. data_sinfo->bytes_may_use += bytes;
  3220. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3221. data_sinfo->flags, bytes, 1);
  3222. spin_unlock(&data_sinfo->lock);
  3223. return 0;
  3224. }
  3225. /*
  3226. * Called if we need to clear a data reservation for this inode.
  3227. */
  3228. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3229. {
  3230. struct btrfs_root *root = BTRFS_I(inode)->root;
  3231. struct btrfs_space_info *data_sinfo;
  3232. /* make sure bytes are sectorsize aligned */
  3233. bytes = ALIGN(bytes, root->sectorsize);
  3234. data_sinfo = root->fs_info->data_sinfo;
  3235. spin_lock(&data_sinfo->lock);
  3236. data_sinfo->bytes_may_use -= bytes;
  3237. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3238. data_sinfo->flags, bytes, 0);
  3239. spin_unlock(&data_sinfo->lock);
  3240. }
  3241. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3242. {
  3243. struct list_head *head = &info->space_info;
  3244. struct btrfs_space_info *found;
  3245. rcu_read_lock();
  3246. list_for_each_entry_rcu(found, head, list) {
  3247. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3248. found->force_alloc = CHUNK_ALLOC_FORCE;
  3249. }
  3250. rcu_read_unlock();
  3251. }
  3252. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3253. {
  3254. return (global->size << 1);
  3255. }
  3256. static int should_alloc_chunk(struct btrfs_root *root,
  3257. struct btrfs_space_info *sinfo, int force)
  3258. {
  3259. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3260. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3261. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3262. u64 thresh;
  3263. if (force == CHUNK_ALLOC_FORCE)
  3264. return 1;
  3265. /*
  3266. * We need to take into account the global rsv because for all intents
  3267. * and purposes it's used space. Don't worry about locking the
  3268. * global_rsv, it doesn't change except when the transaction commits.
  3269. */
  3270. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3271. num_allocated += calc_global_rsv_need_space(global_rsv);
  3272. /*
  3273. * in limited mode, we want to have some free space up to
  3274. * about 1% of the FS size.
  3275. */
  3276. if (force == CHUNK_ALLOC_LIMITED) {
  3277. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3278. thresh = max_t(u64, 64 * 1024 * 1024,
  3279. div_factor_fine(thresh, 1));
  3280. if (num_bytes - num_allocated < thresh)
  3281. return 1;
  3282. }
  3283. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3284. return 0;
  3285. return 1;
  3286. }
  3287. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3288. {
  3289. u64 num_dev;
  3290. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3291. BTRFS_BLOCK_GROUP_RAID0 |
  3292. BTRFS_BLOCK_GROUP_RAID5 |
  3293. BTRFS_BLOCK_GROUP_RAID6))
  3294. num_dev = root->fs_info->fs_devices->rw_devices;
  3295. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3296. num_dev = 2;
  3297. else
  3298. num_dev = 1; /* DUP or single */
  3299. /* metadata for updaing devices and chunk tree */
  3300. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3301. }
  3302. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3303. struct btrfs_root *root, u64 type)
  3304. {
  3305. struct btrfs_space_info *info;
  3306. u64 left;
  3307. u64 thresh;
  3308. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3309. spin_lock(&info->lock);
  3310. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3311. info->bytes_reserved - info->bytes_readonly;
  3312. spin_unlock(&info->lock);
  3313. thresh = get_system_chunk_thresh(root, type);
  3314. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3315. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3316. left, thresh, type);
  3317. dump_space_info(info, 0, 0);
  3318. }
  3319. if (left < thresh) {
  3320. u64 flags;
  3321. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3322. btrfs_alloc_chunk(trans, root, flags);
  3323. }
  3324. }
  3325. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3326. struct btrfs_root *extent_root, u64 flags, int force)
  3327. {
  3328. struct btrfs_space_info *space_info;
  3329. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3330. int wait_for_alloc = 0;
  3331. int ret = 0;
  3332. /* Don't re-enter if we're already allocating a chunk */
  3333. if (trans->allocating_chunk)
  3334. return -ENOSPC;
  3335. space_info = __find_space_info(extent_root->fs_info, flags);
  3336. if (!space_info) {
  3337. ret = update_space_info(extent_root->fs_info, flags,
  3338. 0, 0, &space_info);
  3339. BUG_ON(ret); /* -ENOMEM */
  3340. }
  3341. BUG_ON(!space_info); /* Logic error */
  3342. again:
  3343. spin_lock(&space_info->lock);
  3344. if (force < space_info->force_alloc)
  3345. force = space_info->force_alloc;
  3346. if (space_info->full) {
  3347. spin_unlock(&space_info->lock);
  3348. return 0;
  3349. }
  3350. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3351. spin_unlock(&space_info->lock);
  3352. return 0;
  3353. } else if (space_info->chunk_alloc) {
  3354. wait_for_alloc = 1;
  3355. } else {
  3356. space_info->chunk_alloc = 1;
  3357. }
  3358. spin_unlock(&space_info->lock);
  3359. mutex_lock(&fs_info->chunk_mutex);
  3360. /*
  3361. * The chunk_mutex is held throughout the entirety of a chunk
  3362. * allocation, so once we've acquired the chunk_mutex we know that the
  3363. * other guy is done and we need to recheck and see if we should
  3364. * allocate.
  3365. */
  3366. if (wait_for_alloc) {
  3367. mutex_unlock(&fs_info->chunk_mutex);
  3368. wait_for_alloc = 0;
  3369. goto again;
  3370. }
  3371. trans->allocating_chunk = true;
  3372. /*
  3373. * If we have mixed data/metadata chunks we want to make sure we keep
  3374. * allocating mixed chunks instead of individual chunks.
  3375. */
  3376. if (btrfs_mixed_space_info(space_info))
  3377. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3378. /*
  3379. * if we're doing a data chunk, go ahead and make sure that
  3380. * we keep a reasonable number of metadata chunks allocated in the
  3381. * FS as well.
  3382. */
  3383. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3384. fs_info->data_chunk_allocations++;
  3385. if (!(fs_info->data_chunk_allocations %
  3386. fs_info->metadata_ratio))
  3387. force_metadata_allocation(fs_info);
  3388. }
  3389. /*
  3390. * Check if we have enough space in SYSTEM chunk because we may need
  3391. * to update devices.
  3392. */
  3393. check_system_chunk(trans, extent_root, flags);
  3394. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3395. trans->allocating_chunk = false;
  3396. spin_lock(&space_info->lock);
  3397. if (ret < 0 && ret != -ENOSPC)
  3398. goto out;
  3399. if (ret)
  3400. space_info->full = 1;
  3401. else
  3402. ret = 1;
  3403. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3404. out:
  3405. space_info->chunk_alloc = 0;
  3406. spin_unlock(&space_info->lock);
  3407. mutex_unlock(&fs_info->chunk_mutex);
  3408. return ret;
  3409. }
  3410. static int can_overcommit(struct btrfs_root *root,
  3411. struct btrfs_space_info *space_info, u64 bytes,
  3412. enum btrfs_reserve_flush_enum flush)
  3413. {
  3414. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3415. u64 profile = btrfs_get_alloc_profile(root, 0);
  3416. u64 space_size;
  3417. u64 avail;
  3418. u64 used;
  3419. u64 to_add;
  3420. used = space_info->bytes_used + space_info->bytes_reserved +
  3421. space_info->bytes_pinned + space_info->bytes_readonly;
  3422. /*
  3423. * We only want to allow over committing if we have lots of actual space
  3424. * free, but if we don't have enough space to handle the global reserve
  3425. * space then we could end up having a real enospc problem when trying
  3426. * to allocate a chunk or some other such important allocation.
  3427. */
  3428. spin_lock(&global_rsv->lock);
  3429. space_size = calc_global_rsv_need_space(global_rsv);
  3430. spin_unlock(&global_rsv->lock);
  3431. if (used + space_size >= space_info->total_bytes)
  3432. return 0;
  3433. used += space_info->bytes_may_use;
  3434. spin_lock(&root->fs_info->free_chunk_lock);
  3435. avail = root->fs_info->free_chunk_space;
  3436. spin_unlock(&root->fs_info->free_chunk_lock);
  3437. /*
  3438. * If we have dup, raid1 or raid10 then only half of the free
  3439. * space is actually useable. For raid56, the space info used
  3440. * doesn't include the parity drive, so we don't have to
  3441. * change the math
  3442. */
  3443. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3444. BTRFS_BLOCK_GROUP_RAID1 |
  3445. BTRFS_BLOCK_GROUP_RAID10))
  3446. avail >>= 1;
  3447. to_add = space_info->total_bytes;
  3448. /*
  3449. * If we aren't flushing all things, let us overcommit up to
  3450. * 1/2th of the space. If we can flush, don't let us overcommit
  3451. * too much, let it overcommit up to 1/8 of the space.
  3452. */
  3453. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3454. to_add >>= 3;
  3455. else
  3456. to_add >>= 1;
  3457. /*
  3458. * Limit the overcommit to the amount of free space we could possibly
  3459. * allocate for chunks.
  3460. */
  3461. to_add = min(avail, to_add);
  3462. if (used + bytes < space_info->total_bytes + to_add)
  3463. return 1;
  3464. return 0;
  3465. }
  3466. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3467. unsigned long nr_pages)
  3468. {
  3469. struct super_block *sb = root->fs_info->sb;
  3470. int started;
  3471. /* If we can not start writeback, just sync all the delalloc file. */
  3472. started = try_to_writeback_inodes_sb_nr(sb, nr_pages,
  3473. WB_REASON_FS_FREE_SPACE);
  3474. if (!started) {
  3475. /*
  3476. * We needn't worry the filesystem going from r/w to r/o though
  3477. * we don't acquire ->s_umount mutex, because the filesystem
  3478. * should guarantee the delalloc inodes list be empty after
  3479. * the filesystem is readonly(all dirty pages are written to
  3480. * the disk).
  3481. */
  3482. btrfs_start_all_delalloc_inodes(root->fs_info, 0);
  3483. if (!current->journal_info)
  3484. btrfs_wait_all_ordered_extents(root->fs_info, 0);
  3485. }
  3486. }
  3487. /*
  3488. * shrink metadata reservation for delalloc
  3489. */
  3490. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3491. bool wait_ordered)
  3492. {
  3493. struct btrfs_block_rsv *block_rsv;
  3494. struct btrfs_space_info *space_info;
  3495. struct btrfs_trans_handle *trans;
  3496. u64 delalloc_bytes;
  3497. u64 max_reclaim;
  3498. long time_left;
  3499. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3500. int loops = 0;
  3501. enum btrfs_reserve_flush_enum flush;
  3502. trans = (struct btrfs_trans_handle *)current->journal_info;
  3503. block_rsv = &root->fs_info->delalloc_block_rsv;
  3504. space_info = block_rsv->space_info;
  3505. smp_mb();
  3506. delalloc_bytes = percpu_counter_sum_positive(
  3507. &root->fs_info->delalloc_bytes);
  3508. if (delalloc_bytes == 0) {
  3509. if (trans)
  3510. return;
  3511. btrfs_wait_all_ordered_extents(root->fs_info, 0);
  3512. return;
  3513. }
  3514. while (delalloc_bytes && loops < 3) {
  3515. max_reclaim = min(delalloc_bytes, to_reclaim);
  3516. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3517. btrfs_writeback_inodes_sb_nr(root, nr_pages);
  3518. /*
  3519. * We need to wait for the async pages to actually start before
  3520. * we do anything.
  3521. */
  3522. wait_event(root->fs_info->async_submit_wait,
  3523. !atomic_read(&root->fs_info->async_delalloc_pages));
  3524. if (!trans)
  3525. flush = BTRFS_RESERVE_FLUSH_ALL;
  3526. else
  3527. flush = BTRFS_RESERVE_NO_FLUSH;
  3528. spin_lock(&space_info->lock);
  3529. if (can_overcommit(root, space_info, orig, flush)) {
  3530. spin_unlock(&space_info->lock);
  3531. break;
  3532. }
  3533. spin_unlock(&space_info->lock);
  3534. loops++;
  3535. if (wait_ordered && !trans) {
  3536. btrfs_wait_all_ordered_extents(root->fs_info, 0);
  3537. } else {
  3538. time_left = schedule_timeout_killable(1);
  3539. if (time_left)
  3540. break;
  3541. }
  3542. smp_mb();
  3543. delalloc_bytes = percpu_counter_sum_positive(
  3544. &root->fs_info->delalloc_bytes);
  3545. }
  3546. }
  3547. /**
  3548. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3549. * @root - the root we're allocating for
  3550. * @bytes - the number of bytes we want to reserve
  3551. * @force - force the commit
  3552. *
  3553. * This will check to make sure that committing the transaction will actually
  3554. * get us somewhere and then commit the transaction if it does. Otherwise it
  3555. * will return -ENOSPC.
  3556. */
  3557. static int may_commit_transaction(struct btrfs_root *root,
  3558. struct btrfs_space_info *space_info,
  3559. u64 bytes, int force)
  3560. {
  3561. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3562. struct btrfs_trans_handle *trans;
  3563. trans = (struct btrfs_trans_handle *)current->journal_info;
  3564. if (trans)
  3565. return -EAGAIN;
  3566. if (force)
  3567. goto commit;
  3568. /* See if there is enough pinned space to make this reservation */
  3569. spin_lock(&space_info->lock);
  3570. if (space_info->bytes_pinned >= bytes) {
  3571. spin_unlock(&space_info->lock);
  3572. goto commit;
  3573. }
  3574. spin_unlock(&space_info->lock);
  3575. /*
  3576. * See if there is some space in the delayed insertion reservation for
  3577. * this reservation.
  3578. */
  3579. if (space_info != delayed_rsv->space_info)
  3580. return -ENOSPC;
  3581. spin_lock(&space_info->lock);
  3582. spin_lock(&delayed_rsv->lock);
  3583. if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
  3584. spin_unlock(&delayed_rsv->lock);
  3585. spin_unlock(&space_info->lock);
  3586. return -ENOSPC;
  3587. }
  3588. spin_unlock(&delayed_rsv->lock);
  3589. spin_unlock(&space_info->lock);
  3590. commit:
  3591. trans = btrfs_join_transaction(root);
  3592. if (IS_ERR(trans))
  3593. return -ENOSPC;
  3594. return btrfs_commit_transaction(trans, root);
  3595. }
  3596. enum flush_state {
  3597. FLUSH_DELAYED_ITEMS_NR = 1,
  3598. FLUSH_DELAYED_ITEMS = 2,
  3599. FLUSH_DELALLOC = 3,
  3600. FLUSH_DELALLOC_WAIT = 4,
  3601. ALLOC_CHUNK = 5,
  3602. COMMIT_TRANS = 6,
  3603. };
  3604. static int flush_space(struct btrfs_root *root,
  3605. struct btrfs_space_info *space_info, u64 num_bytes,
  3606. u64 orig_bytes, int state)
  3607. {
  3608. struct btrfs_trans_handle *trans;
  3609. int nr;
  3610. int ret = 0;
  3611. switch (state) {
  3612. case FLUSH_DELAYED_ITEMS_NR:
  3613. case FLUSH_DELAYED_ITEMS:
  3614. if (state == FLUSH_DELAYED_ITEMS_NR) {
  3615. u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
  3616. nr = (int)div64_u64(num_bytes, bytes);
  3617. if (!nr)
  3618. nr = 1;
  3619. nr *= 2;
  3620. } else {
  3621. nr = -1;
  3622. }
  3623. trans = btrfs_join_transaction(root);
  3624. if (IS_ERR(trans)) {
  3625. ret = PTR_ERR(trans);
  3626. break;
  3627. }
  3628. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3629. btrfs_end_transaction(trans, root);
  3630. break;
  3631. case FLUSH_DELALLOC:
  3632. case FLUSH_DELALLOC_WAIT:
  3633. shrink_delalloc(root, num_bytes, orig_bytes,
  3634. state == FLUSH_DELALLOC_WAIT);
  3635. break;
  3636. case ALLOC_CHUNK:
  3637. trans = btrfs_join_transaction(root);
  3638. if (IS_ERR(trans)) {
  3639. ret = PTR_ERR(trans);
  3640. break;
  3641. }
  3642. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3643. btrfs_get_alloc_profile(root, 0),
  3644. CHUNK_ALLOC_NO_FORCE);
  3645. btrfs_end_transaction(trans, root);
  3646. if (ret == -ENOSPC)
  3647. ret = 0;
  3648. break;
  3649. case COMMIT_TRANS:
  3650. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3651. break;
  3652. default:
  3653. ret = -ENOSPC;
  3654. break;
  3655. }
  3656. return ret;
  3657. }
  3658. /**
  3659. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3660. * @root - the root we're allocating for
  3661. * @block_rsv - the block_rsv we're allocating for
  3662. * @orig_bytes - the number of bytes we want
  3663. * @flush - whether or not we can flush to make our reservation
  3664. *
  3665. * This will reserve orgi_bytes number of bytes from the space info associated
  3666. * with the block_rsv. If there is not enough space it will make an attempt to
  3667. * flush out space to make room. It will do this by flushing delalloc if
  3668. * possible or committing the transaction. If flush is 0 then no attempts to
  3669. * regain reservations will be made and this will fail if there is not enough
  3670. * space already.
  3671. */
  3672. static int reserve_metadata_bytes(struct btrfs_root *root,
  3673. struct btrfs_block_rsv *block_rsv,
  3674. u64 orig_bytes,
  3675. enum btrfs_reserve_flush_enum flush)
  3676. {
  3677. struct btrfs_space_info *space_info = block_rsv->space_info;
  3678. u64 used;
  3679. u64 num_bytes = orig_bytes;
  3680. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  3681. int ret = 0;
  3682. bool flushing = false;
  3683. again:
  3684. ret = 0;
  3685. spin_lock(&space_info->lock);
  3686. /*
  3687. * We only want to wait if somebody other than us is flushing and we
  3688. * are actually allowed to flush all things.
  3689. */
  3690. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  3691. space_info->flush) {
  3692. spin_unlock(&space_info->lock);
  3693. /*
  3694. * If we have a trans handle we can't wait because the flusher
  3695. * may have to commit the transaction, which would mean we would
  3696. * deadlock since we are waiting for the flusher to finish, but
  3697. * hold the current transaction open.
  3698. */
  3699. if (current->journal_info)
  3700. return -EAGAIN;
  3701. ret = wait_event_killable(space_info->wait, !space_info->flush);
  3702. /* Must have been killed, return */
  3703. if (ret)
  3704. return -EINTR;
  3705. spin_lock(&space_info->lock);
  3706. }
  3707. ret = -ENOSPC;
  3708. used = space_info->bytes_used + space_info->bytes_reserved +
  3709. space_info->bytes_pinned + space_info->bytes_readonly +
  3710. space_info->bytes_may_use;
  3711. /*
  3712. * The idea here is that we've not already over-reserved the block group
  3713. * then we can go ahead and save our reservation first and then start
  3714. * flushing if we need to. Otherwise if we've already overcommitted
  3715. * lets start flushing stuff first and then come back and try to make
  3716. * our reservation.
  3717. */
  3718. if (used <= space_info->total_bytes) {
  3719. if (used + orig_bytes <= space_info->total_bytes) {
  3720. space_info->bytes_may_use += orig_bytes;
  3721. trace_btrfs_space_reservation(root->fs_info,
  3722. "space_info", space_info->flags, orig_bytes, 1);
  3723. ret = 0;
  3724. } else {
  3725. /*
  3726. * Ok set num_bytes to orig_bytes since we aren't
  3727. * overocmmitted, this way we only try and reclaim what
  3728. * we need.
  3729. */
  3730. num_bytes = orig_bytes;
  3731. }
  3732. } else {
  3733. /*
  3734. * Ok we're over committed, set num_bytes to the overcommitted
  3735. * amount plus the amount of bytes that we need for this
  3736. * reservation.
  3737. */
  3738. num_bytes = used - space_info->total_bytes +
  3739. (orig_bytes * 2);
  3740. }
  3741. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  3742. space_info->bytes_may_use += orig_bytes;
  3743. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3744. space_info->flags, orig_bytes,
  3745. 1);
  3746. ret = 0;
  3747. }
  3748. /*
  3749. * Couldn't make our reservation, save our place so while we're trying
  3750. * to reclaim space we can actually use it instead of somebody else
  3751. * stealing it from us.
  3752. *
  3753. * We make the other tasks wait for the flush only when we can flush
  3754. * all things.
  3755. */
  3756. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  3757. flushing = true;
  3758. space_info->flush = 1;
  3759. }
  3760. spin_unlock(&space_info->lock);
  3761. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  3762. goto out;
  3763. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  3764. flush_state);
  3765. flush_state++;
  3766. /*
  3767. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  3768. * would happen. So skip delalloc flush.
  3769. */
  3770. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3771. (flush_state == FLUSH_DELALLOC ||
  3772. flush_state == FLUSH_DELALLOC_WAIT))
  3773. flush_state = ALLOC_CHUNK;
  3774. if (!ret)
  3775. goto again;
  3776. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3777. flush_state < COMMIT_TRANS)
  3778. goto again;
  3779. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  3780. flush_state <= COMMIT_TRANS)
  3781. goto again;
  3782. out:
  3783. if (ret == -ENOSPC &&
  3784. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  3785. struct btrfs_block_rsv *global_rsv =
  3786. &root->fs_info->global_block_rsv;
  3787. if (block_rsv != global_rsv &&
  3788. !block_rsv_use_bytes(global_rsv, orig_bytes))
  3789. ret = 0;
  3790. }
  3791. if (flushing) {
  3792. spin_lock(&space_info->lock);
  3793. space_info->flush = 0;
  3794. wake_up_all(&space_info->wait);
  3795. spin_unlock(&space_info->lock);
  3796. }
  3797. return ret;
  3798. }
  3799. static struct btrfs_block_rsv *get_block_rsv(
  3800. const struct btrfs_trans_handle *trans,
  3801. const struct btrfs_root *root)
  3802. {
  3803. struct btrfs_block_rsv *block_rsv = NULL;
  3804. if (root->ref_cows)
  3805. block_rsv = trans->block_rsv;
  3806. if (root == root->fs_info->csum_root && trans->adding_csums)
  3807. block_rsv = trans->block_rsv;
  3808. if (!block_rsv)
  3809. block_rsv = root->block_rsv;
  3810. if (!block_rsv)
  3811. block_rsv = &root->fs_info->empty_block_rsv;
  3812. return block_rsv;
  3813. }
  3814. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3815. u64 num_bytes)
  3816. {
  3817. int ret = -ENOSPC;
  3818. spin_lock(&block_rsv->lock);
  3819. if (block_rsv->reserved >= num_bytes) {
  3820. block_rsv->reserved -= num_bytes;
  3821. if (block_rsv->reserved < block_rsv->size)
  3822. block_rsv->full = 0;
  3823. ret = 0;
  3824. }
  3825. spin_unlock(&block_rsv->lock);
  3826. return ret;
  3827. }
  3828. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3829. u64 num_bytes, int update_size)
  3830. {
  3831. spin_lock(&block_rsv->lock);
  3832. block_rsv->reserved += num_bytes;
  3833. if (update_size)
  3834. block_rsv->size += num_bytes;
  3835. else if (block_rsv->reserved >= block_rsv->size)
  3836. block_rsv->full = 1;
  3837. spin_unlock(&block_rsv->lock);
  3838. }
  3839. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  3840. struct btrfs_block_rsv *dest, u64 num_bytes,
  3841. int min_factor)
  3842. {
  3843. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  3844. u64 min_bytes;
  3845. if (global_rsv->space_info != dest->space_info)
  3846. return -ENOSPC;
  3847. spin_lock(&global_rsv->lock);
  3848. min_bytes = div_factor(global_rsv->size, min_factor);
  3849. if (global_rsv->reserved < min_bytes + num_bytes) {
  3850. spin_unlock(&global_rsv->lock);
  3851. return -ENOSPC;
  3852. }
  3853. global_rsv->reserved -= num_bytes;
  3854. if (global_rsv->reserved < global_rsv->size)
  3855. global_rsv->full = 0;
  3856. spin_unlock(&global_rsv->lock);
  3857. block_rsv_add_bytes(dest, num_bytes, 1);
  3858. return 0;
  3859. }
  3860. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3861. struct btrfs_block_rsv *block_rsv,
  3862. struct btrfs_block_rsv *dest, u64 num_bytes)
  3863. {
  3864. struct btrfs_space_info *space_info = block_rsv->space_info;
  3865. spin_lock(&block_rsv->lock);
  3866. if (num_bytes == (u64)-1)
  3867. num_bytes = block_rsv->size;
  3868. block_rsv->size -= num_bytes;
  3869. if (block_rsv->reserved >= block_rsv->size) {
  3870. num_bytes = block_rsv->reserved - block_rsv->size;
  3871. block_rsv->reserved = block_rsv->size;
  3872. block_rsv->full = 1;
  3873. } else {
  3874. num_bytes = 0;
  3875. }
  3876. spin_unlock(&block_rsv->lock);
  3877. if (num_bytes > 0) {
  3878. if (dest) {
  3879. spin_lock(&dest->lock);
  3880. if (!dest->full) {
  3881. u64 bytes_to_add;
  3882. bytes_to_add = dest->size - dest->reserved;
  3883. bytes_to_add = min(num_bytes, bytes_to_add);
  3884. dest->reserved += bytes_to_add;
  3885. if (dest->reserved >= dest->size)
  3886. dest->full = 1;
  3887. num_bytes -= bytes_to_add;
  3888. }
  3889. spin_unlock(&dest->lock);
  3890. }
  3891. if (num_bytes) {
  3892. spin_lock(&space_info->lock);
  3893. space_info->bytes_may_use -= num_bytes;
  3894. trace_btrfs_space_reservation(fs_info, "space_info",
  3895. space_info->flags, num_bytes, 0);
  3896. space_info->reservation_progress++;
  3897. spin_unlock(&space_info->lock);
  3898. }
  3899. }
  3900. }
  3901. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3902. struct btrfs_block_rsv *dst, u64 num_bytes)
  3903. {
  3904. int ret;
  3905. ret = block_rsv_use_bytes(src, num_bytes);
  3906. if (ret)
  3907. return ret;
  3908. block_rsv_add_bytes(dst, num_bytes, 1);
  3909. return 0;
  3910. }
  3911. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  3912. {
  3913. memset(rsv, 0, sizeof(*rsv));
  3914. spin_lock_init(&rsv->lock);
  3915. rsv->type = type;
  3916. }
  3917. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  3918. unsigned short type)
  3919. {
  3920. struct btrfs_block_rsv *block_rsv;
  3921. struct btrfs_fs_info *fs_info = root->fs_info;
  3922. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3923. if (!block_rsv)
  3924. return NULL;
  3925. btrfs_init_block_rsv(block_rsv, type);
  3926. block_rsv->space_info = __find_space_info(fs_info,
  3927. BTRFS_BLOCK_GROUP_METADATA);
  3928. return block_rsv;
  3929. }
  3930. void btrfs_free_block_rsv(struct btrfs_root *root,
  3931. struct btrfs_block_rsv *rsv)
  3932. {
  3933. if (!rsv)
  3934. return;
  3935. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3936. kfree(rsv);
  3937. }
  3938. int btrfs_block_rsv_add(struct btrfs_root *root,
  3939. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  3940. enum btrfs_reserve_flush_enum flush)
  3941. {
  3942. int ret;
  3943. if (num_bytes == 0)
  3944. return 0;
  3945. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3946. if (!ret) {
  3947. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3948. return 0;
  3949. }
  3950. return ret;
  3951. }
  3952. int btrfs_block_rsv_check(struct btrfs_root *root,
  3953. struct btrfs_block_rsv *block_rsv, int min_factor)
  3954. {
  3955. u64 num_bytes = 0;
  3956. int ret = -ENOSPC;
  3957. if (!block_rsv)
  3958. return 0;
  3959. spin_lock(&block_rsv->lock);
  3960. num_bytes = div_factor(block_rsv->size, min_factor);
  3961. if (block_rsv->reserved >= num_bytes)
  3962. ret = 0;
  3963. spin_unlock(&block_rsv->lock);
  3964. return ret;
  3965. }
  3966. int btrfs_block_rsv_refill(struct btrfs_root *root,
  3967. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  3968. enum btrfs_reserve_flush_enum flush)
  3969. {
  3970. u64 num_bytes = 0;
  3971. int ret = -ENOSPC;
  3972. if (!block_rsv)
  3973. return 0;
  3974. spin_lock(&block_rsv->lock);
  3975. num_bytes = min_reserved;
  3976. if (block_rsv->reserved >= num_bytes)
  3977. ret = 0;
  3978. else
  3979. num_bytes -= block_rsv->reserved;
  3980. spin_unlock(&block_rsv->lock);
  3981. if (!ret)
  3982. return 0;
  3983. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3984. if (!ret) {
  3985. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  3986. return 0;
  3987. }
  3988. return ret;
  3989. }
  3990. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  3991. struct btrfs_block_rsv *dst_rsv,
  3992. u64 num_bytes)
  3993. {
  3994. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3995. }
  3996. void btrfs_block_rsv_release(struct btrfs_root *root,
  3997. struct btrfs_block_rsv *block_rsv,
  3998. u64 num_bytes)
  3999. {
  4000. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4001. if (global_rsv->full || global_rsv == block_rsv ||
  4002. block_rsv->space_info != global_rsv->space_info)
  4003. global_rsv = NULL;
  4004. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4005. num_bytes);
  4006. }
  4007. /*
  4008. * helper to calculate size of global block reservation.
  4009. * the desired value is sum of space used by extent tree,
  4010. * checksum tree and root tree
  4011. */
  4012. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4013. {
  4014. struct btrfs_space_info *sinfo;
  4015. u64 num_bytes;
  4016. u64 meta_used;
  4017. u64 data_used;
  4018. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4019. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4020. spin_lock(&sinfo->lock);
  4021. data_used = sinfo->bytes_used;
  4022. spin_unlock(&sinfo->lock);
  4023. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4024. spin_lock(&sinfo->lock);
  4025. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4026. data_used = 0;
  4027. meta_used = sinfo->bytes_used;
  4028. spin_unlock(&sinfo->lock);
  4029. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4030. csum_size * 2;
  4031. num_bytes += div64_u64(data_used + meta_used, 50);
  4032. if (num_bytes * 3 > meta_used)
  4033. num_bytes = div64_u64(meta_used, 3);
  4034. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  4035. }
  4036. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4037. {
  4038. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4039. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4040. u64 num_bytes;
  4041. num_bytes = calc_global_metadata_size(fs_info);
  4042. spin_lock(&sinfo->lock);
  4043. spin_lock(&block_rsv->lock);
  4044. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4045. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4046. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4047. sinfo->bytes_may_use;
  4048. if (sinfo->total_bytes > num_bytes) {
  4049. num_bytes = sinfo->total_bytes - num_bytes;
  4050. block_rsv->reserved += num_bytes;
  4051. sinfo->bytes_may_use += num_bytes;
  4052. trace_btrfs_space_reservation(fs_info, "space_info",
  4053. sinfo->flags, num_bytes, 1);
  4054. }
  4055. if (block_rsv->reserved >= block_rsv->size) {
  4056. num_bytes = block_rsv->reserved - block_rsv->size;
  4057. sinfo->bytes_may_use -= num_bytes;
  4058. trace_btrfs_space_reservation(fs_info, "space_info",
  4059. sinfo->flags, num_bytes, 0);
  4060. sinfo->reservation_progress++;
  4061. block_rsv->reserved = block_rsv->size;
  4062. block_rsv->full = 1;
  4063. }
  4064. spin_unlock(&block_rsv->lock);
  4065. spin_unlock(&sinfo->lock);
  4066. }
  4067. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4068. {
  4069. struct btrfs_space_info *space_info;
  4070. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4071. fs_info->chunk_block_rsv.space_info = space_info;
  4072. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4073. fs_info->global_block_rsv.space_info = space_info;
  4074. fs_info->delalloc_block_rsv.space_info = space_info;
  4075. fs_info->trans_block_rsv.space_info = space_info;
  4076. fs_info->empty_block_rsv.space_info = space_info;
  4077. fs_info->delayed_block_rsv.space_info = space_info;
  4078. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4079. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4080. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4081. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4082. if (fs_info->quota_root)
  4083. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4084. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4085. update_global_block_rsv(fs_info);
  4086. }
  4087. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4088. {
  4089. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4090. (u64)-1);
  4091. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4092. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4093. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4094. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4095. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4096. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4097. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4098. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4099. }
  4100. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4101. struct btrfs_root *root)
  4102. {
  4103. if (!trans->block_rsv)
  4104. return;
  4105. if (!trans->bytes_reserved)
  4106. return;
  4107. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4108. trans->transid, trans->bytes_reserved, 0);
  4109. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4110. trans->bytes_reserved = 0;
  4111. }
  4112. /* Can only return 0 or -ENOSPC */
  4113. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4114. struct inode *inode)
  4115. {
  4116. struct btrfs_root *root = BTRFS_I(inode)->root;
  4117. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4118. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4119. /*
  4120. * We need to hold space in order to delete our orphan item once we've
  4121. * added it, so this takes the reservation so we can release it later
  4122. * when we are truly done with the orphan item.
  4123. */
  4124. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4125. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4126. btrfs_ino(inode), num_bytes, 1);
  4127. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4128. }
  4129. void btrfs_orphan_release_metadata(struct inode *inode)
  4130. {
  4131. struct btrfs_root *root = BTRFS_I(inode)->root;
  4132. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4133. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4134. btrfs_ino(inode), num_bytes, 0);
  4135. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4136. }
  4137. /*
  4138. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4139. * root: the root of the parent directory
  4140. * rsv: block reservation
  4141. * items: the number of items that we need do reservation
  4142. * qgroup_reserved: used to return the reserved size in qgroup
  4143. *
  4144. * This function is used to reserve the space for snapshot/subvolume
  4145. * creation and deletion. Those operations are different with the
  4146. * common file/directory operations, they change two fs/file trees
  4147. * and root tree, the number of items that the qgroup reserves is
  4148. * different with the free space reservation. So we can not use
  4149. * the space reseravtion mechanism in start_transaction().
  4150. */
  4151. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4152. struct btrfs_block_rsv *rsv,
  4153. int items,
  4154. u64 *qgroup_reserved)
  4155. {
  4156. u64 num_bytes;
  4157. int ret;
  4158. if (root->fs_info->quota_enabled) {
  4159. /* One for parent inode, two for dir entries */
  4160. num_bytes = 3 * root->leafsize;
  4161. ret = btrfs_qgroup_reserve(root, num_bytes);
  4162. if (ret)
  4163. return ret;
  4164. } else {
  4165. num_bytes = 0;
  4166. }
  4167. *qgroup_reserved = num_bytes;
  4168. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4169. rsv->space_info = __find_space_info(root->fs_info,
  4170. BTRFS_BLOCK_GROUP_METADATA);
  4171. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4172. BTRFS_RESERVE_FLUSH_ALL);
  4173. if (ret) {
  4174. if (*qgroup_reserved)
  4175. btrfs_qgroup_free(root, *qgroup_reserved);
  4176. }
  4177. return ret;
  4178. }
  4179. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4180. struct btrfs_block_rsv *rsv,
  4181. u64 qgroup_reserved)
  4182. {
  4183. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4184. if (qgroup_reserved)
  4185. btrfs_qgroup_free(root, qgroup_reserved);
  4186. }
  4187. /**
  4188. * drop_outstanding_extent - drop an outstanding extent
  4189. * @inode: the inode we're dropping the extent for
  4190. *
  4191. * This is called when we are freeing up an outstanding extent, either called
  4192. * after an error or after an extent is written. This will return the number of
  4193. * reserved extents that need to be freed. This must be called with
  4194. * BTRFS_I(inode)->lock held.
  4195. */
  4196. static unsigned drop_outstanding_extent(struct inode *inode)
  4197. {
  4198. unsigned drop_inode_space = 0;
  4199. unsigned dropped_extents = 0;
  4200. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  4201. BTRFS_I(inode)->outstanding_extents--;
  4202. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4203. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4204. &BTRFS_I(inode)->runtime_flags))
  4205. drop_inode_space = 1;
  4206. /*
  4207. * If we have more or the same amount of outsanding extents than we have
  4208. * reserved then we need to leave the reserved extents count alone.
  4209. */
  4210. if (BTRFS_I(inode)->outstanding_extents >=
  4211. BTRFS_I(inode)->reserved_extents)
  4212. return drop_inode_space;
  4213. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4214. BTRFS_I(inode)->outstanding_extents;
  4215. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4216. return dropped_extents + drop_inode_space;
  4217. }
  4218. /**
  4219. * calc_csum_metadata_size - return the amount of metada space that must be
  4220. * reserved/free'd for the given bytes.
  4221. * @inode: the inode we're manipulating
  4222. * @num_bytes: the number of bytes in question
  4223. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4224. *
  4225. * This adjusts the number of csum_bytes in the inode and then returns the
  4226. * correct amount of metadata that must either be reserved or freed. We
  4227. * calculate how many checksums we can fit into one leaf and then divide the
  4228. * number of bytes that will need to be checksumed by this value to figure out
  4229. * how many checksums will be required. If we are adding bytes then the number
  4230. * may go up and we will return the number of additional bytes that must be
  4231. * reserved. If it is going down we will return the number of bytes that must
  4232. * be freed.
  4233. *
  4234. * This must be called with BTRFS_I(inode)->lock held.
  4235. */
  4236. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4237. int reserve)
  4238. {
  4239. struct btrfs_root *root = BTRFS_I(inode)->root;
  4240. u64 csum_size;
  4241. int num_csums_per_leaf;
  4242. int num_csums;
  4243. int old_csums;
  4244. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4245. BTRFS_I(inode)->csum_bytes == 0)
  4246. return 0;
  4247. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4248. if (reserve)
  4249. BTRFS_I(inode)->csum_bytes += num_bytes;
  4250. else
  4251. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4252. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  4253. num_csums_per_leaf = (int)div64_u64(csum_size,
  4254. sizeof(struct btrfs_csum_item) +
  4255. sizeof(struct btrfs_disk_key));
  4256. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4257. num_csums = num_csums + num_csums_per_leaf - 1;
  4258. num_csums = num_csums / num_csums_per_leaf;
  4259. old_csums = old_csums + num_csums_per_leaf - 1;
  4260. old_csums = old_csums / num_csums_per_leaf;
  4261. /* No change, no need to reserve more */
  4262. if (old_csums == num_csums)
  4263. return 0;
  4264. if (reserve)
  4265. return btrfs_calc_trans_metadata_size(root,
  4266. num_csums - old_csums);
  4267. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4268. }
  4269. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4270. {
  4271. struct btrfs_root *root = BTRFS_I(inode)->root;
  4272. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4273. u64 to_reserve = 0;
  4274. u64 csum_bytes;
  4275. unsigned nr_extents = 0;
  4276. int extra_reserve = 0;
  4277. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4278. int ret = 0;
  4279. bool delalloc_lock = true;
  4280. u64 to_free = 0;
  4281. unsigned dropped;
  4282. /* If we are a free space inode we need to not flush since we will be in
  4283. * the middle of a transaction commit. We also don't need the delalloc
  4284. * mutex since we won't race with anybody. We need this mostly to make
  4285. * lockdep shut its filthy mouth.
  4286. */
  4287. if (btrfs_is_free_space_inode(inode)) {
  4288. flush = BTRFS_RESERVE_NO_FLUSH;
  4289. delalloc_lock = false;
  4290. }
  4291. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4292. btrfs_transaction_in_commit(root->fs_info))
  4293. schedule_timeout(1);
  4294. if (delalloc_lock)
  4295. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4296. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4297. spin_lock(&BTRFS_I(inode)->lock);
  4298. BTRFS_I(inode)->outstanding_extents++;
  4299. if (BTRFS_I(inode)->outstanding_extents >
  4300. BTRFS_I(inode)->reserved_extents)
  4301. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4302. BTRFS_I(inode)->reserved_extents;
  4303. /*
  4304. * Add an item to reserve for updating the inode when we complete the
  4305. * delalloc io.
  4306. */
  4307. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4308. &BTRFS_I(inode)->runtime_flags)) {
  4309. nr_extents++;
  4310. extra_reserve = 1;
  4311. }
  4312. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4313. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4314. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4315. spin_unlock(&BTRFS_I(inode)->lock);
  4316. if (root->fs_info->quota_enabled) {
  4317. ret = btrfs_qgroup_reserve(root, num_bytes +
  4318. nr_extents * root->leafsize);
  4319. if (ret)
  4320. goto out_fail;
  4321. }
  4322. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4323. if (unlikely(ret)) {
  4324. if (root->fs_info->quota_enabled)
  4325. btrfs_qgroup_free(root, num_bytes +
  4326. nr_extents * root->leafsize);
  4327. goto out_fail;
  4328. }
  4329. spin_lock(&BTRFS_I(inode)->lock);
  4330. if (extra_reserve) {
  4331. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4332. &BTRFS_I(inode)->runtime_flags);
  4333. nr_extents--;
  4334. }
  4335. BTRFS_I(inode)->reserved_extents += nr_extents;
  4336. spin_unlock(&BTRFS_I(inode)->lock);
  4337. if (delalloc_lock)
  4338. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4339. if (to_reserve)
  4340. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  4341. btrfs_ino(inode), to_reserve, 1);
  4342. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4343. return 0;
  4344. out_fail:
  4345. spin_lock(&BTRFS_I(inode)->lock);
  4346. dropped = drop_outstanding_extent(inode);
  4347. /*
  4348. * If the inodes csum_bytes is the same as the original
  4349. * csum_bytes then we know we haven't raced with any free()ers
  4350. * so we can just reduce our inodes csum bytes and carry on.
  4351. */
  4352. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4353. calc_csum_metadata_size(inode, num_bytes, 0);
  4354. } else {
  4355. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4356. u64 bytes;
  4357. /*
  4358. * This is tricky, but first we need to figure out how much we
  4359. * free'd from any free-ers that occured during this
  4360. * reservation, so we reset ->csum_bytes to the csum_bytes
  4361. * before we dropped our lock, and then call the free for the
  4362. * number of bytes that were freed while we were trying our
  4363. * reservation.
  4364. */
  4365. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4366. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4367. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4368. /*
  4369. * Now we need to see how much we would have freed had we not
  4370. * been making this reservation and our ->csum_bytes were not
  4371. * artificially inflated.
  4372. */
  4373. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4374. bytes = csum_bytes - orig_csum_bytes;
  4375. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4376. /*
  4377. * Now reset ->csum_bytes to what it should be. If bytes is
  4378. * more than to_free then we would have free'd more space had we
  4379. * not had an artificially high ->csum_bytes, so we need to free
  4380. * the remainder. If bytes is the same or less then we don't
  4381. * need to do anything, the other free-ers did the correct
  4382. * thing.
  4383. */
  4384. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4385. if (bytes > to_free)
  4386. to_free = bytes - to_free;
  4387. else
  4388. to_free = 0;
  4389. }
  4390. spin_unlock(&BTRFS_I(inode)->lock);
  4391. if (dropped)
  4392. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4393. if (to_free) {
  4394. btrfs_block_rsv_release(root, block_rsv, to_free);
  4395. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4396. btrfs_ino(inode), to_free, 0);
  4397. }
  4398. if (delalloc_lock)
  4399. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4400. return ret;
  4401. }
  4402. /**
  4403. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4404. * @inode: the inode to release the reservation for
  4405. * @num_bytes: the number of bytes we're releasing
  4406. *
  4407. * This will release the metadata reservation for an inode. This can be called
  4408. * once we complete IO for a given set of bytes to release their metadata
  4409. * reservations.
  4410. */
  4411. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4412. {
  4413. struct btrfs_root *root = BTRFS_I(inode)->root;
  4414. u64 to_free = 0;
  4415. unsigned dropped;
  4416. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4417. spin_lock(&BTRFS_I(inode)->lock);
  4418. dropped = drop_outstanding_extent(inode);
  4419. if (num_bytes)
  4420. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4421. spin_unlock(&BTRFS_I(inode)->lock);
  4422. if (dropped > 0)
  4423. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4424. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4425. btrfs_ino(inode), to_free, 0);
  4426. if (root->fs_info->quota_enabled) {
  4427. btrfs_qgroup_free(root, num_bytes +
  4428. dropped * root->leafsize);
  4429. }
  4430. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4431. to_free);
  4432. }
  4433. /**
  4434. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4435. * @inode: inode we're writing to
  4436. * @num_bytes: the number of bytes we want to allocate
  4437. *
  4438. * This will do the following things
  4439. *
  4440. * o reserve space in the data space info for num_bytes
  4441. * o reserve space in the metadata space info based on number of outstanding
  4442. * extents and how much csums will be needed
  4443. * o add to the inodes ->delalloc_bytes
  4444. * o add it to the fs_info's delalloc inodes list.
  4445. *
  4446. * This will return 0 for success and -ENOSPC if there is no space left.
  4447. */
  4448. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4449. {
  4450. int ret;
  4451. ret = btrfs_check_data_free_space(inode, num_bytes);
  4452. if (ret)
  4453. return ret;
  4454. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4455. if (ret) {
  4456. btrfs_free_reserved_data_space(inode, num_bytes);
  4457. return ret;
  4458. }
  4459. return 0;
  4460. }
  4461. /**
  4462. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4463. * @inode: inode we're releasing space for
  4464. * @num_bytes: the number of bytes we want to free up
  4465. *
  4466. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4467. * called in the case that we don't need the metadata AND data reservations
  4468. * anymore. So if there is an error or we insert an inline extent.
  4469. *
  4470. * This function will release the metadata space that was not used and will
  4471. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4472. * list if there are no delalloc bytes left.
  4473. */
  4474. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4475. {
  4476. btrfs_delalloc_release_metadata(inode, num_bytes);
  4477. btrfs_free_reserved_data_space(inode, num_bytes);
  4478. }
  4479. static int update_block_group(struct btrfs_root *root,
  4480. u64 bytenr, u64 num_bytes, int alloc)
  4481. {
  4482. struct btrfs_block_group_cache *cache = NULL;
  4483. struct btrfs_fs_info *info = root->fs_info;
  4484. u64 total = num_bytes;
  4485. u64 old_val;
  4486. u64 byte_in_group;
  4487. int factor;
  4488. /* block accounting for super block */
  4489. spin_lock(&info->delalloc_root_lock);
  4490. old_val = btrfs_super_bytes_used(info->super_copy);
  4491. if (alloc)
  4492. old_val += num_bytes;
  4493. else
  4494. old_val -= num_bytes;
  4495. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4496. spin_unlock(&info->delalloc_root_lock);
  4497. while (total) {
  4498. cache = btrfs_lookup_block_group(info, bytenr);
  4499. if (!cache)
  4500. return -ENOENT;
  4501. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4502. BTRFS_BLOCK_GROUP_RAID1 |
  4503. BTRFS_BLOCK_GROUP_RAID10))
  4504. factor = 2;
  4505. else
  4506. factor = 1;
  4507. /*
  4508. * If this block group has free space cache written out, we
  4509. * need to make sure to load it if we are removing space. This
  4510. * is because we need the unpinning stage to actually add the
  4511. * space back to the block group, otherwise we will leak space.
  4512. */
  4513. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4514. cache_block_group(cache, 1);
  4515. byte_in_group = bytenr - cache->key.objectid;
  4516. WARN_ON(byte_in_group > cache->key.offset);
  4517. spin_lock(&cache->space_info->lock);
  4518. spin_lock(&cache->lock);
  4519. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4520. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4521. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4522. cache->dirty = 1;
  4523. old_val = btrfs_block_group_used(&cache->item);
  4524. num_bytes = min(total, cache->key.offset - byte_in_group);
  4525. if (alloc) {
  4526. old_val += num_bytes;
  4527. btrfs_set_block_group_used(&cache->item, old_val);
  4528. cache->reserved -= num_bytes;
  4529. cache->space_info->bytes_reserved -= num_bytes;
  4530. cache->space_info->bytes_used += num_bytes;
  4531. cache->space_info->disk_used += num_bytes * factor;
  4532. spin_unlock(&cache->lock);
  4533. spin_unlock(&cache->space_info->lock);
  4534. } else {
  4535. old_val -= num_bytes;
  4536. btrfs_set_block_group_used(&cache->item, old_val);
  4537. cache->pinned += num_bytes;
  4538. cache->space_info->bytes_pinned += num_bytes;
  4539. cache->space_info->bytes_used -= num_bytes;
  4540. cache->space_info->disk_used -= num_bytes * factor;
  4541. spin_unlock(&cache->lock);
  4542. spin_unlock(&cache->space_info->lock);
  4543. set_extent_dirty(info->pinned_extents,
  4544. bytenr, bytenr + num_bytes - 1,
  4545. GFP_NOFS | __GFP_NOFAIL);
  4546. }
  4547. btrfs_put_block_group(cache);
  4548. total -= num_bytes;
  4549. bytenr += num_bytes;
  4550. }
  4551. return 0;
  4552. }
  4553. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4554. {
  4555. struct btrfs_block_group_cache *cache;
  4556. u64 bytenr;
  4557. spin_lock(&root->fs_info->block_group_cache_lock);
  4558. bytenr = root->fs_info->first_logical_byte;
  4559. spin_unlock(&root->fs_info->block_group_cache_lock);
  4560. if (bytenr < (u64)-1)
  4561. return bytenr;
  4562. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4563. if (!cache)
  4564. return 0;
  4565. bytenr = cache->key.objectid;
  4566. btrfs_put_block_group(cache);
  4567. return bytenr;
  4568. }
  4569. static int pin_down_extent(struct btrfs_root *root,
  4570. struct btrfs_block_group_cache *cache,
  4571. u64 bytenr, u64 num_bytes, int reserved)
  4572. {
  4573. spin_lock(&cache->space_info->lock);
  4574. spin_lock(&cache->lock);
  4575. cache->pinned += num_bytes;
  4576. cache->space_info->bytes_pinned += num_bytes;
  4577. if (reserved) {
  4578. cache->reserved -= num_bytes;
  4579. cache->space_info->bytes_reserved -= num_bytes;
  4580. }
  4581. spin_unlock(&cache->lock);
  4582. spin_unlock(&cache->space_info->lock);
  4583. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4584. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4585. return 0;
  4586. }
  4587. /*
  4588. * this function must be called within transaction
  4589. */
  4590. int btrfs_pin_extent(struct btrfs_root *root,
  4591. u64 bytenr, u64 num_bytes, int reserved)
  4592. {
  4593. struct btrfs_block_group_cache *cache;
  4594. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4595. BUG_ON(!cache); /* Logic error */
  4596. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4597. btrfs_put_block_group(cache);
  4598. return 0;
  4599. }
  4600. /*
  4601. * this function must be called within transaction
  4602. */
  4603. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  4604. u64 bytenr, u64 num_bytes)
  4605. {
  4606. struct btrfs_block_group_cache *cache;
  4607. int ret;
  4608. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4609. if (!cache)
  4610. return -EINVAL;
  4611. /*
  4612. * pull in the free space cache (if any) so that our pin
  4613. * removes the free space from the cache. We have load_only set
  4614. * to one because the slow code to read in the free extents does check
  4615. * the pinned extents.
  4616. */
  4617. cache_block_group(cache, 1);
  4618. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4619. /* remove us from the free space cache (if we're there at all) */
  4620. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  4621. btrfs_put_block_group(cache);
  4622. return ret;
  4623. }
  4624. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  4625. {
  4626. int ret;
  4627. struct btrfs_block_group_cache *block_group;
  4628. struct btrfs_caching_control *caching_ctl;
  4629. block_group = btrfs_lookup_block_group(root->fs_info, start);
  4630. if (!block_group)
  4631. return -EINVAL;
  4632. cache_block_group(block_group, 0);
  4633. caching_ctl = get_caching_control(block_group);
  4634. if (!caching_ctl) {
  4635. /* Logic error */
  4636. BUG_ON(!block_group_cache_done(block_group));
  4637. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4638. } else {
  4639. mutex_lock(&caching_ctl->mutex);
  4640. if (start >= caching_ctl->progress) {
  4641. ret = add_excluded_extent(root, start, num_bytes);
  4642. } else if (start + num_bytes <= caching_ctl->progress) {
  4643. ret = btrfs_remove_free_space(block_group,
  4644. start, num_bytes);
  4645. } else {
  4646. num_bytes = caching_ctl->progress - start;
  4647. ret = btrfs_remove_free_space(block_group,
  4648. start, num_bytes);
  4649. if (ret)
  4650. goto out_lock;
  4651. num_bytes = (start + num_bytes) -
  4652. caching_ctl->progress;
  4653. start = caching_ctl->progress;
  4654. ret = add_excluded_extent(root, start, num_bytes);
  4655. }
  4656. out_lock:
  4657. mutex_unlock(&caching_ctl->mutex);
  4658. put_caching_control(caching_ctl);
  4659. }
  4660. btrfs_put_block_group(block_group);
  4661. return ret;
  4662. }
  4663. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  4664. struct extent_buffer *eb)
  4665. {
  4666. struct btrfs_file_extent_item *item;
  4667. struct btrfs_key key;
  4668. int found_type;
  4669. int i;
  4670. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  4671. return 0;
  4672. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  4673. btrfs_item_key_to_cpu(eb, &key, i);
  4674. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4675. continue;
  4676. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  4677. found_type = btrfs_file_extent_type(eb, item);
  4678. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  4679. continue;
  4680. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  4681. continue;
  4682. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  4683. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  4684. __exclude_logged_extent(log, key.objectid, key.offset);
  4685. }
  4686. return 0;
  4687. }
  4688. /**
  4689. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4690. * @cache: The cache we are manipulating
  4691. * @num_bytes: The number of bytes in question
  4692. * @reserve: One of the reservation enums
  4693. *
  4694. * This is called by the allocator when it reserves space, or by somebody who is
  4695. * freeing space that was never actually used on disk. For example if you
  4696. * reserve some space for a new leaf in transaction A and before transaction A
  4697. * commits you free that leaf, you call this with reserve set to 0 in order to
  4698. * clear the reservation.
  4699. *
  4700. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4701. * ENOSPC accounting. For data we handle the reservation through clearing the
  4702. * delalloc bits in the io_tree. We have to do this since we could end up
  4703. * allocating less disk space for the amount of data we have reserved in the
  4704. * case of compression.
  4705. *
  4706. * If this is a reservation and the block group has become read only we cannot
  4707. * make the reservation and return -EAGAIN, otherwise this function always
  4708. * succeeds.
  4709. */
  4710. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4711. u64 num_bytes, int reserve)
  4712. {
  4713. struct btrfs_space_info *space_info = cache->space_info;
  4714. int ret = 0;
  4715. spin_lock(&space_info->lock);
  4716. spin_lock(&cache->lock);
  4717. if (reserve != RESERVE_FREE) {
  4718. if (cache->ro) {
  4719. ret = -EAGAIN;
  4720. } else {
  4721. cache->reserved += num_bytes;
  4722. space_info->bytes_reserved += num_bytes;
  4723. if (reserve == RESERVE_ALLOC) {
  4724. trace_btrfs_space_reservation(cache->fs_info,
  4725. "space_info", space_info->flags,
  4726. num_bytes, 0);
  4727. space_info->bytes_may_use -= num_bytes;
  4728. }
  4729. }
  4730. } else {
  4731. if (cache->ro)
  4732. space_info->bytes_readonly += num_bytes;
  4733. cache->reserved -= num_bytes;
  4734. space_info->bytes_reserved -= num_bytes;
  4735. space_info->reservation_progress++;
  4736. }
  4737. spin_unlock(&cache->lock);
  4738. spin_unlock(&space_info->lock);
  4739. return ret;
  4740. }
  4741. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4742. struct btrfs_root *root)
  4743. {
  4744. struct btrfs_fs_info *fs_info = root->fs_info;
  4745. struct btrfs_caching_control *next;
  4746. struct btrfs_caching_control *caching_ctl;
  4747. struct btrfs_block_group_cache *cache;
  4748. down_write(&fs_info->extent_commit_sem);
  4749. list_for_each_entry_safe(caching_ctl, next,
  4750. &fs_info->caching_block_groups, list) {
  4751. cache = caching_ctl->block_group;
  4752. if (block_group_cache_done(cache)) {
  4753. cache->last_byte_to_unpin = (u64)-1;
  4754. list_del_init(&caching_ctl->list);
  4755. put_caching_control(caching_ctl);
  4756. } else {
  4757. cache->last_byte_to_unpin = caching_ctl->progress;
  4758. }
  4759. }
  4760. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4761. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4762. else
  4763. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4764. up_write(&fs_info->extent_commit_sem);
  4765. update_global_block_rsv(fs_info);
  4766. }
  4767. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4768. {
  4769. struct btrfs_fs_info *fs_info = root->fs_info;
  4770. struct btrfs_block_group_cache *cache = NULL;
  4771. struct btrfs_space_info *space_info;
  4772. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4773. u64 len;
  4774. bool readonly;
  4775. while (start <= end) {
  4776. readonly = false;
  4777. if (!cache ||
  4778. start >= cache->key.objectid + cache->key.offset) {
  4779. if (cache)
  4780. btrfs_put_block_group(cache);
  4781. cache = btrfs_lookup_block_group(fs_info, start);
  4782. BUG_ON(!cache); /* Logic error */
  4783. }
  4784. len = cache->key.objectid + cache->key.offset - start;
  4785. len = min(len, end + 1 - start);
  4786. if (start < cache->last_byte_to_unpin) {
  4787. len = min(len, cache->last_byte_to_unpin - start);
  4788. btrfs_add_free_space(cache, start, len);
  4789. }
  4790. start += len;
  4791. space_info = cache->space_info;
  4792. spin_lock(&space_info->lock);
  4793. spin_lock(&cache->lock);
  4794. cache->pinned -= len;
  4795. space_info->bytes_pinned -= len;
  4796. if (cache->ro) {
  4797. space_info->bytes_readonly += len;
  4798. readonly = true;
  4799. }
  4800. spin_unlock(&cache->lock);
  4801. if (!readonly && global_rsv->space_info == space_info) {
  4802. spin_lock(&global_rsv->lock);
  4803. if (!global_rsv->full) {
  4804. len = min(len, global_rsv->size -
  4805. global_rsv->reserved);
  4806. global_rsv->reserved += len;
  4807. space_info->bytes_may_use += len;
  4808. if (global_rsv->reserved >= global_rsv->size)
  4809. global_rsv->full = 1;
  4810. }
  4811. spin_unlock(&global_rsv->lock);
  4812. }
  4813. spin_unlock(&space_info->lock);
  4814. }
  4815. if (cache)
  4816. btrfs_put_block_group(cache);
  4817. return 0;
  4818. }
  4819. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4820. struct btrfs_root *root)
  4821. {
  4822. struct btrfs_fs_info *fs_info = root->fs_info;
  4823. struct extent_io_tree *unpin;
  4824. u64 start;
  4825. u64 end;
  4826. int ret;
  4827. if (trans->aborted)
  4828. return 0;
  4829. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4830. unpin = &fs_info->freed_extents[1];
  4831. else
  4832. unpin = &fs_info->freed_extents[0];
  4833. while (1) {
  4834. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4835. EXTENT_DIRTY, NULL);
  4836. if (ret)
  4837. break;
  4838. if (btrfs_test_opt(root, DISCARD))
  4839. ret = btrfs_discard_extent(root, start,
  4840. end + 1 - start, NULL);
  4841. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4842. unpin_extent_range(root, start, end);
  4843. cond_resched();
  4844. }
  4845. return 0;
  4846. }
  4847. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4848. struct btrfs_root *root,
  4849. u64 bytenr, u64 num_bytes, u64 parent,
  4850. u64 root_objectid, u64 owner_objectid,
  4851. u64 owner_offset, int refs_to_drop,
  4852. struct btrfs_delayed_extent_op *extent_op)
  4853. {
  4854. struct btrfs_key key;
  4855. struct btrfs_path *path;
  4856. struct btrfs_fs_info *info = root->fs_info;
  4857. struct btrfs_root *extent_root = info->extent_root;
  4858. struct extent_buffer *leaf;
  4859. struct btrfs_extent_item *ei;
  4860. struct btrfs_extent_inline_ref *iref;
  4861. int ret;
  4862. int is_data;
  4863. int extent_slot = 0;
  4864. int found_extent = 0;
  4865. int num_to_del = 1;
  4866. u32 item_size;
  4867. u64 refs;
  4868. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  4869. SKINNY_METADATA);
  4870. path = btrfs_alloc_path();
  4871. if (!path)
  4872. return -ENOMEM;
  4873. path->reada = 1;
  4874. path->leave_spinning = 1;
  4875. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4876. BUG_ON(!is_data && refs_to_drop != 1);
  4877. if (is_data)
  4878. skinny_metadata = 0;
  4879. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4880. bytenr, num_bytes, parent,
  4881. root_objectid, owner_objectid,
  4882. owner_offset);
  4883. if (ret == 0) {
  4884. extent_slot = path->slots[0];
  4885. while (extent_slot >= 0) {
  4886. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4887. extent_slot);
  4888. if (key.objectid != bytenr)
  4889. break;
  4890. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4891. key.offset == num_bytes) {
  4892. found_extent = 1;
  4893. break;
  4894. }
  4895. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  4896. key.offset == owner_objectid) {
  4897. found_extent = 1;
  4898. break;
  4899. }
  4900. if (path->slots[0] - extent_slot > 5)
  4901. break;
  4902. extent_slot--;
  4903. }
  4904. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4905. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4906. if (found_extent && item_size < sizeof(*ei))
  4907. found_extent = 0;
  4908. #endif
  4909. if (!found_extent) {
  4910. BUG_ON(iref);
  4911. ret = remove_extent_backref(trans, extent_root, path,
  4912. NULL, refs_to_drop,
  4913. is_data);
  4914. if (ret) {
  4915. btrfs_abort_transaction(trans, extent_root, ret);
  4916. goto out;
  4917. }
  4918. btrfs_release_path(path);
  4919. path->leave_spinning = 1;
  4920. key.objectid = bytenr;
  4921. key.type = BTRFS_EXTENT_ITEM_KEY;
  4922. key.offset = num_bytes;
  4923. if (!is_data && skinny_metadata) {
  4924. key.type = BTRFS_METADATA_ITEM_KEY;
  4925. key.offset = owner_objectid;
  4926. }
  4927. ret = btrfs_search_slot(trans, extent_root,
  4928. &key, path, -1, 1);
  4929. if (ret > 0 && skinny_metadata && path->slots[0]) {
  4930. /*
  4931. * Couldn't find our skinny metadata item,
  4932. * see if we have ye olde extent item.
  4933. */
  4934. path->slots[0]--;
  4935. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4936. path->slots[0]);
  4937. if (key.objectid == bytenr &&
  4938. key.type == BTRFS_EXTENT_ITEM_KEY &&
  4939. key.offset == num_bytes)
  4940. ret = 0;
  4941. }
  4942. if (ret > 0 && skinny_metadata) {
  4943. skinny_metadata = false;
  4944. key.type = BTRFS_EXTENT_ITEM_KEY;
  4945. key.offset = num_bytes;
  4946. btrfs_release_path(path);
  4947. ret = btrfs_search_slot(trans, extent_root,
  4948. &key, path, -1, 1);
  4949. }
  4950. if (ret) {
  4951. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  4952. ret, (unsigned long long)bytenr);
  4953. if (ret > 0)
  4954. btrfs_print_leaf(extent_root,
  4955. path->nodes[0]);
  4956. }
  4957. if (ret < 0) {
  4958. btrfs_abort_transaction(trans, extent_root, ret);
  4959. goto out;
  4960. }
  4961. extent_slot = path->slots[0];
  4962. }
  4963. } else if (ret == -ENOENT) {
  4964. btrfs_print_leaf(extent_root, path->nodes[0]);
  4965. WARN_ON(1);
  4966. btrfs_err(info,
  4967. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  4968. (unsigned long long)bytenr,
  4969. (unsigned long long)parent,
  4970. (unsigned long long)root_objectid,
  4971. (unsigned long long)owner_objectid,
  4972. (unsigned long long)owner_offset);
  4973. } else {
  4974. btrfs_abort_transaction(trans, extent_root, ret);
  4975. goto out;
  4976. }
  4977. leaf = path->nodes[0];
  4978. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4979. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4980. if (item_size < sizeof(*ei)) {
  4981. BUG_ON(found_extent || extent_slot != path->slots[0]);
  4982. ret = convert_extent_item_v0(trans, extent_root, path,
  4983. owner_objectid, 0);
  4984. if (ret < 0) {
  4985. btrfs_abort_transaction(trans, extent_root, ret);
  4986. goto out;
  4987. }
  4988. btrfs_release_path(path);
  4989. path->leave_spinning = 1;
  4990. key.objectid = bytenr;
  4991. key.type = BTRFS_EXTENT_ITEM_KEY;
  4992. key.offset = num_bytes;
  4993. ret = btrfs_search_slot(trans, extent_root, &key, path,
  4994. -1, 1);
  4995. if (ret) {
  4996. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  4997. ret, (unsigned long long)bytenr);
  4998. btrfs_print_leaf(extent_root, path->nodes[0]);
  4999. }
  5000. if (ret < 0) {
  5001. btrfs_abort_transaction(trans, extent_root, ret);
  5002. goto out;
  5003. }
  5004. extent_slot = path->slots[0];
  5005. leaf = path->nodes[0];
  5006. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5007. }
  5008. #endif
  5009. BUG_ON(item_size < sizeof(*ei));
  5010. ei = btrfs_item_ptr(leaf, extent_slot,
  5011. struct btrfs_extent_item);
  5012. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5013. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5014. struct btrfs_tree_block_info *bi;
  5015. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5016. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5017. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5018. }
  5019. refs = btrfs_extent_refs(leaf, ei);
  5020. if (refs < refs_to_drop) {
  5021. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5022. "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
  5023. ret = -EINVAL;
  5024. btrfs_abort_transaction(trans, extent_root, ret);
  5025. goto out;
  5026. }
  5027. refs -= refs_to_drop;
  5028. if (refs > 0) {
  5029. if (extent_op)
  5030. __run_delayed_extent_op(extent_op, leaf, ei);
  5031. /*
  5032. * In the case of inline back ref, reference count will
  5033. * be updated by remove_extent_backref
  5034. */
  5035. if (iref) {
  5036. BUG_ON(!found_extent);
  5037. } else {
  5038. btrfs_set_extent_refs(leaf, ei, refs);
  5039. btrfs_mark_buffer_dirty(leaf);
  5040. }
  5041. if (found_extent) {
  5042. ret = remove_extent_backref(trans, extent_root, path,
  5043. iref, refs_to_drop,
  5044. is_data);
  5045. if (ret) {
  5046. btrfs_abort_transaction(trans, extent_root, ret);
  5047. goto out;
  5048. }
  5049. }
  5050. } else {
  5051. if (found_extent) {
  5052. BUG_ON(is_data && refs_to_drop !=
  5053. extent_data_ref_count(root, path, iref));
  5054. if (iref) {
  5055. BUG_ON(path->slots[0] != extent_slot);
  5056. } else {
  5057. BUG_ON(path->slots[0] != extent_slot + 1);
  5058. path->slots[0] = extent_slot;
  5059. num_to_del = 2;
  5060. }
  5061. }
  5062. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5063. num_to_del);
  5064. if (ret) {
  5065. btrfs_abort_transaction(trans, extent_root, ret);
  5066. goto out;
  5067. }
  5068. btrfs_release_path(path);
  5069. if (is_data) {
  5070. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5071. if (ret) {
  5072. btrfs_abort_transaction(trans, extent_root, ret);
  5073. goto out;
  5074. }
  5075. }
  5076. ret = update_block_group(root, bytenr, num_bytes, 0);
  5077. if (ret) {
  5078. btrfs_abort_transaction(trans, extent_root, ret);
  5079. goto out;
  5080. }
  5081. }
  5082. out:
  5083. btrfs_free_path(path);
  5084. return ret;
  5085. }
  5086. /*
  5087. * when we free an block, it is possible (and likely) that we free the last
  5088. * delayed ref for that extent as well. This searches the delayed ref tree for
  5089. * a given extent, and if there are no other delayed refs to be processed, it
  5090. * removes it from the tree.
  5091. */
  5092. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5093. struct btrfs_root *root, u64 bytenr)
  5094. {
  5095. struct btrfs_delayed_ref_head *head;
  5096. struct btrfs_delayed_ref_root *delayed_refs;
  5097. struct btrfs_delayed_ref_node *ref;
  5098. struct rb_node *node;
  5099. int ret = 0;
  5100. delayed_refs = &trans->transaction->delayed_refs;
  5101. spin_lock(&delayed_refs->lock);
  5102. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5103. if (!head)
  5104. goto out;
  5105. node = rb_prev(&head->node.rb_node);
  5106. if (!node)
  5107. goto out;
  5108. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  5109. /* there are still entries for this ref, we can't drop it */
  5110. if (ref->bytenr == bytenr)
  5111. goto out;
  5112. if (head->extent_op) {
  5113. if (!head->must_insert_reserved)
  5114. goto out;
  5115. btrfs_free_delayed_extent_op(head->extent_op);
  5116. head->extent_op = NULL;
  5117. }
  5118. /*
  5119. * waiting for the lock here would deadlock. If someone else has it
  5120. * locked they are already in the process of dropping it anyway
  5121. */
  5122. if (!mutex_trylock(&head->mutex))
  5123. goto out;
  5124. /*
  5125. * at this point we have a head with no other entries. Go
  5126. * ahead and process it.
  5127. */
  5128. head->node.in_tree = 0;
  5129. rb_erase(&head->node.rb_node, &delayed_refs->root);
  5130. delayed_refs->num_entries--;
  5131. /*
  5132. * we don't take a ref on the node because we're removing it from the
  5133. * tree, so we just steal the ref the tree was holding.
  5134. */
  5135. delayed_refs->num_heads--;
  5136. if (list_empty(&head->cluster))
  5137. delayed_refs->num_heads_ready--;
  5138. list_del_init(&head->cluster);
  5139. spin_unlock(&delayed_refs->lock);
  5140. BUG_ON(head->extent_op);
  5141. if (head->must_insert_reserved)
  5142. ret = 1;
  5143. mutex_unlock(&head->mutex);
  5144. btrfs_put_delayed_ref(&head->node);
  5145. return ret;
  5146. out:
  5147. spin_unlock(&delayed_refs->lock);
  5148. return 0;
  5149. }
  5150. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5151. struct btrfs_root *root,
  5152. struct extent_buffer *buf,
  5153. u64 parent, int last_ref)
  5154. {
  5155. struct btrfs_block_group_cache *cache = NULL;
  5156. int ret;
  5157. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5158. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5159. buf->start, buf->len,
  5160. parent, root->root_key.objectid,
  5161. btrfs_header_level(buf),
  5162. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5163. BUG_ON(ret); /* -ENOMEM */
  5164. }
  5165. if (!last_ref)
  5166. return;
  5167. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5168. if (btrfs_header_generation(buf) == trans->transid) {
  5169. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5170. ret = check_ref_cleanup(trans, root, buf->start);
  5171. if (!ret)
  5172. goto out;
  5173. }
  5174. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5175. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5176. goto out;
  5177. }
  5178. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5179. btrfs_add_free_space(cache, buf->start, buf->len);
  5180. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  5181. }
  5182. out:
  5183. /*
  5184. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5185. * anymore.
  5186. */
  5187. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5188. btrfs_put_block_group(cache);
  5189. }
  5190. /* Can return -ENOMEM */
  5191. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5192. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5193. u64 owner, u64 offset, int for_cow)
  5194. {
  5195. int ret;
  5196. struct btrfs_fs_info *fs_info = root->fs_info;
  5197. /*
  5198. * tree log blocks never actually go into the extent allocation
  5199. * tree, just update pinning info and exit early.
  5200. */
  5201. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5202. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5203. /* unlocks the pinned mutex */
  5204. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5205. ret = 0;
  5206. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5207. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5208. num_bytes,
  5209. parent, root_objectid, (int)owner,
  5210. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  5211. } else {
  5212. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5213. num_bytes,
  5214. parent, root_objectid, owner,
  5215. offset, BTRFS_DROP_DELAYED_REF,
  5216. NULL, for_cow);
  5217. }
  5218. return ret;
  5219. }
  5220. static u64 stripe_align(struct btrfs_root *root,
  5221. struct btrfs_block_group_cache *cache,
  5222. u64 val, u64 num_bytes)
  5223. {
  5224. u64 ret = ALIGN(val, root->stripesize);
  5225. return ret;
  5226. }
  5227. /*
  5228. * when we wait for progress in the block group caching, its because
  5229. * our allocation attempt failed at least once. So, we must sleep
  5230. * and let some progress happen before we try again.
  5231. *
  5232. * This function will sleep at least once waiting for new free space to
  5233. * show up, and then it will check the block group free space numbers
  5234. * for our min num_bytes. Another option is to have it go ahead
  5235. * and look in the rbtree for a free extent of a given size, but this
  5236. * is a good start.
  5237. */
  5238. static noinline int
  5239. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5240. u64 num_bytes)
  5241. {
  5242. struct btrfs_caching_control *caching_ctl;
  5243. caching_ctl = get_caching_control(cache);
  5244. if (!caching_ctl)
  5245. return 0;
  5246. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5247. (cache->free_space_ctl->free_space >= num_bytes));
  5248. put_caching_control(caching_ctl);
  5249. return 0;
  5250. }
  5251. static noinline int
  5252. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5253. {
  5254. struct btrfs_caching_control *caching_ctl;
  5255. caching_ctl = get_caching_control(cache);
  5256. if (!caching_ctl)
  5257. return 0;
  5258. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5259. put_caching_control(caching_ctl);
  5260. return 0;
  5261. }
  5262. int __get_raid_index(u64 flags)
  5263. {
  5264. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5265. return BTRFS_RAID_RAID10;
  5266. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5267. return BTRFS_RAID_RAID1;
  5268. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5269. return BTRFS_RAID_DUP;
  5270. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5271. return BTRFS_RAID_RAID0;
  5272. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5273. return BTRFS_RAID_RAID5;
  5274. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5275. return BTRFS_RAID_RAID6;
  5276. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5277. }
  5278. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  5279. {
  5280. return __get_raid_index(cache->flags);
  5281. }
  5282. enum btrfs_loop_type {
  5283. LOOP_CACHING_NOWAIT = 0,
  5284. LOOP_CACHING_WAIT = 1,
  5285. LOOP_ALLOC_CHUNK = 2,
  5286. LOOP_NO_EMPTY_SIZE = 3,
  5287. };
  5288. /*
  5289. * walks the btree of allocated extents and find a hole of a given size.
  5290. * The key ins is changed to record the hole:
  5291. * ins->objectid == block start
  5292. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5293. * ins->offset == number of blocks
  5294. * Any available blocks before search_start are skipped.
  5295. */
  5296. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  5297. struct btrfs_root *orig_root,
  5298. u64 num_bytes, u64 empty_size,
  5299. u64 hint_byte, struct btrfs_key *ins,
  5300. u64 flags)
  5301. {
  5302. int ret = 0;
  5303. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5304. struct btrfs_free_cluster *last_ptr = NULL;
  5305. struct btrfs_block_group_cache *block_group = NULL;
  5306. struct btrfs_block_group_cache *used_block_group;
  5307. u64 search_start = 0;
  5308. int empty_cluster = 2 * 1024 * 1024;
  5309. struct btrfs_space_info *space_info;
  5310. int loop = 0;
  5311. int index = __get_raid_index(flags);
  5312. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5313. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5314. bool found_uncached_bg = false;
  5315. bool failed_cluster_refill = false;
  5316. bool failed_alloc = false;
  5317. bool use_cluster = true;
  5318. bool have_caching_bg = false;
  5319. WARN_ON(num_bytes < root->sectorsize);
  5320. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  5321. ins->objectid = 0;
  5322. ins->offset = 0;
  5323. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5324. space_info = __find_space_info(root->fs_info, flags);
  5325. if (!space_info) {
  5326. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5327. return -ENOSPC;
  5328. }
  5329. /*
  5330. * If the space info is for both data and metadata it means we have a
  5331. * small filesystem and we can't use the clustering stuff.
  5332. */
  5333. if (btrfs_mixed_space_info(space_info))
  5334. use_cluster = false;
  5335. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5336. last_ptr = &root->fs_info->meta_alloc_cluster;
  5337. if (!btrfs_test_opt(root, SSD))
  5338. empty_cluster = 64 * 1024;
  5339. }
  5340. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5341. btrfs_test_opt(root, SSD)) {
  5342. last_ptr = &root->fs_info->data_alloc_cluster;
  5343. }
  5344. if (last_ptr) {
  5345. spin_lock(&last_ptr->lock);
  5346. if (last_ptr->block_group)
  5347. hint_byte = last_ptr->window_start;
  5348. spin_unlock(&last_ptr->lock);
  5349. }
  5350. search_start = max(search_start, first_logical_byte(root, 0));
  5351. search_start = max(search_start, hint_byte);
  5352. if (!last_ptr)
  5353. empty_cluster = 0;
  5354. if (search_start == hint_byte) {
  5355. block_group = btrfs_lookup_block_group(root->fs_info,
  5356. search_start);
  5357. used_block_group = block_group;
  5358. /*
  5359. * we don't want to use the block group if it doesn't match our
  5360. * allocation bits, or if its not cached.
  5361. *
  5362. * However if we are re-searching with an ideal block group
  5363. * picked out then we don't care that the block group is cached.
  5364. */
  5365. if (block_group && block_group_bits(block_group, flags) &&
  5366. block_group->cached != BTRFS_CACHE_NO) {
  5367. down_read(&space_info->groups_sem);
  5368. if (list_empty(&block_group->list) ||
  5369. block_group->ro) {
  5370. /*
  5371. * someone is removing this block group,
  5372. * we can't jump into the have_block_group
  5373. * target because our list pointers are not
  5374. * valid
  5375. */
  5376. btrfs_put_block_group(block_group);
  5377. up_read(&space_info->groups_sem);
  5378. } else {
  5379. index = get_block_group_index(block_group);
  5380. goto have_block_group;
  5381. }
  5382. } else if (block_group) {
  5383. btrfs_put_block_group(block_group);
  5384. }
  5385. }
  5386. search:
  5387. have_caching_bg = false;
  5388. down_read(&space_info->groups_sem);
  5389. list_for_each_entry(block_group, &space_info->block_groups[index],
  5390. list) {
  5391. u64 offset;
  5392. int cached;
  5393. used_block_group = block_group;
  5394. btrfs_get_block_group(block_group);
  5395. search_start = block_group->key.objectid;
  5396. /*
  5397. * this can happen if we end up cycling through all the
  5398. * raid types, but we want to make sure we only allocate
  5399. * for the proper type.
  5400. */
  5401. if (!block_group_bits(block_group, flags)) {
  5402. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  5403. BTRFS_BLOCK_GROUP_RAID1 |
  5404. BTRFS_BLOCK_GROUP_RAID5 |
  5405. BTRFS_BLOCK_GROUP_RAID6 |
  5406. BTRFS_BLOCK_GROUP_RAID10;
  5407. /*
  5408. * if they asked for extra copies and this block group
  5409. * doesn't provide them, bail. This does allow us to
  5410. * fill raid0 from raid1.
  5411. */
  5412. if ((flags & extra) && !(block_group->flags & extra))
  5413. goto loop;
  5414. }
  5415. have_block_group:
  5416. cached = block_group_cache_done(block_group);
  5417. if (unlikely(!cached)) {
  5418. found_uncached_bg = true;
  5419. ret = cache_block_group(block_group, 0);
  5420. BUG_ON(ret < 0);
  5421. ret = 0;
  5422. }
  5423. if (unlikely(block_group->ro))
  5424. goto loop;
  5425. /*
  5426. * Ok we want to try and use the cluster allocator, so
  5427. * lets look there
  5428. */
  5429. if (last_ptr) {
  5430. unsigned long aligned_cluster;
  5431. /*
  5432. * the refill lock keeps out other
  5433. * people trying to start a new cluster
  5434. */
  5435. spin_lock(&last_ptr->refill_lock);
  5436. used_block_group = last_ptr->block_group;
  5437. if (used_block_group != block_group &&
  5438. (!used_block_group ||
  5439. used_block_group->ro ||
  5440. !block_group_bits(used_block_group, flags))) {
  5441. used_block_group = block_group;
  5442. goto refill_cluster;
  5443. }
  5444. if (used_block_group != block_group)
  5445. btrfs_get_block_group(used_block_group);
  5446. offset = btrfs_alloc_from_cluster(used_block_group,
  5447. last_ptr, num_bytes, used_block_group->key.objectid);
  5448. if (offset) {
  5449. /* we have a block, we're done */
  5450. spin_unlock(&last_ptr->refill_lock);
  5451. trace_btrfs_reserve_extent_cluster(root,
  5452. block_group, search_start, num_bytes);
  5453. goto checks;
  5454. }
  5455. WARN_ON(last_ptr->block_group != used_block_group);
  5456. if (used_block_group != block_group) {
  5457. btrfs_put_block_group(used_block_group);
  5458. used_block_group = block_group;
  5459. }
  5460. refill_cluster:
  5461. BUG_ON(used_block_group != block_group);
  5462. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5463. * set up a new clusters, so lets just skip it
  5464. * and let the allocator find whatever block
  5465. * it can find. If we reach this point, we
  5466. * will have tried the cluster allocator
  5467. * plenty of times and not have found
  5468. * anything, so we are likely way too
  5469. * fragmented for the clustering stuff to find
  5470. * anything.
  5471. *
  5472. * However, if the cluster is taken from the
  5473. * current block group, release the cluster
  5474. * first, so that we stand a better chance of
  5475. * succeeding in the unclustered
  5476. * allocation. */
  5477. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5478. last_ptr->block_group != block_group) {
  5479. spin_unlock(&last_ptr->refill_lock);
  5480. goto unclustered_alloc;
  5481. }
  5482. /*
  5483. * this cluster didn't work out, free it and
  5484. * start over
  5485. */
  5486. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5487. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5488. spin_unlock(&last_ptr->refill_lock);
  5489. goto unclustered_alloc;
  5490. }
  5491. aligned_cluster = max_t(unsigned long,
  5492. empty_cluster + empty_size,
  5493. block_group->full_stripe_len);
  5494. /* allocate a cluster in this block group */
  5495. ret = btrfs_find_space_cluster(trans, root,
  5496. block_group, last_ptr,
  5497. search_start, num_bytes,
  5498. aligned_cluster);
  5499. if (ret == 0) {
  5500. /*
  5501. * now pull our allocation out of this
  5502. * cluster
  5503. */
  5504. offset = btrfs_alloc_from_cluster(block_group,
  5505. last_ptr, num_bytes,
  5506. search_start);
  5507. if (offset) {
  5508. /* we found one, proceed */
  5509. spin_unlock(&last_ptr->refill_lock);
  5510. trace_btrfs_reserve_extent_cluster(root,
  5511. block_group, search_start,
  5512. num_bytes);
  5513. goto checks;
  5514. }
  5515. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5516. && !failed_cluster_refill) {
  5517. spin_unlock(&last_ptr->refill_lock);
  5518. failed_cluster_refill = true;
  5519. wait_block_group_cache_progress(block_group,
  5520. num_bytes + empty_cluster + empty_size);
  5521. goto have_block_group;
  5522. }
  5523. /*
  5524. * at this point we either didn't find a cluster
  5525. * or we weren't able to allocate a block from our
  5526. * cluster. Free the cluster we've been trying
  5527. * to use, and go to the next block group
  5528. */
  5529. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5530. spin_unlock(&last_ptr->refill_lock);
  5531. goto loop;
  5532. }
  5533. unclustered_alloc:
  5534. spin_lock(&block_group->free_space_ctl->tree_lock);
  5535. if (cached &&
  5536. block_group->free_space_ctl->free_space <
  5537. num_bytes + empty_cluster + empty_size) {
  5538. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5539. goto loop;
  5540. }
  5541. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5542. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5543. num_bytes, empty_size);
  5544. /*
  5545. * If we didn't find a chunk, and we haven't failed on this
  5546. * block group before, and this block group is in the middle of
  5547. * caching and we are ok with waiting, then go ahead and wait
  5548. * for progress to be made, and set failed_alloc to true.
  5549. *
  5550. * If failed_alloc is true then we've already waited on this
  5551. * block group once and should move on to the next block group.
  5552. */
  5553. if (!offset && !failed_alloc && !cached &&
  5554. loop > LOOP_CACHING_NOWAIT) {
  5555. wait_block_group_cache_progress(block_group,
  5556. num_bytes + empty_size);
  5557. failed_alloc = true;
  5558. goto have_block_group;
  5559. } else if (!offset) {
  5560. if (!cached)
  5561. have_caching_bg = true;
  5562. goto loop;
  5563. }
  5564. checks:
  5565. search_start = stripe_align(root, used_block_group,
  5566. offset, num_bytes);
  5567. /* move on to the next group */
  5568. if (search_start + num_bytes >
  5569. used_block_group->key.objectid + used_block_group->key.offset) {
  5570. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5571. goto loop;
  5572. }
  5573. if (offset < search_start)
  5574. btrfs_add_free_space(used_block_group, offset,
  5575. search_start - offset);
  5576. BUG_ON(offset > search_start);
  5577. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5578. alloc_type);
  5579. if (ret == -EAGAIN) {
  5580. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5581. goto loop;
  5582. }
  5583. /* we are all good, lets return */
  5584. ins->objectid = search_start;
  5585. ins->offset = num_bytes;
  5586. trace_btrfs_reserve_extent(orig_root, block_group,
  5587. search_start, num_bytes);
  5588. if (used_block_group != block_group)
  5589. btrfs_put_block_group(used_block_group);
  5590. btrfs_put_block_group(block_group);
  5591. break;
  5592. loop:
  5593. failed_cluster_refill = false;
  5594. failed_alloc = false;
  5595. BUG_ON(index != get_block_group_index(block_group));
  5596. if (used_block_group != block_group)
  5597. btrfs_put_block_group(used_block_group);
  5598. btrfs_put_block_group(block_group);
  5599. }
  5600. up_read(&space_info->groups_sem);
  5601. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5602. goto search;
  5603. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5604. goto search;
  5605. /*
  5606. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5607. * caching kthreads as we move along
  5608. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5609. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5610. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5611. * again
  5612. */
  5613. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5614. index = 0;
  5615. loop++;
  5616. if (loop == LOOP_ALLOC_CHUNK) {
  5617. ret = do_chunk_alloc(trans, root, flags,
  5618. CHUNK_ALLOC_FORCE);
  5619. /*
  5620. * Do not bail out on ENOSPC since we
  5621. * can do more things.
  5622. */
  5623. if (ret < 0 && ret != -ENOSPC) {
  5624. btrfs_abort_transaction(trans,
  5625. root, ret);
  5626. goto out;
  5627. }
  5628. }
  5629. if (loop == LOOP_NO_EMPTY_SIZE) {
  5630. empty_size = 0;
  5631. empty_cluster = 0;
  5632. }
  5633. goto search;
  5634. } else if (!ins->objectid) {
  5635. ret = -ENOSPC;
  5636. } else if (ins->objectid) {
  5637. ret = 0;
  5638. }
  5639. out:
  5640. return ret;
  5641. }
  5642. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5643. int dump_block_groups)
  5644. {
  5645. struct btrfs_block_group_cache *cache;
  5646. int index = 0;
  5647. spin_lock(&info->lock);
  5648. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5649. (unsigned long long)info->flags,
  5650. (unsigned long long)(info->total_bytes - info->bytes_used -
  5651. info->bytes_pinned - info->bytes_reserved -
  5652. info->bytes_readonly),
  5653. (info->full) ? "" : "not ");
  5654. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5655. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5656. (unsigned long long)info->total_bytes,
  5657. (unsigned long long)info->bytes_used,
  5658. (unsigned long long)info->bytes_pinned,
  5659. (unsigned long long)info->bytes_reserved,
  5660. (unsigned long long)info->bytes_may_use,
  5661. (unsigned long long)info->bytes_readonly);
  5662. spin_unlock(&info->lock);
  5663. if (!dump_block_groups)
  5664. return;
  5665. down_read(&info->groups_sem);
  5666. again:
  5667. list_for_each_entry(cache, &info->block_groups[index], list) {
  5668. spin_lock(&cache->lock);
  5669. printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
  5670. (unsigned long long)cache->key.objectid,
  5671. (unsigned long long)cache->key.offset,
  5672. (unsigned long long)btrfs_block_group_used(&cache->item),
  5673. (unsigned long long)cache->pinned,
  5674. (unsigned long long)cache->reserved,
  5675. cache->ro ? "[readonly]" : "");
  5676. btrfs_dump_free_space(cache, bytes);
  5677. spin_unlock(&cache->lock);
  5678. }
  5679. if (++index < BTRFS_NR_RAID_TYPES)
  5680. goto again;
  5681. up_read(&info->groups_sem);
  5682. }
  5683. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  5684. struct btrfs_root *root,
  5685. u64 num_bytes, u64 min_alloc_size,
  5686. u64 empty_size, u64 hint_byte,
  5687. struct btrfs_key *ins, int is_data)
  5688. {
  5689. bool final_tried = false;
  5690. u64 flags;
  5691. int ret;
  5692. flags = btrfs_get_alloc_profile(root, is_data);
  5693. again:
  5694. WARN_ON(num_bytes < root->sectorsize);
  5695. ret = find_free_extent(trans, root, num_bytes, empty_size,
  5696. hint_byte, ins, flags);
  5697. if (ret == -ENOSPC) {
  5698. if (!final_tried) {
  5699. num_bytes = num_bytes >> 1;
  5700. num_bytes = round_down(num_bytes, root->sectorsize);
  5701. num_bytes = max(num_bytes, min_alloc_size);
  5702. if (num_bytes == min_alloc_size)
  5703. final_tried = true;
  5704. goto again;
  5705. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5706. struct btrfs_space_info *sinfo;
  5707. sinfo = __find_space_info(root->fs_info, flags);
  5708. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  5709. (unsigned long long)flags,
  5710. (unsigned long long)num_bytes);
  5711. if (sinfo)
  5712. dump_space_info(sinfo, num_bytes, 1);
  5713. }
  5714. }
  5715. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5716. return ret;
  5717. }
  5718. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5719. u64 start, u64 len, int pin)
  5720. {
  5721. struct btrfs_block_group_cache *cache;
  5722. int ret = 0;
  5723. cache = btrfs_lookup_block_group(root->fs_info, start);
  5724. if (!cache) {
  5725. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  5726. (unsigned long long)start);
  5727. return -ENOSPC;
  5728. }
  5729. if (btrfs_test_opt(root, DISCARD))
  5730. ret = btrfs_discard_extent(root, start, len, NULL);
  5731. if (pin)
  5732. pin_down_extent(root, cache, start, len, 1);
  5733. else {
  5734. btrfs_add_free_space(cache, start, len);
  5735. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5736. }
  5737. btrfs_put_block_group(cache);
  5738. trace_btrfs_reserved_extent_free(root, start, len);
  5739. return ret;
  5740. }
  5741. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5742. u64 start, u64 len)
  5743. {
  5744. return __btrfs_free_reserved_extent(root, start, len, 0);
  5745. }
  5746. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5747. u64 start, u64 len)
  5748. {
  5749. return __btrfs_free_reserved_extent(root, start, len, 1);
  5750. }
  5751. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5752. struct btrfs_root *root,
  5753. u64 parent, u64 root_objectid,
  5754. u64 flags, u64 owner, u64 offset,
  5755. struct btrfs_key *ins, int ref_mod)
  5756. {
  5757. int ret;
  5758. struct btrfs_fs_info *fs_info = root->fs_info;
  5759. struct btrfs_extent_item *extent_item;
  5760. struct btrfs_extent_inline_ref *iref;
  5761. struct btrfs_path *path;
  5762. struct extent_buffer *leaf;
  5763. int type;
  5764. u32 size;
  5765. if (parent > 0)
  5766. type = BTRFS_SHARED_DATA_REF_KEY;
  5767. else
  5768. type = BTRFS_EXTENT_DATA_REF_KEY;
  5769. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5770. path = btrfs_alloc_path();
  5771. if (!path)
  5772. return -ENOMEM;
  5773. path->leave_spinning = 1;
  5774. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5775. ins, size);
  5776. if (ret) {
  5777. btrfs_free_path(path);
  5778. return ret;
  5779. }
  5780. leaf = path->nodes[0];
  5781. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5782. struct btrfs_extent_item);
  5783. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5784. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5785. btrfs_set_extent_flags(leaf, extent_item,
  5786. flags | BTRFS_EXTENT_FLAG_DATA);
  5787. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5788. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5789. if (parent > 0) {
  5790. struct btrfs_shared_data_ref *ref;
  5791. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5792. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5793. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5794. } else {
  5795. struct btrfs_extent_data_ref *ref;
  5796. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5797. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5798. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5799. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5800. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5801. }
  5802. btrfs_mark_buffer_dirty(path->nodes[0]);
  5803. btrfs_free_path(path);
  5804. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5805. if (ret) { /* -ENOENT, logic error */
  5806. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5807. (unsigned long long)ins->objectid,
  5808. (unsigned long long)ins->offset);
  5809. BUG();
  5810. }
  5811. return ret;
  5812. }
  5813. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5814. struct btrfs_root *root,
  5815. u64 parent, u64 root_objectid,
  5816. u64 flags, struct btrfs_disk_key *key,
  5817. int level, struct btrfs_key *ins)
  5818. {
  5819. int ret;
  5820. struct btrfs_fs_info *fs_info = root->fs_info;
  5821. struct btrfs_extent_item *extent_item;
  5822. struct btrfs_tree_block_info *block_info;
  5823. struct btrfs_extent_inline_ref *iref;
  5824. struct btrfs_path *path;
  5825. struct extent_buffer *leaf;
  5826. u32 size = sizeof(*extent_item) + sizeof(*iref);
  5827. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5828. SKINNY_METADATA);
  5829. if (!skinny_metadata)
  5830. size += sizeof(*block_info);
  5831. path = btrfs_alloc_path();
  5832. if (!path)
  5833. return -ENOMEM;
  5834. path->leave_spinning = 1;
  5835. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5836. ins, size);
  5837. if (ret) {
  5838. btrfs_free_path(path);
  5839. return ret;
  5840. }
  5841. leaf = path->nodes[0];
  5842. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5843. struct btrfs_extent_item);
  5844. btrfs_set_extent_refs(leaf, extent_item, 1);
  5845. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5846. btrfs_set_extent_flags(leaf, extent_item,
  5847. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5848. if (skinny_metadata) {
  5849. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5850. } else {
  5851. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5852. btrfs_set_tree_block_key(leaf, block_info, key);
  5853. btrfs_set_tree_block_level(leaf, block_info, level);
  5854. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5855. }
  5856. if (parent > 0) {
  5857. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5858. btrfs_set_extent_inline_ref_type(leaf, iref,
  5859. BTRFS_SHARED_BLOCK_REF_KEY);
  5860. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5861. } else {
  5862. btrfs_set_extent_inline_ref_type(leaf, iref,
  5863. BTRFS_TREE_BLOCK_REF_KEY);
  5864. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5865. }
  5866. btrfs_mark_buffer_dirty(leaf);
  5867. btrfs_free_path(path);
  5868. ret = update_block_group(root, ins->objectid, root->leafsize, 1);
  5869. if (ret) { /* -ENOENT, logic error */
  5870. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5871. (unsigned long long)ins->objectid,
  5872. (unsigned long long)ins->offset);
  5873. BUG();
  5874. }
  5875. return ret;
  5876. }
  5877. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5878. struct btrfs_root *root,
  5879. u64 root_objectid, u64 owner,
  5880. u64 offset, struct btrfs_key *ins)
  5881. {
  5882. int ret;
  5883. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5884. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5885. ins->offset, 0,
  5886. root_objectid, owner, offset,
  5887. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5888. return ret;
  5889. }
  5890. /*
  5891. * this is used by the tree logging recovery code. It records that
  5892. * an extent has been allocated and makes sure to clear the free
  5893. * space cache bits as well
  5894. */
  5895. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  5896. struct btrfs_root *root,
  5897. u64 root_objectid, u64 owner, u64 offset,
  5898. struct btrfs_key *ins)
  5899. {
  5900. int ret;
  5901. struct btrfs_block_group_cache *block_group;
  5902. /*
  5903. * Mixed block groups will exclude before processing the log so we only
  5904. * need to do the exlude dance if this fs isn't mixed.
  5905. */
  5906. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  5907. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  5908. if (ret)
  5909. return ret;
  5910. }
  5911. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  5912. if (!block_group)
  5913. return -EINVAL;
  5914. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  5915. RESERVE_ALLOC_NO_ACCOUNT);
  5916. BUG_ON(ret); /* logic error */
  5917. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  5918. 0, owner, offset, ins, 1);
  5919. btrfs_put_block_group(block_group);
  5920. return ret;
  5921. }
  5922. static struct extent_buffer *
  5923. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5924. u64 bytenr, u32 blocksize, int level)
  5925. {
  5926. struct extent_buffer *buf;
  5927. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5928. if (!buf)
  5929. return ERR_PTR(-ENOMEM);
  5930. btrfs_set_header_generation(buf, trans->transid);
  5931. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  5932. btrfs_tree_lock(buf);
  5933. clean_tree_block(trans, root, buf);
  5934. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  5935. btrfs_set_lock_blocking(buf);
  5936. btrfs_set_buffer_uptodate(buf);
  5937. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  5938. /*
  5939. * we allow two log transactions at a time, use different
  5940. * EXENT bit to differentiate dirty pages.
  5941. */
  5942. if (root->log_transid % 2 == 0)
  5943. set_extent_dirty(&root->dirty_log_pages, buf->start,
  5944. buf->start + buf->len - 1, GFP_NOFS);
  5945. else
  5946. set_extent_new(&root->dirty_log_pages, buf->start,
  5947. buf->start + buf->len - 1, GFP_NOFS);
  5948. } else {
  5949. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  5950. buf->start + buf->len - 1, GFP_NOFS);
  5951. }
  5952. trans->blocks_used++;
  5953. /* this returns a buffer locked for blocking */
  5954. return buf;
  5955. }
  5956. static struct btrfs_block_rsv *
  5957. use_block_rsv(struct btrfs_trans_handle *trans,
  5958. struct btrfs_root *root, u32 blocksize)
  5959. {
  5960. struct btrfs_block_rsv *block_rsv;
  5961. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  5962. int ret;
  5963. bool global_updated = false;
  5964. block_rsv = get_block_rsv(trans, root);
  5965. if (unlikely(block_rsv->size == 0))
  5966. goto try_reserve;
  5967. again:
  5968. ret = block_rsv_use_bytes(block_rsv, blocksize);
  5969. if (!ret)
  5970. return block_rsv;
  5971. if (block_rsv->failfast)
  5972. return ERR_PTR(ret);
  5973. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  5974. global_updated = true;
  5975. update_global_block_rsv(root->fs_info);
  5976. goto again;
  5977. }
  5978. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5979. static DEFINE_RATELIMIT_STATE(_rs,
  5980. DEFAULT_RATELIMIT_INTERVAL * 10,
  5981. /*DEFAULT_RATELIMIT_BURST*/ 1);
  5982. if (__ratelimit(&_rs))
  5983. WARN(1, KERN_DEBUG
  5984. "btrfs: block rsv returned %d\n", ret);
  5985. }
  5986. try_reserve:
  5987. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  5988. BTRFS_RESERVE_NO_FLUSH);
  5989. if (!ret)
  5990. return block_rsv;
  5991. /*
  5992. * If we couldn't reserve metadata bytes try and use some from
  5993. * the global reserve if its space type is the same as the global
  5994. * reservation.
  5995. */
  5996. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  5997. block_rsv->space_info == global_rsv->space_info) {
  5998. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5999. if (!ret)
  6000. return global_rsv;
  6001. }
  6002. return ERR_PTR(ret);
  6003. }
  6004. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6005. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6006. {
  6007. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6008. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6009. }
  6010. /*
  6011. * finds a free extent and does all the dirty work required for allocation
  6012. * returns the key for the extent through ins, and a tree buffer for
  6013. * the first block of the extent through buf.
  6014. *
  6015. * returns the tree buffer or NULL.
  6016. */
  6017. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  6018. struct btrfs_root *root, u32 blocksize,
  6019. u64 parent, u64 root_objectid,
  6020. struct btrfs_disk_key *key, int level,
  6021. u64 hint, u64 empty_size)
  6022. {
  6023. struct btrfs_key ins;
  6024. struct btrfs_block_rsv *block_rsv;
  6025. struct extent_buffer *buf;
  6026. u64 flags = 0;
  6027. int ret;
  6028. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6029. SKINNY_METADATA);
  6030. block_rsv = use_block_rsv(trans, root, blocksize);
  6031. if (IS_ERR(block_rsv))
  6032. return ERR_CAST(block_rsv);
  6033. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  6034. empty_size, hint, &ins, 0);
  6035. if (ret) {
  6036. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6037. return ERR_PTR(ret);
  6038. }
  6039. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  6040. blocksize, level);
  6041. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  6042. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6043. if (parent == 0)
  6044. parent = ins.objectid;
  6045. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6046. } else
  6047. BUG_ON(parent > 0);
  6048. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6049. struct btrfs_delayed_extent_op *extent_op;
  6050. extent_op = btrfs_alloc_delayed_extent_op();
  6051. BUG_ON(!extent_op); /* -ENOMEM */
  6052. if (key)
  6053. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6054. else
  6055. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6056. extent_op->flags_to_set = flags;
  6057. if (skinny_metadata)
  6058. extent_op->update_key = 0;
  6059. else
  6060. extent_op->update_key = 1;
  6061. extent_op->update_flags = 1;
  6062. extent_op->is_data = 0;
  6063. extent_op->level = level;
  6064. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6065. ins.objectid,
  6066. ins.offset, parent, root_objectid,
  6067. level, BTRFS_ADD_DELAYED_EXTENT,
  6068. extent_op, 0);
  6069. BUG_ON(ret); /* -ENOMEM */
  6070. }
  6071. return buf;
  6072. }
  6073. struct walk_control {
  6074. u64 refs[BTRFS_MAX_LEVEL];
  6075. u64 flags[BTRFS_MAX_LEVEL];
  6076. struct btrfs_key update_progress;
  6077. int stage;
  6078. int level;
  6079. int shared_level;
  6080. int update_ref;
  6081. int keep_locks;
  6082. int reada_slot;
  6083. int reada_count;
  6084. int for_reloc;
  6085. };
  6086. #define DROP_REFERENCE 1
  6087. #define UPDATE_BACKREF 2
  6088. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6089. struct btrfs_root *root,
  6090. struct walk_control *wc,
  6091. struct btrfs_path *path)
  6092. {
  6093. u64 bytenr;
  6094. u64 generation;
  6095. u64 refs;
  6096. u64 flags;
  6097. u32 nritems;
  6098. u32 blocksize;
  6099. struct btrfs_key key;
  6100. struct extent_buffer *eb;
  6101. int ret;
  6102. int slot;
  6103. int nread = 0;
  6104. if (path->slots[wc->level] < wc->reada_slot) {
  6105. wc->reada_count = wc->reada_count * 2 / 3;
  6106. wc->reada_count = max(wc->reada_count, 2);
  6107. } else {
  6108. wc->reada_count = wc->reada_count * 3 / 2;
  6109. wc->reada_count = min_t(int, wc->reada_count,
  6110. BTRFS_NODEPTRS_PER_BLOCK(root));
  6111. }
  6112. eb = path->nodes[wc->level];
  6113. nritems = btrfs_header_nritems(eb);
  6114. blocksize = btrfs_level_size(root, wc->level - 1);
  6115. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6116. if (nread >= wc->reada_count)
  6117. break;
  6118. cond_resched();
  6119. bytenr = btrfs_node_blockptr(eb, slot);
  6120. generation = btrfs_node_ptr_generation(eb, slot);
  6121. if (slot == path->slots[wc->level])
  6122. goto reada;
  6123. if (wc->stage == UPDATE_BACKREF &&
  6124. generation <= root->root_key.offset)
  6125. continue;
  6126. /* We don't lock the tree block, it's OK to be racy here */
  6127. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6128. wc->level - 1, 1, &refs,
  6129. &flags);
  6130. /* We don't care about errors in readahead. */
  6131. if (ret < 0)
  6132. continue;
  6133. BUG_ON(refs == 0);
  6134. if (wc->stage == DROP_REFERENCE) {
  6135. if (refs == 1)
  6136. goto reada;
  6137. if (wc->level == 1 &&
  6138. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6139. continue;
  6140. if (!wc->update_ref ||
  6141. generation <= root->root_key.offset)
  6142. continue;
  6143. btrfs_node_key_to_cpu(eb, &key, slot);
  6144. ret = btrfs_comp_cpu_keys(&key,
  6145. &wc->update_progress);
  6146. if (ret < 0)
  6147. continue;
  6148. } else {
  6149. if (wc->level == 1 &&
  6150. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6151. continue;
  6152. }
  6153. reada:
  6154. ret = readahead_tree_block(root, bytenr, blocksize,
  6155. generation);
  6156. if (ret)
  6157. break;
  6158. nread++;
  6159. }
  6160. wc->reada_slot = slot;
  6161. }
  6162. /*
  6163. * helper to process tree block while walking down the tree.
  6164. *
  6165. * when wc->stage == UPDATE_BACKREF, this function updates
  6166. * back refs for pointers in the block.
  6167. *
  6168. * NOTE: return value 1 means we should stop walking down.
  6169. */
  6170. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  6171. struct btrfs_root *root,
  6172. struct btrfs_path *path,
  6173. struct walk_control *wc, int lookup_info)
  6174. {
  6175. int level = wc->level;
  6176. struct extent_buffer *eb = path->nodes[level];
  6177. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6178. int ret;
  6179. if (wc->stage == UPDATE_BACKREF &&
  6180. btrfs_header_owner(eb) != root->root_key.objectid)
  6181. return 1;
  6182. /*
  6183. * when reference count of tree block is 1, it won't increase
  6184. * again. once full backref flag is set, we never clear it.
  6185. */
  6186. if (lookup_info &&
  6187. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  6188. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  6189. BUG_ON(!path->locks[level]);
  6190. ret = btrfs_lookup_extent_info(trans, root,
  6191. eb->start, level, 1,
  6192. &wc->refs[level],
  6193. &wc->flags[level]);
  6194. BUG_ON(ret == -ENOMEM);
  6195. if (ret)
  6196. return ret;
  6197. BUG_ON(wc->refs[level] == 0);
  6198. }
  6199. if (wc->stage == DROP_REFERENCE) {
  6200. if (wc->refs[level] > 1)
  6201. return 1;
  6202. if (path->locks[level] && !wc->keep_locks) {
  6203. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6204. path->locks[level] = 0;
  6205. }
  6206. return 0;
  6207. }
  6208. /* wc->stage == UPDATE_BACKREF */
  6209. if (!(wc->flags[level] & flag)) {
  6210. BUG_ON(!path->locks[level]);
  6211. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  6212. BUG_ON(ret); /* -ENOMEM */
  6213. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  6214. BUG_ON(ret); /* -ENOMEM */
  6215. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  6216. eb->len, flag,
  6217. btrfs_header_level(eb), 0);
  6218. BUG_ON(ret); /* -ENOMEM */
  6219. wc->flags[level] |= flag;
  6220. }
  6221. /*
  6222. * the block is shared by multiple trees, so it's not good to
  6223. * keep the tree lock
  6224. */
  6225. if (path->locks[level] && level > 0) {
  6226. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6227. path->locks[level] = 0;
  6228. }
  6229. return 0;
  6230. }
  6231. /*
  6232. * helper to process tree block pointer.
  6233. *
  6234. * when wc->stage == DROP_REFERENCE, this function checks
  6235. * reference count of the block pointed to. if the block
  6236. * is shared and we need update back refs for the subtree
  6237. * rooted at the block, this function changes wc->stage to
  6238. * UPDATE_BACKREF. if the block is shared and there is no
  6239. * need to update back, this function drops the reference
  6240. * to the block.
  6241. *
  6242. * NOTE: return value 1 means we should stop walking down.
  6243. */
  6244. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  6245. struct btrfs_root *root,
  6246. struct btrfs_path *path,
  6247. struct walk_control *wc, int *lookup_info)
  6248. {
  6249. u64 bytenr;
  6250. u64 generation;
  6251. u64 parent;
  6252. u32 blocksize;
  6253. struct btrfs_key key;
  6254. struct extent_buffer *next;
  6255. int level = wc->level;
  6256. int reada = 0;
  6257. int ret = 0;
  6258. generation = btrfs_node_ptr_generation(path->nodes[level],
  6259. path->slots[level]);
  6260. /*
  6261. * if the lower level block was created before the snapshot
  6262. * was created, we know there is no need to update back refs
  6263. * for the subtree
  6264. */
  6265. if (wc->stage == UPDATE_BACKREF &&
  6266. generation <= root->root_key.offset) {
  6267. *lookup_info = 1;
  6268. return 1;
  6269. }
  6270. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  6271. blocksize = btrfs_level_size(root, level - 1);
  6272. next = btrfs_find_tree_block(root, bytenr, blocksize);
  6273. if (!next) {
  6274. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6275. if (!next)
  6276. return -ENOMEM;
  6277. reada = 1;
  6278. }
  6279. btrfs_tree_lock(next);
  6280. btrfs_set_lock_blocking(next);
  6281. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  6282. &wc->refs[level - 1],
  6283. &wc->flags[level - 1]);
  6284. if (ret < 0) {
  6285. btrfs_tree_unlock(next);
  6286. return ret;
  6287. }
  6288. if (unlikely(wc->refs[level - 1] == 0)) {
  6289. btrfs_err(root->fs_info, "Missing references.");
  6290. BUG();
  6291. }
  6292. *lookup_info = 0;
  6293. if (wc->stage == DROP_REFERENCE) {
  6294. if (wc->refs[level - 1] > 1) {
  6295. if (level == 1 &&
  6296. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6297. goto skip;
  6298. if (!wc->update_ref ||
  6299. generation <= root->root_key.offset)
  6300. goto skip;
  6301. btrfs_node_key_to_cpu(path->nodes[level], &key,
  6302. path->slots[level]);
  6303. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  6304. if (ret < 0)
  6305. goto skip;
  6306. wc->stage = UPDATE_BACKREF;
  6307. wc->shared_level = level - 1;
  6308. }
  6309. } else {
  6310. if (level == 1 &&
  6311. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6312. goto skip;
  6313. }
  6314. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  6315. btrfs_tree_unlock(next);
  6316. free_extent_buffer(next);
  6317. next = NULL;
  6318. *lookup_info = 1;
  6319. }
  6320. if (!next) {
  6321. if (reada && level == 1)
  6322. reada_walk_down(trans, root, wc, path);
  6323. next = read_tree_block(root, bytenr, blocksize, generation);
  6324. if (!next || !extent_buffer_uptodate(next)) {
  6325. free_extent_buffer(next);
  6326. return -EIO;
  6327. }
  6328. btrfs_tree_lock(next);
  6329. btrfs_set_lock_blocking(next);
  6330. }
  6331. level--;
  6332. BUG_ON(level != btrfs_header_level(next));
  6333. path->nodes[level] = next;
  6334. path->slots[level] = 0;
  6335. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6336. wc->level = level;
  6337. if (wc->level == 1)
  6338. wc->reada_slot = 0;
  6339. return 0;
  6340. skip:
  6341. wc->refs[level - 1] = 0;
  6342. wc->flags[level - 1] = 0;
  6343. if (wc->stage == DROP_REFERENCE) {
  6344. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  6345. parent = path->nodes[level]->start;
  6346. } else {
  6347. BUG_ON(root->root_key.objectid !=
  6348. btrfs_header_owner(path->nodes[level]));
  6349. parent = 0;
  6350. }
  6351. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  6352. root->root_key.objectid, level - 1, 0, 0);
  6353. BUG_ON(ret); /* -ENOMEM */
  6354. }
  6355. btrfs_tree_unlock(next);
  6356. free_extent_buffer(next);
  6357. *lookup_info = 1;
  6358. return 1;
  6359. }
  6360. /*
  6361. * helper to process tree block while walking up the tree.
  6362. *
  6363. * when wc->stage == DROP_REFERENCE, this function drops
  6364. * reference count on the block.
  6365. *
  6366. * when wc->stage == UPDATE_BACKREF, this function changes
  6367. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  6368. * to UPDATE_BACKREF previously while processing the block.
  6369. *
  6370. * NOTE: return value 1 means we should stop walking up.
  6371. */
  6372. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  6373. struct btrfs_root *root,
  6374. struct btrfs_path *path,
  6375. struct walk_control *wc)
  6376. {
  6377. int ret;
  6378. int level = wc->level;
  6379. struct extent_buffer *eb = path->nodes[level];
  6380. u64 parent = 0;
  6381. if (wc->stage == UPDATE_BACKREF) {
  6382. BUG_ON(wc->shared_level < level);
  6383. if (level < wc->shared_level)
  6384. goto out;
  6385. ret = find_next_key(path, level + 1, &wc->update_progress);
  6386. if (ret > 0)
  6387. wc->update_ref = 0;
  6388. wc->stage = DROP_REFERENCE;
  6389. wc->shared_level = -1;
  6390. path->slots[level] = 0;
  6391. /*
  6392. * check reference count again if the block isn't locked.
  6393. * we should start walking down the tree again if reference
  6394. * count is one.
  6395. */
  6396. if (!path->locks[level]) {
  6397. BUG_ON(level == 0);
  6398. btrfs_tree_lock(eb);
  6399. btrfs_set_lock_blocking(eb);
  6400. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6401. ret = btrfs_lookup_extent_info(trans, root,
  6402. eb->start, level, 1,
  6403. &wc->refs[level],
  6404. &wc->flags[level]);
  6405. if (ret < 0) {
  6406. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6407. path->locks[level] = 0;
  6408. return ret;
  6409. }
  6410. BUG_ON(wc->refs[level] == 0);
  6411. if (wc->refs[level] == 1) {
  6412. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6413. path->locks[level] = 0;
  6414. return 1;
  6415. }
  6416. }
  6417. }
  6418. /* wc->stage == DROP_REFERENCE */
  6419. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6420. if (wc->refs[level] == 1) {
  6421. if (level == 0) {
  6422. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6423. ret = btrfs_dec_ref(trans, root, eb, 1,
  6424. wc->for_reloc);
  6425. else
  6426. ret = btrfs_dec_ref(trans, root, eb, 0,
  6427. wc->for_reloc);
  6428. BUG_ON(ret); /* -ENOMEM */
  6429. }
  6430. /* make block locked assertion in clean_tree_block happy */
  6431. if (!path->locks[level] &&
  6432. btrfs_header_generation(eb) == trans->transid) {
  6433. btrfs_tree_lock(eb);
  6434. btrfs_set_lock_blocking(eb);
  6435. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6436. }
  6437. clean_tree_block(trans, root, eb);
  6438. }
  6439. if (eb == root->node) {
  6440. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6441. parent = eb->start;
  6442. else
  6443. BUG_ON(root->root_key.objectid !=
  6444. btrfs_header_owner(eb));
  6445. } else {
  6446. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6447. parent = path->nodes[level + 1]->start;
  6448. else
  6449. BUG_ON(root->root_key.objectid !=
  6450. btrfs_header_owner(path->nodes[level + 1]));
  6451. }
  6452. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6453. out:
  6454. wc->refs[level] = 0;
  6455. wc->flags[level] = 0;
  6456. return 0;
  6457. }
  6458. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6459. struct btrfs_root *root,
  6460. struct btrfs_path *path,
  6461. struct walk_control *wc)
  6462. {
  6463. int level = wc->level;
  6464. int lookup_info = 1;
  6465. int ret;
  6466. while (level >= 0) {
  6467. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6468. if (ret > 0)
  6469. break;
  6470. if (level == 0)
  6471. break;
  6472. if (path->slots[level] >=
  6473. btrfs_header_nritems(path->nodes[level]))
  6474. break;
  6475. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6476. if (ret > 0) {
  6477. path->slots[level]++;
  6478. continue;
  6479. } else if (ret < 0)
  6480. return ret;
  6481. level = wc->level;
  6482. }
  6483. return 0;
  6484. }
  6485. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6486. struct btrfs_root *root,
  6487. struct btrfs_path *path,
  6488. struct walk_control *wc, int max_level)
  6489. {
  6490. int level = wc->level;
  6491. int ret;
  6492. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6493. while (level < max_level && path->nodes[level]) {
  6494. wc->level = level;
  6495. if (path->slots[level] + 1 <
  6496. btrfs_header_nritems(path->nodes[level])) {
  6497. path->slots[level]++;
  6498. return 0;
  6499. } else {
  6500. ret = walk_up_proc(trans, root, path, wc);
  6501. if (ret > 0)
  6502. return 0;
  6503. if (path->locks[level]) {
  6504. btrfs_tree_unlock_rw(path->nodes[level],
  6505. path->locks[level]);
  6506. path->locks[level] = 0;
  6507. }
  6508. free_extent_buffer(path->nodes[level]);
  6509. path->nodes[level] = NULL;
  6510. level++;
  6511. }
  6512. }
  6513. return 1;
  6514. }
  6515. /*
  6516. * drop a subvolume tree.
  6517. *
  6518. * this function traverses the tree freeing any blocks that only
  6519. * referenced by the tree.
  6520. *
  6521. * when a shared tree block is found. this function decreases its
  6522. * reference count by one. if update_ref is true, this function
  6523. * also make sure backrefs for the shared block and all lower level
  6524. * blocks are properly updated.
  6525. *
  6526. * If called with for_reloc == 0, may exit early with -EAGAIN
  6527. */
  6528. int btrfs_drop_snapshot(struct btrfs_root *root,
  6529. struct btrfs_block_rsv *block_rsv, int update_ref,
  6530. int for_reloc)
  6531. {
  6532. struct btrfs_path *path;
  6533. struct btrfs_trans_handle *trans;
  6534. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6535. struct btrfs_root_item *root_item = &root->root_item;
  6536. struct walk_control *wc;
  6537. struct btrfs_key key;
  6538. int err = 0;
  6539. int ret;
  6540. int level;
  6541. path = btrfs_alloc_path();
  6542. if (!path) {
  6543. err = -ENOMEM;
  6544. goto out;
  6545. }
  6546. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6547. if (!wc) {
  6548. btrfs_free_path(path);
  6549. err = -ENOMEM;
  6550. goto out;
  6551. }
  6552. trans = btrfs_start_transaction(tree_root, 0);
  6553. if (IS_ERR(trans)) {
  6554. err = PTR_ERR(trans);
  6555. goto out_free;
  6556. }
  6557. if (block_rsv)
  6558. trans->block_rsv = block_rsv;
  6559. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6560. level = btrfs_header_level(root->node);
  6561. path->nodes[level] = btrfs_lock_root_node(root);
  6562. btrfs_set_lock_blocking(path->nodes[level]);
  6563. path->slots[level] = 0;
  6564. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6565. memset(&wc->update_progress, 0,
  6566. sizeof(wc->update_progress));
  6567. } else {
  6568. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6569. memcpy(&wc->update_progress, &key,
  6570. sizeof(wc->update_progress));
  6571. level = root_item->drop_level;
  6572. BUG_ON(level == 0);
  6573. path->lowest_level = level;
  6574. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6575. path->lowest_level = 0;
  6576. if (ret < 0) {
  6577. err = ret;
  6578. goto out_end_trans;
  6579. }
  6580. WARN_ON(ret > 0);
  6581. /*
  6582. * unlock our path, this is safe because only this
  6583. * function is allowed to delete this snapshot
  6584. */
  6585. btrfs_unlock_up_safe(path, 0);
  6586. level = btrfs_header_level(root->node);
  6587. while (1) {
  6588. btrfs_tree_lock(path->nodes[level]);
  6589. btrfs_set_lock_blocking(path->nodes[level]);
  6590. ret = btrfs_lookup_extent_info(trans, root,
  6591. path->nodes[level]->start,
  6592. level, 1, &wc->refs[level],
  6593. &wc->flags[level]);
  6594. if (ret < 0) {
  6595. err = ret;
  6596. goto out_end_trans;
  6597. }
  6598. BUG_ON(wc->refs[level] == 0);
  6599. if (level == root_item->drop_level)
  6600. break;
  6601. btrfs_tree_unlock(path->nodes[level]);
  6602. WARN_ON(wc->refs[level] != 1);
  6603. level--;
  6604. }
  6605. }
  6606. wc->level = level;
  6607. wc->shared_level = -1;
  6608. wc->stage = DROP_REFERENCE;
  6609. wc->update_ref = update_ref;
  6610. wc->keep_locks = 0;
  6611. wc->for_reloc = for_reloc;
  6612. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6613. while (1) {
  6614. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  6615. pr_debug("btrfs: drop snapshot early exit\n");
  6616. err = -EAGAIN;
  6617. goto out_end_trans;
  6618. }
  6619. ret = walk_down_tree(trans, root, path, wc);
  6620. if (ret < 0) {
  6621. err = ret;
  6622. break;
  6623. }
  6624. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6625. if (ret < 0) {
  6626. err = ret;
  6627. break;
  6628. }
  6629. if (ret > 0) {
  6630. BUG_ON(wc->stage != DROP_REFERENCE);
  6631. break;
  6632. }
  6633. if (wc->stage == DROP_REFERENCE) {
  6634. level = wc->level;
  6635. btrfs_node_key(path->nodes[level],
  6636. &root_item->drop_progress,
  6637. path->slots[level]);
  6638. root_item->drop_level = level;
  6639. }
  6640. BUG_ON(wc->level == 0);
  6641. if (btrfs_should_end_transaction(trans, tree_root)) {
  6642. ret = btrfs_update_root(trans, tree_root,
  6643. &root->root_key,
  6644. root_item);
  6645. if (ret) {
  6646. btrfs_abort_transaction(trans, tree_root, ret);
  6647. err = ret;
  6648. goto out_end_trans;
  6649. }
  6650. btrfs_end_transaction_throttle(trans, tree_root);
  6651. trans = btrfs_start_transaction(tree_root, 0);
  6652. if (IS_ERR(trans)) {
  6653. err = PTR_ERR(trans);
  6654. goto out_free;
  6655. }
  6656. if (block_rsv)
  6657. trans->block_rsv = block_rsv;
  6658. }
  6659. }
  6660. btrfs_release_path(path);
  6661. if (err)
  6662. goto out_end_trans;
  6663. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6664. if (ret) {
  6665. btrfs_abort_transaction(trans, tree_root, ret);
  6666. goto out_end_trans;
  6667. }
  6668. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6669. ret = btrfs_find_root(tree_root, &root->root_key, path,
  6670. NULL, NULL);
  6671. if (ret < 0) {
  6672. btrfs_abort_transaction(trans, tree_root, ret);
  6673. err = ret;
  6674. goto out_end_trans;
  6675. } else if (ret > 0) {
  6676. /* if we fail to delete the orphan item this time
  6677. * around, it'll get picked up the next time.
  6678. *
  6679. * The most common failure here is just -ENOENT.
  6680. */
  6681. btrfs_del_orphan_item(trans, tree_root,
  6682. root->root_key.objectid);
  6683. }
  6684. }
  6685. if (root->in_radix) {
  6686. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  6687. } else {
  6688. free_extent_buffer(root->node);
  6689. free_extent_buffer(root->commit_root);
  6690. btrfs_put_fs_root(root);
  6691. }
  6692. out_end_trans:
  6693. btrfs_end_transaction_throttle(trans, tree_root);
  6694. out_free:
  6695. kfree(wc);
  6696. btrfs_free_path(path);
  6697. out:
  6698. if (err)
  6699. btrfs_std_error(root->fs_info, err);
  6700. return err;
  6701. }
  6702. /*
  6703. * drop subtree rooted at tree block 'node'.
  6704. *
  6705. * NOTE: this function will unlock and release tree block 'node'
  6706. * only used by relocation code
  6707. */
  6708. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6709. struct btrfs_root *root,
  6710. struct extent_buffer *node,
  6711. struct extent_buffer *parent)
  6712. {
  6713. struct btrfs_path *path;
  6714. struct walk_control *wc;
  6715. int level;
  6716. int parent_level;
  6717. int ret = 0;
  6718. int wret;
  6719. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6720. path = btrfs_alloc_path();
  6721. if (!path)
  6722. return -ENOMEM;
  6723. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6724. if (!wc) {
  6725. btrfs_free_path(path);
  6726. return -ENOMEM;
  6727. }
  6728. btrfs_assert_tree_locked(parent);
  6729. parent_level = btrfs_header_level(parent);
  6730. extent_buffer_get(parent);
  6731. path->nodes[parent_level] = parent;
  6732. path->slots[parent_level] = btrfs_header_nritems(parent);
  6733. btrfs_assert_tree_locked(node);
  6734. level = btrfs_header_level(node);
  6735. path->nodes[level] = node;
  6736. path->slots[level] = 0;
  6737. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6738. wc->refs[parent_level] = 1;
  6739. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6740. wc->level = level;
  6741. wc->shared_level = -1;
  6742. wc->stage = DROP_REFERENCE;
  6743. wc->update_ref = 0;
  6744. wc->keep_locks = 1;
  6745. wc->for_reloc = 1;
  6746. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6747. while (1) {
  6748. wret = walk_down_tree(trans, root, path, wc);
  6749. if (wret < 0) {
  6750. ret = wret;
  6751. break;
  6752. }
  6753. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6754. if (wret < 0)
  6755. ret = wret;
  6756. if (wret != 0)
  6757. break;
  6758. }
  6759. kfree(wc);
  6760. btrfs_free_path(path);
  6761. return ret;
  6762. }
  6763. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6764. {
  6765. u64 num_devices;
  6766. u64 stripped;
  6767. /*
  6768. * if restripe for this chunk_type is on pick target profile and
  6769. * return, otherwise do the usual balance
  6770. */
  6771. stripped = get_restripe_target(root->fs_info, flags);
  6772. if (stripped)
  6773. return extended_to_chunk(stripped);
  6774. /*
  6775. * we add in the count of missing devices because we want
  6776. * to make sure that any RAID levels on a degraded FS
  6777. * continue to be honored.
  6778. */
  6779. num_devices = root->fs_info->fs_devices->rw_devices +
  6780. root->fs_info->fs_devices->missing_devices;
  6781. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6782. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  6783. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6784. if (num_devices == 1) {
  6785. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6786. stripped = flags & ~stripped;
  6787. /* turn raid0 into single device chunks */
  6788. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6789. return stripped;
  6790. /* turn mirroring into duplication */
  6791. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6792. BTRFS_BLOCK_GROUP_RAID10))
  6793. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6794. } else {
  6795. /* they already had raid on here, just return */
  6796. if (flags & stripped)
  6797. return flags;
  6798. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6799. stripped = flags & ~stripped;
  6800. /* switch duplicated blocks with raid1 */
  6801. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6802. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6803. /* this is drive concat, leave it alone */
  6804. }
  6805. return flags;
  6806. }
  6807. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6808. {
  6809. struct btrfs_space_info *sinfo = cache->space_info;
  6810. u64 num_bytes;
  6811. u64 min_allocable_bytes;
  6812. int ret = -ENOSPC;
  6813. /*
  6814. * We need some metadata space and system metadata space for
  6815. * allocating chunks in some corner cases until we force to set
  6816. * it to be readonly.
  6817. */
  6818. if ((sinfo->flags &
  6819. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6820. !force)
  6821. min_allocable_bytes = 1 * 1024 * 1024;
  6822. else
  6823. min_allocable_bytes = 0;
  6824. spin_lock(&sinfo->lock);
  6825. spin_lock(&cache->lock);
  6826. if (cache->ro) {
  6827. ret = 0;
  6828. goto out;
  6829. }
  6830. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6831. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6832. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6833. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6834. min_allocable_bytes <= sinfo->total_bytes) {
  6835. sinfo->bytes_readonly += num_bytes;
  6836. cache->ro = 1;
  6837. ret = 0;
  6838. }
  6839. out:
  6840. spin_unlock(&cache->lock);
  6841. spin_unlock(&sinfo->lock);
  6842. return ret;
  6843. }
  6844. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6845. struct btrfs_block_group_cache *cache)
  6846. {
  6847. struct btrfs_trans_handle *trans;
  6848. u64 alloc_flags;
  6849. int ret;
  6850. BUG_ON(cache->ro);
  6851. trans = btrfs_join_transaction(root);
  6852. if (IS_ERR(trans))
  6853. return PTR_ERR(trans);
  6854. alloc_flags = update_block_group_flags(root, cache->flags);
  6855. if (alloc_flags != cache->flags) {
  6856. ret = do_chunk_alloc(trans, root, alloc_flags,
  6857. CHUNK_ALLOC_FORCE);
  6858. if (ret < 0)
  6859. goto out;
  6860. }
  6861. ret = set_block_group_ro(cache, 0);
  6862. if (!ret)
  6863. goto out;
  6864. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6865. ret = do_chunk_alloc(trans, root, alloc_flags,
  6866. CHUNK_ALLOC_FORCE);
  6867. if (ret < 0)
  6868. goto out;
  6869. ret = set_block_group_ro(cache, 0);
  6870. out:
  6871. btrfs_end_transaction(trans, root);
  6872. return ret;
  6873. }
  6874. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6875. struct btrfs_root *root, u64 type)
  6876. {
  6877. u64 alloc_flags = get_alloc_profile(root, type);
  6878. return do_chunk_alloc(trans, root, alloc_flags,
  6879. CHUNK_ALLOC_FORCE);
  6880. }
  6881. /*
  6882. * helper to account the unused space of all the readonly block group in the
  6883. * list. takes mirrors into account.
  6884. */
  6885. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6886. {
  6887. struct btrfs_block_group_cache *block_group;
  6888. u64 free_bytes = 0;
  6889. int factor;
  6890. list_for_each_entry(block_group, groups_list, list) {
  6891. spin_lock(&block_group->lock);
  6892. if (!block_group->ro) {
  6893. spin_unlock(&block_group->lock);
  6894. continue;
  6895. }
  6896. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6897. BTRFS_BLOCK_GROUP_RAID10 |
  6898. BTRFS_BLOCK_GROUP_DUP))
  6899. factor = 2;
  6900. else
  6901. factor = 1;
  6902. free_bytes += (block_group->key.offset -
  6903. btrfs_block_group_used(&block_group->item)) *
  6904. factor;
  6905. spin_unlock(&block_group->lock);
  6906. }
  6907. return free_bytes;
  6908. }
  6909. /*
  6910. * helper to account the unused space of all the readonly block group in the
  6911. * space_info. takes mirrors into account.
  6912. */
  6913. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  6914. {
  6915. int i;
  6916. u64 free_bytes = 0;
  6917. spin_lock(&sinfo->lock);
  6918. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  6919. if (!list_empty(&sinfo->block_groups[i]))
  6920. free_bytes += __btrfs_get_ro_block_group_free_space(
  6921. &sinfo->block_groups[i]);
  6922. spin_unlock(&sinfo->lock);
  6923. return free_bytes;
  6924. }
  6925. void btrfs_set_block_group_rw(struct btrfs_root *root,
  6926. struct btrfs_block_group_cache *cache)
  6927. {
  6928. struct btrfs_space_info *sinfo = cache->space_info;
  6929. u64 num_bytes;
  6930. BUG_ON(!cache->ro);
  6931. spin_lock(&sinfo->lock);
  6932. spin_lock(&cache->lock);
  6933. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6934. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6935. sinfo->bytes_readonly -= num_bytes;
  6936. cache->ro = 0;
  6937. spin_unlock(&cache->lock);
  6938. spin_unlock(&sinfo->lock);
  6939. }
  6940. /*
  6941. * checks to see if its even possible to relocate this block group.
  6942. *
  6943. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  6944. * ok to go ahead and try.
  6945. */
  6946. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  6947. {
  6948. struct btrfs_block_group_cache *block_group;
  6949. struct btrfs_space_info *space_info;
  6950. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  6951. struct btrfs_device *device;
  6952. u64 min_free;
  6953. u64 dev_min = 1;
  6954. u64 dev_nr = 0;
  6955. u64 target;
  6956. int index;
  6957. int full = 0;
  6958. int ret = 0;
  6959. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  6960. /* odd, couldn't find the block group, leave it alone */
  6961. if (!block_group)
  6962. return -1;
  6963. min_free = btrfs_block_group_used(&block_group->item);
  6964. /* no bytes used, we're good */
  6965. if (!min_free)
  6966. goto out;
  6967. space_info = block_group->space_info;
  6968. spin_lock(&space_info->lock);
  6969. full = space_info->full;
  6970. /*
  6971. * if this is the last block group we have in this space, we can't
  6972. * relocate it unless we're able to allocate a new chunk below.
  6973. *
  6974. * Otherwise, we need to make sure we have room in the space to handle
  6975. * all of the extents from this block group. If we can, we're good
  6976. */
  6977. if ((space_info->total_bytes != block_group->key.offset) &&
  6978. (space_info->bytes_used + space_info->bytes_reserved +
  6979. space_info->bytes_pinned + space_info->bytes_readonly +
  6980. min_free < space_info->total_bytes)) {
  6981. spin_unlock(&space_info->lock);
  6982. goto out;
  6983. }
  6984. spin_unlock(&space_info->lock);
  6985. /*
  6986. * ok we don't have enough space, but maybe we have free space on our
  6987. * devices to allocate new chunks for relocation, so loop through our
  6988. * alloc devices and guess if we have enough space. if this block
  6989. * group is going to be restriped, run checks against the target
  6990. * profile instead of the current one.
  6991. */
  6992. ret = -1;
  6993. /*
  6994. * index:
  6995. * 0: raid10
  6996. * 1: raid1
  6997. * 2: dup
  6998. * 3: raid0
  6999. * 4: single
  7000. */
  7001. target = get_restripe_target(root->fs_info, block_group->flags);
  7002. if (target) {
  7003. index = __get_raid_index(extended_to_chunk(target));
  7004. } else {
  7005. /*
  7006. * this is just a balance, so if we were marked as full
  7007. * we know there is no space for a new chunk
  7008. */
  7009. if (full)
  7010. goto out;
  7011. index = get_block_group_index(block_group);
  7012. }
  7013. if (index == BTRFS_RAID_RAID10) {
  7014. dev_min = 4;
  7015. /* Divide by 2 */
  7016. min_free >>= 1;
  7017. } else if (index == BTRFS_RAID_RAID1) {
  7018. dev_min = 2;
  7019. } else if (index == BTRFS_RAID_DUP) {
  7020. /* Multiply by 2 */
  7021. min_free <<= 1;
  7022. } else if (index == BTRFS_RAID_RAID0) {
  7023. dev_min = fs_devices->rw_devices;
  7024. do_div(min_free, dev_min);
  7025. }
  7026. mutex_lock(&root->fs_info->chunk_mutex);
  7027. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7028. u64 dev_offset;
  7029. /*
  7030. * check to make sure we can actually find a chunk with enough
  7031. * space to fit our block group in.
  7032. */
  7033. if (device->total_bytes > device->bytes_used + min_free &&
  7034. !device->is_tgtdev_for_dev_replace) {
  7035. ret = find_free_dev_extent(device, min_free,
  7036. &dev_offset, NULL);
  7037. if (!ret)
  7038. dev_nr++;
  7039. if (dev_nr >= dev_min)
  7040. break;
  7041. ret = -1;
  7042. }
  7043. }
  7044. mutex_unlock(&root->fs_info->chunk_mutex);
  7045. out:
  7046. btrfs_put_block_group(block_group);
  7047. return ret;
  7048. }
  7049. static int find_first_block_group(struct btrfs_root *root,
  7050. struct btrfs_path *path, struct btrfs_key *key)
  7051. {
  7052. int ret = 0;
  7053. struct btrfs_key found_key;
  7054. struct extent_buffer *leaf;
  7055. int slot;
  7056. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7057. if (ret < 0)
  7058. goto out;
  7059. while (1) {
  7060. slot = path->slots[0];
  7061. leaf = path->nodes[0];
  7062. if (slot >= btrfs_header_nritems(leaf)) {
  7063. ret = btrfs_next_leaf(root, path);
  7064. if (ret == 0)
  7065. continue;
  7066. if (ret < 0)
  7067. goto out;
  7068. break;
  7069. }
  7070. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7071. if (found_key.objectid >= key->objectid &&
  7072. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7073. ret = 0;
  7074. goto out;
  7075. }
  7076. path->slots[0]++;
  7077. }
  7078. out:
  7079. return ret;
  7080. }
  7081. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7082. {
  7083. struct btrfs_block_group_cache *block_group;
  7084. u64 last = 0;
  7085. while (1) {
  7086. struct inode *inode;
  7087. block_group = btrfs_lookup_first_block_group(info, last);
  7088. while (block_group) {
  7089. spin_lock(&block_group->lock);
  7090. if (block_group->iref)
  7091. break;
  7092. spin_unlock(&block_group->lock);
  7093. block_group = next_block_group(info->tree_root,
  7094. block_group);
  7095. }
  7096. if (!block_group) {
  7097. if (last == 0)
  7098. break;
  7099. last = 0;
  7100. continue;
  7101. }
  7102. inode = block_group->inode;
  7103. block_group->iref = 0;
  7104. block_group->inode = NULL;
  7105. spin_unlock(&block_group->lock);
  7106. iput(inode);
  7107. last = block_group->key.objectid + block_group->key.offset;
  7108. btrfs_put_block_group(block_group);
  7109. }
  7110. }
  7111. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7112. {
  7113. struct btrfs_block_group_cache *block_group;
  7114. struct btrfs_space_info *space_info;
  7115. struct btrfs_caching_control *caching_ctl;
  7116. struct rb_node *n;
  7117. down_write(&info->extent_commit_sem);
  7118. while (!list_empty(&info->caching_block_groups)) {
  7119. caching_ctl = list_entry(info->caching_block_groups.next,
  7120. struct btrfs_caching_control, list);
  7121. list_del(&caching_ctl->list);
  7122. put_caching_control(caching_ctl);
  7123. }
  7124. up_write(&info->extent_commit_sem);
  7125. spin_lock(&info->block_group_cache_lock);
  7126. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7127. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7128. cache_node);
  7129. rb_erase(&block_group->cache_node,
  7130. &info->block_group_cache_tree);
  7131. spin_unlock(&info->block_group_cache_lock);
  7132. down_write(&block_group->space_info->groups_sem);
  7133. list_del(&block_group->list);
  7134. up_write(&block_group->space_info->groups_sem);
  7135. if (block_group->cached == BTRFS_CACHE_STARTED)
  7136. wait_block_group_cache_done(block_group);
  7137. /*
  7138. * We haven't cached this block group, which means we could
  7139. * possibly have excluded extents on this block group.
  7140. */
  7141. if (block_group->cached == BTRFS_CACHE_NO)
  7142. free_excluded_extents(info->extent_root, block_group);
  7143. btrfs_remove_free_space_cache(block_group);
  7144. btrfs_put_block_group(block_group);
  7145. spin_lock(&info->block_group_cache_lock);
  7146. }
  7147. spin_unlock(&info->block_group_cache_lock);
  7148. /* now that all the block groups are freed, go through and
  7149. * free all the space_info structs. This is only called during
  7150. * the final stages of unmount, and so we know nobody is
  7151. * using them. We call synchronize_rcu() once before we start,
  7152. * just to be on the safe side.
  7153. */
  7154. synchronize_rcu();
  7155. release_global_block_rsv(info);
  7156. while(!list_empty(&info->space_info)) {
  7157. space_info = list_entry(info->space_info.next,
  7158. struct btrfs_space_info,
  7159. list);
  7160. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  7161. if (space_info->bytes_pinned > 0 ||
  7162. space_info->bytes_reserved > 0 ||
  7163. space_info->bytes_may_use > 0) {
  7164. WARN_ON(1);
  7165. dump_space_info(space_info, 0, 0);
  7166. }
  7167. }
  7168. list_del(&space_info->list);
  7169. kfree(space_info);
  7170. }
  7171. return 0;
  7172. }
  7173. static void __link_block_group(struct btrfs_space_info *space_info,
  7174. struct btrfs_block_group_cache *cache)
  7175. {
  7176. int index = get_block_group_index(cache);
  7177. down_write(&space_info->groups_sem);
  7178. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7179. up_write(&space_info->groups_sem);
  7180. }
  7181. int btrfs_read_block_groups(struct btrfs_root *root)
  7182. {
  7183. struct btrfs_path *path;
  7184. int ret;
  7185. struct btrfs_block_group_cache *cache;
  7186. struct btrfs_fs_info *info = root->fs_info;
  7187. struct btrfs_space_info *space_info;
  7188. struct btrfs_key key;
  7189. struct btrfs_key found_key;
  7190. struct extent_buffer *leaf;
  7191. int need_clear = 0;
  7192. u64 cache_gen;
  7193. root = info->extent_root;
  7194. key.objectid = 0;
  7195. key.offset = 0;
  7196. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7197. path = btrfs_alloc_path();
  7198. if (!path)
  7199. return -ENOMEM;
  7200. path->reada = 1;
  7201. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  7202. if (btrfs_test_opt(root, SPACE_CACHE) &&
  7203. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  7204. need_clear = 1;
  7205. if (btrfs_test_opt(root, CLEAR_CACHE))
  7206. need_clear = 1;
  7207. while (1) {
  7208. ret = find_first_block_group(root, path, &key);
  7209. if (ret > 0)
  7210. break;
  7211. if (ret != 0)
  7212. goto error;
  7213. leaf = path->nodes[0];
  7214. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7215. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7216. if (!cache) {
  7217. ret = -ENOMEM;
  7218. goto error;
  7219. }
  7220. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7221. GFP_NOFS);
  7222. if (!cache->free_space_ctl) {
  7223. kfree(cache);
  7224. ret = -ENOMEM;
  7225. goto error;
  7226. }
  7227. atomic_set(&cache->count, 1);
  7228. spin_lock_init(&cache->lock);
  7229. cache->fs_info = info;
  7230. INIT_LIST_HEAD(&cache->list);
  7231. INIT_LIST_HEAD(&cache->cluster_list);
  7232. if (need_clear) {
  7233. /*
  7234. * When we mount with old space cache, we need to
  7235. * set BTRFS_DC_CLEAR and set dirty flag.
  7236. *
  7237. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  7238. * truncate the old free space cache inode and
  7239. * setup a new one.
  7240. * b) Setting 'dirty flag' makes sure that we flush
  7241. * the new space cache info onto disk.
  7242. */
  7243. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7244. if (btrfs_test_opt(root, SPACE_CACHE))
  7245. cache->dirty = 1;
  7246. }
  7247. read_extent_buffer(leaf, &cache->item,
  7248. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7249. sizeof(cache->item));
  7250. memcpy(&cache->key, &found_key, sizeof(found_key));
  7251. key.objectid = found_key.objectid + found_key.offset;
  7252. btrfs_release_path(path);
  7253. cache->flags = btrfs_block_group_flags(&cache->item);
  7254. cache->sectorsize = root->sectorsize;
  7255. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7256. &root->fs_info->mapping_tree,
  7257. found_key.objectid);
  7258. btrfs_init_free_space_ctl(cache);
  7259. /*
  7260. * We need to exclude the super stripes now so that the space
  7261. * info has super bytes accounted for, otherwise we'll think
  7262. * we have more space than we actually do.
  7263. */
  7264. ret = exclude_super_stripes(root, cache);
  7265. if (ret) {
  7266. /*
  7267. * We may have excluded something, so call this just in
  7268. * case.
  7269. */
  7270. free_excluded_extents(root, cache);
  7271. kfree(cache->free_space_ctl);
  7272. kfree(cache);
  7273. goto error;
  7274. }
  7275. /*
  7276. * check for two cases, either we are full, and therefore
  7277. * don't need to bother with the caching work since we won't
  7278. * find any space, or we are empty, and we can just add all
  7279. * the space in and be done with it. This saves us _alot_ of
  7280. * time, particularly in the full case.
  7281. */
  7282. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7283. cache->last_byte_to_unpin = (u64)-1;
  7284. cache->cached = BTRFS_CACHE_FINISHED;
  7285. free_excluded_extents(root, cache);
  7286. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7287. cache->last_byte_to_unpin = (u64)-1;
  7288. cache->cached = BTRFS_CACHE_FINISHED;
  7289. add_new_free_space(cache, root->fs_info,
  7290. found_key.objectid,
  7291. found_key.objectid +
  7292. found_key.offset);
  7293. free_excluded_extents(root, cache);
  7294. }
  7295. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7296. if (ret) {
  7297. btrfs_remove_free_space_cache(cache);
  7298. btrfs_put_block_group(cache);
  7299. goto error;
  7300. }
  7301. ret = update_space_info(info, cache->flags, found_key.offset,
  7302. btrfs_block_group_used(&cache->item),
  7303. &space_info);
  7304. if (ret) {
  7305. btrfs_remove_free_space_cache(cache);
  7306. spin_lock(&info->block_group_cache_lock);
  7307. rb_erase(&cache->cache_node,
  7308. &info->block_group_cache_tree);
  7309. spin_unlock(&info->block_group_cache_lock);
  7310. btrfs_put_block_group(cache);
  7311. goto error;
  7312. }
  7313. cache->space_info = space_info;
  7314. spin_lock(&cache->space_info->lock);
  7315. cache->space_info->bytes_readonly += cache->bytes_super;
  7316. spin_unlock(&cache->space_info->lock);
  7317. __link_block_group(space_info, cache);
  7318. set_avail_alloc_bits(root->fs_info, cache->flags);
  7319. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7320. set_block_group_ro(cache, 1);
  7321. }
  7322. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7323. if (!(get_alloc_profile(root, space_info->flags) &
  7324. (BTRFS_BLOCK_GROUP_RAID10 |
  7325. BTRFS_BLOCK_GROUP_RAID1 |
  7326. BTRFS_BLOCK_GROUP_RAID5 |
  7327. BTRFS_BLOCK_GROUP_RAID6 |
  7328. BTRFS_BLOCK_GROUP_DUP)))
  7329. continue;
  7330. /*
  7331. * avoid allocating from un-mirrored block group if there are
  7332. * mirrored block groups.
  7333. */
  7334. list_for_each_entry(cache, &space_info->block_groups[3], list)
  7335. set_block_group_ro(cache, 1);
  7336. list_for_each_entry(cache, &space_info->block_groups[4], list)
  7337. set_block_group_ro(cache, 1);
  7338. }
  7339. init_global_block_rsv(info);
  7340. ret = 0;
  7341. error:
  7342. btrfs_free_path(path);
  7343. return ret;
  7344. }
  7345. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  7346. struct btrfs_root *root)
  7347. {
  7348. struct btrfs_block_group_cache *block_group, *tmp;
  7349. struct btrfs_root *extent_root = root->fs_info->extent_root;
  7350. struct btrfs_block_group_item item;
  7351. struct btrfs_key key;
  7352. int ret = 0;
  7353. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  7354. new_bg_list) {
  7355. list_del_init(&block_group->new_bg_list);
  7356. if (ret)
  7357. continue;
  7358. spin_lock(&block_group->lock);
  7359. memcpy(&item, &block_group->item, sizeof(item));
  7360. memcpy(&key, &block_group->key, sizeof(key));
  7361. spin_unlock(&block_group->lock);
  7362. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  7363. sizeof(item));
  7364. if (ret)
  7365. btrfs_abort_transaction(trans, extent_root, ret);
  7366. }
  7367. }
  7368. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7369. struct btrfs_root *root, u64 bytes_used,
  7370. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7371. u64 size)
  7372. {
  7373. int ret;
  7374. struct btrfs_root *extent_root;
  7375. struct btrfs_block_group_cache *cache;
  7376. extent_root = root->fs_info->extent_root;
  7377. root->fs_info->last_trans_log_full_commit = trans->transid;
  7378. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7379. if (!cache)
  7380. return -ENOMEM;
  7381. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7382. GFP_NOFS);
  7383. if (!cache->free_space_ctl) {
  7384. kfree(cache);
  7385. return -ENOMEM;
  7386. }
  7387. cache->key.objectid = chunk_offset;
  7388. cache->key.offset = size;
  7389. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7390. cache->sectorsize = root->sectorsize;
  7391. cache->fs_info = root->fs_info;
  7392. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7393. &root->fs_info->mapping_tree,
  7394. chunk_offset);
  7395. atomic_set(&cache->count, 1);
  7396. spin_lock_init(&cache->lock);
  7397. INIT_LIST_HEAD(&cache->list);
  7398. INIT_LIST_HEAD(&cache->cluster_list);
  7399. INIT_LIST_HEAD(&cache->new_bg_list);
  7400. btrfs_init_free_space_ctl(cache);
  7401. btrfs_set_block_group_used(&cache->item, bytes_used);
  7402. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7403. cache->flags = type;
  7404. btrfs_set_block_group_flags(&cache->item, type);
  7405. cache->last_byte_to_unpin = (u64)-1;
  7406. cache->cached = BTRFS_CACHE_FINISHED;
  7407. ret = exclude_super_stripes(root, cache);
  7408. if (ret) {
  7409. /*
  7410. * We may have excluded something, so call this just in
  7411. * case.
  7412. */
  7413. free_excluded_extents(root, cache);
  7414. kfree(cache->free_space_ctl);
  7415. kfree(cache);
  7416. return ret;
  7417. }
  7418. add_new_free_space(cache, root->fs_info, chunk_offset,
  7419. chunk_offset + size);
  7420. free_excluded_extents(root, cache);
  7421. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7422. if (ret) {
  7423. btrfs_remove_free_space_cache(cache);
  7424. btrfs_put_block_group(cache);
  7425. return ret;
  7426. }
  7427. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7428. &cache->space_info);
  7429. if (ret) {
  7430. btrfs_remove_free_space_cache(cache);
  7431. spin_lock(&root->fs_info->block_group_cache_lock);
  7432. rb_erase(&cache->cache_node,
  7433. &root->fs_info->block_group_cache_tree);
  7434. spin_unlock(&root->fs_info->block_group_cache_lock);
  7435. btrfs_put_block_group(cache);
  7436. return ret;
  7437. }
  7438. update_global_block_rsv(root->fs_info);
  7439. spin_lock(&cache->space_info->lock);
  7440. cache->space_info->bytes_readonly += cache->bytes_super;
  7441. spin_unlock(&cache->space_info->lock);
  7442. __link_block_group(cache->space_info, cache);
  7443. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  7444. set_avail_alloc_bits(extent_root->fs_info, type);
  7445. return 0;
  7446. }
  7447. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  7448. {
  7449. u64 extra_flags = chunk_to_extended(flags) &
  7450. BTRFS_EXTENDED_PROFILE_MASK;
  7451. write_seqlock(&fs_info->profiles_lock);
  7452. if (flags & BTRFS_BLOCK_GROUP_DATA)
  7453. fs_info->avail_data_alloc_bits &= ~extra_flags;
  7454. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  7455. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  7456. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  7457. fs_info->avail_system_alloc_bits &= ~extra_flags;
  7458. write_sequnlock(&fs_info->profiles_lock);
  7459. }
  7460. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7461. struct btrfs_root *root, u64 group_start)
  7462. {
  7463. struct btrfs_path *path;
  7464. struct btrfs_block_group_cache *block_group;
  7465. struct btrfs_free_cluster *cluster;
  7466. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7467. struct btrfs_key key;
  7468. struct inode *inode;
  7469. int ret;
  7470. int index;
  7471. int factor;
  7472. root = root->fs_info->extent_root;
  7473. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7474. BUG_ON(!block_group);
  7475. BUG_ON(!block_group->ro);
  7476. /*
  7477. * Free the reserved super bytes from this block group before
  7478. * remove it.
  7479. */
  7480. free_excluded_extents(root, block_group);
  7481. memcpy(&key, &block_group->key, sizeof(key));
  7482. index = get_block_group_index(block_group);
  7483. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7484. BTRFS_BLOCK_GROUP_RAID1 |
  7485. BTRFS_BLOCK_GROUP_RAID10))
  7486. factor = 2;
  7487. else
  7488. factor = 1;
  7489. /* make sure this block group isn't part of an allocation cluster */
  7490. cluster = &root->fs_info->data_alloc_cluster;
  7491. spin_lock(&cluster->refill_lock);
  7492. btrfs_return_cluster_to_free_space(block_group, cluster);
  7493. spin_unlock(&cluster->refill_lock);
  7494. /*
  7495. * make sure this block group isn't part of a metadata
  7496. * allocation cluster
  7497. */
  7498. cluster = &root->fs_info->meta_alloc_cluster;
  7499. spin_lock(&cluster->refill_lock);
  7500. btrfs_return_cluster_to_free_space(block_group, cluster);
  7501. spin_unlock(&cluster->refill_lock);
  7502. path = btrfs_alloc_path();
  7503. if (!path) {
  7504. ret = -ENOMEM;
  7505. goto out;
  7506. }
  7507. inode = lookup_free_space_inode(tree_root, block_group, path);
  7508. if (!IS_ERR(inode)) {
  7509. ret = btrfs_orphan_add(trans, inode);
  7510. if (ret) {
  7511. btrfs_add_delayed_iput(inode);
  7512. goto out;
  7513. }
  7514. clear_nlink(inode);
  7515. /* One for the block groups ref */
  7516. spin_lock(&block_group->lock);
  7517. if (block_group->iref) {
  7518. block_group->iref = 0;
  7519. block_group->inode = NULL;
  7520. spin_unlock(&block_group->lock);
  7521. iput(inode);
  7522. } else {
  7523. spin_unlock(&block_group->lock);
  7524. }
  7525. /* One for our lookup ref */
  7526. btrfs_add_delayed_iput(inode);
  7527. }
  7528. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7529. key.offset = block_group->key.objectid;
  7530. key.type = 0;
  7531. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7532. if (ret < 0)
  7533. goto out;
  7534. if (ret > 0)
  7535. btrfs_release_path(path);
  7536. if (ret == 0) {
  7537. ret = btrfs_del_item(trans, tree_root, path);
  7538. if (ret)
  7539. goto out;
  7540. btrfs_release_path(path);
  7541. }
  7542. spin_lock(&root->fs_info->block_group_cache_lock);
  7543. rb_erase(&block_group->cache_node,
  7544. &root->fs_info->block_group_cache_tree);
  7545. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  7546. root->fs_info->first_logical_byte = (u64)-1;
  7547. spin_unlock(&root->fs_info->block_group_cache_lock);
  7548. down_write(&block_group->space_info->groups_sem);
  7549. /*
  7550. * we must use list_del_init so people can check to see if they
  7551. * are still on the list after taking the semaphore
  7552. */
  7553. list_del_init(&block_group->list);
  7554. if (list_empty(&block_group->space_info->block_groups[index]))
  7555. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7556. up_write(&block_group->space_info->groups_sem);
  7557. if (block_group->cached == BTRFS_CACHE_STARTED)
  7558. wait_block_group_cache_done(block_group);
  7559. btrfs_remove_free_space_cache(block_group);
  7560. spin_lock(&block_group->space_info->lock);
  7561. block_group->space_info->total_bytes -= block_group->key.offset;
  7562. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7563. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7564. spin_unlock(&block_group->space_info->lock);
  7565. memcpy(&key, &block_group->key, sizeof(key));
  7566. btrfs_clear_space_info_full(root->fs_info);
  7567. btrfs_put_block_group(block_group);
  7568. btrfs_put_block_group(block_group);
  7569. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7570. if (ret > 0)
  7571. ret = -EIO;
  7572. if (ret < 0)
  7573. goto out;
  7574. ret = btrfs_del_item(trans, root, path);
  7575. out:
  7576. btrfs_free_path(path);
  7577. return ret;
  7578. }
  7579. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7580. {
  7581. struct btrfs_space_info *space_info;
  7582. struct btrfs_super_block *disk_super;
  7583. u64 features;
  7584. u64 flags;
  7585. int mixed = 0;
  7586. int ret;
  7587. disk_super = fs_info->super_copy;
  7588. if (!btrfs_super_root(disk_super))
  7589. return 1;
  7590. features = btrfs_super_incompat_flags(disk_super);
  7591. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7592. mixed = 1;
  7593. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7594. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7595. if (ret)
  7596. goto out;
  7597. if (mixed) {
  7598. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7599. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7600. } else {
  7601. flags = BTRFS_BLOCK_GROUP_METADATA;
  7602. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7603. if (ret)
  7604. goto out;
  7605. flags = BTRFS_BLOCK_GROUP_DATA;
  7606. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7607. }
  7608. out:
  7609. return ret;
  7610. }
  7611. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7612. {
  7613. return unpin_extent_range(root, start, end);
  7614. }
  7615. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7616. u64 num_bytes, u64 *actual_bytes)
  7617. {
  7618. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7619. }
  7620. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7621. {
  7622. struct btrfs_fs_info *fs_info = root->fs_info;
  7623. struct btrfs_block_group_cache *cache = NULL;
  7624. u64 group_trimmed;
  7625. u64 start;
  7626. u64 end;
  7627. u64 trimmed = 0;
  7628. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7629. int ret = 0;
  7630. /*
  7631. * try to trim all FS space, our block group may start from non-zero.
  7632. */
  7633. if (range->len == total_bytes)
  7634. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7635. else
  7636. cache = btrfs_lookup_block_group(fs_info, range->start);
  7637. while (cache) {
  7638. if (cache->key.objectid >= (range->start + range->len)) {
  7639. btrfs_put_block_group(cache);
  7640. break;
  7641. }
  7642. start = max(range->start, cache->key.objectid);
  7643. end = min(range->start + range->len,
  7644. cache->key.objectid + cache->key.offset);
  7645. if (end - start >= range->minlen) {
  7646. if (!block_group_cache_done(cache)) {
  7647. ret = cache_block_group(cache, 0);
  7648. if (ret) {
  7649. btrfs_put_block_group(cache);
  7650. break;
  7651. }
  7652. ret = wait_block_group_cache_done(cache);
  7653. if (ret) {
  7654. btrfs_put_block_group(cache);
  7655. break;
  7656. }
  7657. }
  7658. ret = btrfs_trim_block_group(cache,
  7659. &group_trimmed,
  7660. start,
  7661. end,
  7662. range->minlen);
  7663. trimmed += group_trimmed;
  7664. if (ret) {
  7665. btrfs_put_block_group(cache);
  7666. break;
  7667. }
  7668. }
  7669. cache = next_block_group(fs_info->tree_root, cache);
  7670. }
  7671. range->len = trimmed;
  7672. return ret;
  7673. }