extent-tree.c 231 KB

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