extent-tree.c 235 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855
  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. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1828. parent = ref->parent;
  1829. else
  1830. ref_root = ref->root;
  1831. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1832. if (extent_op)
  1833. flags |= extent_op->flags_to_set;
  1834. ret = alloc_reserved_file_extent(trans, root,
  1835. parent, ref_root, flags,
  1836. ref->objectid, ref->offset,
  1837. &ins, node->ref_mod);
  1838. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1839. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1840. node->num_bytes, parent,
  1841. ref_root, ref->objectid,
  1842. ref->offset, node->ref_mod,
  1843. extent_op);
  1844. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1845. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1846. node->num_bytes, parent,
  1847. ref_root, ref->objectid,
  1848. ref->offset, node->ref_mod,
  1849. extent_op);
  1850. } else {
  1851. BUG();
  1852. }
  1853. return ret;
  1854. }
  1855. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1856. struct extent_buffer *leaf,
  1857. struct btrfs_extent_item *ei)
  1858. {
  1859. u64 flags = btrfs_extent_flags(leaf, ei);
  1860. if (extent_op->update_flags) {
  1861. flags |= extent_op->flags_to_set;
  1862. btrfs_set_extent_flags(leaf, ei, flags);
  1863. }
  1864. if (extent_op->update_key) {
  1865. struct btrfs_tree_block_info *bi;
  1866. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1867. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1868. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1869. }
  1870. }
  1871. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1872. struct btrfs_root *root,
  1873. struct btrfs_delayed_ref_node *node,
  1874. struct btrfs_delayed_extent_op *extent_op)
  1875. {
  1876. struct btrfs_key key;
  1877. struct btrfs_path *path;
  1878. struct btrfs_extent_item *ei;
  1879. struct extent_buffer *leaf;
  1880. u32 item_size;
  1881. int ret;
  1882. int err = 0;
  1883. int metadata = !extent_op->is_data;
  1884. if (trans->aborted)
  1885. return 0;
  1886. if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  1887. metadata = 0;
  1888. path = btrfs_alloc_path();
  1889. if (!path)
  1890. return -ENOMEM;
  1891. key.objectid = node->bytenr;
  1892. if (metadata) {
  1893. key.type = BTRFS_METADATA_ITEM_KEY;
  1894. key.offset = extent_op->level;
  1895. } else {
  1896. key.type = BTRFS_EXTENT_ITEM_KEY;
  1897. key.offset = node->num_bytes;
  1898. }
  1899. again:
  1900. path->reada = 1;
  1901. path->leave_spinning = 1;
  1902. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  1903. path, 0, 1);
  1904. if (ret < 0) {
  1905. err = ret;
  1906. goto out;
  1907. }
  1908. if (ret > 0) {
  1909. if (metadata) {
  1910. btrfs_release_path(path);
  1911. metadata = 0;
  1912. key.offset = node->num_bytes;
  1913. key.type = BTRFS_EXTENT_ITEM_KEY;
  1914. goto again;
  1915. }
  1916. err = -EIO;
  1917. goto out;
  1918. }
  1919. leaf = path->nodes[0];
  1920. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1921. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1922. if (item_size < sizeof(*ei)) {
  1923. ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
  1924. path, (u64)-1, 0);
  1925. if (ret < 0) {
  1926. err = ret;
  1927. goto out;
  1928. }
  1929. leaf = path->nodes[0];
  1930. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1931. }
  1932. #endif
  1933. BUG_ON(item_size < sizeof(*ei));
  1934. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1935. __run_delayed_extent_op(extent_op, leaf, ei);
  1936. btrfs_mark_buffer_dirty(leaf);
  1937. out:
  1938. btrfs_free_path(path);
  1939. return err;
  1940. }
  1941. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1942. struct btrfs_root *root,
  1943. struct btrfs_delayed_ref_node *node,
  1944. struct btrfs_delayed_extent_op *extent_op,
  1945. int insert_reserved)
  1946. {
  1947. int ret = 0;
  1948. struct btrfs_delayed_tree_ref *ref;
  1949. struct btrfs_key ins;
  1950. u64 parent = 0;
  1951. u64 ref_root = 0;
  1952. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  1953. SKINNY_METADATA);
  1954. ref = btrfs_delayed_node_to_tree_ref(node);
  1955. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1956. parent = ref->parent;
  1957. else
  1958. ref_root = ref->root;
  1959. ins.objectid = node->bytenr;
  1960. if (skinny_metadata) {
  1961. ins.offset = ref->level;
  1962. ins.type = BTRFS_METADATA_ITEM_KEY;
  1963. } else {
  1964. ins.offset = node->num_bytes;
  1965. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1966. }
  1967. BUG_ON(node->ref_mod != 1);
  1968. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1969. BUG_ON(!extent_op || !extent_op->update_flags);
  1970. ret = alloc_reserved_tree_block(trans, root,
  1971. parent, ref_root,
  1972. extent_op->flags_to_set,
  1973. &extent_op->key,
  1974. ref->level, &ins);
  1975. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1976. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1977. node->num_bytes, parent, ref_root,
  1978. ref->level, 0, 1, extent_op);
  1979. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1980. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1981. node->num_bytes, parent, ref_root,
  1982. ref->level, 0, 1, extent_op);
  1983. } else {
  1984. BUG();
  1985. }
  1986. return ret;
  1987. }
  1988. /* helper function to actually process a single delayed ref entry */
  1989. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  1990. struct btrfs_root *root,
  1991. struct btrfs_delayed_ref_node *node,
  1992. struct btrfs_delayed_extent_op *extent_op,
  1993. int insert_reserved)
  1994. {
  1995. int ret = 0;
  1996. if (trans->aborted)
  1997. return 0;
  1998. if (btrfs_delayed_ref_is_head(node)) {
  1999. struct btrfs_delayed_ref_head *head;
  2000. /*
  2001. * we've hit the end of the chain and we were supposed
  2002. * to insert this extent into the tree. But, it got
  2003. * deleted before we ever needed to insert it, so all
  2004. * we have to do is clean up the accounting
  2005. */
  2006. BUG_ON(extent_op);
  2007. head = btrfs_delayed_node_to_head(node);
  2008. if (insert_reserved) {
  2009. btrfs_pin_extent(root, node->bytenr,
  2010. node->num_bytes, 1);
  2011. if (head->is_data) {
  2012. ret = btrfs_del_csums(trans, root,
  2013. node->bytenr,
  2014. node->num_bytes);
  2015. }
  2016. }
  2017. return ret;
  2018. }
  2019. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  2020. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  2021. ret = run_delayed_tree_ref(trans, root, node, extent_op,
  2022. insert_reserved);
  2023. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  2024. node->type == BTRFS_SHARED_DATA_REF_KEY)
  2025. ret = run_delayed_data_ref(trans, root, node, extent_op,
  2026. insert_reserved);
  2027. else
  2028. BUG();
  2029. return ret;
  2030. }
  2031. static noinline struct btrfs_delayed_ref_node *
  2032. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  2033. {
  2034. struct rb_node *node;
  2035. struct btrfs_delayed_ref_node *ref;
  2036. int action = BTRFS_ADD_DELAYED_REF;
  2037. again:
  2038. /*
  2039. * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
  2040. * this prevents ref count from going down to zero when
  2041. * there still are pending delayed ref.
  2042. */
  2043. node = rb_prev(&head->node.rb_node);
  2044. while (1) {
  2045. if (!node)
  2046. break;
  2047. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2048. rb_node);
  2049. if (ref->bytenr != head->node.bytenr)
  2050. break;
  2051. if (ref->action == action)
  2052. return ref;
  2053. node = rb_prev(node);
  2054. }
  2055. if (action == BTRFS_ADD_DELAYED_REF) {
  2056. action = BTRFS_DROP_DELAYED_REF;
  2057. goto again;
  2058. }
  2059. return NULL;
  2060. }
  2061. /*
  2062. * Returns 0 on success or if called with an already aborted transaction.
  2063. * Returns -ENOMEM or -EIO on failure and will abort the transaction.
  2064. */
  2065. static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
  2066. struct btrfs_root *root,
  2067. struct list_head *cluster)
  2068. {
  2069. struct btrfs_delayed_ref_root *delayed_refs;
  2070. struct btrfs_delayed_ref_node *ref;
  2071. struct btrfs_delayed_ref_head *locked_ref = NULL;
  2072. struct btrfs_delayed_extent_op *extent_op;
  2073. struct btrfs_fs_info *fs_info = root->fs_info;
  2074. int ret;
  2075. int count = 0;
  2076. int must_insert_reserved = 0;
  2077. delayed_refs = &trans->transaction->delayed_refs;
  2078. while (1) {
  2079. if (!locked_ref) {
  2080. /* pick a new head ref from the cluster list */
  2081. if (list_empty(cluster))
  2082. break;
  2083. locked_ref = list_entry(cluster->next,
  2084. struct btrfs_delayed_ref_head, cluster);
  2085. /* grab the lock that says we are going to process
  2086. * all the refs for this head */
  2087. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  2088. /*
  2089. * we may have dropped the spin lock to get the head
  2090. * mutex lock, and that might have given someone else
  2091. * time to free the head. If that's true, it has been
  2092. * removed from our list and we can move on.
  2093. */
  2094. if (ret == -EAGAIN) {
  2095. locked_ref = NULL;
  2096. count++;
  2097. continue;
  2098. }
  2099. }
  2100. /*
  2101. * We need to try and merge add/drops of the same ref since we
  2102. * can run into issues with relocate dropping the implicit ref
  2103. * and then it being added back again before the drop can
  2104. * finish. If we merged anything we need to re-loop so we can
  2105. * get a good ref.
  2106. */
  2107. btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
  2108. locked_ref);
  2109. /*
  2110. * locked_ref is the head node, so we have to go one
  2111. * node back for any delayed ref updates
  2112. */
  2113. ref = select_delayed_ref(locked_ref);
  2114. if (ref && ref->seq &&
  2115. btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
  2116. /*
  2117. * there are still refs with lower seq numbers in the
  2118. * process of being added. Don't run this ref yet.
  2119. */
  2120. list_del_init(&locked_ref->cluster);
  2121. btrfs_delayed_ref_unlock(locked_ref);
  2122. locked_ref = NULL;
  2123. delayed_refs->num_heads_ready++;
  2124. spin_unlock(&delayed_refs->lock);
  2125. cond_resched();
  2126. spin_lock(&delayed_refs->lock);
  2127. continue;
  2128. }
  2129. /*
  2130. * record the must insert reserved flag before we
  2131. * drop the spin lock.
  2132. */
  2133. must_insert_reserved = locked_ref->must_insert_reserved;
  2134. locked_ref->must_insert_reserved = 0;
  2135. extent_op = locked_ref->extent_op;
  2136. locked_ref->extent_op = NULL;
  2137. if (!ref) {
  2138. /* All delayed refs have been processed, Go ahead
  2139. * and send the head node to run_one_delayed_ref,
  2140. * so that any accounting fixes can happen
  2141. */
  2142. ref = &locked_ref->node;
  2143. if (extent_op && must_insert_reserved) {
  2144. btrfs_free_delayed_extent_op(extent_op);
  2145. extent_op = NULL;
  2146. }
  2147. if (extent_op) {
  2148. spin_unlock(&delayed_refs->lock);
  2149. ret = run_delayed_extent_op(trans, root,
  2150. ref, extent_op);
  2151. btrfs_free_delayed_extent_op(extent_op);
  2152. if (ret) {
  2153. btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
  2154. spin_lock(&delayed_refs->lock);
  2155. btrfs_delayed_ref_unlock(locked_ref);
  2156. return ret;
  2157. }
  2158. goto next;
  2159. }
  2160. }
  2161. ref->in_tree = 0;
  2162. rb_erase(&ref->rb_node, &delayed_refs->root);
  2163. delayed_refs->num_entries--;
  2164. if (!btrfs_delayed_ref_is_head(ref)) {
  2165. /*
  2166. * when we play the delayed ref, also correct the
  2167. * ref_mod on head
  2168. */
  2169. switch (ref->action) {
  2170. case BTRFS_ADD_DELAYED_REF:
  2171. case BTRFS_ADD_DELAYED_EXTENT:
  2172. locked_ref->node.ref_mod -= ref->ref_mod;
  2173. break;
  2174. case BTRFS_DROP_DELAYED_REF:
  2175. locked_ref->node.ref_mod += ref->ref_mod;
  2176. break;
  2177. default:
  2178. WARN_ON(1);
  2179. }
  2180. }
  2181. spin_unlock(&delayed_refs->lock);
  2182. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2183. must_insert_reserved);
  2184. btrfs_free_delayed_extent_op(extent_op);
  2185. if (ret) {
  2186. btrfs_delayed_ref_unlock(locked_ref);
  2187. btrfs_put_delayed_ref(ref);
  2188. btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
  2189. spin_lock(&delayed_refs->lock);
  2190. return ret;
  2191. }
  2192. /*
  2193. * If this node is a head, that means all the refs in this head
  2194. * have been dealt with, and we will pick the next head to deal
  2195. * with, so we must unlock the head and drop it from the cluster
  2196. * list before we release it.
  2197. */
  2198. if (btrfs_delayed_ref_is_head(ref)) {
  2199. list_del_init(&locked_ref->cluster);
  2200. btrfs_delayed_ref_unlock(locked_ref);
  2201. locked_ref = NULL;
  2202. }
  2203. btrfs_put_delayed_ref(ref);
  2204. count++;
  2205. next:
  2206. cond_resched();
  2207. spin_lock(&delayed_refs->lock);
  2208. }
  2209. return count;
  2210. }
  2211. #ifdef SCRAMBLE_DELAYED_REFS
  2212. /*
  2213. * Normally delayed refs get processed in ascending bytenr order. This
  2214. * correlates in most cases to the order added. To expose dependencies on this
  2215. * order, we start to process the tree in the middle instead of the beginning
  2216. */
  2217. static u64 find_middle(struct rb_root *root)
  2218. {
  2219. struct rb_node *n = root->rb_node;
  2220. struct btrfs_delayed_ref_node *entry;
  2221. int alt = 1;
  2222. u64 middle;
  2223. u64 first = 0, last = 0;
  2224. n = rb_first(root);
  2225. if (n) {
  2226. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2227. first = entry->bytenr;
  2228. }
  2229. n = rb_last(root);
  2230. if (n) {
  2231. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2232. last = entry->bytenr;
  2233. }
  2234. n = root->rb_node;
  2235. while (n) {
  2236. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2237. WARN_ON(!entry->in_tree);
  2238. middle = entry->bytenr;
  2239. if (alt)
  2240. n = n->rb_left;
  2241. else
  2242. n = n->rb_right;
  2243. alt = 1 - alt;
  2244. }
  2245. return middle;
  2246. }
  2247. #endif
  2248. int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
  2249. struct btrfs_fs_info *fs_info)
  2250. {
  2251. struct qgroup_update *qgroup_update;
  2252. int ret = 0;
  2253. if (list_empty(&trans->qgroup_ref_list) !=
  2254. !trans->delayed_ref_elem.seq) {
  2255. /* list without seq or seq without list */
  2256. btrfs_err(fs_info,
  2257. "qgroup accounting update error, list is%s empty, seq is %#x.%x",
  2258. list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2259. (u32)(trans->delayed_ref_elem.seq >> 32),
  2260. (u32)trans->delayed_ref_elem.seq);
  2261. BUG();
  2262. }
  2263. if (!trans->delayed_ref_elem.seq)
  2264. return 0;
  2265. while (!list_empty(&trans->qgroup_ref_list)) {
  2266. qgroup_update = list_first_entry(&trans->qgroup_ref_list,
  2267. struct qgroup_update, list);
  2268. list_del(&qgroup_update->list);
  2269. if (!ret)
  2270. ret = btrfs_qgroup_account_ref(
  2271. trans, fs_info, qgroup_update->node,
  2272. qgroup_update->extent_op);
  2273. kfree(qgroup_update);
  2274. }
  2275. btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  2276. return ret;
  2277. }
  2278. static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
  2279. int count)
  2280. {
  2281. int val = atomic_read(&delayed_refs->ref_seq);
  2282. if (val < seq || val >= seq + count)
  2283. return 1;
  2284. return 0;
  2285. }
  2286. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2287. {
  2288. u64 num_bytes;
  2289. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2290. sizeof(struct btrfs_extent_inline_ref));
  2291. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2292. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2293. /*
  2294. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2295. * closer to what we're really going to want to ouse.
  2296. */
  2297. return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2298. }
  2299. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2300. struct btrfs_root *root)
  2301. {
  2302. struct btrfs_block_rsv *global_rsv;
  2303. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2304. u64 num_bytes;
  2305. int ret = 0;
  2306. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2307. num_heads = heads_to_leaves(root, num_heads);
  2308. if (num_heads > 1)
  2309. num_bytes += (num_heads - 1) * root->leafsize;
  2310. num_bytes <<= 1;
  2311. global_rsv = &root->fs_info->global_block_rsv;
  2312. /*
  2313. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2314. * wiggle room since running delayed refs can create more delayed refs.
  2315. */
  2316. if (global_rsv->space_info->full)
  2317. num_bytes <<= 1;
  2318. spin_lock(&global_rsv->lock);
  2319. if (global_rsv->reserved <= num_bytes)
  2320. ret = 1;
  2321. spin_unlock(&global_rsv->lock);
  2322. return ret;
  2323. }
  2324. /*
  2325. * this starts processing the delayed reference count updates and
  2326. * extent insertions we have queued up so far. count can be
  2327. * 0, which means to process everything in the tree at the start
  2328. * of the run (but not newly added entries), or it can be some target
  2329. * number you'd like to process.
  2330. *
  2331. * Returns 0 on success or if called with an aborted transaction
  2332. * Returns <0 on error and aborts the transaction
  2333. */
  2334. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2335. struct btrfs_root *root, unsigned long count)
  2336. {
  2337. struct rb_node *node;
  2338. struct btrfs_delayed_ref_root *delayed_refs;
  2339. struct btrfs_delayed_ref_node *ref;
  2340. struct list_head cluster;
  2341. int ret;
  2342. u64 delayed_start;
  2343. int run_all = count == (unsigned long)-1;
  2344. int run_most = 0;
  2345. int loops;
  2346. /* We'll clean this up in btrfs_cleanup_transaction */
  2347. if (trans->aborted)
  2348. return 0;
  2349. if (root == root->fs_info->extent_root)
  2350. root = root->fs_info->tree_root;
  2351. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  2352. delayed_refs = &trans->transaction->delayed_refs;
  2353. INIT_LIST_HEAD(&cluster);
  2354. if (count == 0) {
  2355. count = delayed_refs->num_entries * 2;
  2356. run_most = 1;
  2357. }
  2358. if (!run_all && !run_most) {
  2359. int old;
  2360. int seq = atomic_read(&delayed_refs->ref_seq);
  2361. progress:
  2362. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2363. if (old) {
  2364. DEFINE_WAIT(__wait);
  2365. if (delayed_refs->flushing ||
  2366. !btrfs_should_throttle_delayed_refs(trans, root))
  2367. return 0;
  2368. prepare_to_wait(&delayed_refs->wait, &__wait,
  2369. TASK_UNINTERRUPTIBLE);
  2370. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2371. if (old) {
  2372. schedule();
  2373. finish_wait(&delayed_refs->wait, &__wait);
  2374. if (!refs_newer(delayed_refs, seq, 256))
  2375. goto progress;
  2376. else
  2377. return 0;
  2378. } else {
  2379. finish_wait(&delayed_refs->wait, &__wait);
  2380. goto again;
  2381. }
  2382. }
  2383. } else {
  2384. atomic_inc(&delayed_refs->procs_running_refs);
  2385. }
  2386. again:
  2387. loops = 0;
  2388. spin_lock(&delayed_refs->lock);
  2389. #ifdef SCRAMBLE_DELAYED_REFS
  2390. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2391. #endif
  2392. while (1) {
  2393. if (!(run_all || run_most) &&
  2394. !btrfs_should_throttle_delayed_refs(trans, root))
  2395. break;
  2396. /*
  2397. * go find something we can process in the rbtree. We start at
  2398. * the beginning of the tree, and then build a cluster
  2399. * of refs to process starting at the first one we are able to
  2400. * lock
  2401. */
  2402. delayed_start = delayed_refs->run_delayed_start;
  2403. ret = btrfs_find_ref_cluster(trans, &cluster,
  2404. delayed_refs->run_delayed_start);
  2405. if (ret)
  2406. break;
  2407. ret = run_clustered_refs(trans, root, &cluster);
  2408. if (ret < 0) {
  2409. btrfs_release_ref_cluster(&cluster);
  2410. spin_unlock(&delayed_refs->lock);
  2411. btrfs_abort_transaction(trans, root, ret);
  2412. atomic_dec(&delayed_refs->procs_running_refs);
  2413. wake_up(&delayed_refs->wait);
  2414. return ret;
  2415. }
  2416. atomic_add(ret, &delayed_refs->ref_seq);
  2417. count -= min_t(unsigned long, ret, count);
  2418. if (count == 0)
  2419. break;
  2420. if (delayed_start >= delayed_refs->run_delayed_start) {
  2421. if (loops == 0) {
  2422. /*
  2423. * btrfs_find_ref_cluster looped. let's do one
  2424. * more cycle. if we don't run any delayed ref
  2425. * during that cycle (because we can't because
  2426. * all of them are blocked), bail out.
  2427. */
  2428. loops = 1;
  2429. } else {
  2430. /*
  2431. * no runnable refs left, stop trying
  2432. */
  2433. BUG_ON(run_all);
  2434. break;
  2435. }
  2436. }
  2437. if (ret) {
  2438. /* refs were run, let's reset staleness detection */
  2439. loops = 0;
  2440. }
  2441. }
  2442. if (run_all) {
  2443. if (!list_empty(&trans->new_bgs)) {
  2444. spin_unlock(&delayed_refs->lock);
  2445. btrfs_create_pending_block_groups(trans, root);
  2446. spin_lock(&delayed_refs->lock);
  2447. }
  2448. node = rb_first(&delayed_refs->root);
  2449. if (!node)
  2450. goto out;
  2451. count = (unsigned long)-1;
  2452. while (node) {
  2453. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2454. rb_node);
  2455. if (btrfs_delayed_ref_is_head(ref)) {
  2456. struct btrfs_delayed_ref_head *head;
  2457. head = btrfs_delayed_node_to_head(ref);
  2458. atomic_inc(&ref->refs);
  2459. spin_unlock(&delayed_refs->lock);
  2460. /*
  2461. * Mutex was contended, block until it's
  2462. * released and try again
  2463. */
  2464. mutex_lock(&head->mutex);
  2465. mutex_unlock(&head->mutex);
  2466. btrfs_put_delayed_ref(ref);
  2467. cond_resched();
  2468. goto again;
  2469. }
  2470. node = rb_next(node);
  2471. }
  2472. spin_unlock(&delayed_refs->lock);
  2473. schedule_timeout(1);
  2474. goto again;
  2475. }
  2476. out:
  2477. atomic_dec(&delayed_refs->procs_running_refs);
  2478. smp_mb();
  2479. if (waitqueue_active(&delayed_refs->wait))
  2480. wake_up(&delayed_refs->wait);
  2481. spin_unlock(&delayed_refs->lock);
  2482. assert_qgroups_uptodate(trans);
  2483. return 0;
  2484. }
  2485. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2486. struct btrfs_root *root,
  2487. u64 bytenr, u64 num_bytes, u64 flags,
  2488. int level, int is_data)
  2489. {
  2490. struct btrfs_delayed_extent_op *extent_op;
  2491. int ret;
  2492. extent_op = btrfs_alloc_delayed_extent_op();
  2493. if (!extent_op)
  2494. return -ENOMEM;
  2495. extent_op->flags_to_set = flags;
  2496. extent_op->update_flags = 1;
  2497. extent_op->update_key = 0;
  2498. extent_op->is_data = is_data ? 1 : 0;
  2499. extent_op->level = level;
  2500. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2501. num_bytes, extent_op);
  2502. if (ret)
  2503. btrfs_free_delayed_extent_op(extent_op);
  2504. return ret;
  2505. }
  2506. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2507. struct btrfs_root *root,
  2508. struct btrfs_path *path,
  2509. u64 objectid, u64 offset, u64 bytenr)
  2510. {
  2511. struct btrfs_delayed_ref_head *head;
  2512. struct btrfs_delayed_ref_node *ref;
  2513. struct btrfs_delayed_data_ref *data_ref;
  2514. struct btrfs_delayed_ref_root *delayed_refs;
  2515. struct rb_node *node;
  2516. int ret = 0;
  2517. ret = -ENOENT;
  2518. delayed_refs = &trans->transaction->delayed_refs;
  2519. spin_lock(&delayed_refs->lock);
  2520. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2521. if (!head)
  2522. goto out;
  2523. if (!mutex_trylock(&head->mutex)) {
  2524. atomic_inc(&head->node.refs);
  2525. spin_unlock(&delayed_refs->lock);
  2526. btrfs_release_path(path);
  2527. /*
  2528. * Mutex was contended, block until it's released and let
  2529. * caller try again
  2530. */
  2531. mutex_lock(&head->mutex);
  2532. mutex_unlock(&head->mutex);
  2533. btrfs_put_delayed_ref(&head->node);
  2534. return -EAGAIN;
  2535. }
  2536. node = rb_prev(&head->node.rb_node);
  2537. if (!node)
  2538. goto out_unlock;
  2539. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2540. if (ref->bytenr != bytenr)
  2541. goto out_unlock;
  2542. ret = 1;
  2543. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2544. goto out_unlock;
  2545. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2546. node = rb_prev(node);
  2547. if (node) {
  2548. int seq = ref->seq;
  2549. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2550. if (ref->bytenr == bytenr && ref->seq == seq)
  2551. goto out_unlock;
  2552. }
  2553. if (data_ref->root != root->root_key.objectid ||
  2554. data_ref->objectid != objectid || data_ref->offset != offset)
  2555. goto out_unlock;
  2556. ret = 0;
  2557. out_unlock:
  2558. mutex_unlock(&head->mutex);
  2559. out:
  2560. spin_unlock(&delayed_refs->lock);
  2561. return ret;
  2562. }
  2563. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2564. struct btrfs_root *root,
  2565. struct btrfs_path *path,
  2566. u64 objectid, u64 offset, u64 bytenr)
  2567. {
  2568. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2569. struct extent_buffer *leaf;
  2570. struct btrfs_extent_data_ref *ref;
  2571. struct btrfs_extent_inline_ref *iref;
  2572. struct btrfs_extent_item *ei;
  2573. struct btrfs_key key;
  2574. u32 item_size;
  2575. int ret;
  2576. key.objectid = bytenr;
  2577. key.offset = (u64)-1;
  2578. key.type = BTRFS_EXTENT_ITEM_KEY;
  2579. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2580. if (ret < 0)
  2581. goto out;
  2582. BUG_ON(ret == 0); /* Corruption */
  2583. ret = -ENOENT;
  2584. if (path->slots[0] == 0)
  2585. goto out;
  2586. path->slots[0]--;
  2587. leaf = path->nodes[0];
  2588. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2589. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2590. goto out;
  2591. ret = 1;
  2592. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2593. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2594. if (item_size < sizeof(*ei)) {
  2595. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2596. goto out;
  2597. }
  2598. #endif
  2599. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2600. if (item_size != sizeof(*ei) +
  2601. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2602. goto out;
  2603. if (btrfs_extent_generation(leaf, ei) <=
  2604. btrfs_root_last_snapshot(&root->root_item))
  2605. goto out;
  2606. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2607. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2608. BTRFS_EXTENT_DATA_REF_KEY)
  2609. goto out;
  2610. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2611. if (btrfs_extent_refs(leaf, ei) !=
  2612. btrfs_extent_data_ref_count(leaf, ref) ||
  2613. btrfs_extent_data_ref_root(leaf, ref) !=
  2614. root->root_key.objectid ||
  2615. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2616. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2617. goto out;
  2618. ret = 0;
  2619. out:
  2620. return ret;
  2621. }
  2622. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2623. struct btrfs_root *root,
  2624. u64 objectid, u64 offset, u64 bytenr)
  2625. {
  2626. struct btrfs_path *path;
  2627. int ret;
  2628. int ret2;
  2629. path = btrfs_alloc_path();
  2630. if (!path)
  2631. return -ENOENT;
  2632. do {
  2633. ret = check_committed_ref(trans, root, path, objectid,
  2634. offset, bytenr);
  2635. if (ret && ret != -ENOENT)
  2636. goto out;
  2637. ret2 = check_delayed_ref(trans, root, path, objectid,
  2638. offset, bytenr);
  2639. } while (ret2 == -EAGAIN);
  2640. if (ret2 && ret2 != -ENOENT) {
  2641. ret = ret2;
  2642. goto out;
  2643. }
  2644. if (ret != -ENOENT || ret2 != -ENOENT)
  2645. ret = 0;
  2646. out:
  2647. btrfs_free_path(path);
  2648. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2649. WARN_ON(ret > 0);
  2650. return ret;
  2651. }
  2652. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2653. struct btrfs_root *root,
  2654. struct extent_buffer *buf,
  2655. int full_backref, int inc, int for_cow)
  2656. {
  2657. u64 bytenr;
  2658. u64 num_bytes;
  2659. u64 parent;
  2660. u64 ref_root;
  2661. u32 nritems;
  2662. struct btrfs_key key;
  2663. struct btrfs_file_extent_item *fi;
  2664. int i;
  2665. int level;
  2666. int ret = 0;
  2667. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2668. u64, u64, u64, u64, u64, u64, int);
  2669. ref_root = btrfs_header_owner(buf);
  2670. nritems = btrfs_header_nritems(buf);
  2671. level = btrfs_header_level(buf);
  2672. if (!root->ref_cows && level == 0)
  2673. return 0;
  2674. if (inc)
  2675. process_func = btrfs_inc_extent_ref;
  2676. else
  2677. process_func = btrfs_free_extent;
  2678. if (full_backref)
  2679. parent = buf->start;
  2680. else
  2681. parent = 0;
  2682. for (i = 0; i < nritems; i++) {
  2683. if (level == 0) {
  2684. btrfs_item_key_to_cpu(buf, &key, i);
  2685. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2686. continue;
  2687. fi = btrfs_item_ptr(buf, i,
  2688. struct btrfs_file_extent_item);
  2689. if (btrfs_file_extent_type(buf, fi) ==
  2690. BTRFS_FILE_EXTENT_INLINE)
  2691. continue;
  2692. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2693. if (bytenr == 0)
  2694. continue;
  2695. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2696. key.offset -= btrfs_file_extent_offset(buf, fi);
  2697. ret = process_func(trans, root, bytenr, num_bytes,
  2698. parent, ref_root, key.objectid,
  2699. key.offset, for_cow);
  2700. if (ret)
  2701. goto fail;
  2702. } else {
  2703. bytenr = btrfs_node_blockptr(buf, i);
  2704. num_bytes = btrfs_level_size(root, level - 1);
  2705. ret = process_func(trans, root, bytenr, num_bytes,
  2706. parent, ref_root, level - 1, 0,
  2707. for_cow);
  2708. if (ret)
  2709. goto fail;
  2710. }
  2711. }
  2712. return 0;
  2713. fail:
  2714. return ret;
  2715. }
  2716. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2717. struct extent_buffer *buf, int full_backref, int for_cow)
  2718. {
  2719. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2720. }
  2721. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2722. struct extent_buffer *buf, int full_backref, int for_cow)
  2723. {
  2724. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2725. }
  2726. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2727. struct btrfs_root *root,
  2728. struct btrfs_path *path,
  2729. struct btrfs_block_group_cache *cache)
  2730. {
  2731. int ret;
  2732. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2733. unsigned long bi;
  2734. struct extent_buffer *leaf;
  2735. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2736. if (ret < 0)
  2737. goto fail;
  2738. BUG_ON(ret); /* Corruption */
  2739. leaf = path->nodes[0];
  2740. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2741. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2742. btrfs_mark_buffer_dirty(leaf);
  2743. btrfs_release_path(path);
  2744. fail:
  2745. if (ret) {
  2746. btrfs_abort_transaction(trans, root, ret);
  2747. return ret;
  2748. }
  2749. return 0;
  2750. }
  2751. static struct btrfs_block_group_cache *
  2752. next_block_group(struct btrfs_root *root,
  2753. struct btrfs_block_group_cache *cache)
  2754. {
  2755. struct rb_node *node;
  2756. spin_lock(&root->fs_info->block_group_cache_lock);
  2757. node = rb_next(&cache->cache_node);
  2758. btrfs_put_block_group(cache);
  2759. if (node) {
  2760. cache = rb_entry(node, struct btrfs_block_group_cache,
  2761. cache_node);
  2762. btrfs_get_block_group(cache);
  2763. } else
  2764. cache = NULL;
  2765. spin_unlock(&root->fs_info->block_group_cache_lock);
  2766. return cache;
  2767. }
  2768. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2769. struct btrfs_trans_handle *trans,
  2770. struct btrfs_path *path)
  2771. {
  2772. struct btrfs_root *root = block_group->fs_info->tree_root;
  2773. struct inode *inode = NULL;
  2774. u64 alloc_hint = 0;
  2775. int dcs = BTRFS_DC_ERROR;
  2776. int num_pages = 0;
  2777. int retries = 0;
  2778. int ret = 0;
  2779. /*
  2780. * If this block group is smaller than 100 megs don't bother caching the
  2781. * block group.
  2782. */
  2783. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2784. spin_lock(&block_group->lock);
  2785. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2786. spin_unlock(&block_group->lock);
  2787. return 0;
  2788. }
  2789. again:
  2790. inode = lookup_free_space_inode(root, block_group, path);
  2791. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2792. ret = PTR_ERR(inode);
  2793. btrfs_release_path(path);
  2794. goto out;
  2795. }
  2796. if (IS_ERR(inode)) {
  2797. BUG_ON(retries);
  2798. retries++;
  2799. if (block_group->ro)
  2800. goto out_free;
  2801. ret = create_free_space_inode(root, trans, block_group, path);
  2802. if (ret)
  2803. goto out_free;
  2804. goto again;
  2805. }
  2806. /* We've already setup this transaction, go ahead and exit */
  2807. if (block_group->cache_generation == trans->transid &&
  2808. i_size_read(inode)) {
  2809. dcs = BTRFS_DC_SETUP;
  2810. goto out_put;
  2811. }
  2812. /*
  2813. * We want to set the generation to 0, that way if anything goes wrong
  2814. * from here on out we know not to trust this cache when we load up next
  2815. * time.
  2816. */
  2817. BTRFS_I(inode)->generation = 0;
  2818. ret = btrfs_update_inode(trans, root, inode);
  2819. WARN_ON(ret);
  2820. if (i_size_read(inode) > 0) {
  2821. ret = btrfs_check_trunc_cache_free_space(root,
  2822. &root->fs_info->global_block_rsv);
  2823. if (ret)
  2824. goto out_put;
  2825. ret = btrfs_truncate_free_space_cache(root, trans, path,
  2826. inode);
  2827. if (ret)
  2828. goto out_put;
  2829. }
  2830. spin_lock(&block_group->lock);
  2831. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2832. !btrfs_test_opt(root, SPACE_CACHE)) {
  2833. /*
  2834. * don't bother trying to write stuff out _if_
  2835. * a) we're not cached,
  2836. * b) we're with nospace_cache mount option.
  2837. */
  2838. dcs = BTRFS_DC_WRITTEN;
  2839. spin_unlock(&block_group->lock);
  2840. goto out_put;
  2841. }
  2842. spin_unlock(&block_group->lock);
  2843. /*
  2844. * Try to preallocate enough space based on how big the block group is.
  2845. * Keep in mind this has to include any pinned space which could end up
  2846. * taking up quite a bit since it's not folded into the other space
  2847. * cache.
  2848. */
  2849. num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
  2850. if (!num_pages)
  2851. num_pages = 1;
  2852. num_pages *= 16;
  2853. num_pages *= PAGE_CACHE_SIZE;
  2854. ret = btrfs_check_data_free_space(inode, num_pages);
  2855. if (ret)
  2856. goto out_put;
  2857. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2858. num_pages, num_pages,
  2859. &alloc_hint);
  2860. if (!ret)
  2861. dcs = BTRFS_DC_SETUP;
  2862. btrfs_free_reserved_data_space(inode, num_pages);
  2863. out_put:
  2864. iput(inode);
  2865. out_free:
  2866. btrfs_release_path(path);
  2867. out:
  2868. spin_lock(&block_group->lock);
  2869. if (!ret && dcs == BTRFS_DC_SETUP)
  2870. block_group->cache_generation = trans->transid;
  2871. block_group->disk_cache_state = dcs;
  2872. spin_unlock(&block_group->lock);
  2873. return ret;
  2874. }
  2875. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2876. struct btrfs_root *root)
  2877. {
  2878. struct btrfs_block_group_cache *cache;
  2879. int err = 0;
  2880. struct btrfs_path *path;
  2881. u64 last = 0;
  2882. path = btrfs_alloc_path();
  2883. if (!path)
  2884. return -ENOMEM;
  2885. again:
  2886. while (1) {
  2887. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2888. while (cache) {
  2889. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2890. break;
  2891. cache = next_block_group(root, cache);
  2892. }
  2893. if (!cache) {
  2894. if (last == 0)
  2895. break;
  2896. last = 0;
  2897. continue;
  2898. }
  2899. err = cache_save_setup(cache, trans, path);
  2900. last = cache->key.objectid + cache->key.offset;
  2901. btrfs_put_block_group(cache);
  2902. }
  2903. while (1) {
  2904. if (last == 0) {
  2905. err = btrfs_run_delayed_refs(trans, root,
  2906. (unsigned long)-1);
  2907. if (err) /* File system offline */
  2908. goto out;
  2909. }
  2910. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2911. while (cache) {
  2912. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2913. btrfs_put_block_group(cache);
  2914. goto again;
  2915. }
  2916. if (cache->dirty)
  2917. break;
  2918. cache = next_block_group(root, cache);
  2919. }
  2920. if (!cache) {
  2921. if (last == 0)
  2922. break;
  2923. last = 0;
  2924. continue;
  2925. }
  2926. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2927. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2928. cache->dirty = 0;
  2929. last = cache->key.objectid + cache->key.offset;
  2930. err = write_one_cache_group(trans, root, path, cache);
  2931. if (err) /* File system offline */
  2932. goto out;
  2933. btrfs_put_block_group(cache);
  2934. }
  2935. while (1) {
  2936. /*
  2937. * I don't think this is needed since we're just marking our
  2938. * preallocated extent as written, but just in case it can't
  2939. * hurt.
  2940. */
  2941. if (last == 0) {
  2942. err = btrfs_run_delayed_refs(trans, root,
  2943. (unsigned long)-1);
  2944. if (err) /* File system offline */
  2945. goto out;
  2946. }
  2947. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2948. while (cache) {
  2949. /*
  2950. * Really this shouldn't happen, but it could if we
  2951. * couldn't write the entire preallocated extent and
  2952. * splitting the extent resulted in a new block.
  2953. */
  2954. if (cache->dirty) {
  2955. btrfs_put_block_group(cache);
  2956. goto again;
  2957. }
  2958. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2959. break;
  2960. cache = next_block_group(root, cache);
  2961. }
  2962. if (!cache) {
  2963. if (last == 0)
  2964. break;
  2965. last = 0;
  2966. continue;
  2967. }
  2968. err = btrfs_write_out_cache(root, trans, cache, path);
  2969. /*
  2970. * If we didn't have an error then the cache state is still
  2971. * NEED_WRITE, so we can set it to WRITTEN.
  2972. */
  2973. if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2974. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2975. last = cache->key.objectid + cache->key.offset;
  2976. btrfs_put_block_group(cache);
  2977. }
  2978. out:
  2979. btrfs_free_path(path);
  2980. return err;
  2981. }
  2982. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2983. {
  2984. struct btrfs_block_group_cache *block_group;
  2985. int readonly = 0;
  2986. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2987. if (!block_group || block_group->ro)
  2988. readonly = 1;
  2989. if (block_group)
  2990. btrfs_put_block_group(block_group);
  2991. return readonly;
  2992. }
  2993. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2994. u64 total_bytes, u64 bytes_used,
  2995. struct btrfs_space_info **space_info)
  2996. {
  2997. struct btrfs_space_info *found;
  2998. int i;
  2999. int factor;
  3000. int ret;
  3001. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3002. BTRFS_BLOCK_GROUP_RAID10))
  3003. factor = 2;
  3004. else
  3005. factor = 1;
  3006. found = __find_space_info(info, flags);
  3007. if (found) {
  3008. spin_lock(&found->lock);
  3009. found->total_bytes += total_bytes;
  3010. found->disk_total += total_bytes * factor;
  3011. found->bytes_used += bytes_used;
  3012. found->disk_used += bytes_used * factor;
  3013. found->full = 0;
  3014. spin_unlock(&found->lock);
  3015. *space_info = found;
  3016. return 0;
  3017. }
  3018. found = kzalloc(sizeof(*found), GFP_NOFS);
  3019. if (!found)
  3020. return -ENOMEM;
  3021. ret = percpu_counter_init(&found->total_bytes_pinned, 0);
  3022. if (ret) {
  3023. kfree(found);
  3024. return ret;
  3025. }
  3026. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  3027. INIT_LIST_HEAD(&found->block_groups[i]);
  3028. init_rwsem(&found->groups_sem);
  3029. spin_lock_init(&found->lock);
  3030. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3031. found->total_bytes = total_bytes;
  3032. found->disk_total = total_bytes * factor;
  3033. found->bytes_used = bytes_used;
  3034. found->disk_used = bytes_used * factor;
  3035. found->bytes_pinned = 0;
  3036. found->bytes_reserved = 0;
  3037. found->bytes_readonly = 0;
  3038. found->bytes_may_use = 0;
  3039. found->full = 0;
  3040. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3041. found->chunk_alloc = 0;
  3042. found->flush = 0;
  3043. init_waitqueue_head(&found->wait);
  3044. *space_info = found;
  3045. list_add_rcu(&found->list, &info->space_info);
  3046. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3047. info->data_sinfo = found;
  3048. return 0;
  3049. }
  3050. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3051. {
  3052. u64 extra_flags = chunk_to_extended(flags) &
  3053. BTRFS_EXTENDED_PROFILE_MASK;
  3054. write_seqlock(&fs_info->profiles_lock);
  3055. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3056. fs_info->avail_data_alloc_bits |= extra_flags;
  3057. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3058. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3059. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3060. fs_info->avail_system_alloc_bits |= extra_flags;
  3061. write_sequnlock(&fs_info->profiles_lock);
  3062. }
  3063. /*
  3064. * returns target flags in extended format or 0 if restripe for this
  3065. * chunk_type is not in progress
  3066. *
  3067. * should be called with either volume_mutex or balance_lock held
  3068. */
  3069. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3070. {
  3071. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3072. u64 target = 0;
  3073. if (!bctl)
  3074. return 0;
  3075. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3076. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3077. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3078. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3079. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3080. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3081. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3082. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3083. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3084. }
  3085. return target;
  3086. }
  3087. /*
  3088. * @flags: available profiles in extended format (see ctree.h)
  3089. *
  3090. * Returns reduced profile in chunk format. If profile changing is in
  3091. * progress (either running or paused) picks the target profile (if it's
  3092. * already available), otherwise falls back to plain reducing.
  3093. */
  3094. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3095. {
  3096. /*
  3097. * we add in the count of missing devices because we want
  3098. * to make sure that any RAID levels on a degraded FS
  3099. * continue to be honored.
  3100. */
  3101. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  3102. root->fs_info->fs_devices->missing_devices;
  3103. u64 target;
  3104. u64 tmp;
  3105. /*
  3106. * see if restripe for this chunk_type is in progress, if so
  3107. * try to reduce to the target profile
  3108. */
  3109. spin_lock(&root->fs_info->balance_lock);
  3110. target = get_restripe_target(root->fs_info, flags);
  3111. if (target) {
  3112. /* pick target profile only if it's already available */
  3113. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3114. spin_unlock(&root->fs_info->balance_lock);
  3115. return extended_to_chunk(target);
  3116. }
  3117. }
  3118. spin_unlock(&root->fs_info->balance_lock);
  3119. /* First, mask out the RAID levels which aren't possible */
  3120. if (num_devices == 1)
  3121. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3122. BTRFS_BLOCK_GROUP_RAID5);
  3123. if (num_devices < 3)
  3124. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3125. if (num_devices < 4)
  3126. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3127. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3128. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3129. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3130. flags &= ~tmp;
  3131. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3132. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3133. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3134. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3135. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3136. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3137. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3138. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3139. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3140. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3141. return extended_to_chunk(flags | tmp);
  3142. }
  3143. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  3144. {
  3145. unsigned seq;
  3146. do {
  3147. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3148. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3149. flags |= root->fs_info->avail_data_alloc_bits;
  3150. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3151. flags |= root->fs_info->avail_system_alloc_bits;
  3152. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3153. flags |= root->fs_info->avail_metadata_alloc_bits;
  3154. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3155. return btrfs_reduce_alloc_profile(root, flags);
  3156. }
  3157. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3158. {
  3159. u64 flags;
  3160. u64 ret;
  3161. if (data)
  3162. flags = BTRFS_BLOCK_GROUP_DATA;
  3163. else if (root == root->fs_info->chunk_root)
  3164. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3165. else
  3166. flags = BTRFS_BLOCK_GROUP_METADATA;
  3167. ret = get_alloc_profile(root, flags);
  3168. return ret;
  3169. }
  3170. /*
  3171. * This will check the space that the inode allocates from to make sure we have
  3172. * enough space for bytes.
  3173. */
  3174. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  3175. {
  3176. struct btrfs_space_info *data_sinfo;
  3177. struct btrfs_root *root = BTRFS_I(inode)->root;
  3178. struct btrfs_fs_info *fs_info = root->fs_info;
  3179. u64 used;
  3180. int ret = 0, committed = 0, alloc_chunk = 1;
  3181. /* make sure bytes are sectorsize aligned */
  3182. bytes = ALIGN(bytes, root->sectorsize);
  3183. if (root == root->fs_info->tree_root ||
  3184. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  3185. alloc_chunk = 0;
  3186. committed = 1;
  3187. }
  3188. data_sinfo = fs_info->data_sinfo;
  3189. if (!data_sinfo)
  3190. goto alloc;
  3191. again:
  3192. /* make sure we have enough space to handle the data first */
  3193. spin_lock(&data_sinfo->lock);
  3194. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3195. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3196. data_sinfo->bytes_may_use;
  3197. if (used + bytes > data_sinfo->total_bytes) {
  3198. struct btrfs_trans_handle *trans;
  3199. /*
  3200. * if we don't have enough free bytes in this space then we need
  3201. * to alloc a new chunk.
  3202. */
  3203. if (!data_sinfo->full && alloc_chunk) {
  3204. u64 alloc_target;
  3205. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3206. spin_unlock(&data_sinfo->lock);
  3207. alloc:
  3208. alloc_target = btrfs_get_alloc_profile(root, 1);
  3209. trans = btrfs_join_transaction(root);
  3210. if (IS_ERR(trans))
  3211. return PTR_ERR(trans);
  3212. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3213. alloc_target,
  3214. CHUNK_ALLOC_NO_FORCE);
  3215. btrfs_end_transaction(trans, root);
  3216. if (ret < 0) {
  3217. if (ret != -ENOSPC)
  3218. return ret;
  3219. else
  3220. goto commit_trans;
  3221. }
  3222. if (!data_sinfo)
  3223. data_sinfo = fs_info->data_sinfo;
  3224. goto again;
  3225. }
  3226. /*
  3227. * If we don't have enough pinned space to deal with this
  3228. * allocation don't bother committing the transaction.
  3229. */
  3230. if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
  3231. bytes) < 0)
  3232. committed = 1;
  3233. spin_unlock(&data_sinfo->lock);
  3234. /* commit the current transaction and try again */
  3235. commit_trans:
  3236. if (!committed &&
  3237. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3238. committed = 1;
  3239. trans = btrfs_join_transaction(root);
  3240. if (IS_ERR(trans))
  3241. return PTR_ERR(trans);
  3242. ret = btrfs_commit_transaction(trans, root);
  3243. if (ret)
  3244. return ret;
  3245. goto again;
  3246. }
  3247. return -ENOSPC;
  3248. }
  3249. data_sinfo->bytes_may_use += bytes;
  3250. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3251. data_sinfo->flags, bytes, 1);
  3252. spin_unlock(&data_sinfo->lock);
  3253. return 0;
  3254. }
  3255. /*
  3256. * Called if we need to clear a data reservation for this inode.
  3257. */
  3258. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3259. {
  3260. struct btrfs_root *root = BTRFS_I(inode)->root;
  3261. struct btrfs_space_info *data_sinfo;
  3262. /* make sure bytes are sectorsize aligned */
  3263. bytes = ALIGN(bytes, root->sectorsize);
  3264. data_sinfo = root->fs_info->data_sinfo;
  3265. spin_lock(&data_sinfo->lock);
  3266. WARN_ON(data_sinfo->bytes_may_use < bytes);
  3267. data_sinfo->bytes_may_use -= bytes;
  3268. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3269. data_sinfo->flags, bytes, 0);
  3270. spin_unlock(&data_sinfo->lock);
  3271. }
  3272. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3273. {
  3274. struct list_head *head = &info->space_info;
  3275. struct btrfs_space_info *found;
  3276. rcu_read_lock();
  3277. list_for_each_entry_rcu(found, head, list) {
  3278. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3279. found->force_alloc = CHUNK_ALLOC_FORCE;
  3280. }
  3281. rcu_read_unlock();
  3282. }
  3283. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3284. {
  3285. return (global->size << 1);
  3286. }
  3287. static int should_alloc_chunk(struct btrfs_root *root,
  3288. struct btrfs_space_info *sinfo, int force)
  3289. {
  3290. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3291. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3292. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3293. u64 thresh;
  3294. if (force == CHUNK_ALLOC_FORCE)
  3295. return 1;
  3296. /*
  3297. * We need to take into account the global rsv because for all intents
  3298. * and purposes it's used space. Don't worry about locking the
  3299. * global_rsv, it doesn't change except when the transaction commits.
  3300. */
  3301. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3302. num_allocated += calc_global_rsv_need_space(global_rsv);
  3303. /*
  3304. * in limited mode, we want to have some free space up to
  3305. * about 1% of the FS size.
  3306. */
  3307. if (force == CHUNK_ALLOC_LIMITED) {
  3308. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3309. thresh = max_t(u64, 64 * 1024 * 1024,
  3310. div_factor_fine(thresh, 1));
  3311. if (num_bytes - num_allocated < thresh)
  3312. return 1;
  3313. }
  3314. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3315. return 0;
  3316. return 1;
  3317. }
  3318. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3319. {
  3320. u64 num_dev;
  3321. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3322. BTRFS_BLOCK_GROUP_RAID0 |
  3323. BTRFS_BLOCK_GROUP_RAID5 |
  3324. BTRFS_BLOCK_GROUP_RAID6))
  3325. num_dev = root->fs_info->fs_devices->rw_devices;
  3326. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3327. num_dev = 2;
  3328. else
  3329. num_dev = 1; /* DUP or single */
  3330. /* metadata for updaing devices and chunk tree */
  3331. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3332. }
  3333. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3334. struct btrfs_root *root, u64 type)
  3335. {
  3336. struct btrfs_space_info *info;
  3337. u64 left;
  3338. u64 thresh;
  3339. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3340. spin_lock(&info->lock);
  3341. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3342. info->bytes_reserved - info->bytes_readonly;
  3343. spin_unlock(&info->lock);
  3344. thresh = get_system_chunk_thresh(root, type);
  3345. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3346. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3347. left, thresh, type);
  3348. dump_space_info(info, 0, 0);
  3349. }
  3350. if (left < thresh) {
  3351. u64 flags;
  3352. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3353. btrfs_alloc_chunk(trans, root, flags);
  3354. }
  3355. }
  3356. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3357. struct btrfs_root *extent_root, u64 flags, int force)
  3358. {
  3359. struct btrfs_space_info *space_info;
  3360. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3361. int wait_for_alloc = 0;
  3362. int ret = 0;
  3363. /* Don't re-enter if we're already allocating a chunk */
  3364. if (trans->allocating_chunk)
  3365. return -ENOSPC;
  3366. space_info = __find_space_info(extent_root->fs_info, flags);
  3367. if (!space_info) {
  3368. ret = update_space_info(extent_root->fs_info, flags,
  3369. 0, 0, &space_info);
  3370. BUG_ON(ret); /* -ENOMEM */
  3371. }
  3372. BUG_ON(!space_info); /* Logic error */
  3373. again:
  3374. spin_lock(&space_info->lock);
  3375. if (force < space_info->force_alloc)
  3376. force = space_info->force_alloc;
  3377. if (space_info->full) {
  3378. spin_unlock(&space_info->lock);
  3379. return 0;
  3380. }
  3381. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3382. spin_unlock(&space_info->lock);
  3383. return 0;
  3384. } else if (space_info->chunk_alloc) {
  3385. wait_for_alloc = 1;
  3386. } else {
  3387. space_info->chunk_alloc = 1;
  3388. }
  3389. spin_unlock(&space_info->lock);
  3390. mutex_lock(&fs_info->chunk_mutex);
  3391. /*
  3392. * The chunk_mutex is held throughout the entirety of a chunk
  3393. * allocation, so once we've acquired the chunk_mutex we know that the
  3394. * other guy is done and we need to recheck and see if we should
  3395. * allocate.
  3396. */
  3397. if (wait_for_alloc) {
  3398. mutex_unlock(&fs_info->chunk_mutex);
  3399. wait_for_alloc = 0;
  3400. goto again;
  3401. }
  3402. trans->allocating_chunk = true;
  3403. /*
  3404. * If we have mixed data/metadata chunks we want to make sure we keep
  3405. * allocating mixed chunks instead of individual chunks.
  3406. */
  3407. if (btrfs_mixed_space_info(space_info))
  3408. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3409. /*
  3410. * if we're doing a data chunk, go ahead and make sure that
  3411. * we keep a reasonable number of metadata chunks allocated in the
  3412. * FS as well.
  3413. */
  3414. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3415. fs_info->data_chunk_allocations++;
  3416. if (!(fs_info->data_chunk_allocations %
  3417. fs_info->metadata_ratio))
  3418. force_metadata_allocation(fs_info);
  3419. }
  3420. /*
  3421. * Check if we have enough space in SYSTEM chunk because we may need
  3422. * to update devices.
  3423. */
  3424. check_system_chunk(trans, extent_root, flags);
  3425. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3426. trans->allocating_chunk = false;
  3427. spin_lock(&space_info->lock);
  3428. if (ret < 0 && ret != -ENOSPC)
  3429. goto out;
  3430. if (ret)
  3431. space_info->full = 1;
  3432. else
  3433. ret = 1;
  3434. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3435. out:
  3436. space_info->chunk_alloc = 0;
  3437. spin_unlock(&space_info->lock);
  3438. mutex_unlock(&fs_info->chunk_mutex);
  3439. return ret;
  3440. }
  3441. static int can_overcommit(struct btrfs_root *root,
  3442. struct btrfs_space_info *space_info, u64 bytes,
  3443. enum btrfs_reserve_flush_enum flush)
  3444. {
  3445. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3446. u64 profile = btrfs_get_alloc_profile(root, 0);
  3447. u64 space_size;
  3448. u64 avail;
  3449. u64 used;
  3450. u64 to_add;
  3451. used = space_info->bytes_used + space_info->bytes_reserved +
  3452. space_info->bytes_pinned + space_info->bytes_readonly;
  3453. /*
  3454. * We only want to allow over committing if we have lots of actual space
  3455. * free, but if we don't have enough space to handle the global reserve
  3456. * space then we could end up having a real enospc problem when trying
  3457. * to allocate a chunk or some other such important allocation.
  3458. */
  3459. spin_lock(&global_rsv->lock);
  3460. space_size = calc_global_rsv_need_space(global_rsv);
  3461. spin_unlock(&global_rsv->lock);
  3462. if (used + space_size >= space_info->total_bytes)
  3463. return 0;
  3464. used += space_info->bytes_may_use;
  3465. spin_lock(&root->fs_info->free_chunk_lock);
  3466. avail = root->fs_info->free_chunk_space;
  3467. spin_unlock(&root->fs_info->free_chunk_lock);
  3468. /*
  3469. * If we have dup, raid1 or raid10 then only half of the free
  3470. * space is actually useable. For raid56, the space info used
  3471. * doesn't include the parity drive, so we don't have to
  3472. * change the math
  3473. */
  3474. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3475. BTRFS_BLOCK_GROUP_RAID1 |
  3476. BTRFS_BLOCK_GROUP_RAID10))
  3477. avail >>= 1;
  3478. to_add = space_info->total_bytes;
  3479. /*
  3480. * If we aren't flushing all things, let us overcommit up to
  3481. * 1/2th of the space. If we can flush, don't let us overcommit
  3482. * too much, let it overcommit up to 1/8 of the space.
  3483. */
  3484. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3485. to_add >>= 3;
  3486. else
  3487. to_add >>= 1;
  3488. /*
  3489. * Limit the overcommit to the amount of free space we could possibly
  3490. * allocate for chunks.
  3491. */
  3492. to_add = min(avail, to_add);
  3493. if (used + bytes < space_info->total_bytes + to_add)
  3494. return 1;
  3495. return 0;
  3496. }
  3497. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3498. unsigned long nr_pages)
  3499. {
  3500. struct super_block *sb = root->fs_info->sb;
  3501. if (down_read_trylock(&sb->s_umount)) {
  3502. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  3503. up_read(&sb->s_umount);
  3504. } else {
  3505. /*
  3506. * We needn't worry the filesystem going from r/w to r/o though
  3507. * we don't acquire ->s_umount mutex, because the filesystem
  3508. * should guarantee the delalloc inodes list be empty after
  3509. * the filesystem is readonly(all dirty pages are written to
  3510. * the disk).
  3511. */
  3512. btrfs_start_all_delalloc_inodes(root->fs_info, 0);
  3513. if (!current->journal_info)
  3514. btrfs_wait_all_ordered_extents(root->fs_info, 0);
  3515. }
  3516. }
  3517. /*
  3518. * shrink metadata reservation for delalloc
  3519. */
  3520. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3521. bool wait_ordered)
  3522. {
  3523. struct btrfs_block_rsv *block_rsv;
  3524. struct btrfs_space_info *space_info;
  3525. struct btrfs_trans_handle *trans;
  3526. u64 delalloc_bytes;
  3527. u64 max_reclaim;
  3528. long time_left;
  3529. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3530. int loops = 0;
  3531. enum btrfs_reserve_flush_enum flush;
  3532. trans = (struct btrfs_trans_handle *)current->journal_info;
  3533. block_rsv = &root->fs_info->delalloc_block_rsv;
  3534. space_info = block_rsv->space_info;
  3535. smp_mb();
  3536. delalloc_bytes = percpu_counter_sum_positive(
  3537. &root->fs_info->delalloc_bytes);
  3538. if (delalloc_bytes == 0) {
  3539. if (trans)
  3540. return;
  3541. btrfs_wait_all_ordered_extents(root->fs_info, 0);
  3542. return;
  3543. }
  3544. while (delalloc_bytes && loops < 3) {
  3545. max_reclaim = min(delalloc_bytes, to_reclaim);
  3546. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3547. btrfs_writeback_inodes_sb_nr(root, nr_pages);
  3548. /*
  3549. * We need to wait for the async pages to actually start before
  3550. * we do anything.
  3551. */
  3552. wait_event(root->fs_info->async_submit_wait,
  3553. !atomic_read(&root->fs_info->async_delalloc_pages));
  3554. if (!trans)
  3555. flush = BTRFS_RESERVE_FLUSH_ALL;
  3556. else
  3557. flush = BTRFS_RESERVE_NO_FLUSH;
  3558. spin_lock(&space_info->lock);
  3559. if (can_overcommit(root, space_info, orig, flush)) {
  3560. spin_unlock(&space_info->lock);
  3561. break;
  3562. }
  3563. spin_unlock(&space_info->lock);
  3564. loops++;
  3565. if (wait_ordered && !trans) {
  3566. btrfs_wait_all_ordered_extents(root->fs_info, 0);
  3567. } else {
  3568. time_left = schedule_timeout_killable(1);
  3569. if (time_left)
  3570. break;
  3571. }
  3572. smp_mb();
  3573. delalloc_bytes = percpu_counter_sum_positive(
  3574. &root->fs_info->delalloc_bytes);
  3575. }
  3576. }
  3577. /**
  3578. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3579. * @root - the root we're allocating for
  3580. * @bytes - the number of bytes we want to reserve
  3581. * @force - force the commit
  3582. *
  3583. * This will check to make sure that committing the transaction will actually
  3584. * get us somewhere and then commit the transaction if it does. Otherwise it
  3585. * will return -ENOSPC.
  3586. */
  3587. static int may_commit_transaction(struct btrfs_root *root,
  3588. struct btrfs_space_info *space_info,
  3589. u64 bytes, int force)
  3590. {
  3591. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3592. struct btrfs_trans_handle *trans;
  3593. trans = (struct btrfs_trans_handle *)current->journal_info;
  3594. if (trans)
  3595. return -EAGAIN;
  3596. if (force)
  3597. goto commit;
  3598. /* See if there is enough pinned space to make this reservation */
  3599. spin_lock(&space_info->lock);
  3600. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3601. bytes) >= 0) {
  3602. spin_unlock(&space_info->lock);
  3603. goto commit;
  3604. }
  3605. spin_unlock(&space_info->lock);
  3606. /*
  3607. * See if there is some space in the delayed insertion reservation for
  3608. * this reservation.
  3609. */
  3610. if (space_info != delayed_rsv->space_info)
  3611. return -ENOSPC;
  3612. spin_lock(&space_info->lock);
  3613. spin_lock(&delayed_rsv->lock);
  3614. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3615. bytes - delayed_rsv->size) >= 0) {
  3616. spin_unlock(&delayed_rsv->lock);
  3617. spin_unlock(&space_info->lock);
  3618. return -ENOSPC;
  3619. }
  3620. spin_unlock(&delayed_rsv->lock);
  3621. spin_unlock(&space_info->lock);
  3622. commit:
  3623. trans = btrfs_join_transaction(root);
  3624. if (IS_ERR(trans))
  3625. return -ENOSPC;
  3626. return btrfs_commit_transaction(trans, root);
  3627. }
  3628. enum flush_state {
  3629. FLUSH_DELAYED_ITEMS_NR = 1,
  3630. FLUSH_DELAYED_ITEMS = 2,
  3631. FLUSH_DELALLOC = 3,
  3632. FLUSH_DELALLOC_WAIT = 4,
  3633. ALLOC_CHUNK = 5,
  3634. COMMIT_TRANS = 6,
  3635. };
  3636. static int flush_space(struct btrfs_root *root,
  3637. struct btrfs_space_info *space_info, u64 num_bytes,
  3638. u64 orig_bytes, int state)
  3639. {
  3640. struct btrfs_trans_handle *trans;
  3641. int nr;
  3642. int ret = 0;
  3643. switch (state) {
  3644. case FLUSH_DELAYED_ITEMS_NR:
  3645. case FLUSH_DELAYED_ITEMS:
  3646. if (state == FLUSH_DELAYED_ITEMS_NR) {
  3647. u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
  3648. nr = (int)div64_u64(num_bytes, bytes);
  3649. if (!nr)
  3650. nr = 1;
  3651. nr *= 2;
  3652. } else {
  3653. nr = -1;
  3654. }
  3655. trans = btrfs_join_transaction(root);
  3656. if (IS_ERR(trans)) {
  3657. ret = PTR_ERR(trans);
  3658. break;
  3659. }
  3660. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3661. btrfs_end_transaction(trans, root);
  3662. break;
  3663. case FLUSH_DELALLOC:
  3664. case FLUSH_DELALLOC_WAIT:
  3665. shrink_delalloc(root, num_bytes, orig_bytes,
  3666. state == FLUSH_DELALLOC_WAIT);
  3667. break;
  3668. case ALLOC_CHUNK:
  3669. trans = btrfs_join_transaction(root);
  3670. if (IS_ERR(trans)) {
  3671. ret = PTR_ERR(trans);
  3672. break;
  3673. }
  3674. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3675. btrfs_get_alloc_profile(root, 0),
  3676. CHUNK_ALLOC_NO_FORCE);
  3677. btrfs_end_transaction(trans, root);
  3678. if (ret == -ENOSPC)
  3679. ret = 0;
  3680. break;
  3681. case COMMIT_TRANS:
  3682. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3683. break;
  3684. default:
  3685. ret = -ENOSPC;
  3686. break;
  3687. }
  3688. return ret;
  3689. }
  3690. /**
  3691. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3692. * @root - the root we're allocating for
  3693. * @block_rsv - the block_rsv we're allocating for
  3694. * @orig_bytes - the number of bytes we want
  3695. * @flush - whether or not we can flush to make our reservation
  3696. *
  3697. * This will reserve orgi_bytes number of bytes from the space info associated
  3698. * with the block_rsv. If there is not enough space it will make an attempt to
  3699. * flush out space to make room. It will do this by flushing delalloc if
  3700. * possible or committing the transaction. If flush is 0 then no attempts to
  3701. * regain reservations will be made and this will fail if there is not enough
  3702. * space already.
  3703. */
  3704. static int reserve_metadata_bytes(struct btrfs_root *root,
  3705. struct btrfs_block_rsv *block_rsv,
  3706. u64 orig_bytes,
  3707. enum btrfs_reserve_flush_enum flush)
  3708. {
  3709. struct btrfs_space_info *space_info = block_rsv->space_info;
  3710. u64 used;
  3711. u64 num_bytes = orig_bytes;
  3712. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  3713. int ret = 0;
  3714. bool flushing = false;
  3715. again:
  3716. ret = 0;
  3717. spin_lock(&space_info->lock);
  3718. /*
  3719. * We only want to wait if somebody other than us is flushing and we
  3720. * are actually allowed to flush all things.
  3721. */
  3722. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  3723. space_info->flush) {
  3724. spin_unlock(&space_info->lock);
  3725. /*
  3726. * If we have a trans handle we can't wait because the flusher
  3727. * may have to commit the transaction, which would mean we would
  3728. * deadlock since we are waiting for the flusher to finish, but
  3729. * hold the current transaction open.
  3730. */
  3731. if (current->journal_info)
  3732. return -EAGAIN;
  3733. ret = wait_event_killable(space_info->wait, !space_info->flush);
  3734. /* Must have been killed, return */
  3735. if (ret)
  3736. return -EINTR;
  3737. spin_lock(&space_info->lock);
  3738. }
  3739. ret = -ENOSPC;
  3740. used = space_info->bytes_used + space_info->bytes_reserved +
  3741. space_info->bytes_pinned + space_info->bytes_readonly +
  3742. space_info->bytes_may_use;
  3743. /*
  3744. * The idea here is that we've not already over-reserved the block group
  3745. * then we can go ahead and save our reservation first and then start
  3746. * flushing if we need to. Otherwise if we've already overcommitted
  3747. * lets start flushing stuff first and then come back and try to make
  3748. * our reservation.
  3749. */
  3750. if (used <= space_info->total_bytes) {
  3751. if (used + orig_bytes <= space_info->total_bytes) {
  3752. space_info->bytes_may_use += orig_bytes;
  3753. trace_btrfs_space_reservation(root->fs_info,
  3754. "space_info", space_info->flags, orig_bytes, 1);
  3755. ret = 0;
  3756. } else {
  3757. /*
  3758. * Ok set num_bytes to orig_bytes since we aren't
  3759. * overocmmitted, this way we only try and reclaim what
  3760. * we need.
  3761. */
  3762. num_bytes = orig_bytes;
  3763. }
  3764. } else {
  3765. /*
  3766. * Ok we're over committed, set num_bytes to the overcommitted
  3767. * amount plus the amount of bytes that we need for this
  3768. * reservation.
  3769. */
  3770. num_bytes = used - space_info->total_bytes +
  3771. (orig_bytes * 2);
  3772. }
  3773. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  3774. space_info->bytes_may_use += orig_bytes;
  3775. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3776. space_info->flags, orig_bytes,
  3777. 1);
  3778. ret = 0;
  3779. }
  3780. /*
  3781. * Couldn't make our reservation, save our place so while we're trying
  3782. * to reclaim space we can actually use it instead of somebody else
  3783. * stealing it from us.
  3784. *
  3785. * We make the other tasks wait for the flush only when we can flush
  3786. * all things.
  3787. */
  3788. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  3789. flushing = true;
  3790. space_info->flush = 1;
  3791. }
  3792. spin_unlock(&space_info->lock);
  3793. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  3794. goto out;
  3795. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  3796. flush_state);
  3797. flush_state++;
  3798. /*
  3799. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  3800. * would happen. So skip delalloc flush.
  3801. */
  3802. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3803. (flush_state == FLUSH_DELALLOC ||
  3804. flush_state == FLUSH_DELALLOC_WAIT))
  3805. flush_state = ALLOC_CHUNK;
  3806. if (!ret)
  3807. goto again;
  3808. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3809. flush_state < COMMIT_TRANS)
  3810. goto again;
  3811. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  3812. flush_state <= COMMIT_TRANS)
  3813. goto again;
  3814. out:
  3815. if (ret == -ENOSPC &&
  3816. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  3817. struct btrfs_block_rsv *global_rsv =
  3818. &root->fs_info->global_block_rsv;
  3819. if (block_rsv != global_rsv &&
  3820. !block_rsv_use_bytes(global_rsv, orig_bytes))
  3821. ret = 0;
  3822. }
  3823. if (flushing) {
  3824. spin_lock(&space_info->lock);
  3825. space_info->flush = 0;
  3826. wake_up_all(&space_info->wait);
  3827. spin_unlock(&space_info->lock);
  3828. }
  3829. return ret;
  3830. }
  3831. static struct btrfs_block_rsv *get_block_rsv(
  3832. const struct btrfs_trans_handle *trans,
  3833. const struct btrfs_root *root)
  3834. {
  3835. struct btrfs_block_rsv *block_rsv = NULL;
  3836. if (root->ref_cows)
  3837. block_rsv = trans->block_rsv;
  3838. if (root == root->fs_info->csum_root && trans->adding_csums)
  3839. block_rsv = trans->block_rsv;
  3840. if (!block_rsv)
  3841. block_rsv = root->block_rsv;
  3842. if (!block_rsv)
  3843. block_rsv = &root->fs_info->empty_block_rsv;
  3844. return block_rsv;
  3845. }
  3846. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3847. u64 num_bytes)
  3848. {
  3849. int ret = -ENOSPC;
  3850. spin_lock(&block_rsv->lock);
  3851. if (block_rsv->reserved >= num_bytes) {
  3852. block_rsv->reserved -= num_bytes;
  3853. if (block_rsv->reserved < block_rsv->size)
  3854. block_rsv->full = 0;
  3855. ret = 0;
  3856. }
  3857. spin_unlock(&block_rsv->lock);
  3858. return ret;
  3859. }
  3860. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3861. u64 num_bytes, int update_size)
  3862. {
  3863. spin_lock(&block_rsv->lock);
  3864. block_rsv->reserved += num_bytes;
  3865. if (update_size)
  3866. block_rsv->size += num_bytes;
  3867. else if (block_rsv->reserved >= block_rsv->size)
  3868. block_rsv->full = 1;
  3869. spin_unlock(&block_rsv->lock);
  3870. }
  3871. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  3872. struct btrfs_block_rsv *dest, u64 num_bytes,
  3873. int min_factor)
  3874. {
  3875. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  3876. u64 min_bytes;
  3877. if (global_rsv->space_info != dest->space_info)
  3878. return -ENOSPC;
  3879. spin_lock(&global_rsv->lock);
  3880. min_bytes = div_factor(global_rsv->size, min_factor);
  3881. if (global_rsv->reserved < min_bytes + num_bytes) {
  3882. spin_unlock(&global_rsv->lock);
  3883. return -ENOSPC;
  3884. }
  3885. global_rsv->reserved -= num_bytes;
  3886. if (global_rsv->reserved < global_rsv->size)
  3887. global_rsv->full = 0;
  3888. spin_unlock(&global_rsv->lock);
  3889. block_rsv_add_bytes(dest, num_bytes, 1);
  3890. return 0;
  3891. }
  3892. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3893. struct btrfs_block_rsv *block_rsv,
  3894. struct btrfs_block_rsv *dest, u64 num_bytes)
  3895. {
  3896. struct btrfs_space_info *space_info = block_rsv->space_info;
  3897. spin_lock(&block_rsv->lock);
  3898. if (num_bytes == (u64)-1)
  3899. num_bytes = block_rsv->size;
  3900. block_rsv->size -= num_bytes;
  3901. if (block_rsv->reserved >= block_rsv->size) {
  3902. num_bytes = block_rsv->reserved - block_rsv->size;
  3903. block_rsv->reserved = block_rsv->size;
  3904. block_rsv->full = 1;
  3905. } else {
  3906. num_bytes = 0;
  3907. }
  3908. spin_unlock(&block_rsv->lock);
  3909. if (num_bytes > 0) {
  3910. if (dest) {
  3911. spin_lock(&dest->lock);
  3912. if (!dest->full) {
  3913. u64 bytes_to_add;
  3914. bytes_to_add = dest->size - dest->reserved;
  3915. bytes_to_add = min(num_bytes, bytes_to_add);
  3916. dest->reserved += bytes_to_add;
  3917. if (dest->reserved >= dest->size)
  3918. dest->full = 1;
  3919. num_bytes -= bytes_to_add;
  3920. }
  3921. spin_unlock(&dest->lock);
  3922. }
  3923. if (num_bytes) {
  3924. spin_lock(&space_info->lock);
  3925. space_info->bytes_may_use -= num_bytes;
  3926. trace_btrfs_space_reservation(fs_info, "space_info",
  3927. space_info->flags, num_bytes, 0);
  3928. space_info->reservation_progress++;
  3929. spin_unlock(&space_info->lock);
  3930. }
  3931. }
  3932. }
  3933. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3934. struct btrfs_block_rsv *dst, u64 num_bytes)
  3935. {
  3936. int ret;
  3937. ret = block_rsv_use_bytes(src, num_bytes);
  3938. if (ret)
  3939. return ret;
  3940. block_rsv_add_bytes(dst, num_bytes, 1);
  3941. return 0;
  3942. }
  3943. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  3944. {
  3945. memset(rsv, 0, sizeof(*rsv));
  3946. spin_lock_init(&rsv->lock);
  3947. rsv->type = type;
  3948. }
  3949. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  3950. unsigned short type)
  3951. {
  3952. struct btrfs_block_rsv *block_rsv;
  3953. struct btrfs_fs_info *fs_info = root->fs_info;
  3954. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3955. if (!block_rsv)
  3956. return NULL;
  3957. btrfs_init_block_rsv(block_rsv, type);
  3958. block_rsv->space_info = __find_space_info(fs_info,
  3959. BTRFS_BLOCK_GROUP_METADATA);
  3960. return block_rsv;
  3961. }
  3962. void btrfs_free_block_rsv(struct btrfs_root *root,
  3963. struct btrfs_block_rsv *rsv)
  3964. {
  3965. if (!rsv)
  3966. return;
  3967. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3968. kfree(rsv);
  3969. }
  3970. int btrfs_block_rsv_add(struct btrfs_root *root,
  3971. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  3972. enum btrfs_reserve_flush_enum flush)
  3973. {
  3974. int ret;
  3975. if (num_bytes == 0)
  3976. return 0;
  3977. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3978. if (!ret) {
  3979. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3980. return 0;
  3981. }
  3982. return ret;
  3983. }
  3984. int btrfs_block_rsv_check(struct btrfs_root *root,
  3985. struct btrfs_block_rsv *block_rsv, int min_factor)
  3986. {
  3987. u64 num_bytes = 0;
  3988. int ret = -ENOSPC;
  3989. if (!block_rsv)
  3990. return 0;
  3991. spin_lock(&block_rsv->lock);
  3992. num_bytes = div_factor(block_rsv->size, min_factor);
  3993. if (block_rsv->reserved >= num_bytes)
  3994. ret = 0;
  3995. spin_unlock(&block_rsv->lock);
  3996. return ret;
  3997. }
  3998. int btrfs_block_rsv_refill(struct btrfs_root *root,
  3999. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  4000. enum btrfs_reserve_flush_enum flush)
  4001. {
  4002. u64 num_bytes = 0;
  4003. int ret = -ENOSPC;
  4004. if (!block_rsv)
  4005. return 0;
  4006. spin_lock(&block_rsv->lock);
  4007. num_bytes = min_reserved;
  4008. if (block_rsv->reserved >= num_bytes)
  4009. ret = 0;
  4010. else
  4011. num_bytes -= block_rsv->reserved;
  4012. spin_unlock(&block_rsv->lock);
  4013. if (!ret)
  4014. return 0;
  4015. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4016. if (!ret) {
  4017. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  4018. return 0;
  4019. }
  4020. return ret;
  4021. }
  4022. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  4023. struct btrfs_block_rsv *dst_rsv,
  4024. u64 num_bytes)
  4025. {
  4026. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4027. }
  4028. void btrfs_block_rsv_release(struct btrfs_root *root,
  4029. struct btrfs_block_rsv *block_rsv,
  4030. u64 num_bytes)
  4031. {
  4032. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4033. if (global_rsv->full || global_rsv == block_rsv ||
  4034. block_rsv->space_info != global_rsv->space_info)
  4035. global_rsv = NULL;
  4036. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4037. num_bytes);
  4038. }
  4039. /*
  4040. * helper to calculate size of global block reservation.
  4041. * the desired value is sum of space used by extent tree,
  4042. * checksum tree and root tree
  4043. */
  4044. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4045. {
  4046. struct btrfs_space_info *sinfo;
  4047. u64 num_bytes;
  4048. u64 meta_used;
  4049. u64 data_used;
  4050. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4051. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4052. spin_lock(&sinfo->lock);
  4053. data_used = sinfo->bytes_used;
  4054. spin_unlock(&sinfo->lock);
  4055. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4056. spin_lock(&sinfo->lock);
  4057. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4058. data_used = 0;
  4059. meta_used = sinfo->bytes_used;
  4060. spin_unlock(&sinfo->lock);
  4061. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4062. csum_size * 2;
  4063. num_bytes += div64_u64(data_used + meta_used, 50);
  4064. if (num_bytes * 3 > meta_used)
  4065. num_bytes = div64_u64(meta_used, 3);
  4066. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  4067. }
  4068. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4069. {
  4070. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4071. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4072. u64 num_bytes;
  4073. num_bytes = calc_global_metadata_size(fs_info);
  4074. spin_lock(&sinfo->lock);
  4075. spin_lock(&block_rsv->lock);
  4076. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4077. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4078. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4079. sinfo->bytes_may_use;
  4080. if (sinfo->total_bytes > num_bytes) {
  4081. num_bytes = sinfo->total_bytes - num_bytes;
  4082. block_rsv->reserved += num_bytes;
  4083. sinfo->bytes_may_use += num_bytes;
  4084. trace_btrfs_space_reservation(fs_info, "space_info",
  4085. sinfo->flags, num_bytes, 1);
  4086. }
  4087. if (block_rsv->reserved >= block_rsv->size) {
  4088. num_bytes = block_rsv->reserved - block_rsv->size;
  4089. sinfo->bytes_may_use -= num_bytes;
  4090. trace_btrfs_space_reservation(fs_info, "space_info",
  4091. sinfo->flags, num_bytes, 0);
  4092. sinfo->reservation_progress++;
  4093. block_rsv->reserved = block_rsv->size;
  4094. block_rsv->full = 1;
  4095. }
  4096. spin_unlock(&block_rsv->lock);
  4097. spin_unlock(&sinfo->lock);
  4098. }
  4099. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4100. {
  4101. struct btrfs_space_info *space_info;
  4102. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4103. fs_info->chunk_block_rsv.space_info = space_info;
  4104. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4105. fs_info->global_block_rsv.space_info = space_info;
  4106. fs_info->delalloc_block_rsv.space_info = space_info;
  4107. fs_info->trans_block_rsv.space_info = space_info;
  4108. fs_info->empty_block_rsv.space_info = space_info;
  4109. fs_info->delayed_block_rsv.space_info = space_info;
  4110. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4111. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4112. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4113. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4114. if (fs_info->quota_root)
  4115. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4116. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4117. update_global_block_rsv(fs_info);
  4118. }
  4119. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4120. {
  4121. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4122. (u64)-1);
  4123. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4124. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4125. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4126. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4127. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4128. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4129. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4130. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4131. }
  4132. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4133. struct btrfs_root *root)
  4134. {
  4135. if (!trans->block_rsv)
  4136. return;
  4137. if (!trans->bytes_reserved)
  4138. return;
  4139. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4140. trans->transid, trans->bytes_reserved, 0);
  4141. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4142. trans->bytes_reserved = 0;
  4143. }
  4144. /* Can only return 0 or -ENOSPC */
  4145. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4146. struct inode *inode)
  4147. {
  4148. struct btrfs_root *root = BTRFS_I(inode)->root;
  4149. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4150. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4151. /*
  4152. * We need to hold space in order to delete our orphan item once we've
  4153. * added it, so this takes the reservation so we can release it later
  4154. * when we are truly done with the orphan item.
  4155. */
  4156. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4157. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4158. btrfs_ino(inode), num_bytes, 1);
  4159. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4160. }
  4161. void btrfs_orphan_release_metadata(struct inode *inode)
  4162. {
  4163. struct btrfs_root *root = BTRFS_I(inode)->root;
  4164. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4165. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4166. btrfs_ino(inode), num_bytes, 0);
  4167. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4168. }
  4169. /*
  4170. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4171. * root: the root of the parent directory
  4172. * rsv: block reservation
  4173. * items: the number of items that we need do reservation
  4174. * qgroup_reserved: used to return the reserved size in qgroup
  4175. *
  4176. * This function is used to reserve the space for snapshot/subvolume
  4177. * creation and deletion. Those operations are different with the
  4178. * common file/directory operations, they change two fs/file trees
  4179. * and root tree, the number of items that the qgroup reserves is
  4180. * different with the free space reservation. So we can not use
  4181. * the space reseravtion mechanism in start_transaction().
  4182. */
  4183. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4184. struct btrfs_block_rsv *rsv,
  4185. int items,
  4186. u64 *qgroup_reserved,
  4187. bool use_global_rsv)
  4188. {
  4189. u64 num_bytes;
  4190. int ret;
  4191. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4192. if (root->fs_info->quota_enabled) {
  4193. /* One for parent inode, two for dir entries */
  4194. num_bytes = 3 * root->leafsize;
  4195. ret = btrfs_qgroup_reserve(root, num_bytes);
  4196. if (ret)
  4197. return ret;
  4198. } else {
  4199. num_bytes = 0;
  4200. }
  4201. *qgroup_reserved = num_bytes;
  4202. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4203. rsv->space_info = __find_space_info(root->fs_info,
  4204. BTRFS_BLOCK_GROUP_METADATA);
  4205. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4206. BTRFS_RESERVE_FLUSH_ALL);
  4207. if (ret == -ENOSPC && use_global_rsv)
  4208. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
  4209. if (ret) {
  4210. if (*qgroup_reserved)
  4211. btrfs_qgroup_free(root, *qgroup_reserved);
  4212. }
  4213. return ret;
  4214. }
  4215. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4216. struct btrfs_block_rsv *rsv,
  4217. u64 qgroup_reserved)
  4218. {
  4219. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4220. if (qgroup_reserved)
  4221. btrfs_qgroup_free(root, qgroup_reserved);
  4222. }
  4223. /**
  4224. * drop_outstanding_extent - drop an outstanding extent
  4225. * @inode: the inode we're dropping the extent for
  4226. *
  4227. * This is called when we are freeing up an outstanding extent, either called
  4228. * after an error or after an extent is written. This will return the number of
  4229. * reserved extents that need to be freed. This must be called with
  4230. * BTRFS_I(inode)->lock held.
  4231. */
  4232. static unsigned drop_outstanding_extent(struct inode *inode)
  4233. {
  4234. unsigned drop_inode_space = 0;
  4235. unsigned dropped_extents = 0;
  4236. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  4237. BTRFS_I(inode)->outstanding_extents--;
  4238. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4239. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4240. &BTRFS_I(inode)->runtime_flags))
  4241. drop_inode_space = 1;
  4242. /*
  4243. * If we have more or the same amount of outsanding extents than we have
  4244. * reserved then we need to leave the reserved extents count alone.
  4245. */
  4246. if (BTRFS_I(inode)->outstanding_extents >=
  4247. BTRFS_I(inode)->reserved_extents)
  4248. return drop_inode_space;
  4249. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4250. BTRFS_I(inode)->outstanding_extents;
  4251. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4252. return dropped_extents + drop_inode_space;
  4253. }
  4254. /**
  4255. * calc_csum_metadata_size - return the amount of metada space that must be
  4256. * reserved/free'd for the given bytes.
  4257. * @inode: the inode we're manipulating
  4258. * @num_bytes: the number of bytes in question
  4259. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4260. *
  4261. * This adjusts the number of csum_bytes in the inode and then returns the
  4262. * correct amount of metadata that must either be reserved or freed. We
  4263. * calculate how many checksums we can fit into one leaf and then divide the
  4264. * number of bytes that will need to be checksumed by this value to figure out
  4265. * how many checksums will be required. If we are adding bytes then the number
  4266. * may go up and we will return the number of additional bytes that must be
  4267. * reserved. If it is going down we will return the number of bytes that must
  4268. * be freed.
  4269. *
  4270. * This must be called with BTRFS_I(inode)->lock held.
  4271. */
  4272. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4273. int reserve)
  4274. {
  4275. struct btrfs_root *root = BTRFS_I(inode)->root;
  4276. u64 csum_size;
  4277. int num_csums_per_leaf;
  4278. int num_csums;
  4279. int old_csums;
  4280. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4281. BTRFS_I(inode)->csum_bytes == 0)
  4282. return 0;
  4283. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4284. if (reserve)
  4285. BTRFS_I(inode)->csum_bytes += num_bytes;
  4286. else
  4287. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4288. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  4289. num_csums_per_leaf = (int)div64_u64(csum_size,
  4290. sizeof(struct btrfs_csum_item) +
  4291. sizeof(struct btrfs_disk_key));
  4292. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4293. num_csums = num_csums + num_csums_per_leaf - 1;
  4294. num_csums = num_csums / num_csums_per_leaf;
  4295. old_csums = old_csums + num_csums_per_leaf - 1;
  4296. old_csums = old_csums / num_csums_per_leaf;
  4297. /* No change, no need to reserve more */
  4298. if (old_csums == num_csums)
  4299. return 0;
  4300. if (reserve)
  4301. return btrfs_calc_trans_metadata_size(root,
  4302. num_csums - old_csums);
  4303. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4304. }
  4305. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4306. {
  4307. struct btrfs_root *root = BTRFS_I(inode)->root;
  4308. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4309. u64 to_reserve = 0;
  4310. u64 csum_bytes;
  4311. unsigned nr_extents = 0;
  4312. int extra_reserve = 0;
  4313. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4314. int ret = 0;
  4315. bool delalloc_lock = true;
  4316. u64 to_free = 0;
  4317. unsigned dropped;
  4318. /* If we are a free space inode we need to not flush since we will be in
  4319. * the middle of a transaction commit. We also don't need the delalloc
  4320. * mutex since we won't race with anybody. We need this mostly to make
  4321. * lockdep shut its filthy mouth.
  4322. */
  4323. if (btrfs_is_free_space_inode(inode)) {
  4324. flush = BTRFS_RESERVE_NO_FLUSH;
  4325. delalloc_lock = false;
  4326. }
  4327. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4328. btrfs_transaction_in_commit(root->fs_info))
  4329. schedule_timeout(1);
  4330. if (delalloc_lock)
  4331. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4332. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4333. spin_lock(&BTRFS_I(inode)->lock);
  4334. BTRFS_I(inode)->outstanding_extents++;
  4335. if (BTRFS_I(inode)->outstanding_extents >
  4336. BTRFS_I(inode)->reserved_extents)
  4337. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4338. BTRFS_I(inode)->reserved_extents;
  4339. /*
  4340. * Add an item to reserve for updating the inode when we complete the
  4341. * delalloc io.
  4342. */
  4343. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4344. &BTRFS_I(inode)->runtime_flags)) {
  4345. nr_extents++;
  4346. extra_reserve = 1;
  4347. }
  4348. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4349. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4350. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4351. spin_unlock(&BTRFS_I(inode)->lock);
  4352. if (root->fs_info->quota_enabled) {
  4353. ret = btrfs_qgroup_reserve(root, num_bytes +
  4354. nr_extents * root->leafsize);
  4355. if (ret)
  4356. goto out_fail;
  4357. }
  4358. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4359. if (unlikely(ret)) {
  4360. if (root->fs_info->quota_enabled)
  4361. btrfs_qgroup_free(root, num_bytes +
  4362. nr_extents * root->leafsize);
  4363. goto out_fail;
  4364. }
  4365. spin_lock(&BTRFS_I(inode)->lock);
  4366. if (extra_reserve) {
  4367. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4368. &BTRFS_I(inode)->runtime_flags);
  4369. nr_extents--;
  4370. }
  4371. BTRFS_I(inode)->reserved_extents += nr_extents;
  4372. spin_unlock(&BTRFS_I(inode)->lock);
  4373. if (delalloc_lock)
  4374. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4375. if (to_reserve)
  4376. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  4377. btrfs_ino(inode), to_reserve, 1);
  4378. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4379. return 0;
  4380. out_fail:
  4381. spin_lock(&BTRFS_I(inode)->lock);
  4382. dropped = drop_outstanding_extent(inode);
  4383. /*
  4384. * If the inodes csum_bytes is the same as the original
  4385. * csum_bytes then we know we haven't raced with any free()ers
  4386. * so we can just reduce our inodes csum bytes and carry on.
  4387. */
  4388. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4389. calc_csum_metadata_size(inode, num_bytes, 0);
  4390. } else {
  4391. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4392. u64 bytes;
  4393. /*
  4394. * This is tricky, but first we need to figure out how much we
  4395. * free'd from any free-ers that occured during this
  4396. * reservation, so we reset ->csum_bytes to the csum_bytes
  4397. * before we dropped our lock, and then call the free for the
  4398. * number of bytes that were freed while we were trying our
  4399. * reservation.
  4400. */
  4401. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4402. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4403. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4404. /*
  4405. * Now we need to see how much we would have freed had we not
  4406. * been making this reservation and our ->csum_bytes were not
  4407. * artificially inflated.
  4408. */
  4409. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4410. bytes = csum_bytes - orig_csum_bytes;
  4411. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4412. /*
  4413. * Now reset ->csum_bytes to what it should be. If bytes is
  4414. * more than to_free then we would have free'd more space had we
  4415. * not had an artificially high ->csum_bytes, so we need to free
  4416. * the remainder. If bytes is the same or less then we don't
  4417. * need to do anything, the other free-ers did the correct
  4418. * thing.
  4419. */
  4420. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4421. if (bytes > to_free)
  4422. to_free = bytes - to_free;
  4423. else
  4424. to_free = 0;
  4425. }
  4426. spin_unlock(&BTRFS_I(inode)->lock);
  4427. if (dropped)
  4428. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4429. if (to_free) {
  4430. btrfs_block_rsv_release(root, block_rsv, to_free);
  4431. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4432. btrfs_ino(inode), to_free, 0);
  4433. }
  4434. if (delalloc_lock)
  4435. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4436. return ret;
  4437. }
  4438. /**
  4439. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4440. * @inode: the inode to release the reservation for
  4441. * @num_bytes: the number of bytes we're releasing
  4442. *
  4443. * This will release the metadata reservation for an inode. This can be called
  4444. * once we complete IO for a given set of bytes to release their metadata
  4445. * reservations.
  4446. */
  4447. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4448. {
  4449. struct btrfs_root *root = BTRFS_I(inode)->root;
  4450. u64 to_free = 0;
  4451. unsigned dropped;
  4452. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4453. spin_lock(&BTRFS_I(inode)->lock);
  4454. dropped = drop_outstanding_extent(inode);
  4455. if (num_bytes)
  4456. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4457. spin_unlock(&BTRFS_I(inode)->lock);
  4458. if (dropped > 0)
  4459. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4460. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4461. btrfs_ino(inode), to_free, 0);
  4462. if (root->fs_info->quota_enabled) {
  4463. btrfs_qgroup_free(root, num_bytes +
  4464. dropped * root->leafsize);
  4465. }
  4466. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4467. to_free);
  4468. }
  4469. /**
  4470. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4471. * @inode: inode we're writing to
  4472. * @num_bytes: the number of bytes we want to allocate
  4473. *
  4474. * This will do the following things
  4475. *
  4476. * o reserve space in the data space info for num_bytes
  4477. * o reserve space in the metadata space info based on number of outstanding
  4478. * extents and how much csums will be needed
  4479. * o add to the inodes ->delalloc_bytes
  4480. * o add it to the fs_info's delalloc inodes list.
  4481. *
  4482. * This will return 0 for success and -ENOSPC if there is no space left.
  4483. */
  4484. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4485. {
  4486. int ret;
  4487. ret = btrfs_check_data_free_space(inode, num_bytes);
  4488. if (ret)
  4489. return ret;
  4490. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4491. if (ret) {
  4492. btrfs_free_reserved_data_space(inode, num_bytes);
  4493. return ret;
  4494. }
  4495. return 0;
  4496. }
  4497. /**
  4498. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4499. * @inode: inode we're releasing space for
  4500. * @num_bytes: the number of bytes we want to free up
  4501. *
  4502. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4503. * called in the case that we don't need the metadata AND data reservations
  4504. * anymore. So if there is an error or we insert an inline extent.
  4505. *
  4506. * This function will release the metadata space that was not used and will
  4507. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4508. * list if there are no delalloc bytes left.
  4509. */
  4510. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4511. {
  4512. btrfs_delalloc_release_metadata(inode, num_bytes);
  4513. btrfs_free_reserved_data_space(inode, num_bytes);
  4514. }
  4515. static int update_block_group(struct btrfs_root *root,
  4516. u64 bytenr, u64 num_bytes, int alloc)
  4517. {
  4518. struct btrfs_block_group_cache *cache = NULL;
  4519. struct btrfs_fs_info *info = root->fs_info;
  4520. u64 total = num_bytes;
  4521. u64 old_val;
  4522. u64 byte_in_group;
  4523. int factor;
  4524. /* block accounting for super block */
  4525. spin_lock(&info->delalloc_root_lock);
  4526. old_val = btrfs_super_bytes_used(info->super_copy);
  4527. if (alloc)
  4528. old_val += num_bytes;
  4529. else
  4530. old_val -= num_bytes;
  4531. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4532. spin_unlock(&info->delalloc_root_lock);
  4533. while (total) {
  4534. cache = btrfs_lookup_block_group(info, bytenr);
  4535. if (!cache)
  4536. return -ENOENT;
  4537. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4538. BTRFS_BLOCK_GROUP_RAID1 |
  4539. BTRFS_BLOCK_GROUP_RAID10))
  4540. factor = 2;
  4541. else
  4542. factor = 1;
  4543. /*
  4544. * If this block group has free space cache written out, we
  4545. * need to make sure to load it if we are removing space. This
  4546. * is because we need the unpinning stage to actually add the
  4547. * space back to the block group, otherwise we will leak space.
  4548. */
  4549. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4550. cache_block_group(cache, 1);
  4551. byte_in_group = bytenr - cache->key.objectid;
  4552. WARN_ON(byte_in_group > cache->key.offset);
  4553. spin_lock(&cache->space_info->lock);
  4554. spin_lock(&cache->lock);
  4555. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4556. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4557. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4558. cache->dirty = 1;
  4559. old_val = btrfs_block_group_used(&cache->item);
  4560. num_bytes = min(total, cache->key.offset - byte_in_group);
  4561. if (alloc) {
  4562. old_val += num_bytes;
  4563. btrfs_set_block_group_used(&cache->item, old_val);
  4564. cache->reserved -= num_bytes;
  4565. cache->space_info->bytes_reserved -= num_bytes;
  4566. cache->space_info->bytes_used += num_bytes;
  4567. cache->space_info->disk_used += num_bytes * factor;
  4568. spin_unlock(&cache->lock);
  4569. spin_unlock(&cache->space_info->lock);
  4570. } else {
  4571. old_val -= num_bytes;
  4572. btrfs_set_block_group_used(&cache->item, old_val);
  4573. cache->pinned += num_bytes;
  4574. cache->space_info->bytes_pinned += num_bytes;
  4575. cache->space_info->bytes_used -= num_bytes;
  4576. cache->space_info->disk_used -= num_bytes * factor;
  4577. spin_unlock(&cache->lock);
  4578. spin_unlock(&cache->space_info->lock);
  4579. set_extent_dirty(info->pinned_extents,
  4580. bytenr, bytenr + num_bytes - 1,
  4581. GFP_NOFS | __GFP_NOFAIL);
  4582. }
  4583. btrfs_put_block_group(cache);
  4584. total -= num_bytes;
  4585. bytenr += num_bytes;
  4586. }
  4587. return 0;
  4588. }
  4589. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4590. {
  4591. struct btrfs_block_group_cache *cache;
  4592. u64 bytenr;
  4593. spin_lock(&root->fs_info->block_group_cache_lock);
  4594. bytenr = root->fs_info->first_logical_byte;
  4595. spin_unlock(&root->fs_info->block_group_cache_lock);
  4596. if (bytenr < (u64)-1)
  4597. return bytenr;
  4598. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4599. if (!cache)
  4600. return 0;
  4601. bytenr = cache->key.objectid;
  4602. btrfs_put_block_group(cache);
  4603. return bytenr;
  4604. }
  4605. static int pin_down_extent(struct btrfs_root *root,
  4606. struct btrfs_block_group_cache *cache,
  4607. u64 bytenr, u64 num_bytes, int reserved)
  4608. {
  4609. spin_lock(&cache->space_info->lock);
  4610. spin_lock(&cache->lock);
  4611. cache->pinned += num_bytes;
  4612. cache->space_info->bytes_pinned += num_bytes;
  4613. if (reserved) {
  4614. cache->reserved -= num_bytes;
  4615. cache->space_info->bytes_reserved -= num_bytes;
  4616. }
  4617. spin_unlock(&cache->lock);
  4618. spin_unlock(&cache->space_info->lock);
  4619. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4620. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4621. return 0;
  4622. }
  4623. /*
  4624. * this function must be called within transaction
  4625. */
  4626. int btrfs_pin_extent(struct btrfs_root *root,
  4627. u64 bytenr, u64 num_bytes, int reserved)
  4628. {
  4629. struct btrfs_block_group_cache *cache;
  4630. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4631. BUG_ON(!cache); /* Logic error */
  4632. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4633. btrfs_put_block_group(cache);
  4634. return 0;
  4635. }
  4636. /*
  4637. * this function must be called within transaction
  4638. */
  4639. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  4640. u64 bytenr, u64 num_bytes)
  4641. {
  4642. struct btrfs_block_group_cache *cache;
  4643. int ret;
  4644. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4645. if (!cache)
  4646. return -EINVAL;
  4647. /*
  4648. * pull in the free space cache (if any) so that our pin
  4649. * removes the free space from the cache. We have load_only set
  4650. * to one because the slow code to read in the free extents does check
  4651. * the pinned extents.
  4652. */
  4653. cache_block_group(cache, 1);
  4654. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4655. /* remove us from the free space cache (if we're there at all) */
  4656. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  4657. btrfs_put_block_group(cache);
  4658. return ret;
  4659. }
  4660. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  4661. {
  4662. int ret;
  4663. struct btrfs_block_group_cache *block_group;
  4664. struct btrfs_caching_control *caching_ctl;
  4665. block_group = btrfs_lookup_block_group(root->fs_info, start);
  4666. if (!block_group)
  4667. return -EINVAL;
  4668. cache_block_group(block_group, 0);
  4669. caching_ctl = get_caching_control(block_group);
  4670. if (!caching_ctl) {
  4671. /* Logic error */
  4672. BUG_ON(!block_group_cache_done(block_group));
  4673. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4674. } else {
  4675. mutex_lock(&caching_ctl->mutex);
  4676. if (start >= caching_ctl->progress) {
  4677. ret = add_excluded_extent(root, start, num_bytes);
  4678. } else if (start + num_bytes <= caching_ctl->progress) {
  4679. ret = btrfs_remove_free_space(block_group,
  4680. start, num_bytes);
  4681. } else {
  4682. num_bytes = caching_ctl->progress - start;
  4683. ret = btrfs_remove_free_space(block_group,
  4684. start, num_bytes);
  4685. if (ret)
  4686. goto out_lock;
  4687. num_bytes = (start + num_bytes) -
  4688. caching_ctl->progress;
  4689. start = caching_ctl->progress;
  4690. ret = add_excluded_extent(root, start, num_bytes);
  4691. }
  4692. out_lock:
  4693. mutex_unlock(&caching_ctl->mutex);
  4694. put_caching_control(caching_ctl);
  4695. }
  4696. btrfs_put_block_group(block_group);
  4697. return ret;
  4698. }
  4699. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  4700. struct extent_buffer *eb)
  4701. {
  4702. struct btrfs_file_extent_item *item;
  4703. struct btrfs_key key;
  4704. int found_type;
  4705. int i;
  4706. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  4707. return 0;
  4708. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  4709. btrfs_item_key_to_cpu(eb, &key, i);
  4710. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4711. continue;
  4712. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  4713. found_type = btrfs_file_extent_type(eb, item);
  4714. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  4715. continue;
  4716. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  4717. continue;
  4718. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  4719. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  4720. __exclude_logged_extent(log, key.objectid, key.offset);
  4721. }
  4722. return 0;
  4723. }
  4724. /**
  4725. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4726. * @cache: The cache we are manipulating
  4727. * @num_bytes: The number of bytes in question
  4728. * @reserve: One of the reservation enums
  4729. *
  4730. * This is called by the allocator when it reserves space, or by somebody who is
  4731. * freeing space that was never actually used on disk. For example if you
  4732. * reserve some space for a new leaf in transaction A and before transaction A
  4733. * commits you free that leaf, you call this with reserve set to 0 in order to
  4734. * clear the reservation.
  4735. *
  4736. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4737. * ENOSPC accounting. For data we handle the reservation through clearing the
  4738. * delalloc bits in the io_tree. We have to do this since we could end up
  4739. * allocating less disk space for the amount of data we have reserved in the
  4740. * case of compression.
  4741. *
  4742. * If this is a reservation and the block group has become read only we cannot
  4743. * make the reservation and return -EAGAIN, otherwise this function always
  4744. * succeeds.
  4745. */
  4746. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4747. u64 num_bytes, int reserve)
  4748. {
  4749. struct btrfs_space_info *space_info = cache->space_info;
  4750. int ret = 0;
  4751. spin_lock(&space_info->lock);
  4752. spin_lock(&cache->lock);
  4753. if (reserve != RESERVE_FREE) {
  4754. if (cache->ro) {
  4755. ret = -EAGAIN;
  4756. } else {
  4757. cache->reserved += num_bytes;
  4758. space_info->bytes_reserved += num_bytes;
  4759. if (reserve == RESERVE_ALLOC) {
  4760. trace_btrfs_space_reservation(cache->fs_info,
  4761. "space_info", space_info->flags,
  4762. num_bytes, 0);
  4763. space_info->bytes_may_use -= num_bytes;
  4764. }
  4765. }
  4766. } else {
  4767. if (cache->ro)
  4768. space_info->bytes_readonly += num_bytes;
  4769. cache->reserved -= num_bytes;
  4770. space_info->bytes_reserved -= num_bytes;
  4771. space_info->reservation_progress++;
  4772. }
  4773. spin_unlock(&cache->lock);
  4774. spin_unlock(&space_info->lock);
  4775. return ret;
  4776. }
  4777. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4778. struct btrfs_root *root)
  4779. {
  4780. struct btrfs_fs_info *fs_info = root->fs_info;
  4781. struct btrfs_caching_control *next;
  4782. struct btrfs_caching_control *caching_ctl;
  4783. struct btrfs_block_group_cache *cache;
  4784. struct btrfs_space_info *space_info;
  4785. down_write(&fs_info->extent_commit_sem);
  4786. list_for_each_entry_safe(caching_ctl, next,
  4787. &fs_info->caching_block_groups, list) {
  4788. cache = caching_ctl->block_group;
  4789. if (block_group_cache_done(cache)) {
  4790. cache->last_byte_to_unpin = (u64)-1;
  4791. list_del_init(&caching_ctl->list);
  4792. put_caching_control(caching_ctl);
  4793. } else {
  4794. cache->last_byte_to_unpin = caching_ctl->progress;
  4795. }
  4796. }
  4797. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4798. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4799. else
  4800. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4801. up_write(&fs_info->extent_commit_sem);
  4802. list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
  4803. percpu_counter_set(&space_info->total_bytes_pinned, 0);
  4804. update_global_block_rsv(fs_info);
  4805. }
  4806. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4807. {
  4808. struct btrfs_fs_info *fs_info = root->fs_info;
  4809. struct btrfs_block_group_cache *cache = NULL;
  4810. struct btrfs_space_info *space_info;
  4811. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4812. u64 len;
  4813. bool readonly;
  4814. while (start <= end) {
  4815. readonly = false;
  4816. if (!cache ||
  4817. start >= cache->key.objectid + cache->key.offset) {
  4818. if (cache)
  4819. btrfs_put_block_group(cache);
  4820. cache = btrfs_lookup_block_group(fs_info, start);
  4821. BUG_ON(!cache); /* Logic error */
  4822. }
  4823. len = cache->key.objectid + cache->key.offset - start;
  4824. len = min(len, end + 1 - start);
  4825. if (start < cache->last_byte_to_unpin) {
  4826. len = min(len, cache->last_byte_to_unpin - start);
  4827. btrfs_add_free_space(cache, start, len);
  4828. }
  4829. start += len;
  4830. space_info = cache->space_info;
  4831. spin_lock(&space_info->lock);
  4832. spin_lock(&cache->lock);
  4833. cache->pinned -= len;
  4834. space_info->bytes_pinned -= len;
  4835. if (cache->ro) {
  4836. space_info->bytes_readonly += len;
  4837. readonly = true;
  4838. }
  4839. spin_unlock(&cache->lock);
  4840. if (!readonly && global_rsv->space_info == space_info) {
  4841. spin_lock(&global_rsv->lock);
  4842. if (!global_rsv->full) {
  4843. len = min(len, global_rsv->size -
  4844. global_rsv->reserved);
  4845. global_rsv->reserved += len;
  4846. space_info->bytes_may_use += len;
  4847. if (global_rsv->reserved >= global_rsv->size)
  4848. global_rsv->full = 1;
  4849. }
  4850. spin_unlock(&global_rsv->lock);
  4851. }
  4852. spin_unlock(&space_info->lock);
  4853. }
  4854. if (cache)
  4855. btrfs_put_block_group(cache);
  4856. return 0;
  4857. }
  4858. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4859. struct btrfs_root *root)
  4860. {
  4861. struct btrfs_fs_info *fs_info = root->fs_info;
  4862. struct extent_io_tree *unpin;
  4863. u64 start;
  4864. u64 end;
  4865. int ret;
  4866. if (trans->aborted)
  4867. return 0;
  4868. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4869. unpin = &fs_info->freed_extents[1];
  4870. else
  4871. unpin = &fs_info->freed_extents[0];
  4872. while (1) {
  4873. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4874. EXTENT_DIRTY, NULL);
  4875. if (ret)
  4876. break;
  4877. if (btrfs_test_opt(root, DISCARD))
  4878. ret = btrfs_discard_extent(root, start,
  4879. end + 1 - start, NULL);
  4880. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4881. unpin_extent_range(root, start, end);
  4882. cond_resched();
  4883. }
  4884. return 0;
  4885. }
  4886. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
  4887. u64 owner, u64 root_objectid)
  4888. {
  4889. struct btrfs_space_info *space_info;
  4890. u64 flags;
  4891. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4892. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  4893. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  4894. else
  4895. flags = BTRFS_BLOCK_GROUP_METADATA;
  4896. } else {
  4897. flags = BTRFS_BLOCK_GROUP_DATA;
  4898. }
  4899. space_info = __find_space_info(fs_info, flags);
  4900. BUG_ON(!space_info); /* Logic bug */
  4901. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  4902. }
  4903. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4904. struct btrfs_root *root,
  4905. u64 bytenr, u64 num_bytes, u64 parent,
  4906. u64 root_objectid, u64 owner_objectid,
  4907. u64 owner_offset, int refs_to_drop,
  4908. struct btrfs_delayed_extent_op *extent_op)
  4909. {
  4910. struct btrfs_key key;
  4911. struct btrfs_path *path;
  4912. struct btrfs_fs_info *info = root->fs_info;
  4913. struct btrfs_root *extent_root = info->extent_root;
  4914. struct extent_buffer *leaf;
  4915. struct btrfs_extent_item *ei;
  4916. struct btrfs_extent_inline_ref *iref;
  4917. int ret;
  4918. int is_data;
  4919. int extent_slot = 0;
  4920. int found_extent = 0;
  4921. int num_to_del = 1;
  4922. u32 item_size;
  4923. u64 refs;
  4924. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  4925. SKINNY_METADATA);
  4926. path = btrfs_alloc_path();
  4927. if (!path)
  4928. return -ENOMEM;
  4929. path->reada = 1;
  4930. path->leave_spinning = 1;
  4931. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4932. BUG_ON(!is_data && refs_to_drop != 1);
  4933. if (is_data)
  4934. skinny_metadata = 0;
  4935. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4936. bytenr, num_bytes, parent,
  4937. root_objectid, owner_objectid,
  4938. owner_offset);
  4939. if (ret == 0) {
  4940. extent_slot = path->slots[0];
  4941. while (extent_slot >= 0) {
  4942. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4943. extent_slot);
  4944. if (key.objectid != bytenr)
  4945. break;
  4946. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4947. key.offset == num_bytes) {
  4948. found_extent = 1;
  4949. break;
  4950. }
  4951. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  4952. key.offset == owner_objectid) {
  4953. found_extent = 1;
  4954. break;
  4955. }
  4956. if (path->slots[0] - extent_slot > 5)
  4957. break;
  4958. extent_slot--;
  4959. }
  4960. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4961. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4962. if (found_extent && item_size < sizeof(*ei))
  4963. found_extent = 0;
  4964. #endif
  4965. if (!found_extent) {
  4966. BUG_ON(iref);
  4967. ret = remove_extent_backref(trans, extent_root, path,
  4968. NULL, refs_to_drop,
  4969. is_data);
  4970. if (ret) {
  4971. btrfs_abort_transaction(trans, extent_root, ret);
  4972. goto out;
  4973. }
  4974. btrfs_release_path(path);
  4975. path->leave_spinning = 1;
  4976. key.objectid = bytenr;
  4977. key.type = BTRFS_EXTENT_ITEM_KEY;
  4978. key.offset = num_bytes;
  4979. if (!is_data && skinny_metadata) {
  4980. key.type = BTRFS_METADATA_ITEM_KEY;
  4981. key.offset = owner_objectid;
  4982. }
  4983. ret = btrfs_search_slot(trans, extent_root,
  4984. &key, path, -1, 1);
  4985. if (ret > 0 && skinny_metadata && path->slots[0]) {
  4986. /*
  4987. * Couldn't find our skinny metadata item,
  4988. * see if we have ye olde extent item.
  4989. */
  4990. path->slots[0]--;
  4991. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4992. path->slots[0]);
  4993. if (key.objectid == bytenr &&
  4994. key.type == BTRFS_EXTENT_ITEM_KEY &&
  4995. key.offset == num_bytes)
  4996. ret = 0;
  4997. }
  4998. if (ret > 0 && skinny_metadata) {
  4999. skinny_metadata = false;
  5000. key.type = BTRFS_EXTENT_ITEM_KEY;
  5001. key.offset = num_bytes;
  5002. btrfs_release_path(path);
  5003. ret = btrfs_search_slot(trans, extent_root,
  5004. &key, path, -1, 1);
  5005. }
  5006. if (ret) {
  5007. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5008. ret, (unsigned long long)bytenr);
  5009. if (ret > 0)
  5010. btrfs_print_leaf(extent_root,
  5011. path->nodes[0]);
  5012. }
  5013. if (ret < 0) {
  5014. btrfs_abort_transaction(trans, extent_root, ret);
  5015. goto out;
  5016. }
  5017. extent_slot = path->slots[0];
  5018. }
  5019. } else if (ret == -ENOENT) {
  5020. btrfs_print_leaf(extent_root, path->nodes[0]);
  5021. WARN_ON(1);
  5022. btrfs_err(info,
  5023. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  5024. (unsigned long long)bytenr,
  5025. (unsigned long long)parent,
  5026. (unsigned long long)root_objectid,
  5027. (unsigned long long)owner_objectid,
  5028. (unsigned long long)owner_offset);
  5029. } else {
  5030. btrfs_abort_transaction(trans, extent_root, ret);
  5031. goto out;
  5032. }
  5033. leaf = path->nodes[0];
  5034. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5035. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5036. if (item_size < sizeof(*ei)) {
  5037. BUG_ON(found_extent || extent_slot != path->slots[0]);
  5038. ret = convert_extent_item_v0(trans, extent_root, path,
  5039. owner_objectid, 0);
  5040. if (ret < 0) {
  5041. btrfs_abort_transaction(trans, extent_root, ret);
  5042. goto out;
  5043. }
  5044. btrfs_release_path(path);
  5045. path->leave_spinning = 1;
  5046. key.objectid = bytenr;
  5047. key.type = BTRFS_EXTENT_ITEM_KEY;
  5048. key.offset = num_bytes;
  5049. ret = btrfs_search_slot(trans, extent_root, &key, path,
  5050. -1, 1);
  5051. if (ret) {
  5052. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5053. ret, (unsigned long long)bytenr);
  5054. btrfs_print_leaf(extent_root, path->nodes[0]);
  5055. }
  5056. if (ret < 0) {
  5057. btrfs_abort_transaction(trans, extent_root, ret);
  5058. goto out;
  5059. }
  5060. extent_slot = path->slots[0];
  5061. leaf = path->nodes[0];
  5062. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5063. }
  5064. #endif
  5065. BUG_ON(item_size < sizeof(*ei));
  5066. ei = btrfs_item_ptr(leaf, extent_slot,
  5067. struct btrfs_extent_item);
  5068. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5069. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5070. struct btrfs_tree_block_info *bi;
  5071. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5072. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5073. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5074. }
  5075. refs = btrfs_extent_refs(leaf, ei);
  5076. if (refs < refs_to_drop) {
  5077. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5078. "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
  5079. ret = -EINVAL;
  5080. btrfs_abort_transaction(trans, extent_root, ret);
  5081. goto out;
  5082. }
  5083. refs -= refs_to_drop;
  5084. if (refs > 0) {
  5085. if (extent_op)
  5086. __run_delayed_extent_op(extent_op, leaf, ei);
  5087. /*
  5088. * In the case of inline back ref, reference count will
  5089. * be updated by remove_extent_backref
  5090. */
  5091. if (iref) {
  5092. BUG_ON(!found_extent);
  5093. } else {
  5094. btrfs_set_extent_refs(leaf, ei, refs);
  5095. btrfs_mark_buffer_dirty(leaf);
  5096. }
  5097. if (found_extent) {
  5098. ret = remove_extent_backref(trans, extent_root, path,
  5099. iref, refs_to_drop,
  5100. is_data);
  5101. if (ret) {
  5102. btrfs_abort_transaction(trans, extent_root, ret);
  5103. goto out;
  5104. }
  5105. }
  5106. add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
  5107. root_objectid);
  5108. } else {
  5109. if (found_extent) {
  5110. BUG_ON(is_data && refs_to_drop !=
  5111. extent_data_ref_count(root, path, iref));
  5112. if (iref) {
  5113. BUG_ON(path->slots[0] != extent_slot);
  5114. } else {
  5115. BUG_ON(path->slots[0] != extent_slot + 1);
  5116. path->slots[0] = extent_slot;
  5117. num_to_del = 2;
  5118. }
  5119. }
  5120. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5121. num_to_del);
  5122. if (ret) {
  5123. btrfs_abort_transaction(trans, extent_root, ret);
  5124. goto out;
  5125. }
  5126. btrfs_release_path(path);
  5127. if (is_data) {
  5128. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5129. if (ret) {
  5130. btrfs_abort_transaction(trans, extent_root, ret);
  5131. goto out;
  5132. }
  5133. }
  5134. ret = update_block_group(root, bytenr, num_bytes, 0);
  5135. if (ret) {
  5136. btrfs_abort_transaction(trans, extent_root, ret);
  5137. goto out;
  5138. }
  5139. }
  5140. out:
  5141. btrfs_free_path(path);
  5142. return ret;
  5143. }
  5144. /*
  5145. * when we free an block, it is possible (and likely) that we free the last
  5146. * delayed ref for that extent as well. This searches the delayed ref tree for
  5147. * a given extent, and if there are no other delayed refs to be processed, it
  5148. * removes it from the tree.
  5149. */
  5150. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5151. struct btrfs_root *root, u64 bytenr)
  5152. {
  5153. struct btrfs_delayed_ref_head *head;
  5154. struct btrfs_delayed_ref_root *delayed_refs;
  5155. struct btrfs_delayed_ref_node *ref;
  5156. struct rb_node *node;
  5157. int ret = 0;
  5158. delayed_refs = &trans->transaction->delayed_refs;
  5159. spin_lock(&delayed_refs->lock);
  5160. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5161. if (!head)
  5162. goto out;
  5163. node = rb_prev(&head->node.rb_node);
  5164. if (!node)
  5165. goto out;
  5166. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  5167. /* there are still entries for this ref, we can't drop it */
  5168. if (ref->bytenr == bytenr)
  5169. goto out;
  5170. if (head->extent_op) {
  5171. if (!head->must_insert_reserved)
  5172. goto out;
  5173. btrfs_free_delayed_extent_op(head->extent_op);
  5174. head->extent_op = NULL;
  5175. }
  5176. /*
  5177. * waiting for the lock here would deadlock. If someone else has it
  5178. * locked they are already in the process of dropping it anyway
  5179. */
  5180. if (!mutex_trylock(&head->mutex))
  5181. goto out;
  5182. /*
  5183. * at this point we have a head with no other entries. Go
  5184. * ahead and process it.
  5185. */
  5186. head->node.in_tree = 0;
  5187. rb_erase(&head->node.rb_node, &delayed_refs->root);
  5188. delayed_refs->num_entries--;
  5189. /*
  5190. * we don't take a ref on the node because we're removing it from the
  5191. * tree, so we just steal the ref the tree was holding.
  5192. */
  5193. delayed_refs->num_heads--;
  5194. if (list_empty(&head->cluster))
  5195. delayed_refs->num_heads_ready--;
  5196. list_del_init(&head->cluster);
  5197. spin_unlock(&delayed_refs->lock);
  5198. BUG_ON(head->extent_op);
  5199. if (head->must_insert_reserved)
  5200. ret = 1;
  5201. mutex_unlock(&head->mutex);
  5202. btrfs_put_delayed_ref(&head->node);
  5203. return ret;
  5204. out:
  5205. spin_unlock(&delayed_refs->lock);
  5206. return 0;
  5207. }
  5208. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5209. struct btrfs_root *root,
  5210. struct extent_buffer *buf,
  5211. u64 parent, int last_ref)
  5212. {
  5213. struct btrfs_block_group_cache *cache = NULL;
  5214. int pin = 1;
  5215. int ret;
  5216. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5217. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5218. buf->start, buf->len,
  5219. parent, root->root_key.objectid,
  5220. btrfs_header_level(buf),
  5221. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5222. BUG_ON(ret); /* -ENOMEM */
  5223. }
  5224. if (!last_ref)
  5225. return;
  5226. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5227. if (btrfs_header_generation(buf) == trans->transid) {
  5228. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5229. ret = check_ref_cleanup(trans, root, buf->start);
  5230. if (!ret)
  5231. goto out;
  5232. }
  5233. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5234. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5235. goto out;
  5236. }
  5237. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5238. btrfs_add_free_space(cache, buf->start, buf->len);
  5239. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  5240. pin = 0;
  5241. }
  5242. out:
  5243. if (pin)
  5244. add_pinned_bytes(root->fs_info, buf->len,
  5245. btrfs_header_level(buf),
  5246. root->root_key.objectid);
  5247. /*
  5248. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5249. * anymore.
  5250. */
  5251. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5252. btrfs_put_block_group(cache);
  5253. }
  5254. /* Can return -ENOMEM */
  5255. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5256. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5257. u64 owner, u64 offset, int for_cow)
  5258. {
  5259. int ret;
  5260. struct btrfs_fs_info *fs_info = root->fs_info;
  5261. add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
  5262. /*
  5263. * tree log blocks never actually go into the extent allocation
  5264. * tree, just update pinning info and exit early.
  5265. */
  5266. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5267. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5268. /* unlocks the pinned mutex */
  5269. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5270. ret = 0;
  5271. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5272. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5273. num_bytes,
  5274. parent, root_objectid, (int)owner,
  5275. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  5276. } else {
  5277. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5278. num_bytes,
  5279. parent, root_objectid, owner,
  5280. offset, BTRFS_DROP_DELAYED_REF,
  5281. NULL, for_cow);
  5282. }
  5283. return ret;
  5284. }
  5285. static u64 stripe_align(struct btrfs_root *root,
  5286. struct btrfs_block_group_cache *cache,
  5287. u64 val, u64 num_bytes)
  5288. {
  5289. u64 ret = ALIGN(val, root->stripesize);
  5290. return ret;
  5291. }
  5292. /*
  5293. * when we wait for progress in the block group caching, its because
  5294. * our allocation attempt failed at least once. So, we must sleep
  5295. * and let some progress happen before we try again.
  5296. *
  5297. * This function will sleep at least once waiting for new free space to
  5298. * show up, and then it will check the block group free space numbers
  5299. * for our min num_bytes. Another option is to have it go ahead
  5300. * and look in the rbtree for a free extent of a given size, but this
  5301. * is a good start.
  5302. */
  5303. static noinline int
  5304. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5305. u64 num_bytes)
  5306. {
  5307. struct btrfs_caching_control *caching_ctl;
  5308. caching_ctl = get_caching_control(cache);
  5309. if (!caching_ctl)
  5310. return 0;
  5311. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5312. (cache->free_space_ctl->free_space >= num_bytes));
  5313. put_caching_control(caching_ctl);
  5314. return 0;
  5315. }
  5316. static noinline int
  5317. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5318. {
  5319. struct btrfs_caching_control *caching_ctl;
  5320. caching_ctl = get_caching_control(cache);
  5321. if (!caching_ctl)
  5322. return 0;
  5323. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5324. put_caching_control(caching_ctl);
  5325. return 0;
  5326. }
  5327. int __get_raid_index(u64 flags)
  5328. {
  5329. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5330. return BTRFS_RAID_RAID10;
  5331. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5332. return BTRFS_RAID_RAID1;
  5333. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5334. return BTRFS_RAID_DUP;
  5335. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5336. return BTRFS_RAID_RAID0;
  5337. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5338. return BTRFS_RAID_RAID5;
  5339. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5340. return BTRFS_RAID_RAID6;
  5341. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5342. }
  5343. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  5344. {
  5345. return __get_raid_index(cache->flags);
  5346. }
  5347. enum btrfs_loop_type {
  5348. LOOP_CACHING_NOWAIT = 0,
  5349. LOOP_CACHING_WAIT = 1,
  5350. LOOP_ALLOC_CHUNK = 2,
  5351. LOOP_NO_EMPTY_SIZE = 3,
  5352. };
  5353. /*
  5354. * walks the btree of allocated extents and find a hole of a given size.
  5355. * The key ins is changed to record the hole:
  5356. * ins->objectid == block start
  5357. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5358. * ins->offset == number of blocks
  5359. * Any available blocks before search_start are skipped.
  5360. */
  5361. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  5362. struct btrfs_root *orig_root,
  5363. u64 num_bytes, u64 empty_size,
  5364. u64 hint_byte, struct btrfs_key *ins,
  5365. u64 flags)
  5366. {
  5367. int ret = 0;
  5368. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5369. struct btrfs_free_cluster *last_ptr = NULL;
  5370. struct btrfs_block_group_cache *block_group = NULL;
  5371. struct btrfs_block_group_cache *used_block_group;
  5372. u64 search_start = 0;
  5373. int empty_cluster = 2 * 1024 * 1024;
  5374. struct btrfs_space_info *space_info;
  5375. int loop = 0;
  5376. int index = __get_raid_index(flags);
  5377. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5378. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5379. bool found_uncached_bg = false;
  5380. bool failed_cluster_refill = false;
  5381. bool failed_alloc = false;
  5382. bool use_cluster = true;
  5383. bool have_caching_bg = false;
  5384. WARN_ON(num_bytes < root->sectorsize);
  5385. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  5386. ins->objectid = 0;
  5387. ins->offset = 0;
  5388. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5389. space_info = __find_space_info(root->fs_info, flags);
  5390. if (!space_info) {
  5391. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5392. return -ENOSPC;
  5393. }
  5394. /*
  5395. * If the space info is for both data and metadata it means we have a
  5396. * small filesystem and we can't use the clustering stuff.
  5397. */
  5398. if (btrfs_mixed_space_info(space_info))
  5399. use_cluster = false;
  5400. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5401. last_ptr = &root->fs_info->meta_alloc_cluster;
  5402. if (!btrfs_test_opt(root, SSD))
  5403. empty_cluster = 64 * 1024;
  5404. }
  5405. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5406. btrfs_test_opt(root, SSD)) {
  5407. last_ptr = &root->fs_info->data_alloc_cluster;
  5408. }
  5409. if (last_ptr) {
  5410. spin_lock(&last_ptr->lock);
  5411. if (last_ptr->block_group)
  5412. hint_byte = last_ptr->window_start;
  5413. spin_unlock(&last_ptr->lock);
  5414. }
  5415. search_start = max(search_start, first_logical_byte(root, 0));
  5416. search_start = max(search_start, hint_byte);
  5417. if (!last_ptr)
  5418. empty_cluster = 0;
  5419. if (search_start == hint_byte) {
  5420. block_group = btrfs_lookup_block_group(root->fs_info,
  5421. search_start);
  5422. used_block_group = block_group;
  5423. /*
  5424. * we don't want to use the block group if it doesn't match our
  5425. * allocation bits, or if its not cached.
  5426. *
  5427. * However if we are re-searching with an ideal block group
  5428. * picked out then we don't care that the block group is cached.
  5429. */
  5430. if (block_group && block_group_bits(block_group, flags) &&
  5431. block_group->cached != BTRFS_CACHE_NO) {
  5432. down_read(&space_info->groups_sem);
  5433. if (list_empty(&block_group->list) ||
  5434. block_group->ro) {
  5435. /*
  5436. * someone is removing this block group,
  5437. * we can't jump into the have_block_group
  5438. * target because our list pointers are not
  5439. * valid
  5440. */
  5441. btrfs_put_block_group(block_group);
  5442. up_read(&space_info->groups_sem);
  5443. } else {
  5444. index = get_block_group_index(block_group);
  5445. goto have_block_group;
  5446. }
  5447. } else if (block_group) {
  5448. btrfs_put_block_group(block_group);
  5449. }
  5450. }
  5451. search:
  5452. have_caching_bg = false;
  5453. down_read(&space_info->groups_sem);
  5454. list_for_each_entry(block_group, &space_info->block_groups[index],
  5455. list) {
  5456. u64 offset;
  5457. int cached;
  5458. used_block_group = block_group;
  5459. btrfs_get_block_group(block_group);
  5460. search_start = block_group->key.objectid;
  5461. /*
  5462. * this can happen if we end up cycling through all the
  5463. * raid types, but we want to make sure we only allocate
  5464. * for the proper type.
  5465. */
  5466. if (!block_group_bits(block_group, flags)) {
  5467. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  5468. BTRFS_BLOCK_GROUP_RAID1 |
  5469. BTRFS_BLOCK_GROUP_RAID5 |
  5470. BTRFS_BLOCK_GROUP_RAID6 |
  5471. BTRFS_BLOCK_GROUP_RAID10;
  5472. /*
  5473. * if they asked for extra copies and this block group
  5474. * doesn't provide them, bail. This does allow us to
  5475. * fill raid0 from raid1.
  5476. */
  5477. if ((flags & extra) && !(block_group->flags & extra))
  5478. goto loop;
  5479. }
  5480. have_block_group:
  5481. cached = block_group_cache_done(block_group);
  5482. if (unlikely(!cached)) {
  5483. found_uncached_bg = true;
  5484. ret = cache_block_group(block_group, 0);
  5485. BUG_ON(ret < 0);
  5486. ret = 0;
  5487. }
  5488. if (unlikely(block_group->ro))
  5489. goto loop;
  5490. /*
  5491. * Ok we want to try and use the cluster allocator, so
  5492. * lets look there
  5493. */
  5494. if (last_ptr) {
  5495. unsigned long aligned_cluster;
  5496. /*
  5497. * the refill lock keeps out other
  5498. * people trying to start a new cluster
  5499. */
  5500. spin_lock(&last_ptr->refill_lock);
  5501. used_block_group = last_ptr->block_group;
  5502. if (used_block_group != block_group &&
  5503. (!used_block_group ||
  5504. used_block_group->ro ||
  5505. !block_group_bits(used_block_group, flags))) {
  5506. used_block_group = block_group;
  5507. goto refill_cluster;
  5508. }
  5509. if (used_block_group != block_group)
  5510. btrfs_get_block_group(used_block_group);
  5511. offset = btrfs_alloc_from_cluster(used_block_group,
  5512. last_ptr, num_bytes, used_block_group->key.objectid);
  5513. if (offset) {
  5514. /* we have a block, we're done */
  5515. spin_unlock(&last_ptr->refill_lock);
  5516. trace_btrfs_reserve_extent_cluster(root,
  5517. block_group, search_start, num_bytes);
  5518. goto checks;
  5519. }
  5520. WARN_ON(last_ptr->block_group != used_block_group);
  5521. if (used_block_group != block_group) {
  5522. btrfs_put_block_group(used_block_group);
  5523. used_block_group = block_group;
  5524. }
  5525. refill_cluster:
  5526. BUG_ON(used_block_group != block_group);
  5527. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5528. * set up a new clusters, so lets just skip it
  5529. * and let the allocator find whatever block
  5530. * it can find. If we reach this point, we
  5531. * will have tried the cluster allocator
  5532. * plenty of times and not have found
  5533. * anything, so we are likely way too
  5534. * fragmented for the clustering stuff to find
  5535. * anything.
  5536. *
  5537. * However, if the cluster is taken from the
  5538. * current block group, release the cluster
  5539. * first, so that we stand a better chance of
  5540. * succeeding in the unclustered
  5541. * allocation. */
  5542. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5543. last_ptr->block_group != block_group) {
  5544. spin_unlock(&last_ptr->refill_lock);
  5545. goto unclustered_alloc;
  5546. }
  5547. /*
  5548. * this cluster didn't work out, free it and
  5549. * start over
  5550. */
  5551. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5552. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5553. spin_unlock(&last_ptr->refill_lock);
  5554. goto unclustered_alloc;
  5555. }
  5556. aligned_cluster = max_t(unsigned long,
  5557. empty_cluster + empty_size,
  5558. block_group->full_stripe_len);
  5559. /* allocate a cluster in this block group */
  5560. ret = btrfs_find_space_cluster(trans, root,
  5561. block_group, last_ptr,
  5562. search_start, num_bytes,
  5563. aligned_cluster);
  5564. if (ret == 0) {
  5565. /*
  5566. * now pull our allocation out of this
  5567. * cluster
  5568. */
  5569. offset = btrfs_alloc_from_cluster(block_group,
  5570. last_ptr, num_bytes,
  5571. search_start);
  5572. if (offset) {
  5573. /* we found one, proceed */
  5574. spin_unlock(&last_ptr->refill_lock);
  5575. trace_btrfs_reserve_extent_cluster(root,
  5576. block_group, search_start,
  5577. num_bytes);
  5578. goto checks;
  5579. }
  5580. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5581. && !failed_cluster_refill) {
  5582. spin_unlock(&last_ptr->refill_lock);
  5583. failed_cluster_refill = true;
  5584. wait_block_group_cache_progress(block_group,
  5585. num_bytes + empty_cluster + empty_size);
  5586. goto have_block_group;
  5587. }
  5588. /*
  5589. * at this point we either didn't find a cluster
  5590. * or we weren't able to allocate a block from our
  5591. * cluster. Free the cluster we've been trying
  5592. * to use, and go to the next block group
  5593. */
  5594. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5595. spin_unlock(&last_ptr->refill_lock);
  5596. goto loop;
  5597. }
  5598. unclustered_alloc:
  5599. spin_lock(&block_group->free_space_ctl->tree_lock);
  5600. if (cached &&
  5601. block_group->free_space_ctl->free_space <
  5602. num_bytes + empty_cluster + empty_size) {
  5603. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5604. goto loop;
  5605. }
  5606. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5607. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5608. num_bytes, empty_size);
  5609. /*
  5610. * If we didn't find a chunk, and we haven't failed on this
  5611. * block group before, and this block group is in the middle of
  5612. * caching and we are ok with waiting, then go ahead and wait
  5613. * for progress to be made, and set failed_alloc to true.
  5614. *
  5615. * If failed_alloc is true then we've already waited on this
  5616. * block group once and should move on to the next block group.
  5617. */
  5618. if (!offset && !failed_alloc && !cached &&
  5619. loop > LOOP_CACHING_NOWAIT) {
  5620. wait_block_group_cache_progress(block_group,
  5621. num_bytes + empty_size);
  5622. failed_alloc = true;
  5623. goto have_block_group;
  5624. } else if (!offset) {
  5625. if (!cached)
  5626. have_caching_bg = true;
  5627. goto loop;
  5628. }
  5629. checks:
  5630. search_start = stripe_align(root, used_block_group,
  5631. offset, num_bytes);
  5632. /* move on to the next group */
  5633. if (search_start + num_bytes >
  5634. used_block_group->key.objectid + used_block_group->key.offset) {
  5635. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5636. goto loop;
  5637. }
  5638. if (offset < search_start)
  5639. btrfs_add_free_space(used_block_group, offset,
  5640. search_start - offset);
  5641. BUG_ON(offset > search_start);
  5642. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5643. alloc_type);
  5644. if (ret == -EAGAIN) {
  5645. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5646. goto loop;
  5647. }
  5648. /* we are all good, lets return */
  5649. ins->objectid = search_start;
  5650. ins->offset = num_bytes;
  5651. trace_btrfs_reserve_extent(orig_root, block_group,
  5652. search_start, num_bytes);
  5653. if (used_block_group != block_group)
  5654. btrfs_put_block_group(used_block_group);
  5655. btrfs_put_block_group(block_group);
  5656. break;
  5657. loop:
  5658. failed_cluster_refill = false;
  5659. failed_alloc = false;
  5660. BUG_ON(index != get_block_group_index(block_group));
  5661. if (used_block_group != block_group)
  5662. btrfs_put_block_group(used_block_group);
  5663. btrfs_put_block_group(block_group);
  5664. }
  5665. up_read(&space_info->groups_sem);
  5666. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5667. goto search;
  5668. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5669. goto search;
  5670. /*
  5671. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5672. * caching kthreads as we move along
  5673. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5674. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5675. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5676. * again
  5677. */
  5678. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5679. index = 0;
  5680. loop++;
  5681. if (loop == LOOP_ALLOC_CHUNK) {
  5682. ret = do_chunk_alloc(trans, root, flags,
  5683. CHUNK_ALLOC_FORCE);
  5684. /*
  5685. * Do not bail out on ENOSPC since we
  5686. * can do more things.
  5687. */
  5688. if (ret < 0 && ret != -ENOSPC) {
  5689. btrfs_abort_transaction(trans,
  5690. root, ret);
  5691. goto out;
  5692. }
  5693. }
  5694. if (loop == LOOP_NO_EMPTY_SIZE) {
  5695. empty_size = 0;
  5696. empty_cluster = 0;
  5697. }
  5698. goto search;
  5699. } else if (!ins->objectid) {
  5700. ret = -ENOSPC;
  5701. } else if (ins->objectid) {
  5702. ret = 0;
  5703. }
  5704. out:
  5705. return ret;
  5706. }
  5707. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5708. int dump_block_groups)
  5709. {
  5710. struct btrfs_block_group_cache *cache;
  5711. int index = 0;
  5712. spin_lock(&info->lock);
  5713. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5714. (unsigned long long)info->flags,
  5715. (unsigned long long)(info->total_bytes - info->bytes_used -
  5716. info->bytes_pinned - info->bytes_reserved -
  5717. info->bytes_readonly),
  5718. (info->full) ? "" : "not ");
  5719. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5720. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5721. (unsigned long long)info->total_bytes,
  5722. (unsigned long long)info->bytes_used,
  5723. (unsigned long long)info->bytes_pinned,
  5724. (unsigned long long)info->bytes_reserved,
  5725. (unsigned long long)info->bytes_may_use,
  5726. (unsigned long long)info->bytes_readonly);
  5727. spin_unlock(&info->lock);
  5728. if (!dump_block_groups)
  5729. return;
  5730. down_read(&info->groups_sem);
  5731. again:
  5732. list_for_each_entry(cache, &info->block_groups[index], list) {
  5733. spin_lock(&cache->lock);
  5734. printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
  5735. (unsigned long long)cache->key.objectid,
  5736. (unsigned long long)cache->key.offset,
  5737. (unsigned long long)btrfs_block_group_used(&cache->item),
  5738. (unsigned long long)cache->pinned,
  5739. (unsigned long long)cache->reserved,
  5740. cache->ro ? "[readonly]" : "");
  5741. btrfs_dump_free_space(cache, bytes);
  5742. spin_unlock(&cache->lock);
  5743. }
  5744. if (++index < BTRFS_NR_RAID_TYPES)
  5745. goto again;
  5746. up_read(&info->groups_sem);
  5747. }
  5748. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  5749. struct btrfs_root *root,
  5750. u64 num_bytes, u64 min_alloc_size,
  5751. u64 empty_size, u64 hint_byte,
  5752. struct btrfs_key *ins, int is_data)
  5753. {
  5754. bool final_tried = false;
  5755. u64 flags;
  5756. int ret;
  5757. flags = btrfs_get_alloc_profile(root, is_data);
  5758. again:
  5759. WARN_ON(num_bytes < root->sectorsize);
  5760. ret = find_free_extent(trans, root, num_bytes, empty_size,
  5761. hint_byte, ins, flags);
  5762. if (ret == -ENOSPC) {
  5763. if (!final_tried) {
  5764. num_bytes = num_bytes >> 1;
  5765. num_bytes = round_down(num_bytes, root->sectorsize);
  5766. num_bytes = max(num_bytes, min_alloc_size);
  5767. if (num_bytes == min_alloc_size)
  5768. final_tried = true;
  5769. goto again;
  5770. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5771. struct btrfs_space_info *sinfo;
  5772. sinfo = __find_space_info(root->fs_info, flags);
  5773. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  5774. (unsigned long long)flags,
  5775. (unsigned long long)num_bytes);
  5776. if (sinfo)
  5777. dump_space_info(sinfo, num_bytes, 1);
  5778. }
  5779. }
  5780. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5781. return ret;
  5782. }
  5783. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5784. u64 start, u64 len, int pin)
  5785. {
  5786. struct btrfs_block_group_cache *cache;
  5787. int ret = 0;
  5788. cache = btrfs_lookup_block_group(root->fs_info, start);
  5789. if (!cache) {
  5790. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  5791. (unsigned long long)start);
  5792. return -ENOSPC;
  5793. }
  5794. if (btrfs_test_opt(root, DISCARD))
  5795. ret = btrfs_discard_extent(root, start, len, NULL);
  5796. if (pin)
  5797. pin_down_extent(root, cache, start, len, 1);
  5798. else {
  5799. btrfs_add_free_space(cache, start, len);
  5800. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5801. }
  5802. btrfs_put_block_group(cache);
  5803. trace_btrfs_reserved_extent_free(root, start, len);
  5804. return ret;
  5805. }
  5806. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5807. u64 start, u64 len)
  5808. {
  5809. return __btrfs_free_reserved_extent(root, start, len, 0);
  5810. }
  5811. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5812. u64 start, u64 len)
  5813. {
  5814. return __btrfs_free_reserved_extent(root, start, len, 1);
  5815. }
  5816. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5817. struct btrfs_root *root,
  5818. u64 parent, u64 root_objectid,
  5819. u64 flags, u64 owner, u64 offset,
  5820. struct btrfs_key *ins, int ref_mod)
  5821. {
  5822. int ret;
  5823. struct btrfs_fs_info *fs_info = root->fs_info;
  5824. struct btrfs_extent_item *extent_item;
  5825. struct btrfs_extent_inline_ref *iref;
  5826. struct btrfs_path *path;
  5827. struct extent_buffer *leaf;
  5828. int type;
  5829. u32 size;
  5830. if (parent > 0)
  5831. type = BTRFS_SHARED_DATA_REF_KEY;
  5832. else
  5833. type = BTRFS_EXTENT_DATA_REF_KEY;
  5834. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5835. path = btrfs_alloc_path();
  5836. if (!path)
  5837. return -ENOMEM;
  5838. path->leave_spinning = 1;
  5839. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5840. ins, size);
  5841. if (ret) {
  5842. btrfs_free_path(path);
  5843. return ret;
  5844. }
  5845. leaf = path->nodes[0];
  5846. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5847. struct btrfs_extent_item);
  5848. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5849. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5850. btrfs_set_extent_flags(leaf, extent_item,
  5851. flags | BTRFS_EXTENT_FLAG_DATA);
  5852. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5853. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5854. if (parent > 0) {
  5855. struct btrfs_shared_data_ref *ref;
  5856. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5857. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5858. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5859. } else {
  5860. struct btrfs_extent_data_ref *ref;
  5861. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5862. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5863. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5864. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5865. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5866. }
  5867. btrfs_mark_buffer_dirty(path->nodes[0]);
  5868. btrfs_free_path(path);
  5869. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5870. if (ret) { /* -ENOENT, logic error */
  5871. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5872. (unsigned long long)ins->objectid,
  5873. (unsigned long long)ins->offset);
  5874. BUG();
  5875. }
  5876. return ret;
  5877. }
  5878. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5879. struct btrfs_root *root,
  5880. u64 parent, u64 root_objectid,
  5881. u64 flags, struct btrfs_disk_key *key,
  5882. int level, struct btrfs_key *ins)
  5883. {
  5884. int ret;
  5885. struct btrfs_fs_info *fs_info = root->fs_info;
  5886. struct btrfs_extent_item *extent_item;
  5887. struct btrfs_tree_block_info *block_info;
  5888. struct btrfs_extent_inline_ref *iref;
  5889. struct btrfs_path *path;
  5890. struct extent_buffer *leaf;
  5891. u32 size = sizeof(*extent_item) + sizeof(*iref);
  5892. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5893. SKINNY_METADATA);
  5894. if (!skinny_metadata)
  5895. size += sizeof(*block_info);
  5896. path = btrfs_alloc_path();
  5897. if (!path)
  5898. return -ENOMEM;
  5899. path->leave_spinning = 1;
  5900. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5901. ins, size);
  5902. if (ret) {
  5903. btrfs_free_path(path);
  5904. return ret;
  5905. }
  5906. leaf = path->nodes[0];
  5907. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5908. struct btrfs_extent_item);
  5909. btrfs_set_extent_refs(leaf, extent_item, 1);
  5910. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5911. btrfs_set_extent_flags(leaf, extent_item,
  5912. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5913. if (skinny_metadata) {
  5914. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5915. } else {
  5916. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5917. btrfs_set_tree_block_key(leaf, block_info, key);
  5918. btrfs_set_tree_block_level(leaf, block_info, level);
  5919. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5920. }
  5921. if (parent > 0) {
  5922. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5923. btrfs_set_extent_inline_ref_type(leaf, iref,
  5924. BTRFS_SHARED_BLOCK_REF_KEY);
  5925. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5926. } else {
  5927. btrfs_set_extent_inline_ref_type(leaf, iref,
  5928. BTRFS_TREE_BLOCK_REF_KEY);
  5929. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5930. }
  5931. btrfs_mark_buffer_dirty(leaf);
  5932. btrfs_free_path(path);
  5933. ret = update_block_group(root, ins->objectid, root->leafsize, 1);
  5934. if (ret) { /* -ENOENT, logic error */
  5935. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5936. (unsigned long long)ins->objectid,
  5937. (unsigned long long)ins->offset);
  5938. BUG();
  5939. }
  5940. return ret;
  5941. }
  5942. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5943. struct btrfs_root *root,
  5944. u64 root_objectid, u64 owner,
  5945. u64 offset, struct btrfs_key *ins)
  5946. {
  5947. int ret;
  5948. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5949. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5950. ins->offset, 0,
  5951. root_objectid, owner, offset,
  5952. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5953. return ret;
  5954. }
  5955. /*
  5956. * this is used by the tree logging recovery code. It records that
  5957. * an extent has been allocated and makes sure to clear the free
  5958. * space cache bits as well
  5959. */
  5960. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  5961. struct btrfs_root *root,
  5962. u64 root_objectid, u64 owner, u64 offset,
  5963. struct btrfs_key *ins)
  5964. {
  5965. int ret;
  5966. struct btrfs_block_group_cache *block_group;
  5967. /*
  5968. * Mixed block groups will exclude before processing the log so we only
  5969. * need to do the exlude dance if this fs isn't mixed.
  5970. */
  5971. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  5972. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  5973. if (ret)
  5974. return ret;
  5975. }
  5976. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  5977. if (!block_group)
  5978. return -EINVAL;
  5979. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  5980. RESERVE_ALLOC_NO_ACCOUNT);
  5981. BUG_ON(ret); /* logic error */
  5982. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  5983. 0, owner, offset, ins, 1);
  5984. btrfs_put_block_group(block_group);
  5985. return ret;
  5986. }
  5987. static struct extent_buffer *
  5988. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5989. u64 bytenr, u32 blocksize, int level)
  5990. {
  5991. struct extent_buffer *buf;
  5992. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5993. if (!buf)
  5994. return ERR_PTR(-ENOMEM);
  5995. btrfs_set_header_generation(buf, trans->transid);
  5996. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  5997. btrfs_tree_lock(buf);
  5998. clean_tree_block(trans, root, buf);
  5999. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  6000. btrfs_set_lock_blocking(buf);
  6001. btrfs_set_buffer_uptodate(buf);
  6002. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  6003. /*
  6004. * we allow two log transactions at a time, use different
  6005. * EXENT bit to differentiate dirty pages.
  6006. */
  6007. if (root->log_transid % 2 == 0)
  6008. set_extent_dirty(&root->dirty_log_pages, buf->start,
  6009. buf->start + buf->len - 1, GFP_NOFS);
  6010. else
  6011. set_extent_new(&root->dirty_log_pages, buf->start,
  6012. buf->start + buf->len - 1, GFP_NOFS);
  6013. } else {
  6014. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  6015. buf->start + buf->len - 1, GFP_NOFS);
  6016. }
  6017. trans->blocks_used++;
  6018. /* this returns a buffer locked for blocking */
  6019. return buf;
  6020. }
  6021. static struct btrfs_block_rsv *
  6022. use_block_rsv(struct btrfs_trans_handle *trans,
  6023. struct btrfs_root *root, u32 blocksize)
  6024. {
  6025. struct btrfs_block_rsv *block_rsv;
  6026. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  6027. int ret;
  6028. bool global_updated = false;
  6029. block_rsv = get_block_rsv(trans, root);
  6030. if (unlikely(block_rsv->size == 0))
  6031. goto try_reserve;
  6032. again:
  6033. ret = block_rsv_use_bytes(block_rsv, blocksize);
  6034. if (!ret)
  6035. return block_rsv;
  6036. if (block_rsv->failfast)
  6037. return ERR_PTR(ret);
  6038. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  6039. global_updated = true;
  6040. update_global_block_rsv(root->fs_info);
  6041. goto again;
  6042. }
  6043. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6044. static DEFINE_RATELIMIT_STATE(_rs,
  6045. DEFAULT_RATELIMIT_INTERVAL * 10,
  6046. /*DEFAULT_RATELIMIT_BURST*/ 1);
  6047. if (__ratelimit(&_rs))
  6048. WARN(1, KERN_DEBUG
  6049. "btrfs: block rsv returned %d\n", ret);
  6050. }
  6051. try_reserve:
  6052. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  6053. BTRFS_RESERVE_NO_FLUSH);
  6054. if (!ret)
  6055. return block_rsv;
  6056. /*
  6057. * If we couldn't reserve metadata bytes try and use some from
  6058. * the global reserve if its space type is the same as the global
  6059. * reservation.
  6060. */
  6061. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  6062. block_rsv->space_info == global_rsv->space_info) {
  6063. ret = block_rsv_use_bytes(global_rsv, blocksize);
  6064. if (!ret)
  6065. return global_rsv;
  6066. }
  6067. return ERR_PTR(ret);
  6068. }
  6069. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6070. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6071. {
  6072. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6073. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6074. }
  6075. /*
  6076. * finds a free extent and does all the dirty work required for allocation
  6077. * returns the key for the extent through ins, and a tree buffer for
  6078. * the first block of the extent through buf.
  6079. *
  6080. * returns the tree buffer or NULL.
  6081. */
  6082. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  6083. struct btrfs_root *root, u32 blocksize,
  6084. u64 parent, u64 root_objectid,
  6085. struct btrfs_disk_key *key, int level,
  6086. u64 hint, u64 empty_size)
  6087. {
  6088. struct btrfs_key ins;
  6089. struct btrfs_block_rsv *block_rsv;
  6090. struct extent_buffer *buf;
  6091. u64 flags = 0;
  6092. int ret;
  6093. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6094. SKINNY_METADATA);
  6095. block_rsv = use_block_rsv(trans, root, blocksize);
  6096. if (IS_ERR(block_rsv))
  6097. return ERR_CAST(block_rsv);
  6098. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  6099. empty_size, hint, &ins, 0);
  6100. if (ret) {
  6101. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6102. return ERR_PTR(ret);
  6103. }
  6104. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  6105. blocksize, level);
  6106. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  6107. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6108. if (parent == 0)
  6109. parent = ins.objectid;
  6110. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6111. } else
  6112. BUG_ON(parent > 0);
  6113. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6114. struct btrfs_delayed_extent_op *extent_op;
  6115. extent_op = btrfs_alloc_delayed_extent_op();
  6116. BUG_ON(!extent_op); /* -ENOMEM */
  6117. if (key)
  6118. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6119. else
  6120. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6121. extent_op->flags_to_set = flags;
  6122. if (skinny_metadata)
  6123. extent_op->update_key = 0;
  6124. else
  6125. extent_op->update_key = 1;
  6126. extent_op->update_flags = 1;
  6127. extent_op->is_data = 0;
  6128. extent_op->level = level;
  6129. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6130. ins.objectid,
  6131. ins.offset, parent, root_objectid,
  6132. level, BTRFS_ADD_DELAYED_EXTENT,
  6133. extent_op, 0);
  6134. BUG_ON(ret); /* -ENOMEM */
  6135. }
  6136. return buf;
  6137. }
  6138. struct walk_control {
  6139. u64 refs[BTRFS_MAX_LEVEL];
  6140. u64 flags[BTRFS_MAX_LEVEL];
  6141. struct btrfs_key update_progress;
  6142. int stage;
  6143. int level;
  6144. int shared_level;
  6145. int update_ref;
  6146. int keep_locks;
  6147. int reada_slot;
  6148. int reada_count;
  6149. int for_reloc;
  6150. };
  6151. #define DROP_REFERENCE 1
  6152. #define UPDATE_BACKREF 2
  6153. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6154. struct btrfs_root *root,
  6155. struct walk_control *wc,
  6156. struct btrfs_path *path)
  6157. {
  6158. u64 bytenr;
  6159. u64 generation;
  6160. u64 refs;
  6161. u64 flags;
  6162. u32 nritems;
  6163. u32 blocksize;
  6164. struct btrfs_key key;
  6165. struct extent_buffer *eb;
  6166. int ret;
  6167. int slot;
  6168. int nread = 0;
  6169. if (path->slots[wc->level] < wc->reada_slot) {
  6170. wc->reada_count = wc->reada_count * 2 / 3;
  6171. wc->reada_count = max(wc->reada_count, 2);
  6172. } else {
  6173. wc->reada_count = wc->reada_count * 3 / 2;
  6174. wc->reada_count = min_t(int, wc->reada_count,
  6175. BTRFS_NODEPTRS_PER_BLOCK(root));
  6176. }
  6177. eb = path->nodes[wc->level];
  6178. nritems = btrfs_header_nritems(eb);
  6179. blocksize = btrfs_level_size(root, wc->level - 1);
  6180. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6181. if (nread >= wc->reada_count)
  6182. break;
  6183. cond_resched();
  6184. bytenr = btrfs_node_blockptr(eb, slot);
  6185. generation = btrfs_node_ptr_generation(eb, slot);
  6186. if (slot == path->slots[wc->level])
  6187. goto reada;
  6188. if (wc->stage == UPDATE_BACKREF &&
  6189. generation <= root->root_key.offset)
  6190. continue;
  6191. /* We don't lock the tree block, it's OK to be racy here */
  6192. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6193. wc->level - 1, 1, &refs,
  6194. &flags);
  6195. /* We don't care about errors in readahead. */
  6196. if (ret < 0)
  6197. continue;
  6198. BUG_ON(refs == 0);
  6199. if (wc->stage == DROP_REFERENCE) {
  6200. if (refs == 1)
  6201. goto reada;
  6202. if (wc->level == 1 &&
  6203. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6204. continue;
  6205. if (!wc->update_ref ||
  6206. generation <= root->root_key.offset)
  6207. continue;
  6208. btrfs_node_key_to_cpu(eb, &key, slot);
  6209. ret = btrfs_comp_cpu_keys(&key,
  6210. &wc->update_progress);
  6211. if (ret < 0)
  6212. continue;
  6213. } else {
  6214. if (wc->level == 1 &&
  6215. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6216. continue;
  6217. }
  6218. reada:
  6219. ret = readahead_tree_block(root, bytenr, blocksize,
  6220. generation);
  6221. if (ret)
  6222. break;
  6223. nread++;
  6224. }
  6225. wc->reada_slot = slot;
  6226. }
  6227. /*
  6228. * helper to process tree block while walking down the tree.
  6229. *
  6230. * when wc->stage == UPDATE_BACKREF, this function updates
  6231. * back refs for pointers in the block.
  6232. *
  6233. * NOTE: return value 1 means we should stop walking down.
  6234. */
  6235. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  6236. struct btrfs_root *root,
  6237. struct btrfs_path *path,
  6238. struct walk_control *wc, int lookup_info)
  6239. {
  6240. int level = wc->level;
  6241. struct extent_buffer *eb = path->nodes[level];
  6242. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6243. int ret;
  6244. if (wc->stage == UPDATE_BACKREF &&
  6245. btrfs_header_owner(eb) != root->root_key.objectid)
  6246. return 1;
  6247. /*
  6248. * when reference count of tree block is 1, it won't increase
  6249. * again. once full backref flag is set, we never clear it.
  6250. */
  6251. if (lookup_info &&
  6252. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  6253. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  6254. BUG_ON(!path->locks[level]);
  6255. ret = btrfs_lookup_extent_info(trans, root,
  6256. eb->start, level, 1,
  6257. &wc->refs[level],
  6258. &wc->flags[level]);
  6259. BUG_ON(ret == -ENOMEM);
  6260. if (ret)
  6261. return ret;
  6262. BUG_ON(wc->refs[level] == 0);
  6263. }
  6264. if (wc->stage == DROP_REFERENCE) {
  6265. if (wc->refs[level] > 1)
  6266. return 1;
  6267. if (path->locks[level] && !wc->keep_locks) {
  6268. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6269. path->locks[level] = 0;
  6270. }
  6271. return 0;
  6272. }
  6273. /* wc->stage == UPDATE_BACKREF */
  6274. if (!(wc->flags[level] & flag)) {
  6275. BUG_ON(!path->locks[level]);
  6276. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  6277. BUG_ON(ret); /* -ENOMEM */
  6278. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  6279. BUG_ON(ret); /* -ENOMEM */
  6280. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  6281. eb->len, flag,
  6282. btrfs_header_level(eb), 0);
  6283. BUG_ON(ret); /* -ENOMEM */
  6284. wc->flags[level] |= flag;
  6285. }
  6286. /*
  6287. * the block is shared by multiple trees, so it's not good to
  6288. * keep the tree lock
  6289. */
  6290. if (path->locks[level] && level > 0) {
  6291. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6292. path->locks[level] = 0;
  6293. }
  6294. return 0;
  6295. }
  6296. /*
  6297. * helper to process tree block pointer.
  6298. *
  6299. * when wc->stage == DROP_REFERENCE, this function checks
  6300. * reference count of the block pointed to. if the block
  6301. * is shared and we need update back refs for the subtree
  6302. * rooted at the block, this function changes wc->stage to
  6303. * UPDATE_BACKREF. if the block is shared and there is no
  6304. * need to update back, this function drops the reference
  6305. * to the block.
  6306. *
  6307. * NOTE: return value 1 means we should stop walking down.
  6308. */
  6309. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  6310. struct btrfs_root *root,
  6311. struct btrfs_path *path,
  6312. struct walk_control *wc, int *lookup_info)
  6313. {
  6314. u64 bytenr;
  6315. u64 generation;
  6316. u64 parent;
  6317. u32 blocksize;
  6318. struct btrfs_key key;
  6319. struct extent_buffer *next;
  6320. int level = wc->level;
  6321. int reada = 0;
  6322. int ret = 0;
  6323. generation = btrfs_node_ptr_generation(path->nodes[level],
  6324. path->slots[level]);
  6325. /*
  6326. * if the lower level block was created before the snapshot
  6327. * was created, we know there is no need to update back refs
  6328. * for the subtree
  6329. */
  6330. if (wc->stage == UPDATE_BACKREF &&
  6331. generation <= root->root_key.offset) {
  6332. *lookup_info = 1;
  6333. return 1;
  6334. }
  6335. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  6336. blocksize = btrfs_level_size(root, level - 1);
  6337. next = btrfs_find_tree_block(root, bytenr, blocksize);
  6338. if (!next) {
  6339. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6340. if (!next)
  6341. return -ENOMEM;
  6342. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  6343. level - 1);
  6344. reada = 1;
  6345. }
  6346. btrfs_tree_lock(next);
  6347. btrfs_set_lock_blocking(next);
  6348. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  6349. &wc->refs[level - 1],
  6350. &wc->flags[level - 1]);
  6351. if (ret < 0) {
  6352. btrfs_tree_unlock(next);
  6353. return ret;
  6354. }
  6355. if (unlikely(wc->refs[level - 1] == 0)) {
  6356. btrfs_err(root->fs_info, "Missing references.");
  6357. BUG();
  6358. }
  6359. *lookup_info = 0;
  6360. if (wc->stage == DROP_REFERENCE) {
  6361. if (wc->refs[level - 1] > 1) {
  6362. if (level == 1 &&
  6363. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6364. goto skip;
  6365. if (!wc->update_ref ||
  6366. generation <= root->root_key.offset)
  6367. goto skip;
  6368. btrfs_node_key_to_cpu(path->nodes[level], &key,
  6369. path->slots[level]);
  6370. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  6371. if (ret < 0)
  6372. goto skip;
  6373. wc->stage = UPDATE_BACKREF;
  6374. wc->shared_level = level - 1;
  6375. }
  6376. } else {
  6377. if (level == 1 &&
  6378. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6379. goto skip;
  6380. }
  6381. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  6382. btrfs_tree_unlock(next);
  6383. free_extent_buffer(next);
  6384. next = NULL;
  6385. *lookup_info = 1;
  6386. }
  6387. if (!next) {
  6388. if (reada && level == 1)
  6389. reada_walk_down(trans, root, wc, path);
  6390. next = read_tree_block(root, bytenr, blocksize, generation);
  6391. if (!next || !extent_buffer_uptodate(next)) {
  6392. free_extent_buffer(next);
  6393. return -EIO;
  6394. }
  6395. btrfs_tree_lock(next);
  6396. btrfs_set_lock_blocking(next);
  6397. }
  6398. level--;
  6399. BUG_ON(level != btrfs_header_level(next));
  6400. path->nodes[level] = next;
  6401. path->slots[level] = 0;
  6402. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6403. wc->level = level;
  6404. if (wc->level == 1)
  6405. wc->reada_slot = 0;
  6406. return 0;
  6407. skip:
  6408. wc->refs[level - 1] = 0;
  6409. wc->flags[level - 1] = 0;
  6410. if (wc->stage == DROP_REFERENCE) {
  6411. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  6412. parent = path->nodes[level]->start;
  6413. } else {
  6414. BUG_ON(root->root_key.objectid !=
  6415. btrfs_header_owner(path->nodes[level]));
  6416. parent = 0;
  6417. }
  6418. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  6419. root->root_key.objectid, level - 1, 0, 0);
  6420. BUG_ON(ret); /* -ENOMEM */
  6421. }
  6422. btrfs_tree_unlock(next);
  6423. free_extent_buffer(next);
  6424. *lookup_info = 1;
  6425. return 1;
  6426. }
  6427. /*
  6428. * helper to process tree block while walking up the tree.
  6429. *
  6430. * when wc->stage == DROP_REFERENCE, this function drops
  6431. * reference count on the block.
  6432. *
  6433. * when wc->stage == UPDATE_BACKREF, this function changes
  6434. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  6435. * to UPDATE_BACKREF previously while processing the block.
  6436. *
  6437. * NOTE: return value 1 means we should stop walking up.
  6438. */
  6439. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  6440. struct btrfs_root *root,
  6441. struct btrfs_path *path,
  6442. struct walk_control *wc)
  6443. {
  6444. int ret;
  6445. int level = wc->level;
  6446. struct extent_buffer *eb = path->nodes[level];
  6447. u64 parent = 0;
  6448. if (wc->stage == UPDATE_BACKREF) {
  6449. BUG_ON(wc->shared_level < level);
  6450. if (level < wc->shared_level)
  6451. goto out;
  6452. ret = find_next_key(path, level + 1, &wc->update_progress);
  6453. if (ret > 0)
  6454. wc->update_ref = 0;
  6455. wc->stage = DROP_REFERENCE;
  6456. wc->shared_level = -1;
  6457. path->slots[level] = 0;
  6458. /*
  6459. * check reference count again if the block isn't locked.
  6460. * we should start walking down the tree again if reference
  6461. * count is one.
  6462. */
  6463. if (!path->locks[level]) {
  6464. BUG_ON(level == 0);
  6465. btrfs_tree_lock(eb);
  6466. btrfs_set_lock_blocking(eb);
  6467. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6468. ret = btrfs_lookup_extent_info(trans, root,
  6469. eb->start, level, 1,
  6470. &wc->refs[level],
  6471. &wc->flags[level]);
  6472. if (ret < 0) {
  6473. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6474. path->locks[level] = 0;
  6475. return ret;
  6476. }
  6477. BUG_ON(wc->refs[level] == 0);
  6478. if (wc->refs[level] == 1) {
  6479. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6480. path->locks[level] = 0;
  6481. return 1;
  6482. }
  6483. }
  6484. }
  6485. /* wc->stage == DROP_REFERENCE */
  6486. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6487. if (wc->refs[level] == 1) {
  6488. if (level == 0) {
  6489. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6490. ret = btrfs_dec_ref(trans, root, eb, 1,
  6491. wc->for_reloc);
  6492. else
  6493. ret = btrfs_dec_ref(trans, root, eb, 0,
  6494. wc->for_reloc);
  6495. BUG_ON(ret); /* -ENOMEM */
  6496. }
  6497. /* make block locked assertion in clean_tree_block happy */
  6498. if (!path->locks[level] &&
  6499. btrfs_header_generation(eb) == trans->transid) {
  6500. btrfs_tree_lock(eb);
  6501. btrfs_set_lock_blocking(eb);
  6502. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6503. }
  6504. clean_tree_block(trans, root, eb);
  6505. }
  6506. if (eb == root->node) {
  6507. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6508. parent = eb->start;
  6509. else
  6510. BUG_ON(root->root_key.objectid !=
  6511. btrfs_header_owner(eb));
  6512. } else {
  6513. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6514. parent = path->nodes[level + 1]->start;
  6515. else
  6516. BUG_ON(root->root_key.objectid !=
  6517. btrfs_header_owner(path->nodes[level + 1]));
  6518. }
  6519. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6520. out:
  6521. wc->refs[level] = 0;
  6522. wc->flags[level] = 0;
  6523. return 0;
  6524. }
  6525. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6526. struct btrfs_root *root,
  6527. struct btrfs_path *path,
  6528. struct walk_control *wc)
  6529. {
  6530. int level = wc->level;
  6531. int lookup_info = 1;
  6532. int ret;
  6533. while (level >= 0) {
  6534. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6535. if (ret > 0)
  6536. break;
  6537. if (level == 0)
  6538. break;
  6539. if (path->slots[level] >=
  6540. btrfs_header_nritems(path->nodes[level]))
  6541. break;
  6542. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6543. if (ret > 0) {
  6544. path->slots[level]++;
  6545. continue;
  6546. } else if (ret < 0)
  6547. return ret;
  6548. level = wc->level;
  6549. }
  6550. return 0;
  6551. }
  6552. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6553. struct btrfs_root *root,
  6554. struct btrfs_path *path,
  6555. struct walk_control *wc, int max_level)
  6556. {
  6557. int level = wc->level;
  6558. int ret;
  6559. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6560. while (level < max_level && path->nodes[level]) {
  6561. wc->level = level;
  6562. if (path->slots[level] + 1 <
  6563. btrfs_header_nritems(path->nodes[level])) {
  6564. path->slots[level]++;
  6565. return 0;
  6566. } else {
  6567. ret = walk_up_proc(trans, root, path, wc);
  6568. if (ret > 0)
  6569. return 0;
  6570. if (path->locks[level]) {
  6571. btrfs_tree_unlock_rw(path->nodes[level],
  6572. path->locks[level]);
  6573. path->locks[level] = 0;
  6574. }
  6575. free_extent_buffer(path->nodes[level]);
  6576. path->nodes[level] = NULL;
  6577. level++;
  6578. }
  6579. }
  6580. return 1;
  6581. }
  6582. /*
  6583. * drop a subvolume tree.
  6584. *
  6585. * this function traverses the tree freeing any blocks that only
  6586. * referenced by the tree.
  6587. *
  6588. * when a shared tree block is found. this function decreases its
  6589. * reference count by one. if update_ref is true, this function
  6590. * also make sure backrefs for the shared block and all lower level
  6591. * blocks are properly updated.
  6592. *
  6593. * If called with for_reloc == 0, may exit early with -EAGAIN
  6594. */
  6595. int btrfs_drop_snapshot(struct btrfs_root *root,
  6596. struct btrfs_block_rsv *block_rsv, int update_ref,
  6597. int for_reloc)
  6598. {
  6599. struct btrfs_path *path;
  6600. struct btrfs_trans_handle *trans;
  6601. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6602. struct btrfs_root_item *root_item = &root->root_item;
  6603. struct walk_control *wc;
  6604. struct btrfs_key key;
  6605. int err = 0;
  6606. int ret;
  6607. int level;
  6608. bool root_dropped = false;
  6609. path = btrfs_alloc_path();
  6610. if (!path) {
  6611. err = -ENOMEM;
  6612. goto out;
  6613. }
  6614. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6615. if (!wc) {
  6616. btrfs_free_path(path);
  6617. err = -ENOMEM;
  6618. goto out;
  6619. }
  6620. trans = btrfs_start_transaction(tree_root, 0);
  6621. if (IS_ERR(trans)) {
  6622. err = PTR_ERR(trans);
  6623. goto out_free;
  6624. }
  6625. if (block_rsv)
  6626. trans->block_rsv = block_rsv;
  6627. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6628. level = btrfs_header_level(root->node);
  6629. path->nodes[level] = btrfs_lock_root_node(root);
  6630. btrfs_set_lock_blocking(path->nodes[level]);
  6631. path->slots[level] = 0;
  6632. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6633. memset(&wc->update_progress, 0,
  6634. sizeof(wc->update_progress));
  6635. } else {
  6636. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6637. memcpy(&wc->update_progress, &key,
  6638. sizeof(wc->update_progress));
  6639. level = root_item->drop_level;
  6640. BUG_ON(level == 0);
  6641. path->lowest_level = level;
  6642. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6643. path->lowest_level = 0;
  6644. if (ret < 0) {
  6645. err = ret;
  6646. goto out_end_trans;
  6647. }
  6648. WARN_ON(ret > 0);
  6649. /*
  6650. * unlock our path, this is safe because only this
  6651. * function is allowed to delete this snapshot
  6652. */
  6653. btrfs_unlock_up_safe(path, 0);
  6654. level = btrfs_header_level(root->node);
  6655. while (1) {
  6656. btrfs_tree_lock(path->nodes[level]);
  6657. btrfs_set_lock_blocking(path->nodes[level]);
  6658. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6659. ret = btrfs_lookup_extent_info(trans, root,
  6660. path->nodes[level]->start,
  6661. level, 1, &wc->refs[level],
  6662. &wc->flags[level]);
  6663. if (ret < 0) {
  6664. err = ret;
  6665. goto out_end_trans;
  6666. }
  6667. BUG_ON(wc->refs[level] == 0);
  6668. if (level == root_item->drop_level)
  6669. break;
  6670. btrfs_tree_unlock(path->nodes[level]);
  6671. path->locks[level] = 0;
  6672. WARN_ON(wc->refs[level] != 1);
  6673. level--;
  6674. }
  6675. }
  6676. wc->level = level;
  6677. wc->shared_level = -1;
  6678. wc->stage = DROP_REFERENCE;
  6679. wc->update_ref = update_ref;
  6680. wc->keep_locks = 0;
  6681. wc->for_reloc = for_reloc;
  6682. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6683. while (1) {
  6684. ret = walk_down_tree(trans, root, path, wc);
  6685. if (ret < 0) {
  6686. err = ret;
  6687. break;
  6688. }
  6689. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6690. if (ret < 0) {
  6691. err = ret;
  6692. break;
  6693. }
  6694. if (ret > 0) {
  6695. BUG_ON(wc->stage != DROP_REFERENCE);
  6696. break;
  6697. }
  6698. if (wc->stage == DROP_REFERENCE) {
  6699. level = wc->level;
  6700. btrfs_node_key(path->nodes[level],
  6701. &root_item->drop_progress,
  6702. path->slots[level]);
  6703. root_item->drop_level = level;
  6704. }
  6705. BUG_ON(wc->level == 0);
  6706. if (btrfs_should_end_transaction(trans, tree_root) ||
  6707. (!for_reloc && btrfs_need_cleaner_sleep(root))) {
  6708. ret = btrfs_update_root(trans, tree_root,
  6709. &root->root_key,
  6710. root_item);
  6711. if (ret) {
  6712. btrfs_abort_transaction(trans, tree_root, ret);
  6713. err = ret;
  6714. goto out_end_trans;
  6715. }
  6716. btrfs_end_transaction_throttle(trans, tree_root);
  6717. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  6718. pr_debug("btrfs: drop snapshot early exit\n");
  6719. err = -EAGAIN;
  6720. goto out_free;
  6721. }
  6722. trans = btrfs_start_transaction(tree_root, 0);
  6723. if (IS_ERR(trans)) {
  6724. err = PTR_ERR(trans);
  6725. goto out_free;
  6726. }
  6727. if (block_rsv)
  6728. trans->block_rsv = block_rsv;
  6729. }
  6730. }
  6731. btrfs_release_path(path);
  6732. if (err)
  6733. goto out_end_trans;
  6734. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6735. if (ret) {
  6736. btrfs_abort_transaction(trans, tree_root, ret);
  6737. goto out_end_trans;
  6738. }
  6739. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6740. ret = btrfs_find_root(tree_root, &root->root_key, path,
  6741. NULL, NULL);
  6742. if (ret < 0) {
  6743. btrfs_abort_transaction(trans, tree_root, ret);
  6744. err = ret;
  6745. goto out_end_trans;
  6746. } else if (ret > 0) {
  6747. /* if we fail to delete the orphan item this time
  6748. * around, it'll get picked up the next time.
  6749. *
  6750. * The most common failure here is just -ENOENT.
  6751. */
  6752. btrfs_del_orphan_item(trans, tree_root,
  6753. root->root_key.objectid);
  6754. }
  6755. }
  6756. if (root->in_radix) {
  6757. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  6758. } else {
  6759. free_extent_buffer(root->node);
  6760. free_extent_buffer(root->commit_root);
  6761. btrfs_put_fs_root(root);
  6762. }
  6763. root_dropped = true;
  6764. out_end_trans:
  6765. btrfs_end_transaction_throttle(trans, tree_root);
  6766. out_free:
  6767. kfree(wc);
  6768. btrfs_free_path(path);
  6769. out:
  6770. /*
  6771. * So if we need to stop dropping the snapshot for whatever reason we
  6772. * need to make sure to add it back to the dead root list so that we
  6773. * keep trying to do the work later. This also cleans up roots if we
  6774. * don't have it in the radix (like when we recover after a power fail
  6775. * or unmount) so we don't leak memory.
  6776. */
  6777. if (root_dropped == false)
  6778. btrfs_add_dead_root(root);
  6779. if (err)
  6780. btrfs_std_error(root->fs_info, err);
  6781. return err;
  6782. }
  6783. /*
  6784. * drop subtree rooted at tree block 'node'.
  6785. *
  6786. * NOTE: this function will unlock and release tree block 'node'
  6787. * only used by relocation code
  6788. */
  6789. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6790. struct btrfs_root *root,
  6791. struct extent_buffer *node,
  6792. struct extent_buffer *parent)
  6793. {
  6794. struct btrfs_path *path;
  6795. struct walk_control *wc;
  6796. int level;
  6797. int parent_level;
  6798. int ret = 0;
  6799. int wret;
  6800. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6801. path = btrfs_alloc_path();
  6802. if (!path)
  6803. return -ENOMEM;
  6804. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6805. if (!wc) {
  6806. btrfs_free_path(path);
  6807. return -ENOMEM;
  6808. }
  6809. btrfs_assert_tree_locked(parent);
  6810. parent_level = btrfs_header_level(parent);
  6811. extent_buffer_get(parent);
  6812. path->nodes[parent_level] = parent;
  6813. path->slots[parent_level] = btrfs_header_nritems(parent);
  6814. btrfs_assert_tree_locked(node);
  6815. level = btrfs_header_level(node);
  6816. path->nodes[level] = node;
  6817. path->slots[level] = 0;
  6818. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6819. wc->refs[parent_level] = 1;
  6820. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6821. wc->level = level;
  6822. wc->shared_level = -1;
  6823. wc->stage = DROP_REFERENCE;
  6824. wc->update_ref = 0;
  6825. wc->keep_locks = 1;
  6826. wc->for_reloc = 1;
  6827. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6828. while (1) {
  6829. wret = walk_down_tree(trans, root, path, wc);
  6830. if (wret < 0) {
  6831. ret = wret;
  6832. break;
  6833. }
  6834. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6835. if (wret < 0)
  6836. ret = wret;
  6837. if (wret != 0)
  6838. break;
  6839. }
  6840. kfree(wc);
  6841. btrfs_free_path(path);
  6842. return ret;
  6843. }
  6844. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6845. {
  6846. u64 num_devices;
  6847. u64 stripped;
  6848. /*
  6849. * if restripe for this chunk_type is on pick target profile and
  6850. * return, otherwise do the usual balance
  6851. */
  6852. stripped = get_restripe_target(root->fs_info, flags);
  6853. if (stripped)
  6854. return extended_to_chunk(stripped);
  6855. /*
  6856. * we add in the count of missing devices because we want
  6857. * to make sure that any RAID levels on a degraded FS
  6858. * continue to be honored.
  6859. */
  6860. num_devices = root->fs_info->fs_devices->rw_devices +
  6861. root->fs_info->fs_devices->missing_devices;
  6862. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6863. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  6864. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6865. if (num_devices == 1) {
  6866. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6867. stripped = flags & ~stripped;
  6868. /* turn raid0 into single device chunks */
  6869. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6870. return stripped;
  6871. /* turn mirroring into duplication */
  6872. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6873. BTRFS_BLOCK_GROUP_RAID10))
  6874. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6875. } else {
  6876. /* they already had raid on here, just return */
  6877. if (flags & stripped)
  6878. return flags;
  6879. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6880. stripped = flags & ~stripped;
  6881. /* switch duplicated blocks with raid1 */
  6882. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6883. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6884. /* this is drive concat, leave it alone */
  6885. }
  6886. return flags;
  6887. }
  6888. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6889. {
  6890. struct btrfs_space_info *sinfo = cache->space_info;
  6891. u64 num_bytes;
  6892. u64 min_allocable_bytes;
  6893. int ret = -ENOSPC;
  6894. /*
  6895. * We need some metadata space and system metadata space for
  6896. * allocating chunks in some corner cases until we force to set
  6897. * it to be readonly.
  6898. */
  6899. if ((sinfo->flags &
  6900. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6901. !force)
  6902. min_allocable_bytes = 1 * 1024 * 1024;
  6903. else
  6904. min_allocable_bytes = 0;
  6905. spin_lock(&sinfo->lock);
  6906. spin_lock(&cache->lock);
  6907. if (cache->ro) {
  6908. ret = 0;
  6909. goto out;
  6910. }
  6911. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6912. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6913. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6914. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6915. min_allocable_bytes <= sinfo->total_bytes) {
  6916. sinfo->bytes_readonly += num_bytes;
  6917. cache->ro = 1;
  6918. ret = 0;
  6919. }
  6920. out:
  6921. spin_unlock(&cache->lock);
  6922. spin_unlock(&sinfo->lock);
  6923. return ret;
  6924. }
  6925. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6926. struct btrfs_block_group_cache *cache)
  6927. {
  6928. struct btrfs_trans_handle *trans;
  6929. u64 alloc_flags;
  6930. int ret;
  6931. BUG_ON(cache->ro);
  6932. trans = btrfs_join_transaction(root);
  6933. if (IS_ERR(trans))
  6934. return PTR_ERR(trans);
  6935. alloc_flags = update_block_group_flags(root, cache->flags);
  6936. if (alloc_flags != cache->flags) {
  6937. ret = do_chunk_alloc(trans, root, alloc_flags,
  6938. CHUNK_ALLOC_FORCE);
  6939. if (ret < 0)
  6940. goto out;
  6941. }
  6942. ret = set_block_group_ro(cache, 0);
  6943. if (!ret)
  6944. goto out;
  6945. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6946. ret = do_chunk_alloc(trans, root, alloc_flags,
  6947. CHUNK_ALLOC_FORCE);
  6948. if (ret < 0)
  6949. goto out;
  6950. ret = set_block_group_ro(cache, 0);
  6951. out:
  6952. btrfs_end_transaction(trans, root);
  6953. return ret;
  6954. }
  6955. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6956. struct btrfs_root *root, u64 type)
  6957. {
  6958. u64 alloc_flags = get_alloc_profile(root, type);
  6959. return do_chunk_alloc(trans, root, alloc_flags,
  6960. CHUNK_ALLOC_FORCE);
  6961. }
  6962. /*
  6963. * helper to account the unused space of all the readonly block group in the
  6964. * list. takes mirrors into account.
  6965. */
  6966. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6967. {
  6968. struct btrfs_block_group_cache *block_group;
  6969. u64 free_bytes = 0;
  6970. int factor;
  6971. list_for_each_entry(block_group, groups_list, list) {
  6972. spin_lock(&block_group->lock);
  6973. if (!block_group->ro) {
  6974. spin_unlock(&block_group->lock);
  6975. continue;
  6976. }
  6977. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6978. BTRFS_BLOCK_GROUP_RAID10 |
  6979. BTRFS_BLOCK_GROUP_DUP))
  6980. factor = 2;
  6981. else
  6982. factor = 1;
  6983. free_bytes += (block_group->key.offset -
  6984. btrfs_block_group_used(&block_group->item)) *
  6985. factor;
  6986. spin_unlock(&block_group->lock);
  6987. }
  6988. return free_bytes;
  6989. }
  6990. /*
  6991. * helper to account the unused space of all the readonly block group in the
  6992. * space_info. takes mirrors into account.
  6993. */
  6994. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  6995. {
  6996. int i;
  6997. u64 free_bytes = 0;
  6998. spin_lock(&sinfo->lock);
  6999. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  7000. if (!list_empty(&sinfo->block_groups[i]))
  7001. free_bytes += __btrfs_get_ro_block_group_free_space(
  7002. &sinfo->block_groups[i]);
  7003. spin_unlock(&sinfo->lock);
  7004. return free_bytes;
  7005. }
  7006. void btrfs_set_block_group_rw(struct btrfs_root *root,
  7007. struct btrfs_block_group_cache *cache)
  7008. {
  7009. struct btrfs_space_info *sinfo = cache->space_info;
  7010. u64 num_bytes;
  7011. BUG_ON(!cache->ro);
  7012. spin_lock(&sinfo->lock);
  7013. spin_lock(&cache->lock);
  7014. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7015. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7016. sinfo->bytes_readonly -= num_bytes;
  7017. cache->ro = 0;
  7018. spin_unlock(&cache->lock);
  7019. spin_unlock(&sinfo->lock);
  7020. }
  7021. /*
  7022. * checks to see if its even possible to relocate this block group.
  7023. *
  7024. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7025. * ok to go ahead and try.
  7026. */
  7027. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7028. {
  7029. struct btrfs_block_group_cache *block_group;
  7030. struct btrfs_space_info *space_info;
  7031. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7032. struct btrfs_device *device;
  7033. struct btrfs_trans_handle *trans;
  7034. u64 min_free;
  7035. u64 dev_min = 1;
  7036. u64 dev_nr = 0;
  7037. u64 target;
  7038. int index;
  7039. int full = 0;
  7040. int ret = 0;
  7041. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7042. /* odd, couldn't find the block group, leave it alone */
  7043. if (!block_group)
  7044. return -1;
  7045. min_free = btrfs_block_group_used(&block_group->item);
  7046. /* no bytes used, we're good */
  7047. if (!min_free)
  7048. goto out;
  7049. space_info = block_group->space_info;
  7050. spin_lock(&space_info->lock);
  7051. full = space_info->full;
  7052. /*
  7053. * if this is the last block group we have in this space, we can't
  7054. * relocate it unless we're able to allocate a new chunk below.
  7055. *
  7056. * Otherwise, we need to make sure we have room in the space to handle
  7057. * all of the extents from this block group. If we can, we're good
  7058. */
  7059. if ((space_info->total_bytes != block_group->key.offset) &&
  7060. (space_info->bytes_used + space_info->bytes_reserved +
  7061. space_info->bytes_pinned + space_info->bytes_readonly +
  7062. min_free < space_info->total_bytes)) {
  7063. spin_unlock(&space_info->lock);
  7064. goto out;
  7065. }
  7066. spin_unlock(&space_info->lock);
  7067. /*
  7068. * ok we don't have enough space, but maybe we have free space on our
  7069. * devices to allocate new chunks for relocation, so loop through our
  7070. * alloc devices and guess if we have enough space. if this block
  7071. * group is going to be restriped, run checks against the target
  7072. * profile instead of the current one.
  7073. */
  7074. ret = -1;
  7075. /*
  7076. * index:
  7077. * 0: raid10
  7078. * 1: raid1
  7079. * 2: dup
  7080. * 3: raid0
  7081. * 4: single
  7082. */
  7083. target = get_restripe_target(root->fs_info, block_group->flags);
  7084. if (target) {
  7085. index = __get_raid_index(extended_to_chunk(target));
  7086. } else {
  7087. /*
  7088. * this is just a balance, so if we were marked as full
  7089. * we know there is no space for a new chunk
  7090. */
  7091. if (full)
  7092. goto out;
  7093. index = get_block_group_index(block_group);
  7094. }
  7095. if (index == BTRFS_RAID_RAID10) {
  7096. dev_min = 4;
  7097. /* Divide by 2 */
  7098. min_free >>= 1;
  7099. } else if (index == BTRFS_RAID_RAID1) {
  7100. dev_min = 2;
  7101. } else if (index == BTRFS_RAID_DUP) {
  7102. /* Multiply by 2 */
  7103. min_free <<= 1;
  7104. } else if (index == BTRFS_RAID_RAID0) {
  7105. dev_min = fs_devices->rw_devices;
  7106. do_div(min_free, dev_min);
  7107. }
  7108. /* We need to do this so that we can look at pending chunks */
  7109. trans = btrfs_join_transaction(root);
  7110. if (IS_ERR(trans)) {
  7111. ret = PTR_ERR(trans);
  7112. goto out;
  7113. }
  7114. mutex_lock(&root->fs_info->chunk_mutex);
  7115. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7116. u64 dev_offset;
  7117. /*
  7118. * check to make sure we can actually find a chunk with enough
  7119. * space to fit our block group in.
  7120. */
  7121. if (device->total_bytes > device->bytes_used + min_free &&
  7122. !device->is_tgtdev_for_dev_replace) {
  7123. ret = find_free_dev_extent(trans, device, min_free,
  7124. &dev_offset, NULL);
  7125. if (!ret)
  7126. dev_nr++;
  7127. if (dev_nr >= dev_min)
  7128. break;
  7129. ret = -1;
  7130. }
  7131. }
  7132. mutex_unlock(&root->fs_info->chunk_mutex);
  7133. btrfs_end_transaction(trans, root);
  7134. out:
  7135. btrfs_put_block_group(block_group);
  7136. return ret;
  7137. }
  7138. static int find_first_block_group(struct btrfs_root *root,
  7139. struct btrfs_path *path, struct btrfs_key *key)
  7140. {
  7141. int ret = 0;
  7142. struct btrfs_key found_key;
  7143. struct extent_buffer *leaf;
  7144. int slot;
  7145. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7146. if (ret < 0)
  7147. goto out;
  7148. while (1) {
  7149. slot = path->slots[0];
  7150. leaf = path->nodes[0];
  7151. if (slot >= btrfs_header_nritems(leaf)) {
  7152. ret = btrfs_next_leaf(root, path);
  7153. if (ret == 0)
  7154. continue;
  7155. if (ret < 0)
  7156. goto out;
  7157. break;
  7158. }
  7159. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7160. if (found_key.objectid >= key->objectid &&
  7161. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7162. ret = 0;
  7163. goto out;
  7164. }
  7165. path->slots[0]++;
  7166. }
  7167. out:
  7168. return ret;
  7169. }
  7170. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7171. {
  7172. struct btrfs_block_group_cache *block_group;
  7173. u64 last = 0;
  7174. while (1) {
  7175. struct inode *inode;
  7176. block_group = btrfs_lookup_first_block_group(info, last);
  7177. while (block_group) {
  7178. spin_lock(&block_group->lock);
  7179. if (block_group->iref)
  7180. break;
  7181. spin_unlock(&block_group->lock);
  7182. block_group = next_block_group(info->tree_root,
  7183. block_group);
  7184. }
  7185. if (!block_group) {
  7186. if (last == 0)
  7187. break;
  7188. last = 0;
  7189. continue;
  7190. }
  7191. inode = block_group->inode;
  7192. block_group->iref = 0;
  7193. block_group->inode = NULL;
  7194. spin_unlock(&block_group->lock);
  7195. iput(inode);
  7196. last = block_group->key.objectid + block_group->key.offset;
  7197. btrfs_put_block_group(block_group);
  7198. }
  7199. }
  7200. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7201. {
  7202. struct btrfs_block_group_cache *block_group;
  7203. struct btrfs_space_info *space_info;
  7204. struct btrfs_caching_control *caching_ctl;
  7205. struct rb_node *n;
  7206. down_write(&info->extent_commit_sem);
  7207. while (!list_empty(&info->caching_block_groups)) {
  7208. caching_ctl = list_entry(info->caching_block_groups.next,
  7209. struct btrfs_caching_control, list);
  7210. list_del(&caching_ctl->list);
  7211. put_caching_control(caching_ctl);
  7212. }
  7213. up_write(&info->extent_commit_sem);
  7214. spin_lock(&info->block_group_cache_lock);
  7215. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7216. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7217. cache_node);
  7218. rb_erase(&block_group->cache_node,
  7219. &info->block_group_cache_tree);
  7220. spin_unlock(&info->block_group_cache_lock);
  7221. down_write(&block_group->space_info->groups_sem);
  7222. list_del(&block_group->list);
  7223. up_write(&block_group->space_info->groups_sem);
  7224. if (block_group->cached == BTRFS_CACHE_STARTED)
  7225. wait_block_group_cache_done(block_group);
  7226. /*
  7227. * We haven't cached this block group, which means we could
  7228. * possibly have excluded extents on this block group.
  7229. */
  7230. if (block_group->cached == BTRFS_CACHE_NO)
  7231. free_excluded_extents(info->extent_root, block_group);
  7232. btrfs_remove_free_space_cache(block_group);
  7233. btrfs_put_block_group(block_group);
  7234. spin_lock(&info->block_group_cache_lock);
  7235. }
  7236. spin_unlock(&info->block_group_cache_lock);
  7237. /* now that all the block groups are freed, go through and
  7238. * free all the space_info structs. This is only called during
  7239. * the final stages of unmount, and so we know nobody is
  7240. * using them. We call synchronize_rcu() once before we start,
  7241. * just to be on the safe side.
  7242. */
  7243. synchronize_rcu();
  7244. release_global_block_rsv(info);
  7245. while(!list_empty(&info->space_info)) {
  7246. space_info = list_entry(info->space_info.next,
  7247. struct btrfs_space_info,
  7248. list);
  7249. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  7250. if (space_info->bytes_pinned > 0 ||
  7251. space_info->bytes_reserved > 0 ||
  7252. space_info->bytes_may_use > 0) {
  7253. WARN_ON(1);
  7254. dump_space_info(space_info, 0, 0);
  7255. }
  7256. }
  7257. percpu_counter_destroy(&space_info->total_bytes_pinned);
  7258. list_del(&space_info->list);
  7259. kfree(space_info);
  7260. }
  7261. return 0;
  7262. }
  7263. static void __link_block_group(struct btrfs_space_info *space_info,
  7264. struct btrfs_block_group_cache *cache)
  7265. {
  7266. int index = get_block_group_index(cache);
  7267. down_write(&space_info->groups_sem);
  7268. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7269. up_write(&space_info->groups_sem);
  7270. }
  7271. int btrfs_read_block_groups(struct btrfs_root *root)
  7272. {
  7273. struct btrfs_path *path;
  7274. int ret;
  7275. struct btrfs_block_group_cache *cache;
  7276. struct btrfs_fs_info *info = root->fs_info;
  7277. struct btrfs_space_info *space_info;
  7278. struct btrfs_key key;
  7279. struct btrfs_key found_key;
  7280. struct extent_buffer *leaf;
  7281. int need_clear = 0;
  7282. u64 cache_gen;
  7283. root = info->extent_root;
  7284. key.objectid = 0;
  7285. key.offset = 0;
  7286. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7287. path = btrfs_alloc_path();
  7288. if (!path)
  7289. return -ENOMEM;
  7290. path->reada = 1;
  7291. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  7292. if (btrfs_test_opt(root, SPACE_CACHE) &&
  7293. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  7294. need_clear = 1;
  7295. if (btrfs_test_opt(root, CLEAR_CACHE))
  7296. need_clear = 1;
  7297. while (1) {
  7298. ret = find_first_block_group(root, path, &key);
  7299. if (ret > 0)
  7300. break;
  7301. if (ret != 0)
  7302. goto error;
  7303. leaf = path->nodes[0];
  7304. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7305. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7306. if (!cache) {
  7307. ret = -ENOMEM;
  7308. goto error;
  7309. }
  7310. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7311. GFP_NOFS);
  7312. if (!cache->free_space_ctl) {
  7313. kfree(cache);
  7314. ret = -ENOMEM;
  7315. goto error;
  7316. }
  7317. atomic_set(&cache->count, 1);
  7318. spin_lock_init(&cache->lock);
  7319. cache->fs_info = info;
  7320. INIT_LIST_HEAD(&cache->list);
  7321. INIT_LIST_HEAD(&cache->cluster_list);
  7322. if (need_clear) {
  7323. /*
  7324. * When we mount with old space cache, we need to
  7325. * set BTRFS_DC_CLEAR and set dirty flag.
  7326. *
  7327. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  7328. * truncate the old free space cache inode and
  7329. * setup a new one.
  7330. * b) Setting 'dirty flag' makes sure that we flush
  7331. * the new space cache info onto disk.
  7332. */
  7333. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7334. if (btrfs_test_opt(root, SPACE_CACHE))
  7335. cache->dirty = 1;
  7336. }
  7337. read_extent_buffer(leaf, &cache->item,
  7338. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7339. sizeof(cache->item));
  7340. memcpy(&cache->key, &found_key, sizeof(found_key));
  7341. key.objectid = found_key.objectid + found_key.offset;
  7342. btrfs_release_path(path);
  7343. cache->flags = btrfs_block_group_flags(&cache->item);
  7344. cache->sectorsize = root->sectorsize;
  7345. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7346. &root->fs_info->mapping_tree,
  7347. found_key.objectid);
  7348. btrfs_init_free_space_ctl(cache);
  7349. /*
  7350. * We need to exclude the super stripes now so that the space
  7351. * info has super bytes accounted for, otherwise we'll think
  7352. * we have more space than we actually do.
  7353. */
  7354. ret = exclude_super_stripes(root, cache);
  7355. if (ret) {
  7356. /*
  7357. * We may have excluded something, so call this just in
  7358. * case.
  7359. */
  7360. free_excluded_extents(root, cache);
  7361. kfree(cache->free_space_ctl);
  7362. kfree(cache);
  7363. goto error;
  7364. }
  7365. /*
  7366. * check for two cases, either we are full, and therefore
  7367. * don't need to bother with the caching work since we won't
  7368. * find any space, or we are empty, and we can just add all
  7369. * the space in and be done with it. This saves us _alot_ of
  7370. * time, particularly in the full case.
  7371. */
  7372. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7373. cache->last_byte_to_unpin = (u64)-1;
  7374. cache->cached = BTRFS_CACHE_FINISHED;
  7375. free_excluded_extents(root, cache);
  7376. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7377. cache->last_byte_to_unpin = (u64)-1;
  7378. cache->cached = BTRFS_CACHE_FINISHED;
  7379. add_new_free_space(cache, root->fs_info,
  7380. found_key.objectid,
  7381. found_key.objectid +
  7382. found_key.offset);
  7383. free_excluded_extents(root, cache);
  7384. }
  7385. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7386. if (ret) {
  7387. btrfs_remove_free_space_cache(cache);
  7388. btrfs_put_block_group(cache);
  7389. goto error;
  7390. }
  7391. ret = update_space_info(info, cache->flags, found_key.offset,
  7392. btrfs_block_group_used(&cache->item),
  7393. &space_info);
  7394. if (ret) {
  7395. btrfs_remove_free_space_cache(cache);
  7396. spin_lock(&info->block_group_cache_lock);
  7397. rb_erase(&cache->cache_node,
  7398. &info->block_group_cache_tree);
  7399. spin_unlock(&info->block_group_cache_lock);
  7400. btrfs_put_block_group(cache);
  7401. goto error;
  7402. }
  7403. cache->space_info = space_info;
  7404. spin_lock(&cache->space_info->lock);
  7405. cache->space_info->bytes_readonly += cache->bytes_super;
  7406. spin_unlock(&cache->space_info->lock);
  7407. __link_block_group(space_info, cache);
  7408. set_avail_alloc_bits(root->fs_info, cache->flags);
  7409. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7410. set_block_group_ro(cache, 1);
  7411. }
  7412. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7413. if (!(get_alloc_profile(root, space_info->flags) &
  7414. (BTRFS_BLOCK_GROUP_RAID10 |
  7415. BTRFS_BLOCK_GROUP_RAID1 |
  7416. BTRFS_BLOCK_GROUP_RAID5 |
  7417. BTRFS_BLOCK_GROUP_RAID6 |
  7418. BTRFS_BLOCK_GROUP_DUP)))
  7419. continue;
  7420. /*
  7421. * avoid allocating from un-mirrored block group if there are
  7422. * mirrored block groups.
  7423. */
  7424. list_for_each_entry(cache,
  7425. &space_info->block_groups[BTRFS_RAID_RAID0],
  7426. list)
  7427. set_block_group_ro(cache, 1);
  7428. list_for_each_entry(cache,
  7429. &space_info->block_groups[BTRFS_RAID_SINGLE],
  7430. list)
  7431. set_block_group_ro(cache, 1);
  7432. }
  7433. init_global_block_rsv(info);
  7434. ret = 0;
  7435. error:
  7436. btrfs_free_path(path);
  7437. return ret;
  7438. }
  7439. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  7440. struct btrfs_root *root)
  7441. {
  7442. struct btrfs_block_group_cache *block_group, *tmp;
  7443. struct btrfs_root *extent_root = root->fs_info->extent_root;
  7444. struct btrfs_block_group_item item;
  7445. struct btrfs_key key;
  7446. int ret = 0;
  7447. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  7448. new_bg_list) {
  7449. list_del_init(&block_group->new_bg_list);
  7450. if (ret)
  7451. continue;
  7452. spin_lock(&block_group->lock);
  7453. memcpy(&item, &block_group->item, sizeof(item));
  7454. memcpy(&key, &block_group->key, sizeof(key));
  7455. spin_unlock(&block_group->lock);
  7456. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  7457. sizeof(item));
  7458. if (ret)
  7459. btrfs_abort_transaction(trans, extent_root, ret);
  7460. ret = btrfs_finish_chunk_alloc(trans, extent_root,
  7461. key.objectid, key.offset);
  7462. if (ret)
  7463. btrfs_abort_transaction(trans, extent_root, ret);
  7464. }
  7465. }
  7466. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7467. struct btrfs_root *root, u64 bytes_used,
  7468. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7469. u64 size)
  7470. {
  7471. int ret;
  7472. struct btrfs_root *extent_root;
  7473. struct btrfs_block_group_cache *cache;
  7474. extent_root = root->fs_info->extent_root;
  7475. root->fs_info->last_trans_log_full_commit = trans->transid;
  7476. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7477. if (!cache)
  7478. return -ENOMEM;
  7479. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7480. GFP_NOFS);
  7481. if (!cache->free_space_ctl) {
  7482. kfree(cache);
  7483. return -ENOMEM;
  7484. }
  7485. cache->key.objectid = chunk_offset;
  7486. cache->key.offset = size;
  7487. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7488. cache->sectorsize = root->sectorsize;
  7489. cache->fs_info = root->fs_info;
  7490. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7491. &root->fs_info->mapping_tree,
  7492. chunk_offset);
  7493. atomic_set(&cache->count, 1);
  7494. spin_lock_init(&cache->lock);
  7495. INIT_LIST_HEAD(&cache->list);
  7496. INIT_LIST_HEAD(&cache->cluster_list);
  7497. INIT_LIST_HEAD(&cache->new_bg_list);
  7498. btrfs_init_free_space_ctl(cache);
  7499. btrfs_set_block_group_used(&cache->item, bytes_used);
  7500. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7501. cache->flags = type;
  7502. btrfs_set_block_group_flags(&cache->item, type);
  7503. cache->last_byte_to_unpin = (u64)-1;
  7504. cache->cached = BTRFS_CACHE_FINISHED;
  7505. ret = exclude_super_stripes(root, cache);
  7506. if (ret) {
  7507. /*
  7508. * We may have excluded something, so call this just in
  7509. * case.
  7510. */
  7511. free_excluded_extents(root, cache);
  7512. kfree(cache->free_space_ctl);
  7513. kfree(cache);
  7514. return ret;
  7515. }
  7516. add_new_free_space(cache, root->fs_info, chunk_offset,
  7517. chunk_offset + size);
  7518. free_excluded_extents(root, cache);
  7519. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7520. if (ret) {
  7521. btrfs_remove_free_space_cache(cache);
  7522. btrfs_put_block_group(cache);
  7523. return ret;
  7524. }
  7525. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7526. &cache->space_info);
  7527. if (ret) {
  7528. btrfs_remove_free_space_cache(cache);
  7529. spin_lock(&root->fs_info->block_group_cache_lock);
  7530. rb_erase(&cache->cache_node,
  7531. &root->fs_info->block_group_cache_tree);
  7532. spin_unlock(&root->fs_info->block_group_cache_lock);
  7533. btrfs_put_block_group(cache);
  7534. return ret;
  7535. }
  7536. update_global_block_rsv(root->fs_info);
  7537. spin_lock(&cache->space_info->lock);
  7538. cache->space_info->bytes_readonly += cache->bytes_super;
  7539. spin_unlock(&cache->space_info->lock);
  7540. __link_block_group(cache->space_info, cache);
  7541. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  7542. set_avail_alloc_bits(extent_root->fs_info, type);
  7543. return 0;
  7544. }
  7545. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  7546. {
  7547. u64 extra_flags = chunk_to_extended(flags) &
  7548. BTRFS_EXTENDED_PROFILE_MASK;
  7549. write_seqlock(&fs_info->profiles_lock);
  7550. if (flags & BTRFS_BLOCK_GROUP_DATA)
  7551. fs_info->avail_data_alloc_bits &= ~extra_flags;
  7552. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  7553. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  7554. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  7555. fs_info->avail_system_alloc_bits &= ~extra_flags;
  7556. write_sequnlock(&fs_info->profiles_lock);
  7557. }
  7558. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7559. struct btrfs_root *root, u64 group_start)
  7560. {
  7561. struct btrfs_path *path;
  7562. struct btrfs_block_group_cache *block_group;
  7563. struct btrfs_free_cluster *cluster;
  7564. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7565. struct btrfs_key key;
  7566. struct inode *inode;
  7567. int ret;
  7568. int index;
  7569. int factor;
  7570. root = root->fs_info->extent_root;
  7571. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7572. BUG_ON(!block_group);
  7573. BUG_ON(!block_group->ro);
  7574. /*
  7575. * Free the reserved super bytes from this block group before
  7576. * remove it.
  7577. */
  7578. free_excluded_extents(root, block_group);
  7579. memcpy(&key, &block_group->key, sizeof(key));
  7580. index = get_block_group_index(block_group);
  7581. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7582. BTRFS_BLOCK_GROUP_RAID1 |
  7583. BTRFS_BLOCK_GROUP_RAID10))
  7584. factor = 2;
  7585. else
  7586. factor = 1;
  7587. /* make sure this block group isn't part of an allocation cluster */
  7588. cluster = &root->fs_info->data_alloc_cluster;
  7589. spin_lock(&cluster->refill_lock);
  7590. btrfs_return_cluster_to_free_space(block_group, cluster);
  7591. spin_unlock(&cluster->refill_lock);
  7592. /*
  7593. * make sure this block group isn't part of a metadata
  7594. * allocation cluster
  7595. */
  7596. cluster = &root->fs_info->meta_alloc_cluster;
  7597. spin_lock(&cluster->refill_lock);
  7598. btrfs_return_cluster_to_free_space(block_group, cluster);
  7599. spin_unlock(&cluster->refill_lock);
  7600. path = btrfs_alloc_path();
  7601. if (!path) {
  7602. ret = -ENOMEM;
  7603. goto out;
  7604. }
  7605. inode = lookup_free_space_inode(tree_root, block_group, path);
  7606. if (!IS_ERR(inode)) {
  7607. ret = btrfs_orphan_add(trans, inode);
  7608. if (ret) {
  7609. btrfs_add_delayed_iput(inode);
  7610. goto out;
  7611. }
  7612. clear_nlink(inode);
  7613. /* One for the block groups ref */
  7614. spin_lock(&block_group->lock);
  7615. if (block_group->iref) {
  7616. block_group->iref = 0;
  7617. block_group->inode = NULL;
  7618. spin_unlock(&block_group->lock);
  7619. iput(inode);
  7620. } else {
  7621. spin_unlock(&block_group->lock);
  7622. }
  7623. /* One for our lookup ref */
  7624. btrfs_add_delayed_iput(inode);
  7625. }
  7626. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7627. key.offset = block_group->key.objectid;
  7628. key.type = 0;
  7629. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7630. if (ret < 0)
  7631. goto out;
  7632. if (ret > 0)
  7633. btrfs_release_path(path);
  7634. if (ret == 0) {
  7635. ret = btrfs_del_item(trans, tree_root, path);
  7636. if (ret)
  7637. goto out;
  7638. btrfs_release_path(path);
  7639. }
  7640. spin_lock(&root->fs_info->block_group_cache_lock);
  7641. rb_erase(&block_group->cache_node,
  7642. &root->fs_info->block_group_cache_tree);
  7643. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  7644. root->fs_info->first_logical_byte = (u64)-1;
  7645. spin_unlock(&root->fs_info->block_group_cache_lock);
  7646. down_write(&block_group->space_info->groups_sem);
  7647. /*
  7648. * we must use list_del_init so people can check to see if they
  7649. * are still on the list after taking the semaphore
  7650. */
  7651. list_del_init(&block_group->list);
  7652. if (list_empty(&block_group->space_info->block_groups[index]))
  7653. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7654. up_write(&block_group->space_info->groups_sem);
  7655. if (block_group->cached == BTRFS_CACHE_STARTED)
  7656. wait_block_group_cache_done(block_group);
  7657. btrfs_remove_free_space_cache(block_group);
  7658. spin_lock(&block_group->space_info->lock);
  7659. block_group->space_info->total_bytes -= block_group->key.offset;
  7660. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7661. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7662. spin_unlock(&block_group->space_info->lock);
  7663. memcpy(&key, &block_group->key, sizeof(key));
  7664. btrfs_clear_space_info_full(root->fs_info);
  7665. btrfs_put_block_group(block_group);
  7666. btrfs_put_block_group(block_group);
  7667. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7668. if (ret > 0)
  7669. ret = -EIO;
  7670. if (ret < 0)
  7671. goto out;
  7672. ret = btrfs_del_item(trans, root, path);
  7673. out:
  7674. btrfs_free_path(path);
  7675. return ret;
  7676. }
  7677. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7678. {
  7679. struct btrfs_space_info *space_info;
  7680. struct btrfs_super_block *disk_super;
  7681. u64 features;
  7682. u64 flags;
  7683. int mixed = 0;
  7684. int ret;
  7685. disk_super = fs_info->super_copy;
  7686. if (!btrfs_super_root(disk_super))
  7687. return 1;
  7688. features = btrfs_super_incompat_flags(disk_super);
  7689. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7690. mixed = 1;
  7691. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7692. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7693. if (ret)
  7694. goto out;
  7695. if (mixed) {
  7696. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7697. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7698. } else {
  7699. flags = BTRFS_BLOCK_GROUP_METADATA;
  7700. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7701. if (ret)
  7702. goto out;
  7703. flags = BTRFS_BLOCK_GROUP_DATA;
  7704. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7705. }
  7706. out:
  7707. return ret;
  7708. }
  7709. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7710. {
  7711. return unpin_extent_range(root, start, end);
  7712. }
  7713. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7714. u64 num_bytes, u64 *actual_bytes)
  7715. {
  7716. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7717. }
  7718. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7719. {
  7720. struct btrfs_fs_info *fs_info = root->fs_info;
  7721. struct btrfs_block_group_cache *cache = NULL;
  7722. u64 group_trimmed;
  7723. u64 start;
  7724. u64 end;
  7725. u64 trimmed = 0;
  7726. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7727. int ret = 0;
  7728. /*
  7729. * try to trim all FS space, our block group may start from non-zero.
  7730. */
  7731. if (range->len == total_bytes)
  7732. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7733. else
  7734. cache = btrfs_lookup_block_group(fs_info, range->start);
  7735. while (cache) {
  7736. if (cache->key.objectid >= (range->start + range->len)) {
  7737. btrfs_put_block_group(cache);
  7738. break;
  7739. }
  7740. start = max(range->start, cache->key.objectid);
  7741. end = min(range->start + range->len,
  7742. cache->key.objectid + cache->key.offset);
  7743. if (end - start >= range->minlen) {
  7744. if (!block_group_cache_done(cache)) {
  7745. ret = cache_block_group(cache, 0);
  7746. if (ret) {
  7747. btrfs_put_block_group(cache);
  7748. break;
  7749. }
  7750. ret = wait_block_group_cache_done(cache);
  7751. if (ret) {
  7752. btrfs_put_block_group(cache);
  7753. break;
  7754. }
  7755. }
  7756. ret = btrfs_trim_block_group(cache,
  7757. &group_trimmed,
  7758. start,
  7759. end,
  7760. range->minlen);
  7761. trimmed += group_trimmed;
  7762. if (ret) {
  7763. btrfs_put_block_group(cache);
  7764. break;
  7765. }
  7766. }
  7767. cache = next_block_group(fs_info->tree_root, cache);
  7768. }
  7769. range->len = trimmed;
  7770. return ret;
  7771. }