extent-tree.c 192 KB

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