extent-tree.c 257 KB

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