md.c 253 KB

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