extent-tree.c 221 KB

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