extent-tree.c 194 KB

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