extent-tree.c 192 KB

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