extent-tree.c 215 KB

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