extent-tree.c 213 KB

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