extent-tree.c 195 KB

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