extent-tree.c 192 KB

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