extent-tree.c 244 KB

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