extent-tree.c 200 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658
  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. static void btrfs_issue_discard(struct block_device *bdev,
  1420. u64 start, u64 len)
  1421. {
  1422. blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
  1423. }
  1424. static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
  1425. u64 num_bytes)
  1426. {
  1427. int ret;
  1428. u64 map_length = num_bytes;
  1429. struct btrfs_multi_bio *multi = NULL;
  1430. if (!btrfs_test_opt(root, DISCARD))
  1431. return 0;
  1432. /* Tell the block device(s) that the sectors can be discarded */
  1433. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  1434. bytenr, &map_length, &multi, 0);
  1435. if (!ret) {
  1436. struct btrfs_bio_stripe *stripe = multi->stripes;
  1437. int i;
  1438. if (map_length > num_bytes)
  1439. map_length = num_bytes;
  1440. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  1441. btrfs_issue_discard(stripe->dev->bdev,
  1442. stripe->physical,
  1443. map_length);
  1444. }
  1445. kfree(multi);
  1446. }
  1447. return ret;
  1448. }
  1449. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1450. struct btrfs_root *root,
  1451. u64 bytenr, u64 num_bytes, u64 parent,
  1452. u64 root_objectid, u64 owner, u64 offset)
  1453. {
  1454. int ret;
  1455. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
  1456. root_objectid == BTRFS_TREE_LOG_OBJECTID);
  1457. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1458. ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
  1459. parent, root_objectid, (int)owner,
  1460. BTRFS_ADD_DELAYED_REF, NULL);
  1461. } else {
  1462. ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
  1463. parent, root_objectid, owner, offset,
  1464. BTRFS_ADD_DELAYED_REF, NULL);
  1465. }
  1466. return ret;
  1467. }
  1468. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1469. struct btrfs_root *root,
  1470. u64 bytenr, u64 num_bytes,
  1471. u64 parent, u64 root_objectid,
  1472. u64 owner, u64 offset, int refs_to_add,
  1473. struct btrfs_delayed_extent_op *extent_op)
  1474. {
  1475. struct btrfs_path *path;
  1476. struct extent_buffer *leaf;
  1477. struct btrfs_extent_item *item;
  1478. u64 refs;
  1479. int ret;
  1480. int err = 0;
  1481. path = btrfs_alloc_path();
  1482. if (!path)
  1483. return -ENOMEM;
  1484. path->reada = 1;
  1485. path->leave_spinning = 1;
  1486. /* this will setup the path even if it fails to insert the back ref */
  1487. ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
  1488. path, bytenr, num_bytes, parent,
  1489. root_objectid, owner, offset,
  1490. refs_to_add, extent_op);
  1491. if (ret == 0)
  1492. goto out;
  1493. if (ret != -EAGAIN) {
  1494. err = ret;
  1495. goto out;
  1496. }
  1497. leaf = path->nodes[0];
  1498. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1499. refs = btrfs_extent_refs(leaf, item);
  1500. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  1501. if (extent_op)
  1502. __run_delayed_extent_op(extent_op, leaf, item);
  1503. btrfs_mark_buffer_dirty(leaf);
  1504. btrfs_release_path(root->fs_info->extent_root, path);
  1505. path->reada = 1;
  1506. path->leave_spinning = 1;
  1507. /* now insert the actual backref */
  1508. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1509. path, bytenr, parent, root_objectid,
  1510. owner, offset, refs_to_add);
  1511. BUG_ON(ret);
  1512. out:
  1513. btrfs_free_path(path);
  1514. return err;
  1515. }
  1516. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  1517. struct btrfs_root *root,
  1518. struct btrfs_delayed_ref_node *node,
  1519. struct btrfs_delayed_extent_op *extent_op,
  1520. int insert_reserved)
  1521. {
  1522. int ret = 0;
  1523. struct btrfs_delayed_data_ref *ref;
  1524. struct btrfs_key ins;
  1525. u64 parent = 0;
  1526. u64 ref_root = 0;
  1527. u64 flags = 0;
  1528. ins.objectid = node->bytenr;
  1529. ins.offset = node->num_bytes;
  1530. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1531. ref = btrfs_delayed_node_to_data_ref(node);
  1532. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1533. parent = ref->parent;
  1534. else
  1535. ref_root = ref->root;
  1536. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1537. if (extent_op) {
  1538. BUG_ON(extent_op->update_key);
  1539. flags |= extent_op->flags_to_set;
  1540. }
  1541. ret = alloc_reserved_file_extent(trans, root,
  1542. parent, ref_root, flags,
  1543. ref->objectid, ref->offset,
  1544. &ins, node->ref_mod);
  1545. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1546. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1547. node->num_bytes, parent,
  1548. ref_root, ref->objectid,
  1549. ref->offset, node->ref_mod,
  1550. extent_op);
  1551. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1552. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1553. node->num_bytes, parent,
  1554. ref_root, ref->objectid,
  1555. ref->offset, node->ref_mod,
  1556. extent_op);
  1557. } else {
  1558. BUG();
  1559. }
  1560. return ret;
  1561. }
  1562. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1563. struct extent_buffer *leaf,
  1564. struct btrfs_extent_item *ei)
  1565. {
  1566. u64 flags = btrfs_extent_flags(leaf, ei);
  1567. if (extent_op->update_flags) {
  1568. flags |= extent_op->flags_to_set;
  1569. btrfs_set_extent_flags(leaf, ei, flags);
  1570. }
  1571. if (extent_op->update_key) {
  1572. struct btrfs_tree_block_info *bi;
  1573. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1574. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1575. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1576. }
  1577. }
  1578. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1579. struct btrfs_root *root,
  1580. struct btrfs_delayed_ref_node *node,
  1581. struct btrfs_delayed_extent_op *extent_op)
  1582. {
  1583. struct btrfs_key key;
  1584. struct btrfs_path *path;
  1585. struct btrfs_extent_item *ei;
  1586. struct extent_buffer *leaf;
  1587. u32 item_size;
  1588. int ret;
  1589. int err = 0;
  1590. path = btrfs_alloc_path();
  1591. if (!path)
  1592. return -ENOMEM;
  1593. key.objectid = node->bytenr;
  1594. key.type = BTRFS_EXTENT_ITEM_KEY;
  1595. key.offset = node->num_bytes;
  1596. path->reada = 1;
  1597. path->leave_spinning = 1;
  1598. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  1599. path, 0, 1);
  1600. if (ret < 0) {
  1601. err = ret;
  1602. goto out;
  1603. }
  1604. if (ret > 0) {
  1605. err = -EIO;
  1606. goto out;
  1607. }
  1608. leaf = path->nodes[0];
  1609. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1610. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1611. if (item_size < sizeof(*ei)) {
  1612. ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
  1613. path, (u64)-1, 0);
  1614. if (ret < 0) {
  1615. err = ret;
  1616. goto out;
  1617. }
  1618. leaf = path->nodes[0];
  1619. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1620. }
  1621. #endif
  1622. BUG_ON(item_size < sizeof(*ei));
  1623. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1624. __run_delayed_extent_op(extent_op, leaf, ei);
  1625. btrfs_mark_buffer_dirty(leaf);
  1626. out:
  1627. btrfs_free_path(path);
  1628. return err;
  1629. }
  1630. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1631. struct btrfs_root *root,
  1632. struct btrfs_delayed_ref_node *node,
  1633. struct btrfs_delayed_extent_op *extent_op,
  1634. int insert_reserved)
  1635. {
  1636. int ret = 0;
  1637. struct btrfs_delayed_tree_ref *ref;
  1638. struct btrfs_key ins;
  1639. u64 parent = 0;
  1640. u64 ref_root = 0;
  1641. ins.objectid = node->bytenr;
  1642. ins.offset = node->num_bytes;
  1643. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1644. ref = btrfs_delayed_node_to_tree_ref(node);
  1645. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1646. parent = ref->parent;
  1647. else
  1648. ref_root = ref->root;
  1649. BUG_ON(node->ref_mod != 1);
  1650. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1651. BUG_ON(!extent_op || !extent_op->update_flags ||
  1652. !extent_op->update_key);
  1653. ret = alloc_reserved_tree_block(trans, root,
  1654. parent, ref_root,
  1655. extent_op->flags_to_set,
  1656. &extent_op->key,
  1657. ref->level, &ins);
  1658. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1659. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1660. node->num_bytes, parent, ref_root,
  1661. ref->level, 0, 1, extent_op);
  1662. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1663. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1664. node->num_bytes, parent, ref_root,
  1665. ref->level, 0, 1, extent_op);
  1666. } else {
  1667. BUG();
  1668. }
  1669. return ret;
  1670. }
  1671. /* helper function to actually process a single delayed ref entry */
  1672. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  1673. struct btrfs_root *root,
  1674. struct btrfs_delayed_ref_node *node,
  1675. struct btrfs_delayed_extent_op *extent_op,
  1676. int insert_reserved)
  1677. {
  1678. int ret;
  1679. if (btrfs_delayed_ref_is_head(node)) {
  1680. struct btrfs_delayed_ref_head *head;
  1681. /*
  1682. * we've hit the end of the chain and we were supposed
  1683. * to insert this extent into the tree. But, it got
  1684. * deleted before we ever needed to insert it, so all
  1685. * we have to do is clean up the accounting
  1686. */
  1687. BUG_ON(extent_op);
  1688. head = btrfs_delayed_node_to_head(node);
  1689. if (insert_reserved) {
  1690. int mark_free = 0;
  1691. struct extent_buffer *must_clean = NULL;
  1692. ret = pin_down_bytes(trans, root, NULL,
  1693. node->bytenr, node->num_bytes,
  1694. head->is_data, 1, &must_clean);
  1695. if (ret > 0)
  1696. mark_free = 1;
  1697. if (must_clean) {
  1698. clean_tree_block(NULL, root, must_clean);
  1699. btrfs_tree_unlock(must_clean);
  1700. free_extent_buffer(must_clean);
  1701. }
  1702. if (head->is_data) {
  1703. ret = btrfs_del_csums(trans, root,
  1704. node->bytenr,
  1705. node->num_bytes);
  1706. BUG_ON(ret);
  1707. }
  1708. if (mark_free) {
  1709. ret = btrfs_free_reserved_extent(root,
  1710. node->bytenr,
  1711. node->num_bytes);
  1712. BUG_ON(ret);
  1713. }
  1714. }
  1715. mutex_unlock(&head->mutex);
  1716. return 0;
  1717. }
  1718. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  1719. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1720. ret = run_delayed_tree_ref(trans, root, node, extent_op,
  1721. insert_reserved);
  1722. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  1723. node->type == BTRFS_SHARED_DATA_REF_KEY)
  1724. ret = run_delayed_data_ref(trans, root, node, extent_op,
  1725. insert_reserved);
  1726. else
  1727. BUG();
  1728. return ret;
  1729. }
  1730. static noinline struct btrfs_delayed_ref_node *
  1731. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  1732. {
  1733. struct rb_node *node;
  1734. struct btrfs_delayed_ref_node *ref;
  1735. int action = BTRFS_ADD_DELAYED_REF;
  1736. again:
  1737. /*
  1738. * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
  1739. * this prevents ref count from going down to zero when
  1740. * there still are pending delayed ref.
  1741. */
  1742. node = rb_prev(&head->node.rb_node);
  1743. while (1) {
  1744. if (!node)
  1745. break;
  1746. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  1747. rb_node);
  1748. if (ref->bytenr != head->node.bytenr)
  1749. break;
  1750. if (ref->action == action)
  1751. return ref;
  1752. node = rb_prev(node);
  1753. }
  1754. if (action == BTRFS_ADD_DELAYED_REF) {
  1755. action = BTRFS_DROP_DELAYED_REF;
  1756. goto again;
  1757. }
  1758. return NULL;
  1759. }
  1760. static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
  1761. struct btrfs_root *root,
  1762. struct list_head *cluster)
  1763. {
  1764. struct btrfs_delayed_ref_root *delayed_refs;
  1765. struct btrfs_delayed_ref_node *ref;
  1766. struct btrfs_delayed_ref_head *locked_ref = NULL;
  1767. struct btrfs_delayed_extent_op *extent_op;
  1768. int ret;
  1769. int count = 0;
  1770. int must_insert_reserved = 0;
  1771. delayed_refs = &trans->transaction->delayed_refs;
  1772. while (1) {
  1773. if (!locked_ref) {
  1774. /* pick a new head ref from the cluster list */
  1775. if (list_empty(cluster))
  1776. break;
  1777. locked_ref = list_entry(cluster->next,
  1778. struct btrfs_delayed_ref_head, cluster);
  1779. /* grab the lock that says we are going to process
  1780. * all the refs for this head */
  1781. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  1782. /*
  1783. * we may have dropped the spin lock to get the head
  1784. * mutex lock, and that might have given someone else
  1785. * time to free the head. If that's true, it has been
  1786. * removed from our list and we can move on.
  1787. */
  1788. if (ret == -EAGAIN) {
  1789. locked_ref = NULL;
  1790. count++;
  1791. continue;
  1792. }
  1793. }
  1794. /*
  1795. * record the must insert reserved flag before we
  1796. * drop the spin lock.
  1797. */
  1798. must_insert_reserved = locked_ref->must_insert_reserved;
  1799. locked_ref->must_insert_reserved = 0;
  1800. extent_op = locked_ref->extent_op;
  1801. locked_ref->extent_op = NULL;
  1802. /*
  1803. * locked_ref is the head node, so we have to go one
  1804. * node back for any delayed ref updates
  1805. */
  1806. ref = select_delayed_ref(locked_ref);
  1807. if (!ref) {
  1808. /* All delayed refs have been processed, Go ahead
  1809. * and send the head node to run_one_delayed_ref,
  1810. * so that any accounting fixes can happen
  1811. */
  1812. ref = &locked_ref->node;
  1813. if (extent_op && must_insert_reserved) {
  1814. kfree(extent_op);
  1815. extent_op = NULL;
  1816. }
  1817. if (extent_op) {
  1818. spin_unlock(&delayed_refs->lock);
  1819. ret = run_delayed_extent_op(trans, root,
  1820. ref, extent_op);
  1821. BUG_ON(ret);
  1822. kfree(extent_op);
  1823. cond_resched();
  1824. spin_lock(&delayed_refs->lock);
  1825. continue;
  1826. }
  1827. list_del_init(&locked_ref->cluster);
  1828. locked_ref = NULL;
  1829. }
  1830. ref->in_tree = 0;
  1831. rb_erase(&ref->rb_node, &delayed_refs->root);
  1832. delayed_refs->num_entries--;
  1833. spin_unlock(&delayed_refs->lock);
  1834. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  1835. must_insert_reserved);
  1836. BUG_ON(ret);
  1837. btrfs_put_delayed_ref(ref);
  1838. kfree(extent_op);
  1839. count++;
  1840. cond_resched();
  1841. spin_lock(&delayed_refs->lock);
  1842. }
  1843. return count;
  1844. }
  1845. /*
  1846. * this starts processing the delayed reference count updates and
  1847. * extent insertions we have queued up so far. count can be
  1848. * 0, which means to process everything in the tree at the start
  1849. * of the run (but not newly added entries), or it can be some target
  1850. * number you'd like to process.
  1851. */
  1852. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  1853. struct btrfs_root *root, unsigned long count)
  1854. {
  1855. struct rb_node *node;
  1856. struct btrfs_delayed_ref_root *delayed_refs;
  1857. struct btrfs_delayed_ref_node *ref;
  1858. struct list_head cluster;
  1859. int ret;
  1860. int run_all = count == (unsigned long)-1;
  1861. int run_most = 0;
  1862. if (root == root->fs_info->extent_root)
  1863. root = root->fs_info->tree_root;
  1864. delayed_refs = &trans->transaction->delayed_refs;
  1865. INIT_LIST_HEAD(&cluster);
  1866. again:
  1867. spin_lock(&delayed_refs->lock);
  1868. if (count == 0) {
  1869. count = delayed_refs->num_entries * 2;
  1870. run_most = 1;
  1871. }
  1872. while (1) {
  1873. if (!(run_all || run_most) &&
  1874. delayed_refs->num_heads_ready < 64)
  1875. break;
  1876. /*
  1877. * go find something we can process in the rbtree. We start at
  1878. * the beginning of the tree, and then build a cluster
  1879. * of refs to process starting at the first one we are able to
  1880. * lock
  1881. */
  1882. ret = btrfs_find_ref_cluster(trans, &cluster,
  1883. delayed_refs->run_delayed_start);
  1884. if (ret)
  1885. break;
  1886. ret = run_clustered_refs(trans, root, &cluster);
  1887. BUG_ON(ret < 0);
  1888. count -= min_t(unsigned long, ret, count);
  1889. if (count == 0)
  1890. break;
  1891. }
  1892. if (run_all) {
  1893. node = rb_first(&delayed_refs->root);
  1894. if (!node)
  1895. goto out;
  1896. count = (unsigned long)-1;
  1897. while (node) {
  1898. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  1899. rb_node);
  1900. if (btrfs_delayed_ref_is_head(ref)) {
  1901. struct btrfs_delayed_ref_head *head;
  1902. head = btrfs_delayed_node_to_head(ref);
  1903. atomic_inc(&ref->refs);
  1904. spin_unlock(&delayed_refs->lock);
  1905. mutex_lock(&head->mutex);
  1906. mutex_unlock(&head->mutex);
  1907. btrfs_put_delayed_ref(ref);
  1908. cond_resched();
  1909. goto again;
  1910. }
  1911. node = rb_next(node);
  1912. }
  1913. spin_unlock(&delayed_refs->lock);
  1914. schedule_timeout(1);
  1915. goto again;
  1916. }
  1917. out:
  1918. spin_unlock(&delayed_refs->lock);
  1919. return 0;
  1920. }
  1921. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  1922. struct btrfs_root *root,
  1923. u64 bytenr, u64 num_bytes, u64 flags,
  1924. int is_data)
  1925. {
  1926. struct btrfs_delayed_extent_op *extent_op;
  1927. int ret;
  1928. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  1929. if (!extent_op)
  1930. return -ENOMEM;
  1931. extent_op->flags_to_set = flags;
  1932. extent_op->update_flags = 1;
  1933. extent_op->update_key = 0;
  1934. extent_op->is_data = is_data ? 1 : 0;
  1935. ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
  1936. if (ret)
  1937. kfree(extent_op);
  1938. return ret;
  1939. }
  1940. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  1941. struct btrfs_root *root,
  1942. struct btrfs_path *path,
  1943. u64 objectid, u64 offset, u64 bytenr)
  1944. {
  1945. struct btrfs_delayed_ref_head *head;
  1946. struct btrfs_delayed_ref_node *ref;
  1947. struct btrfs_delayed_data_ref *data_ref;
  1948. struct btrfs_delayed_ref_root *delayed_refs;
  1949. struct rb_node *node;
  1950. int ret = 0;
  1951. ret = -ENOENT;
  1952. delayed_refs = &trans->transaction->delayed_refs;
  1953. spin_lock(&delayed_refs->lock);
  1954. head = btrfs_find_delayed_ref_head(trans, bytenr);
  1955. if (!head)
  1956. goto out;
  1957. if (!mutex_trylock(&head->mutex)) {
  1958. atomic_inc(&head->node.refs);
  1959. spin_unlock(&delayed_refs->lock);
  1960. btrfs_release_path(root->fs_info->extent_root, path);
  1961. mutex_lock(&head->mutex);
  1962. mutex_unlock(&head->mutex);
  1963. btrfs_put_delayed_ref(&head->node);
  1964. return -EAGAIN;
  1965. }
  1966. node = rb_prev(&head->node.rb_node);
  1967. if (!node)
  1968. goto out_unlock;
  1969. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  1970. if (ref->bytenr != bytenr)
  1971. goto out_unlock;
  1972. ret = 1;
  1973. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  1974. goto out_unlock;
  1975. data_ref = btrfs_delayed_node_to_data_ref(ref);
  1976. node = rb_prev(node);
  1977. if (node) {
  1978. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  1979. if (ref->bytenr == bytenr)
  1980. goto out_unlock;
  1981. }
  1982. if (data_ref->root != root->root_key.objectid ||
  1983. data_ref->objectid != objectid || data_ref->offset != offset)
  1984. goto out_unlock;
  1985. ret = 0;
  1986. out_unlock:
  1987. mutex_unlock(&head->mutex);
  1988. out:
  1989. spin_unlock(&delayed_refs->lock);
  1990. return ret;
  1991. }
  1992. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  1993. struct btrfs_root *root,
  1994. struct btrfs_path *path,
  1995. u64 objectid, u64 offset, u64 bytenr)
  1996. {
  1997. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1998. struct extent_buffer *leaf;
  1999. struct btrfs_extent_data_ref *ref;
  2000. struct btrfs_extent_inline_ref *iref;
  2001. struct btrfs_extent_item *ei;
  2002. struct btrfs_key key;
  2003. u32 item_size;
  2004. int ret;
  2005. key.objectid = bytenr;
  2006. key.offset = (u64)-1;
  2007. key.type = BTRFS_EXTENT_ITEM_KEY;
  2008. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2009. if (ret < 0)
  2010. goto out;
  2011. BUG_ON(ret == 0);
  2012. ret = -ENOENT;
  2013. if (path->slots[0] == 0)
  2014. goto out;
  2015. path->slots[0]--;
  2016. leaf = path->nodes[0];
  2017. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2018. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2019. goto out;
  2020. ret = 1;
  2021. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2022. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2023. if (item_size < sizeof(*ei)) {
  2024. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2025. goto out;
  2026. }
  2027. #endif
  2028. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2029. if (item_size != sizeof(*ei) +
  2030. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2031. goto out;
  2032. if (btrfs_extent_generation(leaf, ei) <=
  2033. btrfs_root_last_snapshot(&root->root_item))
  2034. goto out;
  2035. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2036. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2037. BTRFS_EXTENT_DATA_REF_KEY)
  2038. goto out;
  2039. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2040. if (btrfs_extent_refs(leaf, ei) !=
  2041. btrfs_extent_data_ref_count(leaf, ref) ||
  2042. btrfs_extent_data_ref_root(leaf, ref) !=
  2043. root->root_key.objectid ||
  2044. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2045. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2046. goto out;
  2047. ret = 0;
  2048. out:
  2049. return ret;
  2050. }
  2051. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2052. struct btrfs_root *root,
  2053. u64 objectid, u64 offset, u64 bytenr)
  2054. {
  2055. struct btrfs_path *path;
  2056. int ret;
  2057. int ret2;
  2058. path = btrfs_alloc_path();
  2059. if (!path)
  2060. return -ENOENT;
  2061. do {
  2062. ret = check_committed_ref(trans, root, path, objectid,
  2063. offset, bytenr);
  2064. if (ret && ret != -ENOENT)
  2065. goto out;
  2066. ret2 = check_delayed_ref(trans, root, path, objectid,
  2067. offset, bytenr);
  2068. } while (ret2 == -EAGAIN);
  2069. if (ret2 && ret2 != -ENOENT) {
  2070. ret = ret2;
  2071. goto out;
  2072. }
  2073. if (ret != -ENOENT || ret2 != -ENOENT)
  2074. ret = 0;
  2075. out:
  2076. btrfs_free_path(path);
  2077. return ret;
  2078. }
  2079. #if 0
  2080. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2081. struct extent_buffer *buf, u32 nr_extents)
  2082. {
  2083. struct btrfs_key key;
  2084. struct btrfs_file_extent_item *fi;
  2085. u64 root_gen;
  2086. u32 nritems;
  2087. int i;
  2088. int level;
  2089. int ret = 0;
  2090. int shared = 0;
  2091. if (!root->ref_cows)
  2092. return 0;
  2093. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  2094. shared = 0;
  2095. root_gen = root->root_key.offset;
  2096. } else {
  2097. shared = 1;
  2098. root_gen = trans->transid - 1;
  2099. }
  2100. level = btrfs_header_level(buf);
  2101. nritems = btrfs_header_nritems(buf);
  2102. if (level == 0) {
  2103. struct btrfs_leaf_ref *ref;
  2104. struct btrfs_extent_info *info;
  2105. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  2106. if (!ref) {
  2107. ret = -ENOMEM;
  2108. goto out;
  2109. }
  2110. ref->root_gen = root_gen;
  2111. ref->bytenr = buf->start;
  2112. ref->owner = btrfs_header_owner(buf);
  2113. ref->generation = btrfs_header_generation(buf);
  2114. ref->nritems = nr_extents;
  2115. info = ref->extents;
  2116. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  2117. u64 disk_bytenr;
  2118. btrfs_item_key_to_cpu(buf, &key, i);
  2119. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2120. continue;
  2121. fi = btrfs_item_ptr(buf, i,
  2122. struct btrfs_file_extent_item);
  2123. if (btrfs_file_extent_type(buf, fi) ==
  2124. BTRFS_FILE_EXTENT_INLINE)
  2125. continue;
  2126. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2127. if (disk_bytenr == 0)
  2128. continue;
  2129. info->bytenr = disk_bytenr;
  2130. info->num_bytes =
  2131. btrfs_file_extent_disk_num_bytes(buf, fi);
  2132. info->objectid = key.objectid;
  2133. info->offset = key.offset;
  2134. info++;
  2135. }
  2136. ret = btrfs_add_leaf_ref(root, ref, shared);
  2137. if (ret == -EEXIST && shared) {
  2138. struct btrfs_leaf_ref *old;
  2139. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  2140. BUG_ON(!old);
  2141. btrfs_remove_leaf_ref(root, old);
  2142. btrfs_free_leaf_ref(root, old);
  2143. ret = btrfs_add_leaf_ref(root, ref, shared);
  2144. }
  2145. WARN_ON(ret);
  2146. btrfs_free_leaf_ref(root, ref);
  2147. }
  2148. out:
  2149. return ret;
  2150. }
  2151. /* when a block goes through cow, we update the reference counts of
  2152. * everything that block points to. The internal pointers of the block
  2153. * can be in just about any order, and it is likely to have clusters of
  2154. * things that are close together and clusters of things that are not.
  2155. *
  2156. * To help reduce the seeks that come with updating all of these reference
  2157. * counts, sort them by byte number before actual updates are done.
  2158. *
  2159. * struct refsort is used to match byte number to slot in the btree block.
  2160. * we sort based on the byte number and then use the slot to actually
  2161. * find the item.
  2162. *
  2163. * struct refsort is smaller than strcut btrfs_item and smaller than
  2164. * struct btrfs_key_ptr. Since we're currently limited to the page size
  2165. * for a btree block, there's no way for a kmalloc of refsorts for a
  2166. * single node to be bigger than a page.
  2167. */
  2168. struct refsort {
  2169. u64 bytenr;
  2170. u32 slot;
  2171. };
  2172. /*
  2173. * for passing into sort()
  2174. */
  2175. static int refsort_cmp(const void *a_void, const void *b_void)
  2176. {
  2177. const struct refsort *a = a_void;
  2178. const struct refsort *b = b_void;
  2179. if (a->bytenr < b->bytenr)
  2180. return -1;
  2181. if (a->bytenr > b->bytenr)
  2182. return 1;
  2183. return 0;
  2184. }
  2185. #endif
  2186. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2187. struct btrfs_root *root,
  2188. struct extent_buffer *buf,
  2189. int full_backref, int inc)
  2190. {
  2191. u64 bytenr;
  2192. u64 num_bytes;
  2193. u64 parent;
  2194. u64 ref_root;
  2195. u32 nritems;
  2196. struct btrfs_key key;
  2197. struct btrfs_file_extent_item *fi;
  2198. int i;
  2199. int level;
  2200. int ret = 0;
  2201. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2202. u64, u64, u64, u64, u64, u64);
  2203. ref_root = btrfs_header_owner(buf);
  2204. nritems = btrfs_header_nritems(buf);
  2205. level = btrfs_header_level(buf);
  2206. if (!root->ref_cows && level == 0)
  2207. return 0;
  2208. if (inc)
  2209. process_func = btrfs_inc_extent_ref;
  2210. else
  2211. process_func = btrfs_free_extent;
  2212. if (full_backref)
  2213. parent = buf->start;
  2214. else
  2215. parent = 0;
  2216. for (i = 0; i < nritems; i++) {
  2217. if (level == 0) {
  2218. btrfs_item_key_to_cpu(buf, &key, i);
  2219. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2220. continue;
  2221. fi = btrfs_item_ptr(buf, i,
  2222. struct btrfs_file_extent_item);
  2223. if (btrfs_file_extent_type(buf, fi) ==
  2224. BTRFS_FILE_EXTENT_INLINE)
  2225. continue;
  2226. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2227. if (bytenr == 0)
  2228. continue;
  2229. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2230. key.offset -= btrfs_file_extent_offset(buf, fi);
  2231. ret = process_func(trans, root, bytenr, num_bytes,
  2232. parent, ref_root, key.objectid,
  2233. key.offset);
  2234. if (ret)
  2235. goto fail;
  2236. } else {
  2237. bytenr = btrfs_node_blockptr(buf, i);
  2238. num_bytes = btrfs_level_size(root, level - 1);
  2239. ret = process_func(trans, root, bytenr, num_bytes,
  2240. parent, ref_root, level - 1, 0);
  2241. if (ret)
  2242. goto fail;
  2243. }
  2244. }
  2245. return 0;
  2246. fail:
  2247. BUG();
  2248. return ret;
  2249. }
  2250. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2251. struct extent_buffer *buf, int full_backref)
  2252. {
  2253. return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
  2254. }
  2255. int btrfs_dec_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, 0);
  2259. }
  2260. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2261. struct btrfs_root *root,
  2262. struct btrfs_path *path,
  2263. struct btrfs_block_group_cache *cache)
  2264. {
  2265. int ret;
  2266. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2267. unsigned long bi;
  2268. struct extent_buffer *leaf;
  2269. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2270. if (ret < 0)
  2271. goto fail;
  2272. BUG_ON(ret);
  2273. leaf = path->nodes[0];
  2274. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2275. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2276. btrfs_mark_buffer_dirty(leaf);
  2277. btrfs_release_path(extent_root, path);
  2278. fail:
  2279. if (ret)
  2280. return ret;
  2281. return 0;
  2282. }
  2283. static struct btrfs_block_group_cache *
  2284. next_block_group(struct btrfs_root *root,
  2285. struct btrfs_block_group_cache *cache)
  2286. {
  2287. struct rb_node *node;
  2288. spin_lock(&root->fs_info->block_group_cache_lock);
  2289. node = rb_next(&cache->cache_node);
  2290. btrfs_put_block_group(cache);
  2291. if (node) {
  2292. cache = rb_entry(node, struct btrfs_block_group_cache,
  2293. cache_node);
  2294. atomic_inc(&cache->count);
  2295. } else
  2296. cache = NULL;
  2297. spin_unlock(&root->fs_info->block_group_cache_lock);
  2298. return cache;
  2299. }
  2300. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2301. struct btrfs_root *root)
  2302. {
  2303. struct btrfs_block_group_cache *cache;
  2304. int err = 0;
  2305. struct btrfs_path *path;
  2306. u64 last = 0;
  2307. path = btrfs_alloc_path();
  2308. if (!path)
  2309. return -ENOMEM;
  2310. while (1) {
  2311. if (last == 0) {
  2312. err = btrfs_run_delayed_refs(trans, root,
  2313. (unsigned long)-1);
  2314. BUG_ON(err);
  2315. }
  2316. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2317. while (cache) {
  2318. if (cache->dirty)
  2319. break;
  2320. cache = next_block_group(root, cache);
  2321. }
  2322. if (!cache) {
  2323. if (last == 0)
  2324. break;
  2325. last = 0;
  2326. continue;
  2327. }
  2328. cache->dirty = 0;
  2329. last = cache->key.objectid + cache->key.offset;
  2330. err = write_one_cache_group(trans, root, path, cache);
  2331. BUG_ON(err);
  2332. btrfs_put_block_group(cache);
  2333. }
  2334. btrfs_free_path(path);
  2335. return 0;
  2336. }
  2337. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2338. {
  2339. struct btrfs_block_group_cache *block_group;
  2340. int readonly = 0;
  2341. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2342. if (!block_group || block_group->ro)
  2343. readonly = 1;
  2344. if (block_group)
  2345. btrfs_put_block_group(block_group);
  2346. return readonly;
  2347. }
  2348. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2349. u64 total_bytes, u64 bytes_used,
  2350. struct btrfs_space_info **space_info)
  2351. {
  2352. struct btrfs_space_info *found;
  2353. found = __find_space_info(info, flags);
  2354. if (found) {
  2355. spin_lock(&found->lock);
  2356. found->total_bytes += total_bytes;
  2357. found->bytes_used += bytes_used;
  2358. found->full = 0;
  2359. spin_unlock(&found->lock);
  2360. *space_info = found;
  2361. return 0;
  2362. }
  2363. found = kzalloc(sizeof(*found), GFP_NOFS);
  2364. if (!found)
  2365. return -ENOMEM;
  2366. INIT_LIST_HEAD(&found->block_groups);
  2367. init_rwsem(&found->groups_sem);
  2368. spin_lock_init(&found->lock);
  2369. found->flags = flags;
  2370. found->total_bytes = total_bytes;
  2371. found->bytes_used = bytes_used;
  2372. found->bytes_pinned = 0;
  2373. found->bytes_reserved = 0;
  2374. found->bytes_readonly = 0;
  2375. found->bytes_delalloc = 0;
  2376. found->full = 0;
  2377. found->force_alloc = 0;
  2378. *space_info = found;
  2379. list_add_rcu(&found->list, &info->space_info);
  2380. atomic_set(&found->caching_threads, 0);
  2381. return 0;
  2382. }
  2383. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  2384. {
  2385. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  2386. BTRFS_BLOCK_GROUP_RAID1 |
  2387. BTRFS_BLOCK_GROUP_RAID10 |
  2388. BTRFS_BLOCK_GROUP_DUP);
  2389. if (extra_flags) {
  2390. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2391. fs_info->avail_data_alloc_bits |= extra_flags;
  2392. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2393. fs_info->avail_metadata_alloc_bits |= extra_flags;
  2394. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2395. fs_info->avail_system_alloc_bits |= extra_flags;
  2396. }
  2397. }
  2398. static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
  2399. {
  2400. spin_lock(&cache->space_info->lock);
  2401. spin_lock(&cache->lock);
  2402. if (!cache->ro) {
  2403. cache->space_info->bytes_readonly += cache->key.offset -
  2404. btrfs_block_group_used(&cache->item);
  2405. cache->ro = 1;
  2406. }
  2407. spin_unlock(&cache->lock);
  2408. spin_unlock(&cache->space_info->lock);
  2409. }
  2410. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  2411. {
  2412. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  2413. if (num_devices == 1)
  2414. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  2415. if (num_devices < 4)
  2416. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  2417. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  2418. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2419. BTRFS_BLOCK_GROUP_RAID10))) {
  2420. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  2421. }
  2422. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  2423. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  2424. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  2425. }
  2426. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  2427. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  2428. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  2429. (flags & BTRFS_BLOCK_GROUP_DUP)))
  2430. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  2431. return flags;
  2432. }
  2433. static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
  2434. {
  2435. struct btrfs_fs_info *info = root->fs_info;
  2436. u64 alloc_profile;
  2437. if (data) {
  2438. alloc_profile = info->avail_data_alloc_bits &
  2439. info->data_alloc_profile;
  2440. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  2441. } else if (root == root->fs_info->chunk_root) {
  2442. alloc_profile = info->avail_system_alloc_bits &
  2443. info->system_alloc_profile;
  2444. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  2445. } else {
  2446. alloc_profile = info->avail_metadata_alloc_bits &
  2447. info->metadata_alloc_profile;
  2448. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  2449. }
  2450. return btrfs_reduce_alloc_profile(root, data);
  2451. }
  2452. void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
  2453. {
  2454. u64 alloc_target;
  2455. alloc_target = btrfs_get_alloc_profile(root, 1);
  2456. BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
  2457. alloc_target);
  2458. }
  2459. static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
  2460. {
  2461. u64 num_bytes;
  2462. int level;
  2463. level = BTRFS_MAX_LEVEL - 2;
  2464. /*
  2465. * NOTE: these calculations are absolutely the worst possible case.
  2466. * This assumes that _every_ item we insert will require a new leaf, and
  2467. * that the tree has grown to its maximum level size.
  2468. */
  2469. /*
  2470. * for every item we insert we could insert both an extent item and a
  2471. * extent ref item. Then for ever item we insert, we will need to cow
  2472. * both the original leaf, plus the leaf to the left and right of it.
  2473. *
  2474. * Unless we are talking about the extent root, then we just want the
  2475. * number of items * 2, since we just need the extent item plus its ref.
  2476. */
  2477. if (root == root->fs_info->extent_root)
  2478. num_bytes = num_items * 2;
  2479. else
  2480. num_bytes = (num_items + (2 * num_items)) * 3;
  2481. /*
  2482. * num_bytes is total number of leaves we could need times the leaf
  2483. * size, and then for every leaf we could end up cow'ing 2 nodes per
  2484. * level, down to the leaf level.
  2485. */
  2486. num_bytes = (num_bytes * root->leafsize) +
  2487. (num_bytes * (level * 2)) * root->nodesize;
  2488. return num_bytes;
  2489. }
  2490. /*
  2491. * Unreserve metadata space for delalloc. If we have less reserved credits than
  2492. * we have extents, this function does nothing.
  2493. */
  2494. int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
  2495. struct inode *inode, int num_items)
  2496. {
  2497. struct btrfs_fs_info *info = root->fs_info;
  2498. struct btrfs_space_info *meta_sinfo;
  2499. u64 num_bytes;
  2500. u64 alloc_target;
  2501. bool bug = false;
  2502. /* get the space info for where the metadata will live */
  2503. alloc_target = btrfs_get_alloc_profile(root, 0);
  2504. meta_sinfo = __find_space_info(info, alloc_target);
  2505. num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
  2506. num_items);
  2507. spin_lock(&meta_sinfo->lock);
  2508. spin_lock(&BTRFS_I(inode)->accounting_lock);
  2509. if (BTRFS_I(inode)->reserved_extents <=
  2510. BTRFS_I(inode)->outstanding_extents) {
  2511. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  2512. spin_unlock(&meta_sinfo->lock);
  2513. return 0;
  2514. }
  2515. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  2516. BTRFS_I(inode)->reserved_extents--;
  2517. BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
  2518. if (meta_sinfo->bytes_delalloc < num_bytes) {
  2519. bug = true;
  2520. meta_sinfo->bytes_delalloc = 0;
  2521. } else {
  2522. meta_sinfo->bytes_delalloc -= num_bytes;
  2523. }
  2524. spin_unlock(&meta_sinfo->lock);
  2525. BUG_ON(bug);
  2526. return 0;
  2527. }
  2528. static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
  2529. {
  2530. u64 thresh;
  2531. thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
  2532. meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
  2533. meta_sinfo->bytes_super + meta_sinfo->bytes_root +
  2534. meta_sinfo->bytes_may_use;
  2535. thresh = meta_sinfo->total_bytes - thresh;
  2536. thresh *= 80;
  2537. do_div(thresh, 100);
  2538. if (thresh <= meta_sinfo->bytes_delalloc)
  2539. meta_sinfo->force_delalloc = 1;
  2540. else
  2541. meta_sinfo->force_delalloc = 0;
  2542. }
  2543. struct async_flush {
  2544. struct btrfs_root *root;
  2545. struct btrfs_space_info *info;
  2546. struct btrfs_work work;
  2547. };
  2548. static noinline void flush_delalloc_async(struct btrfs_work *work)
  2549. {
  2550. struct async_flush *async;
  2551. struct btrfs_root *root;
  2552. struct btrfs_space_info *info;
  2553. async = container_of(work, struct async_flush, work);
  2554. root = async->root;
  2555. info = async->info;
  2556. btrfs_start_delalloc_inodes(root);
  2557. wake_up(&info->flush_wait);
  2558. btrfs_wait_ordered_extents(root, 0);
  2559. spin_lock(&info->lock);
  2560. info->flushing = 0;
  2561. spin_unlock(&info->lock);
  2562. wake_up(&info->flush_wait);
  2563. kfree(async);
  2564. }
  2565. static void wait_on_flush(struct btrfs_space_info *info)
  2566. {
  2567. DEFINE_WAIT(wait);
  2568. u64 used;
  2569. while (1) {
  2570. prepare_to_wait(&info->flush_wait, &wait,
  2571. TASK_UNINTERRUPTIBLE);
  2572. spin_lock(&info->lock);
  2573. if (!info->flushing) {
  2574. spin_unlock(&info->lock);
  2575. break;
  2576. }
  2577. used = info->bytes_used + info->bytes_reserved +
  2578. info->bytes_pinned + info->bytes_readonly +
  2579. info->bytes_super + info->bytes_root +
  2580. info->bytes_may_use + info->bytes_delalloc;
  2581. if (used < info->total_bytes) {
  2582. spin_unlock(&info->lock);
  2583. break;
  2584. }
  2585. spin_unlock(&info->lock);
  2586. schedule();
  2587. }
  2588. finish_wait(&info->flush_wait, &wait);
  2589. }
  2590. static void flush_delalloc(struct btrfs_root *root,
  2591. struct btrfs_space_info *info)
  2592. {
  2593. struct async_flush *async;
  2594. bool wait = false;
  2595. spin_lock(&info->lock);
  2596. if (!info->flushing) {
  2597. info->flushing = 1;
  2598. init_waitqueue_head(&info->flush_wait);
  2599. } else {
  2600. wait = true;
  2601. }
  2602. spin_unlock(&info->lock);
  2603. if (wait) {
  2604. wait_on_flush(info);
  2605. return;
  2606. }
  2607. async = kzalloc(sizeof(*async), GFP_NOFS);
  2608. if (!async)
  2609. goto flush;
  2610. async->root = root;
  2611. async->info = info;
  2612. async->work.func = flush_delalloc_async;
  2613. btrfs_queue_worker(&root->fs_info->enospc_workers,
  2614. &async->work);
  2615. wait_on_flush(info);
  2616. return;
  2617. flush:
  2618. btrfs_start_delalloc_inodes(root);
  2619. btrfs_wait_ordered_extents(root, 0);
  2620. spin_lock(&info->lock);
  2621. info->flushing = 0;
  2622. spin_unlock(&info->lock);
  2623. wake_up(&info->flush_wait);
  2624. }
  2625. static int maybe_allocate_chunk(struct btrfs_root *root,
  2626. struct btrfs_space_info *info)
  2627. {
  2628. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  2629. struct btrfs_trans_handle *trans;
  2630. bool wait = false;
  2631. int ret = 0;
  2632. u64 min_metadata;
  2633. u64 free_space;
  2634. free_space = btrfs_super_total_bytes(disk_super);
  2635. /*
  2636. * we allow the metadata to grow to a max of either 5gb or 5% of the
  2637. * space in the volume.
  2638. */
  2639. min_metadata = min((u64)5 * 1024 * 1024 * 1024,
  2640. div64_u64(free_space * 5, 100));
  2641. if (info->total_bytes >= min_metadata) {
  2642. spin_unlock(&info->lock);
  2643. return 0;
  2644. }
  2645. if (info->full) {
  2646. spin_unlock(&info->lock);
  2647. return 0;
  2648. }
  2649. if (!info->allocating_chunk) {
  2650. info->force_alloc = 1;
  2651. info->allocating_chunk = 1;
  2652. init_waitqueue_head(&info->allocate_wait);
  2653. } else {
  2654. wait = true;
  2655. }
  2656. spin_unlock(&info->lock);
  2657. if (wait) {
  2658. wait_event(info->allocate_wait,
  2659. !info->allocating_chunk);
  2660. return 1;
  2661. }
  2662. trans = btrfs_start_transaction(root, 1);
  2663. if (!trans) {
  2664. ret = -ENOMEM;
  2665. goto out;
  2666. }
  2667. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2668. 4096 + 2 * 1024 * 1024,
  2669. info->flags, 0);
  2670. btrfs_end_transaction(trans, root);
  2671. if (ret)
  2672. goto out;
  2673. out:
  2674. spin_lock(&info->lock);
  2675. info->allocating_chunk = 0;
  2676. spin_unlock(&info->lock);
  2677. wake_up(&info->allocate_wait);
  2678. if (ret)
  2679. return 0;
  2680. return 1;
  2681. }
  2682. /*
  2683. * Reserve metadata space for delalloc.
  2684. */
  2685. int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
  2686. struct inode *inode, int num_items)
  2687. {
  2688. struct btrfs_fs_info *info = root->fs_info;
  2689. struct btrfs_space_info *meta_sinfo;
  2690. u64 num_bytes;
  2691. u64 used;
  2692. u64 alloc_target;
  2693. int flushed = 0;
  2694. int force_delalloc;
  2695. /* get the space info for where the metadata will live */
  2696. alloc_target = btrfs_get_alloc_profile(root, 0);
  2697. meta_sinfo = __find_space_info(info, alloc_target);
  2698. num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
  2699. num_items);
  2700. again:
  2701. spin_lock(&meta_sinfo->lock);
  2702. force_delalloc = meta_sinfo->force_delalloc;
  2703. if (unlikely(!meta_sinfo->bytes_root))
  2704. meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
  2705. if (!flushed)
  2706. meta_sinfo->bytes_delalloc += num_bytes;
  2707. used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
  2708. meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
  2709. meta_sinfo->bytes_super + meta_sinfo->bytes_root +
  2710. meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
  2711. if (used > meta_sinfo->total_bytes) {
  2712. flushed++;
  2713. if (flushed == 1) {
  2714. if (maybe_allocate_chunk(root, meta_sinfo))
  2715. goto again;
  2716. flushed++;
  2717. } else {
  2718. spin_unlock(&meta_sinfo->lock);
  2719. }
  2720. if (flushed == 2) {
  2721. filemap_flush(inode->i_mapping);
  2722. goto again;
  2723. } else if (flushed == 3) {
  2724. flush_delalloc(root, meta_sinfo);
  2725. goto again;
  2726. }
  2727. spin_lock(&meta_sinfo->lock);
  2728. meta_sinfo->bytes_delalloc -= num_bytes;
  2729. spin_unlock(&meta_sinfo->lock);
  2730. printk(KERN_ERR "enospc, has %d, reserved %d\n",
  2731. BTRFS_I(inode)->outstanding_extents,
  2732. BTRFS_I(inode)->reserved_extents);
  2733. dump_space_info(meta_sinfo, 0, 0);
  2734. return -ENOSPC;
  2735. }
  2736. BTRFS_I(inode)->reserved_extents++;
  2737. check_force_delalloc(meta_sinfo);
  2738. spin_unlock(&meta_sinfo->lock);
  2739. if (!flushed && force_delalloc)
  2740. filemap_flush(inode->i_mapping);
  2741. return 0;
  2742. }
  2743. /*
  2744. * unreserve num_items number of items worth of metadata space. This needs to
  2745. * be paired with btrfs_reserve_metadata_space.
  2746. *
  2747. * NOTE: if you have the option, run this _AFTER_ you do a
  2748. * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
  2749. * oprations which will result in more used metadata, so we want to make sure we
  2750. * can do that without issue.
  2751. */
  2752. int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
  2753. {
  2754. struct btrfs_fs_info *info = root->fs_info;
  2755. struct btrfs_space_info *meta_sinfo;
  2756. u64 num_bytes;
  2757. u64 alloc_target;
  2758. bool bug = false;
  2759. /* get the space info for where the metadata will live */
  2760. alloc_target = btrfs_get_alloc_profile(root, 0);
  2761. meta_sinfo = __find_space_info(info, alloc_target);
  2762. num_bytes = calculate_bytes_needed(root, num_items);
  2763. spin_lock(&meta_sinfo->lock);
  2764. if (meta_sinfo->bytes_may_use < num_bytes) {
  2765. bug = true;
  2766. meta_sinfo->bytes_may_use = 0;
  2767. } else {
  2768. meta_sinfo->bytes_may_use -= num_bytes;
  2769. }
  2770. spin_unlock(&meta_sinfo->lock);
  2771. BUG_ON(bug);
  2772. return 0;
  2773. }
  2774. /*
  2775. * Reserve some metadata space for use. We'll calculate the worste case number
  2776. * of bytes that would be needed to modify num_items number of items. If we
  2777. * have space, fantastic, if not, you get -ENOSPC. Please call
  2778. * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
  2779. * items you reserved, since whatever metadata you needed should have already
  2780. * been allocated.
  2781. *
  2782. * This will commit the transaction to make more space if we don't have enough
  2783. * metadata space. THe only time we don't do this is if we're reserving space
  2784. * inside of a transaction, then we will just return -ENOSPC and it is the
  2785. * callers responsibility to handle it properly.
  2786. */
  2787. int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
  2788. {
  2789. struct btrfs_fs_info *info = root->fs_info;
  2790. struct btrfs_space_info *meta_sinfo;
  2791. u64 num_bytes;
  2792. u64 used;
  2793. u64 alloc_target;
  2794. int retries = 0;
  2795. /* get the space info for where the metadata will live */
  2796. alloc_target = btrfs_get_alloc_profile(root, 0);
  2797. meta_sinfo = __find_space_info(info, alloc_target);
  2798. num_bytes = calculate_bytes_needed(root, num_items);
  2799. again:
  2800. spin_lock(&meta_sinfo->lock);
  2801. if (unlikely(!meta_sinfo->bytes_root))
  2802. meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
  2803. if (!retries)
  2804. meta_sinfo->bytes_may_use += num_bytes;
  2805. used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
  2806. meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
  2807. meta_sinfo->bytes_super + meta_sinfo->bytes_root +
  2808. meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
  2809. if (used > meta_sinfo->total_bytes) {
  2810. retries++;
  2811. if (retries == 1) {
  2812. if (maybe_allocate_chunk(root, meta_sinfo))
  2813. goto again;
  2814. retries++;
  2815. } else {
  2816. spin_unlock(&meta_sinfo->lock);
  2817. }
  2818. if (retries == 2) {
  2819. flush_delalloc(root, meta_sinfo);
  2820. goto again;
  2821. }
  2822. spin_lock(&meta_sinfo->lock);
  2823. meta_sinfo->bytes_may_use -= num_bytes;
  2824. spin_unlock(&meta_sinfo->lock);
  2825. dump_space_info(meta_sinfo, 0, 0);
  2826. return -ENOSPC;
  2827. }
  2828. check_force_delalloc(meta_sinfo);
  2829. spin_unlock(&meta_sinfo->lock);
  2830. return 0;
  2831. }
  2832. /*
  2833. * This will check the space that the inode allocates from to make sure we have
  2834. * enough space for bytes.
  2835. */
  2836. int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
  2837. u64 bytes)
  2838. {
  2839. struct btrfs_space_info *data_sinfo;
  2840. int ret = 0, committed = 0;
  2841. /* make sure bytes are sectorsize aligned */
  2842. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2843. data_sinfo = BTRFS_I(inode)->space_info;
  2844. if (!data_sinfo)
  2845. goto alloc;
  2846. again:
  2847. /* make sure we have enough space to handle the data first */
  2848. spin_lock(&data_sinfo->lock);
  2849. if (data_sinfo->total_bytes - data_sinfo->bytes_used -
  2850. data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
  2851. data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
  2852. data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
  2853. struct btrfs_trans_handle *trans;
  2854. /*
  2855. * if we don't have enough free bytes in this space then we need
  2856. * to alloc a new chunk.
  2857. */
  2858. if (!data_sinfo->full) {
  2859. u64 alloc_target;
  2860. data_sinfo->force_alloc = 1;
  2861. spin_unlock(&data_sinfo->lock);
  2862. alloc:
  2863. alloc_target = btrfs_get_alloc_profile(root, 1);
  2864. trans = btrfs_start_transaction(root, 1);
  2865. if (!trans)
  2866. return -ENOMEM;
  2867. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2868. bytes + 2 * 1024 * 1024,
  2869. alloc_target, 0);
  2870. btrfs_end_transaction(trans, root);
  2871. if (ret)
  2872. return ret;
  2873. if (!data_sinfo) {
  2874. btrfs_set_inode_space_info(root, inode);
  2875. data_sinfo = BTRFS_I(inode)->space_info;
  2876. }
  2877. goto again;
  2878. }
  2879. spin_unlock(&data_sinfo->lock);
  2880. /* commit the current transaction and try again */
  2881. if (!committed && !root->fs_info->open_ioctl_trans) {
  2882. committed = 1;
  2883. trans = btrfs_join_transaction(root, 1);
  2884. if (!trans)
  2885. return -ENOMEM;
  2886. ret = btrfs_commit_transaction(trans, root);
  2887. if (ret)
  2888. return ret;
  2889. goto again;
  2890. }
  2891. printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
  2892. ", %llu bytes_used, %llu bytes_reserved, "
  2893. "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
  2894. "%llu total\n", (unsigned long long)bytes,
  2895. (unsigned long long)data_sinfo->bytes_delalloc,
  2896. (unsigned long long)data_sinfo->bytes_used,
  2897. (unsigned long long)data_sinfo->bytes_reserved,
  2898. (unsigned long long)data_sinfo->bytes_pinned,
  2899. (unsigned long long)data_sinfo->bytes_readonly,
  2900. (unsigned long long)data_sinfo->bytes_may_use,
  2901. (unsigned long long)data_sinfo->total_bytes);
  2902. return -ENOSPC;
  2903. }
  2904. data_sinfo->bytes_may_use += bytes;
  2905. BTRFS_I(inode)->reserved_bytes += bytes;
  2906. spin_unlock(&data_sinfo->lock);
  2907. return 0;
  2908. }
  2909. /*
  2910. * if there was an error for whatever reason after calling
  2911. * btrfs_check_data_free_space, call this so we can cleanup the counters.
  2912. */
  2913. void btrfs_free_reserved_data_space(struct btrfs_root *root,
  2914. struct inode *inode, u64 bytes)
  2915. {
  2916. struct btrfs_space_info *data_sinfo;
  2917. /* make sure bytes are sectorsize aligned */
  2918. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2919. data_sinfo = BTRFS_I(inode)->space_info;
  2920. spin_lock(&data_sinfo->lock);
  2921. data_sinfo->bytes_may_use -= bytes;
  2922. BTRFS_I(inode)->reserved_bytes -= bytes;
  2923. spin_unlock(&data_sinfo->lock);
  2924. }
  2925. /* called when we are adding a delalloc extent to the inode's io_tree */
  2926. void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
  2927. u64 bytes)
  2928. {
  2929. struct btrfs_space_info *data_sinfo;
  2930. /* get the space info for where this inode will be storing its data */
  2931. data_sinfo = BTRFS_I(inode)->space_info;
  2932. /* make sure we have enough space to handle the data first */
  2933. spin_lock(&data_sinfo->lock);
  2934. data_sinfo->bytes_delalloc += bytes;
  2935. /*
  2936. * we are adding a delalloc extent without calling
  2937. * btrfs_check_data_free_space first. This happens on a weird
  2938. * writepage condition, but shouldn't hurt our accounting
  2939. */
  2940. if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
  2941. data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
  2942. BTRFS_I(inode)->reserved_bytes = 0;
  2943. } else {
  2944. data_sinfo->bytes_may_use -= bytes;
  2945. BTRFS_I(inode)->reserved_bytes -= bytes;
  2946. }
  2947. spin_unlock(&data_sinfo->lock);
  2948. }
  2949. /* called when we are clearing an delalloc extent from the inode's io_tree */
  2950. void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
  2951. u64 bytes)
  2952. {
  2953. struct btrfs_space_info *info;
  2954. info = BTRFS_I(inode)->space_info;
  2955. spin_lock(&info->lock);
  2956. info->bytes_delalloc -= bytes;
  2957. spin_unlock(&info->lock);
  2958. }
  2959. static void force_metadata_allocation(struct btrfs_fs_info *info)
  2960. {
  2961. struct list_head *head = &info->space_info;
  2962. struct btrfs_space_info *found;
  2963. rcu_read_lock();
  2964. list_for_each_entry_rcu(found, head, list) {
  2965. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  2966. found->force_alloc = 1;
  2967. }
  2968. rcu_read_unlock();
  2969. }
  2970. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  2971. struct btrfs_root *extent_root, u64 alloc_bytes,
  2972. u64 flags, int force)
  2973. {
  2974. struct btrfs_space_info *space_info;
  2975. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  2976. u64 thresh;
  2977. int ret = 0;
  2978. mutex_lock(&fs_info->chunk_mutex);
  2979. flags = btrfs_reduce_alloc_profile(extent_root, flags);
  2980. space_info = __find_space_info(extent_root->fs_info, flags);
  2981. if (!space_info) {
  2982. ret = update_space_info(extent_root->fs_info, flags,
  2983. 0, 0, &space_info);
  2984. BUG_ON(ret);
  2985. }
  2986. BUG_ON(!space_info);
  2987. spin_lock(&space_info->lock);
  2988. if (space_info->force_alloc)
  2989. force = 1;
  2990. if (space_info->full) {
  2991. spin_unlock(&space_info->lock);
  2992. goto out;
  2993. }
  2994. thresh = space_info->total_bytes - space_info->bytes_readonly;
  2995. thresh = div_factor(thresh, 8);
  2996. if (!force &&
  2997. (space_info->bytes_used + space_info->bytes_pinned +
  2998. space_info->bytes_reserved + alloc_bytes) < thresh) {
  2999. spin_unlock(&space_info->lock);
  3000. goto out;
  3001. }
  3002. spin_unlock(&space_info->lock);
  3003. /*
  3004. * if we're doing a data chunk, go ahead and make sure that
  3005. * we keep a reasonable number of metadata chunks allocated in the
  3006. * FS as well.
  3007. */
  3008. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3009. fs_info->data_chunk_allocations++;
  3010. if (!(fs_info->data_chunk_allocations %
  3011. fs_info->metadata_ratio))
  3012. force_metadata_allocation(fs_info);
  3013. }
  3014. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3015. spin_lock(&space_info->lock);
  3016. if (ret)
  3017. space_info->full = 1;
  3018. space_info->force_alloc = 0;
  3019. spin_unlock(&space_info->lock);
  3020. out:
  3021. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  3022. return ret;
  3023. }
  3024. static int update_block_group(struct btrfs_trans_handle *trans,
  3025. struct btrfs_root *root,
  3026. u64 bytenr, u64 num_bytes, int alloc,
  3027. int mark_free)
  3028. {
  3029. struct btrfs_block_group_cache *cache;
  3030. struct btrfs_fs_info *info = root->fs_info;
  3031. u64 total = num_bytes;
  3032. u64 old_val;
  3033. u64 byte_in_group;
  3034. /* block accounting for super block */
  3035. spin_lock(&info->delalloc_lock);
  3036. old_val = btrfs_super_bytes_used(&info->super_copy);
  3037. if (alloc)
  3038. old_val += num_bytes;
  3039. else
  3040. old_val -= num_bytes;
  3041. btrfs_set_super_bytes_used(&info->super_copy, old_val);
  3042. /* block accounting for root item */
  3043. old_val = btrfs_root_used(&root->root_item);
  3044. if (alloc)
  3045. old_val += num_bytes;
  3046. else
  3047. old_val -= num_bytes;
  3048. btrfs_set_root_used(&root->root_item, old_val);
  3049. spin_unlock(&info->delalloc_lock);
  3050. while (total) {
  3051. cache = btrfs_lookup_block_group(info, bytenr);
  3052. if (!cache)
  3053. return -1;
  3054. byte_in_group = bytenr - cache->key.objectid;
  3055. WARN_ON(byte_in_group > cache->key.offset);
  3056. spin_lock(&cache->space_info->lock);
  3057. spin_lock(&cache->lock);
  3058. cache->dirty = 1;
  3059. old_val = btrfs_block_group_used(&cache->item);
  3060. num_bytes = min(total, cache->key.offset - byte_in_group);
  3061. if (alloc) {
  3062. old_val += num_bytes;
  3063. btrfs_set_block_group_used(&cache->item, old_val);
  3064. cache->reserved -= num_bytes;
  3065. cache->space_info->bytes_used += num_bytes;
  3066. cache->space_info->bytes_reserved -= num_bytes;
  3067. if (cache->ro)
  3068. cache->space_info->bytes_readonly -= num_bytes;
  3069. spin_unlock(&cache->lock);
  3070. spin_unlock(&cache->space_info->lock);
  3071. } else {
  3072. old_val -= num_bytes;
  3073. cache->space_info->bytes_used -= num_bytes;
  3074. if (cache->ro)
  3075. cache->space_info->bytes_readonly += num_bytes;
  3076. btrfs_set_block_group_used(&cache->item, old_val);
  3077. spin_unlock(&cache->lock);
  3078. spin_unlock(&cache->space_info->lock);
  3079. if (mark_free) {
  3080. int ret;
  3081. ret = btrfs_discard_extent(root, bytenr,
  3082. num_bytes);
  3083. WARN_ON(ret);
  3084. ret = btrfs_add_free_space(cache, bytenr,
  3085. num_bytes);
  3086. WARN_ON(ret);
  3087. }
  3088. }
  3089. btrfs_put_block_group(cache);
  3090. total -= num_bytes;
  3091. bytenr += num_bytes;
  3092. }
  3093. return 0;
  3094. }
  3095. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  3096. {
  3097. struct btrfs_block_group_cache *cache;
  3098. u64 bytenr;
  3099. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  3100. if (!cache)
  3101. return 0;
  3102. bytenr = cache->key.objectid;
  3103. btrfs_put_block_group(cache);
  3104. return bytenr;
  3105. }
  3106. /*
  3107. * this function must be called within transaction
  3108. */
  3109. int btrfs_pin_extent(struct btrfs_root *root,
  3110. u64 bytenr, u64 num_bytes, int reserved)
  3111. {
  3112. struct btrfs_fs_info *fs_info = root->fs_info;
  3113. struct btrfs_block_group_cache *cache;
  3114. cache = btrfs_lookup_block_group(fs_info, bytenr);
  3115. BUG_ON(!cache);
  3116. spin_lock(&cache->space_info->lock);
  3117. spin_lock(&cache->lock);
  3118. cache->pinned += num_bytes;
  3119. cache->space_info->bytes_pinned += num_bytes;
  3120. if (reserved) {
  3121. cache->reserved -= num_bytes;
  3122. cache->space_info->bytes_reserved -= num_bytes;
  3123. }
  3124. spin_unlock(&cache->lock);
  3125. spin_unlock(&cache->space_info->lock);
  3126. btrfs_put_block_group(cache);
  3127. set_extent_dirty(fs_info->pinned_extents,
  3128. bytenr, bytenr + num_bytes - 1, GFP_NOFS);
  3129. return 0;
  3130. }
  3131. static int update_reserved_extents(struct btrfs_block_group_cache *cache,
  3132. u64 num_bytes, int reserve)
  3133. {
  3134. spin_lock(&cache->space_info->lock);
  3135. spin_lock(&cache->lock);
  3136. if (reserve) {
  3137. cache->reserved += num_bytes;
  3138. cache->space_info->bytes_reserved += num_bytes;
  3139. } else {
  3140. cache->reserved -= num_bytes;
  3141. cache->space_info->bytes_reserved -= num_bytes;
  3142. }
  3143. spin_unlock(&cache->lock);
  3144. spin_unlock(&cache->space_info->lock);
  3145. return 0;
  3146. }
  3147. int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  3148. struct btrfs_root *root)
  3149. {
  3150. struct btrfs_fs_info *fs_info = root->fs_info;
  3151. struct btrfs_caching_control *next;
  3152. struct btrfs_caching_control *caching_ctl;
  3153. struct btrfs_block_group_cache *cache;
  3154. down_write(&fs_info->extent_commit_sem);
  3155. list_for_each_entry_safe(caching_ctl, next,
  3156. &fs_info->caching_block_groups, list) {
  3157. cache = caching_ctl->block_group;
  3158. if (block_group_cache_done(cache)) {
  3159. cache->last_byte_to_unpin = (u64)-1;
  3160. list_del_init(&caching_ctl->list);
  3161. put_caching_control(caching_ctl);
  3162. } else {
  3163. cache->last_byte_to_unpin = caching_ctl->progress;
  3164. }
  3165. }
  3166. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  3167. fs_info->pinned_extents = &fs_info->freed_extents[1];
  3168. else
  3169. fs_info->pinned_extents = &fs_info->freed_extents[0];
  3170. up_write(&fs_info->extent_commit_sem);
  3171. return 0;
  3172. }
  3173. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  3174. {
  3175. struct btrfs_fs_info *fs_info = root->fs_info;
  3176. struct btrfs_block_group_cache *cache = NULL;
  3177. u64 len;
  3178. while (start <= end) {
  3179. if (!cache ||
  3180. start >= cache->key.objectid + cache->key.offset) {
  3181. if (cache)
  3182. btrfs_put_block_group(cache);
  3183. cache = btrfs_lookup_block_group(fs_info, start);
  3184. BUG_ON(!cache);
  3185. }
  3186. len = cache->key.objectid + cache->key.offset - start;
  3187. len = min(len, end + 1 - start);
  3188. if (start < cache->last_byte_to_unpin) {
  3189. len = min(len, cache->last_byte_to_unpin - start);
  3190. btrfs_add_free_space(cache, start, len);
  3191. }
  3192. spin_lock(&cache->space_info->lock);
  3193. spin_lock(&cache->lock);
  3194. cache->pinned -= len;
  3195. cache->space_info->bytes_pinned -= len;
  3196. spin_unlock(&cache->lock);
  3197. spin_unlock(&cache->space_info->lock);
  3198. start += len;
  3199. }
  3200. if (cache)
  3201. btrfs_put_block_group(cache);
  3202. return 0;
  3203. }
  3204. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  3205. struct btrfs_root *root)
  3206. {
  3207. struct btrfs_fs_info *fs_info = root->fs_info;
  3208. struct extent_io_tree *unpin;
  3209. u64 start;
  3210. u64 end;
  3211. int ret;
  3212. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  3213. unpin = &fs_info->freed_extents[1];
  3214. else
  3215. unpin = &fs_info->freed_extents[0];
  3216. while (1) {
  3217. ret = find_first_extent_bit(unpin, 0, &start, &end,
  3218. EXTENT_DIRTY);
  3219. if (ret)
  3220. break;
  3221. ret = btrfs_discard_extent(root, start, end + 1 - start);
  3222. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  3223. unpin_extent_range(root, start, end);
  3224. cond_resched();
  3225. }
  3226. return ret;
  3227. }
  3228. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  3229. struct btrfs_root *root,
  3230. struct btrfs_path *path,
  3231. u64 bytenr, u64 num_bytes,
  3232. int is_data, int reserved,
  3233. struct extent_buffer **must_clean)
  3234. {
  3235. int err = 0;
  3236. struct extent_buffer *buf;
  3237. if (is_data)
  3238. goto pinit;
  3239. /*
  3240. * discard is sloooow, and so triggering discards on
  3241. * individual btree blocks isn't a good plan. Just
  3242. * pin everything in discard mode.
  3243. */
  3244. if (btrfs_test_opt(root, DISCARD))
  3245. goto pinit;
  3246. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  3247. if (!buf)
  3248. goto pinit;
  3249. /* we can reuse a block if it hasn't been written
  3250. * and it is from this transaction. We can't
  3251. * reuse anything from the tree log root because
  3252. * it has tiny sub-transactions.
  3253. */
  3254. if (btrfs_buffer_uptodate(buf, 0) &&
  3255. btrfs_try_tree_lock(buf)) {
  3256. u64 header_owner = btrfs_header_owner(buf);
  3257. u64 header_transid = btrfs_header_generation(buf);
  3258. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  3259. header_transid == trans->transid &&
  3260. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  3261. *must_clean = buf;
  3262. return 1;
  3263. }
  3264. btrfs_tree_unlock(buf);
  3265. }
  3266. free_extent_buffer(buf);
  3267. pinit:
  3268. if (path)
  3269. btrfs_set_path_blocking(path);
  3270. /* unlocks the pinned mutex */
  3271. btrfs_pin_extent(root, bytenr, num_bytes, reserved);
  3272. BUG_ON(err < 0);
  3273. return 0;
  3274. }
  3275. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  3276. struct btrfs_root *root,
  3277. u64 bytenr, u64 num_bytes, u64 parent,
  3278. u64 root_objectid, u64 owner_objectid,
  3279. u64 owner_offset, int refs_to_drop,
  3280. struct btrfs_delayed_extent_op *extent_op)
  3281. {
  3282. struct btrfs_key key;
  3283. struct btrfs_path *path;
  3284. struct btrfs_fs_info *info = root->fs_info;
  3285. struct btrfs_root *extent_root = info->extent_root;
  3286. struct extent_buffer *leaf;
  3287. struct btrfs_extent_item *ei;
  3288. struct btrfs_extent_inline_ref *iref;
  3289. int ret;
  3290. int is_data;
  3291. int extent_slot = 0;
  3292. int found_extent = 0;
  3293. int num_to_del = 1;
  3294. u32 item_size;
  3295. u64 refs;
  3296. path = btrfs_alloc_path();
  3297. if (!path)
  3298. return -ENOMEM;
  3299. path->reada = 1;
  3300. path->leave_spinning = 1;
  3301. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  3302. BUG_ON(!is_data && refs_to_drop != 1);
  3303. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  3304. bytenr, num_bytes, parent,
  3305. root_objectid, owner_objectid,
  3306. owner_offset);
  3307. if (ret == 0) {
  3308. extent_slot = path->slots[0];
  3309. while (extent_slot >= 0) {
  3310. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3311. extent_slot);
  3312. if (key.objectid != bytenr)
  3313. break;
  3314. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  3315. key.offset == num_bytes) {
  3316. found_extent = 1;
  3317. break;
  3318. }
  3319. if (path->slots[0] - extent_slot > 5)
  3320. break;
  3321. extent_slot--;
  3322. }
  3323. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  3324. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  3325. if (found_extent && item_size < sizeof(*ei))
  3326. found_extent = 0;
  3327. #endif
  3328. if (!found_extent) {
  3329. BUG_ON(iref);
  3330. ret = remove_extent_backref(trans, extent_root, path,
  3331. NULL, refs_to_drop,
  3332. is_data);
  3333. BUG_ON(ret);
  3334. btrfs_release_path(extent_root, path);
  3335. path->leave_spinning = 1;
  3336. key.objectid = bytenr;
  3337. key.type = BTRFS_EXTENT_ITEM_KEY;
  3338. key.offset = num_bytes;
  3339. ret = btrfs_search_slot(trans, extent_root,
  3340. &key, path, -1, 1);
  3341. if (ret) {
  3342. printk(KERN_ERR "umm, got %d back from search"
  3343. ", was looking for %llu\n", ret,
  3344. (unsigned long long)bytenr);
  3345. btrfs_print_leaf(extent_root, path->nodes[0]);
  3346. }
  3347. BUG_ON(ret);
  3348. extent_slot = path->slots[0];
  3349. }
  3350. } else {
  3351. btrfs_print_leaf(extent_root, path->nodes[0]);
  3352. WARN_ON(1);
  3353. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  3354. "parent %llu root %llu owner %llu offset %llu\n",
  3355. (unsigned long long)bytenr,
  3356. (unsigned long long)parent,
  3357. (unsigned long long)root_objectid,
  3358. (unsigned long long)owner_objectid,
  3359. (unsigned long long)owner_offset);
  3360. }
  3361. leaf = path->nodes[0];
  3362. item_size = btrfs_item_size_nr(leaf, extent_slot);
  3363. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  3364. if (item_size < sizeof(*ei)) {
  3365. BUG_ON(found_extent || extent_slot != path->slots[0]);
  3366. ret = convert_extent_item_v0(trans, extent_root, path,
  3367. owner_objectid, 0);
  3368. BUG_ON(ret < 0);
  3369. btrfs_release_path(extent_root, path);
  3370. path->leave_spinning = 1;
  3371. key.objectid = bytenr;
  3372. key.type = BTRFS_EXTENT_ITEM_KEY;
  3373. key.offset = num_bytes;
  3374. ret = btrfs_search_slot(trans, extent_root, &key, path,
  3375. -1, 1);
  3376. if (ret) {
  3377. printk(KERN_ERR "umm, got %d back from search"
  3378. ", was looking for %llu\n", ret,
  3379. (unsigned long long)bytenr);
  3380. btrfs_print_leaf(extent_root, path->nodes[0]);
  3381. }
  3382. BUG_ON(ret);
  3383. extent_slot = path->slots[0];
  3384. leaf = path->nodes[0];
  3385. item_size = btrfs_item_size_nr(leaf, extent_slot);
  3386. }
  3387. #endif
  3388. BUG_ON(item_size < sizeof(*ei));
  3389. ei = btrfs_item_ptr(leaf, extent_slot,
  3390. struct btrfs_extent_item);
  3391. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  3392. struct btrfs_tree_block_info *bi;
  3393. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  3394. bi = (struct btrfs_tree_block_info *)(ei + 1);
  3395. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  3396. }
  3397. refs = btrfs_extent_refs(leaf, ei);
  3398. BUG_ON(refs < refs_to_drop);
  3399. refs -= refs_to_drop;
  3400. if (refs > 0) {
  3401. if (extent_op)
  3402. __run_delayed_extent_op(extent_op, leaf, ei);
  3403. /*
  3404. * In the case of inline back ref, reference count will
  3405. * be updated by remove_extent_backref
  3406. */
  3407. if (iref) {
  3408. BUG_ON(!found_extent);
  3409. } else {
  3410. btrfs_set_extent_refs(leaf, ei, refs);
  3411. btrfs_mark_buffer_dirty(leaf);
  3412. }
  3413. if (found_extent) {
  3414. ret = remove_extent_backref(trans, extent_root, path,
  3415. iref, refs_to_drop,
  3416. is_data);
  3417. BUG_ON(ret);
  3418. }
  3419. } else {
  3420. int mark_free = 0;
  3421. struct extent_buffer *must_clean = NULL;
  3422. if (found_extent) {
  3423. BUG_ON(is_data && refs_to_drop !=
  3424. extent_data_ref_count(root, path, iref));
  3425. if (iref) {
  3426. BUG_ON(path->slots[0] != extent_slot);
  3427. } else {
  3428. BUG_ON(path->slots[0] != extent_slot + 1);
  3429. path->slots[0] = extent_slot;
  3430. num_to_del = 2;
  3431. }
  3432. }
  3433. ret = pin_down_bytes(trans, root, path, bytenr,
  3434. num_bytes, is_data, 0, &must_clean);
  3435. if (ret > 0)
  3436. mark_free = 1;
  3437. BUG_ON(ret < 0);
  3438. /*
  3439. * it is going to be very rare for someone to be waiting
  3440. * on the block we're freeing. del_items might need to
  3441. * schedule, so rather than get fancy, just force it
  3442. * to blocking here
  3443. */
  3444. if (must_clean)
  3445. btrfs_set_lock_blocking(must_clean);
  3446. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  3447. num_to_del);
  3448. BUG_ON(ret);
  3449. btrfs_release_path(extent_root, path);
  3450. if (must_clean) {
  3451. clean_tree_block(NULL, root, must_clean);
  3452. btrfs_tree_unlock(must_clean);
  3453. free_extent_buffer(must_clean);
  3454. }
  3455. if (is_data) {
  3456. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  3457. BUG_ON(ret);
  3458. } else {
  3459. invalidate_mapping_pages(info->btree_inode->i_mapping,
  3460. bytenr >> PAGE_CACHE_SHIFT,
  3461. (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
  3462. }
  3463. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  3464. mark_free);
  3465. BUG_ON(ret);
  3466. }
  3467. btrfs_free_path(path);
  3468. return ret;
  3469. }
  3470. /*
  3471. * when we free an extent, it is possible (and likely) that we free the last
  3472. * delayed ref for that extent as well. This searches the delayed ref tree for
  3473. * a given extent, and if there are no other delayed refs to be processed, it
  3474. * removes it from the tree.
  3475. */
  3476. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  3477. struct btrfs_root *root, u64 bytenr)
  3478. {
  3479. struct btrfs_delayed_ref_head *head;
  3480. struct btrfs_delayed_ref_root *delayed_refs;
  3481. struct btrfs_delayed_ref_node *ref;
  3482. struct rb_node *node;
  3483. int ret;
  3484. delayed_refs = &trans->transaction->delayed_refs;
  3485. spin_lock(&delayed_refs->lock);
  3486. head = btrfs_find_delayed_ref_head(trans, bytenr);
  3487. if (!head)
  3488. goto out;
  3489. node = rb_prev(&head->node.rb_node);
  3490. if (!node)
  3491. goto out;
  3492. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  3493. /* there are still entries for this ref, we can't drop it */
  3494. if (ref->bytenr == bytenr)
  3495. goto out;
  3496. if (head->extent_op) {
  3497. if (!head->must_insert_reserved)
  3498. goto out;
  3499. kfree(head->extent_op);
  3500. head->extent_op = NULL;
  3501. }
  3502. /*
  3503. * waiting for the lock here would deadlock. If someone else has it
  3504. * locked they are already in the process of dropping it anyway
  3505. */
  3506. if (!mutex_trylock(&head->mutex))
  3507. goto out;
  3508. /*
  3509. * at this point we have a head with no other entries. Go
  3510. * ahead and process it.
  3511. */
  3512. head->node.in_tree = 0;
  3513. rb_erase(&head->node.rb_node, &delayed_refs->root);
  3514. delayed_refs->num_entries--;
  3515. /*
  3516. * we don't take a ref on the node because we're removing it from the
  3517. * tree, so we just steal the ref the tree was holding.
  3518. */
  3519. delayed_refs->num_heads--;
  3520. if (list_empty(&head->cluster))
  3521. delayed_refs->num_heads_ready--;
  3522. list_del_init(&head->cluster);
  3523. spin_unlock(&delayed_refs->lock);
  3524. ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
  3525. &head->node, head->extent_op,
  3526. head->must_insert_reserved);
  3527. BUG_ON(ret);
  3528. btrfs_put_delayed_ref(&head->node);
  3529. return 0;
  3530. out:
  3531. spin_unlock(&delayed_refs->lock);
  3532. return 0;
  3533. }
  3534. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  3535. struct btrfs_root *root,
  3536. u64 bytenr, u64 num_bytes, u64 parent,
  3537. u64 root_objectid, u64 owner, u64 offset)
  3538. {
  3539. int ret;
  3540. /*
  3541. * tree log blocks never actually go into the extent allocation
  3542. * tree, just update pinning info and exit early.
  3543. */
  3544. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  3545. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  3546. /* unlocks the pinned mutex */
  3547. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  3548. ret = 0;
  3549. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  3550. ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
  3551. parent, root_objectid, (int)owner,
  3552. BTRFS_DROP_DELAYED_REF, NULL);
  3553. BUG_ON(ret);
  3554. ret = check_ref_cleanup(trans, root, bytenr);
  3555. BUG_ON(ret);
  3556. } else {
  3557. ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
  3558. parent, root_objectid, owner,
  3559. offset, BTRFS_DROP_DELAYED_REF, NULL);
  3560. BUG_ON(ret);
  3561. }
  3562. return ret;
  3563. }
  3564. static u64 stripe_align(struct btrfs_root *root, u64 val)
  3565. {
  3566. u64 mask = ((u64)root->stripesize - 1);
  3567. u64 ret = (val + mask) & ~mask;
  3568. return ret;
  3569. }
  3570. /*
  3571. * when we wait for progress in the block group caching, its because
  3572. * our allocation attempt failed at least once. So, we must sleep
  3573. * and let some progress happen before we try again.
  3574. *
  3575. * This function will sleep at least once waiting for new free space to
  3576. * show up, and then it will check the block group free space numbers
  3577. * for our min num_bytes. Another option is to have it go ahead
  3578. * and look in the rbtree for a free extent of a given size, but this
  3579. * is a good start.
  3580. */
  3581. static noinline int
  3582. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  3583. u64 num_bytes)
  3584. {
  3585. struct btrfs_caching_control *caching_ctl;
  3586. DEFINE_WAIT(wait);
  3587. caching_ctl = get_caching_control(cache);
  3588. if (!caching_ctl)
  3589. return 0;
  3590. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  3591. (cache->free_space >= num_bytes));
  3592. put_caching_control(caching_ctl);
  3593. return 0;
  3594. }
  3595. static noinline int
  3596. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  3597. {
  3598. struct btrfs_caching_control *caching_ctl;
  3599. DEFINE_WAIT(wait);
  3600. caching_ctl = get_caching_control(cache);
  3601. if (!caching_ctl)
  3602. return 0;
  3603. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  3604. put_caching_control(caching_ctl);
  3605. return 0;
  3606. }
  3607. enum btrfs_loop_type {
  3608. LOOP_FIND_IDEAL = 0,
  3609. LOOP_CACHING_NOWAIT = 1,
  3610. LOOP_CACHING_WAIT = 2,
  3611. LOOP_ALLOC_CHUNK = 3,
  3612. LOOP_NO_EMPTY_SIZE = 4,
  3613. };
  3614. /*
  3615. * walks the btree of allocated extents and find a hole of a given size.
  3616. * The key ins is changed to record the hole:
  3617. * ins->objectid == block start
  3618. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  3619. * ins->offset == number of blocks
  3620. * Any available blocks before search_start are skipped.
  3621. */
  3622. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  3623. struct btrfs_root *orig_root,
  3624. u64 num_bytes, u64 empty_size,
  3625. u64 search_start, u64 search_end,
  3626. u64 hint_byte, struct btrfs_key *ins,
  3627. u64 exclude_start, u64 exclude_nr,
  3628. int data)
  3629. {
  3630. int ret = 0;
  3631. struct btrfs_root *root = orig_root->fs_info->extent_root;
  3632. struct btrfs_free_cluster *last_ptr = NULL;
  3633. struct btrfs_block_group_cache *block_group = NULL;
  3634. int empty_cluster = 2 * 1024 * 1024;
  3635. int allowed_chunk_alloc = 0;
  3636. int done_chunk_alloc = 0;
  3637. struct btrfs_space_info *space_info;
  3638. int last_ptr_loop = 0;
  3639. int loop = 0;
  3640. bool found_uncached_bg = false;
  3641. bool failed_cluster_refill = false;
  3642. bool failed_alloc = false;
  3643. u64 ideal_cache_percent = 0;
  3644. u64 ideal_cache_offset = 0;
  3645. WARN_ON(num_bytes < root->sectorsize);
  3646. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  3647. ins->objectid = 0;
  3648. ins->offset = 0;
  3649. space_info = __find_space_info(root->fs_info, data);
  3650. if (orig_root->ref_cows || empty_size)
  3651. allowed_chunk_alloc = 1;
  3652. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  3653. last_ptr = &root->fs_info->meta_alloc_cluster;
  3654. if (!btrfs_test_opt(root, SSD))
  3655. empty_cluster = 64 * 1024;
  3656. }
  3657. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
  3658. last_ptr = &root->fs_info->data_alloc_cluster;
  3659. }
  3660. if (last_ptr) {
  3661. spin_lock(&last_ptr->lock);
  3662. if (last_ptr->block_group)
  3663. hint_byte = last_ptr->window_start;
  3664. spin_unlock(&last_ptr->lock);
  3665. }
  3666. search_start = max(search_start, first_logical_byte(root, 0));
  3667. search_start = max(search_start, hint_byte);
  3668. if (!last_ptr)
  3669. empty_cluster = 0;
  3670. if (search_start == hint_byte) {
  3671. ideal_cache:
  3672. block_group = btrfs_lookup_block_group(root->fs_info,
  3673. search_start);
  3674. /*
  3675. * we don't want to use the block group if it doesn't match our
  3676. * allocation bits, or if its not cached.
  3677. *
  3678. * However if we are re-searching with an ideal block group
  3679. * picked out then we don't care that the block group is cached.
  3680. */
  3681. if (block_group && block_group_bits(block_group, data) &&
  3682. (block_group->cached != BTRFS_CACHE_NO ||
  3683. search_start == ideal_cache_offset)) {
  3684. down_read(&space_info->groups_sem);
  3685. if (list_empty(&block_group->list) ||
  3686. block_group->ro) {
  3687. /*
  3688. * someone is removing this block group,
  3689. * we can't jump into the have_block_group
  3690. * target because our list pointers are not
  3691. * valid
  3692. */
  3693. btrfs_put_block_group(block_group);
  3694. up_read(&space_info->groups_sem);
  3695. } else {
  3696. goto have_block_group;
  3697. }
  3698. } else if (block_group) {
  3699. btrfs_put_block_group(block_group);
  3700. }
  3701. }
  3702. search:
  3703. down_read(&space_info->groups_sem);
  3704. list_for_each_entry(block_group, &space_info->block_groups, list) {
  3705. u64 offset;
  3706. int cached;
  3707. atomic_inc(&block_group->count);
  3708. search_start = block_group->key.objectid;
  3709. have_block_group:
  3710. if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
  3711. u64 free_percent;
  3712. free_percent = btrfs_block_group_used(&block_group->item);
  3713. free_percent *= 100;
  3714. free_percent = div64_u64(free_percent,
  3715. block_group->key.offset);
  3716. free_percent = 100 - free_percent;
  3717. if (free_percent > ideal_cache_percent &&
  3718. likely(!block_group->ro)) {
  3719. ideal_cache_offset = block_group->key.objectid;
  3720. ideal_cache_percent = free_percent;
  3721. }
  3722. /*
  3723. * We only want to start kthread caching if we are at
  3724. * the point where we will wait for caching to make
  3725. * progress, or if our ideal search is over and we've
  3726. * found somebody to start caching.
  3727. */
  3728. if (loop > LOOP_CACHING_NOWAIT ||
  3729. (loop > LOOP_FIND_IDEAL &&
  3730. atomic_read(&space_info->caching_threads) < 2)) {
  3731. ret = cache_block_group(block_group);
  3732. BUG_ON(ret);
  3733. }
  3734. found_uncached_bg = true;
  3735. /*
  3736. * If loop is set for cached only, try the next block
  3737. * group.
  3738. */
  3739. if (loop == LOOP_FIND_IDEAL)
  3740. goto loop;
  3741. }
  3742. cached = block_group_cache_done(block_group);
  3743. if (unlikely(!cached))
  3744. found_uncached_bg = true;
  3745. if (unlikely(block_group->ro))
  3746. goto loop;
  3747. /*
  3748. * Ok we want to try and use the cluster allocator, so lets look
  3749. * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
  3750. * have tried the cluster allocator plenty of times at this
  3751. * point and not have found anything, so we are likely way too
  3752. * fragmented for the clustering stuff to find anything, so lets
  3753. * just skip it and let the allocator find whatever block it can
  3754. * find
  3755. */
  3756. if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
  3757. /*
  3758. * the refill lock keeps out other
  3759. * people trying to start a new cluster
  3760. */
  3761. spin_lock(&last_ptr->refill_lock);
  3762. if (last_ptr->block_group &&
  3763. (last_ptr->block_group->ro ||
  3764. !block_group_bits(last_ptr->block_group, data))) {
  3765. offset = 0;
  3766. goto refill_cluster;
  3767. }
  3768. offset = btrfs_alloc_from_cluster(block_group, last_ptr,
  3769. num_bytes, search_start);
  3770. if (offset) {
  3771. /* we have a block, we're done */
  3772. spin_unlock(&last_ptr->refill_lock);
  3773. goto checks;
  3774. }
  3775. spin_lock(&last_ptr->lock);
  3776. /*
  3777. * whoops, this cluster doesn't actually point to
  3778. * this block group. Get a ref on the block
  3779. * group is does point to and try again
  3780. */
  3781. if (!last_ptr_loop && last_ptr->block_group &&
  3782. last_ptr->block_group != block_group) {
  3783. btrfs_put_block_group(block_group);
  3784. block_group = last_ptr->block_group;
  3785. atomic_inc(&block_group->count);
  3786. spin_unlock(&last_ptr->lock);
  3787. spin_unlock(&last_ptr->refill_lock);
  3788. last_ptr_loop = 1;
  3789. search_start = block_group->key.objectid;
  3790. /*
  3791. * we know this block group is properly
  3792. * in the list because
  3793. * btrfs_remove_block_group, drops the
  3794. * cluster before it removes the block
  3795. * group from the list
  3796. */
  3797. goto have_block_group;
  3798. }
  3799. spin_unlock(&last_ptr->lock);
  3800. refill_cluster:
  3801. /*
  3802. * this cluster didn't work out, free it and
  3803. * start over
  3804. */
  3805. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  3806. last_ptr_loop = 0;
  3807. /* allocate a cluster in this block group */
  3808. ret = btrfs_find_space_cluster(trans, root,
  3809. block_group, last_ptr,
  3810. offset, num_bytes,
  3811. empty_cluster + empty_size);
  3812. if (ret == 0) {
  3813. /*
  3814. * now pull our allocation out of this
  3815. * cluster
  3816. */
  3817. offset = btrfs_alloc_from_cluster(block_group,
  3818. last_ptr, num_bytes,
  3819. search_start);
  3820. if (offset) {
  3821. /* we found one, proceed */
  3822. spin_unlock(&last_ptr->refill_lock);
  3823. goto checks;
  3824. }
  3825. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  3826. && !failed_cluster_refill) {
  3827. spin_unlock(&last_ptr->refill_lock);
  3828. failed_cluster_refill = true;
  3829. wait_block_group_cache_progress(block_group,
  3830. num_bytes + empty_cluster + empty_size);
  3831. goto have_block_group;
  3832. }
  3833. /*
  3834. * at this point we either didn't find a cluster
  3835. * or we weren't able to allocate a block from our
  3836. * cluster. Free the cluster we've been trying
  3837. * to use, and go to the next block group
  3838. */
  3839. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  3840. spin_unlock(&last_ptr->refill_lock);
  3841. goto loop;
  3842. }
  3843. offset = btrfs_find_space_for_alloc(block_group, search_start,
  3844. num_bytes, empty_size);
  3845. /*
  3846. * If we didn't find a chunk, and we haven't failed on this
  3847. * block group before, and this block group is in the middle of
  3848. * caching and we are ok with waiting, then go ahead and wait
  3849. * for progress to be made, and set failed_alloc to true.
  3850. *
  3851. * If failed_alloc is true then we've already waited on this
  3852. * block group once and should move on to the next block group.
  3853. */
  3854. if (!offset && !failed_alloc && !cached &&
  3855. loop > LOOP_CACHING_NOWAIT) {
  3856. wait_block_group_cache_progress(block_group,
  3857. num_bytes + empty_size);
  3858. failed_alloc = true;
  3859. goto have_block_group;
  3860. } else if (!offset) {
  3861. goto loop;
  3862. }
  3863. checks:
  3864. search_start = stripe_align(root, offset);
  3865. /* move on to the next group */
  3866. if (search_start + num_bytes >= search_end) {
  3867. btrfs_add_free_space(block_group, offset, num_bytes);
  3868. goto loop;
  3869. }
  3870. /* move on to the next group */
  3871. if (search_start + num_bytes >
  3872. block_group->key.objectid + block_group->key.offset) {
  3873. btrfs_add_free_space(block_group, offset, num_bytes);
  3874. goto loop;
  3875. }
  3876. if (exclude_nr > 0 &&
  3877. (search_start + num_bytes > exclude_start &&
  3878. search_start < exclude_start + exclude_nr)) {
  3879. search_start = exclude_start + exclude_nr;
  3880. btrfs_add_free_space(block_group, offset, num_bytes);
  3881. /*
  3882. * if search_start is still in this block group
  3883. * then we just re-search this block group
  3884. */
  3885. if (search_start >= block_group->key.objectid &&
  3886. search_start < (block_group->key.objectid +
  3887. block_group->key.offset))
  3888. goto have_block_group;
  3889. goto loop;
  3890. }
  3891. ins->objectid = search_start;
  3892. ins->offset = num_bytes;
  3893. if (offset < search_start)
  3894. btrfs_add_free_space(block_group, offset,
  3895. search_start - offset);
  3896. BUG_ON(offset > search_start);
  3897. update_reserved_extents(block_group, num_bytes, 1);
  3898. /* we are all good, lets return */
  3899. break;
  3900. loop:
  3901. failed_cluster_refill = false;
  3902. failed_alloc = false;
  3903. btrfs_put_block_group(block_group);
  3904. }
  3905. up_read(&space_info->groups_sem);
  3906. /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
  3907. * for them to make caching progress. Also
  3908. * determine the best possible bg to cache
  3909. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  3910. * caching kthreads as we move along
  3911. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  3912. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  3913. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  3914. * again
  3915. */
  3916. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
  3917. (found_uncached_bg || empty_size || empty_cluster ||
  3918. allowed_chunk_alloc)) {
  3919. if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
  3920. found_uncached_bg = false;
  3921. loop++;
  3922. if (!ideal_cache_percent &&
  3923. atomic_read(&space_info->caching_threads))
  3924. goto search;
  3925. /*
  3926. * 1 of the following 2 things have happened so far
  3927. *
  3928. * 1) We found an ideal block group for caching that
  3929. * is mostly full and will cache quickly, so we might
  3930. * as well wait for it.
  3931. *
  3932. * 2) We searched for cached only and we didn't find
  3933. * anything, and we didn't start any caching kthreads
  3934. * either, so chances are we will loop through and
  3935. * start a couple caching kthreads, and then come back
  3936. * around and just wait for them. This will be slower
  3937. * because we will have 2 caching kthreads reading at
  3938. * the same time when we could have just started one
  3939. * and waited for it to get far enough to give us an
  3940. * allocation, so go ahead and go to the wait caching
  3941. * loop.
  3942. */
  3943. loop = LOOP_CACHING_WAIT;
  3944. search_start = ideal_cache_offset;
  3945. ideal_cache_percent = 0;
  3946. goto ideal_cache;
  3947. } else if (loop == LOOP_FIND_IDEAL) {
  3948. /*
  3949. * Didn't find a uncached bg, wait on anything we find
  3950. * next.
  3951. */
  3952. loop = LOOP_CACHING_WAIT;
  3953. goto search;
  3954. }
  3955. if (loop < LOOP_CACHING_WAIT) {
  3956. loop++;
  3957. goto search;
  3958. }
  3959. if (loop == LOOP_ALLOC_CHUNK) {
  3960. empty_size = 0;
  3961. empty_cluster = 0;
  3962. }
  3963. if (allowed_chunk_alloc) {
  3964. ret = do_chunk_alloc(trans, root, num_bytes +
  3965. 2 * 1024 * 1024, data, 1);
  3966. allowed_chunk_alloc = 0;
  3967. done_chunk_alloc = 1;
  3968. } else if (!done_chunk_alloc) {
  3969. space_info->force_alloc = 1;
  3970. }
  3971. if (loop < LOOP_NO_EMPTY_SIZE) {
  3972. loop++;
  3973. goto search;
  3974. }
  3975. ret = -ENOSPC;
  3976. } else if (!ins->objectid) {
  3977. ret = -ENOSPC;
  3978. }
  3979. /* we found what we needed */
  3980. if (ins->objectid) {
  3981. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  3982. trans->block_group = block_group->key.objectid;
  3983. btrfs_put_block_group(block_group);
  3984. ret = 0;
  3985. }
  3986. return ret;
  3987. }
  3988. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  3989. int dump_block_groups)
  3990. {
  3991. struct btrfs_block_group_cache *cache;
  3992. spin_lock(&info->lock);
  3993. printk(KERN_INFO "space_info has %llu free, is %sfull\n",
  3994. (unsigned long long)(info->total_bytes - info->bytes_used -
  3995. info->bytes_pinned - info->bytes_reserved -
  3996. info->bytes_super),
  3997. (info->full) ? "" : "not ");
  3998. printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
  3999. " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
  4000. "\n",
  4001. (unsigned long long)info->total_bytes,
  4002. (unsigned long long)info->bytes_pinned,
  4003. (unsigned long long)info->bytes_delalloc,
  4004. (unsigned long long)info->bytes_may_use,
  4005. (unsigned long long)info->bytes_used,
  4006. (unsigned long long)info->bytes_root,
  4007. (unsigned long long)info->bytes_super,
  4008. (unsigned long long)info->bytes_reserved);
  4009. spin_unlock(&info->lock);
  4010. if (!dump_block_groups)
  4011. return;
  4012. down_read(&info->groups_sem);
  4013. list_for_each_entry(cache, &info->block_groups, list) {
  4014. spin_lock(&cache->lock);
  4015. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  4016. "%llu pinned %llu reserved\n",
  4017. (unsigned long long)cache->key.objectid,
  4018. (unsigned long long)cache->key.offset,
  4019. (unsigned long long)btrfs_block_group_used(&cache->item),
  4020. (unsigned long long)cache->pinned,
  4021. (unsigned long long)cache->reserved);
  4022. btrfs_dump_free_space(cache, bytes);
  4023. spin_unlock(&cache->lock);
  4024. }
  4025. up_read(&info->groups_sem);
  4026. }
  4027. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  4028. struct btrfs_root *root,
  4029. u64 num_bytes, u64 min_alloc_size,
  4030. u64 empty_size, u64 hint_byte,
  4031. u64 search_end, struct btrfs_key *ins,
  4032. u64 data)
  4033. {
  4034. int ret;
  4035. u64 search_start = 0;
  4036. struct btrfs_fs_info *info = root->fs_info;
  4037. data = btrfs_get_alloc_profile(root, data);
  4038. again:
  4039. /*
  4040. * the only place that sets empty_size is btrfs_realloc_node, which
  4041. * is not called recursively on allocations
  4042. */
  4043. if (empty_size || root->ref_cows) {
  4044. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  4045. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  4046. 2 * 1024 * 1024,
  4047. BTRFS_BLOCK_GROUP_METADATA |
  4048. (info->metadata_alloc_profile &
  4049. info->avail_metadata_alloc_bits), 0);
  4050. }
  4051. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  4052. num_bytes + 2 * 1024 * 1024, data, 0);
  4053. }
  4054. WARN_ON(num_bytes < root->sectorsize);
  4055. ret = find_free_extent(trans, root, num_bytes, empty_size,
  4056. search_start, search_end, hint_byte, ins,
  4057. trans->alloc_exclude_start,
  4058. trans->alloc_exclude_nr, data);
  4059. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  4060. num_bytes = num_bytes >> 1;
  4061. num_bytes = num_bytes & ~(root->sectorsize - 1);
  4062. num_bytes = max(num_bytes, min_alloc_size);
  4063. do_chunk_alloc(trans, root->fs_info->extent_root,
  4064. num_bytes, data, 1);
  4065. goto again;
  4066. }
  4067. if (ret == -ENOSPC) {
  4068. struct btrfs_space_info *sinfo;
  4069. sinfo = __find_space_info(root->fs_info, data);
  4070. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  4071. "wanted %llu\n", (unsigned long long)data,
  4072. (unsigned long long)num_bytes);
  4073. dump_space_info(sinfo, num_bytes, 1);
  4074. }
  4075. return ret;
  4076. }
  4077. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  4078. {
  4079. struct btrfs_block_group_cache *cache;
  4080. int ret = 0;
  4081. cache = btrfs_lookup_block_group(root->fs_info, start);
  4082. if (!cache) {
  4083. printk(KERN_ERR "Unable to find block group for %llu\n",
  4084. (unsigned long long)start);
  4085. return -ENOSPC;
  4086. }
  4087. ret = btrfs_discard_extent(root, start, len);
  4088. btrfs_add_free_space(cache, start, len);
  4089. update_reserved_extents(cache, len, 0);
  4090. btrfs_put_block_group(cache);
  4091. return ret;
  4092. }
  4093. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4094. struct btrfs_root *root,
  4095. u64 parent, u64 root_objectid,
  4096. u64 flags, u64 owner, u64 offset,
  4097. struct btrfs_key *ins, int ref_mod)
  4098. {
  4099. int ret;
  4100. struct btrfs_fs_info *fs_info = root->fs_info;
  4101. struct btrfs_extent_item *extent_item;
  4102. struct btrfs_extent_inline_ref *iref;
  4103. struct btrfs_path *path;
  4104. struct extent_buffer *leaf;
  4105. int type;
  4106. u32 size;
  4107. if (parent > 0)
  4108. type = BTRFS_SHARED_DATA_REF_KEY;
  4109. else
  4110. type = BTRFS_EXTENT_DATA_REF_KEY;
  4111. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  4112. path = btrfs_alloc_path();
  4113. BUG_ON(!path);
  4114. path->leave_spinning = 1;
  4115. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  4116. ins, size);
  4117. BUG_ON(ret);
  4118. leaf = path->nodes[0];
  4119. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4120. struct btrfs_extent_item);
  4121. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  4122. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4123. btrfs_set_extent_flags(leaf, extent_item,
  4124. flags | BTRFS_EXTENT_FLAG_DATA);
  4125. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  4126. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  4127. if (parent > 0) {
  4128. struct btrfs_shared_data_ref *ref;
  4129. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  4130. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  4131. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  4132. } else {
  4133. struct btrfs_extent_data_ref *ref;
  4134. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  4135. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  4136. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  4137. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  4138. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  4139. }
  4140. btrfs_mark_buffer_dirty(path->nodes[0]);
  4141. btrfs_free_path(path);
  4142. ret = update_block_group(trans, root, ins->objectid, ins->offset,
  4143. 1, 0);
  4144. if (ret) {
  4145. printk(KERN_ERR "btrfs update block group failed for %llu "
  4146. "%llu\n", (unsigned long long)ins->objectid,
  4147. (unsigned long long)ins->offset);
  4148. BUG();
  4149. }
  4150. return ret;
  4151. }
  4152. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  4153. struct btrfs_root *root,
  4154. u64 parent, u64 root_objectid,
  4155. u64 flags, struct btrfs_disk_key *key,
  4156. int level, struct btrfs_key *ins)
  4157. {
  4158. int ret;
  4159. struct btrfs_fs_info *fs_info = root->fs_info;
  4160. struct btrfs_extent_item *extent_item;
  4161. struct btrfs_tree_block_info *block_info;
  4162. struct btrfs_extent_inline_ref *iref;
  4163. struct btrfs_path *path;
  4164. struct extent_buffer *leaf;
  4165. u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
  4166. path = btrfs_alloc_path();
  4167. BUG_ON(!path);
  4168. path->leave_spinning = 1;
  4169. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  4170. ins, size);
  4171. BUG_ON(ret);
  4172. leaf = path->nodes[0];
  4173. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4174. struct btrfs_extent_item);
  4175. btrfs_set_extent_refs(leaf, extent_item, 1);
  4176. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4177. btrfs_set_extent_flags(leaf, extent_item,
  4178. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  4179. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  4180. btrfs_set_tree_block_key(leaf, block_info, key);
  4181. btrfs_set_tree_block_level(leaf, block_info, level);
  4182. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  4183. if (parent > 0) {
  4184. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  4185. btrfs_set_extent_inline_ref_type(leaf, iref,
  4186. BTRFS_SHARED_BLOCK_REF_KEY);
  4187. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  4188. } else {
  4189. btrfs_set_extent_inline_ref_type(leaf, iref,
  4190. BTRFS_TREE_BLOCK_REF_KEY);
  4191. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  4192. }
  4193. btrfs_mark_buffer_dirty(leaf);
  4194. btrfs_free_path(path);
  4195. ret = update_block_group(trans, root, ins->objectid, ins->offset,
  4196. 1, 0);
  4197. if (ret) {
  4198. printk(KERN_ERR "btrfs update block group failed for %llu "
  4199. "%llu\n", (unsigned long long)ins->objectid,
  4200. (unsigned long long)ins->offset);
  4201. BUG();
  4202. }
  4203. return ret;
  4204. }
  4205. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4206. struct btrfs_root *root,
  4207. u64 root_objectid, u64 owner,
  4208. u64 offset, struct btrfs_key *ins)
  4209. {
  4210. int ret;
  4211. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  4212. ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
  4213. 0, root_objectid, owner, offset,
  4214. BTRFS_ADD_DELAYED_EXTENT, NULL);
  4215. return ret;
  4216. }
  4217. /*
  4218. * this is used by the tree logging recovery code. It records that
  4219. * an extent has been allocated and makes sure to clear the free
  4220. * space cache bits as well
  4221. */
  4222. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  4223. struct btrfs_root *root,
  4224. u64 root_objectid, u64 owner, u64 offset,
  4225. struct btrfs_key *ins)
  4226. {
  4227. int ret;
  4228. struct btrfs_block_group_cache *block_group;
  4229. struct btrfs_caching_control *caching_ctl;
  4230. u64 start = ins->objectid;
  4231. u64 num_bytes = ins->offset;
  4232. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  4233. cache_block_group(block_group);
  4234. caching_ctl = get_caching_control(block_group);
  4235. if (!caching_ctl) {
  4236. BUG_ON(!block_group_cache_done(block_group));
  4237. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4238. BUG_ON(ret);
  4239. } else {
  4240. mutex_lock(&caching_ctl->mutex);
  4241. if (start >= caching_ctl->progress) {
  4242. ret = add_excluded_extent(root, start, num_bytes);
  4243. BUG_ON(ret);
  4244. } else if (start + num_bytes <= caching_ctl->progress) {
  4245. ret = btrfs_remove_free_space(block_group,
  4246. start, num_bytes);
  4247. BUG_ON(ret);
  4248. } else {
  4249. num_bytes = caching_ctl->progress - start;
  4250. ret = btrfs_remove_free_space(block_group,
  4251. start, num_bytes);
  4252. BUG_ON(ret);
  4253. start = caching_ctl->progress;
  4254. num_bytes = ins->objectid + ins->offset -
  4255. caching_ctl->progress;
  4256. ret = add_excluded_extent(root, start, num_bytes);
  4257. BUG_ON(ret);
  4258. }
  4259. mutex_unlock(&caching_ctl->mutex);
  4260. put_caching_control(caching_ctl);
  4261. }
  4262. update_reserved_extents(block_group, ins->offset, 1);
  4263. btrfs_put_block_group(block_group);
  4264. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  4265. 0, owner, offset, ins, 1);
  4266. return ret;
  4267. }
  4268. /*
  4269. * finds a free extent and does all the dirty work required for allocation
  4270. * returns the key for the extent through ins, and a tree buffer for
  4271. * the first block of the extent through buf.
  4272. *
  4273. * returns 0 if everything worked, non-zero otherwise.
  4274. */
  4275. static int alloc_tree_block(struct btrfs_trans_handle *trans,
  4276. struct btrfs_root *root,
  4277. u64 num_bytes, u64 parent, u64 root_objectid,
  4278. struct btrfs_disk_key *key, int level,
  4279. u64 empty_size, u64 hint_byte, u64 search_end,
  4280. struct btrfs_key *ins)
  4281. {
  4282. int ret;
  4283. u64 flags = 0;
  4284. ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
  4285. empty_size, hint_byte, search_end,
  4286. ins, 0);
  4287. if (ret)
  4288. return ret;
  4289. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  4290. if (parent == 0)
  4291. parent = ins->objectid;
  4292. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4293. } else
  4294. BUG_ON(parent > 0);
  4295. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  4296. struct btrfs_delayed_extent_op *extent_op;
  4297. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  4298. BUG_ON(!extent_op);
  4299. if (key)
  4300. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  4301. else
  4302. memset(&extent_op->key, 0, sizeof(extent_op->key));
  4303. extent_op->flags_to_set = flags;
  4304. extent_op->update_key = 1;
  4305. extent_op->update_flags = 1;
  4306. extent_op->is_data = 0;
  4307. ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
  4308. ins->offset, parent, root_objectid,
  4309. level, BTRFS_ADD_DELAYED_EXTENT,
  4310. extent_op);
  4311. BUG_ON(ret);
  4312. }
  4313. return ret;
  4314. }
  4315. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  4316. struct btrfs_root *root,
  4317. u64 bytenr, u32 blocksize,
  4318. int level)
  4319. {
  4320. struct extent_buffer *buf;
  4321. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  4322. if (!buf)
  4323. return ERR_PTR(-ENOMEM);
  4324. btrfs_set_header_generation(buf, trans->transid);
  4325. btrfs_set_buffer_lockdep_class(buf, level);
  4326. btrfs_tree_lock(buf);
  4327. clean_tree_block(trans, root, buf);
  4328. btrfs_set_lock_blocking(buf);
  4329. btrfs_set_buffer_uptodate(buf);
  4330. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  4331. set_extent_dirty(&root->dirty_log_pages, buf->start,
  4332. buf->start + buf->len - 1, GFP_NOFS);
  4333. } else {
  4334. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  4335. buf->start + buf->len - 1, GFP_NOFS);
  4336. }
  4337. trans->blocks_used++;
  4338. /* this returns a buffer locked for blocking */
  4339. return buf;
  4340. }
  4341. /*
  4342. * helper function to allocate a block for a given tree
  4343. * returns the tree buffer or NULL.
  4344. */
  4345. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  4346. struct btrfs_root *root, u32 blocksize,
  4347. u64 parent, u64 root_objectid,
  4348. struct btrfs_disk_key *key, int level,
  4349. u64 hint, u64 empty_size)
  4350. {
  4351. struct btrfs_key ins;
  4352. int ret;
  4353. struct extent_buffer *buf;
  4354. ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
  4355. key, level, empty_size, hint, (u64)-1, &ins);
  4356. if (ret) {
  4357. BUG_ON(ret > 0);
  4358. return ERR_PTR(ret);
  4359. }
  4360. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  4361. blocksize, level);
  4362. return buf;
  4363. }
  4364. struct walk_control {
  4365. u64 refs[BTRFS_MAX_LEVEL];
  4366. u64 flags[BTRFS_MAX_LEVEL];
  4367. struct btrfs_key update_progress;
  4368. int stage;
  4369. int level;
  4370. int shared_level;
  4371. int update_ref;
  4372. int keep_locks;
  4373. int reada_slot;
  4374. int reada_count;
  4375. };
  4376. #define DROP_REFERENCE 1
  4377. #define UPDATE_BACKREF 2
  4378. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  4379. struct btrfs_root *root,
  4380. struct walk_control *wc,
  4381. struct btrfs_path *path)
  4382. {
  4383. u64 bytenr;
  4384. u64 generation;
  4385. u64 refs;
  4386. u64 flags;
  4387. u64 last = 0;
  4388. u32 nritems;
  4389. u32 blocksize;
  4390. struct btrfs_key key;
  4391. struct extent_buffer *eb;
  4392. int ret;
  4393. int slot;
  4394. int nread = 0;
  4395. if (path->slots[wc->level] < wc->reada_slot) {
  4396. wc->reada_count = wc->reada_count * 2 / 3;
  4397. wc->reada_count = max(wc->reada_count, 2);
  4398. } else {
  4399. wc->reada_count = wc->reada_count * 3 / 2;
  4400. wc->reada_count = min_t(int, wc->reada_count,
  4401. BTRFS_NODEPTRS_PER_BLOCK(root));
  4402. }
  4403. eb = path->nodes[wc->level];
  4404. nritems = btrfs_header_nritems(eb);
  4405. blocksize = btrfs_level_size(root, wc->level - 1);
  4406. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  4407. if (nread >= wc->reada_count)
  4408. break;
  4409. cond_resched();
  4410. bytenr = btrfs_node_blockptr(eb, slot);
  4411. generation = btrfs_node_ptr_generation(eb, slot);
  4412. if (slot == path->slots[wc->level])
  4413. goto reada;
  4414. if (wc->stage == UPDATE_BACKREF &&
  4415. generation <= root->root_key.offset)
  4416. continue;
  4417. /* We don't lock the tree block, it's OK to be racy here */
  4418. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  4419. &refs, &flags);
  4420. BUG_ON(ret);
  4421. BUG_ON(refs == 0);
  4422. if (wc->stage == DROP_REFERENCE) {
  4423. if (refs == 1)
  4424. goto reada;
  4425. if (wc->level == 1 &&
  4426. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  4427. continue;
  4428. if (!wc->update_ref ||
  4429. generation <= root->root_key.offset)
  4430. continue;
  4431. btrfs_node_key_to_cpu(eb, &key, slot);
  4432. ret = btrfs_comp_cpu_keys(&key,
  4433. &wc->update_progress);
  4434. if (ret < 0)
  4435. continue;
  4436. } else {
  4437. if (wc->level == 1 &&
  4438. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  4439. continue;
  4440. }
  4441. reada:
  4442. ret = readahead_tree_block(root, bytenr, blocksize,
  4443. generation);
  4444. if (ret)
  4445. break;
  4446. last = bytenr + blocksize;
  4447. nread++;
  4448. }
  4449. wc->reada_slot = slot;
  4450. }
  4451. /*
  4452. * hepler to process tree block while walking down the tree.
  4453. *
  4454. * when wc->stage == UPDATE_BACKREF, this function updates
  4455. * back refs for pointers in the block.
  4456. *
  4457. * NOTE: return value 1 means we should stop walking down.
  4458. */
  4459. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  4460. struct btrfs_root *root,
  4461. struct btrfs_path *path,
  4462. struct walk_control *wc, int lookup_info)
  4463. {
  4464. int level = wc->level;
  4465. struct extent_buffer *eb = path->nodes[level];
  4466. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4467. int ret;
  4468. if (wc->stage == UPDATE_BACKREF &&
  4469. btrfs_header_owner(eb) != root->root_key.objectid)
  4470. return 1;
  4471. /*
  4472. * when reference count of tree block is 1, it won't increase
  4473. * again. once full backref flag is set, we never clear it.
  4474. */
  4475. if (lookup_info &&
  4476. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  4477. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  4478. BUG_ON(!path->locks[level]);
  4479. ret = btrfs_lookup_extent_info(trans, root,
  4480. eb->start, eb->len,
  4481. &wc->refs[level],
  4482. &wc->flags[level]);
  4483. BUG_ON(ret);
  4484. BUG_ON(wc->refs[level] == 0);
  4485. }
  4486. if (wc->stage == DROP_REFERENCE) {
  4487. if (wc->refs[level] > 1)
  4488. return 1;
  4489. if (path->locks[level] && !wc->keep_locks) {
  4490. btrfs_tree_unlock(eb);
  4491. path->locks[level] = 0;
  4492. }
  4493. return 0;
  4494. }
  4495. /* wc->stage == UPDATE_BACKREF */
  4496. if (!(wc->flags[level] & flag)) {
  4497. BUG_ON(!path->locks[level]);
  4498. ret = btrfs_inc_ref(trans, root, eb, 1);
  4499. BUG_ON(ret);
  4500. ret = btrfs_dec_ref(trans, root, eb, 0);
  4501. BUG_ON(ret);
  4502. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  4503. eb->len, flag, 0);
  4504. BUG_ON(ret);
  4505. wc->flags[level] |= flag;
  4506. }
  4507. /*
  4508. * the block is shared by multiple trees, so it's not good to
  4509. * keep the tree lock
  4510. */
  4511. if (path->locks[level] && level > 0) {
  4512. btrfs_tree_unlock(eb);
  4513. path->locks[level] = 0;
  4514. }
  4515. return 0;
  4516. }
  4517. /*
  4518. * hepler to process tree block pointer.
  4519. *
  4520. * when wc->stage == DROP_REFERENCE, this function checks
  4521. * reference count of the block pointed to. if the block
  4522. * is shared and we need update back refs for the subtree
  4523. * rooted at the block, this function changes wc->stage to
  4524. * UPDATE_BACKREF. if the block is shared and there is no
  4525. * need to update back, this function drops the reference
  4526. * to the block.
  4527. *
  4528. * NOTE: return value 1 means we should stop walking down.
  4529. */
  4530. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  4531. struct btrfs_root *root,
  4532. struct btrfs_path *path,
  4533. struct walk_control *wc, int *lookup_info)
  4534. {
  4535. u64 bytenr;
  4536. u64 generation;
  4537. u64 parent;
  4538. u32 blocksize;
  4539. struct btrfs_key key;
  4540. struct extent_buffer *next;
  4541. int level = wc->level;
  4542. int reada = 0;
  4543. int ret = 0;
  4544. generation = btrfs_node_ptr_generation(path->nodes[level],
  4545. path->slots[level]);
  4546. /*
  4547. * if the lower level block was created before the snapshot
  4548. * was created, we know there is no need to update back refs
  4549. * for the subtree
  4550. */
  4551. if (wc->stage == UPDATE_BACKREF &&
  4552. generation <= root->root_key.offset) {
  4553. *lookup_info = 1;
  4554. return 1;
  4555. }
  4556. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  4557. blocksize = btrfs_level_size(root, level - 1);
  4558. next = btrfs_find_tree_block(root, bytenr, blocksize);
  4559. if (!next) {
  4560. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  4561. reada = 1;
  4562. }
  4563. btrfs_tree_lock(next);
  4564. btrfs_set_lock_blocking(next);
  4565. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  4566. &wc->refs[level - 1],
  4567. &wc->flags[level - 1]);
  4568. BUG_ON(ret);
  4569. BUG_ON(wc->refs[level - 1] == 0);
  4570. *lookup_info = 0;
  4571. if (wc->stage == DROP_REFERENCE) {
  4572. if (wc->refs[level - 1] > 1) {
  4573. if (level == 1 &&
  4574. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  4575. goto skip;
  4576. if (!wc->update_ref ||
  4577. generation <= root->root_key.offset)
  4578. goto skip;
  4579. btrfs_node_key_to_cpu(path->nodes[level], &key,
  4580. path->slots[level]);
  4581. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  4582. if (ret < 0)
  4583. goto skip;
  4584. wc->stage = UPDATE_BACKREF;
  4585. wc->shared_level = level - 1;
  4586. }
  4587. } else {
  4588. if (level == 1 &&
  4589. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  4590. goto skip;
  4591. }
  4592. if (!btrfs_buffer_uptodate(next, generation)) {
  4593. btrfs_tree_unlock(next);
  4594. free_extent_buffer(next);
  4595. next = NULL;
  4596. *lookup_info = 1;
  4597. }
  4598. if (!next) {
  4599. if (reada && level == 1)
  4600. reada_walk_down(trans, root, wc, path);
  4601. next = read_tree_block(root, bytenr, blocksize, generation);
  4602. btrfs_tree_lock(next);
  4603. btrfs_set_lock_blocking(next);
  4604. }
  4605. level--;
  4606. BUG_ON(level != btrfs_header_level(next));
  4607. path->nodes[level] = next;
  4608. path->slots[level] = 0;
  4609. path->locks[level] = 1;
  4610. wc->level = level;
  4611. if (wc->level == 1)
  4612. wc->reada_slot = 0;
  4613. return 0;
  4614. skip:
  4615. wc->refs[level - 1] = 0;
  4616. wc->flags[level - 1] = 0;
  4617. if (wc->stage == DROP_REFERENCE) {
  4618. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  4619. parent = path->nodes[level]->start;
  4620. } else {
  4621. BUG_ON(root->root_key.objectid !=
  4622. btrfs_header_owner(path->nodes[level]));
  4623. parent = 0;
  4624. }
  4625. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  4626. root->root_key.objectid, level - 1, 0);
  4627. BUG_ON(ret);
  4628. }
  4629. btrfs_tree_unlock(next);
  4630. free_extent_buffer(next);
  4631. *lookup_info = 1;
  4632. return 1;
  4633. }
  4634. /*
  4635. * hepler to process tree block while walking up the tree.
  4636. *
  4637. * when wc->stage == DROP_REFERENCE, this function drops
  4638. * reference count on the block.
  4639. *
  4640. * when wc->stage == UPDATE_BACKREF, this function changes
  4641. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  4642. * to UPDATE_BACKREF previously while processing the block.
  4643. *
  4644. * NOTE: return value 1 means we should stop walking up.
  4645. */
  4646. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  4647. struct btrfs_root *root,
  4648. struct btrfs_path *path,
  4649. struct walk_control *wc)
  4650. {
  4651. int ret = 0;
  4652. int level = wc->level;
  4653. struct extent_buffer *eb = path->nodes[level];
  4654. u64 parent = 0;
  4655. if (wc->stage == UPDATE_BACKREF) {
  4656. BUG_ON(wc->shared_level < level);
  4657. if (level < wc->shared_level)
  4658. goto out;
  4659. ret = find_next_key(path, level + 1, &wc->update_progress);
  4660. if (ret > 0)
  4661. wc->update_ref = 0;
  4662. wc->stage = DROP_REFERENCE;
  4663. wc->shared_level = -1;
  4664. path->slots[level] = 0;
  4665. /*
  4666. * check reference count again if the block isn't locked.
  4667. * we should start walking down the tree again if reference
  4668. * count is one.
  4669. */
  4670. if (!path->locks[level]) {
  4671. BUG_ON(level == 0);
  4672. btrfs_tree_lock(eb);
  4673. btrfs_set_lock_blocking(eb);
  4674. path->locks[level] = 1;
  4675. ret = btrfs_lookup_extent_info(trans, root,
  4676. eb->start, eb->len,
  4677. &wc->refs[level],
  4678. &wc->flags[level]);
  4679. BUG_ON(ret);
  4680. BUG_ON(wc->refs[level] == 0);
  4681. if (wc->refs[level] == 1) {
  4682. btrfs_tree_unlock(eb);
  4683. path->locks[level] = 0;
  4684. return 1;
  4685. }
  4686. }
  4687. }
  4688. /* wc->stage == DROP_REFERENCE */
  4689. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  4690. if (wc->refs[level] == 1) {
  4691. if (level == 0) {
  4692. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4693. ret = btrfs_dec_ref(trans, root, eb, 1);
  4694. else
  4695. ret = btrfs_dec_ref(trans, root, eb, 0);
  4696. BUG_ON(ret);
  4697. }
  4698. /* make block locked assertion in clean_tree_block happy */
  4699. if (!path->locks[level] &&
  4700. btrfs_header_generation(eb) == trans->transid) {
  4701. btrfs_tree_lock(eb);
  4702. btrfs_set_lock_blocking(eb);
  4703. path->locks[level] = 1;
  4704. }
  4705. clean_tree_block(trans, root, eb);
  4706. }
  4707. if (eb == root->node) {
  4708. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4709. parent = eb->start;
  4710. else
  4711. BUG_ON(root->root_key.objectid !=
  4712. btrfs_header_owner(eb));
  4713. } else {
  4714. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4715. parent = path->nodes[level + 1]->start;
  4716. else
  4717. BUG_ON(root->root_key.objectid !=
  4718. btrfs_header_owner(path->nodes[level + 1]));
  4719. }
  4720. ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
  4721. root->root_key.objectid, level, 0);
  4722. BUG_ON(ret);
  4723. out:
  4724. wc->refs[level] = 0;
  4725. wc->flags[level] = 0;
  4726. return ret;
  4727. }
  4728. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  4729. struct btrfs_root *root,
  4730. struct btrfs_path *path,
  4731. struct walk_control *wc)
  4732. {
  4733. int level = wc->level;
  4734. int lookup_info = 1;
  4735. int ret;
  4736. while (level >= 0) {
  4737. if (path->slots[level] >=
  4738. btrfs_header_nritems(path->nodes[level]))
  4739. break;
  4740. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  4741. if (ret > 0)
  4742. break;
  4743. if (level == 0)
  4744. break;
  4745. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  4746. if (ret > 0) {
  4747. path->slots[level]++;
  4748. continue;
  4749. }
  4750. level = wc->level;
  4751. }
  4752. return 0;
  4753. }
  4754. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  4755. struct btrfs_root *root,
  4756. struct btrfs_path *path,
  4757. struct walk_control *wc, int max_level)
  4758. {
  4759. int level = wc->level;
  4760. int ret;
  4761. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  4762. while (level < max_level && path->nodes[level]) {
  4763. wc->level = level;
  4764. if (path->slots[level] + 1 <
  4765. btrfs_header_nritems(path->nodes[level])) {
  4766. path->slots[level]++;
  4767. return 0;
  4768. } else {
  4769. ret = walk_up_proc(trans, root, path, wc);
  4770. if (ret > 0)
  4771. return 0;
  4772. if (path->locks[level]) {
  4773. btrfs_tree_unlock(path->nodes[level]);
  4774. path->locks[level] = 0;
  4775. }
  4776. free_extent_buffer(path->nodes[level]);
  4777. path->nodes[level] = NULL;
  4778. level++;
  4779. }
  4780. }
  4781. return 1;
  4782. }
  4783. /*
  4784. * drop a subvolume tree.
  4785. *
  4786. * this function traverses the tree freeing any blocks that only
  4787. * referenced by the tree.
  4788. *
  4789. * when a shared tree block is found. this function decreases its
  4790. * reference count by one. if update_ref is true, this function
  4791. * also make sure backrefs for the shared block and all lower level
  4792. * blocks are properly updated.
  4793. */
  4794. int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
  4795. {
  4796. struct btrfs_path *path;
  4797. struct btrfs_trans_handle *trans;
  4798. struct btrfs_root *tree_root = root->fs_info->tree_root;
  4799. struct btrfs_root_item *root_item = &root->root_item;
  4800. struct walk_control *wc;
  4801. struct btrfs_key key;
  4802. int err = 0;
  4803. int ret;
  4804. int level;
  4805. path = btrfs_alloc_path();
  4806. BUG_ON(!path);
  4807. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  4808. BUG_ON(!wc);
  4809. trans = btrfs_start_transaction(tree_root, 1);
  4810. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  4811. level = btrfs_header_level(root->node);
  4812. path->nodes[level] = btrfs_lock_root_node(root);
  4813. btrfs_set_lock_blocking(path->nodes[level]);
  4814. path->slots[level] = 0;
  4815. path->locks[level] = 1;
  4816. memset(&wc->update_progress, 0,
  4817. sizeof(wc->update_progress));
  4818. } else {
  4819. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  4820. memcpy(&wc->update_progress, &key,
  4821. sizeof(wc->update_progress));
  4822. level = root_item->drop_level;
  4823. BUG_ON(level == 0);
  4824. path->lowest_level = level;
  4825. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4826. path->lowest_level = 0;
  4827. if (ret < 0) {
  4828. err = ret;
  4829. goto out;
  4830. }
  4831. WARN_ON(ret > 0);
  4832. /*
  4833. * unlock our path, this is safe because only this
  4834. * function is allowed to delete this snapshot
  4835. */
  4836. btrfs_unlock_up_safe(path, 0);
  4837. level = btrfs_header_level(root->node);
  4838. while (1) {
  4839. btrfs_tree_lock(path->nodes[level]);
  4840. btrfs_set_lock_blocking(path->nodes[level]);
  4841. ret = btrfs_lookup_extent_info(trans, root,
  4842. path->nodes[level]->start,
  4843. path->nodes[level]->len,
  4844. &wc->refs[level],
  4845. &wc->flags[level]);
  4846. BUG_ON(ret);
  4847. BUG_ON(wc->refs[level] == 0);
  4848. if (level == root_item->drop_level)
  4849. break;
  4850. btrfs_tree_unlock(path->nodes[level]);
  4851. WARN_ON(wc->refs[level] != 1);
  4852. level--;
  4853. }
  4854. }
  4855. wc->level = level;
  4856. wc->shared_level = -1;
  4857. wc->stage = DROP_REFERENCE;
  4858. wc->update_ref = update_ref;
  4859. wc->keep_locks = 0;
  4860. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  4861. while (1) {
  4862. ret = walk_down_tree(trans, root, path, wc);
  4863. if (ret < 0) {
  4864. err = ret;
  4865. break;
  4866. }
  4867. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  4868. if (ret < 0) {
  4869. err = ret;
  4870. break;
  4871. }
  4872. if (ret > 0) {
  4873. BUG_ON(wc->stage != DROP_REFERENCE);
  4874. break;
  4875. }
  4876. if (wc->stage == DROP_REFERENCE) {
  4877. level = wc->level;
  4878. btrfs_node_key(path->nodes[level],
  4879. &root_item->drop_progress,
  4880. path->slots[level]);
  4881. root_item->drop_level = level;
  4882. }
  4883. BUG_ON(wc->level == 0);
  4884. if (trans->transaction->in_commit ||
  4885. trans->transaction->delayed_refs.flushing) {
  4886. ret = btrfs_update_root(trans, tree_root,
  4887. &root->root_key,
  4888. root_item);
  4889. BUG_ON(ret);
  4890. btrfs_end_transaction(trans, tree_root);
  4891. trans = btrfs_start_transaction(tree_root, 1);
  4892. } else {
  4893. unsigned long update;
  4894. update = trans->delayed_ref_updates;
  4895. trans->delayed_ref_updates = 0;
  4896. if (update)
  4897. btrfs_run_delayed_refs(trans, tree_root,
  4898. update);
  4899. }
  4900. }
  4901. btrfs_release_path(root, path);
  4902. BUG_ON(err);
  4903. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  4904. BUG_ON(ret);
  4905. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  4906. ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
  4907. NULL, NULL);
  4908. BUG_ON(ret < 0);
  4909. if (ret > 0) {
  4910. ret = btrfs_del_orphan_item(trans, tree_root,
  4911. root->root_key.objectid);
  4912. BUG_ON(ret);
  4913. }
  4914. }
  4915. if (root->in_radix) {
  4916. btrfs_free_fs_root(tree_root->fs_info, root);
  4917. } else {
  4918. free_extent_buffer(root->node);
  4919. free_extent_buffer(root->commit_root);
  4920. kfree(root);
  4921. }
  4922. out:
  4923. btrfs_end_transaction(trans, tree_root);
  4924. kfree(wc);
  4925. btrfs_free_path(path);
  4926. return err;
  4927. }
  4928. /*
  4929. * drop subtree rooted at tree block 'node'.
  4930. *
  4931. * NOTE: this function will unlock and release tree block 'node'
  4932. */
  4933. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  4934. struct btrfs_root *root,
  4935. struct extent_buffer *node,
  4936. struct extent_buffer *parent)
  4937. {
  4938. struct btrfs_path *path;
  4939. struct walk_control *wc;
  4940. int level;
  4941. int parent_level;
  4942. int ret = 0;
  4943. int wret;
  4944. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  4945. path = btrfs_alloc_path();
  4946. BUG_ON(!path);
  4947. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  4948. BUG_ON(!wc);
  4949. btrfs_assert_tree_locked(parent);
  4950. parent_level = btrfs_header_level(parent);
  4951. extent_buffer_get(parent);
  4952. path->nodes[parent_level] = parent;
  4953. path->slots[parent_level] = btrfs_header_nritems(parent);
  4954. btrfs_assert_tree_locked(node);
  4955. level = btrfs_header_level(node);
  4956. path->nodes[level] = node;
  4957. path->slots[level] = 0;
  4958. path->locks[level] = 1;
  4959. wc->refs[parent_level] = 1;
  4960. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4961. wc->level = level;
  4962. wc->shared_level = -1;
  4963. wc->stage = DROP_REFERENCE;
  4964. wc->update_ref = 0;
  4965. wc->keep_locks = 1;
  4966. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  4967. while (1) {
  4968. wret = walk_down_tree(trans, root, path, wc);
  4969. if (wret < 0) {
  4970. ret = wret;
  4971. break;
  4972. }
  4973. wret = walk_up_tree(trans, root, path, wc, parent_level);
  4974. if (wret < 0)
  4975. ret = wret;
  4976. if (wret != 0)
  4977. break;
  4978. }
  4979. kfree(wc);
  4980. btrfs_free_path(path);
  4981. return ret;
  4982. }
  4983. #if 0
  4984. static unsigned long calc_ra(unsigned long start, unsigned long last,
  4985. unsigned long nr)
  4986. {
  4987. return min(last, start + nr - 1);
  4988. }
  4989. static noinline int relocate_inode_pages(struct inode *inode, u64 start,
  4990. u64 len)
  4991. {
  4992. u64 page_start;
  4993. u64 page_end;
  4994. unsigned long first_index;
  4995. unsigned long last_index;
  4996. unsigned long i;
  4997. struct page *page;
  4998. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4999. struct file_ra_state *ra;
  5000. struct btrfs_ordered_extent *ordered;
  5001. unsigned int total_read = 0;
  5002. unsigned int total_dirty = 0;
  5003. int ret = 0;
  5004. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  5005. mutex_lock(&inode->i_mutex);
  5006. first_index = start >> PAGE_CACHE_SHIFT;
  5007. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  5008. /* make sure the dirty trick played by the caller work */
  5009. ret = invalidate_inode_pages2_range(inode->i_mapping,
  5010. first_index, last_index);
  5011. if (ret)
  5012. goto out_unlock;
  5013. file_ra_state_init(ra, inode->i_mapping);
  5014. for (i = first_index ; i <= last_index; i++) {
  5015. if (total_read % ra->ra_pages == 0) {
  5016. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  5017. calc_ra(i, last_index, ra->ra_pages));
  5018. }
  5019. total_read++;
  5020. again:
  5021. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  5022. BUG_ON(1);
  5023. page = grab_cache_page(inode->i_mapping, i);
  5024. if (!page) {
  5025. ret = -ENOMEM;
  5026. goto out_unlock;
  5027. }
  5028. if (!PageUptodate(page)) {
  5029. btrfs_readpage(NULL, page);
  5030. lock_page(page);
  5031. if (!PageUptodate(page)) {
  5032. unlock_page(page);
  5033. page_cache_release(page);
  5034. ret = -EIO;
  5035. goto out_unlock;
  5036. }
  5037. }
  5038. wait_on_page_writeback(page);
  5039. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  5040. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5041. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5042. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5043. if (ordered) {
  5044. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5045. unlock_page(page);
  5046. page_cache_release(page);
  5047. btrfs_start_ordered_extent(inode, ordered, 1);
  5048. btrfs_put_ordered_extent(ordered);
  5049. goto again;
  5050. }
  5051. set_page_extent_mapped(page);
  5052. if (i == first_index)
  5053. set_extent_bits(io_tree, page_start, page_end,
  5054. EXTENT_BOUNDARY, GFP_NOFS);
  5055. btrfs_set_extent_delalloc(inode, page_start, page_end);
  5056. set_page_dirty(page);
  5057. total_dirty++;
  5058. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5059. unlock_page(page);
  5060. page_cache_release(page);
  5061. }
  5062. out_unlock:
  5063. kfree(ra);
  5064. mutex_unlock(&inode->i_mutex);
  5065. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  5066. return ret;
  5067. }
  5068. static noinline int relocate_data_extent(struct inode *reloc_inode,
  5069. struct btrfs_key *extent_key,
  5070. u64 offset)
  5071. {
  5072. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  5073. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  5074. struct extent_map *em;
  5075. u64 start = extent_key->objectid - offset;
  5076. u64 end = start + extent_key->offset - 1;
  5077. em = alloc_extent_map(GFP_NOFS);
  5078. BUG_ON(!em || IS_ERR(em));
  5079. em->start = start;
  5080. em->len = extent_key->offset;
  5081. em->block_len = extent_key->offset;
  5082. em->block_start = extent_key->objectid;
  5083. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5084. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  5085. /* setup extent map to cheat btrfs_readpage */
  5086. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  5087. while (1) {
  5088. int ret;
  5089. write_lock(&em_tree->lock);
  5090. ret = add_extent_mapping(em_tree, em);
  5091. write_unlock(&em_tree->lock);
  5092. if (ret != -EEXIST) {
  5093. free_extent_map(em);
  5094. break;
  5095. }
  5096. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  5097. }
  5098. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  5099. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  5100. }
  5101. struct btrfs_ref_path {
  5102. u64 extent_start;
  5103. u64 nodes[BTRFS_MAX_LEVEL];
  5104. u64 root_objectid;
  5105. u64 root_generation;
  5106. u64 owner_objectid;
  5107. u32 num_refs;
  5108. int lowest_level;
  5109. int current_level;
  5110. int shared_level;
  5111. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  5112. u64 new_nodes[BTRFS_MAX_LEVEL];
  5113. };
  5114. struct disk_extent {
  5115. u64 ram_bytes;
  5116. u64 disk_bytenr;
  5117. u64 disk_num_bytes;
  5118. u64 offset;
  5119. u64 num_bytes;
  5120. u8 compression;
  5121. u8 encryption;
  5122. u16 other_encoding;
  5123. };
  5124. static int is_cowonly_root(u64 root_objectid)
  5125. {
  5126. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  5127. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  5128. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  5129. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  5130. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  5131. root_objectid == BTRFS_CSUM_TREE_OBJECTID)
  5132. return 1;
  5133. return 0;
  5134. }
  5135. static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
  5136. struct btrfs_root *extent_root,
  5137. struct btrfs_ref_path *ref_path,
  5138. int first_time)
  5139. {
  5140. struct extent_buffer *leaf;
  5141. struct btrfs_path *path;
  5142. struct btrfs_extent_ref *ref;
  5143. struct btrfs_key key;
  5144. struct btrfs_key found_key;
  5145. u64 bytenr;
  5146. u32 nritems;
  5147. int level;
  5148. int ret = 1;
  5149. path = btrfs_alloc_path();
  5150. if (!path)
  5151. return -ENOMEM;
  5152. if (first_time) {
  5153. ref_path->lowest_level = -1;
  5154. ref_path->current_level = -1;
  5155. ref_path->shared_level = -1;
  5156. goto walk_up;
  5157. }
  5158. walk_down:
  5159. level = ref_path->current_level - 1;
  5160. while (level >= -1) {
  5161. u64 parent;
  5162. if (level < ref_path->lowest_level)
  5163. break;
  5164. if (level >= 0)
  5165. bytenr = ref_path->nodes[level];
  5166. else
  5167. bytenr = ref_path->extent_start;
  5168. BUG_ON(bytenr == 0);
  5169. parent = ref_path->nodes[level + 1];
  5170. ref_path->nodes[level + 1] = 0;
  5171. ref_path->current_level = level;
  5172. BUG_ON(parent == 0);
  5173. key.objectid = bytenr;
  5174. key.offset = parent + 1;
  5175. key.type = BTRFS_EXTENT_REF_KEY;
  5176. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  5177. if (ret < 0)
  5178. goto out;
  5179. BUG_ON(ret == 0);
  5180. leaf = path->nodes[0];
  5181. nritems = btrfs_header_nritems(leaf);
  5182. if (path->slots[0] >= nritems) {
  5183. ret = btrfs_next_leaf(extent_root, path);
  5184. if (ret < 0)
  5185. goto out;
  5186. if (ret > 0)
  5187. goto next;
  5188. leaf = path->nodes[0];
  5189. }
  5190. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5191. if (found_key.objectid == bytenr &&
  5192. found_key.type == BTRFS_EXTENT_REF_KEY) {
  5193. if (level < ref_path->shared_level)
  5194. ref_path->shared_level = level;
  5195. goto found;
  5196. }
  5197. next:
  5198. level--;
  5199. btrfs_release_path(extent_root, path);
  5200. cond_resched();
  5201. }
  5202. /* reached lowest level */
  5203. ret = 1;
  5204. goto out;
  5205. walk_up:
  5206. level = ref_path->current_level;
  5207. while (level < BTRFS_MAX_LEVEL - 1) {
  5208. u64 ref_objectid;
  5209. if (level >= 0)
  5210. bytenr = ref_path->nodes[level];
  5211. else
  5212. bytenr = ref_path->extent_start;
  5213. BUG_ON(bytenr == 0);
  5214. key.objectid = bytenr;
  5215. key.offset = 0;
  5216. key.type = BTRFS_EXTENT_REF_KEY;
  5217. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  5218. if (ret < 0)
  5219. goto out;
  5220. leaf = path->nodes[0];
  5221. nritems = btrfs_header_nritems(leaf);
  5222. if (path->slots[0] >= nritems) {
  5223. ret = btrfs_next_leaf(extent_root, path);
  5224. if (ret < 0)
  5225. goto out;
  5226. if (ret > 0) {
  5227. /* the extent was freed by someone */
  5228. if (ref_path->lowest_level == level)
  5229. goto out;
  5230. btrfs_release_path(extent_root, path);
  5231. goto walk_down;
  5232. }
  5233. leaf = path->nodes[0];
  5234. }
  5235. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5236. if (found_key.objectid != bytenr ||
  5237. found_key.type != BTRFS_EXTENT_REF_KEY) {
  5238. /* the extent was freed by someone */
  5239. if (ref_path->lowest_level == level) {
  5240. ret = 1;
  5241. goto out;
  5242. }
  5243. btrfs_release_path(extent_root, path);
  5244. goto walk_down;
  5245. }
  5246. found:
  5247. ref = btrfs_item_ptr(leaf, path->slots[0],
  5248. struct btrfs_extent_ref);
  5249. ref_objectid = btrfs_ref_objectid(leaf, ref);
  5250. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  5251. if (first_time) {
  5252. level = (int)ref_objectid;
  5253. BUG_ON(level >= BTRFS_MAX_LEVEL);
  5254. ref_path->lowest_level = level;
  5255. ref_path->current_level = level;
  5256. ref_path->nodes[level] = bytenr;
  5257. } else {
  5258. WARN_ON(ref_objectid != level);
  5259. }
  5260. } else {
  5261. WARN_ON(level != -1);
  5262. }
  5263. first_time = 0;
  5264. if (ref_path->lowest_level == level) {
  5265. ref_path->owner_objectid = ref_objectid;
  5266. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  5267. }
  5268. /*
  5269. * the block is tree root or the block isn't in reference
  5270. * counted tree.
  5271. */
  5272. if (found_key.objectid == found_key.offset ||
  5273. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  5274. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  5275. ref_path->root_generation =
  5276. btrfs_ref_generation(leaf, ref);
  5277. if (level < 0) {
  5278. /* special reference from the tree log */
  5279. ref_path->nodes[0] = found_key.offset;
  5280. ref_path->current_level = 0;
  5281. }
  5282. ret = 0;
  5283. goto out;
  5284. }
  5285. level++;
  5286. BUG_ON(ref_path->nodes[level] != 0);
  5287. ref_path->nodes[level] = found_key.offset;
  5288. ref_path->current_level = level;
  5289. /*
  5290. * the reference was created in the running transaction,
  5291. * no need to continue walking up.
  5292. */
  5293. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  5294. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  5295. ref_path->root_generation =
  5296. btrfs_ref_generation(leaf, ref);
  5297. ret = 0;
  5298. goto out;
  5299. }
  5300. btrfs_release_path(extent_root, path);
  5301. cond_resched();
  5302. }
  5303. /* reached max tree level, but no tree root found. */
  5304. BUG();
  5305. out:
  5306. btrfs_free_path(path);
  5307. return ret;
  5308. }
  5309. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  5310. struct btrfs_root *extent_root,
  5311. struct btrfs_ref_path *ref_path,
  5312. u64 extent_start)
  5313. {
  5314. memset(ref_path, 0, sizeof(*ref_path));
  5315. ref_path->extent_start = extent_start;
  5316. return __next_ref_path(trans, extent_root, ref_path, 1);
  5317. }
  5318. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  5319. struct btrfs_root *extent_root,
  5320. struct btrfs_ref_path *ref_path)
  5321. {
  5322. return __next_ref_path(trans, extent_root, ref_path, 0);
  5323. }
  5324. static noinline int get_new_locations(struct inode *reloc_inode,
  5325. struct btrfs_key *extent_key,
  5326. u64 offset, int no_fragment,
  5327. struct disk_extent **extents,
  5328. int *nr_extents)
  5329. {
  5330. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  5331. struct btrfs_path *path;
  5332. struct btrfs_file_extent_item *fi;
  5333. struct extent_buffer *leaf;
  5334. struct disk_extent *exts = *extents;
  5335. struct btrfs_key found_key;
  5336. u64 cur_pos;
  5337. u64 last_byte;
  5338. u32 nritems;
  5339. int nr = 0;
  5340. int max = *nr_extents;
  5341. int ret;
  5342. WARN_ON(!no_fragment && *extents);
  5343. if (!exts) {
  5344. max = 1;
  5345. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  5346. if (!exts)
  5347. return -ENOMEM;
  5348. }
  5349. path = btrfs_alloc_path();
  5350. BUG_ON(!path);
  5351. cur_pos = extent_key->objectid - offset;
  5352. last_byte = extent_key->objectid + extent_key->offset;
  5353. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  5354. cur_pos, 0);
  5355. if (ret < 0)
  5356. goto out;
  5357. if (ret > 0) {
  5358. ret = -ENOENT;
  5359. goto out;
  5360. }
  5361. while (1) {
  5362. leaf = path->nodes[0];
  5363. nritems = btrfs_header_nritems(leaf);
  5364. if (path->slots[0] >= nritems) {
  5365. ret = btrfs_next_leaf(root, path);
  5366. if (ret < 0)
  5367. goto out;
  5368. if (ret > 0)
  5369. break;
  5370. leaf = path->nodes[0];
  5371. }
  5372. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5373. if (found_key.offset != cur_pos ||
  5374. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  5375. found_key.objectid != reloc_inode->i_ino)
  5376. break;
  5377. fi = btrfs_item_ptr(leaf, path->slots[0],
  5378. struct btrfs_file_extent_item);
  5379. if (btrfs_file_extent_type(leaf, fi) !=
  5380. BTRFS_FILE_EXTENT_REG ||
  5381. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  5382. break;
  5383. if (nr == max) {
  5384. struct disk_extent *old = exts;
  5385. max *= 2;
  5386. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  5387. memcpy(exts, old, sizeof(*exts) * nr);
  5388. if (old != *extents)
  5389. kfree(old);
  5390. }
  5391. exts[nr].disk_bytenr =
  5392. btrfs_file_extent_disk_bytenr(leaf, fi);
  5393. exts[nr].disk_num_bytes =
  5394. btrfs_file_extent_disk_num_bytes(leaf, fi);
  5395. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  5396. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  5397. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  5398. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  5399. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  5400. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  5401. fi);
  5402. BUG_ON(exts[nr].offset > 0);
  5403. BUG_ON(exts[nr].compression || exts[nr].encryption);
  5404. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  5405. cur_pos += exts[nr].num_bytes;
  5406. nr++;
  5407. if (cur_pos + offset >= last_byte)
  5408. break;
  5409. if (no_fragment) {
  5410. ret = 1;
  5411. goto out;
  5412. }
  5413. path->slots[0]++;
  5414. }
  5415. BUG_ON(cur_pos + offset > last_byte);
  5416. if (cur_pos + offset < last_byte) {
  5417. ret = -ENOENT;
  5418. goto out;
  5419. }
  5420. ret = 0;
  5421. out:
  5422. btrfs_free_path(path);
  5423. if (ret) {
  5424. if (exts != *extents)
  5425. kfree(exts);
  5426. } else {
  5427. *extents = exts;
  5428. *nr_extents = nr;
  5429. }
  5430. return ret;
  5431. }
  5432. static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
  5433. struct btrfs_root *root,
  5434. struct btrfs_path *path,
  5435. struct btrfs_key *extent_key,
  5436. struct btrfs_key *leaf_key,
  5437. struct btrfs_ref_path *ref_path,
  5438. struct disk_extent *new_extents,
  5439. int nr_extents)
  5440. {
  5441. struct extent_buffer *leaf;
  5442. struct btrfs_file_extent_item *fi;
  5443. struct inode *inode = NULL;
  5444. struct btrfs_key key;
  5445. u64 lock_start = 0;
  5446. u64 lock_end = 0;
  5447. u64 num_bytes;
  5448. u64 ext_offset;
  5449. u64 search_end = (u64)-1;
  5450. u32 nritems;
  5451. int nr_scaned = 0;
  5452. int extent_locked = 0;
  5453. int extent_type;
  5454. int ret;
  5455. memcpy(&key, leaf_key, sizeof(key));
  5456. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  5457. if (key.objectid < ref_path->owner_objectid ||
  5458. (key.objectid == ref_path->owner_objectid &&
  5459. key.type < BTRFS_EXTENT_DATA_KEY)) {
  5460. key.objectid = ref_path->owner_objectid;
  5461. key.type = BTRFS_EXTENT_DATA_KEY;
  5462. key.offset = 0;
  5463. }
  5464. }
  5465. while (1) {
  5466. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  5467. if (ret < 0)
  5468. goto out;
  5469. leaf = path->nodes[0];
  5470. nritems = btrfs_header_nritems(leaf);
  5471. next:
  5472. if (extent_locked && ret > 0) {
  5473. /*
  5474. * the file extent item was modified by someone
  5475. * before the extent got locked.
  5476. */
  5477. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  5478. lock_end, GFP_NOFS);
  5479. extent_locked = 0;
  5480. }
  5481. if (path->slots[0] >= nritems) {
  5482. if (++nr_scaned > 2)
  5483. break;
  5484. BUG_ON(extent_locked);
  5485. ret = btrfs_next_leaf(root, path);
  5486. if (ret < 0)
  5487. goto out;
  5488. if (ret > 0)
  5489. break;
  5490. leaf = path->nodes[0];
  5491. nritems = btrfs_header_nritems(leaf);
  5492. }
  5493. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  5494. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  5495. if ((key.objectid > ref_path->owner_objectid) ||
  5496. (key.objectid == ref_path->owner_objectid &&
  5497. key.type > BTRFS_EXTENT_DATA_KEY) ||
  5498. key.offset >= search_end)
  5499. break;
  5500. }
  5501. if (inode && key.objectid != inode->i_ino) {
  5502. BUG_ON(extent_locked);
  5503. btrfs_release_path(root, path);
  5504. mutex_unlock(&inode->i_mutex);
  5505. iput(inode);
  5506. inode = NULL;
  5507. continue;
  5508. }
  5509. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  5510. path->slots[0]++;
  5511. ret = 1;
  5512. goto next;
  5513. }
  5514. fi = btrfs_item_ptr(leaf, path->slots[0],
  5515. struct btrfs_file_extent_item);
  5516. extent_type = btrfs_file_extent_type(leaf, fi);
  5517. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  5518. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  5519. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  5520. extent_key->objectid)) {
  5521. path->slots[0]++;
  5522. ret = 1;
  5523. goto next;
  5524. }
  5525. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  5526. ext_offset = btrfs_file_extent_offset(leaf, fi);
  5527. if (search_end == (u64)-1) {
  5528. search_end = key.offset - ext_offset +
  5529. btrfs_file_extent_ram_bytes(leaf, fi);
  5530. }
  5531. if (!extent_locked) {
  5532. lock_start = key.offset;
  5533. lock_end = lock_start + num_bytes - 1;
  5534. } else {
  5535. if (lock_start > key.offset ||
  5536. lock_end + 1 < key.offset + num_bytes) {
  5537. unlock_extent(&BTRFS_I(inode)->io_tree,
  5538. lock_start, lock_end, GFP_NOFS);
  5539. extent_locked = 0;
  5540. }
  5541. }
  5542. if (!inode) {
  5543. btrfs_release_path(root, path);
  5544. inode = btrfs_iget_locked(root->fs_info->sb,
  5545. key.objectid, root);
  5546. if (inode->i_state & I_NEW) {
  5547. BTRFS_I(inode)->root = root;
  5548. BTRFS_I(inode)->location.objectid =
  5549. key.objectid;
  5550. BTRFS_I(inode)->location.type =
  5551. BTRFS_INODE_ITEM_KEY;
  5552. BTRFS_I(inode)->location.offset = 0;
  5553. btrfs_read_locked_inode(inode);
  5554. unlock_new_inode(inode);
  5555. }
  5556. /*
  5557. * some code call btrfs_commit_transaction while
  5558. * holding the i_mutex, so we can't use mutex_lock
  5559. * here.
  5560. */
  5561. if (is_bad_inode(inode) ||
  5562. !mutex_trylock(&inode->i_mutex)) {
  5563. iput(inode);
  5564. inode = NULL;
  5565. key.offset = (u64)-1;
  5566. goto skip;
  5567. }
  5568. }
  5569. if (!extent_locked) {
  5570. struct btrfs_ordered_extent *ordered;
  5571. btrfs_release_path(root, path);
  5572. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  5573. lock_end, GFP_NOFS);
  5574. ordered = btrfs_lookup_first_ordered_extent(inode,
  5575. lock_end);
  5576. if (ordered &&
  5577. ordered->file_offset <= lock_end &&
  5578. ordered->file_offset + ordered->len > lock_start) {
  5579. unlock_extent(&BTRFS_I(inode)->io_tree,
  5580. lock_start, lock_end, GFP_NOFS);
  5581. btrfs_start_ordered_extent(inode, ordered, 1);
  5582. btrfs_put_ordered_extent(ordered);
  5583. key.offset += num_bytes;
  5584. goto skip;
  5585. }
  5586. if (ordered)
  5587. btrfs_put_ordered_extent(ordered);
  5588. extent_locked = 1;
  5589. continue;
  5590. }
  5591. if (nr_extents == 1) {
  5592. /* update extent pointer in place */
  5593. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  5594. new_extents[0].disk_bytenr);
  5595. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  5596. new_extents[0].disk_num_bytes);
  5597. btrfs_mark_buffer_dirty(leaf);
  5598. btrfs_drop_extent_cache(inode, key.offset,
  5599. key.offset + num_bytes - 1, 0);
  5600. ret = btrfs_inc_extent_ref(trans, root,
  5601. new_extents[0].disk_bytenr,
  5602. new_extents[0].disk_num_bytes,
  5603. leaf->start,
  5604. root->root_key.objectid,
  5605. trans->transid,
  5606. key.objectid);
  5607. BUG_ON(ret);
  5608. ret = btrfs_free_extent(trans, root,
  5609. extent_key->objectid,
  5610. extent_key->offset,
  5611. leaf->start,
  5612. btrfs_header_owner(leaf),
  5613. btrfs_header_generation(leaf),
  5614. key.objectid, 0);
  5615. BUG_ON(ret);
  5616. btrfs_release_path(root, path);
  5617. key.offset += num_bytes;
  5618. } else {
  5619. BUG_ON(1);
  5620. #if 0
  5621. u64 alloc_hint;
  5622. u64 extent_len;
  5623. int i;
  5624. /*
  5625. * drop old extent pointer at first, then insert the
  5626. * new pointers one bye one
  5627. */
  5628. btrfs_release_path(root, path);
  5629. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  5630. key.offset + num_bytes,
  5631. key.offset, &alloc_hint);
  5632. BUG_ON(ret);
  5633. for (i = 0; i < nr_extents; i++) {
  5634. if (ext_offset >= new_extents[i].num_bytes) {
  5635. ext_offset -= new_extents[i].num_bytes;
  5636. continue;
  5637. }
  5638. extent_len = min(new_extents[i].num_bytes -
  5639. ext_offset, num_bytes);
  5640. ret = btrfs_insert_empty_item(trans, root,
  5641. path, &key,
  5642. sizeof(*fi));
  5643. BUG_ON(ret);
  5644. leaf = path->nodes[0];
  5645. fi = btrfs_item_ptr(leaf, path->slots[0],
  5646. struct btrfs_file_extent_item);
  5647. btrfs_set_file_extent_generation(leaf, fi,
  5648. trans->transid);
  5649. btrfs_set_file_extent_type(leaf, fi,
  5650. BTRFS_FILE_EXTENT_REG);
  5651. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  5652. new_extents[i].disk_bytenr);
  5653. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  5654. new_extents[i].disk_num_bytes);
  5655. btrfs_set_file_extent_ram_bytes(leaf, fi,
  5656. new_extents[i].ram_bytes);
  5657. btrfs_set_file_extent_compression(leaf, fi,
  5658. new_extents[i].compression);
  5659. btrfs_set_file_extent_encryption(leaf, fi,
  5660. new_extents[i].encryption);
  5661. btrfs_set_file_extent_other_encoding(leaf, fi,
  5662. new_extents[i].other_encoding);
  5663. btrfs_set_file_extent_num_bytes(leaf, fi,
  5664. extent_len);
  5665. ext_offset += new_extents[i].offset;
  5666. btrfs_set_file_extent_offset(leaf, fi,
  5667. ext_offset);
  5668. btrfs_mark_buffer_dirty(leaf);
  5669. btrfs_drop_extent_cache(inode, key.offset,
  5670. key.offset + extent_len - 1, 0);
  5671. ret = btrfs_inc_extent_ref(trans, root,
  5672. new_extents[i].disk_bytenr,
  5673. new_extents[i].disk_num_bytes,
  5674. leaf->start,
  5675. root->root_key.objectid,
  5676. trans->transid, key.objectid);
  5677. BUG_ON(ret);
  5678. btrfs_release_path(root, path);
  5679. inode_add_bytes(inode, extent_len);
  5680. ext_offset = 0;
  5681. num_bytes -= extent_len;
  5682. key.offset += extent_len;
  5683. if (num_bytes == 0)
  5684. break;
  5685. }
  5686. BUG_ON(i >= nr_extents);
  5687. #endif
  5688. }
  5689. if (extent_locked) {
  5690. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  5691. lock_end, GFP_NOFS);
  5692. extent_locked = 0;
  5693. }
  5694. skip:
  5695. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  5696. key.offset >= search_end)
  5697. break;
  5698. cond_resched();
  5699. }
  5700. ret = 0;
  5701. out:
  5702. btrfs_release_path(root, path);
  5703. if (inode) {
  5704. mutex_unlock(&inode->i_mutex);
  5705. if (extent_locked) {
  5706. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  5707. lock_end, GFP_NOFS);
  5708. }
  5709. iput(inode);
  5710. }
  5711. return ret;
  5712. }
  5713. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  5714. struct btrfs_root *root,
  5715. struct extent_buffer *buf, u64 orig_start)
  5716. {
  5717. int level;
  5718. int ret;
  5719. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  5720. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  5721. level = btrfs_header_level(buf);
  5722. if (level == 0) {
  5723. struct btrfs_leaf_ref *ref;
  5724. struct btrfs_leaf_ref *orig_ref;
  5725. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  5726. if (!orig_ref)
  5727. return -ENOENT;
  5728. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  5729. if (!ref) {
  5730. btrfs_free_leaf_ref(root, orig_ref);
  5731. return -ENOMEM;
  5732. }
  5733. ref->nritems = orig_ref->nritems;
  5734. memcpy(ref->extents, orig_ref->extents,
  5735. sizeof(ref->extents[0]) * ref->nritems);
  5736. btrfs_free_leaf_ref(root, orig_ref);
  5737. ref->root_gen = trans->transid;
  5738. ref->bytenr = buf->start;
  5739. ref->owner = btrfs_header_owner(buf);
  5740. ref->generation = btrfs_header_generation(buf);
  5741. ret = btrfs_add_leaf_ref(root, ref, 0);
  5742. WARN_ON(ret);
  5743. btrfs_free_leaf_ref(root, ref);
  5744. }
  5745. return 0;
  5746. }
  5747. static noinline int invalidate_extent_cache(struct btrfs_root *root,
  5748. struct extent_buffer *leaf,
  5749. struct btrfs_block_group_cache *group,
  5750. struct btrfs_root *target_root)
  5751. {
  5752. struct btrfs_key key;
  5753. struct inode *inode = NULL;
  5754. struct btrfs_file_extent_item *fi;
  5755. u64 num_bytes;
  5756. u64 skip_objectid = 0;
  5757. u32 nritems;
  5758. u32 i;
  5759. nritems = btrfs_header_nritems(leaf);
  5760. for (i = 0; i < nritems; i++) {
  5761. btrfs_item_key_to_cpu(leaf, &key, i);
  5762. if (key.objectid == skip_objectid ||
  5763. key.type != BTRFS_EXTENT_DATA_KEY)
  5764. continue;
  5765. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  5766. if (btrfs_file_extent_type(leaf, fi) ==
  5767. BTRFS_FILE_EXTENT_INLINE)
  5768. continue;
  5769. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  5770. continue;
  5771. if (!inode || inode->i_ino != key.objectid) {
  5772. iput(inode);
  5773. inode = btrfs_ilookup(target_root->fs_info->sb,
  5774. key.objectid, target_root, 1);
  5775. }
  5776. if (!inode) {
  5777. skip_objectid = key.objectid;
  5778. continue;
  5779. }
  5780. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  5781. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  5782. key.offset + num_bytes - 1, GFP_NOFS);
  5783. btrfs_drop_extent_cache(inode, key.offset,
  5784. key.offset + num_bytes - 1, 1);
  5785. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  5786. key.offset + num_bytes - 1, GFP_NOFS);
  5787. cond_resched();
  5788. }
  5789. iput(inode);
  5790. return 0;
  5791. }
  5792. static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  5793. struct btrfs_root *root,
  5794. struct extent_buffer *leaf,
  5795. struct btrfs_block_group_cache *group,
  5796. struct inode *reloc_inode)
  5797. {
  5798. struct btrfs_key key;
  5799. struct btrfs_key extent_key;
  5800. struct btrfs_file_extent_item *fi;
  5801. struct btrfs_leaf_ref *ref;
  5802. struct disk_extent *new_extent;
  5803. u64 bytenr;
  5804. u64 num_bytes;
  5805. u32 nritems;
  5806. u32 i;
  5807. int ext_index;
  5808. int nr_extent;
  5809. int ret;
  5810. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  5811. BUG_ON(!new_extent);
  5812. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  5813. BUG_ON(!ref);
  5814. ext_index = -1;
  5815. nritems = btrfs_header_nritems(leaf);
  5816. for (i = 0; i < nritems; i++) {
  5817. btrfs_item_key_to_cpu(leaf, &key, i);
  5818. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  5819. continue;
  5820. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  5821. if (btrfs_file_extent_type(leaf, fi) ==
  5822. BTRFS_FILE_EXTENT_INLINE)
  5823. continue;
  5824. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  5825. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  5826. if (bytenr == 0)
  5827. continue;
  5828. ext_index++;
  5829. if (bytenr >= group->key.objectid + group->key.offset ||
  5830. bytenr + num_bytes <= group->key.objectid)
  5831. continue;
  5832. extent_key.objectid = bytenr;
  5833. extent_key.offset = num_bytes;
  5834. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  5835. nr_extent = 1;
  5836. ret = get_new_locations(reloc_inode, &extent_key,
  5837. group->key.objectid, 1,
  5838. &new_extent, &nr_extent);
  5839. if (ret > 0)
  5840. continue;
  5841. BUG_ON(ret < 0);
  5842. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  5843. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  5844. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  5845. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  5846. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  5847. new_extent->disk_bytenr);
  5848. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  5849. new_extent->disk_num_bytes);
  5850. btrfs_mark_buffer_dirty(leaf);
  5851. ret = btrfs_inc_extent_ref(trans, root,
  5852. new_extent->disk_bytenr,
  5853. new_extent->disk_num_bytes,
  5854. leaf->start,
  5855. root->root_key.objectid,
  5856. trans->transid, key.objectid);
  5857. BUG_ON(ret);
  5858. ret = btrfs_free_extent(trans, root,
  5859. bytenr, num_bytes, leaf->start,
  5860. btrfs_header_owner(leaf),
  5861. btrfs_header_generation(leaf),
  5862. key.objectid, 0);
  5863. BUG_ON(ret);
  5864. cond_resched();
  5865. }
  5866. kfree(new_extent);
  5867. BUG_ON(ext_index + 1 != ref->nritems);
  5868. btrfs_free_leaf_ref(root, ref);
  5869. return 0;
  5870. }
  5871. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  5872. struct btrfs_root *root)
  5873. {
  5874. struct btrfs_root *reloc_root;
  5875. int ret;
  5876. if (root->reloc_root) {
  5877. reloc_root = root->reloc_root;
  5878. root->reloc_root = NULL;
  5879. list_add(&reloc_root->dead_list,
  5880. &root->fs_info->dead_reloc_roots);
  5881. btrfs_set_root_bytenr(&reloc_root->root_item,
  5882. reloc_root->node->start);
  5883. btrfs_set_root_level(&root->root_item,
  5884. btrfs_header_level(reloc_root->node));
  5885. memset(&reloc_root->root_item.drop_progress, 0,
  5886. sizeof(struct btrfs_disk_key));
  5887. reloc_root->root_item.drop_level = 0;
  5888. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  5889. &reloc_root->root_key,
  5890. &reloc_root->root_item);
  5891. BUG_ON(ret);
  5892. }
  5893. return 0;
  5894. }
  5895. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  5896. {
  5897. struct btrfs_trans_handle *trans;
  5898. struct btrfs_root *reloc_root;
  5899. struct btrfs_root *prev_root = NULL;
  5900. struct list_head dead_roots;
  5901. int ret;
  5902. unsigned long nr;
  5903. INIT_LIST_HEAD(&dead_roots);
  5904. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  5905. while (!list_empty(&dead_roots)) {
  5906. reloc_root = list_entry(dead_roots.prev,
  5907. struct btrfs_root, dead_list);
  5908. list_del_init(&reloc_root->dead_list);
  5909. BUG_ON(reloc_root->commit_root != NULL);
  5910. while (1) {
  5911. trans = btrfs_join_transaction(root, 1);
  5912. BUG_ON(!trans);
  5913. mutex_lock(&root->fs_info->drop_mutex);
  5914. ret = btrfs_drop_snapshot(trans, reloc_root);
  5915. if (ret != -EAGAIN)
  5916. break;
  5917. mutex_unlock(&root->fs_info->drop_mutex);
  5918. nr = trans->blocks_used;
  5919. ret = btrfs_end_transaction(trans, root);
  5920. BUG_ON(ret);
  5921. btrfs_btree_balance_dirty(root, nr);
  5922. }
  5923. free_extent_buffer(reloc_root->node);
  5924. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  5925. &reloc_root->root_key);
  5926. BUG_ON(ret);
  5927. mutex_unlock(&root->fs_info->drop_mutex);
  5928. nr = trans->blocks_used;
  5929. ret = btrfs_end_transaction(trans, root);
  5930. BUG_ON(ret);
  5931. btrfs_btree_balance_dirty(root, nr);
  5932. kfree(prev_root);
  5933. prev_root = reloc_root;
  5934. }
  5935. if (prev_root) {
  5936. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  5937. kfree(prev_root);
  5938. }
  5939. return 0;
  5940. }
  5941. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  5942. {
  5943. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  5944. return 0;
  5945. }
  5946. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  5947. {
  5948. struct btrfs_root *reloc_root;
  5949. struct btrfs_trans_handle *trans;
  5950. struct btrfs_key location;
  5951. int found;
  5952. int ret;
  5953. mutex_lock(&root->fs_info->tree_reloc_mutex);
  5954. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  5955. BUG_ON(ret);
  5956. found = !list_empty(&root->fs_info->dead_reloc_roots);
  5957. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  5958. if (found) {
  5959. trans = btrfs_start_transaction(root, 1);
  5960. BUG_ON(!trans);
  5961. ret = btrfs_commit_transaction(trans, root);
  5962. BUG_ON(ret);
  5963. }
  5964. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  5965. location.offset = (u64)-1;
  5966. location.type = BTRFS_ROOT_ITEM_KEY;
  5967. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  5968. BUG_ON(!reloc_root);
  5969. btrfs_orphan_cleanup(reloc_root);
  5970. return 0;
  5971. }
  5972. static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
  5973. struct btrfs_root *root)
  5974. {
  5975. struct btrfs_root *reloc_root;
  5976. struct extent_buffer *eb;
  5977. struct btrfs_root_item *root_item;
  5978. struct btrfs_key root_key;
  5979. int ret;
  5980. BUG_ON(!root->ref_cows);
  5981. if (root->reloc_root)
  5982. return 0;
  5983. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  5984. BUG_ON(!root_item);
  5985. ret = btrfs_copy_root(trans, root, root->commit_root,
  5986. &eb, BTRFS_TREE_RELOC_OBJECTID);
  5987. BUG_ON(ret);
  5988. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  5989. root_key.offset = root->root_key.objectid;
  5990. root_key.type = BTRFS_ROOT_ITEM_KEY;
  5991. memcpy(root_item, &root->root_item, sizeof(root_item));
  5992. btrfs_set_root_refs(root_item, 0);
  5993. btrfs_set_root_bytenr(root_item, eb->start);
  5994. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  5995. btrfs_set_root_generation(root_item, trans->transid);
  5996. btrfs_tree_unlock(eb);
  5997. free_extent_buffer(eb);
  5998. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  5999. &root_key, root_item);
  6000. BUG_ON(ret);
  6001. kfree(root_item);
  6002. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  6003. &root_key);
  6004. BUG_ON(!reloc_root);
  6005. reloc_root->last_trans = trans->transid;
  6006. reloc_root->commit_root = NULL;
  6007. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  6008. root->reloc_root = reloc_root;
  6009. return 0;
  6010. }
  6011. /*
  6012. * Core function of space balance.
  6013. *
  6014. * The idea is using reloc trees to relocate tree blocks in reference
  6015. * counted roots. There is one reloc tree for each subvol, and all
  6016. * reloc trees share same root key objectid. Reloc trees are snapshots
  6017. * of the latest committed roots of subvols (root->commit_root).
  6018. *
  6019. * To relocate a tree block referenced by a subvol, there are two steps.
  6020. * COW the block through subvol's reloc tree, then update block pointer
  6021. * in the subvol to point to the new block. Since all reloc trees share
  6022. * same root key objectid, doing special handing for tree blocks owned
  6023. * by them is easy. Once a tree block has been COWed in one reloc tree,
  6024. * we can use the resulting new block directly when the same block is
  6025. * required to COW again through other reloc trees. By this way, relocated
  6026. * tree blocks are shared between reloc trees, so they are also shared
  6027. * between subvols.
  6028. */
  6029. static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
  6030. struct btrfs_root *root,
  6031. struct btrfs_path *path,
  6032. struct btrfs_key *first_key,
  6033. struct btrfs_ref_path *ref_path,
  6034. struct btrfs_block_group_cache *group,
  6035. struct inode *reloc_inode)
  6036. {
  6037. struct btrfs_root *reloc_root;
  6038. struct extent_buffer *eb = NULL;
  6039. struct btrfs_key *keys;
  6040. u64 *nodes;
  6041. int level;
  6042. int shared_level;
  6043. int lowest_level = 0;
  6044. int ret;
  6045. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  6046. lowest_level = ref_path->owner_objectid;
  6047. if (!root->ref_cows) {
  6048. path->lowest_level = lowest_level;
  6049. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  6050. BUG_ON(ret < 0);
  6051. path->lowest_level = 0;
  6052. btrfs_release_path(root, path);
  6053. return 0;
  6054. }
  6055. mutex_lock(&root->fs_info->tree_reloc_mutex);
  6056. ret = init_reloc_tree(trans, root);
  6057. BUG_ON(ret);
  6058. reloc_root = root->reloc_root;
  6059. shared_level = ref_path->shared_level;
  6060. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  6061. keys = ref_path->node_keys;
  6062. nodes = ref_path->new_nodes;
  6063. memset(&keys[shared_level + 1], 0,
  6064. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  6065. memset(&nodes[shared_level + 1], 0,
  6066. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  6067. if (nodes[lowest_level] == 0) {
  6068. path->lowest_level = lowest_level;
  6069. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  6070. 0, 1);
  6071. BUG_ON(ret);
  6072. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  6073. eb = path->nodes[level];
  6074. if (!eb || eb == reloc_root->node)
  6075. break;
  6076. nodes[level] = eb->start;
  6077. if (level == 0)
  6078. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  6079. else
  6080. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  6081. }
  6082. if (nodes[0] &&
  6083. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6084. eb = path->nodes[0];
  6085. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  6086. group, reloc_inode);
  6087. BUG_ON(ret);
  6088. }
  6089. btrfs_release_path(reloc_root, path);
  6090. } else {
  6091. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  6092. lowest_level);
  6093. BUG_ON(ret);
  6094. }
  6095. /*
  6096. * replace tree blocks in the fs tree with tree blocks in
  6097. * the reloc tree.
  6098. */
  6099. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  6100. BUG_ON(ret < 0);
  6101. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6102. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  6103. 0, 0);
  6104. BUG_ON(ret);
  6105. extent_buffer_get(path->nodes[0]);
  6106. eb = path->nodes[0];
  6107. btrfs_release_path(reloc_root, path);
  6108. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  6109. BUG_ON(ret);
  6110. free_extent_buffer(eb);
  6111. }
  6112. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  6113. path->lowest_level = 0;
  6114. return 0;
  6115. }
  6116. static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
  6117. struct btrfs_root *root,
  6118. struct btrfs_path *path,
  6119. struct btrfs_key *first_key,
  6120. struct btrfs_ref_path *ref_path)
  6121. {
  6122. int ret;
  6123. ret = relocate_one_path(trans, root, path, first_key,
  6124. ref_path, NULL, NULL);
  6125. BUG_ON(ret);
  6126. return 0;
  6127. }
  6128. static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
  6129. struct btrfs_root *extent_root,
  6130. struct btrfs_path *path,
  6131. struct btrfs_key *extent_key)
  6132. {
  6133. int ret;
  6134. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  6135. if (ret)
  6136. goto out;
  6137. ret = btrfs_del_item(trans, extent_root, path);
  6138. out:
  6139. btrfs_release_path(extent_root, path);
  6140. return ret;
  6141. }
  6142. static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
  6143. struct btrfs_ref_path *ref_path)
  6144. {
  6145. struct btrfs_key root_key;
  6146. root_key.objectid = ref_path->root_objectid;
  6147. root_key.type = BTRFS_ROOT_ITEM_KEY;
  6148. if (is_cowonly_root(ref_path->root_objectid))
  6149. root_key.offset = 0;
  6150. else
  6151. root_key.offset = (u64)-1;
  6152. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  6153. }
  6154. static noinline int relocate_one_extent(struct btrfs_root *extent_root,
  6155. struct btrfs_path *path,
  6156. struct btrfs_key *extent_key,
  6157. struct btrfs_block_group_cache *group,
  6158. struct inode *reloc_inode, int pass)
  6159. {
  6160. struct btrfs_trans_handle *trans;
  6161. struct btrfs_root *found_root;
  6162. struct btrfs_ref_path *ref_path = NULL;
  6163. struct disk_extent *new_extents = NULL;
  6164. int nr_extents = 0;
  6165. int loops;
  6166. int ret;
  6167. int level;
  6168. struct btrfs_key first_key;
  6169. u64 prev_block = 0;
  6170. trans = btrfs_start_transaction(extent_root, 1);
  6171. BUG_ON(!trans);
  6172. if (extent_key->objectid == 0) {
  6173. ret = del_extent_zero(trans, extent_root, path, extent_key);
  6174. goto out;
  6175. }
  6176. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  6177. if (!ref_path) {
  6178. ret = -ENOMEM;
  6179. goto out;
  6180. }
  6181. for (loops = 0; ; loops++) {
  6182. if (loops == 0) {
  6183. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  6184. extent_key->objectid);
  6185. } else {
  6186. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  6187. }
  6188. if (ret < 0)
  6189. goto out;
  6190. if (ret > 0)
  6191. break;
  6192. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  6193. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  6194. continue;
  6195. found_root = read_ref_root(extent_root->fs_info, ref_path);
  6196. BUG_ON(!found_root);
  6197. /*
  6198. * for reference counted tree, only process reference paths
  6199. * rooted at the latest committed root.
  6200. */
  6201. if (found_root->ref_cows &&
  6202. ref_path->root_generation != found_root->root_key.offset)
  6203. continue;
  6204. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6205. if (pass == 0) {
  6206. /*
  6207. * copy data extents to new locations
  6208. */
  6209. u64 group_start = group->key.objectid;
  6210. ret = relocate_data_extent(reloc_inode,
  6211. extent_key,
  6212. group_start);
  6213. if (ret < 0)
  6214. goto out;
  6215. break;
  6216. }
  6217. level = 0;
  6218. } else {
  6219. level = ref_path->owner_objectid;
  6220. }
  6221. if (prev_block != ref_path->nodes[level]) {
  6222. struct extent_buffer *eb;
  6223. u64 block_start = ref_path->nodes[level];
  6224. u64 block_size = btrfs_level_size(found_root, level);
  6225. eb = read_tree_block(found_root, block_start,
  6226. block_size, 0);
  6227. btrfs_tree_lock(eb);
  6228. BUG_ON(level != btrfs_header_level(eb));
  6229. if (level == 0)
  6230. btrfs_item_key_to_cpu(eb, &first_key, 0);
  6231. else
  6232. btrfs_node_key_to_cpu(eb, &first_key, 0);
  6233. btrfs_tree_unlock(eb);
  6234. free_extent_buffer(eb);
  6235. prev_block = block_start;
  6236. }
  6237. mutex_lock(&extent_root->fs_info->trans_mutex);
  6238. btrfs_record_root_in_trans(found_root);
  6239. mutex_unlock(&extent_root->fs_info->trans_mutex);
  6240. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6241. /*
  6242. * try to update data extent references while
  6243. * keeping metadata shared between snapshots.
  6244. */
  6245. if (pass == 1) {
  6246. ret = relocate_one_path(trans, found_root,
  6247. path, &first_key, ref_path,
  6248. group, reloc_inode);
  6249. if (ret < 0)
  6250. goto out;
  6251. continue;
  6252. }
  6253. /*
  6254. * use fallback method to process the remaining
  6255. * references.
  6256. */
  6257. if (!new_extents) {
  6258. u64 group_start = group->key.objectid;
  6259. new_extents = kmalloc(sizeof(*new_extents),
  6260. GFP_NOFS);
  6261. nr_extents = 1;
  6262. ret = get_new_locations(reloc_inode,
  6263. extent_key,
  6264. group_start, 1,
  6265. &new_extents,
  6266. &nr_extents);
  6267. if (ret)
  6268. goto out;
  6269. }
  6270. ret = replace_one_extent(trans, found_root,
  6271. path, extent_key,
  6272. &first_key, ref_path,
  6273. new_extents, nr_extents);
  6274. } else {
  6275. ret = relocate_tree_block(trans, found_root, path,
  6276. &first_key, ref_path);
  6277. }
  6278. if (ret < 0)
  6279. goto out;
  6280. }
  6281. ret = 0;
  6282. out:
  6283. btrfs_end_transaction(trans, extent_root);
  6284. kfree(new_extents);
  6285. kfree(ref_path);
  6286. return ret;
  6287. }
  6288. #endif
  6289. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6290. {
  6291. u64 num_devices;
  6292. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6293. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6294. num_devices = root->fs_info->fs_devices->rw_devices;
  6295. if (num_devices == 1) {
  6296. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6297. stripped = flags & ~stripped;
  6298. /* turn raid0 into single device chunks */
  6299. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6300. return stripped;
  6301. /* turn mirroring into duplication */
  6302. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6303. BTRFS_BLOCK_GROUP_RAID10))
  6304. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6305. return flags;
  6306. } else {
  6307. /* they already had raid on here, just return */
  6308. if (flags & stripped)
  6309. return flags;
  6310. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6311. stripped = flags & ~stripped;
  6312. /* switch duplicated blocks with raid1 */
  6313. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6314. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6315. /* turn single device chunks into raid0 */
  6316. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  6317. }
  6318. return flags;
  6319. }
  6320. static int __alloc_chunk_for_shrink(struct btrfs_root *root,
  6321. struct btrfs_block_group_cache *shrink_block_group,
  6322. int force)
  6323. {
  6324. struct btrfs_trans_handle *trans;
  6325. u64 new_alloc_flags;
  6326. u64 calc;
  6327. spin_lock(&shrink_block_group->lock);
  6328. if (btrfs_block_group_used(&shrink_block_group->item) +
  6329. shrink_block_group->reserved > 0) {
  6330. spin_unlock(&shrink_block_group->lock);
  6331. trans = btrfs_start_transaction(root, 1);
  6332. spin_lock(&shrink_block_group->lock);
  6333. new_alloc_flags = update_block_group_flags(root,
  6334. shrink_block_group->flags);
  6335. if (new_alloc_flags != shrink_block_group->flags) {
  6336. calc =
  6337. btrfs_block_group_used(&shrink_block_group->item);
  6338. } else {
  6339. calc = shrink_block_group->key.offset;
  6340. }
  6341. spin_unlock(&shrink_block_group->lock);
  6342. do_chunk_alloc(trans, root->fs_info->extent_root,
  6343. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  6344. btrfs_end_transaction(trans, root);
  6345. } else
  6346. spin_unlock(&shrink_block_group->lock);
  6347. return 0;
  6348. }
  6349. int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
  6350. struct btrfs_block_group_cache *group)
  6351. {
  6352. __alloc_chunk_for_shrink(root, group, 1);
  6353. set_block_group_readonly(group);
  6354. return 0;
  6355. }
  6356. /*
  6357. * checks to see if its even possible to relocate this block group.
  6358. *
  6359. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  6360. * ok to go ahead and try.
  6361. */
  6362. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  6363. {
  6364. struct btrfs_block_group_cache *block_group;
  6365. struct btrfs_space_info *space_info;
  6366. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  6367. struct btrfs_device *device;
  6368. int full = 0;
  6369. int ret = 0;
  6370. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  6371. /* odd, couldn't find the block group, leave it alone */
  6372. if (!block_group)
  6373. return -1;
  6374. /* no bytes used, we're good */
  6375. if (!btrfs_block_group_used(&block_group->item))
  6376. goto out;
  6377. space_info = block_group->space_info;
  6378. spin_lock(&space_info->lock);
  6379. full = space_info->full;
  6380. /*
  6381. * if this is the last block group we have in this space, we can't
  6382. * relocate it unless we're able to allocate a new chunk below.
  6383. *
  6384. * Otherwise, we need to make sure we have room in the space to handle
  6385. * all of the extents from this block group. If we can, we're good
  6386. */
  6387. if ((space_info->total_bytes != block_group->key.offset) &&
  6388. (space_info->bytes_used + space_info->bytes_reserved +
  6389. space_info->bytes_pinned + space_info->bytes_readonly +
  6390. btrfs_block_group_used(&block_group->item) <
  6391. space_info->total_bytes)) {
  6392. spin_unlock(&space_info->lock);
  6393. goto out;
  6394. }
  6395. spin_unlock(&space_info->lock);
  6396. /*
  6397. * ok we don't have enough space, but maybe we have free space on our
  6398. * devices to allocate new chunks for relocation, so loop through our
  6399. * alloc devices and guess if we have enough space. However, if we
  6400. * were marked as full, then we know there aren't enough chunks, and we
  6401. * can just return.
  6402. */
  6403. ret = -1;
  6404. if (full)
  6405. goto out;
  6406. mutex_lock(&root->fs_info->chunk_mutex);
  6407. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  6408. u64 min_free = btrfs_block_group_used(&block_group->item);
  6409. u64 dev_offset, max_avail;
  6410. /*
  6411. * check to make sure we can actually find a chunk with enough
  6412. * space to fit our block group in.
  6413. */
  6414. if (device->total_bytes > device->bytes_used + min_free) {
  6415. ret = find_free_dev_extent(NULL, device, min_free,
  6416. &dev_offset, &max_avail);
  6417. if (!ret)
  6418. break;
  6419. ret = -1;
  6420. }
  6421. }
  6422. mutex_unlock(&root->fs_info->chunk_mutex);
  6423. out:
  6424. btrfs_put_block_group(block_group);
  6425. return ret;
  6426. }
  6427. static int find_first_block_group(struct btrfs_root *root,
  6428. struct btrfs_path *path, struct btrfs_key *key)
  6429. {
  6430. int ret = 0;
  6431. struct btrfs_key found_key;
  6432. struct extent_buffer *leaf;
  6433. int slot;
  6434. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  6435. if (ret < 0)
  6436. goto out;
  6437. while (1) {
  6438. slot = path->slots[0];
  6439. leaf = path->nodes[0];
  6440. if (slot >= btrfs_header_nritems(leaf)) {
  6441. ret = btrfs_next_leaf(root, path);
  6442. if (ret == 0)
  6443. continue;
  6444. if (ret < 0)
  6445. goto out;
  6446. break;
  6447. }
  6448. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6449. if (found_key.objectid >= key->objectid &&
  6450. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  6451. ret = 0;
  6452. goto out;
  6453. }
  6454. path->slots[0]++;
  6455. }
  6456. ret = -ENOENT;
  6457. out:
  6458. return ret;
  6459. }
  6460. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  6461. {
  6462. struct btrfs_block_group_cache *block_group;
  6463. struct btrfs_space_info *space_info;
  6464. struct btrfs_caching_control *caching_ctl;
  6465. struct rb_node *n;
  6466. down_write(&info->extent_commit_sem);
  6467. while (!list_empty(&info->caching_block_groups)) {
  6468. caching_ctl = list_entry(info->caching_block_groups.next,
  6469. struct btrfs_caching_control, list);
  6470. list_del(&caching_ctl->list);
  6471. put_caching_control(caching_ctl);
  6472. }
  6473. up_write(&info->extent_commit_sem);
  6474. spin_lock(&info->block_group_cache_lock);
  6475. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  6476. block_group = rb_entry(n, struct btrfs_block_group_cache,
  6477. cache_node);
  6478. rb_erase(&block_group->cache_node,
  6479. &info->block_group_cache_tree);
  6480. spin_unlock(&info->block_group_cache_lock);
  6481. down_write(&block_group->space_info->groups_sem);
  6482. list_del(&block_group->list);
  6483. up_write(&block_group->space_info->groups_sem);
  6484. if (block_group->cached == BTRFS_CACHE_STARTED)
  6485. wait_block_group_cache_done(block_group);
  6486. btrfs_remove_free_space_cache(block_group);
  6487. WARN_ON(atomic_read(&block_group->count) != 1);
  6488. kfree(block_group);
  6489. spin_lock(&info->block_group_cache_lock);
  6490. }
  6491. spin_unlock(&info->block_group_cache_lock);
  6492. /* now that all the block groups are freed, go through and
  6493. * free all the space_info structs. This is only called during
  6494. * the final stages of unmount, and so we know nobody is
  6495. * using them. We call synchronize_rcu() once before we start,
  6496. * just to be on the safe side.
  6497. */
  6498. synchronize_rcu();
  6499. while(!list_empty(&info->space_info)) {
  6500. space_info = list_entry(info->space_info.next,
  6501. struct btrfs_space_info,
  6502. list);
  6503. list_del(&space_info->list);
  6504. kfree(space_info);
  6505. }
  6506. return 0;
  6507. }
  6508. int btrfs_read_block_groups(struct btrfs_root *root)
  6509. {
  6510. struct btrfs_path *path;
  6511. int ret;
  6512. struct btrfs_block_group_cache *cache;
  6513. struct btrfs_fs_info *info = root->fs_info;
  6514. struct btrfs_space_info *space_info;
  6515. struct btrfs_key key;
  6516. struct btrfs_key found_key;
  6517. struct extent_buffer *leaf;
  6518. root = info->extent_root;
  6519. key.objectid = 0;
  6520. key.offset = 0;
  6521. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  6522. path = btrfs_alloc_path();
  6523. if (!path)
  6524. return -ENOMEM;
  6525. while (1) {
  6526. ret = find_first_block_group(root, path, &key);
  6527. if (ret > 0) {
  6528. ret = 0;
  6529. goto error;
  6530. }
  6531. if (ret != 0)
  6532. goto error;
  6533. leaf = path->nodes[0];
  6534. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6535. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6536. if (!cache) {
  6537. ret = -ENOMEM;
  6538. break;
  6539. }
  6540. atomic_set(&cache->count, 1);
  6541. spin_lock_init(&cache->lock);
  6542. spin_lock_init(&cache->tree_lock);
  6543. cache->fs_info = info;
  6544. INIT_LIST_HEAD(&cache->list);
  6545. INIT_LIST_HEAD(&cache->cluster_list);
  6546. /*
  6547. * we only want to have 32k of ram per block group for keeping
  6548. * track of free space, and if we pass 1/2 of that we want to
  6549. * start converting things over to using bitmaps
  6550. */
  6551. cache->extents_thresh = ((1024 * 32) / 2) /
  6552. sizeof(struct btrfs_free_space);
  6553. read_extent_buffer(leaf, &cache->item,
  6554. btrfs_item_ptr_offset(leaf, path->slots[0]),
  6555. sizeof(cache->item));
  6556. memcpy(&cache->key, &found_key, sizeof(found_key));
  6557. key.objectid = found_key.objectid + found_key.offset;
  6558. btrfs_release_path(root, path);
  6559. cache->flags = btrfs_block_group_flags(&cache->item);
  6560. cache->sectorsize = root->sectorsize;
  6561. /*
  6562. * check for two cases, either we are full, and therefore
  6563. * don't need to bother with the caching work since we won't
  6564. * find any space, or we are empty, and we can just add all
  6565. * the space in and be done with it. This saves us _alot_ of
  6566. * time, particularly in the full case.
  6567. */
  6568. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  6569. exclude_super_stripes(root, cache);
  6570. cache->last_byte_to_unpin = (u64)-1;
  6571. cache->cached = BTRFS_CACHE_FINISHED;
  6572. free_excluded_extents(root, cache);
  6573. } else if (btrfs_block_group_used(&cache->item) == 0) {
  6574. exclude_super_stripes(root, cache);
  6575. cache->last_byte_to_unpin = (u64)-1;
  6576. cache->cached = BTRFS_CACHE_FINISHED;
  6577. add_new_free_space(cache, root->fs_info,
  6578. found_key.objectid,
  6579. found_key.objectid +
  6580. found_key.offset);
  6581. free_excluded_extents(root, cache);
  6582. }
  6583. ret = update_space_info(info, cache->flags, found_key.offset,
  6584. btrfs_block_group_used(&cache->item),
  6585. &space_info);
  6586. BUG_ON(ret);
  6587. cache->space_info = space_info;
  6588. spin_lock(&cache->space_info->lock);
  6589. cache->space_info->bytes_super += cache->bytes_super;
  6590. spin_unlock(&cache->space_info->lock);
  6591. down_write(&space_info->groups_sem);
  6592. list_add_tail(&cache->list, &space_info->block_groups);
  6593. up_write(&space_info->groups_sem);
  6594. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6595. BUG_ON(ret);
  6596. set_avail_alloc_bits(root->fs_info, cache->flags);
  6597. if (btrfs_chunk_readonly(root, cache->key.objectid))
  6598. set_block_group_readonly(cache);
  6599. }
  6600. ret = 0;
  6601. error:
  6602. btrfs_free_path(path);
  6603. return ret;
  6604. }
  6605. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  6606. struct btrfs_root *root, u64 bytes_used,
  6607. u64 type, u64 chunk_objectid, u64 chunk_offset,
  6608. u64 size)
  6609. {
  6610. int ret;
  6611. struct btrfs_root *extent_root;
  6612. struct btrfs_block_group_cache *cache;
  6613. extent_root = root->fs_info->extent_root;
  6614. root->fs_info->last_trans_log_full_commit = trans->transid;
  6615. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6616. if (!cache)
  6617. return -ENOMEM;
  6618. cache->key.objectid = chunk_offset;
  6619. cache->key.offset = size;
  6620. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  6621. cache->sectorsize = root->sectorsize;
  6622. /*
  6623. * we only want to have 32k of ram per block group for keeping track
  6624. * of free space, and if we pass 1/2 of that we want to start
  6625. * converting things over to using bitmaps
  6626. */
  6627. cache->extents_thresh = ((1024 * 32) / 2) /
  6628. sizeof(struct btrfs_free_space);
  6629. atomic_set(&cache->count, 1);
  6630. spin_lock_init(&cache->lock);
  6631. spin_lock_init(&cache->tree_lock);
  6632. INIT_LIST_HEAD(&cache->list);
  6633. INIT_LIST_HEAD(&cache->cluster_list);
  6634. btrfs_set_block_group_used(&cache->item, bytes_used);
  6635. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  6636. cache->flags = type;
  6637. btrfs_set_block_group_flags(&cache->item, type);
  6638. cache->last_byte_to_unpin = (u64)-1;
  6639. cache->cached = BTRFS_CACHE_FINISHED;
  6640. exclude_super_stripes(root, cache);
  6641. add_new_free_space(cache, root->fs_info, chunk_offset,
  6642. chunk_offset + size);
  6643. free_excluded_extents(root, cache);
  6644. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  6645. &cache->space_info);
  6646. BUG_ON(ret);
  6647. spin_lock(&cache->space_info->lock);
  6648. cache->space_info->bytes_super += cache->bytes_super;
  6649. spin_unlock(&cache->space_info->lock);
  6650. down_write(&cache->space_info->groups_sem);
  6651. list_add_tail(&cache->list, &cache->space_info->block_groups);
  6652. up_write(&cache->space_info->groups_sem);
  6653. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6654. BUG_ON(ret);
  6655. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  6656. sizeof(cache->item));
  6657. BUG_ON(ret);
  6658. set_avail_alloc_bits(extent_root->fs_info, type);
  6659. return 0;
  6660. }
  6661. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  6662. struct btrfs_root *root, u64 group_start)
  6663. {
  6664. struct btrfs_path *path;
  6665. struct btrfs_block_group_cache *block_group;
  6666. struct btrfs_free_cluster *cluster;
  6667. struct btrfs_key key;
  6668. int ret;
  6669. root = root->fs_info->extent_root;
  6670. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  6671. BUG_ON(!block_group);
  6672. BUG_ON(!block_group->ro);
  6673. memcpy(&key, &block_group->key, sizeof(key));
  6674. /* make sure this block group isn't part of an allocation cluster */
  6675. cluster = &root->fs_info->data_alloc_cluster;
  6676. spin_lock(&cluster->refill_lock);
  6677. btrfs_return_cluster_to_free_space(block_group, cluster);
  6678. spin_unlock(&cluster->refill_lock);
  6679. /*
  6680. * make sure this block group isn't part of a metadata
  6681. * allocation cluster
  6682. */
  6683. cluster = &root->fs_info->meta_alloc_cluster;
  6684. spin_lock(&cluster->refill_lock);
  6685. btrfs_return_cluster_to_free_space(block_group, cluster);
  6686. spin_unlock(&cluster->refill_lock);
  6687. path = btrfs_alloc_path();
  6688. BUG_ON(!path);
  6689. spin_lock(&root->fs_info->block_group_cache_lock);
  6690. rb_erase(&block_group->cache_node,
  6691. &root->fs_info->block_group_cache_tree);
  6692. spin_unlock(&root->fs_info->block_group_cache_lock);
  6693. down_write(&block_group->space_info->groups_sem);
  6694. /*
  6695. * we must use list_del_init so people can check to see if they
  6696. * are still on the list after taking the semaphore
  6697. */
  6698. list_del_init(&block_group->list);
  6699. up_write(&block_group->space_info->groups_sem);
  6700. if (block_group->cached == BTRFS_CACHE_STARTED)
  6701. wait_block_group_cache_done(block_group);
  6702. btrfs_remove_free_space_cache(block_group);
  6703. spin_lock(&block_group->space_info->lock);
  6704. block_group->space_info->total_bytes -= block_group->key.offset;
  6705. block_group->space_info->bytes_readonly -= block_group->key.offset;
  6706. spin_unlock(&block_group->space_info->lock);
  6707. btrfs_clear_space_info_full(root->fs_info);
  6708. btrfs_put_block_group(block_group);
  6709. btrfs_put_block_group(block_group);
  6710. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  6711. if (ret > 0)
  6712. ret = -EIO;
  6713. if (ret < 0)
  6714. goto out;
  6715. ret = btrfs_del_item(trans, root, path);
  6716. out:
  6717. btrfs_free_path(path);
  6718. return ret;
  6719. }