extent-tree.c 228 KB

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