extent-tree.c 238 KB

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