extent-tree.c 257 KB

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