extent-tree.c 201 KB

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