md.c 246 KB

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