extent-tree.c 199 KB

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