extent-tree.c 163 KB

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