extent-tree.c 187 KB

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