extent-tree.c 228 KB

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