raid5.c 224 KB

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