extent-tree.c 223 KB

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