extent-tree.c 201 KB

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