extent-tree.c 273 KB

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