extent-tree.c 187 KB

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