extent-tree.c 273 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174
  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. if (locked_ref->is_data &&
  2283. locked_ref->total_ref_mod < 0) {
  2284. spin_lock(&delayed_refs->lock);
  2285. delayed_refs->pending_csums -= ref->num_bytes;
  2286. spin_unlock(&delayed_refs->lock);
  2287. }
  2288. btrfs_delayed_ref_unlock(locked_ref);
  2289. locked_ref = NULL;
  2290. }
  2291. btrfs_put_delayed_ref(ref);
  2292. count++;
  2293. cond_resched();
  2294. }
  2295. /*
  2296. * We don't want to include ref heads since we can have empty ref heads
  2297. * and those will drastically skew our runtime down since we just do
  2298. * accounting, no actual extent tree updates.
  2299. */
  2300. if (actual_count > 0) {
  2301. u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
  2302. u64 avg;
  2303. /*
  2304. * We weigh the current average higher than our current runtime
  2305. * to avoid large swings in the average.
  2306. */
  2307. spin_lock(&delayed_refs->lock);
  2308. avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
  2309. fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
  2310. spin_unlock(&delayed_refs->lock);
  2311. }
  2312. return 0;
  2313. }
  2314. #ifdef SCRAMBLE_DELAYED_REFS
  2315. /*
  2316. * Normally delayed refs get processed in ascending bytenr order. This
  2317. * correlates in most cases to the order added. To expose dependencies on this
  2318. * order, we start to process the tree in the middle instead of the beginning
  2319. */
  2320. static u64 find_middle(struct rb_root *root)
  2321. {
  2322. struct rb_node *n = root->rb_node;
  2323. struct btrfs_delayed_ref_node *entry;
  2324. int alt = 1;
  2325. u64 middle;
  2326. u64 first = 0, last = 0;
  2327. n = rb_first(root);
  2328. if (n) {
  2329. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2330. first = entry->bytenr;
  2331. }
  2332. n = rb_last(root);
  2333. if (n) {
  2334. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2335. last = entry->bytenr;
  2336. }
  2337. n = root->rb_node;
  2338. while (n) {
  2339. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2340. WARN_ON(!entry->in_tree);
  2341. middle = entry->bytenr;
  2342. if (alt)
  2343. n = n->rb_left;
  2344. else
  2345. n = n->rb_right;
  2346. alt = 1 - alt;
  2347. }
  2348. return middle;
  2349. }
  2350. #endif
  2351. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2352. {
  2353. u64 num_bytes;
  2354. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2355. sizeof(struct btrfs_extent_inline_ref));
  2356. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2357. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2358. /*
  2359. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2360. * closer to what we're really going to want to ouse.
  2361. */
  2362. return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2363. }
  2364. /*
  2365. * Takes the number of bytes to be csumm'ed and figures out how many leaves it
  2366. * would require to store the csums for that many bytes.
  2367. */
  2368. u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes)
  2369. {
  2370. u64 csum_size;
  2371. u64 num_csums_per_leaf;
  2372. u64 num_csums;
  2373. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  2374. num_csums_per_leaf = div64_u64(csum_size,
  2375. (u64)btrfs_super_csum_size(root->fs_info->super_copy));
  2376. num_csums = div64_u64(csum_bytes, root->sectorsize);
  2377. num_csums += num_csums_per_leaf - 1;
  2378. num_csums = div64_u64(num_csums, num_csums_per_leaf);
  2379. return num_csums;
  2380. }
  2381. int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
  2382. struct btrfs_root *root)
  2383. {
  2384. struct btrfs_block_rsv *global_rsv;
  2385. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2386. u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
  2387. u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
  2388. u64 num_bytes, num_dirty_bgs_bytes;
  2389. int ret = 0;
  2390. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2391. num_heads = heads_to_leaves(root, num_heads);
  2392. if (num_heads > 1)
  2393. num_bytes += (num_heads - 1) * root->nodesize;
  2394. num_bytes <<= 1;
  2395. num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * root->nodesize;
  2396. num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(root,
  2397. num_dirty_bgs);
  2398. global_rsv = &root->fs_info->global_block_rsv;
  2399. /*
  2400. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2401. * wiggle room since running delayed refs can create more delayed refs.
  2402. */
  2403. if (global_rsv->space_info->full) {
  2404. num_dirty_bgs_bytes <<= 1;
  2405. num_bytes <<= 1;
  2406. }
  2407. spin_lock(&global_rsv->lock);
  2408. if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
  2409. ret = 1;
  2410. spin_unlock(&global_rsv->lock);
  2411. return ret;
  2412. }
  2413. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2414. struct btrfs_root *root)
  2415. {
  2416. struct btrfs_fs_info *fs_info = root->fs_info;
  2417. u64 num_entries =
  2418. atomic_read(&trans->transaction->delayed_refs.num_entries);
  2419. u64 avg_runtime;
  2420. u64 val;
  2421. smp_mb();
  2422. avg_runtime = fs_info->avg_delayed_ref_runtime;
  2423. val = num_entries * avg_runtime;
  2424. if (num_entries * avg_runtime >= NSEC_PER_SEC)
  2425. return 1;
  2426. if (val >= NSEC_PER_SEC / 2)
  2427. return 2;
  2428. return btrfs_check_space_for_delayed_refs(trans, root);
  2429. }
  2430. struct async_delayed_refs {
  2431. struct btrfs_root *root;
  2432. int count;
  2433. int error;
  2434. int sync;
  2435. struct completion wait;
  2436. struct btrfs_work work;
  2437. };
  2438. static void delayed_ref_async_start(struct btrfs_work *work)
  2439. {
  2440. struct async_delayed_refs *async;
  2441. struct btrfs_trans_handle *trans;
  2442. int ret;
  2443. async = container_of(work, struct async_delayed_refs, work);
  2444. trans = btrfs_join_transaction(async->root);
  2445. if (IS_ERR(trans)) {
  2446. async->error = PTR_ERR(trans);
  2447. goto done;
  2448. }
  2449. /*
  2450. * trans->sync means that when we call end_transaciton, we won't
  2451. * wait on delayed refs
  2452. */
  2453. trans->sync = true;
  2454. ret = btrfs_run_delayed_refs(trans, async->root, async->count);
  2455. if (ret)
  2456. async->error = ret;
  2457. ret = btrfs_end_transaction(trans, async->root);
  2458. if (ret && !async->error)
  2459. async->error = ret;
  2460. done:
  2461. if (async->sync)
  2462. complete(&async->wait);
  2463. else
  2464. kfree(async);
  2465. }
  2466. int btrfs_async_run_delayed_refs(struct btrfs_root *root,
  2467. unsigned long count, int wait)
  2468. {
  2469. struct async_delayed_refs *async;
  2470. int ret;
  2471. async = kmalloc(sizeof(*async), GFP_NOFS);
  2472. if (!async)
  2473. return -ENOMEM;
  2474. async->root = root->fs_info->tree_root;
  2475. async->count = count;
  2476. async->error = 0;
  2477. if (wait)
  2478. async->sync = 1;
  2479. else
  2480. async->sync = 0;
  2481. init_completion(&async->wait);
  2482. btrfs_init_work(&async->work, btrfs_extent_refs_helper,
  2483. delayed_ref_async_start, NULL, NULL);
  2484. btrfs_queue_work(root->fs_info->extent_workers, &async->work);
  2485. if (wait) {
  2486. wait_for_completion(&async->wait);
  2487. ret = async->error;
  2488. kfree(async);
  2489. return ret;
  2490. }
  2491. return 0;
  2492. }
  2493. /*
  2494. * this starts processing the delayed reference count updates and
  2495. * extent insertions we have queued up so far. count can be
  2496. * 0, which means to process everything in the tree at the start
  2497. * of the run (but not newly added entries), or it can be some target
  2498. * number you'd like to process.
  2499. *
  2500. * Returns 0 on success or if called with an aborted transaction
  2501. * Returns <0 on error and aborts the transaction
  2502. */
  2503. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2504. struct btrfs_root *root, unsigned long count)
  2505. {
  2506. struct rb_node *node;
  2507. struct btrfs_delayed_ref_root *delayed_refs;
  2508. struct btrfs_delayed_ref_head *head;
  2509. int ret;
  2510. int run_all = count == (unsigned long)-1;
  2511. /* We'll clean this up in btrfs_cleanup_transaction */
  2512. if (trans->aborted)
  2513. return 0;
  2514. if (root == root->fs_info->extent_root)
  2515. root = root->fs_info->tree_root;
  2516. delayed_refs = &trans->transaction->delayed_refs;
  2517. if (count == 0)
  2518. count = atomic_read(&delayed_refs->num_entries) * 2;
  2519. again:
  2520. #ifdef SCRAMBLE_DELAYED_REFS
  2521. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2522. #endif
  2523. ret = __btrfs_run_delayed_refs(trans, root, count);
  2524. if (ret < 0) {
  2525. btrfs_abort_transaction(trans, root, ret);
  2526. return ret;
  2527. }
  2528. if (run_all) {
  2529. if (!list_empty(&trans->new_bgs))
  2530. btrfs_create_pending_block_groups(trans, root);
  2531. spin_lock(&delayed_refs->lock);
  2532. node = rb_first(&delayed_refs->href_root);
  2533. if (!node) {
  2534. spin_unlock(&delayed_refs->lock);
  2535. goto out;
  2536. }
  2537. count = (unsigned long)-1;
  2538. while (node) {
  2539. head = rb_entry(node, struct btrfs_delayed_ref_head,
  2540. href_node);
  2541. if (btrfs_delayed_ref_is_head(&head->node)) {
  2542. struct btrfs_delayed_ref_node *ref;
  2543. ref = &head->node;
  2544. atomic_inc(&ref->refs);
  2545. spin_unlock(&delayed_refs->lock);
  2546. /*
  2547. * Mutex was contended, block until it's
  2548. * released and try again
  2549. */
  2550. mutex_lock(&head->mutex);
  2551. mutex_unlock(&head->mutex);
  2552. btrfs_put_delayed_ref(ref);
  2553. cond_resched();
  2554. goto again;
  2555. } else {
  2556. WARN_ON(1);
  2557. }
  2558. node = rb_next(node);
  2559. }
  2560. spin_unlock(&delayed_refs->lock);
  2561. cond_resched();
  2562. goto again;
  2563. }
  2564. out:
  2565. ret = btrfs_delayed_qgroup_accounting(trans, root->fs_info);
  2566. if (ret)
  2567. return ret;
  2568. assert_qgroups_uptodate(trans);
  2569. return 0;
  2570. }
  2571. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2572. struct btrfs_root *root,
  2573. u64 bytenr, u64 num_bytes, u64 flags,
  2574. int level, int is_data)
  2575. {
  2576. struct btrfs_delayed_extent_op *extent_op;
  2577. int ret;
  2578. extent_op = btrfs_alloc_delayed_extent_op();
  2579. if (!extent_op)
  2580. return -ENOMEM;
  2581. extent_op->flags_to_set = flags;
  2582. extent_op->update_flags = 1;
  2583. extent_op->update_key = 0;
  2584. extent_op->is_data = is_data ? 1 : 0;
  2585. extent_op->level = level;
  2586. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2587. num_bytes, extent_op);
  2588. if (ret)
  2589. btrfs_free_delayed_extent_op(extent_op);
  2590. return ret;
  2591. }
  2592. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2593. struct btrfs_root *root,
  2594. struct btrfs_path *path,
  2595. u64 objectid, u64 offset, u64 bytenr)
  2596. {
  2597. struct btrfs_delayed_ref_head *head;
  2598. struct btrfs_delayed_ref_node *ref;
  2599. struct btrfs_delayed_data_ref *data_ref;
  2600. struct btrfs_delayed_ref_root *delayed_refs;
  2601. struct rb_node *node;
  2602. int ret = 0;
  2603. delayed_refs = &trans->transaction->delayed_refs;
  2604. spin_lock(&delayed_refs->lock);
  2605. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2606. if (!head) {
  2607. spin_unlock(&delayed_refs->lock);
  2608. return 0;
  2609. }
  2610. if (!mutex_trylock(&head->mutex)) {
  2611. atomic_inc(&head->node.refs);
  2612. spin_unlock(&delayed_refs->lock);
  2613. btrfs_release_path(path);
  2614. /*
  2615. * Mutex was contended, block until it's released and let
  2616. * caller try again
  2617. */
  2618. mutex_lock(&head->mutex);
  2619. mutex_unlock(&head->mutex);
  2620. btrfs_put_delayed_ref(&head->node);
  2621. return -EAGAIN;
  2622. }
  2623. spin_unlock(&delayed_refs->lock);
  2624. spin_lock(&head->lock);
  2625. node = rb_first(&head->ref_root);
  2626. while (node) {
  2627. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2628. node = rb_next(node);
  2629. /* If it's a shared ref we know a cross reference exists */
  2630. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
  2631. ret = 1;
  2632. break;
  2633. }
  2634. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2635. /*
  2636. * If our ref doesn't match the one we're currently looking at
  2637. * then we have a cross reference.
  2638. */
  2639. if (data_ref->root != root->root_key.objectid ||
  2640. data_ref->objectid != objectid ||
  2641. data_ref->offset != offset) {
  2642. ret = 1;
  2643. break;
  2644. }
  2645. }
  2646. spin_unlock(&head->lock);
  2647. mutex_unlock(&head->mutex);
  2648. return ret;
  2649. }
  2650. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2651. struct btrfs_root *root,
  2652. struct btrfs_path *path,
  2653. u64 objectid, u64 offset, u64 bytenr)
  2654. {
  2655. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2656. struct extent_buffer *leaf;
  2657. struct btrfs_extent_data_ref *ref;
  2658. struct btrfs_extent_inline_ref *iref;
  2659. struct btrfs_extent_item *ei;
  2660. struct btrfs_key key;
  2661. u32 item_size;
  2662. int ret;
  2663. key.objectid = bytenr;
  2664. key.offset = (u64)-1;
  2665. key.type = BTRFS_EXTENT_ITEM_KEY;
  2666. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2667. if (ret < 0)
  2668. goto out;
  2669. BUG_ON(ret == 0); /* Corruption */
  2670. ret = -ENOENT;
  2671. if (path->slots[0] == 0)
  2672. goto out;
  2673. path->slots[0]--;
  2674. leaf = path->nodes[0];
  2675. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2676. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2677. goto out;
  2678. ret = 1;
  2679. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2680. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2681. if (item_size < sizeof(*ei)) {
  2682. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2683. goto out;
  2684. }
  2685. #endif
  2686. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2687. if (item_size != sizeof(*ei) +
  2688. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2689. goto out;
  2690. if (btrfs_extent_generation(leaf, ei) <=
  2691. btrfs_root_last_snapshot(&root->root_item))
  2692. goto out;
  2693. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2694. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2695. BTRFS_EXTENT_DATA_REF_KEY)
  2696. goto out;
  2697. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2698. if (btrfs_extent_refs(leaf, ei) !=
  2699. btrfs_extent_data_ref_count(leaf, ref) ||
  2700. btrfs_extent_data_ref_root(leaf, ref) !=
  2701. root->root_key.objectid ||
  2702. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2703. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2704. goto out;
  2705. ret = 0;
  2706. out:
  2707. return ret;
  2708. }
  2709. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2710. struct btrfs_root *root,
  2711. u64 objectid, u64 offset, u64 bytenr)
  2712. {
  2713. struct btrfs_path *path;
  2714. int ret;
  2715. int ret2;
  2716. path = btrfs_alloc_path();
  2717. if (!path)
  2718. return -ENOENT;
  2719. do {
  2720. ret = check_committed_ref(trans, root, path, objectid,
  2721. offset, bytenr);
  2722. if (ret && ret != -ENOENT)
  2723. goto out;
  2724. ret2 = check_delayed_ref(trans, root, path, objectid,
  2725. offset, bytenr);
  2726. } while (ret2 == -EAGAIN);
  2727. if (ret2 && ret2 != -ENOENT) {
  2728. ret = ret2;
  2729. goto out;
  2730. }
  2731. if (ret != -ENOENT || ret2 != -ENOENT)
  2732. ret = 0;
  2733. out:
  2734. btrfs_free_path(path);
  2735. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2736. WARN_ON(ret > 0);
  2737. return ret;
  2738. }
  2739. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2740. struct btrfs_root *root,
  2741. struct extent_buffer *buf,
  2742. int full_backref, int inc)
  2743. {
  2744. u64 bytenr;
  2745. u64 num_bytes;
  2746. u64 parent;
  2747. u64 ref_root;
  2748. u32 nritems;
  2749. struct btrfs_key key;
  2750. struct btrfs_file_extent_item *fi;
  2751. int i;
  2752. int level;
  2753. int ret = 0;
  2754. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2755. u64, u64, u64, u64, u64, u64, int);
  2756. if (btrfs_test_is_dummy_root(root))
  2757. return 0;
  2758. ref_root = btrfs_header_owner(buf);
  2759. nritems = btrfs_header_nritems(buf);
  2760. level = btrfs_header_level(buf);
  2761. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
  2762. return 0;
  2763. if (inc)
  2764. process_func = btrfs_inc_extent_ref;
  2765. else
  2766. process_func = btrfs_free_extent;
  2767. if (full_backref)
  2768. parent = buf->start;
  2769. else
  2770. parent = 0;
  2771. for (i = 0; i < nritems; i++) {
  2772. if (level == 0) {
  2773. btrfs_item_key_to_cpu(buf, &key, i);
  2774. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2775. continue;
  2776. fi = btrfs_item_ptr(buf, i,
  2777. struct btrfs_file_extent_item);
  2778. if (btrfs_file_extent_type(buf, fi) ==
  2779. BTRFS_FILE_EXTENT_INLINE)
  2780. continue;
  2781. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2782. if (bytenr == 0)
  2783. continue;
  2784. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2785. key.offset -= btrfs_file_extent_offset(buf, fi);
  2786. ret = process_func(trans, root, bytenr, num_bytes,
  2787. parent, ref_root, key.objectid,
  2788. key.offset, 1);
  2789. if (ret)
  2790. goto fail;
  2791. } else {
  2792. bytenr = btrfs_node_blockptr(buf, i);
  2793. num_bytes = root->nodesize;
  2794. ret = process_func(trans, root, bytenr, num_bytes,
  2795. parent, ref_root, level - 1, 0,
  2796. 1);
  2797. if (ret)
  2798. goto fail;
  2799. }
  2800. }
  2801. return 0;
  2802. fail:
  2803. return ret;
  2804. }
  2805. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2806. struct extent_buffer *buf, int full_backref)
  2807. {
  2808. return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
  2809. }
  2810. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2811. struct extent_buffer *buf, int full_backref)
  2812. {
  2813. return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
  2814. }
  2815. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2816. struct btrfs_root *root,
  2817. struct btrfs_path *path,
  2818. struct btrfs_block_group_cache *cache)
  2819. {
  2820. int ret;
  2821. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2822. unsigned long bi;
  2823. struct extent_buffer *leaf;
  2824. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2825. if (ret) {
  2826. if (ret > 0)
  2827. ret = -ENOENT;
  2828. goto fail;
  2829. }
  2830. leaf = path->nodes[0];
  2831. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2832. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2833. btrfs_mark_buffer_dirty(leaf);
  2834. fail:
  2835. btrfs_release_path(path);
  2836. return ret;
  2837. }
  2838. static struct btrfs_block_group_cache *
  2839. next_block_group(struct btrfs_root *root,
  2840. struct btrfs_block_group_cache *cache)
  2841. {
  2842. struct rb_node *node;
  2843. spin_lock(&root->fs_info->block_group_cache_lock);
  2844. /* If our block group was removed, we need a full search. */
  2845. if (RB_EMPTY_NODE(&cache->cache_node)) {
  2846. const u64 next_bytenr = cache->key.objectid + cache->key.offset;
  2847. spin_unlock(&root->fs_info->block_group_cache_lock);
  2848. btrfs_put_block_group(cache);
  2849. cache = btrfs_lookup_first_block_group(root->fs_info,
  2850. next_bytenr);
  2851. return cache;
  2852. }
  2853. node = rb_next(&cache->cache_node);
  2854. btrfs_put_block_group(cache);
  2855. if (node) {
  2856. cache = rb_entry(node, struct btrfs_block_group_cache,
  2857. cache_node);
  2858. btrfs_get_block_group(cache);
  2859. } else
  2860. cache = NULL;
  2861. spin_unlock(&root->fs_info->block_group_cache_lock);
  2862. return cache;
  2863. }
  2864. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2865. struct btrfs_trans_handle *trans,
  2866. struct btrfs_path *path)
  2867. {
  2868. struct btrfs_root *root = block_group->fs_info->tree_root;
  2869. struct inode *inode = NULL;
  2870. u64 alloc_hint = 0;
  2871. int dcs = BTRFS_DC_ERROR;
  2872. u64 num_pages = 0;
  2873. int retries = 0;
  2874. int ret = 0;
  2875. /*
  2876. * If this block group is smaller than 100 megs don't bother caching the
  2877. * block group.
  2878. */
  2879. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2880. spin_lock(&block_group->lock);
  2881. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2882. spin_unlock(&block_group->lock);
  2883. return 0;
  2884. }
  2885. if (trans->aborted)
  2886. return 0;
  2887. again:
  2888. inode = lookup_free_space_inode(root, block_group, path);
  2889. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2890. ret = PTR_ERR(inode);
  2891. btrfs_release_path(path);
  2892. goto out;
  2893. }
  2894. if (IS_ERR(inode)) {
  2895. BUG_ON(retries);
  2896. retries++;
  2897. if (block_group->ro)
  2898. goto out_free;
  2899. ret = create_free_space_inode(root, trans, block_group, path);
  2900. if (ret)
  2901. goto out_free;
  2902. goto again;
  2903. }
  2904. /* We've already setup this transaction, go ahead and exit */
  2905. if (block_group->cache_generation == trans->transid &&
  2906. i_size_read(inode)) {
  2907. dcs = BTRFS_DC_SETUP;
  2908. goto out_put;
  2909. }
  2910. /*
  2911. * We want to set the generation to 0, that way if anything goes wrong
  2912. * from here on out we know not to trust this cache when we load up next
  2913. * time.
  2914. */
  2915. BTRFS_I(inode)->generation = 0;
  2916. ret = btrfs_update_inode(trans, root, inode);
  2917. if (ret) {
  2918. /*
  2919. * So theoretically we could recover from this, simply set the
  2920. * super cache generation to 0 so we know to invalidate the
  2921. * cache, but then we'd have to keep track of the block groups
  2922. * that fail this way so we know we _have_ to reset this cache
  2923. * before the next commit or risk reading stale cache. So to
  2924. * limit our exposure to horrible edge cases lets just abort the
  2925. * transaction, this only happens in really bad situations
  2926. * anyway.
  2927. */
  2928. btrfs_abort_transaction(trans, root, ret);
  2929. goto out_put;
  2930. }
  2931. WARN_ON(ret);
  2932. if (i_size_read(inode) > 0) {
  2933. ret = btrfs_check_trunc_cache_free_space(root,
  2934. &root->fs_info->global_block_rsv);
  2935. if (ret)
  2936. goto out_put;
  2937. ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode);
  2938. if (ret)
  2939. goto out_put;
  2940. }
  2941. spin_lock(&block_group->lock);
  2942. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2943. !btrfs_test_opt(root, SPACE_CACHE)) {
  2944. /*
  2945. * don't bother trying to write stuff out _if_
  2946. * a) we're not cached,
  2947. * b) we're with nospace_cache mount option.
  2948. */
  2949. dcs = BTRFS_DC_WRITTEN;
  2950. spin_unlock(&block_group->lock);
  2951. goto out_put;
  2952. }
  2953. spin_unlock(&block_group->lock);
  2954. /*
  2955. * Try to preallocate enough space based on how big the block group is.
  2956. * Keep in mind this has to include any pinned space which could end up
  2957. * taking up quite a bit since it's not folded into the other space
  2958. * cache.
  2959. */
  2960. num_pages = div_u64(block_group->key.offset, 256 * 1024 * 1024);
  2961. if (!num_pages)
  2962. num_pages = 1;
  2963. num_pages *= 16;
  2964. num_pages *= PAGE_CACHE_SIZE;
  2965. ret = btrfs_check_data_free_space(inode, num_pages, num_pages);
  2966. if (ret)
  2967. goto out_put;
  2968. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2969. num_pages, num_pages,
  2970. &alloc_hint);
  2971. if (!ret)
  2972. dcs = BTRFS_DC_SETUP;
  2973. btrfs_free_reserved_data_space(inode, num_pages);
  2974. out_put:
  2975. iput(inode);
  2976. out_free:
  2977. btrfs_release_path(path);
  2978. out:
  2979. spin_lock(&block_group->lock);
  2980. if (!ret && dcs == BTRFS_DC_SETUP)
  2981. block_group->cache_generation = trans->transid;
  2982. block_group->disk_cache_state = dcs;
  2983. spin_unlock(&block_group->lock);
  2984. return ret;
  2985. }
  2986. int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
  2987. struct btrfs_root *root)
  2988. {
  2989. struct btrfs_block_group_cache *cache, *tmp;
  2990. struct btrfs_transaction *cur_trans = trans->transaction;
  2991. struct btrfs_path *path;
  2992. if (list_empty(&cur_trans->dirty_bgs) ||
  2993. !btrfs_test_opt(root, SPACE_CACHE))
  2994. return 0;
  2995. path = btrfs_alloc_path();
  2996. if (!path)
  2997. return -ENOMEM;
  2998. /* Could add new block groups, use _safe just in case */
  2999. list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
  3000. dirty_list) {
  3001. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  3002. cache_save_setup(cache, trans, path);
  3003. }
  3004. btrfs_free_path(path);
  3005. return 0;
  3006. }
  3007. /*
  3008. * transaction commit does final block group cache writeback during a
  3009. * critical section where nothing is allowed to change the FS. This is
  3010. * required in order for the cache to actually match the block group,
  3011. * but can introduce a lot of latency into the commit.
  3012. *
  3013. * So, btrfs_start_dirty_block_groups is here to kick off block group
  3014. * cache IO. There's a chance we'll have to redo some of it if the
  3015. * block group changes again during the commit, but it greatly reduces
  3016. * the commit latency by getting rid of the easy block groups while
  3017. * we're still allowing others to join the commit.
  3018. */
  3019. int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
  3020. struct btrfs_root *root)
  3021. {
  3022. struct btrfs_block_group_cache *cache;
  3023. struct btrfs_transaction *cur_trans = trans->transaction;
  3024. int ret = 0;
  3025. int should_put;
  3026. struct btrfs_path *path = NULL;
  3027. LIST_HEAD(dirty);
  3028. struct list_head *io = &cur_trans->io_bgs;
  3029. int num_started = 0;
  3030. int loops = 0;
  3031. spin_lock(&cur_trans->dirty_bgs_lock);
  3032. if (list_empty(&cur_trans->dirty_bgs)) {
  3033. spin_unlock(&cur_trans->dirty_bgs_lock);
  3034. return 0;
  3035. }
  3036. list_splice_init(&cur_trans->dirty_bgs, &dirty);
  3037. spin_unlock(&cur_trans->dirty_bgs_lock);
  3038. again:
  3039. /*
  3040. * make sure all the block groups on our dirty list actually
  3041. * exist
  3042. */
  3043. btrfs_create_pending_block_groups(trans, root);
  3044. if (!path) {
  3045. path = btrfs_alloc_path();
  3046. if (!path)
  3047. return -ENOMEM;
  3048. }
  3049. /*
  3050. * cache_write_mutex is here only to save us from balance or automatic
  3051. * removal of empty block groups deleting this block group while we are
  3052. * writing out the cache
  3053. */
  3054. mutex_lock(&trans->transaction->cache_write_mutex);
  3055. while (!list_empty(&dirty)) {
  3056. cache = list_first_entry(&dirty,
  3057. struct btrfs_block_group_cache,
  3058. dirty_list);
  3059. /*
  3060. * this can happen if something re-dirties a block
  3061. * group that is already under IO. Just wait for it to
  3062. * finish and then do it all again
  3063. */
  3064. if (!list_empty(&cache->io_list)) {
  3065. list_del_init(&cache->io_list);
  3066. btrfs_wait_cache_io(root, trans, cache,
  3067. &cache->io_ctl, path,
  3068. cache->key.objectid);
  3069. btrfs_put_block_group(cache);
  3070. }
  3071. /*
  3072. * btrfs_wait_cache_io uses the cache->dirty_list to decide
  3073. * if it should update the cache_state. Don't delete
  3074. * until after we wait.
  3075. *
  3076. * Since we're not running in the commit critical section
  3077. * we need the dirty_bgs_lock to protect from update_block_group
  3078. */
  3079. spin_lock(&cur_trans->dirty_bgs_lock);
  3080. list_del_init(&cache->dirty_list);
  3081. spin_unlock(&cur_trans->dirty_bgs_lock);
  3082. should_put = 1;
  3083. cache_save_setup(cache, trans, path);
  3084. if (cache->disk_cache_state == BTRFS_DC_SETUP) {
  3085. cache->io_ctl.inode = NULL;
  3086. ret = btrfs_write_out_cache(root, trans, cache, path);
  3087. if (ret == 0 && cache->io_ctl.inode) {
  3088. num_started++;
  3089. should_put = 0;
  3090. /*
  3091. * the cache_write_mutex is protecting
  3092. * the io_list
  3093. */
  3094. list_add_tail(&cache->io_list, io);
  3095. } else {
  3096. /*
  3097. * if we failed to write the cache, the
  3098. * generation will be bad and life goes on
  3099. */
  3100. ret = 0;
  3101. }
  3102. }
  3103. if (!ret) {
  3104. ret = write_one_cache_group(trans, root, path, cache);
  3105. /*
  3106. * Our block group might still be attached to the list
  3107. * of new block groups in the transaction handle of some
  3108. * other task (struct btrfs_trans_handle->new_bgs). This
  3109. * means its block group item isn't yet in the extent
  3110. * tree. If this happens ignore the error, as we will
  3111. * try again later in the critical section of the
  3112. * transaction commit.
  3113. */
  3114. if (ret == -ENOENT) {
  3115. ret = 0;
  3116. spin_lock(&cur_trans->dirty_bgs_lock);
  3117. if (list_empty(&cache->dirty_list)) {
  3118. list_add_tail(&cache->dirty_list,
  3119. &cur_trans->dirty_bgs);
  3120. btrfs_get_block_group(cache);
  3121. }
  3122. spin_unlock(&cur_trans->dirty_bgs_lock);
  3123. } else if (ret) {
  3124. btrfs_abort_transaction(trans, root, ret);
  3125. }
  3126. }
  3127. /* if its not on the io list, we need to put the block group */
  3128. if (should_put)
  3129. btrfs_put_block_group(cache);
  3130. if (ret)
  3131. break;
  3132. /*
  3133. * Avoid blocking other tasks for too long. It might even save
  3134. * us from writing caches for block groups that are going to be
  3135. * removed.
  3136. */
  3137. mutex_unlock(&trans->transaction->cache_write_mutex);
  3138. mutex_lock(&trans->transaction->cache_write_mutex);
  3139. }
  3140. mutex_unlock(&trans->transaction->cache_write_mutex);
  3141. /*
  3142. * go through delayed refs for all the stuff we've just kicked off
  3143. * and then loop back (just once)
  3144. */
  3145. ret = btrfs_run_delayed_refs(trans, root, 0);
  3146. if (!ret && loops == 0) {
  3147. loops++;
  3148. spin_lock(&cur_trans->dirty_bgs_lock);
  3149. list_splice_init(&cur_trans->dirty_bgs, &dirty);
  3150. /*
  3151. * dirty_bgs_lock protects us from concurrent block group
  3152. * deletes too (not just cache_write_mutex).
  3153. */
  3154. if (!list_empty(&dirty)) {
  3155. spin_unlock(&cur_trans->dirty_bgs_lock);
  3156. goto again;
  3157. }
  3158. spin_unlock(&cur_trans->dirty_bgs_lock);
  3159. }
  3160. btrfs_free_path(path);
  3161. return ret;
  3162. }
  3163. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  3164. struct btrfs_root *root)
  3165. {
  3166. struct btrfs_block_group_cache *cache;
  3167. struct btrfs_transaction *cur_trans = trans->transaction;
  3168. int ret = 0;
  3169. int should_put;
  3170. struct btrfs_path *path;
  3171. struct list_head *io = &cur_trans->io_bgs;
  3172. int num_started = 0;
  3173. path = btrfs_alloc_path();
  3174. if (!path)
  3175. return -ENOMEM;
  3176. /*
  3177. * We don't need the lock here since we are protected by the transaction
  3178. * commit. We want to do the cache_save_setup first and then run the
  3179. * delayed refs to make sure we have the best chance at doing this all
  3180. * in one shot.
  3181. */
  3182. while (!list_empty(&cur_trans->dirty_bgs)) {
  3183. cache = list_first_entry(&cur_trans->dirty_bgs,
  3184. struct btrfs_block_group_cache,
  3185. dirty_list);
  3186. /*
  3187. * this can happen if cache_save_setup re-dirties a block
  3188. * group that is already under IO. Just wait for it to
  3189. * finish and then do it all again
  3190. */
  3191. if (!list_empty(&cache->io_list)) {
  3192. list_del_init(&cache->io_list);
  3193. btrfs_wait_cache_io(root, trans, cache,
  3194. &cache->io_ctl, path,
  3195. cache->key.objectid);
  3196. btrfs_put_block_group(cache);
  3197. }
  3198. /*
  3199. * don't remove from the dirty list until after we've waited
  3200. * on any pending IO
  3201. */
  3202. list_del_init(&cache->dirty_list);
  3203. should_put = 1;
  3204. cache_save_setup(cache, trans, path);
  3205. if (!ret)
  3206. ret = btrfs_run_delayed_refs(trans, root, (unsigned long) -1);
  3207. if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
  3208. cache->io_ctl.inode = NULL;
  3209. ret = btrfs_write_out_cache(root, trans, cache, path);
  3210. if (ret == 0 && cache->io_ctl.inode) {
  3211. num_started++;
  3212. should_put = 0;
  3213. list_add_tail(&cache->io_list, io);
  3214. } else {
  3215. /*
  3216. * if we failed to write the cache, the
  3217. * generation will be bad and life goes on
  3218. */
  3219. ret = 0;
  3220. }
  3221. }
  3222. if (!ret) {
  3223. ret = write_one_cache_group(trans, root, path, cache);
  3224. if (ret)
  3225. btrfs_abort_transaction(trans, root, ret);
  3226. }
  3227. /* if its not on the io list, we need to put the block group */
  3228. if (should_put)
  3229. btrfs_put_block_group(cache);
  3230. }
  3231. while (!list_empty(io)) {
  3232. cache = list_first_entry(io, struct btrfs_block_group_cache,
  3233. io_list);
  3234. list_del_init(&cache->io_list);
  3235. btrfs_wait_cache_io(root, trans, cache,
  3236. &cache->io_ctl, path, cache->key.objectid);
  3237. btrfs_put_block_group(cache);
  3238. }
  3239. btrfs_free_path(path);
  3240. return ret;
  3241. }
  3242. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  3243. {
  3244. struct btrfs_block_group_cache *block_group;
  3245. int readonly = 0;
  3246. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  3247. if (!block_group || block_group->ro)
  3248. readonly = 1;
  3249. if (block_group)
  3250. btrfs_put_block_group(block_group);
  3251. return readonly;
  3252. }
  3253. static const char *alloc_name(u64 flags)
  3254. {
  3255. switch (flags) {
  3256. case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
  3257. return "mixed";
  3258. case BTRFS_BLOCK_GROUP_METADATA:
  3259. return "metadata";
  3260. case BTRFS_BLOCK_GROUP_DATA:
  3261. return "data";
  3262. case BTRFS_BLOCK_GROUP_SYSTEM:
  3263. return "system";
  3264. default:
  3265. WARN_ON(1);
  3266. return "invalid-combination";
  3267. };
  3268. }
  3269. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  3270. u64 total_bytes, u64 bytes_used,
  3271. struct btrfs_space_info **space_info)
  3272. {
  3273. struct btrfs_space_info *found;
  3274. int i;
  3275. int factor;
  3276. int ret;
  3277. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3278. BTRFS_BLOCK_GROUP_RAID10))
  3279. factor = 2;
  3280. else
  3281. factor = 1;
  3282. found = __find_space_info(info, flags);
  3283. if (found) {
  3284. spin_lock(&found->lock);
  3285. found->total_bytes += total_bytes;
  3286. found->disk_total += total_bytes * factor;
  3287. found->bytes_used += bytes_used;
  3288. found->disk_used += bytes_used * factor;
  3289. found->full = 0;
  3290. spin_unlock(&found->lock);
  3291. *space_info = found;
  3292. return 0;
  3293. }
  3294. found = kzalloc(sizeof(*found), GFP_NOFS);
  3295. if (!found)
  3296. return -ENOMEM;
  3297. ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL);
  3298. if (ret) {
  3299. kfree(found);
  3300. return ret;
  3301. }
  3302. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  3303. INIT_LIST_HEAD(&found->block_groups[i]);
  3304. init_rwsem(&found->groups_sem);
  3305. spin_lock_init(&found->lock);
  3306. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3307. found->total_bytes = total_bytes;
  3308. found->disk_total = total_bytes * factor;
  3309. found->bytes_used = bytes_used;
  3310. found->disk_used = bytes_used * factor;
  3311. found->bytes_pinned = 0;
  3312. found->bytes_reserved = 0;
  3313. found->bytes_readonly = 0;
  3314. found->bytes_may_use = 0;
  3315. found->full = 0;
  3316. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3317. found->chunk_alloc = 0;
  3318. found->flush = 0;
  3319. init_waitqueue_head(&found->wait);
  3320. INIT_LIST_HEAD(&found->ro_bgs);
  3321. ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
  3322. info->space_info_kobj, "%s",
  3323. alloc_name(found->flags));
  3324. if (ret) {
  3325. kfree(found);
  3326. return ret;
  3327. }
  3328. *space_info = found;
  3329. list_add_rcu(&found->list, &info->space_info);
  3330. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3331. info->data_sinfo = found;
  3332. return ret;
  3333. }
  3334. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3335. {
  3336. u64 extra_flags = chunk_to_extended(flags) &
  3337. BTRFS_EXTENDED_PROFILE_MASK;
  3338. write_seqlock(&fs_info->profiles_lock);
  3339. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3340. fs_info->avail_data_alloc_bits |= extra_flags;
  3341. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3342. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3343. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3344. fs_info->avail_system_alloc_bits |= extra_flags;
  3345. write_sequnlock(&fs_info->profiles_lock);
  3346. }
  3347. /*
  3348. * returns target flags in extended format or 0 if restripe for this
  3349. * chunk_type is not in progress
  3350. *
  3351. * should be called with either volume_mutex or balance_lock held
  3352. */
  3353. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3354. {
  3355. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3356. u64 target = 0;
  3357. if (!bctl)
  3358. return 0;
  3359. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3360. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3361. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3362. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3363. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3364. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3365. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3366. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3367. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3368. }
  3369. return target;
  3370. }
  3371. /*
  3372. * @flags: available profiles in extended format (see ctree.h)
  3373. *
  3374. * Returns reduced profile in chunk format. If profile changing is in
  3375. * progress (either running or paused) picks the target profile (if it's
  3376. * already available), otherwise falls back to plain reducing.
  3377. */
  3378. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3379. {
  3380. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  3381. u64 target;
  3382. u64 tmp;
  3383. /*
  3384. * see if restripe for this chunk_type is in progress, if so
  3385. * try to reduce to the target profile
  3386. */
  3387. spin_lock(&root->fs_info->balance_lock);
  3388. target = get_restripe_target(root->fs_info, flags);
  3389. if (target) {
  3390. /* pick target profile only if it's already available */
  3391. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3392. spin_unlock(&root->fs_info->balance_lock);
  3393. return extended_to_chunk(target);
  3394. }
  3395. }
  3396. spin_unlock(&root->fs_info->balance_lock);
  3397. /* First, mask out the RAID levels which aren't possible */
  3398. if (num_devices == 1)
  3399. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3400. BTRFS_BLOCK_GROUP_RAID5);
  3401. if (num_devices < 3)
  3402. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3403. if (num_devices < 4)
  3404. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3405. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3406. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3407. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3408. flags &= ~tmp;
  3409. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3410. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3411. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3412. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3413. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3414. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3415. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3416. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3417. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3418. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3419. return extended_to_chunk(flags | tmp);
  3420. }
  3421. static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
  3422. {
  3423. unsigned seq;
  3424. u64 flags;
  3425. do {
  3426. flags = orig_flags;
  3427. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3428. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3429. flags |= root->fs_info->avail_data_alloc_bits;
  3430. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3431. flags |= root->fs_info->avail_system_alloc_bits;
  3432. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3433. flags |= root->fs_info->avail_metadata_alloc_bits;
  3434. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3435. return btrfs_reduce_alloc_profile(root, flags);
  3436. }
  3437. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3438. {
  3439. u64 flags;
  3440. u64 ret;
  3441. if (data)
  3442. flags = BTRFS_BLOCK_GROUP_DATA;
  3443. else if (root == root->fs_info->chunk_root)
  3444. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3445. else
  3446. flags = BTRFS_BLOCK_GROUP_METADATA;
  3447. ret = get_alloc_profile(root, flags);
  3448. return ret;
  3449. }
  3450. /*
  3451. * This will check the space that the inode allocates from to make sure we have
  3452. * enough space for bytes.
  3453. */
  3454. int btrfs_check_data_free_space(struct inode *inode, u64 bytes, u64 write_bytes)
  3455. {
  3456. struct btrfs_space_info *data_sinfo;
  3457. struct btrfs_root *root = BTRFS_I(inode)->root;
  3458. struct btrfs_fs_info *fs_info = root->fs_info;
  3459. u64 used;
  3460. int ret = 0;
  3461. int need_commit = 2;
  3462. int have_pinned_space;
  3463. /* make sure bytes are sectorsize aligned */
  3464. bytes = ALIGN(bytes, root->sectorsize);
  3465. if (btrfs_is_free_space_inode(inode)) {
  3466. need_commit = 0;
  3467. ASSERT(current->journal_info);
  3468. }
  3469. data_sinfo = fs_info->data_sinfo;
  3470. if (!data_sinfo)
  3471. goto alloc;
  3472. again:
  3473. /* make sure we have enough space to handle the data first */
  3474. spin_lock(&data_sinfo->lock);
  3475. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3476. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3477. data_sinfo->bytes_may_use;
  3478. if (used + bytes > data_sinfo->total_bytes) {
  3479. struct btrfs_trans_handle *trans;
  3480. /*
  3481. * if we don't have enough free bytes in this space then we need
  3482. * to alloc a new chunk.
  3483. */
  3484. if (!data_sinfo->full) {
  3485. u64 alloc_target;
  3486. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3487. spin_unlock(&data_sinfo->lock);
  3488. alloc:
  3489. alloc_target = btrfs_get_alloc_profile(root, 1);
  3490. /*
  3491. * It is ugly that we don't call nolock join
  3492. * transaction for the free space inode case here.
  3493. * But it is safe because we only do the data space
  3494. * reservation for the free space cache in the
  3495. * transaction context, the common join transaction
  3496. * just increase the counter of the current transaction
  3497. * handler, doesn't try to acquire the trans_lock of
  3498. * the fs.
  3499. */
  3500. trans = btrfs_join_transaction(root);
  3501. if (IS_ERR(trans))
  3502. return PTR_ERR(trans);
  3503. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3504. alloc_target,
  3505. CHUNK_ALLOC_NO_FORCE);
  3506. btrfs_end_transaction(trans, root);
  3507. if (ret < 0) {
  3508. if (ret != -ENOSPC)
  3509. return ret;
  3510. else {
  3511. have_pinned_space = 1;
  3512. goto commit_trans;
  3513. }
  3514. }
  3515. if (!data_sinfo)
  3516. data_sinfo = fs_info->data_sinfo;
  3517. goto again;
  3518. }
  3519. /*
  3520. * If we don't have enough pinned space to deal with this
  3521. * allocation, and no removed chunk in current transaction,
  3522. * don't bother committing the transaction.
  3523. */
  3524. have_pinned_space = percpu_counter_compare(
  3525. &data_sinfo->total_bytes_pinned,
  3526. used + bytes - data_sinfo->total_bytes);
  3527. spin_unlock(&data_sinfo->lock);
  3528. /* commit the current transaction and try again */
  3529. commit_trans:
  3530. if (need_commit &&
  3531. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3532. need_commit--;
  3533. trans = btrfs_join_transaction(root);
  3534. if (IS_ERR(trans))
  3535. return PTR_ERR(trans);
  3536. if (have_pinned_space >= 0 ||
  3537. trans->transaction->have_free_bgs ||
  3538. need_commit > 0) {
  3539. ret = btrfs_commit_transaction(trans, root);
  3540. if (ret)
  3541. return ret;
  3542. /*
  3543. * make sure that all running delayed iput are
  3544. * done
  3545. */
  3546. down_write(&root->fs_info->delayed_iput_sem);
  3547. up_write(&root->fs_info->delayed_iput_sem);
  3548. goto again;
  3549. } else {
  3550. btrfs_end_transaction(trans, root);
  3551. }
  3552. }
  3553. trace_btrfs_space_reservation(root->fs_info,
  3554. "space_info:enospc",
  3555. data_sinfo->flags, bytes, 1);
  3556. return -ENOSPC;
  3557. }
  3558. ret = btrfs_qgroup_reserve(root, write_bytes);
  3559. if (ret)
  3560. goto out;
  3561. data_sinfo->bytes_may_use += bytes;
  3562. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3563. data_sinfo->flags, bytes, 1);
  3564. out:
  3565. spin_unlock(&data_sinfo->lock);
  3566. return ret;
  3567. }
  3568. /*
  3569. * Called if we need to clear a data reservation for this inode.
  3570. */
  3571. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3572. {
  3573. struct btrfs_root *root = BTRFS_I(inode)->root;
  3574. struct btrfs_space_info *data_sinfo;
  3575. /* make sure bytes are sectorsize aligned */
  3576. bytes = ALIGN(bytes, root->sectorsize);
  3577. data_sinfo = root->fs_info->data_sinfo;
  3578. spin_lock(&data_sinfo->lock);
  3579. WARN_ON(data_sinfo->bytes_may_use < bytes);
  3580. data_sinfo->bytes_may_use -= bytes;
  3581. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3582. data_sinfo->flags, bytes, 0);
  3583. spin_unlock(&data_sinfo->lock);
  3584. }
  3585. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3586. {
  3587. struct list_head *head = &info->space_info;
  3588. struct btrfs_space_info *found;
  3589. rcu_read_lock();
  3590. list_for_each_entry_rcu(found, head, list) {
  3591. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3592. found->force_alloc = CHUNK_ALLOC_FORCE;
  3593. }
  3594. rcu_read_unlock();
  3595. }
  3596. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3597. {
  3598. return (global->size << 1);
  3599. }
  3600. static int should_alloc_chunk(struct btrfs_root *root,
  3601. struct btrfs_space_info *sinfo, int force)
  3602. {
  3603. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3604. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3605. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3606. u64 thresh;
  3607. if (force == CHUNK_ALLOC_FORCE)
  3608. return 1;
  3609. /*
  3610. * We need to take into account the global rsv because for all intents
  3611. * and purposes it's used space. Don't worry about locking the
  3612. * global_rsv, it doesn't change except when the transaction commits.
  3613. */
  3614. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3615. num_allocated += calc_global_rsv_need_space(global_rsv);
  3616. /*
  3617. * in limited mode, we want to have some free space up to
  3618. * about 1% of the FS size.
  3619. */
  3620. if (force == CHUNK_ALLOC_LIMITED) {
  3621. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3622. thresh = max_t(u64, 64 * 1024 * 1024,
  3623. div_factor_fine(thresh, 1));
  3624. if (num_bytes - num_allocated < thresh)
  3625. return 1;
  3626. }
  3627. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3628. return 0;
  3629. return 1;
  3630. }
  3631. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3632. {
  3633. u64 num_dev;
  3634. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3635. BTRFS_BLOCK_GROUP_RAID0 |
  3636. BTRFS_BLOCK_GROUP_RAID5 |
  3637. BTRFS_BLOCK_GROUP_RAID6))
  3638. num_dev = root->fs_info->fs_devices->rw_devices;
  3639. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3640. num_dev = 2;
  3641. else
  3642. num_dev = 1; /* DUP or single */
  3643. /* metadata for updaing devices and chunk tree */
  3644. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3645. }
  3646. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3647. struct btrfs_root *root, u64 type)
  3648. {
  3649. struct btrfs_space_info *info;
  3650. u64 left;
  3651. u64 thresh;
  3652. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3653. spin_lock(&info->lock);
  3654. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3655. info->bytes_reserved - info->bytes_readonly;
  3656. spin_unlock(&info->lock);
  3657. thresh = get_system_chunk_thresh(root, type);
  3658. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3659. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3660. left, thresh, type);
  3661. dump_space_info(info, 0, 0);
  3662. }
  3663. if (left < thresh) {
  3664. u64 flags;
  3665. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3666. btrfs_alloc_chunk(trans, root, flags);
  3667. }
  3668. }
  3669. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3670. struct btrfs_root *extent_root, u64 flags, int force)
  3671. {
  3672. struct btrfs_space_info *space_info;
  3673. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3674. int wait_for_alloc = 0;
  3675. int ret = 0;
  3676. /* Don't re-enter if we're already allocating a chunk */
  3677. if (trans->allocating_chunk)
  3678. return -ENOSPC;
  3679. space_info = __find_space_info(extent_root->fs_info, flags);
  3680. if (!space_info) {
  3681. ret = update_space_info(extent_root->fs_info, flags,
  3682. 0, 0, &space_info);
  3683. BUG_ON(ret); /* -ENOMEM */
  3684. }
  3685. BUG_ON(!space_info); /* Logic error */
  3686. again:
  3687. spin_lock(&space_info->lock);
  3688. if (force < space_info->force_alloc)
  3689. force = space_info->force_alloc;
  3690. if (space_info->full) {
  3691. if (should_alloc_chunk(extent_root, space_info, force))
  3692. ret = -ENOSPC;
  3693. else
  3694. ret = 0;
  3695. spin_unlock(&space_info->lock);
  3696. return ret;
  3697. }
  3698. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3699. spin_unlock(&space_info->lock);
  3700. return 0;
  3701. } else if (space_info->chunk_alloc) {
  3702. wait_for_alloc = 1;
  3703. } else {
  3704. space_info->chunk_alloc = 1;
  3705. }
  3706. spin_unlock(&space_info->lock);
  3707. mutex_lock(&fs_info->chunk_mutex);
  3708. /*
  3709. * The chunk_mutex is held throughout the entirety of a chunk
  3710. * allocation, so once we've acquired the chunk_mutex we know that the
  3711. * other guy is done and we need to recheck and see if we should
  3712. * allocate.
  3713. */
  3714. if (wait_for_alloc) {
  3715. mutex_unlock(&fs_info->chunk_mutex);
  3716. wait_for_alloc = 0;
  3717. goto again;
  3718. }
  3719. trans->allocating_chunk = true;
  3720. /*
  3721. * If we have mixed data/metadata chunks we want to make sure we keep
  3722. * allocating mixed chunks instead of individual chunks.
  3723. */
  3724. if (btrfs_mixed_space_info(space_info))
  3725. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3726. /*
  3727. * if we're doing a data chunk, go ahead and make sure that
  3728. * we keep a reasonable number of metadata chunks allocated in the
  3729. * FS as well.
  3730. */
  3731. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3732. fs_info->data_chunk_allocations++;
  3733. if (!(fs_info->data_chunk_allocations %
  3734. fs_info->metadata_ratio))
  3735. force_metadata_allocation(fs_info);
  3736. }
  3737. /*
  3738. * Check if we have enough space in SYSTEM chunk because we may need
  3739. * to update devices.
  3740. */
  3741. check_system_chunk(trans, extent_root, flags);
  3742. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3743. trans->allocating_chunk = false;
  3744. spin_lock(&space_info->lock);
  3745. if (ret < 0 && ret != -ENOSPC)
  3746. goto out;
  3747. if (ret)
  3748. space_info->full = 1;
  3749. else
  3750. ret = 1;
  3751. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3752. out:
  3753. space_info->chunk_alloc = 0;
  3754. spin_unlock(&space_info->lock);
  3755. mutex_unlock(&fs_info->chunk_mutex);
  3756. return ret;
  3757. }
  3758. static int can_overcommit(struct btrfs_root *root,
  3759. struct btrfs_space_info *space_info, u64 bytes,
  3760. enum btrfs_reserve_flush_enum flush)
  3761. {
  3762. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3763. u64 profile = btrfs_get_alloc_profile(root, 0);
  3764. u64 space_size;
  3765. u64 avail;
  3766. u64 used;
  3767. used = space_info->bytes_used + space_info->bytes_reserved +
  3768. space_info->bytes_pinned + space_info->bytes_readonly;
  3769. /*
  3770. * We only want to allow over committing if we have lots of actual space
  3771. * free, but if we don't have enough space to handle the global reserve
  3772. * space then we could end up having a real enospc problem when trying
  3773. * to allocate a chunk or some other such important allocation.
  3774. */
  3775. spin_lock(&global_rsv->lock);
  3776. space_size = calc_global_rsv_need_space(global_rsv);
  3777. spin_unlock(&global_rsv->lock);
  3778. if (used + space_size >= space_info->total_bytes)
  3779. return 0;
  3780. used += space_info->bytes_may_use;
  3781. spin_lock(&root->fs_info->free_chunk_lock);
  3782. avail = root->fs_info->free_chunk_space;
  3783. spin_unlock(&root->fs_info->free_chunk_lock);
  3784. /*
  3785. * If we have dup, raid1 or raid10 then only half of the free
  3786. * space is actually useable. For raid56, the space info used
  3787. * doesn't include the parity drive, so we don't have to
  3788. * change the math
  3789. */
  3790. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3791. BTRFS_BLOCK_GROUP_RAID1 |
  3792. BTRFS_BLOCK_GROUP_RAID10))
  3793. avail >>= 1;
  3794. /*
  3795. * If we aren't flushing all things, let us overcommit up to
  3796. * 1/2th of the space. If we can flush, don't let us overcommit
  3797. * too much, let it overcommit up to 1/8 of the space.
  3798. */
  3799. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3800. avail >>= 3;
  3801. else
  3802. avail >>= 1;
  3803. if (used + bytes < space_info->total_bytes + avail)
  3804. return 1;
  3805. return 0;
  3806. }
  3807. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3808. unsigned long nr_pages, int nr_items)
  3809. {
  3810. struct super_block *sb = root->fs_info->sb;
  3811. if (down_read_trylock(&sb->s_umount)) {
  3812. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  3813. up_read(&sb->s_umount);
  3814. } else {
  3815. /*
  3816. * We needn't worry the filesystem going from r/w to r/o though
  3817. * we don't acquire ->s_umount mutex, because the filesystem
  3818. * should guarantee the delalloc inodes list be empty after
  3819. * the filesystem is readonly(all dirty pages are written to
  3820. * the disk).
  3821. */
  3822. btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
  3823. if (!current->journal_info)
  3824. btrfs_wait_ordered_roots(root->fs_info, nr_items);
  3825. }
  3826. }
  3827. static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
  3828. {
  3829. u64 bytes;
  3830. int nr;
  3831. bytes = btrfs_calc_trans_metadata_size(root, 1);
  3832. nr = (int)div64_u64(to_reclaim, bytes);
  3833. if (!nr)
  3834. nr = 1;
  3835. return nr;
  3836. }
  3837. #define EXTENT_SIZE_PER_ITEM (256 * 1024)
  3838. /*
  3839. * shrink metadata reservation for delalloc
  3840. */
  3841. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3842. bool wait_ordered)
  3843. {
  3844. struct btrfs_block_rsv *block_rsv;
  3845. struct btrfs_space_info *space_info;
  3846. struct btrfs_trans_handle *trans;
  3847. u64 delalloc_bytes;
  3848. u64 max_reclaim;
  3849. long time_left;
  3850. unsigned long nr_pages;
  3851. int loops;
  3852. int items;
  3853. enum btrfs_reserve_flush_enum flush;
  3854. /* Calc the number of the pages we need flush for space reservation */
  3855. items = calc_reclaim_items_nr(root, to_reclaim);
  3856. to_reclaim = items * EXTENT_SIZE_PER_ITEM;
  3857. trans = (struct btrfs_trans_handle *)current->journal_info;
  3858. block_rsv = &root->fs_info->delalloc_block_rsv;
  3859. space_info = block_rsv->space_info;
  3860. delalloc_bytes = percpu_counter_sum_positive(
  3861. &root->fs_info->delalloc_bytes);
  3862. if (delalloc_bytes == 0) {
  3863. if (trans)
  3864. return;
  3865. if (wait_ordered)
  3866. btrfs_wait_ordered_roots(root->fs_info, items);
  3867. return;
  3868. }
  3869. loops = 0;
  3870. while (delalloc_bytes && loops < 3) {
  3871. max_reclaim = min(delalloc_bytes, to_reclaim);
  3872. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3873. btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
  3874. /*
  3875. * We need to wait for the async pages to actually start before
  3876. * we do anything.
  3877. */
  3878. max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
  3879. if (!max_reclaim)
  3880. goto skip_async;
  3881. if (max_reclaim <= nr_pages)
  3882. max_reclaim = 0;
  3883. else
  3884. max_reclaim -= nr_pages;
  3885. wait_event(root->fs_info->async_submit_wait,
  3886. atomic_read(&root->fs_info->async_delalloc_pages) <=
  3887. (int)max_reclaim);
  3888. skip_async:
  3889. if (!trans)
  3890. flush = BTRFS_RESERVE_FLUSH_ALL;
  3891. else
  3892. flush = BTRFS_RESERVE_NO_FLUSH;
  3893. spin_lock(&space_info->lock);
  3894. if (can_overcommit(root, space_info, orig, flush)) {
  3895. spin_unlock(&space_info->lock);
  3896. break;
  3897. }
  3898. spin_unlock(&space_info->lock);
  3899. loops++;
  3900. if (wait_ordered && !trans) {
  3901. btrfs_wait_ordered_roots(root->fs_info, items);
  3902. } else {
  3903. time_left = schedule_timeout_killable(1);
  3904. if (time_left)
  3905. break;
  3906. }
  3907. delalloc_bytes = percpu_counter_sum_positive(
  3908. &root->fs_info->delalloc_bytes);
  3909. }
  3910. }
  3911. /**
  3912. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3913. * @root - the root we're allocating for
  3914. * @bytes - the number of bytes we want to reserve
  3915. * @force - force the commit
  3916. *
  3917. * This will check to make sure that committing the transaction will actually
  3918. * get us somewhere and then commit the transaction if it does. Otherwise it
  3919. * will return -ENOSPC.
  3920. */
  3921. static int may_commit_transaction(struct btrfs_root *root,
  3922. struct btrfs_space_info *space_info,
  3923. u64 bytes, int force)
  3924. {
  3925. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3926. struct btrfs_trans_handle *trans;
  3927. trans = (struct btrfs_trans_handle *)current->journal_info;
  3928. if (trans)
  3929. return -EAGAIN;
  3930. if (force)
  3931. goto commit;
  3932. /* See if there is enough pinned space to make this reservation */
  3933. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3934. bytes) >= 0)
  3935. goto commit;
  3936. /*
  3937. * See if there is some space in the delayed insertion reservation for
  3938. * this reservation.
  3939. */
  3940. if (space_info != delayed_rsv->space_info)
  3941. return -ENOSPC;
  3942. spin_lock(&delayed_rsv->lock);
  3943. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3944. bytes - delayed_rsv->size) >= 0) {
  3945. spin_unlock(&delayed_rsv->lock);
  3946. return -ENOSPC;
  3947. }
  3948. spin_unlock(&delayed_rsv->lock);
  3949. commit:
  3950. trans = btrfs_join_transaction(root);
  3951. if (IS_ERR(trans))
  3952. return -ENOSPC;
  3953. return btrfs_commit_transaction(trans, root);
  3954. }
  3955. enum flush_state {
  3956. FLUSH_DELAYED_ITEMS_NR = 1,
  3957. FLUSH_DELAYED_ITEMS = 2,
  3958. FLUSH_DELALLOC = 3,
  3959. FLUSH_DELALLOC_WAIT = 4,
  3960. ALLOC_CHUNK = 5,
  3961. COMMIT_TRANS = 6,
  3962. };
  3963. static int flush_space(struct btrfs_root *root,
  3964. struct btrfs_space_info *space_info, u64 num_bytes,
  3965. u64 orig_bytes, int state)
  3966. {
  3967. struct btrfs_trans_handle *trans;
  3968. int nr;
  3969. int ret = 0;
  3970. switch (state) {
  3971. case FLUSH_DELAYED_ITEMS_NR:
  3972. case FLUSH_DELAYED_ITEMS:
  3973. if (state == FLUSH_DELAYED_ITEMS_NR)
  3974. nr = calc_reclaim_items_nr(root, num_bytes) * 2;
  3975. else
  3976. nr = -1;
  3977. trans = btrfs_join_transaction(root);
  3978. if (IS_ERR(trans)) {
  3979. ret = PTR_ERR(trans);
  3980. break;
  3981. }
  3982. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3983. btrfs_end_transaction(trans, root);
  3984. break;
  3985. case FLUSH_DELALLOC:
  3986. case FLUSH_DELALLOC_WAIT:
  3987. shrink_delalloc(root, num_bytes * 2, orig_bytes,
  3988. state == FLUSH_DELALLOC_WAIT);
  3989. break;
  3990. case ALLOC_CHUNK:
  3991. trans = btrfs_join_transaction(root);
  3992. if (IS_ERR(trans)) {
  3993. ret = PTR_ERR(trans);
  3994. break;
  3995. }
  3996. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3997. btrfs_get_alloc_profile(root, 0),
  3998. CHUNK_ALLOC_NO_FORCE);
  3999. btrfs_end_transaction(trans, root);
  4000. if (ret == -ENOSPC)
  4001. ret = 0;
  4002. break;
  4003. case COMMIT_TRANS:
  4004. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  4005. break;
  4006. default:
  4007. ret = -ENOSPC;
  4008. break;
  4009. }
  4010. return ret;
  4011. }
  4012. static inline u64
  4013. btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
  4014. struct btrfs_space_info *space_info)
  4015. {
  4016. u64 used;
  4017. u64 expected;
  4018. u64 to_reclaim;
  4019. to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
  4020. 16 * 1024 * 1024);
  4021. spin_lock(&space_info->lock);
  4022. if (can_overcommit(root, space_info, to_reclaim,
  4023. BTRFS_RESERVE_FLUSH_ALL)) {
  4024. to_reclaim = 0;
  4025. goto out;
  4026. }
  4027. used = space_info->bytes_used + space_info->bytes_reserved +
  4028. space_info->bytes_pinned + space_info->bytes_readonly +
  4029. space_info->bytes_may_use;
  4030. if (can_overcommit(root, space_info, 1024 * 1024,
  4031. BTRFS_RESERVE_FLUSH_ALL))
  4032. expected = div_factor_fine(space_info->total_bytes, 95);
  4033. else
  4034. expected = div_factor_fine(space_info->total_bytes, 90);
  4035. if (used > expected)
  4036. to_reclaim = used - expected;
  4037. else
  4038. to_reclaim = 0;
  4039. to_reclaim = min(to_reclaim, space_info->bytes_may_use +
  4040. space_info->bytes_reserved);
  4041. out:
  4042. spin_unlock(&space_info->lock);
  4043. return to_reclaim;
  4044. }
  4045. static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
  4046. struct btrfs_fs_info *fs_info, u64 used)
  4047. {
  4048. u64 thresh = div_factor_fine(space_info->total_bytes, 98);
  4049. /* If we're just plain full then async reclaim just slows us down. */
  4050. if (space_info->bytes_used >= thresh)
  4051. return 0;
  4052. return (used >= thresh && !btrfs_fs_closing(fs_info) &&
  4053. !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
  4054. }
  4055. static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
  4056. struct btrfs_fs_info *fs_info,
  4057. int flush_state)
  4058. {
  4059. u64 used;
  4060. spin_lock(&space_info->lock);
  4061. /*
  4062. * We run out of space and have not got any free space via flush_space,
  4063. * so don't bother doing async reclaim.
  4064. */
  4065. if (flush_state > COMMIT_TRANS && space_info->full) {
  4066. spin_unlock(&space_info->lock);
  4067. return 0;
  4068. }
  4069. used = space_info->bytes_used + space_info->bytes_reserved +
  4070. space_info->bytes_pinned + space_info->bytes_readonly +
  4071. space_info->bytes_may_use;
  4072. if (need_do_async_reclaim(space_info, fs_info, used)) {
  4073. spin_unlock(&space_info->lock);
  4074. return 1;
  4075. }
  4076. spin_unlock(&space_info->lock);
  4077. return 0;
  4078. }
  4079. static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
  4080. {
  4081. struct btrfs_fs_info *fs_info;
  4082. struct btrfs_space_info *space_info;
  4083. u64 to_reclaim;
  4084. int flush_state;
  4085. fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
  4086. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4087. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
  4088. space_info);
  4089. if (!to_reclaim)
  4090. return;
  4091. flush_state = FLUSH_DELAYED_ITEMS_NR;
  4092. do {
  4093. flush_space(fs_info->fs_root, space_info, to_reclaim,
  4094. to_reclaim, flush_state);
  4095. flush_state++;
  4096. if (!btrfs_need_do_async_reclaim(space_info, fs_info,
  4097. flush_state))
  4098. return;
  4099. } while (flush_state < COMMIT_TRANS);
  4100. }
  4101. void btrfs_init_async_reclaim_work(struct work_struct *work)
  4102. {
  4103. INIT_WORK(work, btrfs_async_reclaim_metadata_space);
  4104. }
  4105. /**
  4106. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  4107. * @root - the root we're allocating for
  4108. * @block_rsv - the block_rsv we're allocating for
  4109. * @orig_bytes - the number of bytes we want
  4110. * @flush - whether or not we can flush to make our reservation
  4111. *
  4112. * This will reserve orgi_bytes number of bytes from the space info associated
  4113. * with the block_rsv. If there is not enough space it will make an attempt to
  4114. * flush out space to make room. It will do this by flushing delalloc if
  4115. * possible or committing the transaction. If flush is 0 then no attempts to
  4116. * regain reservations will be made and this will fail if there is not enough
  4117. * space already.
  4118. */
  4119. static int reserve_metadata_bytes(struct btrfs_root *root,
  4120. struct btrfs_block_rsv *block_rsv,
  4121. u64 orig_bytes,
  4122. enum btrfs_reserve_flush_enum flush)
  4123. {
  4124. struct btrfs_space_info *space_info = block_rsv->space_info;
  4125. u64 used;
  4126. u64 num_bytes = orig_bytes;
  4127. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  4128. int ret = 0;
  4129. bool flushing = false;
  4130. again:
  4131. ret = 0;
  4132. spin_lock(&space_info->lock);
  4133. /*
  4134. * We only want to wait if somebody other than us is flushing and we
  4135. * are actually allowed to flush all things.
  4136. */
  4137. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  4138. space_info->flush) {
  4139. spin_unlock(&space_info->lock);
  4140. /*
  4141. * If we have a trans handle we can't wait because the flusher
  4142. * may have to commit the transaction, which would mean we would
  4143. * deadlock since we are waiting for the flusher to finish, but
  4144. * hold the current transaction open.
  4145. */
  4146. if (current->journal_info)
  4147. return -EAGAIN;
  4148. ret = wait_event_killable(space_info->wait, !space_info->flush);
  4149. /* Must have been killed, return */
  4150. if (ret)
  4151. return -EINTR;
  4152. spin_lock(&space_info->lock);
  4153. }
  4154. ret = -ENOSPC;
  4155. used = space_info->bytes_used + space_info->bytes_reserved +
  4156. space_info->bytes_pinned + space_info->bytes_readonly +
  4157. space_info->bytes_may_use;
  4158. /*
  4159. * The idea here is that we've not already over-reserved the block group
  4160. * then we can go ahead and save our reservation first and then start
  4161. * flushing if we need to. Otherwise if we've already overcommitted
  4162. * lets start flushing stuff first and then come back and try to make
  4163. * our reservation.
  4164. */
  4165. if (used <= space_info->total_bytes) {
  4166. if (used + orig_bytes <= space_info->total_bytes) {
  4167. space_info->bytes_may_use += orig_bytes;
  4168. trace_btrfs_space_reservation(root->fs_info,
  4169. "space_info", space_info->flags, orig_bytes, 1);
  4170. ret = 0;
  4171. } else {
  4172. /*
  4173. * Ok set num_bytes to orig_bytes since we aren't
  4174. * overocmmitted, this way we only try and reclaim what
  4175. * we need.
  4176. */
  4177. num_bytes = orig_bytes;
  4178. }
  4179. } else {
  4180. /*
  4181. * Ok we're over committed, set num_bytes to the overcommitted
  4182. * amount plus the amount of bytes that we need for this
  4183. * reservation.
  4184. */
  4185. num_bytes = used - space_info->total_bytes +
  4186. (orig_bytes * 2);
  4187. }
  4188. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  4189. space_info->bytes_may_use += orig_bytes;
  4190. trace_btrfs_space_reservation(root->fs_info, "space_info",
  4191. space_info->flags, orig_bytes,
  4192. 1);
  4193. ret = 0;
  4194. }
  4195. /*
  4196. * Couldn't make our reservation, save our place so while we're trying
  4197. * to reclaim space we can actually use it instead of somebody else
  4198. * stealing it from us.
  4199. *
  4200. * We make the other tasks wait for the flush only when we can flush
  4201. * all things.
  4202. */
  4203. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  4204. flushing = true;
  4205. space_info->flush = 1;
  4206. } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
  4207. used += orig_bytes;
  4208. /*
  4209. * We will do the space reservation dance during log replay,
  4210. * which means we won't have fs_info->fs_root set, so don't do
  4211. * the async reclaim as we will panic.
  4212. */
  4213. if (!root->fs_info->log_root_recovering &&
  4214. need_do_async_reclaim(space_info, root->fs_info, used) &&
  4215. !work_busy(&root->fs_info->async_reclaim_work))
  4216. queue_work(system_unbound_wq,
  4217. &root->fs_info->async_reclaim_work);
  4218. }
  4219. spin_unlock(&space_info->lock);
  4220. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  4221. goto out;
  4222. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  4223. flush_state);
  4224. flush_state++;
  4225. /*
  4226. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  4227. * would happen. So skip delalloc flush.
  4228. */
  4229. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  4230. (flush_state == FLUSH_DELALLOC ||
  4231. flush_state == FLUSH_DELALLOC_WAIT))
  4232. flush_state = ALLOC_CHUNK;
  4233. if (!ret)
  4234. goto again;
  4235. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  4236. flush_state < COMMIT_TRANS)
  4237. goto again;
  4238. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  4239. flush_state <= COMMIT_TRANS)
  4240. goto again;
  4241. out:
  4242. if (ret == -ENOSPC &&
  4243. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  4244. struct btrfs_block_rsv *global_rsv =
  4245. &root->fs_info->global_block_rsv;
  4246. if (block_rsv != global_rsv &&
  4247. !block_rsv_use_bytes(global_rsv, orig_bytes))
  4248. ret = 0;
  4249. }
  4250. if (ret == -ENOSPC)
  4251. trace_btrfs_space_reservation(root->fs_info,
  4252. "space_info:enospc",
  4253. space_info->flags, orig_bytes, 1);
  4254. if (flushing) {
  4255. spin_lock(&space_info->lock);
  4256. space_info->flush = 0;
  4257. wake_up_all(&space_info->wait);
  4258. spin_unlock(&space_info->lock);
  4259. }
  4260. return ret;
  4261. }
  4262. static struct btrfs_block_rsv *get_block_rsv(
  4263. const struct btrfs_trans_handle *trans,
  4264. const struct btrfs_root *root)
  4265. {
  4266. struct btrfs_block_rsv *block_rsv = NULL;
  4267. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  4268. block_rsv = trans->block_rsv;
  4269. if (root == root->fs_info->csum_root && trans->adding_csums)
  4270. block_rsv = trans->block_rsv;
  4271. if (root == root->fs_info->uuid_root)
  4272. block_rsv = trans->block_rsv;
  4273. if (!block_rsv)
  4274. block_rsv = root->block_rsv;
  4275. if (!block_rsv)
  4276. block_rsv = &root->fs_info->empty_block_rsv;
  4277. return block_rsv;
  4278. }
  4279. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  4280. u64 num_bytes)
  4281. {
  4282. int ret = -ENOSPC;
  4283. spin_lock(&block_rsv->lock);
  4284. if (block_rsv->reserved >= num_bytes) {
  4285. block_rsv->reserved -= num_bytes;
  4286. if (block_rsv->reserved < block_rsv->size)
  4287. block_rsv->full = 0;
  4288. ret = 0;
  4289. }
  4290. spin_unlock(&block_rsv->lock);
  4291. return ret;
  4292. }
  4293. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  4294. u64 num_bytes, int update_size)
  4295. {
  4296. spin_lock(&block_rsv->lock);
  4297. block_rsv->reserved += num_bytes;
  4298. if (update_size)
  4299. block_rsv->size += num_bytes;
  4300. else if (block_rsv->reserved >= block_rsv->size)
  4301. block_rsv->full = 1;
  4302. spin_unlock(&block_rsv->lock);
  4303. }
  4304. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  4305. struct btrfs_block_rsv *dest, u64 num_bytes,
  4306. int min_factor)
  4307. {
  4308. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4309. u64 min_bytes;
  4310. if (global_rsv->space_info != dest->space_info)
  4311. return -ENOSPC;
  4312. spin_lock(&global_rsv->lock);
  4313. min_bytes = div_factor(global_rsv->size, min_factor);
  4314. if (global_rsv->reserved < min_bytes + num_bytes) {
  4315. spin_unlock(&global_rsv->lock);
  4316. return -ENOSPC;
  4317. }
  4318. global_rsv->reserved -= num_bytes;
  4319. if (global_rsv->reserved < global_rsv->size)
  4320. global_rsv->full = 0;
  4321. spin_unlock(&global_rsv->lock);
  4322. block_rsv_add_bytes(dest, num_bytes, 1);
  4323. return 0;
  4324. }
  4325. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  4326. struct btrfs_block_rsv *block_rsv,
  4327. struct btrfs_block_rsv *dest, u64 num_bytes)
  4328. {
  4329. struct btrfs_space_info *space_info = block_rsv->space_info;
  4330. spin_lock(&block_rsv->lock);
  4331. if (num_bytes == (u64)-1)
  4332. num_bytes = block_rsv->size;
  4333. block_rsv->size -= num_bytes;
  4334. if (block_rsv->reserved >= block_rsv->size) {
  4335. num_bytes = block_rsv->reserved - block_rsv->size;
  4336. block_rsv->reserved = block_rsv->size;
  4337. block_rsv->full = 1;
  4338. } else {
  4339. num_bytes = 0;
  4340. }
  4341. spin_unlock(&block_rsv->lock);
  4342. if (num_bytes > 0) {
  4343. if (dest) {
  4344. spin_lock(&dest->lock);
  4345. if (!dest->full) {
  4346. u64 bytes_to_add;
  4347. bytes_to_add = dest->size - dest->reserved;
  4348. bytes_to_add = min(num_bytes, bytes_to_add);
  4349. dest->reserved += bytes_to_add;
  4350. if (dest->reserved >= dest->size)
  4351. dest->full = 1;
  4352. num_bytes -= bytes_to_add;
  4353. }
  4354. spin_unlock(&dest->lock);
  4355. }
  4356. if (num_bytes) {
  4357. spin_lock(&space_info->lock);
  4358. space_info->bytes_may_use -= num_bytes;
  4359. trace_btrfs_space_reservation(fs_info, "space_info",
  4360. space_info->flags, num_bytes, 0);
  4361. spin_unlock(&space_info->lock);
  4362. }
  4363. }
  4364. }
  4365. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  4366. struct btrfs_block_rsv *dst, u64 num_bytes)
  4367. {
  4368. int ret;
  4369. ret = block_rsv_use_bytes(src, num_bytes);
  4370. if (ret)
  4371. return ret;
  4372. block_rsv_add_bytes(dst, num_bytes, 1);
  4373. return 0;
  4374. }
  4375. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  4376. {
  4377. memset(rsv, 0, sizeof(*rsv));
  4378. spin_lock_init(&rsv->lock);
  4379. rsv->type = type;
  4380. }
  4381. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  4382. unsigned short type)
  4383. {
  4384. struct btrfs_block_rsv *block_rsv;
  4385. struct btrfs_fs_info *fs_info = root->fs_info;
  4386. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  4387. if (!block_rsv)
  4388. return NULL;
  4389. btrfs_init_block_rsv(block_rsv, type);
  4390. block_rsv->space_info = __find_space_info(fs_info,
  4391. BTRFS_BLOCK_GROUP_METADATA);
  4392. return block_rsv;
  4393. }
  4394. void btrfs_free_block_rsv(struct btrfs_root *root,
  4395. struct btrfs_block_rsv *rsv)
  4396. {
  4397. if (!rsv)
  4398. return;
  4399. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4400. kfree(rsv);
  4401. }
  4402. void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
  4403. {
  4404. kfree(rsv);
  4405. }
  4406. int btrfs_block_rsv_add(struct btrfs_root *root,
  4407. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  4408. enum btrfs_reserve_flush_enum flush)
  4409. {
  4410. int ret;
  4411. if (num_bytes == 0)
  4412. return 0;
  4413. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4414. if (!ret) {
  4415. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  4416. return 0;
  4417. }
  4418. return ret;
  4419. }
  4420. int btrfs_block_rsv_check(struct btrfs_root *root,
  4421. struct btrfs_block_rsv *block_rsv, int min_factor)
  4422. {
  4423. u64 num_bytes = 0;
  4424. int ret = -ENOSPC;
  4425. if (!block_rsv)
  4426. return 0;
  4427. spin_lock(&block_rsv->lock);
  4428. num_bytes = div_factor(block_rsv->size, min_factor);
  4429. if (block_rsv->reserved >= num_bytes)
  4430. ret = 0;
  4431. spin_unlock(&block_rsv->lock);
  4432. return ret;
  4433. }
  4434. int btrfs_block_rsv_refill(struct btrfs_root *root,
  4435. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  4436. enum btrfs_reserve_flush_enum flush)
  4437. {
  4438. u64 num_bytes = 0;
  4439. int ret = -ENOSPC;
  4440. if (!block_rsv)
  4441. return 0;
  4442. spin_lock(&block_rsv->lock);
  4443. num_bytes = min_reserved;
  4444. if (block_rsv->reserved >= num_bytes)
  4445. ret = 0;
  4446. else
  4447. num_bytes -= block_rsv->reserved;
  4448. spin_unlock(&block_rsv->lock);
  4449. if (!ret)
  4450. return 0;
  4451. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4452. if (!ret) {
  4453. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  4454. return 0;
  4455. }
  4456. return ret;
  4457. }
  4458. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  4459. struct btrfs_block_rsv *dst_rsv,
  4460. u64 num_bytes)
  4461. {
  4462. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4463. }
  4464. void btrfs_block_rsv_release(struct btrfs_root *root,
  4465. struct btrfs_block_rsv *block_rsv,
  4466. u64 num_bytes)
  4467. {
  4468. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4469. if (global_rsv == block_rsv ||
  4470. block_rsv->space_info != global_rsv->space_info)
  4471. global_rsv = NULL;
  4472. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4473. num_bytes);
  4474. }
  4475. /*
  4476. * helper to calculate size of global block reservation.
  4477. * the desired value is sum of space used by extent tree,
  4478. * checksum tree and root tree
  4479. */
  4480. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4481. {
  4482. struct btrfs_space_info *sinfo;
  4483. u64 num_bytes;
  4484. u64 meta_used;
  4485. u64 data_used;
  4486. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4487. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4488. spin_lock(&sinfo->lock);
  4489. data_used = sinfo->bytes_used;
  4490. spin_unlock(&sinfo->lock);
  4491. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4492. spin_lock(&sinfo->lock);
  4493. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4494. data_used = 0;
  4495. meta_used = sinfo->bytes_used;
  4496. spin_unlock(&sinfo->lock);
  4497. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4498. csum_size * 2;
  4499. num_bytes += div_u64(data_used + meta_used, 50);
  4500. if (num_bytes * 3 > meta_used)
  4501. num_bytes = div_u64(meta_used, 3);
  4502. return ALIGN(num_bytes, fs_info->extent_root->nodesize << 10);
  4503. }
  4504. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4505. {
  4506. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4507. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4508. u64 num_bytes;
  4509. num_bytes = calc_global_metadata_size(fs_info);
  4510. spin_lock(&sinfo->lock);
  4511. spin_lock(&block_rsv->lock);
  4512. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4513. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4514. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4515. sinfo->bytes_may_use;
  4516. if (sinfo->total_bytes > num_bytes) {
  4517. num_bytes = sinfo->total_bytes - num_bytes;
  4518. block_rsv->reserved += num_bytes;
  4519. sinfo->bytes_may_use += num_bytes;
  4520. trace_btrfs_space_reservation(fs_info, "space_info",
  4521. sinfo->flags, num_bytes, 1);
  4522. }
  4523. if (block_rsv->reserved >= block_rsv->size) {
  4524. num_bytes = block_rsv->reserved - block_rsv->size;
  4525. sinfo->bytes_may_use -= num_bytes;
  4526. trace_btrfs_space_reservation(fs_info, "space_info",
  4527. sinfo->flags, num_bytes, 0);
  4528. block_rsv->reserved = block_rsv->size;
  4529. block_rsv->full = 1;
  4530. }
  4531. spin_unlock(&block_rsv->lock);
  4532. spin_unlock(&sinfo->lock);
  4533. }
  4534. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4535. {
  4536. struct btrfs_space_info *space_info;
  4537. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4538. fs_info->chunk_block_rsv.space_info = space_info;
  4539. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4540. fs_info->global_block_rsv.space_info = space_info;
  4541. fs_info->delalloc_block_rsv.space_info = space_info;
  4542. fs_info->trans_block_rsv.space_info = space_info;
  4543. fs_info->empty_block_rsv.space_info = space_info;
  4544. fs_info->delayed_block_rsv.space_info = space_info;
  4545. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4546. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4547. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4548. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4549. if (fs_info->quota_root)
  4550. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4551. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4552. update_global_block_rsv(fs_info);
  4553. }
  4554. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4555. {
  4556. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4557. (u64)-1);
  4558. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4559. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4560. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4561. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4562. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4563. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4564. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4565. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4566. }
  4567. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4568. struct btrfs_root *root)
  4569. {
  4570. if (!trans->block_rsv)
  4571. return;
  4572. if (!trans->bytes_reserved)
  4573. return;
  4574. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4575. trans->transid, trans->bytes_reserved, 0);
  4576. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4577. trans->bytes_reserved = 0;
  4578. }
  4579. /* Can only return 0 or -ENOSPC */
  4580. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4581. struct inode *inode)
  4582. {
  4583. struct btrfs_root *root = BTRFS_I(inode)->root;
  4584. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4585. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4586. /*
  4587. * We need to hold space in order to delete our orphan item once we've
  4588. * added it, so this takes the reservation so we can release it later
  4589. * when we are truly done with the orphan item.
  4590. */
  4591. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4592. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4593. btrfs_ino(inode), num_bytes, 1);
  4594. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4595. }
  4596. void btrfs_orphan_release_metadata(struct inode *inode)
  4597. {
  4598. struct btrfs_root *root = BTRFS_I(inode)->root;
  4599. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4600. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4601. btrfs_ino(inode), num_bytes, 0);
  4602. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4603. }
  4604. /*
  4605. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4606. * root: the root of the parent directory
  4607. * rsv: block reservation
  4608. * items: the number of items that we need do reservation
  4609. * qgroup_reserved: used to return the reserved size in qgroup
  4610. *
  4611. * This function is used to reserve the space for snapshot/subvolume
  4612. * creation and deletion. Those operations are different with the
  4613. * common file/directory operations, they change two fs/file trees
  4614. * and root tree, the number of items that the qgroup reserves is
  4615. * different with the free space reservation. So we can not use
  4616. * the space reseravtion mechanism in start_transaction().
  4617. */
  4618. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4619. struct btrfs_block_rsv *rsv,
  4620. int items,
  4621. u64 *qgroup_reserved,
  4622. bool use_global_rsv)
  4623. {
  4624. u64 num_bytes;
  4625. int ret;
  4626. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4627. if (root->fs_info->quota_enabled) {
  4628. /* One for parent inode, two for dir entries */
  4629. num_bytes = 3 * root->nodesize;
  4630. ret = btrfs_qgroup_reserve(root, num_bytes);
  4631. if (ret)
  4632. return ret;
  4633. } else {
  4634. num_bytes = 0;
  4635. }
  4636. *qgroup_reserved = num_bytes;
  4637. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4638. rsv->space_info = __find_space_info(root->fs_info,
  4639. BTRFS_BLOCK_GROUP_METADATA);
  4640. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4641. BTRFS_RESERVE_FLUSH_ALL);
  4642. if (ret == -ENOSPC && use_global_rsv)
  4643. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
  4644. if (ret) {
  4645. if (*qgroup_reserved)
  4646. btrfs_qgroup_free(root, *qgroup_reserved);
  4647. }
  4648. return ret;
  4649. }
  4650. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4651. struct btrfs_block_rsv *rsv,
  4652. u64 qgroup_reserved)
  4653. {
  4654. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4655. }
  4656. /**
  4657. * drop_outstanding_extent - drop an outstanding extent
  4658. * @inode: the inode we're dropping the extent for
  4659. * @num_bytes: the number of bytes we're relaseing.
  4660. *
  4661. * This is called when we are freeing up an outstanding extent, either called
  4662. * after an error or after an extent is written. This will return the number of
  4663. * reserved extents that need to be freed. This must be called with
  4664. * BTRFS_I(inode)->lock held.
  4665. */
  4666. static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
  4667. {
  4668. unsigned drop_inode_space = 0;
  4669. unsigned dropped_extents = 0;
  4670. unsigned num_extents = 0;
  4671. num_extents = (unsigned)div64_u64(num_bytes +
  4672. BTRFS_MAX_EXTENT_SIZE - 1,
  4673. BTRFS_MAX_EXTENT_SIZE);
  4674. ASSERT(num_extents);
  4675. ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
  4676. BTRFS_I(inode)->outstanding_extents -= num_extents;
  4677. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4678. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4679. &BTRFS_I(inode)->runtime_flags))
  4680. drop_inode_space = 1;
  4681. /*
  4682. * If we have more or the same amount of outsanding extents than we have
  4683. * reserved then we need to leave the reserved extents count alone.
  4684. */
  4685. if (BTRFS_I(inode)->outstanding_extents >=
  4686. BTRFS_I(inode)->reserved_extents)
  4687. return drop_inode_space;
  4688. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4689. BTRFS_I(inode)->outstanding_extents;
  4690. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4691. return dropped_extents + drop_inode_space;
  4692. }
  4693. /**
  4694. * calc_csum_metadata_size - return the amount of metada space that must be
  4695. * reserved/free'd for the given bytes.
  4696. * @inode: the inode we're manipulating
  4697. * @num_bytes: the number of bytes in question
  4698. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4699. *
  4700. * This adjusts the number of csum_bytes in the inode and then returns the
  4701. * correct amount of metadata that must either be reserved or freed. We
  4702. * calculate how many checksums we can fit into one leaf and then divide the
  4703. * number of bytes that will need to be checksumed by this value to figure out
  4704. * how many checksums will be required. If we are adding bytes then the number
  4705. * may go up and we will return the number of additional bytes that must be
  4706. * reserved. If it is going down we will return the number of bytes that must
  4707. * be freed.
  4708. *
  4709. * This must be called with BTRFS_I(inode)->lock held.
  4710. */
  4711. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4712. int reserve)
  4713. {
  4714. struct btrfs_root *root = BTRFS_I(inode)->root;
  4715. u64 old_csums, num_csums;
  4716. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4717. BTRFS_I(inode)->csum_bytes == 0)
  4718. return 0;
  4719. old_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
  4720. if (reserve)
  4721. BTRFS_I(inode)->csum_bytes += num_bytes;
  4722. else
  4723. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4724. num_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
  4725. /* No change, no need to reserve more */
  4726. if (old_csums == num_csums)
  4727. return 0;
  4728. if (reserve)
  4729. return btrfs_calc_trans_metadata_size(root,
  4730. num_csums - old_csums);
  4731. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4732. }
  4733. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4734. {
  4735. struct btrfs_root *root = BTRFS_I(inode)->root;
  4736. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4737. u64 to_reserve = 0;
  4738. u64 csum_bytes;
  4739. unsigned nr_extents = 0;
  4740. int extra_reserve = 0;
  4741. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4742. int ret = 0;
  4743. bool delalloc_lock = true;
  4744. u64 to_free = 0;
  4745. unsigned dropped;
  4746. /* If we are a free space inode we need to not flush since we will be in
  4747. * the middle of a transaction commit. We also don't need the delalloc
  4748. * mutex since we won't race with anybody. We need this mostly to make
  4749. * lockdep shut its filthy mouth.
  4750. */
  4751. if (btrfs_is_free_space_inode(inode)) {
  4752. flush = BTRFS_RESERVE_NO_FLUSH;
  4753. delalloc_lock = false;
  4754. }
  4755. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4756. btrfs_transaction_in_commit(root->fs_info))
  4757. schedule_timeout(1);
  4758. if (delalloc_lock)
  4759. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4760. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4761. spin_lock(&BTRFS_I(inode)->lock);
  4762. nr_extents = (unsigned)div64_u64(num_bytes +
  4763. BTRFS_MAX_EXTENT_SIZE - 1,
  4764. BTRFS_MAX_EXTENT_SIZE);
  4765. BTRFS_I(inode)->outstanding_extents += nr_extents;
  4766. nr_extents = 0;
  4767. if (BTRFS_I(inode)->outstanding_extents >
  4768. BTRFS_I(inode)->reserved_extents)
  4769. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4770. BTRFS_I(inode)->reserved_extents;
  4771. /*
  4772. * Add an item to reserve for updating the inode when we complete the
  4773. * delalloc io.
  4774. */
  4775. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4776. &BTRFS_I(inode)->runtime_flags)) {
  4777. nr_extents++;
  4778. extra_reserve = 1;
  4779. }
  4780. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4781. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4782. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4783. spin_unlock(&BTRFS_I(inode)->lock);
  4784. if (root->fs_info->quota_enabled) {
  4785. ret = btrfs_qgroup_reserve(root, nr_extents * root->nodesize);
  4786. if (ret)
  4787. goto out_fail;
  4788. }
  4789. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4790. if (unlikely(ret)) {
  4791. if (root->fs_info->quota_enabled)
  4792. btrfs_qgroup_free(root, nr_extents * root->nodesize);
  4793. goto out_fail;
  4794. }
  4795. spin_lock(&BTRFS_I(inode)->lock);
  4796. if (extra_reserve) {
  4797. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4798. &BTRFS_I(inode)->runtime_flags);
  4799. nr_extents--;
  4800. }
  4801. BTRFS_I(inode)->reserved_extents += nr_extents;
  4802. spin_unlock(&BTRFS_I(inode)->lock);
  4803. if (delalloc_lock)
  4804. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4805. if (to_reserve)
  4806. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4807. btrfs_ino(inode), to_reserve, 1);
  4808. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4809. return 0;
  4810. out_fail:
  4811. spin_lock(&BTRFS_I(inode)->lock);
  4812. dropped = drop_outstanding_extent(inode, num_bytes);
  4813. /*
  4814. * If the inodes csum_bytes is the same as the original
  4815. * csum_bytes then we know we haven't raced with any free()ers
  4816. * so we can just reduce our inodes csum bytes and carry on.
  4817. */
  4818. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4819. calc_csum_metadata_size(inode, num_bytes, 0);
  4820. } else {
  4821. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4822. u64 bytes;
  4823. /*
  4824. * This is tricky, but first we need to figure out how much we
  4825. * free'd from any free-ers that occured during this
  4826. * reservation, so we reset ->csum_bytes to the csum_bytes
  4827. * before we dropped our lock, and then call the free for the
  4828. * number of bytes that were freed while we were trying our
  4829. * reservation.
  4830. */
  4831. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4832. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4833. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4834. /*
  4835. * Now we need to see how much we would have freed had we not
  4836. * been making this reservation and our ->csum_bytes were not
  4837. * artificially inflated.
  4838. */
  4839. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4840. bytes = csum_bytes - orig_csum_bytes;
  4841. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4842. /*
  4843. * Now reset ->csum_bytes to what it should be. If bytes is
  4844. * more than to_free then we would have free'd more space had we
  4845. * not had an artificially high ->csum_bytes, so we need to free
  4846. * the remainder. If bytes is the same or less then we don't
  4847. * need to do anything, the other free-ers did the correct
  4848. * thing.
  4849. */
  4850. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4851. if (bytes > to_free)
  4852. to_free = bytes - to_free;
  4853. else
  4854. to_free = 0;
  4855. }
  4856. spin_unlock(&BTRFS_I(inode)->lock);
  4857. if (dropped)
  4858. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4859. if (to_free) {
  4860. btrfs_block_rsv_release(root, block_rsv, to_free);
  4861. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4862. btrfs_ino(inode), to_free, 0);
  4863. }
  4864. if (delalloc_lock)
  4865. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4866. return ret;
  4867. }
  4868. /**
  4869. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4870. * @inode: the inode to release the reservation for
  4871. * @num_bytes: the number of bytes we're releasing
  4872. *
  4873. * This will release the metadata reservation for an inode. This can be called
  4874. * once we complete IO for a given set of bytes to release their metadata
  4875. * reservations.
  4876. */
  4877. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4878. {
  4879. struct btrfs_root *root = BTRFS_I(inode)->root;
  4880. u64 to_free = 0;
  4881. unsigned dropped;
  4882. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4883. spin_lock(&BTRFS_I(inode)->lock);
  4884. dropped = drop_outstanding_extent(inode, num_bytes);
  4885. if (num_bytes)
  4886. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4887. spin_unlock(&BTRFS_I(inode)->lock);
  4888. if (dropped > 0)
  4889. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4890. if (btrfs_test_is_dummy_root(root))
  4891. return;
  4892. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4893. btrfs_ino(inode), to_free, 0);
  4894. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4895. to_free);
  4896. }
  4897. /**
  4898. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4899. * @inode: inode we're writing to
  4900. * @num_bytes: the number of bytes we want to allocate
  4901. *
  4902. * This will do the following things
  4903. *
  4904. * o reserve space in the data space info for num_bytes
  4905. * o reserve space in the metadata space info based on number of outstanding
  4906. * extents and how much csums will be needed
  4907. * o add to the inodes ->delalloc_bytes
  4908. * o add it to the fs_info's delalloc inodes list.
  4909. *
  4910. * This will return 0 for success and -ENOSPC if there is no space left.
  4911. */
  4912. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4913. {
  4914. int ret;
  4915. ret = btrfs_check_data_free_space(inode, num_bytes, num_bytes);
  4916. if (ret)
  4917. return ret;
  4918. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4919. if (ret) {
  4920. btrfs_free_reserved_data_space(inode, num_bytes);
  4921. return ret;
  4922. }
  4923. return 0;
  4924. }
  4925. /**
  4926. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4927. * @inode: inode we're releasing space for
  4928. * @num_bytes: the number of bytes we want to free up
  4929. *
  4930. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4931. * called in the case that we don't need the metadata AND data reservations
  4932. * anymore. So if there is an error or we insert an inline extent.
  4933. *
  4934. * This function will release the metadata space that was not used and will
  4935. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4936. * list if there are no delalloc bytes left.
  4937. */
  4938. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4939. {
  4940. btrfs_delalloc_release_metadata(inode, num_bytes);
  4941. btrfs_free_reserved_data_space(inode, num_bytes);
  4942. }
  4943. static int update_block_group(struct btrfs_trans_handle *trans,
  4944. struct btrfs_root *root, u64 bytenr,
  4945. u64 num_bytes, int alloc)
  4946. {
  4947. struct btrfs_block_group_cache *cache = NULL;
  4948. struct btrfs_fs_info *info = root->fs_info;
  4949. u64 total = num_bytes;
  4950. u64 old_val;
  4951. u64 byte_in_group;
  4952. int factor;
  4953. /* block accounting for super block */
  4954. spin_lock(&info->delalloc_root_lock);
  4955. old_val = btrfs_super_bytes_used(info->super_copy);
  4956. if (alloc)
  4957. old_val += num_bytes;
  4958. else
  4959. old_val -= num_bytes;
  4960. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4961. spin_unlock(&info->delalloc_root_lock);
  4962. while (total) {
  4963. cache = btrfs_lookup_block_group(info, bytenr);
  4964. if (!cache)
  4965. return -ENOENT;
  4966. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4967. BTRFS_BLOCK_GROUP_RAID1 |
  4968. BTRFS_BLOCK_GROUP_RAID10))
  4969. factor = 2;
  4970. else
  4971. factor = 1;
  4972. /*
  4973. * If this block group has free space cache written out, we
  4974. * need to make sure to load it if we are removing space. This
  4975. * is because we need the unpinning stage to actually add the
  4976. * space back to the block group, otherwise we will leak space.
  4977. */
  4978. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4979. cache_block_group(cache, 1);
  4980. byte_in_group = bytenr - cache->key.objectid;
  4981. WARN_ON(byte_in_group > cache->key.offset);
  4982. spin_lock(&cache->space_info->lock);
  4983. spin_lock(&cache->lock);
  4984. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4985. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4986. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4987. old_val = btrfs_block_group_used(&cache->item);
  4988. num_bytes = min(total, cache->key.offset - byte_in_group);
  4989. if (alloc) {
  4990. old_val += num_bytes;
  4991. btrfs_set_block_group_used(&cache->item, old_val);
  4992. cache->reserved -= num_bytes;
  4993. cache->space_info->bytes_reserved -= num_bytes;
  4994. cache->space_info->bytes_used += num_bytes;
  4995. cache->space_info->disk_used += num_bytes * factor;
  4996. spin_unlock(&cache->lock);
  4997. spin_unlock(&cache->space_info->lock);
  4998. } else {
  4999. old_val -= num_bytes;
  5000. btrfs_set_block_group_used(&cache->item, old_val);
  5001. cache->pinned += num_bytes;
  5002. cache->space_info->bytes_pinned += num_bytes;
  5003. cache->space_info->bytes_used -= num_bytes;
  5004. cache->space_info->disk_used -= num_bytes * factor;
  5005. spin_unlock(&cache->lock);
  5006. spin_unlock(&cache->space_info->lock);
  5007. set_extent_dirty(info->pinned_extents,
  5008. bytenr, bytenr + num_bytes - 1,
  5009. GFP_NOFS | __GFP_NOFAIL);
  5010. /*
  5011. * No longer have used bytes in this block group, queue
  5012. * it for deletion.
  5013. */
  5014. if (old_val == 0) {
  5015. spin_lock(&info->unused_bgs_lock);
  5016. if (list_empty(&cache->bg_list)) {
  5017. btrfs_get_block_group(cache);
  5018. list_add_tail(&cache->bg_list,
  5019. &info->unused_bgs);
  5020. }
  5021. spin_unlock(&info->unused_bgs_lock);
  5022. }
  5023. }
  5024. spin_lock(&trans->transaction->dirty_bgs_lock);
  5025. if (list_empty(&cache->dirty_list)) {
  5026. list_add_tail(&cache->dirty_list,
  5027. &trans->transaction->dirty_bgs);
  5028. trans->transaction->num_dirty_bgs++;
  5029. btrfs_get_block_group(cache);
  5030. }
  5031. spin_unlock(&trans->transaction->dirty_bgs_lock);
  5032. btrfs_put_block_group(cache);
  5033. total -= num_bytes;
  5034. bytenr += num_bytes;
  5035. }
  5036. return 0;
  5037. }
  5038. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  5039. {
  5040. struct btrfs_block_group_cache *cache;
  5041. u64 bytenr;
  5042. spin_lock(&root->fs_info->block_group_cache_lock);
  5043. bytenr = root->fs_info->first_logical_byte;
  5044. spin_unlock(&root->fs_info->block_group_cache_lock);
  5045. if (bytenr < (u64)-1)
  5046. return bytenr;
  5047. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  5048. if (!cache)
  5049. return 0;
  5050. bytenr = cache->key.objectid;
  5051. btrfs_put_block_group(cache);
  5052. return bytenr;
  5053. }
  5054. static int pin_down_extent(struct btrfs_root *root,
  5055. struct btrfs_block_group_cache *cache,
  5056. u64 bytenr, u64 num_bytes, int reserved)
  5057. {
  5058. spin_lock(&cache->space_info->lock);
  5059. spin_lock(&cache->lock);
  5060. cache->pinned += num_bytes;
  5061. cache->space_info->bytes_pinned += num_bytes;
  5062. if (reserved) {
  5063. cache->reserved -= num_bytes;
  5064. cache->space_info->bytes_reserved -= num_bytes;
  5065. }
  5066. spin_unlock(&cache->lock);
  5067. spin_unlock(&cache->space_info->lock);
  5068. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  5069. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  5070. if (reserved)
  5071. trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
  5072. return 0;
  5073. }
  5074. /*
  5075. * this function must be called within transaction
  5076. */
  5077. int btrfs_pin_extent(struct btrfs_root *root,
  5078. u64 bytenr, u64 num_bytes, int reserved)
  5079. {
  5080. struct btrfs_block_group_cache *cache;
  5081. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  5082. BUG_ON(!cache); /* Logic error */
  5083. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  5084. btrfs_put_block_group(cache);
  5085. return 0;
  5086. }
  5087. /*
  5088. * this function must be called within transaction
  5089. */
  5090. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  5091. u64 bytenr, u64 num_bytes)
  5092. {
  5093. struct btrfs_block_group_cache *cache;
  5094. int ret;
  5095. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  5096. if (!cache)
  5097. return -EINVAL;
  5098. /*
  5099. * pull in the free space cache (if any) so that our pin
  5100. * removes the free space from the cache. We have load_only set
  5101. * to one because the slow code to read in the free extents does check
  5102. * the pinned extents.
  5103. */
  5104. cache_block_group(cache, 1);
  5105. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  5106. /* remove us from the free space cache (if we're there at all) */
  5107. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  5108. btrfs_put_block_group(cache);
  5109. return ret;
  5110. }
  5111. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  5112. {
  5113. int ret;
  5114. struct btrfs_block_group_cache *block_group;
  5115. struct btrfs_caching_control *caching_ctl;
  5116. block_group = btrfs_lookup_block_group(root->fs_info, start);
  5117. if (!block_group)
  5118. return -EINVAL;
  5119. cache_block_group(block_group, 0);
  5120. caching_ctl = get_caching_control(block_group);
  5121. if (!caching_ctl) {
  5122. /* Logic error */
  5123. BUG_ON(!block_group_cache_done(block_group));
  5124. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  5125. } else {
  5126. mutex_lock(&caching_ctl->mutex);
  5127. if (start >= caching_ctl->progress) {
  5128. ret = add_excluded_extent(root, start, num_bytes);
  5129. } else if (start + num_bytes <= caching_ctl->progress) {
  5130. ret = btrfs_remove_free_space(block_group,
  5131. start, num_bytes);
  5132. } else {
  5133. num_bytes = caching_ctl->progress - start;
  5134. ret = btrfs_remove_free_space(block_group,
  5135. start, num_bytes);
  5136. if (ret)
  5137. goto out_lock;
  5138. num_bytes = (start + num_bytes) -
  5139. caching_ctl->progress;
  5140. start = caching_ctl->progress;
  5141. ret = add_excluded_extent(root, start, num_bytes);
  5142. }
  5143. out_lock:
  5144. mutex_unlock(&caching_ctl->mutex);
  5145. put_caching_control(caching_ctl);
  5146. }
  5147. btrfs_put_block_group(block_group);
  5148. return ret;
  5149. }
  5150. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  5151. struct extent_buffer *eb)
  5152. {
  5153. struct btrfs_file_extent_item *item;
  5154. struct btrfs_key key;
  5155. int found_type;
  5156. int i;
  5157. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  5158. return 0;
  5159. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  5160. btrfs_item_key_to_cpu(eb, &key, i);
  5161. if (key.type != BTRFS_EXTENT_DATA_KEY)
  5162. continue;
  5163. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  5164. found_type = btrfs_file_extent_type(eb, item);
  5165. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  5166. continue;
  5167. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  5168. continue;
  5169. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  5170. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  5171. __exclude_logged_extent(log, key.objectid, key.offset);
  5172. }
  5173. return 0;
  5174. }
  5175. /**
  5176. * btrfs_update_reserved_bytes - update the block_group and space info counters
  5177. * @cache: The cache we are manipulating
  5178. * @num_bytes: The number of bytes in question
  5179. * @reserve: One of the reservation enums
  5180. * @delalloc: The blocks are allocated for the delalloc write
  5181. *
  5182. * This is called by the allocator when it reserves space, or by somebody who is
  5183. * freeing space that was never actually used on disk. For example if you
  5184. * reserve some space for a new leaf in transaction A and before transaction A
  5185. * commits you free that leaf, you call this with reserve set to 0 in order to
  5186. * clear the reservation.
  5187. *
  5188. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  5189. * ENOSPC accounting. For data we handle the reservation through clearing the
  5190. * delalloc bits in the io_tree. We have to do this since we could end up
  5191. * allocating less disk space for the amount of data we have reserved in the
  5192. * case of compression.
  5193. *
  5194. * If this is a reservation and the block group has become read only we cannot
  5195. * make the reservation and return -EAGAIN, otherwise this function always
  5196. * succeeds.
  5197. */
  5198. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  5199. u64 num_bytes, int reserve, int delalloc)
  5200. {
  5201. struct btrfs_space_info *space_info = cache->space_info;
  5202. int ret = 0;
  5203. spin_lock(&space_info->lock);
  5204. spin_lock(&cache->lock);
  5205. if (reserve != RESERVE_FREE) {
  5206. if (cache->ro) {
  5207. ret = -EAGAIN;
  5208. } else {
  5209. cache->reserved += num_bytes;
  5210. space_info->bytes_reserved += num_bytes;
  5211. if (reserve == RESERVE_ALLOC) {
  5212. trace_btrfs_space_reservation(cache->fs_info,
  5213. "space_info", space_info->flags,
  5214. num_bytes, 0);
  5215. space_info->bytes_may_use -= num_bytes;
  5216. }
  5217. if (delalloc)
  5218. cache->delalloc_bytes += num_bytes;
  5219. }
  5220. } else {
  5221. if (cache->ro)
  5222. space_info->bytes_readonly += num_bytes;
  5223. cache->reserved -= num_bytes;
  5224. space_info->bytes_reserved -= num_bytes;
  5225. if (delalloc)
  5226. cache->delalloc_bytes -= num_bytes;
  5227. }
  5228. spin_unlock(&cache->lock);
  5229. spin_unlock(&space_info->lock);
  5230. return ret;
  5231. }
  5232. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  5233. struct btrfs_root *root)
  5234. {
  5235. struct btrfs_fs_info *fs_info = root->fs_info;
  5236. struct btrfs_caching_control *next;
  5237. struct btrfs_caching_control *caching_ctl;
  5238. struct btrfs_block_group_cache *cache;
  5239. down_write(&fs_info->commit_root_sem);
  5240. list_for_each_entry_safe(caching_ctl, next,
  5241. &fs_info->caching_block_groups, list) {
  5242. cache = caching_ctl->block_group;
  5243. if (block_group_cache_done(cache)) {
  5244. cache->last_byte_to_unpin = (u64)-1;
  5245. list_del_init(&caching_ctl->list);
  5246. put_caching_control(caching_ctl);
  5247. } else {
  5248. cache->last_byte_to_unpin = caching_ctl->progress;
  5249. }
  5250. }
  5251. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  5252. fs_info->pinned_extents = &fs_info->freed_extents[1];
  5253. else
  5254. fs_info->pinned_extents = &fs_info->freed_extents[0];
  5255. up_write(&fs_info->commit_root_sem);
  5256. update_global_block_rsv(fs_info);
  5257. }
  5258. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end,
  5259. const bool return_free_space)
  5260. {
  5261. struct btrfs_fs_info *fs_info = root->fs_info;
  5262. struct btrfs_block_group_cache *cache = NULL;
  5263. struct btrfs_space_info *space_info;
  5264. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  5265. u64 len;
  5266. bool readonly;
  5267. while (start <= end) {
  5268. readonly = false;
  5269. if (!cache ||
  5270. start >= cache->key.objectid + cache->key.offset) {
  5271. if (cache)
  5272. btrfs_put_block_group(cache);
  5273. cache = btrfs_lookup_block_group(fs_info, start);
  5274. BUG_ON(!cache); /* Logic error */
  5275. }
  5276. len = cache->key.objectid + cache->key.offset - start;
  5277. len = min(len, end + 1 - start);
  5278. if (start < cache->last_byte_to_unpin) {
  5279. len = min(len, cache->last_byte_to_unpin - start);
  5280. if (return_free_space)
  5281. btrfs_add_free_space(cache, start, len);
  5282. }
  5283. start += len;
  5284. space_info = cache->space_info;
  5285. spin_lock(&space_info->lock);
  5286. spin_lock(&cache->lock);
  5287. cache->pinned -= len;
  5288. space_info->bytes_pinned -= len;
  5289. percpu_counter_add(&space_info->total_bytes_pinned, -len);
  5290. if (cache->ro) {
  5291. space_info->bytes_readonly += len;
  5292. readonly = true;
  5293. }
  5294. spin_unlock(&cache->lock);
  5295. if (!readonly && global_rsv->space_info == space_info) {
  5296. spin_lock(&global_rsv->lock);
  5297. if (!global_rsv->full) {
  5298. len = min(len, global_rsv->size -
  5299. global_rsv->reserved);
  5300. global_rsv->reserved += len;
  5301. space_info->bytes_may_use += len;
  5302. if (global_rsv->reserved >= global_rsv->size)
  5303. global_rsv->full = 1;
  5304. }
  5305. spin_unlock(&global_rsv->lock);
  5306. }
  5307. spin_unlock(&space_info->lock);
  5308. }
  5309. if (cache)
  5310. btrfs_put_block_group(cache);
  5311. return 0;
  5312. }
  5313. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  5314. struct btrfs_root *root)
  5315. {
  5316. struct btrfs_fs_info *fs_info = root->fs_info;
  5317. struct extent_io_tree *unpin;
  5318. u64 start;
  5319. u64 end;
  5320. int ret;
  5321. if (trans->aborted)
  5322. return 0;
  5323. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  5324. unpin = &fs_info->freed_extents[1];
  5325. else
  5326. unpin = &fs_info->freed_extents[0];
  5327. while (1) {
  5328. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  5329. ret = find_first_extent_bit(unpin, 0, &start, &end,
  5330. EXTENT_DIRTY, NULL);
  5331. if (ret) {
  5332. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  5333. break;
  5334. }
  5335. if (btrfs_test_opt(root, DISCARD))
  5336. ret = btrfs_discard_extent(root, start,
  5337. end + 1 - start, NULL);
  5338. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  5339. unpin_extent_range(root, start, end, true);
  5340. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  5341. cond_resched();
  5342. }
  5343. return 0;
  5344. }
  5345. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
  5346. u64 owner, u64 root_objectid)
  5347. {
  5348. struct btrfs_space_info *space_info;
  5349. u64 flags;
  5350. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5351. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  5352. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  5353. else
  5354. flags = BTRFS_BLOCK_GROUP_METADATA;
  5355. } else {
  5356. flags = BTRFS_BLOCK_GROUP_DATA;
  5357. }
  5358. space_info = __find_space_info(fs_info, flags);
  5359. BUG_ON(!space_info); /* Logic bug */
  5360. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  5361. }
  5362. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  5363. struct btrfs_root *root,
  5364. u64 bytenr, u64 num_bytes, u64 parent,
  5365. u64 root_objectid, u64 owner_objectid,
  5366. u64 owner_offset, int refs_to_drop,
  5367. struct btrfs_delayed_extent_op *extent_op,
  5368. int no_quota)
  5369. {
  5370. struct btrfs_key key;
  5371. struct btrfs_path *path;
  5372. struct btrfs_fs_info *info = root->fs_info;
  5373. struct btrfs_root *extent_root = info->extent_root;
  5374. struct extent_buffer *leaf;
  5375. struct btrfs_extent_item *ei;
  5376. struct btrfs_extent_inline_ref *iref;
  5377. int ret;
  5378. int is_data;
  5379. int extent_slot = 0;
  5380. int found_extent = 0;
  5381. int num_to_del = 1;
  5382. u32 item_size;
  5383. u64 refs;
  5384. int last_ref = 0;
  5385. enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_SUB_EXCL;
  5386. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5387. SKINNY_METADATA);
  5388. if (!info->quota_enabled || !is_fstree(root_objectid))
  5389. no_quota = 1;
  5390. path = btrfs_alloc_path();
  5391. if (!path)
  5392. return -ENOMEM;
  5393. path->reada = 1;
  5394. path->leave_spinning = 1;
  5395. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  5396. BUG_ON(!is_data && refs_to_drop != 1);
  5397. if (is_data)
  5398. skinny_metadata = 0;
  5399. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  5400. bytenr, num_bytes, parent,
  5401. root_objectid, owner_objectid,
  5402. owner_offset);
  5403. if (ret == 0) {
  5404. extent_slot = path->slots[0];
  5405. while (extent_slot >= 0) {
  5406. btrfs_item_key_to_cpu(path->nodes[0], &key,
  5407. extent_slot);
  5408. if (key.objectid != bytenr)
  5409. break;
  5410. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  5411. key.offset == num_bytes) {
  5412. found_extent = 1;
  5413. break;
  5414. }
  5415. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  5416. key.offset == owner_objectid) {
  5417. found_extent = 1;
  5418. break;
  5419. }
  5420. if (path->slots[0] - extent_slot > 5)
  5421. break;
  5422. extent_slot--;
  5423. }
  5424. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5425. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  5426. if (found_extent && item_size < sizeof(*ei))
  5427. found_extent = 0;
  5428. #endif
  5429. if (!found_extent) {
  5430. BUG_ON(iref);
  5431. ret = remove_extent_backref(trans, extent_root, path,
  5432. NULL, refs_to_drop,
  5433. is_data, &last_ref);
  5434. if (ret) {
  5435. btrfs_abort_transaction(trans, extent_root, ret);
  5436. goto out;
  5437. }
  5438. btrfs_release_path(path);
  5439. path->leave_spinning = 1;
  5440. key.objectid = bytenr;
  5441. key.type = BTRFS_EXTENT_ITEM_KEY;
  5442. key.offset = num_bytes;
  5443. if (!is_data && skinny_metadata) {
  5444. key.type = BTRFS_METADATA_ITEM_KEY;
  5445. key.offset = owner_objectid;
  5446. }
  5447. ret = btrfs_search_slot(trans, extent_root,
  5448. &key, path, -1, 1);
  5449. if (ret > 0 && skinny_metadata && path->slots[0]) {
  5450. /*
  5451. * Couldn't find our skinny metadata item,
  5452. * see if we have ye olde extent item.
  5453. */
  5454. path->slots[0]--;
  5455. btrfs_item_key_to_cpu(path->nodes[0], &key,
  5456. path->slots[0]);
  5457. if (key.objectid == bytenr &&
  5458. key.type == BTRFS_EXTENT_ITEM_KEY &&
  5459. key.offset == num_bytes)
  5460. ret = 0;
  5461. }
  5462. if (ret > 0 && skinny_metadata) {
  5463. skinny_metadata = false;
  5464. key.objectid = bytenr;
  5465. key.type = BTRFS_EXTENT_ITEM_KEY;
  5466. key.offset = num_bytes;
  5467. btrfs_release_path(path);
  5468. ret = btrfs_search_slot(trans, extent_root,
  5469. &key, path, -1, 1);
  5470. }
  5471. if (ret) {
  5472. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5473. ret, bytenr);
  5474. if (ret > 0)
  5475. btrfs_print_leaf(extent_root,
  5476. path->nodes[0]);
  5477. }
  5478. if (ret < 0) {
  5479. btrfs_abort_transaction(trans, extent_root, ret);
  5480. goto out;
  5481. }
  5482. extent_slot = path->slots[0];
  5483. }
  5484. } else if (WARN_ON(ret == -ENOENT)) {
  5485. btrfs_print_leaf(extent_root, path->nodes[0]);
  5486. btrfs_err(info,
  5487. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  5488. bytenr, parent, root_objectid, owner_objectid,
  5489. owner_offset);
  5490. btrfs_abort_transaction(trans, extent_root, ret);
  5491. goto out;
  5492. } else {
  5493. btrfs_abort_transaction(trans, extent_root, ret);
  5494. goto out;
  5495. }
  5496. leaf = path->nodes[0];
  5497. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5498. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5499. if (item_size < sizeof(*ei)) {
  5500. BUG_ON(found_extent || extent_slot != path->slots[0]);
  5501. ret = convert_extent_item_v0(trans, extent_root, path,
  5502. owner_objectid, 0);
  5503. if (ret < 0) {
  5504. btrfs_abort_transaction(trans, extent_root, ret);
  5505. goto out;
  5506. }
  5507. btrfs_release_path(path);
  5508. path->leave_spinning = 1;
  5509. key.objectid = bytenr;
  5510. key.type = BTRFS_EXTENT_ITEM_KEY;
  5511. key.offset = num_bytes;
  5512. ret = btrfs_search_slot(trans, extent_root, &key, path,
  5513. -1, 1);
  5514. if (ret) {
  5515. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5516. ret, bytenr);
  5517. btrfs_print_leaf(extent_root, path->nodes[0]);
  5518. }
  5519. if (ret < 0) {
  5520. btrfs_abort_transaction(trans, extent_root, ret);
  5521. goto out;
  5522. }
  5523. extent_slot = path->slots[0];
  5524. leaf = path->nodes[0];
  5525. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5526. }
  5527. #endif
  5528. BUG_ON(item_size < sizeof(*ei));
  5529. ei = btrfs_item_ptr(leaf, extent_slot,
  5530. struct btrfs_extent_item);
  5531. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5532. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5533. struct btrfs_tree_block_info *bi;
  5534. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5535. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5536. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5537. }
  5538. refs = btrfs_extent_refs(leaf, ei);
  5539. if (refs < refs_to_drop) {
  5540. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5541. "for bytenr %Lu", refs_to_drop, refs, bytenr);
  5542. ret = -EINVAL;
  5543. btrfs_abort_transaction(trans, extent_root, ret);
  5544. goto out;
  5545. }
  5546. refs -= refs_to_drop;
  5547. if (refs > 0) {
  5548. type = BTRFS_QGROUP_OPER_SUB_SHARED;
  5549. if (extent_op)
  5550. __run_delayed_extent_op(extent_op, leaf, ei);
  5551. /*
  5552. * In the case of inline back ref, reference count will
  5553. * be updated by remove_extent_backref
  5554. */
  5555. if (iref) {
  5556. BUG_ON(!found_extent);
  5557. } else {
  5558. btrfs_set_extent_refs(leaf, ei, refs);
  5559. btrfs_mark_buffer_dirty(leaf);
  5560. }
  5561. if (found_extent) {
  5562. ret = remove_extent_backref(trans, extent_root, path,
  5563. iref, refs_to_drop,
  5564. is_data, &last_ref);
  5565. if (ret) {
  5566. btrfs_abort_transaction(trans, extent_root, ret);
  5567. goto out;
  5568. }
  5569. }
  5570. add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
  5571. root_objectid);
  5572. } else {
  5573. if (found_extent) {
  5574. BUG_ON(is_data && refs_to_drop !=
  5575. extent_data_ref_count(root, path, iref));
  5576. if (iref) {
  5577. BUG_ON(path->slots[0] != extent_slot);
  5578. } else {
  5579. BUG_ON(path->slots[0] != extent_slot + 1);
  5580. path->slots[0] = extent_slot;
  5581. num_to_del = 2;
  5582. }
  5583. }
  5584. last_ref = 1;
  5585. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5586. num_to_del);
  5587. if (ret) {
  5588. btrfs_abort_transaction(trans, extent_root, ret);
  5589. goto out;
  5590. }
  5591. btrfs_release_path(path);
  5592. if (is_data) {
  5593. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5594. if (ret) {
  5595. btrfs_abort_transaction(trans, extent_root, ret);
  5596. goto out;
  5597. }
  5598. }
  5599. ret = update_block_group(trans, root, bytenr, num_bytes, 0);
  5600. if (ret) {
  5601. btrfs_abort_transaction(trans, extent_root, ret);
  5602. goto out;
  5603. }
  5604. }
  5605. btrfs_release_path(path);
  5606. /* Deal with the quota accounting */
  5607. if (!ret && last_ref && !no_quota) {
  5608. int mod_seq = 0;
  5609. if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
  5610. type == BTRFS_QGROUP_OPER_SUB_SHARED)
  5611. mod_seq = 1;
  5612. ret = btrfs_qgroup_record_ref(trans, info, root_objectid,
  5613. bytenr, num_bytes, type,
  5614. mod_seq);
  5615. }
  5616. out:
  5617. btrfs_free_path(path);
  5618. return ret;
  5619. }
  5620. /*
  5621. * when we free an block, it is possible (and likely) that we free the last
  5622. * delayed ref for that extent as well. This searches the delayed ref tree for
  5623. * a given extent, and if there are no other delayed refs to be processed, it
  5624. * removes it from the tree.
  5625. */
  5626. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5627. struct btrfs_root *root, u64 bytenr)
  5628. {
  5629. struct btrfs_delayed_ref_head *head;
  5630. struct btrfs_delayed_ref_root *delayed_refs;
  5631. int ret = 0;
  5632. delayed_refs = &trans->transaction->delayed_refs;
  5633. spin_lock(&delayed_refs->lock);
  5634. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5635. if (!head)
  5636. goto out_delayed_unlock;
  5637. spin_lock(&head->lock);
  5638. if (rb_first(&head->ref_root))
  5639. goto out;
  5640. if (head->extent_op) {
  5641. if (!head->must_insert_reserved)
  5642. goto out;
  5643. btrfs_free_delayed_extent_op(head->extent_op);
  5644. head->extent_op = NULL;
  5645. }
  5646. /*
  5647. * waiting for the lock here would deadlock. If someone else has it
  5648. * locked they are already in the process of dropping it anyway
  5649. */
  5650. if (!mutex_trylock(&head->mutex))
  5651. goto out;
  5652. /*
  5653. * at this point we have a head with no other entries. Go
  5654. * ahead and process it.
  5655. */
  5656. head->node.in_tree = 0;
  5657. rb_erase(&head->href_node, &delayed_refs->href_root);
  5658. atomic_dec(&delayed_refs->num_entries);
  5659. /*
  5660. * we don't take a ref on the node because we're removing it from the
  5661. * tree, so we just steal the ref the tree was holding.
  5662. */
  5663. delayed_refs->num_heads--;
  5664. if (head->processing == 0)
  5665. delayed_refs->num_heads_ready--;
  5666. head->processing = 0;
  5667. spin_unlock(&head->lock);
  5668. spin_unlock(&delayed_refs->lock);
  5669. BUG_ON(head->extent_op);
  5670. if (head->must_insert_reserved)
  5671. ret = 1;
  5672. mutex_unlock(&head->mutex);
  5673. btrfs_put_delayed_ref(&head->node);
  5674. return ret;
  5675. out:
  5676. spin_unlock(&head->lock);
  5677. out_delayed_unlock:
  5678. spin_unlock(&delayed_refs->lock);
  5679. return 0;
  5680. }
  5681. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5682. struct btrfs_root *root,
  5683. struct extent_buffer *buf,
  5684. u64 parent, int last_ref)
  5685. {
  5686. int pin = 1;
  5687. int ret;
  5688. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5689. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5690. buf->start, buf->len,
  5691. parent, root->root_key.objectid,
  5692. btrfs_header_level(buf),
  5693. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5694. BUG_ON(ret); /* -ENOMEM */
  5695. }
  5696. if (!last_ref)
  5697. return;
  5698. if (btrfs_header_generation(buf) == trans->transid) {
  5699. struct btrfs_block_group_cache *cache;
  5700. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5701. ret = check_ref_cleanup(trans, root, buf->start);
  5702. if (!ret)
  5703. goto out;
  5704. }
  5705. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5706. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5707. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5708. btrfs_put_block_group(cache);
  5709. goto out;
  5710. }
  5711. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5712. btrfs_add_free_space(cache, buf->start, buf->len);
  5713. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
  5714. btrfs_put_block_group(cache);
  5715. trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
  5716. pin = 0;
  5717. }
  5718. out:
  5719. if (pin)
  5720. add_pinned_bytes(root->fs_info, buf->len,
  5721. btrfs_header_level(buf),
  5722. root->root_key.objectid);
  5723. /*
  5724. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5725. * anymore.
  5726. */
  5727. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5728. }
  5729. /* Can return -ENOMEM */
  5730. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5731. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5732. u64 owner, u64 offset, int no_quota)
  5733. {
  5734. int ret;
  5735. struct btrfs_fs_info *fs_info = root->fs_info;
  5736. if (btrfs_test_is_dummy_root(root))
  5737. return 0;
  5738. add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
  5739. /*
  5740. * tree log blocks never actually go into the extent allocation
  5741. * tree, just update pinning info and exit early.
  5742. */
  5743. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5744. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5745. /* unlocks the pinned mutex */
  5746. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5747. ret = 0;
  5748. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5749. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5750. num_bytes,
  5751. parent, root_objectid, (int)owner,
  5752. BTRFS_DROP_DELAYED_REF, NULL, no_quota);
  5753. } else {
  5754. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5755. num_bytes,
  5756. parent, root_objectid, owner,
  5757. offset, BTRFS_DROP_DELAYED_REF,
  5758. NULL, no_quota);
  5759. }
  5760. return ret;
  5761. }
  5762. /*
  5763. * when we wait for progress in the block group caching, its because
  5764. * our allocation attempt failed at least once. So, we must sleep
  5765. * and let some progress happen before we try again.
  5766. *
  5767. * This function will sleep at least once waiting for new free space to
  5768. * show up, and then it will check the block group free space numbers
  5769. * for our min num_bytes. Another option is to have it go ahead
  5770. * and look in the rbtree for a free extent of a given size, but this
  5771. * is a good start.
  5772. *
  5773. * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
  5774. * any of the information in this block group.
  5775. */
  5776. static noinline void
  5777. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5778. u64 num_bytes)
  5779. {
  5780. struct btrfs_caching_control *caching_ctl;
  5781. caching_ctl = get_caching_control(cache);
  5782. if (!caching_ctl)
  5783. return;
  5784. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5785. (cache->free_space_ctl->free_space >= num_bytes));
  5786. put_caching_control(caching_ctl);
  5787. }
  5788. static noinline int
  5789. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5790. {
  5791. struct btrfs_caching_control *caching_ctl;
  5792. int ret = 0;
  5793. caching_ctl = get_caching_control(cache);
  5794. if (!caching_ctl)
  5795. return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
  5796. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5797. if (cache->cached == BTRFS_CACHE_ERROR)
  5798. ret = -EIO;
  5799. put_caching_control(caching_ctl);
  5800. return ret;
  5801. }
  5802. int __get_raid_index(u64 flags)
  5803. {
  5804. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5805. return BTRFS_RAID_RAID10;
  5806. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5807. return BTRFS_RAID_RAID1;
  5808. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5809. return BTRFS_RAID_DUP;
  5810. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5811. return BTRFS_RAID_RAID0;
  5812. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5813. return BTRFS_RAID_RAID5;
  5814. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5815. return BTRFS_RAID_RAID6;
  5816. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5817. }
  5818. int get_block_group_index(struct btrfs_block_group_cache *cache)
  5819. {
  5820. return __get_raid_index(cache->flags);
  5821. }
  5822. static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
  5823. [BTRFS_RAID_RAID10] = "raid10",
  5824. [BTRFS_RAID_RAID1] = "raid1",
  5825. [BTRFS_RAID_DUP] = "dup",
  5826. [BTRFS_RAID_RAID0] = "raid0",
  5827. [BTRFS_RAID_SINGLE] = "single",
  5828. [BTRFS_RAID_RAID5] = "raid5",
  5829. [BTRFS_RAID_RAID6] = "raid6",
  5830. };
  5831. static const char *get_raid_name(enum btrfs_raid_types type)
  5832. {
  5833. if (type >= BTRFS_NR_RAID_TYPES)
  5834. return NULL;
  5835. return btrfs_raid_type_names[type];
  5836. }
  5837. enum btrfs_loop_type {
  5838. LOOP_CACHING_NOWAIT = 0,
  5839. LOOP_CACHING_WAIT = 1,
  5840. LOOP_ALLOC_CHUNK = 2,
  5841. LOOP_NO_EMPTY_SIZE = 3,
  5842. };
  5843. static inline void
  5844. btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
  5845. int delalloc)
  5846. {
  5847. if (delalloc)
  5848. down_read(&cache->data_rwsem);
  5849. }
  5850. static inline void
  5851. btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
  5852. int delalloc)
  5853. {
  5854. btrfs_get_block_group(cache);
  5855. if (delalloc)
  5856. down_read(&cache->data_rwsem);
  5857. }
  5858. static struct btrfs_block_group_cache *
  5859. btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
  5860. struct btrfs_free_cluster *cluster,
  5861. int delalloc)
  5862. {
  5863. struct btrfs_block_group_cache *used_bg;
  5864. bool locked = false;
  5865. again:
  5866. spin_lock(&cluster->refill_lock);
  5867. if (locked) {
  5868. if (used_bg == cluster->block_group)
  5869. return used_bg;
  5870. up_read(&used_bg->data_rwsem);
  5871. btrfs_put_block_group(used_bg);
  5872. }
  5873. used_bg = cluster->block_group;
  5874. if (!used_bg)
  5875. return NULL;
  5876. if (used_bg == block_group)
  5877. return used_bg;
  5878. btrfs_get_block_group(used_bg);
  5879. if (!delalloc)
  5880. return used_bg;
  5881. if (down_read_trylock(&used_bg->data_rwsem))
  5882. return used_bg;
  5883. spin_unlock(&cluster->refill_lock);
  5884. down_read(&used_bg->data_rwsem);
  5885. locked = true;
  5886. goto again;
  5887. }
  5888. static inline void
  5889. btrfs_release_block_group(struct btrfs_block_group_cache *cache,
  5890. int delalloc)
  5891. {
  5892. if (delalloc)
  5893. up_read(&cache->data_rwsem);
  5894. btrfs_put_block_group(cache);
  5895. }
  5896. /*
  5897. * walks the btree of allocated extents and find a hole of a given size.
  5898. * The key ins is changed to record the hole:
  5899. * ins->objectid == start position
  5900. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5901. * ins->offset == the size of the hole.
  5902. * Any available blocks before search_start are skipped.
  5903. *
  5904. * If there is no suitable free space, we will record the max size of
  5905. * the free space extent currently.
  5906. */
  5907. static noinline int find_free_extent(struct btrfs_root *orig_root,
  5908. u64 num_bytes, u64 empty_size,
  5909. u64 hint_byte, struct btrfs_key *ins,
  5910. u64 flags, int delalloc)
  5911. {
  5912. int ret = 0;
  5913. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5914. struct btrfs_free_cluster *last_ptr = NULL;
  5915. struct btrfs_block_group_cache *block_group = NULL;
  5916. u64 search_start = 0;
  5917. u64 max_extent_size = 0;
  5918. int empty_cluster = 2 * 1024 * 1024;
  5919. struct btrfs_space_info *space_info;
  5920. int loop = 0;
  5921. int index = __get_raid_index(flags);
  5922. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5923. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5924. bool failed_cluster_refill = false;
  5925. bool failed_alloc = false;
  5926. bool use_cluster = true;
  5927. bool have_caching_bg = false;
  5928. WARN_ON(num_bytes < root->sectorsize);
  5929. ins->type = BTRFS_EXTENT_ITEM_KEY;
  5930. ins->objectid = 0;
  5931. ins->offset = 0;
  5932. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5933. space_info = __find_space_info(root->fs_info, flags);
  5934. if (!space_info) {
  5935. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5936. return -ENOSPC;
  5937. }
  5938. /*
  5939. * If the space info is for both data and metadata it means we have a
  5940. * small filesystem and we can't use the clustering stuff.
  5941. */
  5942. if (btrfs_mixed_space_info(space_info))
  5943. use_cluster = false;
  5944. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5945. last_ptr = &root->fs_info->meta_alloc_cluster;
  5946. if (!btrfs_test_opt(root, SSD))
  5947. empty_cluster = 64 * 1024;
  5948. }
  5949. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5950. btrfs_test_opt(root, SSD)) {
  5951. last_ptr = &root->fs_info->data_alloc_cluster;
  5952. }
  5953. if (last_ptr) {
  5954. spin_lock(&last_ptr->lock);
  5955. if (last_ptr->block_group)
  5956. hint_byte = last_ptr->window_start;
  5957. spin_unlock(&last_ptr->lock);
  5958. }
  5959. search_start = max(search_start, first_logical_byte(root, 0));
  5960. search_start = max(search_start, hint_byte);
  5961. if (!last_ptr)
  5962. empty_cluster = 0;
  5963. if (search_start == hint_byte) {
  5964. block_group = btrfs_lookup_block_group(root->fs_info,
  5965. search_start);
  5966. /*
  5967. * we don't want to use the block group if it doesn't match our
  5968. * allocation bits, or if its not cached.
  5969. *
  5970. * However if we are re-searching with an ideal block group
  5971. * picked out then we don't care that the block group is cached.
  5972. */
  5973. if (block_group && block_group_bits(block_group, flags) &&
  5974. block_group->cached != BTRFS_CACHE_NO) {
  5975. down_read(&space_info->groups_sem);
  5976. if (list_empty(&block_group->list) ||
  5977. block_group->ro) {
  5978. /*
  5979. * someone is removing this block group,
  5980. * we can't jump into the have_block_group
  5981. * target because our list pointers are not
  5982. * valid
  5983. */
  5984. btrfs_put_block_group(block_group);
  5985. up_read(&space_info->groups_sem);
  5986. } else {
  5987. index = get_block_group_index(block_group);
  5988. btrfs_lock_block_group(block_group, delalloc);
  5989. goto have_block_group;
  5990. }
  5991. } else if (block_group) {
  5992. btrfs_put_block_group(block_group);
  5993. }
  5994. }
  5995. search:
  5996. have_caching_bg = false;
  5997. down_read(&space_info->groups_sem);
  5998. list_for_each_entry(block_group, &space_info->block_groups[index],
  5999. list) {
  6000. u64 offset;
  6001. int cached;
  6002. btrfs_grab_block_group(block_group, delalloc);
  6003. search_start = block_group->key.objectid;
  6004. /*
  6005. * this can happen if we end up cycling through all the
  6006. * raid types, but we want to make sure we only allocate
  6007. * for the proper type.
  6008. */
  6009. if (!block_group_bits(block_group, flags)) {
  6010. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  6011. BTRFS_BLOCK_GROUP_RAID1 |
  6012. BTRFS_BLOCK_GROUP_RAID5 |
  6013. BTRFS_BLOCK_GROUP_RAID6 |
  6014. BTRFS_BLOCK_GROUP_RAID10;
  6015. /*
  6016. * if they asked for extra copies and this block group
  6017. * doesn't provide them, bail. This does allow us to
  6018. * fill raid0 from raid1.
  6019. */
  6020. if ((flags & extra) && !(block_group->flags & extra))
  6021. goto loop;
  6022. }
  6023. have_block_group:
  6024. cached = block_group_cache_done(block_group);
  6025. if (unlikely(!cached)) {
  6026. ret = cache_block_group(block_group, 0);
  6027. BUG_ON(ret < 0);
  6028. ret = 0;
  6029. }
  6030. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
  6031. goto loop;
  6032. if (unlikely(block_group->ro))
  6033. goto loop;
  6034. /*
  6035. * Ok we want to try and use the cluster allocator, so
  6036. * lets look there
  6037. */
  6038. if (last_ptr) {
  6039. struct btrfs_block_group_cache *used_block_group;
  6040. unsigned long aligned_cluster;
  6041. /*
  6042. * the refill lock keeps out other
  6043. * people trying to start a new cluster
  6044. */
  6045. used_block_group = btrfs_lock_cluster(block_group,
  6046. last_ptr,
  6047. delalloc);
  6048. if (!used_block_group)
  6049. goto refill_cluster;
  6050. if (used_block_group != block_group &&
  6051. (used_block_group->ro ||
  6052. !block_group_bits(used_block_group, flags)))
  6053. goto release_cluster;
  6054. offset = btrfs_alloc_from_cluster(used_block_group,
  6055. last_ptr,
  6056. num_bytes,
  6057. used_block_group->key.objectid,
  6058. &max_extent_size);
  6059. if (offset) {
  6060. /* we have a block, we're done */
  6061. spin_unlock(&last_ptr->refill_lock);
  6062. trace_btrfs_reserve_extent_cluster(root,
  6063. used_block_group,
  6064. search_start, num_bytes);
  6065. if (used_block_group != block_group) {
  6066. btrfs_release_block_group(block_group,
  6067. delalloc);
  6068. block_group = used_block_group;
  6069. }
  6070. goto checks;
  6071. }
  6072. WARN_ON(last_ptr->block_group != used_block_group);
  6073. release_cluster:
  6074. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  6075. * set up a new clusters, so lets just skip it
  6076. * and let the allocator find whatever block
  6077. * it can find. If we reach this point, we
  6078. * will have tried the cluster allocator
  6079. * plenty of times and not have found
  6080. * anything, so we are likely way too
  6081. * fragmented for the clustering stuff to find
  6082. * anything.
  6083. *
  6084. * However, if the cluster is taken from the
  6085. * current block group, release the cluster
  6086. * first, so that we stand a better chance of
  6087. * succeeding in the unclustered
  6088. * allocation. */
  6089. if (loop >= LOOP_NO_EMPTY_SIZE &&
  6090. used_block_group != block_group) {
  6091. spin_unlock(&last_ptr->refill_lock);
  6092. btrfs_release_block_group(used_block_group,
  6093. delalloc);
  6094. goto unclustered_alloc;
  6095. }
  6096. /*
  6097. * this cluster didn't work out, free it and
  6098. * start over
  6099. */
  6100. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  6101. if (used_block_group != block_group)
  6102. btrfs_release_block_group(used_block_group,
  6103. delalloc);
  6104. refill_cluster:
  6105. if (loop >= LOOP_NO_EMPTY_SIZE) {
  6106. spin_unlock(&last_ptr->refill_lock);
  6107. goto unclustered_alloc;
  6108. }
  6109. aligned_cluster = max_t(unsigned long,
  6110. empty_cluster + empty_size,
  6111. block_group->full_stripe_len);
  6112. /* allocate a cluster in this block group */
  6113. ret = btrfs_find_space_cluster(root, block_group,
  6114. last_ptr, search_start,
  6115. num_bytes,
  6116. aligned_cluster);
  6117. if (ret == 0) {
  6118. /*
  6119. * now pull our allocation out of this
  6120. * cluster
  6121. */
  6122. offset = btrfs_alloc_from_cluster(block_group,
  6123. last_ptr,
  6124. num_bytes,
  6125. search_start,
  6126. &max_extent_size);
  6127. if (offset) {
  6128. /* we found one, proceed */
  6129. spin_unlock(&last_ptr->refill_lock);
  6130. trace_btrfs_reserve_extent_cluster(root,
  6131. block_group, search_start,
  6132. num_bytes);
  6133. goto checks;
  6134. }
  6135. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  6136. && !failed_cluster_refill) {
  6137. spin_unlock(&last_ptr->refill_lock);
  6138. failed_cluster_refill = true;
  6139. wait_block_group_cache_progress(block_group,
  6140. num_bytes + empty_cluster + empty_size);
  6141. goto have_block_group;
  6142. }
  6143. /*
  6144. * at this point we either didn't find a cluster
  6145. * or we weren't able to allocate a block from our
  6146. * cluster. Free the cluster we've been trying
  6147. * to use, and go to the next block group
  6148. */
  6149. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  6150. spin_unlock(&last_ptr->refill_lock);
  6151. goto loop;
  6152. }
  6153. unclustered_alloc:
  6154. spin_lock(&block_group->free_space_ctl->tree_lock);
  6155. if (cached &&
  6156. block_group->free_space_ctl->free_space <
  6157. num_bytes + empty_cluster + empty_size) {
  6158. if (block_group->free_space_ctl->free_space >
  6159. max_extent_size)
  6160. max_extent_size =
  6161. block_group->free_space_ctl->free_space;
  6162. spin_unlock(&block_group->free_space_ctl->tree_lock);
  6163. goto loop;
  6164. }
  6165. spin_unlock(&block_group->free_space_ctl->tree_lock);
  6166. offset = btrfs_find_space_for_alloc(block_group, search_start,
  6167. num_bytes, empty_size,
  6168. &max_extent_size);
  6169. /*
  6170. * If we didn't find a chunk, and we haven't failed on this
  6171. * block group before, and this block group is in the middle of
  6172. * caching and we are ok with waiting, then go ahead and wait
  6173. * for progress to be made, and set failed_alloc to true.
  6174. *
  6175. * If failed_alloc is true then we've already waited on this
  6176. * block group once and should move on to the next block group.
  6177. */
  6178. if (!offset && !failed_alloc && !cached &&
  6179. loop > LOOP_CACHING_NOWAIT) {
  6180. wait_block_group_cache_progress(block_group,
  6181. num_bytes + empty_size);
  6182. failed_alloc = true;
  6183. goto have_block_group;
  6184. } else if (!offset) {
  6185. if (!cached)
  6186. have_caching_bg = true;
  6187. goto loop;
  6188. }
  6189. checks:
  6190. search_start = ALIGN(offset, root->stripesize);
  6191. /* move on to the next group */
  6192. if (search_start + num_bytes >
  6193. block_group->key.objectid + block_group->key.offset) {
  6194. btrfs_add_free_space(block_group, offset, num_bytes);
  6195. goto loop;
  6196. }
  6197. if (offset < search_start)
  6198. btrfs_add_free_space(block_group, offset,
  6199. search_start - offset);
  6200. BUG_ON(offset > search_start);
  6201. ret = btrfs_update_reserved_bytes(block_group, num_bytes,
  6202. alloc_type, delalloc);
  6203. if (ret == -EAGAIN) {
  6204. btrfs_add_free_space(block_group, offset, num_bytes);
  6205. goto loop;
  6206. }
  6207. /* we are all good, lets return */
  6208. ins->objectid = search_start;
  6209. ins->offset = num_bytes;
  6210. trace_btrfs_reserve_extent(orig_root, block_group,
  6211. search_start, num_bytes);
  6212. btrfs_release_block_group(block_group, delalloc);
  6213. break;
  6214. loop:
  6215. failed_cluster_refill = false;
  6216. failed_alloc = false;
  6217. BUG_ON(index != get_block_group_index(block_group));
  6218. btrfs_release_block_group(block_group, delalloc);
  6219. }
  6220. up_read(&space_info->groups_sem);
  6221. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  6222. goto search;
  6223. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  6224. goto search;
  6225. /*
  6226. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  6227. * caching kthreads as we move along
  6228. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  6229. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  6230. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  6231. * again
  6232. */
  6233. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  6234. index = 0;
  6235. loop++;
  6236. if (loop == LOOP_ALLOC_CHUNK) {
  6237. struct btrfs_trans_handle *trans;
  6238. int exist = 0;
  6239. trans = current->journal_info;
  6240. if (trans)
  6241. exist = 1;
  6242. else
  6243. trans = btrfs_join_transaction(root);
  6244. if (IS_ERR(trans)) {
  6245. ret = PTR_ERR(trans);
  6246. goto out;
  6247. }
  6248. ret = do_chunk_alloc(trans, root, flags,
  6249. CHUNK_ALLOC_FORCE);
  6250. /*
  6251. * Do not bail out on ENOSPC since we
  6252. * can do more things.
  6253. */
  6254. if (ret < 0 && ret != -ENOSPC)
  6255. btrfs_abort_transaction(trans,
  6256. root, ret);
  6257. else
  6258. ret = 0;
  6259. if (!exist)
  6260. btrfs_end_transaction(trans, root);
  6261. if (ret)
  6262. goto out;
  6263. }
  6264. if (loop == LOOP_NO_EMPTY_SIZE) {
  6265. empty_size = 0;
  6266. empty_cluster = 0;
  6267. }
  6268. goto search;
  6269. } else if (!ins->objectid) {
  6270. ret = -ENOSPC;
  6271. } else if (ins->objectid) {
  6272. ret = 0;
  6273. }
  6274. out:
  6275. if (ret == -ENOSPC)
  6276. ins->offset = max_extent_size;
  6277. return ret;
  6278. }
  6279. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  6280. int dump_block_groups)
  6281. {
  6282. struct btrfs_block_group_cache *cache;
  6283. int index = 0;
  6284. spin_lock(&info->lock);
  6285. printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
  6286. info->flags,
  6287. info->total_bytes - info->bytes_used - info->bytes_pinned -
  6288. info->bytes_reserved - info->bytes_readonly,
  6289. (info->full) ? "" : "not ");
  6290. printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
  6291. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  6292. info->total_bytes, info->bytes_used, info->bytes_pinned,
  6293. info->bytes_reserved, info->bytes_may_use,
  6294. info->bytes_readonly);
  6295. spin_unlock(&info->lock);
  6296. if (!dump_block_groups)
  6297. return;
  6298. down_read(&info->groups_sem);
  6299. again:
  6300. list_for_each_entry(cache, &info->block_groups[index], list) {
  6301. spin_lock(&cache->lock);
  6302. printk(KERN_INFO "BTRFS: "
  6303. "block group %llu has %llu bytes, "
  6304. "%llu used %llu pinned %llu reserved %s\n",
  6305. cache->key.objectid, cache->key.offset,
  6306. btrfs_block_group_used(&cache->item), cache->pinned,
  6307. cache->reserved, cache->ro ? "[readonly]" : "");
  6308. btrfs_dump_free_space(cache, bytes);
  6309. spin_unlock(&cache->lock);
  6310. }
  6311. if (++index < BTRFS_NR_RAID_TYPES)
  6312. goto again;
  6313. up_read(&info->groups_sem);
  6314. }
  6315. int btrfs_reserve_extent(struct btrfs_root *root,
  6316. u64 num_bytes, u64 min_alloc_size,
  6317. u64 empty_size, u64 hint_byte,
  6318. struct btrfs_key *ins, int is_data, int delalloc)
  6319. {
  6320. bool final_tried = false;
  6321. u64 flags;
  6322. int ret;
  6323. flags = btrfs_get_alloc_profile(root, is_data);
  6324. again:
  6325. WARN_ON(num_bytes < root->sectorsize);
  6326. ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
  6327. flags, delalloc);
  6328. if (ret == -ENOSPC) {
  6329. if (!final_tried && ins->offset) {
  6330. num_bytes = min(num_bytes >> 1, ins->offset);
  6331. num_bytes = round_down(num_bytes, root->sectorsize);
  6332. num_bytes = max(num_bytes, min_alloc_size);
  6333. if (num_bytes == min_alloc_size)
  6334. final_tried = true;
  6335. goto again;
  6336. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6337. struct btrfs_space_info *sinfo;
  6338. sinfo = __find_space_info(root->fs_info, flags);
  6339. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  6340. flags, num_bytes);
  6341. if (sinfo)
  6342. dump_space_info(sinfo, num_bytes, 1);
  6343. }
  6344. }
  6345. return ret;
  6346. }
  6347. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  6348. u64 start, u64 len,
  6349. int pin, int delalloc)
  6350. {
  6351. struct btrfs_block_group_cache *cache;
  6352. int ret = 0;
  6353. cache = btrfs_lookup_block_group(root->fs_info, start);
  6354. if (!cache) {
  6355. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  6356. start);
  6357. return -ENOSPC;
  6358. }
  6359. if (pin)
  6360. pin_down_extent(root, cache, start, len, 1);
  6361. else {
  6362. if (btrfs_test_opt(root, DISCARD))
  6363. ret = btrfs_discard_extent(root, start, len, NULL);
  6364. btrfs_add_free_space(cache, start, len);
  6365. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
  6366. }
  6367. btrfs_put_block_group(cache);
  6368. trace_btrfs_reserved_extent_free(root, start, len);
  6369. return ret;
  6370. }
  6371. int btrfs_free_reserved_extent(struct btrfs_root *root,
  6372. u64 start, u64 len, int delalloc)
  6373. {
  6374. return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
  6375. }
  6376. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  6377. u64 start, u64 len)
  6378. {
  6379. return __btrfs_free_reserved_extent(root, start, len, 1, 0);
  6380. }
  6381. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  6382. struct btrfs_root *root,
  6383. u64 parent, u64 root_objectid,
  6384. u64 flags, u64 owner, u64 offset,
  6385. struct btrfs_key *ins, int ref_mod)
  6386. {
  6387. int ret;
  6388. struct btrfs_fs_info *fs_info = root->fs_info;
  6389. struct btrfs_extent_item *extent_item;
  6390. struct btrfs_extent_inline_ref *iref;
  6391. struct btrfs_path *path;
  6392. struct extent_buffer *leaf;
  6393. int type;
  6394. u32 size;
  6395. if (parent > 0)
  6396. type = BTRFS_SHARED_DATA_REF_KEY;
  6397. else
  6398. type = BTRFS_EXTENT_DATA_REF_KEY;
  6399. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  6400. path = btrfs_alloc_path();
  6401. if (!path)
  6402. return -ENOMEM;
  6403. path->leave_spinning = 1;
  6404. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  6405. ins, size);
  6406. if (ret) {
  6407. btrfs_free_path(path);
  6408. return ret;
  6409. }
  6410. leaf = path->nodes[0];
  6411. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  6412. struct btrfs_extent_item);
  6413. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  6414. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  6415. btrfs_set_extent_flags(leaf, extent_item,
  6416. flags | BTRFS_EXTENT_FLAG_DATA);
  6417. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  6418. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  6419. if (parent > 0) {
  6420. struct btrfs_shared_data_ref *ref;
  6421. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  6422. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  6423. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  6424. } else {
  6425. struct btrfs_extent_data_ref *ref;
  6426. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  6427. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  6428. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  6429. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  6430. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  6431. }
  6432. btrfs_mark_buffer_dirty(path->nodes[0]);
  6433. btrfs_free_path(path);
  6434. /* Always set parent to 0 here since its exclusive anyway. */
  6435. ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
  6436. ins->objectid, ins->offset,
  6437. BTRFS_QGROUP_OPER_ADD_EXCL, 0);
  6438. if (ret)
  6439. return ret;
  6440. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  6441. if (ret) { /* -ENOENT, logic error */
  6442. btrfs_err(fs_info, "update block group failed for %llu %llu",
  6443. ins->objectid, ins->offset);
  6444. BUG();
  6445. }
  6446. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  6447. return ret;
  6448. }
  6449. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  6450. struct btrfs_root *root,
  6451. u64 parent, u64 root_objectid,
  6452. u64 flags, struct btrfs_disk_key *key,
  6453. int level, struct btrfs_key *ins,
  6454. int no_quota)
  6455. {
  6456. int ret;
  6457. struct btrfs_fs_info *fs_info = root->fs_info;
  6458. struct btrfs_extent_item *extent_item;
  6459. struct btrfs_tree_block_info *block_info;
  6460. struct btrfs_extent_inline_ref *iref;
  6461. struct btrfs_path *path;
  6462. struct extent_buffer *leaf;
  6463. u32 size = sizeof(*extent_item) + sizeof(*iref);
  6464. u64 num_bytes = ins->offset;
  6465. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6466. SKINNY_METADATA);
  6467. if (!skinny_metadata)
  6468. size += sizeof(*block_info);
  6469. path = btrfs_alloc_path();
  6470. if (!path) {
  6471. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  6472. root->nodesize);
  6473. return -ENOMEM;
  6474. }
  6475. path->leave_spinning = 1;
  6476. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  6477. ins, size);
  6478. if (ret) {
  6479. btrfs_free_path(path);
  6480. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  6481. root->nodesize);
  6482. return ret;
  6483. }
  6484. leaf = path->nodes[0];
  6485. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  6486. struct btrfs_extent_item);
  6487. btrfs_set_extent_refs(leaf, extent_item, 1);
  6488. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  6489. btrfs_set_extent_flags(leaf, extent_item,
  6490. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  6491. if (skinny_metadata) {
  6492. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  6493. num_bytes = root->nodesize;
  6494. } else {
  6495. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  6496. btrfs_set_tree_block_key(leaf, block_info, key);
  6497. btrfs_set_tree_block_level(leaf, block_info, level);
  6498. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  6499. }
  6500. if (parent > 0) {
  6501. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  6502. btrfs_set_extent_inline_ref_type(leaf, iref,
  6503. BTRFS_SHARED_BLOCK_REF_KEY);
  6504. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  6505. } else {
  6506. btrfs_set_extent_inline_ref_type(leaf, iref,
  6507. BTRFS_TREE_BLOCK_REF_KEY);
  6508. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  6509. }
  6510. btrfs_mark_buffer_dirty(leaf);
  6511. btrfs_free_path(path);
  6512. if (!no_quota) {
  6513. ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
  6514. ins->objectid, num_bytes,
  6515. BTRFS_QGROUP_OPER_ADD_EXCL, 0);
  6516. if (ret)
  6517. return ret;
  6518. }
  6519. ret = update_block_group(trans, root, ins->objectid, root->nodesize,
  6520. 1);
  6521. if (ret) { /* -ENOENT, logic error */
  6522. btrfs_err(fs_info, "update block group failed for %llu %llu",
  6523. ins->objectid, ins->offset);
  6524. BUG();
  6525. }
  6526. trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->nodesize);
  6527. return ret;
  6528. }
  6529. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  6530. struct btrfs_root *root,
  6531. u64 root_objectid, u64 owner,
  6532. u64 offset, struct btrfs_key *ins)
  6533. {
  6534. int ret;
  6535. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  6536. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  6537. ins->offset, 0,
  6538. root_objectid, owner, offset,
  6539. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  6540. return ret;
  6541. }
  6542. /*
  6543. * this is used by the tree logging recovery code. It records that
  6544. * an extent has been allocated and makes sure to clear the free
  6545. * space cache bits as well
  6546. */
  6547. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  6548. struct btrfs_root *root,
  6549. u64 root_objectid, u64 owner, u64 offset,
  6550. struct btrfs_key *ins)
  6551. {
  6552. int ret;
  6553. struct btrfs_block_group_cache *block_group;
  6554. /*
  6555. * Mixed block groups will exclude before processing the log so we only
  6556. * need to do the exlude dance if this fs isn't mixed.
  6557. */
  6558. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  6559. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  6560. if (ret)
  6561. return ret;
  6562. }
  6563. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  6564. if (!block_group)
  6565. return -EINVAL;
  6566. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  6567. RESERVE_ALLOC_NO_ACCOUNT, 0);
  6568. BUG_ON(ret); /* logic error */
  6569. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  6570. 0, owner, offset, ins, 1);
  6571. btrfs_put_block_group(block_group);
  6572. return ret;
  6573. }
  6574. static struct extent_buffer *
  6575. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  6576. u64 bytenr, int level)
  6577. {
  6578. struct extent_buffer *buf;
  6579. buf = btrfs_find_create_tree_block(root, bytenr);
  6580. if (!buf)
  6581. return ERR_PTR(-ENOMEM);
  6582. btrfs_set_header_generation(buf, trans->transid);
  6583. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  6584. btrfs_tree_lock(buf);
  6585. clean_tree_block(trans, root->fs_info, buf);
  6586. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  6587. btrfs_set_lock_blocking(buf);
  6588. btrfs_set_buffer_uptodate(buf);
  6589. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  6590. buf->log_index = root->log_transid % 2;
  6591. /*
  6592. * we allow two log transactions at a time, use different
  6593. * EXENT bit to differentiate dirty pages.
  6594. */
  6595. if (buf->log_index == 0)
  6596. set_extent_dirty(&root->dirty_log_pages, buf->start,
  6597. buf->start + buf->len - 1, GFP_NOFS);
  6598. else
  6599. set_extent_new(&root->dirty_log_pages, buf->start,
  6600. buf->start + buf->len - 1, GFP_NOFS);
  6601. } else {
  6602. buf->log_index = -1;
  6603. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  6604. buf->start + buf->len - 1, GFP_NOFS);
  6605. }
  6606. trans->blocks_used++;
  6607. /* this returns a buffer locked for blocking */
  6608. return buf;
  6609. }
  6610. static struct btrfs_block_rsv *
  6611. use_block_rsv(struct btrfs_trans_handle *trans,
  6612. struct btrfs_root *root, u32 blocksize)
  6613. {
  6614. struct btrfs_block_rsv *block_rsv;
  6615. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  6616. int ret;
  6617. bool global_updated = false;
  6618. block_rsv = get_block_rsv(trans, root);
  6619. if (unlikely(block_rsv->size == 0))
  6620. goto try_reserve;
  6621. again:
  6622. ret = block_rsv_use_bytes(block_rsv, blocksize);
  6623. if (!ret)
  6624. return block_rsv;
  6625. if (block_rsv->failfast)
  6626. return ERR_PTR(ret);
  6627. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  6628. global_updated = true;
  6629. update_global_block_rsv(root->fs_info);
  6630. goto again;
  6631. }
  6632. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6633. static DEFINE_RATELIMIT_STATE(_rs,
  6634. DEFAULT_RATELIMIT_INTERVAL * 10,
  6635. /*DEFAULT_RATELIMIT_BURST*/ 1);
  6636. if (__ratelimit(&_rs))
  6637. WARN(1, KERN_DEBUG
  6638. "BTRFS: block rsv returned %d\n", ret);
  6639. }
  6640. try_reserve:
  6641. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  6642. BTRFS_RESERVE_NO_FLUSH);
  6643. if (!ret)
  6644. return block_rsv;
  6645. /*
  6646. * If we couldn't reserve metadata bytes try and use some from
  6647. * the global reserve if its space type is the same as the global
  6648. * reservation.
  6649. */
  6650. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  6651. block_rsv->space_info == global_rsv->space_info) {
  6652. ret = block_rsv_use_bytes(global_rsv, blocksize);
  6653. if (!ret)
  6654. return global_rsv;
  6655. }
  6656. return ERR_PTR(ret);
  6657. }
  6658. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6659. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6660. {
  6661. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6662. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6663. }
  6664. /*
  6665. * finds a free extent and does all the dirty work required for allocation
  6666. * returns the key for the extent through ins, and a tree buffer for
  6667. * the first block of the extent through buf.
  6668. *
  6669. * returns the tree buffer or an ERR_PTR on error.
  6670. */
  6671. struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
  6672. struct btrfs_root *root,
  6673. u64 parent, u64 root_objectid,
  6674. struct btrfs_disk_key *key, int level,
  6675. u64 hint, u64 empty_size)
  6676. {
  6677. struct btrfs_key ins;
  6678. struct btrfs_block_rsv *block_rsv;
  6679. struct extent_buffer *buf;
  6680. struct btrfs_delayed_extent_op *extent_op;
  6681. u64 flags = 0;
  6682. int ret;
  6683. u32 blocksize = root->nodesize;
  6684. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6685. SKINNY_METADATA);
  6686. if (btrfs_test_is_dummy_root(root)) {
  6687. buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
  6688. level);
  6689. if (!IS_ERR(buf))
  6690. root->alloc_bytenr += blocksize;
  6691. return buf;
  6692. }
  6693. block_rsv = use_block_rsv(trans, root, blocksize);
  6694. if (IS_ERR(block_rsv))
  6695. return ERR_CAST(block_rsv);
  6696. ret = btrfs_reserve_extent(root, blocksize, blocksize,
  6697. empty_size, hint, &ins, 0, 0);
  6698. if (ret)
  6699. goto out_unuse;
  6700. buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
  6701. if (IS_ERR(buf)) {
  6702. ret = PTR_ERR(buf);
  6703. goto out_free_reserved;
  6704. }
  6705. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6706. if (parent == 0)
  6707. parent = ins.objectid;
  6708. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6709. } else
  6710. BUG_ON(parent > 0);
  6711. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6712. extent_op = btrfs_alloc_delayed_extent_op();
  6713. if (!extent_op) {
  6714. ret = -ENOMEM;
  6715. goto out_free_buf;
  6716. }
  6717. if (key)
  6718. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6719. else
  6720. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6721. extent_op->flags_to_set = flags;
  6722. if (skinny_metadata)
  6723. extent_op->update_key = 0;
  6724. else
  6725. extent_op->update_key = 1;
  6726. extent_op->update_flags = 1;
  6727. extent_op->is_data = 0;
  6728. extent_op->level = level;
  6729. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6730. ins.objectid, ins.offset,
  6731. parent, root_objectid, level,
  6732. BTRFS_ADD_DELAYED_EXTENT,
  6733. extent_op, 0);
  6734. if (ret)
  6735. goto out_free_delayed;
  6736. }
  6737. return buf;
  6738. out_free_delayed:
  6739. btrfs_free_delayed_extent_op(extent_op);
  6740. out_free_buf:
  6741. free_extent_buffer(buf);
  6742. out_free_reserved:
  6743. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0);
  6744. out_unuse:
  6745. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6746. return ERR_PTR(ret);
  6747. }
  6748. struct walk_control {
  6749. u64 refs[BTRFS_MAX_LEVEL];
  6750. u64 flags[BTRFS_MAX_LEVEL];
  6751. struct btrfs_key update_progress;
  6752. int stage;
  6753. int level;
  6754. int shared_level;
  6755. int update_ref;
  6756. int keep_locks;
  6757. int reada_slot;
  6758. int reada_count;
  6759. int for_reloc;
  6760. };
  6761. #define DROP_REFERENCE 1
  6762. #define UPDATE_BACKREF 2
  6763. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6764. struct btrfs_root *root,
  6765. struct walk_control *wc,
  6766. struct btrfs_path *path)
  6767. {
  6768. u64 bytenr;
  6769. u64 generation;
  6770. u64 refs;
  6771. u64 flags;
  6772. u32 nritems;
  6773. u32 blocksize;
  6774. struct btrfs_key key;
  6775. struct extent_buffer *eb;
  6776. int ret;
  6777. int slot;
  6778. int nread = 0;
  6779. if (path->slots[wc->level] < wc->reada_slot) {
  6780. wc->reada_count = wc->reada_count * 2 / 3;
  6781. wc->reada_count = max(wc->reada_count, 2);
  6782. } else {
  6783. wc->reada_count = wc->reada_count * 3 / 2;
  6784. wc->reada_count = min_t(int, wc->reada_count,
  6785. BTRFS_NODEPTRS_PER_BLOCK(root));
  6786. }
  6787. eb = path->nodes[wc->level];
  6788. nritems = btrfs_header_nritems(eb);
  6789. blocksize = root->nodesize;
  6790. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6791. if (nread >= wc->reada_count)
  6792. break;
  6793. cond_resched();
  6794. bytenr = btrfs_node_blockptr(eb, slot);
  6795. generation = btrfs_node_ptr_generation(eb, slot);
  6796. if (slot == path->slots[wc->level])
  6797. goto reada;
  6798. if (wc->stage == UPDATE_BACKREF &&
  6799. generation <= root->root_key.offset)
  6800. continue;
  6801. /* We don't lock the tree block, it's OK to be racy here */
  6802. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6803. wc->level - 1, 1, &refs,
  6804. &flags);
  6805. /* We don't care about errors in readahead. */
  6806. if (ret < 0)
  6807. continue;
  6808. BUG_ON(refs == 0);
  6809. if (wc->stage == DROP_REFERENCE) {
  6810. if (refs == 1)
  6811. goto reada;
  6812. if (wc->level == 1 &&
  6813. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6814. continue;
  6815. if (!wc->update_ref ||
  6816. generation <= root->root_key.offset)
  6817. continue;
  6818. btrfs_node_key_to_cpu(eb, &key, slot);
  6819. ret = btrfs_comp_cpu_keys(&key,
  6820. &wc->update_progress);
  6821. if (ret < 0)
  6822. continue;
  6823. } else {
  6824. if (wc->level == 1 &&
  6825. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6826. continue;
  6827. }
  6828. reada:
  6829. readahead_tree_block(root, bytenr);
  6830. nread++;
  6831. }
  6832. wc->reada_slot = slot;
  6833. }
  6834. static int account_leaf_items(struct btrfs_trans_handle *trans,
  6835. struct btrfs_root *root,
  6836. struct extent_buffer *eb)
  6837. {
  6838. int nr = btrfs_header_nritems(eb);
  6839. int i, extent_type, ret;
  6840. struct btrfs_key key;
  6841. struct btrfs_file_extent_item *fi;
  6842. u64 bytenr, num_bytes;
  6843. for (i = 0; i < nr; i++) {
  6844. btrfs_item_key_to_cpu(eb, &key, i);
  6845. if (key.type != BTRFS_EXTENT_DATA_KEY)
  6846. continue;
  6847. fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  6848. /* filter out non qgroup-accountable extents */
  6849. extent_type = btrfs_file_extent_type(eb, fi);
  6850. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  6851. continue;
  6852. bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
  6853. if (!bytenr)
  6854. continue;
  6855. num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
  6856. ret = btrfs_qgroup_record_ref(trans, root->fs_info,
  6857. root->objectid,
  6858. bytenr, num_bytes,
  6859. BTRFS_QGROUP_OPER_SUB_SUBTREE, 0);
  6860. if (ret)
  6861. return ret;
  6862. }
  6863. return 0;
  6864. }
  6865. /*
  6866. * Walk up the tree from the bottom, freeing leaves and any interior
  6867. * nodes which have had all slots visited. If a node (leaf or
  6868. * interior) is freed, the node above it will have it's slot
  6869. * incremented. The root node will never be freed.
  6870. *
  6871. * At the end of this function, we should have a path which has all
  6872. * slots incremented to the next position for a search. If we need to
  6873. * read a new node it will be NULL and the node above it will have the
  6874. * correct slot selected for a later read.
  6875. *
  6876. * If we increment the root nodes slot counter past the number of
  6877. * elements, 1 is returned to signal completion of the search.
  6878. */
  6879. static int adjust_slots_upwards(struct btrfs_root *root,
  6880. struct btrfs_path *path, int root_level)
  6881. {
  6882. int level = 0;
  6883. int nr, slot;
  6884. struct extent_buffer *eb;
  6885. if (root_level == 0)
  6886. return 1;
  6887. while (level <= root_level) {
  6888. eb = path->nodes[level];
  6889. nr = btrfs_header_nritems(eb);
  6890. path->slots[level]++;
  6891. slot = path->slots[level];
  6892. if (slot >= nr || level == 0) {
  6893. /*
  6894. * Don't free the root - we will detect this
  6895. * condition after our loop and return a
  6896. * positive value for caller to stop walking the tree.
  6897. */
  6898. if (level != root_level) {
  6899. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6900. path->locks[level] = 0;
  6901. free_extent_buffer(eb);
  6902. path->nodes[level] = NULL;
  6903. path->slots[level] = 0;
  6904. }
  6905. } else {
  6906. /*
  6907. * We have a valid slot to walk back down
  6908. * from. Stop here so caller can process these
  6909. * new nodes.
  6910. */
  6911. break;
  6912. }
  6913. level++;
  6914. }
  6915. eb = path->nodes[root_level];
  6916. if (path->slots[root_level] >= btrfs_header_nritems(eb))
  6917. return 1;
  6918. return 0;
  6919. }
  6920. /*
  6921. * root_eb is the subtree root and is locked before this function is called.
  6922. */
  6923. static int account_shared_subtree(struct btrfs_trans_handle *trans,
  6924. struct btrfs_root *root,
  6925. struct extent_buffer *root_eb,
  6926. u64 root_gen,
  6927. int root_level)
  6928. {
  6929. int ret = 0;
  6930. int level;
  6931. struct extent_buffer *eb = root_eb;
  6932. struct btrfs_path *path = NULL;
  6933. BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
  6934. BUG_ON(root_eb == NULL);
  6935. if (!root->fs_info->quota_enabled)
  6936. return 0;
  6937. if (!extent_buffer_uptodate(root_eb)) {
  6938. ret = btrfs_read_buffer(root_eb, root_gen);
  6939. if (ret)
  6940. goto out;
  6941. }
  6942. if (root_level == 0) {
  6943. ret = account_leaf_items(trans, root, root_eb);
  6944. goto out;
  6945. }
  6946. path = btrfs_alloc_path();
  6947. if (!path)
  6948. return -ENOMEM;
  6949. /*
  6950. * Walk down the tree. Missing extent blocks are filled in as
  6951. * we go. Metadata is accounted every time we read a new
  6952. * extent block.
  6953. *
  6954. * When we reach a leaf, we account for file extent items in it,
  6955. * walk back up the tree (adjusting slot pointers as we go)
  6956. * and restart the search process.
  6957. */
  6958. extent_buffer_get(root_eb); /* For path */
  6959. path->nodes[root_level] = root_eb;
  6960. path->slots[root_level] = 0;
  6961. path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
  6962. walk_down:
  6963. level = root_level;
  6964. while (level >= 0) {
  6965. if (path->nodes[level] == NULL) {
  6966. int parent_slot;
  6967. u64 child_gen;
  6968. u64 child_bytenr;
  6969. /* We need to get child blockptr/gen from
  6970. * parent before we can read it. */
  6971. eb = path->nodes[level + 1];
  6972. parent_slot = path->slots[level + 1];
  6973. child_bytenr = btrfs_node_blockptr(eb, parent_slot);
  6974. child_gen = btrfs_node_ptr_generation(eb, parent_slot);
  6975. eb = read_tree_block(root, child_bytenr, child_gen);
  6976. if (!eb || !extent_buffer_uptodate(eb)) {
  6977. ret = -EIO;
  6978. goto out;
  6979. }
  6980. path->nodes[level] = eb;
  6981. path->slots[level] = 0;
  6982. btrfs_tree_read_lock(eb);
  6983. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  6984. path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
  6985. ret = btrfs_qgroup_record_ref(trans, root->fs_info,
  6986. root->objectid,
  6987. child_bytenr,
  6988. root->nodesize,
  6989. BTRFS_QGROUP_OPER_SUB_SUBTREE,
  6990. 0);
  6991. if (ret)
  6992. goto out;
  6993. }
  6994. if (level == 0) {
  6995. ret = account_leaf_items(trans, root, path->nodes[level]);
  6996. if (ret)
  6997. goto out;
  6998. /* Nonzero return here means we completed our search */
  6999. ret = adjust_slots_upwards(root, path, root_level);
  7000. if (ret)
  7001. break;
  7002. /* Restart search with new slots */
  7003. goto walk_down;
  7004. }
  7005. level--;
  7006. }
  7007. ret = 0;
  7008. out:
  7009. btrfs_free_path(path);
  7010. return ret;
  7011. }
  7012. /*
  7013. * helper to process tree block while walking down the tree.
  7014. *
  7015. * when wc->stage == UPDATE_BACKREF, this function updates
  7016. * back refs for pointers in the block.
  7017. *
  7018. * NOTE: return value 1 means we should stop walking down.
  7019. */
  7020. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  7021. struct btrfs_root *root,
  7022. struct btrfs_path *path,
  7023. struct walk_control *wc, int lookup_info)
  7024. {
  7025. int level = wc->level;
  7026. struct extent_buffer *eb = path->nodes[level];
  7027. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  7028. int ret;
  7029. if (wc->stage == UPDATE_BACKREF &&
  7030. btrfs_header_owner(eb) != root->root_key.objectid)
  7031. return 1;
  7032. /*
  7033. * when reference count of tree block is 1, it won't increase
  7034. * again. once full backref flag is set, we never clear it.
  7035. */
  7036. if (lookup_info &&
  7037. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  7038. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  7039. BUG_ON(!path->locks[level]);
  7040. ret = btrfs_lookup_extent_info(trans, root,
  7041. eb->start, level, 1,
  7042. &wc->refs[level],
  7043. &wc->flags[level]);
  7044. BUG_ON(ret == -ENOMEM);
  7045. if (ret)
  7046. return ret;
  7047. BUG_ON(wc->refs[level] == 0);
  7048. }
  7049. if (wc->stage == DROP_REFERENCE) {
  7050. if (wc->refs[level] > 1)
  7051. return 1;
  7052. if (path->locks[level] && !wc->keep_locks) {
  7053. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7054. path->locks[level] = 0;
  7055. }
  7056. return 0;
  7057. }
  7058. /* wc->stage == UPDATE_BACKREF */
  7059. if (!(wc->flags[level] & flag)) {
  7060. BUG_ON(!path->locks[level]);
  7061. ret = btrfs_inc_ref(trans, root, eb, 1);
  7062. BUG_ON(ret); /* -ENOMEM */
  7063. ret = btrfs_dec_ref(trans, root, eb, 0);
  7064. BUG_ON(ret); /* -ENOMEM */
  7065. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  7066. eb->len, flag,
  7067. btrfs_header_level(eb), 0);
  7068. BUG_ON(ret); /* -ENOMEM */
  7069. wc->flags[level] |= flag;
  7070. }
  7071. /*
  7072. * the block is shared by multiple trees, so it's not good to
  7073. * keep the tree lock
  7074. */
  7075. if (path->locks[level] && level > 0) {
  7076. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7077. path->locks[level] = 0;
  7078. }
  7079. return 0;
  7080. }
  7081. /*
  7082. * helper to process tree block pointer.
  7083. *
  7084. * when wc->stage == DROP_REFERENCE, this function checks
  7085. * reference count of the block pointed to. if the block
  7086. * is shared and we need update back refs for the subtree
  7087. * rooted at the block, this function changes wc->stage to
  7088. * UPDATE_BACKREF. if the block is shared and there is no
  7089. * need to update back, this function drops the reference
  7090. * to the block.
  7091. *
  7092. * NOTE: return value 1 means we should stop walking down.
  7093. */
  7094. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  7095. struct btrfs_root *root,
  7096. struct btrfs_path *path,
  7097. struct walk_control *wc, int *lookup_info)
  7098. {
  7099. u64 bytenr;
  7100. u64 generation;
  7101. u64 parent;
  7102. u32 blocksize;
  7103. struct btrfs_key key;
  7104. struct extent_buffer *next;
  7105. int level = wc->level;
  7106. int reada = 0;
  7107. int ret = 0;
  7108. bool need_account = false;
  7109. generation = btrfs_node_ptr_generation(path->nodes[level],
  7110. path->slots[level]);
  7111. /*
  7112. * if the lower level block was created before the snapshot
  7113. * was created, we know there is no need to update back refs
  7114. * for the subtree
  7115. */
  7116. if (wc->stage == UPDATE_BACKREF &&
  7117. generation <= root->root_key.offset) {
  7118. *lookup_info = 1;
  7119. return 1;
  7120. }
  7121. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  7122. blocksize = root->nodesize;
  7123. next = btrfs_find_tree_block(root->fs_info, bytenr);
  7124. if (!next) {
  7125. next = btrfs_find_create_tree_block(root, bytenr);
  7126. if (!next)
  7127. return -ENOMEM;
  7128. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  7129. level - 1);
  7130. reada = 1;
  7131. }
  7132. btrfs_tree_lock(next);
  7133. btrfs_set_lock_blocking(next);
  7134. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  7135. &wc->refs[level - 1],
  7136. &wc->flags[level - 1]);
  7137. if (ret < 0) {
  7138. btrfs_tree_unlock(next);
  7139. return ret;
  7140. }
  7141. if (unlikely(wc->refs[level - 1] == 0)) {
  7142. btrfs_err(root->fs_info, "Missing references.");
  7143. BUG();
  7144. }
  7145. *lookup_info = 0;
  7146. if (wc->stage == DROP_REFERENCE) {
  7147. if (wc->refs[level - 1] > 1) {
  7148. need_account = true;
  7149. if (level == 1 &&
  7150. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7151. goto skip;
  7152. if (!wc->update_ref ||
  7153. generation <= root->root_key.offset)
  7154. goto skip;
  7155. btrfs_node_key_to_cpu(path->nodes[level], &key,
  7156. path->slots[level]);
  7157. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  7158. if (ret < 0)
  7159. goto skip;
  7160. wc->stage = UPDATE_BACKREF;
  7161. wc->shared_level = level - 1;
  7162. }
  7163. } else {
  7164. if (level == 1 &&
  7165. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7166. goto skip;
  7167. }
  7168. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  7169. btrfs_tree_unlock(next);
  7170. free_extent_buffer(next);
  7171. next = NULL;
  7172. *lookup_info = 1;
  7173. }
  7174. if (!next) {
  7175. if (reada && level == 1)
  7176. reada_walk_down(trans, root, wc, path);
  7177. next = read_tree_block(root, bytenr, generation);
  7178. if (!next || !extent_buffer_uptodate(next)) {
  7179. free_extent_buffer(next);
  7180. return -EIO;
  7181. }
  7182. btrfs_tree_lock(next);
  7183. btrfs_set_lock_blocking(next);
  7184. }
  7185. level--;
  7186. BUG_ON(level != btrfs_header_level(next));
  7187. path->nodes[level] = next;
  7188. path->slots[level] = 0;
  7189. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7190. wc->level = level;
  7191. if (wc->level == 1)
  7192. wc->reada_slot = 0;
  7193. return 0;
  7194. skip:
  7195. wc->refs[level - 1] = 0;
  7196. wc->flags[level - 1] = 0;
  7197. if (wc->stage == DROP_REFERENCE) {
  7198. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  7199. parent = path->nodes[level]->start;
  7200. } else {
  7201. BUG_ON(root->root_key.objectid !=
  7202. btrfs_header_owner(path->nodes[level]));
  7203. parent = 0;
  7204. }
  7205. if (need_account) {
  7206. ret = account_shared_subtree(trans, root, next,
  7207. generation, level - 1);
  7208. if (ret) {
  7209. printk_ratelimited(KERN_ERR "BTRFS: %s Error "
  7210. "%d accounting shared subtree. Quota "
  7211. "is out of sync, rescan required.\n",
  7212. root->fs_info->sb->s_id, ret);
  7213. }
  7214. }
  7215. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  7216. root->root_key.objectid, level - 1, 0, 0);
  7217. BUG_ON(ret); /* -ENOMEM */
  7218. }
  7219. btrfs_tree_unlock(next);
  7220. free_extent_buffer(next);
  7221. *lookup_info = 1;
  7222. return 1;
  7223. }
  7224. /*
  7225. * helper to process tree block while walking up the tree.
  7226. *
  7227. * when wc->stage == DROP_REFERENCE, this function drops
  7228. * reference count on the block.
  7229. *
  7230. * when wc->stage == UPDATE_BACKREF, this function changes
  7231. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  7232. * to UPDATE_BACKREF previously while processing the block.
  7233. *
  7234. * NOTE: return value 1 means we should stop walking up.
  7235. */
  7236. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  7237. struct btrfs_root *root,
  7238. struct btrfs_path *path,
  7239. struct walk_control *wc)
  7240. {
  7241. int ret;
  7242. int level = wc->level;
  7243. struct extent_buffer *eb = path->nodes[level];
  7244. u64 parent = 0;
  7245. if (wc->stage == UPDATE_BACKREF) {
  7246. BUG_ON(wc->shared_level < level);
  7247. if (level < wc->shared_level)
  7248. goto out;
  7249. ret = find_next_key(path, level + 1, &wc->update_progress);
  7250. if (ret > 0)
  7251. wc->update_ref = 0;
  7252. wc->stage = DROP_REFERENCE;
  7253. wc->shared_level = -1;
  7254. path->slots[level] = 0;
  7255. /*
  7256. * check reference count again if the block isn't locked.
  7257. * we should start walking down the tree again if reference
  7258. * count is one.
  7259. */
  7260. if (!path->locks[level]) {
  7261. BUG_ON(level == 0);
  7262. btrfs_tree_lock(eb);
  7263. btrfs_set_lock_blocking(eb);
  7264. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7265. ret = btrfs_lookup_extent_info(trans, root,
  7266. eb->start, level, 1,
  7267. &wc->refs[level],
  7268. &wc->flags[level]);
  7269. if (ret < 0) {
  7270. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7271. path->locks[level] = 0;
  7272. return ret;
  7273. }
  7274. BUG_ON(wc->refs[level] == 0);
  7275. if (wc->refs[level] == 1) {
  7276. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7277. path->locks[level] = 0;
  7278. return 1;
  7279. }
  7280. }
  7281. }
  7282. /* wc->stage == DROP_REFERENCE */
  7283. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  7284. if (wc->refs[level] == 1) {
  7285. if (level == 0) {
  7286. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7287. ret = btrfs_dec_ref(trans, root, eb, 1);
  7288. else
  7289. ret = btrfs_dec_ref(trans, root, eb, 0);
  7290. BUG_ON(ret); /* -ENOMEM */
  7291. ret = account_leaf_items(trans, root, eb);
  7292. if (ret) {
  7293. printk_ratelimited(KERN_ERR "BTRFS: %s Error "
  7294. "%d accounting leaf items. Quota "
  7295. "is out of sync, rescan required.\n",
  7296. root->fs_info->sb->s_id, ret);
  7297. }
  7298. }
  7299. /* make block locked assertion in clean_tree_block happy */
  7300. if (!path->locks[level] &&
  7301. btrfs_header_generation(eb) == trans->transid) {
  7302. btrfs_tree_lock(eb);
  7303. btrfs_set_lock_blocking(eb);
  7304. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7305. }
  7306. clean_tree_block(trans, root->fs_info, eb);
  7307. }
  7308. if (eb == root->node) {
  7309. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7310. parent = eb->start;
  7311. else
  7312. BUG_ON(root->root_key.objectid !=
  7313. btrfs_header_owner(eb));
  7314. } else {
  7315. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7316. parent = path->nodes[level + 1]->start;
  7317. else
  7318. BUG_ON(root->root_key.objectid !=
  7319. btrfs_header_owner(path->nodes[level + 1]));
  7320. }
  7321. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  7322. out:
  7323. wc->refs[level] = 0;
  7324. wc->flags[level] = 0;
  7325. return 0;
  7326. }
  7327. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  7328. struct btrfs_root *root,
  7329. struct btrfs_path *path,
  7330. struct walk_control *wc)
  7331. {
  7332. int level = wc->level;
  7333. int lookup_info = 1;
  7334. int ret;
  7335. while (level >= 0) {
  7336. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  7337. if (ret > 0)
  7338. break;
  7339. if (level == 0)
  7340. break;
  7341. if (path->slots[level] >=
  7342. btrfs_header_nritems(path->nodes[level]))
  7343. break;
  7344. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  7345. if (ret > 0) {
  7346. path->slots[level]++;
  7347. continue;
  7348. } else if (ret < 0)
  7349. return ret;
  7350. level = wc->level;
  7351. }
  7352. return 0;
  7353. }
  7354. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  7355. struct btrfs_root *root,
  7356. struct btrfs_path *path,
  7357. struct walk_control *wc, int max_level)
  7358. {
  7359. int level = wc->level;
  7360. int ret;
  7361. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  7362. while (level < max_level && path->nodes[level]) {
  7363. wc->level = level;
  7364. if (path->slots[level] + 1 <
  7365. btrfs_header_nritems(path->nodes[level])) {
  7366. path->slots[level]++;
  7367. return 0;
  7368. } else {
  7369. ret = walk_up_proc(trans, root, path, wc);
  7370. if (ret > 0)
  7371. return 0;
  7372. if (path->locks[level]) {
  7373. btrfs_tree_unlock_rw(path->nodes[level],
  7374. path->locks[level]);
  7375. path->locks[level] = 0;
  7376. }
  7377. free_extent_buffer(path->nodes[level]);
  7378. path->nodes[level] = NULL;
  7379. level++;
  7380. }
  7381. }
  7382. return 1;
  7383. }
  7384. /*
  7385. * drop a subvolume tree.
  7386. *
  7387. * this function traverses the tree freeing any blocks that only
  7388. * referenced by the tree.
  7389. *
  7390. * when a shared tree block is found. this function decreases its
  7391. * reference count by one. if update_ref is true, this function
  7392. * also make sure backrefs for the shared block and all lower level
  7393. * blocks are properly updated.
  7394. *
  7395. * If called with for_reloc == 0, may exit early with -EAGAIN
  7396. */
  7397. int btrfs_drop_snapshot(struct btrfs_root *root,
  7398. struct btrfs_block_rsv *block_rsv, int update_ref,
  7399. int for_reloc)
  7400. {
  7401. struct btrfs_path *path;
  7402. struct btrfs_trans_handle *trans;
  7403. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7404. struct btrfs_root_item *root_item = &root->root_item;
  7405. struct walk_control *wc;
  7406. struct btrfs_key key;
  7407. int err = 0;
  7408. int ret;
  7409. int level;
  7410. bool root_dropped = false;
  7411. btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid);
  7412. path = btrfs_alloc_path();
  7413. if (!path) {
  7414. err = -ENOMEM;
  7415. goto out;
  7416. }
  7417. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  7418. if (!wc) {
  7419. btrfs_free_path(path);
  7420. err = -ENOMEM;
  7421. goto out;
  7422. }
  7423. trans = btrfs_start_transaction(tree_root, 0);
  7424. if (IS_ERR(trans)) {
  7425. err = PTR_ERR(trans);
  7426. goto out_free;
  7427. }
  7428. if (block_rsv)
  7429. trans->block_rsv = block_rsv;
  7430. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  7431. level = btrfs_header_level(root->node);
  7432. path->nodes[level] = btrfs_lock_root_node(root);
  7433. btrfs_set_lock_blocking(path->nodes[level]);
  7434. path->slots[level] = 0;
  7435. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7436. memset(&wc->update_progress, 0,
  7437. sizeof(wc->update_progress));
  7438. } else {
  7439. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  7440. memcpy(&wc->update_progress, &key,
  7441. sizeof(wc->update_progress));
  7442. level = root_item->drop_level;
  7443. BUG_ON(level == 0);
  7444. path->lowest_level = level;
  7445. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  7446. path->lowest_level = 0;
  7447. if (ret < 0) {
  7448. err = ret;
  7449. goto out_end_trans;
  7450. }
  7451. WARN_ON(ret > 0);
  7452. /*
  7453. * unlock our path, this is safe because only this
  7454. * function is allowed to delete this snapshot
  7455. */
  7456. btrfs_unlock_up_safe(path, 0);
  7457. level = btrfs_header_level(root->node);
  7458. while (1) {
  7459. btrfs_tree_lock(path->nodes[level]);
  7460. btrfs_set_lock_blocking(path->nodes[level]);
  7461. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7462. ret = btrfs_lookup_extent_info(trans, root,
  7463. path->nodes[level]->start,
  7464. level, 1, &wc->refs[level],
  7465. &wc->flags[level]);
  7466. if (ret < 0) {
  7467. err = ret;
  7468. goto out_end_trans;
  7469. }
  7470. BUG_ON(wc->refs[level] == 0);
  7471. if (level == root_item->drop_level)
  7472. break;
  7473. btrfs_tree_unlock(path->nodes[level]);
  7474. path->locks[level] = 0;
  7475. WARN_ON(wc->refs[level] != 1);
  7476. level--;
  7477. }
  7478. }
  7479. wc->level = level;
  7480. wc->shared_level = -1;
  7481. wc->stage = DROP_REFERENCE;
  7482. wc->update_ref = update_ref;
  7483. wc->keep_locks = 0;
  7484. wc->for_reloc = for_reloc;
  7485. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  7486. while (1) {
  7487. ret = walk_down_tree(trans, root, path, wc);
  7488. if (ret < 0) {
  7489. err = ret;
  7490. break;
  7491. }
  7492. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  7493. if (ret < 0) {
  7494. err = ret;
  7495. break;
  7496. }
  7497. if (ret > 0) {
  7498. BUG_ON(wc->stage != DROP_REFERENCE);
  7499. break;
  7500. }
  7501. if (wc->stage == DROP_REFERENCE) {
  7502. level = wc->level;
  7503. btrfs_node_key(path->nodes[level],
  7504. &root_item->drop_progress,
  7505. path->slots[level]);
  7506. root_item->drop_level = level;
  7507. }
  7508. BUG_ON(wc->level == 0);
  7509. if (btrfs_should_end_transaction(trans, tree_root) ||
  7510. (!for_reloc && btrfs_need_cleaner_sleep(root))) {
  7511. ret = btrfs_update_root(trans, tree_root,
  7512. &root->root_key,
  7513. root_item);
  7514. if (ret) {
  7515. btrfs_abort_transaction(trans, tree_root, ret);
  7516. err = ret;
  7517. goto out_end_trans;
  7518. }
  7519. /*
  7520. * Qgroup update accounting is run from
  7521. * delayed ref handling. This usually works
  7522. * out because delayed refs are normally the
  7523. * only way qgroup updates are added. However,
  7524. * we may have added updates during our tree
  7525. * walk so run qgroups here to make sure we
  7526. * don't lose any updates.
  7527. */
  7528. ret = btrfs_delayed_qgroup_accounting(trans,
  7529. root->fs_info);
  7530. if (ret)
  7531. printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
  7532. "running qgroup updates "
  7533. "during snapshot delete. "
  7534. "Quota is out of sync, "
  7535. "rescan required.\n", ret);
  7536. btrfs_end_transaction_throttle(trans, tree_root);
  7537. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  7538. pr_debug("BTRFS: drop snapshot early exit\n");
  7539. err = -EAGAIN;
  7540. goto out_free;
  7541. }
  7542. trans = btrfs_start_transaction(tree_root, 0);
  7543. if (IS_ERR(trans)) {
  7544. err = PTR_ERR(trans);
  7545. goto out_free;
  7546. }
  7547. if (block_rsv)
  7548. trans->block_rsv = block_rsv;
  7549. }
  7550. }
  7551. btrfs_release_path(path);
  7552. if (err)
  7553. goto out_end_trans;
  7554. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  7555. if (ret) {
  7556. btrfs_abort_transaction(trans, tree_root, ret);
  7557. goto out_end_trans;
  7558. }
  7559. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  7560. ret = btrfs_find_root(tree_root, &root->root_key, path,
  7561. NULL, NULL);
  7562. if (ret < 0) {
  7563. btrfs_abort_transaction(trans, tree_root, ret);
  7564. err = ret;
  7565. goto out_end_trans;
  7566. } else if (ret > 0) {
  7567. /* if we fail to delete the orphan item this time
  7568. * around, it'll get picked up the next time.
  7569. *
  7570. * The most common failure here is just -ENOENT.
  7571. */
  7572. btrfs_del_orphan_item(trans, tree_root,
  7573. root->root_key.objectid);
  7574. }
  7575. }
  7576. if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
  7577. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  7578. } else {
  7579. free_extent_buffer(root->node);
  7580. free_extent_buffer(root->commit_root);
  7581. btrfs_put_fs_root(root);
  7582. }
  7583. root_dropped = true;
  7584. out_end_trans:
  7585. ret = btrfs_delayed_qgroup_accounting(trans, tree_root->fs_info);
  7586. if (ret)
  7587. printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
  7588. "running qgroup updates "
  7589. "during snapshot delete. "
  7590. "Quota is out of sync, "
  7591. "rescan required.\n", ret);
  7592. btrfs_end_transaction_throttle(trans, tree_root);
  7593. out_free:
  7594. kfree(wc);
  7595. btrfs_free_path(path);
  7596. out:
  7597. /*
  7598. * So if we need to stop dropping the snapshot for whatever reason we
  7599. * need to make sure to add it back to the dead root list so that we
  7600. * keep trying to do the work later. This also cleans up roots if we
  7601. * don't have it in the radix (like when we recover after a power fail
  7602. * or unmount) so we don't leak memory.
  7603. */
  7604. if (!for_reloc && root_dropped == false)
  7605. btrfs_add_dead_root(root);
  7606. if (err && err != -EAGAIN)
  7607. btrfs_std_error(root->fs_info, err);
  7608. return err;
  7609. }
  7610. /*
  7611. * drop subtree rooted at tree block 'node'.
  7612. *
  7613. * NOTE: this function will unlock and release tree block 'node'
  7614. * only used by relocation code
  7615. */
  7616. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  7617. struct btrfs_root *root,
  7618. struct extent_buffer *node,
  7619. struct extent_buffer *parent)
  7620. {
  7621. struct btrfs_path *path;
  7622. struct walk_control *wc;
  7623. int level;
  7624. int parent_level;
  7625. int ret = 0;
  7626. int wret;
  7627. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  7628. path = btrfs_alloc_path();
  7629. if (!path)
  7630. return -ENOMEM;
  7631. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  7632. if (!wc) {
  7633. btrfs_free_path(path);
  7634. return -ENOMEM;
  7635. }
  7636. btrfs_assert_tree_locked(parent);
  7637. parent_level = btrfs_header_level(parent);
  7638. extent_buffer_get(parent);
  7639. path->nodes[parent_level] = parent;
  7640. path->slots[parent_level] = btrfs_header_nritems(parent);
  7641. btrfs_assert_tree_locked(node);
  7642. level = btrfs_header_level(node);
  7643. path->nodes[level] = node;
  7644. path->slots[level] = 0;
  7645. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7646. wc->refs[parent_level] = 1;
  7647. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  7648. wc->level = level;
  7649. wc->shared_level = -1;
  7650. wc->stage = DROP_REFERENCE;
  7651. wc->update_ref = 0;
  7652. wc->keep_locks = 1;
  7653. wc->for_reloc = 1;
  7654. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  7655. while (1) {
  7656. wret = walk_down_tree(trans, root, path, wc);
  7657. if (wret < 0) {
  7658. ret = wret;
  7659. break;
  7660. }
  7661. wret = walk_up_tree(trans, root, path, wc, parent_level);
  7662. if (wret < 0)
  7663. ret = wret;
  7664. if (wret != 0)
  7665. break;
  7666. }
  7667. kfree(wc);
  7668. btrfs_free_path(path);
  7669. return ret;
  7670. }
  7671. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  7672. {
  7673. u64 num_devices;
  7674. u64 stripped;
  7675. /*
  7676. * if restripe for this chunk_type is on pick target profile and
  7677. * return, otherwise do the usual balance
  7678. */
  7679. stripped = get_restripe_target(root->fs_info, flags);
  7680. if (stripped)
  7681. return extended_to_chunk(stripped);
  7682. num_devices = root->fs_info->fs_devices->rw_devices;
  7683. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  7684. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  7685. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  7686. if (num_devices == 1) {
  7687. stripped |= BTRFS_BLOCK_GROUP_DUP;
  7688. stripped = flags & ~stripped;
  7689. /* turn raid0 into single device chunks */
  7690. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  7691. return stripped;
  7692. /* turn mirroring into duplication */
  7693. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7694. BTRFS_BLOCK_GROUP_RAID10))
  7695. return stripped | BTRFS_BLOCK_GROUP_DUP;
  7696. } else {
  7697. /* they already had raid on here, just return */
  7698. if (flags & stripped)
  7699. return flags;
  7700. stripped |= BTRFS_BLOCK_GROUP_DUP;
  7701. stripped = flags & ~stripped;
  7702. /* switch duplicated blocks with raid1 */
  7703. if (flags & BTRFS_BLOCK_GROUP_DUP)
  7704. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  7705. /* this is drive concat, leave it alone */
  7706. }
  7707. return flags;
  7708. }
  7709. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  7710. {
  7711. struct btrfs_space_info *sinfo = cache->space_info;
  7712. u64 num_bytes;
  7713. u64 min_allocable_bytes;
  7714. int ret = -ENOSPC;
  7715. /*
  7716. * We need some metadata space and system metadata space for
  7717. * allocating chunks in some corner cases until we force to set
  7718. * it to be readonly.
  7719. */
  7720. if ((sinfo->flags &
  7721. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  7722. !force)
  7723. min_allocable_bytes = 1 * 1024 * 1024;
  7724. else
  7725. min_allocable_bytes = 0;
  7726. spin_lock(&sinfo->lock);
  7727. spin_lock(&cache->lock);
  7728. if (cache->ro) {
  7729. ret = 0;
  7730. goto out;
  7731. }
  7732. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7733. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7734. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  7735. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  7736. min_allocable_bytes <= sinfo->total_bytes) {
  7737. sinfo->bytes_readonly += num_bytes;
  7738. cache->ro = 1;
  7739. list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
  7740. ret = 0;
  7741. }
  7742. out:
  7743. spin_unlock(&cache->lock);
  7744. spin_unlock(&sinfo->lock);
  7745. return ret;
  7746. }
  7747. int btrfs_set_block_group_ro(struct btrfs_root *root,
  7748. struct btrfs_block_group_cache *cache)
  7749. {
  7750. struct btrfs_trans_handle *trans;
  7751. u64 alloc_flags;
  7752. int ret;
  7753. BUG_ON(cache->ro);
  7754. again:
  7755. trans = btrfs_join_transaction(root);
  7756. if (IS_ERR(trans))
  7757. return PTR_ERR(trans);
  7758. /*
  7759. * we're not allowed to set block groups readonly after the dirty
  7760. * block groups cache has started writing. If it already started,
  7761. * back off and let this transaction commit
  7762. */
  7763. mutex_lock(&root->fs_info->ro_block_group_mutex);
  7764. if (trans->transaction->dirty_bg_run) {
  7765. u64 transid = trans->transid;
  7766. mutex_unlock(&root->fs_info->ro_block_group_mutex);
  7767. btrfs_end_transaction(trans, root);
  7768. ret = btrfs_wait_for_commit(root, transid);
  7769. if (ret)
  7770. return ret;
  7771. goto again;
  7772. }
  7773. /*
  7774. * if we are changing raid levels, try to allocate a corresponding
  7775. * block group with the new raid level.
  7776. */
  7777. alloc_flags = update_block_group_flags(root, cache->flags);
  7778. if (alloc_flags != cache->flags) {
  7779. ret = do_chunk_alloc(trans, root, alloc_flags,
  7780. CHUNK_ALLOC_FORCE);
  7781. /*
  7782. * ENOSPC is allowed here, we may have enough space
  7783. * already allocated at the new raid level to
  7784. * carry on
  7785. */
  7786. if (ret == -ENOSPC)
  7787. ret = 0;
  7788. if (ret < 0)
  7789. goto out;
  7790. }
  7791. ret = set_block_group_ro(cache, 0);
  7792. if (!ret)
  7793. goto out;
  7794. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  7795. ret = do_chunk_alloc(trans, root, alloc_flags,
  7796. CHUNK_ALLOC_FORCE);
  7797. if (ret < 0)
  7798. goto out;
  7799. ret = set_block_group_ro(cache, 0);
  7800. out:
  7801. if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
  7802. alloc_flags = update_block_group_flags(root, cache->flags);
  7803. lock_chunks(root->fs_info->chunk_root);
  7804. check_system_chunk(trans, root, alloc_flags);
  7805. unlock_chunks(root->fs_info->chunk_root);
  7806. }
  7807. mutex_unlock(&root->fs_info->ro_block_group_mutex);
  7808. btrfs_end_transaction(trans, root);
  7809. return ret;
  7810. }
  7811. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  7812. struct btrfs_root *root, u64 type)
  7813. {
  7814. u64 alloc_flags = get_alloc_profile(root, type);
  7815. return do_chunk_alloc(trans, root, alloc_flags,
  7816. CHUNK_ALLOC_FORCE);
  7817. }
  7818. /*
  7819. * helper to account the unused space of all the readonly block group in the
  7820. * space_info. takes mirrors into account.
  7821. */
  7822. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  7823. {
  7824. struct btrfs_block_group_cache *block_group;
  7825. u64 free_bytes = 0;
  7826. int factor;
  7827. /* It's df, we don't care if it's racey */
  7828. if (list_empty(&sinfo->ro_bgs))
  7829. return 0;
  7830. spin_lock(&sinfo->lock);
  7831. list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
  7832. spin_lock(&block_group->lock);
  7833. if (!block_group->ro) {
  7834. spin_unlock(&block_group->lock);
  7835. continue;
  7836. }
  7837. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7838. BTRFS_BLOCK_GROUP_RAID10 |
  7839. BTRFS_BLOCK_GROUP_DUP))
  7840. factor = 2;
  7841. else
  7842. factor = 1;
  7843. free_bytes += (block_group->key.offset -
  7844. btrfs_block_group_used(&block_group->item)) *
  7845. factor;
  7846. spin_unlock(&block_group->lock);
  7847. }
  7848. spin_unlock(&sinfo->lock);
  7849. return free_bytes;
  7850. }
  7851. void btrfs_set_block_group_rw(struct btrfs_root *root,
  7852. struct btrfs_block_group_cache *cache)
  7853. {
  7854. struct btrfs_space_info *sinfo = cache->space_info;
  7855. u64 num_bytes;
  7856. BUG_ON(!cache->ro);
  7857. spin_lock(&sinfo->lock);
  7858. spin_lock(&cache->lock);
  7859. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7860. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7861. sinfo->bytes_readonly -= num_bytes;
  7862. cache->ro = 0;
  7863. list_del_init(&cache->ro_list);
  7864. spin_unlock(&cache->lock);
  7865. spin_unlock(&sinfo->lock);
  7866. }
  7867. /*
  7868. * checks to see if its even possible to relocate this block group.
  7869. *
  7870. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7871. * ok to go ahead and try.
  7872. */
  7873. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7874. {
  7875. struct btrfs_block_group_cache *block_group;
  7876. struct btrfs_space_info *space_info;
  7877. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7878. struct btrfs_device *device;
  7879. struct btrfs_trans_handle *trans;
  7880. u64 min_free;
  7881. u64 dev_min = 1;
  7882. u64 dev_nr = 0;
  7883. u64 target;
  7884. int index;
  7885. int full = 0;
  7886. int ret = 0;
  7887. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7888. /* odd, couldn't find the block group, leave it alone */
  7889. if (!block_group)
  7890. return -1;
  7891. min_free = btrfs_block_group_used(&block_group->item);
  7892. /* no bytes used, we're good */
  7893. if (!min_free)
  7894. goto out;
  7895. space_info = block_group->space_info;
  7896. spin_lock(&space_info->lock);
  7897. full = space_info->full;
  7898. /*
  7899. * if this is the last block group we have in this space, we can't
  7900. * relocate it unless we're able to allocate a new chunk below.
  7901. *
  7902. * Otherwise, we need to make sure we have room in the space to handle
  7903. * all of the extents from this block group. If we can, we're good
  7904. */
  7905. if ((space_info->total_bytes != block_group->key.offset) &&
  7906. (space_info->bytes_used + space_info->bytes_reserved +
  7907. space_info->bytes_pinned + space_info->bytes_readonly +
  7908. min_free < space_info->total_bytes)) {
  7909. spin_unlock(&space_info->lock);
  7910. goto out;
  7911. }
  7912. spin_unlock(&space_info->lock);
  7913. /*
  7914. * ok we don't have enough space, but maybe we have free space on our
  7915. * devices to allocate new chunks for relocation, so loop through our
  7916. * alloc devices and guess if we have enough space. if this block
  7917. * group is going to be restriped, run checks against the target
  7918. * profile instead of the current one.
  7919. */
  7920. ret = -1;
  7921. /*
  7922. * index:
  7923. * 0: raid10
  7924. * 1: raid1
  7925. * 2: dup
  7926. * 3: raid0
  7927. * 4: single
  7928. */
  7929. target = get_restripe_target(root->fs_info, block_group->flags);
  7930. if (target) {
  7931. index = __get_raid_index(extended_to_chunk(target));
  7932. } else {
  7933. /*
  7934. * this is just a balance, so if we were marked as full
  7935. * we know there is no space for a new chunk
  7936. */
  7937. if (full)
  7938. goto out;
  7939. index = get_block_group_index(block_group);
  7940. }
  7941. if (index == BTRFS_RAID_RAID10) {
  7942. dev_min = 4;
  7943. /* Divide by 2 */
  7944. min_free >>= 1;
  7945. } else if (index == BTRFS_RAID_RAID1) {
  7946. dev_min = 2;
  7947. } else if (index == BTRFS_RAID_DUP) {
  7948. /* Multiply by 2 */
  7949. min_free <<= 1;
  7950. } else if (index == BTRFS_RAID_RAID0) {
  7951. dev_min = fs_devices->rw_devices;
  7952. min_free = div64_u64(min_free, dev_min);
  7953. }
  7954. /* We need to do this so that we can look at pending chunks */
  7955. trans = btrfs_join_transaction(root);
  7956. if (IS_ERR(trans)) {
  7957. ret = PTR_ERR(trans);
  7958. goto out;
  7959. }
  7960. mutex_lock(&root->fs_info->chunk_mutex);
  7961. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7962. u64 dev_offset;
  7963. /*
  7964. * check to make sure we can actually find a chunk with enough
  7965. * space to fit our block group in.
  7966. */
  7967. if (device->total_bytes > device->bytes_used + min_free &&
  7968. !device->is_tgtdev_for_dev_replace) {
  7969. ret = find_free_dev_extent(trans, device, min_free,
  7970. &dev_offset, NULL);
  7971. if (!ret)
  7972. dev_nr++;
  7973. if (dev_nr >= dev_min)
  7974. break;
  7975. ret = -1;
  7976. }
  7977. }
  7978. mutex_unlock(&root->fs_info->chunk_mutex);
  7979. btrfs_end_transaction(trans, root);
  7980. out:
  7981. btrfs_put_block_group(block_group);
  7982. return ret;
  7983. }
  7984. static int find_first_block_group(struct btrfs_root *root,
  7985. struct btrfs_path *path, struct btrfs_key *key)
  7986. {
  7987. int ret = 0;
  7988. struct btrfs_key found_key;
  7989. struct extent_buffer *leaf;
  7990. int slot;
  7991. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7992. if (ret < 0)
  7993. goto out;
  7994. while (1) {
  7995. slot = path->slots[0];
  7996. leaf = path->nodes[0];
  7997. if (slot >= btrfs_header_nritems(leaf)) {
  7998. ret = btrfs_next_leaf(root, path);
  7999. if (ret == 0)
  8000. continue;
  8001. if (ret < 0)
  8002. goto out;
  8003. break;
  8004. }
  8005. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  8006. if (found_key.objectid >= key->objectid &&
  8007. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  8008. ret = 0;
  8009. goto out;
  8010. }
  8011. path->slots[0]++;
  8012. }
  8013. out:
  8014. return ret;
  8015. }
  8016. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  8017. {
  8018. struct btrfs_block_group_cache *block_group;
  8019. u64 last = 0;
  8020. while (1) {
  8021. struct inode *inode;
  8022. block_group = btrfs_lookup_first_block_group(info, last);
  8023. while (block_group) {
  8024. spin_lock(&block_group->lock);
  8025. if (block_group->iref)
  8026. break;
  8027. spin_unlock(&block_group->lock);
  8028. block_group = next_block_group(info->tree_root,
  8029. block_group);
  8030. }
  8031. if (!block_group) {
  8032. if (last == 0)
  8033. break;
  8034. last = 0;
  8035. continue;
  8036. }
  8037. inode = block_group->inode;
  8038. block_group->iref = 0;
  8039. block_group->inode = NULL;
  8040. spin_unlock(&block_group->lock);
  8041. iput(inode);
  8042. last = block_group->key.objectid + block_group->key.offset;
  8043. btrfs_put_block_group(block_group);
  8044. }
  8045. }
  8046. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  8047. {
  8048. struct btrfs_block_group_cache *block_group;
  8049. struct btrfs_space_info *space_info;
  8050. struct btrfs_caching_control *caching_ctl;
  8051. struct rb_node *n;
  8052. down_write(&info->commit_root_sem);
  8053. while (!list_empty(&info->caching_block_groups)) {
  8054. caching_ctl = list_entry(info->caching_block_groups.next,
  8055. struct btrfs_caching_control, list);
  8056. list_del(&caching_ctl->list);
  8057. put_caching_control(caching_ctl);
  8058. }
  8059. up_write(&info->commit_root_sem);
  8060. spin_lock(&info->unused_bgs_lock);
  8061. while (!list_empty(&info->unused_bgs)) {
  8062. block_group = list_first_entry(&info->unused_bgs,
  8063. struct btrfs_block_group_cache,
  8064. bg_list);
  8065. list_del_init(&block_group->bg_list);
  8066. btrfs_put_block_group(block_group);
  8067. }
  8068. spin_unlock(&info->unused_bgs_lock);
  8069. spin_lock(&info->block_group_cache_lock);
  8070. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  8071. block_group = rb_entry(n, struct btrfs_block_group_cache,
  8072. cache_node);
  8073. rb_erase(&block_group->cache_node,
  8074. &info->block_group_cache_tree);
  8075. RB_CLEAR_NODE(&block_group->cache_node);
  8076. spin_unlock(&info->block_group_cache_lock);
  8077. down_write(&block_group->space_info->groups_sem);
  8078. list_del(&block_group->list);
  8079. up_write(&block_group->space_info->groups_sem);
  8080. if (block_group->cached == BTRFS_CACHE_STARTED)
  8081. wait_block_group_cache_done(block_group);
  8082. /*
  8083. * We haven't cached this block group, which means we could
  8084. * possibly have excluded extents on this block group.
  8085. */
  8086. if (block_group->cached == BTRFS_CACHE_NO ||
  8087. block_group->cached == BTRFS_CACHE_ERROR)
  8088. free_excluded_extents(info->extent_root, block_group);
  8089. btrfs_remove_free_space_cache(block_group);
  8090. btrfs_put_block_group(block_group);
  8091. spin_lock(&info->block_group_cache_lock);
  8092. }
  8093. spin_unlock(&info->block_group_cache_lock);
  8094. /* now that all the block groups are freed, go through and
  8095. * free all the space_info structs. This is only called during
  8096. * the final stages of unmount, and so we know nobody is
  8097. * using them. We call synchronize_rcu() once before we start,
  8098. * just to be on the safe side.
  8099. */
  8100. synchronize_rcu();
  8101. release_global_block_rsv(info);
  8102. while (!list_empty(&info->space_info)) {
  8103. int i;
  8104. space_info = list_entry(info->space_info.next,
  8105. struct btrfs_space_info,
  8106. list);
  8107. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  8108. if (WARN_ON(space_info->bytes_pinned > 0 ||
  8109. space_info->bytes_reserved > 0 ||
  8110. space_info->bytes_may_use > 0)) {
  8111. dump_space_info(space_info, 0, 0);
  8112. }
  8113. }
  8114. list_del(&space_info->list);
  8115. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  8116. struct kobject *kobj;
  8117. kobj = space_info->block_group_kobjs[i];
  8118. space_info->block_group_kobjs[i] = NULL;
  8119. if (kobj) {
  8120. kobject_del(kobj);
  8121. kobject_put(kobj);
  8122. }
  8123. }
  8124. kobject_del(&space_info->kobj);
  8125. kobject_put(&space_info->kobj);
  8126. }
  8127. return 0;
  8128. }
  8129. static void __link_block_group(struct btrfs_space_info *space_info,
  8130. struct btrfs_block_group_cache *cache)
  8131. {
  8132. int index = get_block_group_index(cache);
  8133. bool first = false;
  8134. down_write(&space_info->groups_sem);
  8135. if (list_empty(&space_info->block_groups[index]))
  8136. first = true;
  8137. list_add_tail(&cache->list, &space_info->block_groups[index]);
  8138. up_write(&space_info->groups_sem);
  8139. if (first) {
  8140. struct raid_kobject *rkobj;
  8141. int ret;
  8142. rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
  8143. if (!rkobj)
  8144. goto out_err;
  8145. rkobj->raid_type = index;
  8146. kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
  8147. ret = kobject_add(&rkobj->kobj, &space_info->kobj,
  8148. "%s", get_raid_name(index));
  8149. if (ret) {
  8150. kobject_put(&rkobj->kobj);
  8151. goto out_err;
  8152. }
  8153. space_info->block_group_kobjs[index] = &rkobj->kobj;
  8154. }
  8155. return;
  8156. out_err:
  8157. pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
  8158. }
  8159. static struct btrfs_block_group_cache *
  8160. btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
  8161. {
  8162. struct btrfs_block_group_cache *cache;
  8163. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  8164. if (!cache)
  8165. return NULL;
  8166. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  8167. GFP_NOFS);
  8168. if (!cache->free_space_ctl) {
  8169. kfree(cache);
  8170. return NULL;
  8171. }
  8172. cache->key.objectid = start;
  8173. cache->key.offset = size;
  8174. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  8175. cache->sectorsize = root->sectorsize;
  8176. cache->fs_info = root->fs_info;
  8177. cache->full_stripe_len = btrfs_full_stripe_len(root,
  8178. &root->fs_info->mapping_tree,
  8179. start);
  8180. atomic_set(&cache->count, 1);
  8181. spin_lock_init(&cache->lock);
  8182. init_rwsem(&cache->data_rwsem);
  8183. INIT_LIST_HEAD(&cache->list);
  8184. INIT_LIST_HEAD(&cache->cluster_list);
  8185. INIT_LIST_HEAD(&cache->bg_list);
  8186. INIT_LIST_HEAD(&cache->ro_list);
  8187. INIT_LIST_HEAD(&cache->dirty_list);
  8188. INIT_LIST_HEAD(&cache->io_list);
  8189. btrfs_init_free_space_ctl(cache);
  8190. atomic_set(&cache->trimming, 0);
  8191. return cache;
  8192. }
  8193. int btrfs_read_block_groups(struct btrfs_root *root)
  8194. {
  8195. struct btrfs_path *path;
  8196. int ret;
  8197. struct btrfs_block_group_cache *cache;
  8198. struct btrfs_fs_info *info = root->fs_info;
  8199. struct btrfs_space_info *space_info;
  8200. struct btrfs_key key;
  8201. struct btrfs_key found_key;
  8202. struct extent_buffer *leaf;
  8203. int need_clear = 0;
  8204. u64 cache_gen;
  8205. root = info->extent_root;
  8206. key.objectid = 0;
  8207. key.offset = 0;
  8208. key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  8209. path = btrfs_alloc_path();
  8210. if (!path)
  8211. return -ENOMEM;
  8212. path->reada = 1;
  8213. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  8214. if (btrfs_test_opt(root, SPACE_CACHE) &&
  8215. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  8216. need_clear = 1;
  8217. if (btrfs_test_opt(root, CLEAR_CACHE))
  8218. need_clear = 1;
  8219. while (1) {
  8220. ret = find_first_block_group(root, path, &key);
  8221. if (ret > 0)
  8222. break;
  8223. if (ret != 0)
  8224. goto error;
  8225. leaf = path->nodes[0];
  8226. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  8227. cache = btrfs_create_block_group_cache(root, found_key.objectid,
  8228. found_key.offset);
  8229. if (!cache) {
  8230. ret = -ENOMEM;
  8231. goto error;
  8232. }
  8233. if (need_clear) {
  8234. /*
  8235. * When we mount with old space cache, we need to
  8236. * set BTRFS_DC_CLEAR and set dirty flag.
  8237. *
  8238. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  8239. * truncate the old free space cache inode and
  8240. * setup a new one.
  8241. * b) Setting 'dirty flag' makes sure that we flush
  8242. * the new space cache info onto disk.
  8243. */
  8244. if (btrfs_test_opt(root, SPACE_CACHE))
  8245. cache->disk_cache_state = BTRFS_DC_CLEAR;
  8246. }
  8247. read_extent_buffer(leaf, &cache->item,
  8248. btrfs_item_ptr_offset(leaf, path->slots[0]),
  8249. sizeof(cache->item));
  8250. cache->flags = btrfs_block_group_flags(&cache->item);
  8251. key.objectid = found_key.objectid + found_key.offset;
  8252. btrfs_release_path(path);
  8253. /*
  8254. * We need to exclude the super stripes now so that the space
  8255. * info has super bytes accounted for, otherwise we'll think
  8256. * we have more space than we actually do.
  8257. */
  8258. ret = exclude_super_stripes(root, cache);
  8259. if (ret) {
  8260. /*
  8261. * We may have excluded something, so call this just in
  8262. * case.
  8263. */
  8264. free_excluded_extents(root, cache);
  8265. btrfs_put_block_group(cache);
  8266. goto error;
  8267. }
  8268. /*
  8269. * check for two cases, either we are full, and therefore
  8270. * don't need to bother with the caching work since we won't
  8271. * find any space, or we are empty, and we can just add all
  8272. * the space in and be done with it. This saves us _alot_ of
  8273. * time, particularly in the full case.
  8274. */
  8275. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  8276. cache->last_byte_to_unpin = (u64)-1;
  8277. cache->cached = BTRFS_CACHE_FINISHED;
  8278. free_excluded_extents(root, cache);
  8279. } else if (btrfs_block_group_used(&cache->item) == 0) {
  8280. cache->last_byte_to_unpin = (u64)-1;
  8281. cache->cached = BTRFS_CACHE_FINISHED;
  8282. add_new_free_space(cache, root->fs_info,
  8283. found_key.objectid,
  8284. found_key.objectid +
  8285. found_key.offset);
  8286. free_excluded_extents(root, cache);
  8287. }
  8288. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  8289. if (ret) {
  8290. btrfs_remove_free_space_cache(cache);
  8291. btrfs_put_block_group(cache);
  8292. goto error;
  8293. }
  8294. ret = update_space_info(info, cache->flags, found_key.offset,
  8295. btrfs_block_group_used(&cache->item),
  8296. &space_info);
  8297. if (ret) {
  8298. btrfs_remove_free_space_cache(cache);
  8299. spin_lock(&info->block_group_cache_lock);
  8300. rb_erase(&cache->cache_node,
  8301. &info->block_group_cache_tree);
  8302. RB_CLEAR_NODE(&cache->cache_node);
  8303. spin_unlock(&info->block_group_cache_lock);
  8304. btrfs_put_block_group(cache);
  8305. goto error;
  8306. }
  8307. cache->space_info = space_info;
  8308. spin_lock(&cache->space_info->lock);
  8309. cache->space_info->bytes_readonly += cache->bytes_super;
  8310. spin_unlock(&cache->space_info->lock);
  8311. __link_block_group(space_info, cache);
  8312. set_avail_alloc_bits(root->fs_info, cache->flags);
  8313. if (btrfs_chunk_readonly(root, cache->key.objectid)) {
  8314. set_block_group_ro(cache, 1);
  8315. } else if (btrfs_block_group_used(&cache->item) == 0) {
  8316. spin_lock(&info->unused_bgs_lock);
  8317. /* Should always be true but just in case. */
  8318. if (list_empty(&cache->bg_list)) {
  8319. btrfs_get_block_group(cache);
  8320. list_add_tail(&cache->bg_list,
  8321. &info->unused_bgs);
  8322. }
  8323. spin_unlock(&info->unused_bgs_lock);
  8324. }
  8325. }
  8326. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  8327. if (!(get_alloc_profile(root, space_info->flags) &
  8328. (BTRFS_BLOCK_GROUP_RAID10 |
  8329. BTRFS_BLOCK_GROUP_RAID1 |
  8330. BTRFS_BLOCK_GROUP_RAID5 |
  8331. BTRFS_BLOCK_GROUP_RAID6 |
  8332. BTRFS_BLOCK_GROUP_DUP)))
  8333. continue;
  8334. /*
  8335. * avoid allocating from un-mirrored block group if there are
  8336. * mirrored block groups.
  8337. */
  8338. list_for_each_entry(cache,
  8339. &space_info->block_groups[BTRFS_RAID_RAID0],
  8340. list)
  8341. set_block_group_ro(cache, 1);
  8342. list_for_each_entry(cache,
  8343. &space_info->block_groups[BTRFS_RAID_SINGLE],
  8344. list)
  8345. set_block_group_ro(cache, 1);
  8346. }
  8347. init_global_block_rsv(info);
  8348. ret = 0;
  8349. error:
  8350. btrfs_free_path(path);
  8351. return ret;
  8352. }
  8353. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  8354. struct btrfs_root *root)
  8355. {
  8356. struct btrfs_block_group_cache *block_group, *tmp;
  8357. struct btrfs_root *extent_root = root->fs_info->extent_root;
  8358. struct btrfs_block_group_item item;
  8359. struct btrfs_key key;
  8360. int ret = 0;
  8361. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
  8362. if (ret)
  8363. goto next;
  8364. spin_lock(&block_group->lock);
  8365. memcpy(&item, &block_group->item, sizeof(item));
  8366. memcpy(&key, &block_group->key, sizeof(key));
  8367. spin_unlock(&block_group->lock);
  8368. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  8369. sizeof(item));
  8370. if (ret)
  8371. btrfs_abort_transaction(trans, extent_root, ret);
  8372. ret = btrfs_finish_chunk_alloc(trans, extent_root,
  8373. key.objectid, key.offset);
  8374. if (ret)
  8375. btrfs_abort_transaction(trans, extent_root, ret);
  8376. next:
  8377. list_del_init(&block_group->bg_list);
  8378. }
  8379. }
  8380. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  8381. struct btrfs_root *root, u64 bytes_used,
  8382. u64 type, u64 chunk_objectid, u64 chunk_offset,
  8383. u64 size)
  8384. {
  8385. int ret;
  8386. struct btrfs_root *extent_root;
  8387. struct btrfs_block_group_cache *cache;
  8388. extent_root = root->fs_info->extent_root;
  8389. btrfs_set_log_full_commit(root->fs_info, trans);
  8390. cache = btrfs_create_block_group_cache(root, chunk_offset, size);
  8391. if (!cache)
  8392. return -ENOMEM;
  8393. btrfs_set_block_group_used(&cache->item, bytes_used);
  8394. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  8395. btrfs_set_block_group_flags(&cache->item, type);
  8396. cache->flags = type;
  8397. cache->last_byte_to_unpin = (u64)-1;
  8398. cache->cached = BTRFS_CACHE_FINISHED;
  8399. ret = exclude_super_stripes(root, cache);
  8400. if (ret) {
  8401. /*
  8402. * We may have excluded something, so call this just in
  8403. * case.
  8404. */
  8405. free_excluded_extents(root, cache);
  8406. btrfs_put_block_group(cache);
  8407. return ret;
  8408. }
  8409. add_new_free_space(cache, root->fs_info, chunk_offset,
  8410. chunk_offset + size);
  8411. free_excluded_extents(root, cache);
  8412. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  8413. if (ret) {
  8414. btrfs_remove_free_space_cache(cache);
  8415. btrfs_put_block_group(cache);
  8416. return ret;
  8417. }
  8418. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  8419. &cache->space_info);
  8420. if (ret) {
  8421. btrfs_remove_free_space_cache(cache);
  8422. spin_lock(&root->fs_info->block_group_cache_lock);
  8423. rb_erase(&cache->cache_node,
  8424. &root->fs_info->block_group_cache_tree);
  8425. RB_CLEAR_NODE(&cache->cache_node);
  8426. spin_unlock(&root->fs_info->block_group_cache_lock);
  8427. btrfs_put_block_group(cache);
  8428. return ret;
  8429. }
  8430. update_global_block_rsv(root->fs_info);
  8431. spin_lock(&cache->space_info->lock);
  8432. cache->space_info->bytes_readonly += cache->bytes_super;
  8433. spin_unlock(&cache->space_info->lock);
  8434. __link_block_group(cache->space_info, cache);
  8435. list_add_tail(&cache->bg_list, &trans->new_bgs);
  8436. set_avail_alloc_bits(extent_root->fs_info, type);
  8437. return 0;
  8438. }
  8439. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  8440. {
  8441. u64 extra_flags = chunk_to_extended(flags) &
  8442. BTRFS_EXTENDED_PROFILE_MASK;
  8443. write_seqlock(&fs_info->profiles_lock);
  8444. if (flags & BTRFS_BLOCK_GROUP_DATA)
  8445. fs_info->avail_data_alloc_bits &= ~extra_flags;
  8446. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  8447. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  8448. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  8449. fs_info->avail_system_alloc_bits &= ~extra_flags;
  8450. write_sequnlock(&fs_info->profiles_lock);
  8451. }
  8452. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  8453. struct btrfs_root *root, u64 group_start,
  8454. struct extent_map *em)
  8455. {
  8456. struct btrfs_path *path;
  8457. struct btrfs_block_group_cache *block_group;
  8458. struct btrfs_free_cluster *cluster;
  8459. struct btrfs_root *tree_root = root->fs_info->tree_root;
  8460. struct btrfs_key key;
  8461. struct inode *inode;
  8462. struct kobject *kobj = NULL;
  8463. int ret;
  8464. int index;
  8465. int factor;
  8466. struct btrfs_caching_control *caching_ctl = NULL;
  8467. bool remove_em;
  8468. root = root->fs_info->extent_root;
  8469. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  8470. BUG_ON(!block_group);
  8471. BUG_ON(!block_group->ro);
  8472. /*
  8473. * Free the reserved super bytes from this block group before
  8474. * remove it.
  8475. */
  8476. free_excluded_extents(root, block_group);
  8477. memcpy(&key, &block_group->key, sizeof(key));
  8478. index = get_block_group_index(block_group);
  8479. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  8480. BTRFS_BLOCK_GROUP_RAID1 |
  8481. BTRFS_BLOCK_GROUP_RAID10))
  8482. factor = 2;
  8483. else
  8484. factor = 1;
  8485. /* make sure this block group isn't part of an allocation cluster */
  8486. cluster = &root->fs_info->data_alloc_cluster;
  8487. spin_lock(&cluster->refill_lock);
  8488. btrfs_return_cluster_to_free_space(block_group, cluster);
  8489. spin_unlock(&cluster->refill_lock);
  8490. /*
  8491. * make sure this block group isn't part of a metadata
  8492. * allocation cluster
  8493. */
  8494. cluster = &root->fs_info->meta_alloc_cluster;
  8495. spin_lock(&cluster->refill_lock);
  8496. btrfs_return_cluster_to_free_space(block_group, cluster);
  8497. spin_unlock(&cluster->refill_lock);
  8498. path = btrfs_alloc_path();
  8499. if (!path) {
  8500. ret = -ENOMEM;
  8501. goto out;
  8502. }
  8503. /*
  8504. * get the inode first so any iput calls done for the io_list
  8505. * aren't the final iput (no unlinks allowed now)
  8506. */
  8507. inode = lookup_free_space_inode(tree_root, block_group, path);
  8508. mutex_lock(&trans->transaction->cache_write_mutex);
  8509. /*
  8510. * make sure our free spache cache IO is done before remove the
  8511. * free space inode
  8512. */
  8513. spin_lock(&trans->transaction->dirty_bgs_lock);
  8514. if (!list_empty(&block_group->io_list)) {
  8515. list_del_init(&block_group->io_list);
  8516. WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
  8517. spin_unlock(&trans->transaction->dirty_bgs_lock);
  8518. btrfs_wait_cache_io(root, trans, block_group,
  8519. &block_group->io_ctl, path,
  8520. block_group->key.objectid);
  8521. btrfs_put_block_group(block_group);
  8522. spin_lock(&trans->transaction->dirty_bgs_lock);
  8523. }
  8524. if (!list_empty(&block_group->dirty_list)) {
  8525. list_del_init(&block_group->dirty_list);
  8526. btrfs_put_block_group(block_group);
  8527. }
  8528. spin_unlock(&trans->transaction->dirty_bgs_lock);
  8529. mutex_unlock(&trans->transaction->cache_write_mutex);
  8530. if (!IS_ERR(inode)) {
  8531. ret = btrfs_orphan_add(trans, inode);
  8532. if (ret) {
  8533. btrfs_add_delayed_iput(inode);
  8534. goto out;
  8535. }
  8536. clear_nlink(inode);
  8537. /* One for the block groups ref */
  8538. spin_lock(&block_group->lock);
  8539. if (block_group->iref) {
  8540. block_group->iref = 0;
  8541. block_group->inode = NULL;
  8542. spin_unlock(&block_group->lock);
  8543. iput(inode);
  8544. } else {
  8545. spin_unlock(&block_group->lock);
  8546. }
  8547. /* One for our lookup ref */
  8548. btrfs_add_delayed_iput(inode);
  8549. }
  8550. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  8551. key.offset = block_group->key.objectid;
  8552. key.type = 0;
  8553. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  8554. if (ret < 0)
  8555. goto out;
  8556. if (ret > 0)
  8557. btrfs_release_path(path);
  8558. if (ret == 0) {
  8559. ret = btrfs_del_item(trans, tree_root, path);
  8560. if (ret)
  8561. goto out;
  8562. btrfs_release_path(path);
  8563. }
  8564. spin_lock(&root->fs_info->block_group_cache_lock);
  8565. rb_erase(&block_group->cache_node,
  8566. &root->fs_info->block_group_cache_tree);
  8567. RB_CLEAR_NODE(&block_group->cache_node);
  8568. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  8569. root->fs_info->first_logical_byte = (u64)-1;
  8570. spin_unlock(&root->fs_info->block_group_cache_lock);
  8571. down_write(&block_group->space_info->groups_sem);
  8572. /*
  8573. * we must use list_del_init so people can check to see if they
  8574. * are still on the list after taking the semaphore
  8575. */
  8576. list_del_init(&block_group->list);
  8577. if (list_empty(&block_group->space_info->block_groups[index])) {
  8578. kobj = block_group->space_info->block_group_kobjs[index];
  8579. block_group->space_info->block_group_kobjs[index] = NULL;
  8580. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  8581. }
  8582. up_write(&block_group->space_info->groups_sem);
  8583. if (kobj) {
  8584. kobject_del(kobj);
  8585. kobject_put(kobj);
  8586. }
  8587. if (block_group->has_caching_ctl)
  8588. caching_ctl = get_caching_control(block_group);
  8589. if (block_group->cached == BTRFS_CACHE_STARTED)
  8590. wait_block_group_cache_done(block_group);
  8591. if (block_group->has_caching_ctl) {
  8592. down_write(&root->fs_info->commit_root_sem);
  8593. if (!caching_ctl) {
  8594. struct btrfs_caching_control *ctl;
  8595. list_for_each_entry(ctl,
  8596. &root->fs_info->caching_block_groups, list)
  8597. if (ctl->block_group == block_group) {
  8598. caching_ctl = ctl;
  8599. atomic_inc(&caching_ctl->count);
  8600. break;
  8601. }
  8602. }
  8603. if (caching_ctl)
  8604. list_del_init(&caching_ctl->list);
  8605. up_write(&root->fs_info->commit_root_sem);
  8606. if (caching_ctl) {
  8607. /* Once for the caching bgs list and once for us. */
  8608. put_caching_control(caching_ctl);
  8609. put_caching_control(caching_ctl);
  8610. }
  8611. }
  8612. spin_lock(&trans->transaction->dirty_bgs_lock);
  8613. if (!list_empty(&block_group->dirty_list)) {
  8614. WARN_ON(1);
  8615. }
  8616. if (!list_empty(&block_group->io_list)) {
  8617. WARN_ON(1);
  8618. }
  8619. spin_unlock(&trans->transaction->dirty_bgs_lock);
  8620. btrfs_remove_free_space_cache(block_group);
  8621. spin_lock(&block_group->space_info->lock);
  8622. list_del_init(&block_group->ro_list);
  8623. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  8624. WARN_ON(block_group->space_info->total_bytes
  8625. < block_group->key.offset);
  8626. WARN_ON(block_group->space_info->bytes_readonly
  8627. < block_group->key.offset);
  8628. WARN_ON(block_group->space_info->disk_total
  8629. < block_group->key.offset * factor);
  8630. }
  8631. block_group->space_info->total_bytes -= block_group->key.offset;
  8632. block_group->space_info->bytes_readonly -= block_group->key.offset;
  8633. block_group->space_info->disk_total -= block_group->key.offset * factor;
  8634. spin_unlock(&block_group->space_info->lock);
  8635. memcpy(&key, &block_group->key, sizeof(key));
  8636. lock_chunks(root);
  8637. if (!list_empty(&em->list)) {
  8638. /* We're in the transaction->pending_chunks list. */
  8639. free_extent_map(em);
  8640. }
  8641. spin_lock(&block_group->lock);
  8642. block_group->removed = 1;
  8643. /*
  8644. * At this point trimming can't start on this block group, because we
  8645. * removed the block group from the tree fs_info->block_group_cache_tree
  8646. * so no one can't find it anymore and even if someone already got this
  8647. * block group before we removed it from the rbtree, they have already
  8648. * incremented block_group->trimming - if they didn't, they won't find
  8649. * any free space entries because we already removed them all when we
  8650. * called btrfs_remove_free_space_cache().
  8651. *
  8652. * And we must not remove the extent map from the fs_info->mapping_tree
  8653. * to prevent the same logical address range and physical device space
  8654. * ranges from being reused for a new block group. This is because our
  8655. * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
  8656. * completely transactionless, so while it is trimming a range the
  8657. * currently running transaction might finish and a new one start,
  8658. * allowing for new block groups to be created that can reuse the same
  8659. * physical device locations unless we take this special care.
  8660. */
  8661. remove_em = (atomic_read(&block_group->trimming) == 0);
  8662. /*
  8663. * Make sure a trimmer task always sees the em in the pinned_chunks list
  8664. * if it sees block_group->removed == 1 (needs to lock block_group->lock
  8665. * before checking block_group->removed).
  8666. */
  8667. if (!remove_em) {
  8668. /*
  8669. * Our em might be in trans->transaction->pending_chunks which
  8670. * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
  8671. * and so is the fs_info->pinned_chunks list.
  8672. *
  8673. * So at this point we must be holding the chunk_mutex to avoid
  8674. * any races with chunk allocation (more specifically at
  8675. * volumes.c:contains_pending_extent()), to ensure it always
  8676. * sees the em, either in the pending_chunks list or in the
  8677. * pinned_chunks list.
  8678. */
  8679. list_move_tail(&em->list, &root->fs_info->pinned_chunks);
  8680. }
  8681. spin_unlock(&block_group->lock);
  8682. if (remove_em) {
  8683. struct extent_map_tree *em_tree;
  8684. em_tree = &root->fs_info->mapping_tree.map_tree;
  8685. write_lock(&em_tree->lock);
  8686. /*
  8687. * The em might be in the pending_chunks list, so make sure the
  8688. * chunk mutex is locked, since remove_extent_mapping() will
  8689. * delete us from that list.
  8690. */
  8691. remove_extent_mapping(em_tree, em);
  8692. write_unlock(&em_tree->lock);
  8693. /* once for the tree */
  8694. free_extent_map(em);
  8695. }
  8696. unlock_chunks(root);
  8697. btrfs_put_block_group(block_group);
  8698. btrfs_put_block_group(block_group);
  8699. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  8700. if (ret > 0)
  8701. ret = -EIO;
  8702. if (ret < 0)
  8703. goto out;
  8704. ret = btrfs_del_item(trans, root, path);
  8705. out:
  8706. btrfs_free_path(path);
  8707. return ret;
  8708. }
  8709. /*
  8710. * Process the unused_bgs list and remove any that don't have any allocated
  8711. * space inside of them.
  8712. */
  8713. void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
  8714. {
  8715. struct btrfs_block_group_cache *block_group;
  8716. struct btrfs_space_info *space_info;
  8717. struct btrfs_root *root = fs_info->extent_root;
  8718. struct btrfs_trans_handle *trans;
  8719. int ret = 0;
  8720. if (!fs_info->open)
  8721. return;
  8722. spin_lock(&fs_info->unused_bgs_lock);
  8723. while (!list_empty(&fs_info->unused_bgs)) {
  8724. u64 start, end;
  8725. block_group = list_first_entry(&fs_info->unused_bgs,
  8726. struct btrfs_block_group_cache,
  8727. bg_list);
  8728. space_info = block_group->space_info;
  8729. list_del_init(&block_group->bg_list);
  8730. if (ret || btrfs_mixed_space_info(space_info)) {
  8731. btrfs_put_block_group(block_group);
  8732. continue;
  8733. }
  8734. spin_unlock(&fs_info->unused_bgs_lock);
  8735. /* Don't want to race with allocators so take the groups_sem */
  8736. down_write(&space_info->groups_sem);
  8737. spin_lock(&block_group->lock);
  8738. if (block_group->reserved ||
  8739. btrfs_block_group_used(&block_group->item) ||
  8740. block_group->ro) {
  8741. /*
  8742. * We want to bail if we made new allocations or have
  8743. * outstanding allocations in this block group. We do
  8744. * the ro check in case balance is currently acting on
  8745. * this block group.
  8746. */
  8747. spin_unlock(&block_group->lock);
  8748. up_write(&space_info->groups_sem);
  8749. goto next;
  8750. }
  8751. spin_unlock(&block_group->lock);
  8752. /* We don't want to force the issue, only flip if it's ok. */
  8753. ret = set_block_group_ro(block_group, 0);
  8754. up_write(&space_info->groups_sem);
  8755. if (ret < 0) {
  8756. ret = 0;
  8757. goto next;
  8758. }
  8759. /*
  8760. * Want to do this before we do anything else so we can recover
  8761. * properly if we fail to join the transaction.
  8762. */
  8763. /* 1 for btrfs_orphan_reserve_metadata() */
  8764. trans = btrfs_start_transaction(root, 1);
  8765. if (IS_ERR(trans)) {
  8766. btrfs_set_block_group_rw(root, block_group);
  8767. ret = PTR_ERR(trans);
  8768. goto next;
  8769. }
  8770. /*
  8771. * We could have pending pinned extents for this block group,
  8772. * just delete them, we don't care about them anymore.
  8773. */
  8774. start = block_group->key.objectid;
  8775. end = start + block_group->key.offset - 1;
  8776. /*
  8777. * Hold the unused_bg_unpin_mutex lock to avoid racing with
  8778. * btrfs_finish_extent_commit(). If we are at transaction N,
  8779. * another task might be running finish_extent_commit() for the
  8780. * previous transaction N - 1, and have seen a range belonging
  8781. * to the block group in freed_extents[] before we were able to
  8782. * clear the whole block group range from freed_extents[]. This
  8783. * means that task can lookup for the block group after we
  8784. * unpinned it from freed_extents[] and removed it, leading to
  8785. * a BUG_ON() at btrfs_unpin_extent_range().
  8786. */
  8787. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  8788. ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
  8789. EXTENT_DIRTY, GFP_NOFS);
  8790. if (ret) {
  8791. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  8792. btrfs_set_block_group_rw(root, block_group);
  8793. goto end_trans;
  8794. }
  8795. ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
  8796. EXTENT_DIRTY, GFP_NOFS);
  8797. if (ret) {
  8798. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  8799. btrfs_set_block_group_rw(root, block_group);
  8800. goto end_trans;
  8801. }
  8802. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  8803. /* Reset pinned so btrfs_put_block_group doesn't complain */
  8804. spin_lock(&space_info->lock);
  8805. spin_lock(&block_group->lock);
  8806. space_info->bytes_pinned -= block_group->pinned;
  8807. space_info->bytes_readonly += block_group->pinned;
  8808. percpu_counter_add(&space_info->total_bytes_pinned,
  8809. -block_group->pinned);
  8810. block_group->pinned = 0;
  8811. spin_unlock(&block_group->lock);
  8812. spin_unlock(&space_info->lock);
  8813. /*
  8814. * Btrfs_remove_chunk will abort the transaction if things go
  8815. * horribly wrong.
  8816. */
  8817. ret = btrfs_remove_chunk(trans, root,
  8818. block_group->key.objectid);
  8819. end_trans:
  8820. btrfs_end_transaction(trans, root);
  8821. next:
  8822. btrfs_put_block_group(block_group);
  8823. spin_lock(&fs_info->unused_bgs_lock);
  8824. }
  8825. spin_unlock(&fs_info->unused_bgs_lock);
  8826. }
  8827. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  8828. {
  8829. struct btrfs_space_info *space_info;
  8830. struct btrfs_super_block *disk_super;
  8831. u64 features;
  8832. u64 flags;
  8833. int mixed = 0;
  8834. int ret;
  8835. disk_super = fs_info->super_copy;
  8836. if (!btrfs_super_root(disk_super))
  8837. return 1;
  8838. features = btrfs_super_incompat_flags(disk_super);
  8839. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  8840. mixed = 1;
  8841. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  8842. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8843. if (ret)
  8844. goto out;
  8845. if (mixed) {
  8846. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  8847. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8848. } else {
  8849. flags = BTRFS_BLOCK_GROUP_METADATA;
  8850. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8851. if (ret)
  8852. goto out;
  8853. flags = BTRFS_BLOCK_GROUP_DATA;
  8854. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8855. }
  8856. out:
  8857. return ret;
  8858. }
  8859. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  8860. {
  8861. return unpin_extent_range(root, start, end, false);
  8862. }
  8863. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  8864. {
  8865. struct btrfs_fs_info *fs_info = root->fs_info;
  8866. struct btrfs_block_group_cache *cache = NULL;
  8867. u64 group_trimmed;
  8868. u64 start;
  8869. u64 end;
  8870. u64 trimmed = 0;
  8871. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  8872. int ret = 0;
  8873. /*
  8874. * try to trim all FS space, our block group may start from non-zero.
  8875. */
  8876. if (range->len == total_bytes)
  8877. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  8878. else
  8879. cache = btrfs_lookup_block_group(fs_info, range->start);
  8880. while (cache) {
  8881. if (cache->key.objectid >= (range->start + range->len)) {
  8882. btrfs_put_block_group(cache);
  8883. break;
  8884. }
  8885. start = max(range->start, cache->key.objectid);
  8886. end = min(range->start + range->len,
  8887. cache->key.objectid + cache->key.offset);
  8888. if (end - start >= range->minlen) {
  8889. if (!block_group_cache_done(cache)) {
  8890. ret = cache_block_group(cache, 0);
  8891. if (ret) {
  8892. btrfs_put_block_group(cache);
  8893. break;
  8894. }
  8895. ret = wait_block_group_cache_done(cache);
  8896. if (ret) {
  8897. btrfs_put_block_group(cache);
  8898. break;
  8899. }
  8900. }
  8901. ret = btrfs_trim_block_group(cache,
  8902. &group_trimmed,
  8903. start,
  8904. end,
  8905. range->minlen);
  8906. trimmed += group_trimmed;
  8907. if (ret) {
  8908. btrfs_put_block_group(cache);
  8909. break;
  8910. }
  8911. }
  8912. cache = next_block_group(fs_info->tree_root, cache);
  8913. }
  8914. range->len = trimmed;
  8915. return ret;
  8916. }
  8917. /*
  8918. * btrfs_{start,end}_write_no_snapshoting() are similar to
  8919. * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
  8920. * data into the page cache through nocow before the subvolume is snapshoted,
  8921. * but flush the data into disk after the snapshot creation, or to prevent
  8922. * operations while snapshoting is ongoing and that cause the snapshot to be
  8923. * inconsistent (writes followed by expanding truncates for example).
  8924. */
  8925. void btrfs_end_write_no_snapshoting(struct btrfs_root *root)
  8926. {
  8927. percpu_counter_dec(&root->subv_writers->counter);
  8928. /*
  8929. * Make sure counter is updated before we wake up
  8930. * waiters.
  8931. */
  8932. smp_mb();
  8933. if (waitqueue_active(&root->subv_writers->wait))
  8934. wake_up(&root->subv_writers->wait);
  8935. }
  8936. int btrfs_start_write_no_snapshoting(struct btrfs_root *root)
  8937. {
  8938. if (atomic_read(&root->will_be_snapshoted))
  8939. return 0;
  8940. percpu_counter_inc(&root->subv_writers->counter);
  8941. /*
  8942. * Make sure counter is updated before we check for snapshot creation.
  8943. */
  8944. smp_mb();
  8945. if (atomic_read(&root->will_be_snapshoted)) {
  8946. btrfs_end_write_no_snapshoting(root);
  8947. return 0;
  8948. }
  8949. return 1;
  8950. }