extent-tree.c 220 KB

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