raid5.c 233 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269
  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 <linux/flex_array.h>
  56. #include <linux/sched/signal.h>
  57. #include <trace/events/block.h>
  58. #include "md.h"
  59. #include "raid5.h"
  60. #include "raid0.h"
  61. #include "bitmap.h"
  62. #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
  63. #define cpu_to_group(cpu) cpu_to_node(cpu)
  64. #define ANY_GROUP NUMA_NO_NODE
  65. static bool devices_handle_discard_safely = false;
  66. module_param(devices_handle_discard_safely, bool, 0644);
  67. MODULE_PARM_DESC(devices_handle_discard_safely,
  68. "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
  69. static struct workqueue_struct *raid5_wq;
  70. static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
  71. {
  72. int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
  73. return &conf->stripe_hashtbl[hash];
  74. }
  75. static inline int stripe_hash_locks_hash(sector_t sect)
  76. {
  77. return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
  78. }
  79. static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
  80. {
  81. spin_lock_irq(conf->hash_locks + hash);
  82. spin_lock(&conf->device_lock);
  83. }
  84. static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
  85. {
  86. spin_unlock(&conf->device_lock);
  87. spin_unlock_irq(conf->hash_locks + hash);
  88. }
  89. static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
  90. {
  91. int i;
  92. local_irq_disable();
  93. spin_lock(conf->hash_locks);
  94. for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
  95. spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
  96. spin_lock(&conf->device_lock);
  97. }
  98. static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
  99. {
  100. int i;
  101. spin_unlock(&conf->device_lock);
  102. for (i = NR_STRIPE_HASH_LOCKS; i; i--)
  103. spin_unlock(conf->hash_locks + i - 1);
  104. local_irq_enable();
  105. }
  106. /* Find first data disk in a raid6 stripe */
  107. static inline int raid6_d0(struct stripe_head *sh)
  108. {
  109. if (sh->ddf_layout)
  110. /* ddf always start from first device */
  111. return 0;
  112. /* md starts just after Q block */
  113. if (sh->qd_idx == sh->disks - 1)
  114. return 0;
  115. else
  116. return sh->qd_idx + 1;
  117. }
  118. static inline int raid6_next_disk(int disk, int raid_disks)
  119. {
  120. disk++;
  121. return (disk < raid_disks) ? disk : 0;
  122. }
  123. /* When walking through the disks in a raid5, starting at raid6_d0,
  124. * We need to map each disk to a 'slot', where the data disks are slot
  125. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  126. * is raid_disks-1. This help does that mapping.
  127. */
  128. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  129. int *count, int syndrome_disks)
  130. {
  131. int slot = *count;
  132. if (sh->ddf_layout)
  133. (*count)++;
  134. if (idx == sh->pd_idx)
  135. return syndrome_disks;
  136. if (idx == sh->qd_idx)
  137. return syndrome_disks + 1;
  138. if (!sh->ddf_layout)
  139. (*count)++;
  140. return slot;
  141. }
  142. static void return_io(struct bio_list *return_bi)
  143. {
  144. struct bio *bi;
  145. while ((bi = bio_list_pop(return_bi)) != NULL) {
  146. bi->bi_iter.bi_size = 0;
  147. trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
  148. bi, 0);
  149. bio_endio(bi);
  150. }
  151. }
  152. static void print_raid5_conf (struct r5conf *conf);
  153. static int stripe_operations_active(struct stripe_head *sh)
  154. {
  155. return sh->check_state || sh->reconstruct_state ||
  156. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  157. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  158. }
  159. static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
  160. {
  161. struct r5conf *conf = sh->raid_conf;
  162. struct r5worker_group *group;
  163. int thread_cnt;
  164. int i, cpu = sh->cpu;
  165. if (!cpu_online(cpu)) {
  166. cpu = cpumask_any(cpu_online_mask);
  167. sh->cpu = cpu;
  168. }
  169. if (list_empty(&sh->lru)) {
  170. struct r5worker_group *group;
  171. group = conf->worker_groups + cpu_to_group(cpu);
  172. list_add_tail(&sh->lru, &group->handle_list);
  173. group->stripes_cnt++;
  174. sh->group = group;
  175. }
  176. if (conf->worker_cnt_per_group == 0) {
  177. md_wakeup_thread(conf->mddev->thread);
  178. return;
  179. }
  180. group = conf->worker_groups + cpu_to_group(sh->cpu);
  181. group->workers[0].working = true;
  182. /* at least one worker should run to avoid race */
  183. queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
  184. thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
  185. /* wakeup more workers */
  186. for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
  187. if (group->workers[i].working == false) {
  188. group->workers[i].working = true;
  189. queue_work_on(sh->cpu, raid5_wq,
  190. &group->workers[i].work);
  191. thread_cnt--;
  192. }
  193. }
  194. }
  195. static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
  196. struct list_head *temp_inactive_list)
  197. {
  198. int i;
  199. int injournal = 0; /* number of date pages with R5_InJournal */
  200. BUG_ON(!list_empty(&sh->lru));
  201. BUG_ON(atomic_read(&conf->active_stripes)==0);
  202. if (r5c_is_writeback(conf->log))
  203. for (i = sh->disks; i--; )
  204. if (test_bit(R5_InJournal, &sh->dev[i].flags))
  205. injournal++;
  206. /*
  207. * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
  208. * data in journal, so they are not released to cached lists
  209. */
  210. if (conf->quiesce && r5c_is_writeback(conf->log) &&
  211. !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) {
  212. if (test_bit(STRIPE_R5C_CACHING, &sh->state))
  213. r5c_make_stripe_write_out(sh);
  214. set_bit(STRIPE_HANDLE, &sh->state);
  215. }
  216. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  217. if (test_bit(STRIPE_DELAYED, &sh->state) &&
  218. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  219. list_add_tail(&sh->lru, &conf->delayed_list);
  220. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  221. sh->bm_seq - conf->seq_write > 0)
  222. list_add_tail(&sh->lru, &conf->bitmap_list);
  223. else {
  224. clear_bit(STRIPE_DELAYED, &sh->state);
  225. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  226. if (conf->worker_cnt_per_group == 0) {
  227. list_add_tail(&sh->lru, &conf->handle_list);
  228. } else {
  229. raid5_wakeup_stripe_thread(sh);
  230. return;
  231. }
  232. }
  233. md_wakeup_thread(conf->mddev->thread);
  234. } else {
  235. BUG_ON(stripe_operations_active(sh));
  236. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  237. if (atomic_dec_return(&conf->preread_active_stripes)
  238. < IO_THRESHOLD)
  239. md_wakeup_thread(conf->mddev->thread);
  240. atomic_dec(&conf->active_stripes);
  241. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  242. if (!r5c_is_writeback(conf->log))
  243. list_add_tail(&sh->lru, temp_inactive_list);
  244. else {
  245. WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
  246. if (injournal == 0)
  247. list_add_tail(&sh->lru, temp_inactive_list);
  248. else if (injournal == conf->raid_disks - conf->max_degraded) {
  249. /* full stripe */
  250. if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
  251. atomic_inc(&conf->r5c_cached_full_stripes);
  252. if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
  253. atomic_dec(&conf->r5c_cached_partial_stripes);
  254. list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
  255. r5c_check_cached_full_stripe(conf);
  256. } else
  257. /*
  258. * STRIPE_R5C_PARTIAL_STRIPE is set in
  259. * r5c_try_caching_write(). No need to
  260. * set it again.
  261. */
  262. list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
  263. }
  264. }
  265. }
  266. }
  267. static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
  268. struct list_head *temp_inactive_list)
  269. {
  270. if (atomic_dec_and_test(&sh->count))
  271. do_release_stripe(conf, sh, temp_inactive_list);
  272. }
  273. /*
  274. * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
  275. *
  276. * Be careful: Only one task can add/delete stripes from temp_inactive_list at
  277. * given time. Adding stripes only takes device lock, while deleting stripes
  278. * only takes hash lock.
  279. */
  280. static void release_inactive_stripe_list(struct r5conf *conf,
  281. struct list_head *temp_inactive_list,
  282. int hash)
  283. {
  284. int size;
  285. bool do_wakeup = false;
  286. unsigned long flags;
  287. if (hash == NR_STRIPE_HASH_LOCKS) {
  288. size = NR_STRIPE_HASH_LOCKS;
  289. hash = NR_STRIPE_HASH_LOCKS - 1;
  290. } else
  291. size = 1;
  292. while (size) {
  293. struct list_head *list = &temp_inactive_list[size - 1];
  294. /*
  295. * We don't hold any lock here yet, raid5_get_active_stripe() might
  296. * remove stripes from the list
  297. */
  298. if (!list_empty_careful(list)) {
  299. spin_lock_irqsave(conf->hash_locks + hash, flags);
  300. if (list_empty(conf->inactive_list + hash) &&
  301. !list_empty(list))
  302. atomic_dec(&conf->empty_inactive_list_nr);
  303. list_splice_tail_init(list, conf->inactive_list + hash);
  304. do_wakeup = true;
  305. spin_unlock_irqrestore(conf->hash_locks + hash, flags);
  306. }
  307. size--;
  308. hash--;
  309. }
  310. if (do_wakeup) {
  311. wake_up(&conf->wait_for_stripe);
  312. if (atomic_read(&conf->active_stripes) == 0)
  313. wake_up(&conf->wait_for_quiescent);
  314. if (conf->retry_read_aligned)
  315. md_wakeup_thread(conf->mddev->thread);
  316. }
  317. }
  318. /* should hold conf->device_lock already */
  319. static int release_stripe_list(struct r5conf *conf,
  320. struct list_head *temp_inactive_list)
  321. {
  322. struct stripe_head *sh, *t;
  323. int count = 0;
  324. struct llist_node *head;
  325. head = llist_del_all(&conf->released_stripes);
  326. head = llist_reverse_order(head);
  327. llist_for_each_entry_safe(sh, t, head, release_list) {
  328. int hash;
  329. /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
  330. smp_mb();
  331. clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
  332. /*
  333. * Don't worry the bit is set here, because if the bit is set
  334. * again, the count is always > 1. This is true for
  335. * STRIPE_ON_UNPLUG_LIST bit too.
  336. */
  337. hash = sh->hash_lock_index;
  338. __release_stripe(conf, sh, &temp_inactive_list[hash]);
  339. count++;
  340. }
  341. return count;
  342. }
  343. void raid5_release_stripe(struct stripe_head *sh)
  344. {
  345. struct r5conf *conf = sh->raid_conf;
  346. unsigned long flags;
  347. struct list_head list;
  348. int hash;
  349. bool wakeup;
  350. /* Avoid release_list until the last reference.
  351. */
  352. if (atomic_add_unless(&sh->count, -1, 1))
  353. return;
  354. if (unlikely(!conf->mddev->thread) ||
  355. test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
  356. goto slow_path;
  357. wakeup = llist_add(&sh->release_list, &conf->released_stripes);
  358. if (wakeup)
  359. md_wakeup_thread(conf->mddev->thread);
  360. return;
  361. slow_path:
  362. local_irq_save(flags);
  363. /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
  364. if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
  365. INIT_LIST_HEAD(&list);
  366. hash = sh->hash_lock_index;
  367. do_release_stripe(conf, sh, &list);
  368. spin_unlock(&conf->device_lock);
  369. release_inactive_stripe_list(conf, &list, hash);
  370. }
  371. local_irq_restore(flags);
  372. }
  373. static inline void remove_hash(struct stripe_head *sh)
  374. {
  375. pr_debug("remove_hash(), stripe %llu\n",
  376. (unsigned long long)sh->sector);
  377. hlist_del_init(&sh->hash);
  378. }
  379. static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
  380. {
  381. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  382. pr_debug("insert_hash(), stripe %llu\n",
  383. (unsigned long long)sh->sector);
  384. hlist_add_head(&sh->hash, hp);
  385. }
  386. /* find an idle stripe, make sure it is unhashed, and return it. */
  387. static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
  388. {
  389. struct stripe_head *sh = NULL;
  390. struct list_head *first;
  391. if (list_empty(conf->inactive_list + hash))
  392. goto out;
  393. first = (conf->inactive_list + hash)->next;
  394. sh = list_entry(first, struct stripe_head, lru);
  395. list_del_init(first);
  396. remove_hash(sh);
  397. atomic_inc(&conf->active_stripes);
  398. BUG_ON(hash != sh->hash_lock_index);
  399. if (list_empty(conf->inactive_list + hash))
  400. atomic_inc(&conf->empty_inactive_list_nr);
  401. out:
  402. return sh;
  403. }
  404. static void shrink_buffers(struct stripe_head *sh)
  405. {
  406. struct page *p;
  407. int i;
  408. int num = sh->raid_conf->pool_size;
  409. for (i = 0; i < num ; i++) {
  410. WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
  411. p = sh->dev[i].page;
  412. if (!p)
  413. continue;
  414. sh->dev[i].page = NULL;
  415. put_page(p);
  416. }
  417. }
  418. static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
  419. {
  420. int i;
  421. int num = sh->raid_conf->pool_size;
  422. for (i = 0; i < num; i++) {
  423. struct page *page;
  424. if (!(page = alloc_page(gfp))) {
  425. return 1;
  426. }
  427. sh->dev[i].page = page;
  428. sh->dev[i].orig_page = page;
  429. }
  430. return 0;
  431. }
  432. static void raid5_build_block(struct stripe_head *sh, int i, int previous);
  433. static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
  434. struct stripe_head *sh);
  435. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  436. {
  437. struct r5conf *conf = sh->raid_conf;
  438. int i, seq;
  439. BUG_ON(atomic_read(&sh->count) != 0);
  440. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  441. BUG_ON(stripe_operations_active(sh));
  442. BUG_ON(sh->batch_head);
  443. pr_debug("init_stripe called, stripe %llu\n",
  444. (unsigned long long)sector);
  445. retry:
  446. seq = read_seqcount_begin(&conf->gen_lock);
  447. sh->generation = conf->generation - previous;
  448. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  449. sh->sector = sector;
  450. stripe_set_idx(sector, conf, previous, sh);
  451. sh->state = 0;
  452. for (i = sh->disks; i--; ) {
  453. struct r5dev *dev = &sh->dev[i];
  454. if (dev->toread || dev->read || dev->towrite || dev->written ||
  455. test_bit(R5_LOCKED, &dev->flags)) {
  456. pr_err("sector=%llx i=%d %p %p %p %p %d\n",
  457. (unsigned long long)sh->sector, i, dev->toread,
  458. dev->read, dev->towrite, dev->written,
  459. test_bit(R5_LOCKED, &dev->flags));
  460. WARN_ON(1);
  461. }
  462. dev->flags = 0;
  463. raid5_build_block(sh, i, previous);
  464. }
  465. if (read_seqcount_retry(&conf->gen_lock, seq))
  466. goto retry;
  467. sh->overwrite_disks = 0;
  468. insert_hash(conf, sh);
  469. sh->cpu = smp_processor_id();
  470. set_bit(STRIPE_BATCH_READY, &sh->state);
  471. }
  472. static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
  473. short generation)
  474. {
  475. struct stripe_head *sh;
  476. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  477. hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
  478. if (sh->sector == sector && sh->generation == generation)
  479. return sh;
  480. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  481. return NULL;
  482. }
  483. /*
  484. * Need to check if array has failed when deciding whether to:
  485. * - start an array
  486. * - remove non-faulty devices
  487. * - add a spare
  488. * - allow a reshape
  489. * This determination is simple when no reshape is happening.
  490. * However if there is a reshape, we need to carefully check
  491. * both the before and after sections.
  492. * This is because some failed devices may only affect one
  493. * of the two sections, and some non-in_sync devices may
  494. * be insync in the section most affected by failed devices.
  495. */
  496. int raid5_calc_degraded(struct r5conf *conf)
  497. {
  498. int degraded, degraded2;
  499. int i;
  500. rcu_read_lock();
  501. degraded = 0;
  502. for (i = 0; i < conf->previous_raid_disks; i++) {
  503. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  504. if (rdev && test_bit(Faulty, &rdev->flags))
  505. rdev = rcu_dereference(conf->disks[i].replacement);
  506. if (!rdev || test_bit(Faulty, &rdev->flags))
  507. degraded++;
  508. else if (test_bit(In_sync, &rdev->flags))
  509. ;
  510. else
  511. /* not in-sync or faulty.
  512. * If the reshape increases the number of devices,
  513. * this is being recovered by the reshape, so
  514. * this 'previous' section is not in_sync.
  515. * If the number of devices is being reduced however,
  516. * the device can only be part of the array if
  517. * we are reverting a reshape, so this section will
  518. * be in-sync.
  519. */
  520. if (conf->raid_disks >= conf->previous_raid_disks)
  521. degraded++;
  522. }
  523. rcu_read_unlock();
  524. if (conf->raid_disks == conf->previous_raid_disks)
  525. return degraded;
  526. rcu_read_lock();
  527. degraded2 = 0;
  528. for (i = 0; i < conf->raid_disks; i++) {
  529. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  530. if (rdev && test_bit(Faulty, &rdev->flags))
  531. rdev = rcu_dereference(conf->disks[i].replacement);
  532. if (!rdev || test_bit(Faulty, &rdev->flags))
  533. degraded2++;
  534. else if (test_bit(In_sync, &rdev->flags))
  535. ;
  536. else
  537. /* not in-sync or faulty.
  538. * If reshape increases the number of devices, this
  539. * section has already been recovered, else it
  540. * almost certainly hasn't.
  541. */
  542. if (conf->raid_disks <= conf->previous_raid_disks)
  543. degraded2++;
  544. }
  545. rcu_read_unlock();
  546. if (degraded2 > degraded)
  547. return degraded2;
  548. return degraded;
  549. }
  550. static int has_failed(struct r5conf *conf)
  551. {
  552. int degraded;
  553. if (conf->mddev->reshape_position == MaxSector)
  554. return conf->mddev->degraded > conf->max_degraded;
  555. degraded = raid5_calc_degraded(conf);
  556. if (degraded > conf->max_degraded)
  557. return 1;
  558. return 0;
  559. }
  560. struct stripe_head *
  561. raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
  562. int previous, int noblock, int noquiesce)
  563. {
  564. struct stripe_head *sh;
  565. int hash = stripe_hash_locks_hash(sector);
  566. int inc_empty_inactive_list_flag;
  567. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  568. spin_lock_irq(conf->hash_locks + hash);
  569. do {
  570. wait_event_lock_irq(conf->wait_for_quiescent,
  571. conf->quiesce == 0 || noquiesce,
  572. *(conf->hash_locks + hash));
  573. sh = __find_stripe(conf, sector, conf->generation - previous);
  574. if (!sh) {
  575. if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
  576. sh = get_free_stripe(conf, hash);
  577. if (!sh && !test_bit(R5_DID_ALLOC,
  578. &conf->cache_state))
  579. set_bit(R5_ALLOC_MORE,
  580. &conf->cache_state);
  581. }
  582. if (noblock && sh == NULL)
  583. break;
  584. r5c_check_stripe_cache_usage(conf);
  585. if (!sh) {
  586. set_bit(R5_INACTIVE_BLOCKED,
  587. &conf->cache_state);
  588. r5l_wake_reclaim(conf->log, 0);
  589. wait_event_lock_irq(
  590. conf->wait_for_stripe,
  591. !list_empty(conf->inactive_list + hash) &&
  592. (atomic_read(&conf->active_stripes)
  593. < (conf->max_nr_stripes * 3 / 4)
  594. || !test_bit(R5_INACTIVE_BLOCKED,
  595. &conf->cache_state)),
  596. *(conf->hash_locks + hash));
  597. clear_bit(R5_INACTIVE_BLOCKED,
  598. &conf->cache_state);
  599. } else {
  600. init_stripe(sh, sector, previous);
  601. atomic_inc(&sh->count);
  602. }
  603. } else if (!atomic_inc_not_zero(&sh->count)) {
  604. spin_lock(&conf->device_lock);
  605. if (!atomic_read(&sh->count)) {
  606. if (!test_bit(STRIPE_HANDLE, &sh->state))
  607. atomic_inc(&conf->active_stripes);
  608. BUG_ON(list_empty(&sh->lru) &&
  609. !test_bit(STRIPE_EXPANDING, &sh->state));
  610. inc_empty_inactive_list_flag = 0;
  611. if (!list_empty(conf->inactive_list + hash))
  612. inc_empty_inactive_list_flag = 1;
  613. list_del_init(&sh->lru);
  614. if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
  615. atomic_inc(&conf->empty_inactive_list_nr);
  616. if (sh->group) {
  617. sh->group->stripes_cnt--;
  618. sh->group = NULL;
  619. }
  620. }
  621. atomic_inc(&sh->count);
  622. spin_unlock(&conf->device_lock);
  623. }
  624. } while (sh == NULL);
  625. spin_unlock_irq(conf->hash_locks + hash);
  626. return sh;
  627. }
  628. static bool is_full_stripe_write(struct stripe_head *sh)
  629. {
  630. BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
  631. return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
  632. }
  633. static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
  634. {
  635. local_irq_disable();
  636. if (sh1 > sh2) {
  637. spin_lock(&sh2->stripe_lock);
  638. spin_lock_nested(&sh1->stripe_lock, 1);
  639. } else {
  640. spin_lock(&sh1->stripe_lock);
  641. spin_lock_nested(&sh2->stripe_lock, 1);
  642. }
  643. }
  644. static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
  645. {
  646. spin_unlock(&sh1->stripe_lock);
  647. spin_unlock(&sh2->stripe_lock);
  648. local_irq_enable();
  649. }
  650. /* Only freshly new full stripe normal write stripe can be added to a batch list */
  651. static bool stripe_can_batch(struct stripe_head *sh)
  652. {
  653. struct r5conf *conf = sh->raid_conf;
  654. if (conf->log)
  655. return false;
  656. return test_bit(STRIPE_BATCH_READY, &sh->state) &&
  657. !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
  658. is_full_stripe_write(sh);
  659. }
  660. /* we only do back search */
  661. static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
  662. {
  663. struct stripe_head *head;
  664. sector_t head_sector, tmp_sec;
  665. int hash;
  666. int dd_idx;
  667. int inc_empty_inactive_list_flag;
  668. /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
  669. tmp_sec = sh->sector;
  670. if (!sector_div(tmp_sec, conf->chunk_sectors))
  671. return;
  672. head_sector = sh->sector - STRIPE_SECTORS;
  673. hash = stripe_hash_locks_hash(head_sector);
  674. spin_lock_irq(conf->hash_locks + hash);
  675. head = __find_stripe(conf, head_sector, conf->generation);
  676. if (head && !atomic_inc_not_zero(&head->count)) {
  677. spin_lock(&conf->device_lock);
  678. if (!atomic_read(&head->count)) {
  679. if (!test_bit(STRIPE_HANDLE, &head->state))
  680. atomic_inc(&conf->active_stripes);
  681. BUG_ON(list_empty(&head->lru) &&
  682. !test_bit(STRIPE_EXPANDING, &head->state));
  683. inc_empty_inactive_list_flag = 0;
  684. if (!list_empty(conf->inactive_list + hash))
  685. inc_empty_inactive_list_flag = 1;
  686. list_del_init(&head->lru);
  687. if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
  688. atomic_inc(&conf->empty_inactive_list_nr);
  689. if (head->group) {
  690. head->group->stripes_cnt--;
  691. head->group = NULL;
  692. }
  693. }
  694. atomic_inc(&head->count);
  695. spin_unlock(&conf->device_lock);
  696. }
  697. spin_unlock_irq(conf->hash_locks + hash);
  698. if (!head)
  699. return;
  700. if (!stripe_can_batch(head))
  701. goto out;
  702. lock_two_stripes(head, sh);
  703. /* clear_batch_ready clear the flag */
  704. if (!stripe_can_batch(head) || !stripe_can_batch(sh))
  705. goto unlock_out;
  706. if (sh->batch_head)
  707. goto unlock_out;
  708. dd_idx = 0;
  709. while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
  710. dd_idx++;
  711. if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
  712. bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
  713. goto unlock_out;
  714. if (head->batch_head) {
  715. spin_lock(&head->batch_head->batch_lock);
  716. /* This batch list is already running */
  717. if (!stripe_can_batch(head)) {
  718. spin_unlock(&head->batch_head->batch_lock);
  719. goto unlock_out;
  720. }
  721. /*
  722. * at this point, head's BATCH_READY could be cleared, but we
  723. * can still add the stripe to batch list
  724. */
  725. list_add(&sh->batch_list, &head->batch_list);
  726. spin_unlock(&head->batch_head->batch_lock);
  727. sh->batch_head = head->batch_head;
  728. } else {
  729. head->batch_head = head;
  730. sh->batch_head = head->batch_head;
  731. spin_lock(&head->batch_lock);
  732. list_add_tail(&sh->batch_list, &head->batch_list);
  733. spin_unlock(&head->batch_lock);
  734. }
  735. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  736. if (atomic_dec_return(&conf->preread_active_stripes)
  737. < IO_THRESHOLD)
  738. md_wakeup_thread(conf->mddev->thread);
  739. if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
  740. int seq = sh->bm_seq;
  741. if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
  742. sh->batch_head->bm_seq > seq)
  743. seq = sh->batch_head->bm_seq;
  744. set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
  745. sh->batch_head->bm_seq = seq;
  746. }
  747. atomic_inc(&sh->count);
  748. unlock_out:
  749. unlock_two_stripes(head, sh);
  750. out:
  751. raid5_release_stripe(head);
  752. }
  753. /* Determine if 'data_offset' or 'new_data_offset' should be used
  754. * in this stripe_head.
  755. */
  756. static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
  757. {
  758. sector_t progress = conf->reshape_progress;
  759. /* Need a memory barrier to make sure we see the value
  760. * of conf->generation, or ->data_offset that was set before
  761. * reshape_progress was updated.
  762. */
  763. smp_rmb();
  764. if (progress == MaxSector)
  765. return 0;
  766. if (sh->generation == conf->generation - 1)
  767. return 0;
  768. /* We are in a reshape, and this is a new-generation stripe,
  769. * so use new_data_offset.
  770. */
  771. return 1;
  772. }
  773. static void flush_deferred_bios(struct r5conf *conf)
  774. {
  775. struct bio_list tmp;
  776. struct bio *bio;
  777. if (!conf->batch_bio_dispatch || !conf->group_cnt)
  778. return;
  779. bio_list_init(&tmp);
  780. spin_lock(&conf->pending_bios_lock);
  781. bio_list_merge(&tmp, &conf->pending_bios);
  782. bio_list_init(&conf->pending_bios);
  783. spin_unlock(&conf->pending_bios_lock);
  784. while ((bio = bio_list_pop(&tmp)))
  785. generic_make_request(bio);
  786. }
  787. static void defer_bio_issue(struct r5conf *conf, struct bio *bio)
  788. {
  789. /*
  790. * change group_cnt will drain all bios, so this is safe
  791. *
  792. * A read generally means a read-modify-write, which usually means a
  793. * randwrite, so we don't delay it
  794. */
  795. if (!conf->batch_bio_dispatch || !conf->group_cnt ||
  796. bio_op(bio) == REQ_OP_READ) {
  797. generic_make_request(bio);
  798. return;
  799. }
  800. spin_lock(&conf->pending_bios_lock);
  801. bio_list_add(&conf->pending_bios, bio);
  802. spin_unlock(&conf->pending_bios_lock);
  803. md_wakeup_thread(conf->mddev->thread);
  804. }
  805. static void
  806. raid5_end_read_request(struct bio *bi);
  807. static void
  808. raid5_end_write_request(struct bio *bi);
  809. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  810. {
  811. struct r5conf *conf = sh->raid_conf;
  812. int i, disks = sh->disks;
  813. struct stripe_head *head_sh = sh;
  814. might_sleep();
  815. if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  816. /* writing out phase */
  817. if (s->waiting_extra_page)
  818. return;
  819. if (r5l_write_stripe(conf->log, sh) == 0)
  820. return;
  821. } else { /* caching phase */
  822. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) {
  823. r5c_cache_data(conf->log, sh, s);
  824. return;
  825. }
  826. }
  827. for (i = disks; i--; ) {
  828. int op, op_flags = 0;
  829. int replace_only = 0;
  830. struct bio *bi, *rbi;
  831. struct md_rdev *rdev, *rrdev = NULL;
  832. sh = head_sh;
  833. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  834. op = REQ_OP_WRITE;
  835. if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
  836. op_flags = REQ_FUA;
  837. if (test_bit(R5_Discard, &sh->dev[i].flags))
  838. op = REQ_OP_DISCARD;
  839. } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  840. op = REQ_OP_READ;
  841. else if (test_and_clear_bit(R5_WantReplace,
  842. &sh->dev[i].flags)) {
  843. op = REQ_OP_WRITE;
  844. replace_only = 1;
  845. } else
  846. continue;
  847. if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
  848. op_flags |= REQ_SYNC;
  849. again:
  850. bi = &sh->dev[i].req;
  851. rbi = &sh->dev[i].rreq; /* For writing to replacement */
  852. rcu_read_lock();
  853. rrdev = rcu_dereference(conf->disks[i].replacement);
  854. smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
  855. rdev = rcu_dereference(conf->disks[i].rdev);
  856. if (!rdev) {
  857. rdev = rrdev;
  858. rrdev = NULL;
  859. }
  860. if (op_is_write(op)) {
  861. if (replace_only)
  862. rdev = NULL;
  863. if (rdev == rrdev)
  864. /* We raced and saw duplicates */
  865. rrdev = NULL;
  866. } else {
  867. if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
  868. rdev = rrdev;
  869. rrdev = NULL;
  870. }
  871. if (rdev && test_bit(Faulty, &rdev->flags))
  872. rdev = NULL;
  873. if (rdev)
  874. atomic_inc(&rdev->nr_pending);
  875. if (rrdev && test_bit(Faulty, &rrdev->flags))
  876. rrdev = NULL;
  877. if (rrdev)
  878. atomic_inc(&rrdev->nr_pending);
  879. rcu_read_unlock();
  880. /* We have already checked bad blocks for reads. Now
  881. * need to check for writes. We never accept write errors
  882. * on the replacement, so we don't to check rrdev.
  883. */
  884. while (op_is_write(op) && rdev &&
  885. test_bit(WriteErrorSeen, &rdev->flags)) {
  886. sector_t first_bad;
  887. int bad_sectors;
  888. int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  889. &first_bad, &bad_sectors);
  890. if (!bad)
  891. break;
  892. if (bad < 0) {
  893. set_bit(BlockedBadBlocks, &rdev->flags);
  894. if (!conf->mddev->external &&
  895. conf->mddev->sb_flags) {
  896. /* It is very unlikely, but we might
  897. * still need to write out the
  898. * bad block log - better give it
  899. * a chance*/
  900. md_check_recovery(conf->mddev);
  901. }
  902. /*
  903. * Because md_wait_for_blocked_rdev
  904. * will dec nr_pending, we must
  905. * increment it first.
  906. */
  907. atomic_inc(&rdev->nr_pending);
  908. md_wait_for_blocked_rdev(rdev, conf->mddev);
  909. } else {
  910. /* Acknowledged bad block - skip the write */
  911. rdev_dec_pending(rdev, conf->mddev);
  912. rdev = NULL;
  913. }
  914. }
  915. if (rdev) {
  916. if (s->syncing || s->expanding || s->expanded
  917. || s->replacing)
  918. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  919. set_bit(STRIPE_IO_STARTED, &sh->state);
  920. bi->bi_bdev = rdev->bdev;
  921. bio_set_op_attrs(bi, op, op_flags);
  922. bi->bi_end_io = op_is_write(op)
  923. ? raid5_end_write_request
  924. : raid5_end_read_request;
  925. bi->bi_private = sh;
  926. pr_debug("%s: for %llu schedule op %d on disc %d\n",
  927. __func__, (unsigned long long)sh->sector,
  928. bi->bi_opf, i);
  929. atomic_inc(&sh->count);
  930. if (sh != head_sh)
  931. atomic_inc(&head_sh->count);
  932. if (use_new_offset(conf, sh))
  933. bi->bi_iter.bi_sector = (sh->sector
  934. + rdev->new_data_offset);
  935. else
  936. bi->bi_iter.bi_sector = (sh->sector
  937. + rdev->data_offset);
  938. if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
  939. bi->bi_opf |= REQ_NOMERGE;
  940. if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
  941. WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
  942. if (!op_is_write(op) &&
  943. test_bit(R5_InJournal, &sh->dev[i].flags))
  944. /*
  945. * issuing read for a page in journal, this
  946. * must be preparing for prexor in rmw; read
  947. * the data into orig_page
  948. */
  949. sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
  950. else
  951. sh->dev[i].vec.bv_page = sh->dev[i].page;
  952. bi->bi_vcnt = 1;
  953. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  954. bi->bi_io_vec[0].bv_offset = 0;
  955. bi->bi_iter.bi_size = STRIPE_SIZE;
  956. /*
  957. * If this is discard request, set bi_vcnt 0. We don't
  958. * want to confuse SCSI because SCSI will replace payload
  959. */
  960. if (op == REQ_OP_DISCARD)
  961. bi->bi_vcnt = 0;
  962. if (rrdev)
  963. set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
  964. if (conf->mddev->gendisk)
  965. trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
  966. bi, disk_devt(conf->mddev->gendisk),
  967. sh->dev[i].sector);
  968. defer_bio_issue(conf, bi);
  969. }
  970. if (rrdev) {
  971. if (s->syncing || s->expanding || s->expanded
  972. || s->replacing)
  973. md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
  974. set_bit(STRIPE_IO_STARTED, &sh->state);
  975. rbi->bi_bdev = rrdev->bdev;
  976. bio_set_op_attrs(rbi, op, op_flags);
  977. BUG_ON(!op_is_write(op));
  978. rbi->bi_end_io = raid5_end_write_request;
  979. rbi->bi_private = sh;
  980. pr_debug("%s: for %llu schedule op %d on "
  981. "replacement disc %d\n",
  982. __func__, (unsigned long long)sh->sector,
  983. rbi->bi_opf, i);
  984. atomic_inc(&sh->count);
  985. if (sh != head_sh)
  986. atomic_inc(&head_sh->count);
  987. if (use_new_offset(conf, sh))
  988. rbi->bi_iter.bi_sector = (sh->sector
  989. + rrdev->new_data_offset);
  990. else
  991. rbi->bi_iter.bi_sector = (sh->sector
  992. + rrdev->data_offset);
  993. if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
  994. WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
  995. sh->dev[i].rvec.bv_page = sh->dev[i].page;
  996. rbi->bi_vcnt = 1;
  997. rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  998. rbi->bi_io_vec[0].bv_offset = 0;
  999. rbi->bi_iter.bi_size = STRIPE_SIZE;
  1000. /*
  1001. * If this is discard request, set bi_vcnt 0. We don't
  1002. * want to confuse SCSI because SCSI will replace payload
  1003. */
  1004. if (op == REQ_OP_DISCARD)
  1005. rbi->bi_vcnt = 0;
  1006. if (conf->mddev->gendisk)
  1007. trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
  1008. rbi, disk_devt(conf->mddev->gendisk),
  1009. sh->dev[i].sector);
  1010. defer_bio_issue(conf, rbi);
  1011. }
  1012. if (!rdev && !rrdev) {
  1013. if (op_is_write(op))
  1014. set_bit(STRIPE_DEGRADED, &sh->state);
  1015. pr_debug("skip op %d on disc %d for sector %llu\n",
  1016. bi->bi_opf, i, (unsigned long long)sh->sector);
  1017. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1018. set_bit(STRIPE_HANDLE, &sh->state);
  1019. }
  1020. if (!head_sh->batch_head)
  1021. continue;
  1022. sh = list_first_entry(&sh->batch_list, struct stripe_head,
  1023. batch_list);
  1024. if (sh != head_sh)
  1025. goto again;
  1026. }
  1027. }
  1028. static struct dma_async_tx_descriptor *
  1029. async_copy_data(int frombio, struct bio *bio, struct page **page,
  1030. sector_t sector, struct dma_async_tx_descriptor *tx,
  1031. struct stripe_head *sh, int no_skipcopy)
  1032. {
  1033. struct bio_vec bvl;
  1034. struct bvec_iter iter;
  1035. struct page *bio_page;
  1036. int page_offset;
  1037. struct async_submit_ctl submit;
  1038. enum async_tx_flags flags = 0;
  1039. if (bio->bi_iter.bi_sector >= sector)
  1040. page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
  1041. else
  1042. page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
  1043. if (frombio)
  1044. flags |= ASYNC_TX_FENCE;
  1045. init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
  1046. bio_for_each_segment(bvl, bio, iter) {
  1047. int len = bvl.bv_len;
  1048. int clen;
  1049. int b_offset = 0;
  1050. if (page_offset < 0) {
  1051. b_offset = -page_offset;
  1052. page_offset += b_offset;
  1053. len -= b_offset;
  1054. }
  1055. if (len > 0 && page_offset + len > STRIPE_SIZE)
  1056. clen = STRIPE_SIZE - page_offset;
  1057. else
  1058. clen = len;
  1059. if (clen > 0) {
  1060. b_offset += bvl.bv_offset;
  1061. bio_page = bvl.bv_page;
  1062. if (frombio) {
  1063. if (sh->raid_conf->skip_copy &&
  1064. b_offset == 0 && page_offset == 0 &&
  1065. clen == STRIPE_SIZE &&
  1066. !no_skipcopy)
  1067. *page = bio_page;
  1068. else
  1069. tx = async_memcpy(*page, bio_page, page_offset,
  1070. b_offset, clen, &submit);
  1071. } else
  1072. tx = async_memcpy(bio_page, *page, b_offset,
  1073. page_offset, clen, &submit);
  1074. }
  1075. /* chain the operations */
  1076. submit.depend_tx = tx;
  1077. if (clen < len) /* hit end of page */
  1078. break;
  1079. page_offset += len;
  1080. }
  1081. return tx;
  1082. }
  1083. static void ops_complete_biofill(void *stripe_head_ref)
  1084. {
  1085. struct stripe_head *sh = stripe_head_ref;
  1086. struct bio_list return_bi = BIO_EMPTY_LIST;
  1087. int i;
  1088. pr_debug("%s: stripe %llu\n", __func__,
  1089. (unsigned long long)sh->sector);
  1090. /* clear completed biofills */
  1091. for (i = sh->disks; i--; ) {
  1092. struct r5dev *dev = &sh->dev[i];
  1093. /* acknowledge completion of a biofill operation */
  1094. /* and check if we need to reply to a read request,
  1095. * new R5_Wantfill requests are held off until
  1096. * !STRIPE_BIOFILL_RUN
  1097. */
  1098. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  1099. struct bio *rbi, *rbi2;
  1100. BUG_ON(!dev->read);
  1101. rbi = dev->read;
  1102. dev->read = NULL;
  1103. while (rbi && rbi->bi_iter.bi_sector <
  1104. dev->sector + STRIPE_SECTORS) {
  1105. rbi2 = r5_next_bio(rbi, dev->sector);
  1106. if (!raid5_dec_bi_active_stripes(rbi))
  1107. bio_list_add(&return_bi, rbi);
  1108. rbi = rbi2;
  1109. }
  1110. }
  1111. }
  1112. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  1113. return_io(&return_bi);
  1114. set_bit(STRIPE_HANDLE, &sh->state);
  1115. raid5_release_stripe(sh);
  1116. }
  1117. static void ops_run_biofill(struct stripe_head *sh)
  1118. {
  1119. struct dma_async_tx_descriptor *tx = NULL;
  1120. struct async_submit_ctl submit;
  1121. int i;
  1122. BUG_ON(sh->batch_head);
  1123. pr_debug("%s: stripe %llu\n", __func__,
  1124. (unsigned long long)sh->sector);
  1125. for (i = sh->disks; i--; ) {
  1126. struct r5dev *dev = &sh->dev[i];
  1127. if (test_bit(R5_Wantfill, &dev->flags)) {
  1128. struct bio *rbi;
  1129. spin_lock_irq(&sh->stripe_lock);
  1130. dev->read = rbi = dev->toread;
  1131. dev->toread = NULL;
  1132. spin_unlock_irq(&sh->stripe_lock);
  1133. while (rbi && rbi->bi_iter.bi_sector <
  1134. dev->sector + STRIPE_SECTORS) {
  1135. tx = async_copy_data(0, rbi, &dev->page,
  1136. dev->sector, tx, sh, 0);
  1137. rbi = r5_next_bio(rbi, dev->sector);
  1138. }
  1139. }
  1140. }
  1141. atomic_inc(&sh->count);
  1142. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
  1143. async_trigger_callback(&submit);
  1144. }
  1145. static void mark_target_uptodate(struct stripe_head *sh, int target)
  1146. {
  1147. struct r5dev *tgt;
  1148. if (target < 0)
  1149. return;
  1150. tgt = &sh->dev[target];
  1151. set_bit(R5_UPTODATE, &tgt->flags);
  1152. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1153. clear_bit(R5_Wantcompute, &tgt->flags);
  1154. }
  1155. static void ops_complete_compute(void *stripe_head_ref)
  1156. {
  1157. struct stripe_head *sh = stripe_head_ref;
  1158. pr_debug("%s: stripe %llu\n", __func__,
  1159. (unsigned long long)sh->sector);
  1160. /* mark the computed target(s) as uptodate */
  1161. mark_target_uptodate(sh, sh->ops.target);
  1162. mark_target_uptodate(sh, sh->ops.target2);
  1163. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  1164. if (sh->check_state == check_state_compute_run)
  1165. sh->check_state = check_state_compute_result;
  1166. set_bit(STRIPE_HANDLE, &sh->state);
  1167. raid5_release_stripe(sh);
  1168. }
  1169. /* return a pointer to the address conversion region of the scribble buffer */
  1170. static addr_conv_t *to_addr_conv(struct stripe_head *sh,
  1171. struct raid5_percpu *percpu, int i)
  1172. {
  1173. void *addr;
  1174. addr = flex_array_get(percpu->scribble, i);
  1175. return addr + sizeof(struct page *) * (sh->disks + 2);
  1176. }
  1177. /* return a pointer to the address conversion region of the scribble buffer */
  1178. static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
  1179. {
  1180. void *addr;
  1181. addr = flex_array_get(percpu->scribble, i);
  1182. return addr;
  1183. }
  1184. static struct dma_async_tx_descriptor *
  1185. ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
  1186. {
  1187. int disks = sh->disks;
  1188. struct page **xor_srcs = to_addr_page(percpu, 0);
  1189. int target = sh->ops.target;
  1190. struct r5dev *tgt = &sh->dev[target];
  1191. struct page *xor_dest = tgt->page;
  1192. int count = 0;
  1193. struct dma_async_tx_descriptor *tx;
  1194. struct async_submit_ctl submit;
  1195. int i;
  1196. BUG_ON(sh->batch_head);
  1197. pr_debug("%s: stripe %llu block: %d\n",
  1198. __func__, (unsigned long long)sh->sector, target);
  1199. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1200. for (i = disks; i--; )
  1201. if (i != target)
  1202. xor_srcs[count++] = sh->dev[i].page;
  1203. atomic_inc(&sh->count);
  1204. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
  1205. ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
  1206. if (unlikely(count == 1))
  1207. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  1208. else
  1209. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1210. return tx;
  1211. }
  1212. /* set_syndrome_sources - populate source buffers for gen_syndrome
  1213. * @srcs - (struct page *) array of size sh->disks
  1214. * @sh - stripe_head to parse
  1215. *
  1216. * Populates srcs in proper layout order for the stripe and returns the
  1217. * 'count' of sources to be used in a call to async_gen_syndrome. The P
  1218. * destination buffer is recorded in srcs[count] and the Q destination
  1219. * is recorded in srcs[count+1]].
  1220. */
  1221. static int set_syndrome_sources(struct page **srcs,
  1222. struct stripe_head *sh,
  1223. int srctype)
  1224. {
  1225. int disks = sh->disks;
  1226. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  1227. int d0_idx = raid6_d0(sh);
  1228. int count;
  1229. int i;
  1230. for (i = 0; i < disks; i++)
  1231. srcs[i] = NULL;
  1232. count = 0;
  1233. i = d0_idx;
  1234. do {
  1235. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1236. struct r5dev *dev = &sh->dev[i];
  1237. if (i == sh->qd_idx || i == sh->pd_idx ||
  1238. (srctype == SYNDROME_SRC_ALL) ||
  1239. (srctype == SYNDROME_SRC_WANT_DRAIN &&
  1240. (test_bit(R5_Wantdrain, &dev->flags) ||
  1241. test_bit(R5_InJournal, &dev->flags))) ||
  1242. (srctype == SYNDROME_SRC_WRITTEN &&
  1243. (dev->written ||
  1244. test_bit(R5_InJournal, &dev->flags)))) {
  1245. if (test_bit(R5_InJournal, &dev->flags))
  1246. srcs[slot] = sh->dev[i].orig_page;
  1247. else
  1248. srcs[slot] = sh->dev[i].page;
  1249. }
  1250. i = raid6_next_disk(i, disks);
  1251. } while (i != d0_idx);
  1252. return syndrome_disks;
  1253. }
  1254. static struct dma_async_tx_descriptor *
  1255. ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
  1256. {
  1257. int disks = sh->disks;
  1258. struct page **blocks = to_addr_page(percpu, 0);
  1259. int target;
  1260. int qd_idx = sh->qd_idx;
  1261. struct dma_async_tx_descriptor *tx;
  1262. struct async_submit_ctl submit;
  1263. struct r5dev *tgt;
  1264. struct page *dest;
  1265. int i;
  1266. int count;
  1267. BUG_ON(sh->batch_head);
  1268. if (sh->ops.target < 0)
  1269. target = sh->ops.target2;
  1270. else if (sh->ops.target2 < 0)
  1271. target = sh->ops.target;
  1272. else
  1273. /* we should only have one valid target */
  1274. BUG();
  1275. BUG_ON(target < 0);
  1276. pr_debug("%s: stripe %llu block: %d\n",
  1277. __func__, (unsigned long long)sh->sector, target);
  1278. tgt = &sh->dev[target];
  1279. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1280. dest = tgt->page;
  1281. atomic_inc(&sh->count);
  1282. if (target == qd_idx) {
  1283. count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
  1284. blocks[count] = NULL; /* regenerating p is not necessary */
  1285. BUG_ON(blocks[count+1] != dest); /* q should already be set */
  1286. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1287. ops_complete_compute, sh,
  1288. to_addr_conv(sh, percpu, 0));
  1289. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  1290. } else {
  1291. /* Compute any data- or p-drive using XOR */
  1292. count = 0;
  1293. for (i = disks; i-- ; ) {
  1294. if (i == target || i == qd_idx)
  1295. continue;
  1296. blocks[count++] = sh->dev[i].page;
  1297. }
  1298. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  1299. NULL, ops_complete_compute, sh,
  1300. to_addr_conv(sh, percpu, 0));
  1301. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
  1302. }
  1303. return tx;
  1304. }
  1305. static struct dma_async_tx_descriptor *
  1306. ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
  1307. {
  1308. int i, count, disks = sh->disks;
  1309. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  1310. int d0_idx = raid6_d0(sh);
  1311. int faila = -1, failb = -1;
  1312. int target = sh->ops.target;
  1313. int target2 = sh->ops.target2;
  1314. struct r5dev *tgt = &sh->dev[target];
  1315. struct r5dev *tgt2 = &sh->dev[target2];
  1316. struct dma_async_tx_descriptor *tx;
  1317. struct page **blocks = to_addr_page(percpu, 0);
  1318. struct async_submit_ctl submit;
  1319. BUG_ON(sh->batch_head);
  1320. pr_debug("%s: stripe %llu block1: %d block2: %d\n",
  1321. __func__, (unsigned long long)sh->sector, target, target2);
  1322. BUG_ON(target < 0 || target2 < 0);
  1323. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  1324. BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
  1325. /* we need to open-code set_syndrome_sources to handle the
  1326. * slot number conversion for 'faila' and 'failb'
  1327. */
  1328. for (i = 0; i < disks ; i++)
  1329. blocks[i] = NULL;
  1330. count = 0;
  1331. i = d0_idx;
  1332. do {
  1333. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1334. blocks[slot] = sh->dev[i].page;
  1335. if (i == target)
  1336. faila = slot;
  1337. if (i == target2)
  1338. failb = slot;
  1339. i = raid6_next_disk(i, disks);
  1340. } while (i != d0_idx);
  1341. BUG_ON(faila == failb);
  1342. if (failb < faila)
  1343. swap(faila, failb);
  1344. pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
  1345. __func__, (unsigned long long)sh->sector, faila, failb);
  1346. atomic_inc(&sh->count);
  1347. if (failb == syndrome_disks+1) {
  1348. /* Q disk is one of the missing disks */
  1349. if (faila == syndrome_disks) {
  1350. /* Missing P+Q, just recompute */
  1351. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1352. ops_complete_compute, sh,
  1353. to_addr_conv(sh, percpu, 0));
  1354. return async_gen_syndrome(blocks, 0, syndrome_disks+2,
  1355. STRIPE_SIZE, &submit);
  1356. } else {
  1357. struct page *dest;
  1358. int data_target;
  1359. int qd_idx = sh->qd_idx;
  1360. /* Missing D+Q: recompute D from P, then recompute Q */
  1361. if (target == qd_idx)
  1362. data_target = target2;
  1363. else
  1364. data_target = target;
  1365. count = 0;
  1366. for (i = disks; i-- ; ) {
  1367. if (i == data_target || i == qd_idx)
  1368. continue;
  1369. blocks[count++] = sh->dev[i].page;
  1370. }
  1371. dest = sh->dev[data_target].page;
  1372. init_async_submit(&submit,
  1373. ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  1374. NULL, NULL, NULL,
  1375. to_addr_conv(sh, percpu, 0));
  1376. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
  1377. &submit);
  1378. count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
  1379. init_async_submit(&submit, ASYNC_TX_FENCE, tx,
  1380. ops_complete_compute, sh,
  1381. to_addr_conv(sh, percpu, 0));
  1382. return async_gen_syndrome(blocks, 0, count+2,
  1383. STRIPE_SIZE, &submit);
  1384. }
  1385. } else {
  1386. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  1387. ops_complete_compute, sh,
  1388. to_addr_conv(sh, percpu, 0));
  1389. if (failb == syndrome_disks) {
  1390. /* We're missing D+P. */
  1391. return async_raid6_datap_recov(syndrome_disks+2,
  1392. STRIPE_SIZE, faila,
  1393. blocks, &submit);
  1394. } else {
  1395. /* We're missing D+D. */
  1396. return async_raid6_2data_recov(syndrome_disks+2,
  1397. STRIPE_SIZE, faila, failb,
  1398. blocks, &submit);
  1399. }
  1400. }
  1401. }
  1402. static void ops_complete_prexor(void *stripe_head_ref)
  1403. {
  1404. struct stripe_head *sh = stripe_head_ref;
  1405. pr_debug("%s: stripe %llu\n", __func__,
  1406. (unsigned long long)sh->sector);
  1407. if (r5c_is_writeback(sh->raid_conf->log))
  1408. /*
  1409. * raid5-cache write back uses orig_page during prexor.
  1410. * After prexor, it is time to free orig_page
  1411. */
  1412. r5c_release_extra_page(sh);
  1413. }
  1414. static struct dma_async_tx_descriptor *
  1415. ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
  1416. struct dma_async_tx_descriptor *tx)
  1417. {
  1418. int disks = sh->disks;
  1419. struct page **xor_srcs = to_addr_page(percpu, 0);
  1420. int count = 0, pd_idx = sh->pd_idx, i;
  1421. struct async_submit_ctl submit;
  1422. /* existing parity data subtracted */
  1423. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  1424. BUG_ON(sh->batch_head);
  1425. pr_debug("%s: stripe %llu\n", __func__,
  1426. (unsigned long long)sh->sector);
  1427. for (i = disks; i--; ) {
  1428. struct r5dev *dev = &sh->dev[i];
  1429. /* Only process blocks that are known to be uptodate */
  1430. if (test_bit(R5_InJournal, &dev->flags))
  1431. xor_srcs[count++] = dev->orig_page;
  1432. else if (test_bit(R5_Wantdrain, &dev->flags))
  1433. xor_srcs[count++] = dev->page;
  1434. }
  1435. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  1436. ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
  1437. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1438. return tx;
  1439. }
  1440. static struct dma_async_tx_descriptor *
  1441. ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
  1442. struct dma_async_tx_descriptor *tx)
  1443. {
  1444. struct page **blocks = to_addr_page(percpu, 0);
  1445. int count;
  1446. struct async_submit_ctl submit;
  1447. pr_debug("%s: stripe %llu\n", __func__,
  1448. (unsigned long long)sh->sector);
  1449. count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
  1450. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
  1451. ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
  1452. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  1453. return tx;
  1454. }
  1455. static struct dma_async_tx_descriptor *
  1456. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  1457. {
  1458. struct r5conf *conf = sh->raid_conf;
  1459. int disks = sh->disks;
  1460. int i;
  1461. struct stripe_head *head_sh = sh;
  1462. pr_debug("%s: stripe %llu\n", __func__,
  1463. (unsigned long long)sh->sector);
  1464. for (i = disks; i--; ) {
  1465. struct r5dev *dev;
  1466. struct bio *chosen;
  1467. sh = head_sh;
  1468. if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
  1469. struct bio *wbi;
  1470. again:
  1471. dev = &sh->dev[i];
  1472. /*
  1473. * clear R5_InJournal, so when rewriting a page in
  1474. * journal, it is not skipped by r5l_log_stripe()
  1475. */
  1476. clear_bit(R5_InJournal, &dev->flags);
  1477. spin_lock_irq(&sh->stripe_lock);
  1478. chosen = dev->towrite;
  1479. dev->towrite = NULL;
  1480. sh->overwrite_disks = 0;
  1481. BUG_ON(dev->written);
  1482. wbi = dev->written = chosen;
  1483. spin_unlock_irq(&sh->stripe_lock);
  1484. WARN_ON(dev->page != dev->orig_page);
  1485. while (wbi && wbi->bi_iter.bi_sector <
  1486. dev->sector + STRIPE_SECTORS) {
  1487. if (wbi->bi_opf & REQ_FUA)
  1488. set_bit(R5_WantFUA, &dev->flags);
  1489. if (wbi->bi_opf & REQ_SYNC)
  1490. set_bit(R5_SyncIO, &dev->flags);
  1491. if (bio_op(wbi) == REQ_OP_DISCARD)
  1492. set_bit(R5_Discard, &dev->flags);
  1493. else {
  1494. tx = async_copy_data(1, wbi, &dev->page,
  1495. dev->sector, tx, sh,
  1496. r5c_is_writeback(conf->log));
  1497. if (dev->page != dev->orig_page &&
  1498. !r5c_is_writeback(conf->log)) {
  1499. set_bit(R5_SkipCopy, &dev->flags);
  1500. clear_bit(R5_UPTODATE, &dev->flags);
  1501. clear_bit(R5_OVERWRITE, &dev->flags);
  1502. }
  1503. }
  1504. wbi = r5_next_bio(wbi, dev->sector);
  1505. }
  1506. if (head_sh->batch_head) {
  1507. sh = list_first_entry(&sh->batch_list,
  1508. struct stripe_head,
  1509. batch_list);
  1510. if (sh == head_sh)
  1511. continue;
  1512. goto again;
  1513. }
  1514. }
  1515. }
  1516. return tx;
  1517. }
  1518. static void ops_complete_reconstruct(void *stripe_head_ref)
  1519. {
  1520. struct stripe_head *sh = stripe_head_ref;
  1521. int disks = sh->disks;
  1522. int pd_idx = sh->pd_idx;
  1523. int qd_idx = sh->qd_idx;
  1524. int i;
  1525. bool fua = false, sync = false, discard = false;
  1526. pr_debug("%s: stripe %llu\n", __func__,
  1527. (unsigned long long)sh->sector);
  1528. for (i = disks; i--; ) {
  1529. fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
  1530. sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
  1531. discard |= test_bit(R5_Discard, &sh->dev[i].flags);
  1532. }
  1533. for (i = disks; i--; ) {
  1534. struct r5dev *dev = &sh->dev[i];
  1535. if (dev->written || i == pd_idx || i == qd_idx) {
  1536. if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
  1537. set_bit(R5_UPTODATE, &dev->flags);
  1538. if (fua)
  1539. set_bit(R5_WantFUA, &dev->flags);
  1540. if (sync)
  1541. set_bit(R5_SyncIO, &dev->flags);
  1542. }
  1543. }
  1544. if (sh->reconstruct_state == reconstruct_state_drain_run)
  1545. sh->reconstruct_state = reconstruct_state_drain_result;
  1546. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  1547. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  1548. else {
  1549. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  1550. sh->reconstruct_state = reconstruct_state_result;
  1551. }
  1552. set_bit(STRIPE_HANDLE, &sh->state);
  1553. raid5_release_stripe(sh);
  1554. }
  1555. static void
  1556. ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
  1557. struct dma_async_tx_descriptor *tx)
  1558. {
  1559. int disks = sh->disks;
  1560. struct page **xor_srcs;
  1561. struct async_submit_ctl submit;
  1562. int count, pd_idx = sh->pd_idx, i;
  1563. struct page *xor_dest;
  1564. int prexor = 0;
  1565. unsigned long flags;
  1566. int j = 0;
  1567. struct stripe_head *head_sh = sh;
  1568. int last_stripe;
  1569. pr_debug("%s: stripe %llu\n", __func__,
  1570. (unsigned long long)sh->sector);
  1571. for (i = 0; i < sh->disks; i++) {
  1572. if (pd_idx == i)
  1573. continue;
  1574. if (!test_bit(R5_Discard, &sh->dev[i].flags))
  1575. break;
  1576. }
  1577. if (i >= sh->disks) {
  1578. atomic_inc(&sh->count);
  1579. set_bit(R5_Discard, &sh->dev[pd_idx].flags);
  1580. ops_complete_reconstruct(sh);
  1581. return;
  1582. }
  1583. again:
  1584. count = 0;
  1585. xor_srcs = to_addr_page(percpu, j);
  1586. /* check if prexor is active which means only process blocks
  1587. * that are part of a read-modify-write (written)
  1588. */
  1589. if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  1590. prexor = 1;
  1591. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  1592. for (i = disks; i--; ) {
  1593. struct r5dev *dev = &sh->dev[i];
  1594. if (head_sh->dev[i].written ||
  1595. test_bit(R5_InJournal, &head_sh->dev[i].flags))
  1596. xor_srcs[count++] = dev->page;
  1597. }
  1598. } else {
  1599. xor_dest = sh->dev[pd_idx].page;
  1600. for (i = disks; i--; ) {
  1601. struct r5dev *dev = &sh->dev[i];
  1602. if (i != pd_idx)
  1603. xor_srcs[count++] = dev->page;
  1604. }
  1605. }
  1606. /* 1/ if we prexor'd then the dest is reused as a source
  1607. * 2/ if we did not prexor then we are redoing the parity
  1608. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  1609. * for the synchronous xor case
  1610. */
  1611. last_stripe = !head_sh->batch_head ||
  1612. list_first_entry(&sh->batch_list,
  1613. struct stripe_head, batch_list) == head_sh;
  1614. if (last_stripe) {
  1615. flags = ASYNC_TX_ACK |
  1616. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  1617. atomic_inc(&head_sh->count);
  1618. init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
  1619. to_addr_conv(sh, percpu, j));
  1620. } else {
  1621. flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
  1622. init_async_submit(&submit, flags, tx, NULL, NULL,
  1623. to_addr_conv(sh, percpu, j));
  1624. }
  1625. if (unlikely(count == 1))
  1626. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  1627. else
  1628. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  1629. if (!last_stripe) {
  1630. j++;
  1631. sh = list_first_entry(&sh->batch_list, struct stripe_head,
  1632. batch_list);
  1633. goto again;
  1634. }
  1635. }
  1636. static void
  1637. ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
  1638. struct dma_async_tx_descriptor *tx)
  1639. {
  1640. struct async_submit_ctl submit;
  1641. struct page **blocks;
  1642. int count, i, j = 0;
  1643. struct stripe_head *head_sh = sh;
  1644. int last_stripe;
  1645. int synflags;
  1646. unsigned long txflags;
  1647. pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
  1648. for (i = 0; i < sh->disks; i++) {
  1649. if (sh->pd_idx == i || sh->qd_idx == i)
  1650. continue;
  1651. if (!test_bit(R5_Discard, &sh->dev[i].flags))
  1652. break;
  1653. }
  1654. if (i >= sh->disks) {
  1655. atomic_inc(&sh->count);
  1656. set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
  1657. set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
  1658. ops_complete_reconstruct(sh);
  1659. return;
  1660. }
  1661. again:
  1662. blocks = to_addr_page(percpu, j);
  1663. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  1664. synflags = SYNDROME_SRC_WRITTEN;
  1665. txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
  1666. } else {
  1667. synflags = SYNDROME_SRC_ALL;
  1668. txflags = ASYNC_TX_ACK;
  1669. }
  1670. count = set_syndrome_sources(blocks, sh, synflags);
  1671. last_stripe = !head_sh->batch_head ||
  1672. list_first_entry(&sh->batch_list,
  1673. struct stripe_head, batch_list) == head_sh;
  1674. if (last_stripe) {
  1675. atomic_inc(&head_sh->count);
  1676. init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
  1677. head_sh, to_addr_conv(sh, percpu, j));
  1678. } else
  1679. init_async_submit(&submit, 0, tx, NULL, NULL,
  1680. to_addr_conv(sh, percpu, j));
  1681. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  1682. if (!last_stripe) {
  1683. j++;
  1684. sh = list_first_entry(&sh->batch_list, struct stripe_head,
  1685. batch_list);
  1686. goto again;
  1687. }
  1688. }
  1689. static void ops_complete_check(void *stripe_head_ref)
  1690. {
  1691. struct stripe_head *sh = stripe_head_ref;
  1692. pr_debug("%s: stripe %llu\n", __func__,
  1693. (unsigned long long)sh->sector);
  1694. sh->check_state = check_state_check_result;
  1695. set_bit(STRIPE_HANDLE, &sh->state);
  1696. raid5_release_stripe(sh);
  1697. }
  1698. static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
  1699. {
  1700. int disks = sh->disks;
  1701. int pd_idx = sh->pd_idx;
  1702. int qd_idx = sh->qd_idx;
  1703. struct page *xor_dest;
  1704. struct page **xor_srcs = to_addr_page(percpu, 0);
  1705. struct dma_async_tx_descriptor *tx;
  1706. struct async_submit_ctl submit;
  1707. int count;
  1708. int i;
  1709. pr_debug("%s: stripe %llu\n", __func__,
  1710. (unsigned long long)sh->sector);
  1711. BUG_ON(sh->batch_head);
  1712. count = 0;
  1713. xor_dest = sh->dev[pd_idx].page;
  1714. xor_srcs[count++] = xor_dest;
  1715. for (i = disks; i--; ) {
  1716. if (i == pd_idx || i == qd_idx)
  1717. continue;
  1718. xor_srcs[count++] = sh->dev[i].page;
  1719. }
  1720. init_async_submit(&submit, 0, NULL, NULL, NULL,
  1721. to_addr_conv(sh, percpu, 0));
  1722. tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  1723. &sh->ops.zero_sum_result, &submit);
  1724. atomic_inc(&sh->count);
  1725. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
  1726. tx = async_trigger_callback(&submit);
  1727. }
  1728. static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
  1729. {
  1730. struct page **srcs = to_addr_page(percpu, 0);
  1731. struct async_submit_ctl submit;
  1732. int count;
  1733. pr_debug("%s: stripe %llu checkp: %d\n", __func__,
  1734. (unsigned long long)sh->sector, checkp);
  1735. BUG_ON(sh->batch_head);
  1736. count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
  1737. if (!checkp)
  1738. srcs[count] = NULL;
  1739. atomic_inc(&sh->count);
  1740. init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
  1741. sh, to_addr_conv(sh, percpu, 0));
  1742. async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
  1743. &sh->ops.zero_sum_result, percpu->spare_page, &submit);
  1744. }
  1745. static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1746. {
  1747. int overlap_clear = 0, i, disks = sh->disks;
  1748. struct dma_async_tx_descriptor *tx = NULL;
  1749. struct r5conf *conf = sh->raid_conf;
  1750. int level = conf->level;
  1751. struct raid5_percpu *percpu;
  1752. unsigned long cpu;
  1753. cpu = get_cpu();
  1754. percpu = per_cpu_ptr(conf->percpu, cpu);
  1755. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  1756. ops_run_biofill(sh);
  1757. overlap_clear++;
  1758. }
  1759. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  1760. if (level < 6)
  1761. tx = ops_run_compute5(sh, percpu);
  1762. else {
  1763. if (sh->ops.target2 < 0 || sh->ops.target < 0)
  1764. tx = ops_run_compute6_1(sh, percpu);
  1765. else
  1766. tx = ops_run_compute6_2(sh, percpu);
  1767. }
  1768. /* terminate the chain if reconstruct is not set to be run */
  1769. if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
  1770. async_tx_ack(tx);
  1771. }
  1772. if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
  1773. if (level < 6)
  1774. tx = ops_run_prexor5(sh, percpu, tx);
  1775. else
  1776. tx = ops_run_prexor6(sh, percpu, tx);
  1777. }
  1778. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  1779. tx = ops_run_biodrain(sh, tx);
  1780. overlap_clear++;
  1781. }
  1782. if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
  1783. if (level < 6)
  1784. ops_run_reconstruct5(sh, percpu, tx);
  1785. else
  1786. ops_run_reconstruct6(sh, percpu, tx);
  1787. }
  1788. if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
  1789. if (sh->check_state == check_state_run)
  1790. ops_run_check_p(sh, percpu);
  1791. else if (sh->check_state == check_state_run_q)
  1792. ops_run_check_pq(sh, percpu, 0);
  1793. else if (sh->check_state == check_state_run_pq)
  1794. ops_run_check_pq(sh, percpu, 1);
  1795. else
  1796. BUG();
  1797. }
  1798. if (overlap_clear && !sh->batch_head)
  1799. for (i = disks; i--; ) {
  1800. struct r5dev *dev = &sh->dev[i];
  1801. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1802. wake_up(&sh->raid_conf->wait_for_overlap);
  1803. }
  1804. put_cpu();
  1805. }
  1806. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
  1807. int disks)
  1808. {
  1809. struct stripe_head *sh;
  1810. int i;
  1811. sh = kmem_cache_zalloc(sc, gfp);
  1812. if (sh) {
  1813. spin_lock_init(&sh->stripe_lock);
  1814. spin_lock_init(&sh->batch_lock);
  1815. INIT_LIST_HEAD(&sh->batch_list);
  1816. INIT_LIST_HEAD(&sh->lru);
  1817. INIT_LIST_HEAD(&sh->r5c);
  1818. INIT_LIST_HEAD(&sh->log_list);
  1819. atomic_set(&sh->count, 1);
  1820. sh->log_start = MaxSector;
  1821. for (i = 0; i < disks; i++) {
  1822. struct r5dev *dev = &sh->dev[i];
  1823. bio_init(&dev->req, &dev->vec, 1);
  1824. bio_init(&dev->rreq, &dev->rvec, 1);
  1825. }
  1826. }
  1827. return sh;
  1828. }
  1829. static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
  1830. {
  1831. struct stripe_head *sh;
  1832. sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
  1833. if (!sh)
  1834. return 0;
  1835. sh->raid_conf = conf;
  1836. if (grow_buffers(sh, gfp)) {
  1837. shrink_buffers(sh);
  1838. kmem_cache_free(conf->slab_cache, sh);
  1839. return 0;
  1840. }
  1841. sh->hash_lock_index =
  1842. conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
  1843. /* we just created an active stripe so... */
  1844. atomic_inc(&conf->active_stripes);
  1845. raid5_release_stripe(sh);
  1846. conf->max_nr_stripes++;
  1847. return 1;
  1848. }
  1849. static int grow_stripes(struct r5conf *conf, int num)
  1850. {
  1851. struct kmem_cache *sc;
  1852. int devs = max(conf->raid_disks, conf->previous_raid_disks);
  1853. if (conf->mddev->gendisk)
  1854. sprintf(conf->cache_name[0],
  1855. "raid%d-%s", conf->level, mdname(conf->mddev));
  1856. else
  1857. sprintf(conf->cache_name[0],
  1858. "raid%d-%p", conf->level, conf->mddev);
  1859. sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
  1860. conf->active_name = 0;
  1861. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  1862. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  1863. 0, 0, NULL);
  1864. if (!sc)
  1865. return 1;
  1866. conf->slab_cache = sc;
  1867. conf->pool_size = devs;
  1868. while (num--)
  1869. if (!grow_one_stripe(conf, GFP_KERNEL))
  1870. return 1;
  1871. return 0;
  1872. }
  1873. /**
  1874. * scribble_len - return the required size of the scribble region
  1875. * @num - total number of disks in the array
  1876. *
  1877. * The size must be enough to contain:
  1878. * 1/ a struct page pointer for each device in the array +2
  1879. * 2/ room to convert each entry in (1) to its corresponding dma
  1880. * (dma_map_page()) or page (page_address()) address.
  1881. *
  1882. * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
  1883. * calculate over all devices (not just the data blocks), using zeros in place
  1884. * of the P and Q blocks.
  1885. */
  1886. static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
  1887. {
  1888. struct flex_array *ret;
  1889. size_t len;
  1890. len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
  1891. ret = flex_array_alloc(len, cnt, flags);
  1892. if (!ret)
  1893. return NULL;
  1894. /* always prealloc all elements, so no locking is required */
  1895. if (flex_array_prealloc(ret, 0, cnt, flags)) {
  1896. flex_array_free(ret);
  1897. return NULL;
  1898. }
  1899. return ret;
  1900. }
  1901. static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
  1902. {
  1903. unsigned long cpu;
  1904. int err = 0;
  1905. /*
  1906. * Never shrink. And mddev_suspend() could deadlock if this is called
  1907. * from raid5d. In that case, scribble_disks and scribble_sectors
  1908. * should equal to new_disks and new_sectors
  1909. */
  1910. if (conf->scribble_disks >= new_disks &&
  1911. conf->scribble_sectors >= new_sectors)
  1912. return 0;
  1913. mddev_suspend(conf->mddev);
  1914. get_online_cpus();
  1915. for_each_present_cpu(cpu) {
  1916. struct raid5_percpu *percpu;
  1917. struct flex_array *scribble;
  1918. percpu = per_cpu_ptr(conf->percpu, cpu);
  1919. scribble = scribble_alloc(new_disks,
  1920. new_sectors / STRIPE_SECTORS,
  1921. GFP_NOIO);
  1922. if (scribble) {
  1923. flex_array_free(percpu->scribble);
  1924. percpu->scribble = scribble;
  1925. } else {
  1926. err = -ENOMEM;
  1927. break;
  1928. }
  1929. }
  1930. put_online_cpus();
  1931. mddev_resume(conf->mddev);
  1932. if (!err) {
  1933. conf->scribble_disks = new_disks;
  1934. conf->scribble_sectors = new_sectors;
  1935. }
  1936. return err;
  1937. }
  1938. static int resize_stripes(struct r5conf *conf, int newsize)
  1939. {
  1940. /* Make all the stripes able to hold 'newsize' devices.
  1941. * New slots in each stripe get 'page' set to a new page.
  1942. *
  1943. * This happens in stages:
  1944. * 1/ create a new kmem_cache and allocate the required number of
  1945. * stripe_heads.
  1946. * 2/ gather all the old stripe_heads and transfer the pages across
  1947. * to the new stripe_heads. This will have the side effect of
  1948. * freezing the array as once all stripe_heads have been collected,
  1949. * no IO will be possible. Old stripe heads are freed once their
  1950. * pages have been transferred over, and the old kmem_cache is
  1951. * freed when all stripes are done.
  1952. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  1953. * we simple return a failre status - no need to clean anything up.
  1954. * 4/ allocate new pages for the new slots in the new stripe_heads.
  1955. * If this fails, we don't bother trying the shrink the
  1956. * stripe_heads down again, we just leave them as they are.
  1957. * As each stripe_head is processed the new one is released into
  1958. * active service.
  1959. *
  1960. * Once step2 is started, we cannot afford to wait for a write,
  1961. * so we use GFP_NOIO allocations.
  1962. */
  1963. struct stripe_head *osh, *nsh;
  1964. LIST_HEAD(newstripes);
  1965. struct disk_info *ndisks;
  1966. int err;
  1967. struct kmem_cache *sc;
  1968. int i;
  1969. int hash, cnt;
  1970. if (newsize <= conf->pool_size)
  1971. return 0; /* never bother to shrink */
  1972. err = md_allow_write(conf->mddev);
  1973. if (err)
  1974. return err;
  1975. /* Step 1 */
  1976. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  1977. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  1978. 0, 0, NULL);
  1979. if (!sc)
  1980. return -ENOMEM;
  1981. /* Need to ensure auto-resizing doesn't interfere */
  1982. mutex_lock(&conf->cache_size_mutex);
  1983. for (i = conf->max_nr_stripes; i; i--) {
  1984. nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
  1985. if (!nsh)
  1986. break;
  1987. nsh->raid_conf = conf;
  1988. list_add(&nsh->lru, &newstripes);
  1989. }
  1990. if (i) {
  1991. /* didn't get enough, give up */
  1992. while (!list_empty(&newstripes)) {
  1993. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1994. list_del(&nsh->lru);
  1995. kmem_cache_free(sc, nsh);
  1996. }
  1997. kmem_cache_destroy(sc);
  1998. mutex_unlock(&conf->cache_size_mutex);
  1999. return -ENOMEM;
  2000. }
  2001. /* Step 2 - Must use GFP_NOIO now.
  2002. * OK, we have enough stripes, start collecting inactive
  2003. * stripes and copying them over
  2004. */
  2005. hash = 0;
  2006. cnt = 0;
  2007. list_for_each_entry(nsh, &newstripes, lru) {
  2008. lock_device_hash_lock(conf, hash);
  2009. wait_event_cmd(conf->wait_for_stripe,
  2010. !list_empty(conf->inactive_list + hash),
  2011. unlock_device_hash_lock(conf, hash),
  2012. lock_device_hash_lock(conf, hash));
  2013. osh = get_free_stripe(conf, hash);
  2014. unlock_device_hash_lock(conf, hash);
  2015. for(i=0; i<conf->pool_size; i++) {
  2016. nsh->dev[i].page = osh->dev[i].page;
  2017. nsh->dev[i].orig_page = osh->dev[i].page;
  2018. }
  2019. nsh->hash_lock_index = hash;
  2020. kmem_cache_free(conf->slab_cache, osh);
  2021. cnt++;
  2022. if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
  2023. !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
  2024. hash++;
  2025. cnt = 0;
  2026. }
  2027. }
  2028. kmem_cache_destroy(conf->slab_cache);
  2029. /* Step 3.
  2030. * At this point, we are holding all the stripes so the array
  2031. * is completely stalled, so now is a good time to resize
  2032. * conf->disks and the scribble region
  2033. */
  2034. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  2035. if (ndisks) {
  2036. for (i = 0; i < conf->pool_size; i++)
  2037. ndisks[i] = conf->disks[i];
  2038. for (i = conf->pool_size; i < newsize; i++) {
  2039. ndisks[i].extra_page = alloc_page(GFP_NOIO);
  2040. if (!ndisks[i].extra_page)
  2041. err = -ENOMEM;
  2042. }
  2043. if (err) {
  2044. for (i = conf->pool_size; i < newsize; i++)
  2045. if (ndisks[i].extra_page)
  2046. put_page(ndisks[i].extra_page);
  2047. kfree(ndisks);
  2048. } else {
  2049. kfree(conf->disks);
  2050. conf->disks = ndisks;
  2051. }
  2052. } else
  2053. err = -ENOMEM;
  2054. mutex_unlock(&conf->cache_size_mutex);
  2055. /* Step 4, return new stripes to service */
  2056. while(!list_empty(&newstripes)) {
  2057. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  2058. list_del_init(&nsh->lru);
  2059. for (i=conf->raid_disks; i < newsize; i++)
  2060. if (nsh->dev[i].page == NULL) {
  2061. struct page *p = alloc_page(GFP_NOIO);
  2062. nsh->dev[i].page = p;
  2063. nsh->dev[i].orig_page = p;
  2064. if (!p)
  2065. err = -ENOMEM;
  2066. }
  2067. raid5_release_stripe(nsh);
  2068. }
  2069. /* critical section pass, GFP_NOIO no longer needed */
  2070. conf->slab_cache = sc;
  2071. conf->active_name = 1-conf->active_name;
  2072. if (!err)
  2073. conf->pool_size = newsize;
  2074. return err;
  2075. }
  2076. static int drop_one_stripe(struct r5conf *conf)
  2077. {
  2078. struct stripe_head *sh;
  2079. int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
  2080. spin_lock_irq(conf->hash_locks + hash);
  2081. sh = get_free_stripe(conf, hash);
  2082. spin_unlock_irq(conf->hash_locks + hash);
  2083. if (!sh)
  2084. return 0;
  2085. BUG_ON(atomic_read(&sh->count));
  2086. shrink_buffers(sh);
  2087. kmem_cache_free(conf->slab_cache, sh);
  2088. atomic_dec(&conf->active_stripes);
  2089. conf->max_nr_stripes--;
  2090. return 1;
  2091. }
  2092. static void shrink_stripes(struct r5conf *conf)
  2093. {
  2094. while (conf->max_nr_stripes &&
  2095. drop_one_stripe(conf))
  2096. ;
  2097. kmem_cache_destroy(conf->slab_cache);
  2098. conf->slab_cache = NULL;
  2099. }
  2100. static void raid5_end_read_request(struct bio * bi)
  2101. {
  2102. struct stripe_head *sh = bi->bi_private;
  2103. struct r5conf *conf = sh->raid_conf;
  2104. int disks = sh->disks, i;
  2105. char b[BDEVNAME_SIZE];
  2106. struct md_rdev *rdev = NULL;
  2107. sector_t s;
  2108. for (i=0 ; i<disks; i++)
  2109. if (bi == &sh->dev[i].req)
  2110. break;
  2111. pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
  2112. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  2113. bi->bi_error);
  2114. if (i == disks) {
  2115. bio_reset(bi);
  2116. BUG();
  2117. return;
  2118. }
  2119. if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
  2120. /* If replacement finished while this request was outstanding,
  2121. * 'replacement' might be NULL already.
  2122. * In that case it moved down to 'rdev'.
  2123. * rdev is not removed until all requests are finished.
  2124. */
  2125. rdev = conf->disks[i].replacement;
  2126. if (!rdev)
  2127. rdev = conf->disks[i].rdev;
  2128. if (use_new_offset(conf, sh))
  2129. s = sh->sector + rdev->new_data_offset;
  2130. else
  2131. s = sh->sector + rdev->data_offset;
  2132. if (!bi->bi_error) {
  2133. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  2134. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  2135. /* Note that this cannot happen on a
  2136. * replacement device. We just fail those on
  2137. * any error
  2138. */
  2139. pr_info_ratelimited(
  2140. "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
  2141. mdname(conf->mddev), STRIPE_SECTORS,
  2142. (unsigned long long)s,
  2143. bdevname(rdev->bdev, b));
  2144. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2145. clear_bit(R5_ReadError, &sh->dev[i].flags);
  2146. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  2147. } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
  2148. clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  2149. if (test_bit(R5_InJournal, &sh->dev[i].flags))
  2150. /*
  2151. * end read for a page in journal, this
  2152. * must be preparing for prexor in rmw
  2153. */
  2154. set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
  2155. if (atomic_read(&rdev->read_errors))
  2156. atomic_set(&rdev->read_errors, 0);
  2157. } else {
  2158. const char *bdn = bdevname(rdev->bdev, b);
  2159. int retry = 0;
  2160. int set_bad = 0;
  2161. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  2162. atomic_inc(&rdev->read_errors);
  2163. if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
  2164. pr_warn_ratelimited(
  2165. "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
  2166. mdname(conf->mddev),
  2167. (unsigned long long)s,
  2168. bdn);
  2169. else if (conf->mddev->degraded >= conf->max_degraded) {
  2170. set_bad = 1;
  2171. pr_warn_ratelimited(
  2172. "md/raid:%s: read error not correctable (sector %llu on %s).\n",
  2173. mdname(conf->mddev),
  2174. (unsigned long long)s,
  2175. bdn);
  2176. } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
  2177. /* Oh, no!!! */
  2178. set_bad = 1;
  2179. pr_warn_ratelimited(
  2180. "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
  2181. mdname(conf->mddev),
  2182. (unsigned long long)s,
  2183. bdn);
  2184. } else if (atomic_read(&rdev->read_errors)
  2185. > conf->max_nr_stripes)
  2186. pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
  2187. mdname(conf->mddev), bdn);
  2188. else
  2189. retry = 1;
  2190. if (set_bad && test_bit(In_sync, &rdev->flags)
  2191. && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
  2192. retry = 1;
  2193. if (retry)
  2194. if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
  2195. set_bit(R5_ReadError, &sh->dev[i].flags);
  2196. clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  2197. } else
  2198. set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
  2199. else {
  2200. clear_bit(R5_ReadError, &sh->dev[i].flags);
  2201. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  2202. if (!(set_bad
  2203. && test_bit(In_sync, &rdev->flags)
  2204. && rdev_set_badblocks(
  2205. rdev, sh->sector, STRIPE_SECTORS, 0)))
  2206. md_error(conf->mddev, rdev);
  2207. }
  2208. }
  2209. rdev_dec_pending(rdev, conf->mddev);
  2210. bio_reset(bi);
  2211. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2212. set_bit(STRIPE_HANDLE, &sh->state);
  2213. raid5_release_stripe(sh);
  2214. }
  2215. static void raid5_end_write_request(struct bio *bi)
  2216. {
  2217. struct stripe_head *sh = bi->bi_private;
  2218. struct r5conf *conf = sh->raid_conf;
  2219. int disks = sh->disks, i;
  2220. struct md_rdev *uninitialized_var(rdev);
  2221. sector_t first_bad;
  2222. int bad_sectors;
  2223. int replacement = 0;
  2224. for (i = 0 ; i < disks; i++) {
  2225. if (bi == &sh->dev[i].req) {
  2226. rdev = conf->disks[i].rdev;
  2227. break;
  2228. }
  2229. if (bi == &sh->dev[i].rreq) {
  2230. rdev = conf->disks[i].replacement;
  2231. if (rdev)
  2232. replacement = 1;
  2233. else
  2234. /* rdev was removed and 'replacement'
  2235. * replaced it. rdev is not removed
  2236. * until all requests are finished.
  2237. */
  2238. rdev = conf->disks[i].rdev;
  2239. break;
  2240. }
  2241. }
  2242. pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
  2243. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  2244. bi->bi_error);
  2245. if (i == disks) {
  2246. bio_reset(bi);
  2247. BUG();
  2248. return;
  2249. }
  2250. if (replacement) {
  2251. if (bi->bi_error)
  2252. md_error(conf->mddev, rdev);
  2253. else if (is_badblock(rdev, sh->sector,
  2254. STRIPE_SECTORS,
  2255. &first_bad, &bad_sectors))
  2256. set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
  2257. } else {
  2258. if (bi->bi_error) {
  2259. set_bit(STRIPE_DEGRADED, &sh->state);
  2260. set_bit(WriteErrorSeen, &rdev->flags);
  2261. set_bit(R5_WriteError, &sh->dev[i].flags);
  2262. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  2263. set_bit(MD_RECOVERY_NEEDED,
  2264. &rdev->mddev->recovery);
  2265. } else if (is_badblock(rdev, sh->sector,
  2266. STRIPE_SECTORS,
  2267. &first_bad, &bad_sectors)) {
  2268. set_bit(R5_MadeGood, &sh->dev[i].flags);
  2269. if (test_bit(R5_ReadError, &sh->dev[i].flags))
  2270. /* That was a successful write so make
  2271. * sure it looks like we already did
  2272. * a re-write.
  2273. */
  2274. set_bit(R5_ReWrite, &sh->dev[i].flags);
  2275. }
  2276. }
  2277. rdev_dec_pending(rdev, conf->mddev);
  2278. if (sh->batch_head && bi->bi_error && !replacement)
  2279. set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
  2280. bio_reset(bi);
  2281. if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
  2282. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2283. set_bit(STRIPE_HANDLE, &sh->state);
  2284. raid5_release_stripe(sh);
  2285. if (sh->batch_head && sh != sh->batch_head)
  2286. raid5_release_stripe(sh->batch_head);
  2287. }
  2288. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  2289. {
  2290. struct r5dev *dev = &sh->dev[i];
  2291. dev->flags = 0;
  2292. dev->sector = raid5_compute_blocknr(sh, i, previous);
  2293. }
  2294. static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
  2295. {
  2296. char b[BDEVNAME_SIZE];
  2297. struct r5conf *conf = mddev->private;
  2298. unsigned long flags;
  2299. pr_debug("raid456: error called\n");
  2300. spin_lock_irqsave(&conf->device_lock, flags);
  2301. clear_bit(In_sync, &rdev->flags);
  2302. mddev->degraded = raid5_calc_degraded(conf);
  2303. spin_unlock_irqrestore(&conf->device_lock, flags);
  2304. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  2305. set_bit(Blocked, &rdev->flags);
  2306. set_bit(Faulty, &rdev->flags);
  2307. set_mask_bits(&mddev->sb_flags, 0,
  2308. BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
  2309. pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
  2310. "md/raid:%s: Operation continuing on %d devices.\n",
  2311. mdname(mddev),
  2312. bdevname(rdev->bdev, b),
  2313. mdname(mddev),
  2314. conf->raid_disks - mddev->degraded);
  2315. r5c_update_on_rdev_error(mddev);
  2316. }
  2317. /*
  2318. * Input: a 'big' sector number,
  2319. * Output: index of the data and parity disk, and the sector # in them.
  2320. */
  2321. sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
  2322. int previous, int *dd_idx,
  2323. struct stripe_head *sh)
  2324. {
  2325. sector_t stripe, stripe2;
  2326. sector_t chunk_number;
  2327. unsigned int chunk_offset;
  2328. int pd_idx, qd_idx;
  2329. int ddf_layout = 0;
  2330. sector_t new_sector;
  2331. int algorithm = previous ? conf->prev_algo
  2332. : conf->algorithm;
  2333. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  2334. : conf->chunk_sectors;
  2335. int raid_disks = previous ? conf->previous_raid_disks
  2336. : conf->raid_disks;
  2337. int data_disks = raid_disks - conf->max_degraded;
  2338. /* First compute the information on this sector */
  2339. /*
  2340. * Compute the chunk number and the sector offset inside the chunk
  2341. */
  2342. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  2343. chunk_number = r_sector;
  2344. /*
  2345. * Compute the stripe number
  2346. */
  2347. stripe = chunk_number;
  2348. *dd_idx = sector_div(stripe, data_disks);
  2349. stripe2 = stripe;
  2350. /*
  2351. * Select the parity disk based on the user selected algorithm.
  2352. */
  2353. pd_idx = qd_idx = -1;
  2354. switch(conf->level) {
  2355. case 4:
  2356. pd_idx = data_disks;
  2357. break;
  2358. case 5:
  2359. switch (algorithm) {
  2360. case ALGORITHM_LEFT_ASYMMETRIC:
  2361. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  2362. if (*dd_idx >= pd_idx)
  2363. (*dd_idx)++;
  2364. break;
  2365. case ALGORITHM_RIGHT_ASYMMETRIC:
  2366. pd_idx = sector_div(stripe2, raid_disks);
  2367. if (*dd_idx >= pd_idx)
  2368. (*dd_idx)++;
  2369. break;
  2370. case ALGORITHM_LEFT_SYMMETRIC:
  2371. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  2372. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  2373. break;
  2374. case ALGORITHM_RIGHT_SYMMETRIC:
  2375. pd_idx = sector_div(stripe2, raid_disks);
  2376. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  2377. break;
  2378. case ALGORITHM_PARITY_0:
  2379. pd_idx = 0;
  2380. (*dd_idx)++;
  2381. break;
  2382. case ALGORITHM_PARITY_N:
  2383. pd_idx = data_disks;
  2384. break;
  2385. default:
  2386. BUG();
  2387. }
  2388. break;
  2389. case 6:
  2390. switch (algorithm) {
  2391. case ALGORITHM_LEFT_ASYMMETRIC:
  2392. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2393. qd_idx = pd_idx + 1;
  2394. if (pd_idx == raid_disks-1) {
  2395. (*dd_idx)++; /* Q D D D P */
  2396. qd_idx = 0;
  2397. } else if (*dd_idx >= pd_idx)
  2398. (*dd_idx) += 2; /* D D P Q D */
  2399. break;
  2400. case ALGORITHM_RIGHT_ASYMMETRIC:
  2401. pd_idx = sector_div(stripe2, raid_disks);
  2402. qd_idx = pd_idx + 1;
  2403. if (pd_idx == raid_disks-1) {
  2404. (*dd_idx)++; /* Q D D D P */
  2405. qd_idx = 0;
  2406. } else if (*dd_idx >= pd_idx)
  2407. (*dd_idx) += 2; /* D D P Q D */
  2408. break;
  2409. case ALGORITHM_LEFT_SYMMETRIC:
  2410. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2411. qd_idx = (pd_idx + 1) % raid_disks;
  2412. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  2413. break;
  2414. case ALGORITHM_RIGHT_SYMMETRIC:
  2415. pd_idx = sector_div(stripe2, raid_disks);
  2416. qd_idx = (pd_idx + 1) % raid_disks;
  2417. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  2418. break;
  2419. case ALGORITHM_PARITY_0:
  2420. pd_idx = 0;
  2421. qd_idx = 1;
  2422. (*dd_idx) += 2;
  2423. break;
  2424. case ALGORITHM_PARITY_N:
  2425. pd_idx = data_disks;
  2426. qd_idx = data_disks + 1;
  2427. break;
  2428. case ALGORITHM_ROTATING_ZERO_RESTART:
  2429. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  2430. * of blocks for computing Q is different.
  2431. */
  2432. pd_idx = sector_div(stripe2, raid_disks);
  2433. qd_idx = pd_idx + 1;
  2434. if (pd_idx == raid_disks-1) {
  2435. (*dd_idx)++; /* Q D D D P */
  2436. qd_idx = 0;
  2437. } else if (*dd_idx >= pd_idx)
  2438. (*dd_idx) += 2; /* D D P Q D */
  2439. ddf_layout = 1;
  2440. break;
  2441. case ALGORITHM_ROTATING_N_RESTART:
  2442. /* Same a left_asymmetric, by first stripe is
  2443. * D D D P Q rather than
  2444. * Q D D D P
  2445. */
  2446. stripe2 += 1;
  2447. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2448. qd_idx = pd_idx + 1;
  2449. if (pd_idx == raid_disks-1) {
  2450. (*dd_idx)++; /* Q D D D P */
  2451. qd_idx = 0;
  2452. } else if (*dd_idx >= pd_idx)
  2453. (*dd_idx) += 2; /* D D P Q D */
  2454. ddf_layout = 1;
  2455. break;
  2456. case ALGORITHM_ROTATING_N_CONTINUE:
  2457. /* Same as left_symmetric but Q is before P */
  2458. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  2459. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  2460. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  2461. ddf_layout = 1;
  2462. break;
  2463. case ALGORITHM_LEFT_ASYMMETRIC_6:
  2464. /* RAID5 left_asymmetric, with Q on last device */
  2465. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  2466. if (*dd_idx >= pd_idx)
  2467. (*dd_idx)++;
  2468. qd_idx = raid_disks - 1;
  2469. break;
  2470. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  2471. pd_idx = sector_div(stripe2, raid_disks-1);
  2472. if (*dd_idx >= pd_idx)
  2473. (*dd_idx)++;
  2474. qd_idx = raid_disks - 1;
  2475. break;
  2476. case ALGORITHM_LEFT_SYMMETRIC_6:
  2477. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  2478. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  2479. qd_idx = raid_disks - 1;
  2480. break;
  2481. case ALGORITHM_RIGHT_SYMMETRIC_6:
  2482. pd_idx = sector_div(stripe2, raid_disks-1);
  2483. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  2484. qd_idx = raid_disks - 1;
  2485. break;
  2486. case ALGORITHM_PARITY_0_6:
  2487. pd_idx = 0;
  2488. (*dd_idx)++;
  2489. qd_idx = raid_disks - 1;
  2490. break;
  2491. default:
  2492. BUG();
  2493. }
  2494. break;
  2495. }
  2496. if (sh) {
  2497. sh->pd_idx = pd_idx;
  2498. sh->qd_idx = qd_idx;
  2499. sh->ddf_layout = ddf_layout;
  2500. }
  2501. /*
  2502. * Finally, compute the new sector number
  2503. */
  2504. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  2505. return new_sector;
  2506. }
  2507. sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
  2508. {
  2509. struct r5conf *conf = sh->raid_conf;
  2510. int raid_disks = sh->disks;
  2511. int data_disks = raid_disks - conf->max_degraded;
  2512. sector_t new_sector = sh->sector, check;
  2513. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  2514. : conf->chunk_sectors;
  2515. int algorithm = previous ? conf->prev_algo
  2516. : conf->algorithm;
  2517. sector_t stripe;
  2518. int chunk_offset;
  2519. sector_t chunk_number;
  2520. int dummy1, dd_idx = i;
  2521. sector_t r_sector;
  2522. struct stripe_head sh2;
  2523. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  2524. stripe = new_sector;
  2525. if (i == sh->pd_idx)
  2526. return 0;
  2527. switch(conf->level) {
  2528. case 4: break;
  2529. case 5:
  2530. switch (algorithm) {
  2531. case ALGORITHM_LEFT_ASYMMETRIC:
  2532. case ALGORITHM_RIGHT_ASYMMETRIC:
  2533. if (i > sh->pd_idx)
  2534. i--;
  2535. break;
  2536. case ALGORITHM_LEFT_SYMMETRIC:
  2537. case ALGORITHM_RIGHT_SYMMETRIC:
  2538. if (i < sh->pd_idx)
  2539. i += raid_disks;
  2540. i -= (sh->pd_idx + 1);
  2541. break;
  2542. case ALGORITHM_PARITY_0:
  2543. i -= 1;
  2544. break;
  2545. case ALGORITHM_PARITY_N:
  2546. break;
  2547. default:
  2548. BUG();
  2549. }
  2550. break;
  2551. case 6:
  2552. if (i == sh->qd_idx)
  2553. return 0; /* It is the Q disk */
  2554. switch (algorithm) {
  2555. case ALGORITHM_LEFT_ASYMMETRIC:
  2556. case ALGORITHM_RIGHT_ASYMMETRIC:
  2557. case ALGORITHM_ROTATING_ZERO_RESTART:
  2558. case ALGORITHM_ROTATING_N_RESTART:
  2559. if (sh->pd_idx == raid_disks-1)
  2560. i--; /* Q D D D P */
  2561. else if (i > sh->pd_idx)
  2562. i -= 2; /* D D P Q D */
  2563. break;
  2564. case ALGORITHM_LEFT_SYMMETRIC:
  2565. case ALGORITHM_RIGHT_SYMMETRIC:
  2566. if (sh->pd_idx == raid_disks-1)
  2567. i--; /* Q D D D P */
  2568. else {
  2569. /* D D P Q D */
  2570. if (i < sh->pd_idx)
  2571. i += raid_disks;
  2572. i -= (sh->pd_idx + 2);
  2573. }
  2574. break;
  2575. case ALGORITHM_PARITY_0:
  2576. i -= 2;
  2577. break;
  2578. case ALGORITHM_PARITY_N:
  2579. break;
  2580. case ALGORITHM_ROTATING_N_CONTINUE:
  2581. /* Like left_symmetric, but P is before Q */
  2582. if (sh->pd_idx == 0)
  2583. i--; /* P D D D Q */
  2584. else {
  2585. /* D D Q P D */
  2586. if (i < sh->pd_idx)
  2587. i += raid_disks;
  2588. i -= (sh->pd_idx + 1);
  2589. }
  2590. break;
  2591. case ALGORITHM_LEFT_ASYMMETRIC_6:
  2592. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  2593. if (i > sh->pd_idx)
  2594. i--;
  2595. break;
  2596. case ALGORITHM_LEFT_SYMMETRIC_6:
  2597. case ALGORITHM_RIGHT_SYMMETRIC_6:
  2598. if (i < sh->pd_idx)
  2599. i += data_disks + 1;
  2600. i -= (sh->pd_idx + 1);
  2601. break;
  2602. case ALGORITHM_PARITY_0_6:
  2603. i -= 1;
  2604. break;
  2605. default:
  2606. BUG();
  2607. }
  2608. break;
  2609. }
  2610. chunk_number = stripe * data_disks + i;
  2611. r_sector = chunk_number * sectors_per_chunk + chunk_offset;
  2612. check = raid5_compute_sector(conf, r_sector,
  2613. previous, &dummy1, &sh2);
  2614. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  2615. || sh2.qd_idx != sh->qd_idx) {
  2616. pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
  2617. mdname(conf->mddev));
  2618. return 0;
  2619. }
  2620. return r_sector;
  2621. }
  2622. /*
  2623. * There are cases where we want handle_stripe_dirtying() and
  2624. * schedule_reconstruction() to delay towrite to some dev of a stripe.
  2625. *
  2626. * This function checks whether we want to delay the towrite. Specifically,
  2627. * we delay the towrite when:
  2628. *
  2629. * 1. degraded stripe has a non-overwrite to the missing dev, AND this
  2630. * stripe has data in journal (for other devices).
  2631. *
  2632. * In this case, when reading data for the non-overwrite dev, it is
  2633. * necessary to handle complex rmw of write back cache (prexor with
  2634. * orig_page, and xor with page). To keep read path simple, we would
  2635. * like to flush data in journal to RAID disks first, so complex rmw
  2636. * is handled in the write patch (handle_stripe_dirtying).
  2637. *
  2638. * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
  2639. *
  2640. * It is important to be able to flush all stripes in raid5-cache.
  2641. * Therefore, we need reserve some space on the journal device for
  2642. * these flushes. If flush operation includes pending writes to the
  2643. * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
  2644. * for the flush out. If we exclude these pending writes from flush
  2645. * operation, we only need (conf->max_degraded + 1) pages per stripe.
  2646. * Therefore, excluding pending writes in these cases enables more
  2647. * efficient use of the journal device.
  2648. *
  2649. * Note: To make sure the stripe makes progress, we only delay
  2650. * towrite for stripes with data already in journal (injournal > 0).
  2651. * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
  2652. * no_space_stripes list.
  2653. *
  2654. */
  2655. static inline bool delay_towrite(struct r5conf *conf,
  2656. struct r5dev *dev,
  2657. struct stripe_head_state *s)
  2658. {
  2659. /* case 1 above */
  2660. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2661. !test_bit(R5_Insync, &dev->flags) && s->injournal)
  2662. return true;
  2663. /* case 2 above */
  2664. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  2665. s->injournal > 0)
  2666. return true;
  2667. return false;
  2668. }
  2669. static void
  2670. schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
  2671. int rcw, int expand)
  2672. {
  2673. int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
  2674. struct r5conf *conf = sh->raid_conf;
  2675. int level = conf->level;
  2676. if (rcw) {
  2677. /*
  2678. * In some cases, handle_stripe_dirtying initially decided to
  2679. * run rmw and allocates extra page for prexor. However, rcw is
  2680. * cheaper later on. We need to free the extra page now,
  2681. * because we won't be able to do that in ops_complete_prexor().
  2682. */
  2683. r5c_release_extra_page(sh);
  2684. for (i = disks; i--; ) {
  2685. struct r5dev *dev = &sh->dev[i];
  2686. if (dev->towrite && !delay_towrite(conf, dev, s)) {
  2687. set_bit(R5_LOCKED, &dev->flags);
  2688. set_bit(R5_Wantdrain, &dev->flags);
  2689. if (!expand)
  2690. clear_bit(R5_UPTODATE, &dev->flags);
  2691. s->locked++;
  2692. } else if (test_bit(R5_InJournal, &dev->flags)) {
  2693. set_bit(R5_LOCKED, &dev->flags);
  2694. s->locked++;
  2695. }
  2696. }
  2697. /* if we are not expanding this is a proper write request, and
  2698. * there will be bios with new data to be drained into the
  2699. * stripe cache
  2700. */
  2701. if (!expand) {
  2702. if (!s->locked)
  2703. /* False alarm, nothing to do */
  2704. return;
  2705. sh->reconstruct_state = reconstruct_state_drain_run;
  2706. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2707. } else
  2708. sh->reconstruct_state = reconstruct_state_run;
  2709. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  2710. if (s->locked + conf->max_degraded == disks)
  2711. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  2712. atomic_inc(&conf->pending_full_writes);
  2713. } else {
  2714. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  2715. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  2716. BUG_ON(level == 6 &&
  2717. (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
  2718. test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
  2719. for (i = disks; i--; ) {
  2720. struct r5dev *dev = &sh->dev[i];
  2721. if (i == pd_idx || i == qd_idx)
  2722. continue;
  2723. if (dev->towrite &&
  2724. (test_bit(R5_UPTODATE, &dev->flags) ||
  2725. test_bit(R5_Wantcompute, &dev->flags))) {
  2726. set_bit(R5_Wantdrain, &dev->flags);
  2727. set_bit(R5_LOCKED, &dev->flags);
  2728. clear_bit(R5_UPTODATE, &dev->flags);
  2729. s->locked++;
  2730. } else if (test_bit(R5_InJournal, &dev->flags)) {
  2731. set_bit(R5_LOCKED, &dev->flags);
  2732. s->locked++;
  2733. }
  2734. }
  2735. if (!s->locked)
  2736. /* False alarm - nothing to do */
  2737. return;
  2738. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  2739. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  2740. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2741. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  2742. }
  2743. /* keep the parity disk(s) locked while asynchronous operations
  2744. * are in flight
  2745. */
  2746. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  2747. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2748. s->locked++;
  2749. if (level == 6) {
  2750. int qd_idx = sh->qd_idx;
  2751. struct r5dev *dev = &sh->dev[qd_idx];
  2752. set_bit(R5_LOCKED, &dev->flags);
  2753. clear_bit(R5_UPTODATE, &dev->flags);
  2754. s->locked++;
  2755. }
  2756. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  2757. __func__, (unsigned long long)sh->sector,
  2758. s->locked, s->ops_request);
  2759. }
  2760. /*
  2761. * Each stripe/dev can have one or more bion attached.
  2762. * toread/towrite point to the first in a chain.
  2763. * The bi_next chain must be in order.
  2764. */
  2765. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
  2766. int forwrite, int previous)
  2767. {
  2768. struct bio **bip;
  2769. struct r5conf *conf = sh->raid_conf;
  2770. int firstwrite=0;
  2771. pr_debug("adding bi b#%llu to stripe s#%llu\n",
  2772. (unsigned long long)bi->bi_iter.bi_sector,
  2773. (unsigned long long)sh->sector);
  2774. /*
  2775. * If several bio share a stripe. The bio bi_phys_segments acts as a
  2776. * reference count to avoid race. The reference count should already be
  2777. * increased before this function is called (for example, in
  2778. * raid5_make_request()), so other bio sharing this stripe will not free the
  2779. * stripe. If a stripe is owned by one stripe, the stripe lock will
  2780. * protect it.
  2781. */
  2782. spin_lock_irq(&sh->stripe_lock);
  2783. /* Don't allow new IO added to stripes in batch list */
  2784. if (sh->batch_head)
  2785. goto overlap;
  2786. if (forwrite) {
  2787. bip = &sh->dev[dd_idx].towrite;
  2788. if (*bip == NULL)
  2789. firstwrite = 1;
  2790. } else
  2791. bip = &sh->dev[dd_idx].toread;
  2792. while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
  2793. if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
  2794. goto overlap;
  2795. bip = & (*bip)->bi_next;
  2796. }
  2797. if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
  2798. goto overlap;
  2799. if (!forwrite || previous)
  2800. clear_bit(STRIPE_BATCH_READY, &sh->state);
  2801. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  2802. if (*bip)
  2803. bi->bi_next = *bip;
  2804. *bip = bi;
  2805. raid5_inc_bi_active_stripes(bi);
  2806. if (forwrite) {
  2807. /* check if page is covered */
  2808. sector_t sector = sh->dev[dd_idx].sector;
  2809. for (bi=sh->dev[dd_idx].towrite;
  2810. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  2811. bi && bi->bi_iter.bi_sector <= sector;
  2812. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  2813. if (bio_end_sector(bi) >= sector)
  2814. sector = bio_end_sector(bi);
  2815. }
  2816. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  2817. if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
  2818. sh->overwrite_disks++;
  2819. }
  2820. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  2821. (unsigned long long)(*bip)->bi_iter.bi_sector,
  2822. (unsigned long long)sh->sector, dd_idx);
  2823. if (conf->mddev->bitmap && firstwrite) {
  2824. /* Cannot hold spinlock over bitmap_startwrite,
  2825. * but must ensure this isn't added to a batch until
  2826. * we have added to the bitmap and set bm_seq.
  2827. * So set STRIPE_BITMAP_PENDING to prevent
  2828. * batching.
  2829. * If multiple add_stripe_bio() calls race here they
  2830. * much all set STRIPE_BITMAP_PENDING. So only the first one
  2831. * to complete "bitmap_startwrite" gets to set
  2832. * STRIPE_BIT_DELAY. This is important as once a stripe
  2833. * is added to a batch, STRIPE_BIT_DELAY cannot be changed
  2834. * any more.
  2835. */
  2836. set_bit(STRIPE_BITMAP_PENDING, &sh->state);
  2837. spin_unlock_irq(&sh->stripe_lock);
  2838. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  2839. STRIPE_SECTORS, 0);
  2840. spin_lock_irq(&sh->stripe_lock);
  2841. clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
  2842. if (!sh->batch_head) {
  2843. sh->bm_seq = conf->seq_flush+1;
  2844. set_bit(STRIPE_BIT_DELAY, &sh->state);
  2845. }
  2846. }
  2847. spin_unlock_irq(&sh->stripe_lock);
  2848. if (stripe_can_batch(sh))
  2849. stripe_add_to_batch_list(conf, sh);
  2850. return 1;
  2851. overlap:
  2852. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  2853. spin_unlock_irq(&sh->stripe_lock);
  2854. return 0;
  2855. }
  2856. static void end_reshape(struct r5conf *conf);
  2857. static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
  2858. struct stripe_head *sh)
  2859. {
  2860. int sectors_per_chunk =
  2861. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  2862. int dd_idx;
  2863. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  2864. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  2865. raid5_compute_sector(conf,
  2866. stripe * (disks - conf->max_degraded)
  2867. *sectors_per_chunk + chunk_offset,
  2868. previous,
  2869. &dd_idx, sh);
  2870. }
  2871. static void
  2872. handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
  2873. struct stripe_head_state *s, int disks,
  2874. struct bio_list *return_bi)
  2875. {
  2876. int i;
  2877. BUG_ON(sh->batch_head);
  2878. for (i = disks; i--; ) {
  2879. struct bio *bi;
  2880. int bitmap_end = 0;
  2881. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  2882. struct md_rdev *rdev;
  2883. rcu_read_lock();
  2884. rdev = rcu_dereference(conf->disks[i].rdev);
  2885. if (rdev && test_bit(In_sync, &rdev->flags) &&
  2886. !test_bit(Faulty, &rdev->flags))
  2887. atomic_inc(&rdev->nr_pending);
  2888. else
  2889. rdev = NULL;
  2890. rcu_read_unlock();
  2891. if (rdev) {
  2892. if (!rdev_set_badblocks(
  2893. rdev,
  2894. sh->sector,
  2895. STRIPE_SECTORS, 0))
  2896. md_error(conf->mddev, rdev);
  2897. rdev_dec_pending(rdev, conf->mddev);
  2898. }
  2899. }
  2900. spin_lock_irq(&sh->stripe_lock);
  2901. /* fail all writes first */
  2902. bi = sh->dev[i].towrite;
  2903. sh->dev[i].towrite = NULL;
  2904. sh->overwrite_disks = 0;
  2905. spin_unlock_irq(&sh->stripe_lock);
  2906. if (bi)
  2907. bitmap_end = 1;
  2908. r5l_stripe_write_finished(sh);
  2909. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2910. wake_up(&conf->wait_for_overlap);
  2911. while (bi && bi->bi_iter.bi_sector <
  2912. sh->dev[i].sector + STRIPE_SECTORS) {
  2913. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  2914. bi->bi_error = -EIO;
  2915. if (!raid5_dec_bi_active_stripes(bi)) {
  2916. md_write_end(conf->mddev);
  2917. bio_list_add(return_bi, bi);
  2918. }
  2919. bi = nextbi;
  2920. }
  2921. if (bitmap_end)
  2922. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2923. STRIPE_SECTORS, 0, 0);
  2924. bitmap_end = 0;
  2925. /* and fail all 'written' */
  2926. bi = sh->dev[i].written;
  2927. sh->dev[i].written = NULL;
  2928. if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
  2929. WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
  2930. sh->dev[i].page = sh->dev[i].orig_page;
  2931. }
  2932. if (bi) bitmap_end = 1;
  2933. while (bi && bi->bi_iter.bi_sector <
  2934. sh->dev[i].sector + STRIPE_SECTORS) {
  2935. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  2936. bi->bi_error = -EIO;
  2937. if (!raid5_dec_bi_active_stripes(bi)) {
  2938. md_write_end(conf->mddev);
  2939. bio_list_add(return_bi, bi);
  2940. }
  2941. bi = bi2;
  2942. }
  2943. /* fail any reads if this device is non-operational and
  2944. * the data has not reached the cache yet.
  2945. */
  2946. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  2947. s->failed > conf->max_degraded &&
  2948. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  2949. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  2950. spin_lock_irq(&sh->stripe_lock);
  2951. bi = sh->dev[i].toread;
  2952. sh->dev[i].toread = NULL;
  2953. spin_unlock_irq(&sh->stripe_lock);
  2954. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2955. wake_up(&conf->wait_for_overlap);
  2956. if (bi)
  2957. s->to_read--;
  2958. while (bi && bi->bi_iter.bi_sector <
  2959. sh->dev[i].sector + STRIPE_SECTORS) {
  2960. struct bio *nextbi =
  2961. r5_next_bio(bi, sh->dev[i].sector);
  2962. bi->bi_error = -EIO;
  2963. if (!raid5_dec_bi_active_stripes(bi))
  2964. bio_list_add(return_bi, bi);
  2965. bi = nextbi;
  2966. }
  2967. }
  2968. if (bitmap_end)
  2969. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2970. STRIPE_SECTORS, 0, 0);
  2971. /* If we were in the middle of a write the parity block might
  2972. * still be locked - so just clear all R5_LOCKED flags
  2973. */
  2974. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2975. }
  2976. s->to_write = 0;
  2977. s->written = 0;
  2978. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2979. if (atomic_dec_and_test(&conf->pending_full_writes))
  2980. md_wakeup_thread(conf->mddev->thread);
  2981. }
  2982. static void
  2983. handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
  2984. struct stripe_head_state *s)
  2985. {
  2986. int abort = 0;
  2987. int i;
  2988. BUG_ON(sh->batch_head);
  2989. clear_bit(STRIPE_SYNCING, &sh->state);
  2990. if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
  2991. wake_up(&conf->wait_for_overlap);
  2992. s->syncing = 0;
  2993. s->replacing = 0;
  2994. /* There is nothing more to do for sync/check/repair.
  2995. * Don't even need to abort as that is handled elsewhere
  2996. * if needed, and not always wanted e.g. if there is a known
  2997. * bad block here.
  2998. * For recover/replace we need to record a bad block on all
  2999. * non-sync devices, or abort the recovery
  3000. */
  3001. if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
  3002. /* During recovery devices cannot be removed, so
  3003. * locking and refcounting of rdevs is not needed
  3004. */
  3005. rcu_read_lock();
  3006. for (i = 0; i < conf->raid_disks; i++) {
  3007. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  3008. if (rdev
  3009. && !test_bit(Faulty, &rdev->flags)
  3010. && !test_bit(In_sync, &rdev->flags)
  3011. && !rdev_set_badblocks(rdev, sh->sector,
  3012. STRIPE_SECTORS, 0))
  3013. abort = 1;
  3014. rdev = rcu_dereference(conf->disks[i].replacement);
  3015. if (rdev
  3016. && !test_bit(Faulty, &rdev->flags)
  3017. && !test_bit(In_sync, &rdev->flags)
  3018. && !rdev_set_badblocks(rdev, sh->sector,
  3019. STRIPE_SECTORS, 0))
  3020. abort = 1;
  3021. }
  3022. rcu_read_unlock();
  3023. if (abort)
  3024. conf->recovery_disabled =
  3025. conf->mddev->recovery_disabled;
  3026. }
  3027. md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
  3028. }
  3029. static int want_replace(struct stripe_head *sh, int disk_idx)
  3030. {
  3031. struct md_rdev *rdev;
  3032. int rv = 0;
  3033. rcu_read_lock();
  3034. rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
  3035. if (rdev
  3036. && !test_bit(Faulty, &rdev->flags)
  3037. && !test_bit(In_sync, &rdev->flags)
  3038. && (rdev->recovery_offset <= sh->sector
  3039. || rdev->mddev->recovery_cp <= sh->sector))
  3040. rv = 1;
  3041. rcu_read_unlock();
  3042. return rv;
  3043. }
  3044. static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
  3045. int disk_idx, int disks)
  3046. {
  3047. struct r5dev *dev = &sh->dev[disk_idx];
  3048. struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
  3049. &sh->dev[s->failed_num[1]] };
  3050. int i;
  3051. if (test_bit(R5_LOCKED, &dev->flags) ||
  3052. test_bit(R5_UPTODATE, &dev->flags))
  3053. /* No point reading this as we already have it or have
  3054. * decided to get it.
  3055. */
  3056. return 0;
  3057. if (dev->toread ||
  3058. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
  3059. /* We need this block to directly satisfy a request */
  3060. return 1;
  3061. if (s->syncing || s->expanding ||
  3062. (s->replacing && want_replace(sh, disk_idx)))
  3063. /* When syncing, or expanding we read everything.
  3064. * When replacing, we need the replaced block.
  3065. */
  3066. return 1;
  3067. if ((s->failed >= 1 && fdev[0]->toread) ||
  3068. (s->failed >= 2 && fdev[1]->toread))
  3069. /* If we want to read from a failed device, then
  3070. * we need to actually read every other device.
  3071. */
  3072. return 1;
  3073. /* Sometimes neither read-modify-write nor reconstruct-write
  3074. * cycles can work. In those cases we read every block we
  3075. * can. Then the parity-update is certain to have enough to
  3076. * work with.
  3077. * This can only be a problem when we need to write something,
  3078. * and some device has failed. If either of those tests
  3079. * fail we need look no further.
  3080. */
  3081. if (!s->failed || !s->to_write)
  3082. return 0;
  3083. if (test_bit(R5_Insync, &dev->flags) &&
  3084. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3085. /* Pre-reads at not permitted until after short delay
  3086. * to gather multiple requests. However if this
  3087. * device is no Insync, the block could only be be computed
  3088. * and there is no need to delay that.
  3089. */
  3090. return 0;
  3091. for (i = 0; i < s->failed && i < 2; i++) {
  3092. if (fdev[i]->towrite &&
  3093. !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
  3094. !test_bit(R5_OVERWRITE, &fdev[i]->flags))
  3095. /* If we have a partial write to a failed
  3096. * device, then we will need to reconstruct
  3097. * the content of that device, so all other
  3098. * devices must be read.
  3099. */
  3100. return 1;
  3101. }
  3102. /* If we are forced to do a reconstruct-write, either because
  3103. * the current RAID6 implementation only supports that, or
  3104. * or because parity cannot be trusted and we are currently
  3105. * recovering it, there is extra need to be careful.
  3106. * If one of the devices that we would need to read, because
  3107. * it is not being overwritten (and maybe not written at all)
  3108. * is missing/faulty, then we need to read everything we can.
  3109. */
  3110. if (sh->raid_conf->level != 6 &&
  3111. sh->sector < sh->raid_conf->mddev->recovery_cp)
  3112. /* reconstruct-write isn't being forced */
  3113. return 0;
  3114. for (i = 0; i < s->failed && i < 2; i++) {
  3115. if (s->failed_num[i] != sh->pd_idx &&
  3116. s->failed_num[i] != sh->qd_idx &&
  3117. !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
  3118. !test_bit(R5_OVERWRITE, &fdev[i]->flags))
  3119. return 1;
  3120. }
  3121. return 0;
  3122. }
  3123. /* fetch_block - checks the given member device to see if its data needs
  3124. * to be read or computed to satisfy a request.
  3125. *
  3126. * Returns 1 when no more member devices need to be checked, otherwise returns
  3127. * 0 to tell the loop in handle_stripe_fill to continue
  3128. */
  3129. static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
  3130. int disk_idx, int disks)
  3131. {
  3132. struct r5dev *dev = &sh->dev[disk_idx];
  3133. /* is the data in this block needed, and can we get it? */
  3134. if (need_this_block(sh, s, disk_idx, disks)) {
  3135. /* we would like to get this block, possibly by computing it,
  3136. * otherwise read it if the backing disk is insync
  3137. */
  3138. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  3139. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  3140. BUG_ON(sh->batch_head);
  3141. if ((s->uptodate == disks - 1) &&
  3142. (s->failed && (disk_idx == s->failed_num[0] ||
  3143. disk_idx == s->failed_num[1]))) {
  3144. /* have disk failed, and we're requested to fetch it;
  3145. * do compute it
  3146. */
  3147. pr_debug("Computing stripe %llu block %d\n",
  3148. (unsigned long long)sh->sector, disk_idx);
  3149. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3150. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3151. set_bit(R5_Wantcompute, &dev->flags);
  3152. sh->ops.target = disk_idx;
  3153. sh->ops.target2 = -1; /* no 2nd target */
  3154. s->req_compute = 1;
  3155. /* Careful: from this point on 'uptodate' is in the eye
  3156. * of raid_run_ops which services 'compute' operations
  3157. * before writes. R5_Wantcompute flags a block that will
  3158. * be R5_UPTODATE by the time it is needed for a
  3159. * subsequent operation.
  3160. */
  3161. s->uptodate++;
  3162. return 1;
  3163. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  3164. /* Computing 2-failure is *very* expensive; only
  3165. * do it if failed >= 2
  3166. */
  3167. int other;
  3168. for (other = disks; other--; ) {
  3169. if (other == disk_idx)
  3170. continue;
  3171. if (!test_bit(R5_UPTODATE,
  3172. &sh->dev[other].flags))
  3173. break;
  3174. }
  3175. BUG_ON(other < 0);
  3176. pr_debug("Computing stripe %llu blocks %d,%d\n",
  3177. (unsigned long long)sh->sector,
  3178. disk_idx, other);
  3179. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3180. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3181. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  3182. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  3183. sh->ops.target = disk_idx;
  3184. sh->ops.target2 = other;
  3185. s->uptodate += 2;
  3186. s->req_compute = 1;
  3187. return 1;
  3188. } else if (test_bit(R5_Insync, &dev->flags)) {
  3189. set_bit(R5_LOCKED, &dev->flags);
  3190. set_bit(R5_Wantread, &dev->flags);
  3191. s->locked++;
  3192. pr_debug("Reading block %d (sync=%d)\n",
  3193. disk_idx, s->syncing);
  3194. }
  3195. }
  3196. return 0;
  3197. }
  3198. /**
  3199. * handle_stripe_fill - read or compute data to satisfy pending requests.
  3200. */
  3201. static void handle_stripe_fill(struct stripe_head *sh,
  3202. struct stripe_head_state *s,
  3203. int disks)
  3204. {
  3205. int i;
  3206. /* look for blocks to read/compute, skip this if a compute
  3207. * is already in flight, or if the stripe contents are in the
  3208. * midst of changing due to a write
  3209. */
  3210. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  3211. !sh->reconstruct_state) {
  3212. /*
  3213. * For degraded stripe with data in journal, do not handle
  3214. * read requests yet, instead, flush the stripe to raid
  3215. * disks first, this avoids handling complex rmw of write
  3216. * back cache (prexor with orig_page, and then xor with
  3217. * page) in the read path
  3218. */
  3219. if (s->injournal && s->failed) {
  3220. if (test_bit(STRIPE_R5C_CACHING, &sh->state))
  3221. r5c_make_stripe_write_out(sh);
  3222. goto out;
  3223. }
  3224. for (i = disks; i--; )
  3225. if (fetch_block(sh, s, i, disks))
  3226. break;
  3227. }
  3228. out:
  3229. set_bit(STRIPE_HANDLE, &sh->state);
  3230. }
  3231. static void break_stripe_batch_list(struct stripe_head *head_sh,
  3232. unsigned long handle_flags);
  3233. /* handle_stripe_clean_event
  3234. * any written block on an uptodate or failed drive can be returned.
  3235. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  3236. * never LOCKED, so we don't need to test 'failed' directly.
  3237. */
  3238. static void handle_stripe_clean_event(struct r5conf *conf,
  3239. struct stripe_head *sh, int disks, struct bio_list *return_bi)
  3240. {
  3241. int i;
  3242. struct r5dev *dev;
  3243. int discard_pending = 0;
  3244. struct stripe_head *head_sh = sh;
  3245. bool do_endio = false;
  3246. for (i = disks; i--; )
  3247. if (sh->dev[i].written) {
  3248. dev = &sh->dev[i];
  3249. if (!test_bit(R5_LOCKED, &dev->flags) &&
  3250. (test_bit(R5_UPTODATE, &dev->flags) ||
  3251. test_bit(R5_Discard, &dev->flags) ||
  3252. test_bit(R5_SkipCopy, &dev->flags))) {
  3253. /* We can return any write requests */
  3254. struct bio *wbi, *wbi2;
  3255. pr_debug("Return write for disc %d\n", i);
  3256. if (test_and_clear_bit(R5_Discard, &dev->flags))
  3257. clear_bit(R5_UPTODATE, &dev->flags);
  3258. if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
  3259. WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
  3260. }
  3261. do_endio = true;
  3262. returnbi:
  3263. dev->page = dev->orig_page;
  3264. wbi = dev->written;
  3265. dev->written = NULL;
  3266. while (wbi && wbi->bi_iter.bi_sector <
  3267. dev->sector + STRIPE_SECTORS) {
  3268. wbi2 = r5_next_bio(wbi, dev->sector);
  3269. if (!raid5_dec_bi_active_stripes(wbi)) {
  3270. md_write_end(conf->mddev);
  3271. bio_list_add(return_bi, wbi);
  3272. }
  3273. wbi = wbi2;
  3274. }
  3275. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  3276. STRIPE_SECTORS,
  3277. !test_bit(STRIPE_DEGRADED, &sh->state),
  3278. 0);
  3279. if (head_sh->batch_head) {
  3280. sh = list_first_entry(&sh->batch_list,
  3281. struct stripe_head,
  3282. batch_list);
  3283. if (sh != head_sh) {
  3284. dev = &sh->dev[i];
  3285. goto returnbi;
  3286. }
  3287. }
  3288. sh = head_sh;
  3289. dev = &sh->dev[i];
  3290. } else if (test_bit(R5_Discard, &dev->flags))
  3291. discard_pending = 1;
  3292. }
  3293. r5l_stripe_write_finished(sh);
  3294. if (!discard_pending &&
  3295. test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
  3296. int hash;
  3297. clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
  3298. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  3299. if (sh->qd_idx >= 0) {
  3300. clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
  3301. clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
  3302. }
  3303. /* now that discard is done we can proceed with any sync */
  3304. clear_bit(STRIPE_DISCARD, &sh->state);
  3305. /*
  3306. * SCSI discard will change some bio fields and the stripe has
  3307. * no updated data, so remove it from hash list and the stripe
  3308. * will be reinitialized
  3309. */
  3310. unhash:
  3311. hash = sh->hash_lock_index;
  3312. spin_lock_irq(conf->hash_locks + hash);
  3313. remove_hash(sh);
  3314. spin_unlock_irq(conf->hash_locks + hash);
  3315. if (head_sh->batch_head) {
  3316. sh = list_first_entry(&sh->batch_list,
  3317. struct stripe_head, batch_list);
  3318. if (sh != head_sh)
  3319. goto unhash;
  3320. }
  3321. sh = head_sh;
  3322. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
  3323. set_bit(STRIPE_HANDLE, &sh->state);
  3324. }
  3325. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  3326. if (atomic_dec_and_test(&conf->pending_full_writes))
  3327. md_wakeup_thread(conf->mddev->thread);
  3328. if (head_sh->batch_head && do_endio)
  3329. break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
  3330. }
  3331. /*
  3332. * For RMW in write back cache, we need extra page in prexor to store the
  3333. * old data. This page is stored in dev->orig_page.
  3334. *
  3335. * This function checks whether we have data for prexor. The exact logic
  3336. * is:
  3337. * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
  3338. */
  3339. static inline bool uptodate_for_rmw(struct r5dev *dev)
  3340. {
  3341. return (test_bit(R5_UPTODATE, &dev->flags)) &&
  3342. (!test_bit(R5_InJournal, &dev->flags) ||
  3343. test_bit(R5_OrigPageUPTDODATE, &dev->flags));
  3344. }
  3345. static int handle_stripe_dirtying(struct r5conf *conf,
  3346. struct stripe_head *sh,
  3347. struct stripe_head_state *s,
  3348. int disks)
  3349. {
  3350. int rmw = 0, rcw = 0, i;
  3351. sector_t recovery_cp = conf->mddev->recovery_cp;
  3352. /* Check whether resync is now happening or should start.
  3353. * If yes, then the array is dirty (after unclean shutdown or
  3354. * initial creation), so parity in some stripes might be inconsistent.
  3355. * In this case, we need to always do reconstruct-write, to ensure
  3356. * that in case of drive failure or read-error correction, we
  3357. * generate correct data from the parity.
  3358. */
  3359. if (conf->rmw_level == PARITY_DISABLE_RMW ||
  3360. (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
  3361. s->failed == 0)) {
  3362. /* Calculate the real rcw later - for now make it
  3363. * look like rcw is cheaper
  3364. */
  3365. rcw = 1; rmw = 2;
  3366. pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
  3367. conf->rmw_level, (unsigned long long)recovery_cp,
  3368. (unsigned long long)sh->sector);
  3369. } else for (i = disks; i--; ) {
  3370. /* would I have to read this buffer for read_modify_write */
  3371. struct r5dev *dev = &sh->dev[i];
  3372. if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
  3373. i == sh->pd_idx || i == sh->qd_idx ||
  3374. test_bit(R5_InJournal, &dev->flags)) &&
  3375. !test_bit(R5_LOCKED, &dev->flags) &&
  3376. !(uptodate_for_rmw(dev) ||
  3377. test_bit(R5_Wantcompute, &dev->flags))) {
  3378. if (test_bit(R5_Insync, &dev->flags))
  3379. rmw++;
  3380. else
  3381. rmw += 2*disks; /* cannot read it */
  3382. }
  3383. /* Would I have to read this buffer for reconstruct_write */
  3384. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  3385. i != sh->pd_idx && i != sh->qd_idx &&
  3386. !test_bit(R5_LOCKED, &dev->flags) &&
  3387. !(test_bit(R5_UPTODATE, &dev->flags) ||
  3388. test_bit(R5_Wantcompute, &dev->flags))) {
  3389. if (test_bit(R5_Insync, &dev->flags))
  3390. rcw++;
  3391. else
  3392. rcw += 2*disks;
  3393. }
  3394. }
  3395. pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
  3396. (unsigned long long)sh->sector, sh->state, rmw, rcw);
  3397. set_bit(STRIPE_HANDLE, &sh->state);
  3398. if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
  3399. /* prefer read-modify-write, but need to get some data */
  3400. if (conf->mddev->queue)
  3401. blk_add_trace_msg(conf->mddev->queue,
  3402. "raid5 rmw %llu %d",
  3403. (unsigned long long)sh->sector, rmw);
  3404. for (i = disks; i--; ) {
  3405. struct r5dev *dev = &sh->dev[i];
  3406. if (test_bit(R5_InJournal, &dev->flags) &&
  3407. dev->page == dev->orig_page &&
  3408. !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
  3409. /* alloc page for prexor */
  3410. struct page *p = alloc_page(GFP_NOIO);
  3411. if (p) {
  3412. dev->orig_page = p;
  3413. continue;
  3414. }
  3415. /*
  3416. * alloc_page() failed, try use
  3417. * disk_info->extra_page
  3418. */
  3419. if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
  3420. &conf->cache_state)) {
  3421. r5c_use_extra_page(sh);
  3422. break;
  3423. }
  3424. /* extra_page in use, add to delayed_list */
  3425. set_bit(STRIPE_DELAYED, &sh->state);
  3426. s->waiting_extra_page = 1;
  3427. return -EAGAIN;
  3428. }
  3429. }
  3430. for (i = disks; i--; ) {
  3431. struct r5dev *dev = &sh->dev[i];
  3432. if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
  3433. i == sh->pd_idx || i == sh->qd_idx ||
  3434. test_bit(R5_InJournal, &dev->flags)) &&
  3435. !test_bit(R5_LOCKED, &dev->flags) &&
  3436. !(uptodate_for_rmw(dev) ||
  3437. test_bit(R5_Wantcompute, &dev->flags)) &&
  3438. test_bit(R5_Insync, &dev->flags)) {
  3439. if (test_bit(STRIPE_PREREAD_ACTIVE,
  3440. &sh->state)) {
  3441. pr_debug("Read_old block %d for r-m-w\n",
  3442. i);
  3443. set_bit(R5_LOCKED, &dev->flags);
  3444. set_bit(R5_Wantread, &dev->flags);
  3445. s->locked++;
  3446. } else {
  3447. set_bit(STRIPE_DELAYED, &sh->state);
  3448. set_bit(STRIPE_HANDLE, &sh->state);
  3449. }
  3450. }
  3451. }
  3452. }
  3453. if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
  3454. /* want reconstruct write, but need to get some data */
  3455. int qread =0;
  3456. rcw = 0;
  3457. for (i = disks; i--; ) {
  3458. struct r5dev *dev = &sh->dev[i];
  3459. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  3460. i != sh->pd_idx && i != sh->qd_idx &&
  3461. !test_bit(R5_LOCKED, &dev->flags) &&
  3462. !(test_bit(R5_UPTODATE, &dev->flags) ||
  3463. test_bit(R5_Wantcompute, &dev->flags))) {
  3464. rcw++;
  3465. if (test_bit(R5_Insync, &dev->flags) &&
  3466. test_bit(STRIPE_PREREAD_ACTIVE,
  3467. &sh->state)) {
  3468. pr_debug("Read_old block "
  3469. "%d for Reconstruct\n", i);
  3470. set_bit(R5_LOCKED, &dev->flags);
  3471. set_bit(R5_Wantread, &dev->flags);
  3472. s->locked++;
  3473. qread++;
  3474. } else {
  3475. set_bit(STRIPE_DELAYED, &sh->state);
  3476. set_bit(STRIPE_HANDLE, &sh->state);
  3477. }
  3478. }
  3479. }
  3480. if (rcw && conf->mddev->queue)
  3481. blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
  3482. (unsigned long long)sh->sector,
  3483. rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
  3484. }
  3485. if (rcw > disks && rmw > disks &&
  3486. !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3487. set_bit(STRIPE_DELAYED, &sh->state);
  3488. /* now if nothing is locked, and if we have enough data,
  3489. * we can start a write request
  3490. */
  3491. /* since handle_stripe can be called at any time we need to handle the
  3492. * case where a compute block operation has been submitted and then a
  3493. * subsequent call wants to start a write request. raid_run_ops only
  3494. * handles the case where compute block and reconstruct are requested
  3495. * simultaneously. If this is not the case then new writes need to be
  3496. * held off until the compute completes.
  3497. */
  3498. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  3499. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  3500. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  3501. schedule_reconstruction(sh, s, rcw == 0, 0);
  3502. return 0;
  3503. }
  3504. static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
  3505. struct stripe_head_state *s, int disks)
  3506. {
  3507. struct r5dev *dev = NULL;
  3508. BUG_ON(sh->batch_head);
  3509. set_bit(STRIPE_HANDLE, &sh->state);
  3510. switch (sh->check_state) {
  3511. case check_state_idle:
  3512. /* start a new check operation if there are no failures */
  3513. if (s->failed == 0) {
  3514. BUG_ON(s->uptodate != disks);
  3515. sh->check_state = check_state_run;
  3516. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  3517. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  3518. s->uptodate--;
  3519. break;
  3520. }
  3521. dev = &sh->dev[s->failed_num[0]];
  3522. /* fall through */
  3523. case check_state_compute_result:
  3524. sh->check_state = check_state_idle;
  3525. if (!dev)
  3526. dev = &sh->dev[sh->pd_idx];
  3527. /* check that a write has not made the stripe insync */
  3528. if (test_bit(STRIPE_INSYNC, &sh->state))
  3529. break;
  3530. /* either failed parity check, or recovery is happening */
  3531. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  3532. BUG_ON(s->uptodate != disks);
  3533. set_bit(R5_LOCKED, &dev->flags);
  3534. s->locked++;
  3535. set_bit(R5_Wantwrite, &dev->flags);
  3536. clear_bit(STRIPE_DEGRADED, &sh->state);
  3537. set_bit(STRIPE_INSYNC, &sh->state);
  3538. break;
  3539. case check_state_run:
  3540. break; /* we will be called again upon completion */
  3541. case check_state_check_result:
  3542. sh->check_state = check_state_idle;
  3543. /* if a failure occurred during the check operation, leave
  3544. * STRIPE_INSYNC not set and let the stripe be handled again
  3545. */
  3546. if (s->failed)
  3547. break;
  3548. /* handle a successful check operation, if parity is correct
  3549. * we are done. Otherwise update the mismatch count and repair
  3550. * parity if !MD_RECOVERY_CHECK
  3551. */
  3552. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  3553. /* parity is correct (on disc,
  3554. * not in buffer any more)
  3555. */
  3556. set_bit(STRIPE_INSYNC, &sh->state);
  3557. else {
  3558. atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
  3559. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  3560. /* don't try to repair!! */
  3561. set_bit(STRIPE_INSYNC, &sh->state);
  3562. else {
  3563. sh->check_state = check_state_compute_run;
  3564. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3565. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3566. set_bit(R5_Wantcompute,
  3567. &sh->dev[sh->pd_idx].flags);
  3568. sh->ops.target = sh->pd_idx;
  3569. sh->ops.target2 = -1;
  3570. s->uptodate++;
  3571. }
  3572. }
  3573. break;
  3574. case check_state_compute_run:
  3575. break;
  3576. default:
  3577. pr_err("%s: unknown check_state: %d sector: %llu\n",
  3578. __func__, sh->check_state,
  3579. (unsigned long long) sh->sector);
  3580. BUG();
  3581. }
  3582. }
  3583. static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
  3584. struct stripe_head_state *s,
  3585. int disks)
  3586. {
  3587. int pd_idx = sh->pd_idx;
  3588. int qd_idx = sh->qd_idx;
  3589. struct r5dev *dev;
  3590. BUG_ON(sh->batch_head);
  3591. set_bit(STRIPE_HANDLE, &sh->state);
  3592. BUG_ON(s->failed > 2);
  3593. /* Want to check and possibly repair P and Q.
  3594. * However there could be one 'failed' device, in which
  3595. * case we can only check one of them, possibly using the
  3596. * other to generate missing data
  3597. */
  3598. switch (sh->check_state) {
  3599. case check_state_idle:
  3600. /* start a new check operation if there are < 2 failures */
  3601. if (s->failed == s->q_failed) {
  3602. /* The only possible failed device holds Q, so it
  3603. * makes sense to check P (If anything else were failed,
  3604. * we would have used P to recreate it).
  3605. */
  3606. sh->check_state = check_state_run;
  3607. }
  3608. if (!s->q_failed && s->failed < 2) {
  3609. /* Q is not failed, and we didn't use it to generate
  3610. * anything, so it makes sense to check it
  3611. */
  3612. if (sh->check_state == check_state_run)
  3613. sh->check_state = check_state_run_pq;
  3614. else
  3615. sh->check_state = check_state_run_q;
  3616. }
  3617. /* discard potentially stale zero_sum_result */
  3618. sh->ops.zero_sum_result = 0;
  3619. if (sh->check_state == check_state_run) {
  3620. /* async_xor_zero_sum destroys the contents of P */
  3621. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  3622. s->uptodate--;
  3623. }
  3624. if (sh->check_state >= check_state_run &&
  3625. sh->check_state <= check_state_run_pq) {
  3626. /* async_syndrome_zero_sum preserves P and Q, so
  3627. * no need to mark them !uptodate here
  3628. */
  3629. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  3630. break;
  3631. }
  3632. /* we have 2-disk failure */
  3633. BUG_ON(s->failed != 2);
  3634. /* fall through */
  3635. case check_state_compute_result:
  3636. sh->check_state = check_state_idle;
  3637. /* check that a write has not made the stripe insync */
  3638. if (test_bit(STRIPE_INSYNC, &sh->state))
  3639. break;
  3640. /* now write out any block on a failed drive,
  3641. * or P or Q if they were recomputed
  3642. */
  3643. BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
  3644. if (s->failed == 2) {
  3645. dev = &sh->dev[s->failed_num[1]];
  3646. s->locked++;
  3647. set_bit(R5_LOCKED, &dev->flags);
  3648. set_bit(R5_Wantwrite, &dev->flags);
  3649. }
  3650. if (s->failed >= 1) {
  3651. dev = &sh->dev[s->failed_num[0]];
  3652. s->locked++;
  3653. set_bit(R5_LOCKED, &dev->flags);
  3654. set_bit(R5_Wantwrite, &dev->flags);
  3655. }
  3656. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  3657. dev = &sh->dev[pd_idx];
  3658. s->locked++;
  3659. set_bit(R5_LOCKED, &dev->flags);
  3660. set_bit(R5_Wantwrite, &dev->flags);
  3661. }
  3662. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  3663. dev = &sh->dev[qd_idx];
  3664. s->locked++;
  3665. set_bit(R5_LOCKED, &dev->flags);
  3666. set_bit(R5_Wantwrite, &dev->flags);
  3667. }
  3668. clear_bit(STRIPE_DEGRADED, &sh->state);
  3669. set_bit(STRIPE_INSYNC, &sh->state);
  3670. break;
  3671. case check_state_run:
  3672. case check_state_run_q:
  3673. case check_state_run_pq:
  3674. break; /* we will be called again upon completion */
  3675. case check_state_check_result:
  3676. sh->check_state = check_state_idle;
  3677. /* handle a successful check operation, if parity is correct
  3678. * we are done. Otherwise update the mismatch count and repair
  3679. * parity if !MD_RECOVERY_CHECK
  3680. */
  3681. if (sh->ops.zero_sum_result == 0) {
  3682. /* both parities are correct */
  3683. if (!s->failed)
  3684. set_bit(STRIPE_INSYNC, &sh->state);
  3685. else {
  3686. /* in contrast to the raid5 case we can validate
  3687. * parity, but still have a failure to write
  3688. * back
  3689. */
  3690. sh->check_state = check_state_compute_result;
  3691. /* Returning at this point means that we may go
  3692. * off and bring p and/or q uptodate again so
  3693. * we make sure to check zero_sum_result again
  3694. * to verify if p or q need writeback
  3695. */
  3696. }
  3697. } else {
  3698. atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
  3699. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  3700. /* don't try to repair!! */
  3701. set_bit(STRIPE_INSYNC, &sh->state);
  3702. else {
  3703. int *target = &sh->ops.target;
  3704. sh->ops.target = -1;
  3705. sh->ops.target2 = -1;
  3706. sh->check_state = check_state_compute_run;
  3707. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  3708. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  3709. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  3710. set_bit(R5_Wantcompute,
  3711. &sh->dev[pd_idx].flags);
  3712. *target = pd_idx;
  3713. target = &sh->ops.target2;
  3714. s->uptodate++;
  3715. }
  3716. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  3717. set_bit(R5_Wantcompute,
  3718. &sh->dev[qd_idx].flags);
  3719. *target = qd_idx;
  3720. s->uptodate++;
  3721. }
  3722. }
  3723. }
  3724. break;
  3725. case check_state_compute_run:
  3726. break;
  3727. default:
  3728. pr_warn("%s: unknown check_state: %d sector: %llu\n",
  3729. __func__, sh->check_state,
  3730. (unsigned long long) sh->sector);
  3731. BUG();
  3732. }
  3733. }
  3734. static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
  3735. {
  3736. int i;
  3737. /* We have read all the blocks in this stripe and now we need to
  3738. * copy some of them into a target stripe for expand.
  3739. */
  3740. struct dma_async_tx_descriptor *tx = NULL;
  3741. BUG_ON(sh->batch_head);
  3742. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3743. for (i = 0; i < sh->disks; i++)
  3744. if (i != sh->pd_idx && i != sh->qd_idx) {
  3745. int dd_idx, j;
  3746. struct stripe_head *sh2;
  3747. struct async_submit_ctl submit;
  3748. sector_t bn = raid5_compute_blocknr(sh, i, 1);
  3749. sector_t s = raid5_compute_sector(conf, bn, 0,
  3750. &dd_idx, NULL);
  3751. sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
  3752. if (sh2 == NULL)
  3753. /* so far only the early blocks of this stripe
  3754. * have been requested. When later blocks
  3755. * get requested, we will try again
  3756. */
  3757. continue;
  3758. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  3759. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  3760. /* must have already done this block */
  3761. raid5_release_stripe(sh2);
  3762. continue;
  3763. }
  3764. /* place all the copies on one channel */
  3765. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  3766. tx = async_memcpy(sh2->dev[dd_idx].page,
  3767. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  3768. &submit);
  3769. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  3770. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  3771. for (j = 0; j < conf->raid_disks; j++)
  3772. if (j != sh2->pd_idx &&
  3773. j != sh2->qd_idx &&
  3774. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  3775. break;
  3776. if (j == conf->raid_disks) {
  3777. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  3778. set_bit(STRIPE_HANDLE, &sh2->state);
  3779. }
  3780. raid5_release_stripe(sh2);
  3781. }
  3782. /* done submitting copies, wait for them to complete */
  3783. async_tx_quiesce(&tx);
  3784. }
  3785. /*
  3786. * handle_stripe - do things to a stripe.
  3787. *
  3788. * We lock the stripe by setting STRIPE_ACTIVE and then examine the
  3789. * state of various bits to see what needs to be done.
  3790. * Possible results:
  3791. * return some read requests which now have data
  3792. * return some write requests which are safely on storage
  3793. * schedule a read on some buffers
  3794. * schedule a write of some buffers
  3795. * return confirmation of parity correctness
  3796. *
  3797. */
  3798. static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
  3799. {
  3800. struct r5conf *conf = sh->raid_conf;
  3801. int disks = sh->disks;
  3802. struct r5dev *dev;
  3803. int i;
  3804. int do_recovery = 0;
  3805. memset(s, 0, sizeof(*s));
  3806. s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
  3807. s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
  3808. s->failed_num[0] = -1;
  3809. s->failed_num[1] = -1;
  3810. s->log_failed = r5l_log_disk_error(conf);
  3811. /* Now to look around and see what can be done */
  3812. rcu_read_lock();
  3813. for (i=disks; i--; ) {
  3814. struct md_rdev *rdev;
  3815. sector_t first_bad;
  3816. int bad_sectors;
  3817. int is_bad = 0;
  3818. dev = &sh->dev[i];
  3819. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  3820. i, dev->flags,
  3821. dev->toread, dev->towrite, dev->written);
  3822. /* maybe we can reply to a read
  3823. *
  3824. * new wantfill requests are only permitted while
  3825. * ops_complete_biofill is guaranteed to be inactive
  3826. */
  3827. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  3828. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  3829. set_bit(R5_Wantfill, &dev->flags);
  3830. /* now count some things */
  3831. if (test_bit(R5_LOCKED, &dev->flags))
  3832. s->locked++;
  3833. if (test_bit(R5_UPTODATE, &dev->flags))
  3834. s->uptodate++;
  3835. if (test_bit(R5_Wantcompute, &dev->flags)) {
  3836. s->compute++;
  3837. BUG_ON(s->compute > 2);
  3838. }
  3839. if (test_bit(R5_Wantfill, &dev->flags))
  3840. s->to_fill++;
  3841. else if (dev->toread)
  3842. s->to_read++;
  3843. if (dev->towrite) {
  3844. s->to_write++;
  3845. if (!test_bit(R5_OVERWRITE, &dev->flags))
  3846. s->non_overwrite++;
  3847. }
  3848. if (dev->written)
  3849. s->written++;
  3850. /* Prefer to use the replacement for reads, but only
  3851. * if it is recovered enough and has no bad blocks.
  3852. */
  3853. rdev = rcu_dereference(conf->disks[i].replacement);
  3854. if (rdev && !test_bit(Faulty, &rdev->flags) &&
  3855. rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
  3856. !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  3857. &first_bad, &bad_sectors))
  3858. set_bit(R5_ReadRepl, &dev->flags);
  3859. else {
  3860. if (rdev && !test_bit(Faulty, &rdev->flags))
  3861. set_bit(R5_NeedReplace, &dev->flags);
  3862. else
  3863. clear_bit(R5_NeedReplace, &dev->flags);
  3864. rdev = rcu_dereference(conf->disks[i].rdev);
  3865. clear_bit(R5_ReadRepl, &dev->flags);
  3866. }
  3867. if (rdev && test_bit(Faulty, &rdev->flags))
  3868. rdev = NULL;
  3869. if (rdev) {
  3870. is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  3871. &first_bad, &bad_sectors);
  3872. if (s->blocked_rdev == NULL
  3873. && (test_bit(Blocked, &rdev->flags)
  3874. || is_bad < 0)) {
  3875. if (is_bad < 0)
  3876. set_bit(BlockedBadBlocks,
  3877. &rdev->flags);
  3878. s->blocked_rdev = rdev;
  3879. atomic_inc(&rdev->nr_pending);
  3880. }
  3881. }
  3882. clear_bit(R5_Insync, &dev->flags);
  3883. if (!rdev)
  3884. /* Not in-sync */;
  3885. else if (is_bad) {
  3886. /* also not in-sync */
  3887. if (!test_bit(WriteErrorSeen, &rdev->flags) &&
  3888. test_bit(R5_UPTODATE, &dev->flags)) {
  3889. /* treat as in-sync, but with a read error
  3890. * which we can now try to correct
  3891. */
  3892. set_bit(R5_Insync, &dev->flags);
  3893. set_bit(R5_ReadError, &dev->flags);
  3894. }
  3895. } else if (test_bit(In_sync, &rdev->flags))
  3896. set_bit(R5_Insync, &dev->flags);
  3897. else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  3898. /* in sync if before recovery_offset */
  3899. set_bit(R5_Insync, &dev->flags);
  3900. else if (test_bit(R5_UPTODATE, &dev->flags) &&
  3901. test_bit(R5_Expanded, &dev->flags))
  3902. /* If we've reshaped into here, we assume it is Insync.
  3903. * We will shortly update recovery_offset to make
  3904. * it official.
  3905. */
  3906. set_bit(R5_Insync, &dev->flags);
  3907. if (test_bit(R5_WriteError, &dev->flags)) {
  3908. /* This flag does not apply to '.replacement'
  3909. * only to .rdev, so make sure to check that*/
  3910. struct md_rdev *rdev2 = rcu_dereference(
  3911. conf->disks[i].rdev);
  3912. if (rdev2 == rdev)
  3913. clear_bit(R5_Insync, &dev->flags);
  3914. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3915. s->handle_bad_blocks = 1;
  3916. atomic_inc(&rdev2->nr_pending);
  3917. } else
  3918. clear_bit(R5_WriteError, &dev->flags);
  3919. }
  3920. if (test_bit(R5_MadeGood, &dev->flags)) {
  3921. /* This flag does not apply to '.replacement'
  3922. * only to .rdev, so make sure to check that*/
  3923. struct md_rdev *rdev2 = rcu_dereference(
  3924. conf->disks[i].rdev);
  3925. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3926. s->handle_bad_blocks = 1;
  3927. atomic_inc(&rdev2->nr_pending);
  3928. } else
  3929. clear_bit(R5_MadeGood, &dev->flags);
  3930. }
  3931. if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
  3932. struct md_rdev *rdev2 = rcu_dereference(
  3933. conf->disks[i].replacement);
  3934. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  3935. s->handle_bad_blocks = 1;
  3936. atomic_inc(&rdev2->nr_pending);
  3937. } else
  3938. clear_bit(R5_MadeGoodRepl, &dev->flags);
  3939. }
  3940. if (!test_bit(R5_Insync, &dev->flags)) {
  3941. /* The ReadError flag will just be confusing now */
  3942. clear_bit(R5_ReadError, &dev->flags);
  3943. clear_bit(R5_ReWrite, &dev->flags);
  3944. }
  3945. if (test_bit(R5_ReadError, &dev->flags))
  3946. clear_bit(R5_Insync, &dev->flags);
  3947. if (!test_bit(R5_Insync, &dev->flags)) {
  3948. if (s->failed < 2)
  3949. s->failed_num[s->failed] = i;
  3950. s->failed++;
  3951. if (rdev && !test_bit(Faulty, &rdev->flags))
  3952. do_recovery = 1;
  3953. }
  3954. if (test_bit(R5_InJournal, &dev->flags))
  3955. s->injournal++;
  3956. if (test_bit(R5_InJournal, &dev->flags) && dev->written)
  3957. s->just_cached++;
  3958. }
  3959. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  3960. /* If there is a failed device being replaced,
  3961. * we must be recovering.
  3962. * else if we are after recovery_cp, we must be syncing
  3963. * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
  3964. * else we can only be replacing
  3965. * sync and recovery both need to read all devices, and so
  3966. * use the same flag.
  3967. */
  3968. if (do_recovery ||
  3969. sh->sector >= conf->mddev->recovery_cp ||
  3970. test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
  3971. s->syncing = 1;
  3972. else
  3973. s->replacing = 1;
  3974. }
  3975. rcu_read_unlock();
  3976. }
  3977. static int clear_batch_ready(struct stripe_head *sh)
  3978. {
  3979. /* Return '1' if this is a member of batch, or
  3980. * '0' if it is a lone stripe or a head which can now be
  3981. * handled.
  3982. */
  3983. struct stripe_head *tmp;
  3984. if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
  3985. return (sh->batch_head && sh->batch_head != sh);
  3986. spin_lock(&sh->stripe_lock);
  3987. if (!sh->batch_head) {
  3988. spin_unlock(&sh->stripe_lock);
  3989. return 0;
  3990. }
  3991. /*
  3992. * this stripe could be added to a batch list before we check
  3993. * BATCH_READY, skips it
  3994. */
  3995. if (sh->batch_head != sh) {
  3996. spin_unlock(&sh->stripe_lock);
  3997. return 1;
  3998. }
  3999. spin_lock(&sh->batch_lock);
  4000. list_for_each_entry(tmp, &sh->batch_list, batch_list)
  4001. clear_bit(STRIPE_BATCH_READY, &tmp->state);
  4002. spin_unlock(&sh->batch_lock);
  4003. spin_unlock(&sh->stripe_lock);
  4004. /*
  4005. * BATCH_READY is cleared, no new stripes can be added.
  4006. * batch_list can be accessed without lock
  4007. */
  4008. return 0;
  4009. }
  4010. static void break_stripe_batch_list(struct stripe_head *head_sh,
  4011. unsigned long handle_flags)
  4012. {
  4013. struct stripe_head *sh, *next;
  4014. int i;
  4015. int do_wakeup = 0;
  4016. list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
  4017. list_del_init(&sh->batch_list);
  4018. WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
  4019. (1 << STRIPE_SYNCING) |
  4020. (1 << STRIPE_REPLACED) |
  4021. (1 << STRIPE_DELAYED) |
  4022. (1 << STRIPE_BIT_DELAY) |
  4023. (1 << STRIPE_FULL_WRITE) |
  4024. (1 << STRIPE_BIOFILL_RUN) |
  4025. (1 << STRIPE_COMPUTE_RUN) |
  4026. (1 << STRIPE_OPS_REQ_PENDING) |
  4027. (1 << STRIPE_DISCARD) |
  4028. (1 << STRIPE_BATCH_READY) |
  4029. (1 << STRIPE_BATCH_ERR) |
  4030. (1 << STRIPE_BITMAP_PENDING)),
  4031. "stripe state: %lx\n", sh->state);
  4032. WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
  4033. (1 << STRIPE_REPLACED)),
  4034. "head stripe state: %lx\n", head_sh->state);
  4035. set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
  4036. (1 << STRIPE_PREREAD_ACTIVE) |
  4037. (1 << STRIPE_DEGRADED)),
  4038. head_sh->state & (1 << STRIPE_INSYNC));
  4039. sh->check_state = head_sh->check_state;
  4040. sh->reconstruct_state = head_sh->reconstruct_state;
  4041. for (i = 0; i < sh->disks; i++) {
  4042. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  4043. do_wakeup = 1;
  4044. sh->dev[i].flags = head_sh->dev[i].flags &
  4045. (~((1 << R5_WriteError) | (1 << R5_Overlap)));
  4046. }
  4047. spin_lock_irq(&sh->stripe_lock);
  4048. sh->batch_head = NULL;
  4049. spin_unlock_irq(&sh->stripe_lock);
  4050. if (handle_flags == 0 ||
  4051. sh->state & handle_flags)
  4052. set_bit(STRIPE_HANDLE, &sh->state);
  4053. raid5_release_stripe(sh);
  4054. }
  4055. spin_lock_irq(&head_sh->stripe_lock);
  4056. head_sh->batch_head = NULL;
  4057. spin_unlock_irq(&head_sh->stripe_lock);
  4058. for (i = 0; i < head_sh->disks; i++)
  4059. if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
  4060. do_wakeup = 1;
  4061. if (head_sh->state & handle_flags)
  4062. set_bit(STRIPE_HANDLE, &head_sh->state);
  4063. if (do_wakeup)
  4064. wake_up(&head_sh->raid_conf->wait_for_overlap);
  4065. }
  4066. static void handle_stripe(struct stripe_head *sh)
  4067. {
  4068. struct stripe_head_state s;
  4069. struct r5conf *conf = sh->raid_conf;
  4070. int i;
  4071. int prexor;
  4072. int disks = sh->disks;
  4073. struct r5dev *pdev, *qdev;
  4074. clear_bit(STRIPE_HANDLE, &sh->state);
  4075. if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
  4076. /* already being handled, ensure it gets handled
  4077. * again when current action finishes */
  4078. set_bit(STRIPE_HANDLE, &sh->state);
  4079. return;
  4080. }
  4081. if (clear_batch_ready(sh) ) {
  4082. clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
  4083. return;
  4084. }
  4085. if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
  4086. break_stripe_batch_list(sh, 0);
  4087. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
  4088. spin_lock(&sh->stripe_lock);
  4089. /* Cannot process 'sync' concurrently with 'discard' */
  4090. if (!test_bit(STRIPE_DISCARD, &sh->state) &&
  4091. test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
  4092. set_bit(STRIPE_SYNCING, &sh->state);
  4093. clear_bit(STRIPE_INSYNC, &sh->state);
  4094. clear_bit(STRIPE_REPLACED, &sh->state);
  4095. }
  4096. spin_unlock(&sh->stripe_lock);
  4097. }
  4098. clear_bit(STRIPE_DELAYED, &sh->state);
  4099. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  4100. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  4101. (unsigned long long)sh->sector, sh->state,
  4102. atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
  4103. sh->check_state, sh->reconstruct_state);
  4104. analyse_stripe(sh, &s);
  4105. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
  4106. goto finish;
  4107. if (s.handle_bad_blocks) {
  4108. set_bit(STRIPE_HANDLE, &sh->state);
  4109. goto finish;
  4110. }
  4111. if (unlikely(s.blocked_rdev)) {
  4112. if (s.syncing || s.expanding || s.expanded ||
  4113. s.replacing || s.to_write || s.written) {
  4114. set_bit(STRIPE_HANDLE, &sh->state);
  4115. goto finish;
  4116. }
  4117. /* There is nothing for the blocked_rdev to block */
  4118. rdev_dec_pending(s.blocked_rdev, conf->mddev);
  4119. s.blocked_rdev = NULL;
  4120. }
  4121. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  4122. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  4123. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  4124. }
  4125. pr_debug("locked=%d uptodate=%d to_read=%d"
  4126. " to_write=%d failed=%d failed_num=%d,%d\n",
  4127. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  4128. s.failed_num[0], s.failed_num[1]);
  4129. /* check if the array has lost more than max_degraded devices and,
  4130. * if so, some requests might need to be failed.
  4131. */
  4132. if (s.failed > conf->max_degraded || s.log_failed) {
  4133. sh->check_state = 0;
  4134. sh->reconstruct_state = 0;
  4135. break_stripe_batch_list(sh, 0);
  4136. if (s.to_read+s.to_write+s.written)
  4137. handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
  4138. if (s.syncing + s.replacing)
  4139. handle_failed_sync(conf, sh, &s);
  4140. }
  4141. /* Now we check to see if any write operations have recently
  4142. * completed
  4143. */
  4144. prexor = 0;
  4145. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  4146. prexor = 1;
  4147. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  4148. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  4149. sh->reconstruct_state = reconstruct_state_idle;
  4150. /* All the 'written' buffers and the parity block are ready to
  4151. * be written back to disk
  4152. */
  4153. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
  4154. !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
  4155. BUG_ON(sh->qd_idx >= 0 &&
  4156. !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
  4157. !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
  4158. for (i = disks; i--; ) {
  4159. struct r5dev *dev = &sh->dev[i];
  4160. if (test_bit(R5_LOCKED, &dev->flags) &&
  4161. (i == sh->pd_idx || i == sh->qd_idx ||
  4162. dev->written || test_bit(R5_InJournal,
  4163. &dev->flags))) {
  4164. pr_debug("Writing block %d\n", i);
  4165. set_bit(R5_Wantwrite, &dev->flags);
  4166. if (prexor)
  4167. continue;
  4168. if (s.failed > 1)
  4169. continue;
  4170. if (!test_bit(R5_Insync, &dev->flags) ||
  4171. ((i == sh->pd_idx || i == sh->qd_idx) &&
  4172. s.failed == 0))
  4173. set_bit(STRIPE_INSYNC, &sh->state);
  4174. }
  4175. }
  4176. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  4177. s.dec_preread_active = 1;
  4178. }
  4179. /*
  4180. * might be able to return some write requests if the parity blocks
  4181. * are safe, or on a failed drive
  4182. */
  4183. pdev = &sh->dev[sh->pd_idx];
  4184. s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
  4185. || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
  4186. qdev = &sh->dev[sh->qd_idx];
  4187. s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
  4188. || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
  4189. || conf->level < 6;
  4190. if (s.written &&
  4191. (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  4192. && !test_bit(R5_LOCKED, &pdev->flags)
  4193. && (test_bit(R5_UPTODATE, &pdev->flags) ||
  4194. test_bit(R5_Discard, &pdev->flags))))) &&
  4195. (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  4196. && !test_bit(R5_LOCKED, &qdev->flags)
  4197. && (test_bit(R5_UPTODATE, &qdev->flags) ||
  4198. test_bit(R5_Discard, &qdev->flags))))))
  4199. handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
  4200. if (s.just_cached)
  4201. r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi);
  4202. r5l_stripe_write_finished(sh);
  4203. /* Now we might consider reading some blocks, either to check/generate
  4204. * parity, or to satisfy requests
  4205. * or to load a block that is being partially written.
  4206. */
  4207. if (s.to_read || s.non_overwrite
  4208. || (conf->level == 6 && s.to_write && s.failed)
  4209. || (s.syncing && (s.uptodate + s.compute < disks))
  4210. || s.replacing
  4211. || s.expanding)
  4212. handle_stripe_fill(sh, &s, disks);
  4213. /*
  4214. * When the stripe finishes full journal write cycle (write to journal
  4215. * and raid disk), this is the clean up procedure so it is ready for
  4216. * next operation.
  4217. */
  4218. r5c_finish_stripe_write_out(conf, sh, &s);
  4219. /*
  4220. * Now to consider new write requests, cache write back and what else,
  4221. * if anything should be read. We do not handle new writes when:
  4222. * 1/ A 'write' operation (copy+xor) is already in flight.
  4223. * 2/ A 'check' operation is in flight, as it may clobber the parity
  4224. * block.
  4225. * 3/ A r5c cache log write is in flight.
  4226. */
  4227. if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
  4228. if (!r5c_is_writeback(conf->log)) {
  4229. if (s.to_write)
  4230. handle_stripe_dirtying(conf, sh, &s, disks);
  4231. } else { /* write back cache */
  4232. int ret = 0;
  4233. /* First, try handle writes in caching phase */
  4234. if (s.to_write)
  4235. ret = r5c_try_caching_write(conf, sh, &s,
  4236. disks);
  4237. /*
  4238. * If caching phase failed: ret == -EAGAIN
  4239. * OR
  4240. * stripe under reclaim: !caching && injournal
  4241. *
  4242. * fall back to handle_stripe_dirtying()
  4243. */
  4244. if (ret == -EAGAIN ||
  4245. /* stripe under reclaim: !caching && injournal */
  4246. (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
  4247. s.injournal > 0)) {
  4248. ret = handle_stripe_dirtying(conf, sh, &s,
  4249. disks);
  4250. if (ret == -EAGAIN)
  4251. goto finish;
  4252. }
  4253. }
  4254. }
  4255. /* maybe we need to check and possibly fix the parity for this stripe
  4256. * Any reads will already have been scheduled, so we just see if enough
  4257. * data is available. The parity check is held off while parity
  4258. * dependent operations are in flight.
  4259. */
  4260. if (sh->check_state ||
  4261. (s.syncing && s.locked == 0 &&
  4262. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  4263. !test_bit(STRIPE_INSYNC, &sh->state))) {
  4264. if (conf->level == 6)
  4265. handle_parity_checks6(conf, sh, &s, disks);
  4266. else
  4267. handle_parity_checks5(conf, sh, &s, disks);
  4268. }
  4269. if ((s.replacing || s.syncing) && s.locked == 0
  4270. && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
  4271. && !test_bit(STRIPE_REPLACED, &sh->state)) {
  4272. /* Write out to replacement devices where possible */
  4273. for (i = 0; i < conf->raid_disks; i++)
  4274. if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
  4275. WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
  4276. set_bit(R5_WantReplace, &sh->dev[i].flags);
  4277. set_bit(R5_LOCKED, &sh->dev[i].flags);
  4278. s.locked++;
  4279. }
  4280. if (s.replacing)
  4281. set_bit(STRIPE_INSYNC, &sh->state);
  4282. set_bit(STRIPE_REPLACED, &sh->state);
  4283. }
  4284. if ((s.syncing || s.replacing) && s.locked == 0 &&
  4285. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  4286. test_bit(STRIPE_INSYNC, &sh->state)) {
  4287. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  4288. clear_bit(STRIPE_SYNCING, &sh->state);
  4289. if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
  4290. wake_up(&conf->wait_for_overlap);
  4291. }
  4292. /* If the failed drives are just a ReadError, then we might need
  4293. * to progress the repair/check process
  4294. */
  4295. if (s.failed <= conf->max_degraded && !conf->mddev->ro)
  4296. for (i = 0; i < s.failed; i++) {
  4297. struct r5dev *dev = &sh->dev[s.failed_num[i]];
  4298. if (test_bit(R5_ReadError, &dev->flags)
  4299. && !test_bit(R5_LOCKED, &dev->flags)
  4300. && test_bit(R5_UPTODATE, &dev->flags)
  4301. ) {
  4302. if (!test_bit(R5_ReWrite, &dev->flags)) {
  4303. set_bit(R5_Wantwrite, &dev->flags);
  4304. set_bit(R5_ReWrite, &dev->flags);
  4305. set_bit(R5_LOCKED, &dev->flags);
  4306. s.locked++;
  4307. } else {
  4308. /* let's read it back */
  4309. set_bit(R5_Wantread, &dev->flags);
  4310. set_bit(R5_LOCKED, &dev->flags);
  4311. s.locked++;
  4312. }
  4313. }
  4314. }
  4315. /* Finish reconstruct operations initiated by the expansion process */
  4316. if (sh->reconstruct_state == reconstruct_state_result) {
  4317. struct stripe_head *sh_src
  4318. = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
  4319. if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
  4320. /* sh cannot be written until sh_src has been read.
  4321. * so arrange for sh to be delayed a little
  4322. */
  4323. set_bit(STRIPE_DELAYED, &sh->state);
  4324. set_bit(STRIPE_HANDLE, &sh->state);
  4325. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  4326. &sh_src->state))
  4327. atomic_inc(&conf->preread_active_stripes);
  4328. raid5_release_stripe(sh_src);
  4329. goto finish;
  4330. }
  4331. if (sh_src)
  4332. raid5_release_stripe(sh_src);
  4333. sh->reconstruct_state = reconstruct_state_idle;
  4334. clear_bit(STRIPE_EXPANDING, &sh->state);
  4335. for (i = conf->raid_disks; i--; ) {
  4336. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  4337. set_bit(R5_LOCKED, &sh->dev[i].flags);
  4338. s.locked++;
  4339. }
  4340. }
  4341. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  4342. !sh->reconstruct_state) {
  4343. /* Need to write out all blocks after computing parity */
  4344. sh->disks = conf->raid_disks;
  4345. stripe_set_idx(sh->sector, conf, 0, sh);
  4346. schedule_reconstruction(sh, &s, 1, 1);
  4347. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  4348. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  4349. atomic_dec(&conf->reshape_stripes);
  4350. wake_up(&conf->wait_for_overlap);
  4351. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  4352. }
  4353. if (s.expanding && s.locked == 0 &&
  4354. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  4355. handle_stripe_expansion(conf, sh);
  4356. finish:
  4357. /* wait for this device to become unblocked */
  4358. if (unlikely(s.blocked_rdev)) {
  4359. if (conf->mddev->external)
  4360. md_wait_for_blocked_rdev(s.blocked_rdev,
  4361. conf->mddev);
  4362. else
  4363. /* Internal metadata will immediately
  4364. * be written by raid5d, so we don't
  4365. * need to wait here.
  4366. */
  4367. rdev_dec_pending(s.blocked_rdev,
  4368. conf->mddev);
  4369. }
  4370. if (s.handle_bad_blocks)
  4371. for (i = disks; i--; ) {
  4372. struct md_rdev *rdev;
  4373. struct r5dev *dev = &sh->dev[i];
  4374. if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
  4375. /* We own a safe reference to the rdev */
  4376. rdev = conf->disks[i].rdev;
  4377. if (!rdev_set_badblocks(rdev, sh->sector,
  4378. STRIPE_SECTORS, 0))
  4379. md_error(conf->mddev, rdev);
  4380. rdev_dec_pending(rdev, conf->mddev);
  4381. }
  4382. if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
  4383. rdev = conf->disks[i].rdev;
  4384. rdev_clear_badblocks(rdev, sh->sector,
  4385. STRIPE_SECTORS, 0);
  4386. rdev_dec_pending(rdev, conf->mddev);
  4387. }
  4388. if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
  4389. rdev = conf->disks[i].replacement;
  4390. if (!rdev)
  4391. /* rdev have been moved down */
  4392. rdev = conf->disks[i].rdev;
  4393. rdev_clear_badblocks(rdev, sh->sector,
  4394. STRIPE_SECTORS, 0);
  4395. rdev_dec_pending(rdev, conf->mddev);
  4396. }
  4397. }
  4398. if (s.ops_request)
  4399. raid_run_ops(sh, s.ops_request);
  4400. ops_run_io(sh, &s);
  4401. if (s.dec_preread_active) {
  4402. /* We delay this until after ops_run_io so that if make_request
  4403. * is waiting on a flush, it won't continue until the writes
  4404. * have actually been submitted.
  4405. */
  4406. atomic_dec(&conf->preread_active_stripes);
  4407. if (atomic_read(&conf->preread_active_stripes) <
  4408. IO_THRESHOLD)
  4409. md_wakeup_thread(conf->mddev->thread);
  4410. }
  4411. if (!bio_list_empty(&s.return_bi)) {
  4412. if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
  4413. spin_lock_irq(&conf->device_lock);
  4414. bio_list_merge(&conf->return_bi, &s.return_bi);
  4415. spin_unlock_irq(&conf->device_lock);
  4416. md_wakeup_thread(conf->mddev->thread);
  4417. } else
  4418. return_io(&s.return_bi);
  4419. }
  4420. clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
  4421. }
  4422. static void raid5_activate_delayed(struct r5conf *conf)
  4423. {
  4424. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  4425. while (!list_empty(&conf->delayed_list)) {
  4426. struct list_head *l = conf->delayed_list.next;
  4427. struct stripe_head *sh;
  4428. sh = list_entry(l, struct stripe_head, lru);
  4429. list_del_init(l);
  4430. clear_bit(STRIPE_DELAYED, &sh->state);
  4431. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  4432. atomic_inc(&conf->preread_active_stripes);
  4433. list_add_tail(&sh->lru, &conf->hold_list);
  4434. raid5_wakeup_stripe_thread(sh);
  4435. }
  4436. }
  4437. }
  4438. static void activate_bit_delay(struct r5conf *conf,
  4439. struct list_head *temp_inactive_list)
  4440. {
  4441. /* device_lock is held */
  4442. struct list_head head;
  4443. list_add(&head, &conf->bitmap_list);
  4444. list_del_init(&conf->bitmap_list);
  4445. while (!list_empty(&head)) {
  4446. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  4447. int hash;
  4448. list_del_init(&sh->lru);
  4449. atomic_inc(&sh->count);
  4450. hash = sh->hash_lock_index;
  4451. __release_stripe(conf, sh, &temp_inactive_list[hash]);
  4452. }
  4453. }
  4454. static int raid5_congested(struct mddev *mddev, int bits)
  4455. {
  4456. struct r5conf *conf = mddev->private;
  4457. /* No difference between reads and writes. Just check
  4458. * how busy the stripe_cache is
  4459. */
  4460. if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
  4461. return 1;
  4462. /* Also checks whether there is pressure on r5cache log space */
  4463. if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
  4464. return 1;
  4465. if (conf->quiesce)
  4466. return 1;
  4467. if (atomic_read(&conf->empty_inactive_list_nr))
  4468. return 1;
  4469. return 0;
  4470. }
  4471. static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
  4472. {
  4473. struct r5conf *conf = mddev->private;
  4474. sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
  4475. unsigned int chunk_sectors;
  4476. unsigned int bio_sectors = bio_sectors(bio);
  4477. chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
  4478. return chunk_sectors >=
  4479. ((sector & (chunk_sectors - 1)) + bio_sectors);
  4480. }
  4481. /*
  4482. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  4483. * later sampled by raid5d.
  4484. */
  4485. static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
  4486. {
  4487. unsigned long flags;
  4488. spin_lock_irqsave(&conf->device_lock, flags);
  4489. bi->bi_next = conf->retry_read_aligned_list;
  4490. conf->retry_read_aligned_list = bi;
  4491. spin_unlock_irqrestore(&conf->device_lock, flags);
  4492. md_wakeup_thread(conf->mddev->thread);
  4493. }
  4494. static struct bio *remove_bio_from_retry(struct r5conf *conf)
  4495. {
  4496. struct bio *bi;
  4497. bi = conf->retry_read_aligned;
  4498. if (bi) {
  4499. conf->retry_read_aligned = NULL;
  4500. return bi;
  4501. }
  4502. bi = conf->retry_read_aligned_list;
  4503. if(bi) {
  4504. conf->retry_read_aligned_list = bi->bi_next;
  4505. bi->bi_next = NULL;
  4506. /*
  4507. * this sets the active strip count to 1 and the processed
  4508. * strip count to zero (upper 8 bits)
  4509. */
  4510. raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
  4511. }
  4512. return bi;
  4513. }
  4514. /*
  4515. * The "raid5_align_endio" should check if the read succeeded and if it
  4516. * did, call bio_endio on the original bio (having bio_put the new bio
  4517. * first).
  4518. * If the read failed..
  4519. */
  4520. static void raid5_align_endio(struct bio *bi)
  4521. {
  4522. struct bio* raid_bi = bi->bi_private;
  4523. struct mddev *mddev;
  4524. struct r5conf *conf;
  4525. struct md_rdev *rdev;
  4526. int error = bi->bi_error;
  4527. bio_put(bi);
  4528. rdev = (void*)raid_bi->bi_next;
  4529. raid_bi->bi_next = NULL;
  4530. mddev = rdev->mddev;
  4531. conf = mddev->private;
  4532. rdev_dec_pending(rdev, conf->mddev);
  4533. if (!error) {
  4534. bio_endio(raid_bi);
  4535. if (atomic_dec_and_test(&conf->active_aligned_reads))
  4536. wake_up(&conf->wait_for_quiescent);
  4537. return;
  4538. }
  4539. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  4540. add_bio_to_retry(raid_bi, conf);
  4541. }
  4542. static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
  4543. {
  4544. struct r5conf *conf = mddev->private;
  4545. int dd_idx;
  4546. struct bio* align_bi;
  4547. struct md_rdev *rdev;
  4548. sector_t end_sector;
  4549. if (!in_chunk_boundary(mddev, raid_bio)) {
  4550. pr_debug("%s: non aligned\n", __func__);
  4551. return 0;
  4552. }
  4553. /*
  4554. * use bio_clone_fast to make a copy of the bio
  4555. */
  4556. align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set);
  4557. if (!align_bi)
  4558. return 0;
  4559. /*
  4560. * set bi_end_io to a new function, and set bi_private to the
  4561. * original bio.
  4562. */
  4563. align_bi->bi_end_io = raid5_align_endio;
  4564. align_bi->bi_private = raid_bio;
  4565. /*
  4566. * compute position
  4567. */
  4568. align_bi->bi_iter.bi_sector =
  4569. raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
  4570. 0, &dd_idx, NULL);
  4571. end_sector = bio_end_sector(align_bi);
  4572. rcu_read_lock();
  4573. rdev = rcu_dereference(conf->disks[dd_idx].replacement);
  4574. if (!rdev || test_bit(Faulty, &rdev->flags) ||
  4575. rdev->recovery_offset < end_sector) {
  4576. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  4577. if (rdev &&
  4578. (test_bit(Faulty, &rdev->flags) ||
  4579. !(test_bit(In_sync, &rdev->flags) ||
  4580. rdev->recovery_offset >= end_sector)))
  4581. rdev = NULL;
  4582. }
  4583. if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
  4584. rcu_read_unlock();
  4585. bio_put(align_bi);
  4586. return 0;
  4587. }
  4588. if (rdev) {
  4589. sector_t first_bad;
  4590. int bad_sectors;
  4591. atomic_inc(&rdev->nr_pending);
  4592. rcu_read_unlock();
  4593. raid_bio->bi_next = (void*)rdev;
  4594. align_bi->bi_bdev = rdev->bdev;
  4595. bio_clear_flag(align_bi, BIO_SEG_VALID);
  4596. if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
  4597. bio_sectors(align_bi),
  4598. &first_bad, &bad_sectors)) {
  4599. bio_put(align_bi);
  4600. rdev_dec_pending(rdev, mddev);
  4601. return 0;
  4602. }
  4603. /* No reshape active, so we can trust rdev->data_offset */
  4604. align_bi->bi_iter.bi_sector += rdev->data_offset;
  4605. spin_lock_irq(&conf->device_lock);
  4606. wait_event_lock_irq(conf->wait_for_quiescent,
  4607. conf->quiesce == 0,
  4608. conf->device_lock);
  4609. atomic_inc(&conf->active_aligned_reads);
  4610. spin_unlock_irq(&conf->device_lock);
  4611. if (mddev->gendisk)
  4612. trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
  4613. align_bi, disk_devt(mddev->gendisk),
  4614. raid_bio->bi_iter.bi_sector);
  4615. generic_make_request(align_bi);
  4616. return 1;
  4617. } else {
  4618. rcu_read_unlock();
  4619. bio_put(align_bi);
  4620. return 0;
  4621. }
  4622. }
  4623. static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
  4624. {
  4625. struct bio *split;
  4626. do {
  4627. sector_t sector = raid_bio->bi_iter.bi_sector;
  4628. unsigned chunk_sects = mddev->chunk_sectors;
  4629. unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
  4630. if (sectors < bio_sectors(raid_bio)) {
  4631. split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
  4632. bio_chain(split, raid_bio);
  4633. } else
  4634. split = raid_bio;
  4635. if (!raid5_read_one_chunk(mddev, split)) {
  4636. if (split != raid_bio)
  4637. generic_make_request(raid_bio);
  4638. return split;
  4639. }
  4640. } while (split != raid_bio);
  4641. return NULL;
  4642. }
  4643. /* __get_priority_stripe - get the next stripe to process
  4644. *
  4645. * Full stripe writes are allowed to pass preread active stripes up until
  4646. * the bypass_threshold is exceeded. In general the bypass_count
  4647. * increments when the handle_list is handled before the hold_list; however, it
  4648. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  4649. * stripe with in flight i/o. The bypass_count will be reset when the
  4650. * head of the hold_list has changed, i.e. the head was promoted to the
  4651. * handle_list.
  4652. */
  4653. static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
  4654. {
  4655. struct stripe_head *sh = NULL, *tmp;
  4656. struct list_head *handle_list = NULL;
  4657. struct r5worker_group *wg = NULL;
  4658. if (conf->worker_cnt_per_group == 0) {
  4659. handle_list = &conf->handle_list;
  4660. } else if (group != ANY_GROUP) {
  4661. handle_list = &conf->worker_groups[group].handle_list;
  4662. wg = &conf->worker_groups[group];
  4663. } else {
  4664. int i;
  4665. for (i = 0; i < conf->group_cnt; i++) {
  4666. handle_list = &conf->worker_groups[i].handle_list;
  4667. wg = &conf->worker_groups[i];
  4668. if (!list_empty(handle_list))
  4669. break;
  4670. }
  4671. }
  4672. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  4673. __func__,
  4674. list_empty(handle_list) ? "empty" : "busy",
  4675. list_empty(&conf->hold_list) ? "empty" : "busy",
  4676. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  4677. if (!list_empty(handle_list)) {
  4678. sh = list_entry(handle_list->next, typeof(*sh), lru);
  4679. if (list_empty(&conf->hold_list))
  4680. conf->bypass_count = 0;
  4681. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  4682. if (conf->hold_list.next == conf->last_hold)
  4683. conf->bypass_count++;
  4684. else {
  4685. conf->last_hold = conf->hold_list.next;
  4686. conf->bypass_count -= conf->bypass_threshold;
  4687. if (conf->bypass_count < 0)
  4688. conf->bypass_count = 0;
  4689. }
  4690. }
  4691. } else if (!list_empty(&conf->hold_list) &&
  4692. ((conf->bypass_threshold &&
  4693. conf->bypass_count > conf->bypass_threshold) ||
  4694. atomic_read(&conf->pending_full_writes) == 0)) {
  4695. list_for_each_entry(tmp, &conf->hold_list, lru) {
  4696. if (conf->worker_cnt_per_group == 0 ||
  4697. group == ANY_GROUP ||
  4698. !cpu_online(tmp->cpu) ||
  4699. cpu_to_group(tmp->cpu) == group) {
  4700. sh = tmp;
  4701. break;
  4702. }
  4703. }
  4704. if (sh) {
  4705. conf->bypass_count -= conf->bypass_threshold;
  4706. if (conf->bypass_count < 0)
  4707. conf->bypass_count = 0;
  4708. }
  4709. wg = NULL;
  4710. }
  4711. if (!sh)
  4712. return NULL;
  4713. if (wg) {
  4714. wg->stripes_cnt--;
  4715. sh->group = NULL;
  4716. }
  4717. list_del_init(&sh->lru);
  4718. BUG_ON(atomic_inc_return(&sh->count) != 1);
  4719. return sh;
  4720. }
  4721. struct raid5_plug_cb {
  4722. struct blk_plug_cb cb;
  4723. struct list_head list;
  4724. struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
  4725. };
  4726. static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
  4727. {
  4728. struct raid5_plug_cb *cb = container_of(
  4729. blk_cb, struct raid5_plug_cb, cb);
  4730. struct stripe_head *sh;
  4731. struct mddev *mddev = cb->cb.data;
  4732. struct r5conf *conf = mddev->private;
  4733. int cnt = 0;
  4734. int hash;
  4735. if (cb->list.next && !list_empty(&cb->list)) {
  4736. spin_lock_irq(&conf->device_lock);
  4737. while (!list_empty(&cb->list)) {
  4738. sh = list_first_entry(&cb->list, struct stripe_head, lru);
  4739. list_del_init(&sh->lru);
  4740. /*
  4741. * avoid race release_stripe_plug() sees
  4742. * STRIPE_ON_UNPLUG_LIST clear but the stripe
  4743. * is still in our list
  4744. */
  4745. smp_mb__before_atomic();
  4746. clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
  4747. /*
  4748. * STRIPE_ON_RELEASE_LIST could be set here. In that
  4749. * case, the count is always > 1 here
  4750. */
  4751. hash = sh->hash_lock_index;
  4752. __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
  4753. cnt++;
  4754. }
  4755. spin_unlock_irq(&conf->device_lock);
  4756. }
  4757. release_inactive_stripe_list(conf, cb->temp_inactive_list,
  4758. NR_STRIPE_HASH_LOCKS);
  4759. if (mddev->queue)
  4760. trace_block_unplug(mddev->queue, cnt, !from_schedule);
  4761. kfree(cb);
  4762. }
  4763. static void release_stripe_plug(struct mddev *mddev,
  4764. struct stripe_head *sh)
  4765. {
  4766. struct blk_plug_cb *blk_cb = blk_check_plugged(
  4767. raid5_unplug, mddev,
  4768. sizeof(struct raid5_plug_cb));
  4769. struct raid5_plug_cb *cb;
  4770. if (!blk_cb) {
  4771. raid5_release_stripe(sh);
  4772. return;
  4773. }
  4774. cb = container_of(blk_cb, struct raid5_plug_cb, cb);
  4775. if (cb->list.next == NULL) {
  4776. int i;
  4777. INIT_LIST_HEAD(&cb->list);
  4778. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  4779. INIT_LIST_HEAD(cb->temp_inactive_list + i);
  4780. }
  4781. if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
  4782. list_add_tail(&sh->lru, &cb->list);
  4783. else
  4784. raid5_release_stripe(sh);
  4785. }
  4786. static void make_discard_request(struct mddev *mddev, struct bio *bi)
  4787. {
  4788. struct r5conf *conf = mddev->private;
  4789. sector_t logical_sector, last_sector;
  4790. struct stripe_head *sh;
  4791. int remaining;
  4792. int stripe_sectors;
  4793. if (mddev->reshape_position != MaxSector)
  4794. /* Skip discard while reshape is happening */
  4795. return;
  4796. logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  4797. last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
  4798. bi->bi_next = NULL;
  4799. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  4800. stripe_sectors = conf->chunk_sectors *
  4801. (conf->raid_disks - conf->max_degraded);
  4802. logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
  4803. stripe_sectors);
  4804. sector_div(last_sector, stripe_sectors);
  4805. logical_sector *= conf->chunk_sectors;
  4806. last_sector *= conf->chunk_sectors;
  4807. for (; logical_sector < last_sector;
  4808. logical_sector += STRIPE_SECTORS) {
  4809. DEFINE_WAIT(w);
  4810. int d;
  4811. again:
  4812. sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
  4813. prepare_to_wait(&conf->wait_for_overlap, &w,
  4814. TASK_UNINTERRUPTIBLE);
  4815. set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
  4816. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  4817. raid5_release_stripe(sh);
  4818. schedule();
  4819. goto again;
  4820. }
  4821. clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
  4822. spin_lock_irq(&sh->stripe_lock);
  4823. for (d = 0; d < conf->raid_disks; d++) {
  4824. if (d == sh->pd_idx || d == sh->qd_idx)
  4825. continue;
  4826. if (sh->dev[d].towrite || sh->dev[d].toread) {
  4827. set_bit(R5_Overlap, &sh->dev[d].flags);
  4828. spin_unlock_irq(&sh->stripe_lock);
  4829. raid5_release_stripe(sh);
  4830. schedule();
  4831. goto again;
  4832. }
  4833. }
  4834. set_bit(STRIPE_DISCARD, &sh->state);
  4835. finish_wait(&conf->wait_for_overlap, &w);
  4836. sh->overwrite_disks = 0;
  4837. for (d = 0; d < conf->raid_disks; d++) {
  4838. if (d == sh->pd_idx || d == sh->qd_idx)
  4839. continue;
  4840. sh->dev[d].towrite = bi;
  4841. set_bit(R5_OVERWRITE, &sh->dev[d].flags);
  4842. raid5_inc_bi_active_stripes(bi);
  4843. sh->overwrite_disks++;
  4844. }
  4845. spin_unlock_irq(&sh->stripe_lock);
  4846. if (conf->mddev->bitmap) {
  4847. for (d = 0;
  4848. d < conf->raid_disks - conf->max_degraded;
  4849. d++)
  4850. bitmap_startwrite(mddev->bitmap,
  4851. sh->sector,
  4852. STRIPE_SECTORS,
  4853. 0);
  4854. sh->bm_seq = conf->seq_flush + 1;
  4855. set_bit(STRIPE_BIT_DELAY, &sh->state);
  4856. }
  4857. set_bit(STRIPE_HANDLE, &sh->state);
  4858. clear_bit(STRIPE_DELAYED, &sh->state);
  4859. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  4860. atomic_inc(&conf->preread_active_stripes);
  4861. release_stripe_plug(mddev, sh);
  4862. }
  4863. remaining = raid5_dec_bi_active_stripes(bi);
  4864. if (remaining == 0) {
  4865. md_write_end(mddev);
  4866. bio_endio(bi);
  4867. }
  4868. }
  4869. static void raid5_make_request(struct mddev *mddev, struct bio * bi)
  4870. {
  4871. struct r5conf *conf = mddev->private;
  4872. int dd_idx;
  4873. sector_t new_sector;
  4874. sector_t logical_sector, last_sector;
  4875. struct stripe_head *sh;
  4876. const int rw = bio_data_dir(bi);
  4877. int remaining;
  4878. DEFINE_WAIT(w);
  4879. bool do_prepare;
  4880. bool do_flush = false;
  4881. if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
  4882. int ret = r5l_handle_flush_request(conf->log, bi);
  4883. if (ret == 0)
  4884. return;
  4885. if (ret == -ENODEV) {
  4886. md_flush_request(mddev, bi);
  4887. return;
  4888. }
  4889. /* ret == -EAGAIN, fallback */
  4890. /*
  4891. * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
  4892. * we need to flush journal device
  4893. */
  4894. do_flush = bi->bi_opf & REQ_PREFLUSH;
  4895. }
  4896. md_write_start(mddev, bi);
  4897. /*
  4898. * If array is degraded, better not do chunk aligned read because
  4899. * later we might have to read it again in order to reconstruct
  4900. * data on failed drives.
  4901. */
  4902. if (rw == READ && mddev->degraded == 0 &&
  4903. mddev->reshape_position == MaxSector) {
  4904. bi = chunk_aligned_read(mddev, bi);
  4905. if (!bi)
  4906. return;
  4907. }
  4908. if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
  4909. make_discard_request(mddev, bi);
  4910. return;
  4911. }
  4912. logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  4913. last_sector = bio_end_sector(bi);
  4914. bi->bi_next = NULL;
  4915. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  4916. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  4917. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  4918. int previous;
  4919. int seq;
  4920. do_prepare = false;
  4921. retry:
  4922. seq = read_seqcount_begin(&conf->gen_lock);
  4923. previous = 0;
  4924. if (do_prepare)
  4925. prepare_to_wait(&conf->wait_for_overlap, &w,
  4926. TASK_UNINTERRUPTIBLE);
  4927. if (unlikely(conf->reshape_progress != MaxSector)) {
  4928. /* spinlock is needed as reshape_progress may be
  4929. * 64bit on a 32bit platform, and so it might be
  4930. * possible to see a half-updated value
  4931. * Of course reshape_progress could change after
  4932. * the lock is dropped, so once we get a reference
  4933. * to the stripe that we think it is, we will have
  4934. * to check again.
  4935. */
  4936. spin_lock_irq(&conf->device_lock);
  4937. if (mddev->reshape_backwards
  4938. ? logical_sector < conf->reshape_progress
  4939. : logical_sector >= conf->reshape_progress) {
  4940. previous = 1;
  4941. } else {
  4942. if (mddev->reshape_backwards
  4943. ? logical_sector < conf->reshape_safe
  4944. : logical_sector >= conf->reshape_safe) {
  4945. spin_unlock_irq(&conf->device_lock);
  4946. schedule();
  4947. do_prepare = true;
  4948. goto retry;
  4949. }
  4950. }
  4951. spin_unlock_irq(&conf->device_lock);
  4952. }
  4953. new_sector = raid5_compute_sector(conf, logical_sector,
  4954. previous,
  4955. &dd_idx, NULL);
  4956. pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
  4957. (unsigned long long)new_sector,
  4958. (unsigned long long)logical_sector);
  4959. sh = raid5_get_active_stripe(conf, new_sector, previous,
  4960. (bi->bi_opf & REQ_RAHEAD), 0);
  4961. if (sh) {
  4962. if (unlikely(previous)) {
  4963. /* expansion might have moved on while waiting for a
  4964. * stripe, so we must do the range check again.
  4965. * Expansion could still move past after this
  4966. * test, but as we are holding a reference to
  4967. * 'sh', we know that if that happens,
  4968. * STRIPE_EXPANDING will get set and the expansion
  4969. * won't proceed until we finish with the stripe.
  4970. */
  4971. int must_retry = 0;
  4972. spin_lock_irq(&conf->device_lock);
  4973. if (mddev->reshape_backwards
  4974. ? logical_sector >= conf->reshape_progress
  4975. : logical_sector < conf->reshape_progress)
  4976. /* mismatch, need to try again */
  4977. must_retry = 1;
  4978. spin_unlock_irq(&conf->device_lock);
  4979. if (must_retry) {
  4980. raid5_release_stripe(sh);
  4981. schedule();
  4982. do_prepare = true;
  4983. goto retry;
  4984. }
  4985. }
  4986. if (read_seqcount_retry(&conf->gen_lock, seq)) {
  4987. /* Might have got the wrong stripe_head
  4988. * by accident
  4989. */
  4990. raid5_release_stripe(sh);
  4991. goto retry;
  4992. }
  4993. if (rw == WRITE &&
  4994. logical_sector >= mddev->suspend_lo &&
  4995. logical_sector < mddev->suspend_hi) {
  4996. raid5_release_stripe(sh);
  4997. /* As the suspend_* range is controlled by
  4998. * userspace, we want an interruptible
  4999. * wait.
  5000. */
  5001. flush_signals(current);
  5002. prepare_to_wait(&conf->wait_for_overlap,
  5003. &w, TASK_INTERRUPTIBLE);
  5004. if (logical_sector >= mddev->suspend_lo &&
  5005. logical_sector < mddev->suspend_hi) {
  5006. schedule();
  5007. do_prepare = true;
  5008. }
  5009. goto retry;
  5010. }
  5011. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  5012. !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
  5013. /* Stripe is busy expanding or
  5014. * add failed due to overlap. Flush everything
  5015. * and wait a while
  5016. */
  5017. md_wakeup_thread(mddev->thread);
  5018. raid5_release_stripe(sh);
  5019. schedule();
  5020. do_prepare = true;
  5021. goto retry;
  5022. }
  5023. if (do_flush) {
  5024. set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
  5025. /* we only need flush for one stripe */
  5026. do_flush = false;
  5027. }
  5028. set_bit(STRIPE_HANDLE, &sh->state);
  5029. clear_bit(STRIPE_DELAYED, &sh->state);
  5030. if ((!sh->batch_head || sh == sh->batch_head) &&
  5031. (bi->bi_opf & REQ_SYNC) &&
  5032. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  5033. atomic_inc(&conf->preread_active_stripes);
  5034. release_stripe_plug(mddev, sh);
  5035. } else {
  5036. /* cannot get stripe for read-ahead, just give-up */
  5037. bi->bi_error = -EIO;
  5038. break;
  5039. }
  5040. }
  5041. finish_wait(&conf->wait_for_overlap, &w);
  5042. remaining = raid5_dec_bi_active_stripes(bi);
  5043. if (remaining == 0) {
  5044. if ( rw == WRITE )
  5045. md_write_end(mddev);
  5046. trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
  5047. bi, 0);
  5048. bio_endio(bi);
  5049. }
  5050. }
  5051. static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
  5052. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
  5053. {
  5054. /* reshaping is quite different to recovery/resync so it is
  5055. * handled quite separately ... here.
  5056. *
  5057. * On each call to sync_request, we gather one chunk worth of
  5058. * destination stripes and flag them as expanding.
  5059. * Then we find all the source stripes and request reads.
  5060. * As the reads complete, handle_stripe will copy the data
  5061. * into the destination stripe and release that stripe.
  5062. */
  5063. struct r5conf *conf = mddev->private;
  5064. struct stripe_head *sh;
  5065. sector_t first_sector, last_sector;
  5066. int raid_disks = conf->previous_raid_disks;
  5067. int data_disks = raid_disks - conf->max_degraded;
  5068. int new_data_disks = conf->raid_disks - conf->max_degraded;
  5069. int i;
  5070. int dd_idx;
  5071. sector_t writepos, readpos, safepos;
  5072. sector_t stripe_addr;
  5073. int reshape_sectors;
  5074. struct list_head stripes;
  5075. sector_t retn;
  5076. if (sector_nr == 0) {
  5077. /* If restarting in the middle, skip the initial sectors */
  5078. if (mddev->reshape_backwards &&
  5079. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  5080. sector_nr = raid5_size(mddev, 0, 0)
  5081. - conf->reshape_progress;
  5082. } else if (mddev->reshape_backwards &&
  5083. conf->reshape_progress == MaxSector) {
  5084. /* shouldn't happen, but just in case, finish up.*/
  5085. sector_nr = MaxSector;
  5086. } else if (!mddev->reshape_backwards &&
  5087. conf->reshape_progress > 0)
  5088. sector_nr = conf->reshape_progress;
  5089. sector_div(sector_nr, new_data_disks);
  5090. if (sector_nr) {
  5091. mddev->curr_resync_completed = sector_nr;
  5092. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  5093. *skipped = 1;
  5094. retn = sector_nr;
  5095. goto finish;
  5096. }
  5097. }
  5098. /* We need to process a full chunk at a time.
  5099. * If old and new chunk sizes differ, we need to process the
  5100. * largest of these
  5101. */
  5102. reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
  5103. /* We update the metadata at least every 10 seconds, or when
  5104. * the data about to be copied would over-write the source of
  5105. * the data at the front of the range. i.e. one new_stripe
  5106. * along from reshape_progress new_maps to after where
  5107. * reshape_safe old_maps to
  5108. */
  5109. writepos = conf->reshape_progress;
  5110. sector_div(writepos, new_data_disks);
  5111. readpos = conf->reshape_progress;
  5112. sector_div(readpos, data_disks);
  5113. safepos = conf->reshape_safe;
  5114. sector_div(safepos, data_disks);
  5115. if (mddev->reshape_backwards) {
  5116. BUG_ON(writepos < reshape_sectors);
  5117. writepos -= reshape_sectors;
  5118. readpos += reshape_sectors;
  5119. safepos += reshape_sectors;
  5120. } else {
  5121. writepos += reshape_sectors;
  5122. /* readpos and safepos are worst-case calculations.
  5123. * A negative number is overly pessimistic, and causes
  5124. * obvious problems for unsigned storage. So clip to 0.
  5125. */
  5126. readpos -= min_t(sector_t, reshape_sectors, readpos);
  5127. safepos -= min_t(sector_t, reshape_sectors, safepos);
  5128. }
  5129. /* Having calculated the 'writepos' possibly use it
  5130. * to set 'stripe_addr' which is where we will write to.
  5131. */
  5132. if (mddev->reshape_backwards) {
  5133. BUG_ON(conf->reshape_progress == 0);
  5134. stripe_addr = writepos;
  5135. BUG_ON((mddev->dev_sectors &
  5136. ~((sector_t)reshape_sectors - 1))
  5137. - reshape_sectors - stripe_addr
  5138. != sector_nr);
  5139. } else {
  5140. BUG_ON(writepos != sector_nr + reshape_sectors);
  5141. stripe_addr = sector_nr;
  5142. }
  5143. /* 'writepos' is the most advanced device address we might write.
  5144. * 'readpos' is the least advanced device address we might read.
  5145. * 'safepos' is the least address recorded in the metadata as having
  5146. * been reshaped.
  5147. * If there is a min_offset_diff, these are adjusted either by
  5148. * increasing the safepos/readpos if diff is negative, or
  5149. * increasing writepos if diff is positive.
  5150. * If 'readpos' is then behind 'writepos', there is no way that we can
  5151. * ensure safety in the face of a crash - that must be done by userspace
  5152. * making a backup of the data. So in that case there is no particular
  5153. * rush to update metadata.
  5154. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  5155. * update the metadata to advance 'safepos' to match 'readpos' so that
  5156. * we can be safe in the event of a crash.
  5157. * So we insist on updating metadata if safepos is behind writepos and
  5158. * readpos is beyond writepos.
  5159. * In any case, update the metadata every 10 seconds.
  5160. * Maybe that number should be configurable, but I'm not sure it is
  5161. * worth it.... maybe it could be a multiple of safemode_delay???
  5162. */
  5163. if (conf->min_offset_diff < 0) {
  5164. safepos += -conf->min_offset_diff;
  5165. readpos += -conf->min_offset_diff;
  5166. } else
  5167. writepos += conf->min_offset_diff;
  5168. if ((mddev->reshape_backwards
  5169. ? (safepos > writepos && readpos < writepos)
  5170. : (safepos < writepos && readpos > writepos)) ||
  5171. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  5172. /* Cannot proceed until we've updated the superblock... */
  5173. wait_event(conf->wait_for_overlap,
  5174. atomic_read(&conf->reshape_stripes)==0
  5175. || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5176. if (atomic_read(&conf->reshape_stripes) != 0)
  5177. return 0;
  5178. mddev->reshape_position = conf->reshape_progress;
  5179. mddev->curr_resync_completed = sector_nr;
  5180. conf->reshape_checkpoint = jiffies;
  5181. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  5182. md_wakeup_thread(mddev->thread);
  5183. wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
  5184. test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5185. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  5186. return 0;
  5187. spin_lock_irq(&conf->device_lock);
  5188. conf->reshape_safe = mddev->reshape_position;
  5189. spin_unlock_irq(&conf->device_lock);
  5190. wake_up(&conf->wait_for_overlap);
  5191. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  5192. }
  5193. INIT_LIST_HEAD(&stripes);
  5194. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  5195. int j;
  5196. int skipped_disk = 0;
  5197. sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  5198. set_bit(STRIPE_EXPANDING, &sh->state);
  5199. atomic_inc(&conf->reshape_stripes);
  5200. /* If any of this stripe is beyond the end of the old
  5201. * array, then we need to zero those blocks
  5202. */
  5203. for (j=sh->disks; j--;) {
  5204. sector_t s;
  5205. if (j == sh->pd_idx)
  5206. continue;
  5207. if (conf->level == 6 &&
  5208. j == sh->qd_idx)
  5209. continue;
  5210. s = raid5_compute_blocknr(sh, j, 0);
  5211. if (s < raid5_size(mddev, 0, 0)) {
  5212. skipped_disk = 1;
  5213. continue;
  5214. }
  5215. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  5216. set_bit(R5_Expanded, &sh->dev[j].flags);
  5217. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  5218. }
  5219. if (!skipped_disk) {
  5220. set_bit(STRIPE_EXPAND_READY, &sh->state);
  5221. set_bit(STRIPE_HANDLE, &sh->state);
  5222. }
  5223. list_add(&sh->lru, &stripes);
  5224. }
  5225. spin_lock_irq(&conf->device_lock);
  5226. if (mddev->reshape_backwards)
  5227. conf->reshape_progress -= reshape_sectors * new_data_disks;
  5228. else
  5229. conf->reshape_progress += reshape_sectors * new_data_disks;
  5230. spin_unlock_irq(&conf->device_lock);
  5231. /* Ok, those stripe are ready. We can start scheduling
  5232. * reads on the source stripes.
  5233. * The source stripes are determined by mapping the first and last
  5234. * block on the destination stripes.
  5235. */
  5236. first_sector =
  5237. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  5238. 1, &dd_idx, NULL);
  5239. last_sector =
  5240. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  5241. * new_data_disks - 1),
  5242. 1, &dd_idx, NULL);
  5243. if (last_sector >= mddev->dev_sectors)
  5244. last_sector = mddev->dev_sectors - 1;
  5245. while (first_sector <= last_sector) {
  5246. sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
  5247. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  5248. set_bit(STRIPE_HANDLE, &sh->state);
  5249. raid5_release_stripe(sh);
  5250. first_sector += STRIPE_SECTORS;
  5251. }
  5252. /* Now that the sources are clearly marked, we can release
  5253. * the destination stripes
  5254. */
  5255. while (!list_empty(&stripes)) {
  5256. sh = list_entry(stripes.next, struct stripe_head, lru);
  5257. list_del_init(&sh->lru);
  5258. raid5_release_stripe(sh);
  5259. }
  5260. /* If this takes us to the resync_max point where we have to pause,
  5261. * then we need to write out the superblock.
  5262. */
  5263. sector_nr += reshape_sectors;
  5264. retn = reshape_sectors;
  5265. finish:
  5266. if (mddev->curr_resync_completed > mddev->resync_max ||
  5267. (sector_nr - mddev->curr_resync_completed) * 2
  5268. >= mddev->resync_max - mddev->curr_resync_completed) {
  5269. /* Cannot proceed until we've updated the superblock... */
  5270. wait_event(conf->wait_for_overlap,
  5271. atomic_read(&conf->reshape_stripes) == 0
  5272. || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5273. if (atomic_read(&conf->reshape_stripes) != 0)
  5274. goto ret;
  5275. mddev->reshape_position = conf->reshape_progress;
  5276. mddev->curr_resync_completed = sector_nr;
  5277. conf->reshape_checkpoint = jiffies;
  5278. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  5279. md_wakeup_thread(mddev->thread);
  5280. wait_event(mddev->sb_wait,
  5281. !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
  5282. || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  5283. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  5284. goto ret;
  5285. spin_lock_irq(&conf->device_lock);
  5286. conf->reshape_safe = mddev->reshape_position;
  5287. spin_unlock_irq(&conf->device_lock);
  5288. wake_up(&conf->wait_for_overlap);
  5289. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  5290. }
  5291. ret:
  5292. return retn;
  5293. }
  5294. static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
  5295. int *skipped)
  5296. {
  5297. struct r5conf *conf = mddev->private;
  5298. struct stripe_head *sh;
  5299. sector_t max_sector = mddev->dev_sectors;
  5300. sector_t sync_blocks;
  5301. int still_degraded = 0;
  5302. int i;
  5303. if (sector_nr >= max_sector) {
  5304. /* just being told to finish up .. nothing much to do */
  5305. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  5306. end_reshape(conf);
  5307. return 0;
  5308. }
  5309. if (mddev->curr_resync < max_sector) /* aborted */
  5310. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  5311. &sync_blocks, 1);
  5312. else /* completed sync */
  5313. conf->fullsync = 0;
  5314. bitmap_close_sync(mddev->bitmap);
  5315. return 0;
  5316. }
  5317. /* Allow raid5_quiesce to complete */
  5318. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  5319. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  5320. return reshape_request(mddev, sector_nr, skipped);
  5321. /* No need to check resync_max as we never do more than one
  5322. * stripe, and as resync_max will always be on a chunk boundary,
  5323. * if the check in md_do_sync didn't fire, there is no chance
  5324. * of overstepping resync_max here
  5325. */
  5326. /* if there is too many failed drives and we are trying
  5327. * to resync, then assert that we are finished, because there is
  5328. * nothing we can do.
  5329. */
  5330. if (mddev->degraded >= conf->max_degraded &&
  5331. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  5332. sector_t rv = mddev->dev_sectors - sector_nr;
  5333. *skipped = 1;
  5334. return rv;
  5335. }
  5336. if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  5337. !conf->fullsync &&
  5338. !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  5339. sync_blocks >= STRIPE_SECTORS) {
  5340. /* we can skip this block, and probably more */
  5341. sync_blocks /= STRIPE_SECTORS;
  5342. *skipped = 1;
  5343. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  5344. }
  5345. bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
  5346. sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
  5347. if (sh == NULL) {
  5348. sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
  5349. /* make sure we don't swamp the stripe cache if someone else
  5350. * is trying to get access
  5351. */
  5352. schedule_timeout_uninterruptible(1);
  5353. }
  5354. /* Need to check if array will still be degraded after recovery/resync
  5355. * Note in case of > 1 drive failures it's possible we're rebuilding
  5356. * one drive while leaving another faulty drive in array.
  5357. */
  5358. rcu_read_lock();
  5359. for (i = 0; i < conf->raid_disks; i++) {
  5360. struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
  5361. if (rdev == NULL || test_bit(Faulty, &rdev->flags))
  5362. still_degraded = 1;
  5363. }
  5364. rcu_read_unlock();
  5365. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  5366. set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
  5367. set_bit(STRIPE_HANDLE, &sh->state);
  5368. raid5_release_stripe(sh);
  5369. return STRIPE_SECTORS;
  5370. }
  5371. static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
  5372. {
  5373. /* We may not be able to submit a whole bio at once as there
  5374. * may not be enough stripe_heads available.
  5375. * We cannot pre-allocate enough stripe_heads as we may need
  5376. * more than exist in the cache (if we allow ever large chunks).
  5377. * So we do one stripe head at a time and record in
  5378. * ->bi_hw_segments how many have been done.
  5379. *
  5380. * We *know* that this entire raid_bio is in one chunk, so
  5381. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  5382. */
  5383. struct stripe_head *sh;
  5384. int dd_idx;
  5385. sector_t sector, logical_sector, last_sector;
  5386. int scnt = 0;
  5387. int remaining;
  5388. int handled = 0;
  5389. logical_sector = raid_bio->bi_iter.bi_sector &
  5390. ~((sector_t)STRIPE_SECTORS-1);
  5391. sector = raid5_compute_sector(conf, logical_sector,
  5392. 0, &dd_idx, NULL);
  5393. last_sector = bio_end_sector(raid_bio);
  5394. for (; logical_sector < last_sector;
  5395. logical_sector += STRIPE_SECTORS,
  5396. sector += STRIPE_SECTORS,
  5397. scnt++) {
  5398. if (scnt < raid5_bi_processed_stripes(raid_bio))
  5399. /* already done this stripe */
  5400. continue;
  5401. sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
  5402. if (!sh) {
  5403. /* failed to get a stripe - must wait */
  5404. raid5_set_bi_processed_stripes(raid_bio, scnt);
  5405. conf->retry_read_aligned = raid_bio;
  5406. return handled;
  5407. }
  5408. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
  5409. raid5_release_stripe(sh);
  5410. raid5_set_bi_processed_stripes(raid_bio, scnt);
  5411. conf->retry_read_aligned = raid_bio;
  5412. return handled;
  5413. }
  5414. set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
  5415. handle_stripe(sh);
  5416. raid5_release_stripe(sh);
  5417. handled++;
  5418. }
  5419. remaining = raid5_dec_bi_active_stripes(raid_bio);
  5420. if (remaining == 0) {
  5421. trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
  5422. raid_bio, 0);
  5423. bio_endio(raid_bio);
  5424. }
  5425. if (atomic_dec_and_test(&conf->active_aligned_reads))
  5426. wake_up(&conf->wait_for_quiescent);
  5427. return handled;
  5428. }
  5429. static int handle_active_stripes(struct r5conf *conf, int group,
  5430. struct r5worker *worker,
  5431. struct list_head *temp_inactive_list)
  5432. {
  5433. struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
  5434. int i, batch_size = 0, hash;
  5435. bool release_inactive = false;
  5436. while (batch_size < MAX_STRIPE_BATCH &&
  5437. (sh = __get_priority_stripe(conf, group)) != NULL)
  5438. batch[batch_size++] = sh;
  5439. if (batch_size == 0) {
  5440. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  5441. if (!list_empty(temp_inactive_list + i))
  5442. break;
  5443. if (i == NR_STRIPE_HASH_LOCKS) {
  5444. spin_unlock_irq(&conf->device_lock);
  5445. r5l_flush_stripe_to_raid(conf->log);
  5446. spin_lock_irq(&conf->device_lock);
  5447. return batch_size;
  5448. }
  5449. release_inactive = true;
  5450. }
  5451. spin_unlock_irq(&conf->device_lock);
  5452. release_inactive_stripe_list(conf, temp_inactive_list,
  5453. NR_STRIPE_HASH_LOCKS);
  5454. r5l_flush_stripe_to_raid(conf->log);
  5455. if (release_inactive) {
  5456. spin_lock_irq(&conf->device_lock);
  5457. return 0;
  5458. }
  5459. for (i = 0; i < batch_size; i++)
  5460. handle_stripe(batch[i]);
  5461. r5l_write_stripe_run(conf->log);
  5462. cond_resched();
  5463. spin_lock_irq(&conf->device_lock);
  5464. for (i = 0; i < batch_size; i++) {
  5465. hash = batch[i]->hash_lock_index;
  5466. __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
  5467. }
  5468. return batch_size;
  5469. }
  5470. static void raid5_do_work(struct work_struct *work)
  5471. {
  5472. struct r5worker *worker = container_of(work, struct r5worker, work);
  5473. struct r5worker_group *group = worker->group;
  5474. struct r5conf *conf = group->conf;
  5475. int group_id = group - conf->worker_groups;
  5476. int handled;
  5477. struct blk_plug plug;
  5478. pr_debug("+++ raid5worker active\n");
  5479. blk_start_plug(&plug);
  5480. handled = 0;
  5481. spin_lock_irq(&conf->device_lock);
  5482. while (1) {
  5483. int batch_size, released;
  5484. released = release_stripe_list(conf, worker->temp_inactive_list);
  5485. batch_size = handle_active_stripes(conf, group_id, worker,
  5486. worker->temp_inactive_list);
  5487. worker->working = false;
  5488. if (!batch_size && !released)
  5489. break;
  5490. handled += batch_size;
  5491. }
  5492. pr_debug("%d stripes handled\n", handled);
  5493. spin_unlock_irq(&conf->device_lock);
  5494. blk_finish_plug(&plug);
  5495. pr_debug("--- raid5worker inactive\n");
  5496. }
  5497. /*
  5498. * This is our raid5 kernel thread.
  5499. *
  5500. * We scan the hash table for stripes which can be handled now.
  5501. * During the scan, completed stripes are saved for us by the interrupt
  5502. * handler, so that they will not have to wait for our next wakeup.
  5503. */
  5504. static void raid5d(struct md_thread *thread)
  5505. {
  5506. struct mddev *mddev = thread->mddev;
  5507. struct r5conf *conf = mddev->private;
  5508. int handled;
  5509. struct blk_plug plug;
  5510. pr_debug("+++ raid5d active\n");
  5511. md_check_recovery(mddev);
  5512. if (!bio_list_empty(&conf->return_bi) &&
  5513. !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
  5514. struct bio_list tmp = BIO_EMPTY_LIST;
  5515. spin_lock_irq(&conf->device_lock);
  5516. if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
  5517. bio_list_merge(&tmp, &conf->return_bi);
  5518. bio_list_init(&conf->return_bi);
  5519. }
  5520. spin_unlock_irq(&conf->device_lock);
  5521. return_io(&tmp);
  5522. }
  5523. blk_start_plug(&plug);
  5524. handled = 0;
  5525. spin_lock_irq(&conf->device_lock);
  5526. while (1) {
  5527. struct bio *bio;
  5528. int batch_size, released;
  5529. released = release_stripe_list(conf, conf->temp_inactive_list);
  5530. if (released)
  5531. clear_bit(R5_DID_ALLOC, &conf->cache_state);
  5532. if (
  5533. !list_empty(&conf->bitmap_list)) {
  5534. /* Now is a good time to flush some bitmap updates */
  5535. conf->seq_flush++;
  5536. spin_unlock_irq(&conf->device_lock);
  5537. bitmap_unplug(mddev->bitmap);
  5538. spin_lock_irq(&conf->device_lock);
  5539. conf->seq_write = conf->seq_flush;
  5540. activate_bit_delay(conf, conf->temp_inactive_list);
  5541. }
  5542. raid5_activate_delayed(conf);
  5543. while ((bio = remove_bio_from_retry(conf))) {
  5544. int ok;
  5545. spin_unlock_irq(&conf->device_lock);
  5546. ok = retry_aligned_read(conf, bio);
  5547. spin_lock_irq(&conf->device_lock);
  5548. if (!ok)
  5549. break;
  5550. handled++;
  5551. }
  5552. batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
  5553. conf->temp_inactive_list);
  5554. if (!batch_size && !released)
  5555. break;
  5556. handled += batch_size;
  5557. if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
  5558. spin_unlock_irq(&conf->device_lock);
  5559. md_check_recovery(mddev);
  5560. spin_lock_irq(&conf->device_lock);
  5561. }
  5562. }
  5563. pr_debug("%d stripes handled\n", handled);
  5564. spin_unlock_irq(&conf->device_lock);
  5565. if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
  5566. mutex_trylock(&conf->cache_size_mutex)) {
  5567. grow_one_stripe(conf, __GFP_NOWARN);
  5568. /* Set flag even if allocation failed. This helps
  5569. * slow down allocation requests when mem is short
  5570. */
  5571. set_bit(R5_DID_ALLOC, &conf->cache_state);
  5572. mutex_unlock(&conf->cache_size_mutex);
  5573. }
  5574. flush_deferred_bios(conf);
  5575. r5l_flush_stripe_to_raid(conf->log);
  5576. async_tx_issue_pending_all();
  5577. blk_finish_plug(&plug);
  5578. pr_debug("--- raid5d inactive\n");
  5579. }
  5580. static ssize_t
  5581. raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
  5582. {
  5583. struct r5conf *conf;
  5584. int ret = 0;
  5585. spin_lock(&mddev->lock);
  5586. conf = mddev->private;
  5587. if (conf)
  5588. ret = sprintf(page, "%d\n", conf->min_nr_stripes);
  5589. spin_unlock(&mddev->lock);
  5590. return ret;
  5591. }
  5592. int
  5593. raid5_set_cache_size(struct mddev *mddev, int size)
  5594. {
  5595. struct r5conf *conf = mddev->private;
  5596. int err;
  5597. if (size <= 16 || size > 32768)
  5598. return -EINVAL;
  5599. conf->min_nr_stripes = size;
  5600. mutex_lock(&conf->cache_size_mutex);
  5601. while (size < conf->max_nr_stripes &&
  5602. drop_one_stripe(conf))
  5603. ;
  5604. mutex_unlock(&conf->cache_size_mutex);
  5605. err = md_allow_write(mddev);
  5606. if (err)
  5607. return err;
  5608. mutex_lock(&conf->cache_size_mutex);
  5609. while (size > conf->max_nr_stripes)
  5610. if (!grow_one_stripe(conf, GFP_KERNEL))
  5611. break;
  5612. mutex_unlock(&conf->cache_size_mutex);
  5613. return 0;
  5614. }
  5615. EXPORT_SYMBOL(raid5_set_cache_size);
  5616. static ssize_t
  5617. raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
  5618. {
  5619. struct r5conf *conf;
  5620. unsigned long new;
  5621. int err;
  5622. if (len >= PAGE_SIZE)
  5623. return -EINVAL;
  5624. if (kstrtoul(page, 10, &new))
  5625. return -EINVAL;
  5626. err = mddev_lock(mddev);
  5627. if (err)
  5628. return err;
  5629. conf = mddev->private;
  5630. if (!conf)
  5631. err = -ENODEV;
  5632. else
  5633. err = raid5_set_cache_size(mddev, new);
  5634. mddev_unlock(mddev);
  5635. return err ?: len;
  5636. }
  5637. static struct md_sysfs_entry
  5638. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  5639. raid5_show_stripe_cache_size,
  5640. raid5_store_stripe_cache_size);
  5641. static ssize_t
  5642. raid5_show_rmw_level(struct mddev *mddev, char *page)
  5643. {
  5644. struct r5conf *conf = mddev->private;
  5645. if (conf)
  5646. return sprintf(page, "%d\n", conf->rmw_level);
  5647. else
  5648. return 0;
  5649. }
  5650. static ssize_t
  5651. raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
  5652. {
  5653. struct r5conf *conf = mddev->private;
  5654. unsigned long new;
  5655. if (!conf)
  5656. return -ENODEV;
  5657. if (len >= PAGE_SIZE)
  5658. return -EINVAL;
  5659. if (kstrtoul(page, 10, &new))
  5660. return -EINVAL;
  5661. if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
  5662. return -EINVAL;
  5663. if (new != PARITY_DISABLE_RMW &&
  5664. new != PARITY_ENABLE_RMW &&
  5665. new != PARITY_PREFER_RMW)
  5666. return -EINVAL;
  5667. conf->rmw_level = new;
  5668. return len;
  5669. }
  5670. static struct md_sysfs_entry
  5671. raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
  5672. raid5_show_rmw_level,
  5673. raid5_store_rmw_level);
  5674. static ssize_t
  5675. raid5_show_preread_threshold(struct mddev *mddev, char *page)
  5676. {
  5677. struct r5conf *conf;
  5678. int ret = 0;
  5679. spin_lock(&mddev->lock);
  5680. conf = mddev->private;
  5681. if (conf)
  5682. ret = sprintf(page, "%d\n", conf->bypass_threshold);
  5683. spin_unlock(&mddev->lock);
  5684. return ret;
  5685. }
  5686. static ssize_t
  5687. raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
  5688. {
  5689. struct r5conf *conf;
  5690. unsigned long new;
  5691. int err;
  5692. if (len >= PAGE_SIZE)
  5693. return -EINVAL;
  5694. if (kstrtoul(page, 10, &new))
  5695. return -EINVAL;
  5696. err = mddev_lock(mddev);
  5697. if (err)
  5698. return err;
  5699. conf = mddev->private;
  5700. if (!conf)
  5701. err = -ENODEV;
  5702. else if (new > conf->min_nr_stripes)
  5703. err = -EINVAL;
  5704. else
  5705. conf->bypass_threshold = new;
  5706. mddev_unlock(mddev);
  5707. return err ?: len;
  5708. }
  5709. static struct md_sysfs_entry
  5710. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  5711. S_IRUGO | S_IWUSR,
  5712. raid5_show_preread_threshold,
  5713. raid5_store_preread_threshold);
  5714. static ssize_t
  5715. raid5_show_skip_copy(struct mddev *mddev, char *page)
  5716. {
  5717. struct r5conf *conf;
  5718. int ret = 0;
  5719. spin_lock(&mddev->lock);
  5720. conf = mddev->private;
  5721. if (conf)
  5722. ret = sprintf(page, "%d\n", conf->skip_copy);
  5723. spin_unlock(&mddev->lock);
  5724. return ret;
  5725. }
  5726. static ssize_t
  5727. raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
  5728. {
  5729. struct r5conf *conf;
  5730. unsigned long new;
  5731. int err;
  5732. if (len >= PAGE_SIZE)
  5733. return -EINVAL;
  5734. if (kstrtoul(page, 10, &new))
  5735. return -EINVAL;
  5736. new = !!new;
  5737. err = mddev_lock(mddev);
  5738. if (err)
  5739. return err;
  5740. conf = mddev->private;
  5741. if (!conf)
  5742. err = -ENODEV;
  5743. else if (new != conf->skip_copy) {
  5744. mddev_suspend(mddev);
  5745. conf->skip_copy = new;
  5746. if (new)
  5747. mddev->queue->backing_dev_info->capabilities |=
  5748. BDI_CAP_STABLE_WRITES;
  5749. else
  5750. mddev->queue->backing_dev_info->capabilities &=
  5751. ~BDI_CAP_STABLE_WRITES;
  5752. mddev_resume(mddev);
  5753. }
  5754. mddev_unlock(mddev);
  5755. return err ?: len;
  5756. }
  5757. static struct md_sysfs_entry
  5758. raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
  5759. raid5_show_skip_copy,
  5760. raid5_store_skip_copy);
  5761. static ssize_t
  5762. stripe_cache_active_show(struct mddev *mddev, char *page)
  5763. {
  5764. struct r5conf *conf = mddev->private;
  5765. if (conf)
  5766. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  5767. else
  5768. return 0;
  5769. }
  5770. static struct md_sysfs_entry
  5771. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  5772. static ssize_t
  5773. raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
  5774. {
  5775. struct r5conf *conf;
  5776. int ret = 0;
  5777. spin_lock(&mddev->lock);
  5778. conf = mddev->private;
  5779. if (conf)
  5780. ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
  5781. spin_unlock(&mddev->lock);
  5782. return ret;
  5783. }
  5784. static int alloc_thread_groups(struct r5conf *conf, int cnt,
  5785. int *group_cnt,
  5786. int *worker_cnt_per_group,
  5787. struct r5worker_group **worker_groups);
  5788. static ssize_t
  5789. raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
  5790. {
  5791. struct r5conf *conf;
  5792. unsigned long new;
  5793. int err;
  5794. struct r5worker_group *new_groups, *old_groups;
  5795. int group_cnt, worker_cnt_per_group;
  5796. if (len >= PAGE_SIZE)
  5797. return -EINVAL;
  5798. if (kstrtoul(page, 10, &new))
  5799. return -EINVAL;
  5800. err = mddev_lock(mddev);
  5801. if (err)
  5802. return err;
  5803. conf = mddev->private;
  5804. if (!conf)
  5805. err = -ENODEV;
  5806. else if (new != conf->worker_cnt_per_group) {
  5807. mddev_suspend(mddev);
  5808. old_groups = conf->worker_groups;
  5809. if (old_groups)
  5810. flush_workqueue(raid5_wq);
  5811. err = alloc_thread_groups(conf, new,
  5812. &group_cnt, &worker_cnt_per_group,
  5813. &new_groups);
  5814. if (!err) {
  5815. spin_lock_irq(&conf->device_lock);
  5816. conf->group_cnt = group_cnt;
  5817. conf->worker_cnt_per_group = worker_cnt_per_group;
  5818. conf->worker_groups = new_groups;
  5819. spin_unlock_irq(&conf->device_lock);
  5820. if (old_groups)
  5821. kfree(old_groups[0].workers);
  5822. kfree(old_groups);
  5823. }
  5824. mddev_resume(mddev);
  5825. }
  5826. mddev_unlock(mddev);
  5827. return err ?: len;
  5828. }
  5829. static struct md_sysfs_entry
  5830. raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
  5831. raid5_show_group_thread_cnt,
  5832. raid5_store_group_thread_cnt);
  5833. static struct attribute *raid5_attrs[] = {
  5834. &raid5_stripecache_size.attr,
  5835. &raid5_stripecache_active.attr,
  5836. &raid5_preread_bypass_threshold.attr,
  5837. &raid5_group_thread_cnt.attr,
  5838. &raid5_skip_copy.attr,
  5839. &raid5_rmw_level.attr,
  5840. &r5c_journal_mode.attr,
  5841. NULL,
  5842. };
  5843. static struct attribute_group raid5_attrs_group = {
  5844. .name = NULL,
  5845. .attrs = raid5_attrs,
  5846. };
  5847. static int alloc_thread_groups(struct r5conf *conf, int cnt,
  5848. int *group_cnt,
  5849. int *worker_cnt_per_group,
  5850. struct r5worker_group **worker_groups)
  5851. {
  5852. int i, j, k;
  5853. ssize_t size;
  5854. struct r5worker *workers;
  5855. *worker_cnt_per_group = cnt;
  5856. if (cnt == 0) {
  5857. *group_cnt = 0;
  5858. *worker_groups = NULL;
  5859. return 0;
  5860. }
  5861. *group_cnt = num_possible_nodes();
  5862. size = sizeof(struct r5worker) * cnt;
  5863. workers = kzalloc(size * *group_cnt, GFP_NOIO);
  5864. *worker_groups = kzalloc(sizeof(struct r5worker_group) *
  5865. *group_cnt, GFP_NOIO);
  5866. if (!*worker_groups || !workers) {
  5867. kfree(workers);
  5868. kfree(*worker_groups);
  5869. return -ENOMEM;
  5870. }
  5871. for (i = 0; i < *group_cnt; i++) {
  5872. struct r5worker_group *group;
  5873. group = &(*worker_groups)[i];
  5874. INIT_LIST_HEAD(&group->handle_list);
  5875. group->conf = conf;
  5876. group->workers = workers + i * cnt;
  5877. for (j = 0; j < cnt; j++) {
  5878. struct r5worker *worker = group->workers + j;
  5879. worker->group = group;
  5880. INIT_WORK(&worker->work, raid5_do_work);
  5881. for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
  5882. INIT_LIST_HEAD(worker->temp_inactive_list + k);
  5883. }
  5884. }
  5885. return 0;
  5886. }
  5887. static void free_thread_groups(struct r5conf *conf)
  5888. {
  5889. if (conf->worker_groups)
  5890. kfree(conf->worker_groups[0].workers);
  5891. kfree(conf->worker_groups);
  5892. conf->worker_groups = NULL;
  5893. }
  5894. static sector_t
  5895. raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  5896. {
  5897. struct r5conf *conf = mddev->private;
  5898. if (!sectors)
  5899. sectors = mddev->dev_sectors;
  5900. if (!raid_disks)
  5901. /* size is defined by the smallest of previous and new size */
  5902. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  5903. sectors &= ~((sector_t)conf->chunk_sectors - 1);
  5904. sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
  5905. return sectors * (raid_disks - conf->max_degraded);
  5906. }
  5907. static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
  5908. {
  5909. safe_put_page(percpu->spare_page);
  5910. if (percpu->scribble)
  5911. flex_array_free(percpu->scribble);
  5912. percpu->spare_page = NULL;
  5913. percpu->scribble = NULL;
  5914. }
  5915. static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
  5916. {
  5917. if (conf->level == 6 && !percpu->spare_page)
  5918. percpu->spare_page = alloc_page(GFP_KERNEL);
  5919. if (!percpu->scribble)
  5920. percpu->scribble = scribble_alloc(max(conf->raid_disks,
  5921. conf->previous_raid_disks),
  5922. max(conf->chunk_sectors,
  5923. conf->prev_chunk_sectors)
  5924. / STRIPE_SECTORS,
  5925. GFP_KERNEL);
  5926. if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
  5927. free_scratch_buffer(conf, percpu);
  5928. return -ENOMEM;
  5929. }
  5930. return 0;
  5931. }
  5932. static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
  5933. {
  5934. struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
  5935. free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
  5936. return 0;
  5937. }
  5938. static void raid5_free_percpu(struct r5conf *conf)
  5939. {
  5940. if (!conf->percpu)
  5941. return;
  5942. cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
  5943. free_percpu(conf->percpu);
  5944. }
  5945. static void free_conf(struct r5conf *conf)
  5946. {
  5947. int i;
  5948. if (conf->log)
  5949. r5l_exit_log(conf->log);
  5950. if (conf->shrinker.nr_deferred)
  5951. unregister_shrinker(&conf->shrinker);
  5952. free_thread_groups(conf);
  5953. shrink_stripes(conf);
  5954. raid5_free_percpu(conf);
  5955. for (i = 0; i < conf->pool_size; i++)
  5956. if (conf->disks[i].extra_page)
  5957. put_page(conf->disks[i].extra_page);
  5958. kfree(conf->disks);
  5959. kfree(conf->stripe_hashtbl);
  5960. kfree(conf);
  5961. }
  5962. static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
  5963. {
  5964. struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
  5965. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  5966. if (alloc_scratch_buffer(conf, percpu)) {
  5967. pr_warn("%s: failed memory allocation for cpu%u\n",
  5968. __func__, cpu);
  5969. return -ENOMEM;
  5970. }
  5971. return 0;
  5972. }
  5973. static int raid5_alloc_percpu(struct r5conf *conf)
  5974. {
  5975. int err = 0;
  5976. conf->percpu = alloc_percpu(struct raid5_percpu);
  5977. if (!conf->percpu)
  5978. return -ENOMEM;
  5979. err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
  5980. if (!err) {
  5981. conf->scribble_disks = max(conf->raid_disks,
  5982. conf->previous_raid_disks);
  5983. conf->scribble_sectors = max(conf->chunk_sectors,
  5984. conf->prev_chunk_sectors);
  5985. }
  5986. return err;
  5987. }
  5988. static unsigned long raid5_cache_scan(struct shrinker *shrink,
  5989. struct shrink_control *sc)
  5990. {
  5991. struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
  5992. unsigned long ret = SHRINK_STOP;
  5993. if (mutex_trylock(&conf->cache_size_mutex)) {
  5994. ret= 0;
  5995. while (ret < sc->nr_to_scan &&
  5996. conf->max_nr_stripes > conf->min_nr_stripes) {
  5997. if (drop_one_stripe(conf) == 0) {
  5998. ret = SHRINK_STOP;
  5999. break;
  6000. }
  6001. ret++;
  6002. }
  6003. mutex_unlock(&conf->cache_size_mutex);
  6004. }
  6005. return ret;
  6006. }
  6007. static unsigned long raid5_cache_count(struct shrinker *shrink,
  6008. struct shrink_control *sc)
  6009. {
  6010. struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
  6011. if (conf->max_nr_stripes < conf->min_nr_stripes)
  6012. /* unlikely, but not impossible */
  6013. return 0;
  6014. return conf->max_nr_stripes - conf->min_nr_stripes;
  6015. }
  6016. static struct r5conf *setup_conf(struct mddev *mddev)
  6017. {
  6018. struct r5conf *conf;
  6019. int raid_disk, memory, max_disks;
  6020. struct md_rdev *rdev;
  6021. struct disk_info *disk;
  6022. char pers_name[6];
  6023. int i;
  6024. int group_cnt, worker_cnt_per_group;
  6025. struct r5worker_group *new_group;
  6026. if (mddev->new_level != 5
  6027. && mddev->new_level != 4
  6028. && mddev->new_level != 6) {
  6029. pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
  6030. mdname(mddev), mddev->new_level);
  6031. return ERR_PTR(-EIO);
  6032. }
  6033. if ((mddev->new_level == 5
  6034. && !algorithm_valid_raid5(mddev->new_layout)) ||
  6035. (mddev->new_level == 6
  6036. && !algorithm_valid_raid6(mddev->new_layout))) {
  6037. pr_warn("md/raid:%s: layout %d not supported\n",
  6038. mdname(mddev), mddev->new_layout);
  6039. return ERR_PTR(-EIO);
  6040. }
  6041. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  6042. pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
  6043. mdname(mddev), mddev->raid_disks);
  6044. return ERR_PTR(-EINVAL);
  6045. }
  6046. if (!mddev->new_chunk_sectors ||
  6047. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  6048. !is_power_of_2(mddev->new_chunk_sectors)) {
  6049. pr_warn("md/raid:%s: invalid chunk size %d\n",
  6050. mdname(mddev), mddev->new_chunk_sectors << 9);
  6051. return ERR_PTR(-EINVAL);
  6052. }
  6053. conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
  6054. if (conf == NULL)
  6055. goto abort;
  6056. /* Don't enable multi-threading by default*/
  6057. if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
  6058. &new_group)) {
  6059. conf->group_cnt = group_cnt;
  6060. conf->worker_cnt_per_group = worker_cnt_per_group;
  6061. conf->worker_groups = new_group;
  6062. } else
  6063. goto abort;
  6064. spin_lock_init(&conf->device_lock);
  6065. seqcount_init(&conf->gen_lock);
  6066. mutex_init(&conf->cache_size_mutex);
  6067. init_waitqueue_head(&conf->wait_for_quiescent);
  6068. init_waitqueue_head(&conf->wait_for_stripe);
  6069. init_waitqueue_head(&conf->wait_for_overlap);
  6070. INIT_LIST_HEAD(&conf->handle_list);
  6071. INIT_LIST_HEAD(&conf->hold_list);
  6072. INIT_LIST_HEAD(&conf->delayed_list);
  6073. INIT_LIST_HEAD(&conf->bitmap_list);
  6074. bio_list_init(&conf->return_bi);
  6075. init_llist_head(&conf->released_stripes);
  6076. atomic_set(&conf->active_stripes, 0);
  6077. atomic_set(&conf->preread_active_stripes, 0);
  6078. atomic_set(&conf->active_aligned_reads, 0);
  6079. bio_list_init(&conf->pending_bios);
  6080. spin_lock_init(&conf->pending_bios_lock);
  6081. conf->batch_bio_dispatch = true;
  6082. rdev_for_each(rdev, mddev) {
  6083. if (test_bit(Journal, &rdev->flags))
  6084. continue;
  6085. if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
  6086. conf->batch_bio_dispatch = false;
  6087. break;
  6088. }
  6089. }
  6090. conf->bypass_threshold = BYPASS_THRESHOLD;
  6091. conf->recovery_disabled = mddev->recovery_disabled - 1;
  6092. conf->raid_disks = mddev->raid_disks;
  6093. if (mddev->reshape_position == MaxSector)
  6094. conf->previous_raid_disks = mddev->raid_disks;
  6095. else
  6096. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  6097. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  6098. conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
  6099. GFP_KERNEL);
  6100. if (!conf->disks)
  6101. goto abort;
  6102. for (i = 0; i < max_disks; i++) {
  6103. conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
  6104. if (!conf->disks[i].extra_page)
  6105. goto abort;
  6106. }
  6107. conf->mddev = mddev;
  6108. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  6109. goto abort;
  6110. /* We init hash_locks[0] separately to that it can be used
  6111. * as the reference lock in the spin_lock_nest_lock() call
  6112. * in lock_all_device_hash_locks_irq in order to convince
  6113. * lockdep that we know what we are doing.
  6114. */
  6115. spin_lock_init(conf->hash_locks);
  6116. for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
  6117. spin_lock_init(conf->hash_locks + i);
  6118. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  6119. INIT_LIST_HEAD(conf->inactive_list + i);
  6120. for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
  6121. INIT_LIST_HEAD(conf->temp_inactive_list + i);
  6122. atomic_set(&conf->r5c_cached_full_stripes, 0);
  6123. INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
  6124. atomic_set(&conf->r5c_cached_partial_stripes, 0);
  6125. INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
  6126. atomic_set(&conf->r5c_flushing_full_stripes, 0);
  6127. atomic_set(&conf->r5c_flushing_partial_stripes, 0);
  6128. conf->level = mddev->new_level;
  6129. conf->chunk_sectors = mddev->new_chunk_sectors;
  6130. if (raid5_alloc_percpu(conf) != 0)
  6131. goto abort;
  6132. pr_debug("raid456: run(%s) called.\n", mdname(mddev));
  6133. rdev_for_each(rdev, mddev) {
  6134. raid_disk = rdev->raid_disk;
  6135. if (raid_disk >= max_disks
  6136. || raid_disk < 0 || test_bit(Journal, &rdev->flags))
  6137. continue;
  6138. disk = conf->disks + raid_disk;
  6139. if (test_bit(Replacement, &rdev->flags)) {
  6140. if (disk->replacement)
  6141. goto abort;
  6142. disk->replacement = rdev;
  6143. } else {
  6144. if (disk->rdev)
  6145. goto abort;
  6146. disk->rdev = rdev;
  6147. }
  6148. if (test_bit(In_sync, &rdev->flags)) {
  6149. char b[BDEVNAME_SIZE];
  6150. pr_info("md/raid:%s: device %s operational as raid disk %d\n",
  6151. mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
  6152. } else if (rdev->saved_raid_disk != raid_disk)
  6153. /* Cannot rely on bitmap to complete recovery */
  6154. conf->fullsync = 1;
  6155. }
  6156. conf->level = mddev->new_level;
  6157. if (conf->level == 6) {
  6158. conf->max_degraded = 2;
  6159. if (raid6_call.xor_syndrome)
  6160. conf->rmw_level = PARITY_ENABLE_RMW;
  6161. else
  6162. conf->rmw_level = PARITY_DISABLE_RMW;
  6163. } else {
  6164. conf->max_degraded = 1;
  6165. conf->rmw_level = PARITY_ENABLE_RMW;
  6166. }
  6167. conf->algorithm = mddev->new_layout;
  6168. conf->reshape_progress = mddev->reshape_position;
  6169. if (conf->reshape_progress != MaxSector) {
  6170. conf->prev_chunk_sectors = mddev->chunk_sectors;
  6171. conf->prev_algo = mddev->layout;
  6172. } else {
  6173. conf->prev_chunk_sectors = conf->chunk_sectors;
  6174. conf->prev_algo = conf->algorithm;
  6175. }
  6176. conf->min_nr_stripes = NR_STRIPES;
  6177. if (mddev->reshape_position != MaxSector) {
  6178. int stripes = max_t(int,
  6179. ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
  6180. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
  6181. conf->min_nr_stripes = max(NR_STRIPES, stripes);
  6182. if (conf->min_nr_stripes != NR_STRIPES)
  6183. pr_info("md/raid:%s: force stripe size %d for reshape\n",
  6184. mdname(mddev), conf->min_nr_stripes);
  6185. }
  6186. memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
  6187. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  6188. atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
  6189. if (grow_stripes(conf, conf->min_nr_stripes)) {
  6190. pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
  6191. mdname(mddev), memory);
  6192. goto abort;
  6193. } else
  6194. pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
  6195. /*
  6196. * Losing a stripe head costs more than the time to refill it,
  6197. * it reduces the queue depth and so can hurt throughput.
  6198. * So set it rather large, scaled by number of devices.
  6199. */
  6200. conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
  6201. conf->shrinker.scan_objects = raid5_cache_scan;
  6202. conf->shrinker.count_objects = raid5_cache_count;
  6203. conf->shrinker.batch = 128;
  6204. conf->shrinker.flags = 0;
  6205. if (register_shrinker(&conf->shrinker)) {
  6206. pr_warn("md/raid:%s: couldn't register shrinker.\n",
  6207. mdname(mddev));
  6208. goto abort;
  6209. }
  6210. sprintf(pers_name, "raid%d", mddev->new_level);
  6211. conf->thread = md_register_thread(raid5d, mddev, pers_name);
  6212. if (!conf->thread) {
  6213. pr_warn("md/raid:%s: couldn't allocate thread.\n",
  6214. mdname(mddev));
  6215. goto abort;
  6216. }
  6217. return conf;
  6218. abort:
  6219. if (conf) {
  6220. free_conf(conf);
  6221. return ERR_PTR(-EIO);
  6222. } else
  6223. return ERR_PTR(-ENOMEM);
  6224. }
  6225. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  6226. {
  6227. switch (algo) {
  6228. case ALGORITHM_PARITY_0:
  6229. if (raid_disk < max_degraded)
  6230. return 1;
  6231. break;
  6232. case ALGORITHM_PARITY_N:
  6233. if (raid_disk >= raid_disks - max_degraded)
  6234. return 1;
  6235. break;
  6236. case ALGORITHM_PARITY_0_6:
  6237. if (raid_disk == 0 ||
  6238. raid_disk == raid_disks - 1)
  6239. return 1;
  6240. break;
  6241. case ALGORITHM_LEFT_ASYMMETRIC_6:
  6242. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  6243. case ALGORITHM_LEFT_SYMMETRIC_6:
  6244. case ALGORITHM_RIGHT_SYMMETRIC_6:
  6245. if (raid_disk == raid_disks - 1)
  6246. return 1;
  6247. }
  6248. return 0;
  6249. }
  6250. static int raid5_run(struct mddev *mddev)
  6251. {
  6252. struct r5conf *conf;
  6253. int working_disks = 0;
  6254. int dirty_parity_disks = 0;
  6255. struct md_rdev *rdev;
  6256. struct md_rdev *journal_dev = NULL;
  6257. sector_t reshape_offset = 0;
  6258. int i;
  6259. long long min_offset_diff = 0;
  6260. int first = 1;
  6261. if (mddev->recovery_cp != MaxSector)
  6262. pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
  6263. mdname(mddev));
  6264. rdev_for_each(rdev, mddev) {
  6265. long long diff;
  6266. if (test_bit(Journal, &rdev->flags)) {
  6267. journal_dev = rdev;
  6268. continue;
  6269. }
  6270. if (rdev->raid_disk < 0)
  6271. continue;
  6272. diff = (rdev->new_data_offset - rdev->data_offset);
  6273. if (first) {
  6274. min_offset_diff = diff;
  6275. first = 0;
  6276. } else if (mddev->reshape_backwards &&
  6277. diff < min_offset_diff)
  6278. min_offset_diff = diff;
  6279. else if (!mddev->reshape_backwards &&
  6280. diff > min_offset_diff)
  6281. min_offset_diff = diff;
  6282. }
  6283. if (mddev->reshape_position != MaxSector) {
  6284. /* Check that we can continue the reshape.
  6285. * Difficulties arise if the stripe we would write to
  6286. * next is at or after the stripe we would read from next.
  6287. * For a reshape that changes the number of devices, this
  6288. * is only possible for a very short time, and mdadm makes
  6289. * sure that time appears to have past before assembling
  6290. * the array. So we fail if that time hasn't passed.
  6291. * For a reshape that keeps the number of devices the same
  6292. * mdadm must be monitoring the reshape can keeping the
  6293. * critical areas read-only and backed up. It will start
  6294. * the array in read-only mode, so we check for that.
  6295. */
  6296. sector_t here_new, here_old;
  6297. int old_disks;
  6298. int max_degraded = (mddev->level == 6 ? 2 : 1);
  6299. int chunk_sectors;
  6300. int new_data_disks;
  6301. if (journal_dev) {
  6302. pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
  6303. mdname(mddev));
  6304. return -EINVAL;
  6305. }
  6306. if (mddev->new_level != mddev->level) {
  6307. pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
  6308. mdname(mddev));
  6309. return -EINVAL;
  6310. }
  6311. old_disks = mddev->raid_disks - mddev->delta_disks;
  6312. /* reshape_position must be on a new-stripe boundary, and one
  6313. * further up in new geometry must map after here in old
  6314. * geometry.
  6315. * If the chunk sizes are different, then as we perform reshape
  6316. * in units of the largest of the two, reshape_position needs
  6317. * be a multiple of the largest chunk size times new data disks.
  6318. */
  6319. here_new = mddev->reshape_position;
  6320. chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
  6321. new_data_disks = mddev->raid_disks - max_degraded;
  6322. if (sector_div(here_new, chunk_sectors * new_data_disks)) {
  6323. pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
  6324. mdname(mddev));
  6325. return -EINVAL;
  6326. }
  6327. reshape_offset = here_new * chunk_sectors;
  6328. /* here_new is the stripe we will write to */
  6329. here_old = mddev->reshape_position;
  6330. sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
  6331. /* here_old is the first stripe that we might need to read
  6332. * from */
  6333. if (mddev->delta_disks == 0) {
  6334. /* We cannot be sure it is safe to start an in-place
  6335. * reshape. It is only safe if user-space is monitoring
  6336. * and taking constant backups.
  6337. * mdadm always starts a situation like this in
  6338. * readonly mode so it can take control before
  6339. * allowing any writes. So just check for that.
  6340. */
  6341. if (abs(min_offset_diff) >= mddev->chunk_sectors &&
  6342. abs(min_offset_diff) >= mddev->new_chunk_sectors)
  6343. /* not really in-place - so OK */;
  6344. else if (mddev->ro == 0) {
  6345. pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
  6346. mdname(mddev));
  6347. return -EINVAL;
  6348. }
  6349. } else if (mddev->reshape_backwards
  6350. ? (here_new * chunk_sectors + min_offset_diff <=
  6351. here_old * chunk_sectors)
  6352. : (here_new * chunk_sectors >=
  6353. here_old * chunk_sectors + (-min_offset_diff))) {
  6354. /* Reading from the same stripe as writing to - bad */
  6355. pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
  6356. mdname(mddev));
  6357. return -EINVAL;
  6358. }
  6359. pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
  6360. /* OK, we should be able to continue; */
  6361. } else {
  6362. BUG_ON(mddev->level != mddev->new_level);
  6363. BUG_ON(mddev->layout != mddev->new_layout);
  6364. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  6365. BUG_ON(mddev->delta_disks != 0);
  6366. }
  6367. if (mddev->private == NULL)
  6368. conf = setup_conf(mddev);
  6369. else
  6370. conf = mddev->private;
  6371. if (IS_ERR(conf))
  6372. return PTR_ERR(conf);
  6373. if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
  6374. if (!journal_dev) {
  6375. pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
  6376. mdname(mddev));
  6377. mddev->ro = 1;
  6378. set_disk_ro(mddev->gendisk, 1);
  6379. } else if (mddev->recovery_cp == MaxSector)
  6380. set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
  6381. }
  6382. conf->min_offset_diff = min_offset_diff;
  6383. mddev->thread = conf->thread;
  6384. conf->thread = NULL;
  6385. mddev->private = conf;
  6386. for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
  6387. i++) {
  6388. rdev = conf->disks[i].rdev;
  6389. if (!rdev && conf->disks[i].replacement) {
  6390. /* The replacement is all we have yet */
  6391. rdev = conf->disks[i].replacement;
  6392. conf->disks[i].replacement = NULL;
  6393. clear_bit(Replacement, &rdev->flags);
  6394. conf->disks[i].rdev = rdev;
  6395. }
  6396. if (!rdev)
  6397. continue;
  6398. if (conf->disks[i].replacement &&
  6399. conf->reshape_progress != MaxSector) {
  6400. /* replacements and reshape simply do not mix. */
  6401. pr_warn("md: cannot handle concurrent replacement and reshape.\n");
  6402. goto abort;
  6403. }
  6404. if (test_bit(In_sync, &rdev->flags)) {
  6405. working_disks++;
  6406. continue;
  6407. }
  6408. /* This disc is not fully in-sync. However if it
  6409. * just stored parity (beyond the recovery_offset),
  6410. * when we don't need to be concerned about the
  6411. * array being dirty.
  6412. * When reshape goes 'backwards', we never have
  6413. * partially completed devices, so we only need
  6414. * to worry about reshape going forwards.
  6415. */
  6416. /* Hack because v0.91 doesn't store recovery_offset properly. */
  6417. if (mddev->major_version == 0 &&
  6418. mddev->minor_version > 90)
  6419. rdev->recovery_offset = reshape_offset;
  6420. if (rdev->recovery_offset < reshape_offset) {
  6421. /* We need to check old and new layout */
  6422. if (!only_parity(rdev->raid_disk,
  6423. conf->algorithm,
  6424. conf->raid_disks,
  6425. conf->max_degraded))
  6426. continue;
  6427. }
  6428. if (!only_parity(rdev->raid_disk,
  6429. conf->prev_algo,
  6430. conf->previous_raid_disks,
  6431. conf->max_degraded))
  6432. continue;
  6433. dirty_parity_disks++;
  6434. }
  6435. /*
  6436. * 0 for a fully functional array, 1 or 2 for a degraded array.
  6437. */
  6438. mddev->degraded = raid5_calc_degraded(conf);
  6439. if (has_failed(conf)) {
  6440. pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
  6441. mdname(mddev), mddev->degraded, conf->raid_disks);
  6442. goto abort;
  6443. }
  6444. /* device size must be a multiple of chunk size */
  6445. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  6446. mddev->resync_max_sectors = mddev->dev_sectors;
  6447. if (mddev->degraded > dirty_parity_disks &&
  6448. mddev->recovery_cp != MaxSector) {
  6449. if (mddev->ok_start_degraded)
  6450. pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
  6451. mdname(mddev));
  6452. else {
  6453. pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
  6454. mdname(mddev));
  6455. goto abort;
  6456. }
  6457. }
  6458. pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
  6459. mdname(mddev), conf->level,
  6460. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  6461. mddev->new_layout);
  6462. print_raid5_conf(conf);
  6463. if (conf->reshape_progress != MaxSector) {
  6464. conf->reshape_safe = conf->reshape_progress;
  6465. atomic_set(&conf->reshape_stripes, 0);
  6466. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  6467. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  6468. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  6469. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  6470. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  6471. "reshape");
  6472. }
  6473. /* Ok, everything is just fine now */
  6474. if (mddev->to_remove == &raid5_attrs_group)
  6475. mddev->to_remove = NULL;
  6476. else if (mddev->kobj.sd &&
  6477. sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  6478. pr_warn("raid5: failed to create sysfs attributes for %s\n",
  6479. mdname(mddev));
  6480. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  6481. if (mddev->queue) {
  6482. int chunk_size;
  6483. /* read-ahead size must cover two whole stripes, which
  6484. * is 2 * (datadisks) * chunksize where 'n' is the
  6485. * number of raid devices
  6486. */
  6487. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  6488. int stripe = data_disks *
  6489. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  6490. if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
  6491. mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
  6492. chunk_size = mddev->chunk_sectors << 9;
  6493. blk_queue_io_min(mddev->queue, chunk_size);
  6494. blk_queue_io_opt(mddev->queue, chunk_size *
  6495. (conf->raid_disks - conf->max_degraded));
  6496. mddev->queue->limits.raid_partial_stripes_expensive = 1;
  6497. /*
  6498. * We can only discard a whole stripe. It doesn't make sense to
  6499. * discard data disk but write parity disk
  6500. */
  6501. stripe = stripe * PAGE_SIZE;
  6502. /* Round up to power of 2, as discard handling
  6503. * currently assumes that */
  6504. while ((stripe-1) & stripe)
  6505. stripe = (stripe | (stripe-1)) + 1;
  6506. mddev->queue->limits.discard_alignment = stripe;
  6507. mddev->queue->limits.discard_granularity = stripe;
  6508. /*
  6509. * We use 16-bit counter of active stripes in bi_phys_segments
  6510. * (minus one for over-loaded initialization)
  6511. */
  6512. blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
  6513. blk_queue_max_discard_sectors(mddev->queue,
  6514. 0xfffe * STRIPE_SECTORS);
  6515. blk_queue_max_write_same_sectors(mddev->queue, 0);
  6516. blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
  6517. rdev_for_each(rdev, mddev) {
  6518. disk_stack_limits(mddev->gendisk, rdev->bdev,
  6519. rdev->data_offset << 9);
  6520. disk_stack_limits(mddev->gendisk, rdev->bdev,
  6521. rdev->new_data_offset << 9);
  6522. }
  6523. /*
  6524. * zeroing is required, otherwise data
  6525. * could be lost. Consider a scenario: discard a stripe
  6526. * (the stripe could be inconsistent if
  6527. * discard_zeroes_data is 0); write one disk of the
  6528. * stripe (the stripe could be inconsistent again
  6529. * depending on which disks are used to calculate
  6530. * parity); the disk is broken; The stripe data of this
  6531. * disk is lost.
  6532. *
  6533. * We only allow DISCARD if the sysadmin has confirmed that
  6534. * only safe devices are in use by setting a module parameter.
  6535. * A better idea might be to turn DISCARD into WRITE_ZEROES
  6536. * requests, as that is required to be safe.
  6537. */
  6538. if (devices_handle_discard_safely &&
  6539. mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
  6540. mddev->queue->limits.discard_granularity >= stripe)
  6541. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  6542. mddev->queue);
  6543. else
  6544. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
  6545. mddev->queue);
  6546. blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
  6547. }
  6548. if (journal_dev) {
  6549. char b[BDEVNAME_SIZE];
  6550. pr_debug("md/raid:%s: using device %s as journal\n",
  6551. mdname(mddev), bdevname(journal_dev->bdev, b));
  6552. if (r5l_init_log(conf, journal_dev))
  6553. goto abort;
  6554. }
  6555. return 0;
  6556. abort:
  6557. md_unregister_thread(&mddev->thread);
  6558. print_raid5_conf(conf);
  6559. free_conf(conf);
  6560. mddev->private = NULL;
  6561. pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
  6562. return -EIO;
  6563. }
  6564. static void raid5_free(struct mddev *mddev, void *priv)
  6565. {
  6566. struct r5conf *conf = priv;
  6567. free_conf(conf);
  6568. mddev->to_remove = &raid5_attrs_group;
  6569. }
  6570. static void raid5_status(struct seq_file *seq, struct mddev *mddev)
  6571. {
  6572. struct r5conf *conf = mddev->private;
  6573. int i;
  6574. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  6575. conf->chunk_sectors / 2, mddev->layout);
  6576. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  6577. rcu_read_lock();
  6578. for (i = 0; i < conf->raid_disks; i++) {
  6579. struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
  6580. seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  6581. }
  6582. rcu_read_unlock();
  6583. seq_printf (seq, "]");
  6584. }
  6585. static void print_raid5_conf (struct r5conf *conf)
  6586. {
  6587. int i;
  6588. struct disk_info *tmp;
  6589. pr_debug("RAID conf printout:\n");
  6590. if (!conf) {
  6591. pr_debug("(conf==NULL)\n");
  6592. return;
  6593. }
  6594. pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
  6595. conf->raid_disks,
  6596. conf->raid_disks - conf->mddev->degraded);
  6597. for (i = 0; i < conf->raid_disks; i++) {
  6598. char b[BDEVNAME_SIZE];
  6599. tmp = conf->disks + i;
  6600. if (tmp->rdev)
  6601. pr_debug(" disk %d, o:%d, dev:%s\n",
  6602. i, !test_bit(Faulty, &tmp->rdev->flags),
  6603. bdevname(tmp->rdev->bdev, b));
  6604. }
  6605. }
  6606. static int raid5_spare_active(struct mddev *mddev)
  6607. {
  6608. int i;
  6609. struct r5conf *conf = mddev->private;
  6610. struct disk_info *tmp;
  6611. int count = 0;
  6612. unsigned long flags;
  6613. for (i = 0; i < conf->raid_disks; i++) {
  6614. tmp = conf->disks + i;
  6615. if (tmp->replacement
  6616. && tmp->replacement->recovery_offset == MaxSector
  6617. && !test_bit(Faulty, &tmp->replacement->flags)
  6618. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  6619. /* Replacement has just become active. */
  6620. if (!tmp->rdev
  6621. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  6622. count++;
  6623. if (tmp->rdev) {
  6624. /* Replaced device not technically faulty,
  6625. * but we need to be sure it gets removed
  6626. * and never re-added.
  6627. */
  6628. set_bit(Faulty, &tmp->rdev->flags);
  6629. sysfs_notify_dirent_safe(
  6630. tmp->rdev->sysfs_state);
  6631. }
  6632. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  6633. } else if (tmp->rdev
  6634. && tmp->rdev->recovery_offset == MaxSector
  6635. && !test_bit(Faulty, &tmp->rdev->flags)
  6636. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  6637. count++;
  6638. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  6639. }
  6640. }
  6641. spin_lock_irqsave(&conf->device_lock, flags);
  6642. mddev->degraded = raid5_calc_degraded(conf);
  6643. spin_unlock_irqrestore(&conf->device_lock, flags);
  6644. print_raid5_conf(conf);
  6645. return count;
  6646. }
  6647. static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  6648. {
  6649. struct r5conf *conf = mddev->private;
  6650. int err = 0;
  6651. int number = rdev->raid_disk;
  6652. struct md_rdev **rdevp;
  6653. struct disk_info *p = conf->disks + number;
  6654. print_raid5_conf(conf);
  6655. if (test_bit(Journal, &rdev->flags) && conf->log) {
  6656. struct r5l_log *log;
  6657. /*
  6658. * we can't wait pending write here, as this is called in
  6659. * raid5d, wait will deadlock.
  6660. */
  6661. if (atomic_read(&mddev->writes_pending))
  6662. return -EBUSY;
  6663. log = conf->log;
  6664. conf->log = NULL;
  6665. synchronize_rcu();
  6666. r5l_exit_log(log);
  6667. return 0;
  6668. }
  6669. if (rdev == p->rdev)
  6670. rdevp = &p->rdev;
  6671. else if (rdev == p->replacement)
  6672. rdevp = &p->replacement;
  6673. else
  6674. return 0;
  6675. if (number >= conf->raid_disks &&
  6676. conf->reshape_progress == MaxSector)
  6677. clear_bit(In_sync, &rdev->flags);
  6678. if (test_bit(In_sync, &rdev->flags) ||
  6679. atomic_read(&rdev->nr_pending)) {
  6680. err = -EBUSY;
  6681. goto abort;
  6682. }
  6683. /* Only remove non-faulty devices if recovery
  6684. * isn't possible.
  6685. */
  6686. if (!test_bit(Faulty, &rdev->flags) &&
  6687. mddev->recovery_disabled != conf->recovery_disabled &&
  6688. !has_failed(conf) &&
  6689. (!p->replacement || p->replacement == rdev) &&
  6690. number < conf->raid_disks) {
  6691. err = -EBUSY;
  6692. goto abort;
  6693. }
  6694. *rdevp = NULL;
  6695. if (!test_bit(RemoveSynchronized, &rdev->flags)) {
  6696. synchronize_rcu();
  6697. if (atomic_read(&rdev->nr_pending)) {
  6698. /* lost the race, try later */
  6699. err = -EBUSY;
  6700. *rdevp = rdev;
  6701. }
  6702. }
  6703. if (p->replacement) {
  6704. /* We must have just cleared 'rdev' */
  6705. p->rdev = p->replacement;
  6706. clear_bit(Replacement, &p->replacement->flags);
  6707. smp_mb(); /* Make sure other CPUs may see both as identical
  6708. * but will never see neither - if they are careful
  6709. */
  6710. p->replacement = NULL;
  6711. clear_bit(WantReplacement, &rdev->flags);
  6712. } else
  6713. /* We might have just removed the Replacement as faulty-
  6714. * clear the bit just in case
  6715. */
  6716. clear_bit(WantReplacement, &rdev->flags);
  6717. abort:
  6718. print_raid5_conf(conf);
  6719. return err;
  6720. }
  6721. static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  6722. {
  6723. struct r5conf *conf = mddev->private;
  6724. int err = -EEXIST;
  6725. int disk;
  6726. struct disk_info *p;
  6727. int first = 0;
  6728. int last = conf->raid_disks - 1;
  6729. if (test_bit(Journal, &rdev->flags)) {
  6730. char b[BDEVNAME_SIZE];
  6731. if (conf->log)
  6732. return -EBUSY;
  6733. rdev->raid_disk = 0;
  6734. /*
  6735. * The array is in readonly mode if journal is missing, so no
  6736. * write requests running. We should be safe
  6737. */
  6738. r5l_init_log(conf, rdev);
  6739. pr_debug("md/raid:%s: using device %s as journal\n",
  6740. mdname(mddev), bdevname(rdev->bdev, b));
  6741. return 0;
  6742. }
  6743. if (mddev->recovery_disabled == conf->recovery_disabled)
  6744. return -EBUSY;
  6745. if (rdev->saved_raid_disk < 0 && has_failed(conf))
  6746. /* no point adding a device */
  6747. return -EINVAL;
  6748. if (rdev->raid_disk >= 0)
  6749. first = last = rdev->raid_disk;
  6750. /*
  6751. * find the disk ... but prefer rdev->saved_raid_disk
  6752. * if possible.
  6753. */
  6754. if (rdev->saved_raid_disk >= 0 &&
  6755. rdev->saved_raid_disk >= first &&
  6756. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  6757. first = rdev->saved_raid_disk;
  6758. for (disk = first; disk <= last; disk++) {
  6759. p = conf->disks + disk;
  6760. if (p->rdev == NULL) {
  6761. clear_bit(In_sync, &rdev->flags);
  6762. rdev->raid_disk = disk;
  6763. err = 0;
  6764. if (rdev->saved_raid_disk != disk)
  6765. conf->fullsync = 1;
  6766. rcu_assign_pointer(p->rdev, rdev);
  6767. goto out;
  6768. }
  6769. }
  6770. for (disk = first; disk <= last; disk++) {
  6771. p = conf->disks + disk;
  6772. if (test_bit(WantReplacement, &p->rdev->flags) &&
  6773. p->replacement == NULL) {
  6774. clear_bit(In_sync, &rdev->flags);
  6775. set_bit(Replacement, &rdev->flags);
  6776. rdev->raid_disk = disk;
  6777. err = 0;
  6778. conf->fullsync = 1;
  6779. rcu_assign_pointer(p->replacement, rdev);
  6780. break;
  6781. }
  6782. }
  6783. out:
  6784. print_raid5_conf(conf);
  6785. return err;
  6786. }
  6787. static int raid5_resize(struct mddev *mddev, sector_t sectors)
  6788. {
  6789. /* no resync is happening, and there is enough space
  6790. * on all devices, so we can resize.
  6791. * We need to make sure resync covers any new space.
  6792. * If the array is shrinking we should possibly wait until
  6793. * any io in the removed space completes, but it hardly seems
  6794. * worth it.
  6795. */
  6796. sector_t newsize;
  6797. struct r5conf *conf = mddev->private;
  6798. if (conf->log)
  6799. return -EINVAL;
  6800. sectors &= ~((sector_t)conf->chunk_sectors - 1);
  6801. newsize = raid5_size(mddev, sectors, mddev->raid_disks);
  6802. if (mddev->external_size &&
  6803. mddev->array_sectors > newsize)
  6804. return -EINVAL;
  6805. if (mddev->bitmap) {
  6806. int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
  6807. if (ret)
  6808. return ret;
  6809. }
  6810. md_set_array_sectors(mddev, newsize);
  6811. if (sectors > mddev->dev_sectors &&
  6812. mddev->recovery_cp > mddev->dev_sectors) {
  6813. mddev->recovery_cp = mddev->dev_sectors;
  6814. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  6815. }
  6816. mddev->dev_sectors = sectors;
  6817. mddev->resync_max_sectors = sectors;
  6818. return 0;
  6819. }
  6820. static int check_stripe_cache(struct mddev *mddev)
  6821. {
  6822. /* Can only proceed if there are plenty of stripe_heads.
  6823. * We need a minimum of one full stripe,, and for sensible progress
  6824. * it is best to have about 4 times that.
  6825. * If we require 4 times, then the default 256 4K stripe_heads will
  6826. * allow for chunk sizes up to 256K, which is probably OK.
  6827. * If the chunk size is greater, user-space should request more
  6828. * stripe_heads first.
  6829. */
  6830. struct r5conf *conf = mddev->private;
  6831. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  6832. > conf->min_nr_stripes ||
  6833. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  6834. > conf->min_nr_stripes) {
  6835. pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
  6836. mdname(mddev),
  6837. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  6838. / STRIPE_SIZE)*4);
  6839. return 0;
  6840. }
  6841. return 1;
  6842. }
  6843. static int check_reshape(struct mddev *mddev)
  6844. {
  6845. struct r5conf *conf = mddev->private;
  6846. if (conf->log)
  6847. return -EINVAL;
  6848. if (mddev->delta_disks == 0 &&
  6849. mddev->new_layout == mddev->layout &&
  6850. mddev->new_chunk_sectors == mddev->chunk_sectors)
  6851. return 0; /* nothing to do */
  6852. if (has_failed(conf))
  6853. return -EINVAL;
  6854. if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
  6855. /* We might be able to shrink, but the devices must
  6856. * be made bigger first.
  6857. * For raid6, 4 is the minimum size.
  6858. * Otherwise 2 is the minimum
  6859. */
  6860. int min = 2;
  6861. if (mddev->level == 6)
  6862. min = 4;
  6863. if (mddev->raid_disks + mddev->delta_disks < min)
  6864. return -EINVAL;
  6865. }
  6866. if (!check_stripe_cache(mddev))
  6867. return -ENOSPC;
  6868. if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
  6869. mddev->delta_disks > 0)
  6870. if (resize_chunks(conf,
  6871. conf->previous_raid_disks
  6872. + max(0, mddev->delta_disks),
  6873. max(mddev->new_chunk_sectors,
  6874. mddev->chunk_sectors)
  6875. ) < 0)
  6876. return -ENOMEM;
  6877. return resize_stripes(conf, (conf->previous_raid_disks
  6878. + mddev->delta_disks));
  6879. }
  6880. static int raid5_start_reshape(struct mddev *mddev)
  6881. {
  6882. struct r5conf *conf = mddev->private;
  6883. struct md_rdev *rdev;
  6884. int spares = 0;
  6885. unsigned long flags;
  6886. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  6887. return -EBUSY;
  6888. if (!check_stripe_cache(mddev))
  6889. return -ENOSPC;
  6890. if (has_failed(conf))
  6891. return -EINVAL;
  6892. rdev_for_each(rdev, mddev) {
  6893. if (!test_bit(In_sync, &rdev->flags)
  6894. && !test_bit(Faulty, &rdev->flags))
  6895. spares++;
  6896. }
  6897. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  6898. /* Not enough devices even to make a degraded array
  6899. * of that size
  6900. */
  6901. return -EINVAL;
  6902. /* Refuse to reduce size of the array. Any reductions in
  6903. * array size must be through explicit setting of array_size
  6904. * attribute.
  6905. */
  6906. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  6907. < mddev->array_sectors) {
  6908. pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
  6909. mdname(mddev));
  6910. return -EINVAL;
  6911. }
  6912. atomic_set(&conf->reshape_stripes, 0);
  6913. spin_lock_irq(&conf->device_lock);
  6914. write_seqcount_begin(&conf->gen_lock);
  6915. conf->previous_raid_disks = conf->raid_disks;
  6916. conf->raid_disks += mddev->delta_disks;
  6917. conf->prev_chunk_sectors = conf->chunk_sectors;
  6918. conf->chunk_sectors = mddev->new_chunk_sectors;
  6919. conf->prev_algo = conf->algorithm;
  6920. conf->algorithm = mddev->new_layout;
  6921. conf->generation++;
  6922. /* Code that selects data_offset needs to see the generation update
  6923. * if reshape_progress has been set - so a memory barrier needed.
  6924. */
  6925. smp_mb();
  6926. if (mddev->reshape_backwards)
  6927. conf->reshape_progress = raid5_size(mddev, 0, 0);
  6928. else
  6929. conf->reshape_progress = 0;
  6930. conf->reshape_safe = conf->reshape_progress;
  6931. write_seqcount_end(&conf->gen_lock);
  6932. spin_unlock_irq(&conf->device_lock);
  6933. /* Now make sure any requests that proceeded on the assumption
  6934. * the reshape wasn't running - like Discard or Read - have
  6935. * completed.
  6936. */
  6937. mddev_suspend(mddev);
  6938. mddev_resume(mddev);
  6939. /* Add some new drives, as many as will fit.
  6940. * We know there are enough to make the newly sized array work.
  6941. * Don't add devices if we are reducing the number of
  6942. * devices in the array. This is because it is not possible
  6943. * to correctly record the "partially reconstructed" state of
  6944. * such devices during the reshape and confusion could result.
  6945. */
  6946. if (mddev->delta_disks >= 0) {
  6947. rdev_for_each(rdev, mddev)
  6948. if (rdev->raid_disk < 0 &&
  6949. !test_bit(Faulty, &rdev->flags)) {
  6950. if (raid5_add_disk(mddev, rdev) == 0) {
  6951. if (rdev->raid_disk
  6952. >= conf->previous_raid_disks)
  6953. set_bit(In_sync, &rdev->flags);
  6954. else
  6955. rdev->recovery_offset = 0;
  6956. if (sysfs_link_rdev(mddev, rdev))
  6957. /* Failure here is OK */;
  6958. }
  6959. } else if (rdev->raid_disk >= conf->previous_raid_disks
  6960. && !test_bit(Faulty, &rdev->flags)) {
  6961. /* This is a spare that was manually added */
  6962. set_bit(In_sync, &rdev->flags);
  6963. }
  6964. /* When a reshape changes the number of devices,
  6965. * ->degraded is measured against the larger of the
  6966. * pre and post number of devices.
  6967. */
  6968. spin_lock_irqsave(&conf->device_lock, flags);
  6969. mddev->degraded = raid5_calc_degraded(conf);
  6970. spin_unlock_irqrestore(&conf->device_lock, flags);
  6971. }
  6972. mddev->raid_disks = conf->raid_disks;
  6973. mddev->reshape_position = conf->reshape_progress;
  6974. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  6975. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  6976. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  6977. clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
  6978. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  6979. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  6980. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  6981. "reshape");
  6982. if (!mddev->sync_thread) {
  6983. mddev->recovery = 0;
  6984. spin_lock_irq(&conf->device_lock);
  6985. write_seqcount_begin(&conf->gen_lock);
  6986. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  6987. mddev->new_chunk_sectors =
  6988. conf->chunk_sectors = conf->prev_chunk_sectors;
  6989. mddev->new_layout = conf->algorithm = conf->prev_algo;
  6990. rdev_for_each(rdev, mddev)
  6991. rdev->new_data_offset = rdev->data_offset;
  6992. smp_wmb();
  6993. conf->generation --;
  6994. conf->reshape_progress = MaxSector;
  6995. mddev->reshape_position = MaxSector;
  6996. write_seqcount_end(&conf->gen_lock);
  6997. spin_unlock_irq(&conf->device_lock);
  6998. return -EAGAIN;
  6999. }
  7000. conf->reshape_checkpoint = jiffies;
  7001. md_wakeup_thread(mddev->sync_thread);
  7002. md_new_event(mddev);
  7003. return 0;
  7004. }
  7005. /* This is called from the reshape thread and should make any
  7006. * changes needed in 'conf'
  7007. */
  7008. static void end_reshape(struct r5conf *conf)
  7009. {
  7010. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  7011. struct md_rdev *rdev;
  7012. spin_lock_irq(&conf->device_lock);
  7013. conf->previous_raid_disks = conf->raid_disks;
  7014. rdev_for_each(rdev, conf->mddev)
  7015. rdev->data_offset = rdev->new_data_offset;
  7016. smp_wmb();
  7017. conf->reshape_progress = MaxSector;
  7018. conf->mddev->reshape_position = MaxSector;
  7019. spin_unlock_irq(&conf->device_lock);
  7020. wake_up(&conf->wait_for_overlap);
  7021. /* read-ahead size must cover two whole stripes, which is
  7022. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  7023. */
  7024. if (conf->mddev->queue) {
  7025. int data_disks = conf->raid_disks - conf->max_degraded;
  7026. int stripe = data_disks * ((conf->chunk_sectors << 9)
  7027. / PAGE_SIZE);
  7028. if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
  7029. conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
  7030. }
  7031. }
  7032. }
  7033. /* This is called from the raid5d thread with mddev_lock held.
  7034. * It makes config changes to the device.
  7035. */
  7036. static void raid5_finish_reshape(struct mddev *mddev)
  7037. {
  7038. struct r5conf *conf = mddev->private;
  7039. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  7040. if (mddev->delta_disks > 0) {
  7041. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  7042. if (mddev->queue) {
  7043. set_capacity(mddev->gendisk, mddev->array_sectors);
  7044. revalidate_disk(mddev->gendisk);
  7045. }
  7046. } else {
  7047. int d;
  7048. spin_lock_irq(&conf->device_lock);
  7049. mddev->degraded = raid5_calc_degraded(conf);
  7050. spin_unlock_irq(&conf->device_lock);
  7051. for (d = conf->raid_disks ;
  7052. d < conf->raid_disks - mddev->delta_disks;
  7053. d++) {
  7054. struct md_rdev *rdev = conf->disks[d].rdev;
  7055. if (rdev)
  7056. clear_bit(In_sync, &rdev->flags);
  7057. rdev = conf->disks[d].replacement;
  7058. if (rdev)
  7059. clear_bit(In_sync, &rdev->flags);
  7060. }
  7061. }
  7062. mddev->layout = conf->algorithm;
  7063. mddev->chunk_sectors = conf->chunk_sectors;
  7064. mddev->reshape_position = MaxSector;
  7065. mddev->delta_disks = 0;
  7066. mddev->reshape_backwards = 0;
  7067. }
  7068. }
  7069. static void raid5_quiesce(struct mddev *mddev, int state)
  7070. {
  7071. struct r5conf *conf = mddev->private;
  7072. switch(state) {
  7073. case 2: /* resume for a suspend */
  7074. wake_up(&conf->wait_for_overlap);
  7075. break;
  7076. case 1: /* stop all writes */
  7077. lock_all_device_hash_locks_irq(conf);
  7078. /* '2' tells resync/reshape to pause so that all
  7079. * active stripes can drain
  7080. */
  7081. r5c_flush_cache(conf, INT_MAX);
  7082. conf->quiesce = 2;
  7083. wait_event_cmd(conf->wait_for_quiescent,
  7084. atomic_read(&conf->active_stripes) == 0 &&
  7085. atomic_read(&conf->active_aligned_reads) == 0,
  7086. unlock_all_device_hash_locks_irq(conf),
  7087. lock_all_device_hash_locks_irq(conf));
  7088. conf->quiesce = 1;
  7089. unlock_all_device_hash_locks_irq(conf);
  7090. /* allow reshape to continue */
  7091. wake_up(&conf->wait_for_overlap);
  7092. break;
  7093. case 0: /* re-enable writes */
  7094. lock_all_device_hash_locks_irq(conf);
  7095. conf->quiesce = 0;
  7096. wake_up(&conf->wait_for_quiescent);
  7097. wake_up(&conf->wait_for_overlap);
  7098. unlock_all_device_hash_locks_irq(conf);
  7099. break;
  7100. }
  7101. r5l_quiesce(conf->log, state);
  7102. }
  7103. static void *raid45_takeover_raid0(struct mddev *mddev, int level)
  7104. {
  7105. struct r0conf *raid0_conf = mddev->private;
  7106. sector_t sectors;
  7107. /* for raid0 takeover only one zone is supported */
  7108. if (raid0_conf->nr_strip_zones > 1) {
  7109. pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
  7110. mdname(mddev));
  7111. return ERR_PTR(-EINVAL);
  7112. }
  7113. sectors = raid0_conf->strip_zone[0].zone_end;
  7114. sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
  7115. mddev->dev_sectors = sectors;
  7116. mddev->new_level = level;
  7117. mddev->new_layout = ALGORITHM_PARITY_N;
  7118. mddev->new_chunk_sectors = mddev->chunk_sectors;
  7119. mddev->raid_disks += 1;
  7120. mddev->delta_disks = 1;
  7121. /* make sure it will be not marked as dirty */
  7122. mddev->recovery_cp = MaxSector;
  7123. return setup_conf(mddev);
  7124. }
  7125. static void *raid5_takeover_raid1(struct mddev *mddev)
  7126. {
  7127. int chunksect;
  7128. void *ret;
  7129. if (mddev->raid_disks != 2 ||
  7130. mddev->degraded > 1)
  7131. return ERR_PTR(-EINVAL);
  7132. /* Should check if there are write-behind devices? */
  7133. chunksect = 64*2; /* 64K by default */
  7134. /* The array must be an exact multiple of chunksize */
  7135. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  7136. chunksect >>= 1;
  7137. if ((chunksect<<9) < STRIPE_SIZE)
  7138. /* array size does not allow a suitable chunk size */
  7139. return ERR_PTR(-EINVAL);
  7140. mddev->new_level = 5;
  7141. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  7142. mddev->new_chunk_sectors = chunksect;
  7143. ret = setup_conf(mddev);
  7144. if (!IS_ERR(ret))
  7145. mddev_clear_unsupported_flags(mddev,
  7146. UNSUPPORTED_MDDEV_FLAGS);
  7147. return ret;
  7148. }
  7149. static void *raid5_takeover_raid6(struct mddev *mddev)
  7150. {
  7151. int new_layout;
  7152. switch (mddev->layout) {
  7153. case ALGORITHM_LEFT_ASYMMETRIC_6:
  7154. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  7155. break;
  7156. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  7157. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  7158. break;
  7159. case ALGORITHM_LEFT_SYMMETRIC_6:
  7160. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  7161. break;
  7162. case ALGORITHM_RIGHT_SYMMETRIC_6:
  7163. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  7164. break;
  7165. case ALGORITHM_PARITY_0_6:
  7166. new_layout = ALGORITHM_PARITY_0;
  7167. break;
  7168. case ALGORITHM_PARITY_N:
  7169. new_layout = ALGORITHM_PARITY_N;
  7170. break;
  7171. default:
  7172. return ERR_PTR(-EINVAL);
  7173. }
  7174. mddev->new_level = 5;
  7175. mddev->new_layout = new_layout;
  7176. mddev->delta_disks = -1;
  7177. mddev->raid_disks -= 1;
  7178. return setup_conf(mddev);
  7179. }
  7180. static int raid5_check_reshape(struct mddev *mddev)
  7181. {
  7182. /* For a 2-drive array, the layout and chunk size can be changed
  7183. * immediately as not restriping is needed.
  7184. * For larger arrays we record the new value - after validation
  7185. * to be used by a reshape pass.
  7186. */
  7187. struct r5conf *conf = mddev->private;
  7188. int new_chunk = mddev->new_chunk_sectors;
  7189. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  7190. return -EINVAL;
  7191. if (new_chunk > 0) {
  7192. if (!is_power_of_2(new_chunk))
  7193. return -EINVAL;
  7194. if (new_chunk < (PAGE_SIZE>>9))
  7195. return -EINVAL;
  7196. if (mddev->array_sectors & (new_chunk-1))
  7197. /* not factor of array size */
  7198. return -EINVAL;
  7199. }
  7200. /* They look valid */
  7201. if (mddev->raid_disks == 2) {
  7202. /* can make the change immediately */
  7203. if (mddev->new_layout >= 0) {
  7204. conf->algorithm = mddev->new_layout;
  7205. mddev->layout = mddev->new_layout;
  7206. }
  7207. if (new_chunk > 0) {
  7208. conf->chunk_sectors = new_chunk ;
  7209. mddev->chunk_sectors = new_chunk;
  7210. }
  7211. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  7212. md_wakeup_thread(mddev->thread);
  7213. }
  7214. return check_reshape(mddev);
  7215. }
  7216. static int raid6_check_reshape(struct mddev *mddev)
  7217. {
  7218. int new_chunk = mddev->new_chunk_sectors;
  7219. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  7220. return -EINVAL;
  7221. if (new_chunk > 0) {
  7222. if (!is_power_of_2(new_chunk))
  7223. return -EINVAL;
  7224. if (new_chunk < (PAGE_SIZE >> 9))
  7225. return -EINVAL;
  7226. if (mddev->array_sectors & (new_chunk-1))
  7227. /* not factor of array size */
  7228. return -EINVAL;
  7229. }
  7230. /* They look valid */
  7231. return check_reshape(mddev);
  7232. }
  7233. static void *raid5_takeover(struct mddev *mddev)
  7234. {
  7235. /* raid5 can take over:
  7236. * raid0 - if there is only one strip zone - make it a raid4 layout
  7237. * raid1 - if there are two drives. We need to know the chunk size
  7238. * raid4 - trivial - just use a raid4 layout.
  7239. * raid6 - Providing it is a *_6 layout
  7240. */
  7241. if (mddev->level == 0)
  7242. return raid45_takeover_raid0(mddev, 5);
  7243. if (mddev->level == 1)
  7244. return raid5_takeover_raid1(mddev);
  7245. if (mddev->level == 4) {
  7246. mddev->new_layout = ALGORITHM_PARITY_N;
  7247. mddev->new_level = 5;
  7248. return setup_conf(mddev);
  7249. }
  7250. if (mddev->level == 6)
  7251. return raid5_takeover_raid6(mddev);
  7252. return ERR_PTR(-EINVAL);
  7253. }
  7254. static void *raid4_takeover(struct mddev *mddev)
  7255. {
  7256. /* raid4 can take over:
  7257. * raid0 - if there is only one strip zone
  7258. * raid5 - if layout is right
  7259. */
  7260. if (mddev->level == 0)
  7261. return raid45_takeover_raid0(mddev, 4);
  7262. if (mddev->level == 5 &&
  7263. mddev->layout == ALGORITHM_PARITY_N) {
  7264. mddev->new_layout = 0;
  7265. mddev->new_level = 4;
  7266. return setup_conf(mddev);
  7267. }
  7268. return ERR_PTR(-EINVAL);
  7269. }
  7270. static struct md_personality raid5_personality;
  7271. static void *raid6_takeover(struct mddev *mddev)
  7272. {
  7273. /* Currently can only take over a raid5. We map the
  7274. * personality to an equivalent raid6 personality
  7275. * with the Q block at the end.
  7276. */
  7277. int new_layout;
  7278. if (mddev->pers != &raid5_personality)
  7279. return ERR_PTR(-EINVAL);
  7280. if (mddev->degraded > 1)
  7281. return ERR_PTR(-EINVAL);
  7282. if (mddev->raid_disks > 253)
  7283. return ERR_PTR(-EINVAL);
  7284. if (mddev->raid_disks < 3)
  7285. return ERR_PTR(-EINVAL);
  7286. switch (mddev->layout) {
  7287. case ALGORITHM_LEFT_ASYMMETRIC:
  7288. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  7289. break;
  7290. case ALGORITHM_RIGHT_ASYMMETRIC:
  7291. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  7292. break;
  7293. case ALGORITHM_LEFT_SYMMETRIC:
  7294. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  7295. break;
  7296. case ALGORITHM_RIGHT_SYMMETRIC:
  7297. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  7298. break;
  7299. case ALGORITHM_PARITY_0:
  7300. new_layout = ALGORITHM_PARITY_0_6;
  7301. break;
  7302. case ALGORITHM_PARITY_N:
  7303. new_layout = ALGORITHM_PARITY_N;
  7304. break;
  7305. default:
  7306. return ERR_PTR(-EINVAL);
  7307. }
  7308. mddev->new_level = 6;
  7309. mddev->new_layout = new_layout;
  7310. mddev->delta_disks = 1;
  7311. mddev->raid_disks += 1;
  7312. return setup_conf(mddev);
  7313. }
  7314. static struct md_personality raid6_personality =
  7315. {
  7316. .name = "raid6",
  7317. .level = 6,
  7318. .owner = THIS_MODULE,
  7319. .make_request = raid5_make_request,
  7320. .run = raid5_run,
  7321. .free = raid5_free,
  7322. .status = raid5_status,
  7323. .error_handler = raid5_error,
  7324. .hot_add_disk = raid5_add_disk,
  7325. .hot_remove_disk= raid5_remove_disk,
  7326. .spare_active = raid5_spare_active,
  7327. .sync_request = raid5_sync_request,
  7328. .resize = raid5_resize,
  7329. .size = raid5_size,
  7330. .check_reshape = raid6_check_reshape,
  7331. .start_reshape = raid5_start_reshape,
  7332. .finish_reshape = raid5_finish_reshape,
  7333. .quiesce = raid5_quiesce,
  7334. .takeover = raid6_takeover,
  7335. .congested = raid5_congested,
  7336. };
  7337. static struct md_personality raid5_personality =
  7338. {
  7339. .name = "raid5",
  7340. .level = 5,
  7341. .owner = THIS_MODULE,
  7342. .make_request = raid5_make_request,
  7343. .run = raid5_run,
  7344. .free = raid5_free,
  7345. .status = raid5_status,
  7346. .error_handler = raid5_error,
  7347. .hot_add_disk = raid5_add_disk,
  7348. .hot_remove_disk= raid5_remove_disk,
  7349. .spare_active = raid5_spare_active,
  7350. .sync_request = raid5_sync_request,
  7351. .resize = raid5_resize,
  7352. .size = raid5_size,
  7353. .check_reshape = raid5_check_reshape,
  7354. .start_reshape = raid5_start_reshape,
  7355. .finish_reshape = raid5_finish_reshape,
  7356. .quiesce = raid5_quiesce,
  7357. .takeover = raid5_takeover,
  7358. .congested = raid5_congested,
  7359. };
  7360. static struct md_personality raid4_personality =
  7361. {
  7362. .name = "raid4",
  7363. .level = 4,
  7364. .owner = THIS_MODULE,
  7365. .make_request = raid5_make_request,
  7366. .run = raid5_run,
  7367. .free = raid5_free,
  7368. .status = raid5_status,
  7369. .error_handler = raid5_error,
  7370. .hot_add_disk = raid5_add_disk,
  7371. .hot_remove_disk= raid5_remove_disk,
  7372. .spare_active = raid5_spare_active,
  7373. .sync_request = raid5_sync_request,
  7374. .resize = raid5_resize,
  7375. .size = raid5_size,
  7376. .check_reshape = raid5_check_reshape,
  7377. .start_reshape = raid5_start_reshape,
  7378. .finish_reshape = raid5_finish_reshape,
  7379. .quiesce = raid5_quiesce,
  7380. .takeover = raid4_takeover,
  7381. .congested = raid5_congested,
  7382. };
  7383. static int __init raid5_init(void)
  7384. {
  7385. int ret;
  7386. raid5_wq = alloc_workqueue("raid5wq",
  7387. WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
  7388. if (!raid5_wq)
  7389. return -ENOMEM;
  7390. ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
  7391. "md/raid5:prepare",
  7392. raid456_cpu_up_prepare,
  7393. raid456_cpu_dead);
  7394. if (ret) {
  7395. destroy_workqueue(raid5_wq);
  7396. return ret;
  7397. }
  7398. register_md_personality(&raid6_personality);
  7399. register_md_personality(&raid5_personality);
  7400. register_md_personality(&raid4_personality);
  7401. return 0;
  7402. }
  7403. static void raid5_exit(void)
  7404. {
  7405. unregister_md_personality(&raid6_personality);
  7406. unregister_md_personality(&raid5_personality);
  7407. unregister_md_personality(&raid4_personality);
  7408. cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
  7409. destroy_workqueue(raid5_wq);
  7410. }
  7411. module_init(raid5_init);
  7412. module_exit(raid5_exit);
  7413. MODULE_LICENSE("GPL");
  7414. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  7415. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  7416. MODULE_ALIAS("md-raid5");
  7417. MODULE_ALIAS("md-raid4");
  7418. MODULE_ALIAS("md-level-5");
  7419. MODULE_ALIAS("md-level-4");
  7420. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  7421. MODULE_ALIAS("md-raid6");
  7422. MODULE_ALIAS("md-level-6");
  7423. /* This used to be two separate modules, they were: */
  7424. MODULE_ALIAS("raid5");
  7425. MODULE_ALIAS("raid6");