raid5.c 239 KB

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