extent-tree.c 198 KB

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