extent-tree.c 229 KB

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