extent-tree.c 204 KB

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