extent-tree.c 233 KB

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