extent-tree.c 215 KB

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