extent-tree.c 193 KB

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