extent-tree.c 200 KB

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