extent-tree.c 226 KB

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