extent-tree.c 229 KB

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