extent-tree.c 202 KB

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