extent-tree.c 236 KB

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