extent-tree.c 248 KB

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