extent-tree.c 223 KB

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