extent-tree.c 221 KB

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