extent-tree.c 197 KB

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