extent-tree.c 235 KB

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