extent-tree.c 229 KB

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