extent-tree.c 239 KB

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