extent-tree.c 191 KB

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