extent-tree.c 223 KB

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