extent-tree.c 212 KB

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