extent-tree.c 217 KB

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