extent-tree.c 194 KB

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