extent-tree.c 218 KB

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