extent-tree.c 225 KB

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