extent-tree.c 193 KB

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