volumes.c 199 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/bio.h>
  7. #include <linux/slab.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/ratelimit.h>
  11. #include <linux/kthread.h>
  12. #include <linux/raid/pq.h>
  13. #include <linux/semaphore.h>
  14. #include <linux/uuid.h>
  15. #include <linux/list_sort.h>
  16. #include "ctree.h"
  17. #include "extent_map.h"
  18. #include "disk-io.h"
  19. #include "transaction.h"
  20. #include "print-tree.h"
  21. #include "volumes.h"
  22. #include "raid56.h"
  23. #include "async-thread.h"
  24. #include "check-integrity.h"
  25. #include "rcu-string.h"
  26. #include "math.h"
  27. #include "dev-replace.h"
  28. #include "sysfs.h"
  29. const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
  30. [BTRFS_RAID_RAID10] = {
  31. .sub_stripes = 2,
  32. .dev_stripes = 1,
  33. .devs_max = 0, /* 0 == as many as possible */
  34. .devs_min = 4,
  35. .tolerated_failures = 1,
  36. .devs_increment = 2,
  37. .ncopies = 2,
  38. .raid_name = "raid10",
  39. .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
  40. .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
  41. },
  42. [BTRFS_RAID_RAID1] = {
  43. .sub_stripes = 1,
  44. .dev_stripes = 1,
  45. .devs_max = 2,
  46. .devs_min = 2,
  47. .tolerated_failures = 1,
  48. .devs_increment = 2,
  49. .ncopies = 2,
  50. .raid_name = "raid1",
  51. .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
  52. .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
  53. },
  54. [BTRFS_RAID_DUP] = {
  55. .sub_stripes = 1,
  56. .dev_stripes = 2,
  57. .devs_max = 1,
  58. .devs_min = 1,
  59. .tolerated_failures = 0,
  60. .devs_increment = 1,
  61. .ncopies = 2,
  62. .raid_name = "dup",
  63. .bg_flag = BTRFS_BLOCK_GROUP_DUP,
  64. .mindev_error = 0,
  65. },
  66. [BTRFS_RAID_RAID0] = {
  67. .sub_stripes = 1,
  68. .dev_stripes = 1,
  69. .devs_max = 0,
  70. .devs_min = 2,
  71. .tolerated_failures = 0,
  72. .devs_increment = 1,
  73. .ncopies = 1,
  74. .raid_name = "raid0",
  75. .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
  76. .mindev_error = 0,
  77. },
  78. [BTRFS_RAID_SINGLE] = {
  79. .sub_stripes = 1,
  80. .dev_stripes = 1,
  81. .devs_max = 1,
  82. .devs_min = 1,
  83. .tolerated_failures = 0,
  84. .devs_increment = 1,
  85. .ncopies = 1,
  86. .raid_name = "single",
  87. .bg_flag = 0,
  88. .mindev_error = 0,
  89. },
  90. [BTRFS_RAID_RAID5] = {
  91. .sub_stripes = 1,
  92. .dev_stripes = 1,
  93. .devs_max = 0,
  94. .devs_min = 2,
  95. .tolerated_failures = 1,
  96. .devs_increment = 1,
  97. .ncopies = 2,
  98. .raid_name = "raid5",
  99. .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
  100. .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
  101. },
  102. [BTRFS_RAID_RAID6] = {
  103. .sub_stripes = 1,
  104. .dev_stripes = 1,
  105. .devs_max = 0,
  106. .devs_min = 3,
  107. .tolerated_failures = 2,
  108. .devs_increment = 1,
  109. .ncopies = 3,
  110. .raid_name = "raid6",
  111. .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
  112. .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
  113. },
  114. };
  115. const char *get_raid_name(enum btrfs_raid_types type)
  116. {
  117. if (type >= BTRFS_NR_RAID_TYPES)
  118. return NULL;
  119. return btrfs_raid_array[type].raid_name;
  120. }
  121. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  122. struct btrfs_fs_info *fs_info);
  123. static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
  124. static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
  125. static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
  126. static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
  127. static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
  128. enum btrfs_map_op op,
  129. u64 logical, u64 *length,
  130. struct btrfs_bio **bbio_ret,
  131. int mirror_num, int need_raid_map);
  132. /*
  133. * Device locking
  134. * ==============
  135. *
  136. * There are several mutexes that protect manipulation of devices and low-level
  137. * structures like chunks but not block groups, extents or files
  138. *
  139. * uuid_mutex (global lock)
  140. * ------------------------
  141. * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
  142. * the SCAN_DEV ioctl registration or from mount either implicitly (the first
  143. * device) or requested by the device= mount option
  144. *
  145. * the mutex can be very coarse and can cover long-running operations
  146. *
  147. * protects: updates to fs_devices counters like missing devices, rw devices,
  148. * seeding, structure cloning, openning/closing devices at mount/umount time
  149. *
  150. * global::fs_devs - add, remove, updates to the global list
  151. *
  152. * does not protect: manipulation of the fs_devices::devices list!
  153. *
  154. * btrfs_device::name - renames (write side), read is RCU
  155. *
  156. * fs_devices::device_list_mutex (per-fs, with RCU)
  157. * ------------------------------------------------
  158. * protects updates to fs_devices::devices, ie. adding and deleting
  159. *
  160. * simple list traversal with read-only actions can be done with RCU protection
  161. *
  162. * may be used to exclude some operations from running concurrently without any
  163. * modifications to the list (see write_all_supers)
  164. *
  165. * balance_mutex
  166. * -------------
  167. * protects balance structures (status, state) and context accessed from
  168. * several places (internally, ioctl)
  169. *
  170. * chunk_mutex
  171. * -----------
  172. * protects chunks, adding or removing during allocation, trim or when a new
  173. * device is added/removed
  174. *
  175. * cleaner_mutex
  176. * -------------
  177. * a big lock that is held by the cleaner thread and prevents running subvolume
  178. * cleaning together with relocation or delayed iputs
  179. *
  180. *
  181. * Lock nesting
  182. * ============
  183. *
  184. * uuid_mutex
  185. * volume_mutex
  186. * device_list_mutex
  187. * chunk_mutex
  188. * balance_mutex
  189. *
  190. *
  191. * Exclusive operations, BTRFS_FS_EXCL_OP
  192. * ======================================
  193. *
  194. * Maintains the exclusivity of the following operations that apply to the
  195. * whole filesystem and cannot run in parallel.
  196. *
  197. * - Balance (*)
  198. * - Device add
  199. * - Device remove
  200. * - Device replace (*)
  201. * - Resize
  202. *
  203. * The device operations (as above) can be in one of the following states:
  204. *
  205. * - Running state
  206. * - Paused state
  207. * - Completed state
  208. *
  209. * Only device operations marked with (*) can go into the Paused state for the
  210. * following reasons:
  211. *
  212. * - ioctl (only Balance can be Paused through ioctl)
  213. * - filesystem remounted as read-only
  214. * - filesystem unmounted and mounted as read-only
  215. * - system power-cycle and filesystem mounted as read-only
  216. * - filesystem or device errors leading to forced read-only
  217. *
  218. * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
  219. * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
  220. * A device operation in Paused or Running state can be canceled or resumed
  221. * either by ioctl (Balance only) or when remounted as read-write.
  222. * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
  223. * completed.
  224. */
  225. DEFINE_MUTEX(uuid_mutex);
  226. static LIST_HEAD(fs_uuids);
  227. struct list_head *btrfs_get_fs_uuids(void)
  228. {
  229. return &fs_uuids;
  230. }
  231. /*
  232. * alloc_fs_devices - allocate struct btrfs_fs_devices
  233. * @fsid: if not NULL, copy the uuid to fs_devices::fsid
  234. *
  235. * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
  236. * The returned struct is not linked onto any lists and can be destroyed with
  237. * kfree() right away.
  238. */
  239. static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
  240. {
  241. struct btrfs_fs_devices *fs_devs;
  242. fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
  243. if (!fs_devs)
  244. return ERR_PTR(-ENOMEM);
  245. mutex_init(&fs_devs->device_list_mutex);
  246. INIT_LIST_HEAD(&fs_devs->devices);
  247. INIT_LIST_HEAD(&fs_devs->resized_devices);
  248. INIT_LIST_HEAD(&fs_devs->alloc_list);
  249. INIT_LIST_HEAD(&fs_devs->fs_list);
  250. if (fsid)
  251. memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
  252. return fs_devs;
  253. }
  254. void btrfs_free_device(struct btrfs_device *device)
  255. {
  256. rcu_string_free(device->name);
  257. bio_put(device->flush_bio);
  258. kfree(device);
  259. }
  260. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  261. {
  262. struct btrfs_device *device;
  263. WARN_ON(fs_devices->opened);
  264. while (!list_empty(&fs_devices->devices)) {
  265. device = list_entry(fs_devices->devices.next,
  266. struct btrfs_device, dev_list);
  267. list_del(&device->dev_list);
  268. btrfs_free_device(device);
  269. }
  270. kfree(fs_devices);
  271. }
  272. static void btrfs_kobject_uevent(struct block_device *bdev,
  273. enum kobject_action action)
  274. {
  275. int ret;
  276. ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
  277. if (ret)
  278. pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
  279. action,
  280. kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
  281. &disk_to_dev(bdev->bd_disk)->kobj);
  282. }
  283. void __exit btrfs_cleanup_fs_uuids(void)
  284. {
  285. struct btrfs_fs_devices *fs_devices;
  286. while (!list_empty(&fs_uuids)) {
  287. fs_devices = list_entry(fs_uuids.next,
  288. struct btrfs_fs_devices, fs_list);
  289. list_del(&fs_devices->fs_list);
  290. free_fs_devices(fs_devices);
  291. }
  292. }
  293. /*
  294. * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
  295. * Returned struct is not linked onto any lists and must be destroyed using
  296. * btrfs_free_device.
  297. */
  298. static struct btrfs_device *__alloc_device(void)
  299. {
  300. struct btrfs_device *dev;
  301. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  302. if (!dev)
  303. return ERR_PTR(-ENOMEM);
  304. /*
  305. * Preallocate a bio that's always going to be used for flushing device
  306. * barriers and matches the device lifespan
  307. */
  308. dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
  309. if (!dev->flush_bio) {
  310. kfree(dev);
  311. return ERR_PTR(-ENOMEM);
  312. }
  313. INIT_LIST_HEAD(&dev->dev_list);
  314. INIT_LIST_HEAD(&dev->dev_alloc_list);
  315. INIT_LIST_HEAD(&dev->resized_list);
  316. spin_lock_init(&dev->io_lock);
  317. atomic_set(&dev->reada_in_flight, 0);
  318. atomic_set(&dev->dev_stats_ccnt, 0);
  319. btrfs_device_data_ordered_init(dev);
  320. INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
  321. INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
  322. return dev;
  323. }
  324. /*
  325. * Find a device specified by @devid or @uuid in the list of @fs_devices, or
  326. * return NULL.
  327. *
  328. * If devid and uuid are both specified, the match must be exact, otherwise
  329. * only devid is used.
  330. */
  331. static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
  332. u64 devid, const u8 *uuid)
  333. {
  334. struct btrfs_device *dev;
  335. list_for_each_entry(dev, &fs_devices->devices, dev_list) {
  336. if (dev->devid == devid &&
  337. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  338. return dev;
  339. }
  340. }
  341. return NULL;
  342. }
  343. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  344. {
  345. struct btrfs_fs_devices *fs_devices;
  346. list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
  347. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  348. return fs_devices;
  349. }
  350. return NULL;
  351. }
  352. static int
  353. btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
  354. int flush, struct block_device **bdev,
  355. struct buffer_head **bh)
  356. {
  357. int ret;
  358. *bdev = blkdev_get_by_path(device_path, flags, holder);
  359. if (IS_ERR(*bdev)) {
  360. ret = PTR_ERR(*bdev);
  361. goto error;
  362. }
  363. if (flush)
  364. filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
  365. ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
  366. if (ret) {
  367. blkdev_put(*bdev, flags);
  368. goto error;
  369. }
  370. invalidate_bdev(*bdev);
  371. *bh = btrfs_read_dev_super(*bdev);
  372. if (IS_ERR(*bh)) {
  373. ret = PTR_ERR(*bh);
  374. blkdev_put(*bdev, flags);
  375. goto error;
  376. }
  377. return 0;
  378. error:
  379. *bdev = NULL;
  380. *bh = NULL;
  381. return ret;
  382. }
  383. static void requeue_list(struct btrfs_pending_bios *pending_bios,
  384. struct bio *head, struct bio *tail)
  385. {
  386. struct bio *old_head;
  387. old_head = pending_bios->head;
  388. pending_bios->head = head;
  389. if (pending_bios->tail)
  390. tail->bi_next = old_head;
  391. else
  392. pending_bios->tail = tail;
  393. }
  394. /*
  395. * we try to collect pending bios for a device so we don't get a large
  396. * number of procs sending bios down to the same device. This greatly
  397. * improves the schedulers ability to collect and merge the bios.
  398. *
  399. * But, it also turns into a long list of bios to process and that is sure
  400. * to eventually make the worker thread block. The solution here is to
  401. * make some progress and then put this work struct back at the end of
  402. * the list if the block device is congested. This way, multiple devices
  403. * can make progress from a single worker thread.
  404. */
  405. static noinline void run_scheduled_bios(struct btrfs_device *device)
  406. {
  407. struct btrfs_fs_info *fs_info = device->fs_info;
  408. struct bio *pending;
  409. struct backing_dev_info *bdi;
  410. struct btrfs_pending_bios *pending_bios;
  411. struct bio *tail;
  412. struct bio *cur;
  413. int again = 0;
  414. unsigned long num_run;
  415. unsigned long batch_run = 0;
  416. unsigned long last_waited = 0;
  417. int force_reg = 0;
  418. int sync_pending = 0;
  419. struct blk_plug plug;
  420. /*
  421. * this function runs all the bios we've collected for
  422. * a particular device. We don't want to wander off to
  423. * another device without first sending all of these down.
  424. * So, setup a plug here and finish it off before we return
  425. */
  426. blk_start_plug(&plug);
  427. bdi = device->bdev->bd_bdi;
  428. loop:
  429. spin_lock(&device->io_lock);
  430. loop_lock:
  431. num_run = 0;
  432. /* take all the bios off the list at once and process them
  433. * later on (without the lock held). But, remember the
  434. * tail and other pointers so the bios can be properly reinserted
  435. * into the list if we hit congestion
  436. */
  437. if (!force_reg && device->pending_sync_bios.head) {
  438. pending_bios = &device->pending_sync_bios;
  439. force_reg = 1;
  440. } else {
  441. pending_bios = &device->pending_bios;
  442. force_reg = 0;
  443. }
  444. pending = pending_bios->head;
  445. tail = pending_bios->tail;
  446. WARN_ON(pending && !tail);
  447. /*
  448. * if pending was null this time around, no bios need processing
  449. * at all and we can stop. Otherwise it'll loop back up again
  450. * and do an additional check so no bios are missed.
  451. *
  452. * device->running_pending is used to synchronize with the
  453. * schedule_bio code.
  454. */
  455. if (device->pending_sync_bios.head == NULL &&
  456. device->pending_bios.head == NULL) {
  457. again = 0;
  458. device->running_pending = 0;
  459. } else {
  460. again = 1;
  461. device->running_pending = 1;
  462. }
  463. pending_bios->head = NULL;
  464. pending_bios->tail = NULL;
  465. spin_unlock(&device->io_lock);
  466. while (pending) {
  467. rmb();
  468. /* we want to work on both lists, but do more bios on the
  469. * sync list than the regular list
  470. */
  471. if ((num_run > 32 &&
  472. pending_bios != &device->pending_sync_bios &&
  473. device->pending_sync_bios.head) ||
  474. (num_run > 64 && pending_bios == &device->pending_sync_bios &&
  475. device->pending_bios.head)) {
  476. spin_lock(&device->io_lock);
  477. requeue_list(pending_bios, pending, tail);
  478. goto loop_lock;
  479. }
  480. cur = pending;
  481. pending = pending->bi_next;
  482. cur->bi_next = NULL;
  483. BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
  484. /*
  485. * if we're doing the sync list, record that our
  486. * plug has some sync requests on it
  487. *
  488. * If we're doing the regular list and there are
  489. * sync requests sitting around, unplug before
  490. * we add more
  491. */
  492. if (pending_bios == &device->pending_sync_bios) {
  493. sync_pending = 1;
  494. } else if (sync_pending) {
  495. blk_finish_plug(&plug);
  496. blk_start_plug(&plug);
  497. sync_pending = 0;
  498. }
  499. btrfsic_submit_bio(cur);
  500. num_run++;
  501. batch_run++;
  502. cond_resched();
  503. /*
  504. * we made progress, there is more work to do and the bdi
  505. * is now congested. Back off and let other work structs
  506. * run instead
  507. */
  508. if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
  509. fs_info->fs_devices->open_devices > 1) {
  510. struct io_context *ioc;
  511. ioc = current->io_context;
  512. /*
  513. * the main goal here is that we don't want to
  514. * block if we're going to be able to submit
  515. * more requests without blocking.
  516. *
  517. * This code does two great things, it pokes into
  518. * the elevator code from a filesystem _and_
  519. * it makes assumptions about how batching works.
  520. */
  521. if (ioc && ioc->nr_batch_requests > 0 &&
  522. time_before(jiffies, ioc->last_waited + HZ/50UL) &&
  523. (last_waited == 0 ||
  524. ioc->last_waited == last_waited)) {
  525. /*
  526. * we want to go through our batch of
  527. * requests and stop. So, we copy out
  528. * the ioc->last_waited time and test
  529. * against it before looping
  530. */
  531. last_waited = ioc->last_waited;
  532. cond_resched();
  533. continue;
  534. }
  535. spin_lock(&device->io_lock);
  536. requeue_list(pending_bios, pending, tail);
  537. device->running_pending = 1;
  538. spin_unlock(&device->io_lock);
  539. btrfs_queue_work(fs_info->submit_workers,
  540. &device->work);
  541. goto done;
  542. }
  543. }
  544. cond_resched();
  545. if (again)
  546. goto loop;
  547. spin_lock(&device->io_lock);
  548. if (device->pending_bios.head || device->pending_sync_bios.head)
  549. goto loop_lock;
  550. spin_unlock(&device->io_lock);
  551. done:
  552. blk_finish_plug(&plug);
  553. }
  554. static void pending_bios_fn(struct btrfs_work *work)
  555. {
  556. struct btrfs_device *device;
  557. device = container_of(work, struct btrfs_device, work);
  558. run_scheduled_bios(device);
  559. }
  560. /*
  561. * Search and remove all stale (devices which are not mounted) devices.
  562. * When both inputs are NULL, it will search and release all stale devices.
  563. * path: Optional. When provided will it release all unmounted devices
  564. * matching this path only.
  565. * skip_dev: Optional. Will skip this device when searching for the stale
  566. * devices.
  567. */
  568. static void btrfs_free_stale_devices(const char *path,
  569. struct btrfs_device *skip_device)
  570. {
  571. struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
  572. struct btrfs_device *device, *tmp_device;
  573. list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
  574. mutex_lock(&fs_devices->device_list_mutex);
  575. if (fs_devices->opened) {
  576. mutex_unlock(&fs_devices->device_list_mutex);
  577. continue;
  578. }
  579. list_for_each_entry_safe(device, tmp_device,
  580. &fs_devices->devices, dev_list) {
  581. int not_found = 0;
  582. if (skip_device && skip_device == device)
  583. continue;
  584. if (path && !device->name)
  585. continue;
  586. rcu_read_lock();
  587. if (path)
  588. not_found = strcmp(rcu_str_deref(device->name),
  589. path);
  590. rcu_read_unlock();
  591. if (not_found)
  592. continue;
  593. /* delete the stale device */
  594. fs_devices->num_devices--;
  595. list_del(&device->dev_list);
  596. btrfs_free_device(device);
  597. if (fs_devices->num_devices == 0)
  598. break;
  599. }
  600. mutex_unlock(&fs_devices->device_list_mutex);
  601. if (fs_devices->num_devices == 0) {
  602. btrfs_sysfs_remove_fsid(fs_devices);
  603. list_del(&fs_devices->fs_list);
  604. free_fs_devices(fs_devices);
  605. }
  606. }
  607. }
  608. static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
  609. struct btrfs_device *device, fmode_t flags,
  610. void *holder)
  611. {
  612. struct request_queue *q;
  613. struct block_device *bdev;
  614. struct buffer_head *bh;
  615. struct btrfs_super_block *disk_super;
  616. u64 devid;
  617. int ret;
  618. if (device->bdev)
  619. return -EINVAL;
  620. if (!device->name)
  621. return -EINVAL;
  622. ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
  623. &bdev, &bh);
  624. if (ret)
  625. return ret;
  626. disk_super = (struct btrfs_super_block *)bh->b_data;
  627. devid = btrfs_stack_device_id(&disk_super->dev_item);
  628. if (devid != device->devid)
  629. goto error_brelse;
  630. if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
  631. goto error_brelse;
  632. device->generation = btrfs_super_generation(disk_super);
  633. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  634. clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  635. fs_devices->seeding = 1;
  636. } else {
  637. if (bdev_read_only(bdev))
  638. clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  639. else
  640. set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  641. }
  642. q = bdev_get_queue(bdev);
  643. if (!blk_queue_nonrot(q))
  644. fs_devices->rotating = 1;
  645. device->bdev = bdev;
  646. clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
  647. device->mode = flags;
  648. fs_devices->open_devices++;
  649. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
  650. device->devid != BTRFS_DEV_REPLACE_DEVID) {
  651. fs_devices->rw_devices++;
  652. list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
  653. }
  654. brelse(bh);
  655. return 0;
  656. error_brelse:
  657. brelse(bh);
  658. blkdev_put(bdev, flags);
  659. return -EINVAL;
  660. }
  661. /*
  662. * Add new device to list of registered devices
  663. *
  664. * Returns:
  665. * device pointer which was just added or updated when successful
  666. * error pointer when failed
  667. */
  668. static noinline struct btrfs_device *device_list_add(const char *path,
  669. struct btrfs_super_block *disk_super,
  670. bool *new_device_added)
  671. {
  672. struct btrfs_device *device;
  673. struct btrfs_fs_devices *fs_devices;
  674. struct rcu_string *name;
  675. u64 found_transid = btrfs_super_generation(disk_super);
  676. u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
  677. fs_devices = find_fsid(disk_super->fsid);
  678. if (!fs_devices) {
  679. fs_devices = alloc_fs_devices(disk_super->fsid);
  680. if (IS_ERR(fs_devices))
  681. return ERR_CAST(fs_devices);
  682. mutex_lock(&fs_devices->device_list_mutex);
  683. list_add(&fs_devices->fs_list, &fs_uuids);
  684. device = NULL;
  685. } else {
  686. mutex_lock(&fs_devices->device_list_mutex);
  687. device = find_device(fs_devices, devid,
  688. disk_super->dev_item.uuid);
  689. }
  690. if (!device) {
  691. if (fs_devices->opened) {
  692. mutex_unlock(&fs_devices->device_list_mutex);
  693. return ERR_PTR(-EBUSY);
  694. }
  695. device = btrfs_alloc_device(NULL, &devid,
  696. disk_super->dev_item.uuid);
  697. if (IS_ERR(device)) {
  698. mutex_unlock(&fs_devices->device_list_mutex);
  699. /* we can safely leave the fs_devices entry around */
  700. return device;
  701. }
  702. name = rcu_string_strdup(path, GFP_NOFS);
  703. if (!name) {
  704. btrfs_free_device(device);
  705. mutex_unlock(&fs_devices->device_list_mutex);
  706. return ERR_PTR(-ENOMEM);
  707. }
  708. rcu_assign_pointer(device->name, name);
  709. list_add_rcu(&device->dev_list, &fs_devices->devices);
  710. fs_devices->num_devices++;
  711. device->fs_devices = fs_devices;
  712. *new_device_added = true;
  713. if (disk_super->label[0])
  714. pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
  715. disk_super->label, devid, found_transid, path);
  716. else
  717. pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
  718. disk_super->fsid, devid, found_transid, path);
  719. } else if (!device->name || strcmp(device->name->str, path)) {
  720. /*
  721. * When FS is already mounted.
  722. * 1. If you are here and if the device->name is NULL that
  723. * means this device was missing at time of FS mount.
  724. * 2. If you are here and if the device->name is different
  725. * from 'path' that means either
  726. * a. The same device disappeared and reappeared with
  727. * different name. or
  728. * b. The missing-disk-which-was-replaced, has
  729. * reappeared now.
  730. *
  731. * We must allow 1 and 2a above. But 2b would be a spurious
  732. * and unintentional.
  733. *
  734. * Further in case of 1 and 2a above, the disk at 'path'
  735. * would have missed some transaction when it was away and
  736. * in case of 2a the stale bdev has to be updated as well.
  737. * 2b must not be allowed at all time.
  738. */
  739. /*
  740. * For now, we do allow update to btrfs_fs_device through the
  741. * btrfs dev scan cli after FS has been mounted. We're still
  742. * tracking a problem where systems fail mount by subvolume id
  743. * when we reject replacement on a mounted FS.
  744. */
  745. if (!fs_devices->opened && found_transid < device->generation) {
  746. /*
  747. * That is if the FS is _not_ mounted and if you
  748. * are here, that means there is more than one
  749. * disk with same uuid and devid.We keep the one
  750. * with larger generation number or the last-in if
  751. * generation are equal.
  752. */
  753. mutex_unlock(&fs_devices->device_list_mutex);
  754. return ERR_PTR(-EEXIST);
  755. }
  756. /*
  757. * We are going to replace the device path for a given devid,
  758. * make sure it's the same device if the device is mounted
  759. */
  760. if (device->bdev) {
  761. struct block_device *path_bdev;
  762. path_bdev = lookup_bdev(path);
  763. if (IS_ERR(path_bdev)) {
  764. mutex_unlock(&fs_devices->device_list_mutex);
  765. return ERR_CAST(path_bdev);
  766. }
  767. if (device->bdev != path_bdev) {
  768. bdput(path_bdev);
  769. mutex_unlock(&fs_devices->device_list_mutex);
  770. btrfs_warn_in_rcu(device->fs_info,
  771. "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
  772. disk_super->fsid, devid,
  773. rcu_str_deref(device->name), path);
  774. return ERR_PTR(-EEXIST);
  775. }
  776. bdput(path_bdev);
  777. btrfs_info_in_rcu(device->fs_info,
  778. "device fsid %pU devid %llu moved old:%s new:%s",
  779. disk_super->fsid, devid,
  780. rcu_str_deref(device->name), path);
  781. }
  782. name = rcu_string_strdup(path, GFP_NOFS);
  783. if (!name) {
  784. mutex_unlock(&fs_devices->device_list_mutex);
  785. return ERR_PTR(-ENOMEM);
  786. }
  787. rcu_string_free(device->name);
  788. rcu_assign_pointer(device->name, name);
  789. if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
  790. fs_devices->missing_devices--;
  791. clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
  792. }
  793. }
  794. /*
  795. * Unmount does not free the btrfs_device struct but would zero
  796. * generation along with most of the other members. So just update
  797. * it back. We need it to pick the disk with largest generation
  798. * (as above).
  799. */
  800. if (!fs_devices->opened)
  801. device->generation = found_transid;
  802. fs_devices->total_devices = btrfs_super_num_devices(disk_super);
  803. mutex_unlock(&fs_devices->device_list_mutex);
  804. return device;
  805. }
  806. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  807. {
  808. struct btrfs_fs_devices *fs_devices;
  809. struct btrfs_device *device;
  810. struct btrfs_device *orig_dev;
  811. fs_devices = alloc_fs_devices(orig->fsid);
  812. if (IS_ERR(fs_devices))
  813. return fs_devices;
  814. mutex_lock(&orig->device_list_mutex);
  815. fs_devices->total_devices = orig->total_devices;
  816. /* We have held the volume lock, it is safe to get the devices. */
  817. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  818. struct rcu_string *name;
  819. device = btrfs_alloc_device(NULL, &orig_dev->devid,
  820. orig_dev->uuid);
  821. if (IS_ERR(device))
  822. goto error;
  823. /*
  824. * This is ok to do without rcu read locked because we hold the
  825. * uuid mutex so nothing we touch in here is going to disappear.
  826. */
  827. if (orig_dev->name) {
  828. name = rcu_string_strdup(orig_dev->name->str,
  829. GFP_KERNEL);
  830. if (!name) {
  831. btrfs_free_device(device);
  832. goto error;
  833. }
  834. rcu_assign_pointer(device->name, name);
  835. }
  836. list_add(&device->dev_list, &fs_devices->devices);
  837. device->fs_devices = fs_devices;
  838. fs_devices->num_devices++;
  839. }
  840. mutex_unlock(&orig->device_list_mutex);
  841. return fs_devices;
  842. error:
  843. mutex_unlock(&orig->device_list_mutex);
  844. free_fs_devices(fs_devices);
  845. return ERR_PTR(-ENOMEM);
  846. }
  847. /*
  848. * After we have read the system tree and know devids belonging to
  849. * this filesystem, remove the device which does not belong there.
  850. */
  851. void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
  852. {
  853. struct btrfs_device *device, *next;
  854. struct btrfs_device *latest_dev = NULL;
  855. mutex_lock(&uuid_mutex);
  856. again:
  857. /* This is the initialized path, it is safe to release the devices. */
  858. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  859. if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
  860. &device->dev_state)) {
  861. if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
  862. &device->dev_state) &&
  863. (!latest_dev ||
  864. device->generation > latest_dev->generation)) {
  865. latest_dev = device;
  866. }
  867. continue;
  868. }
  869. if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
  870. /*
  871. * In the first step, keep the device which has
  872. * the correct fsid and the devid that is used
  873. * for the dev_replace procedure.
  874. * In the second step, the dev_replace state is
  875. * read from the device tree and it is known
  876. * whether the procedure is really active or
  877. * not, which means whether this device is
  878. * used or whether it should be removed.
  879. */
  880. if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
  881. &device->dev_state)) {
  882. continue;
  883. }
  884. }
  885. if (device->bdev) {
  886. blkdev_put(device->bdev, device->mode);
  887. device->bdev = NULL;
  888. fs_devices->open_devices--;
  889. }
  890. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  891. list_del_init(&device->dev_alloc_list);
  892. clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  893. if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
  894. &device->dev_state))
  895. fs_devices->rw_devices--;
  896. }
  897. list_del_init(&device->dev_list);
  898. fs_devices->num_devices--;
  899. btrfs_free_device(device);
  900. }
  901. if (fs_devices->seed) {
  902. fs_devices = fs_devices->seed;
  903. goto again;
  904. }
  905. fs_devices->latest_bdev = latest_dev->bdev;
  906. mutex_unlock(&uuid_mutex);
  907. }
  908. static void free_device_rcu(struct rcu_head *head)
  909. {
  910. struct btrfs_device *device;
  911. device = container_of(head, struct btrfs_device, rcu);
  912. btrfs_free_device(device);
  913. }
  914. static void btrfs_close_bdev(struct btrfs_device *device)
  915. {
  916. if (!device->bdev)
  917. return;
  918. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  919. sync_blockdev(device->bdev);
  920. invalidate_bdev(device->bdev);
  921. }
  922. blkdev_put(device->bdev, device->mode);
  923. }
  924. static void btrfs_close_one_device(struct btrfs_device *device)
  925. {
  926. struct btrfs_fs_devices *fs_devices = device->fs_devices;
  927. struct btrfs_device *new_device;
  928. struct rcu_string *name;
  929. if (device->bdev)
  930. fs_devices->open_devices--;
  931. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
  932. device->devid != BTRFS_DEV_REPLACE_DEVID) {
  933. list_del_init(&device->dev_alloc_list);
  934. fs_devices->rw_devices--;
  935. }
  936. if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
  937. fs_devices->missing_devices--;
  938. btrfs_close_bdev(device);
  939. new_device = btrfs_alloc_device(NULL, &device->devid,
  940. device->uuid);
  941. BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
  942. /* Safe because we are under uuid_mutex */
  943. if (device->name) {
  944. name = rcu_string_strdup(device->name->str, GFP_NOFS);
  945. BUG_ON(!name); /* -ENOMEM */
  946. rcu_assign_pointer(new_device->name, name);
  947. }
  948. list_replace_rcu(&device->dev_list, &new_device->dev_list);
  949. new_device->fs_devices = device->fs_devices;
  950. call_rcu(&device->rcu, free_device_rcu);
  951. }
  952. static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
  953. {
  954. struct btrfs_device *device, *tmp;
  955. if (--fs_devices->opened > 0)
  956. return 0;
  957. mutex_lock(&fs_devices->device_list_mutex);
  958. list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
  959. btrfs_close_one_device(device);
  960. }
  961. mutex_unlock(&fs_devices->device_list_mutex);
  962. WARN_ON(fs_devices->open_devices);
  963. WARN_ON(fs_devices->rw_devices);
  964. fs_devices->opened = 0;
  965. fs_devices->seeding = 0;
  966. return 0;
  967. }
  968. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  969. {
  970. struct btrfs_fs_devices *seed_devices = NULL;
  971. int ret;
  972. mutex_lock(&uuid_mutex);
  973. ret = close_fs_devices(fs_devices);
  974. if (!fs_devices->opened) {
  975. seed_devices = fs_devices->seed;
  976. fs_devices->seed = NULL;
  977. }
  978. mutex_unlock(&uuid_mutex);
  979. while (seed_devices) {
  980. fs_devices = seed_devices;
  981. seed_devices = fs_devices->seed;
  982. close_fs_devices(fs_devices);
  983. free_fs_devices(fs_devices);
  984. }
  985. return ret;
  986. }
  987. static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
  988. fmode_t flags, void *holder)
  989. {
  990. struct btrfs_device *device;
  991. struct btrfs_device *latest_dev = NULL;
  992. int ret = 0;
  993. flags |= FMODE_EXCL;
  994. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  995. /* Just open everything we can; ignore failures here */
  996. if (btrfs_open_one_device(fs_devices, device, flags, holder))
  997. continue;
  998. if (!latest_dev ||
  999. device->generation > latest_dev->generation)
  1000. latest_dev = device;
  1001. }
  1002. if (fs_devices->open_devices == 0) {
  1003. ret = -EINVAL;
  1004. goto out;
  1005. }
  1006. fs_devices->opened = 1;
  1007. fs_devices->latest_bdev = latest_dev->bdev;
  1008. fs_devices->total_rw_bytes = 0;
  1009. out:
  1010. return ret;
  1011. }
  1012. static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
  1013. {
  1014. struct btrfs_device *dev1, *dev2;
  1015. dev1 = list_entry(a, struct btrfs_device, dev_list);
  1016. dev2 = list_entry(b, struct btrfs_device, dev_list);
  1017. if (dev1->devid < dev2->devid)
  1018. return -1;
  1019. else if (dev1->devid > dev2->devid)
  1020. return 1;
  1021. return 0;
  1022. }
  1023. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  1024. fmode_t flags, void *holder)
  1025. {
  1026. int ret;
  1027. lockdep_assert_held(&uuid_mutex);
  1028. mutex_lock(&fs_devices->device_list_mutex);
  1029. if (fs_devices->opened) {
  1030. fs_devices->opened++;
  1031. ret = 0;
  1032. } else {
  1033. list_sort(NULL, &fs_devices->devices, devid_cmp);
  1034. ret = open_fs_devices(fs_devices, flags, holder);
  1035. }
  1036. mutex_unlock(&fs_devices->device_list_mutex);
  1037. return ret;
  1038. }
  1039. static void btrfs_release_disk_super(struct page *page)
  1040. {
  1041. kunmap(page);
  1042. put_page(page);
  1043. }
  1044. static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
  1045. struct page **page,
  1046. struct btrfs_super_block **disk_super)
  1047. {
  1048. void *p;
  1049. pgoff_t index;
  1050. /* make sure our super fits in the device */
  1051. if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
  1052. return 1;
  1053. /* make sure our super fits in the page */
  1054. if (sizeof(**disk_super) > PAGE_SIZE)
  1055. return 1;
  1056. /* make sure our super doesn't straddle pages on disk */
  1057. index = bytenr >> PAGE_SHIFT;
  1058. if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
  1059. return 1;
  1060. /* pull in the page with our super */
  1061. *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
  1062. index, GFP_KERNEL);
  1063. if (IS_ERR_OR_NULL(*page))
  1064. return 1;
  1065. p = kmap(*page);
  1066. /* align our pointer to the offset of the super block */
  1067. *disk_super = p + (bytenr & ~PAGE_MASK);
  1068. if (btrfs_super_bytenr(*disk_super) != bytenr ||
  1069. btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
  1070. btrfs_release_disk_super(*page);
  1071. return 1;
  1072. }
  1073. if ((*disk_super)->label[0] &&
  1074. (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
  1075. (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
  1076. return 0;
  1077. }
  1078. /*
  1079. * Look for a btrfs signature on a device. This may be called out of the mount path
  1080. * and we are not allowed to call set_blocksize during the scan. The superblock
  1081. * is read via pagecache
  1082. */
  1083. struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
  1084. void *holder)
  1085. {
  1086. struct btrfs_super_block *disk_super;
  1087. bool new_device_added = false;
  1088. struct btrfs_device *device = NULL;
  1089. struct block_device *bdev;
  1090. struct page *page;
  1091. u64 bytenr;
  1092. lockdep_assert_held(&uuid_mutex);
  1093. /*
  1094. * we would like to check all the supers, but that would make
  1095. * a btrfs mount succeed after a mkfs from a different FS.
  1096. * So, we need to add a special mount option to scan for
  1097. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  1098. */
  1099. bytenr = btrfs_sb_offset(0);
  1100. flags |= FMODE_EXCL;
  1101. bdev = blkdev_get_by_path(path, flags, holder);
  1102. if (IS_ERR(bdev))
  1103. return ERR_CAST(bdev);
  1104. if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
  1105. device = ERR_PTR(-EINVAL);
  1106. goto error_bdev_put;
  1107. }
  1108. device = device_list_add(path, disk_super, &new_device_added);
  1109. if (!IS_ERR(device)) {
  1110. if (new_device_added)
  1111. btrfs_free_stale_devices(path, device);
  1112. }
  1113. btrfs_release_disk_super(page);
  1114. error_bdev_put:
  1115. blkdev_put(bdev, flags);
  1116. return device;
  1117. }
  1118. static int contains_pending_extent(struct btrfs_transaction *transaction,
  1119. struct btrfs_device *device,
  1120. u64 *start, u64 len)
  1121. {
  1122. struct btrfs_fs_info *fs_info = device->fs_info;
  1123. struct extent_map *em;
  1124. struct list_head *search_list = &fs_info->pinned_chunks;
  1125. int ret = 0;
  1126. u64 physical_start = *start;
  1127. if (transaction)
  1128. search_list = &transaction->pending_chunks;
  1129. again:
  1130. list_for_each_entry(em, search_list, list) {
  1131. struct map_lookup *map;
  1132. int i;
  1133. map = em->map_lookup;
  1134. for (i = 0; i < map->num_stripes; i++) {
  1135. u64 end;
  1136. if (map->stripes[i].dev != device)
  1137. continue;
  1138. if (map->stripes[i].physical >= physical_start + len ||
  1139. map->stripes[i].physical + em->orig_block_len <=
  1140. physical_start)
  1141. continue;
  1142. /*
  1143. * Make sure that while processing the pinned list we do
  1144. * not override our *start with a lower value, because
  1145. * we can have pinned chunks that fall within this
  1146. * device hole and that have lower physical addresses
  1147. * than the pending chunks we processed before. If we
  1148. * do not take this special care we can end up getting
  1149. * 2 pending chunks that start at the same physical
  1150. * device offsets because the end offset of a pinned
  1151. * chunk can be equal to the start offset of some
  1152. * pending chunk.
  1153. */
  1154. end = map->stripes[i].physical + em->orig_block_len;
  1155. if (end > *start) {
  1156. *start = end;
  1157. ret = 1;
  1158. }
  1159. }
  1160. }
  1161. if (search_list != &fs_info->pinned_chunks) {
  1162. search_list = &fs_info->pinned_chunks;
  1163. goto again;
  1164. }
  1165. return ret;
  1166. }
  1167. /*
  1168. * find_free_dev_extent_start - find free space in the specified device
  1169. * @device: the device which we search the free space in
  1170. * @num_bytes: the size of the free space that we need
  1171. * @search_start: the position from which to begin the search
  1172. * @start: store the start of the free space.
  1173. * @len: the size of the free space. that we find, or the size
  1174. * of the max free space if we don't find suitable free space
  1175. *
  1176. * this uses a pretty simple search, the expectation is that it is
  1177. * called very infrequently and that a given device has a small number
  1178. * of extents
  1179. *
  1180. * @start is used to store the start of the free space if we find. But if we
  1181. * don't find suitable free space, it will be used to store the start position
  1182. * of the max free space.
  1183. *
  1184. * @len is used to store the size of the free space that we find.
  1185. * But if we don't find suitable free space, it is used to store the size of
  1186. * the max free space.
  1187. */
  1188. int find_free_dev_extent_start(struct btrfs_transaction *transaction,
  1189. struct btrfs_device *device, u64 num_bytes,
  1190. u64 search_start, u64 *start, u64 *len)
  1191. {
  1192. struct btrfs_fs_info *fs_info = device->fs_info;
  1193. struct btrfs_root *root = fs_info->dev_root;
  1194. struct btrfs_key key;
  1195. struct btrfs_dev_extent *dev_extent;
  1196. struct btrfs_path *path;
  1197. u64 hole_size;
  1198. u64 max_hole_start;
  1199. u64 max_hole_size;
  1200. u64 extent_end;
  1201. u64 search_end = device->total_bytes;
  1202. int ret;
  1203. int slot;
  1204. struct extent_buffer *l;
  1205. /*
  1206. * We don't want to overwrite the superblock on the drive nor any area
  1207. * used by the boot loader (grub for example), so we make sure to start
  1208. * at an offset of at least 1MB.
  1209. */
  1210. search_start = max_t(u64, search_start, SZ_1M);
  1211. path = btrfs_alloc_path();
  1212. if (!path)
  1213. return -ENOMEM;
  1214. max_hole_start = search_start;
  1215. max_hole_size = 0;
  1216. again:
  1217. if (search_start >= search_end ||
  1218. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  1219. ret = -ENOSPC;
  1220. goto out;
  1221. }
  1222. path->reada = READA_FORWARD;
  1223. path->search_commit_root = 1;
  1224. path->skip_locking = 1;
  1225. key.objectid = device->devid;
  1226. key.offset = search_start;
  1227. key.type = BTRFS_DEV_EXTENT_KEY;
  1228. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1229. if (ret < 0)
  1230. goto out;
  1231. if (ret > 0) {
  1232. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  1233. if (ret < 0)
  1234. goto out;
  1235. }
  1236. while (1) {
  1237. l = path->nodes[0];
  1238. slot = path->slots[0];
  1239. if (slot >= btrfs_header_nritems(l)) {
  1240. ret = btrfs_next_leaf(root, path);
  1241. if (ret == 0)
  1242. continue;
  1243. if (ret < 0)
  1244. goto out;
  1245. break;
  1246. }
  1247. btrfs_item_key_to_cpu(l, &key, slot);
  1248. if (key.objectid < device->devid)
  1249. goto next;
  1250. if (key.objectid > device->devid)
  1251. break;
  1252. if (key.type != BTRFS_DEV_EXTENT_KEY)
  1253. goto next;
  1254. if (key.offset > search_start) {
  1255. hole_size = key.offset - search_start;
  1256. /*
  1257. * Have to check before we set max_hole_start, otherwise
  1258. * we could end up sending back this offset anyway.
  1259. */
  1260. if (contains_pending_extent(transaction, device,
  1261. &search_start,
  1262. hole_size)) {
  1263. if (key.offset >= search_start) {
  1264. hole_size = key.offset - search_start;
  1265. } else {
  1266. WARN_ON_ONCE(1);
  1267. hole_size = 0;
  1268. }
  1269. }
  1270. if (hole_size > max_hole_size) {
  1271. max_hole_start = search_start;
  1272. max_hole_size = hole_size;
  1273. }
  1274. /*
  1275. * If this free space is greater than which we need,
  1276. * it must be the max free space that we have found
  1277. * until now, so max_hole_start must point to the start
  1278. * of this free space and the length of this free space
  1279. * is stored in max_hole_size. Thus, we return
  1280. * max_hole_start and max_hole_size and go back to the
  1281. * caller.
  1282. */
  1283. if (hole_size >= num_bytes) {
  1284. ret = 0;
  1285. goto out;
  1286. }
  1287. }
  1288. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1289. extent_end = key.offset + btrfs_dev_extent_length(l,
  1290. dev_extent);
  1291. if (extent_end > search_start)
  1292. search_start = extent_end;
  1293. next:
  1294. path->slots[0]++;
  1295. cond_resched();
  1296. }
  1297. /*
  1298. * At this point, search_start should be the end of
  1299. * allocated dev extents, and when shrinking the device,
  1300. * search_end may be smaller than search_start.
  1301. */
  1302. if (search_end > search_start) {
  1303. hole_size = search_end - search_start;
  1304. if (contains_pending_extent(transaction, device, &search_start,
  1305. hole_size)) {
  1306. btrfs_release_path(path);
  1307. goto again;
  1308. }
  1309. if (hole_size > max_hole_size) {
  1310. max_hole_start = search_start;
  1311. max_hole_size = hole_size;
  1312. }
  1313. }
  1314. /* See above. */
  1315. if (max_hole_size < num_bytes)
  1316. ret = -ENOSPC;
  1317. else
  1318. ret = 0;
  1319. out:
  1320. btrfs_free_path(path);
  1321. *start = max_hole_start;
  1322. if (len)
  1323. *len = max_hole_size;
  1324. return ret;
  1325. }
  1326. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  1327. struct btrfs_device *device, u64 num_bytes,
  1328. u64 *start, u64 *len)
  1329. {
  1330. /* FIXME use last free of some kind */
  1331. return find_free_dev_extent_start(trans->transaction, device,
  1332. num_bytes, 0, start, len);
  1333. }
  1334. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  1335. struct btrfs_device *device,
  1336. u64 start, u64 *dev_extent_len)
  1337. {
  1338. struct btrfs_fs_info *fs_info = device->fs_info;
  1339. struct btrfs_root *root = fs_info->dev_root;
  1340. int ret;
  1341. struct btrfs_path *path;
  1342. struct btrfs_key key;
  1343. struct btrfs_key found_key;
  1344. struct extent_buffer *leaf = NULL;
  1345. struct btrfs_dev_extent *extent = NULL;
  1346. path = btrfs_alloc_path();
  1347. if (!path)
  1348. return -ENOMEM;
  1349. key.objectid = device->devid;
  1350. key.offset = start;
  1351. key.type = BTRFS_DEV_EXTENT_KEY;
  1352. again:
  1353. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1354. if (ret > 0) {
  1355. ret = btrfs_previous_item(root, path, key.objectid,
  1356. BTRFS_DEV_EXTENT_KEY);
  1357. if (ret)
  1358. goto out;
  1359. leaf = path->nodes[0];
  1360. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1361. extent = btrfs_item_ptr(leaf, path->slots[0],
  1362. struct btrfs_dev_extent);
  1363. BUG_ON(found_key.offset > start || found_key.offset +
  1364. btrfs_dev_extent_length(leaf, extent) < start);
  1365. key = found_key;
  1366. btrfs_release_path(path);
  1367. goto again;
  1368. } else if (ret == 0) {
  1369. leaf = path->nodes[0];
  1370. extent = btrfs_item_ptr(leaf, path->slots[0],
  1371. struct btrfs_dev_extent);
  1372. } else {
  1373. btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
  1374. goto out;
  1375. }
  1376. *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
  1377. ret = btrfs_del_item(trans, root, path);
  1378. if (ret) {
  1379. btrfs_handle_fs_error(fs_info, ret,
  1380. "Failed to remove dev extent item");
  1381. } else {
  1382. set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
  1383. }
  1384. out:
  1385. btrfs_free_path(path);
  1386. return ret;
  1387. }
  1388. static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  1389. struct btrfs_device *device,
  1390. u64 chunk_offset, u64 start, u64 num_bytes)
  1391. {
  1392. int ret;
  1393. struct btrfs_path *path;
  1394. struct btrfs_fs_info *fs_info = device->fs_info;
  1395. struct btrfs_root *root = fs_info->dev_root;
  1396. struct btrfs_dev_extent *extent;
  1397. struct extent_buffer *leaf;
  1398. struct btrfs_key key;
  1399. WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
  1400. WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
  1401. path = btrfs_alloc_path();
  1402. if (!path)
  1403. return -ENOMEM;
  1404. key.objectid = device->devid;
  1405. key.offset = start;
  1406. key.type = BTRFS_DEV_EXTENT_KEY;
  1407. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1408. sizeof(*extent));
  1409. if (ret)
  1410. goto out;
  1411. leaf = path->nodes[0];
  1412. extent = btrfs_item_ptr(leaf, path->slots[0],
  1413. struct btrfs_dev_extent);
  1414. btrfs_set_dev_extent_chunk_tree(leaf, extent,
  1415. BTRFS_CHUNK_TREE_OBJECTID);
  1416. btrfs_set_dev_extent_chunk_objectid(leaf, extent,
  1417. BTRFS_FIRST_CHUNK_TREE_OBJECTID);
  1418. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  1419. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  1420. btrfs_mark_buffer_dirty(leaf);
  1421. out:
  1422. btrfs_free_path(path);
  1423. return ret;
  1424. }
  1425. static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
  1426. {
  1427. struct extent_map_tree *em_tree;
  1428. struct extent_map *em;
  1429. struct rb_node *n;
  1430. u64 ret = 0;
  1431. em_tree = &fs_info->mapping_tree.map_tree;
  1432. read_lock(&em_tree->lock);
  1433. n = rb_last(&em_tree->map);
  1434. if (n) {
  1435. em = rb_entry(n, struct extent_map, rb_node);
  1436. ret = em->start + em->len;
  1437. }
  1438. read_unlock(&em_tree->lock);
  1439. return ret;
  1440. }
  1441. static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
  1442. u64 *devid_ret)
  1443. {
  1444. int ret;
  1445. struct btrfs_key key;
  1446. struct btrfs_key found_key;
  1447. struct btrfs_path *path;
  1448. path = btrfs_alloc_path();
  1449. if (!path)
  1450. return -ENOMEM;
  1451. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1452. key.type = BTRFS_DEV_ITEM_KEY;
  1453. key.offset = (u64)-1;
  1454. ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
  1455. if (ret < 0)
  1456. goto error;
  1457. BUG_ON(ret == 0); /* Corruption */
  1458. ret = btrfs_previous_item(fs_info->chunk_root, path,
  1459. BTRFS_DEV_ITEMS_OBJECTID,
  1460. BTRFS_DEV_ITEM_KEY);
  1461. if (ret) {
  1462. *devid_ret = 1;
  1463. } else {
  1464. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1465. path->slots[0]);
  1466. *devid_ret = found_key.offset + 1;
  1467. }
  1468. ret = 0;
  1469. error:
  1470. btrfs_free_path(path);
  1471. return ret;
  1472. }
  1473. /*
  1474. * the device information is stored in the chunk root
  1475. * the btrfs_device struct should be fully filled in
  1476. */
  1477. static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
  1478. struct btrfs_device *device)
  1479. {
  1480. int ret;
  1481. struct btrfs_path *path;
  1482. struct btrfs_dev_item *dev_item;
  1483. struct extent_buffer *leaf;
  1484. struct btrfs_key key;
  1485. unsigned long ptr;
  1486. path = btrfs_alloc_path();
  1487. if (!path)
  1488. return -ENOMEM;
  1489. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1490. key.type = BTRFS_DEV_ITEM_KEY;
  1491. key.offset = device->devid;
  1492. ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
  1493. &key, sizeof(*dev_item));
  1494. if (ret)
  1495. goto out;
  1496. leaf = path->nodes[0];
  1497. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1498. btrfs_set_device_id(leaf, dev_item, device->devid);
  1499. btrfs_set_device_generation(leaf, dev_item, 0);
  1500. btrfs_set_device_type(leaf, dev_item, device->type);
  1501. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1502. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1503. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1504. btrfs_set_device_total_bytes(leaf, dev_item,
  1505. btrfs_device_get_disk_total_bytes(device));
  1506. btrfs_set_device_bytes_used(leaf, dev_item,
  1507. btrfs_device_get_bytes_used(device));
  1508. btrfs_set_device_group(leaf, dev_item, 0);
  1509. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1510. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1511. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1512. ptr = btrfs_device_uuid(dev_item);
  1513. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1514. ptr = btrfs_device_fsid(dev_item);
  1515. write_extent_buffer(leaf, trans->fs_info->fsid, ptr, BTRFS_FSID_SIZE);
  1516. btrfs_mark_buffer_dirty(leaf);
  1517. ret = 0;
  1518. out:
  1519. btrfs_free_path(path);
  1520. return ret;
  1521. }
  1522. /*
  1523. * Function to update ctime/mtime for a given device path.
  1524. * Mainly used for ctime/mtime based probe like libblkid.
  1525. */
  1526. static void update_dev_time(const char *path_name)
  1527. {
  1528. struct file *filp;
  1529. filp = filp_open(path_name, O_RDWR, 0);
  1530. if (IS_ERR(filp))
  1531. return;
  1532. file_update_time(filp);
  1533. filp_close(filp, NULL);
  1534. }
  1535. static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
  1536. struct btrfs_device *device)
  1537. {
  1538. struct btrfs_root *root = fs_info->chunk_root;
  1539. int ret;
  1540. struct btrfs_path *path;
  1541. struct btrfs_key key;
  1542. struct btrfs_trans_handle *trans;
  1543. path = btrfs_alloc_path();
  1544. if (!path)
  1545. return -ENOMEM;
  1546. trans = btrfs_start_transaction(root, 0);
  1547. if (IS_ERR(trans)) {
  1548. btrfs_free_path(path);
  1549. return PTR_ERR(trans);
  1550. }
  1551. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1552. key.type = BTRFS_DEV_ITEM_KEY;
  1553. key.offset = device->devid;
  1554. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1555. if (ret) {
  1556. if (ret > 0)
  1557. ret = -ENOENT;
  1558. btrfs_abort_transaction(trans, ret);
  1559. btrfs_end_transaction(trans);
  1560. goto out;
  1561. }
  1562. ret = btrfs_del_item(trans, root, path);
  1563. if (ret) {
  1564. btrfs_abort_transaction(trans, ret);
  1565. btrfs_end_transaction(trans);
  1566. }
  1567. out:
  1568. btrfs_free_path(path);
  1569. if (!ret)
  1570. ret = btrfs_commit_transaction(trans);
  1571. return ret;
  1572. }
  1573. /*
  1574. * Verify that @num_devices satisfies the RAID profile constraints in the whole
  1575. * filesystem. It's up to the caller to adjust that number regarding eg. device
  1576. * replace.
  1577. */
  1578. static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
  1579. u64 num_devices)
  1580. {
  1581. u64 all_avail;
  1582. unsigned seq;
  1583. int i;
  1584. do {
  1585. seq = read_seqbegin(&fs_info->profiles_lock);
  1586. all_avail = fs_info->avail_data_alloc_bits |
  1587. fs_info->avail_system_alloc_bits |
  1588. fs_info->avail_metadata_alloc_bits;
  1589. } while (read_seqretry(&fs_info->profiles_lock, seq));
  1590. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1591. if (!(all_avail & btrfs_raid_array[i].bg_flag))
  1592. continue;
  1593. if (num_devices < btrfs_raid_array[i].devs_min) {
  1594. int ret = btrfs_raid_array[i].mindev_error;
  1595. if (ret)
  1596. return ret;
  1597. }
  1598. }
  1599. return 0;
  1600. }
  1601. static struct btrfs_device * btrfs_find_next_active_device(
  1602. struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
  1603. {
  1604. struct btrfs_device *next_device;
  1605. list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
  1606. if (next_device != device &&
  1607. !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
  1608. && next_device->bdev)
  1609. return next_device;
  1610. }
  1611. return NULL;
  1612. }
  1613. /*
  1614. * Helper function to check if the given device is part of s_bdev / latest_bdev
  1615. * and replace it with the provided or the next active device, in the context
  1616. * where this function called, there should be always be another device (or
  1617. * this_dev) which is active.
  1618. */
  1619. void btrfs_assign_next_active_device(struct btrfs_device *device,
  1620. struct btrfs_device *this_dev)
  1621. {
  1622. struct btrfs_fs_info *fs_info = device->fs_info;
  1623. struct btrfs_device *next_device;
  1624. if (this_dev)
  1625. next_device = this_dev;
  1626. else
  1627. next_device = btrfs_find_next_active_device(fs_info->fs_devices,
  1628. device);
  1629. ASSERT(next_device);
  1630. if (fs_info->sb->s_bdev &&
  1631. (fs_info->sb->s_bdev == device->bdev))
  1632. fs_info->sb->s_bdev = next_device->bdev;
  1633. if (fs_info->fs_devices->latest_bdev == device->bdev)
  1634. fs_info->fs_devices->latest_bdev = next_device->bdev;
  1635. }
  1636. int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
  1637. u64 devid)
  1638. {
  1639. struct btrfs_device *device;
  1640. struct btrfs_fs_devices *cur_devices;
  1641. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1642. u64 num_devices;
  1643. int ret = 0;
  1644. mutex_lock(&uuid_mutex);
  1645. num_devices = fs_devices->num_devices;
  1646. btrfs_dev_replace_read_lock(&fs_info->dev_replace);
  1647. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  1648. WARN_ON(num_devices < 1);
  1649. num_devices--;
  1650. }
  1651. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  1652. ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
  1653. if (ret)
  1654. goto out;
  1655. ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
  1656. &device);
  1657. if (ret)
  1658. goto out;
  1659. if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  1660. ret = BTRFS_ERROR_DEV_TGT_REPLACE;
  1661. goto out;
  1662. }
  1663. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
  1664. fs_info->fs_devices->rw_devices == 1) {
  1665. ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
  1666. goto out;
  1667. }
  1668. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  1669. mutex_lock(&fs_info->chunk_mutex);
  1670. list_del_init(&device->dev_alloc_list);
  1671. device->fs_devices->rw_devices--;
  1672. mutex_unlock(&fs_info->chunk_mutex);
  1673. }
  1674. mutex_unlock(&uuid_mutex);
  1675. ret = btrfs_shrink_device(device, 0);
  1676. mutex_lock(&uuid_mutex);
  1677. if (ret)
  1678. goto error_undo;
  1679. /*
  1680. * TODO: the superblock still includes this device in its num_devices
  1681. * counter although write_all_supers() is not locked out. This
  1682. * could give a filesystem state which requires a degraded mount.
  1683. */
  1684. ret = btrfs_rm_dev_item(fs_info, device);
  1685. if (ret)
  1686. goto error_undo;
  1687. clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
  1688. btrfs_scrub_cancel_dev(fs_info, device);
  1689. /*
  1690. * the device list mutex makes sure that we don't change
  1691. * the device list while someone else is writing out all
  1692. * the device supers. Whoever is writing all supers, should
  1693. * lock the device list mutex before getting the number of
  1694. * devices in the super block (super_copy). Conversely,
  1695. * whoever updates the number of devices in the super block
  1696. * (super_copy) should hold the device list mutex.
  1697. */
  1698. /*
  1699. * In normal cases the cur_devices == fs_devices. But in case
  1700. * of deleting a seed device, the cur_devices should point to
  1701. * its own fs_devices listed under the fs_devices->seed.
  1702. */
  1703. cur_devices = device->fs_devices;
  1704. mutex_lock(&fs_devices->device_list_mutex);
  1705. list_del_rcu(&device->dev_list);
  1706. cur_devices->num_devices--;
  1707. cur_devices->total_devices--;
  1708. /* Update total_devices of the parent fs_devices if it's seed */
  1709. if (cur_devices != fs_devices)
  1710. fs_devices->total_devices--;
  1711. if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
  1712. cur_devices->missing_devices--;
  1713. btrfs_assign_next_active_device(device, NULL);
  1714. if (device->bdev) {
  1715. cur_devices->open_devices--;
  1716. /* remove sysfs entry */
  1717. btrfs_sysfs_rm_device_link(fs_devices, device);
  1718. }
  1719. num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
  1720. btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
  1721. mutex_unlock(&fs_devices->device_list_mutex);
  1722. /*
  1723. * at this point, the device is zero sized and detached from
  1724. * the devices list. All that's left is to zero out the old
  1725. * supers and free the device.
  1726. */
  1727. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
  1728. btrfs_scratch_superblocks(device->bdev, device->name->str);
  1729. btrfs_close_bdev(device);
  1730. call_rcu(&device->rcu, free_device_rcu);
  1731. if (cur_devices->open_devices == 0) {
  1732. while (fs_devices) {
  1733. if (fs_devices->seed == cur_devices) {
  1734. fs_devices->seed = cur_devices->seed;
  1735. break;
  1736. }
  1737. fs_devices = fs_devices->seed;
  1738. }
  1739. cur_devices->seed = NULL;
  1740. close_fs_devices(cur_devices);
  1741. free_fs_devices(cur_devices);
  1742. }
  1743. out:
  1744. mutex_unlock(&uuid_mutex);
  1745. return ret;
  1746. error_undo:
  1747. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  1748. mutex_lock(&fs_info->chunk_mutex);
  1749. list_add(&device->dev_alloc_list,
  1750. &fs_devices->alloc_list);
  1751. device->fs_devices->rw_devices++;
  1752. mutex_unlock(&fs_info->chunk_mutex);
  1753. }
  1754. goto out;
  1755. }
  1756. void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
  1757. {
  1758. struct btrfs_fs_devices *fs_devices;
  1759. lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
  1760. /*
  1761. * in case of fs with no seed, srcdev->fs_devices will point
  1762. * to fs_devices of fs_info. However when the dev being replaced is
  1763. * a seed dev it will point to the seed's local fs_devices. In short
  1764. * srcdev will have its correct fs_devices in both the cases.
  1765. */
  1766. fs_devices = srcdev->fs_devices;
  1767. list_del_rcu(&srcdev->dev_list);
  1768. list_del(&srcdev->dev_alloc_list);
  1769. fs_devices->num_devices--;
  1770. if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
  1771. fs_devices->missing_devices--;
  1772. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
  1773. fs_devices->rw_devices--;
  1774. if (srcdev->bdev)
  1775. fs_devices->open_devices--;
  1776. }
  1777. void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
  1778. struct btrfs_device *srcdev)
  1779. {
  1780. struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
  1781. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
  1782. /* zero out the old super if it is writable */
  1783. btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
  1784. }
  1785. btrfs_close_bdev(srcdev);
  1786. call_rcu(&srcdev->rcu, free_device_rcu);
  1787. /* if this is no devs we rather delete the fs_devices */
  1788. if (!fs_devices->num_devices) {
  1789. struct btrfs_fs_devices *tmp_fs_devices;
  1790. /*
  1791. * On a mounted FS, num_devices can't be zero unless it's a
  1792. * seed. In case of a seed device being replaced, the replace
  1793. * target added to the sprout FS, so there will be no more
  1794. * device left under the seed FS.
  1795. */
  1796. ASSERT(fs_devices->seeding);
  1797. tmp_fs_devices = fs_info->fs_devices;
  1798. while (tmp_fs_devices) {
  1799. if (tmp_fs_devices->seed == fs_devices) {
  1800. tmp_fs_devices->seed = fs_devices->seed;
  1801. break;
  1802. }
  1803. tmp_fs_devices = tmp_fs_devices->seed;
  1804. }
  1805. fs_devices->seed = NULL;
  1806. close_fs_devices(fs_devices);
  1807. free_fs_devices(fs_devices);
  1808. }
  1809. }
  1810. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
  1811. {
  1812. struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
  1813. WARN_ON(!tgtdev);
  1814. mutex_lock(&fs_devices->device_list_mutex);
  1815. btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
  1816. if (tgtdev->bdev)
  1817. fs_devices->open_devices--;
  1818. fs_devices->num_devices--;
  1819. btrfs_assign_next_active_device(tgtdev, NULL);
  1820. list_del_rcu(&tgtdev->dev_list);
  1821. mutex_unlock(&fs_devices->device_list_mutex);
  1822. /*
  1823. * The update_dev_time() with in btrfs_scratch_superblocks()
  1824. * may lead to a call to btrfs_show_devname() which will try
  1825. * to hold device_list_mutex. And here this device
  1826. * is already out of device list, so we don't have to hold
  1827. * the device_list_mutex lock.
  1828. */
  1829. btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
  1830. btrfs_close_bdev(tgtdev);
  1831. call_rcu(&tgtdev->rcu, free_device_rcu);
  1832. }
  1833. static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
  1834. const char *device_path,
  1835. struct btrfs_device **device)
  1836. {
  1837. int ret = 0;
  1838. struct btrfs_super_block *disk_super;
  1839. u64 devid;
  1840. u8 *dev_uuid;
  1841. struct block_device *bdev;
  1842. struct buffer_head *bh;
  1843. *device = NULL;
  1844. ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
  1845. fs_info->bdev_holder, 0, &bdev, &bh);
  1846. if (ret)
  1847. return ret;
  1848. disk_super = (struct btrfs_super_block *)bh->b_data;
  1849. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1850. dev_uuid = disk_super->dev_item.uuid;
  1851. *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
  1852. brelse(bh);
  1853. if (!*device)
  1854. ret = -ENOENT;
  1855. blkdev_put(bdev, FMODE_READ);
  1856. return ret;
  1857. }
  1858. int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
  1859. const char *device_path,
  1860. struct btrfs_device **device)
  1861. {
  1862. *device = NULL;
  1863. if (strcmp(device_path, "missing") == 0) {
  1864. struct list_head *devices;
  1865. struct btrfs_device *tmp;
  1866. devices = &fs_info->fs_devices->devices;
  1867. list_for_each_entry(tmp, devices, dev_list) {
  1868. if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
  1869. &tmp->dev_state) && !tmp->bdev) {
  1870. *device = tmp;
  1871. break;
  1872. }
  1873. }
  1874. if (!*device)
  1875. return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
  1876. return 0;
  1877. } else {
  1878. return btrfs_find_device_by_path(fs_info, device_path, device);
  1879. }
  1880. }
  1881. /*
  1882. * Lookup a device given by device id, or the path if the id is 0.
  1883. */
  1884. int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
  1885. const char *devpath,
  1886. struct btrfs_device **device)
  1887. {
  1888. int ret;
  1889. if (devid) {
  1890. ret = 0;
  1891. *device = btrfs_find_device(fs_info, devid, NULL, NULL);
  1892. if (!*device)
  1893. ret = -ENOENT;
  1894. } else {
  1895. if (!devpath || !devpath[0])
  1896. return -EINVAL;
  1897. ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
  1898. device);
  1899. }
  1900. return ret;
  1901. }
  1902. /*
  1903. * does all the dirty work required for changing file system's UUID.
  1904. */
  1905. static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
  1906. {
  1907. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1908. struct btrfs_fs_devices *old_devices;
  1909. struct btrfs_fs_devices *seed_devices;
  1910. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1911. struct btrfs_device *device;
  1912. u64 super_flags;
  1913. lockdep_assert_held(&uuid_mutex);
  1914. if (!fs_devices->seeding)
  1915. return -EINVAL;
  1916. seed_devices = alloc_fs_devices(NULL);
  1917. if (IS_ERR(seed_devices))
  1918. return PTR_ERR(seed_devices);
  1919. old_devices = clone_fs_devices(fs_devices);
  1920. if (IS_ERR(old_devices)) {
  1921. kfree(seed_devices);
  1922. return PTR_ERR(old_devices);
  1923. }
  1924. list_add(&old_devices->fs_list, &fs_uuids);
  1925. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1926. seed_devices->opened = 1;
  1927. INIT_LIST_HEAD(&seed_devices->devices);
  1928. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1929. mutex_init(&seed_devices->device_list_mutex);
  1930. mutex_lock(&fs_devices->device_list_mutex);
  1931. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1932. synchronize_rcu);
  1933. list_for_each_entry(device, &seed_devices->devices, dev_list)
  1934. device->fs_devices = seed_devices;
  1935. mutex_lock(&fs_info->chunk_mutex);
  1936. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1937. mutex_unlock(&fs_info->chunk_mutex);
  1938. fs_devices->seeding = 0;
  1939. fs_devices->num_devices = 0;
  1940. fs_devices->open_devices = 0;
  1941. fs_devices->missing_devices = 0;
  1942. fs_devices->rotating = 0;
  1943. fs_devices->seed = seed_devices;
  1944. generate_random_uuid(fs_devices->fsid);
  1945. memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1946. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1947. mutex_unlock(&fs_devices->device_list_mutex);
  1948. super_flags = btrfs_super_flags(disk_super) &
  1949. ~BTRFS_SUPER_FLAG_SEEDING;
  1950. btrfs_set_super_flags(disk_super, super_flags);
  1951. return 0;
  1952. }
  1953. /*
  1954. * Store the expected generation for seed devices in device items.
  1955. */
  1956. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1957. struct btrfs_fs_info *fs_info)
  1958. {
  1959. struct btrfs_root *root = fs_info->chunk_root;
  1960. struct btrfs_path *path;
  1961. struct extent_buffer *leaf;
  1962. struct btrfs_dev_item *dev_item;
  1963. struct btrfs_device *device;
  1964. struct btrfs_key key;
  1965. u8 fs_uuid[BTRFS_FSID_SIZE];
  1966. u8 dev_uuid[BTRFS_UUID_SIZE];
  1967. u64 devid;
  1968. int ret;
  1969. path = btrfs_alloc_path();
  1970. if (!path)
  1971. return -ENOMEM;
  1972. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1973. key.offset = 0;
  1974. key.type = BTRFS_DEV_ITEM_KEY;
  1975. while (1) {
  1976. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1977. if (ret < 0)
  1978. goto error;
  1979. leaf = path->nodes[0];
  1980. next_slot:
  1981. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1982. ret = btrfs_next_leaf(root, path);
  1983. if (ret > 0)
  1984. break;
  1985. if (ret < 0)
  1986. goto error;
  1987. leaf = path->nodes[0];
  1988. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1989. btrfs_release_path(path);
  1990. continue;
  1991. }
  1992. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1993. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1994. key.type != BTRFS_DEV_ITEM_KEY)
  1995. break;
  1996. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1997. struct btrfs_dev_item);
  1998. devid = btrfs_device_id(leaf, dev_item);
  1999. read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
  2000. BTRFS_UUID_SIZE);
  2001. read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
  2002. BTRFS_FSID_SIZE);
  2003. device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
  2004. BUG_ON(!device); /* Logic error */
  2005. if (device->fs_devices->seeding) {
  2006. btrfs_set_device_generation(leaf, dev_item,
  2007. device->generation);
  2008. btrfs_mark_buffer_dirty(leaf);
  2009. }
  2010. path->slots[0]++;
  2011. goto next_slot;
  2012. }
  2013. ret = 0;
  2014. error:
  2015. btrfs_free_path(path);
  2016. return ret;
  2017. }
  2018. int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
  2019. {
  2020. struct btrfs_root *root = fs_info->dev_root;
  2021. struct request_queue *q;
  2022. struct btrfs_trans_handle *trans;
  2023. struct btrfs_device *device;
  2024. struct block_device *bdev;
  2025. struct super_block *sb = fs_info->sb;
  2026. struct rcu_string *name;
  2027. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2028. u64 orig_super_total_bytes;
  2029. u64 orig_super_num_devices;
  2030. int seeding_dev = 0;
  2031. int ret = 0;
  2032. bool unlocked = false;
  2033. if (sb_rdonly(sb) && !fs_devices->seeding)
  2034. return -EROFS;
  2035. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  2036. fs_info->bdev_holder);
  2037. if (IS_ERR(bdev))
  2038. return PTR_ERR(bdev);
  2039. if (fs_devices->seeding) {
  2040. seeding_dev = 1;
  2041. down_write(&sb->s_umount);
  2042. mutex_lock(&uuid_mutex);
  2043. }
  2044. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  2045. mutex_lock(&fs_devices->device_list_mutex);
  2046. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  2047. if (device->bdev == bdev) {
  2048. ret = -EEXIST;
  2049. mutex_unlock(
  2050. &fs_devices->device_list_mutex);
  2051. goto error;
  2052. }
  2053. }
  2054. mutex_unlock(&fs_devices->device_list_mutex);
  2055. device = btrfs_alloc_device(fs_info, NULL, NULL);
  2056. if (IS_ERR(device)) {
  2057. /* we can safely leave the fs_devices entry around */
  2058. ret = PTR_ERR(device);
  2059. goto error;
  2060. }
  2061. name = rcu_string_strdup(device_path, GFP_KERNEL);
  2062. if (!name) {
  2063. ret = -ENOMEM;
  2064. goto error_free_device;
  2065. }
  2066. rcu_assign_pointer(device->name, name);
  2067. trans = btrfs_start_transaction(root, 0);
  2068. if (IS_ERR(trans)) {
  2069. ret = PTR_ERR(trans);
  2070. goto error_free_device;
  2071. }
  2072. q = bdev_get_queue(bdev);
  2073. set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  2074. device->generation = trans->transid;
  2075. device->io_width = fs_info->sectorsize;
  2076. device->io_align = fs_info->sectorsize;
  2077. device->sector_size = fs_info->sectorsize;
  2078. device->total_bytes = round_down(i_size_read(bdev->bd_inode),
  2079. fs_info->sectorsize);
  2080. device->disk_total_bytes = device->total_bytes;
  2081. device->commit_total_bytes = device->total_bytes;
  2082. device->fs_info = fs_info;
  2083. device->bdev = bdev;
  2084. set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
  2085. clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
  2086. device->mode = FMODE_EXCL;
  2087. device->dev_stats_valid = 1;
  2088. set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
  2089. if (seeding_dev) {
  2090. sb->s_flags &= ~SB_RDONLY;
  2091. ret = btrfs_prepare_sprout(fs_info);
  2092. if (ret) {
  2093. btrfs_abort_transaction(trans, ret);
  2094. goto error_trans;
  2095. }
  2096. }
  2097. device->fs_devices = fs_devices;
  2098. mutex_lock(&fs_devices->device_list_mutex);
  2099. mutex_lock(&fs_info->chunk_mutex);
  2100. list_add_rcu(&device->dev_list, &fs_devices->devices);
  2101. list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
  2102. fs_devices->num_devices++;
  2103. fs_devices->open_devices++;
  2104. fs_devices->rw_devices++;
  2105. fs_devices->total_devices++;
  2106. fs_devices->total_rw_bytes += device->total_bytes;
  2107. atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
  2108. if (!blk_queue_nonrot(q))
  2109. fs_devices->rotating = 1;
  2110. orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  2111. btrfs_set_super_total_bytes(fs_info->super_copy,
  2112. round_down(orig_super_total_bytes + device->total_bytes,
  2113. fs_info->sectorsize));
  2114. orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
  2115. btrfs_set_super_num_devices(fs_info->super_copy,
  2116. orig_super_num_devices + 1);
  2117. /* add sysfs device entry */
  2118. btrfs_sysfs_add_device_link(fs_devices, device);
  2119. /*
  2120. * we've got more storage, clear any full flags on the space
  2121. * infos
  2122. */
  2123. btrfs_clear_space_info_full(fs_info);
  2124. mutex_unlock(&fs_info->chunk_mutex);
  2125. mutex_unlock(&fs_devices->device_list_mutex);
  2126. if (seeding_dev) {
  2127. mutex_lock(&fs_info->chunk_mutex);
  2128. ret = init_first_rw_device(trans, fs_info);
  2129. mutex_unlock(&fs_info->chunk_mutex);
  2130. if (ret) {
  2131. btrfs_abort_transaction(trans, ret);
  2132. goto error_sysfs;
  2133. }
  2134. }
  2135. ret = btrfs_add_dev_item(trans, device);
  2136. if (ret) {
  2137. btrfs_abort_transaction(trans, ret);
  2138. goto error_sysfs;
  2139. }
  2140. if (seeding_dev) {
  2141. char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
  2142. ret = btrfs_finish_sprout(trans, fs_info);
  2143. if (ret) {
  2144. btrfs_abort_transaction(trans, ret);
  2145. goto error_sysfs;
  2146. }
  2147. /* Sprouting would change fsid of the mounted root,
  2148. * so rename the fsid on the sysfs
  2149. */
  2150. snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
  2151. fs_info->fsid);
  2152. if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
  2153. btrfs_warn(fs_info,
  2154. "sysfs: failed to create fsid for sprout");
  2155. }
  2156. ret = btrfs_commit_transaction(trans);
  2157. if (seeding_dev) {
  2158. mutex_unlock(&uuid_mutex);
  2159. up_write(&sb->s_umount);
  2160. unlocked = true;
  2161. if (ret) /* transaction commit */
  2162. return ret;
  2163. ret = btrfs_relocate_sys_chunks(fs_info);
  2164. if (ret < 0)
  2165. btrfs_handle_fs_error(fs_info, ret,
  2166. "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
  2167. trans = btrfs_attach_transaction(root);
  2168. if (IS_ERR(trans)) {
  2169. if (PTR_ERR(trans) == -ENOENT)
  2170. return 0;
  2171. ret = PTR_ERR(trans);
  2172. trans = NULL;
  2173. goto error_sysfs;
  2174. }
  2175. ret = btrfs_commit_transaction(trans);
  2176. }
  2177. /* Update ctime/mtime for libblkid */
  2178. update_dev_time(device_path);
  2179. return ret;
  2180. error_sysfs:
  2181. btrfs_sysfs_rm_device_link(fs_devices, device);
  2182. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2183. mutex_lock(&fs_info->chunk_mutex);
  2184. list_del_rcu(&device->dev_list);
  2185. list_del(&device->dev_alloc_list);
  2186. fs_info->fs_devices->num_devices--;
  2187. fs_info->fs_devices->open_devices--;
  2188. fs_info->fs_devices->rw_devices--;
  2189. fs_info->fs_devices->total_devices--;
  2190. fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
  2191. atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
  2192. btrfs_set_super_total_bytes(fs_info->super_copy,
  2193. orig_super_total_bytes);
  2194. btrfs_set_super_num_devices(fs_info->super_copy,
  2195. orig_super_num_devices);
  2196. mutex_unlock(&fs_info->chunk_mutex);
  2197. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2198. error_trans:
  2199. if (seeding_dev)
  2200. sb->s_flags |= SB_RDONLY;
  2201. if (trans)
  2202. btrfs_end_transaction(trans);
  2203. error_free_device:
  2204. btrfs_free_device(device);
  2205. error:
  2206. blkdev_put(bdev, FMODE_EXCL);
  2207. if (seeding_dev && !unlocked) {
  2208. mutex_unlock(&uuid_mutex);
  2209. up_write(&sb->s_umount);
  2210. }
  2211. return ret;
  2212. }
  2213. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  2214. struct btrfs_device *device)
  2215. {
  2216. int ret;
  2217. struct btrfs_path *path;
  2218. struct btrfs_root *root = device->fs_info->chunk_root;
  2219. struct btrfs_dev_item *dev_item;
  2220. struct extent_buffer *leaf;
  2221. struct btrfs_key key;
  2222. path = btrfs_alloc_path();
  2223. if (!path)
  2224. return -ENOMEM;
  2225. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2226. key.type = BTRFS_DEV_ITEM_KEY;
  2227. key.offset = device->devid;
  2228. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  2229. if (ret < 0)
  2230. goto out;
  2231. if (ret > 0) {
  2232. ret = -ENOENT;
  2233. goto out;
  2234. }
  2235. leaf = path->nodes[0];
  2236. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  2237. btrfs_set_device_id(leaf, dev_item, device->devid);
  2238. btrfs_set_device_type(leaf, dev_item, device->type);
  2239. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  2240. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  2241. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  2242. btrfs_set_device_total_bytes(leaf, dev_item,
  2243. btrfs_device_get_disk_total_bytes(device));
  2244. btrfs_set_device_bytes_used(leaf, dev_item,
  2245. btrfs_device_get_bytes_used(device));
  2246. btrfs_mark_buffer_dirty(leaf);
  2247. out:
  2248. btrfs_free_path(path);
  2249. return ret;
  2250. }
  2251. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  2252. struct btrfs_device *device, u64 new_size)
  2253. {
  2254. struct btrfs_fs_info *fs_info = device->fs_info;
  2255. struct btrfs_super_block *super_copy = fs_info->super_copy;
  2256. struct btrfs_fs_devices *fs_devices;
  2257. u64 old_total;
  2258. u64 diff;
  2259. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
  2260. return -EACCES;
  2261. new_size = round_down(new_size, fs_info->sectorsize);
  2262. mutex_lock(&fs_info->chunk_mutex);
  2263. old_total = btrfs_super_total_bytes(super_copy);
  2264. diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
  2265. if (new_size <= device->total_bytes ||
  2266. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  2267. mutex_unlock(&fs_info->chunk_mutex);
  2268. return -EINVAL;
  2269. }
  2270. fs_devices = fs_info->fs_devices;
  2271. btrfs_set_super_total_bytes(super_copy,
  2272. round_down(old_total + diff, fs_info->sectorsize));
  2273. device->fs_devices->total_rw_bytes += diff;
  2274. btrfs_device_set_total_bytes(device, new_size);
  2275. btrfs_device_set_disk_total_bytes(device, new_size);
  2276. btrfs_clear_space_info_full(device->fs_info);
  2277. if (list_empty(&device->resized_list))
  2278. list_add_tail(&device->resized_list,
  2279. &fs_devices->resized_devices);
  2280. mutex_unlock(&fs_info->chunk_mutex);
  2281. return btrfs_update_device(trans, device);
  2282. }
  2283. static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
  2284. {
  2285. struct btrfs_fs_info *fs_info = trans->fs_info;
  2286. struct btrfs_root *root = fs_info->chunk_root;
  2287. int ret;
  2288. struct btrfs_path *path;
  2289. struct btrfs_key key;
  2290. path = btrfs_alloc_path();
  2291. if (!path)
  2292. return -ENOMEM;
  2293. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2294. key.offset = chunk_offset;
  2295. key.type = BTRFS_CHUNK_ITEM_KEY;
  2296. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2297. if (ret < 0)
  2298. goto out;
  2299. else if (ret > 0) { /* Logic error or corruption */
  2300. btrfs_handle_fs_error(fs_info, -ENOENT,
  2301. "Failed lookup while freeing chunk.");
  2302. ret = -ENOENT;
  2303. goto out;
  2304. }
  2305. ret = btrfs_del_item(trans, root, path);
  2306. if (ret < 0)
  2307. btrfs_handle_fs_error(fs_info, ret,
  2308. "Failed to delete chunk item.");
  2309. out:
  2310. btrfs_free_path(path);
  2311. return ret;
  2312. }
  2313. static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
  2314. {
  2315. struct btrfs_super_block *super_copy = fs_info->super_copy;
  2316. struct btrfs_disk_key *disk_key;
  2317. struct btrfs_chunk *chunk;
  2318. u8 *ptr;
  2319. int ret = 0;
  2320. u32 num_stripes;
  2321. u32 array_size;
  2322. u32 len = 0;
  2323. u32 cur;
  2324. struct btrfs_key key;
  2325. mutex_lock(&fs_info->chunk_mutex);
  2326. array_size = btrfs_super_sys_array_size(super_copy);
  2327. ptr = super_copy->sys_chunk_array;
  2328. cur = 0;
  2329. while (cur < array_size) {
  2330. disk_key = (struct btrfs_disk_key *)ptr;
  2331. btrfs_disk_key_to_cpu(&key, disk_key);
  2332. len = sizeof(*disk_key);
  2333. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2334. chunk = (struct btrfs_chunk *)(ptr + len);
  2335. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  2336. len += btrfs_chunk_item_size(num_stripes);
  2337. } else {
  2338. ret = -EIO;
  2339. break;
  2340. }
  2341. if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
  2342. key.offset == chunk_offset) {
  2343. memmove(ptr, ptr + len, array_size - (cur + len));
  2344. array_size -= len;
  2345. btrfs_set_super_sys_array_size(super_copy, array_size);
  2346. } else {
  2347. ptr += len;
  2348. cur += len;
  2349. }
  2350. }
  2351. mutex_unlock(&fs_info->chunk_mutex);
  2352. return ret;
  2353. }
  2354. static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
  2355. u64 logical, u64 length)
  2356. {
  2357. struct extent_map_tree *em_tree;
  2358. struct extent_map *em;
  2359. em_tree = &fs_info->mapping_tree.map_tree;
  2360. read_lock(&em_tree->lock);
  2361. em = lookup_extent_mapping(em_tree, logical, length);
  2362. read_unlock(&em_tree->lock);
  2363. if (!em) {
  2364. btrfs_crit(fs_info, "unable to find logical %llu length %llu",
  2365. logical, length);
  2366. return ERR_PTR(-EINVAL);
  2367. }
  2368. if (em->start > logical || em->start + em->len < logical) {
  2369. btrfs_crit(fs_info,
  2370. "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
  2371. logical, length, em->start, em->start + em->len);
  2372. free_extent_map(em);
  2373. return ERR_PTR(-EINVAL);
  2374. }
  2375. /* callers are responsible for dropping em's ref. */
  2376. return em;
  2377. }
  2378. int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
  2379. {
  2380. struct btrfs_fs_info *fs_info = trans->fs_info;
  2381. struct extent_map *em;
  2382. struct map_lookup *map;
  2383. u64 dev_extent_len = 0;
  2384. int i, ret = 0;
  2385. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2386. em = get_chunk_map(fs_info, chunk_offset, 1);
  2387. if (IS_ERR(em)) {
  2388. /*
  2389. * This is a logic error, but we don't want to just rely on the
  2390. * user having built with ASSERT enabled, so if ASSERT doesn't
  2391. * do anything we still error out.
  2392. */
  2393. ASSERT(0);
  2394. return PTR_ERR(em);
  2395. }
  2396. map = em->map_lookup;
  2397. mutex_lock(&fs_info->chunk_mutex);
  2398. check_system_chunk(trans, map->type);
  2399. mutex_unlock(&fs_info->chunk_mutex);
  2400. /*
  2401. * Take the device list mutex to prevent races with the final phase of
  2402. * a device replace operation that replaces the device object associated
  2403. * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
  2404. */
  2405. mutex_lock(&fs_devices->device_list_mutex);
  2406. for (i = 0; i < map->num_stripes; i++) {
  2407. struct btrfs_device *device = map->stripes[i].dev;
  2408. ret = btrfs_free_dev_extent(trans, device,
  2409. map->stripes[i].physical,
  2410. &dev_extent_len);
  2411. if (ret) {
  2412. mutex_unlock(&fs_devices->device_list_mutex);
  2413. btrfs_abort_transaction(trans, ret);
  2414. goto out;
  2415. }
  2416. if (device->bytes_used > 0) {
  2417. mutex_lock(&fs_info->chunk_mutex);
  2418. btrfs_device_set_bytes_used(device,
  2419. device->bytes_used - dev_extent_len);
  2420. atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
  2421. btrfs_clear_space_info_full(fs_info);
  2422. mutex_unlock(&fs_info->chunk_mutex);
  2423. }
  2424. if (map->stripes[i].dev) {
  2425. ret = btrfs_update_device(trans, map->stripes[i].dev);
  2426. if (ret) {
  2427. mutex_unlock(&fs_devices->device_list_mutex);
  2428. btrfs_abort_transaction(trans, ret);
  2429. goto out;
  2430. }
  2431. }
  2432. }
  2433. mutex_unlock(&fs_devices->device_list_mutex);
  2434. ret = btrfs_free_chunk(trans, chunk_offset);
  2435. if (ret) {
  2436. btrfs_abort_transaction(trans, ret);
  2437. goto out;
  2438. }
  2439. trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
  2440. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2441. ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
  2442. if (ret) {
  2443. btrfs_abort_transaction(trans, ret);
  2444. goto out;
  2445. }
  2446. }
  2447. ret = btrfs_remove_block_group(trans, chunk_offset, em);
  2448. if (ret) {
  2449. btrfs_abort_transaction(trans, ret);
  2450. goto out;
  2451. }
  2452. out:
  2453. /* once for us */
  2454. free_extent_map(em);
  2455. return ret;
  2456. }
  2457. static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
  2458. {
  2459. struct btrfs_root *root = fs_info->chunk_root;
  2460. struct btrfs_trans_handle *trans;
  2461. int ret;
  2462. /*
  2463. * Prevent races with automatic removal of unused block groups.
  2464. * After we relocate and before we remove the chunk with offset
  2465. * chunk_offset, automatic removal of the block group can kick in,
  2466. * resulting in a failure when calling btrfs_remove_chunk() below.
  2467. *
  2468. * Make sure to acquire this mutex before doing a tree search (dev
  2469. * or chunk trees) to find chunks. Otherwise the cleaner kthread might
  2470. * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
  2471. * we release the path used to search the chunk/dev tree and before
  2472. * the current task acquires this mutex and calls us.
  2473. */
  2474. lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
  2475. ret = btrfs_can_relocate(fs_info, chunk_offset);
  2476. if (ret)
  2477. return -ENOSPC;
  2478. /* step one, relocate all the extents inside this chunk */
  2479. btrfs_scrub_pause(fs_info);
  2480. ret = btrfs_relocate_block_group(fs_info, chunk_offset);
  2481. btrfs_scrub_continue(fs_info);
  2482. if (ret)
  2483. return ret;
  2484. /*
  2485. * We add the kobjects here (and after forcing data chunk creation)
  2486. * since relocation is the only place we'll create chunks of a new
  2487. * type at runtime. The only place where we'll remove the last
  2488. * chunk of a type is the call immediately below this one. Even
  2489. * so, we're protected against races with the cleaner thread since
  2490. * we're covered by the delete_unused_bgs_mutex.
  2491. */
  2492. btrfs_add_raid_kobjects(fs_info);
  2493. trans = btrfs_start_trans_remove_block_group(root->fs_info,
  2494. chunk_offset);
  2495. if (IS_ERR(trans)) {
  2496. ret = PTR_ERR(trans);
  2497. btrfs_handle_fs_error(root->fs_info, ret, NULL);
  2498. return ret;
  2499. }
  2500. /*
  2501. * step two, delete the device extents and the
  2502. * chunk tree entries
  2503. */
  2504. ret = btrfs_remove_chunk(trans, chunk_offset);
  2505. btrfs_end_transaction(trans);
  2506. return ret;
  2507. }
  2508. static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
  2509. {
  2510. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2511. struct btrfs_path *path;
  2512. struct extent_buffer *leaf;
  2513. struct btrfs_chunk *chunk;
  2514. struct btrfs_key key;
  2515. struct btrfs_key found_key;
  2516. u64 chunk_type;
  2517. bool retried = false;
  2518. int failed = 0;
  2519. int ret;
  2520. path = btrfs_alloc_path();
  2521. if (!path)
  2522. return -ENOMEM;
  2523. again:
  2524. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2525. key.offset = (u64)-1;
  2526. key.type = BTRFS_CHUNK_ITEM_KEY;
  2527. while (1) {
  2528. mutex_lock(&fs_info->delete_unused_bgs_mutex);
  2529. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2530. if (ret < 0) {
  2531. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  2532. goto error;
  2533. }
  2534. BUG_ON(ret == 0); /* Corruption */
  2535. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  2536. key.type);
  2537. if (ret)
  2538. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  2539. if (ret < 0)
  2540. goto error;
  2541. if (ret > 0)
  2542. break;
  2543. leaf = path->nodes[0];
  2544. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2545. chunk = btrfs_item_ptr(leaf, path->slots[0],
  2546. struct btrfs_chunk);
  2547. chunk_type = btrfs_chunk_type(leaf, chunk);
  2548. btrfs_release_path(path);
  2549. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2550. ret = btrfs_relocate_chunk(fs_info, found_key.offset);
  2551. if (ret == -ENOSPC)
  2552. failed++;
  2553. else
  2554. BUG_ON(ret);
  2555. }
  2556. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  2557. if (found_key.offset == 0)
  2558. break;
  2559. key.offset = found_key.offset - 1;
  2560. }
  2561. ret = 0;
  2562. if (failed && !retried) {
  2563. failed = 0;
  2564. retried = true;
  2565. goto again;
  2566. } else if (WARN_ON(failed && retried)) {
  2567. ret = -ENOSPC;
  2568. }
  2569. error:
  2570. btrfs_free_path(path);
  2571. return ret;
  2572. }
  2573. /*
  2574. * return 1 : allocate a data chunk successfully,
  2575. * return <0: errors during allocating a data chunk,
  2576. * return 0 : no need to allocate a data chunk.
  2577. */
  2578. static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
  2579. u64 chunk_offset)
  2580. {
  2581. struct btrfs_block_group_cache *cache;
  2582. u64 bytes_used;
  2583. u64 chunk_type;
  2584. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2585. ASSERT(cache);
  2586. chunk_type = cache->flags;
  2587. btrfs_put_block_group(cache);
  2588. if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
  2589. spin_lock(&fs_info->data_sinfo->lock);
  2590. bytes_used = fs_info->data_sinfo->bytes_used;
  2591. spin_unlock(&fs_info->data_sinfo->lock);
  2592. if (!bytes_used) {
  2593. struct btrfs_trans_handle *trans;
  2594. int ret;
  2595. trans = btrfs_join_transaction(fs_info->tree_root);
  2596. if (IS_ERR(trans))
  2597. return PTR_ERR(trans);
  2598. ret = btrfs_force_chunk_alloc(trans,
  2599. BTRFS_BLOCK_GROUP_DATA);
  2600. btrfs_end_transaction(trans);
  2601. if (ret < 0)
  2602. return ret;
  2603. btrfs_add_raid_kobjects(fs_info);
  2604. return 1;
  2605. }
  2606. }
  2607. return 0;
  2608. }
  2609. static int insert_balance_item(struct btrfs_fs_info *fs_info,
  2610. struct btrfs_balance_control *bctl)
  2611. {
  2612. struct btrfs_root *root = fs_info->tree_root;
  2613. struct btrfs_trans_handle *trans;
  2614. struct btrfs_balance_item *item;
  2615. struct btrfs_disk_balance_args disk_bargs;
  2616. struct btrfs_path *path;
  2617. struct extent_buffer *leaf;
  2618. struct btrfs_key key;
  2619. int ret, err;
  2620. path = btrfs_alloc_path();
  2621. if (!path)
  2622. return -ENOMEM;
  2623. trans = btrfs_start_transaction(root, 0);
  2624. if (IS_ERR(trans)) {
  2625. btrfs_free_path(path);
  2626. return PTR_ERR(trans);
  2627. }
  2628. key.objectid = BTRFS_BALANCE_OBJECTID;
  2629. key.type = BTRFS_TEMPORARY_ITEM_KEY;
  2630. key.offset = 0;
  2631. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2632. sizeof(*item));
  2633. if (ret)
  2634. goto out;
  2635. leaf = path->nodes[0];
  2636. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2637. memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
  2638. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  2639. btrfs_set_balance_data(leaf, item, &disk_bargs);
  2640. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  2641. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  2642. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  2643. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  2644. btrfs_set_balance_flags(leaf, item, bctl->flags);
  2645. btrfs_mark_buffer_dirty(leaf);
  2646. out:
  2647. btrfs_free_path(path);
  2648. err = btrfs_commit_transaction(trans);
  2649. if (err && !ret)
  2650. ret = err;
  2651. return ret;
  2652. }
  2653. static int del_balance_item(struct btrfs_fs_info *fs_info)
  2654. {
  2655. struct btrfs_root *root = fs_info->tree_root;
  2656. struct btrfs_trans_handle *trans;
  2657. struct btrfs_path *path;
  2658. struct btrfs_key key;
  2659. int ret, err;
  2660. path = btrfs_alloc_path();
  2661. if (!path)
  2662. return -ENOMEM;
  2663. trans = btrfs_start_transaction(root, 0);
  2664. if (IS_ERR(trans)) {
  2665. btrfs_free_path(path);
  2666. return PTR_ERR(trans);
  2667. }
  2668. key.objectid = BTRFS_BALANCE_OBJECTID;
  2669. key.type = BTRFS_TEMPORARY_ITEM_KEY;
  2670. key.offset = 0;
  2671. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2672. if (ret < 0)
  2673. goto out;
  2674. if (ret > 0) {
  2675. ret = -ENOENT;
  2676. goto out;
  2677. }
  2678. ret = btrfs_del_item(trans, root, path);
  2679. out:
  2680. btrfs_free_path(path);
  2681. err = btrfs_commit_transaction(trans);
  2682. if (err && !ret)
  2683. ret = err;
  2684. return ret;
  2685. }
  2686. /*
  2687. * This is a heuristic used to reduce the number of chunks balanced on
  2688. * resume after balance was interrupted.
  2689. */
  2690. static void update_balance_args(struct btrfs_balance_control *bctl)
  2691. {
  2692. /*
  2693. * Turn on soft mode for chunk types that were being converted.
  2694. */
  2695. if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2696. bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2697. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2698. bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2699. if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2700. bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2701. /*
  2702. * Turn on usage filter if is not already used. The idea is
  2703. * that chunks that we have already balanced should be
  2704. * reasonably full. Don't do it for chunks that are being
  2705. * converted - that will keep us from relocating unconverted
  2706. * (albeit full) chunks.
  2707. */
  2708. if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2709. !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2710. !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2711. bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2712. bctl->data.usage = 90;
  2713. }
  2714. if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2715. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2716. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2717. bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2718. bctl->sys.usage = 90;
  2719. }
  2720. if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2721. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2722. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2723. bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2724. bctl->meta.usage = 90;
  2725. }
  2726. }
  2727. /*
  2728. * Clear the balance status in fs_info and delete the balance item from disk.
  2729. */
  2730. static void reset_balance_state(struct btrfs_fs_info *fs_info)
  2731. {
  2732. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2733. int ret;
  2734. BUG_ON(!fs_info->balance_ctl);
  2735. spin_lock(&fs_info->balance_lock);
  2736. fs_info->balance_ctl = NULL;
  2737. spin_unlock(&fs_info->balance_lock);
  2738. kfree(bctl);
  2739. ret = del_balance_item(fs_info);
  2740. if (ret)
  2741. btrfs_handle_fs_error(fs_info, ret, NULL);
  2742. }
  2743. /*
  2744. * Balance filters. Return 1 if chunk should be filtered out
  2745. * (should not be balanced).
  2746. */
  2747. static int chunk_profiles_filter(u64 chunk_type,
  2748. struct btrfs_balance_args *bargs)
  2749. {
  2750. chunk_type = chunk_to_extended(chunk_type) &
  2751. BTRFS_EXTENDED_PROFILE_MASK;
  2752. if (bargs->profiles & chunk_type)
  2753. return 0;
  2754. return 1;
  2755. }
  2756. static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  2757. struct btrfs_balance_args *bargs)
  2758. {
  2759. struct btrfs_block_group_cache *cache;
  2760. u64 chunk_used;
  2761. u64 user_thresh_min;
  2762. u64 user_thresh_max;
  2763. int ret = 1;
  2764. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2765. chunk_used = btrfs_block_group_used(&cache->item);
  2766. if (bargs->usage_min == 0)
  2767. user_thresh_min = 0;
  2768. else
  2769. user_thresh_min = div_factor_fine(cache->key.offset,
  2770. bargs->usage_min);
  2771. if (bargs->usage_max == 0)
  2772. user_thresh_max = 1;
  2773. else if (bargs->usage_max > 100)
  2774. user_thresh_max = cache->key.offset;
  2775. else
  2776. user_thresh_max = div_factor_fine(cache->key.offset,
  2777. bargs->usage_max);
  2778. if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
  2779. ret = 0;
  2780. btrfs_put_block_group(cache);
  2781. return ret;
  2782. }
  2783. static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
  2784. u64 chunk_offset, struct btrfs_balance_args *bargs)
  2785. {
  2786. struct btrfs_block_group_cache *cache;
  2787. u64 chunk_used, user_thresh;
  2788. int ret = 1;
  2789. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2790. chunk_used = btrfs_block_group_used(&cache->item);
  2791. if (bargs->usage_min == 0)
  2792. user_thresh = 1;
  2793. else if (bargs->usage > 100)
  2794. user_thresh = cache->key.offset;
  2795. else
  2796. user_thresh = div_factor_fine(cache->key.offset,
  2797. bargs->usage);
  2798. if (chunk_used < user_thresh)
  2799. ret = 0;
  2800. btrfs_put_block_group(cache);
  2801. return ret;
  2802. }
  2803. static int chunk_devid_filter(struct extent_buffer *leaf,
  2804. struct btrfs_chunk *chunk,
  2805. struct btrfs_balance_args *bargs)
  2806. {
  2807. struct btrfs_stripe *stripe;
  2808. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2809. int i;
  2810. for (i = 0; i < num_stripes; i++) {
  2811. stripe = btrfs_stripe_nr(chunk, i);
  2812. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  2813. return 0;
  2814. }
  2815. return 1;
  2816. }
  2817. /* [pstart, pend) */
  2818. static int chunk_drange_filter(struct extent_buffer *leaf,
  2819. struct btrfs_chunk *chunk,
  2820. struct btrfs_balance_args *bargs)
  2821. {
  2822. struct btrfs_stripe *stripe;
  2823. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2824. u64 stripe_offset;
  2825. u64 stripe_length;
  2826. int factor;
  2827. int i;
  2828. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  2829. return 0;
  2830. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  2831. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
  2832. factor = num_stripes / 2;
  2833. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
  2834. factor = num_stripes - 1;
  2835. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
  2836. factor = num_stripes - 2;
  2837. } else {
  2838. factor = num_stripes;
  2839. }
  2840. for (i = 0; i < num_stripes; i++) {
  2841. stripe = btrfs_stripe_nr(chunk, i);
  2842. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  2843. continue;
  2844. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  2845. stripe_length = btrfs_chunk_length(leaf, chunk);
  2846. stripe_length = div_u64(stripe_length, factor);
  2847. if (stripe_offset < bargs->pend &&
  2848. stripe_offset + stripe_length > bargs->pstart)
  2849. return 0;
  2850. }
  2851. return 1;
  2852. }
  2853. /* [vstart, vend) */
  2854. static int chunk_vrange_filter(struct extent_buffer *leaf,
  2855. struct btrfs_chunk *chunk,
  2856. u64 chunk_offset,
  2857. struct btrfs_balance_args *bargs)
  2858. {
  2859. if (chunk_offset < bargs->vend &&
  2860. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  2861. /* at least part of the chunk is inside this vrange */
  2862. return 0;
  2863. return 1;
  2864. }
  2865. static int chunk_stripes_range_filter(struct extent_buffer *leaf,
  2866. struct btrfs_chunk *chunk,
  2867. struct btrfs_balance_args *bargs)
  2868. {
  2869. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2870. if (bargs->stripes_min <= num_stripes
  2871. && num_stripes <= bargs->stripes_max)
  2872. return 0;
  2873. return 1;
  2874. }
  2875. static int chunk_soft_convert_filter(u64 chunk_type,
  2876. struct btrfs_balance_args *bargs)
  2877. {
  2878. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  2879. return 0;
  2880. chunk_type = chunk_to_extended(chunk_type) &
  2881. BTRFS_EXTENDED_PROFILE_MASK;
  2882. if (bargs->target == chunk_type)
  2883. return 1;
  2884. return 0;
  2885. }
  2886. static int should_balance_chunk(struct btrfs_fs_info *fs_info,
  2887. struct extent_buffer *leaf,
  2888. struct btrfs_chunk *chunk, u64 chunk_offset)
  2889. {
  2890. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2891. struct btrfs_balance_args *bargs = NULL;
  2892. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  2893. /* type filter */
  2894. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  2895. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  2896. return 0;
  2897. }
  2898. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  2899. bargs = &bctl->data;
  2900. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  2901. bargs = &bctl->sys;
  2902. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  2903. bargs = &bctl->meta;
  2904. /* profiles filter */
  2905. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2906. chunk_profiles_filter(chunk_type, bargs)) {
  2907. return 0;
  2908. }
  2909. /* usage filter */
  2910. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2911. chunk_usage_filter(fs_info, chunk_offset, bargs)) {
  2912. return 0;
  2913. } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2914. chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
  2915. return 0;
  2916. }
  2917. /* devid filter */
  2918. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2919. chunk_devid_filter(leaf, chunk, bargs)) {
  2920. return 0;
  2921. }
  2922. /* drange filter, makes sense only with devid filter */
  2923. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2924. chunk_drange_filter(leaf, chunk, bargs)) {
  2925. return 0;
  2926. }
  2927. /* vrange filter */
  2928. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2929. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2930. return 0;
  2931. }
  2932. /* stripes filter */
  2933. if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
  2934. chunk_stripes_range_filter(leaf, chunk, bargs)) {
  2935. return 0;
  2936. }
  2937. /* soft profile changing mode */
  2938. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2939. chunk_soft_convert_filter(chunk_type, bargs)) {
  2940. return 0;
  2941. }
  2942. /*
  2943. * limited by count, must be the last filter
  2944. */
  2945. if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
  2946. if (bargs->limit == 0)
  2947. return 0;
  2948. else
  2949. bargs->limit--;
  2950. } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
  2951. /*
  2952. * Same logic as the 'limit' filter; the minimum cannot be
  2953. * determined here because we do not have the global information
  2954. * about the count of all chunks that satisfy the filters.
  2955. */
  2956. if (bargs->limit_max == 0)
  2957. return 0;
  2958. else
  2959. bargs->limit_max--;
  2960. }
  2961. return 1;
  2962. }
  2963. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2964. {
  2965. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2966. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2967. struct btrfs_root *dev_root = fs_info->dev_root;
  2968. struct list_head *devices;
  2969. struct btrfs_device *device;
  2970. u64 old_size;
  2971. u64 size_to_free;
  2972. u64 chunk_type;
  2973. struct btrfs_chunk *chunk;
  2974. struct btrfs_path *path = NULL;
  2975. struct btrfs_key key;
  2976. struct btrfs_key found_key;
  2977. struct btrfs_trans_handle *trans;
  2978. struct extent_buffer *leaf;
  2979. int slot;
  2980. int ret;
  2981. int enospc_errors = 0;
  2982. bool counting = true;
  2983. /* The single value limit and min/max limits use the same bytes in the */
  2984. u64 limit_data = bctl->data.limit;
  2985. u64 limit_meta = bctl->meta.limit;
  2986. u64 limit_sys = bctl->sys.limit;
  2987. u32 count_data = 0;
  2988. u32 count_meta = 0;
  2989. u32 count_sys = 0;
  2990. int chunk_reserved = 0;
  2991. /* step one make some room on all the devices */
  2992. devices = &fs_info->fs_devices->devices;
  2993. list_for_each_entry(device, devices, dev_list) {
  2994. old_size = btrfs_device_get_total_bytes(device);
  2995. size_to_free = div_factor(old_size, 1);
  2996. size_to_free = min_t(u64, size_to_free, SZ_1M);
  2997. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
  2998. btrfs_device_get_total_bytes(device) -
  2999. btrfs_device_get_bytes_used(device) > size_to_free ||
  3000. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
  3001. continue;
  3002. ret = btrfs_shrink_device(device, old_size - size_to_free);
  3003. if (ret == -ENOSPC)
  3004. break;
  3005. if (ret) {
  3006. /* btrfs_shrink_device never returns ret > 0 */
  3007. WARN_ON(ret > 0);
  3008. goto error;
  3009. }
  3010. trans = btrfs_start_transaction(dev_root, 0);
  3011. if (IS_ERR(trans)) {
  3012. ret = PTR_ERR(trans);
  3013. btrfs_info_in_rcu(fs_info,
  3014. "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
  3015. rcu_str_deref(device->name), ret,
  3016. old_size, old_size - size_to_free);
  3017. goto error;
  3018. }
  3019. ret = btrfs_grow_device(trans, device, old_size);
  3020. if (ret) {
  3021. btrfs_end_transaction(trans);
  3022. /* btrfs_grow_device never returns ret > 0 */
  3023. WARN_ON(ret > 0);
  3024. btrfs_info_in_rcu(fs_info,
  3025. "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
  3026. rcu_str_deref(device->name), ret,
  3027. old_size, old_size - size_to_free);
  3028. goto error;
  3029. }
  3030. btrfs_end_transaction(trans);
  3031. }
  3032. /* step two, relocate all the chunks */
  3033. path = btrfs_alloc_path();
  3034. if (!path) {
  3035. ret = -ENOMEM;
  3036. goto error;
  3037. }
  3038. /* zero out stat counters */
  3039. spin_lock(&fs_info->balance_lock);
  3040. memset(&bctl->stat, 0, sizeof(bctl->stat));
  3041. spin_unlock(&fs_info->balance_lock);
  3042. again:
  3043. if (!counting) {
  3044. /*
  3045. * The single value limit and min/max limits use the same bytes
  3046. * in the
  3047. */
  3048. bctl->data.limit = limit_data;
  3049. bctl->meta.limit = limit_meta;
  3050. bctl->sys.limit = limit_sys;
  3051. }
  3052. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  3053. key.offset = (u64)-1;
  3054. key.type = BTRFS_CHUNK_ITEM_KEY;
  3055. while (1) {
  3056. if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
  3057. atomic_read(&fs_info->balance_cancel_req)) {
  3058. ret = -ECANCELED;
  3059. goto error;
  3060. }
  3061. mutex_lock(&fs_info->delete_unused_bgs_mutex);
  3062. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  3063. if (ret < 0) {
  3064. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3065. goto error;
  3066. }
  3067. /*
  3068. * this shouldn't happen, it means the last relocate
  3069. * failed
  3070. */
  3071. if (ret == 0)
  3072. BUG(); /* FIXME break ? */
  3073. ret = btrfs_previous_item(chunk_root, path, 0,
  3074. BTRFS_CHUNK_ITEM_KEY);
  3075. if (ret) {
  3076. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3077. ret = 0;
  3078. break;
  3079. }
  3080. leaf = path->nodes[0];
  3081. slot = path->slots[0];
  3082. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3083. if (found_key.objectid != key.objectid) {
  3084. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3085. break;
  3086. }
  3087. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3088. chunk_type = btrfs_chunk_type(leaf, chunk);
  3089. if (!counting) {
  3090. spin_lock(&fs_info->balance_lock);
  3091. bctl->stat.considered++;
  3092. spin_unlock(&fs_info->balance_lock);
  3093. }
  3094. ret = should_balance_chunk(fs_info, leaf, chunk,
  3095. found_key.offset);
  3096. btrfs_release_path(path);
  3097. if (!ret) {
  3098. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3099. goto loop;
  3100. }
  3101. if (counting) {
  3102. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3103. spin_lock(&fs_info->balance_lock);
  3104. bctl->stat.expected++;
  3105. spin_unlock(&fs_info->balance_lock);
  3106. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  3107. count_data++;
  3108. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  3109. count_sys++;
  3110. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  3111. count_meta++;
  3112. goto loop;
  3113. }
  3114. /*
  3115. * Apply limit_min filter, no need to check if the LIMITS
  3116. * filter is used, limit_min is 0 by default
  3117. */
  3118. if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
  3119. count_data < bctl->data.limit_min)
  3120. || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
  3121. count_meta < bctl->meta.limit_min)
  3122. || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
  3123. count_sys < bctl->sys.limit_min)) {
  3124. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3125. goto loop;
  3126. }
  3127. if (!chunk_reserved) {
  3128. /*
  3129. * We may be relocating the only data chunk we have,
  3130. * which could potentially end up with losing data's
  3131. * raid profile, so lets allocate an empty one in
  3132. * advance.
  3133. */
  3134. ret = btrfs_may_alloc_data_chunk(fs_info,
  3135. found_key.offset);
  3136. if (ret < 0) {
  3137. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3138. goto error;
  3139. } else if (ret == 1) {
  3140. chunk_reserved = 1;
  3141. }
  3142. }
  3143. ret = btrfs_relocate_chunk(fs_info, found_key.offset);
  3144. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3145. if (ret && ret != -ENOSPC)
  3146. goto error;
  3147. if (ret == -ENOSPC) {
  3148. enospc_errors++;
  3149. } else {
  3150. spin_lock(&fs_info->balance_lock);
  3151. bctl->stat.completed++;
  3152. spin_unlock(&fs_info->balance_lock);
  3153. }
  3154. loop:
  3155. if (found_key.offset == 0)
  3156. break;
  3157. key.offset = found_key.offset - 1;
  3158. }
  3159. if (counting) {
  3160. btrfs_release_path(path);
  3161. counting = false;
  3162. goto again;
  3163. }
  3164. error:
  3165. btrfs_free_path(path);
  3166. if (enospc_errors) {
  3167. btrfs_info(fs_info, "%d enospc errors during balance",
  3168. enospc_errors);
  3169. if (!ret)
  3170. ret = -ENOSPC;
  3171. }
  3172. return ret;
  3173. }
  3174. /**
  3175. * alloc_profile_is_valid - see if a given profile is valid and reduced
  3176. * @flags: profile to validate
  3177. * @extended: if true @flags is treated as an extended profile
  3178. */
  3179. static int alloc_profile_is_valid(u64 flags, int extended)
  3180. {
  3181. u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
  3182. BTRFS_BLOCK_GROUP_PROFILE_MASK);
  3183. flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
  3184. /* 1) check that all other bits are zeroed */
  3185. if (flags & ~mask)
  3186. return 0;
  3187. /* 2) see if profile is reduced */
  3188. if (flags == 0)
  3189. return !extended; /* "0" is valid for usual profiles */
  3190. /* true if exactly one bit set */
  3191. return (flags & (flags - 1)) == 0;
  3192. }
  3193. static inline int balance_need_close(struct btrfs_fs_info *fs_info)
  3194. {
  3195. /* cancel requested || normal exit path */
  3196. return atomic_read(&fs_info->balance_cancel_req) ||
  3197. (atomic_read(&fs_info->balance_pause_req) == 0 &&
  3198. atomic_read(&fs_info->balance_cancel_req) == 0);
  3199. }
  3200. /* Non-zero return value signifies invalidity */
  3201. static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
  3202. u64 allowed)
  3203. {
  3204. return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  3205. (!alloc_profile_is_valid(bctl_arg->target, 1) ||
  3206. (bctl_arg->target & ~allowed)));
  3207. }
  3208. /*
  3209. * Should be called with balance mutexe held
  3210. */
  3211. int btrfs_balance(struct btrfs_fs_info *fs_info,
  3212. struct btrfs_balance_control *bctl,
  3213. struct btrfs_ioctl_balance_args *bargs)
  3214. {
  3215. u64 meta_target, data_target;
  3216. u64 allowed;
  3217. int mixed = 0;
  3218. int ret;
  3219. u64 num_devices;
  3220. unsigned seq;
  3221. bool reducing_integrity;
  3222. if (btrfs_fs_closing(fs_info) ||
  3223. atomic_read(&fs_info->balance_pause_req) ||
  3224. atomic_read(&fs_info->balance_cancel_req)) {
  3225. ret = -EINVAL;
  3226. goto out;
  3227. }
  3228. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  3229. if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  3230. mixed = 1;
  3231. /*
  3232. * In case of mixed groups both data and meta should be picked,
  3233. * and identical options should be given for both of them.
  3234. */
  3235. allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
  3236. if (mixed && (bctl->flags & allowed)) {
  3237. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  3238. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  3239. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  3240. btrfs_err(fs_info,
  3241. "balance: mixed groups data and metadata options must be the same");
  3242. ret = -EINVAL;
  3243. goto out;
  3244. }
  3245. }
  3246. num_devices = fs_info->fs_devices->num_devices;
  3247. btrfs_dev_replace_read_lock(&fs_info->dev_replace);
  3248. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  3249. BUG_ON(num_devices < 1);
  3250. num_devices--;
  3251. }
  3252. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  3253. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
  3254. if (num_devices > 1)
  3255. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  3256. if (num_devices > 2)
  3257. allowed |= BTRFS_BLOCK_GROUP_RAID5;
  3258. if (num_devices > 3)
  3259. allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
  3260. BTRFS_BLOCK_GROUP_RAID6);
  3261. if (validate_convert_profile(&bctl->data, allowed)) {
  3262. int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
  3263. btrfs_err(fs_info,
  3264. "balance: invalid convert data profile %s",
  3265. get_raid_name(index));
  3266. ret = -EINVAL;
  3267. goto out;
  3268. }
  3269. if (validate_convert_profile(&bctl->meta, allowed)) {
  3270. int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
  3271. btrfs_err(fs_info,
  3272. "balance: invalid convert metadata profile %s",
  3273. get_raid_name(index));
  3274. ret = -EINVAL;
  3275. goto out;
  3276. }
  3277. if (validate_convert_profile(&bctl->sys, allowed)) {
  3278. int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
  3279. btrfs_err(fs_info,
  3280. "balance: invalid convert system profile %s",
  3281. get_raid_name(index));
  3282. ret = -EINVAL;
  3283. goto out;
  3284. }
  3285. /* allow to reduce meta or sys integrity only if force set */
  3286. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3287. BTRFS_BLOCK_GROUP_RAID10 |
  3288. BTRFS_BLOCK_GROUP_RAID5 |
  3289. BTRFS_BLOCK_GROUP_RAID6;
  3290. do {
  3291. seq = read_seqbegin(&fs_info->profiles_lock);
  3292. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  3293. (fs_info->avail_system_alloc_bits & allowed) &&
  3294. !(bctl->sys.target & allowed)) ||
  3295. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  3296. (fs_info->avail_metadata_alloc_bits & allowed) &&
  3297. !(bctl->meta.target & allowed)))
  3298. reducing_integrity = true;
  3299. else
  3300. reducing_integrity = false;
  3301. /* if we're not converting, the target field is uninitialized */
  3302. meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
  3303. bctl->meta.target : fs_info->avail_metadata_alloc_bits;
  3304. data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
  3305. bctl->data.target : fs_info->avail_data_alloc_bits;
  3306. } while (read_seqretry(&fs_info->profiles_lock, seq));
  3307. if (reducing_integrity) {
  3308. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  3309. btrfs_info(fs_info,
  3310. "balance: force reducing metadata integrity");
  3311. } else {
  3312. btrfs_err(fs_info,
  3313. "balance: reduces metadata integrity, use --force if you want this");
  3314. ret = -EINVAL;
  3315. goto out;
  3316. }
  3317. }
  3318. if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
  3319. btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
  3320. int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
  3321. int data_index = btrfs_bg_flags_to_raid_index(data_target);
  3322. btrfs_warn(fs_info,
  3323. "balance: metadata profile %s has lower redundancy than data profile %s",
  3324. get_raid_name(meta_index), get_raid_name(data_index));
  3325. }
  3326. ret = insert_balance_item(fs_info, bctl);
  3327. if (ret && ret != -EEXIST)
  3328. goto out;
  3329. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  3330. BUG_ON(ret == -EEXIST);
  3331. BUG_ON(fs_info->balance_ctl);
  3332. spin_lock(&fs_info->balance_lock);
  3333. fs_info->balance_ctl = bctl;
  3334. spin_unlock(&fs_info->balance_lock);
  3335. } else {
  3336. BUG_ON(ret != -EEXIST);
  3337. spin_lock(&fs_info->balance_lock);
  3338. update_balance_args(bctl);
  3339. spin_unlock(&fs_info->balance_lock);
  3340. }
  3341. ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
  3342. set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
  3343. mutex_unlock(&fs_info->balance_mutex);
  3344. ret = __btrfs_balance(fs_info);
  3345. mutex_lock(&fs_info->balance_mutex);
  3346. clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
  3347. if (bargs) {
  3348. memset(bargs, 0, sizeof(*bargs));
  3349. btrfs_update_ioctl_balance_args(fs_info, bargs);
  3350. }
  3351. if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
  3352. balance_need_close(fs_info)) {
  3353. reset_balance_state(fs_info);
  3354. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  3355. }
  3356. wake_up(&fs_info->balance_wait_q);
  3357. return ret;
  3358. out:
  3359. if (bctl->flags & BTRFS_BALANCE_RESUME)
  3360. reset_balance_state(fs_info);
  3361. else
  3362. kfree(bctl);
  3363. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  3364. return ret;
  3365. }
  3366. static int balance_kthread(void *data)
  3367. {
  3368. struct btrfs_fs_info *fs_info = data;
  3369. int ret = 0;
  3370. mutex_lock(&fs_info->balance_mutex);
  3371. if (fs_info->balance_ctl) {
  3372. btrfs_info(fs_info, "balance: resuming");
  3373. ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
  3374. }
  3375. mutex_unlock(&fs_info->balance_mutex);
  3376. return ret;
  3377. }
  3378. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
  3379. {
  3380. struct task_struct *tsk;
  3381. mutex_lock(&fs_info->balance_mutex);
  3382. if (!fs_info->balance_ctl) {
  3383. mutex_unlock(&fs_info->balance_mutex);
  3384. return 0;
  3385. }
  3386. mutex_unlock(&fs_info->balance_mutex);
  3387. if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
  3388. btrfs_info(fs_info, "balance: resume skipped");
  3389. return 0;
  3390. }
  3391. /*
  3392. * A ro->rw remount sequence should continue with the paused balance
  3393. * regardless of who pauses it, system or the user as of now, so set
  3394. * the resume flag.
  3395. */
  3396. spin_lock(&fs_info->balance_lock);
  3397. fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
  3398. spin_unlock(&fs_info->balance_lock);
  3399. tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
  3400. return PTR_ERR_OR_ZERO(tsk);
  3401. }
  3402. int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
  3403. {
  3404. struct btrfs_balance_control *bctl;
  3405. struct btrfs_balance_item *item;
  3406. struct btrfs_disk_balance_args disk_bargs;
  3407. struct btrfs_path *path;
  3408. struct extent_buffer *leaf;
  3409. struct btrfs_key key;
  3410. int ret;
  3411. path = btrfs_alloc_path();
  3412. if (!path)
  3413. return -ENOMEM;
  3414. key.objectid = BTRFS_BALANCE_OBJECTID;
  3415. key.type = BTRFS_TEMPORARY_ITEM_KEY;
  3416. key.offset = 0;
  3417. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  3418. if (ret < 0)
  3419. goto out;
  3420. if (ret > 0) { /* ret = -ENOENT; */
  3421. ret = 0;
  3422. goto out;
  3423. }
  3424. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3425. if (!bctl) {
  3426. ret = -ENOMEM;
  3427. goto out;
  3428. }
  3429. leaf = path->nodes[0];
  3430. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  3431. bctl->flags = btrfs_balance_flags(leaf, item);
  3432. bctl->flags |= BTRFS_BALANCE_RESUME;
  3433. btrfs_balance_data(leaf, item, &disk_bargs);
  3434. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  3435. btrfs_balance_meta(leaf, item, &disk_bargs);
  3436. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  3437. btrfs_balance_sys(leaf, item, &disk_bargs);
  3438. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  3439. /*
  3440. * This should never happen, as the paused balance state is recovered
  3441. * during mount without any chance of other exclusive ops to collide.
  3442. *
  3443. * This gives the exclusive op status to balance and keeps in paused
  3444. * state until user intervention (cancel or umount). If the ownership
  3445. * cannot be assigned, show a message but do not fail. The balance
  3446. * is in a paused state and must have fs_info::balance_ctl properly
  3447. * set up.
  3448. */
  3449. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
  3450. btrfs_warn(fs_info,
  3451. "balance: cannot set exclusive op status, resume manually");
  3452. mutex_lock(&fs_info->balance_mutex);
  3453. BUG_ON(fs_info->balance_ctl);
  3454. spin_lock(&fs_info->balance_lock);
  3455. fs_info->balance_ctl = bctl;
  3456. spin_unlock(&fs_info->balance_lock);
  3457. mutex_unlock(&fs_info->balance_mutex);
  3458. out:
  3459. btrfs_free_path(path);
  3460. return ret;
  3461. }
  3462. int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
  3463. {
  3464. int ret = 0;
  3465. mutex_lock(&fs_info->balance_mutex);
  3466. if (!fs_info->balance_ctl) {
  3467. mutex_unlock(&fs_info->balance_mutex);
  3468. return -ENOTCONN;
  3469. }
  3470. if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  3471. atomic_inc(&fs_info->balance_pause_req);
  3472. mutex_unlock(&fs_info->balance_mutex);
  3473. wait_event(fs_info->balance_wait_q,
  3474. !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
  3475. mutex_lock(&fs_info->balance_mutex);
  3476. /* we are good with balance_ctl ripped off from under us */
  3477. BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
  3478. atomic_dec(&fs_info->balance_pause_req);
  3479. } else {
  3480. ret = -ENOTCONN;
  3481. }
  3482. mutex_unlock(&fs_info->balance_mutex);
  3483. return ret;
  3484. }
  3485. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
  3486. {
  3487. mutex_lock(&fs_info->balance_mutex);
  3488. if (!fs_info->balance_ctl) {
  3489. mutex_unlock(&fs_info->balance_mutex);
  3490. return -ENOTCONN;
  3491. }
  3492. /*
  3493. * A paused balance with the item stored on disk can be resumed at
  3494. * mount time if the mount is read-write. Otherwise it's still paused
  3495. * and we must not allow cancelling as it deletes the item.
  3496. */
  3497. if (sb_rdonly(fs_info->sb)) {
  3498. mutex_unlock(&fs_info->balance_mutex);
  3499. return -EROFS;
  3500. }
  3501. atomic_inc(&fs_info->balance_cancel_req);
  3502. /*
  3503. * if we are running just wait and return, balance item is
  3504. * deleted in btrfs_balance in this case
  3505. */
  3506. if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  3507. mutex_unlock(&fs_info->balance_mutex);
  3508. wait_event(fs_info->balance_wait_q,
  3509. !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
  3510. mutex_lock(&fs_info->balance_mutex);
  3511. } else {
  3512. mutex_unlock(&fs_info->balance_mutex);
  3513. /*
  3514. * Lock released to allow other waiters to continue, we'll
  3515. * reexamine the status again.
  3516. */
  3517. mutex_lock(&fs_info->balance_mutex);
  3518. if (fs_info->balance_ctl) {
  3519. reset_balance_state(fs_info);
  3520. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  3521. btrfs_info(fs_info, "balance: canceled");
  3522. }
  3523. }
  3524. BUG_ON(fs_info->balance_ctl ||
  3525. test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
  3526. atomic_dec(&fs_info->balance_cancel_req);
  3527. mutex_unlock(&fs_info->balance_mutex);
  3528. return 0;
  3529. }
  3530. static int btrfs_uuid_scan_kthread(void *data)
  3531. {
  3532. struct btrfs_fs_info *fs_info = data;
  3533. struct btrfs_root *root = fs_info->tree_root;
  3534. struct btrfs_key key;
  3535. struct btrfs_path *path = NULL;
  3536. int ret = 0;
  3537. struct extent_buffer *eb;
  3538. int slot;
  3539. struct btrfs_root_item root_item;
  3540. u32 item_size;
  3541. struct btrfs_trans_handle *trans = NULL;
  3542. path = btrfs_alloc_path();
  3543. if (!path) {
  3544. ret = -ENOMEM;
  3545. goto out;
  3546. }
  3547. key.objectid = 0;
  3548. key.type = BTRFS_ROOT_ITEM_KEY;
  3549. key.offset = 0;
  3550. while (1) {
  3551. ret = btrfs_search_forward(root, &key, path,
  3552. BTRFS_OLDEST_GENERATION);
  3553. if (ret) {
  3554. if (ret > 0)
  3555. ret = 0;
  3556. break;
  3557. }
  3558. if (key.type != BTRFS_ROOT_ITEM_KEY ||
  3559. (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
  3560. key.objectid != BTRFS_FS_TREE_OBJECTID) ||
  3561. key.objectid > BTRFS_LAST_FREE_OBJECTID)
  3562. goto skip;
  3563. eb = path->nodes[0];
  3564. slot = path->slots[0];
  3565. item_size = btrfs_item_size_nr(eb, slot);
  3566. if (item_size < sizeof(root_item))
  3567. goto skip;
  3568. read_extent_buffer(eb, &root_item,
  3569. btrfs_item_ptr_offset(eb, slot),
  3570. (int)sizeof(root_item));
  3571. if (btrfs_root_refs(&root_item) == 0)
  3572. goto skip;
  3573. if (!btrfs_is_empty_uuid(root_item.uuid) ||
  3574. !btrfs_is_empty_uuid(root_item.received_uuid)) {
  3575. if (trans)
  3576. goto update_tree;
  3577. btrfs_release_path(path);
  3578. /*
  3579. * 1 - subvol uuid item
  3580. * 1 - received_subvol uuid item
  3581. */
  3582. trans = btrfs_start_transaction(fs_info->uuid_root, 2);
  3583. if (IS_ERR(trans)) {
  3584. ret = PTR_ERR(trans);
  3585. break;
  3586. }
  3587. continue;
  3588. } else {
  3589. goto skip;
  3590. }
  3591. update_tree:
  3592. if (!btrfs_is_empty_uuid(root_item.uuid)) {
  3593. ret = btrfs_uuid_tree_add(trans, root_item.uuid,
  3594. BTRFS_UUID_KEY_SUBVOL,
  3595. key.objectid);
  3596. if (ret < 0) {
  3597. btrfs_warn(fs_info, "uuid_tree_add failed %d",
  3598. ret);
  3599. break;
  3600. }
  3601. }
  3602. if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
  3603. ret = btrfs_uuid_tree_add(trans,
  3604. root_item.received_uuid,
  3605. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3606. key.objectid);
  3607. if (ret < 0) {
  3608. btrfs_warn(fs_info, "uuid_tree_add failed %d",
  3609. ret);
  3610. break;
  3611. }
  3612. }
  3613. skip:
  3614. if (trans) {
  3615. ret = btrfs_end_transaction(trans);
  3616. trans = NULL;
  3617. if (ret)
  3618. break;
  3619. }
  3620. btrfs_release_path(path);
  3621. if (key.offset < (u64)-1) {
  3622. key.offset++;
  3623. } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
  3624. key.offset = 0;
  3625. key.type = BTRFS_ROOT_ITEM_KEY;
  3626. } else if (key.objectid < (u64)-1) {
  3627. key.offset = 0;
  3628. key.type = BTRFS_ROOT_ITEM_KEY;
  3629. key.objectid++;
  3630. } else {
  3631. break;
  3632. }
  3633. cond_resched();
  3634. }
  3635. out:
  3636. btrfs_free_path(path);
  3637. if (trans && !IS_ERR(trans))
  3638. btrfs_end_transaction(trans);
  3639. if (ret)
  3640. btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
  3641. else
  3642. set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
  3643. up(&fs_info->uuid_tree_rescan_sem);
  3644. return 0;
  3645. }
  3646. /*
  3647. * Callback for btrfs_uuid_tree_iterate().
  3648. * returns:
  3649. * 0 check succeeded, the entry is not outdated.
  3650. * < 0 if an error occurred.
  3651. * > 0 if the check failed, which means the caller shall remove the entry.
  3652. */
  3653. static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
  3654. u8 *uuid, u8 type, u64 subid)
  3655. {
  3656. struct btrfs_key key;
  3657. int ret = 0;
  3658. struct btrfs_root *subvol_root;
  3659. if (type != BTRFS_UUID_KEY_SUBVOL &&
  3660. type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
  3661. goto out;
  3662. key.objectid = subid;
  3663. key.type = BTRFS_ROOT_ITEM_KEY;
  3664. key.offset = (u64)-1;
  3665. subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
  3666. if (IS_ERR(subvol_root)) {
  3667. ret = PTR_ERR(subvol_root);
  3668. if (ret == -ENOENT)
  3669. ret = 1;
  3670. goto out;
  3671. }
  3672. switch (type) {
  3673. case BTRFS_UUID_KEY_SUBVOL:
  3674. if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
  3675. ret = 1;
  3676. break;
  3677. case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
  3678. if (memcmp(uuid, subvol_root->root_item.received_uuid,
  3679. BTRFS_UUID_SIZE))
  3680. ret = 1;
  3681. break;
  3682. }
  3683. out:
  3684. return ret;
  3685. }
  3686. static int btrfs_uuid_rescan_kthread(void *data)
  3687. {
  3688. struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
  3689. int ret;
  3690. /*
  3691. * 1st step is to iterate through the existing UUID tree and
  3692. * to delete all entries that contain outdated data.
  3693. * 2nd step is to add all missing entries to the UUID tree.
  3694. */
  3695. ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
  3696. if (ret < 0) {
  3697. btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
  3698. up(&fs_info->uuid_tree_rescan_sem);
  3699. return ret;
  3700. }
  3701. return btrfs_uuid_scan_kthread(data);
  3702. }
  3703. int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
  3704. {
  3705. struct btrfs_trans_handle *trans;
  3706. struct btrfs_root *tree_root = fs_info->tree_root;
  3707. struct btrfs_root *uuid_root;
  3708. struct task_struct *task;
  3709. int ret;
  3710. /*
  3711. * 1 - root node
  3712. * 1 - root item
  3713. */
  3714. trans = btrfs_start_transaction(tree_root, 2);
  3715. if (IS_ERR(trans))
  3716. return PTR_ERR(trans);
  3717. uuid_root = btrfs_create_tree(trans, fs_info,
  3718. BTRFS_UUID_TREE_OBJECTID);
  3719. if (IS_ERR(uuid_root)) {
  3720. ret = PTR_ERR(uuid_root);
  3721. btrfs_abort_transaction(trans, ret);
  3722. btrfs_end_transaction(trans);
  3723. return ret;
  3724. }
  3725. fs_info->uuid_root = uuid_root;
  3726. ret = btrfs_commit_transaction(trans);
  3727. if (ret)
  3728. return ret;
  3729. down(&fs_info->uuid_tree_rescan_sem);
  3730. task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
  3731. if (IS_ERR(task)) {
  3732. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  3733. btrfs_warn(fs_info, "failed to start uuid_scan task");
  3734. up(&fs_info->uuid_tree_rescan_sem);
  3735. return PTR_ERR(task);
  3736. }
  3737. return 0;
  3738. }
  3739. int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
  3740. {
  3741. struct task_struct *task;
  3742. down(&fs_info->uuid_tree_rescan_sem);
  3743. task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
  3744. if (IS_ERR(task)) {
  3745. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  3746. btrfs_warn(fs_info, "failed to start uuid_rescan task");
  3747. up(&fs_info->uuid_tree_rescan_sem);
  3748. return PTR_ERR(task);
  3749. }
  3750. return 0;
  3751. }
  3752. /*
  3753. * shrinking a device means finding all of the device extents past
  3754. * the new size, and then following the back refs to the chunks.
  3755. * The chunk relocation code actually frees the device extent
  3756. */
  3757. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  3758. {
  3759. struct btrfs_fs_info *fs_info = device->fs_info;
  3760. struct btrfs_root *root = fs_info->dev_root;
  3761. struct btrfs_trans_handle *trans;
  3762. struct btrfs_dev_extent *dev_extent = NULL;
  3763. struct btrfs_path *path;
  3764. u64 length;
  3765. u64 chunk_offset;
  3766. int ret;
  3767. int slot;
  3768. int failed = 0;
  3769. bool retried = false;
  3770. bool checked_pending_chunks = false;
  3771. struct extent_buffer *l;
  3772. struct btrfs_key key;
  3773. struct btrfs_super_block *super_copy = fs_info->super_copy;
  3774. u64 old_total = btrfs_super_total_bytes(super_copy);
  3775. u64 old_size = btrfs_device_get_total_bytes(device);
  3776. u64 diff;
  3777. new_size = round_down(new_size, fs_info->sectorsize);
  3778. diff = round_down(old_size - new_size, fs_info->sectorsize);
  3779. if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
  3780. return -EINVAL;
  3781. path = btrfs_alloc_path();
  3782. if (!path)
  3783. return -ENOMEM;
  3784. path->reada = READA_BACK;
  3785. mutex_lock(&fs_info->chunk_mutex);
  3786. btrfs_device_set_total_bytes(device, new_size);
  3787. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  3788. device->fs_devices->total_rw_bytes -= diff;
  3789. atomic64_sub(diff, &fs_info->free_chunk_space);
  3790. }
  3791. mutex_unlock(&fs_info->chunk_mutex);
  3792. again:
  3793. key.objectid = device->devid;
  3794. key.offset = (u64)-1;
  3795. key.type = BTRFS_DEV_EXTENT_KEY;
  3796. do {
  3797. mutex_lock(&fs_info->delete_unused_bgs_mutex);
  3798. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3799. if (ret < 0) {
  3800. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3801. goto done;
  3802. }
  3803. ret = btrfs_previous_item(root, path, 0, key.type);
  3804. if (ret)
  3805. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3806. if (ret < 0)
  3807. goto done;
  3808. if (ret) {
  3809. ret = 0;
  3810. btrfs_release_path(path);
  3811. break;
  3812. }
  3813. l = path->nodes[0];
  3814. slot = path->slots[0];
  3815. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  3816. if (key.objectid != device->devid) {
  3817. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3818. btrfs_release_path(path);
  3819. break;
  3820. }
  3821. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  3822. length = btrfs_dev_extent_length(l, dev_extent);
  3823. if (key.offset + length <= new_size) {
  3824. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3825. btrfs_release_path(path);
  3826. break;
  3827. }
  3828. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3829. btrfs_release_path(path);
  3830. /*
  3831. * We may be relocating the only data chunk we have,
  3832. * which could potentially end up with losing data's
  3833. * raid profile, so lets allocate an empty one in
  3834. * advance.
  3835. */
  3836. ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
  3837. if (ret < 0) {
  3838. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3839. goto done;
  3840. }
  3841. ret = btrfs_relocate_chunk(fs_info, chunk_offset);
  3842. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3843. if (ret && ret != -ENOSPC)
  3844. goto done;
  3845. if (ret == -ENOSPC)
  3846. failed++;
  3847. } while (key.offset-- > 0);
  3848. if (failed && !retried) {
  3849. failed = 0;
  3850. retried = true;
  3851. goto again;
  3852. } else if (failed && retried) {
  3853. ret = -ENOSPC;
  3854. goto done;
  3855. }
  3856. /* Shrinking succeeded, else we would be at "done". */
  3857. trans = btrfs_start_transaction(root, 0);
  3858. if (IS_ERR(trans)) {
  3859. ret = PTR_ERR(trans);
  3860. goto done;
  3861. }
  3862. mutex_lock(&fs_info->chunk_mutex);
  3863. /*
  3864. * We checked in the above loop all device extents that were already in
  3865. * the device tree. However before we have updated the device's
  3866. * total_bytes to the new size, we might have had chunk allocations that
  3867. * have not complete yet (new block groups attached to transaction
  3868. * handles), and therefore their device extents were not yet in the
  3869. * device tree and we missed them in the loop above. So if we have any
  3870. * pending chunk using a device extent that overlaps the device range
  3871. * that we can not use anymore, commit the current transaction and
  3872. * repeat the search on the device tree - this way we guarantee we will
  3873. * not have chunks using device extents that end beyond 'new_size'.
  3874. */
  3875. if (!checked_pending_chunks) {
  3876. u64 start = new_size;
  3877. u64 len = old_size - new_size;
  3878. if (contains_pending_extent(trans->transaction, device,
  3879. &start, len)) {
  3880. mutex_unlock(&fs_info->chunk_mutex);
  3881. checked_pending_chunks = true;
  3882. failed = 0;
  3883. retried = false;
  3884. ret = btrfs_commit_transaction(trans);
  3885. if (ret)
  3886. goto done;
  3887. goto again;
  3888. }
  3889. }
  3890. btrfs_device_set_disk_total_bytes(device, new_size);
  3891. if (list_empty(&device->resized_list))
  3892. list_add_tail(&device->resized_list,
  3893. &fs_info->fs_devices->resized_devices);
  3894. WARN_ON(diff > old_total);
  3895. btrfs_set_super_total_bytes(super_copy,
  3896. round_down(old_total - diff, fs_info->sectorsize));
  3897. mutex_unlock(&fs_info->chunk_mutex);
  3898. /* Now btrfs_update_device() will change the on-disk size. */
  3899. ret = btrfs_update_device(trans, device);
  3900. if (ret < 0) {
  3901. btrfs_abort_transaction(trans, ret);
  3902. btrfs_end_transaction(trans);
  3903. } else {
  3904. ret = btrfs_commit_transaction(trans);
  3905. }
  3906. done:
  3907. btrfs_free_path(path);
  3908. if (ret) {
  3909. mutex_lock(&fs_info->chunk_mutex);
  3910. btrfs_device_set_total_bytes(device, old_size);
  3911. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
  3912. device->fs_devices->total_rw_bytes += diff;
  3913. atomic64_add(diff, &fs_info->free_chunk_space);
  3914. mutex_unlock(&fs_info->chunk_mutex);
  3915. }
  3916. return ret;
  3917. }
  3918. static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
  3919. struct btrfs_key *key,
  3920. struct btrfs_chunk *chunk, int item_size)
  3921. {
  3922. struct btrfs_super_block *super_copy = fs_info->super_copy;
  3923. struct btrfs_disk_key disk_key;
  3924. u32 array_size;
  3925. u8 *ptr;
  3926. mutex_lock(&fs_info->chunk_mutex);
  3927. array_size = btrfs_super_sys_array_size(super_copy);
  3928. if (array_size + item_size + sizeof(disk_key)
  3929. > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
  3930. mutex_unlock(&fs_info->chunk_mutex);
  3931. return -EFBIG;
  3932. }
  3933. ptr = super_copy->sys_chunk_array + array_size;
  3934. btrfs_cpu_key_to_disk(&disk_key, key);
  3935. memcpy(ptr, &disk_key, sizeof(disk_key));
  3936. ptr += sizeof(disk_key);
  3937. memcpy(ptr, chunk, item_size);
  3938. item_size += sizeof(disk_key);
  3939. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  3940. mutex_unlock(&fs_info->chunk_mutex);
  3941. return 0;
  3942. }
  3943. /*
  3944. * sort the devices in descending order by max_avail, total_avail
  3945. */
  3946. static int btrfs_cmp_device_info(const void *a, const void *b)
  3947. {
  3948. const struct btrfs_device_info *di_a = a;
  3949. const struct btrfs_device_info *di_b = b;
  3950. if (di_a->max_avail > di_b->max_avail)
  3951. return -1;
  3952. if (di_a->max_avail < di_b->max_avail)
  3953. return 1;
  3954. if (di_a->total_avail > di_b->total_avail)
  3955. return -1;
  3956. if (di_a->total_avail < di_b->total_avail)
  3957. return 1;
  3958. return 0;
  3959. }
  3960. static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
  3961. {
  3962. if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
  3963. return;
  3964. btrfs_set_fs_incompat(info, RAID56);
  3965. }
  3966. #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
  3967. - sizeof(struct btrfs_chunk)) \
  3968. / sizeof(struct btrfs_stripe) + 1)
  3969. #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
  3970. - 2 * sizeof(struct btrfs_disk_key) \
  3971. - 2 * sizeof(struct btrfs_chunk)) \
  3972. / sizeof(struct btrfs_stripe) + 1)
  3973. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3974. u64 start, u64 type)
  3975. {
  3976. struct btrfs_fs_info *info = trans->fs_info;
  3977. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  3978. struct btrfs_device *device;
  3979. struct map_lookup *map = NULL;
  3980. struct extent_map_tree *em_tree;
  3981. struct extent_map *em;
  3982. struct btrfs_device_info *devices_info = NULL;
  3983. u64 total_avail;
  3984. int num_stripes; /* total number of stripes to allocate */
  3985. int data_stripes; /* number of stripes that count for
  3986. block group size */
  3987. int sub_stripes; /* sub_stripes info for map */
  3988. int dev_stripes; /* stripes per dev */
  3989. int devs_max; /* max devs to use */
  3990. int devs_min; /* min devs needed */
  3991. int devs_increment; /* ndevs has to be a multiple of this */
  3992. int ncopies; /* how many copies to data has */
  3993. int ret;
  3994. u64 max_stripe_size;
  3995. u64 max_chunk_size;
  3996. u64 stripe_size;
  3997. u64 num_bytes;
  3998. int ndevs;
  3999. int i;
  4000. int j;
  4001. int index;
  4002. BUG_ON(!alloc_profile_is_valid(type, 0));
  4003. if (list_empty(&fs_devices->alloc_list)) {
  4004. if (btrfs_test_opt(info, ENOSPC_DEBUG))
  4005. btrfs_debug(info, "%s: no writable device", __func__);
  4006. return -ENOSPC;
  4007. }
  4008. index = btrfs_bg_flags_to_raid_index(type);
  4009. sub_stripes = btrfs_raid_array[index].sub_stripes;
  4010. dev_stripes = btrfs_raid_array[index].dev_stripes;
  4011. devs_max = btrfs_raid_array[index].devs_max;
  4012. devs_min = btrfs_raid_array[index].devs_min;
  4013. devs_increment = btrfs_raid_array[index].devs_increment;
  4014. ncopies = btrfs_raid_array[index].ncopies;
  4015. if (type & BTRFS_BLOCK_GROUP_DATA) {
  4016. max_stripe_size = SZ_1G;
  4017. max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
  4018. if (!devs_max)
  4019. devs_max = BTRFS_MAX_DEVS(info);
  4020. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  4021. /* for larger filesystems, use larger metadata chunks */
  4022. if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
  4023. max_stripe_size = SZ_1G;
  4024. else
  4025. max_stripe_size = SZ_256M;
  4026. max_chunk_size = max_stripe_size;
  4027. if (!devs_max)
  4028. devs_max = BTRFS_MAX_DEVS(info);
  4029. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  4030. max_stripe_size = SZ_32M;
  4031. max_chunk_size = 2 * max_stripe_size;
  4032. if (!devs_max)
  4033. devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
  4034. } else {
  4035. btrfs_err(info, "invalid chunk type 0x%llx requested",
  4036. type);
  4037. BUG_ON(1);
  4038. }
  4039. /* we don't want a chunk larger than 10% of writeable space */
  4040. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  4041. max_chunk_size);
  4042. devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
  4043. GFP_NOFS);
  4044. if (!devices_info)
  4045. return -ENOMEM;
  4046. /*
  4047. * in the first pass through the devices list, we gather information
  4048. * about the available holes on each device.
  4049. */
  4050. ndevs = 0;
  4051. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  4052. u64 max_avail;
  4053. u64 dev_offset;
  4054. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  4055. WARN(1, KERN_ERR
  4056. "BTRFS: read-only device in alloc_list\n");
  4057. continue;
  4058. }
  4059. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
  4060. &device->dev_state) ||
  4061. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
  4062. continue;
  4063. if (device->total_bytes > device->bytes_used)
  4064. total_avail = device->total_bytes - device->bytes_used;
  4065. else
  4066. total_avail = 0;
  4067. /* If there is no space on this device, skip it. */
  4068. if (total_avail == 0)
  4069. continue;
  4070. ret = find_free_dev_extent(trans, device,
  4071. max_stripe_size * dev_stripes,
  4072. &dev_offset, &max_avail);
  4073. if (ret && ret != -ENOSPC)
  4074. goto error;
  4075. if (ret == 0)
  4076. max_avail = max_stripe_size * dev_stripes;
  4077. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
  4078. if (btrfs_test_opt(info, ENOSPC_DEBUG))
  4079. btrfs_debug(info,
  4080. "%s: devid %llu has no free space, have=%llu want=%u",
  4081. __func__, device->devid, max_avail,
  4082. BTRFS_STRIPE_LEN * dev_stripes);
  4083. continue;
  4084. }
  4085. if (ndevs == fs_devices->rw_devices) {
  4086. WARN(1, "%s: found more than %llu devices\n",
  4087. __func__, fs_devices->rw_devices);
  4088. break;
  4089. }
  4090. devices_info[ndevs].dev_offset = dev_offset;
  4091. devices_info[ndevs].max_avail = max_avail;
  4092. devices_info[ndevs].total_avail = total_avail;
  4093. devices_info[ndevs].dev = device;
  4094. ++ndevs;
  4095. }
  4096. /*
  4097. * now sort the devices by hole size / available space
  4098. */
  4099. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  4100. btrfs_cmp_device_info, NULL);
  4101. /* round down to number of usable stripes */
  4102. ndevs = round_down(ndevs, devs_increment);
  4103. if (ndevs < devs_min) {
  4104. ret = -ENOSPC;
  4105. if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
  4106. btrfs_debug(info,
  4107. "%s: not enough devices with free space: have=%d minimum required=%d",
  4108. __func__, ndevs, devs_min);
  4109. }
  4110. goto error;
  4111. }
  4112. ndevs = min(ndevs, devs_max);
  4113. /*
  4114. * The primary goal is to maximize the number of stripes, so use as
  4115. * many devices as possible, even if the stripes are not maximum sized.
  4116. *
  4117. * The DUP profile stores more than one stripe per device, the
  4118. * max_avail is the total size so we have to adjust.
  4119. */
  4120. stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
  4121. num_stripes = ndevs * dev_stripes;
  4122. /*
  4123. * this will have to be fixed for RAID1 and RAID10 over
  4124. * more drives
  4125. */
  4126. data_stripes = num_stripes / ncopies;
  4127. if (type & BTRFS_BLOCK_GROUP_RAID5)
  4128. data_stripes = num_stripes - 1;
  4129. if (type & BTRFS_BLOCK_GROUP_RAID6)
  4130. data_stripes = num_stripes - 2;
  4131. /*
  4132. * Use the number of data stripes to figure out how big this chunk
  4133. * is really going to be in terms of logical address space,
  4134. * and compare that answer with the max chunk size. If it's higher,
  4135. * we try to reduce stripe_size.
  4136. */
  4137. if (stripe_size * data_stripes > max_chunk_size) {
  4138. /*
  4139. * Reduce stripe_size, round it up to a 16MB boundary again and
  4140. * then use it, unless it ends up being even bigger than the
  4141. * previous value we had already.
  4142. */
  4143. stripe_size = min(round_up(div_u64(max_chunk_size,
  4144. data_stripes), SZ_16M),
  4145. stripe_size);
  4146. }
  4147. /* align to BTRFS_STRIPE_LEN */
  4148. stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
  4149. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  4150. if (!map) {
  4151. ret = -ENOMEM;
  4152. goto error;
  4153. }
  4154. map->num_stripes = num_stripes;
  4155. for (i = 0; i < ndevs; ++i) {
  4156. for (j = 0; j < dev_stripes; ++j) {
  4157. int s = i * dev_stripes + j;
  4158. map->stripes[s].dev = devices_info[i].dev;
  4159. map->stripes[s].physical = devices_info[i].dev_offset +
  4160. j * stripe_size;
  4161. }
  4162. }
  4163. map->stripe_len = BTRFS_STRIPE_LEN;
  4164. map->io_align = BTRFS_STRIPE_LEN;
  4165. map->io_width = BTRFS_STRIPE_LEN;
  4166. map->type = type;
  4167. map->sub_stripes = sub_stripes;
  4168. num_bytes = stripe_size * data_stripes;
  4169. trace_btrfs_chunk_alloc(info, map, start, num_bytes);
  4170. em = alloc_extent_map();
  4171. if (!em) {
  4172. kfree(map);
  4173. ret = -ENOMEM;
  4174. goto error;
  4175. }
  4176. set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
  4177. em->map_lookup = map;
  4178. em->start = start;
  4179. em->len = num_bytes;
  4180. em->block_start = 0;
  4181. em->block_len = em->len;
  4182. em->orig_block_len = stripe_size;
  4183. em_tree = &info->mapping_tree.map_tree;
  4184. write_lock(&em_tree->lock);
  4185. ret = add_extent_mapping(em_tree, em, 0);
  4186. if (ret) {
  4187. write_unlock(&em_tree->lock);
  4188. free_extent_map(em);
  4189. goto error;
  4190. }
  4191. list_add_tail(&em->list, &trans->transaction->pending_chunks);
  4192. refcount_inc(&em->refs);
  4193. write_unlock(&em_tree->lock);
  4194. ret = btrfs_make_block_group(trans, 0, type, start, num_bytes);
  4195. if (ret)
  4196. goto error_del_extent;
  4197. for (i = 0; i < map->num_stripes; i++) {
  4198. num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
  4199. btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
  4200. }
  4201. atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
  4202. free_extent_map(em);
  4203. check_raid56_incompat_flag(info, type);
  4204. kfree(devices_info);
  4205. return 0;
  4206. error_del_extent:
  4207. write_lock(&em_tree->lock);
  4208. remove_extent_mapping(em_tree, em);
  4209. write_unlock(&em_tree->lock);
  4210. /* One for our allocation */
  4211. free_extent_map(em);
  4212. /* One for the tree reference */
  4213. free_extent_map(em);
  4214. /* One for the pending_chunks list reference */
  4215. free_extent_map(em);
  4216. error:
  4217. kfree(devices_info);
  4218. return ret;
  4219. }
  4220. int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
  4221. u64 chunk_offset, u64 chunk_size)
  4222. {
  4223. struct btrfs_fs_info *fs_info = trans->fs_info;
  4224. struct btrfs_root *extent_root = fs_info->extent_root;
  4225. struct btrfs_root *chunk_root = fs_info->chunk_root;
  4226. struct btrfs_key key;
  4227. struct btrfs_device *device;
  4228. struct btrfs_chunk *chunk;
  4229. struct btrfs_stripe *stripe;
  4230. struct extent_map *em;
  4231. struct map_lookup *map;
  4232. size_t item_size;
  4233. u64 dev_offset;
  4234. u64 stripe_size;
  4235. int i = 0;
  4236. int ret = 0;
  4237. em = get_chunk_map(fs_info, chunk_offset, chunk_size);
  4238. if (IS_ERR(em))
  4239. return PTR_ERR(em);
  4240. map = em->map_lookup;
  4241. item_size = btrfs_chunk_item_size(map->num_stripes);
  4242. stripe_size = em->orig_block_len;
  4243. chunk = kzalloc(item_size, GFP_NOFS);
  4244. if (!chunk) {
  4245. ret = -ENOMEM;
  4246. goto out;
  4247. }
  4248. /*
  4249. * Take the device list mutex to prevent races with the final phase of
  4250. * a device replace operation that replaces the device object associated
  4251. * with the map's stripes, because the device object's id can change
  4252. * at any time during that final phase of the device replace operation
  4253. * (dev-replace.c:btrfs_dev_replace_finishing()).
  4254. */
  4255. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  4256. for (i = 0; i < map->num_stripes; i++) {
  4257. device = map->stripes[i].dev;
  4258. dev_offset = map->stripes[i].physical;
  4259. ret = btrfs_update_device(trans, device);
  4260. if (ret)
  4261. break;
  4262. ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
  4263. dev_offset, stripe_size);
  4264. if (ret)
  4265. break;
  4266. }
  4267. if (ret) {
  4268. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  4269. goto out;
  4270. }
  4271. stripe = &chunk->stripe;
  4272. for (i = 0; i < map->num_stripes; i++) {
  4273. device = map->stripes[i].dev;
  4274. dev_offset = map->stripes[i].physical;
  4275. btrfs_set_stack_stripe_devid(stripe, device->devid);
  4276. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  4277. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  4278. stripe++;
  4279. }
  4280. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  4281. btrfs_set_stack_chunk_length(chunk, chunk_size);
  4282. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  4283. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  4284. btrfs_set_stack_chunk_type(chunk, map->type);
  4285. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  4286. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  4287. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  4288. btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
  4289. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  4290. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  4291. key.type = BTRFS_CHUNK_ITEM_KEY;
  4292. key.offset = chunk_offset;
  4293. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  4294. if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  4295. /*
  4296. * TODO: Cleanup of inserted chunk root in case of
  4297. * failure.
  4298. */
  4299. ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
  4300. }
  4301. out:
  4302. kfree(chunk);
  4303. free_extent_map(em);
  4304. return ret;
  4305. }
  4306. /*
  4307. * Chunk allocation falls into two parts. The first part does works
  4308. * that make the new allocated chunk useable, but not do any operation
  4309. * that modifies the chunk tree. The second part does the works that
  4310. * require modifying the chunk tree. This division is important for the
  4311. * bootstrap process of adding storage to a seed btrfs.
  4312. */
  4313. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
  4314. {
  4315. u64 chunk_offset;
  4316. lockdep_assert_held(&trans->fs_info->chunk_mutex);
  4317. chunk_offset = find_next_chunk(trans->fs_info);
  4318. return __btrfs_alloc_chunk(trans, chunk_offset, type);
  4319. }
  4320. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  4321. struct btrfs_fs_info *fs_info)
  4322. {
  4323. u64 chunk_offset;
  4324. u64 sys_chunk_offset;
  4325. u64 alloc_profile;
  4326. int ret;
  4327. chunk_offset = find_next_chunk(fs_info);
  4328. alloc_profile = btrfs_metadata_alloc_profile(fs_info);
  4329. ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
  4330. if (ret)
  4331. return ret;
  4332. sys_chunk_offset = find_next_chunk(fs_info);
  4333. alloc_profile = btrfs_system_alloc_profile(fs_info);
  4334. ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
  4335. return ret;
  4336. }
  4337. static inline int btrfs_chunk_max_errors(struct map_lookup *map)
  4338. {
  4339. int max_errors;
  4340. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  4341. BTRFS_BLOCK_GROUP_RAID10 |
  4342. BTRFS_BLOCK_GROUP_RAID5 |
  4343. BTRFS_BLOCK_GROUP_DUP)) {
  4344. max_errors = 1;
  4345. } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
  4346. max_errors = 2;
  4347. } else {
  4348. max_errors = 0;
  4349. }
  4350. return max_errors;
  4351. }
  4352. int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
  4353. {
  4354. struct extent_map *em;
  4355. struct map_lookup *map;
  4356. int readonly = 0;
  4357. int miss_ndevs = 0;
  4358. int i;
  4359. em = get_chunk_map(fs_info, chunk_offset, 1);
  4360. if (IS_ERR(em))
  4361. return 1;
  4362. map = em->map_lookup;
  4363. for (i = 0; i < map->num_stripes; i++) {
  4364. if (test_bit(BTRFS_DEV_STATE_MISSING,
  4365. &map->stripes[i].dev->dev_state)) {
  4366. miss_ndevs++;
  4367. continue;
  4368. }
  4369. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
  4370. &map->stripes[i].dev->dev_state)) {
  4371. readonly = 1;
  4372. goto end;
  4373. }
  4374. }
  4375. /*
  4376. * If the number of missing devices is larger than max errors,
  4377. * we can not write the data into that chunk successfully, so
  4378. * set it readonly.
  4379. */
  4380. if (miss_ndevs > btrfs_chunk_max_errors(map))
  4381. readonly = 1;
  4382. end:
  4383. free_extent_map(em);
  4384. return readonly;
  4385. }
  4386. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  4387. {
  4388. extent_map_tree_init(&tree->map_tree);
  4389. }
  4390. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  4391. {
  4392. struct extent_map *em;
  4393. while (1) {
  4394. write_lock(&tree->map_tree.lock);
  4395. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  4396. if (em)
  4397. remove_extent_mapping(&tree->map_tree, em);
  4398. write_unlock(&tree->map_tree.lock);
  4399. if (!em)
  4400. break;
  4401. /* once for us */
  4402. free_extent_map(em);
  4403. /* once for the tree */
  4404. free_extent_map(em);
  4405. }
  4406. }
  4407. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  4408. {
  4409. struct extent_map *em;
  4410. struct map_lookup *map;
  4411. int ret;
  4412. em = get_chunk_map(fs_info, logical, len);
  4413. if (IS_ERR(em))
  4414. /*
  4415. * We could return errors for these cases, but that could get
  4416. * ugly and we'd probably do the same thing which is just not do
  4417. * anything else and exit, so return 1 so the callers don't try
  4418. * to use other copies.
  4419. */
  4420. return 1;
  4421. map = em->map_lookup;
  4422. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  4423. ret = map->num_stripes;
  4424. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  4425. ret = map->sub_stripes;
  4426. else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
  4427. ret = 2;
  4428. else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  4429. /*
  4430. * There could be two corrupted data stripes, we need
  4431. * to loop retry in order to rebuild the correct data.
  4432. *
  4433. * Fail a stripe at a time on every retry except the
  4434. * stripe under reconstruction.
  4435. */
  4436. ret = map->num_stripes;
  4437. else
  4438. ret = 1;
  4439. free_extent_map(em);
  4440. btrfs_dev_replace_read_lock(&fs_info->dev_replace);
  4441. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
  4442. fs_info->dev_replace.tgtdev)
  4443. ret++;
  4444. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  4445. return ret;
  4446. }
  4447. unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
  4448. u64 logical)
  4449. {
  4450. struct extent_map *em;
  4451. struct map_lookup *map;
  4452. unsigned long len = fs_info->sectorsize;
  4453. em = get_chunk_map(fs_info, logical, len);
  4454. if (!WARN_ON(IS_ERR(em))) {
  4455. map = em->map_lookup;
  4456. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  4457. len = map->stripe_len * nr_data_stripes(map);
  4458. free_extent_map(em);
  4459. }
  4460. return len;
  4461. }
  4462. int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  4463. {
  4464. struct extent_map *em;
  4465. struct map_lookup *map;
  4466. int ret = 0;
  4467. em = get_chunk_map(fs_info, logical, len);
  4468. if(!WARN_ON(IS_ERR(em))) {
  4469. map = em->map_lookup;
  4470. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  4471. ret = 1;
  4472. free_extent_map(em);
  4473. }
  4474. return ret;
  4475. }
  4476. static int find_live_mirror(struct btrfs_fs_info *fs_info,
  4477. struct map_lookup *map, int first,
  4478. int dev_replace_is_ongoing)
  4479. {
  4480. int i;
  4481. int num_stripes;
  4482. int preferred_mirror;
  4483. int tolerance;
  4484. struct btrfs_device *srcdev;
  4485. ASSERT((map->type &
  4486. (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
  4487. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  4488. num_stripes = map->sub_stripes;
  4489. else
  4490. num_stripes = map->num_stripes;
  4491. preferred_mirror = first + current->pid % num_stripes;
  4492. if (dev_replace_is_ongoing &&
  4493. fs_info->dev_replace.cont_reading_from_srcdev_mode ==
  4494. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
  4495. srcdev = fs_info->dev_replace.srcdev;
  4496. else
  4497. srcdev = NULL;
  4498. /*
  4499. * try to avoid the drive that is the source drive for a
  4500. * dev-replace procedure, only choose it if no other non-missing
  4501. * mirror is available
  4502. */
  4503. for (tolerance = 0; tolerance < 2; tolerance++) {
  4504. if (map->stripes[preferred_mirror].dev->bdev &&
  4505. (tolerance || map->stripes[preferred_mirror].dev != srcdev))
  4506. return preferred_mirror;
  4507. for (i = first; i < first + num_stripes; i++) {
  4508. if (map->stripes[i].dev->bdev &&
  4509. (tolerance || map->stripes[i].dev != srcdev))
  4510. return i;
  4511. }
  4512. }
  4513. /* we couldn't find one that doesn't fail. Just return something
  4514. * and the io error handling code will clean up eventually
  4515. */
  4516. return preferred_mirror;
  4517. }
  4518. static inline int parity_smaller(u64 a, u64 b)
  4519. {
  4520. return a > b;
  4521. }
  4522. /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
  4523. static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
  4524. {
  4525. struct btrfs_bio_stripe s;
  4526. int i;
  4527. u64 l;
  4528. int again = 1;
  4529. while (again) {
  4530. again = 0;
  4531. for (i = 0; i < num_stripes - 1; i++) {
  4532. if (parity_smaller(bbio->raid_map[i],
  4533. bbio->raid_map[i+1])) {
  4534. s = bbio->stripes[i];
  4535. l = bbio->raid_map[i];
  4536. bbio->stripes[i] = bbio->stripes[i+1];
  4537. bbio->raid_map[i] = bbio->raid_map[i+1];
  4538. bbio->stripes[i+1] = s;
  4539. bbio->raid_map[i+1] = l;
  4540. again = 1;
  4541. }
  4542. }
  4543. }
  4544. }
  4545. static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
  4546. {
  4547. struct btrfs_bio *bbio = kzalloc(
  4548. /* the size of the btrfs_bio */
  4549. sizeof(struct btrfs_bio) +
  4550. /* plus the variable array for the stripes */
  4551. sizeof(struct btrfs_bio_stripe) * (total_stripes) +
  4552. /* plus the variable array for the tgt dev */
  4553. sizeof(int) * (real_stripes) +
  4554. /*
  4555. * plus the raid_map, which includes both the tgt dev
  4556. * and the stripes
  4557. */
  4558. sizeof(u64) * (total_stripes),
  4559. GFP_NOFS|__GFP_NOFAIL);
  4560. atomic_set(&bbio->error, 0);
  4561. refcount_set(&bbio->refs, 1);
  4562. return bbio;
  4563. }
  4564. void btrfs_get_bbio(struct btrfs_bio *bbio)
  4565. {
  4566. WARN_ON(!refcount_read(&bbio->refs));
  4567. refcount_inc(&bbio->refs);
  4568. }
  4569. void btrfs_put_bbio(struct btrfs_bio *bbio)
  4570. {
  4571. if (!bbio)
  4572. return;
  4573. if (refcount_dec_and_test(&bbio->refs))
  4574. kfree(bbio);
  4575. }
  4576. /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
  4577. /*
  4578. * Please note that, discard won't be sent to target device of device
  4579. * replace.
  4580. */
  4581. static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
  4582. u64 logical, u64 length,
  4583. struct btrfs_bio **bbio_ret)
  4584. {
  4585. struct extent_map *em;
  4586. struct map_lookup *map;
  4587. struct btrfs_bio *bbio;
  4588. u64 offset;
  4589. u64 stripe_nr;
  4590. u64 stripe_nr_end;
  4591. u64 stripe_end_offset;
  4592. u64 stripe_cnt;
  4593. u64 stripe_len;
  4594. u64 stripe_offset;
  4595. u64 num_stripes;
  4596. u32 stripe_index;
  4597. u32 factor = 0;
  4598. u32 sub_stripes = 0;
  4599. u64 stripes_per_dev = 0;
  4600. u32 remaining_stripes = 0;
  4601. u32 last_stripe = 0;
  4602. int ret = 0;
  4603. int i;
  4604. /* discard always return a bbio */
  4605. ASSERT(bbio_ret);
  4606. em = get_chunk_map(fs_info, logical, length);
  4607. if (IS_ERR(em))
  4608. return PTR_ERR(em);
  4609. map = em->map_lookup;
  4610. /* we don't discard raid56 yet */
  4611. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  4612. ret = -EOPNOTSUPP;
  4613. goto out;
  4614. }
  4615. offset = logical - em->start;
  4616. length = min_t(u64, em->len - offset, length);
  4617. stripe_len = map->stripe_len;
  4618. /*
  4619. * stripe_nr counts the total number of stripes we have to stride
  4620. * to get to this block
  4621. */
  4622. stripe_nr = div64_u64(offset, stripe_len);
  4623. /* stripe_offset is the offset of this block in its stripe */
  4624. stripe_offset = offset - stripe_nr * stripe_len;
  4625. stripe_nr_end = round_up(offset + length, map->stripe_len);
  4626. stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
  4627. stripe_cnt = stripe_nr_end - stripe_nr;
  4628. stripe_end_offset = stripe_nr_end * map->stripe_len -
  4629. (offset + length);
  4630. /*
  4631. * after this, stripe_nr is the number of stripes on this
  4632. * device we have to walk to find the data, and stripe_index is
  4633. * the number of our device in the stripe array
  4634. */
  4635. num_stripes = 1;
  4636. stripe_index = 0;
  4637. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  4638. BTRFS_BLOCK_GROUP_RAID10)) {
  4639. if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4640. sub_stripes = 1;
  4641. else
  4642. sub_stripes = map->sub_stripes;
  4643. factor = map->num_stripes / sub_stripes;
  4644. num_stripes = min_t(u64, map->num_stripes,
  4645. sub_stripes * stripe_cnt);
  4646. stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
  4647. stripe_index *= sub_stripes;
  4648. stripes_per_dev = div_u64_rem(stripe_cnt, factor,
  4649. &remaining_stripes);
  4650. div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
  4651. last_stripe *= sub_stripes;
  4652. } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  4653. BTRFS_BLOCK_GROUP_DUP)) {
  4654. num_stripes = map->num_stripes;
  4655. } else {
  4656. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
  4657. &stripe_index);
  4658. }
  4659. bbio = alloc_btrfs_bio(num_stripes, 0);
  4660. if (!bbio) {
  4661. ret = -ENOMEM;
  4662. goto out;
  4663. }
  4664. for (i = 0; i < num_stripes; i++) {
  4665. bbio->stripes[i].physical =
  4666. map->stripes[stripe_index].physical +
  4667. stripe_offset + stripe_nr * map->stripe_len;
  4668. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  4669. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  4670. BTRFS_BLOCK_GROUP_RAID10)) {
  4671. bbio->stripes[i].length = stripes_per_dev *
  4672. map->stripe_len;
  4673. if (i / sub_stripes < remaining_stripes)
  4674. bbio->stripes[i].length +=
  4675. map->stripe_len;
  4676. /*
  4677. * Special for the first stripe and
  4678. * the last stripe:
  4679. *
  4680. * |-------|...|-------|
  4681. * |----------|
  4682. * off end_off
  4683. */
  4684. if (i < sub_stripes)
  4685. bbio->stripes[i].length -=
  4686. stripe_offset;
  4687. if (stripe_index >= last_stripe &&
  4688. stripe_index <= (last_stripe +
  4689. sub_stripes - 1))
  4690. bbio->stripes[i].length -=
  4691. stripe_end_offset;
  4692. if (i == sub_stripes - 1)
  4693. stripe_offset = 0;
  4694. } else {
  4695. bbio->stripes[i].length = length;
  4696. }
  4697. stripe_index++;
  4698. if (stripe_index == map->num_stripes) {
  4699. stripe_index = 0;
  4700. stripe_nr++;
  4701. }
  4702. }
  4703. *bbio_ret = bbio;
  4704. bbio->map_type = map->type;
  4705. bbio->num_stripes = num_stripes;
  4706. out:
  4707. free_extent_map(em);
  4708. return ret;
  4709. }
  4710. /*
  4711. * In dev-replace case, for repair case (that's the only case where the mirror
  4712. * is selected explicitly when calling btrfs_map_block), blocks left of the
  4713. * left cursor can also be read from the target drive.
  4714. *
  4715. * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
  4716. * array of stripes.
  4717. * For READ, it also needs to be supported using the same mirror number.
  4718. *
  4719. * If the requested block is not left of the left cursor, EIO is returned. This
  4720. * can happen because btrfs_num_copies() returns one more in the dev-replace
  4721. * case.
  4722. */
  4723. static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
  4724. u64 logical, u64 length,
  4725. u64 srcdev_devid, int *mirror_num,
  4726. u64 *physical)
  4727. {
  4728. struct btrfs_bio *bbio = NULL;
  4729. int num_stripes;
  4730. int index_srcdev = 0;
  4731. int found = 0;
  4732. u64 physical_of_found = 0;
  4733. int i;
  4734. int ret = 0;
  4735. ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
  4736. logical, &length, &bbio, 0, 0);
  4737. if (ret) {
  4738. ASSERT(bbio == NULL);
  4739. return ret;
  4740. }
  4741. num_stripes = bbio->num_stripes;
  4742. if (*mirror_num > num_stripes) {
  4743. /*
  4744. * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
  4745. * that means that the requested area is not left of the left
  4746. * cursor
  4747. */
  4748. btrfs_put_bbio(bbio);
  4749. return -EIO;
  4750. }
  4751. /*
  4752. * process the rest of the function using the mirror_num of the source
  4753. * drive. Therefore look it up first. At the end, patch the device
  4754. * pointer to the one of the target drive.
  4755. */
  4756. for (i = 0; i < num_stripes; i++) {
  4757. if (bbio->stripes[i].dev->devid != srcdev_devid)
  4758. continue;
  4759. /*
  4760. * In case of DUP, in order to keep it simple, only add the
  4761. * mirror with the lowest physical address
  4762. */
  4763. if (found &&
  4764. physical_of_found <= bbio->stripes[i].physical)
  4765. continue;
  4766. index_srcdev = i;
  4767. found = 1;
  4768. physical_of_found = bbio->stripes[i].physical;
  4769. }
  4770. btrfs_put_bbio(bbio);
  4771. ASSERT(found);
  4772. if (!found)
  4773. return -EIO;
  4774. *mirror_num = index_srcdev + 1;
  4775. *physical = physical_of_found;
  4776. return ret;
  4777. }
  4778. static void handle_ops_on_dev_replace(enum btrfs_map_op op,
  4779. struct btrfs_bio **bbio_ret,
  4780. struct btrfs_dev_replace *dev_replace,
  4781. int *num_stripes_ret, int *max_errors_ret)
  4782. {
  4783. struct btrfs_bio *bbio = *bbio_ret;
  4784. u64 srcdev_devid = dev_replace->srcdev->devid;
  4785. int tgtdev_indexes = 0;
  4786. int num_stripes = *num_stripes_ret;
  4787. int max_errors = *max_errors_ret;
  4788. int i;
  4789. if (op == BTRFS_MAP_WRITE) {
  4790. int index_where_to_add;
  4791. /*
  4792. * duplicate the write operations while the dev replace
  4793. * procedure is running. Since the copying of the old disk to
  4794. * the new disk takes place at run time while the filesystem is
  4795. * mounted writable, the regular write operations to the old
  4796. * disk have to be duplicated to go to the new disk as well.
  4797. *
  4798. * Note that device->missing is handled by the caller, and that
  4799. * the write to the old disk is already set up in the stripes
  4800. * array.
  4801. */
  4802. index_where_to_add = num_stripes;
  4803. for (i = 0; i < num_stripes; i++) {
  4804. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4805. /* write to new disk, too */
  4806. struct btrfs_bio_stripe *new =
  4807. bbio->stripes + index_where_to_add;
  4808. struct btrfs_bio_stripe *old =
  4809. bbio->stripes + i;
  4810. new->physical = old->physical;
  4811. new->length = old->length;
  4812. new->dev = dev_replace->tgtdev;
  4813. bbio->tgtdev_map[i] = index_where_to_add;
  4814. index_where_to_add++;
  4815. max_errors++;
  4816. tgtdev_indexes++;
  4817. }
  4818. }
  4819. num_stripes = index_where_to_add;
  4820. } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
  4821. int index_srcdev = 0;
  4822. int found = 0;
  4823. u64 physical_of_found = 0;
  4824. /*
  4825. * During the dev-replace procedure, the target drive can also
  4826. * be used to read data in case it is needed to repair a corrupt
  4827. * block elsewhere. This is possible if the requested area is
  4828. * left of the left cursor. In this area, the target drive is a
  4829. * full copy of the source drive.
  4830. */
  4831. for (i = 0; i < num_stripes; i++) {
  4832. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4833. /*
  4834. * In case of DUP, in order to keep it simple,
  4835. * only add the mirror with the lowest physical
  4836. * address
  4837. */
  4838. if (found &&
  4839. physical_of_found <=
  4840. bbio->stripes[i].physical)
  4841. continue;
  4842. index_srcdev = i;
  4843. found = 1;
  4844. physical_of_found = bbio->stripes[i].physical;
  4845. }
  4846. }
  4847. if (found) {
  4848. struct btrfs_bio_stripe *tgtdev_stripe =
  4849. bbio->stripes + num_stripes;
  4850. tgtdev_stripe->physical = physical_of_found;
  4851. tgtdev_stripe->length =
  4852. bbio->stripes[index_srcdev].length;
  4853. tgtdev_stripe->dev = dev_replace->tgtdev;
  4854. bbio->tgtdev_map[index_srcdev] = num_stripes;
  4855. tgtdev_indexes++;
  4856. num_stripes++;
  4857. }
  4858. }
  4859. *num_stripes_ret = num_stripes;
  4860. *max_errors_ret = max_errors;
  4861. bbio->num_tgtdevs = tgtdev_indexes;
  4862. *bbio_ret = bbio;
  4863. }
  4864. static bool need_full_stripe(enum btrfs_map_op op)
  4865. {
  4866. return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
  4867. }
  4868. static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
  4869. enum btrfs_map_op op,
  4870. u64 logical, u64 *length,
  4871. struct btrfs_bio **bbio_ret,
  4872. int mirror_num, int need_raid_map)
  4873. {
  4874. struct extent_map *em;
  4875. struct map_lookup *map;
  4876. u64 offset;
  4877. u64 stripe_offset;
  4878. u64 stripe_nr;
  4879. u64 stripe_len;
  4880. u32 stripe_index;
  4881. int i;
  4882. int ret = 0;
  4883. int num_stripes;
  4884. int max_errors = 0;
  4885. int tgtdev_indexes = 0;
  4886. struct btrfs_bio *bbio = NULL;
  4887. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  4888. int dev_replace_is_ongoing = 0;
  4889. int num_alloc_stripes;
  4890. int patch_the_first_stripe_for_dev_replace = 0;
  4891. u64 physical_to_patch_in_first_stripe = 0;
  4892. u64 raid56_full_stripe_start = (u64)-1;
  4893. if (op == BTRFS_MAP_DISCARD)
  4894. return __btrfs_map_block_for_discard(fs_info, logical,
  4895. *length, bbio_ret);
  4896. em = get_chunk_map(fs_info, logical, *length);
  4897. if (IS_ERR(em))
  4898. return PTR_ERR(em);
  4899. map = em->map_lookup;
  4900. offset = logical - em->start;
  4901. stripe_len = map->stripe_len;
  4902. stripe_nr = offset;
  4903. /*
  4904. * stripe_nr counts the total number of stripes we have to stride
  4905. * to get to this block
  4906. */
  4907. stripe_nr = div64_u64(stripe_nr, stripe_len);
  4908. stripe_offset = stripe_nr * stripe_len;
  4909. if (offset < stripe_offset) {
  4910. btrfs_crit(fs_info,
  4911. "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
  4912. stripe_offset, offset, em->start, logical,
  4913. stripe_len);
  4914. free_extent_map(em);
  4915. return -EINVAL;
  4916. }
  4917. /* stripe_offset is the offset of this block in its stripe*/
  4918. stripe_offset = offset - stripe_offset;
  4919. /* if we're here for raid56, we need to know the stripe aligned start */
  4920. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  4921. unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
  4922. raid56_full_stripe_start = offset;
  4923. /* allow a write of a full stripe, but make sure we don't
  4924. * allow straddling of stripes
  4925. */
  4926. raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
  4927. full_stripe_len);
  4928. raid56_full_stripe_start *= full_stripe_len;
  4929. }
  4930. if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  4931. u64 max_len;
  4932. /* For writes to RAID[56], allow a full stripeset across all disks.
  4933. For other RAID types and for RAID[56] reads, just allow a single
  4934. stripe (on a single disk). */
  4935. if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
  4936. (op == BTRFS_MAP_WRITE)) {
  4937. max_len = stripe_len * nr_data_stripes(map) -
  4938. (offset - raid56_full_stripe_start);
  4939. } else {
  4940. /* we limit the length of each bio to what fits in a stripe */
  4941. max_len = stripe_len - stripe_offset;
  4942. }
  4943. *length = min_t(u64, em->len - offset, max_len);
  4944. } else {
  4945. *length = em->len - offset;
  4946. }
  4947. /* This is for when we're called from btrfs_merge_bio_hook() and all
  4948. it cares about is the length */
  4949. if (!bbio_ret)
  4950. goto out;
  4951. btrfs_dev_replace_read_lock(dev_replace);
  4952. dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
  4953. if (!dev_replace_is_ongoing)
  4954. btrfs_dev_replace_read_unlock(dev_replace);
  4955. else
  4956. btrfs_dev_replace_set_lock_blocking(dev_replace);
  4957. if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
  4958. !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
  4959. ret = get_extra_mirror_from_replace(fs_info, logical, *length,
  4960. dev_replace->srcdev->devid,
  4961. &mirror_num,
  4962. &physical_to_patch_in_first_stripe);
  4963. if (ret)
  4964. goto out;
  4965. else
  4966. patch_the_first_stripe_for_dev_replace = 1;
  4967. } else if (mirror_num > map->num_stripes) {
  4968. mirror_num = 0;
  4969. }
  4970. num_stripes = 1;
  4971. stripe_index = 0;
  4972. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  4973. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
  4974. &stripe_index);
  4975. if (!need_full_stripe(op))
  4976. mirror_num = 1;
  4977. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  4978. if (need_full_stripe(op))
  4979. num_stripes = map->num_stripes;
  4980. else if (mirror_num)
  4981. stripe_index = mirror_num - 1;
  4982. else {
  4983. stripe_index = find_live_mirror(fs_info, map, 0,
  4984. dev_replace_is_ongoing);
  4985. mirror_num = stripe_index + 1;
  4986. }
  4987. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  4988. if (need_full_stripe(op)) {
  4989. num_stripes = map->num_stripes;
  4990. } else if (mirror_num) {
  4991. stripe_index = mirror_num - 1;
  4992. } else {
  4993. mirror_num = 1;
  4994. }
  4995. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  4996. u32 factor = map->num_stripes / map->sub_stripes;
  4997. stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
  4998. stripe_index *= map->sub_stripes;
  4999. if (need_full_stripe(op))
  5000. num_stripes = map->sub_stripes;
  5001. else if (mirror_num)
  5002. stripe_index += mirror_num - 1;
  5003. else {
  5004. int old_stripe_index = stripe_index;
  5005. stripe_index = find_live_mirror(fs_info, map,
  5006. stripe_index,
  5007. dev_replace_is_ongoing);
  5008. mirror_num = stripe_index - old_stripe_index + 1;
  5009. }
  5010. } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  5011. if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
  5012. /* push stripe_nr back to the start of the full stripe */
  5013. stripe_nr = div64_u64(raid56_full_stripe_start,
  5014. stripe_len * nr_data_stripes(map));
  5015. /* RAID[56] write or recovery. Return all stripes */
  5016. num_stripes = map->num_stripes;
  5017. max_errors = nr_parity_stripes(map);
  5018. *length = map->stripe_len;
  5019. stripe_index = 0;
  5020. stripe_offset = 0;
  5021. } else {
  5022. /*
  5023. * Mirror #0 or #1 means the original data block.
  5024. * Mirror #2 is RAID5 parity block.
  5025. * Mirror #3 is RAID6 Q block.
  5026. */
  5027. stripe_nr = div_u64_rem(stripe_nr,
  5028. nr_data_stripes(map), &stripe_index);
  5029. if (mirror_num > 1)
  5030. stripe_index = nr_data_stripes(map) +
  5031. mirror_num - 2;
  5032. /* We distribute the parity blocks across stripes */
  5033. div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
  5034. &stripe_index);
  5035. if (!need_full_stripe(op) && mirror_num <= 1)
  5036. mirror_num = 1;
  5037. }
  5038. } else {
  5039. /*
  5040. * after this, stripe_nr is the number of stripes on this
  5041. * device we have to walk to find the data, and stripe_index is
  5042. * the number of our device in the stripe array
  5043. */
  5044. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
  5045. &stripe_index);
  5046. mirror_num = stripe_index + 1;
  5047. }
  5048. if (stripe_index >= map->num_stripes) {
  5049. btrfs_crit(fs_info,
  5050. "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
  5051. stripe_index, map->num_stripes);
  5052. ret = -EINVAL;
  5053. goto out;
  5054. }
  5055. num_alloc_stripes = num_stripes;
  5056. if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
  5057. if (op == BTRFS_MAP_WRITE)
  5058. num_alloc_stripes <<= 1;
  5059. if (op == BTRFS_MAP_GET_READ_MIRRORS)
  5060. num_alloc_stripes++;
  5061. tgtdev_indexes = num_stripes;
  5062. }
  5063. bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
  5064. if (!bbio) {
  5065. ret = -ENOMEM;
  5066. goto out;
  5067. }
  5068. if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
  5069. bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
  5070. /* build raid_map */
  5071. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
  5072. (need_full_stripe(op) || mirror_num > 1)) {
  5073. u64 tmp;
  5074. unsigned rot;
  5075. bbio->raid_map = (u64 *)((void *)bbio->stripes +
  5076. sizeof(struct btrfs_bio_stripe) *
  5077. num_alloc_stripes +
  5078. sizeof(int) * tgtdev_indexes);
  5079. /* Work out the disk rotation on this stripe-set */
  5080. div_u64_rem(stripe_nr, num_stripes, &rot);
  5081. /* Fill in the logical address of each stripe */
  5082. tmp = stripe_nr * nr_data_stripes(map);
  5083. for (i = 0; i < nr_data_stripes(map); i++)
  5084. bbio->raid_map[(i+rot) % num_stripes] =
  5085. em->start + (tmp + i) * map->stripe_len;
  5086. bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
  5087. if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  5088. bbio->raid_map[(i+rot+1) % num_stripes] =
  5089. RAID6_Q_STRIPE;
  5090. }
  5091. for (i = 0; i < num_stripes; i++) {
  5092. bbio->stripes[i].physical =
  5093. map->stripes[stripe_index].physical +
  5094. stripe_offset +
  5095. stripe_nr * map->stripe_len;
  5096. bbio->stripes[i].dev =
  5097. map->stripes[stripe_index].dev;
  5098. stripe_index++;
  5099. }
  5100. if (need_full_stripe(op))
  5101. max_errors = btrfs_chunk_max_errors(map);
  5102. if (bbio->raid_map)
  5103. sort_parity_stripes(bbio, num_stripes);
  5104. if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
  5105. need_full_stripe(op)) {
  5106. handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
  5107. &max_errors);
  5108. }
  5109. *bbio_ret = bbio;
  5110. bbio->map_type = map->type;
  5111. bbio->num_stripes = num_stripes;
  5112. bbio->max_errors = max_errors;
  5113. bbio->mirror_num = mirror_num;
  5114. /*
  5115. * this is the case that REQ_READ && dev_replace_is_ongoing &&
  5116. * mirror_num == num_stripes + 1 && dev_replace target drive is
  5117. * available as a mirror
  5118. */
  5119. if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
  5120. WARN_ON(num_stripes > 1);
  5121. bbio->stripes[0].dev = dev_replace->tgtdev;
  5122. bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
  5123. bbio->mirror_num = map->num_stripes + 1;
  5124. }
  5125. out:
  5126. if (dev_replace_is_ongoing) {
  5127. btrfs_dev_replace_clear_lock_blocking(dev_replace);
  5128. btrfs_dev_replace_read_unlock(dev_replace);
  5129. }
  5130. free_extent_map(em);
  5131. return ret;
  5132. }
  5133. int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
  5134. u64 logical, u64 *length,
  5135. struct btrfs_bio **bbio_ret, int mirror_num)
  5136. {
  5137. return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
  5138. mirror_num, 0);
  5139. }
  5140. /* For Scrub/replace */
  5141. int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
  5142. u64 logical, u64 *length,
  5143. struct btrfs_bio **bbio_ret)
  5144. {
  5145. return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
  5146. }
  5147. int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
  5148. u64 physical, u64 **logical, int *naddrs, int *stripe_len)
  5149. {
  5150. struct extent_map *em;
  5151. struct map_lookup *map;
  5152. u64 *buf;
  5153. u64 bytenr;
  5154. u64 length;
  5155. u64 stripe_nr;
  5156. u64 rmap_len;
  5157. int i, j, nr = 0;
  5158. em = get_chunk_map(fs_info, chunk_start, 1);
  5159. if (IS_ERR(em))
  5160. return -EIO;
  5161. map = em->map_lookup;
  5162. length = em->len;
  5163. rmap_len = map->stripe_len;
  5164. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  5165. length = div_u64(length, map->num_stripes / map->sub_stripes);
  5166. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  5167. length = div_u64(length, map->num_stripes);
  5168. else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  5169. length = div_u64(length, nr_data_stripes(map));
  5170. rmap_len = map->stripe_len * nr_data_stripes(map);
  5171. }
  5172. buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
  5173. BUG_ON(!buf); /* -ENOMEM */
  5174. for (i = 0; i < map->num_stripes; i++) {
  5175. if (map->stripes[i].physical > physical ||
  5176. map->stripes[i].physical + length <= physical)
  5177. continue;
  5178. stripe_nr = physical - map->stripes[i].physical;
  5179. stripe_nr = div64_u64(stripe_nr, map->stripe_len);
  5180. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  5181. stripe_nr = stripe_nr * map->num_stripes + i;
  5182. stripe_nr = div_u64(stripe_nr, map->sub_stripes);
  5183. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  5184. stripe_nr = stripe_nr * map->num_stripes + i;
  5185. } /* else if RAID[56], multiply by nr_data_stripes().
  5186. * Alternatively, just use rmap_len below instead of
  5187. * map->stripe_len */
  5188. bytenr = chunk_start + stripe_nr * rmap_len;
  5189. WARN_ON(nr >= map->num_stripes);
  5190. for (j = 0; j < nr; j++) {
  5191. if (buf[j] == bytenr)
  5192. break;
  5193. }
  5194. if (j == nr) {
  5195. WARN_ON(nr >= map->num_stripes);
  5196. buf[nr++] = bytenr;
  5197. }
  5198. }
  5199. *logical = buf;
  5200. *naddrs = nr;
  5201. *stripe_len = rmap_len;
  5202. free_extent_map(em);
  5203. return 0;
  5204. }
  5205. static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
  5206. {
  5207. bio->bi_private = bbio->private;
  5208. bio->bi_end_io = bbio->end_io;
  5209. bio_endio(bio);
  5210. btrfs_put_bbio(bbio);
  5211. }
  5212. static void btrfs_end_bio(struct bio *bio)
  5213. {
  5214. struct btrfs_bio *bbio = bio->bi_private;
  5215. int is_orig_bio = 0;
  5216. if (bio->bi_status) {
  5217. atomic_inc(&bbio->error);
  5218. if (bio->bi_status == BLK_STS_IOERR ||
  5219. bio->bi_status == BLK_STS_TARGET) {
  5220. unsigned int stripe_index =
  5221. btrfs_io_bio(bio)->stripe_index;
  5222. struct btrfs_device *dev;
  5223. BUG_ON(stripe_index >= bbio->num_stripes);
  5224. dev = bbio->stripes[stripe_index].dev;
  5225. if (dev->bdev) {
  5226. if (bio_op(bio) == REQ_OP_WRITE)
  5227. btrfs_dev_stat_inc_and_print(dev,
  5228. BTRFS_DEV_STAT_WRITE_ERRS);
  5229. else
  5230. btrfs_dev_stat_inc_and_print(dev,
  5231. BTRFS_DEV_STAT_READ_ERRS);
  5232. if (bio->bi_opf & REQ_PREFLUSH)
  5233. btrfs_dev_stat_inc_and_print(dev,
  5234. BTRFS_DEV_STAT_FLUSH_ERRS);
  5235. }
  5236. }
  5237. }
  5238. if (bio == bbio->orig_bio)
  5239. is_orig_bio = 1;
  5240. btrfs_bio_counter_dec(bbio->fs_info);
  5241. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  5242. if (!is_orig_bio) {
  5243. bio_put(bio);
  5244. bio = bbio->orig_bio;
  5245. }
  5246. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  5247. /* only send an error to the higher layers if it is
  5248. * beyond the tolerance of the btrfs bio
  5249. */
  5250. if (atomic_read(&bbio->error) > bbio->max_errors) {
  5251. bio->bi_status = BLK_STS_IOERR;
  5252. } else {
  5253. /*
  5254. * this bio is actually up to date, we didn't
  5255. * go over the max number of errors
  5256. */
  5257. bio->bi_status = BLK_STS_OK;
  5258. }
  5259. btrfs_end_bbio(bbio, bio);
  5260. } else if (!is_orig_bio) {
  5261. bio_put(bio);
  5262. }
  5263. }
  5264. /*
  5265. * see run_scheduled_bios for a description of why bios are collected for
  5266. * async submit.
  5267. *
  5268. * This will add one bio to the pending list for a device and make sure
  5269. * the work struct is scheduled.
  5270. */
  5271. static noinline void btrfs_schedule_bio(struct btrfs_device *device,
  5272. struct bio *bio)
  5273. {
  5274. struct btrfs_fs_info *fs_info = device->fs_info;
  5275. int should_queue = 1;
  5276. struct btrfs_pending_bios *pending_bios;
  5277. if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
  5278. !device->bdev) {
  5279. bio_io_error(bio);
  5280. return;
  5281. }
  5282. /* don't bother with additional async steps for reads, right now */
  5283. if (bio_op(bio) == REQ_OP_READ) {
  5284. btrfsic_submit_bio(bio);
  5285. return;
  5286. }
  5287. WARN_ON(bio->bi_next);
  5288. bio->bi_next = NULL;
  5289. spin_lock(&device->io_lock);
  5290. if (op_is_sync(bio->bi_opf))
  5291. pending_bios = &device->pending_sync_bios;
  5292. else
  5293. pending_bios = &device->pending_bios;
  5294. if (pending_bios->tail)
  5295. pending_bios->tail->bi_next = bio;
  5296. pending_bios->tail = bio;
  5297. if (!pending_bios->head)
  5298. pending_bios->head = bio;
  5299. if (device->running_pending)
  5300. should_queue = 0;
  5301. spin_unlock(&device->io_lock);
  5302. if (should_queue)
  5303. btrfs_queue_work(fs_info->submit_workers, &device->work);
  5304. }
  5305. static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
  5306. u64 physical, int dev_nr, int async)
  5307. {
  5308. struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
  5309. struct btrfs_fs_info *fs_info = bbio->fs_info;
  5310. bio->bi_private = bbio;
  5311. btrfs_io_bio(bio)->stripe_index = dev_nr;
  5312. bio->bi_end_io = btrfs_end_bio;
  5313. bio->bi_iter.bi_sector = physical >> 9;
  5314. btrfs_debug_in_rcu(fs_info,
  5315. "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
  5316. bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
  5317. (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
  5318. bio->bi_iter.bi_size);
  5319. bio_set_dev(bio, dev->bdev);
  5320. btrfs_bio_counter_inc_noblocked(fs_info);
  5321. if (async)
  5322. btrfs_schedule_bio(dev, bio);
  5323. else
  5324. btrfsic_submit_bio(bio);
  5325. }
  5326. static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
  5327. {
  5328. atomic_inc(&bbio->error);
  5329. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  5330. /* Should be the original bio. */
  5331. WARN_ON(bio != bbio->orig_bio);
  5332. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  5333. bio->bi_iter.bi_sector = logical >> 9;
  5334. if (atomic_read(&bbio->error) > bbio->max_errors)
  5335. bio->bi_status = BLK_STS_IOERR;
  5336. else
  5337. bio->bi_status = BLK_STS_OK;
  5338. btrfs_end_bbio(bbio, bio);
  5339. }
  5340. }
  5341. blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
  5342. int mirror_num, int async_submit)
  5343. {
  5344. struct btrfs_device *dev;
  5345. struct bio *first_bio = bio;
  5346. u64 logical = (u64)bio->bi_iter.bi_sector << 9;
  5347. u64 length = 0;
  5348. u64 map_length;
  5349. int ret;
  5350. int dev_nr;
  5351. int total_devs;
  5352. struct btrfs_bio *bbio = NULL;
  5353. length = bio->bi_iter.bi_size;
  5354. map_length = length;
  5355. btrfs_bio_counter_inc_blocked(fs_info);
  5356. ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
  5357. &map_length, &bbio, mirror_num, 1);
  5358. if (ret) {
  5359. btrfs_bio_counter_dec(fs_info);
  5360. return errno_to_blk_status(ret);
  5361. }
  5362. total_devs = bbio->num_stripes;
  5363. bbio->orig_bio = first_bio;
  5364. bbio->private = first_bio->bi_private;
  5365. bbio->end_io = first_bio->bi_end_io;
  5366. bbio->fs_info = fs_info;
  5367. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  5368. if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
  5369. ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
  5370. /* In this case, map_length has been set to the length of
  5371. a single stripe; not the whole write */
  5372. if (bio_op(bio) == REQ_OP_WRITE) {
  5373. ret = raid56_parity_write(fs_info, bio, bbio,
  5374. map_length);
  5375. } else {
  5376. ret = raid56_parity_recover(fs_info, bio, bbio,
  5377. map_length, mirror_num, 1);
  5378. }
  5379. btrfs_bio_counter_dec(fs_info);
  5380. return errno_to_blk_status(ret);
  5381. }
  5382. if (map_length < length) {
  5383. btrfs_crit(fs_info,
  5384. "mapping failed logical %llu bio len %llu len %llu",
  5385. logical, length, map_length);
  5386. BUG();
  5387. }
  5388. for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
  5389. dev = bbio->stripes[dev_nr].dev;
  5390. if (!dev || !dev->bdev ||
  5391. (bio_op(first_bio) == REQ_OP_WRITE &&
  5392. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
  5393. bbio_error(bbio, first_bio, logical);
  5394. continue;
  5395. }
  5396. if (dev_nr < total_devs - 1)
  5397. bio = btrfs_bio_clone(first_bio);
  5398. else
  5399. bio = first_bio;
  5400. submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
  5401. dev_nr, async_submit);
  5402. }
  5403. btrfs_bio_counter_dec(fs_info);
  5404. return BLK_STS_OK;
  5405. }
  5406. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  5407. u8 *uuid, u8 *fsid)
  5408. {
  5409. struct btrfs_device *device;
  5410. struct btrfs_fs_devices *cur_devices;
  5411. cur_devices = fs_info->fs_devices;
  5412. while (cur_devices) {
  5413. if (!fsid ||
  5414. !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
  5415. device = find_device(cur_devices, devid, uuid);
  5416. if (device)
  5417. return device;
  5418. }
  5419. cur_devices = cur_devices->seed;
  5420. }
  5421. return NULL;
  5422. }
  5423. static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
  5424. u64 devid, u8 *dev_uuid)
  5425. {
  5426. struct btrfs_device *device;
  5427. device = btrfs_alloc_device(NULL, &devid, dev_uuid);
  5428. if (IS_ERR(device))
  5429. return device;
  5430. list_add(&device->dev_list, &fs_devices->devices);
  5431. device->fs_devices = fs_devices;
  5432. fs_devices->num_devices++;
  5433. set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
  5434. fs_devices->missing_devices++;
  5435. return device;
  5436. }
  5437. /**
  5438. * btrfs_alloc_device - allocate struct btrfs_device
  5439. * @fs_info: used only for generating a new devid, can be NULL if
  5440. * devid is provided (i.e. @devid != NULL).
  5441. * @devid: a pointer to devid for this device. If NULL a new devid
  5442. * is generated.
  5443. * @uuid: a pointer to UUID for this device. If NULL a new UUID
  5444. * is generated.
  5445. *
  5446. * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
  5447. * on error. Returned struct is not linked onto any lists and must be
  5448. * destroyed with btrfs_free_device.
  5449. */
  5450. struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
  5451. const u64 *devid,
  5452. const u8 *uuid)
  5453. {
  5454. struct btrfs_device *dev;
  5455. u64 tmp;
  5456. if (WARN_ON(!devid && !fs_info))
  5457. return ERR_PTR(-EINVAL);
  5458. dev = __alloc_device();
  5459. if (IS_ERR(dev))
  5460. return dev;
  5461. if (devid)
  5462. tmp = *devid;
  5463. else {
  5464. int ret;
  5465. ret = find_next_devid(fs_info, &tmp);
  5466. if (ret) {
  5467. btrfs_free_device(dev);
  5468. return ERR_PTR(ret);
  5469. }
  5470. }
  5471. dev->devid = tmp;
  5472. if (uuid)
  5473. memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
  5474. else
  5475. generate_random_uuid(dev->uuid);
  5476. btrfs_init_work(&dev->work, btrfs_submit_helper,
  5477. pending_bios_fn, NULL, NULL);
  5478. return dev;
  5479. }
  5480. /* Return -EIO if any error, otherwise return 0. */
  5481. static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
  5482. struct extent_buffer *leaf,
  5483. struct btrfs_chunk *chunk, u64 logical)
  5484. {
  5485. u64 length;
  5486. u64 stripe_len;
  5487. u16 num_stripes;
  5488. u16 sub_stripes;
  5489. u64 type;
  5490. u64 features;
  5491. bool mixed = false;
  5492. length = btrfs_chunk_length(leaf, chunk);
  5493. stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  5494. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  5495. sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  5496. type = btrfs_chunk_type(leaf, chunk);
  5497. if (!num_stripes) {
  5498. btrfs_err(fs_info, "invalid chunk num_stripes: %u",
  5499. num_stripes);
  5500. return -EIO;
  5501. }
  5502. if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
  5503. btrfs_err(fs_info, "invalid chunk logical %llu", logical);
  5504. return -EIO;
  5505. }
  5506. if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
  5507. btrfs_err(fs_info, "invalid chunk sectorsize %u",
  5508. btrfs_chunk_sector_size(leaf, chunk));
  5509. return -EIO;
  5510. }
  5511. if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
  5512. btrfs_err(fs_info, "invalid chunk length %llu", length);
  5513. return -EIO;
  5514. }
  5515. if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
  5516. btrfs_err(fs_info, "invalid chunk stripe length: %llu",
  5517. stripe_len);
  5518. return -EIO;
  5519. }
  5520. if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
  5521. type) {
  5522. btrfs_err(fs_info, "unrecognized chunk type: %llu",
  5523. ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
  5524. BTRFS_BLOCK_GROUP_PROFILE_MASK) &
  5525. btrfs_chunk_type(leaf, chunk));
  5526. return -EIO;
  5527. }
  5528. if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
  5529. btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type);
  5530. return -EIO;
  5531. }
  5532. if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
  5533. (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
  5534. btrfs_err(fs_info,
  5535. "system chunk with data or metadata type: 0x%llx", type);
  5536. return -EIO;
  5537. }
  5538. features = btrfs_super_incompat_flags(fs_info->super_copy);
  5539. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  5540. mixed = true;
  5541. if (!mixed) {
  5542. if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
  5543. (type & BTRFS_BLOCK_GROUP_DATA)) {
  5544. btrfs_err(fs_info,
  5545. "mixed chunk type in non-mixed mode: 0x%llx", type);
  5546. return -EIO;
  5547. }
  5548. }
  5549. if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
  5550. (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
  5551. (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
  5552. (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
  5553. (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
  5554. ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
  5555. num_stripes != 1)) {
  5556. btrfs_err(fs_info,
  5557. "invalid num_stripes:sub_stripes %u:%u for profile %llu",
  5558. num_stripes, sub_stripes,
  5559. type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
  5560. return -EIO;
  5561. }
  5562. return 0;
  5563. }
  5564. static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
  5565. u64 devid, u8 *uuid, bool error)
  5566. {
  5567. if (error)
  5568. btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
  5569. devid, uuid);
  5570. else
  5571. btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
  5572. devid, uuid);
  5573. }
  5574. static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
  5575. struct extent_buffer *leaf,
  5576. struct btrfs_chunk *chunk)
  5577. {
  5578. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  5579. struct map_lookup *map;
  5580. struct extent_map *em;
  5581. u64 logical;
  5582. u64 length;
  5583. u64 devid;
  5584. u8 uuid[BTRFS_UUID_SIZE];
  5585. int num_stripes;
  5586. int ret;
  5587. int i;
  5588. logical = key->offset;
  5589. length = btrfs_chunk_length(leaf, chunk);
  5590. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  5591. ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
  5592. if (ret)
  5593. return ret;
  5594. read_lock(&map_tree->map_tree.lock);
  5595. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  5596. read_unlock(&map_tree->map_tree.lock);
  5597. /* already mapped? */
  5598. if (em && em->start <= logical && em->start + em->len > logical) {
  5599. free_extent_map(em);
  5600. return 0;
  5601. } else if (em) {
  5602. free_extent_map(em);
  5603. }
  5604. em = alloc_extent_map();
  5605. if (!em)
  5606. return -ENOMEM;
  5607. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  5608. if (!map) {
  5609. free_extent_map(em);
  5610. return -ENOMEM;
  5611. }
  5612. set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
  5613. em->map_lookup = map;
  5614. em->start = logical;
  5615. em->len = length;
  5616. em->orig_start = 0;
  5617. em->block_start = 0;
  5618. em->block_len = em->len;
  5619. map->num_stripes = num_stripes;
  5620. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  5621. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  5622. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  5623. map->type = btrfs_chunk_type(leaf, chunk);
  5624. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  5625. map->verified_stripes = 0;
  5626. for (i = 0; i < num_stripes; i++) {
  5627. map->stripes[i].physical =
  5628. btrfs_stripe_offset_nr(leaf, chunk, i);
  5629. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  5630. read_extent_buffer(leaf, uuid, (unsigned long)
  5631. btrfs_stripe_dev_uuid_nr(chunk, i),
  5632. BTRFS_UUID_SIZE);
  5633. map->stripes[i].dev = btrfs_find_device(fs_info, devid,
  5634. uuid, NULL);
  5635. if (!map->stripes[i].dev &&
  5636. !btrfs_test_opt(fs_info, DEGRADED)) {
  5637. free_extent_map(em);
  5638. btrfs_report_missing_device(fs_info, devid, uuid, true);
  5639. return -ENOENT;
  5640. }
  5641. if (!map->stripes[i].dev) {
  5642. map->stripes[i].dev =
  5643. add_missing_dev(fs_info->fs_devices, devid,
  5644. uuid);
  5645. if (IS_ERR(map->stripes[i].dev)) {
  5646. free_extent_map(em);
  5647. btrfs_err(fs_info,
  5648. "failed to init missing dev %llu: %ld",
  5649. devid, PTR_ERR(map->stripes[i].dev));
  5650. return PTR_ERR(map->stripes[i].dev);
  5651. }
  5652. btrfs_report_missing_device(fs_info, devid, uuid, false);
  5653. }
  5654. set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
  5655. &(map->stripes[i].dev->dev_state));
  5656. }
  5657. write_lock(&map_tree->map_tree.lock);
  5658. ret = add_extent_mapping(&map_tree->map_tree, em, 0);
  5659. write_unlock(&map_tree->map_tree.lock);
  5660. if (ret < 0) {
  5661. btrfs_err(fs_info,
  5662. "failed to add chunk map, start=%llu len=%llu: %d",
  5663. em->start, em->len, ret);
  5664. }
  5665. free_extent_map(em);
  5666. return ret;
  5667. }
  5668. static void fill_device_from_item(struct extent_buffer *leaf,
  5669. struct btrfs_dev_item *dev_item,
  5670. struct btrfs_device *device)
  5671. {
  5672. unsigned long ptr;
  5673. device->devid = btrfs_device_id(leaf, dev_item);
  5674. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  5675. device->total_bytes = device->disk_total_bytes;
  5676. device->commit_total_bytes = device->disk_total_bytes;
  5677. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  5678. device->commit_bytes_used = device->bytes_used;
  5679. device->type = btrfs_device_type(leaf, dev_item);
  5680. device->io_align = btrfs_device_io_align(leaf, dev_item);
  5681. device->io_width = btrfs_device_io_width(leaf, dev_item);
  5682. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  5683. WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
  5684. clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
  5685. ptr = btrfs_device_uuid(dev_item);
  5686. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  5687. }
  5688. static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
  5689. u8 *fsid)
  5690. {
  5691. struct btrfs_fs_devices *fs_devices;
  5692. int ret;
  5693. lockdep_assert_held(&uuid_mutex);
  5694. ASSERT(fsid);
  5695. fs_devices = fs_info->fs_devices->seed;
  5696. while (fs_devices) {
  5697. if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
  5698. return fs_devices;
  5699. fs_devices = fs_devices->seed;
  5700. }
  5701. fs_devices = find_fsid(fsid);
  5702. if (!fs_devices) {
  5703. if (!btrfs_test_opt(fs_info, DEGRADED))
  5704. return ERR_PTR(-ENOENT);
  5705. fs_devices = alloc_fs_devices(fsid);
  5706. if (IS_ERR(fs_devices))
  5707. return fs_devices;
  5708. fs_devices->seeding = 1;
  5709. fs_devices->opened = 1;
  5710. return fs_devices;
  5711. }
  5712. fs_devices = clone_fs_devices(fs_devices);
  5713. if (IS_ERR(fs_devices))
  5714. return fs_devices;
  5715. ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
  5716. if (ret) {
  5717. free_fs_devices(fs_devices);
  5718. fs_devices = ERR_PTR(ret);
  5719. goto out;
  5720. }
  5721. if (!fs_devices->seeding) {
  5722. close_fs_devices(fs_devices);
  5723. free_fs_devices(fs_devices);
  5724. fs_devices = ERR_PTR(-EINVAL);
  5725. goto out;
  5726. }
  5727. fs_devices->seed = fs_info->fs_devices->seed;
  5728. fs_info->fs_devices->seed = fs_devices;
  5729. out:
  5730. return fs_devices;
  5731. }
  5732. static int read_one_dev(struct btrfs_fs_info *fs_info,
  5733. struct extent_buffer *leaf,
  5734. struct btrfs_dev_item *dev_item)
  5735. {
  5736. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5737. struct btrfs_device *device;
  5738. u64 devid;
  5739. int ret;
  5740. u8 fs_uuid[BTRFS_FSID_SIZE];
  5741. u8 dev_uuid[BTRFS_UUID_SIZE];
  5742. devid = btrfs_device_id(leaf, dev_item);
  5743. read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
  5744. BTRFS_UUID_SIZE);
  5745. read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
  5746. BTRFS_FSID_SIZE);
  5747. if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
  5748. fs_devices = open_seed_devices(fs_info, fs_uuid);
  5749. if (IS_ERR(fs_devices))
  5750. return PTR_ERR(fs_devices);
  5751. }
  5752. device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
  5753. if (!device) {
  5754. if (!btrfs_test_opt(fs_info, DEGRADED)) {
  5755. btrfs_report_missing_device(fs_info, devid,
  5756. dev_uuid, true);
  5757. return -ENOENT;
  5758. }
  5759. device = add_missing_dev(fs_devices, devid, dev_uuid);
  5760. if (IS_ERR(device)) {
  5761. btrfs_err(fs_info,
  5762. "failed to add missing dev %llu: %ld",
  5763. devid, PTR_ERR(device));
  5764. return PTR_ERR(device);
  5765. }
  5766. btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
  5767. } else {
  5768. if (!device->bdev) {
  5769. if (!btrfs_test_opt(fs_info, DEGRADED)) {
  5770. btrfs_report_missing_device(fs_info,
  5771. devid, dev_uuid, true);
  5772. return -ENOENT;
  5773. }
  5774. btrfs_report_missing_device(fs_info, devid,
  5775. dev_uuid, false);
  5776. }
  5777. if (!device->bdev &&
  5778. !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
  5779. /*
  5780. * this happens when a device that was properly setup
  5781. * in the device info lists suddenly goes bad.
  5782. * device->bdev is NULL, and so we have to set
  5783. * device->missing to one here
  5784. */
  5785. device->fs_devices->missing_devices++;
  5786. set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
  5787. }
  5788. /* Move the device to its own fs_devices */
  5789. if (device->fs_devices != fs_devices) {
  5790. ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
  5791. &device->dev_state));
  5792. list_move(&device->dev_list, &fs_devices->devices);
  5793. device->fs_devices->num_devices--;
  5794. fs_devices->num_devices++;
  5795. device->fs_devices->missing_devices--;
  5796. fs_devices->missing_devices++;
  5797. device->fs_devices = fs_devices;
  5798. }
  5799. }
  5800. if (device->fs_devices != fs_info->fs_devices) {
  5801. BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
  5802. if (device->generation !=
  5803. btrfs_device_generation(leaf, dev_item))
  5804. return -EINVAL;
  5805. }
  5806. fill_device_from_item(leaf, dev_item, device);
  5807. set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
  5808. if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
  5809. !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  5810. device->fs_devices->total_rw_bytes += device->total_bytes;
  5811. atomic64_add(device->total_bytes - device->bytes_used,
  5812. &fs_info->free_chunk_space);
  5813. }
  5814. ret = 0;
  5815. return ret;
  5816. }
  5817. int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
  5818. {
  5819. struct btrfs_root *root = fs_info->tree_root;
  5820. struct btrfs_super_block *super_copy = fs_info->super_copy;
  5821. struct extent_buffer *sb;
  5822. struct btrfs_disk_key *disk_key;
  5823. struct btrfs_chunk *chunk;
  5824. u8 *array_ptr;
  5825. unsigned long sb_array_offset;
  5826. int ret = 0;
  5827. u32 num_stripes;
  5828. u32 array_size;
  5829. u32 len = 0;
  5830. u32 cur_offset;
  5831. u64 type;
  5832. struct btrfs_key key;
  5833. ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
  5834. /*
  5835. * This will create extent buffer of nodesize, superblock size is
  5836. * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
  5837. * overallocate but we can keep it as-is, only the first page is used.
  5838. */
  5839. sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
  5840. if (IS_ERR(sb))
  5841. return PTR_ERR(sb);
  5842. set_extent_buffer_uptodate(sb);
  5843. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  5844. /*
  5845. * The sb extent buffer is artificial and just used to read the system array.
  5846. * set_extent_buffer_uptodate() call does not properly mark all it's
  5847. * pages up-to-date when the page is larger: extent does not cover the
  5848. * whole page and consequently check_page_uptodate does not find all
  5849. * the page's extents up-to-date (the hole beyond sb),
  5850. * write_extent_buffer then triggers a WARN_ON.
  5851. *
  5852. * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
  5853. * but sb spans only this function. Add an explicit SetPageUptodate call
  5854. * to silence the warning eg. on PowerPC 64.
  5855. */
  5856. if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
  5857. SetPageUptodate(sb->pages[0]);
  5858. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  5859. array_size = btrfs_super_sys_array_size(super_copy);
  5860. array_ptr = super_copy->sys_chunk_array;
  5861. sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
  5862. cur_offset = 0;
  5863. while (cur_offset < array_size) {
  5864. disk_key = (struct btrfs_disk_key *)array_ptr;
  5865. len = sizeof(*disk_key);
  5866. if (cur_offset + len > array_size)
  5867. goto out_short_read;
  5868. btrfs_disk_key_to_cpu(&key, disk_key);
  5869. array_ptr += len;
  5870. sb_array_offset += len;
  5871. cur_offset += len;
  5872. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  5873. chunk = (struct btrfs_chunk *)sb_array_offset;
  5874. /*
  5875. * At least one btrfs_chunk with one stripe must be
  5876. * present, exact stripe count check comes afterwards
  5877. */
  5878. len = btrfs_chunk_item_size(1);
  5879. if (cur_offset + len > array_size)
  5880. goto out_short_read;
  5881. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  5882. if (!num_stripes) {
  5883. btrfs_err(fs_info,
  5884. "invalid number of stripes %u in sys_array at offset %u",
  5885. num_stripes, cur_offset);
  5886. ret = -EIO;
  5887. break;
  5888. }
  5889. type = btrfs_chunk_type(sb, chunk);
  5890. if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
  5891. btrfs_err(fs_info,
  5892. "invalid chunk type %llu in sys_array at offset %u",
  5893. type, cur_offset);
  5894. ret = -EIO;
  5895. break;
  5896. }
  5897. len = btrfs_chunk_item_size(num_stripes);
  5898. if (cur_offset + len > array_size)
  5899. goto out_short_read;
  5900. ret = read_one_chunk(fs_info, &key, sb, chunk);
  5901. if (ret)
  5902. break;
  5903. } else {
  5904. btrfs_err(fs_info,
  5905. "unexpected item type %u in sys_array at offset %u",
  5906. (u32)key.type, cur_offset);
  5907. ret = -EIO;
  5908. break;
  5909. }
  5910. array_ptr += len;
  5911. sb_array_offset += len;
  5912. cur_offset += len;
  5913. }
  5914. clear_extent_buffer_uptodate(sb);
  5915. free_extent_buffer_stale(sb);
  5916. return ret;
  5917. out_short_read:
  5918. btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
  5919. len, cur_offset);
  5920. clear_extent_buffer_uptodate(sb);
  5921. free_extent_buffer_stale(sb);
  5922. return -EIO;
  5923. }
  5924. /*
  5925. * Check if all chunks in the fs are OK for read-write degraded mount
  5926. *
  5927. * If the @failing_dev is specified, it's accounted as missing.
  5928. *
  5929. * Return true if all chunks meet the minimal RW mount requirements.
  5930. * Return false if any chunk doesn't meet the minimal RW mount requirements.
  5931. */
  5932. bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
  5933. struct btrfs_device *failing_dev)
  5934. {
  5935. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  5936. struct extent_map *em;
  5937. u64 next_start = 0;
  5938. bool ret = true;
  5939. read_lock(&map_tree->map_tree.lock);
  5940. em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
  5941. read_unlock(&map_tree->map_tree.lock);
  5942. /* No chunk at all? Return false anyway */
  5943. if (!em) {
  5944. ret = false;
  5945. goto out;
  5946. }
  5947. while (em) {
  5948. struct map_lookup *map;
  5949. int missing = 0;
  5950. int max_tolerated;
  5951. int i;
  5952. map = em->map_lookup;
  5953. max_tolerated =
  5954. btrfs_get_num_tolerated_disk_barrier_failures(
  5955. map->type);
  5956. for (i = 0; i < map->num_stripes; i++) {
  5957. struct btrfs_device *dev = map->stripes[i].dev;
  5958. if (!dev || !dev->bdev ||
  5959. test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
  5960. dev->last_flush_error)
  5961. missing++;
  5962. else if (failing_dev && failing_dev == dev)
  5963. missing++;
  5964. }
  5965. if (missing > max_tolerated) {
  5966. if (!failing_dev)
  5967. btrfs_warn(fs_info,
  5968. "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
  5969. em->start, missing, max_tolerated);
  5970. free_extent_map(em);
  5971. ret = false;
  5972. goto out;
  5973. }
  5974. next_start = extent_map_end(em);
  5975. free_extent_map(em);
  5976. read_lock(&map_tree->map_tree.lock);
  5977. em = lookup_extent_mapping(&map_tree->map_tree, next_start,
  5978. (u64)(-1) - next_start);
  5979. read_unlock(&map_tree->map_tree.lock);
  5980. }
  5981. out:
  5982. return ret;
  5983. }
  5984. int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
  5985. {
  5986. struct btrfs_root *root = fs_info->chunk_root;
  5987. struct btrfs_path *path;
  5988. struct extent_buffer *leaf;
  5989. struct btrfs_key key;
  5990. struct btrfs_key found_key;
  5991. int ret;
  5992. int slot;
  5993. u64 total_dev = 0;
  5994. path = btrfs_alloc_path();
  5995. if (!path)
  5996. return -ENOMEM;
  5997. /*
  5998. * uuid_mutex is needed only if we are mounting a sprout FS
  5999. * otherwise we don't need it.
  6000. */
  6001. mutex_lock(&uuid_mutex);
  6002. mutex_lock(&fs_info->chunk_mutex);
  6003. /*
  6004. * Read all device items, and then all the chunk items. All
  6005. * device items are found before any chunk item (their object id
  6006. * is smaller than the lowest possible object id for a chunk
  6007. * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
  6008. */
  6009. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  6010. key.offset = 0;
  6011. key.type = 0;
  6012. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6013. if (ret < 0)
  6014. goto error;
  6015. while (1) {
  6016. leaf = path->nodes[0];
  6017. slot = path->slots[0];
  6018. if (slot >= btrfs_header_nritems(leaf)) {
  6019. ret = btrfs_next_leaf(root, path);
  6020. if (ret == 0)
  6021. continue;
  6022. if (ret < 0)
  6023. goto error;
  6024. break;
  6025. }
  6026. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6027. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  6028. struct btrfs_dev_item *dev_item;
  6029. dev_item = btrfs_item_ptr(leaf, slot,
  6030. struct btrfs_dev_item);
  6031. ret = read_one_dev(fs_info, leaf, dev_item);
  6032. if (ret)
  6033. goto error;
  6034. total_dev++;
  6035. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  6036. struct btrfs_chunk *chunk;
  6037. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  6038. ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
  6039. if (ret)
  6040. goto error;
  6041. }
  6042. path->slots[0]++;
  6043. }
  6044. /*
  6045. * After loading chunk tree, we've got all device information,
  6046. * do another round of validation checks.
  6047. */
  6048. if (total_dev != fs_info->fs_devices->total_devices) {
  6049. btrfs_err(fs_info,
  6050. "super_num_devices %llu mismatch with num_devices %llu found here",
  6051. btrfs_super_num_devices(fs_info->super_copy),
  6052. total_dev);
  6053. ret = -EINVAL;
  6054. goto error;
  6055. }
  6056. if (btrfs_super_total_bytes(fs_info->super_copy) <
  6057. fs_info->fs_devices->total_rw_bytes) {
  6058. btrfs_err(fs_info,
  6059. "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
  6060. btrfs_super_total_bytes(fs_info->super_copy),
  6061. fs_info->fs_devices->total_rw_bytes);
  6062. ret = -EINVAL;
  6063. goto error;
  6064. }
  6065. ret = 0;
  6066. error:
  6067. mutex_unlock(&fs_info->chunk_mutex);
  6068. mutex_unlock(&uuid_mutex);
  6069. btrfs_free_path(path);
  6070. return ret;
  6071. }
  6072. void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
  6073. {
  6074. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6075. struct btrfs_device *device;
  6076. while (fs_devices) {
  6077. mutex_lock(&fs_devices->device_list_mutex);
  6078. list_for_each_entry(device, &fs_devices->devices, dev_list)
  6079. device->fs_info = fs_info;
  6080. mutex_unlock(&fs_devices->device_list_mutex);
  6081. fs_devices = fs_devices->seed;
  6082. }
  6083. }
  6084. static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
  6085. {
  6086. int i;
  6087. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  6088. btrfs_dev_stat_reset(dev, i);
  6089. }
  6090. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
  6091. {
  6092. struct btrfs_key key;
  6093. struct btrfs_key found_key;
  6094. struct btrfs_root *dev_root = fs_info->dev_root;
  6095. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6096. struct extent_buffer *eb;
  6097. int slot;
  6098. int ret = 0;
  6099. struct btrfs_device *device;
  6100. struct btrfs_path *path = NULL;
  6101. int i;
  6102. path = btrfs_alloc_path();
  6103. if (!path) {
  6104. ret = -ENOMEM;
  6105. goto out;
  6106. }
  6107. mutex_lock(&fs_devices->device_list_mutex);
  6108. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  6109. int item_size;
  6110. struct btrfs_dev_stats_item *ptr;
  6111. key.objectid = BTRFS_DEV_STATS_OBJECTID;
  6112. key.type = BTRFS_PERSISTENT_ITEM_KEY;
  6113. key.offset = device->devid;
  6114. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  6115. if (ret) {
  6116. __btrfs_reset_dev_stats(device);
  6117. device->dev_stats_valid = 1;
  6118. btrfs_release_path(path);
  6119. continue;
  6120. }
  6121. slot = path->slots[0];
  6122. eb = path->nodes[0];
  6123. btrfs_item_key_to_cpu(eb, &found_key, slot);
  6124. item_size = btrfs_item_size_nr(eb, slot);
  6125. ptr = btrfs_item_ptr(eb, slot,
  6126. struct btrfs_dev_stats_item);
  6127. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  6128. if (item_size >= (1 + i) * sizeof(__le64))
  6129. btrfs_dev_stat_set(device, i,
  6130. btrfs_dev_stats_value(eb, ptr, i));
  6131. else
  6132. btrfs_dev_stat_reset(device, i);
  6133. }
  6134. device->dev_stats_valid = 1;
  6135. btrfs_dev_stat_print_on_load(device);
  6136. btrfs_release_path(path);
  6137. }
  6138. mutex_unlock(&fs_devices->device_list_mutex);
  6139. out:
  6140. btrfs_free_path(path);
  6141. return ret < 0 ? ret : 0;
  6142. }
  6143. static int update_dev_stat_item(struct btrfs_trans_handle *trans,
  6144. struct btrfs_device *device)
  6145. {
  6146. struct btrfs_fs_info *fs_info = trans->fs_info;
  6147. struct btrfs_root *dev_root = fs_info->dev_root;
  6148. struct btrfs_path *path;
  6149. struct btrfs_key key;
  6150. struct extent_buffer *eb;
  6151. struct btrfs_dev_stats_item *ptr;
  6152. int ret;
  6153. int i;
  6154. key.objectid = BTRFS_DEV_STATS_OBJECTID;
  6155. key.type = BTRFS_PERSISTENT_ITEM_KEY;
  6156. key.offset = device->devid;
  6157. path = btrfs_alloc_path();
  6158. if (!path)
  6159. return -ENOMEM;
  6160. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  6161. if (ret < 0) {
  6162. btrfs_warn_in_rcu(fs_info,
  6163. "error %d while searching for dev_stats item for device %s",
  6164. ret, rcu_str_deref(device->name));
  6165. goto out;
  6166. }
  6167. if (ret == 0 &&
  6168. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  6169. /* need to delete old one and insert a new one */
  6170. ret = btrfs_del_item(trans, dev_root, path);
  6171. if (ret != 0) {
  6172. btrfs_warn_in_rcu(fs_info,
  6173. "delete too small dev_stats item for device %s failed %d",
  6174. rcu_str_deref(device->name), ret);
  6175. goto out;
  6176. }
  6177. ret = 1;
  6178. }
  6179. if (ret == 1) {
  6180. /* need to insert a new item */
  6181. btrfs_release_path(path);
  6182. ret = btrfs_insert_empty_item(trans, dev_root, path,
  6183. &key, sizeof(*ptr));
  6184. if (ret < 0) {
  6185. btrfs_warn_in_rcu(fs_info,
  6186. "insert dev_stats item for device %s failed %d",
  6187. rcu_str_deref(device->name), ret);
  6188. goto out;
  6189. }
  6190. }
  6191. eb = path->nodes[0];
  6192. ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
  6193. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  6194. btrfs_set_dev_stats_value(eb, ptr, i,
  6195. btrfs_dev_stat_read(device, i));
  6196. btrfs_mark_buffer_dirty(eb);
  6197. out:
  6198. btrfs_free_path(path);
  6199. return ret;
  6200. }
  6201. /*
  6202. * called from commit_transaction. Writes all changed device stats to disk.
  6203. */
  6204. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  6205. struct btrfs_fs_info *fs_info)
  6206. {
  6207. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6208. struct btrfs_device *device;
  6209. int stats_cnt;
  6210. int ret = 0;
  6211. mutex_lock(&fs_devices->device_list_mutex);
  6212. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  6213. stats_cnt = atomic_read(&device->dev_stats_ccnt);
  6214. if (!device->dev_stats_valid || stats_cnt == 0)
  6215. continue;
  6216. /*
  6217. * There is a LOAD-LOAD control dependency between the value of
  6218. * dev_stats_ccnt and updating the on-disk values which requires
  6219. * reading the in-memory counters. Such control dependencies
  6220. * require explicit read memory barriers.
  6221. *
  6222. * This memory barriers pairs with smp_mb__before_atomic in
  6223. * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
  6224. * barrier implied by atomic_xchg in
  6225. * btrfs_dev_stats_read_and_reset
  6226. */
  6227. smp_rmb();
  6228. ret = update_dev_stat_item(trans, device);
  6229. if (!ret)
  6230. atomic_sub(stats_cnt, &device->dev_stats_ccnt);
  6231. }
  6232. mutex_unlock(&fs_devices->device_list_mutex);
  6233. return ret;
  6234. }
  6235. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
  6236. {
  6237. btrfs_dev_stat_inc(dev, index);
  6238. btrfs_dev_stat_print_on_error(dev);
  6239. }
  6240. static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
  6241. {
  6242. if (!dev->dev_stats_valid)
  6243. return;
  6244. btrfs_err_rl_in_rcu(dev->fs_info,
  6245. "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
  6246. rcu_str_deref(dev->name),
  6247. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  6248. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  6249. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  6250. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  6251. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  6252. }
  6253. static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
  6254. {
  6255. int i;
  6256. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  6257. if (btrfs_dev_stat_read(dev, i) != 0)
  6258. break;
  6259. if (i == BTRFS_DEV_STAT_VALUES_MAX)
  6260. return; /* all values == 0, suppress message */
  6261. btrfs_info_in_rcu(dev->fs_info,
  6262. "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
  6263. rcu_str_deref(dev->name),
  6264. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  6265. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  6266. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  6267. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  6268. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  6269. }
  6270. int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
  6271. struct btrfs_ioctl_get_dev_stats *stats)
  6272. {
  6273. struct btrfs_device *dev;
  6274. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6275. int i;
  6276. mutex_lock(&fs_devices->device_list_mutex);
  6277. dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
  6278. mutex_unlock(&fs_devices->device_list_mutex);
  6279. if (!dev) {
  6280. btrfs_warn(fs_info, "get dev_stats failed, device not found");
  6281. return -ENODEV;
  6282. } else if (!dev->dev_stats_valid) {
  6283. btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
  6284. return -ENODEV;
  6285. } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
  6286. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  6287. if (stats->nr_items > i)
  6288. stats->values[i] =
  6289. btrfs_dev_stat_read_and_reset(dev, i);
  6290. else
  6291. btrfs_dev_stat_reset(dev, i);
  6292. }
  6293. } else {
  6294. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  6295. if (stats->nr_items > i)
  6296. stats->values[i] = btrfs_dev_stat_read(dev, i);
  6297. }
  6298. if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
  6299. stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
  6300. return 0;
  6301. }
  6302. void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
  6303. {
  6304. struct buffer_head *bh;
  6305. struct btrfs_super_block *disk_super;
  6306. int copy_num;
  6307. if (!bdev)
  6308. return;
  6309. for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
  6310. copy_num++) {
  6311. if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
  6312. continue;
  6313. disk_super = (struct btrfs_super_block *)bh->b_data;
  6314. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  6315. set_buffer_dirty(bh);
  6316. sync_dirty_buffer(bh);
  6317. brelse(bh);
  6318. }
  6319. /* Notify udev that device has changed */
  6320. btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
  6321. /* Update ctime/mtime for device path for libblkid */
  6322. update_dev_time(device_path);
  6323. }
  6324. /*
  6325. * Update the size of all devices, which is used for writing out the
  6326. * super blocks.
  6327. */
  6328. void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
  6329. {
  6330. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6331. struct btrfs_device *curr, *next;
  6332. if (list_empty(&fs_devices->resized_devices))
  6333. return;
  6334. mutex_lock(&fs_devices->device_list_mutex);
  6335. mutex_lock(&fs_info->chunk_mutex);
  6336. list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
  6337. resized_list) {
  6338. list_del_init(&curr->resized_list);
  6339. curr->commit_total_bytes = curr->disk_total_bytes;
  6340. }
  6341. mutex_unlock(&fs_info->chunk_mutex);
  6342. mutex_unlock(&fs_devices->device_list_mutex);
  6343. }
  6344. /* Must be invoked during the transaction commit */
  6345. void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
  6346. {
  6347. struct btrfs_fs_info *fs_info = trans->fs_info;
  6348. struct extent_map *em;
  6349. struct map_lookup *map;
  6350. struct btrfs_device *dev;
  6351. int i;
  6352. if (list_empty(&trans->pending_chunks))
  6353. return;
  6354. /* In order to kick the device replace finish process */
  6355. mutex_lock(&fs_info->chunk_mutex);
  6356. list_for_each_entry(em, &trans->pending_chunks, list) {
  6357. map = em->map_lookup;
  6358. for (i = 0; i < map->num_stripes; i++) {
  6359. dev = map->stripes[i].dev;
  6360. dev->commit_bytes_used = dev->bytes_used;
  6361. }
  6362. }
  6363. mutex_unlock(&fs_info->chunk_mutex);
  6364. }
  6365. void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
  6366. {
  6367. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6368. while (fs_devices) {
  6369. fs_devices->fs_info = fs_info;
  6370. fs_devices = fs_devices->seed;
  6371. }
  6372. }
  6373. void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
  6374. {
  6375. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6376. while (fs_devices) {
  6377. fs_devices->fs_info = NULL;
  6378. fs_devices = fs_devices->seed;
  6379. }
  6380. }
  6381. /*
  6382. * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
  6383. */
  6384. int btrfs_bg_type_to_factor(u64 flags)
  6385. {
  6386. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  6387. BTRFS_BLOCK_GROUP_RAID10))
  6388. return 2;
  6389. return 1;
  6390. }
  6391. static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
  6392. {
  6393. int index = btrfs_bg_flags_to_raid_index(type);
  6394. int ncopies = btrfs_raid_array[index].ncopies;
  6395. int data_stripes;
  6396. switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  6397. case BTRFS_BLOCK_GROUP_RAID5:
  6398. data_stripes = num_stripes - 1;
  6399. break;
  6400. case BTRFS_BLOCK_GROUP_RAID6:
  6401. data_stripes = num_stripes - 2;
  6402. break;
  6403. default:
  6404. data_stripes = num_stripes / ncopies;
  6405. break;
  6406. }
  6407. return div_u64(chunk_len, data_stripes);
  6408. }
  6409. static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
  6410. u64 chunk_offset, u64 devid,
  6411. u64 physical_offset, u64 physical_len)
  6412. {
  6413. struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
  6414. struct extent_map *em;
  6415. struct map_lookup *map;
  6416. u64 stripe_len;
  6417. bool found = false;
  6418. int ret = 0;
  6419. int i;
  6420. read_lock(&em_tree->lock);
  6421. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  6422. read_unlock(&em_tree->lock);
  6423. if (!em) {
  6424. btrfs_err(fs_info,
  6425. "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
  6426. physical_offset, devid);
  6427. ret = -EUCLEAN;
  6428. goto out;
  6429. }
  6430. map = em->map_lookup;
  6431. stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
  6432. if (physical_len != stripe_len) {
  6433. btrfs_err(fs_info,
  6434. "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
  6435. physical_offset, devid, em->start, physical_len,
  6436. stripe_len);
  6437. ret = -EUCLEAN;
  6438. goto out;
  6439. }
  6440. for (i = 0; i < map->num_stripes; i++) {
  6441. if (map->stripes[i].dev->devid == devid &&
  6442. map->stripes[i].physical == physical_offset) {
  6443. found = true;
  6444. if (map->verified_stripes >= map->num_stripes) {
  6445. btrfs_err(fs_info,
  6446. "too many dev extents for chunk %llu found",
  6447. em->start);
  6448. ret = -EUCLEAN;
  6449. goto out;
  6450. }
  6451. map->verified_stripes++;
  6452. break;
  6453. }
  6454. }
  6455. if (!found) {
  6456. btrfs_err(fs_info,
  6457. "dev extent physical offset %llu devid %llu has no corresponding chunk",
  6458. physical_offset, devid);
  6459. ret = -EUCLEAN;
  6460. }
  6461. out:
  6462. free_extent_map(em);
  6463. return ret;
  6464. }
  6465. static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
  6466. {
  6467. struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
  6468. struct extent_map *em;
  6469. struct rb_node *node;
  6470. int ret = 0;
  6471. read_lock(&em_tree->lock);
  6472. for (node = rb_first(&em_tree->map); node; node = rb_next(node)) {
  6473. em = rb_entry(node, struct extent_map, rb_node);
  6474. if (em->map_lookup->num_stripes !=
  6475. em->map_lookup->verified_stripes) {
  6476. btrfs_err(fs_info,
  6477. "chunk %llu has missing dev extent, have %d expect %d",
  6478. em->start, em->map_lookup->verified_stripes,
  6479. em->map_lookup->num_stripes);
  6480. ret = -EUCLEAN;
  6481. goto out;
  6482. }
  6483. }
  6484. out:
  6485. read_unlock(&em_tree->lock);
  6486. return ret;
  6487. }
  6488. /*
  6489. * Ensure that all dev extents are mapped to correct chunk, otherwise
  6490. * later chunk allocation/free would cause unexpected behavior.
  6491. *
  6492. * NOTE: This will iterate through the whole device tree, which should be of
  6493. * the same size level as the chunk tree. This slightly increases mount time.
  6494. */
  6495. int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
  6496. {
  6497. struct btrfs_path *path;
  6498. struct btrfs_root *root = fs_info->dev_root;
  6499. struct btrfs_key key;
  6500. u64 prev_devid = 0;
  6501. u64 prev_dev_ext_end = 0;
  6502. int ret = 0;
  6503. key.objectid = 1;
  6504. key.type = BTRFS_DEV_EXTENT_KEY;
  6505. key.offset = 0;
  6506. path = btrfs_alloc_path();
  6507. if (!path)
  6508. return -ENOMEM;
  6509. path->reada = READA_FORWARD;
  6510. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6511. if (ret < 0)
  6512. goto out;
  6513. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  6514. ret = btrfs_next_item(root, path);
  6515. if (ret < 0)
  6516. goto out;
  6517. /* No dev extents at all? Not good */
  6518. if (ret > 0) {
  6519. ret = -EUCLEAN;
  6520. goto out;
  6521. }
  6522. }
  6523. while (1) {
  6524. struct extent_buffer *leaf = path->nodes[0];
  6525. struct btrfs_dev_extent *dext;
  6526. int slot = path->slots[0];
  6527. u64 chunk_offset;
  6528. u64 physical_offset;
  6529. u64 physical_len;
  6530. u64 devid;
  6531. btrfs_item_key_to_cpu(leaf, &key, slot);
  6532. if (key.type != BTRFS_DEV_EXTENT_KEY)
  6533. break;
  6534. devid = key.objectid;
  6535. physical_offset = key.offset;
  6536. dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
  6537. chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
  6538. physical_len = btrfs_dev_extent_length(leaf, dext);
  6539. /* Check if this dev extent overlaps with the previous one */
  6540. if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
  6541. btrfs_err(fs_info,
  6542. "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
  6543. devid, physical_offset, prev_dev_ext_end);
  6544. ret = -EUCLEAN;
  6545. goto out;
  6546. }
  6547. ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
  6548. physical_offset, physical_len);
  6549. if (ret < 0)
  6550. goto out;
  6551. prev_devid = devid;
  6552. prev_dev_ext_end = physical_offset + physical_len;
  6553. ret = btrfs_next_item(root, path);
  6554. if (ret < 0)
  6555. goto out;
  6556. if (ret > 0) {
  6557. ret = 0;
  6558. break;
  6559. }
  6560. }
  6561. /* Ensure all chunks have corresponding dev extents */
  6562. ret = verify_chunk_dev_extent_mapping(fs_info);
  6563. out:
  6564. btrfs_free_path(path);
  6565. return ret;
  6566. }