extent-tree.c 221 KB

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