md.c 245 KB

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