extent-tree.c 203 KB

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