extent-tree.c 192 KB

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