extent-tree.c 198 KB

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