extent-tree.c 234 KB

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