extent-tree.c 224 KB

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