extent-tree.c 193 KB

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