extent-tree.c 237 KB

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