extent-tree.c 187 KB

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