raid5.c 221 KB

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