extent-tree.c 254 KB

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