raid5.c 218 KB

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