extent-tree.c 218 KB

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