extent-tree.c 192 KB

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