extent-tree.c 218 KB

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