extent-tree.c 211 KB

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