extent-tree.c 234 KB

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