extent-tree.c 212 KB

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