md.c 234 KB

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