md.c 194 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583
  1. /*
  2. md.c : Multiple Devices driver for Linux
  3. Copyright (C) 1998, 1999, 2000 Ingo Molnar
  4. completely rewritten, based on the MD driver code from Marc Zyngier
  5. Changes:
  6. - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
  7. - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
  8. - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
  9. - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
  10. - kmod support by: Cyrus Durgin
  11. - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
  12. - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
  13. - lots of fixes and improvements to the RAID1/RAID5 and generic
  14. RAID code (such as request based resynchronization):
  15. Neil Brown <neilb@cse.unsw.edu.au>.
  16. - persistent bitmap code
  17. Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2, or (at your option)
  21. any later version.
  22. You should have received a copy of the GNU General Public License
  23. (for example /usr/src/linux/COPYING); if not, write to the Free
  24. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/kthread.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/sysctl.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/mutex.h>
  31. #include <linux/buffer_head.h> /* for invalidate_bdev */
  32. #include <linux/poll.h>
  33. #include <linux/ctype.h>
  34. #include <linux/string.h>
  35. #include <linux/hdreg.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/random.h>
  38. #include <linux/reboot.h>
  39. #include <linux/file.h>
  40. #include <linux/compat.h>
  41. #include <linux/delay.h>
  42. #include <linux/raid/md_p.h>
  43. #include <linux/raid/md_u.h>
  44. #include <linux/slab.h>
  45. #include "md.h"
  46. #include "bitmap.h"
  47. #define DEBUG 0
  48. #define dprintk(x...) ((void)(DEBUG && printk(x)))
  49. #ifndef MODULE
  50. static void autostart_arrays(int part);
  51. #endif
  52. /* pers_list is a list of registered personalities protected
  53. * by pers_lock.
  54. * pers_lock does extra service to protect accesses to
  55. * mddev->thread when the mutex cannot be held.
  56. */
  57. static LIST_HEAD(pers_list);
  58. static DEFINE_SPINLOCK(pers_lock);
  59. static void md_print_devices(void);
  60. static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
  61. static struct workqueue_struct *md_wq;
  62. static struct workqueue_struct *md_misc_wq;
  63. #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
  64. /*
  65. * Default number of read corrections we'll attempt on an rdev
  66. * before ejecting it from the array. We divide the read error
  67. * count by 2 for every hour elapsed between read errors.
  68. */
  69. #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
  70. /*
  71. * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
  72. * is 1000 KB/sec, so the extra system load does not show up that much.
  73. * Increase it if you want to have more _guaranteed_ speed. Note that
  74. * the RAID driver will use the maximum available bandwidth if the IO
  75. * subsystem is idle. There is also an 'absolute maximum' reconstruction
  76. * speed limit - in case reconstruction slows down your system despite
  77. * idle IO detection.
  78. *
  79. * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
  80. * or /sys/block/mdX/md/sync_speed_{min,max}
  81. */
  82. static int sysctl_speed_limit_min = 1000;
  83. static int sysctl_speed_limit_max = 200000;
  84. static inline int speed_min(mddev_t *mddev)
  85. {
  86. return mddev->sync_speed_min ?
  87. mddev->sync_speed_min : sysctl_speed_limit_min;
  88. }
  89. static inline int speed_max(mddev_t *mddev)
  90. {
  91. return mddev->sync_speed_max ?
  92. mddev->sync_speed_max : sysctl_speed_limit_max;
  93. }
  94. static struct ctl_table_header *raid_table_header;
  95. static ctl_table raid_table[] = {
  96. {
  97. .procname = "speed_limit_min",
  98. .data = &sysctl_speed_limit_min,
  99. .maxlen = sizeof(int),
  100. .mode = S_IRUGO|S_IWUSR,
  101. .proc_handler = proc_dointvec,
  102. },
  103. {
  104. .procname = "speed_limit_max",
  105. .data = &sysctl_speed_limit_max,
  106. .maxlen = sizeof(int),
  107. .mode = S_IRUGO|S_IWUSR,
  108. .proc_handler = proc_dointvec,
  109. },
  110. { }
  111. };
  112. static ctl_table raid_dir_table[] = {
  113. {
  114. .procname = "raid",
  115. .maxlen = 0,
  116. .mode = S_IRUGO|S_IXUGO,
  117. .child = raid_table,
  118. },
  119. { }
  120. };
  121. static ctl_table raid_root_table[] = {
  122. {
  123. .procname = "dev",
  124. .maxlen = 0,
  125. .mode = 0555,
  126. .child = raid_dir_table,
  127. },
  128. { }
  129. };
  130. static const struct block_device_operations md_fops;
  131. static int start_readonly;
  132. /* bio_clone_mddev
  133. * like bio_clone, but with a local bio set
  134. */
  135. static void mddev_bio_destructor(struct bio *bio)
  136. {
  137. mddev_t *mddev, **mddevp;
  138. mddevp = (void*)bio;
  139. mddev = mddevp[-1];
  140. bio_free(bio, mddev->bio_set);
  141. }
  142. struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
  143. mddev_t *mddev)
  144. {
  145. struct bio *b;
  146. mddev_t **mddevp;
  147. if (!mddev || !mddev->bio_set)
  148. return bio_alloc(gfp_mask, nr_iovecs);
  149. b = bio_alloc_bioset(gfp_mask, nr_iovecs,
  150. mddev->bio_set);
  151. if (!b)
  152. return NULL;
  153. mddevp = (void*)b;
  154. mddevp[-1] = mddev;
  155. b->bi_destructor = mddev_bio_destructor;
  156. return b;
  157. }
  158. EXPORT_SYMBOL_GPL(bio_alloc_mddev);
  159. struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
  160. mddev_t *mddev)
  161. {
  162. struct bio *b;
  163. mddev_t **mddevp;
  164. if (!mddev || !mddev->bio_set)
  165. return bio_clone(bio, gfp_mask);
  166. b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
  167. mddev->bio_set);
  168. if (!b)
  169. return NULL;
  170. mddevp = (void*)b;
  171. mddevp[-1] = mddev;
  172. b->bi_destructor = mddev_bio_destructor;
  173. __bio_clone(b, bio);
  174. if (bio_integrity(bio)) {
  175. int ret;
  176. ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
  177. if (ret < 0) {
  178. bio_put(b);
  179. return NULL;
  180. }
  181. }
  182. return b;
  183. }
  184. EXPORT_SYMBOL_GPL(bio_clone_mddev);
  185. /*
  186. * We have a system wide 'event count' that is incremented
  187. * on any 'interesting' event, and readers of /proc/mdstat
  188. * can use 'poll' or 'select' to find out when the event
  189. * count increases.
  190. *
  191. * Events are:
  192. * start array, stop array, error, add device, remove device,
  193. * start build, activate spare
  194. */
  195. static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
  196. static atomic_t md_event_count;
  197. void md_new_event(mddev_t *mddev)
  198. {
  199. atomic_inc(&md_event_count);
  200. wake_up(&md_event_waiters);
  201. }
  202. EXPORT_SYMBOL_GPL(md_new_event);
  203. /* Alternate version that can be called from interrupts
  204. * when calling sysfs_notify isn't needed.
  205. */
  206. static void md_new_event_inintr(mddev_t *mddev)
  207. {
  208. atomic_inc(&md_event_count);
  209. wake_up(&md_event_waiters);
  210. }
  211. /*
  212. * Enables to iterate over all existing md arrays
  213. * all_mddevs_lock protects this list.
  214. */
  215. static LIST_HEAD(all_mddevs);
  216. static DEFINE_SPINLOCK(all_mddevs_lock);
  217. /*
  218. * iterates through all used mddevs in the system.
  219. * We take care to grab the all_mddevs_lock whenever navigating
  220. * the list, and to always hold a refcount when unlocked.
  221. * Any code which breaks out of this loop while own
  222. * a reference to the current mddev and must mddev_put it.
  223. */
  224. #define for_each_mddev(mddev,tmp) \
  225. \
  226. for (({ spin_lock(&all_mddevs_lock); \
  227. tmp = all_mddevs.next; \
  228. mddev = NULL;}); \
  229. ({ if (tmp != &all_mddevs) \
  230. mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
  231. spin_unlock(&all_mddevs_lock); \
  232. if (mddev) mddev_put(mddev); \
  233. mddev = list_entry(tmp, mddev_t, all_mddevs); \
  234. tmp != &all_mddevs;}); \
  235. ({ spin_lock(&all_mddevs_lock); \
  236. tmp = tmp->next;}) \
  237. )
  238. /* Rather than calling directly into the personality make_request function,
  239. * IO requests come here first so that we can check if the device is
  240. * being suspended pending a reconfiguration.
  241. * We hold a refcount over the call to ->make_request. By the time that
  242. * call has finished, the bio has been linked into some internal structure
  243. * and so is visible to ->quiesce(), so we don't need the refcount any more.
  244. */
  245. static int md_make_request(struct request_queue *q, struct bio *bio)
  246. {
  247. const int rw = bio_data_dir(bio);
  248. mddev_t *mddev = q->queuedata;
  249. int rv;
  250. int cpu;
  251. unsigned int sectors;
  252. if (mddev == NULL || mddev->pers == NULL
  253. || !mddev->ready) {
  254. bio_io_error(bio);
  255. return 0;
  256. }
  257. smp_rmb(); /* Ensure implications of 'active' are visible */
  258. rcu_read_lock();
  259. if (mddev->suspended) {
  260. DEFINE_WAIT(__wait);
  261. for (;;) {
  262. prepare_to_wait(&mddev->sb_wait, &__wait,
  263. TASK_UNINTERRUPTIBLE);
  264. if (!mddev->suspended)
  265. break;
  266. rcu_read_unlock();
  267. schedule();
  268. rcu_read_lock();
  269. }
  270. finish_wait(&mddev->sb_wait, &__wait);
  271. }
  272. atomic_inc(&mddev->active_io);
  273. rcu_read_unlock();
  274. /*
  275. * save the sectors now since our bio can
  276. * go away inside make_request
  277. */
  278. sectors = bio_sectors(bio);
  279. rv = mddev->pers->make_request(mddev, bio);
  280. cpu = part_stat_lock();
  281. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  282. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
  283. part_stat_unlock();
  284. if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
  285. wake_up(&mddev->sb_wait);
  286. return rv;
  287. }
  288. /* mddev_suspend makes sure no new requests are submitted
  289. * to the device, and that any requests that have been submitted
  290. * are completely handled.
  291. * Once ->stop is called and completes, the module will be completely
  292. * unused.
  293. */
  294. void mddev_suspend(mddev_t *mddev)
  295. {
  296. BUG_ON(mddev->suspended);
  297. mddev->suspended = 1;
  298. synchronize_rcu();
  299. wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
  300. mddev->pers->quiesce(mddev, 1);
  301. }
  302. EXPORT_SYMBOL_GPL(mddev_suspend);
  303. void mddev_resume(mddev_t *mddev)
  304. {
  305. mddev->suspended = 0;
  306. wake_up(&mddev->sb_wait);
  307. mddev->pers->quiesce(mddev, 0);
  308. md_wakeup_thread(mddev->thread);
  309. md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
  310. }
  311. EXPORT_SYMBOL_GPL(mddev_resume);
  312. int mddev_congested(mddev_t *mddev, int bits)
  313. {
  314. return mddev->suspended;
  315. }
  316. EXPORT_SYMBOL(mddev_congested);
  317. /*
  318. * Generic flush handling for md
  319. */
  320. static void md_end_flush(struct bio *bio, int err)
  321. {
  322. mdk_rdev_t *rdev = bio->bi_private;
  323. mddev_t *mddev = rdev->mddev;
  324. rdev_dec_pending(rdev, mddev);
  325. if (atomic_dec_and_test(&mddev->flush_pending)) {
  326. /* The pre-request flush has finished */
  327. queue_work(md_wq, &mddev->flush_work);
  328. }
  329. bio_put(bio);
  330. }
  331. static void md_submit_flush_data(struct work_struct *ws);
  332. static void submit_flushes(struct work_struct *ws)
  333. {
  334. mddev_t *mddev = container_of(ws, mddev_t, flush_work);
  335. mdk_rdev_t *rdev;
  336. INIT_WORK(&mddev->flush_work, md_submit_flush_data);
  337. atomic_set(&mddev->flush_pending, 1);
  338. rcu_read_lock();
  339. list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
  340. if (rdev->raid_disk >= 0 &&
  341. !test_bit(Faulty, &rdev->flags)) {
  342. /* Take two references, one is dropped
  343. * when request finishes, one after
  344. * we reclaim rcu_read_lock
  345. */
  346. struct bio *bi;
  347. atomic_inc(&rdev->nr_pending);
  348. atomic_inc(&rdev->nr_pending);
  349. rcu_read_unlock();
  350. bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
  351. bi->bi_end_io = md_end_flush;
  352. bi->bi_private = rdev;
  353. bi->bi_bdev = rdev->bdev;
  354. atomic_inc(&mddev->flush_pending);
  355. submit_bio(WRITE_FLUSH, bi);
  356. rcu_read_lock();
  357. rdev_dec_pending(rdev, mddev);
  358. }
  359. rcu_read_unlock();
  360. if (atomic_dec_and_test(&mddev->flush_pending))
  361. queue_work(md_wq, &mddev->flush_work);
  362. }
  363. static void md_submit_flush_data(struct work_struct *ws)
  364. {
  365. mddev_t *mddev = container_of(ws, mddev_t, flush_work);
  366. struct bio *bio = mddev->flush_bio;
  367. if (bio->bi_size == 0)
  368. /* an empty barrier - all done */
  369. bio_endio(bio, 0);
  370. else {
  371. bio->bi_rw &= ~REQ_FLUSH;
  372. if (mddev->pers->make_request(mddev, bio))
  373. generic_make_request(bio);
  374. }
  375. mddev->flush_bio = NULL;
  376. wake_up(&mddev->sb_wait);
  377. }
  378. void md_flush_request(mddev_t *mddev, struct bio *bio)
  379. {
  380. spin_lock_irq(&mddev->write_lock);
  381. wait_event_lock_irq(mddev->sb_wait,
  382. !mddev->flush_bio,
  383. mddev->write_lock, /*nothing*/);
  384. mddev->flush_bio = bio;
  385. spin_unlock_irq(&mddev->write_lock);
  386. INIT_WORK(&mddev->flush_work, submit_flushes);
  387. queue_work(md_wq, &mddev->flush_work);
  388. }
  389. EXPORT_SYMBOL(md_flush_request);
  390. /* Support for plugging.
  391. * This mirrors the plugging support in request_queue, but does not
  392. * require having a whole queue or request structures.
  393. * We allocate an md_plug_cb for each md device and each thread it gets
  394. * plugged on. This links tot the private plug_handle structure in the
  395. * personality data where we keep a count of the number of outstanding
  396. * plugs so other code can see if a plug is active.
  397. */
  398. struct md_plug_cb {
  399. struct blk_plug_cb cb;
  400. mddev_t *mddev;
  401. };
  402. static void plugger_unplug(struct blk_plug_cb *cb)
  403. {
  404. struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
  405. if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
  406. md_wakeup_thread(mdcb->mddev->thread);
  407. kfree(mdcb);
  408. }
  409. /* Check that an unplug wakeup will come shortly.
  410. * If not, wakeup the md thread immediately
  411. */
  412. int mddev_check_plugged(mddev_t *mddev)
  413. {
  414. struct blk_plug *plug = current->plug;
  415. struct md_plug_cb *mdcb;
  416. if (!plug)
  417. return 0;
  418. list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
  419. if (mdcb->cb.callback == plugger_unplug &&
  420. mdcb->mddev == mddev) {
  421. /* Already on the list, move to top */
  422. if (mdcb != list_first_entry(&plug->cb_list,
  423. struct md_plug_cb,
  424. cb.list))
  425. list_move(&mdcb->cb.list, &plug->cb_list);
  426. return 1;
  427. }
  428. }
  429. /* Not currently on the callback list */
  430. mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
  431. if (!mdcb)
  432. return 0;
  433. mdcb->mddev = mddev;
  434. mdcb->cb.callback = plugger_unplug;
  435. atomic_inc(&mddev->plug_cnt);
  436. list_add(&mdcb->cb.list, &plug->cb_list);
  437. return 1;
  438. }
  439. EXPORT_SYMBOL_GPL(mddev_check_plugged);
  440. static inline mddev_t *mddev_get(mddev_t *mddev)
  441. {
  442. atomic_inc(&mddev->active);
  443. return mddev;
  444. }
  445. static void mddev_delayed_delete(struct work_struct *ws);
  446. static void mddev_put(mddev_t *mddev)
  447. {
  448. struct bio_set *bs = NULL;
  449. if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
  450. return;
  451. if (!mddev->raid_disks && list_empty(&mddev->disks) &&
  452. mddev->ctime == 0 && !mddev->hold_active) {
  453. /* Array is not configured at all, and not held active,
  454. * so destroy it */
  455. list_del(&mddev->all_mddevs);
  456. bs = mddev->bio_set;
  457. mddev->bio_set = NULL;
  458. if (mddev->gendisk) {
  459. /* We did a probe so need to clean up. Call
  460. * queue_work inside the spinlock so that
  461. * flush_workqueue() after mddev_find will
  462. * succeed in waiting for the work to be done.
  463. */
  464. INIT_WORK(&mddev->del_work, mddev_delayed_delete);
  465. queue_work(md_misc_wq, &mddev->del_work);
  466. } else
  467. kfree(mddev);
  468. }
  469. spin_unlock(&all_mddevs_lock);
  470. if (bs)
  471. bioset_free(bs);
  472. }
  473. void mddev_init(mddev_t *mddev)
  474. {
  475. mutex_init(&mddev->open_mutex);
  476. mutex_init(&mddev->reconfig_mutex);
  477. mutex_init(&mddev->bitmap_info.mutex);
  478. INIT_LIST_HEAD(&mddev->disks);
  479. INIT_LIST_HEAD(&mddev->all_mddevs);
  480. init_timer(&mddev->safemode_timer);
  481. atomic_set(&mddev->active, 1);
  482. atomic_set(&mddev->openers, 0);
  483. atomic_set(&mddev->active_io, 0);
  484. atomic_set(&mddev->plug_cnt, 0);
  485. spin_lock_init(&mddev->write_lock);
  486. atomic_set(&mddev->flush_pending, 0);
  487. init_waitqueue_head(&mddev->sb_wait);
  488. init_waitqueue_head(&mddev->recovery_wait);
  489. mddev->reshape_position = MaxSector;
  490. mddev->resync_min = 0;
  491. mddev->resync_max = MaxSector;
  492. mddev->level = LEVEL_NONE;
  493. }
  494. EXPORT_SYMBOL_GPL(mddev_init);
  495. static mddev_t * mddev_find(dev_t unit)
  496. {
  497. mddev_t *mddev, *new = NULL;
  498. if (unit && MAJOR(unit) != MD_MAJOR)
  499. unit &= ~((1<<MdpMinorShift)-1);
  500. retry:
  501. spin_lock(&all_mddevs_lock);
  502. if (unit) {
  503. list_for_each_entry(mddev, &all_mddevs, all_mddevs)
  504. if (mddev->unit == unit) {
  505. mddev_get(mddev);
  506. spin_unlock(&all_mddevs_lock);
  507. kfree(new);
  508. return mddev;
  509. }
  510. if (new) {
  511. list_add(&new->all_mddevs, &all_mddevs);
  512. spin_unlock(&all_mddevs_lock);
  513. new->hold_active = UNTIL_IOCTL;
  514. return new;
  515. }
  516. } else if (new) {
  517. /* find an unused unit number */
  518. static int next_minor = 512;
  519. int start = next_minor;
  520. int is_free = 0;
  521. int dev = 0;
  522. while (!is_free) {
  523. dev = MKDEV(MD_MAJOR, next_minor);
  524. next_minor++;
  525. if (next_minor > MINORMASK)
  526. next_minor = 0;
  527. if (next_minor == start) {
  528. /* Oh dear, all in use. */
  529. spin_unlock(&all_mddevs_lock);
  530. kfree(new);
  531. return NULL;
  532. }
  533. is_free = 1;
  534. list_for_each_entry(mddev, &all_mddevs, all_mddevs)
  535. if (mddev->unit == dev) {
  536. is_free = 0;
  537. break;
  538. }
  539. }
  540. new->unit = dev;
  541. new->md_minor = MINOR(dev);
  542. new->hold_active = UNTIL_STOP;
  543. list_add(&new->all_mddevs, &all_mddevs);
  544. spin_unlock(&all_mddevs_lock);
  545. return new;
  546. }
  547. spin_unlock(&all_mddevs_lock);
  548. new = kzalloc(sizeof(*new), GFP_KERNEL);
  549. if (!new)
  550. return NULL;
  551. new->unit = unit;
  552. if (MAJOR(unit) == MD_MAJOR)
  553. new->md_minor = MINOR(unit);
  554. else
  555. new->md_minor = MINOR(unit) >> MdpMinorShift;
  556. mddev_init(new);
  557. goto retry;
  558. }
  559. static inline int mddev_lock(mddev_t * mddev)
  560. {
  561. return mutex_lock_interruptible(&mddev->reconfig_mutex);
  562. }
  563. static inline int mddev_is_locked(mddev_t *mddev)
  564. {
  565. return mutex_is_locked(&mddev->reconfig_mutex);
  566. }
  567. static inline int mddev_trylock(mddev_t * mddev)
  568. {
  569. return mutex_trylock(&mddev->reconfig_mutex);
  570. }
  571. static struct attribute_group md_redundancy_group;
  572. static void mddev_unlock(mddev_t * mddev)
  573. {
  574. if (mddev->to_remove) {
  575. /* These cannot be removed under reconfig_mutex as
  576. * an access to the files will try to take reconfig_mutex
  577. * while holding the file unremovable, which leads to
  578. * a deadlock.
  579. * So hold set sysfs_active while the remove in happeing,
  580. * and anything else which might set ->to_remove or my
  581. * otherwise change the sysfs namespace will fail with
  582. * -EBUSY if sysfs_active is still set.
  583. * We set sysfs_active under reconfig_mutex and elsewhere
  584. * test it under the same mutex to ensure its correct value
  585. * is seen.
  586. */
  587. struct attribute_group *to_remove = mddev->to_remove;
  588. mddev->to_remove = NULL;
  589. mddev->sysfs_active = 1;
  590. mutex_unlock(&mddev->reconfig_mutex);
  591. if (mddev->kobj.sd) {
  592. if (to_remove != &md_redundancy_group)
  593. sysfs_remove_group(&mddev->kobj, to_remove);
  594. if (mddev->pers == NULL ||
  595. mddev->pers->sync_request == NULL) {
  596. sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
  597. if (mddev->sysfs_action)
  598. sysfs_put(mddev->sysfs_action);
  599. mddev->sysfs_action = NULL;
  600. }
  601. }
  602. mddev->sysfs_active = 0;
  603. } else
  604. mutex_unlock(&mddev->reconfig_mutex);
  605. /* was we've dropped the mutex we need a spinlock to
  606. * make sur the thread doesn't disappear
  607. */
  608. spin_lock(&pers_lock);
  609. md_wakeup_thread(mddev->thread);
  610. spin_unlock(&pers_lock);
  611. }
  612. static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
  613. {
  614. mdk_rdev_t *rdev;
  615. list_for_each_entry(rdev, &mddev->disks, same_set)
  616. if (rdev->desc_nr == nr)
  617. return rdev;
  618. return NULL;
  619. }
  620. static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
  621. {
  622. mdk_rdev_t *rdev;
  623. list_for_each_entry(rdev, &mddev->disks, same_set)
  624. if (rdev->bdev->bd_dev == dev)
  625. return rdev;
  626. return NULL;
  627. }
  628. static struct mdk_personality *find_pers(int level, char *clevel)
  629. {
  630. struct mdk_personality *pers;
  631. list_for_each_entry(pers, &pers_list, list) {
  632. if (level != LEVEL_NONE && pers->level == level)
  633. return pers;
  634. if (strcmp(pers->name, clevel)==0)
  635. return pers;
  636. }
  637. return NULL;
  638. }
  639. /* return the offset of the super block in 512byte sectors */
  640. static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
  641. {
  642. sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
  643. return MD_NEW_SIZE_SECTORS(num_sectors);
  644. }
  645. static int alloc_disk_sb(mdk_rdev_t * rdev)
  646. {
  647. if (rdev->sb_page)
  648. MD_BUG();
  649. rdev->sb_page = alloc_page(GFP_KERNEL);
  650. if (!rdev->sb_page) {
  651. printk(KERN_ALERT "md: out of memory.\n");
  652. return -ENOMEM;
  653. }
  654. return 0;
  655. }
  656. static void free_disk_sb(mdk_rdev_t * rdev)
  657. {
  658. if (rdev->sb_page) {
  659. put_page(rdev->sb_page);
  660. rdev->sb_loaded = 0;
  661. rdev->sb_page = NULL;
  662. rdev->sb_start = 0;
  663. rdev->sectors = 0;
  664. }
  665. }
  666. static void super_written(struct bio *bio, int error)
  667. {
  668. mdk_rdev_t *rdev = bio->bi_private;
  669. mddev_t *mddev = rdev->mddev;
  670. if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  671. printk("md: super_written gets error=%d, uptodate=%d\n",
  672. error, test_bit(BIO_UPTODATE, &bio->bi_flags));
  673. WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
  674. md_error(mddev, rdev);
  675. }
  676. if (atomic_dec_and_test(&mddev->pending_writes))
  677. wake_up(&mddev->sb_wait);
  678. bio_put(bio);
  679. }
  680. void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
  681. sector_t sector, int size, struct page *page)
  682. {
  683. /* write first size bytes of page to sector of rdev
  684. * Increment mddev->pending_writes before returning
  685. * and decrement it on completion, waking up sb_wait
  686. * if zero is reached.
  687. * If an error occurred, call md_error
  688. */
  689. struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
  690. bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
  691. bio->bi_sector = sector;
  692. bio_add_page(bio, page, size, 0);
  693. bio->bi_private = rdev;
  694. bio->bi_end_io = super_written;
  695. atomic_inc(&mddev->pending_writes);
  696. submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
  697. }
  698. void md_super_wait(mddev_t *mddev)
  699. {
  700. /* wait for all superblock writes that were scheduled to complete */
  701. DEFINE_WAIT(wq);
  702. for(;;) {
  703. prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
  704. if (atomic_read(&mddev->pending_writes)==0)
  705. break;
  706. schedule();
  707. }
  708. finish_wait(&mddev->sb_wait, &wq);
  709. }
  710. static void bi_complete(struct bio *bio, int error)
  711. {
  712. complete((struct completion*)bio->bi_private);
  713. }
  714. int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
  715. struct page *page, int rw, bool metadata_op)
  716. {
  717. struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
  718. struct completion event;
  719. int ret;
  720. rw |= REQ_SYNC;
  721. bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
  722. rdev->meta_bdev : rdev->bdev;
  723. if (metadata_op)
  724. bio->bi_sector = sector + rdev->sb_start;
  725. else
  726. bio->bi_sector = sector + rdev->data_offset;
  727. bio_add_page(bio, page, size, 0);
  728. init_completion(&event);
  729. bio->bi_private = &event;
  730. bio->bi_end_io = bi_complete;
  731. submit_bio(rw, bio);
  732. wait_for_completion(&event);
  733. ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
  734. bio_put(bio);
  735. return ret;
  736. }
  737. EXPORT_SYMBOL_GPL(sync_page_io);
  738. static int read_disk_sb(mdk_rdev_t * rdev, int size)
  739. {
  740. char b[BDEVNAME_SIZE];
  741. if (!rdev->sb_page) {
  742. MD_BUG();
  743. return -EINVAL;
  744. }
  745. if (rdev->sb_loaded)
  746. return 0;
  747. if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
  748. goto fail;
  749. rdev->sb_loaded = 1;
  750. return 0;
  751. fail:
  752. printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
  753. bdevname(rdev->bdev,b));
  754. return -EINVAL;
  755. }
  756. static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
  757. {
  758. return sb1->set_uuid0 == sb2->set_uuid0 &&
  759. sb1->set_uuid1 == sb2->set_uuid1 &&
  760. sb1->set_uuid2 == sb2->set_uuid2 &&
  761. sb1->set_uuid3 == sb2->set_uuid3;
  762. }
  763. static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
  764. {
  765. int ret;
  766. mdp_super_t *tmp1, *tmp2;
  767. tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
  768. tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
  769. if (!tmp1 || !tmp2) {
  770. ret = 0;
  771. printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
  772. goto abort;
  773. }
  774. *tmp1 = *sb1;
  775. *tmp2 = *sb2;
  776. /*
  777. * nr_disks is not constant
  778. */
  779. tmp1->nr_disks = 0;
  780. tmp2->nr_disks = 0;
  781. ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
  782. abort:
  783. kfree(tmp1);
  784. kfree(tmp2);
  785. return ret;
  786. }
  787. static u32 md_csum_fold(u32 csum)
  788. {
  789. csum = (csum & 0xffff) + (csum >> 16);
  790. return (csum & 0xffff) + (csum >> 16);
  791. }
  792. static unsigned int calc_sb_csum(mdp_super_t * sb)
  793. {
  794. u64 newcsum = 0;
  795. u32 *sb32 = (u32*)sb;
  796. int i;
  797. unsigned int disk_csum, csum;
  798. disk_csum = sb->sb_csum;
  799. sb->sb_csum = 0;
  800. for (i = 0; i < MD_SB_BYTES/4 ; i++)
  801. newcsum += sb32[i];
  802. csum = (newcsum & 0xffffffff) + (newcsum>>32);
  803. #ifdef CONFIG_ALPHA
  804. /* This used to use csum_partial, which was wrong for several
  805. * reasons including that different results are returned on
  806. * different architectures. It isn't critical that we get exactly
  807. * the same return value as before (we always csum_fold before
  808. * testing, and that removes any differences). However as we
  809. * know that csum_partial always returned a 16bit value on
  810. * alphas, do a fold to maximise conformity to previous behaviour.
  811. */
  812. sb->sb_csum = md_csum_fold(disk_csum);
  813. #else
  814. sb->sb_csum = disk_csum;
  815. #endif
  816. return csum;
  817. }
  818. /*
  819. * Handle superblock details.
  820. * We want to be able to handle multiple superblock formats
  821. * so we have a common interface to them all, and an array of
  822. * different handlers.
  823. * We rely on user-space to write the initial superblock, and support
  824. * reading and updating of superblocks.
  825. * Interface methods are:
  826. * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
  827. * loads and validates a superblock on dev.
  828. * if refdev != NULL, compare superblocks on both devices
  829. * Return:
  830. * 0 - dev has a superblock that is compatible with refdev
  831. * 1 - dev has a superblock that is compatible and newer than refdev
  832. * so dev should be used as the refdev in future
  833. * -EINVAL superblock incompatible or invalid
  834. * -othererror e.g. -EIO
  835. *
  836. * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
  837. * Verify that dev is acceptable into mddev.
  838. * The first time, mddev->raid_disks will be 0, and data from
  839. * dev should be merged in. Subsequent calls check that dev
  840. * is new enough. Return 0 or -EINVAL
  841. *
  842. * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
  843. * Update the superblock for rdev with data in mddev
  844. * This does not write to disc.
  845. *
  846. */
  847. struct super_type {
  848. char *name;
  849. struct module *owner;
  850. int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
  851. int minor_version);
  852. int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
  853. void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
  854. unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
  855. sector_t num_sectors);
  856. };
  857. /*
  858. * Check that the given mddev has no bitmap.
  859. *
  860. * This function is called from the run method of all personalities that do not
  861. * support bitmaps. It prints an error message and returns non-zero if mddev
  862. * has a bitmap. Otherwise, it returns 0.
  863. *
  864. */
  865. int md_check_no_bitmap(mddev_t *mddev)
  866. {
  867. if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
  868. return 0;
  869. printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
  870. mdname(mddev), mddev->pers->name);
  871. return 1;
  872. }
  873. EXPORT_SYMBOL(md_check_no_bitmap);
  874. /*
  875. * load_super for 0.90.0
  876. */
  877. static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
  878. {
  879. char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
  880. mdp_super_t *sb;
  881. int ret;
  882. /*
  883. * Calculate the position of the superblock (512byte sectors),
  884. * it's at the end of the disk.
  885. *
  886. * It also happens to be a multiple of 4Kb.
  887. */
  888. rdev->sb_start = calc_dev_sboffset(rdev);
  889. ret = read_disk_sb(rdev, MD_SB_BYTES);
  890. if (ret) return ret;
  891. ret = -EINVAL;
  892. bdevname(rdev->bdev, b);
  893. sb = (mdp_super_t*)page_address(rdev->sb_page);
  894. if (sb->md_magic != MD_SB_MAGIC) {
  895. printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
  896. b);
  897. goto abort;
  898. }
  899. if (sb->major_version != 0 ||
  900. sb->minor_version < 90 ||
  901. sb->minor_version > 91) {
  902. printk(KERN_WARNING "Bad version number %d.%d on %s\n",
  903. sb->major_version, sb->minor_version,
  904. b);
  905. goto abort;
  906. }
  907. if (sb->raid_disks <= 0)
  908. goto abort;
  909. if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
  910. printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
  911. b);
  912. goto abort;
  913. }
  914. rdev->preferred_minor = sb->md_minor;
  915. rdev->data_offset = 0;
  916. rdev->sb_size = MD_SB_BYTES;
  917. if (sb->level == LEVEL_MULTIPATH)
  918. rdev->desc_nr = -1;
  919. else
  920. rdev->desc_nr = sb->this_disk.number;
  921. if (!refdev) {
  922. ret = 1;
  923. } else {
  924. __u64 ev1, ev2;
  925. mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
  926. if (!uuid_equal(refsb, sb)) {
  927. printk(KERN_WARNING "md: %s has different UUID to %s\n",
  928. b, bdevname(refdev->bdev,b2));
  929. goto abort;
  930. }
  931. if (!sb_equal(refsb, sb)) {
  932. printk(KERN_WARNING "md: %s has same UUID"
  933. " but different superblock to %s\n",
  934. b, bdevname(refdev->bdev, b2));
  935. goto abort;
  936. }
  937. ev1 = md_event(sb);
  938. ev2 = md_event(refsb);
  939. if (ev1 > ev2)
  940. ret = 1;
  941. else
  942. ret = 0;
  943. }
  944. rdev->sectors = rdev->sb_start;
  945. /* Limit to 4TB as metadata cannot record more than that */
  946. if (rdev->sectors >= (2ULL << 32))
  947. rdev->sectors = (2ULL << 32) - 2;
  948. if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
  949. /* "this cannot possibly happen" ... */
  950. ret = -EINVAL;
  951. abort:
  952. return ret;
  953. }
  954. /*
  955. * validate_super for 0.90.0
  956. */
  957. static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
  958. {
  959. mdp_disk_t *desc;
  960. mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
  961. __u64 ev1 = md_event(sb);
  962. rdev->raid_disk = -1;
  963. clear_bit(Faulty, &rdev->flags);
  964. clear_bit(In_sync, &rdev->flags);
  965. clear_bit(WriteMostly, &rdev->flags);
  966. if (mddev->raid_disks == 0) {
  967. mddev->major_version = 0;
  968. mddev->minor_version = sb->minor_version;
  969. mddev->patch_version = sb->patch_version;
  970. mddev->external = 0;
  971. mddev->chunk_sectors = sb->chunk_size >> 9;
  972. mddev->ctime = sb->ctime;
  973. mddev->utime = sb->utime;
  974. mddev->level = sb->level;
  975. mddev->clevel[0] = 0;
  976. mddev->layout = sb->layout;
  977. mddev->raid_disks = sb->raid_disks;
  978. mddev->dev_sectors = ((sector_t)sb->size) * 2;
  979. mddev->events = ev1;
  980. mddev->bitmap_info.offset = 0;
  981. mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
  982. if (mddev->minor_version >= 91) {
  983. mddev->reshape_position = sb->reshape_position;
  984. mddev->delta_disks = sb->delta_disks;
  985. mddev->new_level = sb->new_level;
  986. mddev->new_layout = sb->new_layout;
  987. mddev->new_chunk_sectors = sb->new_chunk >> 9;
  988. } else {
  989. mddev->reshape_position = MaxSector;
  990. mddev->delta_disks = 0;
  991. mddev->new_level = mddev->level;
  992. mddev->new_layout = mddev->layout;
  993. mddev->new_chunk_sectors = mddev->chunk_sectors;
  994. }
  995. if (sb->state & (1<<MD_SB_CLEAN))
  996. mddev->recovery_cp = MaxSector;
  997. else {
  998. if (sb->events_hi == sb->cp_events_hi &&
  999. sb->events_lo == sb->cp_events_lo) {
  1000. mddev->recovery_cp = sb->recovery_cp;
  1001. } else
  1002. mddev->recovery_cp = 0;
  1003. }
  1004. memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
  1005. memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
  1006. memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
  1007. memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
  1008. mddev->max_disks = MD_SB_DISKS;
  1009. if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
  1010. mddev->bitmap_info.file == NULL)
  1011. mddev->bitmap_info.offset =
  1012. mddev->bitmap_info.default_offset;
  1013. } else if (mddev->pers == NULL) {
  1014. /* Insist on good event counter while assembling, except
  1015. * for spares (which don't need an event count) */
  1016. ++ev1;
  1017. if (sb->disks[rdev->desc_nr].state & (
  1018. (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
  1019. if (ev1 < mddev->events)
  1020. return -EINVAL;
  1021. } else if (mddev->bitmap) {
  1022. /* if adding to array with a bitmap, then we can accept an
  1023. * older device ... but not too old.
  1024. */
  1025. if (ev1 < mddev->bitmap->events_cleared)
  1026. return 0;
  1027. } else {
  1028. if (ev1 < mddev->events)
  1029. /* just a hot-add of a new device, leave raid_disk at -1 */
  1030. return 0;
  1031. }
  1032. if (mddev->level != LEVEL_MULTIPATH) {
  1033. desc = sb->disks + rdev->desc_nr;
  1034. if (desc->state & (1<<MD_DISK_FAULTY))
  1035. set_bit(Faulty, &rdev->flags);
  1036. else if (desc->state & (1<<MD_DISK_SYNC) /* &&
  1037. desc->raid_disk < mddev->raid_disks */) {
  1038. set_bit(In_sync, &rdev->flags);
  1039. rdev->raid_disk = desc->raid_disk;
  1040. } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
  1041. /* active but not in sync implies recovery up to
  1042. * reshape position. We don't know exactly where
  1043. * that is, so set to zero for now */
  1044. if (mddev->minor_version >= 91) {
  1045. rdev->recovery_offset = 0;
  1046. rdev->raid_disk = desc->raid_disk;
  1047. }
  1048. }
  1049. if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
  1050. set_bit(WriteMostly, &rdev->flags);
  1051. } else /* MULTIPATH are always insync */
  1052. set_bit(In_sync, &rdev->flags);
  1053. return 0;
  1054. }
  1055. /*
  1056. * sync_super for 0.90.0
  1057. */
  1058. static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
  1059. {
  1060. mdp_super_t *sb;
  1061. mdk_rdev_t *rdev2;
  1062. int next_spare = mddev->raid_disks;
  1063. /* make rdev->sb match mddev data..
  1064. *
  1065. * 1/ zero out disks
  1066. * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
  1067. * 3/ any empty disks < next_spare become removed
  1068. *
  1069. * disks[0] gets initialised to REMOVED because
  1070. * we cannot be sure from other fields if it has
  1071. * been initialised or not.
  1072. */
  1073. int i;
  1074. int active=0, working=0,failed=0,spare=0,nr_disks=0;
  1075. rdev->sb_size = MD_SB_BYTES;
  1076. sb = (mdp_super_t*)page_address(rdev->sb_page);
  1077. memset(sb, 0, sizeof(*sb));
  1078. sb->md_magic = MD_SB_MAGIC;
  1079. sb->major_version = mddev->major_version;
  1080. sb->patch_version = mddev->patch_version;
  1081. sb->gvalid_words = 0; /* ignored */
  1082. memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
  1083. memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
  1084. memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
  1085. memcpy(&sb->set_uuid3, mddev->uuid+12,4);
  1086. sb->ctime = mddev->ctime;
  1087. sb->level = mddev->level;
  1088. sb->size = mddev->dev_sectors / 2;
  1089. sb->raid_disks = mddev->raid_disks;
  1090. sb->md_minor = mddev->md_minor;
  1091. sb->not_persistent = 0;
  1092. sb->utime = mddev->utime;
  1093. sb->state = 0;
  1094. sb->events_hi = (mddev->events>>32);
  1095. sb->events_lo = (u32)mddev->events;
  1096. if (mddev->reshape_position == MaxSector)
  1097. sb->minor_version = 90;
  1098. else {
  1099. sb->minor_version = 91;
  1100. sb->reshape_position = mddev->reshape_position;
  1101. sb->new_level = mddev->new_level;
  1102. sb->delta_disks = mddev->delta_disks;
  1103. sb->new_layout = mddev->new_layout;
  1104. sb->new_chunk = mddev->new_chunk_sectors << 9;
  1105. }
  1106. mddev->minor_version = sb->minor_version;
  1107. if (mddev->in_sync)
  1108. {
  1109. sb->recovery_cp = mddev->recovery_cp;
  1110. sb->cp_events_hi = (mddev->events>>32);
  1111. sb->cp_events_lo = (u32)mddev->events;
  1112. if (mddev->recovery_cp == MaxSector)
  1113. sb->state = (1<< MD_SB_CLEAN);
  1114. } else
  1115. sb->recovery_cp = 0;
  1116. sb->layout = mddev->layout;
  1117. sb->chunk_size = mddev->chunk_sectors << 9;
  1118. if (mddev->bitmap && mddev->bitmap_info.file == NULL)
  1119. sb->state |= (1<<MD_SB_BITMAP_PRESENT);
  1120. sb->disks[0].state = (1<<MD_DISK_REMOVED);
  1121. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  1122. mdp_disk_t *d;
  1123. int desc_nr;
  1124. int is_active = test_bit(In_sync, &rdev2->flags);
  1125. if (rdev2->raid_disk >= 0 &&
  1126. sb->minor_version >= 91)
  1127. /* we have nowhere to store the recovery_offset,
  1128. * but if it is not below the reshape_position,
  1129. * we can piggy-back on that.
  1130. */
  1131. is_active = 1;
  1132. if (rdev2->raid_disk < 0 ||
  1133. test_bit(Faulty, &rdev2->flags))
  1134. is_active = 0;
  1135. if (is_active)
  1136. desc_nr = rdev2->raid_disk;
  1137. else
  1138. desc_nr = next_spare++;
  1139. rdev2->desc_nr = desc_nr;
  1140. d = &sb->disks[rdev2->desc_nr];
  1141. nr_disks++;
  1142. d->number = rdev2->desc_nr;
  1143. d->major = MAJOR(rdev2->bdev->bd_dev);
  1144. d->minor = MINOR(rdev2->bdev->bd_dev);
  1145. if (is_active)
  1146. d->raid_disk = rdev2->raid_disk;
  1147. else
  1148. d->raid_disk = rdev2->desc_nr; /* compatibility */
  1149. if (test_bit(Faulty, &rdev2->flags))
  1150. d->state = (1<<MD_DISK_FAULTY);
  1151. else if (is_active) {
  1152. d->state = (1<<MD_DISK_ACTIVE);
  1153. if (test_bit(In_sync, &rdev2->flags))
  1154. d->state |= (1<<MD_DISK_SYNC);
  1155. active++;
  1156. working++;
  1157. } else {
  1158. d->state = 0;
  1159. spare++;
  1160. working++;
  1161. }
  1162. if (test_bit(WriteMostly, &rdev2->flags))
  1163. d->state |= (1<<MD_DISK_WRITEMOSTLY);
  1164. }
  1165. /* now set the "removed" and "faulty" bits on any missing devices */
  1166. for (i=0 ; i < mddev->raid_disks ; i++) {
  1167. mdp_disk_t *d = &sb->disks[i];
  1168. if (d->state == 0 && d->number == 0) {
  1169. d->number = i;
  1170. d->raid_disk = i;
  1171. d->state = (1<<MD_DISK_REMOVED);
  1172. d->state |= (1<<MD_DISK_FAULTY);
  1173. failed++;
  1174. }
  1175. }
  1176. sb->nr_disks = nr_disks;
  1177. sb->active_disks = active;
  1178. sb->working_disks = working;
  1179. sb->failed_disks = failed;
  1180. sb->spare_disks = spare;
  1181. sb->this_disk = sb->disks[rdev->desc_nr];
  1182. sb->sb_csum = calc_sb_csum(sb);
  1183. }
  1184. /*
  1185. * rdev_size_change for 0.90.0
  1186. */
  1187. static unsigned long long
  1188. super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
  1189. {
  1190. if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
  1191. return 0; /* component must fit device */
  1192. if (rdev->mddev->bitmap_info.offset)
  1193. return 0; /* can't move bitmap */
  1194. rdev->sb_start = calc_dev_sboffset(rdev);
  1195. if (!num_sectors || num_sectors > rdev->sb_start)
  1196. num_sectors = rdev->sb_start;
  1197. /* Limit to 4TB as metadata cannot record more than that.
  1198. * 4TB == 2^32 KB, or 2*2^32 sectors.
  1199. */
  1200. if (num_sectors >= (2ULL << 32))
  1201. num_sectors = (2ULL << 32) - 2;
  1202. md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
  1203. rdev->sb_page);
  1204. md_super_wait(rdev->mddev);
  1205. return num_sectors;
  1206. }
  1207. /*
  1208. * version 1 superblock
  1209. */
  1210. static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
  1211. {
  1212. __le32 disk_csum;
  1213. u32 csum;
  1214. unsigned long long newcsum;
  1215. int size = 256 + le32_to_cpu(sb->max_dev)*2;
  1216. __le32 *isuper = (__le32*)sb;
  1217. int i;
  1218. disk_csum = sb->sb_csum;
  1219. sb->sb_csum = 0;
  1220. newcsum = 0;
  1221. for (i=0; size>=4; size -= 4 )
  1222. newcsum += le32_to_cpu(*isuper++);
  1223. if (size == 2)
  1224. newcsum += le16_to_cpu(*(__le16*) isuper);
  1225. csum = (newcsum & 0xffffffff) + (newcsum >> 32);
  1226. sb->sb_csum = disk_csum;
  1227. return cpu_to_le32(csum);
  1228. }
  1229. static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
  1230. {
  1231. struct mdp_superblock_1 *sb;
  1232. int ret;
  1233. sector_t sb_start;
  1234. char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
  1235. int bmask;
  1236. /*
  1237. * Calculate the position of the superblock in 512byte sectors.
  1238. * It is always aligned to a 4K boundary and
  1239. * depeding on minor_version, it can be:
  1240. * 0: At least 8K, but less than 12K, from end of device
  1241. * 1: At start of device
  1242. * 2: 4K from start of device.
  1243. */
  1244. switch(minor_version) {
  1245. case 0:
  1246. sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
  1247. sb_start -= 8*2;
  1248. sb_start &= ~(sector_t)(4*2-1);
  1249. break;
  1250. case 1:
  1251. sb_start = 0;
  1252. break;
  1253. case 2:
  1254. sb_start = 8;
  1255. break;
  1256. default:
  1257. return -EINVAL;
  1258. }
  1259. rdev->sb_start = sb_start;
  1260. /* superblock is rarely larger than 1K, but it can be larger,
  1261. * and it is safe to read 4k, so we do that
  1262. */
  1263. ret = read_disk_sb(rdev, 4096);
  1264. if (ret) return ret;
  1265. sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
  1266. if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
  1267. sb->major_version != cpu_to_le32(1) ||
  1268. le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
  1269. le64_to_cpu(sb->super_offset) != rdev->sb_start ||
  1270. (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
  1271. return -EINVAL;
  1272. if (calc_sb_1_csum(sb) != sb->sb_csum) {
  1273. printk("md: invalid superblock checksum on %s\n",
  1274. bdevname(rdev->bdev,b));
  1275. return -EINVAL;
  1276. }
  1277. if (le64_to_cpu(sb->data_size) < 10) {
  1278. printk("md: data_size too small on %s\n",
  1279. bdevname(rdev->bdev,b));
  1280. return -EINVAL;
  1281. }
  1282. rdev->preferred_minor = 0xffff;
  1283. rdev->data_offset = le64_to_cpu(sb->data_offset);
  1284. atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
  1285. rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
  1286. bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
  1287. if (rdev->sb_size & bmask)
  1288. rdev->sb_size = (rdev->sb_size | bmask) + 1;
  1289. if (minor_version
  1290. && rdev->data_offset < sb_start + (rdev->sb_size/512))
  1291. return -EINVAL;
  1292. if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
  1293. rdev->desc_nr = -1;
  1294. else
  1295. rdev->desc_nr = le32_to_cpu(sb->dev_number);
  1296. if (!refdev) {
  1297. ret = 1;
  1298. } else {
  1299. __u64 ev1, ev2;
  1300. struct mdp_superblock_1 *refsb =
  1301. (struct mdp_superblock_1*)page_address(refdev->sb_page);
  1302. if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
  1303. sb->level != refsb->level ||
  1304. sb->layout != refsb->layout ||
  1305. sb->chunksize != refsb->chunksize) {
  1306. printk(KERN_WARNING "md: %s has strangely different"
  1307. " superblock to %s\n",
  1308. bdevname(rdev->bdev,b),
  1309. bdevname(refdev->bdev,b2));
  1310. return -EINVAL;
  1311. }
  1312. ev1 = le64_to_cpu(sb->events);
  1313. ev2 = le64_to_cpu(refsb->events);
  1314. if (ev1 > ev2)
  1315. ret = 1;
  1316. else
  1317. ret = 0;
  1318. }
  1319. if (minor_version)
  1320. rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
  1321. le64_to_cpu(sb->data_offset);
  1322. else
  1323. rdev->sectors = rdev->sb_start;
  1324. if (rdev->sectors < le64_to_cpu(sb->data_size))
  1325. return -EINVAL;
  1326. rdev->sectors = le64_to_cpu(sb->data_size);
  1327. if (le64_to_cpu(sb->size) > rdev->sectors)
  1328. return -EINVAL;
  1329. return ret;
  1330. }
  1331. static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
  1332. {
  1333. struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
  1334. __u64 ev1 = le64_to_cpu(sb->events);
  1335. rdev->raid_disk = -1;
  1336. clear_bit(Faulty, &rdev->flags);
  1337. clear_bit(In_sync, &rdev->flags);
  1338. clear_bit(WriteMostly, &rdev->flags);
  1339. if (mddev->raid_disks == 0) {
  1340. mddev->major_version = 1;
  1341. mddev->patch_version = 0;
  1342. mddev->external = 0;
  1343. mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
  1344. mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
  1345. mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
  1346. mddev->level = le32_to_cpu(sb->level);
  1347. mddev->clevel[0] = 0;
  1348. mddev->layout = le32_to_cpu(sb->layout);
  1349. mddev->raid_disks = le32_to_cpu(sb->raid_disks);
  1350. mddev->dev_sectors = le64_to_cpu(sb->size);
  1351. mddev->events = ev1;
  1352. mddev->bitmap_info.offset = 0;
  1353. mddev->bitmap_info.default_offset = 1024 >> 9;
  1354. mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
  1355. memcpy(mddev->uuid, sb->set_uuid, 16);
  1356. mddev->max_disks = (4096-256)/2;
  1357. if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
  1358. mddev->bitmap_info.file == NULL )
  1359. mddev->bitmap_info.offset =
  1360. (__s32)le32_to_cpu(sb->bitmap_offset);
  1361. if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
  1362. mddev->reshape_position = le64_to_cpu(sb->reshape_position);
  1363. mddev->delta_disks = le32_to_cpu(sb->delta_disks);
  1364. mddev->new_level = le32_to_cpu(sb->new_level);
  1365. mddev->new_layout = le32_to_cpu(sb->new_layout);
  1366. mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
  1367. } else {
  1368. mddev->reshape_position = MaxSector;
  1369. mddev->delta_disks = 0;
  1370. mddev->new_level = mddev->level;
  1371. mddev->new_layout = mddev->layout;
  1372. mddev->new_chunk_sectors = mddev->chunk_sectors;
  1373. }
  1374. } else if (mddev->pers == NULL) {
  1375. /* Insist of good event counter while assembling, except for
  1376. * spares (which don't need an event count) */
  1377. ++ev1;
  1378. if (rdev->desc_nr >= 0 &&
  1379. rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
  1380. le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
  1381. if (ev1 < mddev->events)
  1382. return -EINVAL;
  1383. } else if (mddev->bitmap) {
  1384. /* If adding to array with a bitmap, then we can accept an
  1385. * older device, but not too old.
  1386. */
  1387. if (ev1 < mddev->bitmap->events_cleared)
  1388. return 0;
  1389. } else {
  1390. if (ev1 < mddev->events)
  1391. /* just a hot-add of a new device, leave raid_disk at -1 */
  1392. return 0;
  1393. }
  1394. if (mddev->level != LEVEL_MULTIPATH) {
  1395. int role;
  1396. if (rdev->desc_nr < 0 ||
  1397. rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
  1398. role = 0xffff;
  1399. rdev->desc_nr = -1;
  1400. } else
  1401. role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
  1402. switch(role) {
  1403. case 0xffff: /* spare */
  1404. break;
  1405. case 0xfffe: /* faulty */
  1406. set_bit(Faulty, &rdev->flags);
  1407. break;
  1408. default:
  1409. if ((le32_to_cpu(sb->feature_map) &
  1410. MD_FEATURE_RECOVERY_OFFSET))
  1411. rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
  1412. else
  1413. set_bit(In_sync, &rdev->flags);
  1414. rdev->raid_disk = role;
  1415. break;
  1416. }
  1417. if (sb->devflags & WriteMostly1)
  1418. set_bit(WriteMostly, &rdev->flags);
  1419. } else /* MULTIPATH are always insync */
  1420. set_bit(In_sync, &rdev->flags);
  1421. return 0;
  1422. }
  1423. static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
  1424. {
  1425. struct mdp_superblock_1 *sb;
  1426. mdk_rdev_t *rdev2;
  1427. int max_dev, i;
  1428. /* make rdev->sb match mddev and rdev data. */
  1429. sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
  1430. sb->feature_map = 0;
  1431. sb->pad0 = 0;
  1432. sb->recovery_offset = cpu_to_le64(0);
  1433. memset(sb->pad1, 0, sizeof(sb->pad1));
  1434. memset(sb->pad2, 0, sizeof(sb->pad2));
  1435. memset(sb->pad3, 0, sizeof(sb->pad3));
  1436. sb->utime = cpu_to_le64((__u64)mddev->utime);
  1437. sb->events = cpu_to_le64(mddev->events);
  1438. if (mddev->in_sync)
  1439. sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
  1440. else
  1441. sb->resync_offset = cpu_to_le64(0);
  1442. sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
  1443. sb->raid_disks = cpu_to_le32(mddev->raid_disks);
  1444. sb->size = cpu_to_le64(mddev->dev_sectors);
  1445. sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
  1446. sb->level = cpu_to_le32(mddev->level);
  1447. sb->layout = cpu_to_le32(mddev->layout);
  1448. if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
  1449. sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
  1450. sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
  1451. }
  1452. if (rdev->raid_disk >= 0 &&
  1453. !test_bit(In_sync, &rdev->flags)) {
  1454. sb->feature_map |=
  1455. cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
  1456. sb->recovery_offset =
  1457. cpu_to_le64(rdev->recovery_offset);
  1458. }
  1459. if (mddev->reshape_position != MaxSector) {
  1460. sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
  1461. sb->reshape_position = cpu_to_le64(mddev->reshape_position);
  1462. sb->new_layout = cpu_to_le32(mddev->new_layout);
  1463. sb->delta_disks = cpu_to_le32(mddev->delta_disks);
  1464. sb->new_level = cpu_to_le32(mddev->new_level);
  1465. sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
  1466. }
  1467. max_dev = 0;
  1468. list_for_each_entry(rdev2, &mddev->disks, same_set)
  1469. if (rdev2->desc_nr+1 > max_dev)
  1470. max_dev = rdev2->desc_nr+1;
  1471. if (max_dev > le32_to_cpu(sb->max_dev)) {
  1472. int bmask;
  1473. sb->max_dev = cpu_to_le32(max_dev);
  1474. rdev->sb_size = max_dev * 2 + 256;
  1475. bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
  1476. if (rdev->sb_size & bmask)
  1477. rdev->sb_size = (rdev->sb_size | bmask) + 1;
  1478. } else
  1479. max_dev = le32_to_cpu(sb->max_dev);
  1480. for (i=0; i<max_dev;i++)
  1481. sb->dev_roles[i] = cpu_to_le16(0xfffe);
  1482. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  1483. i = rdev2->desc_nr;
  1484. if (test_bit(Faulty, &rdev2->flags))
  1485. sb->dev_roles[i] = cpu_to_le16(0xfffe);
  1486. else if (test_bit(In_sync, &rdev2->flags))
  1487. sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
  1488. else if (rdev2->raid_disk >= 0)
  1489. sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
  1490. else
  1491. sb->dev_roles[i] = cpu_to_le16(0xffff);
  1492. }
  1493. sb->sb_csum = calc_sb_1_csum(sb);
  1494. }
  1495. static unsigned long long
  1496. super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
  1497. {
  1498. struct mdp_superblock_1 *sb;
  1499. sector_t max_sectors;
  1500. if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
  1501. return 0; /* component must fit device */
  1502. if (rdev->sb_start < rdev->data_offset) {
  1503. /* minor versions 1 and 2; superblock before data */
  1504. max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
  1505. max_sectors -= rdev->data_offset;
  1506. if (!num_sectors || num_sectors > max_sectors)
  1507. num_sectors = max_sectors;
  1508. } else if (rdev->mddev->bitmap_info.offset) {
  1509. /* minor version 0 with bitmap we can't move */
  1510. return 0;
  1511. } else {
  1512. /* minor version 0; superblock after data */
  1513. sector_t sb_start;
  1514. sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
  1515. sb_start &= ~(sector_t)(4*2 - 1);
  1516. max_sectors = rdev->sectors + sb_start - rdev->sb_start;
  1517. if (!num_sectors || num_sectors > max_sectors)
  1518. num_sectors = max_sectors;
  1519. rdev->sb_start = sb_start;
  1520. }
  1521. sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
  1522. sb->data_size = cpu_to_le64(num_sectors);
  1523. sb->super_offset = rdev->sb_start;
  1524. sb->sb_csum = calc_sb_1_csum(sb);
  1525. md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
  1526. rdev->sb_page);
  1527. md_super_wait(rdev->mddev);
  1528. return num_sectors;
  1529. }
  1530. static struct super_type super_types[] = {
  1531. [0] = {
  1532. .name = "0.90.0",
  1533. .owner = THIS_MODULE,
  1534. .load_super = super_90_load,
  1535. .validate_super = super_90_validate,
  1536. .sync_super = super_90_sync,
  1537. .rdev_size_change = super_90_rdev_size_change,
  1538. },
  1539. [1] = {
  1540. .name = "md-1",
  1541. .owner = THIS_MODULE,
  1542. .load_super = super_1_load,
  1543. .validate_super = super_1_validate,
  1544. .sync_super = super_1_sync,
  1545. .rdev_size_change = super_1_rdev_size_change,
  1546. },
  1547. };
  1548. static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev)
  1549. {
  1550. if (mddev->sync_super) {
  1551. mddev->sync_super(mddev, rdev);
  1552. return;
  1553. }
  1554. BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
  1555. super_types[mddev->major_version].sync_super(mddev, rdev);
  1556. }
  1557. static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
  1558. {
  1559. mdk_rdev_t *rdev, *rdev2;
  1560. rcu_read_lock();
  1561. rdev_for_each_rcu(rdev, mddev1)
  1562. rdev_for_each_rcu(rdev2, mddev2)
  1563. if (rdev->bdev->bd_contains ==
  1564. rdev2->bdev->bd_contains) {
  1565. rcu_read_unlock();
  1566. return 1;
  1567. }
  1568. rcu_read_unlock();
  1569. return 0;
  1570. }
  1571. static LIST_HEAD(pending_raid_disks);
  1572. /*
  1573. * Try to register data integrity profile for an mddev
  1574. *
  1575. * This is called when an array is started and after a disk has been kicked
  1576. * from the array. It only succeeds if all working and active component devices
  1577. * are integrity capable with matching profiles.
  1578. */
  1579. int md_integrity_register(mddev_t *mddev)
  1580. {
  1581. mdk_rdev_t *rdev, *reference = NULL;
  1582. if (list_empty(&mddev->disks))
  1583. return 0; /* nothing to do */
  1584. if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
  1585. return 0; /* shouldn't register, or already is */
  1586. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1587. /* skip spares and non-functional disks */
  1588. if (test_bit(Faulty, &rdev->flags))
  1589. continue;
  1590. if (rdev->raid_disk < 0)
  1591. continue;
  1592. if (!reference) {
  1593. /* Use the first rdev as the reference */
  1594. reference = rdev;
  1595. continue;
  1596. }
  1597. /* does this rdev's profile match the reference profile? */
  1598. if (blk_integrity_compare(reference->bdev->bd_disk,
  1599. rdev->bdev->bd_disk) < 0)
  1600. return -EINVAL;
  1601. }
  1602. if (!reference || !bdev_get_integrity(reference->bdev))
  1603. return 0;
  1604. /*
  1605. * All component devices are integrity capable and have matching
  1606. * profiles, register the common profile for the md device.
  1607. */
  1608. if (blk_integrity_register(mddev->gendisk,
  1609. bdev_get_integrity(reference->bdev)) != 0) {
  1610. printk(KERN_ERR "md: failed to register integrity for %s\n",
  1611. mdname(mddev));
  1612. return -EINVAL;
  1613. }
  1614. printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
  1615. if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
  1616. printk(KERN_ERR "md: failed to create integrity pool for %s\n",
  1617. mdname(mddev));
  1618. return -EINVAL;
  1619. }
  1620. return 0;
  1621. }
  1622. EXPORT_SYMBOL(md_integrity_register);
  1623. /* Disable data integrity if non-capable/non-matching disk is being added */
  1624. void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  1625. {
  1626. struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
  1627. struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
  1628. if (!bi_mddev) /* nothing to do */
  1629. return;
  1630. if (rdev->raid_disk < 0) /* skip spares */
  1631. return;
  1632. if (bi_rdev && blk_integrity_compare(mddev->gendisk,
  1633. rdev->bdev->bd_disk) >= 0)
  1634. return;
  1635. printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
  1636. blk_integrity_unregister(mddev->gendisk);
  1637. }
  1638. EXPORT_SYMBOL(md_integrity_add_rdev);
  1639. static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
  1640. {
  1641. char b[BDEVNAME_SIZE];
  1642. struct kobject *ko;
  1643. char *s;
  1644. int err;
  1645. if (rdev->mddev) {
  1646. MD_BUG();
  1647. return -EINVAL;
  1648. }
  1649. /* prevent duplicates */
  1650. if (find_rdev(mddev, rdev->bdev->bd_dev))
  1651. return -EEXIST;
  1652. /* make sure rdev->sectors exceeds mddev->dev_sectors */
  1653. if (rdev->sectors && (mddev->dev_sectors == 0 ||
  1654. rdev->sectors < mddev->dev_sectors)) {
  1655. if (mddev->pers) {
  1656. /* Cannot change size, so fail
  1657. * If mddev->level <= 0, then we don't care
  1658. * about aligning sizes (e.g. linear)
  1659. */
  1660. if (mddev->level > 0)
  1661. return -ENOSPC;
  1662. } else
  1663. mddev->dev_sectors = rdev->sectors;
  1664. }
  1665. /* Verify rdev->desc_nr is unique.
  1666. * If it is -1, assign a free number, else
  1667. * check number is not in use
  1668. */
  1669. if (rdev->desc_nr < 0) {
  1670. int choice = 0;
  1671. if (mddev->pers) choice = mddev->raid_disks;
  1672. while (find_rdev_nr(mddev, choice))
  1673. choice++;
  1674. rdev->desc_nr = choice;
  1675. } else {
  1676. if (find_rdev_nr(mddev, rdev->desc_nr))
  1677. return -EBUSY;
  1678. }
  1679. if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
  1680. printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
  1681. mdname(mddev), mddev->max_disks);
  1682. return -EBUSY;
  1683. }
  1684. bdevname(rdev->bdev,b);
  1685. while ( (s=strchr(b, '/')) != NULL)
  1686. *s = '!';
  1687. rdev->mddev = mddev;
  1688. printk(KERN_INFO "md: bind<%s>\n", b);
  1689. if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
  1690. goto fail;
  1691. ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
  1692. if (sysfs_create_link(&rdev->kobj, ko, "block"))
  1693. /* failure here is OK */;
  1694. rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
  1695. list_add_rcu(&rdev->same_set, &mddev->disks);
  1696. bd_link_disk_holder(rdev->bdev, mddev->gendisk);
  1697. /* May as well allow recovery to be retried once */
  1698. mddev->recovery_disabled = 0;
  1699. return 0;
  1700. fail:
  1701. printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
  1702. b, mdname(mddev));
  1703. return err;
  1704. }
  1705. static void md_delayed_delete(struct work_struct *ws)
  1706. {
  1707. mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
  1708. kobject_del(&rdev->kobj);
  1709. kobject_put(&rdev->kobj);
  1710. }
  1711. static void unbind_rdev_from_array(mdk_rdev_t * rdev)
  1712. {
  1713. char b[BDEVNAME_SIZE];
  1714. if (!rdev->mddev) {
  1715. MD_BUG();
  1716. return;
  1717. }
  1718. bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
  1719. list_del_rcu(&rdev->same_set);
  1720. printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
  1721. rdev->mddev = NULL;
  1722. sysfs_remove_link(&rdev->kobj, "block");
  1723. sysfs_put(rdev->sysfs_state);
  1724. rdev->sysfs_state = NULL;
  1725. /* We need to delay this, otherwise we can deadlock when
  1726. * writing to 'remove' to "dev/state". We also need
  1727. * to delay it due to rcu usage.
  1728. */
  1729. synchronize_rcu();
  1730. INIT_WORK(&rdev->del_work, md_delayed_delete);
  1731. kobject_get(&rdev->kobj);
  1732. queue_work(md_misc_wq, &rdev->del_work);
  1733. }
  1734. /*
  1735. * prevent the device from being mounted, repartitioned or
  1736. * otherwise reused by a RAID array (or any other kernel
  1737. * subsystem), by bd_claiming the device.
  1738. */
  1739. static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
  1740. {
  1741. int err = 0;
  1742. struct block_device *bdev;
  1743. char b[BDEVNAME_SIZE];
  1744. bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
  1745. shared ? (mdk_rdev_t *)lock_rdev : rdev);
  1746. if (IS_ERR(bdev)) {
  1747. printk(KERN_ERR "md: could not open %s.\n",
  1748. __bdevname(dev, b));
  1749. return PTR_ERR(bdev);
  1750. }
  1751. rdev->bdev = bdev;
  1752. return err;
  1753. }
  1754. static void unlock_rdev(mdk_rdev_t *rdev)
  1755. {
  1756. struct block_device *bdev = rdev->bdev;
  1757. rdev->bdev = NULL;
  1758. if (!bdev)
  1759. MD_BUG();
  1760. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1761. }
  1762. void md_autodetect_dev(dev_t dev);
  1763. static void export_rdev(mdk_rdev_t * rdev)
  1764. {
  1765. char b[BDEVNAME_SIZE];
  1766. printk(KERN_INFO "md: export_rdev(%s)\n",
  1767. bdevname(rdev->bdev,b));
  1768. if (rdev->mddev)
  1769. MD_BUG();
  1770. free_disk_sb(rdev);
  1771. #ifndef MODULE
  1772. if (test_bit(AutoDetected, &rdev->flags))
  1773. md_autodetect_dev(rdev->bdev->bd_dev);
  1774. #endif
  1775. unlock_rdev(rdev);
  1776. kobject_put(&rdev->kobj);
  1777. }
  1778. static void kick_rdev_from_array(mdk_rdev_t * rdev)
  1779. {
  1780. unbind_rdev_from_array(rdev);
  1781. export_rdev(rdev);
  1782. }
  1783. static void export_array(mddev_t *mddev)
  1784. {
  1785. mdk_rdev_t *rdev, *tmp;
  1786. rdev_for_each(rdev, tmp, mddev) {
  1787. if (!rdev->mddev) {
  1788. MD_BUG();
  1789. continue;
  1790. }
  1791. kick_rdev_from_array(rdev);
  1792. }
  1793. if (!list_empty(&mddev->disks))
  1794. MD_BUG();
  1795. mddev->raid_disks = 0;
  1796. mddev->major_version = 0;
  1797. }
  1798. static void print_desc(mdp_disk_t *desc)
  1799. {
  1800. printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
  1801. desc->major,desc->minor,desc->raid_disk,desc->state);
  1802. }
  1803. static void print_sb_90(mdp_super_t *sb)
  1804. {
  1805. int i;
  1806. printk(KERN_INFO
  1807. "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
  1808. sb->major_version, sb->minor_version, sb->patch_version,
  1809. sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
  1810. sb->ctime);
  1811. printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
  1812. sb->level, sb->size, sb->nr_disks, sb->raid_disks,
  1813. sb->md_minor, sb->layout, sb->chunk_size);
  1814. printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
  1815. " FD:%d SD:%d CSUM:%08x E:%08lx\n",
  1816. sb->utime, sb->state, sb->active_disks, sb->working_disks,
  1817. sb->failed_disks, sb->spare_disks,
  1818. sb->sb_csum, (unsigned long)sb->events_lo);
  1819. printk(KERN_INFO);
  1820. for (i = 0; i < MD_SB_DISKS; i++) {
  1821. mdp_disk_t *desc;
  1822. desc = sb->disks + i;
  1823. if (desc->number || desc->major || desc->minor ||
  1824. desc->raid_disk || (desc->state && (desc->state != 4))) {
  1825. printk(" D %2d: ", i);
  1826. print_desc(desc);
  1827. }
  1828. }
  1829. printk(KERN_INFO "md: THIS: ");
  1830. print_desc(&sb->this_disk);
  1831. }
  1832. static void print_sb_1(struct mdp_superblock_1 *sb)
  1833. {
  1834. __u8 *uuid;
  1835. uuid = sb->set_uuid;
  1836. printk(KERN_INFO
  1837. "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
  1838. "md: Name: \"%s\" CT:%llu\n",
  1839. le32_to_cpu(sb->major_version),
  1840. le32_to_cpu(sb->feature_map),
  1841. uuid,
  1842. sb->set_name,
  1843. (unsigned long long)le64_to_cpu(sb->ctime)
  1844. & MD_SUPERBLOCK_1_TIME_SEC_MASK);
  1845. uuid = sb->device_uuid;
  1846. printk(KERN_INFO
  1847. "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
  1848. " RO:%llu\n"
  1849. "md: Dev:%08x UUID: %pU\n"
  1850. "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
  1851. "md: (MaxDev:%u) \n",
  1852. le32_to_cpu(sb->level),
  1853. (unsigned long long)le64_to_cpu(sb->size),
  1854. le32_to_cpu(sb->raid_disks),
  1855. le32_to_cpu(sb->layout),
  1856. le32_to_cpu(sb->chunksize),
  1857. (unsigned long long)le64_to_cpu(sb->data_offset),
  1858. (unsigned long long)le64_to_cpu(sb->data_size),
  1859. (unsigned long long)le64_to_cpu(sb->super_offset),
  1860. (unsigned long long)le64_to_cpu(sb->recovery_offset),
  1861. le32_to_cpu(sb->dev_number),
  1862. uuid,
  1863. sb->devflags,
  1864. (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
  1865. (unsigned long long)le64_to_cpu(sb->events),
  1866. (unsigned long long)le64_to_cpu(sb->resync_offset),
  1867. le32_to_cpu(sb->sb_csum),
  1868. le32_to_cpu(sb->max_dev)
  1869. );
  1870. }
  1871. static void print_rdev(mdk_rdev_t *rdev, int major_version)
  1872. {
  1873. char b[BDEVNAME_SIZE];
  1874. printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
  1875. bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
  1876. test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
  1877. rdev->desc_nr);
  1878. if (rdev->sb_loaded) {
  1879. printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
  1880. switch (major_version) {
  1881. case 0:
  1882. print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
  1883. break;
  1884. case 1:
  1885. print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
  1886. break;
  1887. }
  1888. } else
  1889. printk(KERN_INFO "md: no rdev superblock!\n");
  1890. }
  1891. static void md_print_devices(void)
  1892. {
  1893. struct list_head *tmp;
  1894. mdk_rdev_t *rdev;
  1895. mddev_t *mddev;
  1896. char b[BDEVNAME_SIZE];
  1897. printk("\n");
  1898. printk("md: **********************************\n");
  1899. printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
  1900. printk("md: **********************************\n");
  1901. for_each_mddev(mddev, tmp) {
  1902. if (mddev->bitmap)
  1903. bitmap_print_sb(mddev->bitmap);
  1904. else
  1905. printk("%s: ", mdname(mddev));
  1906. list_for_each_entry(rdev, &mddev->disks, same_set)
  1907. printk("<%s>", bdevname(rdev->bdev,b));
  1908. printk("\n");
  1909. list_for_each_entry(rdev, &mddev->disks, same_set)
  1910. print_rdev(rdev, mddev->major_version);
  1911. }
  1912. printk("md: **********************************\n");
  1913. printk("\n");
  1914. }
  1915. static void sync_sbs(mddev_t * mddev, int nospares)
  1916. {
  1917. /* Update each superblock (in-memory image), but
  1918. * if we are allowed to, skip spares which already
  1919. * have the right event counter, or have one earlier
  1920. * (which would mean they aren't being marked as dirty
  1921. * with the rest of the array)
  1922. */
  1923. mdk_rdev_t *rdev;
  1924. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1925. if (rdev->sb_events == mddev->events ||
  1926. (nospares &&
  1927. rdev->raid_disk < 0 &&
  1928. rdev->sb_events+1 == mddev->events)) {
  1929. /* Don't update this superblock */
  1930. rdev->sb_loaded = 2;
  1931. } else {
  1932. sync_super(mddev, rdev);
  1933. rdev->sb_loaded = 1;
  1934. }
  1935. }
  1936. }
  1937. static void md_update_sb(mddev_t * mddev, int force_change)
  1938. {
  1939. mdk_rdev_t *rdev;
  1940. int sync_req;
  1941. int nospares = 0;
  1942. repeat:
  1943. /* First make sure individual recovery_offsets are correct */
  1944. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1945. if (rdev->raid_disk >= 0 &&
  1946. mddev->delta_disks >= 0 &&
  1947. !test_bit(In_sync, &rdev->flags) &&
  1948. mddev->curr_resync_completed > rdev->recovery_offset)
  1949. rdev->recovery_offset = mddev->curr_resync_completed;
  1950. }
  1951. if (!mddev->persistent) {
  1952. clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
  1953. clear_bit(MD_CHANGE_DEVS, &mddev->flags);
  1954. if (!mddev->external)
  1955. clear_bit(MD_CHANGE_PENDING, &mddev->flags);
  1956. wake_up(&mddev->sb_wait);
  1957. return;
  1958. }
  1959. spin_lock_irq(&mddev->write_lock);
  1960. mddev->utime = get_seconds();
  1961. if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
  1962. force_change = 1;
  1963. if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
  1964. /* just a clean<-> dirty transition, possibly leave spares alone,
  1965. * though if events isn't the right even/odd, we will have to do
  1966. * spares after all
  1967. */
  1968. nospares = 1;
  1969. if (force_change)
  1970. nospares = 0;
  1971. if (mddev->degraded)
  1972. /* If the array is degraded, then skipping spares is both
  1973. * dangerous and fairly pointless.
  1974. * Dangerous because a device that was removed from the array
  1975. * might have a event_count that still looks up-to-date,
  1976. * so it can be re-added without a resync.
  1977. * Pointless because if there are any spares to skip,
  1978. * then a recovery will happen and soon that array won't
  1979. * be degraded any more and the spare can go back to sleep then.
  1980. */
  1981. nospares = 0;
  1982. sync_req = mddev->in_sync;
  1983. /* If this is just a dirty<->clean transition, and the array is clean
  1984. * and 'events' is odd, we can roll back to the previous clean state */
  1985. if (nospares
  1986. && (mddev->in_sync && mddev->recovery_cp == MaxSector)
  1987. && mddev->can_decrease_events
  1988. && mddev->events != 1) {
  1989. mddev->events--;
  1990. mddev->can_decrease_events = 0;
  1991. } else {
  1992. /* otherwise we have to go forward and ... */
  1993. mddev->events ++;
  1994. mddev->can_decrease_events = nospares;
  1995. }
  1996. if (!mddev->events) {
  1997. /*
  1998. * oops, this 64-bit counter should never wrap.
  1999. * Either we are in around ~1 trillion A.C., assuming
  2000. * 1 reboot per second, or we have a bug:
  2001. */
  2002. MD_BUG();
  2003. mddev->events --;
  2004. }
  2005. sync_sbs(mddev, nospares);
  2006. spin_unlock_irq(&mddev->write_lock);
  2007. dprintk(KERN_INFO
  2008. "md: updating %s RAID superblock on device (in sync %d)\n",
  2009. mdname(mddev),mddev->in_sync);
  2010. bitmap_update_sb(mddev->bitmap);
  2011. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2012. char b[BDEVNAME_SIZE];
  2013. dprintk(KERN_INFO "md: ");
  2014. if (rdev->sb_loaded != 1)
  2015. continue; /* no noise on spare devices */
  2016. if (test_bit(Faulty, &rdev->flags))
  2017. dprintk("(skipping faulty ");
  2018. dprintk("%s ", bdevname(rdev->bdev,b));
  2019. if (!test_bit(Faulty, &rdev->flags)) {
  2020. md_super_write(mddev,rdev,
  2021. rdev->sb_start, rdev->sb_size,
  2022. rdev->sb_page);
  2023. dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
  2024. bdevname(rdev->bdev,b),
  2025. (unsigned long long)rdev->sb_start);
  2026. rdev->sb_events = mddev->events;
  2027. } else
  2028. dprintk(")\n");
  2029. if (mddev->level == LEVEL_MULTIPATH)
  2030. /* only need to write one superblock... */
  2031. break;
  2032. }
  2033. md_super_wait(mddev);
  2034. /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
  2035. spin_lock_irq(&mddev->write_lock);
  2036. if (mddev->in_sync != sync_req ||
  2037. test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
  2038. /* have to write it out again */
  2039. spin_unlock_irq(&mddev->write_lock);
  2040. goto repeat;
  2041. }
  2042. clear_bit(MD_CHANGE_PENDING, &mddev->flags);
  2043. spin_unlock_irq(&mddev->write_lock);
  2044. wake_up(&mddev->sb_wait);
  2045. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  2046. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  2047. }
  2048. /* words written to sysfs files may, or may not, be \n terminated.
  2049. * We want to accept with case. For this we use cmd_match.
  2050. */
  2051. static int cmd_match(const char *cmd, const char *str)
  2052. {
  2053. /* See if cmd, written into a sysfs file, matches
  2054. * str. They must either be the same, or cmd can
  2055. * have a trailing newline
  2056. */
  2057. while (*cmd && *str && *cmd == *str) {
  2058. cmd++;
  2059. str++;
  2060. }
  2061. if (*cmd == '\n')
  2062. cmd++;
  2063. if (*str || *cmd)
  2064. return 0;
  2065. return 1;
  2066. }
  2067. struct rdev_sysfs_entry {
  2068. struct attribute attr;
  2069. ssize_t (*show)(mdk_rdev_t *, char *);
  2070. ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
  2071. };
  2072. static ssize_t
  2073. state_show(mdk_rdev_t *rdev, char *page)
  2074. {
  2075. char *sep = "";
  2076. size_t len = 0;
  2077. if (test_bit(Faulty, &rdev->flags)) {
  2078. len+= sprintf(page+len, "%sfaulty",sep);
  2079. sep = ",";
  2080. }
  2081. if (test_bit(In_sync, &rdev->flags)) {
  2082. len += sprintf(page+len, "%sin_sync",sep);
  2083. sep = ",";
  2084. }
  2085. if (test_bit(WriteMostly, &rdev->flags)) {
  2086. len += sprintf(page+len, "%swrite_mostly",sep);
  2087. sep = ",";
  2088. }
  2089. if (test_bit(Blocked, &rdev->flags)) {
  2090. len += sprintf(page+len, "%sblocked", sep);
  2091. sep = ",";
  2092. }
  2093. if (!test_bit(Faulty, &rdev->flags) &&
  2094. !test_bit(In_sync, &rdev->flags)) {
  2095. len += sprintf(page+len, "%sspare", sep);
  2096. sep = ",";
  2097. }
  2098. return len+sprintf(page+len, "\n");
  2099. }
  2100. static ssize_t
  2101. state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
  2102. {
  2103. /* can write
  2104. * faulty - simulates and error
  2105. * remove - disconnects the device
  2106. * writemostly - sets write_mostly
  2107. * -writemostly - clears write_mostly
  2108. * blocked - sets the Blocked flag
  2109. * -blocked - clears the Blocked flag
  2110. * insync - sets Insync providing device isn't active
  2111. */
  2112. int err = -EINVAL;
  2113. if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
  2114. md_error(rdev->mddev, rdev);
  2115. err = 0;
  2116. } else if (cmd_match(buf, "remove")) {
  2117. if (rdev->raid_disk >= 0)
  2118. err = -EBUSY;
  2119. else {
  2120. mddev_t *mddev = rdev->mddev;
  2121. kick_rdev_from_array(rdev);
  2122. if (mddev->pers)
  2123. md_update_sb(mddev, 1);
  2124. md_new_event(mddev);
  2125. err = 0;
  2126. }
  2127. } else if (cmd_match(buf, "writemostly")) {
  2128. set_bit(WriteMostly, &rdev->flags);
  2129. err = 0;
  2130. } else if (cmd_match(buf, "-writemostly")) {
  2131. clear_bit(WriteMostly, &rdev->flags);
  2132. err = 0;
  2133. } else if (cmd_match(buf, "blocked")) {
  2134. set_bit(Blocked, &rdev->flags);
  2135. err = 0;
  2136. } else if (cmd_match(buf, "-blocked")) {
  2137. clear_bit(Blocked, &rdev->flags);
  2138. wake_up(&rdev->blocked_wait);
  2139. set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
  2140. md_wakeup_thread(rdev->mddev->thread);
  2141. err = 0;
  2142. } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
  2143. set_bit(In_sync, &rdev->flags);
  2144. err = 0;
  2145. }
  2146. if (!err)
  2147. sysfs_notify_dirent_safe(rdev->sysfs_state);
  2148. return err ? err : len;
  2149. }
  2150. static struct rdev_sysfs_entry rdev_state =
  2151. __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
  2152. static ssize_t
  2153. errors_show(mdk_rdev_t *rdev, char *page)
  2154. {
  2155. return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
  2156. }
  2157. static ssize_t
  2158. errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
  2159. {
  2160. char *e;
  2161. unsigned long n = simple_strtoul(buf, &e, 10);
  2162. if (*buf && (*e == 0 || *e == '\n')) {
  2163. atomic_set(&rdev->corrected_errors, n);
  2164. return len;
  2165. }
  2166. return -EINVAL;
  2167. }
  2168. static struct rdev_sysfs_entry rdev_errors =
  2169. __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
  2170. static ssize_t
  2171. slot_show(mdk_rdev_t *rdev, char *page)
  2172. {
  2173. if (rdev->raid_disk < 0)
  2174. return sprintf(page, "none\n");
  2175. else
  2176. return sprintf(page, "%d\n", rdev->raid_disk);
  2177. }
  2178. static ssize_t
  2179. slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
  2180. {
  2181. char *e;
  2182. int err;
  2183. char nm[20];
  2184. int slot = simple_strtoul(buf, &e, 10);
  2185. if (strncmp(buf, "none", 4)==0)
  2186. slot = -1;
  2187. else if (e==buf || (*e && *e!= '\n'))
  2188. return -EINVAL;
  2189. if (rdev->mddev->pers && slot == -1) {
  2190. /* Setting 'slot' on an active array requires also
  2191. * updating the 'rd%d' link, and communicating
  2192. * with the personality with ->hot_*_disk.
  2193. * For now we only support removing
  2194. * failed/spare devices. This normally happens automatically,
  2195. * but not when the metadata is externally managed.
  2196. */
  2197. if (rdev->raid_disk == -1)
  2198. return -EEXIST;
  2199. /* personality does all needed checks */
  2200. if (rdev->mddev->pers->hot_remove_disk == NULL)
  2201. return -EINVAL;
  2202. err = rdev->mddev->pers->
  2203. hot_remove_disk(rdev->mddev, rdev->raid_disk);
  2204. if (err)
  2205. return err;
  2206. sprintf(nm, "rd%d", rdev->raid_disk);
  2207. sysfs_remove_link(&rdev->mddev->kobj, nm);
  2208. rdev->raid_disk = -1;
  2209. set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
  2210. md_wakeup_thread(rdev->mddev->thread);
  2211. } else if (rdev->mddev->pers) {
  2212. mdk_rdev_t *rdev2;
  2213. /* Activating a spare .. or possibly reactivating
  2214. * if we ever get bitmaps working here.
  2215. */
  2216. if (rdev->raid_disk != -1)
  2217. return -EBUSY;
  2218. if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
  2219. return -EBUSY;
  2220. if (rdev->mddev->pers->hot_add_disk == NULL)
  2221. return -EINVAL;
  2222. list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
  2223. if (rdev2->raid_disk == slot)
  2224. return -EEXIST;
  2225. if (slot >= rdev->mddev->raid_disks &&
  2226. slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
  2227. return -ENOSPC;
  2228. rdev->raid_disk = slot;
  2229. if (test_bit(In_sync, &rdev->flags))
  2230. rdev->saved_raid_disk = slot;
  2231. else
  2232. rdev->saved_raid_disk = -1;
  2233. err = rdev->mddev->pers->
  2234. hot_add_disk(rdev->mddev, rdev);
  2235. if (err) {
  2236. rdev->raid_disk = -1;
  2237. return err;
  2238. } else
  2239. sysfs_notify_dirent_safe(rdev->sysfs_state);
  2240. sprintf(nm, "rd%d", rdev->raid_disk);
  2241. if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
  2242. /* failure here is OK */;
  2243. /* don't wakeup anyone, leave that to userspace. */
  2244. } else {
  2245. if (slot >= rdev->mddev->raid_disks &&
  2246. slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
  2247. return -ENOSPC;
  2248. rdev->raid_disk = slot;
  2249. /* assume it is working */
  2250. clear_bit(Faulty, &rdev->flags);
  2251. clear_bit(WriteMostly, &rdev->flags);
  2252. set_bit(In_sync, &rdev->flags);
  2253. sysfs_notify_dirent_safe(rdev->sysfs_state);
  2254. }
  2255. return len;
  2256. }
  2257. static struct rdev_sysfs_entry rdev_slot =
  2258. __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
  2259. static ssize_t
  2260. offset_show(mdk_rdev_t *rdev, char *page)
  2261. {
  2262. return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
  2263. }
  2264. static ssize_t
  2265. offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
  2266. {
  2267. char *e;
  2268. unsigned long long offset = simple_strtoull(buf, &e, 10);
  2269. if (e==buf || (*e && *e != '\n'))
  2270. return -EINVAL;
  2271. if (rdev->mddev->pers && rdev->raid_disk >= 0)
  2272. return -EBUSY;
  2273. if (rdev->sectors && rdev->mddev->external)
  2274. /* Must set offset before size, so overlap checks
  2275. * can be sane */
  2276. return -EBUSY;
  2277. rdev->data_offset = offset;
  2278. return len;
  2279. }
  2280. static struct rdev_sysfs_entry rdev_offset =
  2281. __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
  2282. static ssize_t
  2283. rdev_size_show(mdk_rdev_t *rdev, char *page)
  2284. {
  2285. return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
  2286. }
  2287. static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
  2288. {
  2289. /* check if two start/length pairs overlap */
  2290. if (s1+l1 <= s2)
  2291. return 0;
  2292. if (s2+l2 <= s1)
  2293. return 0;
  2294. return 1;
  2295. }
  2296. static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
  2297. {
  2298. unsigned long long blocks;
  2299. sector_t new;
  2300. if (strict_strtoull(buf, 10, &blocks) < 0)
  2301. return -EINVAL;
  2302. if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
  2303. return -EINVAL; /* sector conversion overflow */
  2304. new = blocks * 2;
  2305. if (new != blocks * 2)
  2306. return -EINVAL; /* unsigned long long to sector_t overflow */
  2307. *sectors = new;
  2308. return 0;
  2309. }
  2310. static ssize_t
  2311. rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
  2312. {
  2313. mddev_t *my_mddev = rdev->mddev;
  2314. sector_t oldsectors = rdev->sectors;
  2315. sector_t sectors;
  2316. if (strict_blocks_to_sectors(buf, &sectors) < 0)
  2317. return -EINVAL;
  2318. if (my_mddev->pers && rdev->raid_disk >= 0) {
  2319. if (my_mddev->persistent) {
  2320. sectors = super_types[my_mddev->major_version].
  2321. rdev_size_change(rdev, sectors);
  2322. if (!sectors)
  2323. return -EBUSY;
  2324. } else if (!sectors)
  2325. sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
  2326. rdev->data_offset;
  2327. }
  2328. if (sectors < my_mddev->dev_sectors)
  2329. return -EINVAL; /* component must fit device */
  2330. rdev->sectors = sectors;
  2331. if (sectors > oldsectors && my_mddev->external) {
  2332. /* need to check that all other rdevs with the same ->bdev
  2333. * do not overlap. We need to unlock the mddev to avoid
  2334. * a deadlock. We have already changed rdev->sectors, and if
  2335. * we have to change it back, we will have the lock again.
  2336. */
  2337. mddev_t *mddev;
  2338. int overlap = 0;
  2339. struct list_head *tmp;
  2340. mddev_unlock(my_mddev);
  2341. for_each_mddev(mddev, tmp) {
  2342. mdk_rdev_t *rdev2;
  2343. mddev_lock(mddev);
  2344. list_for_each_entry(rdev2, &mddev->disks, same_set)
  2345. if (rdev->bdev == rdev2->bdev &&
  2346. rdev != rdev2 &&
  2347. overlaps(rdev->data_offset, rdev->sectors,
  2348. rdev2->data_offset,
  2349. rdev2->sectors)) {
  2350. overlap = 1;
  2351. break;
  2352. }
  2353. mddev_unlock(mddev);
  2354. if (overlap) {
  2355. mddev_put(mddev);
  2356. break;
  2357. }
  2358. }
  2359. mddev_lock(my_mddev);
  2360. if (overlap) {
  2361. /* Someone else could have slipped in a size
  2362. * change here, but doing so is just silly.
  2363. * We put oldsectors back because we *know* it is
  2364. * safe, and trust userspace not to race with
  2365. * itself
  2366. */
  2367. rdev->sectors = oldsectors;
  2368. return -EBUSY;
  2369. }
  2370. }
  2371. return len;
  2372. }
  2373. static struct rdev_sysfs_entry rdev_size =
  2374. __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
  2375. static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
  2376. {
  2377. unsigned long long recovery_start = rdev->recovery_offset;
  2378. if (test_bit(In_sync, &rdev->flags) ||
  2379. recovery_start == MaxSector)
  2380. return sprintf(page, "none\n");
  2381. return sprintf(page, "%llu\n", recovery_start);
  2382. }
  2383. static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
  2384. {
  2385. unsigned long long recovery_start;
  2386. if (cmd_match(buf, "none"))
  2387. recovery_start = MaxSector;
  2388. else if (strict_strtoull(buf, 10, &recovery_start))
  2389. return -EINVAL;
  2390. if (rdev->mddev->pers &&
  2391. rdev->raid_disk >= 0)
  2392. return -EBUSY;
  2393. rdev->recovery_offset = recovery_start;
  2394. if (recovery_start == MaxSector)
  2395. set_bit(In_sync, &rdev->flags);
  2396. else
  2397. clear_bit(In_sync, &rdev->flags);
  2398. return len;
  2399. }
  2400. static struct rdev_sysfs_entry rdev_recovery_start =
  2401. __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
  2402. static struct attribute *rdev_default_attrs[] = {
  2403. &rdev_state.attr,
  2404. &rdev_errors.attr,
  2405. &rdev_slot.attr,
  2406. &rdev_offset.attr,
  2407. &rdev_size.attr,
  2408. &rdev_recovery_start.attr,
  2409. NULL,
  2410. };
  2411. static ssize_t
  2412. rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  2413. {
  2414. struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
  2415. mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
  2416. mddev_t *mddev = rdev->mddev;
  2417. ssize_t rv;
  2418. if (!entry->show)
  2419. return -EIO;
  2420. rv = mddev ? mddev_lock(mddev) : -EBUSY;
  2421. if (!rv) {
  2422. if (rdev->mddev == NULL)
  2423. rv = -EBUSY;
  2424. else
  2425. rv = entry->show(rdev, page);
  2426. mddev_unlock(mddev);
  2427. }
  2428. return rv;
  2429. }
  2430. static ssize_t
  2431. rdev_attr_store(struct kobject *kobj, struct attribute *attr,
  2432. const char *page, size_t length)
  2433. {
  2434. struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
  2435. mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
  2436. ssize_t rv;
  2437. mddev_t *mddev = rdev->mddev;
  2438. if (!entry->store)
  2439. return -EIO;
  2440. if (!capable(CAP_SYS_ADMIN))
  2441. return -EACCES;
  2442. rv = mddev ? mddev_lock(mddev): -EBUSY;
  2443. if (!rv) {
  2444. if (rdev->mddev == NULL)
  2445. rv = -EBUSY;
  2446. else
  2447. rv = entry->store(rdev, page, length);
  2448. mddev_unlock(mddev);
  2449. }
  2450. return rv;
  2451. }
  2452. static void rdev_free(struct kobject *ko)
  2453. {
  2454. mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
  2455. kfree(rdev);
  2456. }
  2457. static const struct sysfs_ops rdev_sysfs_ops = {
  2458. .show = rdev_attr_show,
  2459. .store = rdev_attr_store,
  2460. };
  2461. static struct kobj_type rdev_ktype = {
  2462. .release = rdev_free,
  2463. .sysfs_ops = &rdev_sysfs_ops,
  2464. .default_attrs = rdev_default_attrs,
  2465. };
  2466. void md_rdev_init(mdk_rdev_t *rdev)
  2467. {
  2468. rdev->desc_nr = -1;
  2469. rdev->saved_raid_disk = -1;
  2470. rdev->raid_disk = -1;
  2471. rdev->flags = 0;
  2472. rdev->data_offset = 0;
  2473. rdev->sb_events = 0;
  2474. rdev->last_read_error.tv_sec = 0;
  2475. rdev->last_read_error.tv_nsec = 0;
  2476. atomic_set(&rdev->nr_pending, 0);
  2477. atomic_set(&rdev->read_errors, 0);
  2478. atomic_set(&rdev->corrected_errors, 0);
  2479. INIT_LIST_HEAD(&rdev->same_set);
  2480. init_waitqueue_head(&rdev->blocked_wait);
  2481. }
  2482. EXPORT_SYMBOL_GPL(md_rdev_init);
  2483. /*
  2484. * Import a device. If 'super_format' >= 0, then sanity check the superblock
  2485. *
  2486. * mark the device faulty if:
  2487. *
  2488. * - the device is nonexistent (zero size)
  2489. * - the device has no valid superblock
  2490. *
  2491. * a faulty rdev _never_ has rdev->sb set.
  2492. */
  2493. static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
  2494. {
  2495. char b[BDEVNAME_SIZE];
  2496. int err;
  2497. mdk_rdev_t *rdev;
  2498. sector_t size;
  2499. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  2500. if (!rdev) {
  2501. printk(KERN_ERR "md: could not alloc mem for new device!\n");
  2502. return ERR_PTR(-ENOMEM);
  2503. }
  2504. md_rdev_init(rdev);
  2505. if ((err = alloc_disk_sb(rdev)))
  2506. goto abort_free;
  2507. err = lock_rdev(rdev, newdev, super_format == -2);
  2508. if (err)
  2509. goto abort_free;
  2510. kobject_init(&rdev->kobj, &rdev_ktype);
  2511. size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
  2512. if (!size) {
  2513. printk(KERN_WARNING
  2514. "md: %s has zero or unknown size, marking faulty!\n",
  2515. bdevname(rdev->bdev,b));
  2516. err = -EINVAL;
  2517. goto abort_free;
  2518. }
  2519. if (super_format >= 0) {
  2520. err = super_types[super_format].
  2521. load_super(rdev, NULL, super_minor);
  2522. if (err == -EINVAL) {
  2523. printk(KERN_WARNING
  2524. "md: %s does not have a valid v%d.%d "
  2525. "superblock, not importing!\n",
  2526. bdevname(rdev->bdev,b),
  2527. super_format, super_minor);
  2528. goto abort_free;
  2529. }
  2530. if (err < 0) {
  2531. printk(KERN_WARNING
  2532. "md: could not read %s's sb, not importing!\n",
  2533. bdevname(rdev->bdev,b));
  2534. goto abort_free;
  2535. }
  2536. }
  2537. return rdev;
  2538. abort_free:
  2539. if (rdev->sb_page) {
  2540. if (rdev->bdev)
  2541. unlock_rdev(rdev);
  2542. free_disk_sb(rdev);
  2543. }
  2544. kfree(rdev);
  2545. return ERR_PTR(err);
  2546. }
  2547. /*
  2548. * Check a full RAID array for plausibility
  2549. */
  2550. static void analyze_sbs(mddev_t * mddev)
  2551. {
  2552. int i;
  2553. mdk_rdev_t *rdev, *freshest, *tmp;
  2554. char b[BDEVNAME_SIZE];
  2555. freshest = NULL;
  2556. rdev_for_each(rdev, tmp, mddev)
  2557. switch (super_types[mddev->major_version].
  2558. load_super(rdev, freshest, mddev->minor_version)) {
  2559. case 1:
  2560. freshest = rdev;
  2561. break;
  2562. case 0:
  2563. break;
  2564. default:
  2565. printk( KERN_ERR \
  2566. "md: fatal superblock inconsistency in %s"
  2567. " -- removing from array\n",
  2568. bdevname(rdev->bdev,b));
  2569. kick_rdev_from_array(rdev);
  2570. }
  2571. super_types[mddev->major_version].
  2572. validate_super(mddev, freshest);
  2573. i = 0;
  2574. rdev_for_each(rdev, tmp, mddev) {
  2575. if (mddev->max_disks &&
  2576. (rdev->desc_nr >= mddev->max_disks ||
  2577. i > mddev->max_disks)) {
  2578. printk(KERN_WARNING
  2579. "md: %s: %s: only %d devices permitted\n",
  2580. mdname(mddev), bdevname(rdev->bdev, b),
  2581. mddev->max_disks);
  2582. kick_rdev_from_array(rdev);
  2583. continue;
  2584. }
  2585. if (rdev != freshest)
  2586. if (super_types[mddev->major_version].
  2587. validate_super(mddev, rdev)) {
  2588. printk(KERN_WARNING "md: kicking non-fresh %s"
  2589. " from array!\n",
  2590. bdevname(rdev->bdev,b));
  2591. kick_rdev_from_array(rdev);
  2592. continue;
  2593. }
  2594. if (mddev->level == LEVEL_MULTIPATH) {
  2595. rdev->desc_nr = i++;
  2596. rdev->raid_disk = rdev->desc_nr;
  2597. set_bit(In_sync, &rdev->flags);
  2598. } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
  2599. rdev->raid_disk = -1;
  2600. clear_bit(In_sync, &rdev->flags);
  2601. }
  2602. }
  2603. }
  2604. /* Read a fixed-point number.
  2605. * Numbers in sysfs attributes should be in "standard" units where
  2606. * possible, so time should be in seconds.
  2607. * However we internally use a a much smaller unit such as
  2608. * milliseconds or jiffies.
  2609. * This function takes a decimal number with a possible fractional
  2610. * component, and produces an integer which is the result of
  2611. * multiplying that number by 10^'scale'.
  2612. * all without any floating-point arithmetic.
  2613. */
  2614. int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
  2615. {
  2616. unsigned long result = 0;
  2617. long decimals = -1;
  2618. while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
  2619. if (*cp == '.')
  2620. decimals = 0;
  2621. else if (decimals < scale) {
  2622. unsigned int value;
  2623. value = *cp - '0';
  2624. result = result * 10 + value;
  2625. if (decimals >= 0)
  2626. decimals++;
  2627. }
  2628. cp++;
  2629. }
  2630. if (*cp == '\n')
  2631. cp++;
  2632. if (*cp)
  2633. return -EINVAL;
  2634. if (decimals < 0)
  2635. decimals = 0;
  2636. while (decimals < scale) {
  2637. result *= 10;
  2638. decimals ++;
  2639. }
  2640. *res = result;
  2641. return 0;
  2642. }
  2643. static void md_safemode_timeout(unsigned long data);
  2644. static ssize_t
  2645. safe_delay_show(mddev_t *mddev, char *page)
  2646. {
  2647. int msec = (mddev->safemode_delay*1000)/HZ;
  2648. return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
  2649. }
  2650. static ssize_t
  2651. safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
  2652. {
  2653. unsigned long msec;
  2654. if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
  2655. return -EINVAL;
  2656. if (msec == 0)
  2657. mddev->safemode_delay = 0;
  2658. else {
  2659. unsigned long old_delay = mddev->safemode_delay;
  2660. mddev->safemode_delay = (msec*HZ)/1000;
  2661. if (mddev->safemode_delay == 0)
  2662. mddev->safemode_delay = 1;
  2663. if (mddev->safemode_delay < old_delay)
  2664. md_safemode_timeout((unsigned long)mddev);
  2665. }
  2666. return len;
  2667. }
  2668. static struct md_sysfs_entry md_safe_delay =
  2669. __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
  2670. static ssize_t
  2671. level_show(mddev_t *mddev, char *page)
  2672. {
  2673. struct mdk_personality *p = mddev->pers;
  2674. if (p)
  2675. return sprintf(page, "%s\n", p->name);
  2676. else if (mddev->clevel[0])
  2677. return sprintf(page, "%s\n", mddev->clevel);
  2678. else if (mddev->level != LEVEL_NONE)
  2679. return sprintf(page, "%d\n", mddev->level);
  2680. else
  2681. return 0;
  2682. }
  2683. static ssize_t
  2684. level_store(mddev_t *mddev, const char *buf, size_t len)
  2685. {
  2686. char clevel[16];
  2687. ssize_t rv = len;
  2688. struct mdk_personality *pers;
  2689. long level;
  2690. void *priv;
  2691. mdk_rdev_t *rdev;
  2692. if (mddev->pers == NULL) {
  2693. if (len == 0)
  2694. return 0;
  2695. if (len >= sizeof(mddev->clevel))
  2696. return -ENOSPC;
  2697. strncpy(mddev->clevel, buf, len);
  2698. if (mddev->clevel[len-1] == '\n')
  2699. len--;
  2700. mddev->clevel[len] = 0;
  2701. mddev->level = LEVEL_NONE;
  2702. return rv;
  2703. }
  2704. /* request to change the personality. Need to ensure:
  2705. * - array is not engaged in resync/recovery/reshape
  2706. * - old personality can be suspended
  2707. * - new personality will access other array.
  2708. */
  2709. if (mddev->sync_thread ||
  2710. mddev->reshape_position != MaxSector ||
  2711. mddev->sysfs_active)
  2712. return -EBUSY;
  2713. if (!mddev->pers->quiesce) {
  2714. printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
  2715. mdname(mddev), mddev->pers->name);
  2716. return -EINVAL;
  2717. }
  2718. /* Now find the new personality */
  2719. if (len == 0 || len >= sizeof(clevel))
  2720. return -EINVAL;
  2721. strncpy(clevel, buf, len);
  2722. if (clevel[len-1] == '\n')
  2723. len--;
  2724. clevel[len] = 0;
  2725. if (strict_strtol(clevel, 10, &level))
  2726. level = LEVEL_NONE;
  2727. if (request_module("md-%s", clevel) != 0)
  2728. request_module("md-level-%s", clevel);
  2729. spin_lock(&pers_lock);
  2730. pers = find_pers(level, clevel);
  2731. if (!pers || !try_module_get(pers->owner)) {
  2732. spin_unlock(&pers_lock);
  2733. printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
  2734. return -EINVAL;
  2735. }
  2736. spin_unlock(&pers_lock);
  2737. if (pers == mddev->pers) {
  2738. /* Nothing to do! */
  2739. module_put(pers->owner);
  2740. return rv;
  2741. }
  2742. if (!pers->takeover) {
  2743. module_put(pers->owner);
  2744. printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
  2745. mdname(mddev), clevel);
  2746. return -EINVAL;
  2747. }
  2748. list_for_each_entry(rdev, &mddev->disks, same_set)
  2749. rdev->new_raid_disk = rdev->raid_disk;
  2750. /* ->takeover must set new_* and/or delta_disks
  2751. * if it succeeds, and may set them when it fails.
  2752. */
  2753. priv = pers->takeover(mddev);
  2754. if (IS_ERR(priv)) {
  2755. mddev->new_level = mddev->level;
  2756. mddev->new_layout = mddev->layout;
  2757. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2758. mddev->raid_disks -= mddev->delta_disks;
  2759. mddev->delta_disks = 0;
  2760. module_put(pers->owner);
  2761. printk(KERN_WARNING "md: %s: %s would not accept array\n",
  2762. mdname(mddev), clevel);
  2763. return PTR_ERR(priv);
  2764. }
  2765. /* Looks like we have a winner */
  2766. mddev_suspend(mddev);
  2767. mddev->pers->stop(mddev);
  2768. if (mddev->pers->sync_request == NULL &&
  2769. pers->sync_request != NULL) {
  2770. /* need to add the md_redundancy_group */
  2771. if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
  2772. printk(KERN_WARNING
  2773. "md: cannot register extra attributes for %s\n",
  2774. mdname(mddev));
  2775. mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
  2776. }
  2777. if (mddev->pers->sync_request != NULL &&
  2778. pers->sync_request == NULL) {
  2779. /* need to remove the md_redundancy_group */
  2780. if (mddev->to_remove == NULL)
  2781. mddev->to_remove = &md_redundancy_group;
  2782. }
  2783. if (mddev->pers->sync_request == NULL &&
  2784. mddev->external) {
  2785. /* We are converting from a no-redundancy array
  2786. * to a redundancy array and metadata is managed
  2787. * externally so we need to be sure that writes
  2788. * won't block due to a need to transition
  2789. * clean->dirty
  2790. * until external management is started.
  2791. */
  2792. mddev->in_sync = 0;
  2793. mddev->safemode_delay = 0;
  2794. mddev->safemode = 0;
  2795. }
  2796. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2797. char nm[20];
  2798. if (rdev->raid_disk < 0)
  2799. continue;
  2800. if (rdev->new_raid_disk >= mddev->raid_disks)
  2801. rdev->new_raid_disk = -1;
  2802. if (rdev->new_raid_disk == rdev->raid_disk)
  2803. continue;
  2804. sprintf(nm, "rd%d", rdev->raid_disk);
  2805. sysfs_remove_link(&mddev->kobj, nm);
  2806. }
  2807. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2808. if (rdev->raid_disk < 0)
  2809. continue;
  2810. if (rdev->new_raid_disk == rdev->raid_disk)
  2811. continue;
  2812. rdev->raid_disk = rdev->new_raid_disk;
  2813. if (rdev->raid_disk < 0)
  2814. clear_bit(In_sync, &rdev->flags);
  2815. else {
  2816. char nm[20];
  2817. sprintf(nm, "rd%d", rdev->raid_disk);
  2818. if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
  2819. printk("md: cannot register %s for %s after level change\n",
  2820. nm, mdname(mddev));
  2821. }
  2822. }
  2823. module_put(mddev->pers->owner);
  2824. mddev->pers = pers;
  2825. mddev->private = priv;
  2826. strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
  2827. mddev->level = mddev->new_level;
  2828. mddev->layout = mddev->new_layout;
  2829. mddev->chunk_sectors = mddev->new_chunk_sectors;
  2830. mddev->delta_disks = 0;
  2831. mddev->degraded = 0;
  2832. if (mddev->pers->sync_request == NULL) {
  2833. /* this is now an array without redundancy, so
  2834. * it must always be in_sync
  2835. */
  2836. mddev->in_sync = 1;
  2837. del_timer_sync(&mddev->safemode_timer);
  2838. }
  2839. pers->run(mddev);
  2840. mddev_resume(mddev);
  2841. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  2842. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2843. md_wakeup_thread(mddev->thread);
  2844. sysfs_notify(&mddev->kobj, NULL, "level");
  2845. md_new_event(mddev);
  2846. return rv;
  2847. }
  2848. static struct md_sysfs_entry md_level =
  2849. __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
  2850. static ssize_t
  2851. layout_show(mddev_t *mddev, char *page)
  2852. {
  2853. /* just a number, not meaningful for all levels */
  2854. if (mddev->reshape_position != MaxSector &&
  2855. mddev->layout != mddev->new_layout)
  2856. return sprintf(page, "%d (%d)\n",
  2857. mddev->new_layout, mddev->layout);
  2858. return sprintf(page, "%d\n", mddev->layout);
  2859. }
  2860. static ssize_t
  2861. layout_store(mddev_t *mddev, const char *buf, size_t len)
  2862. {
  2863. char *e;
  2864. unsigned long n = simple_strtoul(buf, &e, 10);
  2865. if (!*buf || (*e && *e != '\n'))
  2866. return -EINVAL;
  2867. if (mddev->pers) {
  2868. int err;
  2869. if (mddev->pers->check_reshape == NULL)
  2870. return -EBUSY;
  2871. mddev->new_layout = n;
  2872. err = mddev->pers->check_reshape(mddev);
  2873. if (err) {
  2874. mddev->new_layout = mddev->layout;
  2875. return err;
  2876. }
  2877. } else {
  2878. mddev->new_layout = n;
  2879. if (mddev->reshape_position == MaxSector)
  2880. mddev->layout = n;
  2881. }
  2882. return len;
  2883. }
  2884. static struct md_sysfs_entry md_layout =
  2885. __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
  2886. static ssize_t
  2887. raid_disks_show(mddev_t *mddev, char *page)
  2888. {
  2889. if (mddev->raid_disks == 0)
  2890. return 0;
  2891. if (mddev->reshape_position != MaxSector &&
  2892. mddev->delta_disks != 0)
  2893. return sprintf(page, "%d (%d)\n", mddev->raid_disks,
  2894. mddev->raid_disks - mddev->delta_disks);
  2895. return sprintf(page, "%d\n", mddev->raid_disks);
  2896. }
  2897. static int update_raid_disks(mddev_t *mddev, int raid_disks);
  2898. static ssize_t
  2899. raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
  2900. {
  2901. char *e;
  2902. int rv = 0;
  2903. unsigned long n = simple_strtoul(buf, &e, 10);
  2904. if (!*buf || (*e && *e != '\n'))
  2905. return -EINVAL;
  2906. if (mddev->pers)
  2907. rv = update_raid_disks(mddev, n);
  2908. else if (mddev->reshape_position != MaxSector) {
  2909. int olddisks = mddev->raid_disks - mddev->delta_disks;
  2910. mddev->delta_disks = n - olddisks;
  2911. mddev->raid_disks = n;
  2912. } else
  2913. mddev->raid_disks = n;
  2914. return rv ? rv : len;
  2915. }
  2916. static struct md_sysfs_entry md_raid_disks =
  2917. __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
  2918. static ssize_t
  2919. chunk_size_show(mddev_t *mddev, char *page)
  2920. {
  2921. if (mddev->reshape_position != MaxSector &&
  2922. mddev->chunk_sectors != mddev->new_chunk_sectors)
  2923. return sprintf(page, "%d (%d)\n",
  2924. mddev->new_chunk_sectors << 9,
  2925. mddev->chunk_sectors << 9);
  2926. return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
  2927. }
  2928. static ssize_t
  2929. chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
  2930. {
  2931. char *e;
  2932. unsigned long n = simple_strtoul(buf, &e, 10);
  2933. if (!*buf || (*e && *e != '\n'))
  2934. return -EINVAL;
  2935. if (mddev->pers) {
  2936. int err;
  2937. if (mddev->pers->check_reshape == NULL)
  2938. return -EBUSY;
  2939. mddev->new_chunk_sectors = n >> 9;
  2940. err = mddev->pers->check_reshape(mddev);
  2941. if (err) {
  2942. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2943. return err;
  2944. }
  2945. } else {
  2946. mddev->new_chunk_sectors = n >> 9;
  2947. if (mddev->reshape_position == MaxSector)
  2948. mddev->chunk_sectors = n >> 9;
  2949. }
  2950. return len;
  2951. }
  2952. static struct md_sysfs_entry md_chunk_size =
  2953. __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
  2954. static ssize_t
  2955. resync_start_show(mddev_t *mddev, char *page)
  2956. {
  2957. if (mddev->recovery_cp == MaxSector)
  2958. return sprintf(page, "none\n");
  2959. return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
  2960. }
  2961. static ssize_t
  2962. resync_start_store(mddev_t *mddev, const char *buf, size_t len)
  2963. {
  2964. char *e;
  2965. unsigned long long n = simple_strtoull(buf, &e, 10);
  2966. if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
  2967. return -EBUSY;
  2968. if (cmd_match(buf, "none"))
  2969. n = MaxSector;
  2970. else if (!*buf || (*e && *e != '\n'))
  2971. return -EINVAL;
  2972. mddev->recovery_cp = n;
  2973. return len;
  2974. }
  2975. static struct md_sysfs_entry md_resync_start =
  2976. __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
  2977. /*
  2978. * The array state can be:
  2979. *
  2980. * clear
  2981. * No devices, no size, no level
  2982. * Equivalent to STOP_ARRAY ioctl
  2983. * inactive
  2984. * May have some settings, but array is not active
  2985. * all IO results in error
  2986. * When written, doesn't tear down array, but just stops it
  2987. * suspended (not supported yet)
  2988. * All IO requests will block. The array can be reconfigured.
  2989. * Writing this, if accepted, will block until array is quiescent
  2990. * readonly
  2991. * no resync can happen. no superblocks get written.
  2992. * write requests fail
  2993. * read-auto
  2994. * like readonly, but behaves like 'clean' on a write request.
  2995. *
  2996. * clean - no pending writes, but otherwise active.
  2997. * When written to inactive array, starts without resync
  2998. * If a write request arrives then
  2999. * if metadata is known, mark 'dirty' and switch to 'active'.
  3000. * if not known, block and switch to write-pending
  3001. * If written to an active array that has pending writes, then fails.
  3002. * active
  3003. * fully active: IO and resync can be happening.
  3004. * When written to inactive array, starts with resync
  3005. *
  3006. * write-pending
  3007. * clean, but writes are blocked waiting for 'active' to be written.
  3008. *
  3009. * active-idle
  3010. * like active, but no writes have been seen for a while (100msec).
  3011. *
  3012. */
  3013. enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
  3014. write_pending, active_idle, bad_word};
  3015. static char *array_states[] = {
  3016. "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
  3017. "write-pending", "active-idle", NULL };
  3018. static int match_word(const char *word, char **list)
  3019. {
  3020. int n;
  3021. for (n=0; list[n]; n++)
  3022. if (cmd_match(word, list[n]))
  3023. break;
  3024. return n;
  3025. }
  3026. static ssize_t
  3027. array_state_show(mddev_t *mddev, char *page)
  3028. {
  3029. enum array_state st = inactive;
  3030. if (mddev->pers)
  3031. switch(mddev->ro) {
  3032. case 1:
  3033. st = readonly;
  3034. break;
  3035. case 2:
  3036. st = read_auto;
  3037. break;
  3038. case 0:
  3039. if (mddev->in_sync)
  3040. st = clean;
  3041. else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
  3042. st = write_pending;
  3043. else if (mddev->safemode)
  3044. st = active_idle;
  3045. else
  3046. st = active;
  3047. }
  3048. else {
  3049. if (list_empty(&mddev->disks) &&
  3050. mddev->raid_disks == 0 &&
  3051. mddev->dev_sectors == 0)
  3052. st = clear;
  3053. else
  3054. st = inactive;
  3055. }
  3056. return sprintf(page, "%s\n", array_states[st]);
  3057. }
  3058. static int do_md_stop(mddev_t * mddev, int ro, int is_open);
  3059. static int md_set_readonly(mddev_t * mddev, int is_open);
  3060. static int do_md_run(mddev_t * mddev);
  3061. static int restart_array(mddev_t *mddev);
  3062. static ssize_t
  3063. array_state_store(mddev_t *mddev, const char *buf, size_t len)
  3064. {
  3065. int err = -EINVAL;
  3066. enum array_state st = match_word(buf, array_states);
  3067. switch(st) {
  3068. case bad_word:
  3069. break;
  3070. case clear:
  3071. /* stopping an active array */
  3072. if (atomic_read(&mddev->openers) > 0)
  3073. return -EBUSY;
  3074. err = do_md_stop(mddev, 0, 0);
  3075. break;
  3076. case inactive:
  3077. /* stopping an active array */
  3078. if (mddev->pers) {
  3079. if (atomic_read(&mddev->openers) > 0)
  3080. return -EBUSY;
  3081. err = do_md_stop(mddev, 2, 0);
  3082. } else
  3083. err = 0; /* already inactive */
  3084. break;
  3085. case suspended:
  3086. break; /* not supported yet */
  3087. case readonly:
  3088. if (mddev->pers)
  3089. err = md_set_readonly(mddev, 0);
  3090. else {
  3091. mddev->ro = 1;
  3092. set_disk_ro(mddev->gendisk, 1);
  3093. err = do_md_run(mddev);
  3094. }
  3095. break;
  3096. case read_auto:
  3097. if (mddev->pers) {
  3098. if (mddev->ro == 0)
  3099. err = md_set_readonly(mddev, 0);
  3100. else if (mddev->ro == 1)
  3101. err = restart_array(mddev);
  3102. if (err == 0) {
  3103. mddev->ro = 2;
  3104. set_disk_ro(mddev->gendisk, 0);
  3105. }
  3106. } else {
  3107. mddev->ro = 2;
  3108. err = do_md_run(mddev);
  3109. }
  3110. break;
  3111. case clean:
  3112. if (mddev->pers) {
  3113. restart_array(mddev);
  3114. spin_lock_irq(&mddev->write_lock);
  3115. if (atomic_read(&mddev->writes_pending) == 0) {
  3116. if (mddev->in_sync == 0) {
  3117. mddev->in_sync = 1;
  3118. if (mddev->safemode == 1)
  3119. mddev->safemode = 0;
  3120. set_bit(MD_CHANGE_CLEAN, &mddev->flags);
  3121. }
  3122. err = 0;
  3123. } else
  3124. err = -EBUSY;
  3125. spin_unlock_irq(&mddev->write_lock);
  3126. } else
  3127. err = -EINVAL;
  3128. break;
  3129. case active:
  3130. if (mddev->pers) {
  3131. restart_array(mddev);
  3132. clear_bit(MD_CHANGE_PENDING, &mddev->flags);
  3133. wake_up(&mddev->sb_wait);
  3134. err = 0;
  3135. } else {
  3136. mddev->ro = 0;
  3137. set_disk_ro(mddev->gendisk, 0);
  3138. err = do_md_run(mddev);
  3139. }
  3140. break;
  3141. case write_pending:
  3142. case active_idle:
  3143. /* these cannot be set */
  3144. break;
  3145. }
  3146. if (err)
  3147. return err;
  3148. else {
  3149. sysfs_notify_dirent_safe(mddev->sysfs_state);
  3150. return len;
  3151. }
  3152. }
  3153. static struct md_sysfs_entry md_array_state =
  3154. __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
  3155. static ssize_t
  3156. max_corrected_read_errors_show(mddev_t *mddev, char *page) {
  3157. return sprintf(page, "%d\n",
  3158. atomic_read(&mddev->max_corr_read_errors));
  3159. }
  3160. static ssize_t
  3161. max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
  3162. {
  3163. char *e;
  3164. unsigned long n = simple_strtoul(buf, &e, 10);
  3165. if (*buf && (*e == 0 || *e == '\n')) {
  3166. atomic_set(&mddev->max_corr_read_errors, n);
  3167. return len;
  3168. }
  3169. return -EINVAL;
  3170. }
  3171. static struct md_sysfs_entry max_corr_read_errors =
  3172. __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
  3173. max_corrected_read_errors_store);
  3174. static ssize_t
  3175. null_show(mddev_t *mddev, char *page)
  3176. {
  3177. return -EINVAL;
  3178. }
  3179. static ssize_t
  3180. new_dev_store(mddev_t *mddev, const char *buf, size_t len)
  3181. {
  3182. /* buf must be %d:%d\n? giving major and minor numbers */
  3183. /* The new device is added to the array.
  3184. * If the array has a persistent superblock, we read the
  3185. * superblock to initialise info and check validity.
  3186. * Otherwise, only checking done is that in bind_rdev_to_array,
  3187. * which mainly checks size.
  3188. */
  3189. char *e;
  3190. int major = simple_strtoul(buf, &e, 10);
  3191. int minor;
  3192. dev_t dev;
  3193. mdk_rdev_t *rdev;
  3194. int err;
  3195. if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
  3196. return -EINVAL;
  3197. minor = simple_strtoul(e+1, &e, 10);
  3198. if (*e && *e != '\n')
  3199. return -EINVAL;
  3200. dev = MKDEV(major, minor);
  3201. if (major != MAJOR(dev) ||
  3202. minor != MINOR(dev))
  3203. return -EOVERFLOW;
  3204. if (mddev->persistent) {
  3205. rdev = md_import_device(dev, mddev->major_version,
  3206. mddev->minor_version);
  3207. if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
  3208. mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
  3209. mdk_rdev_t, same_set);
  3210. err = super_types[mddev->major_version]
  3211. .load_super(rdev, rdev0, mddev->minor_version);
  3212. if (err < 0)
  3213. goto out;
  3214. }
  3215. } else if (mddev->external)
  3216. rdev = md_import_device(dev, -2, -1);
  3217. else
  3218. rdev = md_import_device(dev, -1, -1);
  3219. if (IS_ERR(rdev))
  3220. return PTR_ERR(rdev);
  3221. err = bind_rdev_to_array(rdev, mddev);
  3222. out:
  3223. if (err)
  3224. export_rdev(rdev);
  3225. return err ? err : len;
  3226. }
  3227. static struct md_sysfs_entry md_new_device =
  3228. __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
  3229. static ssize_t
  3230. bitmap_store(mddev_t *mddev, const char *buf, size_t len)
  3231. {
  3232. char *end;
  3233. unsigned long chunk, end_chunk;
  3234. if (!mddev->bitmap)
  3235. goto out;
  3236. /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
  3237. while (*buf) {
  3238. chunk = end_chunk = simple_strtoul(buf, &end, 0);
  3239. if (buf == end) break;
  3240. if (*end == '-') { /* range */
  3241. buf = end + 1;
  3242. end_chunk = simple_strtoul(buf, &end, 0);
  3243. if (buf == end) break;
  3244. }
  3245. if (*end && !isspace(*end)) break;
  3246. bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
  3247. buf = skip_spaces(end);
  3248. }
  3249. bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
  3250. out:
  3251. return len;
  3252. }
  3253. static struct md_sysfs_entry md_bitmap =
  3254. __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
  3255. static ssize_t
  3256. size_show(mddev_t *mddev, char *page)
  3257. {
  3258. return sprintf(page, "%llu\n",
  3259. (unsigned long long)mddev->dev_sectors / 2);
  3260. }
  3261. static int update_size(mddev_t *mddev, sector_t num_sectors);
  3262. static ssize_t
  3263. size_store(mddev_t *mddev, const char *buf, size_t len)
  3264. {
  3265. /* If array is inactive, we can reduce the component size, but
  3266. * not increase it (except from 0).
  3267. * If array is active, we can try an on-line resize
  3268. */
  3269. sector_t sectors;
  3270. int err = strict_blocks_to_sectors(buf, &sectors);
  3271. if (err < 0)
  3272. return err;
  3273. if (mddev->pers) {
  3274. err = update_size(mddev, sectors);
  3275. md_update_sb(mddev, 1);
  3276. } else {
  3277. if (mddev->dev_sectors == 0 ||
  3278. mddev->dev_sectors > sectors)
  3279. mddev->dev_sectors = sectors;
  3280. else
  3281. err = -ENOSPC;
  3282. }
  3283. return err ? err : len;
  3284. }
  3285. static struct md_sysfs_entry md_size =
  3286. __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
  3287. /* Metdata version.
  3288. * This is one of
  3289. * 'none' for arrays with no metadata (good luck...)
  3290. * 'external' for arrays with externally managed metadata,
  3291. * or N.M for internally known formats
  3292. */
  3293. static ssize_t
  3294. metadata_show(mddev_t *mddev, char *page)
  3295. {
  3296. if (mddev->persistent)
  3297. return sprintf(page, "%d.%d\n",
  3298. mddev->major_version, mddev->minor_version);
  3299. else if (mddev->external)
  3300. return sprintf(page, "external:%s\n", mddev->metadata_type);
  3301. else
  3302. return sprintf(page, "none\n");
  3303. }
  3304. static ssize_t
  3305. metadata_store(mddev_t *mddev, const char *buf, size_t len)
  3306. {
  3307. int major, minor;
  3308. char *e;
  3309. /* Changing the details of 'external' metadata is
  3310. * always permitted. Otherwise there must be
  3311. * no devices attached to the array.
  3312. */
  3313. if (mddev->external && strncmp(buf, "external:", 9) == 0)
  3314. ;
  3315. else if (!list_empty(&mddev->disks))
  3316. return -EBUSY;
  3317. if (cmd_match(buf, "none")) {
  3318. mddev->persistent = 0;
  3319. mddev->external = 0;
  3320. mddev->major_version = 0;
  3321. mddev->minor_version = 90;
  3322. return len;
  3323. }
  3324. if (strncmp(buf, "external:", 9) == 0) {
  3325. size_t namelen = len-9;
  3326. if (namelen >= sizeof(mddev->metadata_type))
  3327. namelen = sizeof(mddev->metadata_type)-1;
  3328. strncpy(mddev->metadata_type, buf+9, namelen);
  3329. mddev->metadata_type[namelen] = 0;
  3330. if (namelen && mddev->metadata_type[namelen-1] == '\n')
  3331. mddev->metadata_type[--namelen] = 0;
  3332. mddev->persistent = 0;
  3333. mddev->external = 1;
  3334. mddev->major_version = 0;
  3335. mddev->minor_version = 90;
  3336. return len;
  3337. }
  3338. major = simple_strtoul(buf, &e, 10);
  3339. if (e==buf || *e != '.')
  3340. return -EINVAL;
  3341. buf = e+1;
  3342. minor = simple_strtoul(buf, &e, 10);
  3343. if (e==buf || (*e && *e != '\n') )
  3344. return -EINVAL;
  3345. if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
  3346. return -ENOENT;
  3347. mddev->major_version = major;
  3348. mddev->minor_version = minor;
  3349. mddev->persistent = 1;
  3350. mddev->external = 0;
  3351. return len;
  3352. }
  3353. static struct md_sysfs_entry md_metadata =
  3354. __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  3355. static ssize_t
  3356. action_show(mddev_t *mddev, char *page)
  3357. {
  3358. char *type = "idle";
  3359. if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
  3360. type = "frozen";
  3361. else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
  3362. (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
  3363. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3364. type = "reshape";
  3365. else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3366. if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  3367. type = "resync";
  3368. else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  3369. type = "check";
  3370. else
  3371. type = "repair";
  3372. } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
  3373. type = "recover";
  3374. }
  3375. return sprintf(page, "%s\n", type);
  3376. }
  3377. static void reap_sync_thread(mddev_t *mddev);
  3378. static ssize_t
  3379. action_store(mddev_t *mddev, const char *page, size_t len)
  3380. {
  3381. if (!mddev->pers || !mddev->pers->sync_request)
  3382. return -EINVAL;
  3383. if (cmd_match(page, "frozen"))
  3384. set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  3385. else
  3386. clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  3387. if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
  3388. if (mddev->sync_thread) {
  3389. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  3390. reap_sync_thread(mddev);
  3391. }
  3392. } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
  3393. test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
  3394. return -EBUSY;
  3395. else if (cmd_match(page, "resync"))
  3396. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3397. else if (cmd_match(page, "recover")) {
  3398. set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  3399. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3400. } else if (cmd_match(page, "reshape")) {
  3401. int err;
  3402. if (mddev->pers->start_reshape == NULL)
  3403. return -EINVAL;
  3404. err = mddev->pers->start_reshape(mddev);
  3405. if (err)
  3406. return err;
  3407. sysfs_notify(&mddev->kobj, NULL, "degraded");
  3408. } else {
  3409. if (cmd_match(page, "check"))
  3410. set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3411. else if (!cmd_match(page, "repair"))
  3412. return -EINVAL;
  3413. set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
  3414. set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3415. }
  3416. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3417. md_wakeup_thread(mddev->thread);
  3418. sysfs_notify_dirent_safe(mddev->sysfs_action);
  3419. return len;
  3420. }
  3421. static ssize_t
  3422. mismatch_cnt_show(mddev_t *mddev, char *page)
  3423. {
  3424. return sprintf(page, "%llu\n",
  3425. (unsigned long long) mddev->resync_mismatches);
  3426. }
  3427. static struct md_sysfs_entry md_scan_mode =
  3428. __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
  3429. static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
  3430. static ssize_t
  3431. sync_min_show(mddev_t *mddev, char *page)
  3432. {
  3433. return sprintf(page, "%d (%s)\n", speed_min(mddev),
  3434. mddev->sync_speed_min ? "local": "system");
  3435. }
  3436. static ssize_t
  3437. sync_min_store(mddev_t *mddev, const char *buf, size_t len)
  3438. {
  3439. int min;
  3440. char *e;
  3441. if (strncmp(buf, "system", 6)==0) {
  3442. mddev->sync_speed_min = 0;
  3443. return len;
  3444. }
  3445. min = simple_strtoul(buf, &e, 10);
  3446. if (buf == e || (*e && *e != '\n') || min <= 0)
  3447. return -EINVAL;
  3448. mddev->sync_speed_min = min;
  3449. return len;
  3450. }
  3451. static struct md_sysfs_entry md_sync_min =
  3452. __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
  3453. static ssize_t
  3454. sync_max_show(mddev_t *mddev, char *page)
  3455. {
  3456. return sprintf(page, "%d (%s)\n", speed_max(mddev),
  3457. mddev->sync_speed_max ? "local": "system");
  3458. }
  3459. static ssize_t
  3460. sync_max_store(mddev_t *mddev, const char *buf, size_t len)
  3461. {
  3462. int max;
  3463. char *e;
  3464. if (strncmp(buf, "system", 6)==0) {
  3465. mddev->sync_speed_max = 0;
  3466. return len;
  3467. }
  3468. max = simple_strtoul(buf, &e, 10);
  3469. if (buf == e || (*e && *e != '\n') || max <= 0)
  3470. return -EINVAL;
  3471. mddev->sync_speed_max = max;
  3472. return len;
  3473. }
  3474. static struct md_sysfs_entry md_sync_max =
  3475. __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
  3476. static ssize_t
  3477. degraded_show(mddev_t *mddev, char *page)
  3478. {
  3479. return sprintf(page, "%d\n", mddev->degraded);
  3480. }
  3481. static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
  3482. static ssize_t
  3483. sync_force_parallel_show(mddev_t *mddev, char *page)
  3484. {
  3485. return sprintf(page, "%d\n", mddev->parallel_resync);
  3486. }
  3487. static ssize_t
  3488. sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
  3489. {
  3490. long n;
  3491. if (strict_strtol(buf, 10, &n))
  3492. return -EINVAL;
  3493. if (n != 0 && n != 1)
  3494. return -EINVAL;
  3495. mddev->parallel_resync = n;
  3496. if (mddev->sync_thread)
  3497. wake_up(&resync_wait);
  3498. return len;
  3499. }
  3500. /* force parallel resync, even with shared block devices */
  3501. static struct md_sysfs_entry md_sync_force_parallel =
  3502. __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
  3503. sync_force_parallel_show, sync_force_parallel_store);
  3504. static ssize_t
  3505. sync_speed_show(mddev_t *mddev, char *page)
  3506. {
  3507. unsigned long resync, dt, db;
  3508. if (mddev->curr_resync == 0)
  3509. return sprintf(page, "none\n");
  3510. resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
  3511. dt = (jiffies - mddev->resync_mark) / HZ;
  3512. if (!dt) dt++;
  3513. db = resync - mddev->resync_mark_cnt;
  3514. return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
  3515. }
  3516. static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
  3517. static ssize_t
  3518. sync_completed_show(mddev_t *mddev, char *page)
  3519. {
  3520. unsigned long long max_sectors, resync;
  3521. if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3522. return sprintf(page, "none\n");
  3523. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  3524. max_sectors = mddev->resync_max_sectors;
  3525. else
  3526. max_sectors = mddev->dev_sectors;
  3527. resync = mddev->curr_resync_completed;
  3528. return sprintf(page, "%llu / %llu\n", resync, max_sectors);
  3529. }
  3530. static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
  3531. static ssize_t
  3532. min_sync_show(mddev_t *mddev, char *page)
  3533. {
  3534. return sprintf(page, "%llu\n",
  3535. (unsigned long long)mddev->resync_min);
  3536. }
  3537. static ssize_t
  3538. min_sync_store(mddev_t *mddev, const char *buf, size_t len)
  3539. {
  3540. unsigned long long min;
  3541. if (strict_strtoull(buf, 10, &min))
  3542. return -EINVAL;
  3543. if (min > mddev->resync_max)
  3544. return -EINVAL;
  3545. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3546. return -EBUSY;
  3547. /* Must be a multiple of chunk_size */
  3548. if (mddev->chunk_sectors) {
  3549. sector_t temp = min;
  3550. if (sector_div(temp, mddev->chunk_sectors))
  3551. return -EINVAL;
  3552. }
  3553. mddev->resync_min = min;
  3554. return len;
  3555. }
  3556. static struct md_sysfs_entry md_min_sync =
  3557. __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
  3558. static ssize_t
  3559. max_sync_show(mddev_t *mddev, char *page)
  3560. {
  3561. if (mddev->resync_max == MaxSector)
  3562. return sprintf(page, "max\n");
  3563. else
  3564. return sprintf(page, "%llu\n",
  3565. (unsigned long long)mddev->resync_max);
  3566. }
  3567. static ssize_t
  3568. max_sync_store(mddev_t *mddev, const char *buf, size_t len)
  3569. {
  3570. if (strncmp(buf, "max", 3) == 0)
  3571. mddev->resync_max = MaxSector;
  3572. else {
  3573. unsigned long long max;
  3574. if (strict_strtoull(buf, 10, &max))
  3575. return -EINVAL;
  3576. if (max < mddev->resync_min)
  3577. return -EINVAL;
  3578. if (max < mddev->resync_max &&
  3579. mddev->ro == 0 &&
  3580. test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3581. return -EBUSY;
  3582. /* Must be a multiple of chunk_size */
  3583. if (mddev->chunk_sectors) {
  3584. sector_t temp = max;
  3585. if (sector_div(temp, mddev->chunk_sectors))
  3586. return -EINVAL;
  3587. }
  3588. mddev->resync_max = max;
  3589. }
  3590. wake_up(&mddev->recovery_wait);
  3591. return len;
  3592. }
  3593. static struct md_sysfs_entry md_max_sync =
  3594. __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
  3595. static ssize_t
  3596. suspend_lo_show(mddev_t *mddev, char *page)
  3597. {
  3598. return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
  3599. }
  3600. static ssize_t
  3601. suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
  3602. {
  3603. char *e;
  3604. unsigned long long new = simple_strtoull(buf, &e, 10);
  3605. unsigned long long old = mddev->suspend_lo;
  3606. if (mddev->pers == NULL ||
  3607. mddev->pers->quiesce == NULL)
  3608. return -EINVAL;
  3609. if (buf == e || (*e && *e != '\n'))
  3610. return -EINVAL;
  3611. mddev->suspend_lo = new;
  3612. if (new >= old)
  3613. /* Shrinking suspended region */
  3614. mddev->pers->quiesce(mddev, 2);
  3615. else {
  3616. /* Expanding suspended region - need to wait */
  3617. mddev->pers->quiesce(mddev, 1);
  3618. mddev->pers->quiesce(mddev, 0);
  3619. }
  3620. return len;
  3621. }
  3622. static struct md_sysfs_entry md_suspend_lo =
  3623. __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
  3624. static ssize_t
  3625. suspend_hi_show(mddev_t *mddev, char *page)
  3626. {
  3627. return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
  3628. }
  3629. static ssize_t
  3630. suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
  3631. {
  3632. char *e;
  3633. unsigned long long new = simple_strtoull(buf, &e, 10);
  3634. unsigned long long old = mddev->suspend_hi;
  3635. if (mddev->pers == NULL ||
  3636. mddev->pers->quiesce == NULL)
  3637. return -EINVAL;
  3638. if (buf == e || (*e && *e != '\n'))
  3639. return -EINVAL;
  3640. mddev->suspend_hi = new;
  3641. if (new <= old)
  3642. /* Shrinking suspended region */
  3643. mddev->pers->quiesce(mddev, 2);
  3644. else {
  3645. /* Expanding suspended region - need to wait */
  3646. mddev->pers->quiesce(mddev, 1);
  3647. mddev->pers->quiesce(mddev, 0);
  3648. }
  3649. return len;
  3650. }
  3651. static struct md_sysfs_entry md_suspend_hi =
  3652. __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
  3653. static ssize_t
  3654. reshape_position_show(mddev_t *mddev, char *page)
  3655. {
  3656. if (mddev->reshape_position != MaxSector)
  3657. return sprintf(page, "%llu\n",
  3658. (unsigned long long)mddev->reshape_position);
  3659. strcpy(page, "none\n");
  3660. return 5;
  3661. }
  3662. static ssize_t
  3663. reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
  3664. {
  3665. char *e;
  3666. unsigned long long new = simple_strtoull(buf, &e, 10);
  3667. if (mddev->pers)
  3668. return -EBUSY;
  3669. if (buf == e || (*e && *e != '\n'))
  3670. return -EINVAL;
  3671. mddev->reshape_position = new;
  3672. mddev->delta_disks = 0;
  3673. mddev->new_level = mddev->level;
  3674. mddev->new_layout = mddev->layout;
  3675. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3676. return len;
  3677. }
  3678. static struct md_sysfs_entry md_reshape_position =
  3679. __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
  3680. reshape_position_store);
  3681. static ssize_t
  3682. array_size_show(mddev_t *mddev, char *page)
  3683. {
  3684. if (mddev->external_size)
  3685. return sprintf(page, "%llu\n",
  3686. (unsigned long long)mddev->array_sectors/2);
  3687. else
  3688. return sprintf(page, "default\n");
  3689. }
  3690. static ssize_t
  3691. array_size_store(mddev_t *mddev, const char *buf, size_t len)
  3692. {
  3693. sector_t sectors;
  3694. if (strncmp(buf, "default", 7) == 0) {
  3695. if (mddev->pers)
  3696. sectors = mddev->pers->size(mddev, 0, 0);
  3697. else
  3698. sectors = mddev->array_sectors;
  3699. mddev->external_size = 0;
  3700. } else {
  3701. if (strict_blocks_to_sectors(buf, &sectors) < 0)
  3702. return -EINVAL;
  3703. if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
  3704. return -E2BIG;
  3705. mddev->external_size = 1;
  3706. }
  3707. mddev->array_sectors = sectors;
  3708. if (mddev->pers) {
  3709. set_capacity(mddev->gendisk, mddev->array_sectors);
  3710. revalidate_disk(mddev->gendisk);
  3711. }
  3712. return len;
  3713. }
  3714. static struct md_sysfs_entry md_array_size =
  3715. __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
  3716. array_size_store);
  3717. static struct attribute *md_default_attrs[] = {
  3718. &md_level.attr,
  3719. &md_layout.attr,
  3720. &md_raid_disks.attr,
  3721. &md_chunk_size.attr,
  3722. &md_size.attr,
  3723. &md_resync_start.attr,
  3724. &md_metadata.attr,
  3725. &md_new_device.attr,
  3726. &md_safe_delay.attr,
  3727. &md_array_state.attr,
  3728. &md_reshape_position.attr,
  3729. &md_array_size.attr,
  3730. &max_corr_read_errors.attr,
  3731. NULL,
  3732. };
  3733. static struct attribute *md_redundancy_attrs[] = {
  3734. &md_scan_mode.attr,
  3735. &md_mismatches.attr,
  3736. &md_sync_min.attr,
  3737. &md_sync_max.attr,
  3738. &md_sync_speed.attr,
  3739. &md_sync_force_parallel.attr,
  3740. &md_sync_completed.attr,
  3741. &md_min_sync.attr,
  3742. &md_max_sync.attr,
  3743. &md_suspend_lo.attr,
  3744. &md_suspend_hi.attr,
  3745. &md_bitmap.attr,
  3746. &md_degraded.attr,
  3747. NULL,
  3748. };
  3749. static struct attribute_group md_redundancy_group = {
  3750. .name = NULL,
  3751. .attrs = md_redundancy_attrs,
  3752. };
  3753. static ssize_t
  3754. md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  3755. {
  3756. struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
  3757. mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
  3758. ssize_t rv;
  3759. if (!entry->show)
  3760. return -EIO;
  3761. rv = mddev_lock(mddev);
  3762. if (!rv) {
  3763. rv = entry->show(mddev, page);
  3764. mddev_unlock(mddev);
  3765. }
  3766. return rv;
  3767. }
  3768. static ssize_t
  3769. md_attr_store(struct kobject *kobj, struct attribute *attr,
  3770. const char *page, size_t length)
  3771. {
  3772. struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
  3773. mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
  3774. ssize_t rv;
  3775. if (!entry->store)
  3776. return -EIO;
  3777. if (!capable(CAP_SYS_ADMIN))
  3778. return -EACCES;
  3779. rv = mddev_lock(mddev);
  3780. if (mddev->hold_active == UNTIL_IOCTL)
  3781. mddev->hold_active = 0;
  3782. if (!rv) {
  3783. rv = entry->store(mddev, page, length);
  3784. mddev_unlock(mddev);
  3785. }
  3786. return rv;
  3787. }
  3788. static void md_free(struct kobject *ko)
  3789. {
  3790. mddev_t *mddev = container_of(ko, mddev_t, kobj);
  3791. if (mddev->sysfs_state)
  3792. sysfs_put(mddev->sysfs_state);
  3793. if (mddev->gendisk) {
  3794. del_gendisk(mddev->gendisk);
  3795. put_disk(mddev->gendisk);
  3796. }
  3797. if (mddev->queue)
  3798. blk_cleanup_queue(mddev->queue);
  3799. kfree(mddev);
  3800. }
  3801. static const struct sysfs_ops md_sysfs_ops = {
  3802. .show = md_attr_show,
  3803. .store = md_attr_store,
  3804. };
  3805. static struct kobj_type md_ktype = {
  3806. .release = md_free,
  3807. .sysfs_ops = &md_sysfs_ops,
  3808. .default_attrs = md_default_attrs,
  3809. };
  3810. int mdp_major = 0;
  3811. static void mddev_delayed_delete(struct work_struct *ws)
  3812. {
  3813. mddev_t *mddev = container_of(ws, mddev_t, del_work);
  3814. sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
  3815. kobject_del(&mddev->kobj);
  3816. kobject_put(&mddev->kobj);
  3817. }
  3818. static int md_alloc(dev_t dev, char *name)
  3819. {
  3820. static DEFINE_MUTEX(disks_mutex);
  3821. mddev_t *mddev = mddev_find(dev);
  3822. struct gendisk *disk;
  3823. int partitioned;
  3824. int shift;
  3825. int unit;
  3826. int error;
  3827. if (!mddev)
  3828. return -ENODEV;
  3829. partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
  3830. shift = partitioned ? MdpMinorShift : 0;
  3831. unit = MINOR(mddev->unit) >> shift;
  3832. /* wait for any previous instance of this device to be
  3833. * completely removed (mddev_delayed_delete).
  3834. */
  3835. flush_workqueue(md_misc_wq);
  3836. mutex_lock(&disks_mutex);
  3837. error = -EEXIST;
  3838. if (mddev->gendisk)
  3839. goto abort;
  3840. if (name) {
  3841. /* Need to ensure that 'name' is not a duplicate.
  3842. */
  3843. mddev_t *mddev2;
  3844. spin_lock(&all_mddevs_lock);
  3845. list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
  3846. if (mddev2->gendisk &&
  3847. strcmp(mddev2->gendisk->disk_name, name) == 0) {
  3848. spin_unlock(&all_mddevs_lock);
  3849. goto abort;
  3850. }
  3851. spin_unlock(&all_mddevs_lock);
  3852. }
  3853. error = -ENOMEM;
  3854. mddev->queue = blk_alloc_queue(GFP_KERNEL);
  3855. if (!mddev->queue)
  3856. goto abort;
  3857. mddev->queue->queuedata = mddev;
  3858. blk_queue_make_request(mddev->queue, md_make_request);
  3859. disk = alloc_disk(1 << shift);
  3860. if (!disk) {
  3861. blk_cleanup_queue(mddev->queue);
  3862. mddev->queue = NULL;
  3863. goto abort;
  3864. }
  3865. disk->major = MAJOR(mddev->unit);
  3866. disk->first_minor = unit << shift;
  3867. if (name)
  3868. strcpy(disk->disk_name, name);
  3869. else if (partitioned)
  3870. sprintf(disk->disk_name, "md_d%d", unit);
  3871. else
  3872. sprintf(disk->disk_name, "md%d", unit);
  3873. disk->fops = &md_fops;
  3874. disk->private_data = mddev;
  3875. disk->queue = mddev->queue;
  3876. blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
  3877. /* Allow extended partitions. This makes the
  3878. * 'mdp' device redundant, but we can't really
  3879. * remove it now.
  3880. */
  3881. disk->flags |= GENHD_FL_EXT_DEVT;
  3882. mddev->gendisk = disk;
  3883. /* As soon as we call add_disk(), another thread could get
  3884. * through to md_open, so make sure it doesn't get too far
  3885. */
  3886. mutex_lock(&mddev->open_mutex);
  3887. add_disk(disk);
  3888. error = kobject_init_and_add(&mddev->kobj, &md_ktype,
  3889. &disk_to_dev(disk)->kobj, "%s", "md");
  3890. if (error) {
  3891. /* This isn't possible, but as kobject_init_and_add is marked
  3892. * __must_check, we must do something with the result
  3893. */
  3894. printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
  3895. disk->disk_name);
  3896. error = 0;
  3897. }
  3898. if (mddev->kobj.sd &&
  3899. sysfs_create_group(&mddev->kobj, &md_bitmap_group))
  3900. printk(KERN_DEBUG "pointless warning\n");
  3901. mutex_unlock(&mddev->open_mutex);
  3902. abort:
  3903. mutex_unlock(&disks_mutex);
  3904. if (!error && mddev->kobj.sd) {
  3905. kobject_uevent(&mddev->kobj, KOBJ_ADD);
  3906. mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
  3907. }
  3908. mddev_put(mddev);
  3909. return error;
  3910. }
  3911. static struct kobject *md_probe(dev_t dev, int *part, void *data)
  3912. {
  3913. md_alloc(dev, NULL);
  3914. return NULL;
  3915. }
  3916. static int add_named_array(const char *val, struct kernel_param *kp)
  3917. {
  3918. /* val must be "md_*" where * is not all digits.
  3919. * We allocate an array with a large free minor number, and
  3920. * set the name to val. val must not already be an active name.
  3921. */
  3922. int len = strlen(val);
  3923. char buf[DISK_NAME_LEN];
  3924. while (len && val[len-1] == '\n')
  3925. len--;
  3926. if (len >= DISK_NAME_LEN)
  3927. return -E2BIG;
  3928. strlcpy(buf, val, len+1);
  3929. if (strncmp(buf, "md_", 3) != 0)
  3930. return -EINVAL;
  3931. return md_alloc(0, buf);
  3932. }
  3933. static void md_safemode_timeout(unsigned long data)
  3934. {
  3935. mddev_t *mddev = (mddev_t *) data;
  3936. if (!atomic_read(&mddev->writes_pending)) {
  3937. mddev->safemode = 1;
  3938. if (mddev->external)
  3939. sysfs_notify_dirent_safe(mddev->sysfs_state);
  3940. }
  3941. md_wakeup_thread(mddev->thread);
  3942. }
  3943. static int start_dirty_degraded;
  3944. int md_run(mddev_t *mddev)
  3945. {
  3946. int err;
  3947. mdk_rdev_t *rdev;
  3948. struct mdk_personality *pers;
  3949. if (list_empty(&mddev->disks))
  3950. /* cannot run an array with no devices.. */
  3951. return -EINVAL;
  3952. if (mddev->pers)
  3953. return -EBUSY;
  3954. /* Cannot run until previous stop completes properly */
  3955. if (mddev->sysfs_active)
  3956. return -EBUSY;
  3957. /*
  3958. * Analyze all RAID superblock(s)
  3959. */
  3960. if (!mddev->raid_disks) {
  3961. if (!mddev->persistent)
  3962. return -EINVAL;
  3963. analyze_sbs(mddev);
  3964. }
  3965. if (mddev->level != LEVEL_NONE)
  3966. request_module("md-level-%d", mddev->level);
  3967. else if (mddev->clevel[0])
  3968. request_module("md-%s", mddev->clevel);
  3969. /*
  3970. * Drop all container device buffers, from now on
  3971. * the only valid external interface is through the md
  3972. * device.
  3973. */
  3974. list_for_each_entry(rdev, &mddev->disks, same_set) {
  3975. if (test_bit(Faulty, &rdev->flags))
  3976. continue;
  3977. sync_blockdev(rdev->bdev);
  3978. invalidate_bdev(rdev->bdev);
  3979. /* perform some consistency tests on the device.
  3980. * We don't want the data to overlap the metadata,
  3981. * Internal Bitmap issues have been handled elsewhere.
  3982. */
  3983. if (rdev->meta_bdev) {
  3984. /* Nothing to check */;
  3985. } else if (rdev->data_offset < rdev->sb_start) {
  3986. if (mddev->dev_sectors &&
  3987. rdev->data_offset + mddev->dev_sectors
  3988. > rdev->sb_start) {
  3989. printk("md: %s: data overlaps metadata\n",
  3990. mdname(mddev));
  3991. return -EINVAL;
  3992. }
  3993. } else {
  3994. if (rdev->sb_start + rdev->sb_size/512
  3995. > rdev->data_offset) {
  3996. printk("md: %s: metadata overlaps data\n",
  3997. mdname(mddev));
  3998. return -EINVAL;
  3999. }
  4000. }
  4001. sysfs_notify_dirent_safe(rdev->sysfs_state);
  4002. }
  4003. if (mddev->bio_set == NULL)
  4004. mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
  4005. spin_lock(&pers_lock);
  4006. pers = find_pers(mddev->level, mddev->clevel);
  4007. if (!pers || !try_module_get(pers->owner)) {
  4008. spin_unlock(&pers_lock);
  4009. if (mddev->level != LEVEL_NONE)
  4010. printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
  4011. mddev->level);
  4012. else
  4013. printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
  4014. mddev->clevel);
  4015. return -EINVAL;
  4016. }
  4017. mddev->pers = pers;
  4018. spin_unlock(&pers_lock);
  4019. if (mddev->level != pers->level) {
  4020. mddev->level = pers->level;
  4021. mddev->new_level = pers->level;
  4022. }
  4023. strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
  4024. if (mddev->reshape_position != MaxSector &&
  4025. pers->start_reshape == NULL) {
  4026. /* This personality cannot handle reshaping... */
  4027. mddev->pers = NULL;
  4028. module_put(pers->owner);
  4029. return -EINVAL;
  4030. }
  4031. if (pers->sync_request) {
  4032. /* Warn if this is a potentially silly
  4033. * configuration.
  4034. */
  4035. char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
  4036. mdk_rdev_t *rdev2;
  4037. int warned = 0;
  4038. list_for_each_entry(rdev, &mddev->disks, same_set)
  4039. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  4040. if (rdev < rdev2 &&
  4041. rdev->bdev->bd_contains ==
  4042. rdev2->bdev->bd_contains) {
  4043. printk(KERN_WARNING
  4044. "%s: WARNING: %s appears to be"
  4045. " on the same physical disk as"
  4046. " %s.\n",
  4047. mdname(mddev),
  4048. bdevname(rdev->bdev,b),
  4049. bdevname(rdev2->bdev,b2));
  4050. warned = 1;
  4051. }
  4052. }
  4053. if (warned)
  4054. printk(KERN_WARNING
  4055. "True protection against single-disk"
  4056. " failure might be compromised.\n");
  4057. }
  4058. mddev->recovery = 0;
  4059. /* may be over-ridden by personality */
  4060. mddev->resync_max_sectors = mddev->dev_sectors;
  4061. mddev->ok_start_degraded = start_dirty_degraded;
  4062. if (start_readonly && mddev->ro == 0)
  4063. mddev->ro = 2; /* read-only, but switch on first write */
  4064. err = mddev->pers->run(mddev);
  4065. if (err)
  4066. printk(KERN_ERR "md: pers->run() failed ...\n");
  4067. else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
  4068. WARN_ONCE(!mddev->external_size, "%s: default size too small,"
  4069. " but 'external_size' not in effect?\n", __func__);
  4070. printk(KERN_ERR
  4071. "md: invalid array_size %llu > default size %llu\n",
  4072. (unsigned long long)mddev->array_sectors / 2,
  4073. (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
  4074. err = -EINVAL;
  4075. mddev->pers->stop(mddev);
  4076. }
  4077. if (err == 0 && mddev->pers->sync_request) {
  4078. err = bitmap_create(mddev);
  4079. if (err) {
  4080. printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
  4081. mdname(mddev), err);
  4082. mddev->pers->stop(mddev);
  4083. }
  4084. }
  4085. if (err) {
  4086. module_put(mddev->pers->owner);
  4087. mddev->pers = NULL;
  4088. bitmap_destroy(mddev);
  4089. return err;
  4090. }
  4091. if (mddev->pers->sync_request) {
  4092. if (mddev->kobj.sd &&
  4093. sysfs_create_group(&mddev->kobj, &md_redundancy_group))
  4094. printk(KERN_WARNING
  4095. "md: cannot register extra attributes for %s\n",
  4096. mdname(mddev));
  4097. mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
  4098. } else if (mddev->ro == 2) /* auto-readonly not meaningful */
  4099. mddev->ro = 0;
  4100. atomic_set(&mddev->writes_pending,0);
  4101. atomic_set(&mddev->max_corr_read_errors,
  4102. MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
  4103. mddev->safemode = 0;
  4104. mddev->safemode_timer.function = md_safemode_timeout;
  4105. mddev->safemode_timer.data = (unsigned long) mddev;
  4106. mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
  4107. mddev->in_sync = 1;
  4108. smp_wmb();
  4109. mddev->ready = 1;
  4110. list_for_each_entry(rdev, &mddev->disks, same_set)
  4111. if (rdev->raid_disk >= 0) {
  4112. char nm[20];
  4113. sprintf(nm, "rd%d", rdev->raid_disk);
  4114. if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
  4115. /* failure here is OK */;
  4116. }
  4117. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4118. if (mddev->flags)
  4119. md_update_sb(mddev, 0);
  4120. md_new_event(mddev);
  4121. sysfs_notify_dirent_safe(mddev->sysfs_state);
  4122. sysfs_notify_dirent_safe(mddev->sysfs_action);
  4123. sysfs_notify(&mddev->kobj, NULL, "degraded");
  4124. return 0;
  4125. }
  4126. EXPORT_SYMBOL_GPL(md_run);
  4127. static int do_md_run(mddev_t *mddev)
  4128. {
  4129. int err;
  4130. err = md_run(mddev);
  4131. if (err)
  4132. goto out;
  4133. err = bitmap_load(mddev);
  4134. if (err) {
  4135. bitmap_destroy(mddev);
  4136. goto out;
  4137. }
  4138. md_wakeup_thread(mddev->thread);
  4139. md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
  4140. set_capacity(mddev->gendisk, mddev->array_sectors);
  4141. revalidate_disk(mddev->gendisk);
  4142. mddev->changed = 1;
  4143. kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
  4144. out:
  4145. return err;
  4146. }
  4147. static int restart_array(mddev_t *mddev)
  4148. {
  4149. struct gendisk *disk = mddev->gendisk;
  4150. /* Complain if it has no devices */
  4151. if (list_empty(&mddev->disks))
  4152. return -ENXIO;
  4153. if (!mddev->pers)
  4154. return -EINVAL;
  4155. if (!mddev->ro)
  4156. return -EBUSY;
  4157. mddev->safemode = 0;
  4158. mddev->ro = 0;
  4159. set_disk_ro(disk, 0);
  4160. printk(KERN_INFO "md: %s switched to read-write mode.\n",
  4161. mdname(mddev));
  4162. /* Kick recovery or resync if necessary */
  4163. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4164. md_wakeup_thread(mddev->thread);
  4165. md_wakeup_thread(mddev->sync_thread);
  4166. sysfs_notify_dirent_safe(mddev->sysfs_state);
  4167. return 0;
  4168. }
  4169. /* similar to deny_write_access, but accounts for our holding a reference
  4170. * to the file ourselves */
  4171. static int deny_bitmap_write_access(struct file * file)
  4172. {
  4173. struct inode *inode = file->f_mapping->host;
  4174. spin_lock(&inode->i_lock);
  4175. if (atomic_read(&inode->i_writecount) > 1) {
  4176. spin_unlock(&inode->i_lock);
  4177. return -ETXTBSY;
  4178. }
  4179. atomic_set(&inode->i_writecount, -1);
  4180. spin_unlock(&inode->i_lock);
  4181. return 0;
  4182. }
  4183. void restore_bitmap_write_access(struct file *file)
  4184. {
  4185. struct inode *inode = file->f_mapping->host;
  4186. spin_lock(&inode->i_lock);
  4187. atomic_set(&inode->i_writecount, 1);
  4188. spin_unlock(&inode->i_lock);
  4189. }
  4190. static void md_clean(mddev_t *mddev)
  4191. {
  4192. mddev->array_sectors = 0;
  4193. mddev->external_size = 0;
  4194. mddev->dev_sectors = 0;
  4195. mddev->raid_disks = 0;
  4196. mddev->recovery_cp = 0;
  4197. mddev->resync_min = 0;
  4198. mddev->resync_max = MaxSector;
  4199. mddev->reshape_position = MaxSector;
  4200. mddev->external = 0;
  4201. mddev->persistent = 0;
  4202. mddev->level = LEVEL_NONE;
  4203. mddev->clevel[0] = 0;
  4204. mddev->flags = 0;
  4205. mddev->ro = 0;
  4206. mddev->metadata_type[0] = 0;
  4207. mddev->chunk_sectors = 0;
  4208. mddev->ctime = mddev->utime = 0;
  4209. mddev->layout = 0;
  4210. mddev->max_disks = 0;
  4211. mddev->events = 0;
  4212. mddev->can_decrease_events = 0;
  4213. mddev->delta_disks = 0;
  4214. mddev->new_level = LEVEL_NONE;
  4215. mddev->new_layout = 0;
  4216. mddev->new_chunk_sectors = 0;
  4217. mddev->curr_resync = 0;
  4218. mddev->resync_mismatches = 0;
  4219. mddev->suspend_lo = mddev->suspend_hi = 0;
  4220. mddev->sync_speed_min = mddev->sync_speed_max = 0;
  4221. mddev->recovery = 0;
  4222. mddev->in_sync = 0;
  4223. mddev->changed = 0;
  4224. mddev->degraded = 0;
  4225. mddev->safemode = 0;
  4226. mddev->bitmap_info.offset = 0;
  4227. mddev->bitmap_info.default_offset = 0;
  4228. mddev->bitmap_info.chunksize = 0;
  4229. mddev->bitmap_info.daemon_sleep = 0;
  4230. mddev->bitmap_info.max_write_behind = 0;
  4231. }
  4232. static void __md_stop_writes(mddev_t *mddev)
  4233. {
  4234. if (mddev->sync_thread) {
  4235. set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  4236. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  4237. reap_sync_thread(mddev);
  4238. }
  4239. del_timer_sync(&mddev->safemode_timer);
  4240. bitmap_flush(mddev);
  4241. md_super_wait(mddev);
  4242. if (!mddev->in_sync || mddev->flags) {
  4243. /* mark array as shutdown cleanly */
  4244. mddev->in_sync = 1;
  4245. md_update_sb(mddev, 1);
  4246. }
  4247. }
  4248. void md_stop_writes(mddev_t *mddev)
  4249. {
  4250. mddev_lock(mddev);
  4251. __md_stop_writes(mddev);
  4252. mddev_unlock(mddev);
  4253. }
  4254. EXPORT_SYMBOL_GPL(md_stop_writes);
  4255. void md_stop(mddev_t *mddev)
  4256. {
  4257. mddev->ready = 0;
  4258. mddev->pers->stop(mddev);
  4259. if (mddev->pers->sync_request && mddev->to_remove == NULL)
  4260. mddev->to_remove = &md_redundancy_group;
  4261. module_put(mddev->pers->owner);
  4262. mddev->pers = NULL;
  4263. clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  4264. }
  4265. EXPORT_SYMBOL_GPL(md_stop);
  4266. static int md_set_readonly(mddev_t *mddev, int is_open)
  4267. {
  4268. int err = 0;
  4269. mutex_lock(&mddev->open_mutex);
  4270. if (atomic_read(&mddev->openers) > is_open) {
  4271. printk("md: %s still in use.\n",mdname(mddev));
  4272. err = -EBUSY;
  4273. goto out;
  4274. }
  4275. if (mddev->pers) {
  4276. __md_stop_writes(mddev);
  4277. err = -ENXIO;
  4278. if (mddev->ro==1)
  4279. goto out;
  4280. mddev->ro = 1;
  4281. set_disk_ro(mddev->gendisk, 1);
  4282. clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  4283. sysfs_notify_dirent_safe(mddev->sysfs_state);
  4284. err = 0;
  4285. }
  4286. out:
  4287. mutex_unlock(&mddev->open_mutex);
  4288. return err;
  4289. }
  4290. /* mode:
  4291. * 0 - completely stop and dis-assemble array
  4292. * 2 - stop but do not disassemble array
  4293. */
  4294. static int do_md_stop(mddev_t * mddev, int mode, int is_open)
  4295. {
  4296. struct gendisk *disk = mddev->gendisk;
  4297. mdk_rdev_t *rdev;
  4298. mutex_lock(&mddev->open_mutex);
  4299. if (atomic_read(&mddev->openers) > is_open ||
  4300. mddev->sysfs_active) {
  4301. printk("md: %s still in use.\n",mdname(mddev));
  4302. mutex_unlock(&mddev->open_mutex);
  4303. return -EBUSY;
  4304. }
  4305. if (mddev->pers) {
  4306. if (mddev->ro)
  4307. set_disk_ro(disk, 0);
  4308. __md_stop_writes(mddev);
  4309. md_stop(mddev);
  4310. mddev->queue->merge_bvec_fn = NULL;
  4311. mddev->queue->backing_dev_info.congested_fn = NULL;
  4312. /* tell userspace to handle 'inactive' */
  4313. sysfs_notify_dirent_safe(mddev->sysfs_state);
  4314. list_for_each_entry(rdev, &mddev->disks, same_set)
  4315. if (rdev->raid_disk >= 0) {
  4316. char nm[20];
  4317. sprintf(nm, "rd%d", rdev->raid_disk);
  4318. sysfs_remove_link(&mddev->kobj, nm);
  4319. }
  4320. set_capacity(disk, 0);
  4321. mutex_unlock(&mddev->open_mutex);
  4322. mddev->changed = 1;
  4323. revalidate_disk(disk);
  4324. if (mddev->ro)
  4325. mddev->ro = 0;
  4326. } else
  4327. mutex_unlock(&mddev->open_mutex);
  4328. /*
  4329. * Free resources if final stop
  4330. */
  4331. if (mode == 0) {
  4332. printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
  4333. bitmap_destroy(mddev);
  4334. if (mddev->bitmap_info.file) {
  4335. restore_bitmap_write_access(mddev->bitmap_info.file);
  4336. fput(mddev->bitmap_info.file);
  4337. mddev->bitmap_info.file = NULL;
  4338. }
  4339. mddev->bitmap_info.offset = 0;
  4340. export_array(mddev);
  4341. md_clean(mddev);
  4342. kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
  4343. if (mddev->hold_active == UNTIL_STOP)
  4344. mddev->hold_active = 0;
  4345. }
  4346. blk_integrity_unregister(disk);
  4347. md_new_event(mddev);
  4348. sysfs_notify_dirent_safe(mddev->sysfs_state);
  4349. return 0;
  4350. }
  4351. #ifndef MODULE
  4352. static void autorun_array(mddev_t *mddev)
  4353. {
  4354. mdk_rdev_t *rdev;
  4355. int err;
  4356. if (list_empty(&mddev->disks))
  4357. return;
  4358. printk(KERN_INFO "md: running: ");
  4359. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4360. char b[BDEVNAME_SIZE];
  4361. printk("<%s>", bdevname(rdev->bdev,b));
  4362. }
  4363. printk("\n");
  4364. err = do_md_run(mddev);
  4365. if (err) {
  4366. printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
  4367. do_md_stop(mddev, 0, 0);
  4368. }
  4369. }
  4370. /*
  4371. * lets try to run arrays based on all disks that have arrived
  4372. * until now. (those are in pending_raid_disks)
  4373. *
  4374. * the method: pick the first pending disk, collect all disks with
  4375. * the same UUID, remove all from the pending list and put them into
  4376. * the 'same_array' list. Then order this list based on superblock
  4377. * update time (freshest comes first), kick out 'old' disks and
  4378. * compare superblocks. If everything's fine then run it.
  4379. *
  4380. * If "unit" is allocated, then bump its reference count
  4381. */
  4382. static void autorun_devices(int part)
  4383. {
  4384. mdk_rdev_t *rdev0, *rdev, *tmp;
  4385. mddev_t *mddev;
  4386. char b[BDEVNAME_SIZE];
  4387. printk(KERN_INFO "md: autorun ...\n");
  4388. while (!list_empty(&pending_raid_disks)) {
  4389. int unit;
  4390. dev_t dev;
  4391. LIST_HEAD(candidates);
  4392. rdev0 = list_entry(pending_raid_disks.next,
  4393. mdk_rdev_t, same_set);
  4394. printk(KERN_INFO "md: considering %s ...\n",
  4395. bdevname(rdev0->bdev,b));
  4396. INIT_LIST_HEAD(&candidates);
  4397. rdev_for_each_list(rdev, tmp, &pending_raid_disks)
  4398. if (super_90_load(rdev, rdev0, 0) >= 0) {
  4399. printk(KERN_INFO "md: adding %s ...\n",
  4400. bdevname(rdev->bdev,b));
  4401. list_move(&rdev->same_set, &candidates);
  4402. }
  4403. /*
  4404. * now we have a set of devices, with all of them having
  4405. * mostly sane superblocks. It's time to allocate the
  4406. * mddev.
  4407. */
  4408. if (part) {
  4409. dev = MKDEV(mdp_major,
  4410. rdev0->preferred_minor << MdpMinorShift);
  4411. unit = MINOR(dev) >> MdpMinorShift;
  4412. } else {
  4413. dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
  4414. unit = MINOR(dev);
  4415. }
  4416. if (rdev0->preferred_minor != unit) {
  4417. printk(KERN_INFO "md: unit number in %s is bad: %d\n",
  4418. bdevname(rdev0->bdev, b), rdev0->preferred_minor);
  4419. break;
  4420. }
  4421. md_probe(dev, NULL, NULL);
  4422. mddev = mddev_find(dev);
  4423. if (!mddev || !mddev->gendisk) {
  4424. if (mddev)
  4425. mddev_put(mddev);
  4426. printk(KERN_ERR
  4427. "md: cannot allocate memory for md drive.\n");
  4428. break;
  4429. }
  4430. if (mddev_lock(mddev))
  4431. printk(KERN_WARNING "md: %s locked, cannot run\n",
  4432. mdname(mddev));
  4433. else if (mddev->raid_disks || mddev->major_version
  4434. || !list_empty(&mddev->disks)) {
  4435. printk(KERN_WARNING
  4436. "md: %s already running, cannot run %s\n",
  4437. mdname(mddev), bdevname(rdev0->bdev,b));
  4438. mddev_unlock(mddev);
  4439. } else {
  4440. printk(KERN_INFO "md: created %s\n", mdname(mddev));
  4441. mddev->persistent = 1;
  4442. rdev_for_each_list(rdev, tmp, &candidates) {
  4443. list_del_init(&rdev->same_set);
  4444. if (bind_rdev_to_array(rdev, mddev))
  4445. export_rdev(rdev);
  4446. }
  4447. autorun_array(mddev);
  4448. mddev_unlock(mddev);
  4449. }
  4450. /* on success, candidates will be empty, on error
  4451. * it won't...
  4452. */
  4453. rdev_for_each_list(rdev, tmp, &candidates) {
  4454. list_del_init(&rdev->same_set);
  4455. export_rdev(rdev);
  4456. }
  4457. mddev_put(mddev);
  4458. }
  4459. printk(KERN_INFO "md: ... autorun DONE.\n");
  4460. }
  4461. #endif /* !MODULE */
  4462. static int get_version(void __user * arg)
  4463. {
  4464. mdu_version_t ver;
  4465. ver.major = MD_MAJOR_VERSION;
  4466. ver.minor = MD_MINOR_VERSION;
  4467. ver.patchlevel = MD_PATCHLEVEL_VERSION;
  4468. if (copy_to_user(arg, &ver, sizeof(ver)))
  4469. return -EFAULT;
  4470. return 0;
  4471. }
  4472. static int get_array_info(mddev_t * mddev, void __user * arg)
  4473. {
  4474. mdu_array_info_t info;
  4475. int nr,working,insync,failed,spare;
  4476. mdk_rdev_t *rdev;
  4477. nr=working=insync=failed=spare=0;
  4478. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4479. nr++;
  4480. if (test_bit(Faulty, &rdev->flags))
  4481. failed++;
  4482. else {
  4483. working++;
  4484. if (test_bit(In_sync, &rdev->flags))
  4485. insync++;
  4486. else
  4487. spare++;
  4488. }
  4489. }
  4490. info.major_version = mddev->major_version;
  4491. info.minor_version = mddev->minor_version;
  4492. info.patch_version = MD_PATCHLEVEL_VERSION;
  4493. info.ctime = mddev->ctime;
  4494. info.level = mddev->level;
  4495. info.size = mddev->dev_sectors / 2;
  4496. if (info.size != mddev->dev_sectors / 2) /* overflow */
  4497. info.size = -1;
  4498. info.nr_disks = nr;
  4499. info.raid_disks = mddev->raid_disks;
  4500. info.md_minor = mddev->md_minor;
  4501. info.not_persistent= !mddev->persistent;
  4502. info.utime = mddev->utime;
  4503. info.state = 0;
  4504. if (mddev->in_sync)
  4505. info.state = (1<<MD_SB_CLEAN);
  4506. if (mddev->bitmap && mddev->bitmap_info.offset)
  4507. info.state = (1<<MD_SB_BITMAP_PRESENT);
  4508. info.active_disks = insync;
  4509. info.working_disks = working;
  4510. info.failed_disks = failed;
  4511. info.spare_disks = spare;
  4512. info.layout = mddev->layout;
  4513. info.chunk_size = mddev->chunk_sectors << 9;
  4514. if (copy_to_user(arg, &info, sizeof(info)))
  4515. return -EFAULT;
  4516. return 0;
  4517. }
  4518. static int get_bitmap_file(mddev_t * mddev, void __user * arg)
  4519. {
  4520. mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
  4521. char *ptr, *buf = NULL;
  4522. int err = -ENOMEM;
  4523. if (md_allow_write(mddev))
  4524. file = kmalloc(sizeof(*file), GFP_NOIO);
  4525. else
  4526. file = kmalloc(sizeof(*file), GFP_KERNEL);
  4527. if (!file)
  4528. goto out;
  4529. /* bitmap disabled, zero the first byte and copy out */
  4530. if (!mddev->bitmap || !mddev->bitmap->file) {
  4531. file->pathname[0] = '\0';
  4532. goto copy_out;
  4533. }
  4534. buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
  4535. if (!buf)
  4536. goto out;
  4537. ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
  4538. if (IS_ERR(ptr))
  4539. goto out;
  4540. strcpy(file->pathname, ptr);
  4541. copy_out:
  4542. err = 0;
  4543. if (copy_to_user(arg, file, sizeof(*file)))
  4544. err = -EFAULT;
  4545. out:
  4546. kfree(buf);
  4547. kfree(file);
  4548. return err;
  4549. }
  4550. static int get_disk_info(mddev_t * mddev, void __user * arg)
  4551. {
  4552. mdu_disk_info_t info;
  4553. mdk_rdev_t *rdev;
  4554. if (copy_from_user(&info, arg, sizeof(info)))
  4555. return -EFAULT;
  4556. rdev = find_rdev_nr(mddev, info.number);
  4557. if (rdev) {
  4558. info.major = MAJOR(rdev->bdev->bd_dev);
  4559. info.minor = MINOR(rdev->bdev->bd_dev);
  4560. info.raid_disk = rdev->raid_disk;
  4561. info.state = 0;
  4562. if (test_bit(Faulty, &rdev->flags))
  4563. info.state |= (1<<MD_DISK_FAULTY);
  4564. else if (test_bit(In_sync, &rdev->flags)) {
  4565. info.state |= (1<<MD_DISK_ACTIVE);
  4566. info.state |= (1<<MD_DISK_SYNC);
  4567. }
  4568. if (test_bit(WriteMostly, &rdev->flags))
  4569. info.state |= (1<<MD_DISK_WRITEMOSTLY);
  4570. } else {
  4571. info.major = info.minor = 0;
  4572. info.raid_disk = -1;
  4573. info.state = (1<<MD_DISK_REMOVED);
  4574. }
  4575. if (copy_to_user(arg, &info, sizeof(info)))
  4576. return -EFAULT;
  4577. return 0;
  4578. }
  4579. static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
  4580. {
  4581. char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
  4582. mdk_rdev_t *rdev;
  4583. dev_t dev = MKDEV(info->major,info->minor);
  4584. if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
  4585. return -EOVERFLOW;
  4586. if (!mddev->raid_disks) {
  4587. int err;
  4588. /* expecting a device which has a superblock */
  4589. rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
  4590. if (IS_ERR(rdev)) {
  4591. printk(KERN_WARNING
  4592. "md: md_import_device returned %ld\n",
  4593. PTR_ERR(rdev));
  4594. return PTR_ERR(rdev);
  4595. }
  4596. if (!list_empty(&mddev->disks)) {
  4597. mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
  4598. mdk_rdev_t, same_set);
  4599. err = super_types[mddev->major_version]
  4600. .load_super(rdev, rdev0, mddev->minor_version);
  4601. if (err < 0) {
  4602. printk(KERN_WARNING
  4603. "md: %s has different UUID to %s\n",
  4604. bdevname(rdev->bdev,b),
  4605. bdevname(rdev0->bdev,b2));
  4606. export_rdev(rdev);
  4607. return -EINVAL;
  4608. }
  4609. }
  4610. err = bind_rdev_to_array(rdev, mddev);
  4611. if (err)
  4612. export_rdev(rdev);
  4613. return err;
  4614. }
  4615. /*
  4616. * add_new_disk can be used once the array is assembled
  4617. * to add "hot spares". They must already have a superblock
  4618. * written
  4619. */
  4620. if (mddev->pers) {
  4621. int err;
  4622. if (!mddev->pers->hot_add_disk) {
  4623. printk(KERN_WARNING
  4624. "%s: personality does not support diskops!\n",
  4625. mdname(mddev));
  4626. return -EINVAL;
  4627. }
  4628. if (mddev->persistent)
  4629. rdev = md_import_device(dev, mddev->major_version,
  4630. mddev->minor_version);
  4631. else
  4632. rdev = md_import_device(dev, -1, -1);
  4633. if (IS_ERR(rdev)) {
  4634. printk(KERN_WARNING
  4635. "md: md_import_device returned %ld\n",
  4636. PTR_ERR(rdev));
  4637. return PTR_ERR(rdev);
  4638. }
  4639. /* set saved_raid_disk if appropriate */
  4640. if (!mddev->persistent) {
  4641. if (info->state & (1<<MD_DISK_SYNC) &&
  4642. info->raid_disk < mddev->raid_disks) {
  4643. rdev->raid_disk = info->raid_disk;
  4644. set_bit(In_sync, &rdev->flags);
  4645. } else
  4646. rdev->raid_disk = -1;
  4647. } else
  4648. super_types[mddev->major_version].
  4649. validate_super(mddev, rdev);
  4650. if ((info->state & (1<<MD_DISK_SYNC)) &&
  4651. (!test_bit(In_sync, &rdev->flags) ||
  4652. rdev->raid_disk != info->raid_disk)) {
  4653. /* This was a hot-add request, but events doesn't
  4654. * match, so reject it.
  4655. */
  4656. export_rdev(rdev);
  4657. return -EINVAL;
  4658. }
  4659. if (test_bit(In_sync, &rdev->flags))
  4660. rdev->saved_raid_disk = rdev->raid_disk;
  4661. else
  4662. rdev->saved_raid_disk = -1;
  4663. clear_bit(In_sync, &rdev->flags); /* just to be sure */
  4664. if (info->state & (1<<MD_DISK_WRITEMOSTLY))
  4665. set_bit(WriteMostly, &rdev->flags);
  4666. else
  4667. clear_bit(WriteMostly, &rdev->flags);
  4668. rdev->raid_disk = -1;
  4669. err = bind_rdev_to_array(rdev, mddev);
  4670. if (!err && !mddev->pers->hot_remove_disk) {
  4671. /* If there is hot_add_disk but no hot_remove_disk
  4672. * then added disks for geometry changes,
  4673. * and should be added immediately.
  4674. */
  4675. super_types[mddev->major_version].
  4676. validate_super(mddev, rdev);
  4677. err = mddev->pers->hot_add_disk(mddev, rdev);
  4678. if (err)
  4679. unbind_rdev_from_array(rdev);
  4680. }
  4681. if (err)
  4682. export_rdev(rdev);
  4683. else
  4684. sysfs_notify_dirent_safe(rdev->sysfs_state);
  4685. md_update_sb(mddev, 1);
  4686. if (mddev->degraded)
  4687. set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  4688. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4689. if (!err)
  4690. md_new_event(mddev);
  4691. md_wakeup_thread(mddev->thread);
  4692. return err;
  4693. }
  4694. /* otherwise, add_new_disk is only allowed
  4695. * for major_version==0 superblocks
  4696. */
  4697. if (mddev->major_version != 0) {
  4698. printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
  4699. mdname(mddev));
  4700. return -EINVAL;
  4701. }
  4702. if (!(info->state & (1<<MD_DISK_FAULTY))) {
  4703. int err;
  4704. rdev = md_import_device(dev, -1, 0);
  4705. if (IS_ERR(rdev)) {
  4706. printk(KERN_WARNING
  4707. "md: error, md_import_device() returned %ld\n",
  4708. PTR_ERR(rdev));
  4709. return PTR_ERR(rdev);
  4710. }
  4711. rdev->desc_nr = info->number;
  4712. if (info->raid_disk < mddev->raid_disks)
  4713. rdev->raid_disk = info->raid_disk;
  4714. else
  4715. rdev->raid_disk = -1;
  4716. if (rdev->raid_disk < mddev->raid_disks)
  4717. if (info->state & (1<<MD_DISK_SYNC))
  4718. set_bit(In_sync, &rdev->flags);
  4719. if (info->state & (1<<MD_DISK_WRITEMOSTLY))
  4720. set_bit(WriteMostly, &rdev->flags);
  4721. if (!mddev->persistent) {
  4722. printk(KERN_INFO "md: nonpersistent superblock ...\n");
  4723. rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
  4724. } else
  4725. rdev->sb_start = calc_dev_sboffset(rdev);
  4726. rdev->sectors = rdev->sb_start;
  4727. err = bind_rdev_to_array(rdev, mddev);
  4728. if (err) {
  4729. export_rdev(rdev);
  4730. return err;
  4731. }
  4732. }
  4733. return 0;
  4734. }
  4735. static int hot_remove_disk(mddev_t * mddev, dev_t dev)
  4736. {
  4737. char b[BDEVNAME_SIZE];
  4738. mdk_rdev_t *rdev;
  4739. rdev = find_rdev(mddev, dev);
  4740. if (!rdev)
  4741. return -ENXIO;
  4742. if (rdev->raid_disk >= 0)
  4743. goto busy;
  4744. kick_rdev_from_array(rdev);
  4745. md_update_sb(mddev, 1);
  4746. md_new_event(mddev);
  4747. return 0;
  4748. busy:
  4749. printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
  4750. bdevname(rdev->bdev,b), mdname(mddev));
  4751. return -EBUSY;
  4752. }
  4753. static int hot_add_disk(mddev_t * mddev, dev_t dev)
  4754. {
  4755. char b[BDEVNAME_SIZE];
  4756. int err;
  4757. mdk_rdev_t *rdev;
  4758. if (!mddev->pers)
  4759. return -ENODEV;
  4760. if (mddev->major_version != 0) {
  4761. printk(KERN_WARNING "%s: HOT_ADD may only be used with"
  4762. " version-0 superblocks.\n",
  4763. mdname(mddev));
  4764. return -EINVAL;
  4765. }
  4766. if (!mddev->pers->hot_add_disk) {
  4767. printk(KERN_WARNING
  4768. "%s: personality does not support diskops!\n",
  4769. mdname(mddev));
  4770. return -EINVAL;
  4771. }
  4772. rdev = md_import_device(dev, -1, 0);
  4773. if (IS_ERR(rdev)) {
  4774. printk(KERN_WARNING
  4775. "md: error, md_import_device() returned %ld\n",
  4776. PTR_ERR(rdev));
  4777. return -EINVAL;
  4778. }
  4779. if (mddev->persistent)
  4780. rdev->sb_start = calc_dev_sboffset(rdev);
  4781. else
  4782. rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
  4783. rdev->sectors = rdev->sb_start;
  4784. if (test_bit(Faulty, &rdev->flags)) {
  4785. printk(KERN_WARNING
  4786. "md: can not hot-add faulty %s disk to %s!\n",
  4787. bdevname(rdev->bdev,b), mdname(mddev));
  4788. err = -EINVAL;
  4789. goto abort_export;
  4790. }
  4791. clear_bit(In_sync, &rdev->flags);
  4792. rdev->desc_nr = -1;
  4793. rdev->saved_raid_disk = -1;
  4794. err = bind_rdev_to_array(rdev, mddev);
  4795. if (err)
  4796. goto abort_export;
  4797. /*
  4798. * The rest should better be atomic, we can have disk failures
  4799. * noticed in interrupt contexts ...
  4800. */
  4801. rdev->raid_disk = -1;
  4802. md_update_sb(mddev, 1);
  4803. /*
  4804. * Kick recovery, maybe this spare has to be added to the
  4805. * array immediately.
  4806. */
  4807. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4808. md_wakeup_thread(mddev->thread);
  4809. md_new_event(mddev);
  4810. return 0;
  4811. abort_export:
  4812. export_rdev(rdev);
  4813. return err;
  4814. }
  4815. static int set_bitmap_file(mddev_t *mddev, int fd)
  4816. {
  4817. int err;
  4818. if (mddev->pers) {
  4819. if (!mddev->pers->quiesce)
  4820. return -EBUSY;
  4821. if (mddev->recovery || mddev->sync_thread)
  4822. return -EBUSY;
  4823. /* we should be able to change the bitmap.. */
  4824. }
  4825. if (fd >= 0) {
  4826. if (mddev->bitmap)
  4827. return -EEXIST; /* cannot add when bitmap is present */
  4828. mddev->bitmap_info.file = fget(fd);
  4829. if (mddev->bitmap_info.file == NULL) {
  4830. printk(KERN_ERR "%s: error: failed to get bitmap file\n",
  4831. mdname(mddev));
  4832. return -EBADF;
  4833. }
  4834. err = deny_bitmap_write_access(mddev->bitmap_info.file);
  4835. if (err) {
  4836. printk(KERN_ERR "%s: error: bitmap file is already in use\n",
  4837. mdname(mddev));
  4838. fput(mddev->bitmap_info.file);
  4839. mddev->bitmap_info.file = NULL;
  4840. return err;
  4841. }
  4842. mddev->bitmap_info.offset = 0; /* file overrides offset */
  4843. } else if (mddev->bitmap == NULL)
  4844. return -ENOENT; /* cannot remove what isn't there */
  4845. err = 0;
  4846. if (mddev->pers) {
  4847. mddev->pers->quiesce(mddev, 1);
  4848. if (fd >= 0) {
  4849. err = bitmap_create(mddev);
  4850. if (!err)
  4851. err = bitmap_load(mddev);
  4852. }
  4853. if (fd < 0 || err) {
  4854. bitmap_destroy(mddev);
  4855. fd = -1; /* make sure to put the file */
  4856. }
  4857. mddev->pers->quiesce(mddev, 0);
  4858. }
  4859. if (fd < 0) {
  4860. if (mddev->bitmap_info.file) {
  4861. restore_bitmap_write_access(mddev->bitmap_info.file);
  4862. fput(mddev->bitmap_info.file);
  4863. }
  4864. mddev->bitmap_info.file = NULL;
  4865. }
  4866. return err;
  4867. }
  4868. /*
  4869. * set_array_info is used two different ways
  4870. * The original usage is when creating a new array.
  4871. * In this usage, raid_disks is > 0 and it together with
  4872. * level, size, not_persistent,layout,chunksize determine the
  4873. * shape of the array.
  4874. * This will always create an array with a type-0.90.0 superblock.
  4875. * The newer usage is when assembling an array.
  4876. * In this case raid_disks will be 0, and the major_version field is
  4877. * use to determine which style super-blocks are to be found on the devices.
  4878. * The minor and patch _version numbers are also kept incase the
  4879. * super_block handler wishes to interpret them.
  4880. */
  4881. static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
  4882. {
  4883. if (info->raid_disks == 0) {
  4884. /* just setting version number for superblock loading */
  4885. if (info->major_version < 0 ||
  4886. info->major_version >= ARRAY_SIZE(super_types) ||
  4887. super_types[info->major_version].name == NULL) {
  4888. /* maybe try to auto-load a module? */
  4889. printk(KERN_INFO
  4890. "md: superblock version %d not known\n",
  4891. info->major_version);
  4892. return -EINVAL;
  4893. }
  4894. mddev->major_version = info->major_version;
  4895. mddev->minor_version = info->minor_version;
  4896. mddev->patch_version = info->patch_version;
  4897. mddev->persistent = !info->not_persistent;
  4898. /* ensure mddev_put doesn't delete this now that there
  4899. * is some minimal configuration.
  4900. */
  4901. mddev->ctime = get_seconds();
  4902. return 0;
  4903. }
  4904. mddev->major_version = MD_MAJOR_VERSION;
  4905. mddev->minor_version = MD_MINOR_VERSION;
  4906. mddev->patch_version = MD_PATCHLEVEL_VERSION;
  4907. mddev->ctime = get_seconds();
  4908. mddev->level = info->level;
  4909. mddev->clevel[0] = 0;
  4910. mddev->dev_sectors = 2 * (sector_t)info->size;
  4911. mddev->raid_disks = info->raid_disks;
  4912. /* don't set md_minor, it is determined by which /dev/md* was
  4913. * openned
  4914. */
  4915. if (info->state & (1<<MD_SB_CLEAN))
  4916. mddev->recovery_cp = MaxSector;
  4917. else
  4918. mddev->recovery_cp = 0;
  4919. mddev->persistent = ! info->not_persistent;
  4920. mddev->external = 0;
  4921. mddev->layout = info->layout;
  4922. mddev->chunk_sectors = info->chunk_size >> 9;
  4923. mddev->max_disks = MD_SB_DISKS;
  4924. if (mddev->persistent)
  4925. mddev->flags = 0;
  4926. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4927. mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
  4928. mddev->bitmap_info.offset = 0;
  4929. mddev->reshape_position = MaxSector;
  4930. /*
  4931. * Generate a 128 bit UUID
  4932. */
  4933. get_random_bytes(mddev->uuid, 16);
  4934. mddev->new_level = mddev->level;
  4935. mddev->new_chunk_sectors = mddev->chunk_sectors;
  4936. mddev->new_layout = mddev->layout;
  4937. mddev->delta_disks = 0;
  4938. return 0;
  4939. }
  4940. void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
  4941. {
  4942. WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
  4943. if (mddev->external_size)
  4944. return;
  4945. mddev->array_sectors = array_sectors;
  4946. }
  4947. EXPORT_SYMBOL(md_set_array_sectors);
  4948. static int update_size(mddev_t *mddev, sector_t num_sectors)
  4949. {
  4950. mdk_rdev_t *rdev;
  4951. int rv;
  4952. int fit = (num_sectors == 0);
  4953. if (mddev->pers->resize == NULL)
  4954. return -EINVAL;
  4955. /* The "num_sectors" is the number of sectors of each device that
  4956. * is used. This can only make sense for arrays with redundancy.
  4957. * linear and raid0 always use whatever space is available. We can only
  4958. * consider changing this number if no resync or reconstruction is
  4959. * happening, and if the new size is acceptable. It must fit before the
  4960. * sb_start or, if that is <data_offset, it must fit before the size
  4961. * of each device. If num_sectors is zero, we find the largest size
  4962. * that fits.
  4963. */
  4964. if (mddev->sync_thread)
  4965. return -EBUSY;
  4966. if (mddev->bitmap)
  4967. /* Sorry, cannot grow a bitmap yet, just remove it,
  4968. * grow, and re-add.
  4969. */
  4970. return -EBUSY;
  4971. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4972. sector_t avail = rdev->sectors;
  4973. if (fit && (num_sectors == 0 || num_sectors > avail))
  4974. num_sectors = avail;
  4975. if (avail < num_sectors)
  4976. return -ENOSPC;
  4977. }
  4978. rv = mddev->pers->resize(mddev, num_sectors);
  4979. if (!rv)
  4980. revalidate_disk(mddev->gendisk);
  4981. return rv;
  4982. }
  4983. static int update_raid_disks(mddev_t *mddev, int raid_disks)
  4984. {
  4985. int rv;
  4986. /* change the number of raid disks */
  4987. if (mddev->pers->check_reshape == NULL)
  4988. return -EINVAL;
  4989. if (raid_disks <= 0 ||
  4990. (mddev->max_disks && raid_disks >= mddev->max_disks))
  4991. return -EINVAL;
  4992. if (mddev->sync_thread || mddev->reshape_position != MaxSector)
  4993. return -EBUSY;
  4994. mddev->delta_disks = raid_disks - mddev->raid_disks;
  4995. rv = mddev->pers->check_reshape(mddev);
  4996. if (rv < 0)
  4997. mddev->delta_disks = 0;
  4998. return rv;
  4999. }
  5000. /*
  5001. * update_array_info is used to change the configuration of an
  5002. * on-line array.
  5003. * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
  5004. * fields in the info are checked against the array.
  5005. * Any differences that cannot be handled will cause an error.
  5006. * Normally, only one change can be managed at a time.
  5007. */
  5008. static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
  5009. {
  5010. int rv = 0;
  5011. int cnt = 0;
  5012. int state = 0;
  5013. /* calculate expected state,ignoring low bits */
  5014. if (mddev->bitmap && mddev->bitmap_info.offset)
  5015. state |= (1 << MD_SB_BITMAP_PRESENT);
  5016. if (mddev->major_version != info->major_version ||
  5017. mddev->minor_version != info->minor_version ||
  5018. /* mddev->patch_version != info->patch_version || */
  5019. mddev->ctime != info->ctime ||
  5020. mddev->level != info->level ||
  5021. /* mddev->layout != info->layout || */
  5022. !mddev->persistent != info->not_persistent||
  5023. mddev->chunk_sectors != info->chunk_size >> 9 ||
  5024. /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
  5025. ((state^info->state) & 0xfffffe00)
  5026. )
  5027. return -EINVAL;
  5028. /* Check there is only one change */
  5029. if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
  5030. cnt++;
  5031. if (mddev->raid_disks != info->raid_disks)
  5032. cnt++;
  5033. if (mddev->layout != info->layout)
  5034. cnt++;
  5035. if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
  5036. cnt++;
  5037. if (cnt == 0)
  5038. return 0;
  5039. if (cnt > 1)
  5040. return -EINVAL;
  5041. if (mddev->layout != info->layout) {
  5042. /* Change layout
  5043. * we don't need to do anything at the md level, the
  5044. * personality will take care of it all.
  5045. */
  5046. if (mddev->pers->check_reshape == NULL)
  5047. return -EINVAL;
  5048. else {
  5049. mddev->new_layout = info->layout;
  5050. rv = mddev->pers->check_reshape(mddev);
  5051. if (rv)
  5052. mddev->new_layout = mddev->layout;
  5053. return rv;
  5054. }
  5055. }
  5056. if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
  5057. rv = update_size(mddev, (sector_t)info->size * 2);
  5058. if (mddev->raid_disks != info->raid_disks)
  5059. rv = update_raid_disks(mddev, info->raid_disks);
  5060. if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
  5061. if (mddev->pers->quiesce == NULL)
  5062. return -EINVAL;
  5063. if (mddev->recovery || mddev->sync_thread)
  5064. return -EBUSY;
  5065. if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
  5066. /* add the bitmap */
  5067. if (mddev->bitmap)
  5068. return -EEXIST;
  5069. if (mddev->bitmap_info.default_offset == 0)
  5070. return -EINVAL;
  5071. mddev->bitmap_info.offset =
  5072. mddev->bitmap_info.default_offset;
  5073. mddev->pers->quiesce(mddev, 1);
  5074. rv = bitmap_create(mddev);
  5075. if (!rv)
  5076. rv = bitmap_load(mddev);
  5077. if (rv)
  5078. bitmap_destroy(mddev);
  5079. mddev->pers->quiesce(mddev, 0);
  5080. } else {
  5081. /* remove the bitmap */
  5082. if (!mddev->bitmap)
  5083. return -ENOENT;
  5084. if (mddev->bitmap->file)
  5085. return -EINVAL;
  5086. mddev->pers->quiesce(mddev, 1);
  5087. bitmap_destroy(mddev);
  5088. mddev->pers->quiesce(mddev, 0);
  5089. mddev->bitmap_info.offset = 0;
  5090. }
  5091. }
  5092. md_update_sb(mddev, 1);
  5093. return rv;
  5094. }
  5095. static int set_disk_faulty(mddev_t *mddev, dev_t dev)
  5096. {
  5097. mdk_rdev_t *rdev;
  5098. if (mddev->pers == NULL)
  5099. return -ENODEV;
  5100. rdev = find_rdev(mddev, dev);
  5101. if (!rdev)
  5102. return -ENODEV;
  5103. md_error(mddev, rdev);
  5104. return 0;
  5105. }
  5106. /*
  5107. * We have a problem here : there is no easy way to give a CHS
  5108. * virtual geometry. We currently pretend that we have a 2 heads
  5109. * 4 sectors (with a BIG number of cylinders...). This drives
  5110. * dosfs just mad... ;-)
  5111. */
  5112. static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  5113. {
  5114. mddev_t *mddev = bdev->bd_disk->private_data;
  5115. geo->heads = 2;
  5116. geo->sectors = 4;
  5117. geo->cylinders = mddev->array_sectors / 8;
  5118. return 0;
  5119. }
  5120. static int md_ioctl(struct block_device *bdev, fmode_t mode,
  5121. unsigned int cmd, unsigned long arg)
  5122. {
  5123. int err = 0;
  5124. void __user *argp = (void __user *)arg;
  5125. mddev_t *mddev = NULL;
  5126. int ro;
  5127. if (!capable(CAP_SYS_ADMIN))
  5128. return -EACCES;
  5129. /*
  5130. * Commands dealing with the RAID driver but not any
  5131. * particular array:
  5132. */
  5133. switch (cmd)
  5134. {
  5135. case RAID_VERSION:
  5136. err = get_version(argp);
  5137. goto done;
  5138. case PRINT_RAID_DEBUG:
  5139. err = 0;
  5140. md_print_devices();
  5141. goto done;
  5142. #ifndef MODULE
  5143. case RAID_AUTORUN:
  5144. err = 0;
  5145. autostart_arrays(arg);
  5146. goto done;
  5147. #endif
  5148. default:;
  5149. }
  5150. /*
  5151. * Commands creating/starting a new array:
  5152. */
  5153. mddev = bdev->bd_disk->private_data;
  5154. if (!mddev) {
  5155. BUG();
  5156. goto abort;
  5157. }
  5158. err = mddev_lock(mddev);
  5159. if (err) {
  5160. printk(KERN_INFO
  5161. "md: ioctl lock interrupted, reason %d, cmd %d\n",
  5162. err, cmd);
  5163. goto abort;
  5164. }
  5165. switch (cmd)
  5166. {
  5167. case SET_ARRAY_INFO:
  5168. {
  5169. mdu_array_info_t info;
  5170. if (!arg)
  5171. memset(&info, 0, sizeof(info));
  5172. else if (copy_from_user(&info, argp, sizeof(info))) {
  5173. err = -EFAULT;
  5174. goto abort_unlock;
  5175. }
  5176. if (mddev->pers) {
  5177. err = update_array_info(mddev, &info);
  5178. if (err) {
  5179. printk(KERN_WARNING "md: couldn't update"
  5180. " array info. %d\n", err);
  5181. goto abort_unlock;
  5182. }
  5183. goto done_unlock;
  5184. }
  5185. if (!list_empty(&mddev->disks)) {
  5186. printk(KERN_WARNING
  5187. "md: array %s already has disks!\n",
  5188. mdname(mddev));
  5189. err = -EBUSY;
  5190. goto abort_unlock;
  5191. }
  5192. if (mddev->raid_disks) {
  5193. printk(KERN_WARNING
  5194. "md: array %s already initialised!\n",
  5195. mdname(mddev));
  5196. err = -EBUSY;
  5197. goto abort_unlock;
  5198. }
  5199. err = set_array_info(mddev, &info);
  5200. if (err) {
  5201. printk(KERN_WARNING "md: couldn't set"
  5202. " array info. %d\n", err);
  5203. goto abort_unlock;
  5204. }
  5205. }
  5206. goto done_unlock;
  5207. default:;
  5208. }
  5209. /*
  5210. * Commands querying/configuring an existing array:
  5211. */
  5212. /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
  5213. * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
  5214. if ((!mddev->raid_disks && !mddev->external)
  5215. && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
  5216. && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
  5217. && cmd != GET_BITMAP_FILE) {
  5218. err = -ENODEV;
  5219. goto abort_unlock;
  5220. }
  5221. /*
  5222. * Commands even a read-only array can execute:
  5223. */
  5224. switch (cmd)
  5225. {
  5226. case GET_ARRAY_INFO:
  5227. err = get_array_info(mddev, argp);
  5228. goto done_unlock;
  5229. case GET_BITMAP_FILE:
  5230. err = get_bitmap_file(mddev, argp);
  5231. goto done_unlock;
  5232. case GET_DISK_INFO:
  5233. err = get_disk_info(mddev, argp);
  5234. goto done_unlock;
  5235. case RESTART_ARRAY_RW:
  5236. err = restart_array(mddev);
  5237. goto done_unlock;
  5238. case STOP_ARRAY:
  5239. err = do_md_stop(mddev, 0, 1);
  5240. goto done_unlock;
  5241. case STOP_ARRAY_RO:
  5242. err = md_set_readonly(mddev, 1);
  5243. goto done_unlock;
  5244. case BLKROSET:
  5245. if (get_user(ro, (int __user *)(arg))) {
  5246. err = -EFAULT;
  5247. goto done_unlock;
  5248. }
  5249. err = -EINVAL;
  5250. /* if the bdev is going readonly the value of mddev->ro
  5251. * does not matter, no writes are coming
  5252. */
  5253. if (ro)
  5254. goto done_unlock;
  5255. /* are we are already prepared for writes? */
  5256. if (mddev->ro != 1)
  5257. goto done_unlock;
  5258. /* transitioning to readauto need only happen for
  5259. * arrays that call md_write_start
  5260. */
  5261. if (mddev->pers) {
  5262. err = restart_array(mddev);
  5263. if (err == 0) {
  5264. mddev->ro = 2;
  5265. set_disk_ro(mddev->gendisk, 0);
  5266. }
  5267. }
  5268. goto done_unlock;
  5269. }
  5270. /*
  5271. * The remaining ioctls are changing the state of the
  5272. * superblock, so we do not allow them on read-only arrays.
  5273. * However non-MD ioctls (e.g. get-size) will still come through
  5274. * here and hit the 'default' below, so only disallow
  5275. * 'md' ioctls, and switch to rw mode if started auto-readonly.
  5276. */
  5277. if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
  5278. if (mddev->ro == 2) {
  5279. mddev->ro = 0;
  5280. sysfs_notify_dirent_safe(mddev->sysfs_state);
  5281. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  5282. md_wakeup_thread(mddev->thread);
  5283. } else {
  5284. err = -EROFS;
  5285. goto abort_unlock;
  5286. }
  5287. }
  5288. switch (cmd)
  5289. {
  5290. case ADD_NEW_DISK:
  5291. {
  5292. mdu_disk_info_t info;
  5293. if (copy_from_user(&info, argp, sizeof(info)))
  5294. err = -EFAULT;
  5295. else
  5296. err = add_new_disk(mddev, &info);
  5297. goto done_unlock;
  5298. }
  5299. case HOT_REMOVE_DISK:
  5300. err = hot_remove_disk(mddev, new_decode_dev(arg));
  5301. goto done_unlock;
  5302. case HOT_ADD_DISK:
  5303. err = hot_add_disk(mddev, new_decode_dev(arg));
  5304. goto done_unlock;
  5305. case SET_DISK_FAULTY:
  5306. err = set_disk_faulty(mddev, new_decode_dev(arg));
  5307. goto done_unlock;
  5308. case RUN_ARRAY:
  5309. err = do_md_run(mddev);
  5310. goto done_unlock;
  5311. case SET_BITMAP_FILE:
  5312. err = set_bitmap_file(mddev, (int)arg);
  5313. goto done_unlock;
  5314. default:
  5315. err = -EINVAL;
  5316. goto abort_unlock;
  5317. }
  5318. done_unlock:
  5319. abort_unlock:
  5320. if (mddev->hold_active == UNTIL_IOCTL &&
  5321. err != -EINVAL)
  5322. mddev->hold_active = 0;
  5323. mddev_unlock(mddev);
  5324. return err;
  5325. done:
  5326. if (err)
  5327. MD_BUG();
  5328. abort:
  5329. return err;
  5330. }
  5331. #ifdef CONFIG_COMPAT
  5332. static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
  5333. unsigned int cmd, unsigned long arg)
  5334. {
  5335. switch (cmd) {
  5336. case HOT_REMOVE_DISK:
  5337. case HOT_ADD_DISK:
  5338. case SET_DISK_FAULTY:
  5339. case SET_BITMAP_FILE:
  5340. /* These take in integer arg, do not convert */
  5341. break;
  5342. default:
  5343. arg = (unsigned long)compat_ptr(arg);
  5344. break;
  5345. }
  5346. return md_ioctl(bdev, mode, cmd, arg);
  5347. }
  5348. #endif /* CONFIG_COMPAT */
  5349. static int md_open(struct block_device *bdev, fmode_t mode)
  5350. {
  5351. /*
  5352. * Succeed if we can lock the mddev, which confirms that
  5353. * it isn't being stopped right now.
  5354. */
  5355. mddev_t *mddev = mddev_find(bdev->bd_dev);
  5356. int err;
  5357. if (mddev->gendisk != bdev->bd_disk) {
  5358. /* we are racing with mddev_put which is discarding this
  5359. * bd_disk.
  5360. */
  5361. mddev_put(mddev);
  5362. /* Wait until bdev->bd_disk is definitely gone */
  5363. flush_workqueue(md_misc_wq);
  5364. /* Then retry the open from the top */
  5365. return -ERESTARTSYS;
  5366. }
  5367. BUG_ON(mddev != bdev->bd_disk->private_data);
  5368. if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
  5369. goto out;
  5370. err = 0;
  5371. atomic_inc(&mddev->openers);
  5372. mutex_unlock(&mddev->open_mutex);
  5373. check_disk_change(bdev);
  5374. out:
  5375. return err;
  5376. }
  5377. static int md_release(struct gendisk *disk, fmode_t mode)
  5378. {
  5379. mddev_t *mddev = disk->private_data;
  5380. BUG_ON(!mddev);
  5381. atomic_dec(&mddev->openers);
  5382. mddev_put(mddev);
  5383. return 0;
  5384. }
  5385. static int md_media_changed(struct gendisk *disk)
  5386. {
  5387. mddev_t *mddev = disk->private_data;
  5388. return mddev->changed;
  5389. }
  5390. static int md_revalidate(struct gendisk *disk)
  5391. {
  5392. mddev_t *mddev = disk->private_data;
  5393. mddev->changed = 0;
  5394. return 0;
  5395. }
  5396. static const struct block_device_operations md_fops =
  5397. {
  5398. .owner = THIS_MODULE,
  5399. .open = md_open,
  5400. .release = md_release,
  5401. .ioctl = md_ioctl,
  5402. #ifdef CONFIG_COMPAT
  5403. .compat_ioctl = md_compat_ioctl,
  5404. #endif
  5405. .getgeo = md_getgeo,
  5406. .media_changed = md_media_changed,
  5407. .revalidate_disk= md_revalidate,
  5408. };
  5409. static int md_thread(void * arg)
  5410. {
  5411. mdk_thread_t *thread = arg;
  5412. /*
  5413. * md_thread is a 'system-thread', it's priority should be very
  5414. * high. We avoid resource deadlocks individually in each
  5415. * raid personality. (RAID5 does preallocation) We also use RR and
  5416. * the very same RT priority as kswapd, thus we will never get
  5417. * into a priority inversion deadlock.
  5418. *
  5419. * we definitely have to have equal or higher priority than
  5420. * bdflush, otherwise bdflush will deadlock if there are too
  5421. * many dirty RAID5 blocks.
  5422. */
  5423. allow_signal(SIGKILL);
  5424. while (!kthread_should_stop()) {
  5425. /* We need to wait INTERRUPTIBLE so that
  5426. * we don't add to the load-average.
  5427. * That means we need to be sure no signals are
  5428. * pending
  5429. */
  5430. if (signal_pending(current))
  5431. flush_signals(current);
  5432. wait_event_interruptible_timeout
  5433. (thread->wqueue,
  5434. test_bit(THREAD_WAKEUP, &thread->flags)
  5435. || kthread_should_stop(),
  5436. thread->timeout);
  5437. clear_bit(THREAD_WAKEUP, &thread->flags);
  5438. if (!kthread_should_stop())
  5439. thread->run(thread->mddev);
  5440. }
  5441. return 0;
  5442. }
  5443. void md_wakeup_thread(mdk_thread_t *thread)
  5444. {
  5445. if (thread) {
  5446. dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
  5447. set_bit(THREAD_WAKEUP, &thread->flags);
  5448. wake_up(&thread->wqueue);
  5449. }
  5450. }
  5451. mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
  5452. const char *name)
  5453. {
  5454. mdk_thread_t *thread;
  5455. thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
  5456. if (!thread)
  5457. return NULL;
  5458. init_waitqueue_head(&thread->wqueue);
  5459. thread->run = run;
  5460. thread->mddev = mddev;
  5461. thread->timeout = MAX_SCHEDULE_TIMEOUT;
  5462. thread->tsk = kthread_run(md_thread, thread,
  5463. "%s_%s",
  5464. mdname(thread->mddev),
  5465. name ?: mddev->pers->name);
  5466. if (IS_ERR(thread->tsk)) {
  5467. kfree(thread);
  5468. return NULL;
  5469. }
  5470. return thread;
  5471. }
  5472. void md_unregister_thread(mdk_thread_t **threadp)
  5473. {
  5474. mdk_thread_t *thread = *threadp;
  5475. if (!thread)
  5476. return;
  5477. dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
  5478. /* Locking ensures that mddev_unlock does not wake_up a
  5479. * non-existent thread
  5480. */
  5481. spin_lock(&pers_lock);
  5482. *threadp = NULL;
  5483. spin_unlock(&pers_lock);
  5484. kthread_stop(thread->tsk);
  5485. kfree(thread);
  5486. }
  5487. void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
  5488. {
  5489. if (!mddev) {
  5490. MD_BUG();
  5491. return;
  5492. }
  5493. if (!rdev || test_bit(Faulty, &rdev->flags))
  5494. return;
  5495. if (mddev->external)
  5496. set_bit(Blocked, &rdev->flags);
  5497. /*
  5498. dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
  5499. mdname(mddev),
  5500. MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
  5501. __builtin_return_address(0),__builtin_return_address(1),
  5502. __builtin_return_address(2),__builtin_return_address(3));
  5503. */
  5504. if (!mddev->pers)
  5505. return;
  5506. if (!mddev->pers->error_handler)
  5507. return;
  5508. mddev->pers->error_handler(mddev,rdev);
  5509. if (mddev->degraded)
  5510. set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  5511. sysfs_notify_dirent_safe(rdev->sysfs_state);
  5512. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  5513. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  5514. md_wakeup_thread(mddev->thread);
  5515. if (mddev->event_work.func)
  5516. queue_work(md_misc_wq, &mddev->event_work);
  5517. md_new_event_inintr(mddev);
  5518. }
  5519. /* seq_file implementation /proc/mdstat */
  5520. static void status_unused(struct seq_file *seq)
  5521. {
  5522. int i = 0;
  5523. mdk_rdev_t *rdev;
  5524. seq_printf(seq, "unused devices: ");
  5525. list_for_each_entry(rdev, &pending_raid_disks, same_set) {
  5526. char b[BDEVNAME_SIZE];
  5527. i++;
  5528. seq_printf(seq, "%s ",
  5529. bdevname(rdev->bdev,b));
  5530. }
  5531. if (!i)
  5532. seq_printf(seq, "<none>");
  5533. seq_printf(seq, "\n");
  5534. }
  5535. static void status_resync(struct seq_file *seq, mddev_t * mddev)
  5536. {
  5537. sector_t max_sectors, resync, res;
  5538. unsigned long dt, db;
  5539. sector_t rt;
  5540. int scale;
  5541. unsigned int per_milli;
  5542. resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
  5543. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  5544. max_sectors = mddev->resync_max_sectors;
  5545. else
  5546. max_sectors = mddev->dev_sectors;
  5547. /*
  5548. * Should not happen.
  5549. */
  5550. if (!max_sectors) {
  5551. MD_BUG();
  5552. return;
  5553. }
  5554. /* Pick 'scale' such that (resync>>scale)*1000 will fit
  5555. * in a sector_t, and (max_sectors>>scale) will fit in a
  5556. * u32, as those are the requirements for sector_div.
  5557. * Thus 'scale' must be at least 10
  5558. */
  5559. scale = 10;
  5560. if (sizeof(sector_t) > sizeof(unsigned long)) {
  5561. while ( max_sectors/2 > (1ULL<<(scale+32)))
  5562. scale++;
  5563. }
  5564. res = (resync>>scale)*1000;
  5565. sector_div(res, (u32)((max_sectors>>scale)+1));
  5566. per_milli = res;
  5567. {
  5568. int i, x = per_milli/50, y = 20-x;
  5569. seq_printf(seq, "[");
  5570. for (i = 0; i < x; i++)
  5571. seq_printf(seq, "=");
  5572. seq_printf(seq, ">");
  5573. for (i = 0; i < y; i++)
  5574. seq_printf(seq, ".");
  5575. seq_printf(seq, "] ");
  5576. }
  5577. seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
  5578. (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
  5579. "reshape" :
  5580. (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
  5581. "check" :
  5582. (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
  5583. "resync" : "recovery"))),
  5584. per_milli/10, per_milli % 10,
  5585. (unsigned long long) resync/2,
  5586. (unsigned long long) max_sectors/2);
  5587. /*
  5588. * dt: time from mark until now
  5589. * db: blocks written from mark until now
  5590. * rt: remaining time
  5591. *
  5592. * rt is a sector_t, so could be 32bit or 64bit.
  5593. * So we divide before multiply in case it is 32bit and close
  5594. * to the limit.
  5595. * We scale the divisor (db) by 32 to avoid losing precision
  5596. * near the end of resync when the number of remaining sectors
  5597. * is close to 'db'.
  5598. * We then divide rt by 32 after multiplying by db to compensate.
  5599. * The '+1' avoids division by zero if db is very small.
  5600. */
  5601. dt = ((jiffies - mddev->resync_mark) / HZ);
  5602. if (!dt) dt++;
  5603. db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
  5604. - mddev->resync_mark_cnt;
  5605. rt = max_sectors - resync; /* number of remaining sectors */
  5606. sector_div(rt, db/32+1);
  5607. rt *= dt;
  5608. rt >>= 5;
  5609. seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
  5610. ((unsigned long)rt % 60)/6);
  5611. seq_printf(seq, " speed=%ldK/sec", db/2/dt);
  5612. }
  5613. static void *md_seq_start(struct seq_file *seq, loff_t *pos)
  5614. {
  5615. struct list_head *tmp;
  5616. loff_t l = *pos;
  5617. mddev_t *mddev;
  5618. if (l >= 0x10000)
  5619. return NULL;
  5620. if (!l--)
  5621. /* header */
  5622. return (void*)1;
  5623. spin_lock(&all_mddevs_lock);
  5624. list_for_each(tmp,&all_mddevs)
  5625. if (!l--) {
  5626. mddev = list_entry(tmp, mddev_t, all_mddevs);
  5627. mddev_get(mddev);
  5628. spin_unlock(&all_mddevs_lock);
  5629. return mddev;
  5630. }
  5631. spin_unlock(&all_mddevs_lock);
  5632. if (!l--)
  5633. return (void*)2;/* tail */
  5634. return NULL;
  5635. }
  5636. static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  5637. {
  5638. struct list_head *tmp;
  5639. mddev_t *next_mddev, *mddev = v;
  5640. ++*pos;
  5641. if (v == (void*)2)
  5642. return NULL;
  5643. spin_lock(&all_mddevs_lock);
  5644. if (v == (void*)1)
  5645. tmp = all_mddevs.next;
  5646. else
  5647. tmp = mddev->all_mddevs.next;
  5648. if (tmp != &all_mddevs)
  5649. next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
  5650. else {
  5651. next_mddev = (void*)2;
  5652. *pos = 0x10000;
  5653. }
  5654. spin_unlock(&all_mddevs_lock);
  5655. if (v != (void*)1)
  5656. mddev_put(mddev);
  5657. return next_mddev;
  5658. }
  5659. static void md_seq_stop(struct seq_file *seq, void *v)
  5660. {
  5661. mddev_t *mddev = v;
  5662. if (mddev && v != (void*)1 && v != (void*)2)
  5663. mddev_put(mddev);
  5664. }
  5665. struct mdstat_info {
  5666. int event;
  5667. };
  5668. static int md_seq_show(struct seq_file *seq, void *v)
  5669. {
  5670. mddev_t *mddev = v;
  5671. sector_t sectors;
  5672. mdk_rdev_t *rdev;
  5673. struct mdstat_info *mi = seq->private;
  5674. struct bitmap *bitmap;
  5675. if (v == (void*)1) {
  5676. struct mdk_personality *pers;
  5677. seq_printf(seq, "Personalities : ");
  5678. spin_lock(&pers_lock);
  5679. list_for_each_entry(pers, &pers_list, list)
  5680. seq_printf(seq, "[%s] ", pers->name);
  5681. spin_unlock(&pers_lock);
  5682. seq_printf(seq, "\n");
  5683. mi->event = atomic_read(&md_event_count);
  5684. return 0;
  5685. }
  5686. if (v == (void*)2) {
  5687. status_unused(seq);
  5688. return 0;
  5689. }
  5690. if (mddev_lock(mddev) < 0)
  5691. return -EINTR;
  5692. if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
  5693. seq_printf(seq, "%s : %sactive", mdname(mddev),
  5694. mddev->pers ? "" : "in");
  5695. if (mddev->pers) {
  5696. if (mddev->ro==1)
  5697. seq_printf(seq, " (read-only)");
  5698. if (mddev->ro==2)
  5699. seq_printf(seq, " (auto-read-only)");
  5700. seq_printf(seq, " %s", mddev->pers->name);
  5701. }
  5702. sectors = 0;
  5703. list_for_each_entry(rdev, &mddev->disks, same_set) {
  5704. char b[BDEVNAME_SIZE];
  5705. seq_printf(seq, " %s[%d]",
  5706. bdevname(rdev->bdev,b), rdev->desc_nr);
  5707. if (test_bit(WriteMostly, &rdev->flags))
  5708. seq_printf(seq, "(W)");
  5709. if (test_bit(Faulty, &rdev->flags)) {
  5710. seq_printf(seq, "(F)");
  5711. continue;
  5712. } else if (rdev->raid_disk < 0)
  5713. seq_printf(seq, "(S)"); /* spare */
  5714. sectors += rdev->sectors;
  5715. }
  5716. if (!list_empty(&mddev->disks)) {
  5717. if (mddev->pers)
  5718. seq_printf(seq, "\n %llu blocks",
  5719. (unsigned long long)
  5720. mddev->array_sectors / 2);
  5721. else
  5722. seq_printf(seq, "\n %llu blocks",
  5723. (unsigned long long)sectors / 2);
  5724. }
  5725. if (mddev->persistent) {
  5726. if (mddev->major_version != 0 ||
  5727. mddev->minor_version != 90) {
  5728. seq_printf(seq," super %d.%d",
  5729. mddev->major_version,
  5730. mddev->minor_version);
  5731. }
  5732. } else if (mddev->external)
  5733. seq_printf(seq, " super external:%s",
  5734. mddev->metadata_type);
  5735. else
  5736. seq_printf(seq, " super non-persistent");
  5737. if (mddev->pers) {
  5738. mddev->pers->status(seq, mddev);
  5739. seq_printf(seq, "\n ");
  5740. if (mddev->pers->sync_request) {
  5741. if (mddev->curr_resync > 2) {
  5742. status_resync(seq, mddev);
  5743. seq_printf(seq, "\n ");
  5744. } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
  5745. seq_printf(seq, "\tresync=DELAYED\n ");
  5746. else if (mddev->recovery_cp < MaxSector)
  5747. seq_printf(seq, "\tresync=PENDING\n ");
  5748. }
  5749. } else
  5750. seq_printf(seq, "\n ");
  5751. if ((bitmap = mddev->bitmap)) {
  5752. unsigned long chunk_kb;
  5753. unsigned long flags;
  5754. spin_lock_irqsave(&bitmap->lock, flags);
  5755. chunk_kb = mddev->bitmap_info.chunksize >> 10;
  5756. seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
  5757. "%lu%s chunk",
  5758. bitmap->pages - bitmap->missing_pages,
  5759. bitmap->pages,
  5760. (bitmap->pages - bitmap->missing_pages)
  5761. << (PAGE_SHIFT - 10),
  5762. chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
  5763. chunk_kb ? "KB" : "B");
  5764. if (bitmap->file) {
  5765. seq_printf(seq, ", file: ");
  5766. seq_path(seq, &bitmap->file->f_path, " \t\n");
  5767. }
  5768. seq_printf(seq, "\n");
  5769. spin_unlock_irqrestore(&bitmap->lock, flags);
  5770. }
  5771. seq_printf(seq, "\n");
  5772. }
  5773. mddev_unlock(mddev);
  5774. return 0;
  5775. }
  5776. static const struct seq_operations md_seq_ops = {
  5777. .start = md_seq_start,
  5778. .next = md_seq_next,
  5779. .stop = md_seq_stop,
  5780. .show = md_seq_show,
  5781. };
  5782. static int md_seq_open(struct inode *inode, struct file *file)
  5783. {
  5784. int error;
  5785. struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
  5786. if (mi == NULL)
  5787. return -ENOMEM;
  5788. error = seq_open(file, &md_seq_ops);
  5789. if (error)
  5790. kfree(mi);
  5791. else {
  5792. struct seq_file *p = file->private_data;
  5793. p->private = mi;
  5794. mi->event = atomic_read(&md_event_count);
  5795. }
  5796. return error;
  5797. }
  5798. static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
  5799. {
  5800. struct seq_file *m = filp->private_data;
  5801. struct mdstat_info *mi = m->private;
  5802. int mask;
  5803. poll_wait(filp, &md_event_waiters, wait);
  5804. /* always allow read */
  5805. mask = POLLIN | POLLRDNORM;
  5806. if (mi->event != atomic_read(&md_event_count))
  5807. mask |= POLLERR | POLLPRI;
  5808. return mask;
  5809. }
  5810. static const struct file_operations md_seq_fops = {
  5811. .owner = THIS_MODULE,
  5812. .open = md_seq_open,
  5813. .read = seq_read,
  5814. .llseek = seq_lseek,
  5815. .release = seq_release_private,
  5816. .poll = mdstat_poll,
  5817. };
  5818. int register_md_personality(struct mdk_personality *p)
  5819. {
  5820. spin_lock(&pers_lock);
  5821. list_add_tail(&p->list, &pers_list);
  5822. printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
  5823. spin_unlock(&pers_lock);
  5824. return 0;
  5825. }
  5826. int unregister_md_personality(struct mdk_personality *p)
  5827. {
  5828. printk(KERN_INFO "md: %s personality unregistered\n", p->name);
  5829. spin_lock(&pers_lock);
  5830. list_del_init(&p->list);
  5831. spin_unlock(&pers_lock);
  5832. return 0;
  5833. }
  5834. static int is_mddev_idle(mddev_t *mddev, int init)
  5835. {
  5836. mdk_rdev_t * rdev;
  5837. int idle;
  5838. int curr_events;
  5839. idle = 1;
  5840. rcu_read_lock();
  5841. rdev_for_each_rcu(rdev, mddev) {
  5842. struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
  5843. curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
  5844. (int)part_stat_read(&disk->part0, sectors[1]) -
  5845. atomic_read(&disk->sync_io);
  5846. /* sync IO will cause sync_io to increase before the disk_stats
  5847. * as sync_io is counted when a request starts, and
  5848. * disk_stats is counted when it completes.
  5849. * So resync activity will cause curr_events to be smaller than
  5850. * when there was no such activity.
  5851. * non-sync IO will cause disk_stat to increase without
  5852. * increasing sync_io so curr_events will (eventually)
  5853. * be larger than it was before. Once it becomes
  5854. * substantially larger, the test below will cause
  5855. * the array to appear non-idle, and resync will slow
  5856. * down.
  5857. * If there is a lot of outstanding resync activity when
  5858. * we set last_event to curr_events, then all that activity
  5859. * completing might cause the array to appear non-idle
  5860. * and resync will be slowed down even though there might
  5861. * not have been non-resync activity. This will only
  5862. * happen once though. 'last_events' will soon reflect
  5863. * the state where there is little or no outstanding
  5864. * resync requests, and further resync activity will
  5865. * always make curr_events less than last_events.
  5866. *
  5867. */
  5868. if (init || curr_events - rdev->last_events > 64) {
  5869. rdev->last_events = curr_events;
  5870. idle = 0;
  5871. }
  5872. }
  5873. rcu_read_unlock();
  5874. return idle;
  5875. }
  5876. void md_done_sync(mddev_t *mddev, int blocks, int ok)
  5877. {
  5878. /* another "blocks" (512byte) blocks have been synced */
  5879. atomic_sub(blocks, &mddev->recovery_active);
  5880. wake_up(&mddev->recovery_wait);
  5881. if (!ok) {
  5882. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  5883. md_wakeup_thread(mddev->thread);
  5884. // stop recovery, signal do_sync ....
  5885. }
  5886. }
  5887. /* md_write_start(mddev, bi)
  5888. * If we need to update some array metadata (e.g. 'active' flag
  5889. * in superblock) before writing, schedule a superblock update
  5890. * and wait for it to complete.
  5891. */
  5892. void md_write_start(mddev_t *mddev, struct bio *bi)
  5893. {
  5894. int did_change = 0;
  5895. if (bio_data_dir(bi) != WRITE)
  5896. return;
  5897. BUG_ON(mddev->ro == 1);
  5898. if (mddev->ro == 2) {
  5899. /* need to switch to read/write */
  5900. mddev->ro = 0;
  5901. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  5902. md_wakeup_thread(mddev->thread);
  5903. md_wakeup_thread(mddev->sync_thread);
  5904. did_change = 1;
  5905. }
  5906. atomic_inc(&mddev->writes_pending);
  5907. if (mddev->safemode == 1)
  5908. mddev->safemode = 0;
  5909. if (mddev->in_sync) {
  5910. spin_lock_irq(&mddev->write_lock);
  5911. if (mddev->in_sync) {
  5912. mddev->in_sync = 0;
  5913. set_bit(MD_CHANGE_CLEAN, &mddev->flags);
  5914. set_bit(MD_CHANGE_PENDING, &mddev->flags);
  5915. md_wakeup_thread(mddev->thread);
  5916. did_change = 1;
  5917. }
  5918. spin_unlock_irq(&mddev->write_lock);
  5919. }
  5920. if (did_change)
  5921. sysfs_notify_dirent_safe(mddev->sysfs_state);
  5922. wait_event(mddev->sb_wait,
  5923. !test_bit(MD_CHANGE_PENDING, &mddev->flags));
  5924. }
  5925. void md_write_end(mddev_t *mddev)
  5926. {
  5927. if (atomic_dec_and_test(&mddev->writes_pending)) {
  5928. if (mddev->safemode == 2)
  5929. md_wakeup_thread(mddev->thread);
  5930. else if (mddev->safemode_delay)
  5931. mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
  5932. }
  5933. }
  5934. /* md_allow_write(mddev)
  5935. * Calling this ensures that the array is marked 'active' so that writes
  5936. * may proceed without blocking. It is important to call this before
  5937. * attempting a GFP_KERNEL allocation while holding the mddev lock.
  5938. * Must be called with mddev_lock held.
  5939. *
  5940. * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
  5941. * is dropped, so return -EAGAIN after notifying userspace.
  5942. */
  5943. int md_allow_write(mddev_t *mddev)
  5944. {
  5945. if (!mddev->pers)
  5946. return 0;
  5947. if (mddev->ro)
  5948. return 0;
  5949. if (!mddev->pers->sync_request)
  5950. return 0;
  5951. spin_lock_irq(&mddev->write_lock);
  5952. if (mddev->in_sync) {
  5953. mddev->in_sync = 0;
  5954. set_bit(MD_CHANGE_CLEAN, &mddev->flags);
  5955. set_bit(MD_CHANGE_PENDING, &mddev->flags);
  5956. if (mddev->safemode_delay &&
  5957. mddev->safemode == 0)
  5958. mddev->safemode = 1;
  5959. spin_unlock_irq(&mddev->write_lock);
  5960. md_update_sb(mddev, 0);
  5961. sysfs_notify_dirent_safe(mddev->sysfs_state);
  5962. } else
  5963. spin_unlock_irq(&mddev->write_lock);
  5964. if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
  5965. return -EAGAIN;
  5966. else
  5967. return 0;
  5968. }
  5969. EXPORT_SYMBOL_GPL(md_allow_write);
  5970. #define SYNC_MARKS 10
  5971. #define SYNC_MARK_STEP (3*HZ)
  5972. void md_do_sync(mddev_t *mddev)
  5973. {
  5974. mddev_t *mddev2;
  5975. unsigned int currspeed = 0,
  5976. window;
  5977. sector_t max_sectors,j, io_sectors;
  5978. unsigned long mark[SYNC_MARKS];
  5979. sector_t mark_cnt[SYNC_MARKS];
  5980. int last_mark,m;
  5981. struct list_head *tmp;
  5982. sector_t last_check;
  5983. int skipped = 0;
  5984. mdk_rdev_t *rdev;
  5985. char *desc;
  5986. /* just incase thread restarts... */
  5987. if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
  5988. return;
  5989. if (mddev->ro) /* never try to sync a read-only array */
  5990. return;
  5991. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  5992. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  5993. desc = "data-check";
  5994. else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  5995. desc = "requested-resync";
  5996. else
  5997. desc = "resync";
  5998. } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  5999. desc = "reshape";
  6000. else
  6001. desc = "recovery";
  6002. /* we overload curr_resync somewhat here.
  6003. * 0 == not engaged in resync at all
  6004. * 2 == checking that there is no conflict with another sync
  6005. * 1 == like 2, but have yielded to allow conflicting resync to
  6006. * commense
  6007. * other == active in resync - this many blocks
  6008. *
  6009. * Before starting a resync we must have set curr_resync to
  6010. * 2, and then checked that every "conflicting" array has curr_resync
  6011. * less than ours. When we find one that is the same or higher
  6012. * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
  6013. * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
  6014. * This will mean we have to start checking from the beginning again.
  6015. *
  6016. */
  6017. do {
  6018. mddev->curr_resync = 2;
  6019. try_again:
  6020. if (kthread_should_stop())
  6021. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  6022. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  6023. goto skip;
  6024. for_each_mddev(mddev2, tmp) {
  6025. if (mddev2 == mddev)
  6026. continue;
  6027. if (!mddev->parallel_resync
  6028. && mddev2->curr_resync
  6029. && match_mddev_units(mddev, mddev2)) {
  6030. DEFINE_WAIT(wq);
  6031. if (mddev < mddev2 && mddev->curr_resync == 2) {
  6032. /* arbitrarily yield */
  6033. mddev->curr_resync = 1;
  6034. wake_up(&resync_wait);
  6035. }
  6036. if (mddev > mddev2 && mddev->curr_resync == 1)
  6037. /* no need to wait here, we can wait the next
  6038. * time 'round when curr_resync == 2
  6039. */
  6040. continue;
  6041. /* We need to wait 'interruptible' so as not to
  6042. * contribute to the load average, and not to
  6043. * be caught by 'softlockup'
  6044. */
  6045. prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
  6046. if (!kthread_should_stop() &&
  6047. mddev2->curr_resync >= mddev->curr_resync) {
  6048. printk(KERN_INFO "md: delaying %s of %s"
  6049. " until %s has finished (they"
  6050. " share one or more physical units)\n",
  6051. desc, mdname(mddev), mdname(mddev2));
  6052. mddev_put(mddev2);
  6053. if (signal_pending(current))
  6054. flush_signals(current);
  6055. schedule();
  6056. finish_wait(&resync_wait, &wq);
  6057. goto try_again;
  6058. }
  6059. finish_wait(&resync_wait, &wq);
  6060. }
  6061. }
  6062. } while (mddev->curr_resync < 2);
  6063. j = 0;
  6064. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  6065. /* resync follows the size requested by the personality,
  6066. * which defaults to physical size, but can be virtual size
  6067. */
  6068. max_sectors = mddev->resync_max_sectors;
  6069. mddev->resync_mismatches = 0;
  6070. /* we don't use the checkpoint if there's a bitmap */
  6071. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  6072. j = mddev->resync_min;
  6073. else if (!mddev->bitmap)
  6074. j = mddev->recovery_cp;
  6075. } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  6076. max_sectors = mddev->dev_sectors;
  6077. else {
  6078. /* recovery follows the physical size of devices */
  6079. max_sectors = mddev->dev_sectors;
  6080. j = MaxSector;
  6081. rcu_read_lock();
  6082. list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
  6083. if (rdev->raid_disk >= 0 &&
  6084. !test_bit(Faulty, &rdev->flags) &&
  6085. !test_bit(In_sync, &rdev->flags) &&
  6086. rdev->recovery_offset < j)
  6087. j = rdev->recovery_offset;
  6088. rcu_read_unlock();
  6089. }
  6090. printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
  6091. printk(KERN_INFO "md: minimum _guaranteed_ speed:"
  6092. " %d KB/sec/disk.\n", speed_min(mddev));
  6093. printk(KERN_INFO "md: using maximum available idle IO bandwidth "
  6094. "(but not more than %d KB/sec) for %s.\n",
  6095. speed_max(mddev), desc);
  6096. is_mddev_idle(mddev, 1); /* this initializes IO event counters */
  6097. io_sectors = 0;
  6098. for (m = 0; m < SYNC_MARKS; m++) {
  6099. mark[m] = jiffies;
  6100. mark_cnt[m] = io_sectors;
  6101. }
  6102. last_mark = 0;
  6103. mddev->resync_mark = mark[last_mark];
  6104. mddev->resync_mark_cnt = mark_cnt[last_mark];
  6105. /*
  6106. * Tune reconstruction:
  6107. */
  6108. window = 32*(PAGE_SIZE/512);
  6109. printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
  6110. window/2, (unsigned long long)max_sectors/2);
  6111. atomic_set(&mddev->recovery_active, 0);
  6112. last_check = 0;
  6113. if (j>2) {
  6114. printk(KERN_INFO
  6115. "md: resuming %s of %s from checkpoint.\n",
  6116. desc, mdname(mddev));
  6117. mddev->curr_resync = j;
  6118. }
  6119. mddev->curr_resync_completed = j;
  6120. while (j < max_sectors) {
  6121. sector_t sectors;
  6122. skipped = 0;
  6123. if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  6124. ((mddev->curr_resync > mddev->curr_resync_completed &&
  6125. (mddev->curr_resync - mddev->curr_resync_completed)
  6126. > (max_sectors >> 4)) ||
  6127. (j - mddev->curr_resync_completed)*2
  6128. >= mddev->resync_max - mddev->curr_resync_completed
  6129. )) {
  6130. /* time to update curr_resync_completed */
  6131. wait_event(mddev->recovery_wait,
  6132. atomic_read(&mddev->recovery_active) == 0);
  6133. mddev->curr_resync_completed = j;
  6134. set_bit(MD_CHANGE_CLEAN, &mddev->flags);
  6135. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  6136. }
  6137. while (j >= mddev->resync_max && !kthread_should_stop()) {
  6138. /* As this condition is controlled by user-space,
  6139. * we can block indefinitely, so use '_interruptible'
  6140. * to avoid triggering warnings.
  6141. */
  6142. flush_signals(current); /* just in case */
  6143. wait_event_interruptible(mddev->recovery_wait,
  6144. mddev->resync_max > j
  6145. || kthread_should_stop());
  6146. }
  6147. if (kthread_should_stop())
  6148. goto interrupted;
  6149. sectors = mddev->pers->sync_request(mddev, j, &skipped,
  6150. currspeed < speed_min(mddev));
  6151. if (sectors == 0) {
  6152. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  6153. goto out;
  6154. }
  6155. if (!skipped) { /* actual IO requested */
  6156. io_sectors += sectors;
  6157. atomic_add(sectors, &mddev->recovery_active);
  6158. }
  6159. j += sectors;
  6160. if (j>1) mddev->curr_resync = j;
  6161. mddev->curr_mark_cnt = io_sectors;
  6162. if (last_check == 0)
  6163. /* this is the earliers that rebuilt will be
  6164. * visible in /proc/mdstat
  6165. */
  6166. md_new_event(mddev);
  6167. if (last_check + window > io_sectors || j == max_sectors)
  6168. continue;
  6169. last_check = io_sectors;
  6170. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  6171. break;
  6172. repeat:
  6173. if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
  6174. /* step marks */
  6175. int next = (last_mark+1) % SYNC_MARKS;
  6176. mddev->resync_mark = mark[next];
  6177. mddev->resync_mark_cnt = mark_cnt[next];
  6178. mark[next] = jiffies;
  6179. mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
  6180. last_mark = next;
  6181. }
  6182. if (kthread_should_stop())
  6183. goto interrupted;
  6184. /*
  6185. * this loop exits only if either when we are slower than
  6186. * the 'hard' speed limit, or the system was IO-idle for
  6187. * a jiffy.
  6188. * the system might be non-idle CPU-wise, but we only care
  6189. * about not overloading the IO subsystem. (things like an
  6190. * e2fsck being done on the RAID array should execute fast)
  6191. */
  6192. cond_resched();
  6193. currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
  6194. /((jiffies-mddev->resync_mark)/HZ +1) +1;
  6195. if (currspeed > speed_min(mddev)) {
  6196. if ((currspeed > speed_max(mddev)) ||
  6197. !is_mddev_idle(mddev, 0)) {
  6198. msleep(500);
  6199. goto repeat;
  6200. }
  6201. }
  6202. }
  6203. printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
  6204. /*
  6205. * this also signals 'finished resyncing' to md_stop
  6206. */
  6207. out:
  6208. wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
  6209. /* tell personality that we are finished */
  6210. mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
  6211. if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
  6212. mddev->curr_resync > 2) {
  6213. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  6214. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  6215. if (mddev->curr_resync >= mddev->recovery_cp) {
  6216. printk(KERN_INFO
  6217. "md: checkpointing %s of %s.\n",
  6218. desc, mdname(mddev));
  6219. mddev->recovery_cp = mddev->curr_resync;
  6220. }
  6221. } else
  6222. mddev->recovery_cp = MaxSector;
  6223. } else {
  6224. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  6225. mddev->curr_resync = MaxSector;
  6226. rcu_read_lock();
  6227. list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
  6228. if (rdev->raid_disk >= 0 &&
  6229. mddev->delta_disks >= 0 &&
  6230. !test_bit(Faulty, &rdev->flags) &&
  6231. !test_bit(In_sync, &rdev->flags) &&
  6232. rdev->recovery_offset < mddev->curr_resync)
  6233. rdev->recovery_offset = mddev->curr_resync;
  6234. rcu_read_unlock();
  6235. }
  6236. }
  6237. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  6238. skip:
  6239. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  6240. /* We completed so min/max setting can be forgotten if used. */
  6241. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  6242. mddev->resync_min = 0;
  6243. mddev->resync_max = MaxSector;
  6244. } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  6245. mddev->resync_min = mddev->curr_resync_completed;
  6246. mddev->curr_resync = 0;
  6247. wake_up(&resync_wait);
  6248. set_bit(MD_RECOVERY_DONE, &mddev->recovery);
  6249. md_wakeup_thread(mddev->thread);
  6250. return;
  6251. interrupted:
  6252. /*
  6253. * got a signal, exit.
  6254. */
  6255. printk(KERN_INFO
  6256. "md: md_do_sync() got signal ... exiting\n");
  6257. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  6258. goto out;
  6259. }
  6260. EXPORT_SYMBOL_GPL(md_do_sync);
  6261. static int remove_and_add_spares(mddev_t *mddev)
  6262. {
  6263. mdk_rdev_t *rdev;
  6264. int spares = 0;
  6265. mddev->curr_resync_completed = 0;
  6266. list_for_each_entry(rdev, &mddev->disks, same_set)
  6267. if (rdev->raid_disk >= 0 &&
  6268. !test_bit(Blocked, &rdev->flags) &&
  6269. (test_bit(Faulty, &rdev->flags) ||
  6270. ! test_bit(In_sync, &rdev->flags)) &&
  6271. atomic_read(&rdev->nr_pending)==0) {
  6272. if (mddev->pers->hot_remove_disk(
  6273. mddev, rdev->raid_disk)==0) {
  6274. char nm[20];
  6275. sprintf(nm,"rd%d", rdev->raid_disk);
  6276. sysfs_remove_link(&mddev->kobj, nm);
  6277. rdev->raid_disk = -1;
  6278. }
  6279. }
  6280. if (mddev->degraded && !mddev->recovery_disabled) {
  6281. list_for_each_entry(rdev, &mddev->disks, same_set) {
  6282. if (rdev->raid_disk >= 0 &&
  6283. !test_bit(In_sync, &rdev->flags) &&
  6284. !test_bit(Faulty, &rdev->flags) &&
  6285. !test_bit(Blocked, &rdev->flags))
  6286. spares++;
  6287. if (rdev->raid_disk < 0
  6288. && !test_bit(Faulty, &rdev->flags)) {
  6289. rdev->recovery_offset = 0;
  6290. if (mddev->pers->
  6291. hot_add_disk(mddev, rdev) == 0) {
  6292. char nm[20];
  6293. sprintf(nm, "rd%d", rdev->raid_disk);
  6294. if (sysfs_create_link(&mddev->kobj,
  6295. &rdev->kobj, nm))
  6296. /* failure here is OK */;
  6297. spares++;
  6298. md_new_event(mddev);
  6299. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  6300. } else
  6301. break;
  6302. }
  6303. }
  6304. }
  6305. return spares;
  6306. }
  6307. static void reap_sync_thread(mddev_t *mddev)
  6308. {
  6309. mdk_rdev_t *rdev;
  6310. /* resync has finished, collect result */
  6311. md_unregister_thread(&mddev->sync_thread);
  6312. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
  6313. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  6314. /* success...*/
  6315. /* activate any spares */
  6316. if (mddev->pers->spare_active(mddev))
  6317. sysfs_notify(&mddev->kobj, NULL,
  6318. "degraded");
  6319. }
  6320. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  6321. mddev->pers->finish_reshape)
  6322. mddev->pers->finish_reshape(mddev);
  6323. md_update_sb(mddev, 1);
  6324. /* if array is no-longer degraded, then any saved_raid_disk
  6325. * information must be scrapped
  6326. */
  6327. if (!mddev->degraded)
  6328. list_for_each_entry(rdev, &mddev->disks, same_set)
  6329. rdev->saved_raid_disk = -1;
  6330. clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  6331. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  6332. clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  6333. clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
  6334. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  6335. /* flag recovery needed just to double check */
  6336. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  6337. sysfs_notify_dirent_safe(mddev->sysfs_action);
  6338. md_new_event(mddev);
  6339. }
  6340. /*
  6341. * This routine is regularly called by all per-raid-array threads to
  6342. * deal with generic issues like resync and super-block update.
  6343. * Raid personalities that don't have a thread (linear/raid0) do not
  6344. * need this as they never do any recovery or update the superblock.
  6345. *
  6346. * It does not do any resync itself, but rather "forks" off other threads
  6347. * to do that as needed.
  6348. * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
  6349. * "->recovery" and create a thread at ->sync_thread.
  6350. * When the thread finishes it sets MD_RECOVERY_DONE
  6351. * and wakeups up this thread which will reap the thread and finish up.
  6352. * This thread also removes any faulty devices (with nr_pending == 0).
  6353. *
  6354. * The overall approach is:
  6355. * 1/ if the superblock needs updating, update it.
  6356. * 2/ If a recovery thread is running, don't do anything else.
  6357. * 3/ If recovery has finished, clean up, possibly marking spares active.
  6358. * 4/ If there are any faulty devices, remove them.
  6359. * 5/ If array is degraded, try to add spares devices
  6360. * 6/ If array has spares or is not in-sync, start a resync thread.
  6361. */
  6362. void md_check_recovery(mddev_t *mddev)
  6363. {
  6364. if (mddev->suspended)
  6365. return;
  6366. if (mddev->bitmap)
  6367. bitmap_daemon_work(mddev);
  6368. if (mddev->ro)
  6369. return;
  6370. if (signal_pending(current)) {
  6371. if (mddev->pers->sync_request && !mddev->external) {
  6372. printk(KERN_INFO "md: %s in immediate safe mode\n",
  6373. mdname(mddev));
  6374. mddev->safemode = 2;
  6375. }
  6376. flush_signals(current);
  6377. }
  6378. if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
  6379. return;
  6380. if ( ! (
  6381. (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
  6382. test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
  6383. test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
  6384. (mddev->external == 0 && mddev->safemode == 1) ||
  6385. (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
  6386. && !mddev->in_sync && mddev->recovery_cp == MaxSector)
  6387. ))
  6388. return;
  6389. if (mddev_trylock(mddev)) {
  6390. int spares = 0;
  6391. if (mddev->ro) {
  6392. /* Only thing we do on a ro array is remove
  6393. * failed devices.
  6394. */
  6395. mdk_rdev_t *rdev;
  6396. list_for_each_entry(rdev, &mddev->disks, same_set)
  6397. if (rdev->raid_disk >= 0 &&
  6398. !test_bit(Blocked, &rdev->flags) &&
  6399. test_bit(Faulty, &rdev->flags) &&
  6400. atomic_read(&rdev->nr_pending)==0) {
  6401. if (mddev->pers->hot_remove_disk(
  6402. mddev, rdev->raid_disk)==0) {
  6403. char nm[20];
  6404. sprintf(nm,"rd%d", rdev->raid_disk);
  6405. sysfs_remove_link(&mddev->kobj, nm);
  6406. rdev->raid_disk = -1;
  6407. }
  6408. }
  6409. clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  6410. goto unlock;
  6411. }
  6412. if (!mddev->external) {
  6413. int did_change = 0;
  6414. spin_lock_irq(&mddev->write_lock);
  6415. if (mddev->safemode &&
  6416. !atomic_read(&mddev->writes_pending) &&
  6417. !mddev->in_sync &&
  6418. mddev->recovery_cp == MaxSector) {
  6419. mddev->in_sync = 1;
  6420. did_change = 1;
  6421. set_bit(MD_CHANGE_CLEAN, &mddev->flags);
  6422. }
  6423. if (mddev->safemode == 1)
  6424. mddev->safemode = 0;
  6425. spin_unlock_irq(&mddev->write_lock);
  6426. if (did_change)
  6427. sysfs_notify_dirent_safe(mddev->sysfs_state);
  6428. }
  6429. if (mddev->flags)
  6430. md_update_sb(mddev, 0);
  6431. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
  6432. !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
  6433. /* resync/recovery still happening */
  6434. clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  6435. goto unlock;
  6436. }
  6437. if (mddev->sync_thread) {
  6438. reap_sync_thread(mddev);
  6439. goto unlock;
  6440. }
  6441. /* Set RUNNING before clearing NEEDED to avoid
  6442. * any transients in the value of "sync_action".
  6443. */
  6444. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  6445. clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  6446. /* Clear some bits that don't mean anything, but
  6447. * might be left set
  6448. */
  6449. clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
  6450. clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
  6451. if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
  6452. goto unlock;
  6453. /* no recovery is running.
  6454. * remove any failed drives, then
  6455. * add spares if possible.
  6456. * Spare are also removed and re-added, to allow
  6457. * the personality to fail the re-add.
  6458. */
  6459. if (mddev->reshape_position != MaxSector) {
  6460. if (mddev->pers->check_reshape == NULL ||
  6461. mddev->pers->check_reshape(mddev) != 0)
  6462. /* Cannot proceed */
  6463. goto unlock;
  6464. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  6465. clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  6466. } else if ((spares = remove_and_add_spares(mddev))) {
  6467. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  6468. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  6469. clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
  6470. set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  6471. } else if (mddev->recovery_cp < MaxSector) {
  6472. set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  6473. clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  6474. } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  6475. /* nothing to be done ... */
  6476. goto unlock;
  6477. if (mddev->pers->sync_request) {
  6478. if (spares && mddev->bitmap && ! mddev->bitmap->file) {
  6479. /* We are adding a device or devices to an array
  6480. * which has the bitmap stored on all devices.
  6481. * So make sure all bitmap pages get written
  6482. */
  6483. bitmap_write_all(mddev->bitmap);
  6484. }
  6485. mddev->sync_thread = md_register_thread(md_do_sync,
  6486. mddev,
  6487. "resync");
  6488. if (!mddev->sync_thread) {
  6489. printk(KERN_ERR "%s: could not start resync"
  6490. " thread...\n",
  6491. mdname(mddev));
  6492. /* leave the spares where they are, it shouldn't hurt */
  6493. clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  6494. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  6495. clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  6496. clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
  6497. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  6498. } else
  6499. md_wakeup_thread(mddev->sync_thread);
  6500. sysfs_notify_dirent_safe(mddev->sysfs_action);
  6501. md_new_event(mddev);
  6502. }
  6503. unlock:
  6504. if (!mddev->sync_thread) {
  6505. clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  6506. if (test_and_clear_bit(MD_RECOVERY_RECOVER,
  6507. &mddev->recovery))
  6508. if (mddev->sysfs_action)
  6509. sysfs_notify_dirent_safe(mddev->sysfs_action);
  6510. }
  6511. mddev_unlock(mddev);
  6512. }
  6513. }
  6514. void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  6515. {
  6516. sysfs_notify_dirent_safe(rdev->sysfs_state);
  6517. wait_event_timeout(rdev->blocked_wait,
  6518. !test_bit(Blocked, &rdev->flags),
  6519. msecs_to_jiffies(5000));
  6520. rdev_dec_pending(rdev, mddev);
  6521. }
  6522. EXPORT_SYMBOL(md_wait_for_blocked_rdev);
  6523. static int md_notify_reboot(struct notifier_block *this,
  6524. unsigned long code, void *x)
  6525. {
  6526. struct list_head *tmp;
  6527. mddev_t *mddev;
  6528. if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
  6529. printk(KERN_INFO "md: stopping all md devices.\n");
  6530. for_each_mddev(mddev, tmp)
  6531. if (mddev_trylock(mddev)) {
  6532. /* Force a switch to readonly even array
  6533. * appears to still be in use. Hence
  6534. * the '100'.
  6535. */
  6536. md_set_readonly(mddev, 100);
  6537. mddev_unlock(mddev);
  6538. }
  6539. /*
  6540. * certain more exotic SCSI devices are known to be
  6541. * volatile wrt too early system reboots. While the
  6542. * right place to handle this issue is the given
  6543. * driver, we do want to have a safe RAID driver ...
  6544. */
  6545. mdelay(1000*1);
  6546. }
  6547. return NOTIFY_DONE;
  6548. }
  6549. static struct notifier_block md_notifier = {
  6550. .notifier_call = md_notify_reboot,
  6551. .next = NULL,
  6552. .priority = INT_MAX, /* before any real devices */
  6553. };
  6554. static void md_geninit(void)
  6555. {
  6556. dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
  6557. proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
  6558. }
  6559. static int __init md_init(void)
  6560. {
  6561. int ret = -ENOMEM;
  6562. md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
  6563. if (!md_wq)
  6564. goto err_wq;
  6565. md_misc_wq = alloc_workqueue("md_misc", 0, 0);
  6566. if (!md_misc_wq)
  6567. goto err_misc_wq;
  6568. if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
  6569. goto err_md;
  6570. if ((ret = register_blkdev(0, "mdp")) < 0)
  6571. goto err_mdp;
  6572. mdp_major = ret;
  6573. blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
  6574. md_probe, NULL, NULL);
  6575. blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
  6576. md_probe, NULL, NULL);
  6577. register_reboot_notifier(&md_notifier);
  6578. raid_table_header = register_sysctl_table(raid_root_table);
  6579. md_geninit();
  6580. return 0;
  6581. err_mdp:
  6582. unregister_blkdev(MD_MAJOR, "md");
  6583. err_md:
  6584. destroy_workqueue(md_misc_wq);
  6585. err_misc_wq:
  6586. destroy_workqueue(md_wq);
  6587. err_wq:
  6588. return ret;
  6589. }
  6590. #ifndef MODULE
  6591. /*
  6592. * Searches all registered partitions for autorun RAID arrays
  6593. * at boot time.
  6594. */
  6595. static LIST_HEAD(all_detected_devices);
  6596. struct detected_devices_node {
  6597. struct list_head list;
  6598. dev_t dev;
  6599. };
  6600. void md_autodetect_dev(dev_t dev)
  6601. {
  6602. struct detected_devices_node *node_detected_dev;
  6603. node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
  6604. if (node_detected_dev) {
  6605. node_detected_dev->dev = dev;
  6606. list_add_tail(&node_detected_dev->list, &all_detected_devices);
  6607. } else {
  6608. printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
  6609. ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
  6610. }
  6611. }
  6612. static void autostart_arrays(int part)
  6613. {
  6614. mdk_rdev_t *rdev;
  6615. struct detected_devices_node *node_detected_dev;
  6616. dev_t dev;
  6617. int i_scanned, i_passed;
  6618. i_scanned = 0;
  6619. i_passed = 0;
  6620. printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
  6621. while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
  6622. i_scanned++;
  6623. node_detected_dev = list_entry(all_detected_devices.next,
  6624. struct detected_devices_node, list);
  6625. list_del(&node_detected_dev->list);
  6626. dev = node_detected_dev->dev;
  6627. kfree(node_detected_dev);
  6628. rdev = md_import_device(dev,0, 90);
  6629. if (IS_ERR(rdev))
  6630. continue;
  6631. if (test_bit(Faulty, &rdev->flags)) {
  6632. MD_BUG();
  6633. continue;
  6634. }
  6635. set_bit(AutoDetected, &rdev->flags);
  6636. list_add(&rdev->same_set, &pending_raid_disks);
  6637. i_passed++;
  6638. }
  6639. printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
  6640. i_scanned, i_passed);
  6641. autorun_devices(part);
  6642. }
  6643. #endif /* !MODULE */
  6644. static __exit void md_exit(void)
  6645. {
  6646. mddev_t *mddev;
  6647. struct list_head *tmp;
  6648. blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
  6649. blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
  6650. unregister_blkdev(MD_MAJOR,"md");
  6651. unregister_blkdev(mdp_major, "mdp");
  6652. unregister_reboot_notifier(&md_notifier);
  6653. unregister_sysctl_table(raid_table_header);
  6654. remove_proc_entry("mdstat", NULL);
  6655. for_each_mddev(mddev, tmp) {
  6656. export_array(mddev);
  6657. mddev->hold_active = 0;
  6658. }
  6659. destroy_workqueue(md_misc_wq);
  6660. destroy_workqueue(md_wq);
  6661. }
  6662. subsys_initcall(md_init);
  6663. module_exit(md_exit)
  6664. static int get_ro(char *buffer, struct kernel_param *kp)
  6665. {
  6666. return sprintf(buffer, "%d", start_readonly);
  6667. }
  6668. static int set_ro(const char *val, struct kernel_param *kp)
  6669. {
  6670. char *e;
  6671. int num = simple_strtoul(val, &e, 10);
  6672. if (*val && (*e == '\0' || *e == '\n')) {
  6673. start_readonly = num;
  6674. return 0;
  6675. }
  6676. return -EINVAL;
  6677. }
  6678. module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
  6679. module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
  6680. module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
  6681. EXPORT_SYMBOL(register_md_personality);
  6682. EXPORT_SYMBOL(unregister_md_personality);
  6683. EXPORT_SYMBOL(md_error);
  6684. EXPORT_SYMBOL(md_done_sync);
  6685. EXPORT_SYMBOL(md_write_start);
  6686. EXPORT_SYMBOL(md_write_end);
  6687. EXPORT_SYMBOL(md_register_thread);
  6688. EXPORT_SYMBOL(md_unregister_thread);
  6689. EXPORT_SYMBOL(md_wakeup_thread);
  6690. EXPORT_SYMBOL(md_check_recovery);
  6691. MODULE_LICENSE("GPL");
  6692. MODULE_DESCRIPTION("MD RAID framework");
  6693. MODULE_ALIAS("md");
  6694. MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);