extent-tree.c 239 KB

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