md.c 233 KB

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