raid5.c 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->seq_write is the number of the last batch successfully written.
  30. * conf->seq_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is seq_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/blkdev.h>
  45. #include <linux/kthread.h>
  46. #include <linux/raid/pq.h>
  47. #include <linux/async_tx.h>
  48. #include <linux/module.h>
  49. #include <linux/async.h>
  50. #include <linux/seq_file.h>
  51. #include <linux/cpu.h>
  52. #include <linux/slab.h>
  53. #include <linux/ratelimit.h>
  54. #include <linux/nodemask.h>
  55. #include <trace/events/block.h>
  56. #include "md.h"
  57. #include "raid5.h"
  58. #include "raid0.h"
  59. #include "bitmap.h"
  60. #define cpu_to_group(cpu) cpu_to_node(cpu)
  61. #define ANY_GROUP NUMA_NO_NODE
  62. static struct workqueue_struct *raid5_wq;
  63. /*
  64. * Stripe cache
  65. */
  66. #define NR_STRIPES 256
  67. #define STRIPE_SIZE PAGE_SIZE
  68. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  69. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  70. #define IO_THRESHOLD 1
  71. #define BYPASS_THRESHOLD 1
  72. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  73. #define HASH_MASK (NR_HASH - 1)
  74. static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
  75. {
  76. int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
  77. return &conf->stripe_hashtbl[hash];
  78. }
  79. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  80. * order without overlap. There may be several bio's per stripe+device, and
  81. * a bio could span several devices.
  82. * When walking this list for a particular stripe+device, we must never proceed
  83. * beyond a bio that extends past this device, as the next bio might no longer
  84. * be valid.
  85. * This function is used to determine the 'next' bio in the list, given the sector
  86. * of the current stripe+device
  87. */
  88. static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
  89. {
  90. int sectors = bio_sectors(bio);
  91. if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
  92. return bio->bi_next;
  93. else
  94. return NULL;
  95. }
  96. /*
  97. * We maintain a biased count of active stripes in the bottom 16 bits of
  98. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  99. */
  100. static inline int raid5_bi_processed_stripes(struct bio *bio)
  101. {
  102. atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
  103. return (atomic_read(segments) >> 16) & 0xffff;
  104. }
  105. static inline int raid5_dec_bi_active_stripes(struct bio *bio)
  106. {
  107. atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
  108. return atomic_sub_return(1, segments) & 0xffff;
  109. }
  110. static inline void raid5_inc_bi_active_stripes(struct bio *bio)
  111. {
  112. atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
  113. atomic_inc(segments);
  114. }
  115. static inline void raid5_set_bi_processed_stripes(struct bio *bio,
  116. unsigned int cnt)
  117. {
  118. atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
  119. int old, new;
  120. do {
  121. old = atomic_read(segments);
  122. new = (old & 0xffff) | (cnt << 16);
  123. } while (atomic_cmpxchg(segments, old, new) != old);
  124. }
  125. static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
  126. {
  127. atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
  128. atomic_set(segments, cnt);
  129. }
  130. /* Find first data disk in a raid6 stripe */
  131. static inline int raid6_d0(struct stripe_head *sh)
  132. {
  133. if (sh->ddf_layout)
  134. /* ddf always start from first device */
  135. return 0;
  136. /* md starts just after Q block */
  137. if (sh->qd_idx == sh->disks - 1)
  138. return 0;
  139. else
  140. return sh->qd_idx + 1;
  141. }
  142. static inline int raid6_next_disk(int disk, int raid_disks)
  143. {
  144. disk++;
  145. return (disk < raid_disks) ? disk : 0;
  146. }
  147. /* When walking through the disks in a raid5, starting at raid6_d0,
  148. * We need to map each disk to a 'slot', where the data disks are slot
  149. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  150. * is raid_disks-1. This help does that mapping.
  151. */
  152. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  153. int *count, int syndrome_disks)
  154. {
  155. int slot = *count;
  156. if (sh->ddf_layout)
  157. (*count)++;
  158. if (idx == sh->pd_idx)
  159. return syndrome_disks;
  160. if (idx == sh->qd_idx)
  161. return syndrome_disks + 1;
  162. if (!sh->ddf_layout)
  163. (*count)++;
  164. return slot;
  165. }
  166. static void return_io(struct bio *return_bi)
  167. {
  168. struct bio *bi = return_bi;
  169. while (bi) {
  170. return_bi = bi->bi_next;
  171. bi->bi_next = NULL;
  172. bi->bi_size = 0;
  173. trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
  174. bi, 0);
  175. bio_endio(bi, 0);
  176. bi = return_bi;
  177. }
  178. }
  179. static void print_raid5_conf (struct r5conf *conf);
  180. static int stripe_operations_active(struct stripe_head *sh)
  181. {
  182. return sh->check_state || sh->reconstruct_state ||
  183. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  184. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  185. }
  186. static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
  187. {
  188. struct r5conf *conf = sh->raid_conf;
  189. struct r5worker_group *group;
  190. int i, cpu = sh->cpu;
  191. if (!cpu_online(cpu)) {
  192. cpu = cpumask_any(cpu_online_mask);
  193. sh->cpu = cpu;
  194. }
  195. if (list_empty(&sh->lru)) {
  196. struct r5worker_group *group;
  197. group = conf->worker_groups + cpu_to_group(cpu);
  198. list_add_tail(&sh->lru, &group->handle_list);
  199. }
  200. if (conf->worker_cnt_per_group == 0) {
  201. md_wakeup_thread(conf->mddev->thread);
  202. return;
  203. }
  204. group = conf->worker_groups + cpu_to_group(sh->cpu);
  205. for (i = 0; i < conf->worker_cnt_per_group; i++)
  206. queue_work_on(sh->cpu, raid5_wq, &group->workers[i].work);
  207. }
  208. static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
  209. {
  210. BUG_ON(!list_empty(&sh->lru));
  211. BUG_ON(atomic_read(&conf->active_stripes)==0);
  212. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  213. if (test_bit(STRIPE_DELAYED, &sh->state) &&
  214. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  215. list_add_tail(&sh->lru, &conf->delayed_list);
  216. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  217. sh->bm_seq - conf->seq_write > 0)
  218. list_add_tail(&sh->lru, &conf->bitmap_list);
  219. else {
  220. clear_bit(STRIPE_DELAYED, &sh->state);
  221. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  222. if (conf->worker_cnt_per_group == 0) {
  223. list_add_tail(&sh->lru, &conf->handle_list);
  224. } else {
  225. raid5_wakeup_stripe_thread(sh);
  226. return;
  227. }
  228. }
  229. md_wakeup_thread(conf->mddev->thread);
  230. } else {
  231. BUG_ON(stripe_operations_active(sh));
  232. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  233. if (atomic_dec_return(&conf->preread_active_stripes)
  234. < IO_THRESHOLD)
  235. md_wakeup_thread(conf->mddev->thread);
  236. atomic_dec(&conf->active_stripes);
  237. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  238. list_add_tail(&sh->lru, &conf->inactive_list);
  239. wake_up(&conf->wait_for_stripe);
  240. if (conf->retry_read_aligned)
  241. md_wakeup_thread(conf->mddev->thread);
  242. }
  243. }
  244. }
  245. static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
  246. {
  247. if (atomic_dec_and_test(&sh->count))
  248. do_release_stripe(conf, sh);
  249. }
  250. static struct llist_node *llist_reverse_order(struct llist_node *head)
  251. {
  252. struct llist_node *new_head = NULL;
  253. while (head) {
  254. struct llist_node *tmp = head;
  255. head = head->next;
  256. tmp->next = new_head;
  257. new_head = tmp;
  258. }
  259. return new_head;
  260. }
  261. /* should hold conf->device_lock already */
  262. static int release_stripe_list(struct r5conf *conf)
  263. {
  264. struct stripe_head *sh;
  265. int count = 0;
  266. struct llist_node *head;
  267. head = llist_del_all(&conf->released_stripes);
  268. head = llist_reverse_order(head);
  269. while (head) {
  270. sh = llist_entry(head, struct stripe_head, release_list);
  271. head = llist_next(head);
  272. /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
  273. smp_mb();
  274. clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
  275. /*
  276. * Don't worry the bit is set here, because if the bit is set
  277. * again, the count is always > 1. This is true for
  278. * STRIPE_ON_UNPLUG_LIST bit too.
  279. */
  280. __release_stripe(conf, sh);
  281. count++;
  282. }
  283. return count;
  284. }
  285. static void release_stripe(struct stripe_head *sh)
  286. {
  287. struct r5conf *conf = sh->raid_conf;
  288. unsigned long flags;
  289. bool wakeup;
  290. if (test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
  291. goto slow_path;
  292. wakeup = llist_add(&sh->release_list, &conf->released_stripes);
  293. if (wakeup)
  294. md_wakeup_thread(conf->mddev->thread);
  295. return;
  296. slow_path:
  297. local_irq_save(flags);
  298. /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
  299. if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
  300. do_release_stripe(conf, sh);
  301. spin_unlock(&conf->device_lock);
  302. }
  303. local_irq_restore(flags);
  304. }
  305. static inline void remove_hash(struct stripe_head *sh)
  306. {
  307. pr_debug("remove_hash(), stripe %llu\n",
  308. (unsigned long long)sh->sector);
  309. hlist_del_init(&sh->hash);
  310. }
  311. static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
  312. {
  313. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  314. pr_debug("insert_hash(), stripe %llu\n",
  315. (unsigned long long)sh->sector);
  316. hlist_add_head(&sh->hash, hp);
  317. }
  318. /* find an idle stripe, make sure it is unhashed, and return it. */
  319. static struct stripe_head *get_free_stripe(struct r5conf *conf)
  320. {
  321. struct stripe_head *sh = NULL;
  322. struct list_head *first;
  323. if (list_empty(&conf->inactive_list))
  324. goto out;
  325. first = conf->inactive_list.next;
  326. sh = list_entry(first, struct stripe_head, lru);
  327. list_del_init(first);
  328. remove_hash(sh);
  329. atomic_inc(&conf->active_stripes);
  330. out:
  331. return sh;
  332. }
  333. static void shrink_buffers(struct stripe_head *sh)
  334. {
  335. struct page *p;
  336. int i;
  337. int num = sh->raid_conf->pool_size;
  338. for (i = 0; i < num ; i++) {
  339. p = sh->dev[i].page;
  340. if (!p)
  341. continue;
  342. sh->dev[i].page = NULL;
  343. put_page(p);
  344. }
  345. }
  346. static int grow_buffers(struct stripe_head *sh)
  347. {
  348. int i;
  349. int num = sh->raid_conf->pool_size;
  350. for (i = 0; i < num; i++) {
  351. struct page *page;
  352. if (!(page = alloc_page(GFP_KERNEL))) {
  353. return 1;
  354. }
  355. sh->dev[i].page = page;
  356. }
  357. return 0;
  358. }
  359. static void raid5_build_block(struct stripe_head *sh, int i, int previous);
  360. static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
  361. struct stripe_head *sh);
  362. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  363. {
  364. struct r5conf *conf = sh->raid_conf;
  365. int i;
  366. BUG_ON(atomic_read(&sh->count) != 0);
  367. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  368. BUG_ON(stripe_operations_active(sh));
  369. pr_debug("init_stripe called, stripe %llu\n",
  370. (unsigned long long)sh->sector);
  371. remove_hash(sh);
  372. sh->generation = conf->generation - previous;
  373. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  374. sh->sector = sector;
  375. stripe_set_idx(sector, conf, previous, sh);
  376. sh->state = 0;
  377. for (i = sh->disks; i--; ) {
  378. struct r5dev *dev = &sh->dev[i];
  379. if (dev->toread || dev->read || dev->towrite || dev->written ||
  380. test_bit(R5_LOCKED, &dev->flags)) {
  381. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  382. (unsigned long long)sh->sector, i, dev->toread,
  383. dev->read, dev->towrite, dev->written,
  384. test_bit(R5_LOCKED, &dev->flags));
  385. WARN_ON(1);
  386. }
  387. dev->flags = 0;
  388. raid5_build_block(sh, i, previous);
  389. }
  390. insert_hash(conf, sh);
  391. sh->cpu = smp_processor_id();
  392. }
  393. static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
  394. short generation)
  395. {
  396. struct stripe_head *sh;
  397. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  398. hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
  399. if (sh->sector == sector && sh->generation == generation)
  400. return sh;
  401. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  402. return NULL;
  403. }
  404. /*
  405. * Need to check if array has failed when deciding whether to:
  406. * - start an array
  407. * - remove non-faulty devices
  408. * - add a spare
  409. * - allow a reshape
  410. * This determination is simple when no reshape is happening.
  411. * However if there is a reshape, we need to carefully check
  412. * both the before and after sections.
  413. * This is because some failed devices may only affect one
  414. * of the two sections, and some non-in_sync devices may
  415. * be insync in the section most affected by failed devices.
  416. */
  417. static int calc_degraded(struct r5conf *conf)
  418. {
  419. int degraded, degraded2;
  420. int i;
  421. rcu_read_lock();
  422. degraded = 0;
  423. for (i = 0; i < conf->previous_raid_disks; i++) {
  424. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  425. if (rdev && test_bit(Faulty, &rdev->flags))
  426. rdev = rcu_dereference(conf->disks[i].replacement);
  427. if (!rdev || test_bit(Faulty, &rdev->flags))
  428. degraded++;
  429. else if (test_bit(In_sync, &rdev->flags))
  430. ;
  431. else
  432. /* not in-sync or faulty.
  433. * If the reshape increases the number of devices,
  434. * this is being recovered by the reshape, so
  435. * this 'previous' section is not in_sync.
  436. * If the number of devices is being reduced however,
  437. * the device can only be part of the array if
  438. * we are reverting a reshape, so this section will
  439. * be in-sync.
  440. */
  441. if (conf->raid_disks >= conf->previous_raid_disks)
  442. degraded++;
  443. }
  444. rcu_read_unlock();
  445. if (conf->raid_disks == conf->previous_raid_disks)
  446. return degraded;
  447. rcu_read_lock();
  448. degraded2 = 0;
  449. for (i = 0; i < conf->raid_disks; i++) {
  450. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  451. if (rdev && test_bit(Faulty, &rdev->flags))
  452. rdev = rcu_dereference(conf->disks[i].replacement);
  453. if (!rdev || test_bit(Faulty, &rdev->flags))
  454. degraded2++;
  455. else if (test_bit(In_sync, &rdev->flags))
  456. ;
  457. else
  458. /* not in-sync or faulty.
  459. * If reshape increases the number of devices, this
  460. * section has already been recovered, else it
  461. * almost certainly hasn't.
  462. */
  463. if (conf->raid_disks <= conf->previous_raid_disks)
  464. degraded2++;
  465. }
  466. rcu_read_unlock();
  467. if (degraded2 > degraded)
  468. return degraded2;
  469. return degraded;
  470. }
  471. static int has_failed(struct r5conf *conf)
  472. {
  473. int degraded;
  474. if (conf->mddev->reshape_position == MaxSector)
  475. return conf->mddev->degraded > conf->max_degraded;
  476. degraded = calc_degraded(conf);
  477. if (degraded > conf->max_degraded)
  478. return 1;
  479. return 0;
  480. }
  481. static struct stripe_head *
  482. get_active_stripe(struct r5conf *conf, sector_t sector,
  483. int previous, int noblock, int noquiesce)
  484. {
  485. struct stripe_head *sh;
  486. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  487. spin_lock_irq(&conf->device_lock);
  488. do {
  489. wait_event_lock_irq(conf->wait_for_stripe,
  490. conf->quiesce == 0 || noquiesce,
  491. conf->device_lock);
  492. sh = __find_stripe(conf, sector, conf->generation - previous);
  493. if (!sh) {
  494. if (!conf->inactive_blocked)
  495. sh = get_free_stripe(conf);
  496. if (noblock && sh == NULL)
  497. break;
  498. if (!sh) {
  499. conf->inactive_blocked = 1;
  500. wait_event_lock_irq(conf->wait_for_stripe,
  501. !list_empty(&conf->inactive_list) &&
  502. (atomic_read(&conf->active_stripes)
  503. < (conf->max_nr_stripes *3/4)
  504. || !conf->inactive_blocked),
  505. conf->device_lock);
  506. conf->inactive_blocked = 0;
  507. } else
  508. init_stripe(sh, sector, previous);
  509. } else {
  510. if (atomic_read(&sh->count)) {
  511. BUG_ON(!list_empty(&sh->lru)
  512. && !test_bit(STRIPE_EXPANDING, &sh->state)
  513. && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
  514. && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
  515. } else {
  516. if (!test_bit(STRIPE_HANDLE, &sh->state))
  517. atomic_inc(&conf->active_stripes);
  518. if (list_empty(&sh->lru) &&
  519. !test_bit(STRIPE_EXPANDING, &sh->state))
  520. BUG();
  521. list_del_init(&sh->lru);
  522. }
  523. }
  524. } while (sh == NULL);
  525. if (sh)
  526. atomic_inc(&sh->count);
  527. spin_unlock_irq(&conf->device_lock);
  528. return sh;
  529. }
  530. /* Determine if 'data_offset' or 'new_data_offset' should be used
  531. * in this stripe_head.
  532. */
  533. static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
  534. {
  535. sector_t progress = conf->reshape_progress;
  536. /* Need a memory barrier to make sure we see the value
  537. * of conf->generation, or ->data_offset that was set before
  538. * reshape_progress was updated.
  539. */
  540. smp_rmb();
  541. if (progress == MaxSector)
  542. return 0;
  543. if (sh->generation == conf->generation - 1)
  544. return 0;
  545. /* We are in a reshape, and this is a new-generation stripe,
  546. * so use new_data_offset.
  547. */
  548. return 1;
  549. }
  550. static void
  551. raid5_end_read_request(struct bio *bi, int error);
  552. static void
  553. raid5_end_write_request(struct bio *bi, int error);
  554. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  555. {
  556. struct r5conf *conf = sh->raid_conf;
  557. int i, disks = sh->disks;
  558. might_sleep();
  559. for (i = disks; i--; ) {
  560. int rw;
  561. int replace_only = 0;
  562. struct bio *bi, *rbi;
  563. struct md_rdev *rdev, *rrdev = NULL;
  564. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  565. if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
  566. rw = WRITE_FUA;
  567. else
  568. rw = WRITE;
  569. if (test_bit(R5_Discard, &sh->dev[i].flags))
  570. rw |= REQ_DISCARD;
  571. } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  572. rw = READ;
  573. else if (test_and_clear_bit(R5_WantReplace,
  574. &sh->dev[i].flags)) {
  575. rw = WRITE;
  576. replace_only = 1;
  577. } else
  578. continue;
  579. if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
  580. rw |= REQ_SYNC;
  581. bi = &sh->dev[i].req;
  582. rbi = &sh->dev[i].rreq; /* For writing to replacement */
  583. rcu_read_lock();
  584. rrdev = rcu_dereference(conf->disks[i].replacement);
  585. smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
  586. rdev = rcu_dereference(conf->disks[i].rdev);
  587. if (!rdev) {
  588. rdev = rrdev;
  589. rrdev = NULL;
  590. }
  591. if (rw & WRITE) {
  592. if (replace_only)
  593. rdev = NULL;
  594. if (rdev == rrdev)
  595. /* We raced and saw duplicates */
  596. rrdev = NULL;
  597. } else {
  598. if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
  599. rdev = rrdev;
  600. rrdev = NULL;
  601. }
  602. if (rdev && test_bit(Faulty, &rdev->flags))
  603. rdev = NULL;
  604. if (rdev)
  605. atomic_inc(&rdev->nr_pending);
  606. if (rrdev && test_bit(Faulty, &rrdev->flags))
  607. rrdev = NULL;
  608. if (rrdev)
  609. atomic_inc(&rrdev->nr_pending);
  610. rcu_read_unlock();
  611. /* We have already checked bad blocks for reads. Now
  612. * need to check for writes. We never accept write errors
  613. * on the replacement, so we don't to check rrdev.
  614. */
  615. while ((rw & WRITE) && rdev &&
  616. test_bit(WriteErrorSeen, &rdev->flags)) {
  617. sector_t first_bad;
  618. int bad_sectors;
  619. int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  620. &first_bad, &bad_sectors);
  621. if (!bad)
  622. break;
  623. if (bad < 0) {
  624. set_bit(BlockedBadBlocks, &rdev->flags);
  625. if (!conf->mddev->external &&
  626. conf->mddev->flags) {
  627. /* It is very unlikely, but we might
  628. * still need to write out the
  629. * bad block log - better give it
  630. * a chance*/
  631. md_check_recovery(conf->mddev);
  632. }
  633. /*
  634. * Because md_wait_for_blocked_rdev
  635. * will dec nr_pending, we must
  636. * increment it first.
  637. */
  638. atomic_inc(&rdev->nr_pending);
  639. md_wait_for_blocked_rdev(rdev, conf->mddev);
  640. } else {
  641. /* Acknowledged bad block - skip the write */
  642. rdev_dec_pending(rdev, conf->mddev);
  643. rdev = NULL;
  644. }
  645. }
  646. if (rdev) {
  647. if (s->syncing || s->expanding || s->expanded
  648. || s->replacing)
  649. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  650. set_bit(STRIPE_IO_STARTED, &sh->state);
  651. bio_reset(bi);
  652. bi->bi_bdev = rdev->bdev;
  653. bi->bi_rw = rw;
  654. bi->bi_end_io = (rw & WRITE)
  655. ? raid5_end_write_request
  656. : raid5_end_read_request;
  657. bi->bi_private = sh;
  658. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  659. __func__, (unsigned long long)sh->sector,
  660. bi->bi_rw, i);
  661. atomic_inc(&sh->count);
  662. if (use_new_offset(conf, sh))
  663. bi->bi_sector = (sh->sector
  664. + rdev->new_data_offset);
  665. else
  666. bi->bi_sector = (sh->sector
  667. + rdev->data_offset);
  668. if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
  669. bi->bi_rw |= REQ_FLUSH;
  670. bi->bi_vcnt = 1;
  671. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  672. bi->bi_io_vec[0].bv_offset = 0;
  673. bi->bi_size = STRIPE_SIZE;
  674. if (rrdev)
  675. set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
  676. if (conf->mddev->gendisk)
  677. trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
  678. bi, disk_devt(conf->mddev->gendisk),
  679. sh->dev[i].sector);
  680. generic_make_request(bi);
  681. }
  682. if (rrdev) {
  683. if (s->syncing || s->expanding || s->expanded
  684. || s->replacing)
  685. md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
  686. set_bit(STRIPE_IO_STARTED, &sh->state);
  687. bio_reset(rbi);
  688. rbi->bi_bdev = rrdev->bdev;
  689. rbi->bi_rw = rw;
  690. BUG_ON(!(rw & WRITE));
  691. rbi->bi_end_io = raid5_end_write_request;
  692. rbi->bi_private = sh;
  693. pr_debug("%s: for %llu schedule op %ld on "
  694. "replacement disc %d\n",
  695. __func__, (unsigned long long)sh->sector,
  696. rbi->bi_rw, i);
  697. atomic_inc(&sh->count);
  698. if (use_new_offset(conf, sh))
  699. rbi->bi_sector = (sh->sector
  700. + rrdev->new_data_offset);
  701. else
  702. rbi->bi_sector = (sh->sector
  703. + rrdev->data_offset);
  704. rbi->bi_vcnt = 1;
  705. rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  706. rbi->bi_io_vec[0].bv_offset = 0;
  707. rbi->bi_size = STRIPE_SIZE;
  708. if (conf->mddev->gendisk)
  709. trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
  710. rbi, disk_devt(conf->mddev->gendisk),
  711. sh->dev[i].sector);
  712. generic_make_request(rbi);
  713. }
  714. if (!rdev && !rrdev) {
  715. if (rw & WRITE)
  716. set_bit(STRIPE_DEGRADED, &sh->state);
  717. pr_debug("skip op %ld on disc %d for sector %llu\n",
  718. bi->bi_rw, i, (unsigned long long)sh->sector);
  719. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  720. set_bit(STRIPE_HANDLE, &sh->state);
  721. }
  722. }
  723. }
  724. static struct dma_async_tx_descriptor *
  725. async_copy_data(int frombio, struct bio *bio, struct page *page,
  726. sector_t sector, struct dma_async_tx_descriptor *tx)
  727. {
  728. struct bio_vec *bvl;
  729. struct page *bio_page;
  730. int i;
  731. int page_offset;
  732. struct async_submit_ctl submit;
  733. enum async_tx_flags flags = 0;
  734. if (bio->bi_sector >= sector)
  735. page_offset = (signed)(bio->bi_sector - sector) * 512;
  736. else
  737. page_offset = (signed)(sector - bio->bi_sector) * -512;
  738. if (frombio)
  739. flags |= ASYNC_TX_FENCE;
  740. init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
  741. bio_for_each_segment(bvl, bio, i) {
  742. int len = bvl->bv_len;
  743. int clen;
  744. int b_offset = 0;
  745. if (page_offset < 0) {
  746. b_offset = -page_offset;
  747. page_offset += b_offset;
  748. len -= b_offset;
  749. }
  750. if (len > 0 && page_offset + len > STRIPE_SIZE)
  751. clen = STRIPE_SIZE - page_offset;
  752. else
  753. clen = len;
  754. if (clen > 0) {
  755. b_offset += bvl->bv_offset;
  756. bio_page = bvl->bv_page;
  757. if (frombio)
  758. tx = async_memcpy(page, bio_page, page_offset,
  759. b_offset, clen, &submit);
  760. else
  761. tx = async_memcpy(bio_page, page, b_offset,
  762. page_offset, clen, &submit);
  763. }
  764. /* chain the operations */
  765. submit.depend_tx = tx;
  766. if (clen < len) /* hit end of page */
  767. break;
  768. page_offset += len;
  769. }
  770. return tx;
  771. }
  772. static void ops_complete_biofill(void *stripe_head_ref)
  773. {
  774. struct stripe_head *sh = stripe_head_ref;
  775. struct bio *return_bi = NULL;
  776. int i;
  777. pr_debug("%s: stripe %llu\n", __func__,
  778. (unsigned long long)sh->sector);
  779. /* clear completed biofills */
  780. for (i = sh->disks; i--; ) {
  781. struct r5dev *dev = &sh->dev[i];
  782. /* acknowledge completion of a biofill operation */
  783. /* and check if we need to reply to a read request,
  784. * new R5_Wantfill requests are held off until
  785. * !STRIPE_BIOFILL_RUN
  786. */
  787. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  788. struct bio *rbi, *rbi2;
  789. BUG_ON(!dev->read);
  790. rbi = dev->read;
  791. dev->read = NULL;
  792. while (rbi && rbi->bi_sector <
  793. dev->sector + STRIPE_SECTORS) {
  794. rbi2 = r5_next_bio(rbi, dev->sector);
  795. if (!raid5_dec_bi_active_stripes(rbi)) {
  796. rbi->bi_next = return_bi;
  797. return_bi = rbi;
  798. }
  799. rbi = rbi2;
  800. }
  801. }
  802. }
  803. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  804. return_io(return_bi);
  805. set_bit(STRIPE_HANDLE, &sh->state);
  806. release_stripe(sh);
  807. }
  808. static void ops_run_biofill(struct stripe_head *sh)
  809. {
  810. struct dma_async_tx_descriptor *tx = NULL;
  811. struct async_submit_ctl submit;
  812. int i;
  813. pr_debug("%s: stripe %llu\n", __func__,
  814. (unsigned long long)sh->sector);
  815. for (i = sh->disks; i--; ) {
  816. struct r5dev *dev = &sh->dev[i];
  817. if (test_bit(R5_Wantfill, &dev->flags)) {
  818. struct bio *rbi;
  819. spin_lock_irq(&sh->stripe_lock);
  820. dev->read = rbi = dev->toread;
  821. dev->toread = NULL;
  822. spin_unlock_irq(&sh->stripe_lock);
  823. while (rbi && rbi->bi_sector <
  824. dev->sector + STRIPE_SECTORS) {
  825. tx = async_copy_data(0, rbi, dev->page,
  826. dev->sector, tx);
  827. rbi = r5_next_bio(rbi, dev->sector);
  828. }
  829. }
  830. }
  831. atomic_inc(&sh->count);
  832. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
  833. async_trigger_callback(&submit);
  834. }
  835. static void mark_target_uptodate(struct stripe_head *sh, int target)
  836. {
  837. struct r5dev *tgt;
  838. if (target < 0)
  839. return;
  840. tgt = &sh->dev[target];
  841. set_bit(R5_UPTODATE, &tgt->flags);
  842. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  843. clear_bit(R5_Wantcompute, &tgt->flags);
  844. }
  845. static void ops_complete_compute(void *stripe_head_ref)
  846. {
  847. struct stripe_head *sh = stripe_head_ref;
  848. pr_debug("%s: stripe %llu\n", __func__,
  849. (unsigned long long)sh->sector);
  850. /* mark the computed target(s) as uptodate */
  851. mark_target_uptodate(sh, sh->ops.target);
  852. mark_target_uptodate(sh, sh->ops.target2);
  853. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  854. if (sh->check_state == check_state_compute_run)
  855. sh->check_state = check_state_compute_result;
  856. set_bit(STRIPE_HANDLE, &sh->state);
  857. release_stripe(sh);
  858. }
  859. /* return a pointer to the address conversion region of the scribble buffer */
  860. static addr_conv_t *to_addr_conv(struct stripe_head *sh,
  861. struct raid5_percpu *percpu)
  862. {
  863. return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
  864. }
  865. static struct dma_async_tx_descriptor *
  866. ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
  867. {
  868. int disks = sh->disks;
  869. struct page **xor_srcs = percpu->scribble;
  870. int target = sh->ops.target;
  871. struct r5dev *tgt = &sh->dev[target];
  872. struct page *xor_dest = tgt->page;
  873. int count = 0;
  874. struct dma_async_tx_descriptor *tx;
  875. struct async_submit_ctl submit;
  876. int i;
  877. pr_debug("%s: stripe %llu block: %d\n",
  878. __func__, (unsigned long long)sh->sector, target);
  879. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  880. for (i = disks; i--; )
  881. if (i != target)
  882. xor_srcs[count++] = sh->dev[i].page;
  883. atomic_inc(&sh->count);
  884. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
  885. ops_complete_compute, sh, to_addr_conv(sh, percpu));
  886. if (unlikely(count == 1))
  887. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  888. else
  889. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  890. return tx;
  891. }
  892. /* set_syndrome_sources - populate source buffers for gen_syndrome
  893. * @srcs - (struct page *) array of size sh->disks
  894. * @sh - stripe_head to parse
  895. *
  896. * Populates srcs in proper layout order for the stripe and returns the
  897. * 'count' of sources to be used in a call to async_gen_syndrome. The P
  898. * destination buffer is recorded in srcs[count] and the Q destination
  899. * is recorded in srcs[count+1]].
  900. */
  901. static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
  902. {
  903. int disks = sh->disks;
  904. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  905. int d0_idx = raid6_d0(sh);
  906. int count;
  907. int i;
  908. for (i = 0; i < disks; i++)
  909. srcs[i] = NULL;
  910. count = 0;
  911. i = d0_idx;
  912. do {
  913. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  914. srcs[slot] = sh->dev[i].page;
  915. i = raid6_next_disk(i, disks);
  916. } while (i != d0_idx);
  917. return syndrome_disks;
  918. }
  919. static struct dma_async_tx_descriptor *
  920. ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
  921. {
  922. int disks = sh->disks;
  923. struct page **blocks = percpu->scribble;
  924. int target;
  925. int qd_idx = sh->qd_idx;
  926. struct dma_async_tx_descriptor *tx;
  927. struct async_submit_ctl submit;
  928. struct r5dev *tgt;
  929. struct page *dest;
  930. int i;
  931. int count;
  932. if (sh->ops.target < 0)
  933. target = sh->ops.target2;
  934. else if (sh->ops.target2 < 0)
  935. target = sh->ops.target;
  936. else
  937. /* we should only have one valid target */
  938. BUG();
  939. BUG_ON(target < 0);
  940. pr_debug("%s: stripe %llu block: %d\n",
  941. __func__, (unsigned long long)sh->sector, target);
  942. tgt = &sh->dev[target];
  943. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  944. dest = tgt->page;
  945. atomic_inc(&sh->count);
  946. if (target == qd_idx) {
  947. count = set_syndrome_sources(blocks, sh);
  948. blocks[count] = NULL; /* regenerating p is not necessary */
  949. BUG_ON(blocks[count+1] != dest); /* q should already be set */
  950. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  951. ops_complete_compute, sh,
  952. to_addr_conv(sh, percpu));
  953. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  954. } else {
  955. /* Compute any data- or p-drive using XOR */
  956. count = 0;
  957. for (i = disks; i-- ; ) {
  958. if (i == target || i == qd_idx)
  959. continue;
  960. blocks[count++] = sh->dev[i].page;
  961. }
  962. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  963. NULL, ops_complete_compute, sh,
  964. to_addr_conv(sh, percpu));
  965. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
  966. }
  967. return tx;
  968. }
  969. static struct dma_async_tx_descriptor *
  970. ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
  971. {
  972. int i, count, disks = sh->disks;
  973. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  974. int d0_idx = raid6_d0(sh);
  975. int faila = -1, failb = -1;
  976. int target = sh->ops.target;
  977. int target2 = sh->ops.target2;
  978. struct r5dev *tgt = &sh->dev[target];
  979. struct r5dev *tgt2 = &sh->dev[target2];
  980. struct dma_async_tx_descriptor *tx;
  981. struct page **blocks = percpu->scribble;
  982. struct async_submit_ctl submit;
  983. pr_debug("%s: stripe %llu block1: %d block2: %d\n",
  984. __func__, (unsigned long long)sh->sector, target, target2);
  985. BUG_ON(target < 0 || target2 < 0);
  986. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  987. BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
  988. /* we need to open-code set_syndrome_sources to handle the
  989. * slot number conversion for 'faila' and 'failb'
  990. */
  991. for (i = 0; i < disks ; i++)
  992. blocks[i] = NULL;
  993. count = 0;
  994. i = d0_idx;
  995. do {
  996. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  997. blocks[slot] = sh->dev[i].page;
  998. if (i == target)
  999. faila = slot;
  1000. if (i == target2)
  1001. failb = slot;
  1002. i = raid6_next_disk(i, disks);
  1003. } while (i != d0_idx);
  1004. BUG_ON(faila == failb);
  1005. if (failb < faila)
  1006. swap(faila, failb);
  1007. pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
  1008. __func__, (unsigned long long)sh->sector, faila, failb);
  1009. atomic_inc(&sh->count);
  1010. if (failb == syndrome_disks+1) {
  1011. /* Q disk is one of the missing disks */
  1012. if (faila == syndrome_disks) {
  1013. /* Missing P+Q, just recompute */
  1014. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1015. ops_complete_compute, sh,
  1016. to_addr_conv(sh, percpu));
  1017. return async_gen_syndrome(blocks, 0, syndrome_disks+2,
  1018. STRIPE_SIZE, &submit);
  1019. } else {
  1020. struct page *dest;
  1021. int data_target;
  1022. int qd_idx = sh->qd_idx;
  1023. /* Missing D+Q: recompute D from P, then recompute Q */
  1024. if (target == qd_idx)
  1025. data_target = target2;
  1026. else
  1027. data_target = target;
  1028. count = 0;
  1029. for (i = disks; i-- ; ) {
  1030. if (i == data_target || i == qd_idx)
  1031. continue;
  1032. blocks[count++] = sh->dev[i].page;
  1033. }
  1034. dest = sh->dev[data_target].page;
  1035. init_async_submit(&submit,
  1036. ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  1037. NULL, NULL, NULL,
  1038. to_addr_conv(sh, percpu));
  1039. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
  1040. &submit);
  1041. count = set_syndrome_sources(blocks, sh);
  1042. init_async_submit(&submit, ASYNC_TX_FENCE, tx,
  1043. ops_complete_compute, sh,
  1044. to_addr_conv(sh, percpu));
  1045. return async_gen_syndrome(blocks, 0, count+2,
  1046. STRIPE_SIZE, &submit);
  1047. }
  1048. } else {
  1049. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1050. ops_complete_compute, sh,
  1051. to_addr_conv(sh, percpu));
  1052. if (failb == syndrome_disks) {
  1053. /* We're missing D+P. */
  1054. return async_raid6_datap_recov(syndrome_disks+2,
  1055. STRIPE_SIZE, faila,
  1056. blocks, &submit);
  1057. } else {
  1058. /* We're missing D+D. */
  1059. return async_raid6_2data_recov(syndrome_disks+2,
  1060. STRIPE_SIZE, faila, failb,
  1061. blocks, &submit);
  1062. }
  1063. }
  1064. }
  1065. static void ops_complete_prexor(void *stripe_head_ref)
  1066. {
  1067. struct stripe_head *sh = stripe_head_ref;
  1068. pr_debug("%s: stripe %llu\n", __func__,
  1069. (unsigned long long)sh->sector);
  1070. }
  1071. static struct dma_async_tx_descriptor *
  1072. ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
  1073. struct dma_async_tx_descriptor *tx)
  1074. {
  1075. int disks = sh->disks;
  1076. struct page **xor_srcs = percpu->scribble;
  1077. int count = 0, pd_idx = sh->pd_idx, i;
  1078. struct async_submit_ctl submit;
  1079. /* existing parity data subtracted */
  1080. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  1081. pr_debug("%s: stripe %llu\n", __func__,
  1082. (unsigned long long)sh->sector);
  1083. for (i = disks; i--; ) {
  1084. struct r5dev *dev = &sh->dev[i];
  1085. /* Only process blocks that are known to be uptodate */
  1086. if (test_bit(R5_Wantdrain, &dev->flags))
  1087. xor_srcs[count++] = dev->page;
  1088. }
  1089. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  1090. ops_complete_prexor, sh, to_addr_conv(sh, percpu));
  1091. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1092. return tx;
  1093. }
  1094. static struct dma_async_tx_descriptor *
  1095. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  1096. {
  1097. int disks = sh->disks;
  1098. int i;
  1099. pr_debug("%s: stripe %llu\n", __func__,
  1100. (unsigned long long)sh->sector);
  1101. for (i = disks; i--; ) {
  1102. struct r5dev *dev = &sh->dev[i];
  1103. struct bio *chosen;
  1104. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  1105. struct bio *wbi;
  1106. spin_lock_irq(&sh->stripe_lock);
  1107. chosen = dev->towrite;
  1108. dev->towrite = NULL;
  1109. BUG_ON(dev->written);
  1110. wbi = dev->written = chosen;
  1111. spin_unlock_irq(&sh->stripe_lock);
  1112. while (wbi && wbi->bi_sector <
  1113. dev->sector + STRIPE_SECTORS) {
  1114. if (wbi->bi_rw & REQ_FUA)
  1115. set_bit(R5_WantFUA, &dev->flags);
  1116. if (wbi->bi_rw & REQ_SYNC)
  1117. set_bit(R5_SyncIO, &dev->flags);
  1118. if (wbi->bi_rw & REQ_DISCARD)
  1119. set_bit(R5_Discard, &dev->flags);
  1120. else
  1121. tx = async_copy_data(1, wbi, dev->page,
  1122. dev->sector, tx);
  1123. wbi = r5_next_bio(wbi, dev->sector);
  1124. }
  1125. }
  1126. }
  1127. return tx;
  1128. }
  1129. static void ops_complete_reconstruct(void *stripe_head_ref)
  1130. {
  1131. struct stripe_head *sh = stripe_head_ref;
  1132. int disks = sh->disks;
  1133. int pd_idx = sh->pd_idx;
  1134. int qd_idx = sh->qd_idx;
  1135. int i;
  1136. bool fua = false, sync = false, discard = false;
  1137. pr_debug("%s: stripe %llu\n", __func__,
  1138. (unsigned long long)sh->sector);
  1139. for (i = disks; i--; ) {
  1140. fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
  1141. sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
  1142. discard |= test_bit(R5_Discard, &sh->dev[i].flags);
  1143. }
  1144. for (i = disks; i--; ) {
  1145. struct r5dev *dev = &sh->dev[i];
  1146. if (dev->written || i == pd_idx || i == qd_idx) {
  1147. if (!discard)
  1148. set_bit(R5_UPTODATE, &dev->flags);
  1149. if (fua)
  1150. set_bit(R5_WantFUA, &dev->flags);
  1151. if (sync)
  1152. set_bit(R5_SyncIO, &dev->flags);
  1153. }
  1154. }
  1155. if (sh->reconstruct_state == reconstruct_state_drain_run)
  1156. sh->reconstruct_state = reconstruct_state_drain_result;
  1157. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  1158. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  1159. else {
  1160. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  1161. sh->reconstruct_state = reconstruct_state_result;
  1162. }
  1163. set_bit(STRIPE_HANDLE, &sh->state);
  1164. release_stripe(sh);
  1165. }
  1166. static void
  1167. ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
  1168. struct dma_async_tx_descriptor *tx)
  1169. {
  1170. int disks = sh->disks;
  1171. struct page **xor_srcs = percpu->scribble;
  1172. struct async_submit_ctl submit;
  1173. int count = 0, pd_idx = sh->pd_idx, i;
  1174. struct page *xor_dest;
  1175. int prexor = 0;
  1176. unsigned long flags;
  1177. pr_debug("%s: stripe %llu\n", __func__,
  1178. (unsigned long long)sh->sector);
  1179. for (i = 0; i < sh->disks; i++) {
  1180. if (pd_idx == i)
  1181. continue;
  1182. if (!test_bit(R5_Discard, &sh->dev[i].flags))
  1183. break;
  1184. }
  1185. if (i >= sh->disks) {
  1186. atomic_inc(&sh->count);
  1187. set_bit(R5_Discard, &sh->dev[pd_idx].flags);
  1188. ops_complete_reconstruct(sh);
  1189. return;
  1190. }
  1191. /* check if prexor is active which means only process blocks
  1192. * that are part of a read-modify-write (written)
  1193. */
  1194. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  1195. prexor = 1;
  1196. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  1197. for (i = disks; i--; ) {
  1198. struct r5dev *dev = &sh->dev[i];
  1199. if (dev->written)
  1200. xor_srcs[count++] = dev->page;
  1201. }
  1202. } else {
  1203. xor_dest = sh->dev[pd_idx].page;
  1204. for (i = disks; i--; ) {
  1205. struct r5dev *dev = &sh->dev[i];
  1206. if (i != pd_idx)
  1207. xor_srcs[count++] = dev->page;
  1208. }
  1209. }
  1210. /* 1/ if we prexor'd then the dest is reused as a source
  1211. * 2/ if we did not prexor then we are redoing the parity
  1212. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  1213. * for the synchronous xor case
  1214. */
  1215. flags = ASYNC_TX_ACK |
  1216. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  1217. atomic_inc(&sh->count);
  1218. init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
  1219. to_addr_conv(sh, percpu));
  1220. if (unlikely(count == 1))
  1221. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  1222. else
  1223. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1224. }
  1225. static void
  1226. ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
  1227. struct dma_async_tx_descriptor *tx)
  1228. {
  1229. struct async_submit_ctl submit;
  1230. struct page **blocks = percpu->scribble;
  1231. int count, i;
  1232. pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
  1233. for (i = 0; i < sh->disks; i++) {
  1234. if (sh->pd_idx == i || sh->qd_idx == i)
  1235. continue;
  1236. if (!test_bit(R5_Discard, &sh->dev[i].flags))
  1237. break;
  1238. }
  1239. if (i >= sh->disks) {
  1240. atomic_inc(&sh->count);
  1241. set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
  1242. set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
  1243. ops_complete_reconstruct(sh);
  1244. return;
  1245. }
  1246. count = set_syndrome_sources(blocks, sh);
  1247. atomic_inc(&sh->count);
  1248. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
  1249. sh, to_addr_conv(sh, percpu));
  1250. async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  1251. }
  1252. static void ops_complete_check(void *stripe_head_ref)
  1253. {
  1254. struct stripe_head *sh = stripe_head_ref;
  1255. pr_debug("%s: stripe %llu\n", __func__,
  1256. (unsigned long long)sh->sector);
  1257. sh->check_state = check_state_check_result;
  1258. set_bit(STRIPE_HANDLE, &sh->state);
  1259. release_stripe(sh);
  1260. }
  1261. static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
  1262. {
  1263. int disks = sh->disks;
  1264. int pd_idx = sh->pd_idx;
  1265. int qd_idx = sh->qd_idx;
  1266. struct page *xor_dest;
  1267. struct page **xor_srcs = percpu->scribble;
  1268. struct dma_async_tx_descriptor *tx;
  1269. struct async_submit_ctl submit;
  1270. int count;
  1271. int i;
  1272. pr_debug("%s: stripe %llu\n", __func__,
  1273. (unsigned long long)sh->sector);
  1274. count = 0;
  1275. xor_dest = sh->dev[pd_idx].page;
  1276. xor_srcs[count++] = xor_dest;
  1277. for (i = disks; i--; ) {
  1278. if (i == pd_idx || i == qd_idx)
  1279. continue;
  1280. xor_srcs[count++] = sh->dev[i].page;
  1281. }
  1282. init_async_submit(&submit, 0, NULL, NULL, NULL,
  1283. to_addr_conv(sh, percpu));
  1284. tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  1285. &sh->ops.zero_sum_result, &submit);
  1286. atomic_inc(&sh->count);
  1287. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
  1288. tx = async_trigger_callback(&submit);
  1289. }
  1290. static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
  1291. {
  1292. struct page **srcs = percpu->scribble;
  1293. struct async_submit_ctl submit;
  1294. int count;
  1295. pr_debug("%s: stripe %llu checkp: %d\n", __func__,
  1296. (unsigned long long)sh->sector, checkp);
  1297. count = set_syndrome_sources(srcs, sh);
  1298. if (!checkp)
  1299. srcs[count] = NULL;
  1300. atomic_inc(&sh->count);
  1301. init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
  1302. sh, to_addr_conv(sh, percpu));
  1303. async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
  1304. &sh->ops.zero_sum_result, percpu->spare_page, &submit);
  1305. }
  1306. static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1307. {
  1308. int overlap_clear = 0, i, disks = sh->disks;
  1309. struct dma_async_tx_descriptor *tx = NULL;
  1310. struct r5conf *conf = sh->raid_conf;
  1311. int level = conf->level;
  1312. struct raid5_percpu *percpu;
  1313. unsigned long cpu;
  1314. cpu = get_cpu();
  1315. percpu = per_cpu_ptr(conf->percpu, cpu);
  1316. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  1317. ops_run_biofill(sh);
  1318. overlap_clear++;
  1319. }
  1320. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  1321. if (level < 6)
  1322. tx = ops_run_compute5(sh, percpu);
  1323. else {
  1324. if (sh->ops.target2 < 0 || sh->ops.target < 0)
  1325. tx = ops_run_compute6_1(sh, percpu);
  1326. else
  1327. tx = ops_run_compute6_2(sh, percpu);
  1328. }
  1329. /* terminate the chain if reconstruct is not set to be run */
  1330. if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
  1331. async_tx_ack(tx);
  1332. }
  1333. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  1334. tx = ops_run_prexor(sh, percpu, tx);
  1335. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  1336. tx = ops_run_biodrain(sh, tx);
  1337. overlap_clear++;
  1338. }
  1339. if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
  1340. if (level < 6)
  1341. ops_run_reconstruct5(sh, percpu, tx);
  1342. else
  1343. ops_run_reconstruct6(sh, percpu, tx);
  1344. }
  1345. if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
  1346. if (sh->check_state == check_state_run)
  1347. ops_run_check_p(sh, percpu);
  1348. else if (sh->check_state == check_state_run_q)
  1349. ops_run_check_pq(sh, percpu, 0);
  1350. else if (sh->check_state == check_state_run_pq)
  1351. ops_run_check_pq(sh, percpu, 1);
  1352. else
  1353. BUG();
  1354. }
  1355. if (overlap_clear)
  1356. for (i = disks; i--; ) {
  1357. struct r5dev *dev = &sh->dev[i];
  1358. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1359. wake_up(&sh->raid_conf->wait_for_overlap);
  1360. }
  1361. put_cpu();
  1362. }
  1363. static int grow_one_stripe(struct r5conf *conf)
  1364. {
  1365. struct stripe_head *sh;
  1366. sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
  1367. if (!sh)
  1368. return 0;
  1369. sh->raid_conf = conf;
  1370. spin_lock_init(&sh->stripe_lock);
  1371. if (grow_buffers(sh)) {
  1372. shrink_buffers(sh);
  1373. kmem_cache_free(conf->slab_cache, sh);
  1374. return 0;
  1375. }
  1376. /* we just created an active stripe so... */
  1377. atomic_set(&sh->count, 1);
  1378. atomic_inc(&conf->active_stripes);
  1379. INIT_LIST_HEAD(&sh->lru);
  1380. release_stripe(sh);
  1381. return 1;
  1382. }
  1383. static int grow_stripes(struct r5conf *conf, int num)
  1384. {
  1385. struct kmem_cache *sc;
  1386. int devs = max(conf->raid_disks, conf->previous_raid_disks);
  1387. if (conf->mddev->gendisk)
  1388. sprintf(conf->cache_name[0],
  1389. "raid%d-%s", conf->level, mdname(conf->mddev));
  1390. else
  1391. sprintf(conf->cache_name[0],
  1392. "raid%d-%p", conf->level, conf->mddev);
  1393. sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
  1394. conf->active_name = 0;
  1395. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  1396. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  1397. 0, 0, NULL);
  1398. if (!sc)
  1399. return 1;
  1400. conf->slab_cache = sc;
  1401. conf->pool_size = devs;
  1402. while (num--)
  1403. if (!grow_one_stripe(conf))
  1404. return 1;
  1405. return 0;
  1406. }
  1407. /**
  1408. * scribble_len - return the required size of the scribble region
  1409. * @num - total number of disks in the array
  1410. *
  1411. * The size must be enough to contain:
  1412. * 1/ a struct page pointer for each device in the array +2
  1413. * 2/ room to convert each entry in (1) to its corresponding dma
  1414. * (dma_map_page()) or page (page_address()) address.
  1415. *
  1416. * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
  1417. * calculate over all devices (not just the data blocks), using zeros in place
  1418. * of the P and Q blocks.
  1419. */
  1420. static size_t scribble_len(int num)
  1421. {
  1422. size_t len;
  1423. len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
  1424. return len;
  1425. }
  1426. static int resize_stripes(struct r5conf *conf, int newsize)
  1427. {
  1428. /* Make all the stripes able to hold 'newsize' devices.
  1429. * New slots in each stripe get 'page' set to a new page.
  1430. *
  1431. * This happens in stages:
  1432. * 1/ create a new kmem_cache and allocate the required number of
  1433. * stripe_heads.
  1434. * 2/ gather all the old stripe_heads and transfer the pages across
  1435. * to the new stripe_heads. This will have the side effect of
  1436. * freezing the array as once all stripe_heads have been collected,
  1437. * no IO will be possible. Old stripe heads are freed once their
  1438. * pages have been transferred over, and the old kmem_cache is
  1439. * freed when all stripes are done.
  1440. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  1441. * we simple return a failre status - no need to clean anything up.
  1442. * 4/ allocate new pages for the new slots in the new stripe_heads.
  1443. * If this fails, we don't bother trying the shrink the
  1444. * stripe_heads down again, we just leave them as they are.
  1445. * As each stripe_head is processed the new one is released into
  1446. * active service.
  1447. *
  1448. * Once step2 is started, we cannot afford to wait for a write,
  1449. * so we use GFP_NOIO allocations.
  1450. */
  1451. struct stripe_head *osh, *nsh;
  1452. LIST_HEAD(newstripes);
  1453. struct disk_info *ndisks;
  1454. unsigned long cpu;
  1455. int err;
  1456. struct kmem_cache *sc;
  1457. int i;
  1458. if (newsize <= conf->pool_size)
  1459. return 0; /* never bother to shrink */
  1460. err = md_allow_write(conf->mddev);
  1461. if (err)
  1462. return err;
  1463. /* Step 1 */
  1464. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  1465. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  1466. 0, 0, NULL);
  1467. if (!sc)
  1468. return -ENOMEM;
  1469. for (i = conf->max_nr_stripes; i; i--) {
  1470. nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
  1471. if (!nsh)
  1472. break;
  1473. nsh->raid_conf = conf;
  1474. spin_lock_init(&nsh->stripe_lock);
  1475. list_add(&nsh->lru, &newstripes);
  1476. }
  1477. if (i) {
  1478. /* didn't get enough, give up */
  1479. while (!list_empty(&newstripes)) {
  1480. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1481. list_del(&nsh->lru);
  1482. kmem_cache_free(sc, nsh);
  1483. }
  1484. kmem_cache_destroy(sc);
  1485. return -ENOMEM;
  1486. }
  1487. /* Step 2 - Must use GFP_NOIO now.
  1488. * OK, we have enough stripes, start collecting inactive
  1489. * stripes and copying them over
  1490. */
  1491. list_for_each_entry(nsh, &newstripes, lru) {
  1492. spin_lock_irq(&conf->device_lock);
  1493. wait_event_lock_irq(conf->wait_for_stripe,
  1494. !list_empty(&conf->inactive_list),
  1495. conf->device_lock);
  1496. osh = get_free_stripe(conf);
  1497. spin_unlock_irq(&conf->device_lock);
  1498. atomic_set(&nsh->count, 1);
  1499. for(i=0; i<conf->pool_size; i++)
  1500. nsh->dev[i].page = osh->dev[i].page;
  1501. for( ; i<newsize; i++)
  1502. nsh->dev[i].page = NULL;
  1503. kmem_cache_free(conf->slab_cache, osh);
  1504. }
  1505. kmem_cache_destroy(conf->slab_cache);
  1506. /* Step 3.
  1507. * At this point, we are holding all the stripes so the array
  1508. * is completely stalled, so now is a good time to resize
  1509. * conf->disks and the scribble region
  1510. */
  1511. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  1512. if (ndisks) {
  1513. for (i=0; i<conf->raid_disks; i++)
  1514. ndisks[i] = conf->disks[i];
  1515. kfree(conf->disks);
  1516. conf->disks = ndisks;
  1517. } else
  1518. err = -ENOMEM;
  1519. get_online_cpus();
  1520. conf->scribble_len = scribble_len(newsize);
  1521. for_each_present_cpu(cpu) {
  1522. struct raid5_percpu *percpu;
  1523. void *scribble;
  1524. percpu = per_cpu_ptr(conf->percpu, cpu);
  1525. scribble = kmalloc(conf->scribble_len, GFP_NOIO);
  1526. if (scribble) {
  1527. kfree(percpu->scribble);
  1528. percpu->scribble = scribble;
  1529. } else {
  1530. err = -ENOMEM;
  1531. break;
  1532. }
  1533. }
  1534. put_online_cpus();
  1535. /* Step 4, return new stripes to service */
  1536. while(!list_empty(&newstripes)) {
  1537. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1538. list_del_init(&nsh->lru);
  1539. for (i=conf->raid_disks; i < newsize; i++)
  1540. if (nsh->dev[i].page == NULL) {
  1541. struct page *p = alloc_page(GFP_NOIO);
  1542. nsh->dev[i].page = p;
  1543. if (!p)
  1544. err = -ENOMEM;
  1545. }
  1546. release_stripe(nsh);
  1547. }
  1548. /* critical section pass, GFP_NOIO no longer needed */
  1549. conf->slab_cache = sc;
  1550. conf->active_name = 1-conf->active_name;
  1551. conf->pool_size = newsize;
  1552. return err;
  1553. }
  1554. static int drop_one_stripe(struct r5conf *conf)
  1555. {
  1556. struct stripe_head *sh;
  1557. spin_lock_irq(&conf->device_lock);
  1558. sh = get_free_stripe(conf);
  1559. spin_unlock_irq(&conf->device_lock);
  1560. if (!sh)
  1561. return 0;
  1562. BUG_ON(atomic_read(&sh->count));
  1563. shrink_buffers(sh);
  1564. kmem_cache_free(conf->slab_cache, sh);
  1565. atomic_dec(&conf->active_stripes);
  1566. return 1;
  1567. }
  1568. static void shrink_stripes(struct r5conf *conf)
  1569. {
  1570. while (drop_one_stripe(conf))
  1571. ;
  1572. if (conf->slab_cache)
  1573. kmem_cache_destroy(conf->slab_cache);
  1574. conf->slab_cache = NULL;
  1575. }
  1576. static void raid5_end_read_request(struct bio * bi, int error)
  1577. {
  1578. struct stripe_head *sh = bi->bi_private;
  1579. struct r5conf *conf = sh->raid_conf;
  1580. int disks = sh->disks, i;
  1581. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1582. char b[BDEVNAME_SIZE];
  1583. struct md_rdev *rdev = NULL;
  1584. sector_t s;
  1585. for (i=0 ; i<disks; i++)
  1586. if (bi == &sh->dev[i].req)
  1587. break;
  1588. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  1589. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1590. uptodate);
  1591. if (i == disks) {
  1592. BUG();
  1593. return;
  1594. }
  1595. if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
  1596. /* If replacement finished while this request was outstanding,
  1597. * 'replacement' might be NULL already.
  1598. * In that case it moved down to 'rdev'.
  1599. * rdev is not removed until all requests are finished.
  1600. */
  1601. rdev = conf->disks[i].replacement;
  1602. if (!rdev)
  1603. rdev = conf->disks[i].rdev;
  1604. if (use_new_offset(conf, sh))
  1605. s = sh->sector + rdev->new_data_offset;
  1606. else
  1607. s = sh->sector + rdev->data_offset;
  1608. if (uptodate) {
  1609. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1610. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1611. /* Note that this cannot happen on a
  1612. * replacement device. We just fail those on
  1613. * any error
  1614. */
  1615. printk_ratelimited(
  1616. KERN_INFO
  1617. "md/raid:%s: read error corrected"
  1618. " (%lu sectors at %llu on %s)\n",
  1619. mdname(conf->mddev), STRIPE_SECTORS,
  1620. (unsigned long long)s,
  1621. bdevname(rdev->bdev, b));
  1622. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1623. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1624. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1625. } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
  1626. clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  1627. if (atomic_read(&rdev->read_errors))
  1628. atomic_set(&rdev->read_errors, 0);
  1629. } else {
  1630. const char *bdn = bdevname(rdev->bdev, b);
  1631. int retry = 0;
  1632. int set_bad = 0;
  1633. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  1634. atomic_inc(&rdev->read_errors);
  1635. if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
  1636. printk_ratelimited(
  1637. KERN_WARNING
  1638. "md/raid:%s: read error on replacement device "
  1639. "(sector %llu on %s).\n",
  1640. mdname(conf->mddev),
  1641. (unsigned long long)s,
  1642. bdn);
  1643. else if (conf->mddev->degraded >= conf->max_degraded) {
  1644. set_bad = 1;
  1645. printk_ratelimited(
  1646. KERN_WARNING
  1647. "md/raid:%s: read error not correctable "
  1648. "(sector %llu on %s).\n",
  1649. mdname(conf->mddev),
  1650. (unsigned long long)s,
  1651. bdn);
  1652. } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
  1653. /* Oh, no!!! */
  1654. set_bad = 1;
  1655. printk_ratelimited(
  1656. KERN_WARNING
  1657. "md/raid:%s: read error NOT corrected!! "
  1658. "(sector %llu on %s).\n",
  1659. mdname(conf->mddev),
  1660. (unsigned long long)s,
  1661. bdn);
  1662. } else if (atomic_read(&rdev->read_errors)
  1663. > conf->max_nr_stripes)
  1664. printk(KERN_WARNING
  1665. "md/raid:%s: Too many read errors, failing device %s.\n",
  1666. mdname(conf->mddev), bdn);
  1667. else
  1668. retry = 1;
  1669. if (retry)
  1670. if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
  1671. set_bit(R5_ReadError, &sh->dev[i].flags);
  1672. clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  1673. } else
  1674. set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  1675. else {
  1676. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1677. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1678. if (!(set_bad
  1679. && test_bit(In_sync, &rdev->flags)
  1680. && rdev_set_badblocks(
  1681. rdev, sh->sector, STRIPE_SECTORS, 0)))
  1682. md_error(conf->mddev, rdev);
  1683. }
  1684. }
  1685. rdev_dec_pending(rdev, conf->mddev);
  1686. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1687. set_bit(STRIPE_HANDLE, &sh->state);
  1688. release_stripe(sh);
  1689. }
  1690. static void raid5_end_write_request(struct bio *bi, int error)
  1691. {
  1692. struct stripe_head *sh = bi->bi_private;
  1693. struct r5conf *conf = sh->raid_conf;
  1694. int disks = sh->disks, i;
  1695. struct md_rdev *uninitialized_var(rdev);
  1696. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1697. sector_t first_bad;
  1698. int bad_sectors;
  1699. int replacement = 0;
  1700. for (i = 0 ; i < disks; i++) {
  1701. if (bi == &sh->dev[i].req) {
  1702. rdev = conf->disks[i].rdev;
  1703. break;
  1704. }
  1705. if (bi == &sh->dev[i].rreq) {
  1706. rdev = conf->disks[i].replacement;
  1707. if (rdev)
  1708. replacement = 1;
  1709. else
  1710. /* rdev was removed and 'replacement'
  1711. * replaced it. rdev is not removed
  1712. * until all requests are finished.
  1713. */
  1714. rdev = conf->disks[i].rdev;
  1715. break;
  1716. }
  1717. }
  1718. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1719. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1720. uptodate);
  1721. if (i == disks) {
  1722. BUG();
  1723. return;
  1724. }
  1725. if (replacement) {
  1726. if (!uptodate)
  1727. md_error(conf->mddev, rdev);
  1728. else if (is_badblock(rdev, sh->sector,
  1729. STRIPE_SECTORS,
  1730. &first_bad, &bad_sectors))
  1731. set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
  1732. } else {
  1733. if (!uptodate) {
  1734. set_bit(WriteErrorSeen, &rdev->flags);
  1735. set_bit(R5_WriteError, &sh->dev[i].flags);
  1736. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1737. set_bit(MD_RECOVERY_NEEDED,
  1738. &rdev->mddev->recovery);
  1739. } else if (is_badblock(rdev, sh->sector,
  1740. STRIPE_SECTORS,
  1741. &first_bad, &bad_sectors)) {
  1742. set_bit(R5_MadeGood, &sh->dev[i].flags);
  1743. if (test_bit(R5_ReadError, &sh->dev[i].flags))
  1744. /* That was a successful write so make
  1745. * sure it looks like we already did
  1746. * a re-write.
  1747. */
  1748. set_bit(R5_ReWrite, &sh->dev[i].flags);
  1749. }
  1750. }
  1751. rdev_dec_pending(rdev, conf->mddev);
  1752. if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
  1753. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1754. set_bit(STRIPE_HANDLE, &sh->state);
  1755. release_stripe(sh);
  1756. }
  1757. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
  1758. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  1759. {
  1760. struct r5dev *dev = &sh->dev[i];
  1761. bio_init(&dev->req);
  1762. dev->req.bi_io_vec = &dev->vec;
  1763. dev->req.bi_vcnt++;
  1764. dev->req.bi_max_vecs++;
  1765. dev->req.bi_private = sh;
  1766. dev->vec.bv_page = dev->page;
  1767. bio_init(&dev->rreq);
  1768. dev->rreq.bi_io_vec = &dev->rvec;
  1769. dev->rreq.bi_vcnt++;
  1770. dev->rreq.bi_max_vecs++;
  1771. dev->rreq.bi_private = sh;
  1772. dev->rvec.bv_page = dev->page;
  1773. dev->flags = 0;
  1774. dev->sector = compute_blocknr(sh, i, previous);
  1775. }
  1776. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1777. {
  1778. char b[BDEVNAME_SIZE];
  1779. struct r5conf *conf = mddev->private;
  1780. unsigned long flags;
  1781. pr_debug("raid456: error called\n");
  1782. spin_lock_irqsave(&conf->device_lock, flags);
  1783. clear_bit(In_sync, &rdev->flags);
  1784. mddev->degraded = calc_degraded(conf);
  1785. spin_unlock_irqrestore(&conf->device_lock, flags);
  1786. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1787. set_bit(Blocked, &rdev->flags);
  1788. set_bit(Faulty, &rdev->flags);
  1789. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1790. printk(KERN_ALERT
  1791. "md/raid:%s: Disk failure on %s, disabling device.\n"
  1792. "md/raid:%s: Operation continuing on %d devices.\n",
  1793. mdname(mddev),
  1794. bdevname(rdev->bdev, b),
  1795. mdname(mddev),
  1796. conf->raid_disks - mddev->degraded);
  1797. }
  1798. /*
  1799. * Input: a 'big' sector number,
  1800. * Output: index of the data and parity disk, and the sector # in them.
  1801. */
  1802. static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
  1803. int previous, int *dd_idx,
  1804. struct stripe_head *sh)
  1805. {
  1806. sector_t stripe, stripe2;
  1807. sector_t chunk_number;
  1808. unsigned int chunk_offset;
  1809. int pd_idx, qd_idx;
  1810. int ddf_layout = 0;
  1811. sector_t new_sector;
  1812. int algorithm = previous ? conf->prev_algo
  1813. : conf->algorithm;
  1814. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1815. : conf->chunk_sectors;
  1816. int raid_disks = previous ? conf->previous_raid_disks
  1817. : conf->raid_disks;
  1818. int data_disks = raid_disks - conf->max_degraded;
  1819. /* First compute the information on this sector */
  1820. /*
  1821. * Compute the chunk number and the sector offset inside the chunk
  1822. */
  1823. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1824. chunk_number = r_sector;
  1825. /*
  1826. * Compute the stripe number
  1827. */
  1828. stripe = chunk_number;
  1829. *dd_idx = sector_div(stripe, data_disks);
  1830. stripe2 = stripe;
  1831. /*
  1832. * Select the parity disk based on the user selected algorithm.
  1833. */
  1834. pd_idx = qd_idx = -1;
  1835. switch(conf->level) {
  1836. case 4:
  1837. pd_idx = data_disks;
  1838. break;
  1839. case 5:
  1840. switch (algorithm) {
  1841. case ALGORITHM_LEFT_ASYMMETRIC:
  1842. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1843. if (*dd_idx >= pd_idx)
  1844. (*dd_idx)++;
  1845. break;
  1846. case ALGORITHM_RIGHT_ASYMMETRIC:
  1847. pd_idx = sector_div(stripe2, raid_disks);
  1848. if (*dd_idx >= pd_idx)
  1849. (*dd_idx)++;
  1850. break;
  1851. case ALGORITHM_LEFT_SYMMETRIC:
  1852. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1853. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1854. break;
  1855. case ALGORITHM_RIGHT_SYMMETRIC:
  1856. pd_idx = sector_div(stripe2, raid_disks);
  1857. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1858. break;
  1859. case ALGORITHM_PARITY_0:
  1860. pd_idx = 0;
  1861. (*dd_idx)++;
  1862. break;
  1863. case ALGORITHM_PARITY_N:
  1864. pd_idx = data_disks;
  1865. break;
  1866. default:
  1867. BUG();
  1868. }
  1869. break;
  1870. case 6:
  1871. switch (algorithm) {
  1872. case ALGORITHM_LEFT_ASYMMETRIC:
  1873. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1874. qd_idx = pd_idx + 1;
  1875. if (pd_idx == raid_disks-1) {
  1876. (*dd_idx)++; /* Q D D D P */
  1877. qd_idx = 0;
  1878. } else if (*dd_idx >= pd_idx)
  1879. (*dd_idx) += 2; /* D D P Q D */
  1880. break;
  1881. case ALGORITHM_RIGHT_ASYMMETRIC:
  1882. pd_idx = sector_div(stripe2, raid_disks);
  1883. qd_idx = pd_idx + 1;
  1884. if (pd_idx == raid_disks-1) {
  1885. (*dd_idx)++; /* Q D D D P */
  1886. qd_idx = 0;
  1887. } else if (*dd_idx >= pd_idx)
  1888. (*dd_idx) += 2; /* D D P Q D */
  1889. break;
  1890. case ALGORITHM_LEFT_SYMMETRIC:
  1891. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1892. qd_idx = (pd_idx + 1) % raid_disks;
  1893. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1894. break;
  1895. case ALGORITHM_RIGHT_SYMMETRIC:
  1896. pd_idx = sector_div(stripe2, raid_disks);
  1897. qd_idx = (pd_idx + 1) % raid_disks;
  1898. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1899. break;
  1900. case ALGORITHM_PARITY_0:
  1901. pd_idx = 0;
  1902. qd_idx = 1;
  1903. (*dd_idx) += 2;
  1904. break;
  1905. case ALGORITHM_PARITY_N:
  1906. pd_idx = data_disks;
  1907. qd_idx = data_disks + 1;
  1908. break;
  1909. case ALGORITHM_ROTATING_ZERO_RESTART:
  1910. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1911. * of blocks for computing Q is different.
  1912. */
  1913. pd_idx = sector_div(stripe2, raid_disks);
  1914. qd_idx = pd_idx + 1;
  1915. if (pd_idx == raid_disks-1) {
  1916. (*dd_idx)++; /* Q D D D P */
  1917. qd_idx = 0;
  1918. } else if (*dd_idx >= pd_idx)
  1919. (*dd_idx) += 2; /* D D P Q D */
  1920. ddf_layout = 1;
  1921. break;
  1922. case ALGORITHM_ROTATING_N_RESTART:
  1923. /* Same a left_asymmetric, by first stripe is
  1924. * D D D P Q rather than
  1925. * Q D D D P
  1926. */
  1927. stripe2 += 1;
  1928. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1929. qd_idx = pd_idx + 1;
  1930. if (pd_idx == raid_disks-1) {
  1931. (*dd_idx)++; /* Q D D D P */
  1932. qd_idx = 0;
  1933. } else if (*dd_idx >= pd_idx)
  1934. (*dd_idx) += 2; /* D D P Q D */
  1935. ddf_layout = 1;
  1936. break;
  1937. case ALGORITHM_ROTATING_N_CONTINUE:
  1938. /* Same as left_symmetric but Q is before P */
  1939. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1940. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1941. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1942. ddf_layout = 1;
  1943. break;
  1944. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1945. /* RAID5 left_asymmetric, with Q on last device */
  1946. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1947. if (*dd_idx >= pd_idx)
  1948. (*dd_idx)++;
  1949. qd_idx = raid_disks - 1;
  1950. break;
  1951. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1952. pd_idx = sector_div(stripe2, raid_disks-1);
  1953. if (*dd_idx >= pd_idx)
  1954. (*dd_idx)++;
  1955. qd_idx = raid_disks - 1;
  1956. break;
  1957. case ALGORITHM_LEFT_SYMMETRIC_6:
  1958. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1959. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1960. qd_idx = raid_disks - 1;
  1961. break;
  1962. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1963. pd_idx = sector_div(stripe2, raid_disks-1);
  1964. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1965. qd_idx = raid_disks - 1;
  1966. break;
  1967. case ALGORITHM_PARITY_0_6:
  1968. pd_idx = 0;
  1969. (*dd_idx)++;
  1970. qd_idx = raid_disks - 1;
  1971. break;
  1972. default:
  1973. BUG();
  1974. }
  1975. break;
  1976. }
  1977. if (sh) {
  1978. sh->pd_idx = pd_idx;
  1979. sh->qd_idx = qd_idx;
  1980. sh->ddf_layout = ddf_layout;
  1981. }
  1982. /*
  1983. * Finally, compute the new sector number
  1984. */
  1985. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1986. return new_sector;
  1987. }
  1988. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
  1989. {
  1990. struct r5conf *conf = sh->raid_conf;
  1991. int raid_disks = sh->disks;
  1992. int data_disks = raid_disks - conf->max_degraded;
  1993. sector_t new_sector = sh->sector, check;
  1994. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1995. : conf->chunk_sectors;
  1996. int algorithm = previous ? conf->prev_algo
  1997. : conf->algorithm;
  1998. sector_t stripe;
  1999. int chunk_offset;
  2000. sector_t chunk_number;
  2001. int dummy1, dd_idx = i;
  2002. sector_t r_sector;
  2003. struct stripe_head sh2;
  2004. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  2005. stripe = new_sector;
  2006. if (i == sh->pd_idx)
  2007. return 0;
  2008. switch(conf->level) {
  2009. case 4: break;
  2010. case 5:
  2011. switch (algorithm) {
  2012. case ALGORITHM_LEFT_ASYMMETRIC:
  2013. case ALGORITHM_RIGHT_ASYMMETRIC:
  2014. if (i > sh->pd_idx)
  2015. i--;
  2016. break;
  2017. case ALGORITHM_LEFT_SYMMETRIC:
  2018. case ALGORITHM_RIGHT_SYMMETRIC:
  2019. if (i < sh->pd_idx)
  2020. i += raid_disks;
  2021. i -= (sh->pd_idx + 1);
  2022. break;
  2023. case ALGORITHM_PARITY_0:
  2024. i -= 1;
  2025. break;
  2026. case ALGORITHM_PARITY_N:
  2027. break;
  2028. default:
  2029. BUG();
  2030. }
  2031. break;
  2032. case 6:
  2033. if (i == sh->qd_idx)
  2034. return 0; /* It is the Q disk */
  2035. switch (algorithm) {
  2036. case ALGORITHM_LEFT_ASYMMETRIC:
  2037. case ALGORITHM_RIGHT_ASYMMETRIC:
  2038. case ALGORITHM_ROTATING_ZERO_RESTART:
  2039. case ALGORITHM_ROTATING_N_RESTART:
  2040. if (sh->pd_idx == raid_disks-1)
  2041. i--; /* Q D D D P */
  2042. else if (i > sh->pd_idx)
  2043. i -= 2; /* D D P Q D */
  2044. break;
  2045. case ALGORITHM_LEFT_SYMMETRIC:
  2046. case ALGORITHM_RIGHT_SYMMETRIC:
  2047. if (sh->pd_idx == raid_disks-1)
  2048. i--; /* Q D D D P */
  2049. else {
  2050. /* D D P Q D */
  2051. if (i < sh->pd_idx)
  2052. i += raid_disks;
  2053. i -= (sh->pd_idx + 2);
  2054. }
  2055. break;
  2056. case ALGORITHM_PARITY_0:
  2057. i -= 2;
  2058. break;
  2059. case ALGORITHM_PARITY_N:
  2060. break;
  2061. case ALGORITHM_ROTATING_N_CONTINUE:
  2062. /* Like left_symmetric, but P is before Q */
  2063. if (sh->pd_idx == 0)
  2064. i--; /* P D D D Q */
  2065. else {
  2066. /* D D Q P D */
  2067. if (i < sh->pd_idx)
  2068. i += raid_disks;
  2069. i -= (sh->pd_idx + 1);
  2070. }
  2071. break;
  2072. case ALGORITHM_LEFT_ASYMMETRIC_6:
  2073. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  2074. if (i > sh->pd_idx)
  2075. i--;
  2076. break;
  2077. case ALGORITHM_LEFT_SYMMETRIC_6:
  2078. case ALGORITHM_RIGHT_SYMMETRIC_6:
  2079. if (i < sh->pd_idx)
  2080. i += data_disks + 1;
  2081. i -= (sh->pd_idx + 1);
  2082. break;
  2083. case ALGORITHM_PARITY_0_6:
  2084. i -= 1;
  2085. break;
  2086. default:
  2087. BUG();
  2088. }
  2089. break;
  2090. }
  2091. chunk_number = stripe * data_disks + i;
  2092. r_sector = chunk_number * sectors_per_chunk + chunk_offset;
  2093. check = raid5_compute_sector(conf, r_sector,
  2094. previous, &dummy1, &sh2);
  2095. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  2096. || sh2.qd_idx != sh->qd_idx) {
  2097. printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
  2098. mdname(conf->mddev));
  2099. return 0;
  2100. }
  2101. return r_sector;
  2102. }
  2103. static void
  2104. schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
  2105. int rcw, int expand)
  2106. {
  2107. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  2108. struct r5conf *conf = sh->raid_conf;
  2109. int level = conf->level;
  2110. if (rcw) {
  2111. for (i = disks; i--; ) {
  2112. struct r5dev *dev = &sh->dev[i];
  2113. if (dev->towrite) {
  2114. set_bit(R5_LOCKED, &dev->flags);
  2115. set_bit(R5_Wantdrain, &dev->flags);
  2116. if (!expand)
  2117. clear_bit(R5_UPTODATE, &dev->flags);
  2118. s->locked++;
  2119. }
  2120. }
  2121. /* if we are not expanding this is a proper write request, and
  2122. * there will be bios with new data to be drained into the
  2123. * stripe cache
  2124. */
  2125. if (!expand) {
  2126. if (!s->locked)
  2127. /* False alarm, nothing to do */
  2128. return;
  2129. sh->reconstruct_state = reconstruct_state_drain_run;
  2130. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2131. } else
  2132. sh->reconstruct_state = reconstruct_state_run;
  2133. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  2134. if (s->locked + conf->max_degraded == disks)
  2135. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  2136. atomic_inc(&conf->pending_full_writes);
  2137. } else {
  2138. BUG_ON(level == 6);
  2139. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  2140. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  2141. for (i = disks; i--; ) {
  2142. struct r5dev *dev = &sh->dev[i];
  2143. if (i == pd_idx)
  2144. continue;
  2145. if (dev->towrite &&
  2146. (test_bit(R5_UPTODATE, &dev->flags) ||
  2147. test_bit(R5_Wantcompute, &dev->flags))) {
  2148. set_bit(R5_Wantdrain, &dev->flags);
  2149. set_bit(R5_LOCKED, &dev->flags);
  2150. clear_bit(R5_UPTODATE, &dev->flags);
  2151. s->locked++;
  2152. }
  2153. }
  2154. if (!s->locked)
  2155. /* False alarm - nothing to do */
  2156. return;
  2157. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  2158. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  2159. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2160. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  2161. }
  2162. /* keep the parity disk(s) locked while asynchronous operations
  2163. * are in flight
  2164. */
  2165. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  2166. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2167. s->locked++;
  2168. if (level == 6) {
  2169. int qd_idx = sh->qd_idx;
  2170. struct r5dev *dev = &sh->dev[qd_idx];
  2171. set_bit(R5_LOCKED, &dev->flags);
  2172. clear_bit(R5_UPTODATE, &dev->flags);
  2173. s->locked++;
  2174. }
  2175. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  2176. __func__, (unsigned long long)sh->sector,
  2177. s->locked, s->ops_request);
  2178. }
  2179. /*
  2180. * Each stripe/dev can have one or more bion attached.
  2181. * toread/towrite point to the first in a chain.
  2182. * The bi_next chain must be in order.
  2183. */
  2184. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  2185. {
  2186. struct bio **bip;
  2187. struct r5conf *conf = sh->raid_conf;
  2188. int firstwrite=0;
  2189. pr_debug("adding bi b#%llu to stripe s#%llu\n",
  2190. (unsigned long long)bi->bi_sector,
  2191. (unsigned long long)sh->sector);
  2192. /*
  2193. * If several bio share a stripe. The bio bi_phys_segments acts as a
  2194. * reference count to avoid race. The reference count should already be
  2195. * increased before this function is called (for example, in
  2196. * make_request()), so other bio sharing this stripe will not free the
  2197. * stripe. If a stripe is owned by one stripe, the stripe lock will
  2198. * protect it.
  2199. */
  2200. spin_lock_irq(&sh->stripe_lock);
  2201. if (forwrite) {
  2202. bip = &sh->dev[dd_idx].towrite;
  2203. if (*bip == NULL)
  2204. firstwrite = 1;
  2205. } else
  2206. bip = &sh->dev[dd_idx].toread;
  2207. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  2208. if (bio_end_sector(*bip) > bi->bi_sector)
  2209. goto overlap;
  2210. bip = & (*bip)->bi_next;
  2211. }
  2212. if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
  2213. goto overlap;
  2214. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  2215. if (*bip)
  2216. bi->bi_next = *bip;
  2217. *bip = bi;
  2218. raid5_inc_bi_active_stripes(bi);
  2219. if (forwrite) {
  2220. /* check if page is covered */
  2221. sector_t sector = sh->dev[dd_idx].sector;
  2222. for (bi=sh->dev[dd_idx].towrite;
  2223. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  2224. bi && bi->bi_sector <= sector;
  2225. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  2226. if (bio_end_sector(bi) >= sector)
  2227. sector = bio_end_sector(bi);
  2228. }
  2229. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  2230. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  2231. }
  2232. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  2233. (unsigned long long)(*bip)->bi_sector,
  2234. (unsigned long long)sh->sector, dd_idx);
  2235. spin_unlock_irq(&sh->stripe_lock);
  2236. if (conf->mddev->bitmap && firstwrite) {
  2237. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  2238. STRIPE_SECTORS, 0);
  2239. sh->bm_seq = conf->seq_flush+1;
  2240. set_bit(STRIPE_BIT_DELAY, &sh->state);
  2241. }
  2242. return 1;
  2243. overlap:
  2244. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  2245. spin_unlock_irq(&sh->stripe_lock);
  2246. return 0;
  2247. }
  2248. static void end_reshape(struct r5conf *conf);
  2249. static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
  2250. struct stripe_head *sh)
  2251. {
  2252. int sectors_per_chunk =
  2253. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  2254. int dd_idx;
  2255. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  2256. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  2257. raid5_compute_sector(conf,
  2258. stripe * (disks - conf->max_degraded)
  2259. *sectors_per_chunk + chunk_offset,
  2260. previous,
  2261. &dd_idx, sh);
  2262. }
  2263. static void
  2264. handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
  2265. struct stripe_head_state *s, int disks,
  2266. struct bio **return_bi)
  2267. {
  2268. int i;
  2269. for (i = disks; i--; ) {
  2270. struct bio *bi;
  2271. int bitmap_end = 0;
  2272. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  2273. struct md_rdev *rdev;
  2274. rcu_read_lock();
  2275. rdev = rcu_dereference(conf->disks[i].rdev);
  2276. if (rdev && test_bit(In_sync, &rdev->flags))
  2277. atomic_inc(&rdev->nr_pending);
  2278. else
  2279. rdev = NULL;
  2280. rcu_read_unlock();
  2281. if (rdev) {
  2282. if (!rdev_set_badblocks(
  2283. rdev,
  2284. sh->sector,
  2285. STRIPE_SECTORS, 0))
  2286. md_error(conf->mddev, rdev);
  2287. rdev_dec_pending(rdev, conf->mddev);
  2288. }
  2289. }
  2290. spin_lock_irq(&sh->stripe_lock);
  2291. /* fail all writes first */
  2292. bi = sh->dev[i].towrite;
  2293. sh->dev[i].towrite = NULL;
  2294. spin_unlock_irq(&sh->stripe_lock);
  2295. if (bi)
  2296. bitmap_end = 1;
  2297. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2298. wake_up(&conf->wait_for_overlap);
  2299. while (bi && bi->bi_sector <
  2300. sh->dev[i].sector + STRIPE_SECTORS) {
  2301. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  2302. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2303. if (!raid5_dec_bi_active_stripes(bi)) {
  2304. md_write_end(conf->mddev);
  2305. bi->bi_next = *return_bi;
  2306. *return_bi = bi;
  2307. }
  2308. bi = nextbi;
  2309. }
  2310. if (bitmap_end)
  2311. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2312. STRIPE_SECTORS, 0, 0);
  2313. bitmap_end = 0;
  2314. /* and fail all 'written' */
  2315. bi = sh->dev[i].written;
  2316. sh->dev[i].written = NULL;
  2317. if (bi) bitmap_end = 1;
  2318. while (bi && bi->bi_sector <
  2319. sh->dev[i].sector + STRIPE_SECTORS) {
  2320. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  2321. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2322. if (!raid5_dec_bi_active_stripes(bi)) {
  2323. md_write_end(conf->mddev);
  2324. bi->bi_next = *return_bi;
  2325. *return_bi = bi;
  2326. }
  2327. bi = bi2;
  2328. }
  2329. /* fail any reads if this device is non-operational and
  2330. * the data has not reached the cache yet.
  2331. */
  2332. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  2333. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  2334. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  2335. spin_lock_irq(&sh->stripe_lock);
  2336. bi = sh->dev[i].toread;
  2337. sh->dev[i].toread = NULL;
  2338. spin_unlock_irq(&sh->stripe_lock);
  2339. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2340. wake_up(&conf->wait_for_overlap);
  2341. while (bi && bi->bi_sector <
  2342. sh->dev[i].sector + STRIPE_SECTORS) {
  2343. struct bio *nextbi =
  2344. r5_next_bio(bi, sh->dev[i].sector);
  2345. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2346. if (!raid5_dec_bi_active_stripes(bi)) {
  2347. bi->bi_next = *return_bi;
  2348. *return_bi = bi;
  2349. }
  2350. bi = nextbi;
  2351. }
  2352. }
  2353. if (bitmap_end)
  2354. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2355. STRIPE_SECTORS, 0, 0);
  2356. /* If we were in the middle of a write the parity block might
  2357. * still be locked - so just clear all R5_LOCKED flags
  2358. */
  2359. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2360. }
  2361. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2362. if (atomic_dec_and_test(&conf->pending_full_writes))
  2363. md_wakeup_thread(conf->mddev->thread);
  2364. }
  2365. static void
  2366. handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
  2367. struct stripe_head_state *s)
  2368. {
  2369. int abort = 0;
  2370. int i;
  2371. clear_bit(STRIPE_SYNCING, &sh->state);
  2372. if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
  2373. wake_up(&conf->wait_for_overlap);
  2374. s->syncing = 0;
  2375. s->replacing = 0;
  2376. /* There is nothing more to do for sync/check/repair.
  2377. * Don't even need to abort as that is handled elsewhere
  2378. * if needed, and not always wanted e.g. if there is a known
  2379. * bad block here.
  2380. * For recover/replace we need to record a bad block on all
  2381. * non-sync devices, or abort the recovery
  2382. */
  2383. if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
  2384. /* During recovery devices cannot be removed, so
  2385. * locking and refcounting of rdevs is not needed
  2386. */
  2387. for (i = 0; i < conf->raid_disks; i++) {
  2388. struct md_rdev *rdev = conf->disks[i].rdev;
  2389. if (rdev
  2390. && !test_bit(Faulty, &rdev->flags)
  2391. && !test_bit(In_sync, &rdev->flags)
  2392. && !rdev_set_badblocks(rdev, sh->sector,
  2393. STRIPE_SECTORS, 0))
  2394. abort = 1;
  2395. rdev = conf->disks[i].replacement;
  2396. if (rdev
  2397. && !test_bit(Faulty, &rdev->flags)
  2398. && !test_bit(In_sync, &rdev->flags)
  2399. && !rdev_set_badblocks(rdev, sh->sector,
  2400. STRIPE_SECTORS, 0))
  2401. abort = 1;
  2402. }
  2403. if (abort)
  2404. conf->recovery_disabled =
  2405. conf->mddev->recovery_disabled;
  2406. }
  2407. md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
  2408. }
  2409. static int want_replace(struct stripe_head *sh, int disk_idx)
  2410. {
  2411. struct md_rdev *rdev;
  2412. int rv = 0;
  2413. /* Doing recovery so rcu locking not required */
  2414. rdev = sh->raid_conf->disks[disk_idx].replacement;
  2415. if (rdev
  2416. && !test_bit(Faulty, &rdev->flags)
  2417. && !test_bit(In_sync, &rdev->flags)
  2418. && (rdev->recovery_offset <= sh->sector
  2419. || rdev->mddev->recovery_cp <= sh->sector))
  2420. rv = 1;
  2421. return rv;
  2422. }
  2423. /* fetch_block - checks the given member device to see if its data needs
  2424. * to be read or computed to satisfy a request.
  2425. *
  2426. * Returns 1 when no more member devices need to be checked, otherwise returns
  2427. * 0 to tell the loop in handle_stripe_fill to continue
  2428. */
  2429. static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
  2430. int disk_idx, int disks)
  2431. {
  2432. struct r5dev *dev = &sh->dev[disk_idx];
  2433. struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
  2434. &sh->dev[s->failed_num[1]] };
  2435. /* is the data in this block needed, and can we get it? */
  2436. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2437. !test_bit(R5_UPTODATE, &dev->flags) &&
  2438. (dev->toread ||
  2439. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2440. s->syncing || s->expanding ||
  2441. (s->replacing && want_replace(sh, disk_idx)) ||
  2442. (s->failed >= 1 && fdev[0]->toread) ||
  2443. (s->failed >= 2 && fdev[1]->toread) ||
  2444. (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
  2445. !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
  2446. (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
  2447. /* we would like to get this block, possibly by computing it,
  2448. * otherwise read it if the backing disk is insync
  2449. */
  2450. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  2451. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  2452. if ((s->uptodate == disks - 1) &&
  2453. (s->failed && (disk_idx == s->failed_num[0] ||
  2454. disk_idx == s->failed_num[1]))) {
  2455. /* have disk failed, and we're requested to fetch it;
  2456. * do compute it
  2457. */
  2458. pr_debug("Computing stripe %llu block %d\n",
  2459. (unsigned long long)sh->sector, disk_idx);
  2460. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2461. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2462. set_bit(R5_Wantcompute, &dev->flags);
  2463. sh->ops.target = disk_idx;
  2464. sh->ops.target2 = -1; /* no 2nd target */
  2465. s->req_compute = 1;
  2466. /* Careful: from this point on 'uptodate' is in the eye
  2467. * of raid_run_ops which services 'compute' operations
  2468. * before writes. R5_Wantcompute flags a block that will
  2469. * be R5_UPTODATE by the time it is needed for a
  2470. * subsequent operation.
  2471. */
  2472. s->uptodate++;
  2473. return 1;
  2474. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  2475. /* Computing 2-failure is *very* expensive; only
  2476. * do it if failed >= 2
  2477. */
  2478. int other;
  2479. for (other = disks; other--; ) {
  2480. if (other == disk_idx)
  2481. continue;
  2482. if (!test_bit(R5_UPTODATE,
  2483. &sh->dev[other].flags))
  2484. break;
  2485. }
  2486. BUG_ON(other < 0);
  2487. pr_debug("Computing stripe %llu blocks %d,%d\n",
  2488. (unsigned long long)sh->sector,
  2489. disk_idx, other);
  2490. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2491. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2492. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  2493. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  2494. sh->ops.target = disk_idx;
  2495. sh->ops.target2 = other;
  2496. s->uptodate += 2;
  2497. s->req_compute = 1;
  2498. return 1;
  2499. } else if (test_bit(R5_Insync, &dev->flags)) {
  2500. set_bit(R5_LOCKED, &dev->flags);
  2501. set_bit(R5_Wantread, &dev->flags);
  2502. s->locked++;
  2503. pr_debug("Reading block %d (sync=%d)\n",
  2504. disk_idx, s->syncing);
  2505. }
  2506. }
  2507. return 0;
  2508. }
  2509. /**
  2510. * handle_stripe_fill - read or compute data to satisfy pending requests.
  2511. */
  2512. static void handle_stripe_fill(struct stripe_head *sh,
  2513. struct stripe_head_state *s,
  2514. int disks)
  2515. {
  2516. int i;
  2517. /* look for blocks to read/compute, skip this if a compute
  2518. * is already in flight, or if the stripe contents are in the
  2519. * midst of changing due to a write
  2520. */
  2521. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2522. !sh->reconstruct_state)
  2523. for (i = disks; i--; )
  2524. if (fetch_block(sh, s, i, disks))
  2525. break;
  2526. set_bit(STRIPE_HANDLE, &sh->state);
  2527. }
  2528. /* handle_stripe_clean_event
  2529. * any written block on an uptodate or failed drive can be returned.
  2530. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  2531. * never LOCKED, so we don't need to test 'failed' directly.
  2532. */
  2533. static void handle_stripe_clean_event(struct r5conf *conf,
  2534. struct stripe_head *sh, int disks, struct bio **return_bi)
  2535. {
  2536. int i;
  2537. struct r5dev *dev;
  2538. int discard_pending = 0;
  2539. for (i = disks; i--; )
  2540. if (sh->dev[i].written) {
  2541. dev = &sh->dev[i];
  2542. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2543. (test_bit(R5_UPTODATE, &dev->flags) ||
  2544. test_bit(R5_Discard, &dev->flags))) {
  2545. /* We can return any write requests */
  2546. struct bio *wbi, *wbi2;
  2547. pr_debug("Return write for disc %d\n", i);
  2548. if (test_and_clear_bit(R5_Discard, &dev->flags))
  2549. clear_bit(R5_UPTODATE, &dev->flags);
  2550. wbi = dev->written;
  2551. dev->written = NULL;
  2552. while (wbi && wbi->bi_sector <
  2553. dev->sector + STRIPE_SECTORS) {
  2554. wbi2 = r5_next_bio(wbi, dev->sector);
  2555. if (!raid5_dec_bi_active_stripes(wbi)) {
  2556. md_write_end(conf->mddev);
  2557. wbi->bi_next = *return_bi;
  2558. *return_bi = wbi;
  2559. }
  2560. wbi = wbi2;
  2561. }
  2562. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2563. STRIPE_SECTORS,
  2564. !test_bit(STRIPE_DEGRADED, &sh->state),
  2565. 0);
  2566. } else if (test_bit(R5_Discard, &dev->flags))
  2567. discard_pending = 1;
  2568. }
  2569. if (!discard_pending &&
  2570. test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
  2571. clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
  2572. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2573. if (sh->qd_idx >= 0) {
  2574. clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
  2575. clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
  2576. }
  2577. /* now that discard is done we can proceed with any sync */
  2578. clear_bit(STRIPE_DISCARD, &sh->state);
  2579. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
  2580. set_bit(STRIPE_HANDLE, &sh->state);
  2581. }
  2582. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2583. if (atomic_dec_and_test(&conf->pending_full_writes))
  2584. md_wakeup_thread(conf->mddev->thread);
  2585. }
  2586. static void handle_stripe_dirtying(struct r5conf *conf,
  2587. struct stripe_head *sh,
  2588. struct stripe_head_state *s,
  2589. int disks)
  2590. {
  2591. int rmw = 0, rcw = 0, i;
  2592. sector_t recovery_cp = conf->mddev->recovery_cp;
  2593. /* RAID6 requires 'rcw' in current implementation.
  2594. * Otherwise, check whether resync is now happening or should start.
  2595. * If yes, then the array is dirty (after unclean shutdown or
  2596. * initial creation), so parity in some stripes might be inconsistent.
  2597. * In this case, we need to always do reconstruct-write, to ensure
  2598. * that in case of drive failure or read-error correction, we
  2599. * generate correct data from the parity.
  2600. */
  2601. if (conf->max_degraded == 2 ||
  2602. (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
  2603. /* Calculate the real rcw later - for now make it
  2604. * look like rcw is cheaper
  2605. */
  2606. rcw = 1; rmw = 2;
  2607. pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
  2608. conf->max_degraded, (unsigned long long)recovery_cp,
  2609. (unsigned long long)sh->sector);
  2610. } else for (i = disks; i--; ) {
  2611. /* would I have to read this buffer for read_modify_write */
  2612. struct r5dev *dev = &sh->dev[i];
  2613. if ((dev->towrite || i == sh->pd_idx) &&
  2614. !test_bit(R5_LOCKED, &dev->flags) &&
  2615. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2616. test_bit(R5_Wantcompute, &dev->flags))) {
  2617. if (test_bit(R5_Insync, &dev->flags))
  2618. rmw++;
  2619. else
  2620. rmw += 2*disks; /* cannot read it */
  2621. }
  2622. /* Would I have to read this buffer for reconstruct_write */
  2623. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2624. !test_bit(R5_LOCKED, &dev->flags) &&
  2625. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2626. test_bit(R5_Wantcompute, &dev->flags))) {
  2627. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2628. else
  2629. rcw += 2*disks;
  2630. }
  2631. }
  2632. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2633. (unsigned long long)sh->sector, rmw, rcw);
  2634. set_bit(STRIPE_HANDLE, &sh->state);
  2635. if (rmw < rcw && rmw > 0) {
  2636. /* prefer read-modify-write, but need to get some data */
  2637. if (conf->mddev->queue)
  2638. blk_add_trace_msg(conf->mddev->queue,
  2639. "raid5 rmw %llu %d",
  2640. (unsigned long long)sh->sector, rmw);
  2641. for (i = disks; i--; ) {
  2642. struct r5dev *dev = &sh->dev[i];
  2643. if ((dev->towrite || i == sh->pd_idx) &&
  2644. !test_bit(R5_LOCKED, &dev->flags) &&
  2645. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2646. test_bit(R5_Wantcompute, &dev->flags)) &&
  2647. test_bit(R5_Insync, &dev->flags)) {
  2648. if (
  2649. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2650. pr_debug("Read_old block "
  2651. "%d for r-m-w\n", i);
  2652. set_bit(R5_LOCKED, &dev->flags);
  2653. set_bit(R5_Wantread, &dev->flags);
  2654. s->locked++;
  2655. } else {
  2656. set_bit(STRIPE_DELAYED, &sh->state);
  2657. set_bit(STRIPE_HANDLE, &sh->state);
  2658. }
  2659. }
  2660. }
  2661. }
  2662. if (rcw <= rmw && rcw > 0) {
  2663. /* want reconstruct write, but need to get some data */
  2664. int qread =0;
  2665. rcw = 0;
  2666. for (i = disks; i--; ) {
  2667. struct r5dev *dev = &sh->dev[i];
  2668. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2669. i != sh->pd_idx && i != sh->qd_idx &&
  2670. !test_bit(R5_LOCKED, &dev->flags) &&
  2671. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2672. test_bit(R5_Wantcompute, &dev->flags))) {
  2673. rcw++;
  2674. if (!test_bit(R5_Insync, &dev->flags))
  2675. continue; /* it's a failed drive */
  2676. if (
  2677. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2678. pr_debug("Read_old block "
  2679. "%d for Reconstruct\n", i);
  2680. set_bit(R5_LOCKED, &dev->flags);
  2681. set_bit(R5_Wantread, &dev->flags);
  2682. s->locked++;
  2683. qread++;
  2684. } else {
  2685. set_bit(STRIPE_DELAYED, &sh->state);
  2686. set_bit(STRIPE_HANDLE, &sh->state);
  2687. }
  2688. }
  2689. }
  2690. if (rcw && conf->mddev->queue)
  2691. blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
  2692. (unsigned long long)sh->sector,
  2693. rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
  2694. }
  2695. /* now if nothing is locked, and if we have enough data,
  2696. * we can start a write request
  2697. */
  2698. /* since handle_stripe can be called at any time we need to handle the
  2699. * case where a compute block operation has been submitted and then a
  2700. * subsequent call wants to start a write request. raid_run_ops only
  2701. * handles the case where compute block and reconstruct are requested
  2702. * simultaneously. If this is not the case then new writes need to be
  2703. * held off until the compute completes.
  2704. */
  2705. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2706. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2707. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2708. schedule_reconstruction(sh, s, rcw == 0, 0);
  2709. }
  2710. static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
  2711. struct stripe_head_state *s, int disks)
  2712. {
  2713. struct r5dev *dev = NULL;
  2714. set_bit(STRIPE_HANDLE, &sh->state);
  2715. switch (sh->check_state) {
  2716. case check_state_idle:
  2717. /* start a new check operation if there are no failures */
  2718. if (s->failed == 0) {
  2719. BUG_ON(s->uptodate != disks);
  2720. sh->check_state = check_state_run;
  2721. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2722. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2723. s->uptodate--;
  2724. break;
  2725. }
  2726. dev = &sh->dev[s->failed_num[0]];
  2727. /* fall through */
  2728. case check_state_compute_result:
  2729. sh->check_state = check_state_idle;
  2730. if (!dev)
  2731. dev = &sh->dev[sh->pd_idx];
  2732. /* check that a write has not made the stripe insync */
  2733. if (test_bit(STRIPE_INSYNC, &sh->state))
  2734. break;
  2735. /* either failed parity check, or recovery is happening */
  2736. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2737. BUG_ON(s->uptodate != disks);
  2738. set_bit(R5_LOCKED, &dev->flags);
  2739. s->locked++;
  2740. set_bit(R5_Wantwrite, &dev->flags);
  2741. clear_bit(STRIPE_DEGRADED, &sh->state);
  2742. set_bit(STRIPE_INSYNC, &sh->state);
  2743. break;
  2744. case check_state_run:
  2745. break; /* we will be called again upon completion */
  2746. case check_state_check_result:
  2747. sh->check_state = check_state_idle;
  2748. /* if a failure occurred during the check operation, leave
  2749. * STRIPE_INSYNC not set and let the stripe be handled again
  2750. */
  2751. if (s->failed)
  2752. break;
  2753. /* handle a successful check operation, if parity is correct
  2754. * we are done. Otherwise update the mismatch count and repair
  2755. * parity if !MD_RECOVERY_CHECK
  2756. */
  2757. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  2758. /* parity is correct (on disc,
  2759. * not in buffer any more)
  2760. */
  2761. set_bit(STRIPE_INSYNC, &sh->state);
  2762. else {
  2763. atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
  2764. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2765. /* don't try to repair!! */
  2766. set_bit(STRIPE_INSYNC, &sh->state);
  2767. else {
  2768. sh->check_state = check_state_compute_run;
  2769. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2770. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2771. set_bit(R5_Wantcompute,
  2772. &sh->dev[sh->pd_idx].flags);
  2773. sh->ops.target = sh->pd_idx;
  2774. sh->ops.target2 = -1;
  2775. s->uptodate++;
  2776. }
  2777. }
  2778. break;
  2779. case check_state_compute_run:
  2780. break;
  2781. default:
  2782. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2783. __func__, sh->check_state,
  2784. (unsigned long long) sh->sector);
  2785. BUG();
  2786. }
  2787. }
  2788. static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
  2789. struct stripe_head_state *s,
  2790. int disks)
  2791. {
  2792. int pd_idx = sh->pd_idx;
  2793. int qd_idx = sh->qd_idx;
  2794. struct r5dev *dev;
  2795. set_bit(STRIPE_HANDLE, &sh->state);
  2796. BUG_ON(s->failed > 2);
  2797. /* Want to check and possibly repair P and Q.
  2798. * However there could be one 'failed' device, in which
  2799. * case we can only check one of them, possibly using the
  2800. * other to generate missing data
  2801. */
  2802. switch (sh->check_state) {
  2803. case check_state_idle:
  2804. /* start a new check operation if there are < 2 failures */
  2805. if (s->failed == s->q_failed) {
  2806. /* The only possible failed device holds Q, so it
  2807. * makes sense to check P (If anything else were failed,
  2808. * we would have used P to recreate it).
  2809. */
  2810. sh->check_state = check_state_run;
  2811. }
  2812. if (!s->q_failed && s->failed < 2) {
  2813. /* Q is not failed, and we didn't use it to generate
  2814. * anything, so it makes sense to check it
  2815. */
  2816. if (sh->check_state == check_state_run)
  2817. sh->check_state = check_state_run_pq;
  2818. else
  2819. sh->check_state = check_state_run_q;
  2820. }
  2821. /* discard potentially stale zero_sum_result */
  2822. sh->ops.zero_sum_result = 0;
  2823. if (sh->check_state == check_state_run) {
  2824. /* async_xor_zero_sum destroys the contents of P */
  2825. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2826. s->uptodate--;
  2827. }
  2828. if (sh->check_state >= check_state_run &&
  2829. sh->check_state <= check_state_run_pq) {
  2830. /* async_syndrome_zero_sum preserves P and Q, so
  2831. * no need to mark them !uptodate here
  2832. */
  2833. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2834. break;
  2835. }
  2836. /* we have 2-disk failure */
  2837. BUG_ON(s->failed != 2);
  2838. /* fall through */
  2839. case check_state_compute_result:
  2840. sh->check_state = check_state_idle;
  2841. /* check that a write has not made the stripe insync */
  2842. if (test_bit(STRIPE_INSYNC, &sh->state))
  2843. break;
  2844. /* now write out any block on a failed drive,
  2845. * or P or Q if they were recomputed
  2846. */
  2847. BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
  2848. if (s->failed == 2) {
  2849. dev = &sh->dev[s->failed_num[1]];
  2850. s->locked++;
  2851. set_bit(R5_LOCKED, &dev->flags);
  2852. set_bit(R5_Wantwrite, &dev->flags);
  2853. }
  2854. if (s->failed >= 1) {
  2855. dev = &sh->dev[s->failed_num[0]];
  2856. s->locked++;
  2857. set_bit(R5_LOCKED, &dev->flags);
  2858. set_bit(R5_Wantwrite, &dev->flags);
  2859. }
  2860. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2861. dev = &sh->dev[pd_idx];
  2862. s->locked++;
  2863. set_bit(R5_LOCKED, &dev->flags);
  2864. set_bit(R5_Wantwrite, &dev->flags);
  2865. }
  2866. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2867. dev = &sh->dev[qd_idx];
  2868. s->locked++;
  2869. set_bit(R5_LOCKED, &dev->flags);
  2870. set_bit(R5_Wantwrite, &dev->flags);
  2871. }
  2872. clear_bit(STRIPE_DEGRADED, &sh->state);
  2873. set_bit(STRIPE_INSYNC, &sh->state);
  2874. break;
  2875. case check_state_run:
  2876. case check_state_run_q:
  2877. case check_state_run_pq:
  2878. break; /* we will be called again upon completion */
  2879. case check_state_check_result:
  2880. sh->check_state = check_state_idle;
  2881. /* handle a successful check operation, if parity is correct
  2882. * we are done. Otherwise update the mismatch count and repair
  2883. * parity if !MD_RECOVERY_CHECK
  2884. */
  2885. if (sh->ops.zero_sum_result == 0) {
  2886. /* both parities are correct */
  2887. if (!s->failed)
  2888. set_bit(STRIPE_INSYNC, &sh->state);
  2889. else {
  2890. /* in contrast to the raid5 case we can validate
  2891. * parity, but still have a failure to write
  2892. * back
  2893. */
  2894. sh->check_state = check_state_compute_result;
  2895. /* Returning at this point means that we may go
  2896. * off and bring p and/or q uptodate again so
  2897. * we make sure to check zero_sum_result again
  2898. * to verify if p or q need writeback
  2899. */
  2900. }
  2901. } else {
  2902. atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
  2903. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2904. /* don't try to repair!! */
  2905. set_bit(STRIPE_INSYNC, &sh->state);
  2906. else {
  2907. int *target = &sh->ops.target;
  2908. sh->ops.target = -1;
  2909. sh->ops.target2 = -1;
  2910. sh->check_state = check_state_compute_run;
  2911. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2912. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2913. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2914. set_bit(R5_Wantcompute,
  2915. &sh->dev[pd_idx].flags);
  2916. *target = pd_idx;
  2917. target = &sh->ops.target2;
  2918. s->uptodate++;
  2919. }
  2920. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2921. set_bit(R5_Wantcompute,
  2922. &sh->dev[qd_idx].flags);
  2923. *target = qd_idx;
  2924. s->uptodate++;
  2925. }
  2926. }
  2927. }
  2928. break;
  2929. case check_state_compute_run:
  2930. break;
  2931. default:
  2932. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2933. __func__, sh->check_state,
  2934. (unsigned long long) sh->sector);
  2935. BUG();
  2936. }
  2937. }
  2938. static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
  2939. {
  2940. int i;
  2941. /* We have read all the blocks in this stripe and now we need to
  2942. * copy some of them into a target stripe for expand.
  2943. */
  2944. struct dma_async_tx_descriptor *tx = NULL;
  2945. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2946. for (i = 0; i < sh->disks; i++)
  2947. if (i != sh->pd_idx && i != sh->qd_idx) {
  2948. int dd_idx, j;
  2949. struct stripe_head *sh2;
  2950. struct async_submit_ctl submit;
  2951. sector_t bn = compute_blocknr(sh, i, 1);
  2952. sector_t s = raid5_compute_sector(conf, bn, 0,
  2953. &dd_idx, NULL);
  2954. sh2 = get_active_stripe(conf, s, 0, 1, 1);
  2955. if (sh2 == NULL)
  2956. /* so far only the early blocks of this stripe
  2957. * have been requested. When later blocks
  2958. * get requested, we will try again
  2959. */
  2960. continue;
  2961. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2962. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2963. /* must have already done this block */
  2964. release_stripe(sh2);
  2965. continue;
  2966. }
  2967. /* place all the copies on one channel */
  2968. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  2969. tx = async_memcpy(sh2->dev[dd_idx].page,
  2970. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2971. &submit);
  2972. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2973. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2974. for (j = 0; j < conf->raid_disks; j++)
  2975. if (j != sh2->pd_idx &&
  2976. j != sh2->qd_idx &&
  2977. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2978. break;
  2979. if (j == conf->raid_disks) {
  2980. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2981. set_bit(STRIPE_HANDLE, &sh2->state);
  2982. }
  2983. release_stripe(sh2);
  2984. }
  2985. /* done submitting copies, wait for them to complete */
  2986. async_tx_quiesce(&tx);
  2987. }
  2988. /*
  2989. * handle_stripe - do things to a stripe.
  2990. *
  2991. * We lock the stripe by setting STRIPE_ACTIVE and then examine the
  2992. * state of various bits to see what needs to be done.
  2993. * Possible results:
  2994. * return some read requests which now have data
  2995. * return some write requests which are safely on storage
  2996. * schedule a read on some buffers
  2997. * schedule a write of some buffers
  2998. * return confirmation of parity correctness
  2999. *
  3000. */
  3001. static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
  3002. {
  3003. struct r5conf *conf = sh->raid_conf;
  3004. int disks = sh->disks;
  3005. struct r5dev *dev;
  3006. int i;
  3007. int do_recovery = 0;
  3008. memset(s, 0, sizeof(*s));
  3009. s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3010. s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  3011. s->failed_num[0] = -1;
  3012. s->failed_num[1] = -1;
  3013. /* Now to look around and see what can be done */
  3014. rcu_read_lock();
  3015. for (i=disks; i--; ) {
  3016. struct md_rdev *rdev;
  3017. sector_t first_bad;
  3018. int bad_sectors;
  3019. int is_bad = 0;
  3020. dev = &sh->dev[i];
  3021. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  3022. i, dev->flags,
  3023. dev->toread, dev->towrite, dev->written);
  3024. /* maybe we can reply to a read
  3025. *
  3026. * new wantfill requests are only permitted while
  3027. * ops_complete_biofill is guaranteed to be inactive
  3028. */
  3029. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  3030. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  3031. set_bit(R5_Wantfill, &dev->flags);
  3032. /* now count some things */
  3033. if (test_bit(R5_LOCKED, &dev->flags))
  3034. s->locked++;
  3035. if (test_bit(R5_UPTODATE, &dev->flags))
  3036. s->uptodate++;
  3037. if (test_bit(R5_Wantcompute, &dev->flags)) {
  3038. s->compute++;
  3039. BUG_ON(s->compute > 2);
  3040. }
  3041. if (test_bit(R5_Wantfill, &dev->flags))
  3042. s->to_fill++;
  3043. else if (dev->toread)
  3044. s->to_read++;
  3045. if (dev->towrite) {
  3046. s->to_write++;
  3047. if (!test_bit(R5_OVERWRITE, &dev->flags))
  3048. s->non_overwrite++;
  3049. }
  3050. if (dev->written)
  3051. s->written++;
  3052. /* Prefer to use the replacement for reads, but only
  3053. * if it is recovered enough and has no bad blocks.
  3054. */
  3055. rdev = rcu_dereference(conf->disks[i].replacement);
  3056. if (rdev && !test_bit(Faulty, &rdev->flags) &&
  3057. rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
  3058. !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  3059. &first_bad, &bad_sectors))
  3060. set_bit(R5_ReadRepl, &dev->flags);
  3061. else {
  3062. if (rdev)
  3063. set_bit(R5_NeedReplace, &dev->flags);
  3064. rdev = rcu_dereference(conf->disks[i].rdev);
  3065. clear_bit(R5_ReadRepl, &dev->flags);
  3066. }
  3067. if (rdev && test_bit(Faulty, &rdev->flags))
  3068. rdev = NULL;
  3069. if (rdev) {
  3070. is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  3071. &first_bad, &bad_sectors);
  3072. if (s->blocked_rdev == NULL
  3073. && (test_bit(Blocked, &rdev->flags)
  3074. || is_bad < 0)) {
  3075. if (is_bad < 0)
  3076. set_bit(BlockedBadBlocks,
  3077. &rdev->flags);
  3078. s->blocked_rdev = rdev;
  3079. atomic_inc(&rdev->nr_pending);
  3080. }
  3081. }
  3082. clear_bit(R5_Insync, &dev->flags);
  3083. if (!rdev)
  3084. /* Not in-sync */;
  3085. else if (is_bad) {
  3086. /* also not in-sync */
  3087. if (!test_bit(WriteErrorSeen, &rdev->flags) &&
  3088. test_bit(R5_UPTODATE, &dev->flags)) {
  3089. /* treat as in-sync, but with a read error
  3090. * which we can now try to correct
  3091. */
  3092. set_bit(R5_Insync, &dev->flags);
  3093. set_bit(R5_ReadError, &dev->flags);
  3094. }
  3095. } else if (test_bit(In_sync, &rdev->flags))
  3096. set_bit(R5_Insync, &dev->flags);
  3097. else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  3098. /* in sync if before recovery_offset */
  3099. set_bit(R5_Insync, &dev->flags);
  3100. else if (test_bit(R5_UPTODATE, &dev->flags) &&
  3101. test_bit(R5_Expanded, &dev->flags))
  3102. /* If we've reshaped into here, we assume it is Insync.
  3103. * We will shortly update recovery_offset to make
  3104. * it official.
  3105. */
  3106. set_bit(R5_Insync, &dev->flags);
  3107. if (rdev && test_bit(R5_WriteError, &dev->flags)) {
  3108. /* This flag does not apply to '.replacement'
  3109. * only to .rdev, so make sure to check that*/
  3110. struct md_rdev *rdev2 = rcu_dereference(
  3111. conf->disks[i].rdev);
  3112. if (rdev2 == rdev)
  3113. clear_bit(R5_Insync, &dev->flags);
  3114. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3115. s->handle_bad_blocks = 1;
  3116. atomic_inc(&rdev2->nr_pending);
  3117. } else
  3118. clear_bit(R5_WriteError, &dev->flags);
  3119. }
  3120. if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
  3121. /* This flag does not apply to '.replacement'
  3122. * only to .rdev, so make sure to check that*/
  3123. struct md_rdev *rdev2 = rcu_dereference(
  3124. conf->disks[i].rdev);
  3125. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3126. s->handle_bad_blocks = 1;
  3127. atomic_inc(&rdev2->nr_pending);
  3128. } else
  3129. clear_bit(R5_MadeGood, &dev->flags);
  3130. }
  3131. if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
  3132. struct md_rdev *rdev2 = rcu_dereference(
  3133. conf->disks[i].replacement);
  3134. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3135. s->handle_bad_blocks = 1;
  3136. atomic_inc(&rdev2->nr_pending);
  3137. } else
  3138. clear_bit(R5_MadeGoodRepl, &dev->flags);
  3139. }
  3140. if (!test_bit(R5_Insync, &dev->flags)) {
  3141. /* The ReadError flag will just be confusing now */
  3142. clear_bit(R5_ReadError, &dev->flags);
  3143. clear_bit(R5_ReWrite, &dev->flags);
  3144. }
  3145. if (test_bit(R5_ReadError, &dev->flags))
  3146. clear_bit(R5_Insync, &dev->flags);
  3147. if (!test_bit(R5_Insync, &dev->flags)) {
  3148. if (s->failed < 2)
  3149. s->failed_num[s->failed] = i;
  3150. s->failed++;
  3151. if (rdev && !test_bit(Faulty, &rdev->flags))
  3152. do_recovery = 1;
  3153. }
  3154. }
  3155. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  3156. /* If there is a failed device being replaced,
  3157. * we must be recovering.
  3158. * else if we are after recovery_cp, we must be syncing
  3159. * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
  3160. * else we can only be replacing
  3161. * sync and recovery both need to read all devices, and so
  3162. * use the same flag.
  3163. */
  3164. if (do_recovery ||
  3165. sh->sector >= conf->mddev->recovery_cp ||
  3166. test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
  3167. s->syncing = 1;
  3168. else
  3169. s->replacing = 1;
  3170. }
  3171. rcu_read_unlock();
  3172. }
  3173. static void handle_stripe(struct stripe_head *sh)
  3174. {
  3175. struct stripe_head_state s;
  3176. struct r5conf *conf = sh->raid_conf;
  3177. int i;
  3178. int prexor;
  3179. int disks = sh->disks;
  3180. struct r5dev *pdev, *qdev;
  3181. clear_bit(STRIPE_HANDLE, &sh->state);
  3182. if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
  3183. /* already being handled, ensure it gets handled
  3184. * again when current action finishes */
  3185. set_bit(STRIPE_HANDLE, &sh->state);
  3186. return;
  3187. }
  3188. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
  3189. spin_lock(&sh->stripe_lock);
  3190. /* Cannot process 'sync' concurrently with 'discard' */
  3191. if (!test_bit(STRIPE_DISCARD, &sh->state) &&
  3192. test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
  3193. set_bit(STRIPE_SYNCING, &sh->state);
  3194. clear_bit(STRIPE_INSYNC, &sh->state);
  3195. clear_bit(STRIPE_REPLACED, &sh->state);
  3196. }
  3197. spin_unlock(&sh->stripe_lock);
  3198. }
  3199. clear_bit(STRIPE_DELAYED, &sh->state);
  3200. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  3201. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  3202. (unsigned long long)sh->sector, sh->state,
  3203. atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
  3204. sh->check_state, sh->reconstruct_state);
  3205. analyse_stripe(sh, &s);
  3206. if (s.handle_bad_blocks) {
  3207. set_bit(STRIPE_HANDLE, &sh->state);
  3208. goto finish;
  3209. }
  3210. if (unlikely(s.blocked_rdev)) {
  3211. if (s.syncing || s.expanding || s.expanded ||
  3212. s.replacing || s.to_write || s.written) {
  3213. set_bit(STRIPE_HANDLE, &sh->state);
  3214. goto finish;
  3215. }
  3216. /* There is nothing for the blocked_rdev to block */
  3217. rdev_dec_pending(s.blocked_rdev, conf->mddev);
  3218. s.blocked_rdev = NULL;
  3219. }
  3220. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  3221. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  3222. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  3223. }
  3224. pr_debug("locked=%d uptodate=%d to_read=%d"
  3225. " to_write=%d failed=%d failed_num=%d,%d\n",
  3226. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  3227. s.failed_num[0], s.failed_num[1]);
  3228. /* check if the array has lost more than max_degraded devices and,
  3229. * if so, some requests might need to be failed.
  3230. */
  3231. if (s.failed > conf->max_degraded) {
  3232. sh->check_state = 0;
  3233. sh->reconstruct_state = 0;
  3234. if (s.to_read+s.to_write+s.written)
  3235. handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
  3236. if (s.syncing + s.replacing)
  3237. handle_failed_sync(conf, sh, &s);
  3238. }
  3239. /* Now we check to see if any write operations have recently
  3240. * completed
  3241. */
  3242. prexor = 0;
  3243. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  3244. prexor = 1;
  3245. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  3246. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  3247. sh->reconstruct_state = reconstruct_state_idle;
  3248. /* All the 'written' buffers and the parity block are ready to
  3249. * be written back to disk
  3250. */
  3251. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
  3252. !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
  3253. BUG_ON(sh->qd_idx >= 0 &&
  3254. !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
  3255. !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
  3256. for (i = disks; i--; ) {
  3257. struct r5dev *dev = &sh->dev[i];
  3258. if (test_bit(R5_LOCKED, &dev->flags) &&
  3259. (i == sh->pd_idx || i == sh->qd_idx ||
  3260. dev->written)) {
  3261. pr_debug("Writing block %d\n", i);
  3262. set_bit(R5_Wantwrite, &dev->flags);
  3263. if (prexor)
  3264. continue;
  3265. if (!test_bit(R5_Insync, &dev->flags) ||
  3266. ((i == sh->pd_idx || i == sh->qd_idx) &&
  3267. s.failed == 0))
  3268. set_bit(STRIPE_INSYNC, &sh->state);
  3269. }
  3270. }
  3271. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3272. s.dec_preread_active = 1;
  3273. }
  3274. /*
  3275. * might be able to return some write requests if the parity blocks
  3276. * are safe, or on a failed drive
  3277. */
  3278. pdev = &sh->dev[sh->pd_idx];
  3279. s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
  3280. || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
  3281. qdev = &sh->dev[sh->qd_idx];
  3282. s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
  3283. || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
  3284. || conf->level < 6;
  3285. if (s.written &&
  3286. (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  3287. && !test_bit(R5_LOCKED, &pdev->flags)
  3288. && (test_bit(R5_UPTODATE, &pdev->flags) ||
  3289. test_bit(R5_Discard, &pdev->flags))))) &&
  3290. (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  3291. && !test_bit(R5_LOCKED, &qdev->flags)
  3292. && (test_bit(R5_UPTODATE, &qdev->flags) ||
  3293. test_bit(R5_Discard, &qdev->flags))))))
  3294. handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
  3295. /* Now we might consider reading some blocks, either to check/generate
  3296. * parity, or to satisfy requests
  3297. * or to load a block that is being partially written.
  3298. */
  3299. if (s.to_read || s.non_overwrite
  3300. || (conf->level == 6 && s.to_write && s.failed)
  3301. || (s.syncing && (s.uptodate + s.compute < disks))
  3302. || s.replacing
  3303. || s.expanding)
  3304. handle_stripe_fill(sh, &s, disks);
  3305. /* Now to consider new write requests and what else, if anything
  3306. * should be read. We do not handle new writes when:
  3307. * 1/ A 'write' operation (copy+xor) is already in flight.
  3308. * 2/ A 'check' operation is in flight, as it may clobber the parity
  3309. * block.
  3310. */
  3311. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  3312. handle_stripe_dirtying(conf, sh, &s, disks);
  3313. /* maybe we need to check and possibly fix the parity for this stripe
  3314. * Any reads will already have been scheduled, so we just see if enough
  3315. * data is available. The parity check is held off while parity
  3316. * dependent operations are in flight.
  3317. */
  3318. if (sh->check_state ||
  3319. (s.syncing && s.locked == 0 &&
  3320. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  3321. !test_bit(STRIPE_INSYNC, &sh->state))) {
  3322. if (conf->level == 6)
  3323. handle_parity_checks6(conf, sh, &s, disks);
  3324. else
  3325. handle_parity_checks5(conf, sh, &s, disks);
  3326. }
  3327. if ((s.replacing || s.syncing) && s.locked == 0
  3328. && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
  3329. && !test_bit(STRIPE_REPLACED, &sh->state)) {
  3330. /* Write out to replacement devices where possible */
  3331. for (i = 0; i < conf->raid_disks; i++)
  3332. if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
  3333. WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
  3334. set_bit(R5_WantReplace, &sh->dev[i].flags);
  3335. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3336. s.locked++;
  3337. }
  3338. if (s.replacing)
  3339. set_bit(STRIPE_INSYNC, &sh->state);
  3340. set_bit(STRIPE_REPLACED, &sh->state);
  3341. }
  3342. if ((s.syncing || s.replacing) && s.locked == 0 &&
  3343. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  3344. test_bit(STRIPE_INSYNC, &sh->state)) {
  3345. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3346. clear_bit(STRIPE_SYNCING, &sh->state);
  3347. if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
  3348. wake_up(&conf->wait_for_overlap);
  3349. }
  3350. /* If the failed drives are just a ReadError, then we might need
  3351. * to progress the repair/check process
  3352. */
  3353. if (s.failed <= conf->max_degraded && !conf->mddev->ro)
  3354. for (i = 0; i < s.failed; i++) {
  3355. struct r5dev *dev = &sh->dev[s.failed_num[i]];
  3356. if (test_bit(R5_ReadError, &dev->flags)
  3357. && !test_bit(R5_LOCKED, &dev->flags)
  3358. && test_bit(R5_UPTODATE, &dev->flags)
  3359. ) {
  3360. if (!test_bit(R5_ReWrite, &dev->flags)) {
  3361. set_bit(R5_Wantwrite, &dev->flags);
  3362. set_bit(R5_ReWrite, &dev->flags);
  3363. set_bit(R5_LOCKED, &dev->flags);
  3364. s.locked++;
  3365. } else {
  3366. /* let's read it back */
  3367. set_bit(R5_Wantread, &dev->flags);
  3368. set_bit(R5_LOCKED, &dev->flags);
  3369. s.locked++;
  3370. }
  3371. }
  3372. }
  3373. /* Finish reconstruct operations initiated by the expansion process */
  3374. if (sh->reconstruct_state == reconstruct_state_result) {
  3375. struct stripe_head *sh_src
  3376. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  3377. if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
  3378. /* sh cannot be written until sh_src has been read.
  3379. * so arrange for sh to be delayed a little
  3380. */
  3381. set_bit(STRIPE_DELAYED, &sh->state);
  3382. set_bit(STRIPE_HANDLE, &sh->state);
  3383. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  3384. &sh_src->state))
  3385. atomic_inc(&conf->preread_active_stripes);
  3386. release_stripe(sh_src);
  3387. goto finish;
  3388. }
  3389. if (sh_src)
  3390. release_stripe(sh_src);
  3391. sh->reconstruct_state = reconstruct_state_idle;
  3392. clear_bit(STRIPE_EXPANDING, &sh->state);
  3393. for (i = conf->raid_disks; i--; ) {
  3394. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  3395. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3396. s.locked++;
  3397. }
  3398. }
  3399. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  3400. !sh->reconstruct_state) {
  3401. /* Need to write out all blocks after computing parity */
  3402. sh->disks = conf->raid_disks;
  3403. stripe_set_idx(sh->sector, conf, 0, sh);
  3404. schedule_reconstruction(sh, &s, 1, 1);
  3405. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  3406. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  3407. atomic_dec(&conf->reshape_stripes);
  3408. wake_up(&conf->wait_for_overlap);
  3409. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3410. }
  3411. if (s.expanding && s.locked == 0 &&
  3412. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  3413. handle_stripe_expansion(conf, sh);
  3414. finish:
  3415. /* wait for this device to become unblocked */
  3416. if (unlikely(s.blocked_rdev)) {
  3417. if (conf->mddev->external)
  3418. md_wait_for_blocked_rdev(s.blocked_rdev,
  3419. conf->mddev);
  3420. else
  3421. /* Internal metadata will immediately
  3422. * be written by raid5d, so we don't
  3423. * need to wait here.
  3424. */
  3425. rdev_dec_pending(s.blocked_rdev,
  3426. conf->mddev);
  3427. }
  3428. if (s.handle_bad_blocks)
  3429. for (i = disks; i--; ) {
  3430. struct md_rdev *rdev;
  3431. struct r5dev *dev = &sh->dev[i];
  3432. if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
  3433. /* We own a safe reference to the rdev */
  3434. rdev = conf->disks[i].rdev;
  3435. if (!rdev_set_badblocks(rdev, sh->sector,
  3436. STRIPE_SECTORS, 0))
  3437. md_error(conf->mddev, rdev);
  3438. rdev_dec_pending(rdev, conf->mddev);
  3439. }
  3440. if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
  3441. rdev = conf->disks[i].rdev;
  3442. rdev_clear_badblocks(rdev, sh->sector,
  3443. STRIPE_SECTORS, 0);
  3444. rdev_dec_pending(rdev, conf->mddev);
  3445. }
  3446. if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
  3447. rdev = conf->disks[i].replacement;
  3448. if (!rdev)
  3449. /* rdev have been moved down */
  3450. rdev = conf->disks[i].rdev;
  3451. rdev_clear_badblocks(rdev, sh->sector,
  3452. STRIPE_SECTORS, 0);
  3453. rdev_dec_pending(rdev, conf->mddev);
  3454. }
  3455. }
  3456. if (s.ops_request)
  3457. raid_run_ops(sh, s.ops_request);
  3458. ops_run_io(sh, &s);
  3459. if (s.dec_preread_active) {
  3460. /* We delay this until after ops_run_io so that if make_request
  3461. * is waiting on a flush, it won't continue until the writes
  3462. * have actually been submitted.
  3463. */
  3464. atomic_dec(&conf->preread_active_stripes);
  3465. if (atomic_read(&conf->preread_active_stripes) <
  3466. IO_THRESHOLD)
  3467. md_wakeup_thread(conf->mddev->thread);
  3468. }
  3469. return_io(s.return_bi);
  3470. clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
  3471. }
  3472. static void raid5_activate_delayed(struct r5conf *conf)
  3473. {
  3474. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  3475. while (!list_empty(&conf->delayed_list)) {
  3476. struct list_head *l = conf->delayed_list.next;
  3477. struct stripe_head *sh;
  3478. sh = list_entry(l, struct stripe_head, lru);
  3479. list_del_init(l);
  3480. clear_bit(STRIPE_DELAYED, &sh->state);
  3481. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3482. atomic_inc(&conf->preread_active_stripes);
  3483. list_add_tail(&sh->lru, &conf->hold_list);
  3484. raid5_wakeup_stripe_thread(sh);
  3485. }
  3486. }
  3487. }
  3488. static void activate_bit_delay(struct r5conf *conf)
  3489. {
  3490. /* device_lock is held */
  3491. struct list_head head;
  3492. list_add(&head, &conf->bitmap_list);
  3493. list_del_init(&conf->bitmap_list);
  3494. while (!list_empty(&head)) {
  3495. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  3496. list_del_init(&sh->lru);
  3497. atomic_inc(&sh->count);
  3498. __release_stripe(conf, sh);
  3499. }
  3500. }
  3501. int md_raid5_congested(struct mddev *mddev, int bits)
  3502. {
  3503. struct r5conf *conf = mddev->private;
  3504. /* No difference between reads and writes. Just check
  3505. * how busy the stripe_cache is
  3506. */
  3507. if (conf->inactive_blocked)
  3508. return 1;
  3509. if (conf->quiesce)
  3510. return 1;
  3511. if (list_empty_careful(&conf->inactive_list))
  3512. return 1;
  3513. return 0;
  3514. }
  3515. EXPORT_SYMBOL_GPL(md_raid5_congested);
  3516. static int raid5_congested(void *data, int bits)
  3517. {
  3518. struct mddev *mddev = data;
  3519. return mddev_congested(mddev, bits) ||
  3520. md_raid5_congested(mddev, bits);
  3521. }
  3522. /* We want read requests to align with chunks where possible,
  3523. * but write requests don't need to.
  3524. */
  3525. static int raid5_mergeable_bvec(struct request_queue *q,
  3526. struct bvec_merge_data *bvm,
  3527. struct bio_vec *biovec)
  3528. {
  3529. struct mddev *mddev = q->queuedata;
  3530. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  3531. int max;
  3532. unsigned int chunk_sectors = mddev->chunk_sectors;
  3533. unsigned int bio_sectors = bvm->bi_size >> 9;
  3534. if ((bvm->bi_rw & 1) == WRITE)
  3535. return biovec->bv_len; /* always allow writes to be mergeable */
  3536. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3537. chunk_sectors = mddev->new_chunk_sectors;
  3538. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  3539. if (max < 0) max = 0;
  3540. if (max <= biovec->bv_len && bio_sectors == 0)
  3541. return biovec->bv_len;
  3542. else
  3543. return max;
  3544. }
  3545. static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
  3546. {
  3547. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  3548. unsigned int chunk_sectors = mddev->chunk_sectors;
  3549. unsigned int bio_sectors = bio_sectors(bio);
  3550. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3551. chunk_sectors = mddev->new_chunk_sectors;
  3552. return chunk_sectors >=
  3553. ((sector & (chunk_sectors - 1)) + bio_sectors);
  3554. }
  3555. /*
  3556. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  3557. * later sampled by raid5d.
  3558. */
  3559. static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
  3560. {
  3561. unsigned long flags;
  3562. spin_lock_irqsave(&conf->device_lock, flags);
  3563. bi->bi_next = conf->retry_read_aligned_list;
  3564. conf->retry_read_aligned_list = bi;
  3565. spin_unlock_irqrestore(&conf->device_lock, flags);
  3566. md_wakeup_thread(conf->mddev->thread);
  3567. }
  3568. static struct bio *remove_bio_from_retry(struct r5conf *conf)
  3569. {
  3570. struct bio *bi;
  3571. bi = conf->retry_read_aligned;
  3572. if (bi) {
  3573. conf->retry_read_aligned = NULL;
  3574. return bi;
  3575. }
  3576. bi = conf->retry_read_aligned_list;
  3577. if(bi) {
  3578. conf->retry_read_aligned_list = bi->bi_next;
  3579. bi->bi_next = NULL;
  3580. /*
  3581. * this sets the active strip count to 1 and the processed
  3582. * strip count to zero (upper 8 bits)
  3583. */
  3584. raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
  3585. }
  3586. return bi;
  3587. }
  3588. /*
  3589. * The "raid5_align_endio" should check if the read succeeded and if it
  3590. * did, call bio_endio on the original bio (having bio_put the new bio
  3591. * first).
  3592. * If the read failed..
  3593. */
  3594. static void raid5_align_endio(struct bio *bi, int error)
  3595. {
  3596. struct bio* raid_bi = bi->bi_private;
  3597. struct mddev *mddev;
  3598. struct r5conf *conf;
  3599. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3600. struct md_rdev *rdev;
  3601. bio_put(bi);
  3602. rdev = (void*)raid_bi->bi_next;
  3603. raid_bi->bi_next = NULL;
  3604. mddev = rdev->mddev;
  3605. conf = mddev->private;
  3606. rdev_dec_pending(rdev, conf->mddev);
  3607. if (!error && uptodate) {
  3608. trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
  3609. raid_bi, 0);
  3610. bio_endio(raid_bi, 0);
  3611. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3612. wake_up(&conf->wait_for_stripe);
  3613. return;
  3614. }
  3615. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3616. add_bio_to_retry(raid_bi, conf);
  3617. }
  3618. static int bio_fits_rdev(struct bio *bi)
  3619. {
  3620. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3621. if (bio_sectors(bi) > queue_max_sectors(q))
  3622. return 0;
  3623. blk_recount_segments(q, bi);
  3624. if (bi->bi_phys_segments > queue_max_segments(q))
  3625. return 0;
  3626. if (q->merge_bvec_fn)
  3627. /* it's too hard to apply the merge_bvec_fn at this stage,
  3628. * just just give up
  3629. */
  3630. return 0;
  3631. return 1;
  3632. }
  3633. static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
  3634. {
  3635. struct r5conf *conf = mddev->private;
  3636. int dd_idx;
  3637. struct bio* align_bi;
  3638. struct md_rdev *rdev;
  3639. sector_t end_sector;
  3640. if (!in_chunk_boundary(mddev, raid_bio)) {
  3641. pr_debug("chunk_aligned_read : non aligned\n");
  3642. return 0;
  3643. }
  3644. /*
  3645. * use bio_clone_mddev to make a copy of the bio
  3646. */
  3647. align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
  3648. if (!align_bi)
  3649. return 0;
  3650. /*
  3651. * set bi_end_io to a new function, and set bi_private to the
  3652. * original bio.
  3653. */
  3654. align_bi->bi_end_io = raid5_align_endio;
  3655. align_bi->bi_private = raid_bio;
  3656. /*
  3657. * compute position
  3658. */
  3659. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3660. 0,
  3661. &dd_idx, NULL);
  3662. end_sector = bio_end_sector(align_bi);
  3663. rcu_read_lock();
  3664. rdev = rcu_dereference(conf->disks[dd_idx].replacement);
  3665. if (!rdev || test_bit(Faulty, &rdev->flags) ||
  3666. rdev->recovery_offset < end_sector) {
  3667. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3668. if (rdev &&
  3669. (test_bit(Faulty, &rdev->flags) ||
  3670. !(test_bit(In_sync, &rdev->flags) ||
  3671. rdev->recovery_offset >= end_sector)))
  3672. rdev = NULL;
  3673. }
  3674. if (rdev) {
  3675. sector_t first_bad;
  3676. int bad_sectors;
  3677. atomic_inc(&rdev->nr_pending);
  3678. rcu_read_unlock();
  3679. raid_bio->bi_next = (void*)rdev;
  3680. align_bi->bi_bdev = rdev->bdev;
  3681. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3682. if (!bio_fits_rdev(align_bi) ||
  3683. is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
  3684. &first_bad, &bad_sectors)) {
  3685. /* too big in some way, or has a known bad block */
  3686. bio_put(align_bi);
  3687. rdev_dec_pending(rdev, mddev);
  3688. return 0;
  3689. }
  3690. /* No reshape active, so we can trust rdev->data_offset */
  3691. align_bi->bi_sector += rdev->data_offset;
  3692. spin_lock_irq(&conf->device_lock);
  3693. wait_event_lock_irq(conf->wait_for_stripe,
  3694. conf->quiesce == 0,
  3695. conf->device_lock);
  3696. atomic_inc(&conf->active_aligned_reads);
  3697. spin_unlock_irq(&conf->device_lock);
  3698. if (mddev->gendisk)
  3699. trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
  3700. align_bi, disk_devt(mddev->gendisk),
  3701. raid_bio->bi_sector);
  3702. generic_make_request(align_bi);
  3703. return 1;
  3704. } else {
  3705. rcu_read_unlock();
  3706. bio_put(align_bi);
  3707. return 0;
  3708. }
  3709. }
  3710. /* __get_priority_stripe - get the next stripe to process
  3711. *
  3712. * Full stripe writes are allowed to pass preread active stripes up until
  3713. * the bypass_threshold is exceeded. In general the bypass_count
  3714. * increments when the handle_list is handled before the hold_list; however, it
  3715. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3716. * stripe with in flight i/o. The bypass_count will be reset when the
  3717. * head of the hold_list has changed, i.e. the head was promoted to the
  3718. * handle_list.
  3719. */
  3720. static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
  3721. {
  3722. struct stripe_head *sh = NULL, *tmp;
  3723. struct list_head *handle_list = NULL;
  3724. if (conf->worker_cnt_per_group == 0) {
  3725. handle_list = &conf->handle_list;
  3726. } else if (group != ANY_GROUP) {
  3727. handle_list = &conf->worker_groups[group].handle_list;
  3728. } else {
  3729. int i;
  3730. for (i = 0; i < conf->group_cnt; i++) {
  3731. handle_list = &conf->worker_groups[i].handle_list;
  3732. if (!list_empty(handle_list))
  3733. break;
  3734. }
  3735. }
  3736. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3737. __func__,
  3738. list_empty(handle_list) ? "empty" : "busy",
  3739. list_empty(&conf->hold_list) ? "empty" : "busy",
  3740. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3741. if (!list_empty(handle_list)) {
  3742. sh = list_entry(handle_list->next, typeof(*sh), lru);
  3743. if (list_empty(&conf->hold_list))
  3744. conf->bypass_count = 0;
  3745. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3746. if (conf->hold_list.next == conf->last_hold)
  3747. conf->bypass_count++;
  3748. else {
  3749. conf->last_hold = conf->hold_list.next;
  3750. conf->bypass_count -= conf->bypass_threshold;
  3751. if (conf->bypass_count < 0)
  3752. conf->bypass_count = 0;
  3753. }
  3754. }
  3755. } else if (!list_empty(&conf->hold_list) &&
  3756. ((conf->bypass_threshold &&
  3757. conf->bypass_count > conf->bypass_threshold) ||
  3758. atomic_read(&conf->pending_full_writes) == 0)) {
  3759. list_for_each_entry(tmp, &conf->hold_list, lru) {
  3760. if (conf->worker_cnt_per_group == 0 ||
  3761. group == ANY_GROUP ||
  3762. !cpu_online(tmp->cpu) ||
  3763. cpu_to_group(tmp->cpu) == group) {
  3764. sh = tmp;
  3765. break;
  3766. }
  3767. }
  3768. if (sh) {
  3769. conf->bypass_count -= conf->bypass_threshold;
  3770. if (conf->bypass_count < 0)
  3771. conf->bypass_count = 0;
  3772. }
  3773. }
  3774. if (!sh)
  3775. return NULL;
  3776. list_del_init(&sh->lru);
  3777. atomic_inc(&sh->count);
  3778. BUG_ON(atomic_read(&sh->count) != 1);
  3779. return sh;
  3780. }
  3781. struct raid5_plug_cb {
  3782. struct blk_plug_cb cb;
  3783. struct list_head list;
  3784. };
  3785. static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
  3786. {
  3787. struct raid5_plug_cb *cb = container_of(
  3788. blk_cb, struct raid5_plug_cb, cb);
  3789. struct stripe_head *sh;
  3790. struct mddev *mddev = cb->cb.data;
  3791. struct r5conf *conf = mddev->private;
  3792. int cnt = 0;
  3793. if (cb->list.next && !list_empty(&cb->list)) {
  3794. spin_lock_irq(&conf->device_lock);
  3795. while (!list_empty(&cb->list)) {
  3796. sh = list_first_entry(&cb->list, struct stripe_head, lru);
  3797. list_del_init(&sh->lru);
  3798. /*
  3799. * avoid race release_stripe_plug() sees
  3800. * STRIPE_ON_UNPLUG_LIST clear but the stripe
  3801. * is still in our list
  3802. */
  3803. smp_mb__before_clear_bit();
  3804. clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
  3805. /*
  3806. * STRIPE_ON_RELEASE_LIST could be set here. In that
  3807. * case, the count is always > 1 here
  3808. */
  3809. __release_stripe(conf, sh);
  3810. cnt++;
  3811. }
  3812. spin_unlock_irq(&conf->device_lock);
  3813. }
  3814. if (mddev->queue)
  3815. trace_block_unplug(mddev->queue, cnt, !from_schedule);
  3816. kfree(cb);
  3817. }
  3818. static void release_stripe_plug(struct mddev *mddev,
  3819. struct stripe_head *sh)
  3820. {
  3821. struct blk_plug_cb *blk_cb = blk_check_plugged(
  3822. raid5_unplug, mddev,
  3823. sizeof(struct raid5_plug_cb));
  3824. struct raid5_plug_cb *cb;
  3825. if (!blk_cb) {
  3826. release_stripe(sh);
  3827. return;
  3828. }
  3829. cb = container_of(blk_cb, struct raid5_plug_cb, cb);
  3830. if (cb->list.next == NULL)
  3831. INIT_LIST_HEAD(&cb->list);
  3832. if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
  3833. list_add_tail(&sh->lru, &cb->list);
  3834. else
  3835. release_stripe(sh);
  3836. }
  3837. static void make_discard_request(struct mddev *mddev, struct bio *bi)
  3838. {
  3839. struct r5conf *conf = mddev->private;
  3840. sector_t logical_sector, last_sector;
  3841. struct stripe_head *sh;
  3842. int remaining;
  3843. int stripe_sectors;
  3844. if (mddev->reshape_position != MaxSector)
  3845. /* Skip discard while reshape is happening */
  3846. return;
  3847. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3848. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3849. bi->bi_next = NULL;
  3850. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3851. stripe_sectors = conf->chunk_sectors *
  3852. (conf->raid_disks - conf->max_degraded);
  3853. logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
  3854. stripe_sectors);
  3855. sector_div(last_sector, stripe_sectors);
  3856. logical_sector *= conf->chunk_sectors;
  3857. last_sector *= conf->chunk_sectors;
  3858. for (; logical_sector < last_sector;
  3859. logical_sector += STRIPE_SECTORS) {
  3860. DEFINE_WAIT(w);
  3861. int d;
  3862. again:
  3863. sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
  3864. prepare_to_wait(&conf->wait_for_overlap, &w,
  3865. TASK_UNINTERRUPTIBLE);
  3866. set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
  3867. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  3868. release_stripe(sh);
  3869. schedule();
  3870. goto again;
  3871. }
  3872. clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
  3873. spin_lock_irq(&sh->stripe_lock);
  3874. for (d = 0; d < conf->raid_disks; d++) {
  3875. if (d == sh->pd_idx || d == sh->qd_idx)
  3876. continue;
  3877. if (sh->dev[d].towrite || sh->dev[d].toread) {
  3878. set_bit(R5_Overlap, &sh->dev[d].flags);
  3879. spin_unlock_irq(&sh->stripe_lock);
  3880. release_stripe(sh);
  3881. schedule();
  3882. goto again;
  3883. }
  3884. }
  3885. set_bit(STRIPE_DISCARD, &sh->state);
  3886. finish_wait(&conf->wait_for_overlap, &w);
  3887. for (d = 0; d < conf->raid_disks; d++) {
  3888. if (d == sh->pd_idx || d == sh->qd_idx)
  3889. continue;
  3890. sh->dev[d].towrite = bi;
  3891. set_bit(R5_OVERWRITE, &sh->dev[d].flags);
  3892. raid5_inc_bi_active_stripes(bi);
  3893. }
  3894. spin_unlock_irq(&sh->stripe_lock);
  3895. if (conf->mddev->bitmap) {
  3896. for (d = 0;
  3897. d < conf->raid_disks - conf->max_degraded;
  3898. d++)
  3899. bitmap_startwrite(mddev->bitmap,
  3900. sh->sector,
  3901. STRIPE_SECTORS,
  3902. 0);
  3903. sh->bm_seq = conf->seq_flush + 1;
  3904. set_bit(STRIPE_BIT_DELAY, &sh->state);
  3905. }
  3906. set_bit(STRIPE_HANDLE, &sh->state);
  3907. clear_bit(STRIPE_DELAYED, &sh->state);
  3908. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3909. atomic_inc(&conf->preread_active_stripes);
  3910. release_stripe_plug(mddev, sh);
  3911. }
  3912. remaining = raid5_dec_bi_active_stripes(bi);
  3913. if (remaining == 0) {
  3914. md_write_end(mddev);
  3915. bio_endio(bi, 0);
  3916. }
  3917. }
  3918. static void make_request(struct mddev *mddev, struct bio * bi)
  3919. {
  3920. struct r5conf *conf = mddev->private;
  3921. int dd_idx;
  3922. sector_t new_sector;
  3923. sector_t logical_sector, last_sector;
  3924. struct stripe_head *sh;
  3925. const int rw = bio_data_dir(bi);
  3926. int remaining;
  3927. if (unlikely(bi->bi_rw & REQ_FLUSH)) {
  3928. md_flush_request(mddev, bi);
  3929. return;
  3930. }
  3931. md_write_start(mddev, bi);
  3932. if (rw == READ &&
  3933. mddev->reshape_position == MaxSector &&
  3934. chunk_aligned_read(mddev,bi))
  3935. return;
  3936. if (unlikely(bi->bi_rw & REQ_DISCARD)) {
  3937. make_discard_request(mddev, bi);
  3938. return;
  3939. }
  3940. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3941. last_sector = bio_end_sector(bi);
  3942. bi->bi_next = NULL;
  3943. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3944. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3945. DEFINE_WAIT(w);
  3946. int previous;
  3947. int seq;
  3948. retry:
  3949. seq = read_seqcount_begin(&conf->gen_lock);
  3950. previous = 0;
  3951. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3952. if (unlikely(conf->reshape_progress != MaxSector)) {
  3953. /* spinlock is needed as reshape_progress may be
  3954. * 64bit on a 32bit platform, and so it might be
  3955. * possible to see a half-updated value
  3956. * Of course reshape_progress could change after
  3957. * the lock is dropped, so once we get a reference
  3958. * to the stripe that we think it is, we will have
  3959. * to check again.
  3960. */
  3961. spin_lock_irq(&conf->device_lock);
  3962. if (mddev->reshape_backwards
  3963. ? logical_sector < conf->reshape_progress
  3964. : logical_sector >= conf->reshape_progress) {
  3965. previous = 1;
  3966. } else {
  3967. if (mddev->reshape_backwards
  3968. ? logical_sector < conf->reshape_safe
  3969. : logical_sector >= conf->reshape_safe) {
  3970. spin_unlock_irq(&conf->device_lock);
  3971. schedule();
  3972. goto retry;
  3973. }
  3974. }
  3975. spin_unlock_irq(&conf->device_lock);
  3976. }
  3977. new_sector = raid5_compute_sector(conf, logical_sector,
  3978. previous,
  3979. &dd_idx, NULL);
  3980. pr_debug("raid456: make_request, sector %llu logical %llu\n",
  3981. (unsigned long long)new_sector,
  3982. (unsigned long long)logical_sector);
  3983. sh = get_active_stripe(conf, new_sector, previous,
  3984. (bi->bi_rw&RWA_MASK), 0);
  3985. if (sh) {
  3986. if (unlikely(previous)) {
  3987. /* expansion might have moved on while waiting for a
  3988. * stripe, so we must do the range check again.
  3989. * Expansion could still move past after this
  3990. * test, but as we are holding a reference to
  3991. * 'sh', we know that if that happens,
  3992. * STRIPE_EXPANDING will get set and the expansion
  3993. * won't proceed until we finish with the stripe.
  3994. */
  3995. int must_retry = 0;
  3996. spin_lock_irq(&conf->device_lock);
  3997. if (mddev->reshape_backwards
  3998. ? logical_sector >= conf->reshape_progress
  3999. : logical_sector < conf->reshape_progress)
  4000. /* mismatch, need to try again */
  4001. must_retry = 1;
  4002. spin_unlock_irq(&conf->device_lock);
  4003. if (must_retry) {
  4004. release_stripe(sh);
  4005. schedule();
  4006. goto retry;
  4007. }
  4008. }
  4009. if (read_seqcount_retry(&conf->gen_lock, seq)) {
  4010. /* Might have got the wrong stripe_head
  4011. * by accident
  4012. */
  4013. release_stripe(sh);
  4014. goto retry;
  4015. }
  4016. if (rw == WRITE &&
  4017. logical_sector >= mddev->suspend_lo &&
  4018. logical_sector < mddev->suspend_hi) {
  4019. release_stripe(sh);
  4020. /* As the suspend_* range is controlled by
  4021. * userspace, we want an interruptible
  4022. * wait.
  4023. */
  4024. flush_signals(current);
  4025. prepare_to_wait(&conf->wait_for_overlap,
  4026. &w, TASK_INTERRUPTIBLE);
  4027. if (logical_sector >= mddev->suspend_lo &&
  4028. logical_sector < mddev->suspend_hi)
  4029. schedule();
  4030. goto retry;
  4031. }
  4032. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  4033. !add_stripe_bio(sh, bi, dd_idx, rw)) {
  4034. /* Stripe is busy expanding or
  4035. * add failed due to overlap. Flush everything
  4036. * and wait a while
  4037. */
  4038. md_wakeup_thread(mddev->thread);
  4039. release_stripe(sh);
  4040. schedule();
  4041. goto retry;
  4042. }
  4043. finish_wait(&conf->wait_for_overlap, &w);
  4044. set_bit(STRIPE_HANDLE, &sh->state);
  4045. clear_bit(STRIPE_DELAYED, &sh->state);
  4046. if ((bi->bi_rw & REQ_SYNC) &&
  4047. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  4048. atomic_inc(&conf->preread_active_stripes);
  4049. release_stripe_plug(mddev, sh);
  4050. } else {
  4051. /* cannot get stripe for read-ahead, just give-up */
  4052. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  4053. finish_wait(&conf->wait_for_overlap, &w);
  4054. break;
  4055. }
  4056. }
  4057. remaining = raid5_dec_bi_active_stripes(bi);
  4058. if (remaining == 0) {
  4059. if ( rw == WRITE )
  4060. md_write_end(mddev);
  4061. trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
  4062. bi, 0);
  4063. bio_endio(bi, 0);
  4064. }
  4065. }
  4066. static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
  4067. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
  4068. {
  4069. /* reshaping is quite different to recovery/resync so it is
  4070. * handled quite separately ... here.
  4071. *
  4072. * On each call to sync_request, we gather one chunk worth of
  4073. * destination stripes and flag them as expanding.
  4074. * Then we find all the source stripes and request reads.
  4075. * As the reads complete, handle_stripe will copy the data
  4076. * into the destination stripe and release that stripe.
  4077. */
  4078. struct r5conf *conf = mddev->private;
  4079. struct stripe_head *sh;
  4080. sector_t first_sector, last_sector;
  4081. int raid_disks = conf->previous_raid_disks;
  4082. int data_disks = raid_disks - conf->max_degraded;
  4083. int new_data_disks = conf->raid_disks - conf->max_degraded;
  4084. int i;
  4085. int dd_idx;
  4086. sector_t writepos, readpos, safepos;
  4087. sector_t stripe_addr;
  4088. int reshape_sectors;
  4089. struct list_head stripes;
  4090. if (sector_nr == 0) {
  4091. /* If restarting in the middle, skip the initial sectors */
  4092. if (mddev->reshape_backwards &&
  4093. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  4094. sector_nr = raid5_size(mddev, 0, 0)
  4095. - conf->reshape_progress;
  4096. } else if (!mddev->reshape_backwards &&
  4097. conf->reshape_progress > 0)
  4098. sector_nr = conf->reshape_progress;
  4099. sector_div(sector_nr, new_data_disks);
  4100. if (sector_nr) {
  4101. mddev->curr_resync_completed = sector_nr;
  4102. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  4103. *skipped = 1;
  4104. return sector_nr;
  4105. }
  4106. }
  4107. /* We need to process a full chunk at a time.
  4108. * If old and new chunk sizes differ, we need to process the
  4109. * largest of these
  4110. */
  4111. if (mddev->new_chunk_sectors > mddev->chunk_sectors)
  4112. reshape_sectors = mddev->new_chunk_sectors;
  4113. else
  4114. reshape_sectors = mddev->chunk_sectors;
  4115. /* We update the metadata at least every 10 seconds, or when
  4116. * the data about to be copied would over-write the source of
  4117. * the data at the front of the range. i.e. one new_stripe
  4118. * along from reshape_progress new_maps to after where
  4119. * reshape_safe old_maps to
  4120. */
  4121. writepos = conf->reshape_progress;
  4122. sector_div(writepos, new_data_disks);
  4123. readpos = conf->reshape_progress;
  4124. sector_div(readpos, data_disks);
  4125. safepos = conf->reshape_safe;
  4126. sector_div(safepos, data_disks);
  4127. if (mddev->reshape_backwards) {
  4128. writepos -= min_t(sector_t, reshape_sectors, writepos);
  4129. readpos += reshape_sectors;
  4130. safepos += reshape_sectors;
  4131. } else {
  4132. writepos += reshape_sectors;
  4133. readpos -= min_t(sector_t, reshape_sectors, readpos);
  4134. safepos -= min_t(sector_t, reshape_sectors, safepos);
  4135. }
  4136. /* Having calculated the 'writepos' possibly use it
  4137. * to set 'stripe_addr' which is where we will write to.
  4138. */
  4139. if (mddev->reshape_backwards) {
  4140. BUG_ON(conf->reshape_progress == 0);
  4141. stripe_addr = writepos;
  4142. BUG_ON((mddev->dev_sectors &
  4143. ~((sector_t)reshape_sectors - 1))
  4144. - reshape_sectors - stripe_addr
  4145. != sector_nr);
  4146. } else {
  4147. BUG_ON(writepos != sector_nr + reshape_sectors);
  4148. stripe_addr = sector_nr;
  4149. }
  4150. /* 'writepos' is the most advanced device address we might write.
  4151. * 'readpos' is the least advanced device address we might read.
  4152. * 'safepos' is the least address recorded in the metadata as having
  4153. * been reshaped.
  4154. * If there is a min_offset_diff, these are adjusted either by
  4155. * increasing the safepos/readpos if diff is negative, or
  4156. * increasing writepos if diff is positive.
  4157. * If 'readpos' is then behind 'writepos', there is no way that we can
  4158. * ensure safety in the face of a crash - that must be done by userspace
  4159. * making a backup of the data. So in that case there is no particular
  4160. * rush to update metadata.
  4161. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  4162. * update the metadata to advance 'safepos' to match 'readpos' so that
  4163. * we can be safe in the event of a crash.
  4164. * So we insist on updating metadata if safepos is behind writepos and
  4165. * readpos is beyond writepos.
  4166. * In any case, update the metadata every 10 seconds.
  4167. * Maybe that number should be configurable, but I'm not sure it is
  4168. * worth it.... maybe it could be a multiple of safemode_delay???
  4169. */
  4170. if (conf->min_offset_diff < 0) {
  4171. safepos += -conf->min_offset_diff;
  4172. readpos += -conf->min_offset_diff;
  4173. } else
  4174. writepos += conf->min_offset_diff;
  4175. if ((mddev->reshape_backwards
  4176. ? (safepos > writepos && readpos < writepos)
  4177. : (safepos < writepos && readpos > writepos)) ||
  4178. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  4179. /* Cannot proceed until we've updated the superblock... */
  4180. wait_event(conf->wait_for_overlap,
  4181. atomic_read(&conf->reshape_stripes)==0);
  4182. mddev->reshape_position = conf->reshape_progress;
  4183. mddev->curr_resync_completed = sector_nr;
  4184. conf->reshape_checkpoint = jiffies;
  4185. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4186. md_wakeup_thread(mddev->thread);
  4187. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  4188. kthread_should_stop());
  4189. spin_lock_irq(&conf->device_lock);
  4190. conf->reshape_safe = mddev->reshape_position;
  4191. spin_unlock_irq(&conf->device_lock);
  4192. wake_up(&conf->wait_for_overlap);
  4193. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  4194. }
  4195. INIT_LIST_HEAD(&stripes);
  4196. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  4197. int j;
  4198. int skipped_disk = 0;
  4199. sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  4200. set_bit(STRIPE_EXPANDING, &sh->state);
  4201. atomic_inc(&conf->reshape_stripes);
  4202. /* If any of this stripe is beyond the end of the old
  4203. * array, then we need to zero those blocks
  4204. */
  4205. for (j=sh->disks; j--;) {
  4206. sector_t s;
  4207. if (j == sh->pd_idx)
  4208. continue;
  4209. if (conf->level == 6 &&
  4210. j == sh->qd_idx)
  4211. continue;
  4212. s = compute_blocknr(sh, j, 0);
  4213. if (s < raid5_size(mddev, 0, 0)) {
  4214. skipped_disk = 1;
  4215. continue;
  4216. }
  4217. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  4218. set_bit(R5_Expanded, &sh->dev[j].flags);
  4219. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  4220. }
  4221. if (!skipped_disk) {
  4222. set_bit(STRIPE_EXPAND_READY, &sh->state);
  4223. set_bit(STRIPE_HANDLE, &sh->state);
  4224. }
  4225. list_add(&sh->lru, &stripes);
  4226. }
  4227. spin_lock_irq(&conf->device_lock);
  4228. if (mddev->reshape_backwards)
  4229. conf->reshape_progress -= reshape_sectors * new_data_disks;
  4230. else
  4231. conf->reshape_progress += reshape_sectors * new_data_disks;
  4232. spin_unlock_irq(&conf->device_lock);
  4233. /* Ok, those stripe are ready. We can start scheduling
  4234. * reads on the source stripes.
  4235. * The source stripes are determined by mapping the first and last
  4236. * block on the destination stripes.
  4237. */
  4238. first_sector =
  4239. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  4240. 1, &dd_idx, NULL);
  4241. last_sector =
  4242. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  4243. * new_data_disks - 1),
  4244. 1, &dd_idx, NULL);
  4245. if (last_sector >= mddev->dev_sectors)
  4246. last_sector = mddev->dev_sectors - 1;
  4247. while (first_sector <= last_sector) {
  4248. sh = get_active_stripe(conf, first_sector, 1, 0, 1);
  4249. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  4250. set_bit(STRIPE_HANDLE, &sh->state);
  4251. release_stripe(sh);
  4252. first_sector += STRIPE_SECTORS;
  4253. }
  4254. /* Now that the sources are clearly marked, we can release
  4255. * the destination stripes
  4256. */
  4257. while (!list_empty(&stripes)) {
  4258. sh = list_entry(stripes.next, struct stripe_head, lru);
  4259. list_del_init(&sh->lru);
  4260. release_stripe(sh);
  4261. }
  4262. /* If this takes us to the resync_max point where we have to pause,
  4263. * then we need to write out the superblock.
  4264. */
  4265. sector_nr += reshape_sectors;
  4266. if ((sector_nr - mddev->curr_resync_completed) * 2
  4267. >= mddev->resync_max - mddev->curr_resync_completed) {
  4268. /* Cannot proceed until we've updated the superblock... */
  4269. wait_event(conf->wait_for_overlap,
  4270. atomic_read(&conf->reshape_stripes) == 0);
  4271. mddev->reshape_position = conf->reshape_progress;
  4272. mddev->curr_resync_completed = sector_nr;
  4273. conf->reshape_checkpoint = jiffies;
  4274. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4275. md_wakeup_thread(mddev->thread);
  4276. wait_event(mddev->sb_wait,
  4277. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  4278. || kthread_should_stop());
  4279. spin_lock_irq(&conf->device_lock);
  4280. conf->reshape_safe = mddev->reshape_position;
  4281. spin_unlock_irq(&conf->device_lock);
  4282. wake_up(&conf->wait_for_overlap);
  4283. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  4284. }
  4285. return reshape_sectors;
  4286. }
  4287. /* FIXME go_faster isn't used */
  4288. static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
  4289. {
  4290. struct r5conf *conf = mddev->private;
  4291. struct stripe_head *sh;
  4292. sector_t max_sector = mddev->dev_sectors;
  4293. sector_t sync_blocks;
  4294. int still_degraded = 0;
  4295. int i;
  4296. if (sector_nr >= max_sector) {
  4297. /* just being told to finish up .. nothing much to do */
  4298. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  4299. end_reshape(conf);
  4300. return 0;
  4301. }
  4302. if (mddev->curr_resync < max_sector) /* aborted */
  4303. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  4304. &sync_blocks, 1);
  4305. else /* completed sync */
  4306. conf->fullsync = 0;
  4307. bitmap_close_sync(mddev->bitmap);
  4308. return 0;
  4309. }
  4310. /* Allow raid5_quiesce to complete */
  4311. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  4312. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  4313. return reshape_request(mddev, sector_nr, skipped);
  4314. /* No need to check resync_max as we never do more than one
  4315. * stripe, and as resync_max will always be on a chunk boundary,
  4316. * if the check in md_do_sync didn't fire, there is no chance
  4317. * of overstepping resync_max here
  4318. */
  4319. /* if there is too many failed drives and we are trying
  4320. * to resync, then assert that we are finished, because there is
  4321. * nothing we can do.
  4322. */
  4323. if (mddev->degraded >= conf->max_degraded &&
  4324. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  4325. sector_t rv = mddev->dev_sectors - sector_nr;
  4326. *skipped = 1;
  4327. return rv;
  4328. }
  4329. if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  4330. !conf->fullsync &&
  4331. !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  4332. sync_blocks >= STRIPE_SECTORS) {
  4333. /* we can skip this block, and probably more */
  4334. sync_blocks /= STRIPE_SECTORS;
  4335. *skipped = 1;
  4336. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  4337. }
  4338. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  4339. sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
  4340. if (sh == NULL) {
  4341. sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
  4342. /* make sure we don't swamp the stripe cache if someone else
  4343. * is trying to get access
  4344. */
  4345. schedule_timeout_uninterruptible(1);
  4346. }
  4347. /* Need to check if array will still be degraded after recovery/resync
  4348. * We don't need to check the 'failed' flag as when that gets set,
  4349. * recovery aborts.
  4350. */
  4351. for (i = 0; i < conf->raid_disks; i++)
  4352. if (conf->disks[i].rdev == NULL)
  4353. still_degraded = 1;
  4354. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  4355. set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
  4356. handle_stripe(sh);
  4357. release_stripe(sh);
  4358. return STRIPE_SECTORS;
  4359. }
  4360. static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
  4361. {
  4362. /* We may not be able to submit a whole bio at once as there
  4363. * may not be enough stripe_heads available.
  4364. * We cannot pre-allocate enough stripe_heads as we may need
  4365. * more than exist in the cache (if we allow ever large chunks).
  4366. * So we do one stripe head at a time and record in
  4367. * ->bi_hw_segments how many have been done.
  4368. *
  4369. * We *know* that this entire raid_bio is in one chunk, so
  4370. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  4371. */
  4372. struct stripe_head *sh;
  4373. int dd_idx;
  4374. sector_t sector, logical_sector, last_sector;
  4375. int scnt = 0;
  4376. int remaining;
  4377. int handled = 0;
  4378. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  4379. sector = raid5_compute_sector(conf, logical_sector,
  4380. 0, &dd_idx, NULL);
  4381. last_sector = bio_end_sector(raid_bio);
  4382. for (; logical_sector < last_sector;
  4383. logical_sector += STRIPE_SECTORS,
  4384. sector += STRIPE_SECTORS,
  4385. scnt++) {
  4386. if (scnt < raid5_bi_processed_stripes(raid_bio))
  4387. /* already done this stripe */
  4388. continue;
  4389. sh = get_active_stripe(conf, sector, 0, 1, 0);
  4390. if (!sh) {
  4391. /* failed to get a stripe - must wait */
  4392. raid5_set_bi_processed_stripes(raid_bio, scnt);
  4393. conf->retry_read_aligned = raid_bio;
  4394. return handled;
  4395. }
  4396. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  4397. release_stripe(sh);
  4398. raid5_set_bi_processed_stripes(raid_bio, scnt);
  4399. conf->retry_read_aligned = raid_bio;
  4400. return handled;
  4401. }
  4402. set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
  4403. handle_stripe(sh);
  4404. release_stripe(sh);
  4405. handled++;
  4406. }
  4407. remaining = raid5_dec_bi_active_stripes(raid_bio);
  4408. if (remaining == 0) {
  4409. trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
  4410. raid_bio, 0);
  4411. bio_endio(raid_bio, 0);
  4412. }
  4413. if (atomic_dec_and_test(&conf->active_aligned_reads))
  4414. wake_up(&conf->wait_for_stripe);
  4415. return handled;
  4416. }
  4417. #define MAX_STRIPE_BATCH 8
  4418. static int handle_active_stripes(struct r5conf *conf, int group)
  4419. {
  4420. struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
  4421. int i, batch_size = 0;
  4422. while (batch_size < MAX_STRIPE_BATCH &&
  4423. (sh = __get_priority_stripe(conf, group)) != NULL)
  4424. batch[batch_size++] = sh;
  4425. if (batch_size == 0)
  4426. return batch_size;
  4427. spin_unlock_irq(&conf->device_lock);
  4428. for (i = 0; i < batch_size; i++)
  4429. handle_stripe(batch[i]);
  4430. cond_resched();
  4431. spin_lock_irq(&conf->device_lock);
  4432. for (i = 0; i < batch_size; i++)
  4433. __release_stripe(conf, batch[i]);
  4434. return batch_size;
  4435. }
  4436. static void raid5_do_work(struct work_struct *work)
  4437. {
  4438. struct r5worker *worker = container_of(work, struct r5worker, work);
  4439. struct r5worker_group *group = worker->group;
  4440. struct r5conf *conf = group->conf;
  4441. int group_id = group - conf->worker_groups;
  4442. int handled;
  4443. struct blk_plug plug;
  4444. pr_debug("+++ raid5worker active\n");
  4445. blk_start_plug(&plug);
  4446. handled = 0;
  4447. spin_lock_irq(&conf->device_lock);
  4448. while (1) {
  4449. int batch_size, released;
  4450. released = release_stripe_list(conf);
  4451. batch_size = handle_active_stripes(conf, group_id);
  4452. if (!batch_size && !released)
  4453. break;
  4454. handled += batch_size;
  4455. }
  4456. pr_debug("%d stripes handled\n", handled);
  4457. spin_unlock_irq(&conf->device_lock);
  4458. blk_finish_plug(&plug);
  4459. pr_debug("--- raid5worker inactive\n");
  4460. }
  4461. /*
  4462. * This is our raid5 kernel thread.
  4463. *
  4464. * We scan the hash table for stripes which can be handled now.
  4465. * During the scan, completed stripes are saved for us by the interrupt
  4466. * handler, so that they will not have to wait for our next wakeup.
  4467. */
  4468. static void raid5d(struct md_thread *thread)
  4469. {
  4470. struct mddev *mddev = thread->mddev;
  4471. struct r5conf *conf = mddev->private;
  4472. int handled;
  4473. struct blk_plug plug;
  4474. pr_debug("+++ raid5d active\n");
  4475. md_check_recovery(mddev);
  4476. blk_start_plug(&plug);
  4477. handled = 0;
  4478. spin_lock_irq(&conf->device_lock);
  4479. while (1) {
  4480. struct bio *bio;
  4481. int batch_size, released;
  4482. released = release_stripe_list(conf);
  4483. if (
  4484. !list_empty(&conf->bitmap_list)) {
  4485. /* Now is a good time to flush some bitmap updates */
  4486. conf->seq_flush++;
  4487. spin_unlock_irq(&conf->device_lock);
  4488. bitmap_unplug(mddev->bitmap);
  4489. spin_lock_irq(&conf->device_lock);
  4490. conf->seq_write = conf->seq_flush;
  4491. activate_bit_delay(conf);
  4492. }
  4493. raid5_activate_delayed(conf);
  4494. while ((bio = remove_bio_from_retry(conf))) {
  4495. int ok;
  4496. spin_unlock_irq(&conf->device_lock);
  4497. ok = retry_aligned_read(conf, bio);
  4498. spin_lock_irq(&conf->device_lock);
  4499. if (!ok)
  4500. break;
  4501. handled++;
  4502. }
  4503. batch_size = handle_active_stripes(conf, ANY_GROUP);
  4504. if (!batch_size && !released)
  4505. break;
  4506. handled += batch_size;
  4507. if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
  4508. spin_unlock_irq(&conf->device_lock);
  4509. md_check_recovery(mddev);
  4510. spin_lock_irq(&conf->device_lock);
  4511. }
  4512. }
  4513. pr_debug("%d stripes handled\n", handled);
  4514. spin_unlock_irq(&conf->device_lock);
  4515. async_tx_issue_pending_all();
  4516. blk_finish_plug(&plug);
  4517. pr_debug("--- raid5d inactive\n");
  4518. }
  4519. static ssize_t
  4520. raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
  4521. {
  4522. struct r5conf *conf = mddev->private;
  4523. if (conf)
  4524. return sprintf(page, "%d\n", conf->max_nr_stripes);
  4525. else
  4526. return 0;
  4527. }
  4528. int
  4529. raid5_set_cache_size(struct mddev *mddev, int size)
  4530. {
  4531. struct r5conf *conf = mddev->private;
  4532. int err;
  4533. if (size <= 16 || size > 32768)
  4534. return -EINVAL;
  4535. while (size < conf->max_nr_stripes) {
  4536. if (drop_one_stripe(conf))
  4537. conf->max_nr_stripes--;
  4538. else
  4539. break;
  4540. }
  4541. err = md_allow_write(mddev);
  4542. if (err)
  4543. return err;
  4544. while (size > conf->max_nr_stripes) {
  4545. if (grow_one_stripe(conf))
  4546. conf->max_nr_stripes++;
  4547. else break;
  4548. }
  4549. return 0;
  4550. }
  4551. EXPORT_SYMBOL(raid5_set_cache_size);
  4552. static ssize_t
  4553. raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
  4554. {
  4555. struct r5conf *conf = mddev->private;
  4556. unsigned long new;
  4557. int err;
  4558. if (len >= PAGE_SIZE)
  4559. return -EINVAL;
  4560. if (!conf)
  4561. return -ENODEV;
  4562. if (kstrtoul(page, 10, &new))
  4563. return -EINVAL;
  4564. err = raid5_set_cache_size(mddev, new);
  4565. if (err)
  4566. return err;
  4567. return len;
  4568. }
  4569. static struct md_sysfs_entry
  4570. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  4571. raid5_show_stripe_cache_size,
  4572. raid5_store_stripe_cache_size);
  4573. static ssize_t
  4574. raid5_show_preread_threshold(struct mddev *mddev, char *page)
  4575. {
  4576. struct r5conf *conf = mddev->private;
  4577. if (conf)
  4578. return sprintf(page, "%d\n", conf->bypass_threshold);
  4579. else
  4580. return 0;
  4581. }
  4582. static ssize_t
  4583. raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
  4584. {
  4585. struct r5conf *conf = mddev->private;
  4586. unsigned long new;
  4587. if (len >= PAGE_SIZE)
  4588. return -EINVAL;
  4589. if (!conf)
  4590. return -ENODEV;
  4591. if (kstrtoul(page, 10, &new))
  4592. return -EINVAL;
  4593. if (new > conf->max_nr_stripes)
  4594. return -EINVAL;
  4595. conf->bypass_threshold = new;
  4596. return len;
  4597. }
  4598. static struct md_sysfs_entry
  4599. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  4600. S_IRUGO | S_IWUSR,
  4601. raid5_show_preread_threshold,
  4602. raid5_store_preread_threshold);
  4603. static ssize_t
  4604. stripe_cache_active_show(struct mddev *mddev, char *page)
  4605. {
  4606. struct r5conf *conf = mddev->private;
  4607. if (conf)
  4608. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  4609. else
  4610. return 0;
  4611. }
  4612. static struct md_sysfs_entry
  4613. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  4614. static ssize_t
  4615. raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
  4616. {
  4617. struct r5conf *conf = mddev->private;
  4618. if (conf)
  4619. return sprintf(page, "%d\n", conf->worker_cnt_per_group);
  4620. else
  4621. return 0;
  4622. }
  4623. static int alloc_thread_groups(struct r5conf *conf, int cnt);
  4624. static ssize_t
  4625. raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
  4626. {
  4627. struct r5conf *conf = mddev->private;
  4628. unsigned long new;
  4629. int err;
  4630. struct r5worker_group *old_groups;
  4631. int old_group_cnt;
  4632. if (len >= PAGE_SIZE)
  4633. return -EINVAL;
  4634. if (!conf)
  4635. return -ENODEV;
  4636. if (kstrtoul(page, 10, &new))
  4637. return -EINVAL;
  4638. if (new == conf->worker_cnt_per_group)
  4639. return len;
  4640. mddev_suspend(mddev);
  4641. old_groups = conf->worker_groups;
  4642. old_group_cnt = conf->worker_cnt_per_group;
  4643. conf->worker_groups = NULL;
  4644. err = alloc_thread_groups(conf, new);
  4645. if (err) {
  4646. conf->worker_groups = old_groups;
  4647. conf->worker_cnt_per_group = old_group_cnt;
  4648. } else {
  4649. if (old_groups)
  4650. kfree(old_groups[0].workers);
  4651. kfree(old_groups);
  4652. }
  4653. mddev_resume(mddev);
  4654. if (err)
  4655. return err;
  4656. return len;
  4657. }
  4658. static struct md_sysfs_entry
  4659. raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
  4660. raid5_show_group_thread_cnt,
  4661. raid5_store_group_thread_cnt);
  4662. static struct attribute *raid5_attrs[] = {
  4663. &raid5_stripecache_size.attr,
  4664. &raid5_stripecache_active.attr,
  4665. &raid5_preread_bypass_threshold.attr,
  4666. &raid5_group_thread_cnt.attr,
  4667. NULL,
  4668. };
  4669. static struct attribute_group raid5_attrs_group = {
  4670. .name = NULL,
  4671. .attrs = raid5_attrs,
  4672. };
  4673. static int alloc_thread_groups(struct r5conf *conf, int cnt)
  4674. {
  4675. int i, j;
  4676. ssize_t size;
  4677. struct r5worker *workers;
  4678. conf->worker_cnt_per_group = cnt;
  4679. if (cnt == 0) {
  4680. conf->worker_groups = NULL;
  4681. return 0;
  4682. }
  4683. conf->group_cnt = num_possible_nodes();
  4684. size = sizeof(struct r5worker) * cnt;
  4685. workers = kzalloc(size * conf->group_cnt, GFP_NOIO);
  4686. conf->worker_groups = kzalloc(sizeof(struct r5worker_group) *
  4687. conf->group_cnt, GFP_NOIO);
  4688. if (!conf->worker_groups || !workers) {
  4689. kfree(workers);
  4690. kfree(conf->worker_groups);
  4691. conf->worker_groups = NULL;
  4692. return -ENOMEM;
  4693. }
  4694. for (i = 0; i < conf->group_cnt; i++) {
  4695. struct r5worker_group *group;
  4696. group = &conf->worker_groups[i];
  4697. INIT_LIST_HEAD(&group->handle_list);
  4698. group->conf = conf;
  4699. group->workers = workers + i * cnt;
  4700. for (j = 0; j < cnt; j++) {
  4701. group->workers[j].group = group;
  4702. INIT_WORK(&group->workers[j].work, raid5_do_work);
  4703. }
  4704. }
  4705. return 0;
  4706. }
  4707. static void free_thread_groups(struct r5conf *conf)
  4708. {
  4709. if (conf->worker_groups)
  4710. kfree(conf->worker_groups[0].workers);
  4711. kfree(conf->worker_groups);
  4712. conf->worker_groups = NULL;
  4713. }
  4714. static sector_t
  4715. raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  4716. {
  4717. struct r5conf *conf = mddev->private;
  4718. if (!sectors)
  4719. sectors = mddev->dev_sectors;
  4720. if (!raid_disks)
  4721. /* size is defined by the smallest of previous and new size */
  4722. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  4723. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4724. sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
  4725. return sectors * (raid_disks - conf->max_degraded);
  4726. }
  4727. static void raid5_free_percpu(struct r5conf *conf)
  4728. {
  4729. struct raid5_percpu *percpu;
  4730. unsigned long cpu;
  4731. if (!conf->percpu)
  4732. return;
  4733. get_online_cpus();
  4734. for_each_possible_cpu(cpu) {
  4735. percpu = per_cpu_ptr(conf->percpu, cpu);
  4736. safe_put_page(percpu->spare_page);
  4737. kfree(percpu->scribble);
  4738. }
  4739. #ifdef CONFIG_HOTPLUG_CPU
  4740. unregister_cpu_notifier(&conf->cpu_notify);
  4741. #endif
  4742. put_online_cpus();
  4743. free_percpu(conf->percpu);
  4744. }
  4745. static void free_conf(struct r5conf *conf)
  4746. {
  4747. free_thread_groups(conf);
  4748. shrink_stripes(conf);
  4749. raid5_free_percpu(conf);
  4750. kfree(conf->disks);
  4751. kfree(conf->stripe_hashtbl);
  4752. kfree(conf);
  4753. }
  4754. #ifdef CONFIG_HOTPLUG_CPU
  4755. static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
  4756. void *hcpu)
  4757. {
  4758. struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
  4759. long cpu = (long)hcpu;
  4760. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  4761. switch (action) {
  4762. case CPU_UP_PREPARE:
  4763. case CPU_UP_PREPARE_FROZEN:
  4764. if (conf->level == 6 && !percpu->spare_page)
  4765. percpu->spare_page = alloc_page(GFP_KERNEL);
  4766. if (!percpu->scribble)
  4767. percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4768. if (!percpu->scribble ||
  4769. (conf->level == 6 && !percpu->spare_page)) {
  4770. safe_put_page(percpu->spare_page);
  4771. kfree(percpu->scribble);
  4772. pr_err("%s: failed memory allocation for cpu%ld\n",
  4773. __func__, cpu);
  4774. return notifier_from_errno(-ENOMEM);
  4775. }
  4776. break;
  4777. case CPU_DEAD:
  4778. case CPU_DEAD_FROZEN:
  4779. safe_put_page(percpu->spare_page);
  4780. kfree(percpu->scribble);
  4781. percpu->spare_page = NULL;
  4782. percpu->scribble = NULL;
  4783. break;
  4784. default:
  4785. break;
  4786. }
  4787. return NOTIFY_OK;
  4788. }
  4789. #endif
  4790. static int raid5_alloc_percpu(struct r5conf *conf)
  4791. {
  4792. unsigned long cpu;
  4793. struct page *spare_page;
  4794. struct raid5_percpu __percpu *allcpus;
  4795. void *scribble;
  4796. int err;
  4797. allcpus = alloc_percpu(struct raid5_percpu);
  4798. if (!allcpus)
  4799. return -ENOMEM;
  4800. conf->percpu = allcpus;
  4801. get_online_cpus();
  4802. err = 0;
  4803. for_each_present_cpu(cpu) {
  4804. if (conf->level == 6) {
  4805. spare_page = alloc_page(GFP_KERNEL);
  4806. if (!spare_page) {
  4807. err = -ENOMEM;
  4808. break;
  4809. }
  4810. per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
  4811. }
  4812. scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4813. if (!scribble) {
  4814. err = -ENOMEM;
  4815. break;
  4816. }
  4817. per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
  4818. }
  4819. #ifdef CONFIG_HOTPLUG_CPU
  4820. conf->cpu_notify.notifier_call = raid456_cpu_notify;
  4821. conf->cpu_notify.priority = 0;
  4822. if (err == 0)
  4823. err = register_cpu_notifier(&conf->cpu_notify);
  4824. #endif
  4825. put_online_cpus();
  4826. return err;
  4827. }
  4828. static struct r5conf *setup_conf(struct mddev *mddev)
  4829. {
  4830. struct r5conf *conf;
  4831. int raid_disk, memory, max_disks;
  4832. struct md_rdev *rdev;
  4833. struct disk_info *disk;
  4834. char pers_name[6];
  4835. if (mddev->new_level != 5
  4836. && mddev->new_level != 4
  4837. && mddev->new_level != 6) {
  4838. printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
  4839. mdname(mddev), mddev->new_level);
  4840. return ERR_PTR(-EIO);
  4841. }
  4842. if ((mddev->new_level == 5
  4843. && !algorithm_valid_raid5(mddev->new_layout)) ||
  4844. (mddev->new_level == 6
  4845. && !algorithm_valid_raid6(mddev->new_layout))) {
  4846. printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
  4847. mdname(mddev), mddev->new_layout);
  4848. return ERR_PTR(-EIO);
  4849. }
  4850. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  4851. printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
  4852. mdname(mddev), mddev->raid_disks);
  4853. return ERR_PTR(-EINVAL);
  4854. }
  4855. if (!mddev->new_chunk_sectors ||
  4856. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  4857. !is_power_of_2(mddev->new_chunk_sectors)) {
  4858. printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
  4859. mdname(mddev), mddev->new_chunk_sectors << 9);
  4860. return ERR_PTR(-EINVAL);
  4861. }
  4862. conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
  4863. if (conf == NULL)
  4864. goto abort;
  4865. /* Don't enable multi-threading by default*/
  4866. if (alloc_thread_groups(conf, 0))
  4867. goto abort;
  4868. spin_lock_init(&conf->device_lock);
  4869. seqcount_init(&conf->gen_lock);
  4870. init_waitqueue_head(&conf->wait_for_stripe);
  4871. init_waitqueue_head(&conf->wait_for_overlap);
  4872. INIT_LIST_HEAD(&conf->handle_list);
  4873. INIT_LIST_HEAD(&conf->hold_list);
  4874. INIT_LIST_HEAD(&conf->delayed_list);
  4875. INIT_LIST_HEAD(&conf->bitmap_list);
  4876. INIT_LIST_HEAD(&conf->inactive_list);
  4877. init_llist_head(&conf->released_stripes);
  4878. atomic_set(&conf->active_stripes, 0);
  4879. atomic_set(&conf->preread_active_stripes, 0);
  4880. atomic_set(&conf->active_aligned_reads, 0);
  4881. conf->bypass_threshold = BYPASS_THRESHOLD;
  4882. conf->recovery_disabled = mddev->recovery_disabled - 1;
  4883. conf->raid_disks = mddev->raid_disks;
  4884. if (mddev->reshape_position == MaxSector)
  4885. conf->previous_raid_disks = mddev->raid_disks;
  4886. else
  4887. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  4888. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  4889. conf->scribble_len = scribble_len(max_disks);
  4890. conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
  4891. GFP_KERNEL);
  4892. if (!conf->disks)
  4893. goto abort;
  4894. conf->mddev = mddev;
  4895. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  4896. goto abort;
  4897. conf->level = mddev->new_level;
  4898. if (raid5_alloc_percpu(conf) != 0)
  4899. goto abort;
  4900. pr_debug("raid456: run(%s) called.\n", mdname(mddev));
  4901. rdev_for_each(rdev, mddev) {
  4902. raid_disk = rdev->raid_disk;
  4903. if (raid_disk >= max_disks
  4904. || raid_disk < 0)
  4905. continue;
  4906. disk = conf->disks + raid_disk;
  4907. if (test_bit(Replacement, &rdev->flags)) {
  4908. if (disk->replacement)
  4909. goto abort;
  4910. disk->replacement = rdev;
  4911. } else {
  4912. if (disk->rdev)
  4913. goto abort;
  4914. disk->rdev = rdev;
  4915. }
  4916. if (test_bit(In_sync, &rdev->flags)) {
  4917. char b[BDEVNAME_SIZE];
  4918. printk(KERN_INFO "md/raid:%s: device %s operational as raid"
  4919. " disk %d\n",
  4920. mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
  4921. } else if (rdev->saved_raid_disk != raid_disk)
  4922. /* Cannot rely on bitmap to complete recovery */
  4923. conf->fullsync = 1;
  4924. }
  4925. conf->chunk_sectors = mddev->new_chunk_sectors;
  4926. conf->level = mddev->new_level;
  4927. if (conf->level == 6)
  4928. conf->max_degraded = 2;
  4929. else
  4930. conf->max_degraded = 1;
  4931. conf->algorithm = mddev->new_layout;
  4932. conf->max_nr_stripes = NR_STRIPES;
  4933. conf->reshape_progress = mddev->reshape_position;
  4934. if (conf->reshape_progress != MaxSector) {
  4935. conf->prev_chunk_sectors = mddev->chunk_sectors;
  4936. conf->prev_algo = mddev->layout;
  4937. }
  4938. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  4939. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  4940. if (grow_stripes(conf, conf->max_nr_stripes)) {
  4941. printk(KERN_ERR
  4942. "md/raid:%s: couldn't allocate %dkB for buffers\n",
  4943. mdname(mddev), memory);
  4944. goto abort;
  4945. } else
  4946. printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
  4947. mdname(mddev), memory);
  4948. sprintf(pers_name, "raid%d", mddev->new_level);
  4949. conf->thread = md_register_thread(raid5d, mddev, pers_name);
  4950. if (!conf->thread) {
  4951. printk(KERN_ERR
  4952. "md/raid:%s: couldn't allocate thread.\n",
  4953. mdname(mddev));
  4954. goto abort;
  4955. }
  4956. return conf;
  4957. abort:
  4958. if (conf) {
  4959. free_conf(conf);
  4960. return ERR_PTR(-EIO);
  4961. } else
  4962. return ERR_PTR(-ENOMEM);
  4963. }
  4964. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  4965. {
  4966. switch (algo) {
  4967. case ALGORITHM_PARITY_0:
  4968. if (raid_disk < max_degraded)
  4969. return 1;
  4970. break;
  4971. case ALGORITHM_PARITY_N:
  4972. if (raid_disk >= raid_disks - max_degraded)
  4973. return 1;
  4974. break;
  4975. case ALGORITHM_PARITY_0_6:
  4976. if (raid_disk == 0 ||
  4977. raid_disk == raid_disks - 1)
  4978. return 1;
  4979. break;
  4980. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4981. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4982. case ALGORITHM_LEFT_SYMMETRIC_6:
  4983. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4984. if (raid_disk == raid_disks - 1)
  4985. return 1;
  4986. }
  4987. return 0;
  4988. }
  4989. static int run(struct mddev *mddev)
  4990. {
  4991. struct r5conf *conf;
  4992. int working_disks = 0;
  4993. int dirty_parity_disks = 0;
  4994. struct md_rdev *rdev;
  4995. sector_t reshape_offset = 0;
  4996. int i;
  4997. long long min_offset_diff = 0;
  4998. int first = 1;
  4999. if (mddev->recovery_cp != MaxSector)
  5000. printk(KERN_NOTICE "md/raid:%s: not clean"
  5001. " -- starting background reconstruction\n",
  5002. mdname(mddev));
  5003. rdev_for_each(rdev, mddev) {
  5004. long long diff;
  5005. if (rdev->raid_disk < 0)
  5006. continue;
  5007. diff = (rdev->new_data_offset - rdev->data_offset);
  5008. if (first) {
  5009. min_offset_diff = diff;
  5010. first = 0;
  5011. } else if (mddev->reshape_backwards &&
  5012. diff < min_offset_diff)
  5013. min_offset_diff = diff;
  5014. else if (!mddev->reshape_backwards &&
  5015. diff > min_offset_diff)
  5016. min_offset_diff = diff;
  5017. }
  5018. if (mddev->reshape_position != MaxSector) {
  5019. /* Check that we can continue the reshape.
  5020. * Difficulties arise if the stripe we would write to
  5021. * next is at or after the stripe we would read from next.
  5022. * For a reshape that changes the number of devices, this
  5023. * is only possible for a very short time, and mdadm makes
  5024. * sure that time appears to have past before assembling
  5025. * the array. So we fail if that time hasn't passed.
  5026. * For a reshape that keeps the number of devices the same
  5027. * mdadm must be monitoring the reshape can keeping the
  5028. * critical areas read-only and backed up. It will start
  5029. * the array in read-only mode, so we check for that.
  5030. */
  5031. sector_t here_new, here_old;
  5032. int old_disks;
  5033. int max_degraded = (mddev->level == 6 ? 2 : 1);
  5034. if (mddev->new_level != mddev->level) {
  5035. printk(KERN_ERR "md/raid:%s: unsupported reshape "
  5036. "required - aborting.\n",
  5037. mdname(mddev));
  5038. return -EINVAL;
  5039. }
  5040. old_disks = mddev->raid_disks - mddev->delta_disks;
  5041. /* reshape_position must be on a new-stripe boundary, and one
  5042. * further up in new geometry must map after here in old
  5043. * geometry.
  5044. */
  5045. here_new = mddev->reshape_position;
  5046. if (sector_div(here_new, mddev->new_chunk_sectors *
  5047. (mddev->raid_disks - max_degraded))) {
  5048. printk(KERN_ERR "md/raid:%s: reshape_position not "
  5049. "on a stripe boundary\n", mdname(mddev));
  5050. return -EINVAL;
  5051. }
  5052. reshape_offset = here_new * mddev->new_chunk_sectors;
  5053. /* here_new is the stripe we will write to */
  5054. here_old = mddev->reshape_position;
  5055. sector_div(here_old, mddev->chunk_sectors *
  5056. (old_disks-max_degraded));
  5057. /* here_old is the first stripe that we might need to read
  5058. * from */
  5059. if (mddev->delta_disks == 0) {
  5060. if ((here_new * mddev->new_chunk_sectors !=
  5061. here_old * mddev->chunk_sectors)) {
  5062. printk(KERN_ERR "md/raid:%s: reshape position is"
  5063. " confused - aborting\n", mdname(mddev));
  5064. return -EINVAL;
  5065. }
  5066. /* We cannot be sure it is safe to start an in-place
  5067. * reshape. It is only safe if user-space is monitoring
  5068. * and taking constant backups.
  5069. * mdadm always starts a situation like this in
  5070. * readonly mode so it can take control before
  5071. * allowing any writes. So just check for that.
  5072. */
  5073. if (abs(min_offset_diff) >= mddev->chunk_sectors &&
  5074. abs(min_offset_diff) >= mddev->new_chunk_sectors)
  5075. /* not really in-place - so OK */;
  5076. else if (mddev->ro == 0) {
  5077. printk(KERN_ERR "md/raid:%s: in-place reshape "
  5078. "must be started in read-only mode "
  5079. "- aborting\n",
  5080. mdname(mddev));
  5081. return -EINVAL;
  5082. }
  5083. } else if (mddev->reshape_backwards
  5084. ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
  5085. here_old * mddev->chunk_sectors)
  5086. : (here_new * mddev->new_chunk_sectors >=
  5087. here_old * mddev->chunk_sectors + (-min_offset_diff))) {
  5088. /* Reading from the same stripe as writing to - bad */
  5089. printk(KERN_ERR "md/raid:%s: reshape_position too early for "
  5090. "auto-recovery - aborting.\n",
  5091. mdname(mddev));
  5092. return -EINVAL;
  5093. }
  5094. printk(KERN_INFO "md/raid:%s: reshape will continue\n",
  5095. mdname(mddev));
  5096. /* OK, we should be able to continue; */
  5097. } else {
  5098. BUG_ON(mddev->level != mddev->new_level);
  5099. BUG_ON(mddev->layout != mddev->new_layout);
  5100. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  5101. BUG_ON(mddev->delta_disks != 0);
  5102. }
  5103. if (mddev->private == NULL)
  5104. conf = setup_conf(mddev);
  5105. else
  5106. conf = mddev->private;
  5107. if (IS_ERR(conf))
  5108. return PTR_ERR(conf);
  5109. conf->min_offset_diff = min_offset_diff;
  5110. mddev->thread = conf->thread;
  5111. conf->thread = NULL;
  5112. mddev->private = conf;
  5113. for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
  5114. i++) {
  5115. rdev = conf->disks[i].rdev;
  5116. if (!rdev && conf->disks[i].replacement) {
  5117. /* The replacement is all we have yet */
  5118. rdev = conf->disks[i].replacement;
  5119. conf->disks[i].replacement = NULL;
  5120. clear_bit(Replacement, &rdev->flags);
  5121. conf->disks[i].rdev = rdev;
  5122. }
  5123. if (!rdev)
  5124. continue;
  5125. if (conf->disks[i].replacement &&
  5126. conf->reshape_progress != MaxSector) {
  5127. /* replacements and reshape simply do not mix. */
  5128. printk(KERN_ERR "md: cannot handle concurrent "
  5129. "replacement and reshape.\n");
  5130. goto abort;
  5131. }
  5132. if (test_bit(In_sync, &rdev->flags)) {
  5133. working_disks++;
  5134. continue;
  5135. }
  5136. /* This disc is not fully in-sync. However if it
  5137. * just stored parity (beyond the recovery_offset),
  5138. * when we don't need to be concerned about the
  5139. * array being dirty.
  5140. * When reshape goes 'backwards', we never have
  5141. * partially completed devices, so we only need
  5142. * to worry about reshape going forwards.
  5143. */
  5144. /* Hack because v0.91 doesn't store recovery_offset properly. */
  5145. if (mddev->major_version == 0 &&
  5146. mddev->minor_version > 90)
  5147. rdev->recovery_offset = reshape_offset;
  5148. if (rdev->recovery_offset < reshape_offset) {
  5149. /* We need to check old and new layout */
  5150. if (!only_parity(rdev->raid_disk,
  5151. conf->algorithm,
  5152. conf->raid_disks,
  5153. conf->max_degraded))
  5154. continue;
  5155. }
  5156. if (!only_parity(rdev->raid_disk,
  5157. conf->prev_algo,
  5158. conf->previous_raid_disks,
  5159. conf->max_degraded))
  5160. continue;
  5161. dirty_parity_disks++;
  5162. }
  5163. /*
  5164. * 0 for a fully functional array, 1 or 2 for a degraded array.
  5165. */
  5166. mddev->degraded = calc_degraded(conf);
  5167. if (has_failed(conf)) {
  5168. printk(KERN_ERR "md/raid:%s: not enough operational devices"
  5169. " (%d/%d failed)\n",
  5170. mdname(mddev), mddev->degraded, conf->raid_disks);
  5171. goto abort;
  5172. }
  5173. /* device size must be a multiple of chunk size */
  5174. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  5175. mddev->resync_max_sectors = mddev->dev_sectors;
  5176. if (mddev->degraded > dirty_parity_disks &&
  5177. mddev->recovery_cp != MaxSector) {
  5178. if (mddev->ok_start_degraded)
  5179. printk(KERN_WARNING
  5180. "md/raid:%s: starting dirty degraded array"
  5181. " - data corruption possible.\n",
  5182. mdname(mddev));
  5183. else {
  5184. printk(KERN_ERR
  5185. "md/raid:%s: cannot start dirty degraded array.\n",
  5186. mdname(mddev));
  5187. goto abort;
  5188. }
  5189. }
  5190. if (mddev->degraded == 0)
  5191. printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
  5192. " devices, algorithm %d\n", mdname(mddev), conf->level,
  5193. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  5194. mddev->new_layout);
  5195. else
  5196. printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
  5197. " out of %d devices, algorithm %d\n",
  5198. mdname(mddev), conf->level,
  5199. mddev->raid_disks - mddev->degraded,
  5200. mddev->raid_disks, mddev->new_layout);
  5201. print_raid5_conf(conf);
  5202. if (conf->reshape_progress != MaxSector) {
  5203. conf->reshape_safe = conf->reshape_progress;
  5204. atomic_set(&conf->reshape_stripes, 0);
  5205. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  5206. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  5207. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  5208. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  5209. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  5210. "reshape");
  5211. }
  5212. /* Ok, everything is just fine now */
  5213. if (mddev->to_remove == &raid5_attrs_group)
  5214. mddev->to_remove = NULL;
  5215. else if (mddev->kobj.sd &&
  5216. sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  5217. printk(KERN_WARNING
  5218. "raid5: failed to create sysfs attributes for %s\n",
  5219. mdname(mddev));
  5220. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  5221. if (mddev->queue) {
  5222. int chunk_size;
  5223. bool discard_supported = true;
  5224. /* read-ahead size must cover two whole stripes, which
  5225. * is 2 * (datadisks) * chunksize where 'n' is the
  5226. * number of raid devices
  5227. */
  5228. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  5229. int stripe = data_disks *
  5230. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  5231. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  5232. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  5233. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  5234. mddev->queue->backing_dev_info.congested_data = mddev;
  5235. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  5236. chunk_size = mddev->chunk_sectors << 9;
  5237. blk_queue_io_min(mddev->queue, chunk_size);
  5238. blk_queue_io_opt(mddev->queue, chunk_size *
  5239. (conf->raid_disks - conf->max_degraded));
  5240. /*
  5241. * We can only discard a whole stripe. It doesn't make sense to
  5242. * discard data disk but write parity disk
  5243. */
  5244. stripe = stripe * PAGE_SIZE;
  5245. /* Round up to power of 2, as discard handling
  5246. * currently assumes that */
  5247. while ((stripe-1) & stripe)
  5248. stripe = (stripe | (stripe-1)) + 1;
  5249. mddev->queue->limits.discard_alignment = stripe;
  5250. mddev->queue->limits.discard_granularity = stripe;
  5251. /*
  5252. * unaligned part of discard request will be ignored, so can't
  5253. * guarantee discard_zerors_data
  5254. */
  5255. mddev->queue->limits.discard_zeroes_data = 0;
  5256. blk_queue_max_write_same_sectors(mddev->queue, 0);
  5257. rdev_for_each(rdev, mddev) {
  5258. disk_stack_limits(mddev->gendisk, rdev->bdev,
  5259. rdev->data_offset << 9);
  5260. disk_stack_limits(mddev->gendisk, rdev->bdev,
  5261. rdev->new_data_offset << 9);
  5262. /*
  5263. * discard_zeroes_data is required, otherwise data
  5264. * could be lost. Consider a scenario: discard a stripe
  5265. * (the stripe could be inconsistent if
  5266. * discard_zeroes_data is 0); write one disk of the
  5267. * stripe (the stripe could be inconsistent again
  5268. * depending on which disks are used to calculate
  5269. * parity); the disk is broken; The stripe data of this
  5270. * disk is lost.
  5271. */
  5272. if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
  5273. !bdev_get_queue(rdev->bdev)->
  5274. limits.discard_zeroes_data)
  5275. discard_supported = false;
  5276. }
  5277. if (discard_supported &&
  5278. mddev->queue->limits.max_discard_sectors >= stripe &&
  5279. mddev->queue->limits.discard_granularity >= stripe)
  5280. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  5281. mddev->queue);
  5282. else
  5283. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
  5284. mddev->queue);
  5285. }
  5286. return 0;
  5287. abort:
  5288. md_unregister_thread(&mddev->thread);
  5289. print_raid5_conf(conf);
  5290. free_conf(conf);
  5291. mddev->private = NULL;
  5292. printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
  5293. return -EIO;
  5294. }
  5295. static int stop(struct mddev *mddev)
  5296. {
  5297. struct r5conf *conf = mddev->private;
  5298. md_unregister_thread(&mddev->thread);
  5299. if (mddev->queue)
  5300. mddev->queue->backing_dev_info.congested_fn = NULL;
  5301. free_conf(conf);
  5302. mddev->private = NULL;
  5303. mddev->to_remove = &raid5_attrs_group;
  5304. return 0;
  5305. }
  5306. static void status(struct seq_file *seq, struct mddev *mddev)
  5307. {
  5308. struct r5conf *conf = mddev->private;
  5309. int i;
  5310. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  5311. mddev->chunk_sectors / 2, mddev->layout);
  5312. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  5313. for (i = 0; i < conf->raid_disks; i++)
  5314. seq_printf (seq, "%s",
  5315. conf->disks[i].rdev &&
  5316. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  5317. seq_printf (seq, "]");
  5318. }
  5319. static void print_raid5_conf (struct r5conf *conf)
  5320. {
  5321. int i;
  5322. struct disk_info *tmp;
  5323. printk(KERN_DEBUG "RAID conf printout:\n");
  5324. if (!conf) {
  5325. printk("(conf==NULL)\n");
  5326. return;
  5327. }
  5328. printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
  5329. conf->raid_disks,
  5330. conf->raid_disks - conf->mddev->degraded);
  5331. for (i = 0; i < conf->raid_disks; i++) {
  5332. char b[BDEVNAME_SIZE];
  5333. tmp = conf->disks + i;
  5334. if (tmp->rdev)
  5335. printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
  5336. i, !test_bit(Faulty, &tmp->rdev->flags),
  5337. bdevname(tmp->rdev->bdev, b));
  5338. }
  5339. }
  5340. static int raid5_spare_active(struct mddev *mddev)
  5341. {
  5342. int i;
  5343. struct r5conf *conf = mddev->private;
  5344. struct disk_info *tmp;
  5345. int count = 0;
  5346. unsigned long flags;
  5347. for (i = 0; i < conf->raid_disks; i++) {
  5348. tmp = conf->disks + i;
  5349. if (tmp->replacement
  5350. && tmp->replacement->recovery_offset == MaxSector
  5351. && !test_bit(Faulty, &tmp->replacement->flags)
  5352. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  5353. /* Replacement has just become active. */
  5354. if (!tmp->rdev
  5355. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  5356. count++;
  5357. if (tmp->rdev) {
  5358. /* Replaced device not technically faulty,
  5359. * but we need to be sure it gets removed
  5360. * and never re-added.
  5361. */
  5362. set_bit(Faulty, &tmp->rdev->flags);
  5363. sysfs_notify_dirent_safe(
  5364. tmp->rdev->sysfs_state);
  5365. }
  5366. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  5367. } else if (tmp->rdev
  5368. && tmp->rdev->recovery_offset == MaxSector
  5369. && !test_bit(Faulty, &tmp->rdev->flags)
  5370. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  5371. count++;
  5372. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  5373. }
  5374. }
  5375. spin_lock_irqsave(&conf->device_lock, flags);
  5376. mddev->degraded = calc_degraded(conf);
  5377. spin_unlock_irqrestore(&conf->device_lock, flags);
  5378. print_raid5_conf(conf);
  5379. return count;
  5380. }
  5381. static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  5382. {
  5383. struct r5conf *conf = mddev->private;
  5384. int err = 0;
  5385. int number = rdev->raid_disk;
  5386. struct md_rdev **rdevp;
  5387. struct disk_info *p = conf->disks + number;
  5388. print_raid5_conf(conf);
  5389. if (rdev == p->rdev)
  5390. rdevp = &p->rdev;
  5391. else if (rdev == p->replacement)
  5392. rdevp = &p->replacement;
  5393. else
  5394. return 0;
  5395. if (number >= conf->raid_disks &&
  5396. conf->reshape_progress == MaxSector)
  5397. clear_bit(In_sync, &rdev->flags);
  5398. if (test_bit(In_sync, &rdev->flags) ||
  5399. atomic_read(&rdev->nr_pending)) {
  5400. err = -EBUSY;
  5401. goto abort;
  5402. }
  5403. /* Only remove non-faulty devices if recovery
  5404. * isn't possible.
  5405. */
  5406. if (!test_bit(Faulty, &rdev->flags) &&
  5407. mddev->recovery_disabled != conf->recovery_disabled &&
  5408. !has_failed(conf) &&
  5409. (!p->replacement || p->replacement == rdev) &&
  5410. number < conf->raid_disks) {
  5411. err = -EBUSY;
  5412. goto abort;
  5413. }
  5414. *rdevp = NULL;
  5415. synchronize_rcu();
  5416. if (atomic_read(&rdev->nr_pending)) {
  5417. /* lost the race, try later */
  5418. err = -EBUSY;
  5419. *rdevp = rdev;
  5420. } else if (p->replacement) {
  5421. /* We must have just cleared 'rdev' */
  5422. p->rdev = p->replacement;
  5423. clear_bit(Replacement, &p->replacement->flags);
  5424. smp_mb(); /* Make sure other CPUs may see both as identical
  5425. * but will never see neither - if they are careful
  5426. */
  5427. p->replacement = NULL;
  5428. clear_bit(WantReplacement, &rdev->flags);
  5429. } else
  5430. /* We might have just removed the Replacement as faulty-
  5431. * clear the bit just in case
  5432. */
  5433. clear_bit(WantReplacement, &rdev->flags);
  5434. abort:
  5435. print_raid5_conf(conf);
  5436. return err;
  5437. }
  5438. static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  5439. {
  5440. struct r5conf *conf = mddev->private;
  5441. int err = -EEXIST;
  5442. int disk;
  5443. struct disk_info *p;
  5444. int first = 0;
  5445. int last = conf->raid_disks - 1;
  5446. if (mddev->recovery_disabled == conf->recovery_disabled)
  5447. return -EBUSY;
  5448. if (rdev->saved_raid_disk < 0 && has_failed(conf))
  5449. /* no point adding a device */
  5450. return -EINVAL;
  5451. if (rdev->raid_disk >= 0)
  5452. first = last = rdev->raid_disk;
  5453. /*
  5454. * find the disk ... but prefer rdev->saved_raid_disk
  5455. * if possible.
  5456. */
  5457. if (rdev->saved_raid_disk >= 0 &&
  5458. rdev->saved_raid_disk >= first &&
  5459. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  5460. first = rdev->saved_raid_disk;
  5461. for (disk = first; disk <= last; disk++) {
  5462. p = conf->disks + disk;
  5463. if (p->rdev == NULL) {
  5464. clear_bit(In_sync, &rdev->flags);
  5465. rdev->raid_disk = disk;
  5466. err = 0;
  5467. if (rdev->saved_raid_disk != disk)
  5468. conf->fullsync = 1;
  5469. rcu_assign_pointer(p->rdev, rdev);
  5470. goto out;
  5471. }
  5472. }
  5473. for (disk = first; disk <= last; disk++) {
  5474. p = conf->disks + disk;
  5475. if (test_bit(WantReplacement, &p->rdev->flags) &&
  5476. p->replacement == NULL) {
  5477. clear_bit(In_sync, &rdev->flags);
  5478. set_bit(Replacement, &rdev->flags);
  5479. rdev->raid_disk = disk;
  5480. err = 0;
  5481. conf->fullsync = 1;
  5482. rcu_assign_pointer(p->replacement, rdev);
  5483. break;
  5484. }
  5485. }
  5486. out:
  5487. print_raid5_conf(conf);
  5488. return err;
  5489. }
  5490. static int raid5_resize(struct mddev *mddev, sector_t sectors)
  5491. {
  5492. /* no resync is happening, and there is enough space
  5493. * on all devices, so we can resize.
  5494. * We need to make sure resync covers any new space.
  5495. * If the array is shrinking we should possibly wait until
  5496. * any io in the removed space completes, but it hardly seems
  5497. * worth it.
  5498. */
  5499. sector_t newsize;
  5500. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  5501. newsize = raid5_size(mddev, sectors, mddev->raid_disks);
  5502. if (mddev->external_size &&
  5503. mddev->array_sectors > newsize)
  5504. return -EINVAL;
  5505. if (mddev->bitmap) {
  5506. int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
  5507. if (ret)
  5508. return ret;
  5509. }
  5510. md_set_array_sectors(mddev, newsize);
  5511. set_capacity(mddev->gendisk, mddev->array_sectors);
  5512. revalidate_disk(mddev->gendisk);
  5513. if (sectors > mddev->dev_sectors &&
  5514. mddev->recovery_cp > mddev->dev_sectors) {
  5515. mddev->recovery_cp = mddev->dev_sectors;
  5516. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  5517. }
  5518. mddev->dev_sectors = sectors;
  5519. mddev->resync_max_sectors = sectors;
  5520. return 0;
  5521. }
  5522. static int check_stripe_cache(struct mddev *mddev)
  5523. {
  5524. /* Can only proceed if there are plenty of stripe_heads.
  5525. * We need a minimum of one full stripe,, and for sensible progress
  5526. * it is best to have about 4 times that.
  5527. * If we require 4 times, then the default 256 4K stripe_heads will
  5528. * allow for chunk sizes up to 256K, which is probably OK.
  5529. * If the chunk size is greater, user-space should request more
  5530. * stripe_heads first.
  5531. */
  5532. struct r5conf *conf = mddev->private;
  5533. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  5534. > conf->max_nr_stripes ||
  5535. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  5536. > conf->max_nr_stripes) {
  5537. printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
  5538. mdname(mddev),
  5539. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  5540. / STRIPE_SIZE)*4);
  5541. return 0;
  5542. }
  5543. return 1;
  5544. }
  5545. static int check_reshape(struct mddev *mddev)
  5546. {
  5547. struct r5conf *conf = mddev->private;
  5548. if (mddev->delta_disks == 0 &&
  5549. mddev->new_layout == mddev->layout &&
  5550. mddev->new_chunk_sectors == mddev->chunk_sectors)
  5551. return 0; /* nothing to do */
  5552. if (has_failed(conf))
  5553. return -EINVAL;
  5554. if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
  5555. /* We might be able to shrink, but the devices must
  5556. * be made bigger first.
  5557. * For raid6, 4 is the minimum size.
  5558. * Otherwise 2 is the minimum
  5559. */
  5560. int min = 2;
  5561. if (mddev->level == 6)
  5562. min = 4;
  5563. if (mddev->raid_disks + mddev->delta_disks < min)
  5564. return -EINVAL;
  5565. }
  5566. if (!check_stripe_cache(mddev))
  5567. return -ENOSPC;
  5568. return resize_stripes(conf, (conf->previous_raid_disks
  5569. + mddev->delta_disks));
  5570. }
  5571. static int raid5_start_reshape(struct mddev *mddev)
  5572. {
  5573. struct r5conf *conf = mddev->private;
  5574. struct md_rdev *rdev;
  5575. int spares = 0;
  5576. unsigned long flags;
  5577. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  5578. return -EBUSY;
  5579. if (!check_stripe_cache(mddev))
  5580. return -ENOSPC;
  5581. if (has_failed(conf))
  5582. return -EINVAL;
  5583. rdev_for_each(rdev, mddev) {
  5584. if (!test_bit(In_sync, &rdev->flags)
  5585. && !test_bit(Faulty, &rdev->flags))
  5586. spares++;
  5587. }
  5588. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  5589. /* Not enough devices even to make a degraded array
  5590. * of that size
  5591. */
  5592. return -EINVAL;
  5593. /* Refuse to reduce size of the array. Any reductions in
  5594. * array size must be through explicit setting of array_size
  5595. * attribute.
  5596. */
  5597. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  5598. < mddev->array_sectors) {
  5599. printk(KERN_ERR "md/raid:%s: array size must be reduced "
  5600. "before number of disks\n", mdname(mddev));
  5601. return -EINVAL;
  5602. }
  5603. atomic_set(&conf->reshape_stripes, 0);
  5604. spin_lock_irq(&conf->device_lock);
  5605. write_seqcount_begin(&conf->gen_lock);
  5606. conf->previous_raid_disks = conf->raid_disks;
  5607. conf->raid_disks += mddev->delta_disks;
  5608. conf->prev_chunk_sectors = conf->chunk_sectors;
  5609. conf->chunk_sectors = mddev->new_chunk_sectors;
  5610. conf->prev_algo = conf->algorithm;
  5611. conf->algorithm = mddev->new_layout;
  5612. conf->generation++;
  5613. /* Code that selects data_offset needs to see the generation update
  5614. * if reshape_progress has been set - so a memory barrier needed.
  5615. */
  5616. smp_mb();
  5617. if (mddev->reshape_backwards)
  5618. conf->reshape_progress = raid5_size(mddev, 0, 0);
  5619. else
  5620. conf->reshape_progress = 0;
  5621. conf->reshape_safe = conf->reshape_progress;
  5622. write_seqcount_end(&conf->gen_lock);
  5623. spin_unlock_irq(&conf->device_lock);
  5624. /* Now make sure any requests that proceeded on the assumption
  5625. * the reshape wasn't running - like Discard or Read - have
  5626. * completed.
  5627. */
  5628. mddev_suspend(mddev);
  5629. mddev_resume(mddev);
  5630. /* Add some new drives, as many as will fit.
  5631. * We know there are enough to make the newly sized array work.
  5632. * Don't add devices if we are reducing the number of
  5633. * devices in the array. This is because it is not possible
  5634. * to correctly record the "partially reconstructed" state of
  5635. * such devices during the reshape and confusion could result.
  5636. */
  5637. if (mddev->delta_disks >= 0) {
  5638. rdev_for_each(rdev, mddev)
  5639. if (rdev->raid_disk < 0 &&
  5640. !test_bit(Faulty, &rdev->flags)) {
  5641. if (raid5_add_disk(mddev, rdev) == 0) {
  5642. if (rdev->raid_disk
  5643. >= conf->previous_raid_disks)
  5644. set_bit(In_sync, &rdev->flags);
  5645. else
  5646. rdev->recovery_offset = 0;
  5647. if (sysfs_link_rdev(mddev, rdev))
  5648. /* Failure here is OK */;
  5649. }
  5650. } else if (rdev->raid_disk >= conf->previous_raid_disks
  5651. && !test_bit(Faulty, &rdev->flags)) {
  5652. /* This is a spare that was manually added */
  5653. set_bit(In_sync, &rdev->flags);
  5654. }
  5655. /* When a reshape changes the number of devices,
  5656. * ->degraded is measured against the larger of the
  5657. * pre and post number of devices.
  5658. */
  5659. spin_lock_irqsave(&conf->device_lock, flags);
  5660. mddev->degraded = calc_degraded(conf);
  5661. spin_unlock_irqrestore(&conf->device_lock, flags);
  5662. }
  5663. mddev->raid_disks = conf->raid_disks;
  5664. mddev->reshape_position = conf->reshape_progress;
  5665. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5666. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  5667. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  5668. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  5669. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  5670. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  5671. "reshape");
  5672. if (!mddev->sync_thread) {
  5673. mddev->recovery = 0;
  5674. spin_lock_irq(&conf->device_lock);
  5675. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  5676. rdev_for_each(rdev, mddev)
  5677. rdev->new_data_offset = rdev->data_offset;
  5678. smp_wmb();
  5679. conf->reshape_progress = MaxSector;
  5680. mddev->reshape_position = MaxSector;
  5681. spin_unlock_irq(&conf->device_lock);
  5682. return -EAGAIN;
  5683. }
  5684. conf->reshape_checkpoint = jiffies;
  5685. md_wakeup_thread(mddev->sync_thread);
  5686. md_new_event(mddev);
  5687. return 0;
  5688. }
  5689. /* This is called from the reshape thread and should make any
  5690. * changes needed in 'conf'
  5691. */
  5692. static void end_reshape(struct r5conf *conf)
  5693. {
  5694. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  5695. struct md_rdev *rdev;
  5696. spin_lock_irq(&conf->device_lock);
  5697. conf->previous_raid_disks = conf->raid_disks;
  5698. rdev_for_each(rdev, conf->mddev)
  5699. rdev->data_offset = rdev->new_data_offset;
  5700. smp_wmb();
  5701. conf->reshape_progress = MaxSector;
  5702. spin_unlock_irq(&conf->device_lock);
  5703. wake_up(&conf->wait_for_overlap);
  5704. /* read-ahead size must cover two whole stripes, which is
  5705. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  5706. */
  5707. if (conf->mddev->queue) {
  5708. int data_disks = conf->raid_disks - conf->max_degraded;
  5709. int stripe = data_disks * ((conf->chunk_sectors << 9)
  5710. / PAGE_SIZE);
  5711. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  5712. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  5713. }
  5714. }
  5715. }
  5716. /* This is called from the raid5d thread with mddev_lock held.
  5717. * It makes config changes to the device.
  5718. */
  5719. static void raid5_finish_reshape(struct mddev *mddev)
  5720. {
  5721. struct r5conf *conf = mddev->private;
  5722. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  5723. if (mddev->delta_disks > 0) {
  5724. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  5725. set_capacity(mddev->gendisk, mddev->array_sectors);
  5726. revalidate_disk(mddev->gendisk);
  5727. } else {
  5728. int d;
  5729. spin_lock_irq(&conf->device_lock);
  5730. mddev->degraded = calc_degraded(conf);
  5731. spin_unlock_irq(&conf->device_lock);
  5732. for (d = conf->raid_disks ;
  5733. d < conf->raid_disks - mddev->delta_disks;
  5734. d++) {
  5735. struct md_rdev *rdev = conf->disks[d].rdev;
  5736. if (rdev)
  5737. clear_bit(In_sync, &rdev->flags);
  5738. rdev = conf->disks[d].replacement;
  5739. if (rdev)
  5740. clear_bit(In_sync, &rdev->flags);
  5741. }
  5742. }
  5743. mddev->layout = conf->algorithm;
  5744. mddev->chunk_sectors = conf->chunk_sectors;
  5745. mddev->reshape_position = MaxSector;
  5746. mddev->delta_disks = 0;
  5747. mddev->reshape_backwards = 0;
  5748. }
  5749. }
  5750. static void raid5_quiesce(struct mddev *mddev, int state)
  5751. {
  5752. struct r5conf *conf = mddev->private;
  5753. switch(state) {
  5754. case 2: /* resume for a suspend */
  5755. wake_up(&conf->wait_for_overlap);
  5756. break;
  5757. case 1: /* stop all writes */
  5758. spin_lock_irq(&conf->device_lock);
  5759. /* '2' tells resync/reshape to pause so that all
  5760. * active stripes can drain
  5761. */
  5762. conf->quiesce = 2;
  5763. wait_event_lock_irq(conf->wait_for_stripe,
  5764. atomic_read(&conf->active_stripes) == 0 &&
  5765. atomic_read(&conf->active_aligned_reads) == 0,
  5766. conf->device_lock);
  5767. conf->quiesce = 1;
  5768. spin_unlock_irq(&conf->device_lock);
  5769. /* allow reshape to continue */
  5770. wake_up(&conf->wait_for_overlap);
  5771. break;
  5772. case 0: /* re-enable writes */
  5773. spin_lock_irq(&conf->device_lock);
  5774. conf->quiesce = 0;
  5775. wake_up(&conf->wait_for_stripe);
  5776. wake_up(&conf->wait_for_overlap);
  5777. spin_unlock_irq(&conf->device_lock);
  5778. break;
  5779. }
  5780. }
  5781. static void *raid45_takeover_raid0(struct mddev *mddev, int level)
  5782. {
  5783. struct r0conf *raid0_conf = mddev->private;
  5784. sector_t sectors;
  5785. /* for raid0 takeover only one zone is supported */
  5786. if (raid0_conf->nr_strip_zones > 1) {
  5787. printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
  5788. mdname(mddev));
  5789. return ERR_PTR(-EINVAL);
  5790. }
  5791. sectors = raid0_conf->strip_zone[0].zone_end;
  5792. sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
  5793. mddev->dev_sectors = sectors;
  5794. mddev->new_level = level;
  5795. mddev->new_layout = ALGORITHM_PARITY_N;
  5796. mddev->new_chunk_sectors = mddev->chunk_sectors;
  5797. mddev->raid_disks += 1;
  5798. mddev->delta_disks = 1;
  5799. /* make sure it will be not marked as dirty */
  5800. mddev->recovery_cp = MaxSector;
  5801. return setup_conf(mddev);
  5802. }
  5803. static void *raid5_takeover_raid1(struct mddev *mddev)
  5804. {
  5805. int chunksect;
  5806. if (mddev->raid_disks != 2 ||
  5807. mddev->degraded > 1)
  5808. return ERR_PTR(-EINVAL);
  5809. /* Should check if there are write-behind devices? */
  5810. chunksect = 64*2; /* 64K by default */
  5811. /* The array must be an exact multiple of chunksize */
  5812. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  5813. chunksect >>= 1;
  5814. if ((chunksect<<9) < STRIPE_SIZE)
  5815. /* array size does not allow a suitable chunk size */
  5816. return ERR_PTR(-EINVAL);
  5817. mddev->new_level = 5;
  5818. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5819. mddev->new_chunk_sectors = chunksect;
  5820. return setup_conf(mddev);
  5821. }
  5822. static void *raid5_takeover_raid6(struct mddev *mddev)
  5823. {
  5824. int new_layout;
  5825. switch (mddev->layout) {
  5826. case ALGORITHM_LEFT_ASYMMETRIC_6:
  5827. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  5828. break;
  5829. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  5830. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  5831. break;
  5832. case ALGORITHM_LEFT_SYMMETRIC_6:
  5833. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5834. break;
  5835. case ALGORITHM_RIGHT_SYMMETRIC_6:
  5836. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  5837. break;
  5838. case ALGORITHM_PARITY_0_6:
  5839. new_layout = ALGORITHM_PARITY_0;
  5840. break;
  5841. case ALGORITHM_PARITY_N:
  5842. new_layout = ALGORITHM_PARITY_N;
  5843. break;
  5844. default:
  5845. return ERR_PTR(-EINVAL);
  5846. }
  5847. mddev->new_level = 5;
  5848. mddev->new_layout = new_layout;
  5849. mddev->delta_disks = -1;
  5850. mddev->raid_disks -= 1;
  5851. return setup_conf(mddev);
  5852. }
  5853. static int raid5_check_reshape(struct mddev *mddev)
  5854. {
  5855. /* For a 2-drive array, the layout and chunk size can be changed
  5856. * immediately as not restriping is needed.
  5857. * For larger arrays we record the new value - after validation
  5858. * to be used by a reshape pass.
  5859. */
  5860. struct r5conf *conf = mddev->private;
  5861. int new_chunk = mddev->new_chunk_sectors;
  5862. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  5863. return -EINVAL;
  5864. if (new_chunk > 0) {
  5865. if (!is_power_of_2(new_chunk))
  5866. return -EINVAL;
  5867. if (new_chunk < (PAGE_SIZE>>9))
  5868. return -EINVAL;
  5869. if (mddev->array_sectors & (new_chunk-1))
  5870. /* not factor of array size */
  5871. return -EINVAL;
  5872. }
  5873. /* They look valid */
  5874. if (mddev->raid_disks == 2) {
  5875. /* can make the change immediately */
  5876. if (mddev->new_layout >= 0) {
  5877. conf->algorithm = mddev->new_layout;
  5878. mddev->layout = mddev->new_layout;
  5879. }
  5880. if (new_chunk > 0) {
  5881. conf->chunk_sectors = new_chunk ;
  5882. mddev->chunk_sectors = new_chunk;
  5883. }
  5884. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5885. md_wakeup_thread(mddev->thread);
  5886. }
  5887. return check_reshape(mddev);
  5888. }
  5889. static int raid6_check_reshape(struct mddev *mddev)
  5890. {
  5891. int new_chunk = mddev->new_chunk_sectors;
  5892. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  5893. return -EINVAL;
  5894. if (new_chunk > 0) {
  5895. if (!is_power_of_2(new_chunk))
  5896. return -EINVAL;
  5897. if (new_chunk < (PAGE_SIZE >> 9))
  5898. return -EINVAL;
  5899. if (mddev->array_sectors & (new_chunk-1))
  5900. /* not factor of array size */
  5901. return -EINVAL;
  5902. }
  5903. /* They look valid */
  5904. return check_reshape(mddev);
  5905. }
  5906. static void *raid5_takeover(struct mddev *mddev)
  5907. {
  5908. /* raid5 can take over:
  5909. * raid0 - if there is only one strip zone - make it a raid4 layout
  5910. * raid1 - if there are two drives. We need to know the chunk size
  5911. * raid4 - trivial - just use a raid4 layout.
  5912. * raid6 - Providing it is a *_6 layout
  5913. */
  5914. if (mddev->level == 0)
  5915. return raid45_takeover_raid0(mddev, 5);
  5916. if (mddev->level == 1)
  5917. return raid5_takeover_raid1(mddev);
  5918. if (mddev->level == 4) {
  5919. mddev->new_layout = ALGORITHM_PARITY_N;
  5920. mddev->new_level = 5;
  5921. return setup_conf(mddev);
  5922. }
  5923. if (mddev->level == 6)
  5924. return raid5_takeover_raid6(mddev);
  5925. return ERR_PTR(-EINVAL);
  5926. }
  5927. static void *raid4_takeover(struct mddev *mddev)
  5928. {
  5929. /* raid4 can take over:
  5930. * raid0 - if there is only one strip zone
  5931. * raid5 - if layout is right
  5932. */
  5933. if (mddev->level == 0)
  5934. return raid45_takeover_raid0(mddev, 4);
  5935. if (mddev->level == 5 &&
  5936. mddev->layout == ALGORITHM_PARITY_N) {
  5937. mddev->new_layout = 0;
  5938. mddev->new_level = 4;
  5939. return setup_conf(mddev);
  5940. }
  5941. return ERR_PTR(-EINVAL);
  5942. }
  5943. static struct md_personality raid5_personality;
  5944. static void *raid6_takeover(struct mddev *mddev)
  5945. {
  5946. /* Currently can only take over a raid5. We map the
  5947. * personality to an equivalent raid6 personality
  5948. * with the Q block at the end.
  5949. */
  5950. int new_layout;
  5951. if (mddev->pers != &raid5_personality)
  5952. return ERR_PTR(-EINVAL);
  5953. if (mddev->degraded > 1)
  5954. return ERR_PTR(-EINVAL);
  5955. if (mddev->raid_disks > 253)
  5956. return ERR_PTR(-EINVAL);
  5957. if (mddev->raid_disks < 3)
  5958. return ERR_PTR(-EINVAL);
  5959. switch (mddev->layout) {
  5960. case ALGORITHM_LEFT_ASYMMETRIC:
  5961. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  5962. break;
  5963. case ALGORITHM_RIGHT_ASYMMETRIC:
  5964. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  5965. break;
  5966. case ALGORITHM_LEFT_SYMMETRIC:
  5967. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  5968. break;
  5969. case ALGORITHM_RIGHT_SYMMETRIC:
  5970. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  5971. break;
  5972. case ALGORITHM_PARITY_0:
  5973. new_layout = ALGORITHM_PARITY_0_6;
  5974. break;
  5975. case ALGORITHM_PARITY_N:
  5976. new_layout = ALGORITHM_PARITY_N;
  5977. break;
  5978. default:
  5979. return ERR_PTR(-EINVAL);
  5980. }
  5981. mddev->new_level = 6;
  5982. mddev->new_layout = new_layout;
  5983. mddev->delta_disks = 1;
  5984. mddev->raid_disks += 1;
  5985. return setup_conf(mddev);
  5986. }
  5987. static struct md_personality raid6_personality =
  5988. {
  5989. .name = "raid6",
  5990. .level = 6,
  5991. .owner = THIS_MODULE,
  5992. .make_request = make_request,
  5993. .run = run,
  5994. .stop = stop,
  5995. .status = status,
  5996. .error_handler = error,
  5997. .hot_add_disk = raid5_add_disk,
  5998. .hot_remove_disk= raid5_remove_disk,
  5999. .spare_active = raid5_spare_active,
  6000. .sync_request = sync_request,
  6001. .resize = raid5_resize,
  6002. .size = raid5_size,
  6003. .check_reshape = raid6_check_reshape,
  6004. .start_reshape = raid5_start_reshape,
  6005. .finish_reshape = raid5_finish_reshape,
  6006. .quiesce = raid5_quiesce,
  6007. .takeover = raid6_takeover,
  6008. };
  6009. static struct md_personality raid5_personality =
  6010. {
  6011. .name = "raid5",
  6012. .level = 5,
  6013. .owner = THIS_MODULE,
  6014. .make_request = make_request,
  6015. .run = run,
  6016. .stop = stop,
  6017. .status = status,
  6018. .error_handler = error,
  6019. .hot_add_disk = raid5_add_disk,
  6020. .hot_remove_disk= raid5_remove_disk,
  6021. .spare_active = raid5_spare_active,
  6022. .sync_request = sync_request,
  6023. .resize = raid5_resize,
  6024. .size = raid5_size,
  6025. .check_reshape = raid5_check_reshape,
  6026. .start_reshape = raid5_start_reshape,
  6027. .finish_reshape = raid5_finish_reshape,
  6028. .quiesce = raid5_quiesce,
  6029. .takeover = raid5_takeover,
  6030. };
  6031. static struct md_personality raid4_personality =
  6032. {
  6033. .name = "raid4",
  6034. .level = 4,
  6035. .owner = THIS_MODULE,
  6036. .make_request = make_request,
  6037. .run = run,
  6038. .stop = stop,
  6039. .status = status,
  6040. .error_handler = error,
  6041. .hot_add_disk = raid5_add_disk,
  6042. .hot_remove_disk= raid5_remove_disk,
  6043. .spare_active = raid5_spare_active,
  6044. .sync_request = sync_request,
  6045. .resize = raid5_resize,
  6046. .size = raid5_size,
  6047. .check_reshape = raid5_check_reshape,
  6048. .start_reshape = raid5_start_reshape,
  6049. .finish_reshape = raid5_finish_reshape,
  6050. .quiesce = raid5_quiesce,
  6051. .takeover = raid4_takeover,
  6052. };
  6053. static int __init raid5_init(void)
  6054. {
  6055. raid5_wq = alloc_workqueue("raid5wq",
  6056. WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
  6057. if (!raid5_wq)
  6058. return -ENOMEM;
  6059. register_md_personality(&raid6_personality);
  6060. register_md_personality(&raid5_personality);
  6061. register_md_personality(&raid4_personality);
  6062. return 0;
  6063. }
  6064. static void raid5_exit(void)
  6065. {
  6066. unregister_md_personality(&raid6_personality);
  6067. unregister_md_personality(&raid5_personality);
  6068. unregister_md_personality(&raid4_personality);
  6069. destroy_workqueue(raid5_wq);
  6070. }
  6071. module_init(raid5_init);
  6072. module_exit(raid5_exit);
  6073. MODULE_LICENSE("GPL");
  6074. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  6075. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  6076. MODULE_ALIAS("md-raid5");
  6077. MODULE_ALIAS("md-raid4");
  6078. MODULE_ALIAS("md-level-5");
  6079. MODULE_ALIAS("md-level-4");
  6080. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  6081. MODULE_ALIAS("md-raid6");
  6082. MODULE_ALIAS("md-level-6");
  6083. /* This used to be two separate modules, they were: */
  6084. MODULE_ALIAS("raid5");
  6085. MODULE_ALIAS("raid6");