extent-tree.c 236 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882
  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. } else {
  2191. list_del_init(&locked_ref->cluster);
  2192. }
  2193. spin_unlock(&delayed_refs->lock);
  2194. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2195. must_insert_reserved);
  2196. btrfs_free_delayed_extent_op(extent_op);
  2197. if (ret) {
  2198. btrfs_delayed_ref_unlock(locked_ref);
  2199. btrfs_put_delayed_ref(ref);
  2200. btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
  2201. spin_lock(&delayed_refs->lock);
  2202. return ret;
  2203. }
  2204. /*
  2205. * If this node is a head, that means all the refs in this head
  2206. * have been dealt with, and we will pick the next head to deal
  2207. * with, so we must unlock the head and drop it from the cluster
  2208. * list before we release it.
  2209. */
  2210. if (btrfs_delayed_ref_is_head(ref)) {
  2211. btrfs_delayed_ref_unlock(locked_ref);
  2212. locked_ref = NULL;
  2213. }
  2214. btrfs_put_delayed_ref(ref);
  2215. count++;
  2216. next:
  2217. cond_resched();
  2218. spin_lock(&delayed_refs->lock);
  2219. }
  2220. return count;
  2221. }
  2222. #ifdef SCRAMBLE_DELAYED_REFS
  2223. /*
  2224. * Normally delayed refs get processed in ascending bytenr order. This
  2225. * correlates in most cases to the order added. To expose dependencies on this
  2226. * order, we start to process the tree in the middle instead of the beginning
  2227. */
  2228. static u64 find_middle(struct rb_root *root)
  2229. {
  2230. struct rb_node *n = root->rb_node;
  2231. struct btrfs_delayed_ref_node *entry;
  2232. int alt = 1;
  2233. u64 middle;
  2234. u64 first = 0, last = 0;
  2235. n = rb_first(root);
  2236. if (n) {
  2237. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2238. first = entry->bytenr;
  2239. }
  2240. n = rb_last(root);
  2241. if (n) {
  2242. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2243. last = entry->bytenr;
  2244. }
  2245. n = root->rb_node;
  2246. while (n) {
  2247. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2248. WARN_ON(!entry->in_tree);
  2249. middle = entry->bytenr;
  2250. if (alt)
  2251. n = n->rb_left;
  2252. else
  2253. n = n->rb_right;
  2254. alt = 1 - alt;
  2255. }
  2256. return middle;
  2257. }
  2258. #endif
  2259. int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
  2260. struct btrfs_fs_info *fs_info)
  2261. {
  2262. struct qgroup_update *qgroup_update;
  2263. int ret = 0;
  2264. if (list_empty(&trans->qgroup_ref_list) !=
  2265. !trans->delayed_ref_elem.seq) {
  2266. /* list without seq or seq without list */
  2267. btrfs_err(fs_info,
  2268. "qgroup accounting update error, list is%s empty, seq is %#x.%x",
  2269. list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2270. (u32)(trans->delayed_ref_elem.seq >> 32),
  2271. (u32)trans->delayed_ref_elem.seq);
  2272. BUG();
  2273. }
  2274. if (!trans->delayed_ref_elem.seq)
  2275. return 0;
  2276. while (!list_empty(&trans->qgroup_ref_list)) {
  2277. qgroup_update = list_first_entry(&trans->qgroup_ref_list,
  2278. struct qgroup_update, list);
  2279. list_del(&qgroup_update->list);
  2280. if (!ret)
  2281. ret = btrfs_qgroup_account_ref(
  2282. trans, fs_info, qgroup_update->node,
  2283. qgroup_update->extent_op);
  2284. kfree(qgroup_update);
  2285. }
  2286. btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  2287. return ret;
  2288. }
  2289. static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
  2290. int count)
  2291. {
  2292. int val = atomic_read(&delayed_refs->ref_seq);
  2293. if (val < seq || val >= seq + count)
  2294. return 1;
  2295. return 0;
  2296. }
  2297. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2298. {
  2299. u64 num_bytes;
  2300. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2301. sizeof(struct btrfs_extent_inline_ref));
  2302. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2303. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2304. /*
  2305. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2306. * closer to what we're really going to want to ouse.
  2307. */
  2308. return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2309. }
  2310. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2311. struct btrfs_root *root)
  2312. {
  2313. struct btrfs_block_rsv *global_rsv;
  2314. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2315. u64 num_bytes;
  2316. int ret = 0;
  2317. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2318. num_heads = heads_to_leaves(root, num_heads);
  2319. if (num_heads > 1)
  2320. num_bytes += (num_heads - 1) * root->leafsize;
  2321. num_bytes <<= 1;
  2322. global_rsv = &root->fs_info->global_block_rsv;
  2323. /*
  2324. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2325. * wiggle room since running delayed refs can create more delayed refs.
  2326. */
  2327. if (global_rsv->space_info->full)
  2328. num_bytes <<= 1;
  2329. spin_lock(&global_rsv->lock);
  2330. if (global_rsv->reserved <= num_bytes)
  2331. ret = 1;
  2332. spin_unlock(&global_rsv->lock);
  2333. return ret;
  2334. }
  2335. /*
  2336. * this starts processing the delayed reference count updates and
  2337. * extent insertions we have queued up so far. count can be
  2338. * 0, which means to process everything in the tree at the start
  2339. * of the run (but not newly added entries), or it can be some target
  2340. * number you'd like to process.
  2341. *
  2342. * Returns 0 on success or if called with an aborted transaction
  2343. * Returns <0 on error and aborts the transaction
  2344. */
  2345. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2346. struct btrfs_root *root, unsigned long count)
  2347. {
  2348. struct rb_node *node;
  2349. struct btrfs_delayed_ref_root *delayed_refs;
  2350. struct btrfs_delayed_ref_node *ref;
  2351. struct list_head cluster;
  2352. int ret;
  2353. u64 delayed_start;
  2354. int run_all = count == (unsigned long)-1;
  2355. int run_most = 0;
  2356. int loops;
  2357. /* We'll clean this up in btrfs_cleanup_transaction */
  2358. if (trans->aborted)
  2359. return 0;
  2360. if (root == root->fs_info->extent_root)
  2361. root = root->fs_info->tree_root;
  2362. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  2363. delayed_refs = &trans->transaction->delayed_refs;
  2364. INIT_LIST_HEAD(&cluster);
  2365. if (count == 0) {
  2366. count = delayed_refs->num_entries * 2;
  2367. run_most = 1;
  2368. }
  2369. if (!run_all && !run_most) {
  2370. int old;
  2371. int seq = atomic_read(&delayed_refs->ref_seq);
  2372. progress:
  2373. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2374. if (old) {
  2375. DEFINE_WAIT(__wait);
  2376. if (delayed_refs->flushing ||
  2377. !btrfs_should_throttle_delayed_refs(trans, root))
  2378. return 0;
  2379. prepare_to_wait(&delayed_refs->wait, &__wait,
  2380. TASK_UNINTERRUPTIBLE);
  2381. old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
  2382. if (old) {
  2383. schedule();
  2384. finish_wait(&delayed_refs->wait, &__wait);
  2385. if (!refs_newer(delayed_refs, seq, 256))
  2386. goto progress;
  2387. else
  2388. return 0;
  2389. } else {
  2390. finish_wait(&delayed_refs->wait, &__wait);
  2391. goto again;
  2392. }
  2393. }
  2394. } else {
  2395. atomic_inc(&delayed_refs->procs_running_refs);
  2396. }
  2397. again:
  2398. loops = 0;
  2399. spin_lock(&delayed_refs->lock);
  2400. #ifdef SCRAMBLE_DELAYED_REFS
  2401. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2402. #endif
  2403. while (1) {
  2404. if (!(run_all || run_most) &&
  2405. !btrfs_should_throttle_delayed_refs(trans, root))
  2406. break;
  2407. /*
  2408. * go find something we can process in the rbtree. We start at
  2409. * the beginning of the tree, and then build a cluster
  2410. * of refs to process starting at the first one we are able to
  2411. * lock
  2412. */
  2413. delayed_start = delayed_refs->run_delayed_start;
  2414. ret = btrfs_find_ref_cluster(trans, &cluster,
  2415. delayed_refs->run_delayed_start);
  2416. if (ret)
  2417. break;
  2418. ret = run_clustered_refs(trans, root, &cluster);
  2419. if (ret < 0) {
  2420. btrfs_release_ref_cluster(&cluster);
  2421. spin_unlock(&delayed_refs->lock);
  2422. btrfs_abort_transaction(trans, root, ret);
  2423. atomic_dec(&delayed_refs->procs_running_refs);
  2424. wake_up(&delayed_refs->wait);
  2425. return ret;
  2426. }
  2427. atomic_add(ret, &delayed_refs->ref_seq);
  2428. count -= min_t(unsigned long, ret, count);
  2429. if (count == 0)
  2430. break;
  2431. if (delayed_start >= delayed_refs->run_delayed_start) {
  2432. if (loops == 0) {
  2433. /*
  2434. * btrfs_find_ref_cluster looped. let's do one
  2435. * more cycle. if we don't run any delayed ref
  2436. * during that cycle (because we can't because
  2437. * all of them are blocked), bail out.
  2438. */
  2439. loops = 1;
  2440. } else {
  2441. /*
  2442. * no runnable refs left, stop trying
  2443. */
  2444. BUG_ON(run_all);
  2445. break;
  2446. }
  2447. }
  2448. if (ret) {
  2449. /* refs were run, let's reset staleness detection */
  2450. loops = 0;
  2451. }
  2452. }
  2453. if (run_all) {
  2454. if (!list_empty(&trans->new_bgs)) {
  2455. spin_unlock(&delayed_refs->lock);
  2456. btrfs_create_pending_block_groups(trans, root);
  2457. spin_lock(&delayed_refs->lock);
  2458. }
  2459. node = rb_first(&delayed_refs->root);
  2460. if (!node)
  2461. goto out;
  2462. count = (unsigned long)-1;
  2463. while (node) {
  2464. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2465. rb_node);
  2466. if (btrfs_delayed_ref_is_head(ref)) {
  2467. struct btrfs_delayed_ref_head *head;
  2468. head = btrfs_delayed_node_to_head(ref);
  2469. atomic_inc(&ref->refs);
  2470. spin_unlock(&delayed_refs->lock);
  2471. /*
  2472. * Mutex was contended, block until it's
  2473. * released and try again
  2474. */
  2475. mutex_lock(&head->mutex);
  2476. mutex_unlock(&head->mutex);
  2477. btrfs_put_delayed_ref(ref);
  2478. cond_resched();
  2479. goto again;
  2480. }
  2481. node = rb_next(node);
  2482. }
  2483. spin_unlock(&delayed_refs->lock);
  2484. schedule_timeout(1);
  2485. goto again;
  2486. }
  2487. out:
  2488. atomic_dec(&delayed_refs->procs_running_refs);
  2489. smp_mb();
  2490. if (waitqueue_active(&delayed_refs->wait))
  2491. wake_up(&delayed_refs->wait);
  2492. spin_unlock(&delayed_refs->lock);
  2493. assert_qgroups_uptodate(trans);
  2494. return 0;
  2495. }
  2496. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2497. struct btrfs_root *root,
  2498. u64 bytenr, u64 num_bytes, u64 flags,
  2499. int level, int is_data)
  2500. {
  2501. struct btrfs_delayed_extent_op *extent_op;
  2502. int ret;
  2503. extent_op = btrfs_alloc_delayed_extent_op();
  2504. if (!extent_op)
  2505. return -ENOMEM;
  2506. extent_op->flags_to_set = flags;
  2507. extent_op->update_flags = 1;
  2508. extent_op->update_key = 0;
  2509. extent_op->is_data = is_data ? 1 : 0;
  2510. extent_op->level = level;
  2511. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2512. num_bytes, extent_op);
  2513. if (ret)
  2514. btrfs_free_delayed_extent_op(extent_op);
  2515. return ret;
  2516. }
  2517. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2518. struct btrfs_root *root,
  2519. struct btrfs_path *path,
  2520. u64 objectid, u64 offset, u64 bytenr)
  2521. {
  2522. struct btrfs_delayed_ref_head *head;
  2523. struct btrfs_delayed_ref_node *ref;
  2524. struct btrfs_delayed_data_ref *data_ref;
  2525. struct btrfs_delayed_ref_root *delayed_refs;
  2526. struct rb_node *node;
  2527. int ret = 0;
  2528. ret = -ENOENT;
  2529. delayed_refs = &trans->transaction->delayed_refs;
  2530. spin_lock(&delayed_refs->lock);
  2531. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2532. if (!head)
  2533. goto out;
  2534. if (!mutex_trylock(&head->mutex)) {
  2535. atomic_inc(&head->node.refs);
  2536. spin_unlock(&delayed_refs->lock);
  2537. btrfs_release_path(path);
  2538. /*
  2539. * Mutex was contended, block until it's released and let
  2540. * caller try again
  2541. */
  2542. mutex_lock(&head->mutex);
  2543. mutex_unlock(&head->mutex);
  2544. btrfs_put_delayed_ref(&head->node);
  2545. return -EAGAIN;
  2546. }
  2547. node = rb_prev(&head->node.rb_node);
  2548. if (!node)
  2549. goto out_unlock;
  2550. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2551. if (ref->bytenr != bytenr)
  2552. goto out_unlock;
  2553. ret = 1;
  2554. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2555. goto out_unlock;
  2556. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2557. node = rb_prev(node);
  2558. if (node) {
  2559. int seq = ref->seq;
  2560. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2561. if (ref->bytenr == bytenr && ref->seq == seq)
  2562. goto out_unlock;
  2563. }
  2564. if (data_ref->root != root->root_key.objectid ||
  2565. data_ref->objectid != objectid || data_ref->offset != offset)
  2566. goto out_unlock;
  2567. ret = 0;
  2568. out_unlock:
  2569. mutex_unlock(&head->mutex);
  2570. out:
  2571. spin_unlock(&delayed_refs->lock);
  2572. return ret;
  2573. }
  2574. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2575. struct btrfs_root *root,
  2576. struct btrfs_path *path,
  2577. u64 objectid, u64 offset, u64 bytenr)
  2578. {
  2579. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2580. struct extent_buffer *leaf;
  2581. struct btrfs_extent_data_ref *ref;
  2582. struct btrfs_extent_inline_ref *iref;
  2583. struct btrfs_extent_item *ei;
  2584. struct btrfs_key key;
  2585. u32 item_size;
  2586. int ret;
  2587. key.objectid = bytenr;
  2588. key.offset = (u64)-1;
  2589. key.type = BTRFS_EXTENT_ITEM_KEY;
  2590. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2591. if (ret < 0)
  2592. goto out;
  2593. BUG_ON(ret == 0); /* Corruption */
  2594. ret = -ENOENT;
  2595. if (path->slots[0] == 0)
  2596. goto out;
  2597. path->slots[0]--;
  2598. leaf = path->nodes[0];
  2599. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2600. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2601. goto out;
  2602. ret = 1;
  2603. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2604. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2605. if (item_size < sizeof(*ei)) {
  2606. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2607. goto out;
  2608. }
  2609. #endif
  2610. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2611. if (item_size != sizeof(*ei) +
  2612. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2613. goto out;
  2614. if (btrfs_extent_generation(leaf, ei) <=
  2615. btrfs_root_last_snapshot(&root->root_item))
  2616. goto out;
  2617. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2618. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2619. BTRFS_EXTENT_DATA_REF_KEY)
  2620. goto out;
  2621. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2622. if (btrfs_extent_refs(leaf, ei) !=
  2623. btrfs_extent_data_ref_count(leaf, ref) ||
  2624. btrfs_extent_data_ref_root(leaf, ref) !=
  2625. root->root_key.objectid ||
  2626. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2627. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2628. goto out;
  2629. ret = 0;
  2630. out:
  2631. return ret;
  2632. }
  2633. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2634. struct btrfs_root *root,
  2635. u64 objectid, u64 offset, u64 bytenr)
  2636. {
  2637. struct btrfs_path *path;
  2638. int ret;
  2639. int ret2;
  2640. path = btrfs_alloc_path();
  2641. if (!path)
  2642. return -ENOENT;
  2643. do {
  2644. ret = check_committed_ref(trans, root, path, objectid,
  2645. offset, bytenr);
  2646. if (ret && ret != -ENOENT)
  2647. goto out;
  2648. ret2 = check_delayed_ref(trans, root, path, objectid,
  2649. offset, bytenr);
  2650. } while (ret2 == -EAGAIN);
  2651. if (ret2 && ret2 != -ENOENT) {
  2652. ret = ret2;
  2653. goto out;
  2654. }
  2655. if (ret != -ENOENT || ret2 != -ENOENT)
  2656. ret = 0;
  2657. out:
  2658. btrfs_free_path(path);
  2659. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2660. WARN_ON(ret > 0);
  2661. return ret;
  2662. }
  2663. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2664. struct btrfs_root *root,
  2665. struct extent_buffer *buf,
  2666. int full_backref, int inc, int for_cow)
  2667. {
  2668. u64 bytenr;
  2669. u64 num_bytes;
  2670. u64 parent;
  2671. u64 ref_root;
  2672. u32 nritems;
  2673. struct btrfs_key key;
  2674. struct btrfs_file_extent_item *fi;
  2675. int i;
  2676. int level;
  2677. int ret = 0;
  2678. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2679. u64, u64, u64, u64, u64, u64, int);
  2680. ref_root = btrfs_header_owner(buf);
  2681. nritems = btrfs_header_nritems(buf);
  2682. level = btrfs_header_level(buf);
  2683. if (!root->ref_cows && level == 0)
  2684. return 0;
  2685. if (inc)
  2686. process_func = btrfs_inc_extent_ref;
  2687. else
  2688. process_func = btrfs_free_extent;
  2689. if (full_backref)
  2690. parent = buf->start;
  2691. else
  2692. parent = 0;
  2693. for (i = 0; i < nritems; i++) {
  2694. if (level == 0) {
  2695. btrfs_item_key_to_cpu(buf, &key, i);
  2696. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2697. continue;
  2698. fi = btrfs_item_ptr(buf, i,
  2699. struct btrfs_file_extent_item);
  2700. if (btrfs_file_extent_type(buf, fi) ==
  2701. BTRFS_FILE_EXTENT_INLINE)
  2702. continue;
  2703. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2704. if (bytenr == 0)
  2705. continue;
  2706. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2707. key.offset -= btrfs_file_extent_offset(buf, fi);
  2708. ret = process_func(trans, root, bytenr, num_bytes,
  2709. parent, ref_root, key.objectid,
  2710. key.offset, for_cow);
  2711. if (ret)
  2712. goto fail;
  2713. } else {
  2714. bytenr = btrfs_node_blockptr(buf, i);
  2715. num_bytes = btrfs_level_size(root, level - 1);
  2716. ret = process_func(trans, root, bytenr, num_bytes,
  2717. parent, ref_root, level - 1, 0,
  2718. for_cow);
  2719. if (ret)
  2720. goto fail;
  2721. }
  2722. }
  2723. return 0;
  2724. fail:
  2725. return ret;
  2726. }
  2727. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2728. struct extent_buffer *buf, int full_backref, int for_cow)
  2729. {
  2730. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2731. }
  2732. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2733. struct extent_buffer *buf, int full_backref, int for_cow)
  2734. {
  2735. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2736. }
  2737. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2738. struct btrfs_root *root,
  2739. struct btrfs_path *path,
  2740. struct btrfs_block_group_cache *cache)
  2741. {
  2742. int ret;
  2743. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2744. unsigned long bi;
  2745. struct extent_buffer *leaf;
  2746. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2747. if (ret < 0)
  2748. goto fail;
  2749. BUG_ON(ret); /* Corruption */
  2750. leaf = path->nodes[0];
  2751. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2752. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2753. btrfs_mark_buffer_dirty(leaf);
  2754. btrfs_release_path(path);
  2755. fail:
  2756. if (ret) {
  2757. btrfs_abort_transaction(trans, root, ret);
  2758. return ret;
  2759. }
  2760. return 0;
  2761. }
  2762. static struct btrfs_block_group_cache *
  2763. next_block_group(struct btrfs_root *root,
  2764. struct btrfs_block_group_cache *cache)
  2765. {
  2766. struct rb_node *node;
  2767. spin_lock(&root->fs_info->block_group_cache_lock);
  2768. node = rb_next(&cache->cache_node);
  2769. btrfs_put_block_group(cache);
  2770. if (node) {
  2771. cache = rb_entry(node, struct btrfs_block_group_cache,
  2772. cache_node);
  2773. btrfs_get_block_group(cache);
  2774. } else
  2775. cache = NULL;
  2776. spin_unlock(&root->fs_info->block_group_cache_lock);
  2777. return cache;
  2778. }
  2779. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2780. struct btrfs_trans_handle *trans,
  2781. struct btrfs_path *path)
  2782. {
  2783. struct btrfs_root *root = block_group->fs_info->tree_root;
  2784. struct inode *inode = NULL;
  2785. u64 alloc_hint = 0;
  2786. int dcs = BTRFS_DC_ERROR;
  2787. int num_pages = 0;
  2788. int retries = 0;
  2789. int ret = 0;
  2790. /*
  2791. * If this block group is smaller than 100 megs don't bother caching the
  2792. * block group.
  2793. */
  2794. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2795. spin_lock(&block_group->lock);
  2796. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2797. spin_unlock(&block_group->lock);
  2798. return 0;
  2799. }
  2800. again:
  2801. inode = lookup_free_space_inode(root, block_group, path);
  2802. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2803. ret = PTR_ERR(inode);
  2804. btrfs_release_path(path);
  2805. goto out;
  2806. }
  2807. if (IS_ERR(inode)) {
  2808. BUG_ON(retries);
  2809. retries++;
  2810. if (block_group->ro)
  2811. goto out_free;
  2812. ret = create_free_space_inode(root, trans, block_group, path);
  2813. if (ret)
  2814. goto out_free;
  2815. goto again;
  2816. }
  2817. /* We've already setup this transaction, go ahead and exit */
  2818. if (block_group->cache_generation == trans->transid &&
  2819. i_size_read(inode)) {
  2820. dcs = BTRFS_DC_SETUP;
  2821. goto out_put;
  2822. }
  2823. /*
  2824. * We want to set the generation to 0, that way if anything goes wrong
  2825. * from here on out we know not to trust this cache when we load up next
  2826. * time.
  2827. */
  2828. BTRFS_I(inode)->generation = 0;
  2829. ret = btrfs_update_inode(trans, root, inode);
  2830. WARN_ON(ret);
  2831. if (i_size_read(inode) > 0) {
  2832. ret = btrfs_check_trunc_cache_free_space(root,
  2833. &root->fs_info->global_block_rsv);
  2834. if (ret)
  2835. goto out_put;
  2836. ret = btrfs_truncate_free_space_cache(root, trans, path,
  2837. inode);
  2838. if (ret)
  2839. goto out_put;
  2840. }
  2841. spin_lock(&block_group->lock);
  2842. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2843. !btrfs_test_opt(root, SPACE_CACHE)) {
  2844. /*
  2845. * don't bother trying to write stuff out _if_
  2846. * a) we're not cached,
  2847. * b) we're with nospace_cache mount option.
  2848. */
  2849. dcs = BTRFS_DC_WRITTEN;
  2850. spin_unlock(&block_group->lock);
  2851. goto out_put;
  2852. }
  2853. spin_unlock(&block_group->lock);
  2854. /*
  2855. * Try to preallocate enough space based on how big the block group is.
  2856. * Keep in mind this has to include any pinned space which could end up
  2857. * taking up quite a bit since it's not folded into the other space
  2858. * cache.
  2859. */
  2860. num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
  2861. if (!num_pages)
  2862. num_pages = 1;
  2863. num_pages *= 16;
  2864. num_pages *= PAGE_CACHE_SIZE;
  2865. ret = btrfs_check_data_free_space(inode, num_pages);
  2866. if (ret)
  2867. goto out_put;
  2868. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2869. num_pages, num_pages,
  2870. &alloc_hint);
  2871. if (!ret)
  2872. dcs = BTRFS_DC_SETUP;
  2873. btrfs_free_reserved_data_space(inode, num_pages);
  2874. out_put:
  2875. iput(inode);
  2876. out_free:
  2877. btrfs_release_path(path);
  2878. out:
  2879. spin_lock(&block_group->lock);
  2880. if (!ret && dcs == BTRFS_DC_SETUP)
  2881. block_group->cache_generation = trans->transid;
  2882. block_group->disk_cache_state = dcs;
  2883. spin_unlock(&block_group->lock);
  2884. return ret;
  2885. }
  2886. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2887. struct btrfs_root *root)
  2888. {
  2889. struct btrfs_block_group_cache *cache;
  2890. int err = 0;
  2891. struct btrfs_path *path;
  2892. u64 last = 0;
  2893. path = btrfs_alloc_path();
  2894. if (!path)
  2895. return -ENOMEM;
  2896. again:
  2897. while (1) {
  2898. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2899. while (cache) {
  2900. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2901. break;
  2902. cache = next_block_group(root, cache);
  2903. }
  2904. if (!cache) {
  2905. if (last == 0)
  2906. break;
  2907. last = 0;
  2908. continue;
  2909. }
  2910. err = cache_save_setup(cache, trans, path);
  2911. last = cache->key.objectid + cache->key.offset;
  2912. btrfs_put_block_group(cache);
  2913. }
  2914. while (1) {
  2915. if (last == 0) {
  2916. err = btrfs_run_delayed_refs(trans, root,
  2917. (unsigned long)-1);
  2918. if (err) /* File system offline */
  2919. goto out;
  2920. }
  2921. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2922. while (cache) {
  2923. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2924. btrfs_put_block_group(cache);
  2925. goto again;
  2926. }
  2927. if (cache->dirty)
  2928. break;
  2929. cache = next_block_group(root, cache);
  2930. }
  2931. if (!cache) {
  2932. if (last == 0)
  2933. break;
  2934. last = 0;
  2935. continue;
  2936. }
  2937. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2938. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2939. cache->dirty = 0;
  2940. last = cache->key.objectid + cache->key.offset;
  2941. err = write_one_cache_group(trans, root, path, cache);
  2942. if (err) /* File system offline */
  2943. goto out;
  2944. btrfs_put_block_group(cache);
  2945. }
  2946. while (1) {
  2947. /*
  2948. * I don't think this is needed since we're just marking our
  2949. * preallocated extent as written, but just in case it can't
  2950. * hurt.
  2951. */
  2952. if (last == 0) {
  2953. err = btrfs_run_delayed_refs(trans, root,
  2954. (unsigned long)-1);
  2955. if (err) /* File system offline */
  2956. goto out;
  2957. }
  2958. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2959. while (cache) {
  2960. /*
  2961. * Really this shouldn't happen, but it could if we
  2962. * couldn't write the entire preallocated extent and
  2963. * splitting the extent resulted in a new block.
  2964. */
  2965. if (cache->dirty) {
  2966. btrfs_put_block_group(cache);
  2967. goto again;
  2968. }
  2969. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2970. break;
  2971. cache = next_block_group(root, cache);
  2972. }
  2973. if (!cache) {
  2974. if (last == 0)
  2975. break;
  2976. last = 0;
  2977. continue;
  2978. }
  2979. err = btrfs_write_out_cache(root, trans, cache, path);
  2980. /*
  2981. * If we didn't have an error then the cache state is still
  2982. * NEED_WRITE, so we can set it to WRITTEN.
  2983. */
  2984. if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2985. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2986. last = cache->key.objectid + cache->key.offset;
  2987. btrfs_put_block_group(cache);
  2988. }
  2989. out:
  2990. btrfs_free_path(path);
  2991. return err;
  2992. }
  2993. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2994. {
  2995. struct btrfs_block_group_cache *block_group;
  2996. int readonly = 0;
  2997. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2998. if (!block_group || block_group->ro)
  2999. readonly = 1;
  3000. if (block_group)
  3001. btrfs_put_block_group(block_group);
  3002. return readonly;
  3003. }
  3004. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  3005. u64 total_bytes, u64 bytes_used,
  3006. struct btrfs_space_info **space_info)
  3007. {
  3008. struct btrfs_space_info *found;
  3009. int i;
  3010. int factor;
  3011. int ret;
  3012. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3013. BTRFS_BLOCK_GROUP_RAID10))
  3014. factor = 2;
  3015. else
  3016. factor = 1;
  3017. found = __find_space_info(info, flags);
  3018. if (found) {
  3019. spin_lock(&found->lock);
  3020. found->total_bytes += total_bytes;
  3021. found->disk_total += total_bytes * factor;
  3022. found->bytes_used += bytes_used;
  3023. found->disk_used += bytes_used * factor;
  3024. found->full = 0;
  3025. spin_unlock(&found->lock);
  3026. *space_info = found;
  3027. return 0;
  3028. }
  3029. found = kzalloc(sizeof(*found), GFP_NOFS);
  3030. if (!found)
  3031. return -ENOMEM;
  3032. ret = percpu_counter_init(&found->total_bytes_pinned, 0);
  3033. if (ret) {
  3034. kfree(found);
  3035. return ret;
  3036. }
  3037. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  3038. INIT_LIST_HEAD(&found->block_groups[i]);
  3039. init_rwsem(&found->groups_sem);
  3040. spin_lock_init(&found->lock);
  3041. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3042. found->total_bytes = total_bytes;
  3043. found->disk_total = total_bytes * factor;
  3044. found->bytes_used = bytes_used;
  3045. found->disk_used = bytes_used * factor;
  3046. found->bytes_pinned = 0;
  3047. found->bytes_reserved = 0;
  3048. found->bytes_readonly = 0;
  3049. found->bytes_may_use = 0;
  3050. found->full = 0;
  3051. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3052. found->chunk_alloc = 0;
  3053. found->flush = 0;
  3054. init_waitqueue_head(&found->wait);
  3055. *space_info = found;
  3056. list_add_rcu(&found->list, &info->space_info);
  3057. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3058. info->data_sinfo = found;
  3059. return 0;
  3060. }
  3061. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3062. {
  3063. u64 extra_flags = chunk_to_extended(flags) &
  3064. BTRFS_EXTENDED_PROFILE_MASK;
  3065. write_seqlock(&fs_info->profiles_lock);
  3066. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3067. fs_info->avail_data_alloc_bits |= extra_flags;
  3068. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3069. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3070. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3071. fs_info->avail_system_alloc_bits |= extra_flags;
  3072. write_sequnlock(&fs_info->profiles_lock);
  3073. }
  3074. /*
  3075. * returns target flags in extended format or 0 if restripe for this
  3076. * chunk_type is not in progress
  3077. *
  3078. * should be called with either volume_mutex or balance_lock held
  3079. */
  3080. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3081. {
  3082. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3083. u64 target = 0;
  3084. if (!bctl)
  3085. return 0;
  3086. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3087. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3088. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3089. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3090. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3091. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3092. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3093. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3094. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3095. }
  3096. return target;
  3097. }
  3098. /*
  3099. * @flags: available profiles in extended format (see ctree.h)
  3100. *
  3101. * Returns reduced profile in chunk format. If profile changing is in
  3102. * progress (either running or paused) picks the target profile (if it's
  3103. * already available), otherwise falls back to plain reducing.
  3104. */
  3105. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3106. {
  3107. /*
  3108. * we add in the count of missing devices because we want
  3109. * to make sure that any RAID levels on a degraded FS
  3110. * continue to be honored.
  3111. */
  3112. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  3113. root->fs_info->fs_devices->missing_devices;
  3114. u64 target;
  3115. u64 tmp;
  3116. /*
  3117. * see if restripe for this chunk_type is in progress, if so
  3118. * try to reduce to the target profile
  3119. */
  3120. spin_lock(&root->fs_info->balance_lock);
  3121. target = get_restripe_target(root->fs_info, flags);
  3122. if (target) {
  3123. /* pick target profile only if it's already available */
  3124. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3125. spin_unlock(&root->fs_info->balance_lock);
  3126. return extended_to_chunk(target);
  3127. }
  3128. }
  3129. spin_unlock(&root->fs_info->balance_lock);
  3130. /* First, mask out the RAID levels which aren't possible */
  3131. if (num_devices == 1)
  3132. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3133. BTRFS_BLOCK_GROUP_RAID5);
  3134. if (num_devices < 3)
  3135. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3136. if (num_devices < 4)
  3137. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3138. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3139. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3140. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3141. flags &= ~tmp;
  3142. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3143. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3144. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3145. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3146. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3147. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3148. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3149. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3150. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3151. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3152. return extended_to_chunk(flags | tmp);
  3153. }
  3154. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  3155. {
  3156. unsigned seq;
  3157. do {
  3158. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3159. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3160. flags |= root->fs_info->avail_data_alloc_bits;
  3161. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3162. flags |= root->fs_info->avail_system_alloc_bits;
  3163. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3164. flags |= root->fs_info->avail_metadata_alloc_bits;
  3165. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3166. return btrfs_reduce_alloc_profile(root, flags);
  3167. }
  3168. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3169. {
  3170. u64 flags;
  3171. u64 ret;
  3172. if (data)
  3173. flags = BTRFS_BLOCK_GROUP_DATA;
  3174. else if (root == root->fs_info->chunk_root)
  3175. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3176. else
  3177. flags = BTRFS_BLOCK_GROUP_METADATA;
  3178. ret = get_alloc_profile(root, flags);
  3179. return ret;
  3180. }
  3181. /*
  3182. * This will check the space that the inode allocates from to make sure we have
  3183. * enough space for bytes.
  3184. */
  3185. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  3186. {
  3187. struct btrfs_space_info *data_sinfo;
  3188. struct btrfs_root *root = BTRFS_I(inode)->root;
  3189. struct btrfs_fs_info *fs_info = root->fs_info;
  3190. u64 used;
  3191. int ret = 0, committed = 0, alloc_chunk = 1;
  3192. /* make sure bytes are sectorsize aligned */
  3193. bytes = ALIGN(bytes, root->sectorsize);
  3194. if (root == root->fs_info->tree_root ||
  3195. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  3196. alloc_chunk = 0;
  3197. committed = 1;
  3198. }
  3199. data_sinfo = fs_info->data_sinfo;
  3200. if (!data_sinfo)
  3201. goto alloc;
  3202. again:
  3203. /* make sure we have enough space to handle the data first */
  3204. spin_lock(&data_sinfo->lock);
  3205. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3206. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3207. data_sinfo->bytes_may_use;
  3208. if (used + bytes > data_sinfo->total_bytes) {
  3209. struct btrfs_trans_handle *trans;
  3210. /*
  3211. * if we don't have enough free bytes in this space then we need
  3212. * to alloc a new chunk.
  3213. */
  3214. if (!data_sinfo->full && alloc_chunk) {
  3215. u64 alloc_target;
  3216. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3217. spin_unlock(&data_sinfo->lock);
  3218. alloc:
  3219. alloc_target = btrfs_get_alloc_profile(root, 1);
  3220. trans = btrfs_join_transaction(root);
  3221. if (IS_ERR(trans))
  3222. return PTR_ERR(trans);
  3223. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3224. alloc_target,
  3225. CHUNK_ALLOC_NO_FORCE);
  3226. btrfs_end_transaction(trans, root);
  3227. if (ret < 0) {
  3228. if (ret != -ENOSPC)
  3229. return ret;
  3230. else
  3231. goto commit_trans;
  3232. }
  3233. if (!data_sinfo)
  3234. data_sinfo = fs_info->data_sinfo;
  3235. goto again;
  3236. }
  3237. /*
  3238. * If we don't have enough pinned space to deal with this
  3239. * allocation don't bother committing the transaction.
  3240. */
  3241. if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
  3242. bytes) < 0)
  3243. committed = 1;
  3244. spin_unlock(&data_sinfo->lock);
  3245. /* commit the current transaction and try again */
  3246. commit_trans:
  3247. if (!committed &&
  3248. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3249. committed = 1;
  3250. trans = btrfs_join_transaction(root);
  3251. if (IS_ERR(trans))
  3252. return PTR_ERR(trans);
  3253. ret = btrfs_commit_transaction(trans, root);
  3254. if (ret)
  3255. return ret;
  3256. goto again;
  3257. }
  3258. return -ENOSPC;
  3259. }
  3260. data_sinfo->bytes_may_use += bytes;
  3261. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3262. data_sinfo->flags, bytes, 1);
  3263. spin_unlock(&data_sinfo->lock);
  3264. return 0;
  3265. }
  3266. /*
  3267. * Called if we need to clear a data reservation for this inode.
  3268. */
  3269. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3270. {
  3271. struct btrfs_root *root = BTRFS_I(inode)->root;
  3272. struct btrfs_space_info *data_sinfo;
  3273. /* make sure bytes are sectorsize aligned */
  3274. bytes = ALIGN(bytes, root->sectorsize);
  3275. data_sinfo = root->fs_info->data_sinfo;
  3276. spin_lock(&data_sinfo->lock);
  3277. WARN_ON(data_sinfo->bytes_may_use < bytes);
  3278. data_sinfo->bytes_may_use -= bytes;
  3279. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3280. data_sinfo->flags, bytes, 0);
  3281. spin_unlock(&data_sinfo->lock);
  3282. }
  3283. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3284. {
  3285. struct list_head *head = &info->space_info;
  3286. struct btrfs_space_info *found;
  3287. rcu_read_lock();
  3288. list_for_each_entry_rcu(found, head, list) {
  3289. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3290. found->force_alloc = CHUNK_ALLOC_FORCE;
  3291. }
  3292. rcu_read_unlock();
  3293. }
  3294. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3295. {
  3296. return (global->size << 1);
  3297. }
  3298. static int should_alloc_chunk(struct btrfs_root *root,
  3299. struct btrfs_space_info *sinfo, int force)
  3300. {
  3301. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3302. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3303. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3304. u64 thresh;
  3305. if (force == CHUNK_ALLOC_FORCE)
  3306. return 1;
  3307. /*
  3308. * We need to take into account the global rsv because for all intents
  3309. * and purposes it's used space. Don't worry about locking the
  3310. * global_rsv, it doesn't change except when the transaction commits.
  3311. */
  3312. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3313. num_allocated += calc_global_rsv_need_space(global_rsv);
  3314. /*
  3315. * in limited mode, we want to have some free space up to
  3316. * about 1% of the FS size.
  3317. */
  3318. if (force == CHUNK_ALLOC_LIMITED) {
  3319. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3320. thresh = max_t(u64, 64 * 1024 * 1024,
  3321. div_factor_fine(thresh, 1));
  3322. if (num_bytes - num_allocated < thresh)
  3323. return 1;
  3324. }
  3325. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3326. return 0;
  3327. return 1;
  3328. }
  3329. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3330. {
  3331. u64 num_dev;
  3332. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3333. BTRFS_BLOCK_GROUP_RAID0 |
  3334. BTRFS_BLOCK_GROUP_RAID5 |
  3335. BTRFS_BLOCK_GROUP_RAID6))
  3336. num_dev = root->fs_info->fs_devices->rw_devices;
  3337. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3338. num_dev = 2;
  3339. else
  3340. num_dev = 1; /* DUP or single */
  3341. /* metadata for updaing devices and chunk tree */
  3342. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3343. }
  3344. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3345. struct btrfs_root *root, u64 type)
  3346. {
  3347. struct btrfs_space_info *info;
  3348. u64 left;
  3349. u64 thresh;
  3350. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3351. spin_lock(&info->lock);
  3352. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3353. info->bytes_reserved - info->bytes_readonly;
  3354. spin_unlock(&info->lock);
  3355. thresh = get_system_chunk_thresh(root, type);
  3356. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3357. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3358. left, thresh, type);
  3359. dump_space_info(info, 0, 0);
  3360. }
  3361. if (left < thresh) {
  3362. u64 flags;
  3363. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3364. btrfs_alloc_chunk(trans, root, flags);
  3365. }
  3366. }
  3367. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3368. struct btrfs_root *extent_root, u64 flags, int force)
  3369. {
  3370. struct btrfs_space_info *space_info;
  3371. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3372. int wait_for_alloc = 0;
  3373. int ret = 0;
  3374. /* Don't re-enter if we're already allocating a chunk */
  3375. if (trans->allocating_chunk)
  3376. return -ENOSPC;
  3377. space_info = __find_space_info(extent_root->fs_info, flags);
  3378. if (!space_info) {
  3379. ret = update_space_info(extent_root->fs_info, flags,
  3380. 0, 0, &space_info);
  3381. BUG_ON(ret); /* -ENOMEM */
  3382. }
  3383. BUG_ON(!space_info); /* Logic error */
  3384. again:
  3385. spin_lock(&space_info->lock);
  3386. if (force < space_info->force_alloc)
  3387. force = space_info->force_alloc;
  3388. if (space_info->full) {
  3389. if (should_alloc_chunk(extent_root, space_info, force))
  3390. ret = -ENOSPC;
  3391. else
  3392. ret = 0;
  3393. spin_unlock(&space_info->lock);
  3394. return ret;
  3395. }
  3396. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3397. spin_unlock(&space_info->lock);
  3398. return 0;
  3399. } else if (space_info->chunk_alloc) {
  3400. wait_for_alloc = 1;
  3401. } else {
  3402. space_info->chunk_alloc = 1;
  3403. }
  3404. spin_unlock(&space_info->lock);
  3405. mutex_lock(&fs_info->chunk_mutex);
  3406. /*
  3407. * The chunk_mutex is held throughout the entirety of a chunk
  3408. * allocation, so once we've acquired the chunk_mutex we know that the
  3409. * other guy is done and we need to recheck and see if we should
  3410. * allocate.
  3411. */
  3412. if (wait_for_alloc) {
  3413. mutex_unlock(&fs_info->chunk_mutex);
  3414. wait_for_alloc = 0;
  3415. goto again;
  3416. }
  3417. trans->allocating_chunk = true;
  3418. /*
  3419. * If we have mixed data/metadata chunks we want to make sure we keep
  3420. * allocating mixed chunks instead of individual chunks.
  3421. */
  3422. if (btrfs_mixed_space_info(space_info))
  3423. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3424. /*
  3425. * if we're doing a data chunk, go ahead and make sure that
  3426. * we keep a reasonable number of metadata chunks allocated in the
  3427. * FS as well.
  3428. */
  3429. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3430. fs_info->data_chunk_allocations++;
  3431. if (!(fs_info->data_chunk_allocations %
  3432. fs_info->metadata_ratio))
  3433. force_metadata_allocation(fs_info);
  3434. }
  3435. /*
  3436. * Check if we have enough space in SYSTEM chunk because we may need
  3437. * to update devices.
  3438. */
  3439. check_system_chunk(trans, extent_root, flags);
  3440. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3441. trans->allocating_chunk = false;
  3442. spin_lock(&space_info->lock);
  3443. if (ret < 0 && ret != -ENOSPC)
  3444. goto out;
  3445. if (ret)
  3446. space_info->full = 1;
  3447. else
  3448. ret = 1;
  3449. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3450. out:
  3451. space_info->chunk_alloc = 0;
  3452. spin_unlock(&space_info->lock);
  3453. mutex_unlock(&fs_info->chunk_mutex);
  3454. return ret;
  3455. }
  3456. static int can_overcommit(struct btrfs_root *root,
  3457. struct btrfs_space_info *space_info, u64 bytes,
  3458. enum btrfs_reserve_flush_enum flush)
  3459. {
  3460. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3461. u64 profile = btrfs_get_alloc_profile(root, 0);
  3462. u64 space_size;
  3463. u64 avail;
  3464. u64 used;
  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. /*
  3493. * If we aren't flushing all things, let us overcommit up to
  3494. * 1/2th of the space. If we can flush, don't let us overcommit
  3495. * too much, let it overcommit up to 1/8 of the space.
  3496. */
  3497. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3498. avail >>= 3;
  3499. else
  3500. avail >>= 1;
  3501. if (used + bytes < space_info->total_bytes + avail)
  3502. return 1;
  3503. return 0;
  3504. }
  3505. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3506. unsigned long nr_pages)
  3507. {
  3508. struct super_block *sb = root->fs_info->sb;
  3509. if (down_read_trylock(&sb->s_umount)) {
  3510. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  3511. up_read(&sb->s_umount);
  3512. } else {
  3513. /*
  3514. * We needn't worry the filesystem going from r/w to r/o though
  3515. * we don't acquire ->s_umount mutex, because the filesystem
  3516. * should guarantee the delalloc inodes list be empty after
  3517. * the filesystem is readonly(all dirty pages are written to
  3518. * the disk).
  3519. */
  3520. btrfs_start_all_delalloc_inodes(root->fs_info, 0);
  3521. if (!current->journal_info)
  3522. btrfs_wait_all_ordered_extents(root->fs_info);
  3523. }
  3524. }
  3525. /*
  3526. * shrink metadata reservation for delalloc
  3527. */
  3528. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3529. bool wait_ordered)
  3530. {
  3531. struct btrfs_block_rsv *block_rsv;
  3532. struct btrfs_space_info *space_info;
  3533. struct btrfs_trans_handle *trans;
  3534. u64 delalloc_bytes;
  3535. u64 max_reclaim;
  3536. long time_left;
  3537. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3538. int loops = 0;
  3539. enum btrfs_reserve_flush_enum flush;
  3540. trans = (struct btrfs_trans_handle *)current->journal_info;
  3541. block_rsv = &root->fs_info->delalloc_block_rsv;
  3542. space_info = block_rsv->space_info;
  3543. smp_mb();
  3544. delalloc_bytes = percpu_counter_sum_positive(
  3545. &root->fs_info->delalloc_bytes);
  3546. if (delalloc_bytes == 0) {
  3547. if (trans)
  3548. return;
  3549. btrfs_wait_all_ordered_extents(root->fs_info);
  3550. return;
  3551. }
  3552. while (delalloc_bytes && loops < 3) {
  3553. max_reclaim = min(delalloc_bytes, to_reclaim);
  3554. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3555. btrfs_writeback_inodes_sb_nr(root, nr_pages);
  3556. /*
  3557. * We need to wait for the async pages to actually start before
  3558. * we do anything.
  3559. */
  3560. wait_event(root->fs_info->async_submit_wait,
  3561. !atomic_read(&root->fs_info->async_delalloc_pages));
  3562. if (!trans)
  3563. flush = BTRFS_RESERVE_FLUSH_ALL;
  3564. else
  3565. flush = BTRFS_RESERVE_NO_FLUSH;
  3566. spin_lock(&space_info->lock);
  3567. if (can_overcommit(root, space_info, orig, flush)) {
  3568. spin_unlock(&space_info->lock);
  3569. break;
  3570. }
  3571. spin_unlock(&space_info->lock);
  3572. loops++;
  3573. if (wait_ordered && !trans) {
  3574. btrfs_wait_all_ordered_extents(root->fs_info);
  3575. } else {
  3576. time_left = schedule_timeout_killable(1);
  3577. if (time_left)
  3578. break;
  3579. }
  3580. smp_mb();
  3581. delalloc_bytes = percpu_counter_sum_positive(
  3582. &root->fs_info->delalloc_bytes);
  3583. }
  3584. }
  3585. /**
  3586. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3587. * @root - the root we're allocating for
  3588. * @bytes - the number of bytes we want to reserve
  3589. * @force - force the commit
  3590. *
  3591. * This will check to make sure that committing the transaction will actually
  3592. * get us somewhere and then commit the transaction if it does. Otherwise it
  3593. * will return -ENOSPC.
  3594. */
  3595. static int may_commit_transaction(struct btrfs_root *root,
  3596. struct btrfs_space_info *space_info,
  3597. u64 bytes, int force)
  3598. {
  3599. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3600. struct btrfs_trans_handle *trans;
  3601. trans = (struct btrfs_trans_handle *)current->journal_info;
  3602. if (trans)
  3603. return -EAGAIN;
  3604. if (force)
  3605. goto commit;
  3606. /* See if there is enough pinned space to make this reservation */
  3607. spin_lock(&space_info->lock);
  3608. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3609. bytes) >= 0) {
  3610. spin_unlock(&space_info->lock);
  3611. goto commit;
  3612. }
  3613. spin_unlock(&space_info->lock);
  3614. /*
  3615. * See if there is some space in the delayed insertion reservation for
  3616. * this reservation.
  3617. */
  3618. if (space_info != delayed_rsv->space_info)
  3619. return -ENOSPC;
  3620. spin_lock(&space_info->lock);
  3621. spin_lock(&delayed_rsv->lock);
  3622. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3623. bytes - delayed_rsv->size) >= 0) {
  3624. spin_unlock(&delayed_rsv->lock);
  3625. spin_unlock(&space_info->lock);
  3626. return -ENOSPC;
  3627. }
  3628. spin_unlock(&delayed_rsv->lock);
  3629. spin_unlock(&space_info->lock);
  3630. commit:
  3631. trans = btrfs_join_transaction(root);
  3632. if (IS_ERR(trans))
  3633. return -ENOSPC;
  3634. return btrfs_commit_transaction(trans, root);
  3635. }
  3636. enum flush_state {
  3637. FLUSH_DELAYED_ITEMS_NR = 1,
  3638. FLUSH_DELAYED_ITEMS = 2,
  3639. FLUSH_DELALLOC = 3,
  3640. FLUSH_DELALLOC_WAIT = 4,
  3641. ALLOC_CHUNK = 5,
  3642. COMMIT_TRANS = 6,
  3643. };
  3644. static int flush_space(struct btrfs_root *root,
  3645. struct btrfs_space_info *space_info, u64 num_bytes,
  3646. u64 orig_bytes, int state)
  3647. {
  3648. struct btrfs_trans_handle *trans;
  3649. int nr;
  3650. int ret = 0;
  3651. switch (state) {
  3652. case FLUSH_DELAYED_ITEMS_NR:
  3653. case FLUSH_DELAYED_ITEMS:
  3654. if (state == FLUSH_DELAYED_ITEMS_NR) {
  3655. u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
  3656. nr = (int)div64_u64(num_bytes, bytes);
  3657. if (!nr)
  3658. nr = 1;
  3659. nr *= 2;
  3660. } else {
  3661. nr = -1;
  3662. }
  3663. trans = btrfs_join_transaction(root);
  3664. if (IS_ERR(trans)) {
  3665. ret = PTR_ERR(trans);
  3666. break;
  3667. }
  3668. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3669. btrfs_end_transaction(trans, root);
  3670. break;
  3671. case FLUSH_DELALLOC:
  3672. case FLUSH_DELALLOC_WAIT:
  3673. shrink_delalloc(root, num_bytes, orig_bytes,
  3674. state == FLUSH_DELALLOC_WAIT);
  3675. break;
  3676. case ALLOC_CHUNK:
  3677. trans = btrfs_join_transaction(root);
  3678. if (IS_ERR(trans)) {
  3679. ret = PTR_ERR(trans);
  3680. break;
  3681. }
  3682. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3683. btrfs_get_alloc_profile(root, 0),
  3684. CHUNK_ALLOC_NO_FORCE);
  3685. btrfs_end_transaction(trans, root);
  3686. if (ret == -ENOSPC)
  3687. ret = 0;
  3688. break;
  3689. case COMMIT_TRANS:
  3690. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3691. break;
  3692. default:
  3693. ret = -ENOSPC;
  3694. break;
  3695. }
  3696. return ret;
  3697. }
  3698. /**
  3699. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3700. * @root - the root we're allocating for
  3701. * @block_rsv - the block_rsv we're allocating for
  3702. * @orig_bytes - the number of bytes we want
  3703. * @flush - whether or not we can flush to make our reservation
  3704. *
  3705. * This will reserve orgi_bytes number of bytes from the space info associated
  3706. * with the block_rsv. If there is not enough space it will make an attempt to
  3707. * flush out space to make room. It will do this by flushing delalloc if
  3708. * possible or committing the transaction. If flush is 0 then no attempts to
  3709. * regain reservations will be made and this will fail if there is not enough
  3710. * space already.
  3711. */
  3712. static int reserve_metadata_bytes(struct btrfs_root *root,
  3713. struct btrfs_block_rsv *block_rsv,
  3714. u64 orig_bytes,
  3715. enum btrfs_reserve_flush_enum flush)
  3716. {
  3717. struct btrfs_space_info *space_info = block_rsv->space_info;
  3718. u64 used;
  3719. u64 num_bytes = orig_bytes;
  3720. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  3721. int ret = 0;
  3722. bool flushing = false;
  3723. again:
  3724. ret = 0;
  3725. spin_lock(&space_info->lock);
  3726. /*
  3727. * We only want to wait if somebody other than us is flushing and we
  3728. * are actually allowed to flush all things.
  3729. */
  3730. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  3731. space_info->flush) {
  3732. spin_unlock(&space_info->lock);
  3733. /*
  3734. * If we have a trans handle we can't wait because the flusher
  3735. * may have to commit the transaction, which would mean we would
  3736. * deadlock since we are waiting for the flusher to finish, but
  3737. * hold the current transaction open.
  3738. */
  3739. if (current->journal_info)
  3740. return -EAGAIN;
  3741. ret = wait_event_killable(space_info->wait, !space_info->flush);
  3742. /* Must have been killed, return */
  3743. if (ret)
  3744. return -EINTR;
  3745. spin_lock(&space_info->lock);
  3746. }
  3747. ret = -ENOSPC;
  3748. used = space_info->bytes_used + space_info->bytes_reserved +
  3749. space_info->bytes_pinned + space_info->bytes_readonly +
  3750. space_info->bytes_may_use;
  3751. /*
  3752. * The idea here is that we've not already over-reserved the block group
  3753. * then we can go ahead and save our reservation first and then start
  3754. * flushing if we need to. Otherwise if we've already overcommitted
  3755. * lets start flushing stuff first and then come back and try to make
  3756. * our reservation.
  3757. */
  3758. if (used <= space_info->total_bytes) {
  3759. if (used + orig_bytes <= space_info->total_bytes) {
  3760. space_info->bytes_may_use += orig_bytes;
  3761. trace_btrfs_space_reservation(root->fs_info,
  3762. "space_info", space_info->flags, orig_bytes, 1);
  3763. ret = 0;
  3764. } else {
  3765. /*
  3766. * Ok set num_bytes to orig_bytes since we aren't
  3767. * overocmmitted, this way we only try and reclaim what
  3768. * we need.
  3769. */
  3770. num_bytes = orig_bytes;
  3771. }
  3772. } else {
  3773. /*
  3774. * Ok we're over committed, set num_bytes to the overcommitted
  3775. * amount plus the amount of bytes that we need for this
  3776. * reservation.
  3777. */
  3778. num_bytes = used - space_info->total_bytes +
  3779. (orig_bytes * 2);
  3780. }
  3781. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  3782. space_info->bytes_may_use += orig_bytes;
  3783. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3784. space_info->flags, orig_bytes,
  3785. 1);
  3786. ret = 0;
  3787. }
  3788. /*
  3789. * Couldn't make our reservation, save our place so while we're trying
  3790. * to reclaim space we can actually use it instead of somebody else
  3791. * stealing it from us.
  3792. *
  3793. * We make the other tasks wait for the flush only when we can flush
  3794. * all things.
  3795. */
  3796. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  3797. flushing = true;
  3798. space_info->flush = 1;
  3799. }
  3800. spin_unlock(&space_info->lock);
  3801. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  3802. goto out;
  3803. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  3804. flush_state);
  3805. flush_state++;
  3806. /*
  3807. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  3808. * would happen. So skip delalloc flush.
  3809. */
  3810. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3811. (flush_state == FLUSH_DELALLOC ||
  3812. flush_state == FLUSH_DELALLOC_WAIT))
  3813. flush_state = ALLOC_CHUNK;
  3814. if (!ret)
  3815. goto again;
  3816. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3817. flush_state < COMMIT_TRANS)
  3818. goto again;
  3819. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  3820. flush_state <= COMMIT_TRANS)
  3821. goto again;
  3822. out:
  3823. if (ret == -ENOSPC &&
  3824. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  3825. struct btrfs_block_rsv *global_rsv =
  3826. &root->fs_info->global_block_rsv;
  3827. if (block_rsv != global_rsv &&
  3828. !block_rsv_use_bytes(global_rsv, orig_bytes))
  3829. ret = 0;
  3830. }
  3831. if (flushing) {
  3832. spin_lock(&space_info->lock);
  3833. space_info->flush = 0;
  3834. wake_up_all(&space_info->wait);
  3835. spin_unlock(&space_info->lock);
  3836. }
  3837. return ret;
  3838. }
  3839. static struct btrfs_block_rsv *get_block_rsv(
  3840. const struct btrfs_trans_handle *trans,
  3841. const struct btrfs_root *root)
  3842. {
  3843. struct btrfs_block_rsv *block_rsv = NULL;
  3844. if (root->ref_cows)
  3845. block_rsv = trans->block_rsv;
  3846. if (root == root->fs_info->csum_root && trans->adding_csums)
  3847. block_rsv = trans->block_rsv;
  3848. if (root == root->fs_info->uuid_root)
  3849. block_rsv = trans->block_rsv;
  3850. if (!block_rsv)
  3851. block_rsv = root->block_rsv;
  3852. if (!block_rsv)
  3853. block_rsv = &root->fs_info->empty_block_rsv;
  3854. return block_rsv;
  3855. }
  3856. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3857. u64 num_bytes)
  3858. {
  3859. int ret = -ENOSPC;
  3860. spin_lock(&block_rsv->lock);
  3861. if (block_rsv->reserved >= num_bytes) {
  3862. block_rsv->reserved -= num_bytes;
  3863. if (block_rsv->reserved < block_rsv->size)
  3864. block_rsv->full = 0;
  3865. ret = 0;
  3866. }
  3867. spin_unlock(&block_rsv->lock);
  3868. return ret;
  3869. }
  3870. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3871. u64 num_bytes, int update_size)
  3872. {
  3873. spin_lock(&block_rsv->lock);
  3874. block_rsv->reserved += num_bytes;
  3875. if (update_size)
  3876. block_rsv->size += num_bytes;
  3877. else if (block_rsv->reserved >= block_rsv->size)
  3878. block_rsv->full = 1;
  3879. spin_unlock(&block_rsv->lock);
  3880. }
  3881. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  3882. struct btrfs_block_rsv *dest, u64 num_bytes,
  3883. int min_factor)
  3884. {
  3885. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  3886. u64 min_bytes;
  3887. if (global_rsv->space_info != dest->space_info)
  3888. return -ENOSPC;
  3889. spin_lock(&global_rsv->lock);
  3890. min_bytes = div_factor(global_rsv->size, min_factor);
  3891. if (global_rsv->reserved < min_bytes + num_bytes) {
  3892. spin_unlock(&global_rsv->lock);
  3893. return -ENOSPC;
  3894. }
  3895. global_rsv->reserved -= num_bytes;
  3896. if (global_rsv->reserved < global_rsv->size)
  3897. global_rsv->full = 0;
  3898. spin_unlock(&global_rsv->lock);
  3899. block_rsv_add_bytes(dest, num_bytes, 1);
  3900. return 0;
  3901. }
  3902. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3903. struct btrfs_block_rsv *block_rsv,
  3904. struct btrfs_block_rsv *dest, u64 num_bytes)
  3905. {
  3906. struct btrfs_space_info *space_info = block_rsv->space_info;
  3907. spin_lock(&block_rsv->lock);
  3908. if (num_bytes == (u64)-1)
  3909. num_bytes = block_rsv->size;
  3910. block_rsv->size -= num_bytes;
  3911. if (block_rsv->reserved >= block_rsv->size) {
  3912. num_bytes = block_rsv->reserved - block_rsv->size;
  3913. block_rsv->reserved = block_rsv->size;
  3914. block_rsv->full = 1;
  3915. } else {
  3916. num_bytes = 0;
  3917. }
  3918. spin_unlock(&block_rsv->lock);
  3919. if (num_bytes > 0) {
  3920. if (dest) {
  3921. spin_lock(&dest->lock);
  3922. if (!dest->full) {
  3923. u64 bytes_to_add;
  3924. bytes_to_add = dest->size - dest->reserved;
  3925. bytes_to_add = min(num_bytes, bytes_to_add);
  3926. dest->reserved += bytes_to_add;
  3927. if (dest->reserved >= dest->size)
  3928. dest->full = 1;
  3929. num_bytes -= bytes_to_add;
  3930. }
  3931. spin_unlock(&dest->lock);
  3932. }
  3933. if (num_bytes) {
  3934. spin_lock(&space_info->lock);
  3935. space_info->bytes_may_use -= num_bytes;
  3936. trace_btrfs_space_reservation(fs_info, "space_info",
  3937. space_info->flags, num_bytes, 0);
  3938. spin_unlock(&space_info->lock);
  3939. }
  3940. }
  3941. }
  3942. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3943. struct btrfs_block_rsv *dst, u64 num_bytes)
  3944. {
  3945. int ret;
  3946. ret = block_rsv_use_bytes(src, num_bytes);
  3947. if (ret)
  3948. return ret;
  3949. block_rsv_add_bytes(dst, num_bytes, 1);
  3950. return 0;
  3951. }
  3952. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  3953. {
  3954. memset(rsv, 0, sizeof(*rsv));
  3955. spin_lock_init(&rsv->lock);
  3956. rsv->type = type;
  3957. }
  3958. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  3959. unsigned short type)
  3960. {
  3961. struct btrfs_block_rsv *block_rsv;
  3962. struct btrfs_fs_info *fs_info = root->fs_info;
  3963. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3964. if (!block_rsv)
  3965. return NULL;
  3966. btrfs_init_block_rsv(block_rsv, type);
  3967. block_rsv->space_info = __find_space_info(fs_info,
  3968. BTRFS_BLOCK_GROUP_METADATA);
  3969. return block_rsv;
  3970. }
  3971. void btrfs_free_block_rsv(struct btrfs_root *root,
  3972. struct btrfs_block_rsv *rsv)
  3973. {
  3974. if (!rsv)
  3975. return;
  3976. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3977. kfree(rsv);
  3978. }
  3979. int btrfs_block_rsv_add(struct btrfs_root *root,
  3980. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  3981. enum btrfs_reserve_flush_enum flush)
  3982. {
  3983. int ret;
  3984. if (num_bytes == 0)
  3985. return 0;
  3986. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3987. if (!ret) {
  3988. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3989. return 0;
  3990. }
  3991. return ret;
  3992. }
  3993. int btrfs_block_rsv_check(struct btrfs_root *root,
  3994. struct btrfs_block_rsv *block_rsv, int min_factor)
  3995. {
  3996. u64 num_bytes = 0;
  3997. int ret = -ENOSPC;
  3998. if (!block_rsv)
  3999. return 0;
  4000. spin_lock(&block_rsv->lock);
  4001. num_bytes = div_factor(block_rsv->size, min_factor);
  4002. if (block_rsv->reserved >= num_bytes)
  4003. ret = 0;
  4004. spin_unlock(&block_rsv->lock);
  4005. return ret;
  4006. }
  4007. int btrfs_block_rsv_refill(struct btrfs_root *root,
  4008. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  4009. enum btrfs_reserve_flush_enum flush)
  4010. {
  4011. u64 num_bytes = 0;
  4012. int ret = -ENOSPC;
  4013. if (!block_rsv)
  4014. return 0;
  4015. spin_lock(&block_rsv->lock);
  4016. num_bytes = min_reserved;
  4017. if (block_rsv->reserved >= num_bytes)
  4018. ret = 0;
  4019. else
  4020. num_bytes -= block_rsv->reserved;
  4021. spin_unlock(&block_rsv->lock);
  4022. if (!ret)
  4023. return 0;
  4024. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4025. if (!ret) {
  4026. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  4027. return 0;
  4028. }
  4029. return ret;
  4030. }
  4031. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  4032. struct btrfs_block_rsv *dst_rsv,
  4033. u64 num_bytes)
  4034. {
  4035. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4036. }
  4037. void btrfs_block_rsv_release(struct btrfs_root *root,
  4038. struct btrfs_block_rsv *block_rsv,
  4039. u64 num_bytes)
  4040. {
  4041. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4042. if (global_rsv->full || global_rsv == block_rsv ||
  4043. block_rsv->space_info != global_rsv->space_info)
  4044. global_rsv = NULL;
  4045. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4046. num_bytes);
  4047. }
  4048. /*
  4049. * helper to calculate size of global block reservation.
  4050. * the desired value is sum of space used by extent tree,
  4051. * checksum tree and root tree
  4052. */
  4053. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4054. {
  4055. struct btrfs_space_info *sinfo;
  4056. u64 num_bytes;
  4057. u64 meta_used;
  4058. u64 data_used;
  4059. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4060. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4061. spin_lock(&sinfo->lock);
  4062. data_used = sinfo->bytes_used;
  4063. spin_unlock(&sinfo->lock);
  4064. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4065. spin_lock(&sinfo->lock);
  4066. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4067. data_used = 0;
  4068. meta_used = sinfo->bytes_used;
  4069. spin_unlock(&sinfo->lock);
  4070. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4071. csum_size * 2;
  4072. num_bytes += div64_u64(data_used + meta_used, 50);
  4073. if (num_bytes * 3 > meta_used)
  4074. num_bytes = div64_u64(meta_used, 3);
  4075. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  4076. }
  4077. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4078. {
  4079. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4080. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4081. u64 num_bytes;
  4082. num_bytes = calc_global_metadata_size(fs_info);
  4083. spin_lock(&sinfo->lock);
  4084. spin_lock(&block_rsv->lock);
  4085. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4086. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4087. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4088. sinfo->bytes_may_use;
  4089. if (sinfo->total_bytes > num_bytes) {
  4090. num_bytes = sinfo->total_bytes - num_bytes;
  4091. block_rsv->reserved += num_bytes;
  4092. sinfo->bytes_may_use += num_bytes;
  4093. trace_btrfs_space_reservation(fs_info, "space_info",
  4094. sinfo->flags, num_bytes, 1);
  4095. }
  4096. if (block_rsv->reserved >= block_rsv->size) {
  4097. num_bytes = block_rsv->reserved - block_rsv->size;
  4098. sinfo->bytes_may_use -= num_bytes;
  4099. trace_btrfs_space_reservation(fs_info, "space_info",
  4100. sinfo->flags, num_bytes, 0);
  4101. block_rsv->reserved = block_rsv->size;
  4102. block_rsv->full = 1;
  4103. }
  4104. spin_unlock(&block_rsv->lock);
  4105. spin_unlock(&sinfo->lock);
  4106. }
  4107. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4108. {
  4109. struct btrfs_space_info *space_info;
  4110. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4111. fs_info->chunk_block_rsv.space_info = space_info;
  4112. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4113. fs_info->global_block_rsv.space_info = space_info;
  4114. fs_info->delalloc_block_rsv.space_info = space_info;
  4115. fs_info->trans_block_rsv.space_info = space_info;
  4116. fs_info->empty_block_rsv.space_info = space_info;
  4117. fs_info->delayed_block_rsv.space_info = space_info;
  4118. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4119. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4120. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4121. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4122. if (fs_info->quota_root)
  4123. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4124. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4125. update_global_block_rsv(fs_info);
  4126. }
  4127. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4128. {
  4129. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4130. (u64)-1);
  4131. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4132. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4133. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4134. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4135. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4136. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4137. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4138. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4139. }
  4140. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4141. struct btrfs_root *root)
  4142. {
  4143. if (!trans->block_rsv)
  4144. return;
  4145. if (!trans->bytes_reserved)
  4146. return;
  4147. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4148. trans->transid, trans->bytes_reserved, 0);
  4149. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4150. trans->bytes_reserved = 0;
  4151. }
  4152. /* Can only return 0 or -ENOSPC */
  4153. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4154. struct inode *inode)
  4155. {
  4156. struct btrfs_root *root = BTRFS_I(inode)->root;
  4157. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4158. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4159. /*
  4160. * We need to hold space in order to delete our orphan item once we've
  4161. * added it, so this takes the reservation so we can release it later
  4162. * when we are truly done with the orphan item.
  4163. */
  4164. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4165. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4166. btrfs_ino(inode), num_bytes, 1);
  4167. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4168. }
  4169. void btrfs_orphan_release_metadata(struct inode *inode)
  4170. {
  4171. struct btrfs_root *root = BTRFS_I(inode)->root;
  4172. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4173. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4174. btrfs_ino(inode), num_bytes, 0);
  4175. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4176. }
  4177. /*
  4178. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4179. * root: the root of the parent directory
  4180. * rsv: block reservation
  4181. * items: the number of items that we need do reservation
  4182. * qgroup_reserved: used to return the reserved size in qgroup
  4183. *
  4184. * This function is used to reserve the space for snapshot/subvolume
  4185. * creation and deletion. Those operations are different with the
  4186. * common file/directory operations, they change two fs/file trees
  4187. * and root tree, the number of items that the qgroup reserves is
  4188. * different with the free space reservation. So we can not use
  4189. * the space reseravtion mechanism in start_transaction().
  4190. */
  4191. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4192. struct btrfs_block_rsv *rsv,
  4193. int items,
  4194. u64 *qgroup_reserved,
  4195. bool use_global_rsv)
  4196. {
  4197. u64 num_bytes;
  4198. int ret;
  4199. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4200. if (root->fs_info->quota_enabled) {
  4201. /* One for parent inode, two for dir entries */
  4202. num_bytes = 3 * root->leafsize;
  4203. ret = btrfs_qgroup_reserve(root, num_bytes);
  4204. if (ret)
  4205. return ret;
  4206. } else {
  4207. num_bytes = 0;
  4208. }
  4209. *qgroup_reserved = num_bytes;
  4210. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4211. rsv->space_info = __find_space_info(root->fs_info,
  4212. BTRFS_BLOCK_GROUP_METADATA);
  4213. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4214. BTRFS_RESERVE_FLUSH_ALL);
  4215. if (ret == -ENOSPC && use_global_rsv)
  4216. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
  4217. if (ret) {
  4218. if (*qgroup_reserved)
  4219. btrfs_qgroup_free(root, *qgroup_reserved);
  4220. }
  4221. return ret;
  4222. }
  4223. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4224. struct btrfs_block_rsv *rsv,
  4225. u64 qgroup_reserved)
  4226. {
  4227. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4228. if (qgroup_reserved)
  4229. btrfs_qgroup_free(root, qgroup_reserved);
  4230. }
  4231. /**
  4232. * drop_outstanding_extent - drop an outstanding extent
  4233. * @inode: the inode we're dropping the extent for
  4234. *
  4235. * This is called when we are freeing up an outstanding extent, either called
  4236. * after an error or after an extent is written. This will return the number of
  4237. * reserved extents that need to be freed. This must be called with
  4238. * BTRFS_I(inode)->lock held.
  4239. */
  4240. static unsigned drop_outstanding_extent(struct inode *inode)
  4241. {
  4242. unsigned drop_inode_space = 0;
  4243. unsigned dropped_extents = 0;
  4244. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  4245. BTRFS_I(inode)->outstanding_extents--;
  4246. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4247. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4248. &BTRFS_I(inode)->runtime_flags))
  4249. drop_inode_space = 1;
  4250. /*
  4251. * If we have more or the same amount of outsanding extents than we have
  4252. * reserved then we need to leave the reserved extents count alone.
  4253. */
  4254. if (BTRFS_I(inode)->outstanding_extents >=
  4255. BTRFS_I(inode)->reserved_extents)
  4256. return drop_inode_space;
  4257. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4258. BTRFS_I(inode)->outstanding_extents;
  4259. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4260. return dropped_extents + drop_inode_space;
  4261. }
  4262. /**
  4263. * calc_csum_metadata_size - return the amount of metada space that must be
  4264. * reserved/free'd for the given bytes.
  4265. * @inode: the inode we're manipulating
  4266. * @num_bytes: the number of bytes in question
  4267. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4268. *
  4269. * This adjusts the number of csum_bytes in the inode and then returns the
  4270. * correct amount of metadata that must either be reserved or freed. We
  4271. * calculate how many checksums we can fit into one leaf and then divide the
  4272. * number of bytes that will need to be checksumed by this value to figure out
  4273. * how many checksums will be required. If we are adding bytes then the number
  4274. * may go up and we will return the number of additional bytes that must be
  4275. * reserved. If it is going down we will return the number of bytes that must
  4276. * be freed.
  4277. *
  4278. * This must be called with BTRFS_I(inode)->lock held.
  4279. */
  4280. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4281. int reserve)
  4282. {
  4283. struct btrfs_root *root = BTRFS_I(inode)->root;
  4284. u64 csum_size;
  4285. int num_csums_per_leaf;
  4286. int num_csums;
  4287. int old_csums;
  4288. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4289. BTRFS_I(inode)->csum_bytes == 0)
  4290. return 0;
  4291. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4292. if (reserve)
  4293. BTRFS_I(inode)->csum_bytes += num_bytes;
  4294. else
  4295. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4296. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  4297. num_csums_per_leaf = (int)div64_u64(csum_size,
  4298. sizeof(struct btrfs_csum_item) +
  4299. sizeof(struct btrfs_disk_key));
  4300. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  4301. num_csums = num_csums + num_csums_per_leaf - 1;
  4302. num_csums = num_csums / num_csums_per_leaf;
  4303. old_csums = old_csums + num_csums_per_leaf - 1;
  4304. old_csums = old_csums / num_csums_per_leaf;
  4305. /* No change, no need to reserve more */
  4306. if (old_csums == num_csums)
  4307. return 0;
  4308. if (reserve)
  4309. return btrfs_calc_trans_metadata_size(root,
  4310. num_csums - old_csums);
  4311. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4312. }
  4313. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4314. {
  4315. struct btrfs_root *root = BTRFS_I(inode)->root;
  4316. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4317. u64 to_reserve = 0;
  4318. u64 csum_bytes;
  4319. unsigned nr_extents = 0;
  4320. int extra_reserve = 0;
  4321. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4322. int ret = 0;
  4323. bool delalloc_lock = true;
  4324. u64 to_free = 0;
  4325. unsigned dropped;
  4326. /* If we are a free space inode we need to not flush since we will be in
  4327. * the middle of a transaction commit. We also don't need the delalloc
  4328. * mutex since we won't race with anybody. We need this mostly to make
  4329. * lockdep shut its filthy mouth.
  4330. */
  4331. if (btrfs_is_free_space_inode(inode)) {
  4332. flush = BTRFS_RESERVE_NO_FLUSH;
  4333. delalloc_lock = false;
  4334. }
  4335. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4336. btrfs_transaction_in_commit(root->fs_info))
  4337. schedule_timeout(1);
  4338. if (delalloc_lock)
  4339. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4340. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4341. spin_lock(&BTRFS_I(inode)->lock);
  4342. BTRFS_I(inode)->outstanding_extents++;
  4343. if (BTRFS_I(inode)->outstanding_extents >
  4344. BTRFS_I(inode)->reserved_extents)
  4345. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4346. BTRFS_I(inode)->reserved_extents;
  4347. /*
  4348. * Add an item to reserve for updating the inode when we complete the
  4349. * delalloc io.
  4350. */
  4351. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4352. &BTRFS_I(inode)->runtime_flags)) {
  4353. nr_extents++;
  4354. extra_reserve = 1;
  4355. }
  4356. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4357. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4358. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4359. spin_unlock(&BTRFS_I(inode)->lock);
  4360. if (root->fs_info->quota_enabled) {
  4361. ret = btrfs_qgroup_reserve(root, num_bytes +
  4362. nr_extents * root->leafsize);
  4363. if (ret)
  4364. goto out_fail;
  4365. }
  4366. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4367. if (unlikely(ret)) {
  4368. if (root->fs_info->quota_enabled)
  4369. btrfs_qgroup_free(root, num_bytes +
  4370. nr_extents * root->leafsize);
  4371. goto out_fail;
  4372. }
  4373. spin_lock(&BTRFS_I(inode)->lock);
  4374. if (extra_reserve) {
  4375. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4376. &BTRFS_I(inode)->runtime_flags);
  4377. nr_extents--;
  4378. }
  4379. BTRFS_I(inode)->reserved_extents += nr_extents;
  4380. spin_unlock(&BTRFS_I(inode)->lock);
  4381. if (delalloc_lock)
  4382. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4383. if (to_reserve)
  4384. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  4385. btrfs_ino(inode), to_reserve, 1);
  4386. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4387. return 0;
  4388. out_fail:
  4389. spin_lock(&BTRFS_I(inode)->lock);
  4390. dropped = drop_outstanding_extent(inode);
  4391. /*
  4392. * If the inodes csum_bytes is the same as the original
  4393. * csum_bytes then we know we haven't raced with any free()ers
  4394. * so we can just reduce our inodes csum bytes and carry on.
  4395. */
  4396. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4397. calc_csum_metadata_size(inode, num_bytes, 0);
  4398. } else {
  4399. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4400. u64 bytes;
  4401. /*
  4402. * This is tricky, but first we need to figure out how much we
  4403. * free'd from any free-ers that occured during this
  4404. * reservation, so we reset ->csum_bytes to the csum_bytes
  4405. * before we dropped our lock, and then call the free for the
  4406. * number of bytes that were freed while we were trying our
  4407. * reservation.
  4408. */
  4409. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4410. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4411. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4412. /*
  4413. * Now we need to see how much we would have freed had we not
  4414. * been making this reservation and our ->csum_bytes were not
  4415. * artificially inflated.
  4416. */
  4417. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4418. bytes = csum_bytes - orig_csum_bytes;
  4419. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4420. /*
  4421. * Now reset ->csum_bytes to what it should be. If bytes is
  4422. * more than to_free then we would have free'd more space had we
  4423. * not had an artificially high ->csum_bytes, so we need to free
  4424. * the remainder. If bytes is the same or less then we don't
  4425. * need to do anything, the other free-ers did the correct
  4426. * thing.
  4427. */
  4428. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4429. if (bytes > to_free)
  4430. to_free = bytes - to_free;
  4431. else
  4432. to_free = 0;
  4433. }
  4434. spin_unlock(&BTRFS_I(inode)->lock);
  4435. if (dropped)
  4436. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4437. if (to_free) {
  4438. btrfs_block_rsv_release(root, block_rsv, to_free);
  4439. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4440. btrfs_ino(inode), to_free, 0);
  4441. }
  4442. if (delalloc_lock)
  4443. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4444. return ret;
  4445. }
  4446. /**
  4447. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4448. * @inode: the inode to release the reservation for
  4449. * @num_bytes: the number of bytes we're releasing
  4450. *
  4451. * This will release the metadata reservation for an inode. This can be called
  4452. * once we complete IO for a given set of bytes to release their metadata
  4453. * reservations.
  4454. */
  4455. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4456. {
  4457. struct btrfs_root *root = BTRFS_I(inode)->root;
  4458. u64 to_free = 0;
  4459. unsigned dropped;
  4460. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4461. spin_lock(&BTRFS_I(inode)->lock);
  4462. dropped = drop_outstanding_extent(inode);
  4463. if (num_bytes)
  4464. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4465. spin_unlock(&BTRFS_I(inode)->lock);
  4466. if (dropped > 0)
  4467. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4468. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4469. btrfs_ino(inode), to_free, 0);
  4470. if (root->fs_info->quota_enabled) {
  4471. btrfs_qgroup_free(root, num_bytes +
  4472. dropped * root->leafsize);
  4473. }
  4474. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4475. to_free);
  4476. }
  4477. /**
  4478. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4479. * @inode: inode we're writing to
  4480. * @num_bytes: the number of bytes we want to allocate
  4481. *
  4482. * This will do the following things
  4483. *
  4484. * o reserve space in the data space info for num_bytes
  4485. * o reserve space in the metadata space info based on number of outstanding
  4486. * extents and how much csums will be needed
  4487. * o add to the inodes ->delalloc_bytes
  4488. * o add it to the fs_info's delalloc inodes list.
  4489. *
  4490. * This will return 0 for success and -ENOSPC if there is no space left.
  4491. */
  4492. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4493. {
  4494. int ret;
  4495. ret = btrfs_check_data_free_space(inode, num_bytes);
  4496. if (ret)
  4497. return ret;
  4498. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4499. if (ret) {
  4500. btrfs_free_reserved_data_space(inode, num_bytes);
  4501. return ret;
  4502. }
  4503. return 0;
  4504. }
  4505. /**
  4506. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4507. * @inode: inode we're releasing space for
  4508. * @num_bytes: the number of bytes we want to free up
  4509. *
  4510. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4511. * called in the case that we don't need the metadata AND data reservations
  4512. * anymore. So if there is an error or we insert an inline extent.
  4513. *
  4514. * This function will release the metadata space that was not used and will
  4515. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4516. * list if there are no delalloc bytes left.
  4517. */
  4518. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4519. {
  4520. btrfs_delalloc_release_metadata(inode, num_bytes);
  4521. btrfs_free_reserved_data_space(inode, num_bytes);
  4522. }
  4523. static int update_block_group(struct btrfs_root *root,
  4524. u64 bytenr, u64 num_bytes, int alloc)
  4525. {
  4526. struct btrfs_block_group_cache *cache = NULL;
  4527. struct btrfs_fs_info *info = root->fs_info;
  4528. u64 total = num_bytes;
  4529. u64 old_val;
  4530. u64 byte_in_group;
  4531. int factor;
  4532. /* block accounting for super block */
  4533. spin_lock(&info->delalloc_root_lock);
  4534. old_val = btrfs_super_bytes_used(info->super_copy);
  4535. if (alloc)
  4536. old_val += num_bytes;
  4537. else
  4538. old_val -= num_bytes;
  4539. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4540. spin_unlock(&info->delalloc_root_lock);
  4541. while (total) {
  4542. cache = btrfs_lookup_block_group(info, bytenr);
  4543. if (!cache)
  4544. return -ENOENT;
  4545. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4546. BTRFS_BLOCK_GROUP_RAID1 |
  4547. BTRFS_BLOCK_GROUP_RAID10))
  4548. factor = 2;
  4549. else
  4550. factor = 1;
  4551. /*
  4552. * If this block group has free space cache written out, we
  4553. * need to make sure to load it if we are removing space. This
  4554. * is because we need the unpinning stage to actually add the
  4555. * space back to the block group, otherwise we will leak space.
  4556. */
  4557. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4558. cache_block_group(cache, 1);
  4559. byte_in_group = bytenr - cache->key.objectid;
  4560. WARN_ON(byte_in_group > cache->key.offset);
  4561. spin_lock(&cache->space_info->lock);
  4562. spin_lock(&cache->lock);
  4563. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4564. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4565. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4566. cache->dirty = 1;
  4567. old_val = btrfs_block_group_used(&cache->item);
  4568. num_bytes = min(total, cache->key.offset - byte_in_group);
  4569. if (alloc) {
  4570. old_val += num_bytes;
  4571. btrfs_set_block_group_used(&cache->item, old_val);
  4572. cache->reserved -= num_bytes;
  4573. cache->space_info->bytes_reserved -= num_bytes;
  4574. cache->space_info->bytes_used += num_bytes;
  4575. cache->space_info->disk_used += num_bytes * factor;
  4576. spin_unlock(&cache->lock);
  4577. spin_unlock(&cache->space_info->lock);
  4578. } else {
  4579. old_val -= num_bytes;
  4580. btrfs_set_block_group_used(&cache->item, old_val);
  4581. cache->pinned += num_bytes;
  4582. cache->space_info->bytes_pinned += num_bytes;
  4583. cache->space_info->bytes_used -= num_bytes;
  4584. cache->space_info->disk_used -= num_bytes * factor;
  4585. spin_unlock(&cache->lock);
  4586. spin_unlock(&cache->space_info->lock);
  4587. set_extent_dirty(info->pinned_extents,
  4588. bytenr, bytenr + num_bytes - 1,
  4589. GFP_NOFS | __GFP_NOFAIL);
  4590. }
  4591. btrfs_put_block_group(cache);
  4592. total -= num_bytes;
  4593. bytenr += num_bytes;
  4594. }
  4595. return 0;
  4596. }
  4597. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4598. {
  4599. struct btrfs_block_group_cache *cache;
  4600. u64 bytenr;
  4601. spin_lock(&root->fs_info->block_group_cache_lock);
  4602. bytenr = root->fs_info->first_logical_byte;
  4603. spin_unlock(&root->fs_info->block_group_cache_lock);
  4604. if (bytenr < (u64)-1)
  4605. return bytenr;
  4606. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4607. if (!cache)
  4608. return 0;
  4609. bytenr = cache->key.objectid;
  4610. btrfs_put_block_group(cache);
  4611. return bytenr;
  4612. }
  4613. static int pin_down_extent(struct btrfs_root *root,
  4614. struct btrfs_block_group_cache *cache,
  4615. u64 bytenr, u64 num_bytes, int reserved)
  4616. {
  4617. spin_lock(&cache->space_info->lock);
  4618. spin_lock(&cache->lock);
  4619. cache->pinned += num_bytes;
  4620. cache->space_info->bytes_pinned += num_bytes;
  4621. if (reserved) {
  4622. cache->reserved -= num_bytes;
  4623. cache->space_info->bytes_reserved -= num_bytes;
  4624. }
  4625. spin_unlock(&cache->lock);
  4626. spin_unlock(&cache->space_info->lock);
  4627. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4628. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4629. return 0;
  4630. }
  4631. /*
  4632. * this function must be called within transaction
  4633. */
  4634. int btrfs_pin_extent(struct btrfs_root *root,
  4635. u64 bytenr, u64 num_bytes, int reserved)
  4636. {
  4637. struct btrfs_block_group_cache *cache;
  4638. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4639. BUG_ON(!cache); /* Logic error */
  4640. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4641. btrfs_put_block_group(cache);
  4642. return 0;
  4643. }
  4644. /*
  4645. * this function must be called within transaction
  4646. */
  4647. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  4648. u64 bytenr, u64 num_bytes)
  4649. {
  4650. struct btrfs_block_group_cache *cache;
  4651. int ret;
  4652. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4653. if (!cache)
  4654. return -EINVAL;
  4655. /*
  4656. * pull in the free space cache (if any) so that our pin
  4657. * removes the free space from the cache. We have load_only set
  4658. * to one because the slow code to read in the free extents does check
  4659. * the pinned extents.
  4660. */
  4661. cache_block_group(cache, 1);
  4662. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4663. /* remove us from the free space cache (if we're there at all) */
  4664. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  4665. btrfs_put_block_group(cache);
  4666. return ret;
  4667. }
  4668. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  4669. {
  4670. int ret;
  4671. struct btrfs_block_group_cache *block_group;
  4672. struct btrfs_caching_control *caching_ctl;
  4673. block_group = btrfs_lookup_block_group(root->fs_info, start);
  4674. if (!block_group)
  4675. return -EINVAL;
  4676. cache_block_group(block_group, 0);
  4677. caching_ctl = get_caching_control(block_group);
  4678. if (!caching_ctl) {
  4679. /* Logic error */
  4680. BUG_ON(!block_group_cache_done(block_group));
  4681. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4682. } else {
  4683. mutex_lock(&caching_ctl->mutex);
  4684. if (start >= caching_ctl->progress) {
  4685. ret = add_excluded_extent(root, start, num_bytes);
  4686. } else if (start + num_bytes <= caching_ctl->progress) {
  4687. ret = btrfs_remove_free_space(block_group,
  4688. start, num_bytes);
  4689. } else {
  4690. num_bytes = caching_ctl->progress - start;
  4691. ret = btrfs_remove_free_space(block_group,
  4692. start, num_bytes);
  4693. if (ret)
  4694. goto out_lock;
  4695. num_bytes = (start + num_bytes) -
  4696. caching_ctl->progress;
  4697. start = caching_ctl->progress;
  4698. ret = add_excluded_extent(root, start, num_bytes);
  4699. }
  4700. out_lock:
  4701. mutex_unlock(&caching_ctl->mutex);
  4702. put_caching_control(caching_ctl);
  4703. }
  4704. btrfs_put_block_group(block_group);
  4705. return ret;
  4706. }
  4707. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  4708. struct extent_buffer *eb)
  4709. {
  4710. struct btrfs_file_extent_item *item;
  4711. struct btrfs_key key;
  4712. int found_type;
  4713. int i;
  4714. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  4715. return 0;
  4716. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  4717. btrfs_item_key_to_cpu(eb, &key, i);
  4718. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4719. continue;
  4720. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  4721. found_type = btrfs_file_extent_type(eb, item);
  4722. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  4723. continue;
  4724. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  4725. continue;
  4726. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  4727. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  4728. __exclude_logged_extent(log, key.objectid, key.offset);
  4729. }
  4730. return 0;
  4731. }
  4732. /**
  4733. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4734. * @cache: The cache we are manipulating
  4735. * @num_bytes: The number of bytes in question
  4736. * @reserve: One of the reservation enums
  4737. *
  4738. * This is called by the allocator when it reserves space, or by somebody who is
  4739. * freeing space that was never actually used on disk. For example if you
  4740. * reserve some space for a new leaf in transaction A and before transaction A
  4741. * commits you free that leaf, you call this with reserve set to 0 in order to
  4742. * clear the reservation.
  4743. *
  4744. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4745. * ENOSPC accounting. For data we handle the reservation through clearing the
  4746. * delalloc bits in the io_tree. We have to do this since we could end up
  4747. * allocating less disk space for the amount of data we have reserved in the
  4748. * case of compression.
  4749. *
  4750. * If this is a reservation and the block group has become read only we cannot
  4751. * make the reservation and return -EAGAIN, otherwise this function always
  4752. * succeeds.
  4753. */
  4754. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4755. u64 num_bytes, int reserve)
  4756. {
  4757. struct btrfs_space_info *space_info = cache->space_info;
  4758. int ret = 0;
  4759. spin_lock(&space_info->lock);
  4760. spin_lock(&cache->lock);
  4761. if (reserve != RESERVE_FREE) {
  4762. if (cache->ro) {
  4763. ret = -EAGAIN;
  4764. } else {
  4765. cache->reserved += num_bytes;
  4766. space_info->bytes_reserved += num_bytes;
  4767. if (reserve == RESERVE_ALLOC) {
  4768. trace_btrfs_space_reservation(cache->fs_info,
  4769. "space_info", space_info->flags,
  4770. num_bytes, 0);
  4771. space_info->bytes_may_use -= num_bytes;
  4772. }
  4773. }
  4774. } else {
  4775. if (cache->ro)
  4776. space_info->bytes_readonly += num_bytes;
  4777. cache->reserved -= num_bytes;
  4778. space_info->bytes_reserved -= num_bytes;
  4779. }
  4780. spin_unlock(&cache->lock);
  4781. spin_unlock(&space_info->lock);
  4782. return ret;
  4783. }
  4784. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4785. struct btrfs_root *root)
  4786. {
  4787. struct btrfs_fs_info *fs_info = root->fs_info;
  4788. struct btrfs_caching_control *next;
  4789. struct btrfs_caching_control *caching_ctl;
  4790. struct btrfs_block_group_cache *cache;
  4791. struct btrfs_space_info *space_info;
  4792. down_write(&fs_info->extent_commit_sem);
  4793. list_for_each_entry_safe(caching_ctl, next,
  4794. &fs_info->caching_block_groups, list) {
  4795. cache = caching_ctl->block_group;
  4796. if (block_group_cache_done(cache)) {
  4797. cache->last_byte_to_unpin = (u64)-1;
  4798. list_del_init(&caching_ctl->list);
  4799. put_caching_control(caching_ctl);
  4800. } else {
  4801. cache->last_byte_to_unpin = caching_ctl->progress;
  4802. }
  4803. }
  4804. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4805. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4806. else
  4807. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4808. up_write(&fs_info->extent_commit_sem);
  4809. list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
  4810. percpu_counter_set(&space_info->total_bytes_pinned, 0);
  4811. update_global_block_rsv(fs_info);
  4812. }
  4813. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4814. {
  4815. struct btrfs_fs_info *fs_info = root->fs_info;
  4816. struct btrfs_block_group_cache *cache = NULL;
  4817. struct btrfs_space_info *space_info;
  4818. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4819. u64 len;
  4820. bool readonly;
  4821. while (start <= end) {
  4822. readonly = false;
  4823. if (!cache ||
  4824. start >= cache->key.objectid + cache->key.offset) {
  4825. if (cache)
  4826. btrfs_put_block_group(cache);
  4827. cache = btrfs_lookup_block_group(fs_info, start);
  4828. BUG_ON(!cache); /* Logic error */
  4829. }
  4830. len = cache->key.objectid + cache->key.offset - start;
  4831. len = min(len, end + 1 - start);
  4832. if (start < cache->last_byte_to_unpin) {
  4833. len = min(len, cache->last_byte_to_unpin - start);
  4834. btrfs_add_free_space(cache, start, len);
  4835. }
  4836. start += len;
  4837. space_info = cache->space_info;
  4838. spin_lock(&space_info->lock);
  4839. spin_lock(&cache->lock);
  4840. cache->pinned -= len;
  4841. space_info->bytes_pinned -= len;
  4842. if (cache->ro) {
  4843. space_info->bytes_readonly += len;
  4844. readonly = true;
  4845. }
  4846. spin_unlock(&cache->lock);
  4847. if (!readonly && global_rsv->space_info == space_info) {
  4848. spin_lock(&global_rsv->lock);
  4849. if (!global_rsv->full) {
  4850. len = min(len, global_rsv->size -
  4851. global_rsv->reserved);
  4852. global_rsv->reserved += len;
  4853. space_info->bytes_may_use += len;
  4854. if (global_rsv->reserved >= global_rsv->size)
  4855. global_rsv->full = 1;
  4856. }
  4857. spin_unlock(&global_rsv->lock);
  4858. }
  4859. spin_unlock(&space_info->lock);
  4860. }
  4861. if (cache)
  4862. btrfs_put_block_group(cache);
  4863. return 0;
  4864. }
  4865. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4866. struct btrfs_root *root)
  4867. {
  4868. struct btrfs_fs_info *fs_info = root->fs_info;
  4869. struct extent_io_tree *unpin;
  4870. u64 start;
  4871. u64 end;
  4872. int ret;
  4873. if (trans->aborted)
  4874. return 0;
  4875. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4876. unpin = &fs_info->freed_extents[1];
  4877. else
  4878. unpin = &fs_info->freed_extents[0];
  4879. while (1) {
  4880. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4881. EXTENT_DIRTY, NULL);
  4882. if (ret)
  4883. break;
  4884. if (btrfs_test_opt(root, DISCARD))
  4885. ret = btrfs_discard_extent(root, start,
  4886. end + 1 - start, NULL);
  4887. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4888. unpin_extent_range(root, start, end);
  4889. cond_resched();
  4890. }
  4891. return 0;
  4892. }
  4893. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
  4894. u64 owner, u64 root_objectid)
  4895. {
  4896. struct btrfs_space_info *space_info;
  4897. u64 flags;
  4898. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4899. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  4900. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  4901. else
  4902. flags = BTRFS_BLOCK_GROUP_METADATA;
  4903. } else {
  4904. flags = BTRFS_BLOCK_GROUP_DATA;
  4905. }
  4906. space_info = __find_space_info(fs_info, flags);
  4907. BUG_ON(!space_info); /* Logic bug */
  4908. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  4909. }
  4910. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4911. struct btrfs_root *root,
  4912. u64 bytenr, u64 num_bytes, u64 parent,
  4913. u64 root_objectid, u64 owner_objectid,
  4914. u64 owner_offset, int refs_to_drop,
  4915. struct btrfs_delayed_extent_op *extent_op)
  4916. {
  4917. struct btrfs_key key;
  4918. struct btrfs_path *path;
  4919. struct btrfs_fs_info *info = root->fs_info;
  4920. struct btrfs_root *extent_root = info->extent_root;
  4921. struct extent_buffer *leaf;
  4922. struct btrfs_extent_item *ei;
  4923. struct btrfs_extent_inline_ref *iref;
  4924. int ret;
  4925. int is_data;
  4926. int extent_slot = 0;
  4927. int found_extent = 0;
  4928. int num_to_del = 1;
  4929. u32 item_size;
  4930. u64 refs;
  4931. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  4932. SKINNY_METADATA);
  4933. path = btrfs_alloc_path();
  4934. if (!path)
  4935. return -ENOMEM;
  4936. path->reada = 1;
  4937. path->leave_spinning = 1;
  4938. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4939. BUG_ON(!is_data && refs_to_drop != 1);
  4940. if (is_data)
  4941. skinny_metadata = 0;
  4942. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4943. bytenr, num_bytes, parent,
  4944. root_objectid, owner_objectid,
  4945. owner_offset);
  4946. if (ret == 0) {
  4947. extent_slot = path->slots[0];
  4948. while (extent_slot >= 0) {
  4949. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4950. extent_slot);
  4951. if (key.objectid != bytenr)
  4952. break;
  4953. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4954. key.offset == num_bytes) {
  4955. found_extent = 1;
  4956. break;
  4957. }
  4958. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  4959. key.offset == owner_objectid) {
  4960. found_extent = 1;
  4961. break;
  4962. }
  4963. if (path->slots[0] - extent_slot > 5)
  4964. break;
  4965. extent_slot--;
  4966. }
  4967. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4968. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4969. if (found_extent && item_size < sizeof(*ei))
  4970. found_extent = 0;
  4971. #endif
  4972. if (!found_extent) {
  4973. BUG_ON(iref);
  4974. ret = remove_extent_backref(trans, extent_root, path,
  4975. NULL, refs_to_drop,
  4976. is_data);
  4977. if (ret) {
  4978. btrfs_abort_transaction(trans, extent_root, ret);
  4979. goto out;
  4980. }
  4981. btrfs_release_path(path);
  4982. path->leave_spinning = 1;
  4983. key.objectid = bytenr;
  4984. key.type = BTRFS_EXTENT_ITEM_KEY;
  4985. key.offset = num_bytes;
  4986. if (!is_data && skinny_metadata) {
  4987. key.type = BTRFS_METADATA_ITEM_KEY;
  4988. key.offset = owner_objectid;
  4989. }
  4990. ret = btrfs_search_slot(trans, extent_root,
  4991. &key, path, -1, 1);
  4992. if (ret > 0 && skinny_metadata && path->slots[0]) {
  4993. /*
  4994. * Couldn't find our skinny metadata item,
  4995. * see if we have ye olde extent item.
  4996. */
  4997. path->slots[0]--;
  4998. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4999. path->slots[0]);
  5000. if (key.objectid == bytenr &&
  5001. key.type == BTRFS_EXTENT_ITEM_KEY &&
  5002. key.offset == num_bytes)
  5003. ret = 0;
  5004. }
  5005. if (ret > 0 && skinny_metadata) {
  5006. skinny_metadata = false;
  5007. key.type = BTRFS_EXTENT_ITEM_KEY;
  5008. key.offset = num_bytes;
  5009. btrfs_release_path(path);
  5010. ret = btrfs_search_slot(trans, extent_root,
  5011. &key, path, -1, 1);
  5012. }
  5013. if (ret) {
  5014. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5015. ret, bytenr);
  5016. if (ret > 0)
  5017. btrfs_print_leaf(extent_root,
  5018. path->nodes[0]);
  5019. }
  5020. if (ret < 0) {
  5021. btrfs_abort_transaction(trans, extent_root, ret);
  5022. goto out;
  5023. }
  5024. extent_slot = path->slots[0];
  5025. }
  5026. } else if (ret == -ENOENT) {
  5027. btrfs_print_leaf(extent_root, path->nodes[0]);
  5028. WARN_ON(1);
  5029. btrfs_err(info,
  5030. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  5031. bytenr, parent, root_objectid, owner_objectid,
  5032. owner_offset);
  5033. } else {
  5034. btrfs_abort_transaction(trans, extent_root, ret);
  5035. goto out;
  5036. }
  5037. leaf = path->nodes[0];
  5038. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5039. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5040. if (item_size < sizeof(*ei)) {
  5041. BUG_ON(found_extent || extent_slot != path->slots[0]);
  5042. ret = convert_extent_item_v0(trans, extent_root, path,
  5043. owner_objectid, 0);
  5044. if (ret < 0) {
  5045. btrfs_abort_transaction(trans, extent_root, ret);
  5046. goto out;
  5047. }
  5048. btrfs_release_path(path);
  5049. path->leave_spinning = 1;
  5050. key.objectid = bytenr;
  5051. key.type = BTRFS_EXTENT_ITEM_KEY;
  5052. key.offset = num_bytes;
  5053. ret = btrfs_search_slot(trans, extent_root, &key, path,
  5054. -1, 1);
  5055. if (ret) {
  5056. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5057. ret, bytenr);
  5058. btrfs_print_leaf(extent_root, path->nodes[0]);
  5059. }
  5060. if (ret < 0) {
  5061. btrfs_abort_transaction(trans, extent_root, ret);
  5062. goto out;
  5063. }
  5064. extent_slot = path->slots[0];
  5065. leaf = path->nodes[0];
  5066. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5067. }
  5068. #endif
  5069. BUG_ON(item_size < sizeof(*ei));
  5070. ei = btrfs_item_ptr(leaf, extent_slot,
  5071. struct btrfs_extent_item);
  5072. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5073. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5074. struct btrfs_tree_block_info *bi;
  5075. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5076. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5077. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5078. }
  5079. refs = btrfs_extent_refs(leaf, ei);
  5080. if (refs < refs_to_drop) {
  5081. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5082. "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
  5083. ret = -EINVAL;
  5084. btrfs_abort_transaction(trans, extent_root, ret);
  5085. goto out;
  5086. }
  5087. refs -= refs_to_drop;
  5088. if (refs > 0) {
  5089. if (extent_op)
  5090. __run_delayed_extent_op(extent_op, leaf, ei);
  5091. /*
  5092. * In the case of inline back ref, reference count will
  5093. * be updated by remove_extent_backref
  5094. */
  5095. if (iref) {
  5096. BUG_ON(!found_extent);
  5097. } else {
  5098. btrfs_set_extent_refs(leaf, ei, refs);
  5099. btrfs_mark_buffer_dirty(leaf);
  5100. }
  5101. if (found_extent) {
  5102. ret = remove_extent_backref(trans, extent_root, path,
  5103. iref, refs_to_drop,
  5104. is_data);
  5105. if (ret) {
  5106. btrfs_abort_transaction(trans, extent_root, ret);
  5107. goto out;
  5108. }
  5109. }
  5110. add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
  5111. root_objectid);
  5112. } else {
  5113. if (found_extent) {
  5114. BUG_ON(is_data && refs_to_drop !=
  5115. extent_data_ref_count(root, path, iref));
  5116. if (iref) {
  5117. BUG_ON(path->slots[0] != extent_slot);
  5118. } else {
  5119. BUG_ON(path->slots[0] != extent_slot + 1);
  5120. path->slots[0] = extent_slot;
  5121. num_to_del = 2;
  5122. }
  5123. }
  5124. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5125. num_to_del);
  5126. if (ret) {
  5127. btrfs_abort_transaction(trans, extent_root, ret);
  5128. goto out;
  5129. }
  5130. btrfs_release_path(path);
  5131. if (is_data) {
  5132. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5133. if (ret) {
  5134. btrfs_abort_transaction(trans, extent_root, ret);
  5135. goto out;
  5136. }
  5137. }
  5138. ret = update_block_group(root, bytenr, num_bytes, 0);
  5139. if (ret) {
  5140. btrfs_abort_transaction(trans, extent_root, ret);
  5141. goto out;
  5142. }
  5143. }
  5144. out:
  5145. btrfs_free_path(path);
  5146. return ret;
  5147. }
  5148. /*
  5149. * when we free an block, it is possible (and likely) that we free the last
  5150. * delayed ref for that extent as well. This searches the delayed ref tree for
  5151. * a given extent, and if there are no other delayed refs to be processed, it
  5152. * removes it from the tree.
  5153. */
  5154. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5155. struct btrfs_root *root, u64 bytenr)
  5156. {
  5157. struct btrfs_delayed_ref_head *head;
  5158. struct btrfs_delayed_ref_root *delayed_refs;
  5159. struct btrfs_delayed_ref_node *ref;
  5160. struct rb_node *node;
  5161. int ret = 0;
  5162. delayed_refs = &trans->transaction->delayed_refs;
  5163. spin_lock(&delayed_refs->lock);
  5164. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5165. if (!head)
  5166. goto out;
  5167. node = rb_prev(&head->node.rb_node);
  5168. if (!node)
  5169. goto out;
  5170. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  5171. /* there are still entries for this ref, we can't drop it */
  5172. if (ref->bytenr == bytenr)
  5173. goto out;
  5174. if (head->extent_op) {
  5175. if (!head->must_insert_reserved)
  5176. goto out;
  5177. btrfs_free_delayed_extent_op(head->extent_op);
  5178. head->extent_op = NULL;
  5179. }
  5180. /*
  5181. * waiting for the lock here would deadlock. If someone else has it
  5182. * locked they are already in the process of dropping it anyway
  5183. */
  5184. if (!mutex_trylock(&head->mutex))
  5185. goto out;
  5186. /*
  5187. * at this point we have a head with no other entries. Go
  5188. * ahead and process it.
  5189. */
  5190. head->node.in_tree = 0;
  5191. rb_erase(&head->node.rb_node, &delayed_refs->root);
  5192. delayed_refs->num_entries--;
  5193. /*
  5194. * we don't take a ref on the node because we're removing it from the
  5195. * tree, so we just steal the ref the tree was holding.
  5196. */
  5197. delayed_refs->num_heads--;
  5198. if (list_empty(&head->cluster))
  5199. delayed_refs->num_heads_ready--;
  5200. list_del_init(&head->cluster);
  5201. spin_unlock(&delayed_refs->lock);
  5202. BUG_ON(head->extent_op);
  5203. if (head->must_insert_reserved)
  5204. ret = 1;
  5205. mutex_unlock(&head->mutex);
  5206. btrfs_put_delayed_ref(&head->node);
  5207. return ret;
  5208. out:
  5209. spin_unlock(&delayed_refs->lock);
  5210. return 0;
  5211. }
  5212. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5213. struct btrfs_root *root,
  5214. struct extent_buffer *buf,
  5215. u64 parent, int last_ref)
  5216. {
  5217. struct btrfs_block_group_cache *cache = NULL;
  5218. int pin = 1;
  5219. int ret;
  5220. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5221. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5222. buf->start, buf->len,
  5223. parent, root->root_key.objectid,
  5224. btrfs_header_level(buf),
  5225. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5226. BUG_ON(ret); /* -ENOMEM */
  5227. }
  5228. if (!last_ref)
  5229. return;
  5230. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5231. if (btrfs_header_generation(buf) == trans->transid) {
  5232. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5233. ret = check_ref_cleanup(trans, root, buf->start);
  5234. if (!ret)
  5235. goto out;
  5236. }
  5237. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5238. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5239. goto out;
  5240. }
  5241. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5242. btrfs_add_free_space(cache, buf->start, buf->len);
  5243. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  5244. pin = 0;
  5245. }
  5246. out:
  5247. if (pin)
  5248. add_pinned_bytes(root->fs_info, buf->len,
  5249. btrfs_header_level(buf),
  5250. root->root_key.objectid);
  5251. /*
  5252. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5253. * anymore.
  5254. */
  5255. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5256. btrfs_put_block_group(cache);
  5257. }
  5258. /* Can return -ENOMEM */
  5259. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5260. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5261. u64 owner, u64 offset, int for_cow)
  5262. {
  5263. int ret;
  5264. struct btrfs_fs_info *fs_info = root->fs_info;
  5265. add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
  5266. /*
  5267. * tree log blocks never actually go into the extent allocation
  5268. * tree, just update pinning info and exit early.
  5269. */
  5270. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5271. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5272. /* unlocks the pinned mutex */
  5273. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5274. ret = 0;
  5275. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5276. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5277. num_bytes,
  5278. parent, root_objectid, (int)owner,
  5279. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  5280. } else {
  5281. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5282. num_bytes,
  5283. parent, root_objectid, owner,
  5284. offset, BTRFS_DROP_DELAYED_REF,
  5285. NULL, for_cow);
  5286. }
  5287. return ret;
  5288. }
  5289. static u64 stripe_align(struct btrfs_root *root,
  5290. struct btrfs_block_group_cache *cache,
  5291. u64 val, u64 num_bytes)
  5292. {
  5293. u64 ret = ALIGN(val, root->stripesize);
  5294. return ret;
  5295. }
  5296. /*
  5297. * when we wait for progress in the block group caching, its because
  5298. * our allocation attempt failed at least once. So, we must sleep
  5299. * and let some progress happen before we try again.
  5300. *
  5301. * This function will sleep at least once waiting for new free space to
  5302. * show up, and then it will check the block group free space numbers
  5303. * for our min num_bytes. Another option is to have it go ahead
  5304. * and look in the rbtree for a free extent of a given size, but this
  5305. * is a good start.
  5306. *
  5307. * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
  5308. * any of the information in this block group.
  5309. */
  5310. static noinline void
  5311. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5312. u64 num_bytes)
  5313. {
  5314. struct btrfs_caching_control *caching_ctl;
  5315. caching_ctl = get_caching_control(cache);
  5316. if (!caching_ctl)
  5317. return;
  5318. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5319. (cache->free_space_ctl->free_space >= num_bytes));
  5320. put_caching_control(caching_ctl);
  5321. }
  5322. static noinline int
  5323. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5324. {
  5325. struct btrfs_caching_control *caching_ctl;
  5326. int ret = 0;
  5327. caching_ctl = get_caching_control(cache);
  5328. if (!caching_ctl)
  5329. return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
  5330. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5331. if (cache->cached == BTRFS_CACHE_ERROR)
  5332. ret = -EIO;
  5333. put_caching_control(caching_ctl);
  5334. return ret;
  5335. }
  5336. int __get_raid_index(u64 flags)
  5337. {
  5338. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5339. return BTRFS_RAID_RAID10;
  5340. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5341. return BTRFS_RAID_RAID1;
  5342. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5343. return BTRFS_RAID_DUP;
  5344. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5345. return BTRFS_RAID_RAID0;
  5346. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5347. return BTRFS_RAID_RAID5;
  5348. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5349. return BTRFS_RAID_RAID6;
  5350. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5351. }
  5352. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  5353. {
  5354. return __get_raid_index(cache->flags);
  5355. }
  5356. enum btrfs_loop_type {
  5357. LOOP_CACHING_NOWAIT = 0,
  5358. LOOP_CACHING_WAIT = 1,
  5359. LOOP_ALLOC_CHUNK = 2,
  5360. LOOP_NO_EMPTY_SIZE = 3,
  5361. };
  5362. /*
  5363. * walks the btree of allocated extents and find a hole of a given size.
  5364. * The key ins is changed to record the hole:
  5365. * ins->objectid == start position
  5366. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5367. * ins->offset == the size of the hole.
  5368. * Any available blocks before search_start are skipped.
  5369. *
  5370. * If there is no suitable free space, we will record the max size of
  5371. * the free space extent currently.
  5372. */
  5373. static noinline int find_free_extent(struct btrfs_root *orig_root,
  5374. u64 num_bytes, u64 empty_size,
  5375. u64 hint_byte, struct btrfs_key *ins,
  5376. u64 flags)
  5377. {
  5378. int ret = 0;
  5379. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5380. struct btrfs_free_cluster *last_ptr = NULL;
  5381. struct btrfs_block_group_cache *block_group = NULL;
  5382. struct btrfs_block_group_cache *used_block_group;
  5383. u64 search_start = 0;
  5384. u64 max_extent_size = 0;
  5385. int empty_cluster = 2 * 1024 * 1024;
  5386. struct btrfs_space_info *space_info;
  5387. int loop = 0;
  5388. int index = __get_raid_index(flags);
  5389. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5390. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5391. bool found_uncached_bg = false;
  5392. bool failed_cluster_refill = false;
  5393. bool failed_alloc = false;
  5394. bool use_cluster = true;
  5395. bool have_caching_bg = false;
  5396. WARN_ON(num_bytes < root->sectorsize);
  5397. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  5398. ins->objectid = 0;
  5399. ins->offset = 0;
  5400. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5401. space_info = __find_space_info(root->fs_info, flags);
  5402. if (!space_info) {
  5403. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5404. return -ENOSPC;
  5405. }
  5406. /*
  5407. * If the space info is for both data and metadata it means we have a
  5408. * small filesystem and we can't use the clustering stuff.
  5409. */
  5410. if (btrfs_mixed_space_info(space_info))
  5411. use_cluster = false;
  5412. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5413. last_ptr = &root->fs_info->meta_alloc_cluster;
  5414. if (!btrfs_test_opt(root, SSD))
  5415. empty_cluster = 64 * 1024;
  5416. }
  5417. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5418. btrfs_test_opt(root, SSD)) {
  5419. last_ptr = &root->fs_info->data_alloc_cluster;
  5420. }
  5421. if (last_ptr) {
  5422. spin_lock(&last_ptr->lock);
  5423. if (last_ptr->block_group)
  5424. hint_byte = last_ptr->window_start;
  5425. spin_unlock(&last_ptr->lock);
  5426. }
  5427. search_start = max(search_start, first_logical_byte(root, 0));
  5428. search_start = max(search_start, hint_byte);
  5429. if (!last_ptr)
  5430. empty_cluster = 0;
  5431. if (search_start == hint_byte) {
  5432. block_group = btrfs_lookup_block_group(root->fs_info,
  5433. search_start);
  5434. used_block_group = block_group;
  5435. /*
  5436. * we don't want to use the block group if it doesn't match our
  5437. * allocation bits, or if its not cached.
  5438. *
  5439. * However if we are re-searching with an ideal block group
  5440. * picked out then we don't care that the block group is cached.
  5441. */
  5442. if (block_group && block_group_bits(block_group, flags) &&
  5443. block_group->cached != BTRFS_CACHE_NO) {
  5444. down_read(&space_info->groups_sem);
  5445. if (list_empty(&block_group->list) ||
  5446. block_group->ro) {
  5447. /*
  5448. * someone is removing this block group,
  5449. * we can't jump into the have_block_group
  5450. * target because our list pointers are not
  5451. * valid
  5452. */
  5453. btrfs_put_block_group(block_group);
  5454. up_read(&space_info->groups_sem);
  5455. } else {
  5456. index = get_block_group_index(block_group);
  5457. goto have_block_group;
  5458. }
  5459. } else if (block_group) {
  5460. btrfs_put_block_group(block_group);
  5461. }
  5462. }
  5463. search:
  5464. have_caching_bg = false;
  5465. down_read(&space_info->groups_sem);
  5466. list_for_each_entry(block_group, &space_info->block_groups[index],
  5467. list) {
  5468. u64 offset;
  5469. int cached;
  5470. used_block_group = block_group;
  5471. btrfs_get_block_group(block_group);
  5472. search_start = block_group->key.objectid;
  5473. /*
  5474. * this can happen if we end up cycling through all the
  5475. * raid types, but we want to make sure we only allocate
  5476. * for the proper type.
  5477. */
  5478. if (!block_group_bits(block_group, flags)) {
  5479. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  5480. BTRFS_BLOCK_GROUP_RAID1 |
  5481. BTRFS_BLOCK_GROUP_RAID5 |
  5482. BTRFS_BLOCK_GROUP_RAID6 |
  5483. BTRFS_BLOCK_GROUP_RAID10;
  5484. /*
  5485. * if they asked for extra copies and this block group
  5486. * doesn't provide them, bail. This does allow us to
  5487. * fill raid0 from raid1.
  5488. */
  5489. if ((flags & extra) && !(block_group->flags & extra))
  5490. goto loop;
  5491. }
  5492. have_block_group:
  5493. cached = block_group_cache_done(block_group);
  5494. if (unlikely(!cached)) {
  5495. found_uncached_bg = true;
  5496. ret = cache_block_group(block_group, 0);
  5497. BUG_ON(ret < 0);
  5498. ret = 0;
  5499. }
  5500. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
  5501. goto loop;
  5502. if (unlikely(block_group->ro))
  5503. goto loop;
  5504. /*
  5505. * Ok we want to try and use the cluster allocator, so
  5506. * lets look there
  5507. */
  5508. if (last_ptr) {
  5509. unsigned long aligned_cluster;
  5510. /*
  5511. * the refill lock keeps out other
  5512. * people trying to start a new cluster
  5513. */
  5514. spin_lock(&last_ptr->refill_lock);
  5515. used_block_group = last_ptr->block_group;
  5516. if (used_block_group != block_group &&
  5517. (!used_block_group ||
  5518. used_block_group->ro ||
  5519. !block_group_bits(used_block_group, flags))) {
  5520. used_block_group = block_group;
  5521. goto refill_cluster;
  5522. }
  5523. if (used_block_group != block_group)
  5524. btrfs_get_block_group(used_block_group);
  5525. offset = btrfs_alloc_from_cluster(used_block_group,
  5526. last_ptr,
  5527. num_bytes,
  5528. used_block_group->key.objectid,
  5529. &max_extent_size);
  5530. if (offset) {
  5531. /* we have a block, we're done */
  5532. spin_unlock(&last_ptr->refill_lock);
  5533. trace_btrfs_reserve_extent_cluster(root,
  5534. block_group, search_start, num_bytes);
  5535. goto checks;
  5536. }
  5537. WARN_ON(last_ptr->block_group != used_block_group);
  5538. if (used_block_group != block_group) {
  5539. btrfs_put_block_group(used_block_group);
  5540. used_block_group = block_group;
  5541. }
  5542. refill_cluster:
  5543. BUG_ON(used_block_group != block_group);
  5544. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5545. * set up a new clusters, so lets just skip it
  5546. * and let the allocator find whatever block
  5547. * it can find. If we reach this point, we
  5548. * will have tried the cluster allocator
  5549. * plenty of times and not have found
  5550. * anything, so we are likely way too
  5551. * fragmented for the clustering stuff to find
  5552. * anything.
  5553. *
  5554. * However, if the cluster is taken from the
  5555. * current block group, release the cluster
  5556. * first, so that we stand a better chance of
  5557. * succeeding in the unclustered
  5558. * allocation. */
  5559. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5560. last_ptr->block_group != block_group) {
  5561. spin_unlock(&last_ptr->refill_lock);
  5562. goto unclustered_alloc;
  5563. }
  5564. /*
  5565. * this cluster didn't work out, free it and
  5566. * start over
  5567. */
  5568. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5569. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5570. spin_unlock(&last_ptr->refill_lock);
  5571. goto unclustered_alloc;
  5572. }
  5573. aligned_cluster = max_t(unsigned long,
  5574. empty_cluster + empty_size,
  5575. block_group->full_stripe_len);
  5576. /* allocate a cluster in this block group */
  5577. ret = btrfs_find_space_cluster(root, block_group,
  5578. last_ptr, search_start,
  5579. num_bytes,
  5580. aligned_cluster);
  5581. if (ret == 0) {
  5582. /*
  5583. * now pull our allocation out of this
  5584. * cluster
  5585. */
  5586. offset = btrfs_alloc_from_cluster(block_group,
  5587. last_ptr,
  5588. num_bytes,
  5589. search_start,
  5590. &max_extent_size);
  5591. if (offset) {
  5592. /* we found one, proceed */
  5593. spin_unlock(&last_ptr->refill_lock);
  5594. trace_btrfs_reserve_extent_cluster(root,
  5595. block_group, search_start,
  5596. num_bytes);
  5597. goto checks;
  5598. }
  5599. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5600. && !failed_cluster_refill) {
  5601. spin_unlock(&last_ptr->refill_lock);
  5602. failed_cluster_refill = true;
  5603. wait_block_group_cache_progress(block_group,
  5604. num_bytes + empty_cluster + empty_size);
  5605. goto have_block_group;
  5606. }
  5607. /*
  5608. * at this point we either didn't find a cluster
  5609. * or we weren't able to allocate a block from our
  5610. * cluster. Free the cluster we've been trying
  5611. * to use, and go to the next block group
  5612. */
  5613. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5614. spin_unlock(&last_ptr->refill_lock);
  5615. goto loop;
  5616. }
  5617. unclustered_alloc:
  5618. spin_lock(&block_group->free_space_ctl->tree_lock);
  5619. if (cached &&
  5620. block_group->free_space_ctl->free_space <
  5621. num_bytes + empty_cluster + empty_size) {
  5622. if (block_group->free_space_ctl->free_space >
  5623. max_extent_size)
  5624. max_extent_size =
  5625. block_group->free_space_ctl->free_space;
  5626. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5627. goto loop;
  5628. }
  5629. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5630. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5631. num_bytes, empty_size,
  5632. &max_extent_size);
  5633. /*
  5634. * If we didn't find a chunk, and we haven't failed on this
  5635. * block group before, and this block group is in the middle of
  5636. * caching and we are ok with waiting, then go ahead and wait
  5637. * for progress to be made, and set failed_alloc to true.
  5638. *
  5639. * If failed_alloc is true then we've already waited on this
  5640. * block group once and should move on to the next block group.
  5641. */
  5642. if (!offset && !failed_alloc && !cached &&
  5643. loop > LOOP_CACHING_NOWAIT) {
  5644. wait_block_group_cache_progress(block_group,
  5645. num_bytes + empty_size);
  5646. failed_alloc = true;
  5647. goto have_block_group;
  5648. } else if (!offset) {
  5649. if (!cached)
  5650. have_caching_bg = true;
  5651. goto loop;
  5652. }
  5653. checks:
  5654. search_start = stripe_align(root, used_block_group,
  5655. offset, num_bytes);
  5656. /* move on to the next group */
  5657. if (search_start + num_bytes >
  5658. used_block_group->key.objectid + used_block_group->key.offset) {
  5659. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5660. goto loop;
  5661. }
  5662. if (offset < search_start)
  5663. btrfs_add_free_space(used_block_group, offset,
  5664. search_start - offset);
  5665. BUG_ON(offset > search_start);
  5666. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5667. alloc_type);
  5668. if (ret == -EAGAIN) {
  5669. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5670. goto loop;
  5671. }
  5672. /* we are all good, lets return */
  5673. ins->objectid = search_start;
  5674. ins->offset = num_bytes;
  5675. trace_btrfs_reserve_extent(orig_root, block_group,
  5676. search_start, num_bytes);
  5677. if (used_block_group != block_group)
  5678. btrfs_put_block_group(used_block_group);
  5679. btrfs_put_block_group(block_group);
  5680. break;
  5681. loop:
  5682. failed_cluster_refill = false;
  5683. failed_alloc = false;
  5684. BUG_ON(index != get_block_group_index(block_group));
  5685. if (used_block_group != block_group)
  5686. btrfs_put_block_group(used_block_group);
  5687. btrfs_put_block_group(block_group);
  5688. }
  5689. up_read(&space_info->groups_sem);
  5690. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5691. goto search;
  5692. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5693. goto search;
  5694. /*
  5695. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5696. * caching kthreads as we move along
  5697. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5698. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5699. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5700. * again
  5701. */
  5702. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5703. index = 0;
  5704. loop++;
  5705. if (loop == LOOP_ALLOC_CHUNK) {
  5706. struct btrfs_trans_handle *trans;
  5707. trans = btrfs_join_transaction(root);
  5708. if (IS_ERR(trans)) {
  5709. ret = PTR_ERR(trans);
  5710. goto out;
  5711. }
  5712. ret = do_chunk_alloc(trans, root, flags,
  5713. CHUNK_ALLOC_FORCE);
  5714. /*
  5715. * Do not bail out on ENOSPC since we
  5716. * can do more things.
  5717. */
  5718. if (ret < 0 && ret != -ENOSPC)
  5719. btrfs_abort_transaction(trans,
  5720. root, ret);
  5721. else
  5722. ret = 0;
  5723. btrfs_end_transaction(trans, root);
  5724. if (ret)
  5725. goto out;
  5726. }
  5727. if (loop == LOOP_NO_EMPTY_SIZE) {
  5728. empty_size = 0;
  5729. empty_cluster = 0;
  5730. }
  5731. goto search;
  5732. } else if (!ins->objectid) {
  5733. ret = -ENOSPC;
  5734. } else if (ins->objectid) {
  5735. ret = 0;
  5736. }
  5737. out:
  5738. if (ret == -ENOSPC)
  5739. ins->offset = max_extent_size;
  5740. return ret;
  5741. }
  5742. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5743. int dump_block_groups)
  5744. {
  5745. struct btrfs_block_group_cache *cache;
  5746. int index = 0;
  5747. spin_lock(&info->lock);
  5748. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5749. info->flags,
  5750. info->total_bytes - info->bytes_used - info->bytes_pinned -
  5751. info->bytes_reserved - info->bytes_readonly,
  5752. (info->full) ? "" : "not ");
  5753. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5754. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5755. info->total_bytes, info->bytes_used, info->bytes_pinned,
  5756. info->bytes_reserved, info->bytes_may_use,
  5757. info->bytes_readonly);
  5758. spin_unlock(&info->lock);
  5759. if (!dump_block_groups)
  5760. return;
  5761. down_read(&info->groups_sem);
  5762. again:
  5763. list_for_each_entry(cache, &info->block_groups[index], list) {
  5764. spin_lock(&cache->lock);
  5765. printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
  5766. cache->key.objectid, cache->key.offset,
  5767. btrfs_block_group_used(&cache->item), cache->pinned,
  5768. cache->reserved, cache->ro ? "[readonly]" : "");
  5769. btrfs_dump_free_space(cache, bytes);
  5770. spin_unlock(&cache->lock);
  5771. }
  5772. if (++index < BTRFS_NR_RAID_TYPES)
  5773. goto again;
  5774. up_read(&info->groups_sem);
  5775. }
  5776. int btrfs_reserve_extent(struct btrfs_root *root,
  5777. u64 num_bytes, u64 min_alloc_size,
  5778. u64 empty_size, u64 hint_byte,
  5779. struct btrfs_key *ins, int is_data)
  5780. {
  5781. bool final_tried = false;
  5782. u64 flags;
  5783. int ret;
  5784. flags = btrfs_get_alloc_profile(root, is_data);
  5785. again:
  5786. WARN_ON(num_bytes < root->sectorsize);
  5787. ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
  5788. flags);
  5789. if (ret == -ENOSPC) {
  5790. if (!final_tried && ins->offset) {
  5791. num_bytes = min(num_bytes >> 1, ins->offset);
  5792. num_bytes = round_down(num_bytes, root->sectorsize);
  5793. num_bytes = max(num_bytes, min_alloc_size);
  5794. if (num_bytes == min_alloc_size)
  5795. final_tried = true;
  5796. goto again;
  5797. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5798. struct btrfs_space_info *sinfo;
  5799. sinfo = __find_space_info(root->fs_info, flags);
  5800. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  5801. flags, num_bytes);
  5802. if (sinfo)
  5803. dump_space_info(sinfo, num_bytes, 1);
  5804. }
  5805. }
  5806. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5807. return ret;
  5808. }
  5809. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5810. u64 start, u64 len, int pin)
  5811. {
  5812. struct btrfs_block_group_cache *cache;
  5813. int ret = 0;
  5814. cache = btrfs_lookup_block_group(root->fs_info, start);
  5815. if (!cache) {
  5816. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  5817. start);
  5818. return -ENOSPC;
  5819. }
  5820. if (btrfs_test_opt(root, DISCARD))
  5821. ret = btrfs_discard_extent(root, start, len, NULL);
  5822. if (pin)
  5823. pin_down_extent(root, cache, start, len, 1);
  5824. else {
  5825. btrfs_add_free_space(cache, start, len);
  5826. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5827. }
  5828. btrfs_put_block_group(cache);
  5829. trace_btrfs_reserved_extent_free(root, start, len);
  5830. return ret;
  5831. }
  5832. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5833. u64 start, u64 len)
  5834. {
  5835. return __btrfs_free_reserved_extent(root, start, len, 0);
  5836. }
  5837. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5838. u64 start, u64 len)
  5839. {
  5840. return __btrfs_free_reserved_extent(root, start, len, 1);
  5841. }
  5842. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5843. struct btrfs_root *root,
  5844. u64 parent, u64 root_objectid,
  5845. u64 flags, u64 owner, u64 offset,
  5846. struct btrfs_key *ins, int ref_mod)
  5847. {
  5848. int ret;
  5849. struct btrfs_fs_info *fs_info = root->fs_info;
  5850. struct btrfs_extent_item *extent_item;
  5851. struct btrfs_extent_inline_ref *iref;
  5852. struct btrfs_path *path;
  5853. struct extent_buffer *leaf;
  5854. int type;
  5855. u32 size;
  5856. if (parent > 0)
  5857. type = BTRFS_SHARED_DATA_REF_KEY;
  5858. else
  5859. type = BTRFS_EXTENT_DATA_REF_KEY;
  5860. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5861. path = btrfs_alloc_path();
  5862. if (!path)
  5863. return -ENOMEM;
  5864. path->leave_spinning = 1;
  5865. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5866. ins, size);
  5867. if (ret) {
  5868. btrfs_free_path(path);
  5869. return ret;
  5870. }
  5871. leaf = path->nodes[0];
  5872. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5873. struct btrfs_extent_item);
  5874. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5875. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5876. btrfs_set_extent_flags(leaf, extent_item,
  5877. flags | BTRFS_EXTENT_FLAG_DATA);
  5878. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5879. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5880. if (parent > 0) {
  5881. struct btrfs_shared_data_ref *ref;
  5882. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5883. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5884. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5885. } else {
  5886. struct btrfs_extent_data_ref *ref;
  5887. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5888. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5889. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5890. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5891. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5892. }
  5893. btrfs_mark_buffer_dirty(path->nodes[0]);
  5894. btrfs_free_path(path);
  5895. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5896. if (ret) { /* -ENOENT, logic error */
  5897. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5898. ins->objectid, ins->offset);
  5899. BUG();
  5900. }
  5901. return ret;
  5902. }
  5903. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5904. struct btrfs_root *root,
  5905. u64 parent, u64 root_objectid,
  5906. u64 flags, struct btrfs_disk_key *key,
  5907. int level, struct btrfs_key *ins)
  5908. {
  5909. int ret;
  5910. struct btrfs_fs_info *fs_info = root->fs_info;
  5911. struct btrfs_extent_item *extent_item;
  5912. struct btrfs_tree_block_info *block_info;
  5913. struct btrfs_extent_inline_ref *iref;
  5914. struct btrfs_path *path;
  5915. struct extent_buffer *leaf;
  5916. u32 size = sizeof(*extent_item) + sizeof(*iref);
  5917. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5918. SKINNY_METADATA);
  5919. if (!skinny_metadata)
  5920. size += sizeof(*block_info);
  5921. path = btrfs_alloc_path();
  5922. if (!path)
  5923. return -ENOMEM;
  5924. path->leave_spinning = 1;
  5925. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5926. ins, size);
  5927. if (ret) {
  5928. btrfs_free_path(path);
  5929. return ret;
  5930. }
  5931. leaf = path->nodes[0];
  5932. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5933. struct btrfs_extent_item);
  5934. btrfs_set_extent_refs(leaf, extent_item, 1);
  5935. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5936. btrfs_set_extent_flags(leaf, extent_item,
  5937. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5938. if (skinny_metadata) {
  5939. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5940. } else {
  5941. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5942. btrfs_set_tree_block_key(leaf, block_info, key);
  5943. btrfs_set_tree_block_level(leaf, block_info, level);
  5944. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5945. }
  5946. if (parent > 0) {
  5947. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5948. btrfs_set_extent_inline_ref_type(leaf, iref,
  5949. BTRFS_SHARED_BLOCK_REF_KEY);
  5950. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5951. } else {
  5952. btrfs_set_extent_inline_ref_type(leaf, iref,
  5953. BTRFS_TREE_BLOCK_REF_KEY);
  5954. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5955. }
  5956. btrfs_mark_buffer_dirty(leaf);
  5957. btrfs_free_path(path);
  5958. ret = update_block_group(root, ins->objectid, root->leafsize, 1);
  5959. if (ret) { /* -ENOENT, logic error */
  5960. btrfs_err(fs_info, "update block group failed for %llu %llu",
  5961. ins->objectid, ins->offset);
  5962. BUG();
  5963. }
  5964. return ret;
  5965. }
  5966. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5967. struct btrfs_root *root,
  5968. u64 root_objectid, u64 owner,
  5969. u64 offset, struct btrfs_key *ins)
  5970. {
  5971. int ret;
  5972. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5973. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5974. ins->offset, 0,
  5975. root_objectid, owner, offset,
  5976. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5977. return ret;
  5978. }
  5979. /*
  5980. * this is used by the tree logging recovery code. It records that
  5981. * an extent has been allocated and makes sure to clear the free
  5982. * space cache bits as well
  5983. */
  5984. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  5985. struct btrfs_root *root,
  5986. u64 root_objectid, u64 owner, u64 offset,
  5987. struct btrfs_key *ins)
  5988. {
  5989. int ret;
  5990. struct btrfs_block_group_cache *block_group;
  5991. /*
  5992. * Mixed block groups will exclude before processing the log so we only
  5993. * need to do the exlude dance if this fs isn't mixed.
  5994. */
  5995. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  5996. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  5997. if (ret)
  5998. return ret;
  5999. }
  6000. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  6001. if (!block_group)
  6002. return -EINVAL;
  6003. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  6004. RESERVE_ALLOC_NO_ACCOUNT);
  6005. BUG_ON(ret); /* logic error */
  6006. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  6007. 0, owner, offset, ins, 1);
  6008. btrfs_put_block_group(block_group);
  6009. return ret;
  6010. }
  6011. static struct extent_buffer *
  6012. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  6013. u64 bytenr, u32 blocksize, int level)
  6014. {
  6015. struct extent_buffer *buf;
  6016. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6017. if (!buf)
  6018. return ERR_PTR(-ENOMEM);
  6019. btrfs_set_header_generation(buf, trans->transid);
  6020. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  6021. btrfs_tree_lock(buf);
  6022. clean_tree_block(trans, root, buf);
  6023. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  6024. btrfs_set_lock_blocking(buf);
  6025. btrfs_set_buffer_uptodate(buf);
  6026. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  6027. /*
  6028. * we allow two log transactions at a time, use different
  6029. * EXENT bit to differentiate dirty pages.
  6030. */
  6031. if (root->log_transid % 2 == 0)
  6032. set_extent_dirty(&root->dirty_log_pages, buf->start,
  6033. buf->start + buf->len - 1, GFP_NOFS);
  6034. else
  6035. set_extent_new(&root->dirty_log_pages, buf->start,
  6036. buf->start + buf->len - 1, GFP_NOFS);
  6037. } else {
  6038. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  6039. buf->start + buf->len - 1, GFP_NOFS);
  6040. }
  6041. trans->blocks_used++;
  6042. /* this returns a buffer locked for blocking */
  6043. return buf;
  6044. }
  6045. static struct btrfs_block_rsv *
  6046. use_block_rsv(struct btrfs_trans_handle *trans,
  6047. struct btrfs_root *root, u32 blocksize)
  6048. {
  6049. struct btrfs_block_rsv *block_rsv;
  6050. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  6051. int ret;
  6052. bool global_updated = false;
  6053. block_rsv = get_block_rsv(trans, root);
  6054. if (unlikely(block_rsv->size == 0))
  6055. goto try_reserve;
  6056. again:
  6057. ret = block_rsv_use_bytes(block_rsv, blocksize);
  6058. if (!ret)
  6059. return block_rsv;
  6060. if (block_rsv->failfast)
  6061. return ERR_PTR(ret);
  6062. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  6063. global_updated = true;
  6064. update_global_block_rsv(root->fs_info);
  6065. goto again;
  6066. }
  6067. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6068. static DEFINE_RATELIMIT_STATE(_rs,
  6069. DEFAULT_RATELIMIT_INTERVAL * 10,
  6070. /*DEFAULT_RATELIMIT_BURST*/ 1);
  6071. if (__ratelimit(&_rs))
  6072. WARN(1, KERN_DEBUG
  6073. "btrfs: block rsv returned %d\n", ret);
  6074. }
  6075. try_reserve:
  6076. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  6077. BTRFS_RESERVE_NO_FLUSH);
  6078. if (!ret)
  6079. return block_rsv;
  6080. /*
  6081. * If we couldn't reserve metadata bytes try and use some from
  6082. * the global reserve if its space type is the same as the global
  6083. * reservation.
  6084. */
  6085. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  6086. block_rsv->space_info == global_rsv->space_info) {
  6087. ret = block_rsv_use_bytes(global_rsv, blocksize);
  6088. if (!ret)
  6089. return global_rsv;
  6090. }
  6091. return ERR_PTR(ret);
  6092. }
  6093. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6094. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6095. {
  6096. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6097. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6098. }
  6099. /*
  6100. * finds a free extent and does all the dirty work required for allocation
  6101. * returns the key for the extent through ins, and a tree buffer for
  6102. * the first block of the extent through buf.
  6103. *
  6104. * returns the tree buffer or NULL.
  6105. */
  6106. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  6107. struct btrfs_root *root, u32 blocksize,
  6108. u64 parent, u64 root_objectid,
  6109. struct btrfs_disk_key *key, int level,
  6110. u64 hint, u64 empty_size)
  6111. {
  6112. struct btrfs_key ins;
  6113. struct btrfs_block_rsv *block_rsv;
  6114. struct extent_buffer *buf;
  6115. u64 flags = 0;
  6116. int ret;
  6117. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6118. SKINNY_METADATA);
  6119. block_rsv = use_block_rsv(trans, root, blocksize);
  6120. if (IS_ERR(block_rsv))
  6121. return ERR_CAST(block_rsv);
  6122. ret = btrfs_reserve_extent(root, blocksize, blocksize,
  6123. empty_size, hint, &ins, 0);
  6124. if (ret) {
  6125. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6126. return ERR_PTR(ret);
  6127. }
  6128. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  6129. blocksize, level);
  6130. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  6131. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6132. if (parent == 0)
  6133. parent = ins.objectid;
  6134. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6135. } else
  6136. BUG_ON(parent > 0);
  6137. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6138. struct btrfs_delayed_extent_op *extent_op;
  6139. extent_op = btrfs_alloc_delayed_extent_op();
  6140. BUG_ON(!extent_op); /* -ENOMEM */
  6141. if (key)
  6142. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6143. else
  6144. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6145. extent_op->flags_to_set = flags;
  6146. if (skinny_metadata)
  6147. extent_op->update_key = 0;
  6148. else
  6149. extent_op->update_key = 1;
  6150. extent_op->update_flags = 1;
  6151. extent_op->is_data = 0;
  6152. extent_op->level = level;
  6153. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6154. ins.objectid,
  6155. ins.offset, parent, root_objectid,
  6156. level, BTRFS_ADD_DELAYED_EXTENT,
  6157. extent_op, 0);
  6158. BUG_ON(ret); /* -ENOMEM */
  6159. }
  6160. return buf;
  6161. }
  6162. struct walk_control {
  6163. u64 refs[BTRFS_MAX_LEVEL];
  6164. u64 flags[BTRFS_MAX_LEVEL];
  6165. struct btrfs_key update_progress;
  6166. int stage;
  6167. int level;
  6168. int shared_level;
  6169. int update_ref;
  6170. int keep_locks;
  6171. int reada_slot;
  6172. int reada_count;
  6173. int for_reloc;
  6174. };
  6175. #define DROP_REFERENCE 1
  6176. #define UPDATE_BACKREF 2
  6177. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6178. struct btrfs_root *root,
  6179. struct walk_control *wc,
  6180. struct btrfs_path *path)
  6181. {
  6182. u64 bytenr;
  6183. u64 generation;
  6184. u64 refs;
  6185. u64 flags;
  6186. u32 nritems;
  6187. u32 blocksize;
  6188. struct btrfs_key key;
  6189. struct extent_buffer *eb;
  6190. int ret;
  6191. int slot;
  6192. int nread = 0;
  6193. if (path->slots[wc->level] < wc->reada_slot) {
  6194. wc->reada_count = wc->reada_count * 2 / 3;
  6195. wc->reada_count = max(wc->reada_count, 2);
  6196. } else {
  6197. wc->reada_count = wc->reada_count * 3 / 2;
  6198. wc->reada_count = min_t(int, wc->reada_count,
  6199. BTRFS_NODEPTRS_PER_BLOCK(root));
  6200. }
  6201. eb = path->nodes[wc->level];
  6202. nritems = btrfs_header_nritems(eb);
  6203. blocksize = btrfs_level_size(root, wc->level - 1);
  6204. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6205. if (nread >= wc->reada_count)
  6206. break;
  6207. cond_resched();
  6208. bytenr = btrfs_node_blockptr(eb, slot);
  6209. generation = btrfs_node_ptr_generation(eb, slot);
  6210. if (slot == path->slots[wc->level])
  6211. goto reada;
  6212. if (wc->stage == UPDATE_BACKREF &&
  6213. generation <= root->root_key.offset)
  6214. continue;
  6215. /* We don't lock the tree block, it's OK to be racy here */
  6216. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6217. wc->level - 1, 1, &refs,
  6218. &flags);
  6219. /* We don't care about errors in readahead. */
  6220. if (ret < 0)
  6221. continue;
  6222. BUG_ON(refs == 0);
  6223. if (wc->stage == DROP_REFERENCE) {
  6224. if (refs == 1)
  6225. goto reada;
  6226. if (wc->level == 1 &&
  6227. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6228. continue;
  6229. if (!wc->update_ref ||
  6230. generation <= root->root_key.offset)
  6231. continue;
  6232. btrfs_node_key_to_cpu(eb, &key, slot);
  6233. ret = btrfs_comp_cpu_keys(&key,
  6234. &wc->update_progress);
  6235. if (ret < 0)
  6236. continue;
  6237. } else {
  6238. if (wc->level == 1 &&
  6239. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6240. continue;
  6241. }
  6242. reada:
  6243. ret = readahead_tree_block(root, bytenr, blocksize,
  6244. generation);
  6245. if (ret)
  6246. break;
  6247. nread++;
  6248. }
  6249. wc->reada_slot = slot;
  6250. }
  6251. /*
  6252. * helper to process tree block while walking down the tree.
  6253. *
  6254. * when wc->stage == UPDATE_BACKREF, this function updates
  6255. * back refs for pointers in the block.
  6256. *
  6257. * NOTE: return value 1 means we should stop walking down.
  6258. */
  6259. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  6260. struct btrfs_root *root,
  6261. struct btrfs_path *path,
  6262. struct walk_control *wc, int lookup_info)
  6263. {
  6264. int level = wc->level;
  6265. struct extent_buffer *eb = path->nodes[level];
  6266. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6267. int ret;
  6268. if (wc->stage == UPDATE_BACKREF &&
  6269. btrfs_header_owner(eb) != root->root_key.objectid)
  6270. return 1;
  6271. /*
  6272. * when reference count of tree block is 1, it won't increase
  6273. * again. once full backref flag is set, we never clear it.
  6274. */
  6275. if (lookup_info &&
  6276. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  6277. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  6278. BUG_ON(!path->locks[level]);
  6279. ret = btrfs_lookup_extent_info(trans, root,
  6280. eb->start, level, 1,
  6281. &wc->refs[level],
  6282. &wc->flags[level]);
  6283. BUG_ON(ret == -ENOMEM);
  6284. if (ret)
  6285. return ret;
  6286. BUG_ON(wc->refs[level] == 0);
  6287. }
  6288. if (wc->stage == DROP_REFERENCE) {
  6289. if (wc->refs[level] > 1)
  6290. return 1;
  6291. if (path->locks[level] && !wc->keep_locks) {
  6292. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6293. path->locks[level] = 0;
  6294. }
  6295. return 0;
  6296. }
  6297. /* wc->stage == UPDATE_BACKREF */
  6298. if (!(wc->flags[level] & flag)) {
  6299. BUG_ON(!path->locks[level]);
  6300. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  6301. BUG_ON(ret); /* -ENOMEM */
  6302. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  6303. BUG_ON(ret); /* -ENOMEM */
  6304. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  6305. eb->len, flag,
  6306. btrfs_header_level(eb), 0);
  6307. BUG_ON(ret); /* -ENOMEM */
  6308. wc->flags[level] |= flag;
  6309. }
  6310. /*
  6311. * the block is shared by multiple trees, so it's not good to
  6312. * keep the tree lock
  6313. */
  6314. if (path->locks[level] && level > 0) {
  6315. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6316. path->locks[level] = 0;
  6317. }
  6318. return 0;
  6319. }
  6320. /*
  6321. * helper to process tree block pointer.
  6322. *
  6323. * when wc->stage == DROP_REFERENCE, this function checks
  6324. * reference count of the block pointed to. if the block
  6325. * is shared and we need update back refs for the subtree
  6326. * rooted at the block, this function changes wc->stage to
  6327. * UPDATE_BACKREF. if the block is shared and there is no
  6328. * need to update back, this function drops the reference
  6329. * to the block.
  6330. *
  6331. * NOTE: return value 1 means we should stop walking down.
  6332. */
  6333. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  6334. struct btrfs_root *root,
  6335. struct btrfs_path *path,
  6336. struct walk_control *wc, int *lookup_info)
  6337. {
  6338. u64 bytenr;
  6339. u64 generation;
  6340. u64 parent;
  6341. u32 blocksize;
  6342. struct btrfs_key key;
  6343. struct extent_buffer *next;
  6344. int level = wc->level;
  6345. int reada = 0;
  6346. int ret = 0;
  6347. generation = btrfs_node_ptr_generation(path->nodes[level],
  6348. path->slots[level]);
  6349. /*
  6350. * if the lower level block was created before the snapshot
  6351. * was created, we know there is no need to update back refs
  6352. * for the subtree
  6353. */
  6354. if (wc->stage == UPDATE_BACKREF &&
  6355. generation <= root->root_key.offset) {
  6356. *lookup_info = 1;
  6357. return 1;
  6358. }
  6359. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  6360. blocksize = btrfs_level_size(root, level - 1);
  6361. next = btrfs_find_tree_block(root, bytenr, blocksize);
  6362. if (!next) {
  6363. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6364. if (!next)
  6365. return -ENOMEM;
  6366. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  6367. level - 1);
  6368. reada = 1;
  6369. }
  6370. btrfs_tree_lock(next);
  6371. btrfs_set_lock_blocking(next);
  6372. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  6373. &wc->refs[level - 1],
  6374. &wc->flags[level - 1]);
  6375. if (ret < 0) {
  6376. btrfs_tree_unlock(next);
  6377. return ret;
  6378. }
  6379. if (unlikely(wc->refs[level - 1] == 0)) {
  6380. btrfs_err(root->fs_info, "Missing references.");
  6381. BUG();
  6382. }
  6383. *lookup_info = 0;
  6384. if (wc->stage == DROP_REFERENCE) {
  6385. if (wc->refs[level - 1] > 1) {
  6386. if (level == 1 &&
  6387. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6388. goto skip;
  6389. if (!wc->update_ref ||
  6390. generation <= root->root_key.offset)
  6391. goto skip;
  6392. btrfs_node_key_to_cpu(path->nodes[level], &key,
  6393. path->slots[level]);
  6394. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  6395. if (ret < 0)
  6396. goto skip;
  6397. wc->stage = UPDATE_BACKREF;
  6398. wc->shared_level = level - 1;
  6399. }
  6400. } else {
  6401. if (level == 1 &&
  6402. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6403. goto skip;
  6404. }
  6405. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  6406. btrfs_tree_unlock(next);
  6407. free_extent_buffer(next);
  6408. next = NULL;
  6409. *lookup_info = 1;
  6410. }
  6411. if (!next) {
  6412. if (reada && level == 1)
  6413. reada_walk_down(trans, root, wc, path);
  6414. next = read_tree_block(root, bytenr, blocksize, generation);
  6415. if (!next || !extent_buffer_uptodate(next)) {
  6416. free_extent_buffer(next);
  6417. return -EIO;
  6418. }
  6419. btrfs_tree_lock(next);
  6420. btrfs_set_lock_blocking(next);
  6421. }
  6422. level--;
  6423. BUG_ON(level != btrfs_header_level(next));
  6424. path->nodes[level] = next;
  6425. path->slots[level] = 0;
  6426. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6427. wc->level = level;
  6428. if (wc->level == 1)
  6429. wc->reada_slot = 0;
  6430. return 0;
  6431. skip:
  6432. wc->refs[level - 1] = 0;
  6433. wc->flags[level - 1] = 0;
  6434. if (wc->stage == DROP_REFERENCE) {
  6435. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  6436. parent = path->nodes[level]->start;
  6437. } else {
  6438. BUG_ON(root->root_key.objectid !=
  6439. btrfs_header_owner(path->nodes[level]));
  6440. parent = 0;
  6441. }
  6442. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  6443. root->root_key.objectid, level - 1, 0, 0);
  6444. BUG_ON(ret); /* -ENOMEM */
  6445. }
  6446. btrfs_tree_unlock(next);
  6447. free_extent_buffer(next);
  6448. *lookup_info = 1;
  6449. return 1;
  6450. }
  6451. /*
  6452. * helper to process tree block while walking up the tree.
  6453. *
  6454. * when wc->stage == DROP_REFERENCE, this function drops
  6455. * reference count on the block.
  6456. *
  6457. * when wc->stage == UPDATE_BACKREF, this function changes
  6458. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  6459. * to UPDATE_BACKREF previously while processing the block.
  6460. *
  6461. * NOTE: return value 1 means we should stop walking up.
  6462. */
  6463. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  6464. struct btrfs_root *root,
  6465. struct btrfs_path *path,
  6466. struct walk_control *wc)
  6467. {
  6468. int ret;
  6469. int level = wc->level;
  6470. struct extent_buffer *eb = path->nodes[level];
  6471. u64 parent = 0;
  6472. if (wc->stage == UPDATE_BACKREF) {
  6473. BUG_ON(wc->shared_level < level);
  6474. if (level < wc->shared_level)
  6475. goto out;
  6476. ret = find_next_key(path, level + 1, &wc->update_progress);
  6477. if (ret > 0)
  6478. wc->update_ref = 0;
  6479. wc->stage = DROP_REFERENCE;
  6480. wc->shared_level = -1;
  6481. path->slots[level] = 0;
  6482. /*
  6483. * check reference count again if the block isn't locked.
  6484. * we should start walking down the tree again if reference
  6485. * count is one.
  6486. */
  6487. if (!path->locks[level]) {
  6488. BUG_ON(level == 0);
  6489. btrfs_tree_lock(eb);
  6490. btrfs_set_lock_blocking(eb);
  6491. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6492. ret = btrfs_lookup_extent_info(trans, root,
  6493. eb->start, level, 1,
  6494. &wc->refs[level],
  6495. &wc->flags[level]);
  6496. if (ret < 0) {
  6497. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6498. path->locks[level] = 0;
  6499. return ret;
  6500. }
  6501. BUG_ON(wc->refs[level] == 0);
  6502. if (wc->refs[level] == 1) {
  6503. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6504. path->locks[level] = 0;
  6505. return 1;
  6506. }
  6507. }
  6508. }
  6509. /* wc->stage == DROP_REFERENCE */
  6510. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6511. if (wc->refs[level] == 1) {
  6512. if (level == 0) {
  6513. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6514. ret = btrfs_dec_ref(trans, root, eb, 1,
  6515. wc->for_reloc);
  6516. else
  6517. ret = btrfs_dec_ref(trans, root, eb, 0,
  6518. wc->for_reloc);
  6519. BUG_ON(ret); /* -ENOMEM */
  6520. }
  6521. /* make block locked assertion in clean_tree_block happy */
  6522. if (!path->locks[level] &&
  6523. btrfs_header_generation(eb) == trans->transid) {
  6524. btrfs_tree_lock(eb);
  6525. btrfs_set_lock_blocking(eb);
  6526. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6527. }
  6528. clean_tree_block(trans, root, eb);
  6529. }
  6530. if (eb == root->node) {
  6531. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6532. parent = eb->start;
  6533. else
  6534. BUG_ON(root->root_key.objectid !=
  6535. btrfs_header_owner(eb));
  6536. } else {
  6537. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6538. parent = path->nodes[level + 1]->start;
  6539. else
  6540. BUG_ON(root->root_key.objectid !=
  6541. btrfs_header_owner(path->nodes[level + 1]));
  6542. }
  6543. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6544. out:
  6545. wc->refs[level] = 0;
  6546. wc->flags[level] = 0;
  6547. return 0;
  6548. }
  6549. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6550. struct btrfs_root *root,
  6551. struct btrfs_path *path,
  6552. struct walk_control *wc)
  6553. {
  6554. int level = wc->level;
  6555. int lookup_info = 1;
  6556. int ret;
  6557. while (level >= 0) {
  6558. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6559. if (ret > 0)
  6560. break;
  6561. if (level == 0)
  6562. break;
  6563. if (path->slots[level] >=
  6564. btrfs_header_nritems(path->nodes[level]))
  6565. break;
  6566. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6567. if (ret > 0) {
  6568. path->slots[level]++;
  6569. continue;
  6570. } else if (ret < 0)
  6571. return ret;
  6572. level = wc->level;
  6573. }
  6574. return 0;
  6575. }
  6576. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6577. struct btrfs_root *root,
  6578. struct btrfs_path *path,
  6579. struct walk_control *wc, int max_level)
  6580. {
  6581. int level = wc->level;
  6582. int ret;
  6583. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6584. while (level < max_level && path->nodes[level]) {
  6585. wc->level = level;
  6586. if (path->slots[level] + 1 <
  6587. btrfs_header_nritems(path->nodes[level])) {
  6588. path->slots[level]++;
  6589. return 0;
  6590. } else {
  6591. ret = walk_up_proc(trans, root, path, wc);
  6592. if (ret > 0)
  6593. return 0;
  6594. if (path->locks[level]) {
  6595. btrfs_tree_unlock_rw(path->nodes[level],
  6596. path->locks[level]);
  6597. path->locks[level] = 0;
  6598. }
  6599. free_extent_buffer(path->nodes[level]);
  6600. path->nodes[level] = NULL;
  6601. level++;
  6602. }
  6603. }
  6604. return 1;
  6605. }
  6606. /*
  6607. * drop a subvolume tree.
  6608. *
  6609. * this function traverses the tree freeing any blocks that only
  6610. * referenced by the tree.
  6611. *
  6612. * when a shared tree block is found. this function decreases its
  6613. * reference count by one. if update_ref is true, this function
  6614. * also make sure backrefs for the shared block and all lower level
  6615. * blocks are properly updated.
  6616. *
  6617. * If called with for_reloc == 0, may exit early with -EAGAIN
  6618. */
  6619. int btrfs_drop_snapshot(struct btrfs_root *root,
  6620. struct btrfs_block_rsv *block_rsv, int update_ref,
  6621. int for_reloc)
  6622. {
  6623. struct btrfs_path *path;
  6624. struct btrfs_trans_handle *trans;
  6625. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6626. struct btrfs_root_item *root_item = &root->root_item;
  6627. struct walk_control *wc;
  6628. struct btrfs_key key;
  6629. int err = 0;
  6630. int ret;
  6631. int level;
  6632. bool root_dropped = false;
  6633. path = btrfs_alloc_path();
  6634. if (!path) {
  6635. err = -ENOMEM;
  6636. goto out;
  6637. }
  6638. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6639. if (!wc) {
  6640. btrfs_free_path(path);
  6641. err = -ENOMEM;
  6642. goto out;
  6643. }
  6644. trans = btrfs_start_transaction(tree_root, 0);
  6645. if (IS_ERR(trans)) {
  6646. err = PTR_ERR(trans);
  6647. goto out_free;
  6648. }
  6649. if (block_rsv)
  6650. trans->block_rsv = block_rsv;
  6651. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6652. level = btrfs_header_level(root->node);
  6653. path->nodes[level] = btrfs_lock_root_node(root);
  6654. btrfs_set_lock_blocking(path->nodes[level]);
  6655. path->slots[level] = 0;
  6656. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6657. memset(&wc->update_progress, 0,
  6658. sizeof(wc->update_progress));
  6659. } else {
  6660. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6661. memcpy(&wc->update_progress, &key,
  6662. sizeof(wc->update_progress));
  6663. level = root_item->drop_level;
  6664. BUG_ON(level == 0);
  6665. path->lowest_level = level;
  6666. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6667. path->lowest_level = 0;
  6668. if (ret < 0) {
  6669. err = ret;
  6670. goto out_end_trans;
  6671. }
  6672. WARN_ON(ret > 0);
  6673. /*
  6674. * unlock our path, this is safe because only this
  6675. * function is allowed to delete this snapshot
  6676. */
  6677. btrfs_unlock_up_safe(path, 0);
  6678. level = btrfs_header_level(root->node);
  6679. while (1) {
  6680. btrfs_tree_lock(path->nodes[level]);
  6681. btrfs_set_lock_blocking(path->nodes[level]);
  6682. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6683. ret = btrfs_lookup_extent_info(trans, root,
  6684. path->nodes[level]->start,
  6685. level, 1, &wc->refs[level],
  6686. &wc->flags[level]);
  6687. if (ret < 0) {
  6688. err = ret;
  6689. goto out_end_trans;
  6690. }
  6691. BUG_ON(wc->refs[level] == 0);
  6692. if (level == root_item->drop_level)
  6693. break;
  6694. btrfs_tree_unlock(path->nodes[level]);
  6695. path->locks[level] = 0;
  6696. WARN_ON(wc->refs[level] != 1);
  6697. level--;
  6698. }
  6699. }
  6700. wc->level = level;
  6701. wc->shared_level = -1;
  6702. wc->stage = DROP_REFERENCE;
  6703. wc->update_ref = update_ref;
  6704. wc->keep_locks = 0;
  6705. wc->for_reloc = for_reloc;
  6706. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6707. while (1) {
  6708. ret = walk_down_tree(trans, root, path, wc);
  6709. if (ret < 0) {
  6710. err = ret;
  6711. break;
  6712. }
  6713. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6714. if (ret < 0) {
  6715. err = ret;
  6716. break;
  6717. }
  6718. if (ret > 0) {
  6719. BUG_ON(wc->stage != DROP_REFERENCE);
  6720. break;
  6721. }
  6722. if (wc->stage == DROP_REFERENCE) {
  6723. level = wc->level;
  6724. btrfs_node_key(path->nodes[level],
  6725. &root_item->drop_progress,
  6726. path->slots[level]);
  6727. root_item->drop_level = level;
  6728. }
  6729. BUG_ON(wc->level == 0);
  6730. if (btrfs_should_end_transaction(trans, tree_root) ||
  6731. (!for_reloc && btrfs_need_cleaner_sleep(root))) {
  6732. ret = btrfs_update_root(trans, tree_root,
  6733. &root->root_key,
  6734. root_item);
  6735. if (ret) {
  6736. btrfs_abort_transaction(trans, tree_root, ret);
  6737. err = ret;
  6738. goto out_end_trans;
  6739. }
  6740. btrfs_end_transaction_throttle(trans, tree_root);
  6741. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  6742. pr_debug("btrfs: drop snapshot early exit\n");
  6743. err = -EAGAIN;
  6744. goto out_free;
  6745. }
  6746. trans = btrfs_start_transaction(tree_root, 0);
  6747. if (IS_ERR(trans)) {
  6748. err = PTR_ERR(trans);
  6749. goto out_free;
  6750. }
  6751. if (block_rsv)
  6752. trans->block_rsv = block_rsv;
  6753. }
  6754. }
  6755. btrfs_release_path(path);
  6756. if (err)
  6757. goto out_end_trans;
  6758. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6759. if (ret) {
  6760. btrfs_abort_transaction(trans, tree_root, ret);
  6761. goto out_end_trans;
  6762. }
  6763. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6764. ret = btrfs_find_root(tree_root, &root->root_key, path,
  6765. NULL, NULL);
  6766. if (ret < 0) {
  6767. btrfs_abort_transaction(trans, tree_root, ret);
  6768. err = ret;
  6769. goto out_end_trans;
  6770. } else if (ret > 0) {
  6771. /* if we fail to delete the orphan item this time
  6772. * around, it'll get picked up the next time.
  6773. *
  6774. * The most common failure here is just -ENOENT.
  6775. */
  6776. btrfs_del_orphan_item(trans, tree_root,
  6777. root->root_key.objectid);
  6778. }
  6779. }
  6780. if (root->in_radix) {
  6781. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  6782. } else {
  6783. free_extent_buffer(root->node);
  6784. free_extent_buffer(root->commit_root);
  6785. btrfs_put_fs_root(root);
  6786. }
  6787. root_dropped = true;
  6788. out_end_trans:
  6789. btrfs_end_transaction_throttle(trans, tree_root);
  6790. out_free:
  6791. kfree(wc);
  6792. btrfs_free_path(path);
  6793. out:
  6794. /*
  6795. * So if we need to stop dropping the snapshot for whatever reason we
  6796. * need to make sure to add it back to the dead root list so that we
  6797. * keep trying to do the work later. This also cleans up roots if we
  6798. * don't have it in the radix (like when we recover after a power fail
  6799. * or unmount) so we don't leak memory.
  6800. */
  6801. if (!for_reloc && root_dropped == false)
  6802. btrfs_add_dead_root(root);
  6803. if (err)
  6804. btrfs_std_error(root->fs_info, err);
  6805. return err;
  6806. }
  6807. /*
  6808. * drop subtree rooted at tree block 'node'.
  6809. *
  6810. * NOTE: this function will unlock and release tree block 'node'
  6811. * only used by relocation code
  6812. */
  6813. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6814. struct btrfs_root *root,
  6815. struct extent_buffer *node,
  6816. struct extent_buffer *parent)
  6817. {
  6818. struct btrfs_path *path;
  6819. struct walk_control *wc;
  6820. int level;
  6821. int parent_level;
  6822. int ret = 0;
  6823. int wret;
  6824. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6825. path = btrfs_alloc_path();
  6826. if (!path)
  6827. return -ENOMEM;
  6828. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6829. if (!wc) {
  6830. btrfs_free_path(path);
  6831. return -ENOMEM;
  6832. }
  6833. btrfs_assert_tree_locked(parent);
  6834. parent_level = btrfs_header_level(parent);
  6835. extent_buffer_get(parent);
  6836. path->nodes[parent_level] = parent;
  6837. path->slots[parent_level] = btrfs_header_nritems(parent);
  6838. btrfs_assert_tree_locked(node);
  6839. level = btrfs_header_level(node);
  6840. path->nodes[level] = node;
  6841. path->slots[level] = 0;
  6842. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6843. wc->refs[parent_level] = 1;
  6844. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6845. wc->level = level;
  6846. wc->shared_level = -1;
  6847. wc->stage = DROP_REFERENCE;
  6848. wc->update_ref = 0;
  6849. wc->keep_locks = 1;
  6850. wc->for_reloc = 1;
  6851. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6852. while (1) {
  6853. wret = walk_down_tree(trans, root, path, wc);
  6854. if (wret < 0) {
  6855. ret = wret;
  6856. break;
  6857. }
  6858. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6859. if (wret < 0)
  6860. ret = wret;
  6861. if (wret != 0)
  6862. break;
  6863. }
  6864. kfree(wc);
  6865. btrfs_free_path(path);
  6866. return ret;
  6867. }
  6868. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6869. {
  6870. u64 num_devices;
  6871. u64 stripped;
  6872. /*
  6873. * if restripe for this chunk_type is on pick target profile and
  6874. * return, otherwise do the usual balance
  6875. */
  6876. stripped = get_restripe_target(root->fs_info, flags);
  6877. if (stripped)
  6878. return extended_to_chunk(stripped);
  6879. /*
  6880. * we add in the count of missing devices because we want
  6881. * to make sure that any RAID levels on a degraded FS
  6882. * continue to be honored.
  6883. */
  6884. num_devices = root->fs_info->fs_devices->rw_devices +
  6885. root->fs_info->fs_devices->missing_devices;
  6886. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6887. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  6888. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6889. if (num_devices == 1) {
  6890. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6891. stripped = flags & ~stripped;
  6892. /* turn raid0 into single device chunks */
  6893. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6894. return stripped;
  6895. /* turn mirroring into duplication */
  6896. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6897. BTRFS_BLOCK_GROUP_RAID10))
  6898. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6899. } else {
  6900. /* they already had raid on here, just return */
  6901. if (flags & stripped)
  6902. return flags;
  6903. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6904. stripped = flags & ~stripped;
  6905. /* switch duplicated blocks with raid1 */
  6906. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6907. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6908. /* this is drive concat, leave it alone */
  6909. }
  6910. return flags;
  6911. }
  6912. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6913. {
  6914. struct btrfs_space_info *sinfo = cache->space_info;
  6915. u64 num_bytes;
  6916. u64 min_allocable_bytes;
  6917. int ret = -ENOSPC;
  6918. /*
  6919. * We need some metadata space and system metadata space for
  6920. * allocating chunks in some corner cases until we force to set
  6921. * it to be readonly.
  6922. */
  6923. if ((sinfo->flags &
  6924. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6925. !force)
  6926. min_allocable_bytes = 1 * 1024 * 1024;
  6927. else
  6928. min_allocable_bytes = 0;
  6929. spin_lock(&sinfo->lock);
  6930. spin_lock(&cache->lock);
  6931. if (cache->ro) {
  6932. ret = 0;
  6933. goto out;
  6934. }
  6935. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6936. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6937. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6938. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6939. min_allocable_bytes <= sinfo->total_bytes) {
  6940. sinfo->bytes_readonly += num_bytes;
  6941. cache->ro = 1;
  6942. ret = 0;
  6943. }
  6944. out:
  6945. spin_unlock(&cache->lock);
  6946. spin_unlock(&sinfo->lock);
  6947. return ret;
  6948. }
  6949. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6950. struct btrfs_block_group_cache *cache)
  6951. {
  6952. struct btrfs_trans_handle *trans;
  6953. u64 alloc_flags;
  6954. int ret;
  6955. BUG_ON(cache->ro);
  6956. trans = btrfs_join_transaction(root);
  6957. if (IS_ERR(trans))
  6958. return PTR_ERR(trans);
  6959. alloc_flags = update_block_group_flags(root, cache->flags);
  6960. if (alloc_flags != cache->flags) {
  6961. ret = do_chunk_alloc(trans, root, alloc_flags,
  6962. CHUNK_ALLOC_FORCE);
  6963. if (ret < 0)
  6964. goto out;
  6965. }
  6966. ret = set_block_group_ro(cache, 0);
  6967. if (!ret)
  6968. goto out;
  6969. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6970. ret = do_chunk_alloc(trans, root, alloc_flags,
  6971. CHUNK_ALLOC_FORCE);
  6972. if (ret < 0)
  6973. goto out;
  6974. ret = set_block_group_ro(cache, 0);
  6975. out:
  6976. btrfs_end_transaction(trans, root);
  6977. return ret;
  6978. }
  6979. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6980. struct btrfs_root *root, u64 type)
  6981. {
  6982. u64 alloc_flags = get_alloc_profile(root, type);
  6983. return do_chunk_alloc(trans, root, alloc_flags,
  6984. CHUNK_ALLOC_FORCE);
  6985. }
  6986. /*
  6987. * helper to account the unused space of all the readonly block group in the
  6988. * list. takes mirrors into account.
  6989. */
  6990. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6991. {
  6992. struct btrfs_block_group_cache *block_group;
  6993. u64 free_bytes = 0;
  6994. int factor;
  6995. list_for_each_entry(block_group, groups_list, list) {
  6996. spin_lock(&block_group->lock);
  6997. if (!block_group->ro) {
  6998. spin_unlock(&block_group->lock);
  6999. continue;
  7000. }
  7001. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7002. BTRFS_BLOCK_GROUP_RAID10 |
  7003. BTRFS_BLOCK_GROUP_DUP))
  7004. factor = 2;
  7005. else
  7006. factor = 1;
  7007. free_bytes += (block_group->key.offset -
  7008. btrfs_block_group_used(&block_group->item)) *
  7009. factor;
  7010. spin_unlock(&block_group->lock);
  7011. }
  7012. return free_bytes;
  7013. }
  7014. /*
  7015. * helper to account the unused space of all the readonly block group in the
  7016. * space_info. takes mirrors into account.
  7017. */
  7018. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  7019. {
  7020. int i;
  7021. u64 free_bytes = 0;
  7022. spin_lock(&sinfo->lock);
  7023. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  7024. if (!list_empty(&sinfo->block_groups[i]))
  7025. free_bytes += __btrfs_get_ro_block_group_free_space(
  7026. &sinfo->block_groups[i]);
  7027. spin_unlock(&sinfo->lock);
  7028. return free_bytes;
  7029. }
  7030. void btrfs_set_block_group_rw(struct btrfs_root *root,
  7031. struct btrfs_block_group_cache *cache)
  7032. {
  7033. struct btrfs_space_info *sinfo = cache->space_info;
  7034. u64 num_bytes;
  7035. BUG_ON(!cache->ro);
  7036. spin_lock(&sinfo->lock);
  7037. spin_lock(&cache->lock);
  7038. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7039. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7040. sinfo->bytes_readonly -= num_bytes;
  7041. cache->ro = 0;
  7042. spin_unlock(&cache->lock);
  7043. spin_unlock(&sinfo->lock);
  7044. }
  7045. /*
  7046. * checks to see if its even possible to relocate this block group.
  7047. *
  7048. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7049. * ok to go ahead and try.
  7050. */
  7051. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7052. {
  7053. struct btrfs_block_group_cache *block_group;
  7054. struct btrfs_space_info *space_info;
  7055. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7056. struct btrfs_device *device;
  7057. struct btrfs_trans_handle *trans;
  7058. u64 min_free;
  7059. u64 dev_min = 1;
  7060. u64 dev_nr = 0;
  7061. u64 target;
  7062. int index;
  7063. int full = 0;
  7064. int ret = 0;
  7065. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7066. /* odd, couldn't find the block group, leave it alone */
  7067. if (!block_group)
  7068. return -1;
  7069. min_free = btrfs_block_group_used(&block_group->item);
  7070. /* no bytes used, we're good */
  7071. if (!min_free)
  7072. goto out;
  7073. space_info = block_group->space_info;
  7074. spin_lock(&space_info->lock);
  7075. full = space_info->full;
  7076. /*
  7077. * if this is the last block group we have in this space, we can't
  7078. * relocate it unless we're able to allocate a new chunk below.
  7079. *
  7080. * Otherwise, we need to make sure we have room in the space to handle
  7081. * all of the extents from this block group. If we can, we're good
  7082. */
  7083. if ((space_info->total_bytes != block_group->key.offset) &&
  7084. (space_info->bytes_used + space_info->bytes_reserved +
  7085. space_info->bytes_pinned + space_info->bytes_readonly +
  7086. min_free < space_info->total_bytes)) {
  7087. spin_unlock(&space_info->lock);
  7088. goto out;
  7089. }
  7090. spin_unlock(&space_info->lock);
  7091. /*
  7092. * ok we don't have enough space, but maybe we have free space on our
  7093. * devices to allocate new chunks for relocation, so loop through our
  7094. * alloc devices and guess if we have enough space. if this block
  7095. * group is going to be restriped, run checks against the target
  7096. * profile instead of the current one.
  7097. */
  7098. ret = -1;
  7099. /*
  7100. * index:
  7101. * 0: raid10
  7102. * 1: raid1
  7103. * 2: dup
  7104. * 3: raid0
  7105. * 4: single
  7106. */
  7107. target = get_restripe_target(root->fs_info, block_group->flags);
  7108. if (target) {
  7109. index = __get_raid_index(extended_to_chunk(target));
  7110. } else {
  7111. /*
  7112. * this is just a balance, so if we were marked as full
  7113. * we know there is no space for a new chunk
  7114. */
  7115. if (full)
  7116. goto out;
  7117. index = get_block_group_index(block_group);
  7118. }
  7119. if (index == BTRFS_RAID_RAID10) {
  7120. dev_min = 4;
  7121. /* Divide by 2 */
  7122. min_free >>= 1;
  7123. } else if (index == BTRFS_RAID_RAID1) {
  7124. dev_min = 2;
  7125. } else if (index == BTRFS_RAID_DUP) {
  7126. /* Multiply by 2 */
  7127. min_free <<= 1;
  7128. } else if (index == BTRFS_RAID_RAID0) {
  7129. dev_min = fs_devices->rw_devices;
  7130. do_div(min_free, dev_min);
  7131. }
  7132. /* We need to do this so that we can look at pending chunks */
  7133. trans = btrfs_join_transaction(root);
  7134. if (IS_ERR(trans)) {
  7135. ret = PTR_ERR(trans);
  7136. goto out;
  7137. }
  7138. mutex_lock(&root->fs_info->chunk_mutex);
  7139. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7140. u64 dev_offset;
  7141. /*
  7142. * check to make sure we can actually find a chunk with enough
  7143. * space to fit our block group in.
  7144. */
  7145. if (device->total_bytes > device->bytes_used + min_free &&
  7146. !device->is_tgtdev_for_dev_replace) {
  7147. ret = find_free_dev_extent(trans, device, min_free,
  7148. &dev_offset, NULL);
  7149. if (!ret)
  7150. dev_nr++;
  7151. if (dev_nr >= dev_min)
  7152. break;
  7153. ret = -1;
  7154. }
  7155. }
  7156. mutex_unlock(&root->fs_info->chunk_mutex);
  7157. btrfs_end_transaction(trans, root);
  7158. out:
  7159. btrfs_put_block_group(block_group);
  7160. return ret;
  7161. }
  7162. static int find_first_block_group(struct btrfs_root *root,
  7163. struct btrfs_path *path, struct btrfs_key *key)
  7164. {
  7165. int ret = 0;
  7166. struct btrfs_key found_key;
  7167. struct extent_buffer *leaf;
  7168. int slot;
  7169. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7170. if (ret < 0)
  7171. goto out;
  7172. while (1) {
  7173. slot = path->slots[0];
  7174. leaf = path->nodes[0];
  7175. if (slot >= btrfs_header_nritems(leaf)) {
  7176. ret = btrfs_next_leaf(root, path);
  7177. if (ret == 0)
  7178. continue;
  7179. if (ret < 0)
  7180. goto out;
  7181. break;
  7182. }
  7183. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7184. if (found_key.objectid >= key->objectid &&
  7185. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7186. ret = 0;
  7187. goto out;
  7188. }
  7189. path->slots[0]++;
  7190. }
  7191. out:
  7192. return ret;
  7193. }
  7194. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7195. {
  7196. struct btrfs_block_group_cache *block_group;
  7197. u64 last = 0;
  7198. while (1) {
  7199. struct inode *inode;
  7200. block_group = btrfs_lookup_first_block_group(info, last);
  7201. while (block_group) {
  7202. spin_lock(&block_group->lock);
  7203. if (block_group->iref)
  7204. break;
  7205. spin_unlock(&block_group->lock);
  7206. block_group = next_block_group(info->tree_root,
  7207. block_group);
  7208. }
  7209. if (!block_group) {
  7210. if (last == 0)
  7211. break;
  7212. last = 0;
  7213. continue;
  7214. }
  7215. inode = block_group->inode;
  7216. block_group->iref = 0;
  7217. block_group->inode = NULL;
  7218. spin_unlock(&block_group->lock);
  7219. iput(inode);
  7220. last = block_group->key.objectid + block_group->key.offset;
  7221. btrfs_put_block_group(block_group);
  7222. }
  7223. }
  7224. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7225. {
  7226. struct btrfs_block_group_cache *block_group;
  7227. struct btrfs_space_info *space_info;
  7228. struct btrfs_caching_control *caching_ctl;
  7229. struct rb_node *n;
  7230. down_write(&info->extent_commit_sem);
  7231. while (!list_empty(&info->caching_block_groups)) {
  7232. caching_ctl = list_entry(info->caching_block_groups.next,
  7233. struct btrfs_caching_control, list);
  7234. list_del(&caching_ctl->list);
  7235. put_caching_control(caching_ctl);
  7236. }
  7237. up_write(&info->extent_commit_sem);
  7238. spin_lock(&info->block_group_cache_lock);
  7239. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7240. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7241. cache_node);
  7242. rb_erase(&block_group->cache_node,
  7243. &info->block_group_cache_tree);
  7244. spin_unlock(&info->block_group_cache_lock);
  7245. down_write(&block_group->space_info->groups_sem);
  7246. list_del(&block_group->list);
  7247. up_write(&block_group->space_info->groups_sem);
  7248. if (block_group->cached == BTRFS_CACHE_STARTED)
  7249. wait_block_group_cache_done(block_group);
  7250. /*
  7251. * We haven't cached this block group, which means we could
  7252. * possibly have excluded extents on this block group.
  7253. */
  7254. if (block_group->cached == BTRFS_CACHE_NO ||
  7255. block_group->cached == BTRFS_CACHE_ERROR)
  7256. free_excluded_extents(info->extent_root, block_group);
  7257. btrfs_remove_free_space_cache(block_group);
  7258. btrfs_put_block_group(block_group);
  7259. spin_lock(&info->block_group_cache_lock);
  7260. }
  7261. spin_unlock(&info->block_group_cache_lock);
  7262. /* now that all the block groups are freed, go through and
  7263. * free all the space_info structs. This is only called during
  7264. * the final stages of unmount, and so we know nobody is
  7265. * using them. We call synchronize_rcu() once before we start,
  7266. * just to be on the safe side.
  7267. */
  7268. synchronize_rcu();
  7269. release_global_block_rsv(info);
  7270. while(!list_empty(&info->space_info)) {
  7271. space_info = list_entry(info->space_info.next,
  7272. struct btrfs_space_info,
  7273. list);
  7274. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  7275. if (space_info->bytes_pinned > 0 ||
  7276. space_info->bytes_reserved > 0 ||
  7277. space_info->bytes_may_use > 0) {
  7278. WARN_ON(1);
  7279. dump_space_info(space_info, 0, 0);
  7280. }
  7281. }
  7282. percpu_counter_destroy(&space_info->total_bytes_pinned);
  7283. list_del(&space_info->list);
  7284. kfree(space_info);
  7285. }
  7286. return 0;
  7287. }
  7288. static void __link_block_group(struct btrfs_space_info *space_info,
  7289. struct btrfs_block_group_cache *cache)
  7290. {
  7291. int index = get_block_group_index(cache);
  7292. down_write(&space_info->groups_sem);
  7293. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7294. up_write(&space_info->groups_sem);
  7295. }
  7296. int btrfs_read_block_groups(struct btrfs_root *root)
  7297. {
  7298. struct btrfs_path *path;
  7299. int ret;
  7300. struct btrfs_block_group_cache *cache;
  7301. struct btrfs_fs_info *info = root->fs_info;
  7302. struct btrfs_space_info *space_info;
  7303. struct btrfs_key key;
  7304. struct btrfs_key found_key;
  7305. struct extent_buffer *leaf;
  7306. int need_clear = 0;
  7307. u64 cache_gen;
  7308. root = info->extent_root;
  7309. key.objectid = 0;
  7310. key.offset = 0;
  7311. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7312. path = btrfs_alloc_path();
  7313. if (!path)
  7314. return -ENOMEM;
  7315. path->reada = 1;
  7316. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  7317. if (btrfs_test_opt(root, SPACE_CACHE) &&
  7318. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  7319. need_clear = 1;
  7320. if (btrfs_test_opt(root, CLEAR_CACHE))
  7321. need_clear = 1;
  7322. while (1) {
  7323. ret = find_first_block_group(root, path, &key);
  7324. if (ret > 0)
  7325. break;
  7326. if (ret != 0)
  7327. goto error;
  7328. leaf = path->nodes[0];
  7329. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7330. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7331. if (!cache) {
  7332. ret = -ENOMEM;
  7333. goto error;
  7334. }
  7335. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7336. GFP_NOFS);
  7337. if (!cache->free_space_ctl) {
  7338. kfree(cache);
  7339. ret = -ENOMEM;
  7340. goto error;
  7341. }
  7342. atomic_set(&cache->count, 1);
  7343. spin_lock_init(&cache->lock);
  7344. cache->fs_info = info;
  7345. INIT_LIST_HEAD(&cache->list);
  7346. INIT_LIST_HEAD(&cache->cluster_list);
  7347. if (need_clear) {
  7348. /*
  7349. * When we mount with old space cache, we need to
  7350. * set BTRFS_DC_CLEAR and set dirty flag.
  7351. *
  7352. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  7353. * truncate the old free space cache inode and
  7354. * setup a new one.
  7355. * b) Setting 'dirty flag' makes sure that we flush
  7356. * the new space cache info onto disk.
  7357. */
  7358. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7359. if (btrfs_test_opt(root, SPACE_CACHE))
  7360. cache->dirty = 1;
  7361. }
  7362. read_extent_buffer(leaf, &cache->item,
  7363. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7364. sizeof(cache->item));
  7365. memcpy(&cache->key, &found_key, sizeof(found_key));
  7366. key.objectid = found_key.objectid + found_key.offset;
  7367. btrfs_release_path(path);
  7368. cache->flags = btrfs_block_group_flags(&cache->item);
  7369. cache->sectorsize = root->sectorsize;
  7370. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7371. &root->fs_info->mapping_tree,
  7372. found_key.objectid);
  7373. btrfs_init_free_space_ctl(cache);
  7374. /*
  7375. * We need to exclude the super stripes now so that the space
  7376. * info has super bytes accounted for, otherwise we'll think
  7377. * we have more space than we actually do.
  7378. */
  7379. ret = exclude_super_stripes(root, cache);
  7380. if (ret) {
  7381. /*
  7382. * We may have excluded something, so call this just in
  7383. * case.
  7384. */
  7385. free_excluded_extents(root, cache);
  7386. kfree(cache->free_space_ctl);
  7387. kfree(cache);
  7388. goto error;
  7389. }
  7390. /*
  7391. * check for two cases, either we are full, and therefore
  7392. * don't need to bother with the caching work since we won't
  7393. * find any space, or we are empty, and we can just add all
  7394. * the space in and be done with it. This saves us _alot_ of
  7395. * time, particularly in the full case.
  7396. */
  7397. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7398. cache->last_byte_to_unpin = (u64)-1;
  7399. cache->cached = BTRFS_CACHE_FINISHED;
  7400. free_excluded_extents(root, cache);
  7401. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7402. cache->last_byte_to_unpin = (u64)-1;
  7403. cache->cached = BTRFS_CACHE_FINISHED;
  7404. add_new_free_space(cache, root->fs_info,
  7405. found_key.objectid,
  7406. found_key.objectid +
  7407. found_key.offset);
  7408. free_excluded_extents(root, cache);
  7409. }
  7410. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7411. if (ret) {
  7412. btrfs_remove_free_space_cache(cache);
  7413. btrfs_put_block_group(cache);
  7414. goto error;
  7415. }
  7416. ret = update_space_info(info, cache->flags, found_key.offset,
  7417. btrfs_block_group_used(&cache->item),
  7418. &space_info);
  7419. if (ret) {
  7420. btrfs_remove_free_space_cache(cache);
  7421. spin_lock(&info->block_group_cache_lock);
  7422. rb_erase(&cache->cache_node,
  7423. &info->block_group_cache_tree);
  7424. spin_unlock(&info->block_group_cache_lock);
  7425. btrfs_put_block_group(cache);
  7426. goto error;
  7427. }
  7428. cache->space_info = space_info;
  7429. spin_lock(&cache->space_info->lock);
  7430. cache->space_info->bytes_readonly += cache->bytes_super;
  7431. spin_unlock(&cache->space_info->lock);
  7432. __link_block_group(space_info, cache);
  7433. set_avail_alloc_bits(root->fs_info, cache->flags);
  7434. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7435. set_block_group_ro(cache, 1);
  7436. }
  7437. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7438. if (!(get_alloc_profile(root, space_info->flags) &
  7439. (BTRFS_BLOCK_GROUP_RAID10 |
  7440. BTRFS_BLOCK_GROUP_RAID1 |
  7441. BTRFS_BLOCK_GROUP_RAID5 |
  7442. BTRFS_BLOCK_GROUP_RAID6 |
  7443. BTRFS_BLOCK_GROUP_DUP)))
  7444. continue;
  7445. /*
  7446. * avoid allocating from un-mirrored block group if there are
  7447. * mirrored block groups.
  7448. */
  7449. list_for_each_entry(cache,
  7450. &space_info->block_groups[BTRFS_RAID_RAID0],
  7451. list)
  7452. set_block_group_ro(cache, 1);
  7453. list_for_each_entry(cache,
  7454. &space_info->block_groups[BTRFS_RAID_SINGLE],
  7455. list)
  7456. set_block_group_ro(cache, 1);
  7457. }
  7458. init_global_block_rsv(info);
  7459. ret = 0;
  7460. error:
  7461. btrfs_free_path(path);
  7462. return ret;
  7463. }
  7464. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  7465. struct btrfs_root *root)
  7466. {
  7467. struct btrfs_block_group_cache *block_group, *tmp;
  7468. struct btrfs_root *extent_root = root->fs_info->extent_root;
  7469. struct btrfs_block_group_item item;
  7470. struct btrfs_key key;
  7471. int ret = 0;
  7472. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  7473. new_bg_list) {
  7474. list_del_init(&block_group->new_bg_list);
  7475. if (ret)
  7476. continue;
  7477. spin_lock(&block_group->lock);
  7478. memcpy(&item, &block_group->item, sizeof(item));
  7479. memcpy(&key, &block_group->key, sizeof(key));
  7480. spin_unlock(&block_group->lock);
  7481. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  7482. sizeof(item));
  7483. if (ret)
  7484. btrfs_abort_transaction(trans, extent_root, ret);
  7485. ret = btrfs_finish_chunk_alloc(trans, extent_root,
  7486. key.objectid, key.offset);
  7487. if (ret)
  7488. btrfs_abort_transaction(trans, extent_root, ret);
  7489. }
  7490. }
  7491. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7492. struct btrfs_root *root, u64 bytes_used,
  7493. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7494. u64 size)
  7495. {
  7496. int ret;
  7497. struct btrfs_root *extent_root;
  7498. struct btrfs_block_group_cache *cache;
  7499. extent_root = root->fs_info->extent_root;
  7500. root->fs_info->last_trans_log_full_commit = trans->transid;
  7501. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7502. if (!cache)
  7503. return -ENOMEM;
  7504. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7505. GFP_NOFS);
  7506. if (!cache->free_space_ctl) {
  7507. kfree(cache);
  7508. return -ENOMEM;
  7509. }
  7510. cache->key.objectid = chunk_offset;
  7511. cache->key.offset = size;
  7512. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7513. cache->sectorsize = root->sectorsize;
  7514. cache->fs_info = root->fs_info;
  7515. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7516. &root->fs_info->mapping_tree,
  7517. chunk_offset);
  7518. atomic_set(&cache->count, 1);
  7519. spin_lock_init(&cache->lock);
  7520. INIT_LIST_HEAD(&cache->list);
  7521. INIT_LIST_HEAD(&cache->cluster_list);
  7522. INIT_LIST_HEAD(&cache->new_bg_list);
  7523. btrfs_init_free_space_ctl(cache);
  7524. btrfs_set_block_group_used(&cache->item, bytes_used);
  7525. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7526. cache->flags = type;
  7527. btrfs_set_block_group_flags(&cache->item, type);
  7528. cache->last_byte_to_unpin = (u64)-1;
  7529. cache->cached = BTRFS_CACHE_FINISHED;
  7530. ret = exclude_super_stripes(root, cache);
  7531. if (ret) {
  7532. /*
  7533. * We may have excluded something, so call this just in
  7534. * case.
  7535. */
  7536. free_excluded_extents(root, cache);
  7537. kfree(cache->free_space_ctl);
  7538. kfree(cache);
  7539. return ret;
  7540. }
  7541. add_new_free_space(cache, root->fs_info, chunk_offset,
  7542. chunk_offset + size);
  7543. free_excluded_extents(root, cache);
  7544. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7545. if (ret) {
  7546. btrfs_remove_free_space_cache(cache);
  7547. btrfs_put_block_group(cache);
  7548. return ret;
  7549. }
  7550. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7551. &cache->space_info);
  7552. if (ret) {
  7553. btrfs_remove_free_space_cache(cache);
  7554. spin_lock(&root->fs_info->block_group_cache_lock);
  7555. rb_erase(&cache->cache_node,
  7556. &root->fs_info->block_group_cache_tree);
  7557. spin_unlock(&root->fs_info->block_group_cache_lock);
  7558. btrfs_put_block_group(cache);
  7559. return ret;
  7560. }
  7561. update_global_block_rsv(root->fs_info);
  7562. spin_lock(&cache->space_info->lock);
  7563. cache->space_info->bytes_readonly += cache->bytes_super;
  7564. spin_unlock(&cache->space_info->lock);
  7565. __link_block_group(cache->space_info, cache);
  7566. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  7567. set_avail_alloc_bits(extent_root->fs_info, type);
  7568. return 0;
  7569. }
  7570. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  7571. {
  7572. u64 extra_flags = chunk_to_extended(flags) &
  7573. BTRFS_EXTENDED_PROFILE_MASK;
  7574. write_seqlock(&fs_info->profiles_lock);
  7575. if (flags & BTRFS_BLOCK_GROUP_DATA)
  7576. fs_info->avail_data_alloc_bits &= ~extra_flags;
  7577. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  7578. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  7579. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  7580. fs_info->avail_system_alloc_bits &= ~extra_flags;
  7581. write_sequnlock(&fs_info->profiles_lock);
  7582. }
  7583. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7584. struct btrfs_root *root, u64 group_start)
  7585. {
  7586. struct btrfs_path *path;
  7587. struct btrfs_block_group_cache *block_group;
  7588. struct btrfs_free_cluster *cluster;
  7589. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7590. struct btrfs_key key;
  7591. struct inode *inode;
  7592. int ret;
  7593. int index;
  7594. int factor;
  7595. root = root->fs_info->extent_root;
  7596. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7597. BUG_ON(!block_group);
  7598. BUG_ON(!block_group->ro);
  7599. /*
  7600. * Free the reserved super bytes from this block group before
  7601. * remove it.
  7602. */
  7603. free_excluded_extents(root, block_group);
  7604. memcpy(&key, &block_group->key, sizeof(key));
  7605. index = get_block_group_index(block_group);
  7606. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7607. BTRFS_BLOCK_GROUP_RAID1 |
  7608. BTRFS_BLOCK_GROUP_RAID10))
  7609. factor = 2;
  7610. else
  7611. factor = 1;
  7612. /* make sure this block group isn't part of an allocation cluster */
  7613. cluster = &root->fs_info->data_alloc_cluster;
  7614. spin_lock(&cluster->refill_lock);
  7615. btrfs_return_cluster_to_free_space(block_group, cluster);
  7616. spin_unlock(&cluster->refill_lock);
  7617. /*
  7618. * make sure this block group isn't part of a metadata
  7619. * allocation cluster
  7620. */
  7621. cluster = &root->fs_info->meta_alloc_cluster;
  7622. spin_lock(&cluster->refill_lock);
  7623. btrfs_return_cluster_to_free_space(block_group, cluster);
  7624. spin_unlock(&cluster->refill_lock);
  7625. path = btrfs_alloc_path();
  7626. if (!path) {
  7627. ret = -ENOMEM;
  7628. goto out;
  7629. }
  7630. inode = lookup_free_space_inode(tree_root, block_group, path);
  7631. if (!IS_ERR(inode)) {
  7632. ret = btrfs_orphan_add(trans, inode);
  7633. if (ret) {
  7634. btrfs_add_delayed_iput(inode);
  7635. goto out;
  7636. }
  7637. clear_nlink(inode);
  7638. /* One for the block groups ref */
  7639. spin_lock(&block_group->lock);
  7640. if (block_group->iref) {
  7641. block_group->iref = 0;
  7642. block_group->inode = NULL;
  7643. spin_unlock(&block_group->lock);
  7644. iput(inode);
  7645. } else {
  7646. spin_unlock(&block_group->lock);
  7647. }
  7648. /* One for our lookup ref */
  7649. btrfs_add_delayed_iput(inode);
  7650. }
  7651. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7652. key.offset = block_group->key.objectid;
  7653. key.type = 0;
  7654. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7655. if (ret < 0)
  7656. goto out;
  7657. if (ret > 0)
  7658. btrfs_release_path(path);
  7659. if (ret == 0) {
  7660. ret = btrfs_del_item(trans, tree_root, path);
  7661. if (ret)
  7662. goto out;
  7663. btrfs_release_path(path);
  7664. }
  7665. spin_lock(&root->fs_info->block_group_cache_lock);
  7666. rb_erase(&block_group->cache_node,
  7667. &root->fs_info->block_group_cache_tree);
  7668. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  7669. root->fs_info->first_logical_byte = (u64)-1;
  7670. spin_unlock(&root->fs_info->block_group_cache_lock);
  7671. down_write(&block_group->space_info->groups_sem);
  7672. /*
  7673. * we must use list_del_init so people can check to see if they
  7674. * are still on the list after taking the semaphore
  7675. */
  7676. list_del_init(&block_group->list);
  7677. if (list_empty(&block_group->space_info->block_groups[index]))
  7678. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7679. up_write(&block_group->space_info->groups_sem);
  7680. if (block_group->cached == BTRFS_CACHE_STARTED)
  7681. wait_block_group_cache_done(block_group);
  7682. btrfs_remove_free_space_cache(block_group);
  7683. spin_lock(&block_group->space_info->lock);
  7684. block_group->space_info->total_bytes -= block_group->key.offset;
  7685. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7686. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7687. spin_unlock(&block_group->space_info->lock);
  7688. memcpy(&key, &block_group->key, sizeof(key));
  7689. btrfs_clear_space_info_full(root->fs_info);
  7690. btrfs_put_block_group(block_group);
  7691. btrfs_put_block_group(block_group);
  7692. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7693. if (ret > 0)
  7694. ret = -EIO;
  7695. if (ret < 0)
  7696. goto out;
  7697. ret = btrfs_del_item(trans, root, path);
  7698. out:
  7699. btrfs_free_path(path);
  7700. return ret;
  7701. }
  7702. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7703. {
  7704. struct btrfs_space_info *space_info;
  7705. struct btrfs_super_block *disk_super;
  7706. u64 features;
  7707. u64 flags;
  7708. int mixed = 0;
  7709. int ret;
  7710. disk_super = fs_info->super_copy;
  7711. if (!btrfs_super_root(disk_super))
  7712. return 1;
  7713. features = btrfs_super_incompat_flags(disk_super);
  7714. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7715. mixed = 1;
  7716. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7717. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7718. if (ret)
  7719. goto out;
  7720. if (mixed) {
  7721. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7722. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7723. } else {
  7724. flags = BTRFS_BLOCK_GROUP_METADATA;
  7725. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7726. if (ret)
  7727. goto out;
  7728. flags = BTRFS_BLOCK_GROUP_DATA;
  7729. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7730. }
  7731. out:
  7732. return ret;
  7733. }
  7734. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7735. {
  7736. return unpin_extent_range(root, start, end);
  7737. }
  7738. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7739. u64 num_bytes, u64 *actual_bytes)
  7740. {
  7741. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7742. }
  7743. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7744. {
  7745. struct btrfs_fs_info *fs_info = root->fs_info;
  7746. struct btrfs_block_group_cache *cache = NULL;
  7747. u64 group_trimmed;
  7748. u64 start;
  7749. u64 end;
  7750. u64 trimmed = 0;
  7751. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7752. int ret = 0;
  7753. /*
  7754. * try to trim all FS space, our block group may start from non-zero.
  7755. */
  7756. if (range->len == total_bytes)
  7757. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7758. else
  7759. cache = btrfs_lookup_block_group(fs_info, range->start);
  7760. while (cache) {
  7761. if (cache->key.objectid >= (range->start + range->len)) {
  7762. btrfs_put_block_group(cache);
  7763. break;
  7764. }
  7765. start = max(range->start, cache->key.objectid);
  7766. end = min(range->start + range->len,
  7767. cache->key.objectid + cache->key.offset);
  7768. if (end - start >= range->minlen) {
  7769. if (!block_group_cache_done(cache)) {
  7770. ret = cache_block_group(cache, 0);
  7771. if (ret) {
  7772. btrfs_put_block_group(cache);
  7773. break;
  7774. }
  7775. ret = wait_block_group_cache_done(cache);
  7776. if (ret) {
  7777. btrfs_put_block_group(cache);
  7778. break;
  7779. }
  7780. }
  7781. ret = btrfs_trim_block_group(cache,
  7782. &group_trimmed,
  7783. start,
  7784. end,
  7785. range->minlen);
  7786. trimmed += group_trimmed;
  7787. if (ret) {
  7788. btrfs_put_block_group(cache);
  7789. break;
  7790. }
  7791. }
  7792. cache = next_block_group(fs_info->tree_root, cache);
  7793. }
  7794. range->len = trimmed;
  7795. return ret;
  7796. }