extent-tree.c 215 KB

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