extent-tree.c 197 KB

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