extent-tree.c 214 KB

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