extent-tree.c 211 KB

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