extent-tree.c 233 KB

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