extent-tree.c 261 KB

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