extent-tree.c 234 KB

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