md.c 215 KB

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