raid5.c 254 KB

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