extent-tree.c 201 KB

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