md.c 233 KB

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