extent-tree.c 194 KB

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