extent-tree.c 271 KB

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