extent-tree.c 224 KB

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