volumes.c 196 KB

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