raid5.c 226 KB

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