extent-tree.c 196 KB

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