extent-tree.c 228 KB

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