extent-tree.c 186 KB

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