extent-tree.c 195 KB

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