extent-tree.c 233 KB

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