extent-tree.c 197 KB

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