extent-tree.c 229 KB

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