extent-tree.c 181 KB

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