extent-tree.c 201 KB

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